Script is 4X! bigger than it needs to be, please help


Hi, studying RhinoScript 101 by David Rutten, pg 58 ‘Geodesic Curve’

After studying the script closely I deleted about 100 lines of apparently unnecessary codes, and discovered the script still works perfectly!. How? Why would David write almost 100 lines more code than necessary? I deleted everything after line 32 and the script works just fine.

L31 Call Rhino.Print("Geodesic curve added with length: " & dblNewLength)
L32 End Sub

Could someone please explain. Copy of script below.


Option Explicit

Call GeodesicCurve()
Sub GeodesicCurve()
Dim strSurfaceID
strSurfaceID = Rhino.GetObject(“Select surface for geodesic curve solution”, 8, True, True)
If IsNull(strSurfaceID) Then Exit Sub

Dim arrV
arrV = GetR2PathOnSurface(strSurfaceID, 10, "Start of geodesic curve", "End of geodesic curve")
If IsNull(arrV) Then Exit Sub

Dim dblTolerance : dblTolerance = Rhino.UnitAbsoluteTolerance() / 10
Dim dblLength : dblLength = 1e300
Dim dblNewLength : dblNewLength = 0.0

	Call Rhino.Prompt("Solving geodesic fit for " & UBound(arrV) & " samples")
	Call GeodesicFit(arrV, strSurfaceID, dblTolerance)

	dblNewLength = PolylineLength(arrV)
	If (Abs(dblNewLength - dblLength) < dblTolerance) Then Exit Do

	If (UBound(arrV) > 1000) Then Exit Do

	arrV = SubDividePolyline(arrV)
	dblLength = dblNewLength

Call Rhino.AddPolyline(arrV)
Call Rhino.Print("Geodesic curve added with length: " & dblNewLength)

End Sub

Sub GeodesicFit(ByRef arrVertices, strSurfaceID, dblTolerance)
Dim dblLength
dblLength = PolylineLength(arrVertices)

Dim dblNewLength

	Call SmoothPolyline(arrVertices)
	Call ProjectPolyline(arrVertices, strSurfaceID)

	dblNewLength = PolylineLength(arrVertices)
	If (Abs(dblNewLength - dblLength) < dblTolerance) Then Exit Do

	dblLength = dblNewLength

End Sub

Sub SmoothPolyline(ByRef arrVertices)
Dim arrCopy : arrCopy = arrVertices
Dim i, j

For i = 1 To UBound(arrVertices) - 1
	For j = 0 To 2
		arrVertices(i)(j) = (arrCopy(i - 1)(j) + _
			arrCopy(i)(j) + _
			arrCopy(i + 1)(j)) / 3.0

End Sub

Sub ProjectPolyline(ByRef arrVertices, strSurfaceID)
Dim arrProjPt, i

For i = 1 To UBound(arrVertices) - 1
	arrProjPt = Rhino.BRepClosestPoint(strSurfaceID, arrVertices(i))
	If Not IsNull(arrProjPt) Then
		arrVertices(i) = arrProjPt(0)
	End If

End Sub

Function GetR2PathOnSurface(strSurfaceID, intSegments, strPrompt1, strPrompt2)
GetR2PathOnSurface = Null

Dim ptStart, ptEnd
ptStart = Rhino.GetPointOnSurface(strSurfaceID, strPrompt1)
If IsNull(ptStart) Then Exit Function

ptEnd = Rhino.GetPointOnSurface(strSurfaceID, strPrompt2)
If IsNull(ptEnd) Then Exit Function

If (Rhino.Distance(ptStart, ptEnd) = 0.0) Then Exit Function

Dim uvA : uvA = Rhino.SurfaceClosestPoint(strSurfaceID, ptStart)
Dim uvB : uvB = Rhino.SurfaceClosestPoint(strSurfaceID, ptEnd)
Dim arrV() : ReDim arrV(intSegments)

Dim i, t, u, v
For i = 0 To intSegments
	t = i / intSegments
	u = uvA(0) + t * (uvB(0) - uvA(0))
	v = uvA(1) + t * (uvB(1) - uvA(1))
	arrV(i) = Rhino.EvaluateSurface(strSurfaceID, Array(u, v))

GetR2PathOnSurface = arrV

End Function

Function SubDividePolyline(ByRef arrV)
Dim arrSubD()
ReDim arrSubD(2 * UBound(arrV))

Dim i
For i = 0 To UBound(arrV) - 1
	'copy the original vertex location
	arrSubD(i * 2) = arrV(i)
	'compute the average of the current vertex and the next one
	arrSubD(i * 2 + 1) = Array((arrV(i)(0) + arrV(i + 1)(0)) / 2.0, _
		(arrV(i)(1) + arrV(i + 1)(1)) / 2.0, _
		(arrV(i)(2) + arrV(i + 1)(2)) / 2.0)

'copy the last vertex (this is skipped by the loop)
arrSubD(UBound(arrSubD)) = arrV(UBound(arrV))

SubDividePolyline = arrSubD

End Function

Function PolylineLength(ByRef arrVertices)
PolylineLength = 0.0

Dim i
For i = 0 To UBound(arrVertices) - 1
	PolylineLength = PolylineLength + Rhino.Distance(arrVertices(i), arrVertices(i + 1))

End Function

(Dale Fugier) #2

If you close Rhino and restart, does your shortened script still run?


The shortened script does not work when I shutdown and restart Rhinocerous. but when I retest the original script then retest the shortened script the shortened script does work.

(Dale Fugier) #4

When you load a script, it resides in the system’s memory until you close Rhino. Thus, you might have shortened the script. But the missing piece was still in memory, thus the script continued to work.

Read through the introduction section of the RhinoScript help file. It’s pretty sort, but contains some good information.


Yeah, I would consider that a bug, not a ‘feature’. It should just run the script I tell it to.


It is indeed a useful feature and not a bug. For instance you could tell rhino to automatically load some files with functions you defined which are useful in your everyday scripting work. So to speak some sort of library. This is actually quite convenient and can save time usually needed to paste together code snippets from previous scripts. In ironpython there are much more sophisticated ways to deal with existing code, external libraries and so on. So you might what to consider to take a look into the python world. It is in the end a much more powerful scripting tool for rhino. I just shifted from vbs/ to python.


Thanks for the input.