├── .gitignore ├── LICENSE ├── Lessons ├── gh-csharp-adv01.cs ├── gh-csharp-adv02.cs ├── gh-csharp-int01.cs ├── gh-csharp01.cs ├── gh-csharp02.cs ├── gh-csharp03.cs ├── gh-csharp04.cs ├── gh-csharp05.cs ├── gh-csharp06.cs ├── gh-csharp07.cs ├── gh-csharp08.cs ├── gh-csharp09.cs └── gh-csharp10.cs └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | #vim swp files 2 | .swp -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Designalyze 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /Lessons/gh-csharp-adv01.cs: -------------------------------------------------------------------------------- 1 | //Advanced C# scripting in Grasshopper Lesson:01 2 | //http://www.designalyze.com/advanced-scripting-01-branching 3 | 4 | //Set the branch angle by converting it from degs to radians 5 | double bAngRad = ((Math.PI / 180) * branchAngle); 6 | List lines = new List(); 7 | //Create a new line in the yDirection from the origin and add it to the list 8 | Point3d stPt = new Point3d(0, 0, 0); 9 | Vector3d unitYvect = new Vector3d(0, 1, 0); 10 | Line ln0 = new Line(stPt, unitYvect, length); 11 | lines.Add(ln0); 12 | //Create two temporary lists and set the first equal to the lines list 13 | List tempList = new List(); 14 | List tempList2 = new List(); 15 | tempList = lines; 16 | 17 | int i = 0; 18 | while(i < num){ 19 | tempList2 = CreateBranch(tempList, branchScale, bAngRad); 20 | tempList = tempList2; 21 | lines.AddRange(tempList2); 22 | i++; 23 | } 24 | A = lines; 25 | 26 | 27 | // 28 | 29 | 30 | public List CreateBranch(List lines, double bScale, double bAng) 31 | { 32 | List newLines = new List(); 33 | foreach(Line ln in lines){ 34 | //Get the length of the current trunk and create a new scaled branch 35 | double newLength = ln.Length * bScale; 36 | //Get the endPt of the trunk and it's tangent vector 37 | Point3d endPt = ln.To; 38 | Vector3d unitTan = ln.UnitTangent; 39 | //Create two new lines and make the second line have a negative rotation angle 40 | Line ln1 = new Line(endPt, unitTan, newLength); 41 | ln1.Transform(Rhino.Geometry.Transform.Rotation(bAng, endPt)); 42 | Line ln2 = new Line(endPt, unitTan, newLength); 43 | ln2.Transform(Rhino.Geometry.Transform.Rotation(bAng * -1, endPt)); 44 | //Add these new branches to the list 45 | newLines.Add(ln1); 46 | newLines.Add(ln2); 47 | } 48 | return newLines; 49 | } 50 | -------------------------------------------------------------------------------- /Lessons/gh-csharp-adv02.cs: -------------------------------------------------------------------------------- 1 | //Advanced C# scripting in Grasshopper Lesson:02 2 | //http://www.designalyze.com/advanced-scripting-02-kochsnowflake 3 | 4 | //Create an empty list of lines to hold our snowflake curves 5 | List lines = new List(); 6 | //Call the CreateTri function and pass the radius and flip parameters 7 | Polyline tri = CreateTri(radius, flip); 8 | //Create an array of Lines by extracting the segements from the triangle 9 | Line[] triangle = tri.GetSegments(); 10 | //Add these three lines to the lines List 11 | lines.Add(triangle[0]); 12 | lines.Add(triangle[1]); 13 | lines.Add(triangle[2]); 14 | //Create a temporary list of lines 15 | List tempList = new List(); 16 | //Begin the while loop 17 | int i = 0; 18 | while(i < num){ 19 | //make sure the list is empty by calling the Clear() method 20 | tempList.Clear(); 21 | tempList.AddRange(lines); 22 | lines.Clear(); 23 | lines.AddRange(CreateKochSnowflake(tempList)); 24 | i++; 25 | } 26 | A = lines; 27 | 28 | public List CreateKochSnowflake(List lines){ 29 | List newLines = new List(); 30 | foreach( Line ln in lines){ 31 | //Get the length of the line and 4 points to define new snowflake edge 32 | double tempLength = ln.Length; 33 | Point3d ptStarttemp = ln.PointAt(0.0); 34 | Point3d pt1temp = ln.PointAt(0.333); 35 | Point3d pt2temp = ln.PointAt(0.666); 36 | Point3d ptEndtemp = ln.PointAt(1.0); 37 | //Create a new line and rotate it by 60 degs or PI/3 38 | Line rotatedLine = new Line(pt1temp, pt2temp); 39 | rotatedLine.Transform(Rhino.Geometry.Transform.Rotation(Math.PI / 3, pt1temp)); 40 | //Create a new point from the end of the rotatedLine and complete the lines 41 | Point3d triTop = rotatedLine.PointAt(1.0); 42 | Line completeTriLine = new Line(triTop, pt2temp); 43 | Line lnStart = new Line(ptStarttemp, pt1temp); 44 | Line lnEnd = new Line(pt2temp, ptEndtemp); 45 | //Add all of the lines to the newLines list in order 46 | newLines.Add(lnStart); 47 | newLines.Add(rotatedLine); 48 | newLines.Add(completeTriLine); 49 | newLines.Add(lnEnd); 50 | } 51 | return newLines; 52 | } 53 | 54 | public Polyline CreateTri(double radius, bool reverseTri) 55 | { 56 | List pts = new List(); 57 | 58 | //Angles for triangle points 59 | const double ang0 = (Math.PI / 2); 60 | const double ang1 = (7 * Math.PI / 6); 61 | const double ang2 = (11 * Math.PI / 6); 62 | //Create the three points of the triangle 63 | Point3d pt0 = new Point3d(radius * Math.Cos(ang0), radius * Math.Sin(ang0), 0); 64 | Point3d pt1 = new Point3d(radius * Math.Cos(ang1), radius * Math.Sin(ang1), 0); 65 | Point3d pt2 = new Point3d(radius * Math.Cos(ang2), radius * Math.Sin(ang2), 0); 66 | //Add the points to the pts list 67 | pts.Add(pt0); 68 | pts.Add(pt1); 69 | pts.Add(pt2); 70 | //Create a new polyline called pl 71 | Polyline pl = new Polyline(); 72 | //loop through the list of points(should only be three but will use any number b/c we aren't checking BAD!) 73 | foreach(Point3d pt in pts){ 74 | pl.Add(pt); 75 | } 76 | //Add a copy of the first point to the end of the list to close the polyline 77 | pl.Add(pts[0]); 78 | //Check to see if the polyline should be reversed and if true...call the reverse method 79 | if(reverseTri == true){ 80 | pl.Reverse(); 81 | } 82 | return pl; 83 | } 84 | -------------------------------------------------------------------------------- /Lessons/gh-csharp-int01.cs: -------------------------------------------------------------------------------- 1 | //Intermediate C# scripting in Grasshopper Lesson:01 2 | //http://www.designalyze.com/intermediate-scripting-01-inch-precision 3 | 4 | int wholeNumber = (int) x; 5 | 6 | double inchPart = Math.Round((x-wholeNumber) * inchPrecision); 7 | if (inchPart == inchPrecision) { 8 | inchPart = 0; 9 | wholeNumber = wholeNumber + 1; 10 | } 11 | //The following line is not necessary I just used it as a check 12 | B = wholeNumber + (inchPart / inchPrecision); 13 | 14 | string answer = String.Format("{0} {1}/{2}", wholeNumber, inchPart, inchPrecision); 15 | 16 | A = answer; 17 | -------------------------------------------------------------------------------- /Lessons/gh-csharp01.cs: -------------------------------------------------------------------------------- 1 | //Intro to C# scripting in Grasshopper Lesson:01 2 | //http://designalyze.com/GH_Scripting_Intro_Part_01 3 | 4 | Point3d pt = new Point3d(x,y,z); 5 | A = pt; 6 | -------------------------------------------------------------------------------- /Lessons/gh-csharp02.cs: -------------------------------------------------------------------------------- 1 | //Intro to C# scripting in Grasshopper Lesson:02 2 | //http://designalyze.com/GH_Scripting_Intro_Part_02 3 | 4 | Line ln = new Line(x,y); 5 | A = ln; 6 | -------------------------------------------------------------------------------- /Lessons/gh-csharp03.cs: -------------------------------------------------------------------------------- 1 | //Intro to C# scripting in Grasshopper Lesson:03 2 | //http://designalyze.com/GH_Scripting_Intro_Part_03 3 | 4 | //Define a new List called numbers of type int 5 | List numbers = new List(); 6 | 7 | // for (initialization-clause; condition-clause; iteration-clause) { statement-block; } 8 | for (int i = 0; i < 10; i++) { 9 | numbers.Add(i); 10 | } 11 | 12 | A = numbers; 13 | -------------------------------------------------------------------------------- /Lessons/gh-csharp04.cs: -------------------------------------------------------------------------------- 1 | //Intro to C# scripting in Grasshopper Lesson:04 2 | //http://designalyze.com/GH_Scripting_Intro_Part_04 3 | 4 | List pts = new List(); 5 | for (int i = 0; i < 10; i++) { 6 | Point3d tempPt = new Point3d(i, 0, 0); 7 | pts.Add(tempPt); 8 | } 9 | A = pts; 10 | -------------------------------------------------------------------------------- /Lessons/gh-csharp05.cs: -------------------------------------------------------------------------------- 1 | //Intro to C# scripting in Grasshopper Lesson:05 2 | //http://designalyze.com/GH_Scripting_Intro_Part_05 3 | 4 | List xpos = new List(); 5 | foreach(Point3d pt in pts) { 6 | xpos.Add(pt.X); 7 | } 8 | A = xpos; 9 | 10 | 11 | /////////////////////////////////////////////////// 12 | 13 | List circles = new List(); 14 | foreach(Point3d pt in pts) { 15 | Circle tempCircle = new Circle(pt, 1.0); 16 | circles.Add(tempCircle); 17 | } 18 | A = circles; 19 | -------------------------------------------------------------------------------- /Lessons/gh-csharp06.cs: -------------------------------------------------------------------------------- 1 | //Intro to C# scripting in Grasshopper Lesson:06 2 | //http://www.designalyze.com/GH_Scripting_Intro_Part_06 3 | 4 | 5 | List numbers = new List(); 6 | 7 | int i = 0; 8 | while (i < count) { 9 | numbers.Add((i*step) + start); 10 | i++; 11 | } 12 | A = numbers; 13 | -------------------------------------------------------------------------------- /Lessons/gh-csharp07.cs: -------------------------------------------------------------------------------- 1 | //Intro to C# scripting in Grasshopper Lesson:07 2 | //http://www.designalyze.com/GH_Scripting_Intro_Part_07 3 | 4 | List pts = new List(); 5 | 6 | if (x == null) { 7 | x = new Point3d(0,0,0); 8 | } 9 | 10 | double spacingX = xSize / numDivsX; 11 | double spacingY = ySize / numDivsY; 12 | 13 | Point3d tempPt = x; 14 | 15 | for (int i = 0; i < numDivsX; i++) { 16 | tempPt.X = x.X + (spacingX * i); 17 | 18 | for (int j = 0; j < numDivsY; j++) { 19 | tempPt.Y = x.Y + (spacingY * j); 20 | pts.Add(tempPt); 21 | } 22 | } 23 | 24 | A = pts; 25 | -------------------------------------------------------------------------------- /Lessons/gh-csharp08.cs: -------------------------------------------------------------------------------- 1 | //Intro to C# scripting in Grasshopper Lesson:08 2 | //http://www.designalyze.com/GH_Scripting_Intro_Part_08 3 | 4 | if (x == true) { 5 | A = "It's True!"; 6 | } else { 7 | A = "It's False"; 8 | } 9 | 10 | /////////////////////////////////////////////////// 11 | 12 | List pts = new List(); 13 | 14 | for (int i = 0; i < 100; i++) { 15 | if (i%3 == 0 || i %5 == 0) { 16 | Point3d ptTemp = new Point3d(i, 0, 0); 17 | pts.Add(ptTemp); 18 | } 19 | } 20 | A = pts; 21 | -------------------------------------------------------------------------------- /Lessons/gh-csharp09.cs: -------------------------------------------------------------------------------- 1 | //Intro to C# scripting in Grasshopper Lesson:09 2 | //http://www.designalyze.com/GH_Scripting_Intro_Part_09 3 | 4 | Point3d pt = new Point3d(); 5 | pt = x.PointAtNormalizedLength(t); 6 | 7 | A = pt; 8 | 9 | /////////////////////////////////////////////////// 10 | 11 | List pts = new List(); 12 | Point3d pt = new Point3d(); 13 | 14 | double spacing = 1.0 / numDivs; 15 | double t; 16 | 17 | for (int i = 0; i <= numDivs; i++) { 18 | t = i * spacing; 19 | pt = x.PointAtNormalizedLength(t); 20 | pts.Add(pt); 21 | } 22 | 23 | A = pts; 24 | -------------------------------------------------------------------------------- /Lessons/gh-csharp10.cs: -------------------------------------------------------------------------------- 1 | //Intro to C# scripting in Grasshopper Lesson:10 2 | //http://www.designalyze.com/GH_Scripting_Intro_Part_10 3 | 4 | //Example 1 5 | //Create an empty list of lines 6 | List lines = new List(); 7 | //Initialize a new instance of the Random class called random 8 | Random random = new Random(); 9 | //start a loop 10 | for(int i = start; i <= end; i = i+2){ 11 | //Create a new integer called randint from the Next method of the random class 12 | int randint = random.Next(0, 100); 13 | //Create a line from 6 input values in the form of (x1, y1, z1, x2, y2, z2) 14 | Line ln = new Line(i, 10, 0, randint, 50, 0); 15 | //Add our new line to the list of lines 16 | lines.Add(ln); 17 | } 18 | A = lines; 19 | 20 | /////////////////////////////////////////////////// 21 | 22 | //Example 2 23 | //Create empty points, planes, and rectangle lists 24 | List pts = new List(); 25 | List plns = new List(); 26 | List rects = new List(); 27 | 28 | //Initialize a new instance of the Random class called random 29 | Random random = new Random(); 30 | //Create a new unitZ vector 31 | Vector3d unitZ = new Vector3d(0,0,1); 32 | 33 | //Start a loop 34 | for(int i = start; i <= end; i = i + 2) { 35 | //Create random values 36 | int randint = random.Next(0,100); 37 | int randint2 = random.Next(0,10); 38 | int randint3 = random.Next(0,5); 39 | 40 | //Create new points, planes, and rectangles based on these random values 41 | Point3d pt = new Point3d(i, randint, randint2); 42 | Plane pln = new Plane(pt, unitZ); 43 | Rectangle3d rect = new Rectangle3d(pln, randint3, randint3); 44 | 45 | //Add all of these to the appropriate lists (Actually, since we are only using the rects you don't need the others FYI) 46 | pts.Add(pt); 47 | plns.Add(pln); 48 | rects.Add(rect); 49 | } 50 | 51 | A = rects; 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | csharp-tutorials-for-gh 2 | ======================= 3 | 4 | ##C# scripting tutorials for Grasshopper 5 | 6 | * [Intro to C# scripting in Grasshopper Lesson:01](http://designalyze.com/GH_Scripting_Intro_Part_01) 7 | * [Intro to C# scripting in Grasshopper Lesson:02](http://designalyze.com/GH_Scripting_Intro_Part_02) 8 | * [Intro to C# scripting in Grasshopper Lesson:03](http://designalyze.com/GH_Scripting_Intro_Part_03) 9 | * [Intro to C# scripting in Grasshopper Lesson:04](http://designalyze.com/GH_Scripting_Intro_Part_04) 10 | * [Intro to C# scripting in Grasshopper Lesson:05](http://designalyze.com/GH_Scripting_Intro_Part_05) 11 | * [Intro to C# scripting in Grasshopper Lesson:06](http://designalyze.com/GH_Scripting_Intro_Part_06) 12 | * [Intro to C# scripting in Grasshopper Lesson:07](http://designalyze.com/GH_Scripting_Intro_Part_07) 13 | * [Intro to C# scripting in Grasshopper Lesson:08](http://designalyze.com/GH_Scripting_Intro_Part_08) 14 | * [Intro to C# scripting in Grasshopper Lesson:09](http://designalyze.com/GH_Scripting_Intro_Part_09) 15 | * [Intro to C# scripting in Grasshopper Lesson:10](http://designalyze.com/GH_Scripting_Intro_Part_10) 16 | * [Intermediate C# scripting in Grasshopper Lesson:01](http://www.designalyze.com/intermediate-scripting-01-inch-precision) 17 | * [Advanced C# scripting in Grasshopper Lesson:01](http://www.designalyze.com/advanced-scripting-01-branching) 18 | * [Advanced C# scripting in Grasshopper Lesson:02](http://www.designalyze.com/advanced-scripting-02-kochsnowflake) 19 | --------------------------------------------------------------------------------