Grasshopper

algorithmic modeling for Rhino

Hello guys!!!

I am new at GH and even newer at scripting! A friend of mine created this code and I would like to learn how to create the component(s) - button(s) out of it

Option Explicit

Call Main()
Sub Main()

' user input

Dim intHowManyTrees : intHowManyTrees = Rhino.GetInteger("how many trees is a forest",20,1,50)
Dim intHowManyGenerations : intHowManyGenerations = Rhino.GetInteger("how many generations is a tree?",6,1,10)
Dim dblForestLength : dblForestLength = Rhino.GetReal("was is the desired length of your forest",50)
Dim dblForestWidth : dblForestWidth = Rhino.GetReal("was is the desired width of your forest",50)
Dim dblInitLength : dblInitLength = Rhino.GetReal("was is the desired length of the first main Branch?",10)
Dim ang : ang = Rhino.GetReal("angle of rotation", 30, 1, 180)
Dim scale : scale = Rhino.GetReal("scale of branch", 0.9)
'loop for number of generations i
'ReDim arrgenerations (Ubound (intHowManyGenerations))
Dim layer_0 : layer_0 = Rhino.AddLayer("0")
Dim i
For i=0 To intHowManyTrees
Dim arrSeed : arrSeed = Array(Rnd*dblForestLength, Rnd*dblForestWidth,0)
Dim strLine : strLine = Rhino.AddLine(Array (arrSeed(0), arrSeed (1), arrSeed (2)+Rnd*3), Array(arrSeed(0), arrSeed(1),arrSeed(2)+Rnd*dblInitLength+3))

Call Rhino.ObjectLayer (strLine, "0" )
Call Rhino.ObjectName (strLine, i)
Next
Dim j,vec
ReDim arrbranch (intHowManyGenerations)
For i=1 To intHowManyGenerations
'select the elements
arrbranch (i) = Rhino.ObjectsByLayer (i-1)
Dim strBranch
For j=0 To Ubound(arrBranch(i))
strBranch = arrBranch(i)(j)
Dim strParentName : strParentName = Rhino.ObjectName(strBranch)
' get start and end points
Dim arrStartPt : arrStartPt = Rhino.CurveStartPoint(strBranch)
Dim arrEndPt : arrEndPt = Rhino.CurveEndPoint(strBranch)

' get a vector between start and end
vec = Rhino.VectorCreate(arrEndPt, arrStartPt) ''''' vec(x,y,z)

' scale vector
vec = Rhino.VectorScale(vec, scale)

' rotate vectors
Dim arrPlane : arrPlane = Rhino.CurvePerpFrame (strBranch, Rhino.CurveDomain(strBranch)(1))

Dim arrRotAxis
Dim layer
layer = Rhino.AddLayer ( i )

arrRotAxis = arrPlane(Int(Rnd*3))
Dim vec1 : vec1 = Rhino.VectorRotate(vec, ang, arrRotAxis)
' add the vector to the end point
Dim newEndPt : newEndPt = Rhino.VectorAdd(arrEndPt, vec1)

Dim line1: line1 = Rhino.AddLine(arrEndPt, newEndPt)
Call Rhino.ObjectLayer (line1, i)
Call Rhino.ObjectName(line1, strParentName)

If rnd<0.8 Then
arrRotAxis = arrPlane(Int(Rnd*3))
Dim vec2 : vec2 = Rhino.VectorRotate(vec, -ang, arrRotAxis)
Dim newEndPt2 : newEndPt2 = Rhino.VectorAdd(arrEndPt, vec2)

Dim line2: line2 = Rhino.AddLine(arrEndPt, newEndPt2)
Call Rhino.ObjectLayer (line2, i)
Call Rhino.ObjectName(line2, strParentName)
End If

If rnd<0.2 Then
arrRotAxis = arrPlane(Int(Rnd*3))
Dim vec3 : vec3 = Rhino.VectorRotate(vec, ang*(rnd-0.5)*2, arrRotAxis)
Dim newEndPt3 : newEndPt3 = Rhino.VectorAdd(arrEndPt, vec3)

Dim line3: line3 = Rhino.AddLine(arrEndPt, newEndPt3)
Call Rhino.ObjectLayer (line3, i)
Call Rhino.ObjectName(line3, strParentName)
End If
Next
Next
End Sub

Any kind of help would be highly appreciated

Views: 337

Replies to This Discussion

anyone????

Hi,I also just learn the script, I learn is python, you can see the tutorials 101tutorial,

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service