1. clausneergaard (Jul 14, 2010 23.51):
hi,
I'm wondering why this happens .. I'm relatively new to rhino scripting, so I'm a bit confused as to why I get a unexpected 'Next' compiling error, when running this code?
thanks in advance!
Option Explicit
' script written by Claus, july 2010
Call Main()
Sub Main()
Dim i, count
Dim dblDist
Dim strExplodeSrfs, strPts
Dim arrSrfCentroidPts, arrSrfPts, arrSrfPlane, arrPts
' user input
Dim strBox : strBox = Rhino.GetObject("select box", 16)
If IsNull(strBox) Then Exit Sub
Dim intPts : intPts = Rhino.GetInteger("how many points?", 100, 20, 1000)
If IsNull(intPts) Then Exit Sub
Dim arrBBox : arrBBox = Rhino.BoundingBox(strBox)
If IsNull(arrBbox) Then Exit Sub
' get distances, so random pts can be created within specified boundary
Dim dblDistX : dblDistX = Rhino.Distance(arrBBox(0),arrBBox(1))
Dim dblDistY : dblDistY = Rhino.Distance(arrBBox(0),arrBBox(3))
Dim dblDistZ : dblDistZ = Rhino.Distance(arrBBox(0),arrBBox(4))
' resize arrays
count = 0
ReDim arrSrfCentroidPts(count)
ReDim arrSrfPlane(count)
ReDim strPts(intPts)
ReDim dblDist(intPts)
ReDim arrPts(intPts)
Rhino.EnableRedraw(False)
' create random pts
For i = 0 To intPts Step 1
strPts(i) = Rhino.AddPoint(Array(Rnd*dblDistX+arrBBox(0)(0),Rnd*dblDistY+arrBBox(0)(1),Rnd*dblDistZ+arrBBox(0)(2)))
Next
' explode box and get closest surface to each pt
strExplodeSrfs = Rhino.ExplodePolysurfaces(strBox, False)
' this loop creates planes for each vertical surface and adds text dot with number
For i = 0 To UBound(strExplodeSrfs) Step 1
' resize array
ReDim Preserve arrSrfCentroidPts(count)
ReDim Preserve arrSrfPlane(count)
' get four corner pts from each srf
arrSrfPts = Rhino.SurfacePoints(strExplodeSrfs(i))
' create plane based on three of the srf corner pts
arrSrfPlane(count) = Rhino.PlaneFromPoints(arrSrfPts(0), arrSrfPts(1), arrSrfPts(2))
' get centroid from each srf
arrSrfCentroidPts(count) = Rhino.SurfaceAreaCentroid(strExplodeSrfs(i))
' add text dot to each srf
Call Rhino.AddTextDot("srf" & i, arrSrfCentroidPts(i)(0))
' redim counter
count = count+1
Next
' distance to planes (uses planes from above loop)
For i = 0 To UBound(strPts)
' get point coordinates
arrPts(i) = Rhino.PointCoordinates(strPts(i))
' get distances to vertical planes from pts
dblDist(i)(0) = Rhino.DistanceToPlane(arrSrfPlane(0), arrPts(i))
dblDist(i)(1) = Rhino.DistanceToPlane(arrSrfPlane(1), arrPts(i))
dblDist(i)(2) = Rhino.DistanceToPlane(arrSrfPlane(2), arrPts(i))
dblDist(i)(3) = Rhino.DistanceToPlane(arrSrfPlane(3), arrPts(i))
If IsPoint(arrPts(i)) Then
' if statement deciding which plane to interact with each pt
If dblDist(i)(0) > dblDist(i)(1) And dblDist(i)(0) > dblDist(i)(2) And dblDist(i)(0) > dblDist(i)(3) Then
' plane0
Rhino.AddLine arrPts(i), arrSrfCentroidPts(0)
Else If dblDist(i)(1) > dblDist(i)(0) And dblDist(i)(1) > dblDist(i)(2) And dblDist(i)(1) > dblDist(i)(3) Then
' plane1
Rhino.AddLine arrPts(i), arrSrfCentroidPts(1)
Else If dblDist(i)(2) > dblDist(i)(0) And dblDist(i)(2) > dblDist(i)(1) And dblDist(i)(2) > dblDist(i)(3) Then
' plane2
Rhino.AddLine arrPts(i), arrSrfCentroidPts(2)
Else If dblDist(i)(3) > dblDist(i)(0) And dblDist(i)(3) > dblDist(i)(1) And dblDist(i)(3) > dblDist(i)(2) Then
' plane3
Rhino.AddLine arrPts(i), arrSrfCentroidPts(3)
End If
' add sphere
Rhino.AddSphere arrPts(i), dblDist(i)/2
End If
Next
Rhino.EnableRedraw(True)
End Sub