From scripting
Jump to: navigation, search

Direct: http://crtl-i.com/blog/?p=115

Option Explicit
‘by Andrew Kudless | andrew@materialsystems.org | april, 2005
‘Edited by Howard Jiho Kim | kimjiho@gmail.com | March, 2008

Sub OrientObjTwoSurfaces()

Dim orientObj
Dim orientPoint(2)

Dim sourceSurf, sourceSurf2
Dim uDiv, vDiv, maxDiv
Dim uArray, vArray
Dim i,j, index

’surface 1 variables 
Dim uMax, vMax
Dim uInc, vInc
Dim uStart, vStart

’surface 2 variables
Dim u2Max, v2Max
Dim u2Inc, v2Inc
Dim u2Start, v2Start

’surface points variables
Dim arrParam(1), arr2Param(1), arrPoint, arr2Point
Dim surface1Points(), surface2Points()

‘Rhino.EnableRedraw vbFalse

orientObj = Rhino.GetObject (”Select the Object to Orient”)
If IsNull(orientObj) Then Exit Sub

orientPoint(0) = Rhino.GetPoint(”Select Point 1 to Orient”)
If IsNull(orientPoint(0)) Then Exit Sub
orientPoint(1) = Rhino.GetPoint(”Select Point 2 to Orient”)
If IsNull(orientPoint(1)) Then Exit Sub
orientPoint(2) = Rhino.GetPoint(”Select Point 3 to Orient”)
If IsNull(orientPoint(2)) Then Exit Sub
uDiv = Rhino.GetInteger (”Enter the number of divisions in the U direction”)
If IsNull(uDiv) Then Exit Sub

vDiv = Rhino.GetInteger (”Enter the number of divisions in the V direction”)
If IsNull(vDiv) Then Exit Sub

‘Get inputs
sourceSurf = Rhino.GetObject (”Select the base Surface”, 8)
If IsNull(sourceSurf) Then Exit Sub

‘Get inputs
sourceSurf2 = Rhino.GetObject (”Select a target Surface”, 8)
If IsNull(sourceSurf2) Then Exit Sub

ReDim uVal(uDiv)
ReDim vVal(vDiv)
ReDim u2Val(uDiv)
ReDim v2Val(vDiv)

uMax = Rhino.SurfaceDomain (sourceSurf, 0)
vMax = Rhino.SurfaceDomain (sourceSurf, 1)

uInc = uMax(1)/uDiv
vInc = vMax(1)/vDiv

For i = 0 To uDiv
uVal(i) = i * uInc
Rhino.Print “uVal(” & i & “):” & uVal(i)


For i = 0 To vDiv
vVal(i) = i * vInc
Rhino.Print “vVal(” & i & “):” & vVal(i)

u2Max = Rhino.SurfaceDomain (sourceSurf2, 0)
v2Max = Rhino.SurfaceDomain (sourceSurf2, 1)

u2Inc = u2Max(1)/uDiv
v2Inc = v2Max(1)/vDiv

For i = 0 To uDiv 
u2Val(i) = i * u2Inc
Rhino.Print “u2Val(” & i & “):” & u2Val(i)

For i = 0 To vDiv
v2Val(i) = i * v2Inc
Rhino.Print “v2Val(” & i & “):” & v2Val(i)

Rhino.Print “Calculating Array…”

‘Find which direction has more points
If uDiv >= vDiv Then
maxDiv = uDiv
maxDiv = vDiv
End If

Dim targetPoint(2) 
Dim p1, arrVector, NewLine, NewLine2, line1, line2, line3, arrXform
Dim arrPlane, arrRotated

For i=0 To uDiv

For j=0 To vDiv
arrParam(0) = uVal(i)
arrParam(1) = vVal(j)
arrPoint = Rhino.EvaluateSurface(sourceSurf, arrParam)
line1 = Rhino.AddPoint(arrPoint)
targetPoint(0) = Rhino.PointCoordinates(line1)

arr2Param(0) = uVal(i)
arr2Param(1) = vVal(j)
arr2Point = Rhino.EvaluateSurface(sourceSurf2, arr2Param) 
line2 = Rhino.AddPoint(arr2Point)
targetPoint(2) = Rhino.PointCoordinates(line2)

NewLine = Rhino.AddLine (arrPoint, arr2Point)

arrVector = Rhino.VectorCreate(arr2Point, arrPoint)
Rhino.ViewCPlane , Rhino.PlaneFromNormal(arrPoint, arrVector)

arrPlane = ViewCPlane
arrRotated = RotatePlane(arrPlane, 90, arrPlane(1))
Rhino.ViewCPlane , arrRotated

arrPlane = ViewCPlane

NewLine2 = Rhino.RotateObject(NewLine, arrPlane(0), 90.0, ,True)

arrPoint = Rhino.CurveEndPoint(NewLine2)
line3 = Rhino.AddPoint(arrPoint)
targetPoint(1) = Rhino.PointCoordinates(line3)

If IsArray(orientPoint) Then
If IsArray(targetPoint) Then
Rhino.OrientObject orientObj, orientPoint, targetPoint, 1
End If
End If

Rhino.DeleteObject (line1)
Rhino.DeleteObject (line2)
Rhino.DeleteObject (line3)
‘Rhino.DeleteObject (NewLine)
Rhino.DeleteObject (NewLine2)

‘Rhino.EnableRedraw vbTrue
End Sub

Rhino.Print “Array Complete”