12 May 2009

Gradient color Rhinoscript.










Inspired by Paramod, last month i adapted one rhinoscript found in this blog in order to realize a script that draw cones at u,v of surface and change their color controlled by the Z value of the cones. In other case a gradient color, vector lines and a complete numeration of the normals at surface are plotted as you can see at these images.

//////////////

Option Explicit
'             Script written by PARAMOD
              'adapted by Davide del Giudice
'             http://madeincalifornia.blogspot.com/
'Script version giovedì 15 aprile 2009

Call GradientColorAddNormalVector()
Sub GradientColorAddNormalVector()

Const rhObjectPoint = 1

Dim strObject, arrCoordPoint
Dim i, j, n: n=0
DIM o: o=0
Dim p: p=0
Dim strSrf
Dim Udomain, Vdomain
Dim arrPt, strTxt
Dim Ustep, Vstep
Dim intDivider
Dim u, v, zspread, zz, zround, zcolor
Dim vectNormal, arrPointNormal, strCoords, arrCoords
Dim strLine
Dim arrPlane
Dim strCircle
Dim arrCones
Dim strMax, maxZ, minZ
Dim strtextdot
'pick surface
strSrf = Rhino.GetObject ("pick surface", 8)
'get surface domain
Udomain = Rhino.SurfaceDomain (strSrf, 0)
Vdomain = Rhino.SurfaceDomain (strSrf, 1)
Call rhino.Print(UDomain(0) & "," & UDomain(1) )
Call rhino.Print(VDomain(0) & "," & VDomain(1))
'get domain point
Dim Umean, Vmean
Umean = UDomain(0)+ (UDomain (1) - Udomain (0)) / 2
Umean = Round (Umean,2)
Vmean = VDomain(0)+ (VDomain (1) - Vdomain (0)) / 2
Vmean = Round (Vmean,2)
Call rhino.Print(Umean & "," & Vmean)
'step
intDivider = rhino.Getinteger ("get number of intDivider",1,10,50)
Ustep = (Udomain(1) - Udomain (0)) / IntDivider
Vstep = (Vdomain(1) - Vdomain (0)) / IntDivider
'Call rhino.EnableRedraw (False)
'loop
For i = 0 To intDivider
For j = 0 To IntDivider
u = Udomain (0)+ Ustep*i
v = Vdomain(0)+ Vstep*j
arrPt = rhino.EvaluateSurface (strSrf, array (u,v))
Call rhino.addpoint(arrPt)
strCoords = rhino.addpoint(arrPt)
arrCoords = Rhino.PointCoordinates (strCoords)
'strtextdot= Rhino.AddTextDot (n+1, arrPt)
ReDim Preserve Z(n) 
Z(n) = arrCoords(2)
zz = arrCoords(2)
'Call Rhino.AddTextDot (zz, arrCoords)
If n > 0 Then
End If
n = n+1
 
Next
Next
maxZ = Rhino.Max(z)
Call Rhino.Print (maxZ & "maximum")
minZ = Rhino.Min(z)
Call Rhino.Print (minZ & "minimum")
zspread = maxZ - minZ
Call Rhino.Print (Zspread & "spread")
'loop
For i = 0 To intDivider
For j = 0 To IntDivider
u = Udomain (0)+ Ustep*i
v = Vdomain(0)+ Vstep*j
arrPt = rhino.EvaluateSurface (strSrf, array (u,v))
Call rhino.addpoint(arrPt)
strCoords = rhino.addpoint(arrPt)
arrCoords = Rhino.PointCoordinates (strCoords)
strObject = arrCoords
'arrCoordPoint = Rhino.PointCoordinates(strObject)
'Call Rhino.Print Rhino.Pt2Str(arrCoordPoint, 3)
'Call Rhino.AddTextDot (arrCoords(2),arrCoords)
vectNormal  = Rhino.SurfaceNormal (strSrf, array (u,v))
vectNormal = rhino.VectorScale (vectNormal, 2)
arrPointNormal = Rhino.PointAdd (arrPt, vectNormal)
zz = arrCoords(2)
strLine = rhino.AddLine (arrPt, arrPointNormal)
Call Rhino.CurveArrows (strLine ,2)
arrPlane = Rhino.PlaneFromNormal (arrPt, vectNormal)
'Call Rhino.AddPlaneSurface (arrPlane, 2, 2)
'strCircle =  rhino.AddCircle (arrPlane, 10/intDivider*(i))
'Call Rhino.AddPlanarSrf ( array (strCircle) )
'arrCones = Rhino.AddCone (arrPlane, 2,1, True)
zcolor = ((zz - minZ)*(255/(maxZ-minZ)))
zround = 50 * (Rhino.Floor(zcolor/50))
'Call rhino.ObjectColor (arrcones, rgb (200,zround,150) )
Call rhino.ObjectColor (strline, rgb (200,zround,150) )
strtextdot= Rhino.AddTextDot (zround, arrPointNormal)
Call rhino.ObjectColor (strtextdot, rgb (200,zround,150) )
Next
Next
' Call rhino.EnableRedraw (True)
End Sub

//////////////

2 commenti:

Leo said...

Davide:
First and foremost, your work is INCREDIBLE. I think the closest word to describe it is inspirational. I am a current student at the AA London in Landscape Urbanism where we are playing with grasshopper and parametric tools. I found this script quite interesting and I was wondering if you could answer a question for me. One I run the script on a surface, Rhino calculates the normals and colors but does not proceed in making the cones. Do you know what could be wrong with it?
I would appreciate your help. Really, your website and work are amazing. Keep up the good work.
Leonardo

DAVIDE DEL GIUDICE said...

Hi Leo, thx for this comment.
Please write me your mail and i'll send you the script with array of cones.

Regards,

D