Loft multiple curves

I said difficult, but not impossible…

First: as Rhino.EnableHistoryRecording() is implemented in vb Rhinoscript, below is a vb Rhinoscript that will turn History on (if it’s not already on) and then return it to its previous state when done. There is a small bug currently in Rhino.EnableHistoryRecording() but I was able to work around it.

Then there is a REAL kludge Python version which works around the non-implementation of rs.EnableHistoryRecording() by first executing the command History and then parsing the command line result. (I told you it was a kludge, just want to see if I could make it work). This will only work on English-speaking Rhinos, however… FWIW, just for fun…

–Mitch

Option Explicit
'Script written by Mitch Heynick
'Version January 14, 2014

Call LoftCurvePairsWSeamHistory2()
Sub LoftCurvePairsWSeamHistory2()	
	Dim msg,commOpts,crvs,prevHState
	msg = "Pick a pair of curves to loft, or press Enter to exit"
	commOpts = " _Type=_Normal _Simplify=_None _Closed=_No _Enter"
	
	'check history state, turn on
	prevHState = Rhino.EnableHistoryRecording(True)
	'the following line is a bug workaround
	If IsEmpty(prevHState) Then prevHState = False
	'turn on history
	'Call Rhino.EnableHistoryRecording(True)
	
	Do
		Call Rhino.UnselectAllObjects()
		crvs = Rhino.GetObjects(msg, 4,, True,,, 2, 2)
		If Not IsArray(crvs) Then Exit Do
		
		If Rhino.IsCurveClosed(crvs(0)) And Rhino.IsCurveClosed(crvs(1)) Then
			Call Rhino.SelectObjects(crvs)
			Call Rhino.Command("_CrvSeam")
			Call Rhino.Command("-Loft _Enter" + commOpts, False)
		ElseIf Not Rhino.IsCurveClosed(crvs(0)) And Not Rhino.IsCurveClosed(crvs(1)) Then
			Call Rhino.SelectObjects(crvs)
			Call Rhino.Command("-Loft" + commOpts, False)
		Else
			Call Rhino.Print("Cannot loft an open curve with a closed curve")
		End If		
	Loop
	
	'return history to previous state
	Call Rhino.EnableHistoryRecording(prevHState)
End Sub

Python version

import rhinoscriptsyntax as rs
import Rhino

def CheckHistoryState():
    rs.Command("_History _Enter")
    #parse the command line result
    ch=Rhino.RhinoApp.CommandHistoryWindowText
    lastline=ch.split(chr(10))[-2]
    state=lastline.split(" ")[4]
    ans=state.split("=")[1]
    if ans=="Yes": return True

def LoftCurvePairsWSeamHistory2():
    msg="Pick a pair of curves to loft, or press Enter to exit"
    commOpts=" _Type=_Normal _Simplify=_None _Closed=_No _Enter"
    setHistFlag=True
    histState=CheckHistoryState()
    if not histState: setHistFlag=False
    while True:
        rs.UnselectAllObjects()
        crvs=rs.GetObjects(msg,4,preselect=True,minimum_count=2,maximum_count=2)
        if not crvs: break        
        #turn on history
        if not setHistFlag:
            rs.Command("_History _Record=_Yes _Enter",False)
        if rs.IsCurveClosed(crvs[0]) and rs.IsCurveClosed(crvs[1]):
            rs.SelectObjects(crvs)
            rs.Command("_CrvSeam")
            rs.Command("-Loft _Enter"+commOpts,False)
        elif not rs.IsCurveClosed(crvs[0]) and not rs.IsCurveClosed(crvs[1]):
            rs.SelectObjects(crvs)
            rs.Command("-Loft"+commOpts,False)
        else:
            print "Cannot loft an open curve with a closed curve"
            continue
    if not setHistFlag:
            #turn history back off
            rs.Command("_History _Record=_No _Enter",False)
        
LoftCurvePairsWSeamHistory2()
2 Likes