A random bit of code I wrote at University, which at some point got
picked up by some Chinese blogs, and has been floating around the
internets ever since. This is probably the first real "program" I
ever wrote.
---
Sub Cutsurface
Dim strCutpath, strAngle, strBound, arrCenterpt
Dim arrStartpts, arrEndpts, arrTemp, strTemp, arrTemp2, strStartPt, strEndPt
Dim strStartLineID, strEndLineID
Dim strDetail
Dim strStartpts, strEndpts
Dim strTheta, PI, arrMatrix, arrOrigin, strOrigin
Dim arrEdgesrfs
strOrigin = "0,0,0"
arrOrigin = Rhino.Str2Pt(strOrigin)
PI = 3.141592653589793
strCutpath = Rhino.GetObject("Curve of cut path (This should be in the
XZ Plane)")
If IsNull(strCutpath) Then
Exit Sub
End If
strAngle = Rhino.GetString("Total angle of rotation during cut")
If IsNull(strAngle) Then
Exit Sub
End If
strDetail = 100
'strDetail = Rhino.GetString("# of points to make along edge (detail)")
'If IsNull(strDetail) Then
'Exit Sub
'End If
strBound = Rhino.GetDistance(,,"Distance to extend surface from path (radius)")
If IsNull(strBound) Then
Exit Sub
End If
arrCenterpt = Rhino.Getpoint("Point to rotate surface around (center of table)")
If IsNull(arrCenterpt) Then
Exit Sub
End If
Rhino.AddPoint arrCenterpt
strStartpts = ""
strEndpts = ""
'everything should be gotten at this point
'to do: check that data is correct? make sure cut is in XZ plane!!
Rhino.EnableRedraw False
'divide the curve up, make lines, and rotate each 1 degree.
arrDividePts = Rhino.DivideCurve(strCutpath, strDetail,False) ' Make
one point for each angle
'Rhino.Print(strAngle)
ReDim arrStartPts(strDetail)
ReDim arrEndPts(strDetail)
For i=0 To strDetail
'Rhino.Print(i) 'Debugging stuff
'Rhino.Print(Rhino.Pt2Str(arrDividePts(i))) 'Debug
'OK, you have to convert the point to a string, split that into
'an array, center it on 0,0,0, rotate it, move it back (Is there a
better way to rotate about a pt?)
strTemp = Rhino.Pt2Str(arrDividePts(i))
arrTemp = Split(strTemp, ",",-1)
arrTemp(0) = arrTemp(0) - arrCenterpt(0)
arrTemp(1) = arrTemp(1) + strBound - arrCenterpt(1)
arrTemp(2) = arrTemp(2) - arrCenterpt(2) 'OK, the start
point is ready to be made
'and is centered on 0,0,0
strStartpt = Rhino.AddPoint(arrTemp)
'strTemp = Join(arrTemp, ",") 'This is how
you join the string back together.
arrTemp(1) = arrTemp(1) - (2 * strBound) 'then make the end
pt, still using arrTemp
strEndpt = Rhino.Addpoint(arrTemp)
strTheta = strAngle - (i * (strAngle / strDetail))
'Begin code to rotate points around Z
strTheta = strTheta * PI / 180.0
ReDim arrMatrix(3,3)
arrMatrix(0,0) = Cos(strTheta)
arrMatrix(0,1) = -Sin(strTheta)
arrMatrix(0,2) = 0
arrMatrix(0,3) = 0
arrMatrix(1,0) = Sin(strTheta)
arrMatrix(1,1) = Cos(strTheta)
arrMatrix(1,2) = 0.0
arrMatrix(1,3) = 0.0
arrMatrix(2,0) = 0.0
arrMatrix(2,1) = 0.0
arrMatrix(2,2) = 1.0
arrMatrix(2,3) = 0.0
arrMatrix(3,0) = 0.0
arrMatrix(3,1) = 0.0
arrMatrix(3,2) = 0.0
arrMatrix(3,3) = 1.0
If Not IsNull(strStartpt) Then
Rhino.TransformObject strStartpt, arrMatrix
Rhino.TransformObject strEndpt, arrMatrix
End If 'End of rotating script
Rhino.MoveObject strStartpt, arrOrigin, arrCenterpt
Rhino.MoveObject strEndpt, arrOrigin, arrCenterpt
strTemp = Rhino.Pt2Str( Rhino.PointCoordinates(strStartpt) )
strStartpts = strStartpts & " " & strTemp 'OK, store
all the points as a giant string.
strTemp = Rhino.Pt2Str( Rhino.PointCoordinates(strEndpt) )
strEndpts = strEndpts & " " & strTemp
If i = 0 Then
Rhino.Addpoint arrDividePts(0) 'Add so you know
which way the line is drawn
End If
Rhino.DeleteObject(strStartpt)
Rhino.DeleteObject(strEndpt)
Next
'Rhino.Print "Startpts:"
'Rhino.Print strStartpts
'Rhino.Print "Endpts:"
'Rhino.Print strEndpts
arrStartpts = Rhino.Str2PtArray(strStartpts)
arrEndpts = Rhino.Str2PtArray(strEndpts)
ReDim arrEdgesrfs(3)
arrEdgesrfs(0) = Rhino.AddCurve(arrStartpts)
arrEdgesrfs(1) = Rhino.AddCurve(arrEndpts)
arrEdgesrfs(2) = Rhino.AddLine( Rhino.CurveStartpoint(arrEdgesrfs(0)),
Rhino.CurveStartpoint(arrEdgesrfs(1)) )
arrEdgesrfs(3) = Rhino.Addline( Rhino.CurveEndpoint(arrEdgesrfs(0)),
Rhino.CurveEndpoint(arrEdgesrfs(1)) )
Rhino.AddEdgesrf arrEdgesrfs
For i = 0 To 3
Rhino.DeleteObject(arrEdgesrfs(i))
Next
Rhino.EnableRedraw True
End Sub
'2005 Japhy Bartlett (
japhy@nolimyn.com)