07 September 2011

rh_Object scale, shade color and material from an attractor point

I wrote these scripts for testing the use of distance value from a group of object to one attractor to define the scale of objects themselves, color shade and material color. It's just for fun and for exercise with VBrhinoscript.

ownload attractor.rvb

Option Explicit
'this script plays with an attractor point
'Script written by davide del giudice
'Script copyrighted by co-de-it
MsgBox "Code by Davide del Giudice www.Co-de-iT.com"
Call Main()
Sub Main()

Dim arrpts, arrpt,arrNormal
Dim i,u
Dim dbdist,strline1,arrPlane,circle
'select a grid of points
arrpts = Rhino.GetPointCoordinates ("Select grid of points",1)
'select an attractor
arrpt= Rhino.GetPointCoordinates ("Select attractor")
If IsNull (arrpts) Then
Exit Sub
If IsNull (point) Then
Exit Sub
End If
End If
'loop for the grid of points
For i= 0 To Ubound (arrpts)
'loop for the attractor
For u = 0 To UBound (arrpt)
'calculates the distance between every points to the attractor
dbdist = Rhino.Distance (arrpts(i), arrpt(u))
'draws one line from i-point and the attractor
strline1=Rhino.AddLine (arrpts(i), arrpt(0))
'defines the shade color of the line
Rhino.ObjectColor strline1, rgb (dbdist*6,dbdist*15,100)
'calculates a vector from i-point and the attractor
arrNormal = Rhino.VectorCreate (arrpts (i), arrpt(0))
arrPlane=Rhino.PlaneFromNormal(arrpts(i), arrNormal)
circle= rhino.AddCircle (arrPlane, dbdist/10)
Dim planarsrf
planarsrf= Rhino.AddPlanarSrf (array(circle))
Rhino.ObjectColor planarsrf, rgb (dbdist*6,dbdist*15,100)
'defines the material of the planar surface
Dim material
material = rhino.AddMaterialToObject ( planarsrf(0))
Call rhino.MaterialColor (material, rgb (dbdist*6,dbdist*15,100))
Call rhino.MaterialShine (material, 255)

End Sub

Option Explicit
MsgBox "Code by Davide del Giudice www.Co-de-iT.com"
Call ArrayPointsOnSurface()
Sub ArrayPointsOnSurface()

Dim strObject, nRows, nColumns,strpt,oript,arrplane,arrplane2, strLoftSRF
Dim U, V, i, j, arrParam(1), arrPoint,endpt,pts,arrClosest,arrNormal,Line,d2,pt,plane,circle,circle2

' Get the surface object
strObject = Rhino.GetObject("Select surface", 8)
oript= rhino.GetObject("pickpoint",1)
strpt= rhino.pointcoordinates(oript)

If IsNull(strObject) Then Exit Sub

' Get the number of rows
nRows = Rhino.GetInteger("Number of rows", 20, 2)
If IsNull(nRows) Then Exit Sub

nRows = nRows - 1
' Get the number of columns
nColumns = Rhino.GetInteger("Number of columns", 20, 2)

If IsNull(nColumns) Then Exit Sub

nColumns = nColumns - 1

' Get the domain of the surface
U = Rhino.SurfaceDomain(strObject, 0)
V = Rhino.SurfaceDomain(strObject, 1)
If Not IsArray(U) Or Not IsArray(V) Then Exit Sub

' Add the points
For i = 0 To nRows
arrParam(0) = U(0) + (((U(1) - U(0)) / nRows) * i)
For j = 0 To nColumns
arrParam(1) = V(0) + (((V(1) - V(0)) / nColumns) * j)
arrPoint = Rhino.EvaluateSurface(strObject, arrParam)

If IsArray(arrPoint) Then
arrClosest = Rhino.SurfaceClosestPoint(strObject, arrPoint)
arrNormal = Rhino.SurfaceNormal(strObject, arrParam)

Rhino.Print Rhino.Pt2Str(arrNormal)
Line= rhino.addline (arrpoint,endpt)
d2=rhino.Distance (arrpoint,strpt)
arrPlane= Rhino.PlaneFromNormal (arrPoint, arrNormal)
arrPlane2= Rhino.PlaneFromNormal (endpt, arrNormal)
circle=Rhino.AddCircle (arrPlane, d2/50)
circle2=Rhino.AddCircle (arrPlane2, d2/100)
Dim material
strLoftSRF=Rhino.AddLoftSrf (array(circle,circle2))
material = rhino.AddMaterialToObject (strLoftSRF(0))
Call rhino.MaterialColor (material, rgb (d2*3,d2*5,150))
Call rhino.MaterialShine (material, 255)
Call rhino.ObjectColor (Line, rgb (d2*3,d2*5,150) )
Call rhino.ObjectColor (circle, rgb (d2*3,d2*5,150) )
Call rhino.ObjectColor (circle2, rgb (d2*3,d2*5,150) )
Call rhino.ObjectColor (strLoftSRF, rgb (d2*3,d2*5,150) )

End If
rhino.HideObject strobject
End Sub

0 commenti: