Documentation
Documentation
NET
Framework for Windows applications
C# - F# - VB.NET - J# - P# ...
Programming Languages to write Windows applications.
ASSEMBLY
DLL OR EXE
namespace
namespace
class class
class
APPLICATION
ASSEMBLY
ASSEMBLY
ASSEMBLY
ASSEMBLY
ASSEMBLY
ASSEMBLY ASSEMBLY
ASSEMBLY
ARCHITECTURE .NET
class height
weight
data = age
methods origin
walk()
write()
read()
SYNTAX
COMMENTS
C# TYPE SYNTAX
double Double 8 …
decimal Decimal 16 -7.9 x 10 to 7.9 x 1028
28
C# TYPE SYNTAX
SIGN OPERATORS +-
ARITHMETIC +-*/%
LOGICAL & | ^ ! ~ && || true false
STRING CONCATENATION +
INCREMENT, DECREMENT ++ --
SHIFT << >>
RELATIONAL == != < > <= >=
ASSIGNMENT = += -= *= /= %= &= |= ^= <<= >>=
MEMBER ACCESS .
INDEXING []
CAST ()
TERNARY ?:
DELEGATE CONCATENATION AND REMOVAL +-
OBJECT CREATION new
TYPE INFORMATION as is sizeof typeof
OVERFLOW EXCEPTION CONTROL checked unchecked
INDIRECTION AND ADDRESS * -> [ ] &
LAMBDA =>
CONDITIONALS
C# TYPE SYNTAX
switch (minute)
{
case (60):
…
break;
case (30):
…
break;
case (10):
case (20):
…
break;
default:
…
break;
}
ITERATIONS
LOOP TYPE SYNTAX
DO-WHILE do
{
…
i++;
} while (i < 10);
LOOP STATEMENTS
STATEMENT SYNTAX
CONTINUE continue;
JUMPS TO THE NEXT ITERATION
ARRAYS
ARRAYS SYNTAX
LIST
var numbers = new List <int> ();
Access/assign one element var numbers = new List <int> {1,2,3,4};
Copy List var numbers = new List <int> (anotherList);
FORMATING
All lower//upper case letters ToLower() / ToUpper()
Removes white spaces Trim()
SEARCHING
letter IndexOf(‘a’)
last index LastIndexOf(“Hello”)
SUBSTRINGS
gets string from letter index Substring(startIndex)
Substring(startIndex, length)
REPLACING
replace letter Replace(‘a’, ‘!’)
replace string Replace(“Point3d”, “Point3f”)
NULL CHECKING
is empty or whitespace String.IsNullOrEmpty(str) / String.IsNullOrWhiteSpace(str)
SPLITTING
split by whitespaces str.Split(‘ ‘)
CONVERT
parse string to int int i = int.Parse(s);
convert string to int int j = Convert.ToInt32(s);
convert numbers to strings int i = 1234;
string s = i.ToString();
FILE
C# TYPE SYNTAX
FILE File.Copy(@"C:\Users\pves\Desktop\Folder1\Hello.txt",
@"C:\Users\pves\Desktop\Folder2\Hello.txt",true);
static methods
File.Delete(@"C:\Users\pves\Desktop\Folder2\Hello.txt");
used if you want to execute
small number of operations var path = @"C:\Users\pves\Desktop\Folder1\Hello.txt";
File.Exists(path)
if (fileInfo.Exists)
DIRECTORY
C# TYPE SYNTAX
DIRECTORY
Directory.CreateDirectory(@"C:\Users\pves\Desktop\Folder1\temp");
static methods
var files = Directory.GetFiles(@"C:\Users\pves\Desktop\Folder1",
used if you want to execute "*.txt", SearchOption.AllDirectories);
small number of operations
var directories = Directory.GetDirectories(@"C:\Users\pves\Desktop",
"*.*", SearchOption.AllDirectories);
Directory.Exists("...");
DIRECTORY INFO
var directoryInfo = new DirectoryInfo("...");
instance methods
where you have to create directoryInfo.GetFiles();
new object each command
directoryInfo.GetDirectories();
FUNCTIONS
C# TYPE SYNTAX
CREATE FUNCTION
CALLING FUNCTIONS
Console.WriteLine(a);
OUT int a = 20;
SomeFunction(out a);
But in out you need
initialize variables public void AddNumbers(out int d) {
and your passed d = 0;
variable is not d = d + 2;
taken inside the }
function as ref, just
modified. Console.WriteLine(a);
The user can change names, types and number of inputs and outputs.
C# COMPONENT INPUTS
Base types
Imports
External .dll’s that you might use in your code. Most
of them are DotNET system imports, but there is
also RhinoCommon and Grasshopper assemblies.
Utility functions
Print text and component error / warnings to 'out'
parameters.
Members
Rhino, Grasshopper documents, component
properties and iteration count.
RunScript
This is the main function that the user writes the
code within.
override
BoundingBox
You need to get 1 bounding box of all objects you
display for non-flickering display.
override DrawViewportMeshes
Display objects such as Lines and Curves
override
DrawViewportWires
Display objects such as Meshes, Surfaces, Breps
Print Rhino.RhinoApp.WriteLine(x.ToString());
Command-line Rhino.RhinoApp.Write (x.ToString());
Print Print(nakedPtsB.GetType().ToString());
Component Out Print("Number of vertices");
Parameter
ITEM / LIST / TREE
DESCRIPTION C# SYNTAX
A = x;
}
Type hint: private void RunScript(DataTree<double> x, double y, ref object A)
Tree Access {
(loop through all for(int i = 0 ; i < x.BranchCount; i++)
branches, loop for(int j = 0; j < x.Branch(i).Count; j++)
through specific x.Branch(i)[j] += y;
branch items)
A = x;
}
TIMER
DESCRIPTION C# SYNTAX
A = n;
A = circles;
A = pl;
VECTOR MATH
DESCRIPTION C# SYNTAX
//Vector subtraction
Vector3d vSubtraction = v0 - v1;
//Scale vector
Vector3d vScaled = 0.1 * v0;
//Change weight
int index = 3;
nc.Points.SetPoint(index, CPoints[index].X, CPoints[index].Y, CPoints[index].Z,
weight);
//Divide curve
Point3d[] points;
nc.DivideByCount(divisions, true, out points);
//Point on curve
Point3d pt = new Point3d();
pt = crv.PointAtNormalizedLength(t);
//Change domain
crv.Domain = new Interval(0, 1);
SURFACE
DESCRIPTION C# SYNTAX
//Add Vertices
mesh.Vertices.AddVertices(points);
//Create faces
mesh.Faces.AddFace(new MeshFace(0, 1, 4, 5));
mesh.Faces.AddFace(new MeshFace(1, 2, 3, 4));
mesh.Faces.AddFace(new MeshFace(5, 4, 7, 6));
mesh.Faces.AddFace(new MeshFace(7, 4, 3, 8));
//Clean mesh
mesh.Vertices.CombineIdentical(true, true);
mesh.Vertices.CullUnused();
mesh.Weld(3.14159265358979);
mesh.UnifyNormals();
mesh.FaceNormals.ComputeFaceNormals();
mesh.Normals.ComputeNormals();
POINTCLOUD
DESCRIPTION C# SYNTAX
_pointCloud = pointCloud;
//Find center
A = amp.Centroid;
//Find center
B = vmp.Centroid;
TRANSFORM
DESCRIPTION C# SYNTAX
Line Line newLine = line; //Line is a struct so copy lines is simple like this
newLine.Transform(Transform.Scale(newLine.PointAt(0.5), factor));
A = points;
A = points;
NOISE / RANDOM
DESCRIPTION C# SYNTAX
Function private void RunScript(int generations, double height, Polyline tri, ref object A) {
List < Polyline > a = subdivide(tri, height);
without List < Polyline > b = new List<Polyline>();
recursion. for(int i = 0; i < generations; i++){
b.Clear();
for(int j = 0; j < a.Count; j++)
This is a b.AddRange(subdivide(a[j], height));
subdivision a = new List<Polyline>(b);
example. }
A = a; //Output
}
Random private void RunScript(List<Line> lineList, double radius, int item, ref object A, ref object B)
{
lineList2 = lineList;
cl.Clear();
ci.Clear();
create rtree RTree tree = new RTree();
https://marketplace.visualstudio.com/items?itemName=McNeel.GrasshopperAssemblyforv5
…AND INSTALL IT (YOU MUST HAVE VISUAL STUDIO)
CREATE NEW PROJECT
CREATE NEW PROJECT
Add 4 references:
1. System.Drawing
2. Grasshopper
3. GH_IO
4. RhinoCommon
SET LOCAL COPY TO FALSE
2. Rhino.exe directory
namespace Components
{
public class AssemblyInfo : GH_AssemblyInfo
{
public override string Name => "ElephantGun";
public override Bitmap Icon => null;
public override string Description => "";
public override Guid Id => new Guid("35bc357c-5d7d-424f-a91d-a9fe9203cfc8");
public override string AuthorName => "Petras";
public override string AuthorContact => "";
}
}
Right click on your project name in solution explorer and create class.
This class is needed for overall assembly not for one component. General Information.
FINALLY WRITE COMPONENT
using System;
using System.Collections.Generic;
using Grasshopper.Kernel;
using System.Drawing;
namespace ComponentTest
{
public class MyFirstComponent : GH_Component
{
//Properties
public override Guid ComponentGuid { get { return new Guid("7e5cbf56-5cf0-4d2b-ada0-4103564afee4"); } }
protected override Bitmap Icon { get { return null; } }
//Constructor
public MyFirstComponent() : base ("MyFirstComponent", "MFC", "Testing my first component", "Temp", "TempSubCategory") { }
//Methods
protected override void RegisterInputParams(GH_InputParamManager pManager) {
pManager.AddNumberParameter("Start", "S", "Start number", GH_ParamAccess.item, 0);
pManager.AddNumberParameter("Step", "N", "Step number", GH_ParamAccess.item, 0.5);
pManager.AddIntegerParameter("Count", "C", "How many elements", GH_ParamAccess.item, 10);
}
protected override void RegisterOutputParams(GH_OutputParamManager pManager) {
pManager.AddNumberParameter("Series", "S", "List of numbers", GH_ParamAccess.list);
}
protected override void SolveInstance(IGH_DataAccess DA)
{
//Get all numbers
double s = 0.0;
double n = 0.5;
int c = 10;
DA.GetData(0, ref s);
DA.GetData(1, ref n);
DA.GetData(2, ref c);
//Do something
List<double> numbers = new List<double>();
for (double i = s; i < n*c; i+=n)
numbers.Add(i);
//Output
DA.SetDataList(0, numbers);
}
}
}
BUILD IT -> RUN IT
RHINO IS LOADING NOW…