0% found this document useful (0 votes)
24 views70 pages

Documentation

Uploaded by

houhuamilan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views70 pages

Documentation

Uploaded by

houhuamilan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

.

NET
Framework for Windows applications

CLR Class Library


Before C# language there was C/C++ languages. Compiler The .NET Framework class library is a library of classes,
compile code written in Native Language for the machine interfaces, and value types that provide access to system
to understand. In this way for instance, applications for functionality. It is the foundation on which .NET Framework
Windows may not run on Linux. applications, components, and controls are built.

But in C# code is not translated to Native Code. The idea is System


borrowed from Java, when code is translated to Byte Code, System.Drawing
but in C# it is called IL Code (Intermediate Language). System.Linq
System.Threading
It is independent on computer on which it is running. System.Runtime
Now we need something that translates the code the code System.Windows
to Native Code and that is the job of CLR (common System.Net
language runtime). This process is called Just-In-Time Microsoft.CSharp
Compilation (JIT). …
.NET LANGUAGES

C# - F# - VB.NET - J# - P# ...
Programming Languages to write Windows applications.
ASSEMBLY
DLL OR EXE
namespace
namespace
class class

class class class


class class
namespace
class
class
class class

class
APPLICATION

ASSEMBLY
ASSEMBLY
ASSEMBLY

ASSEMBLY
ASSEMBLY

ASSEMBLY ASSEMBLY
ASSEMBLY
ARCHITECTURE .NET

class class class


class
class class class
class
class class
class class
class
class class class
class
class
class
class class class
CLASS
Bob

class height
weight
data = age
methods origin
walk()
write()
read()
SYNTAX
COMMENTS
C# TYPE SYNTAX

SINGLE LINE COMMENT // text

Comment many lines


Ctrl + K + C
Uncomment many lines
Ctrl + K + U

MUTLI LINE COMMENT /*


text
*/

XML COMMENT /// text


PRIMITIVE TYPES

C# TYPE .NET TYPE BYTES RANGE

INTEGRAL NUMBERS byte Byte 1 0 to 255


short Int16 2 -32.768 to 32.767
int Int32 4 -2.1B to 2.1B
long Int64 8 …
REAL NUMBERS float Single 4 -3.4 x 10 to 3.4 x 1036
36

double Double 8 …
decimal Decimal 16 -7.9 x 10 to 7.9 x 1028
28

CHARACTER char Char 2 Unicode Characters


BOOLEAN bool Boolean 1 true/false
NON-PRIMITIVE TYPES

C# TYPE SYNTAX

VALUE TYPES USER - DEFINED enum


struct
REFERENCED TYPES - PREDEFINED string
object
REFERENCED TYPES - USER DEFINED class
Array
delegate
interface
OPERATORS
C# TYPE SYMBOL

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

IF/ELSE int hour = 10;

if(hour > 0 && hour < 12)



else if(hour >= 12 && hour < 18)

else

CONDITIONAL OPERATOR bool isGoldCustomer = true;

double price = (isGoldCustomer) ? 19.95 : 29.95;


SWITCH/CASE int minute = 10;

switch (minute)
{

case (60):

break;

case (30):

break;

case (10):
case (20):

break;

default:

break;

}
ITERATIONS
LOOP TYPE SYNTAX

FOR for (var i = 0; i < 10; i++)


iterating number of times {

}

FOREACH foreach (var numbers in numbers)


iterating over a list of {
objects, but they cannot be …
changed }

WHILE while (i < 10)


when you do not know how {
many iterations you will have …
i++;
}

DO-WHILE do
{

i++;
} while (i < 10);
LOOP STATEMENTS

STATEMENT SYNTAX

BREAK LOOP break;


JUMPS OUT OF THE LOOP

CONTINUE continue;
JUMPS TO THE NEXT ITERATION
ARRAYS
ARRAYS SYNTAX

SINGLE DIMENSION ARRAY var numbers = new int[5];


var numbers = new int[5] { 1,2,3,4,5 };

Access/assign one element var element = numbers[0];

MULTIDIMENSION ARRAYS var matrix = new int[3, 5];


RECTANGULAR (MATRICES) var matrix = new int[3, 5]
{
{ 1, 2, 3, 4, 5 },
{ 6, 7, 8, 9, 10 },
{ 11, 12, 13, 14, 15 } };

Access/assign one element var element = matrix[0,0];

MULTIDIMENSION ARRAYS var array = new int[3][];


JAGGED
array[0] = new int[4];
array[1] = new int[5];
array[2] = new int[3];

Access/assign one element array[0][0] = 1;


C# ARRAY METHODS SYNTAX

ARRAY LENGTH numbers.Length();

GET MAXIMUM / MINIMUM ELEMENT int a = numbers.Max();


int b = numbers.Max();

ADD TWO ARRAYS TOGETHER var array1 = new int[] {1,2,3};


var array2 = new int[] {4,5,6};
var zip = array1.Zip(array2, (a,b) => (a+b) );

GET INDEX OF ELEMENT Array.IndexOf(numbers, 3);

SET PORTION OF ELEMENTS TO Array.Clear(numbers, 0, 2);


ZERO, FALSE OR NULL

COPY PORTION OF ARRAY TO Array.Copy(numbers, another, 3);


ANOTHER ARRAY

SORT ELEMENTS IN ARRAY Array.Sort(numbers);

REVERSE ELEMENTS IN ARRAY Array.Reverse(numbers);


LISTS
C# TYPE 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);

C# LIST METHODS SYNTAX

ADD ELEMENT numbers.Add(1);

ADD MORE THAN ONE ELEMENT numbers.AddRange(new List <int> {4,5,6,7} );

DELETE FIRST MATCHING ELEMENT numbers.Remove(1);


OTHERWICE USE THIS IN FOR LOOP
DELETE ELEMENT AT INDEX numbers.RemoveAt(0);

GET INDEX OF AN ELEMENT int index = numbers.IndexOf(1);


OTHERWISE IT RETURNS -1
CHECK IF LIST CONTAINS AND bool flag = numbers.Contains(1);
ELEMENT
LIST LENGTH int howmany = numbers.Count;
TIME
C# TYPE SYNTAX

DATETIME var dateTime = new DateTime (2015,1,1);

immutable var now = DateTime.Now;


methods var tomorrow = now.AddDays(1);
var tomorrow = now.AddDays(-1);

convert to string string a = now.ToLongDateString();


string b = now.ToShortDateString();
string c = now.ToLongTimeString();
string d =now.ToShortTimeString();
string e =now.ToString("yy-MM-dd HH:mm");

TIMESPAN var timeSpan = new TimeSpan(1,2,3);

immutable var timeSpan2 = TimeSpan.FromHours(1);


methods timeSpan2.Add(TimeSpan.FromMinutes(2)));
timeSpan2.Subtract(TimeSpan.FromMinutes(2)));

properties int minutes = timeSpan.Minutes;


convert to string string a = timeSpan.ToString();
parsing TimeSpan b = TimeSpan.Parse("01:02:03");
STRINGS
C# TYPE SYNTAX

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)

FILE INFO var fileInfo = new FileInfo(path);

instance methods fileInfo.CopyTo("...");


where you have to create
new object each command fileInfo.Delete();

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

visibility return-type name (parameters) public void DoStuff()


{ {
function code Console.WriteLine("Hello");
} }

public int AddNumbers(int a, int b)


{
int result = a + b;
return result;
}

CALLING FUNCTIONS

empty function DoStuff();

function with return type int sum = AddNumbers(1,2);


REF VS OUT
KEYWORD SYNTAX

REF int a = 20;


SomeFunction(ref a);
Here a will be equal
to 22 without ref public void AddNumbers(ref int d) {
keyword a would d = d + 2;
remain 20. }

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);

1. OUT AND REF HELPS TO PASS BY REFERENCE


2. REF IS TWO WAY FROM CALLER TO CALLEE AND BACK
3. OUT IS ONE WAY IT SENDS DATA BACK FROM CALLEE TO CALLER.
DEBUGGING
METHOD SHORTCUT

1. YOU NEED TO BUT BREAK POINT F9


2. RUN APPLICATION IN DEBUG MODE F5
3. RUN APPLICATION WITHOUT DEBUG MODE Ctrl + F5
4. CONTINUE EXECUTION (STEP OVER) F10
5. STEP INTO THE METHOD F11
6. STEP OUT Shift + F11
7. TO STOP DEBUG MODE Shift + F5
RHINOCOMMON
C#
GRASSHOPPER
C# COMPONENT

Input String for debugging


Parameter ”x” and compiling messages

Input Output parameter “A”


Parameter ”y” script return values

The user can change names, types and number of inputs and outputs.
C# COMPONENT INPUTS

The ultimate base class of all classes in the .NET

Base types

Access type RhinoCommon types

Note: there are no types associated with output.


They are defined as generic system type “object”
C# EXTERNAL ASSEMBLIES

Drag and drop .dll file


C# SCRIPT EDITOR
Double click in the middle of the component to open
C# script editor.

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.

Custom additional code


Here you can write classes, function and declare
variables.
Variables declared here are saved in memory.
C# SCRIPT EDITOR OVERRIDES
Click on Preview Overrides button

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

Note: if you are displaying geometry


and outputing geometry you have to
use base.DrawViewportMeshes(args);
after one of the override method.
PRINT
DESCRIPTION C# SYNTAX

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

Type hint: private void RunScript(double x, double y, ref object A)


Item Access {
A = x + y;
}

Type hint: private void RunScript(List<double> x, double y, ref object A)


List Access {
(loop through the for(int i = 0 ; i < x.Count; i++)
list of items) x[i] += y;

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

TIMER private void RunScript(bool reset, bool run, ref object A)


{
ExpireSolution
method can be if (reset)
replaced with timer n = 0;
component. else if(run){
n++;
Component.ExpireSolution(true);
}

A = n;

// <Custom additional code>


int n = 0;
PRIMITIVE GEOMETRY
DESCRIPTION C# SYNTAX

Point Point3d pt = new Point3d(x, y, z);


A = pt;

Line Line ln = new Line(new Point3d(0, 0, 0), new Point3d(0, 0, 100));


A = ln;

Circle List<Circle> circles = new List<Circle>();

foreach (Point3d pt in pts


circles.Add(new Circle(pt, 10))

A = circles;

Polyline Polyline pl = new Polyline();

for (int i = 0; i < 10; i ++)


pl.Add(i, 0, Math.Pow(i, 2));

A = pl;
VECTOR MATH
DESCRIPTION C# SYNTAX

Vector3d //Get Unit Vector


v1.Unitize();

//Vector subtraction
Vector3d vSubtraction = v0 - v1;

//Vector dot product


(if result in positive number then vector are in the same direction)
double dotProduct = v0 * v1;

//Scale vector
Vector3d vScaled = 0.1 * v0;

//Move a point by a vector


Point3d movedPoint = Point3d.Origin + v0;

//Distance between two points


double distance = Point3d.Origin.DistanceTo(movedPoint);

//Get Vector length


double vectorLength = v0.Length;
NURBS CURVE
DESCRIPTION C# SYNTAX

NurbsCurve //Create nurbs curve


int degree = 3;
NurbsCurve nc = NurbsCurve.Create(true, degree, CPoints);

//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

Surface //Create grid of points


List<Point3d> Pts = new List<Point3d>();
create surface from for (int i = 0; i < n1; i++)
for (int j = 0; j < n2; j++)
points Pts.Add(new Point3d(i, j, Math.Cos(i) * Math.Sin(j)));
//Create surface
NurbsSurface surface = Rhino.Geometry.NurbsSurface.CreateThroughPoints(Pts, n1, n2,
uDegree, vDegree, false, false);
//Change control point weight
surface.Points.SetControlPoint(6, 6, new ControlPoint(surface.Points.GetControlPoint(6,
6).Location, weight));

divide a surface //Find Step - n must be greater than 1


double step = 1 / ((double) n - 1);
//Take one brep face and convert it to nurbs surface
NurbsSurface ns = brep.Faces[0].ToNurbsSurface();
//Normalize domain
ns.SetDomain(0, new Interval(0, 1));
ns.SetDomain(1, new Interval(0, 1));
//Divide surface into points
var points = new List<Point3d>();
for(double i = 0 ; i < 1; i += step)
for(double j = 0 ; j < 1; j += step)
points.Add(ns.PointAt(i, j));
BREP
DESCRIPTION C# SYNTAX

Brep //Create cone


new Cone(new Plane(Point3d.Origin, new Vector3d(0, 1, 1)), height, radius);
create breps
//Create sphere
Sphere sphere = new Sphere(Point3d.Origin, radius);
//Get bounding box
BoundingBox bbox = sphere.BoundingBox;
//Find Box center
Point3d center = bbox.Center;

//Create from points


Brep brep = Rhino.Geometry.Brep.CreateFromBox(Corners);
//Create loft surface
get brep properties Brep.CreateFromLoft(crv, Point3d.Unset, Point3d.Unset, LoftType.Normal, false);

Print("N of brep.Curves2D = " + brep.Curves2D.Count.ToString());


Print("N of brep.Curves3D = " + brep.Curves3D.Count.ToString());
Print("N of brep.Edges = " + brep.Edges.Count.ToString());
Print("N of brep.Faces = " + brep.Faces.Count.ToString());
Print("N of brep.Loops = " + brep.Loops.Count.ToString());
Print("N of brep.Surfaces = " + brep.Surfaces.Count.ToString());
Print("N of brep.Trims = " + brep.Trims.Count.ToString());
Print("N of brep.Vertices = " + brep.Vertices.Count.ToString());
MESH
DESCRIPTION C# SYNTAX

Mesh //Initialize mesh


Mesh mesh = new Mesh();

//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 pointCloud = new PointCloud();

for (int i = 0; i < n1 ; i++)


for(int j = 0; j < n2; j++)
for(int k = 0; k < n3; k++)
pointCloud.Add(new Point3d(i * dist, j * dist, k * dist),
Color.FromArgb(i * 2 % 255, j * 2 % 255, k * 2 % 255));

_pointCloud = pointCloud;

// </Custom additional code> Display PointCloud


PointCloud _pointCloud = new PointCloud();

public override BoundingBox ClippingBox


{
get { return _pointCloud.GetBoundingBox(false);}
}

public override void DrawViewportMeshes(IGH_PreviewArgs args){


args.Display.DrawPointCloud(_pointCloud, 5);
}
MASS PROPERTIES
DESCRIPTION C# SYNTAX

AREA AreaMassProperties amp = AreaMassProperties.Compute(geo);


Print("Area: " + amp.Area.ToString());

//Find center
A = amp.Centroid;

VOLUME VolumeMassProperties vmp = VolumeMassProperties.Compute(geo);


Print("Volume: " + vmp.Volume);

//Find center
B = vmp.Centroid;
TRANSFORM
DESCRIPTION C# SYNTAX

Point3d pt.Transform(Transform.Translation(Vector3d.ZAxis)); //Transform matrix


pt += Vector3d.ZAxis; //Add vector
pt += new Point3d(0, 0, 1); //Add points

Line Line newLine = line; //Line is a struct so copy lines is simple like this
newLine.Transform(Transform.Scale(newLine.PointAt(0.5), factor));

Ellipse Ellipse c = new Ellipse(Plane.WorldXY, r1,r2);


var nurbs = c.ToNurbsCurve();
nurbs.Rotate(R * i, Vector3d.YAxis, new Point3d(X, 0, 0));

Curve Plane basePlane = new Plane(profile.GetBoundingBox(false).Center, Vector3d.ZAxis);


path.Domain = new Interval(0, 1);
List<Curve> orientedCrv = new List<Curve>();

for(int i = 0 ; i <= n; i++){


//Get perpendicular frame and orient curves
Plane plane;
path.PerpendicularFrameAt((double) i / n, out plane);
perFrames.Add(plane);
Curve newCurve = profile.DuplicateCurve();
newCurve.Transform(Transform.PlaneToPlane(basePlane, plane));
orientedCrv.Add(newCurve);
}
TRANSFORM
DESCRIPTION C# SYNTAX

Brep srf.SetDomain(0, new Interval(0, 1));


srf.SetDomain(1, new Interval(0, 1));
The same List<Plane> planes = new List<Plane>();
List<Plane> orientplanes = new List<Plane>();
transformation List<Brep> objects = new List<Brep>();
method could be
applied to meshes //Get orientation plane
and surfaces Point3d pointCenter = b.GetBoundingBox(true).Center;
Point3d pointLowest = b.GetBoundingBox(true).Min;
Point3d pointLowCen = new Point3d(pointCenter.X, pointCenter.Y, pointLowest.Z);
orientplanes.Add(new Plane(pointLowCen, Vector3d.ZAxis));

for(int i = 0; i < n; i++){


for(int j = 0; j < n; j++){
//Get Planes
Plane plane;
srf.FrameAt((double) i / n, (double) j / n, out plane);
planes.Add(plane);
//Orient breps
Brep newBrep = breps[0].DuplicateBrep();
newBrep.Transform(Transform.PlaneToPlane(orientplanes[0], plane));
objects.Add(newBrep);
}
}
C# COLLECTIONS IN GH
DESCRIPTION C# SYNTAX

2D Arrays of Point3d[][] points = new Point3d[n][];


Arrays
(Jagged arrays) for (int i = 0; i < n ; i++){
are not readable points[i] = new Point3d[i * i];
for(int j = 0; j < i * i; j++)
points[i][j] = new Point3d( i * dist, j * dist, 0);
}

A = points;

3D Arrays of Point3d[][][] points = new Point3d[n1][][];


Arrays
(Jagged arrays) for (int i = 0; i < n1 ; i++){
are not readable points[i] = new Point3d[n2][];
for(int j = 0; j < n2; j++){
points[i][j] = new Point3d[n3*j];
for(int k = 0; k < n3*j; k++)
points[i][j][k] = new Point3d( i * dist, j * dist, k * dist);
}
}
A = points;
DESCRIPTION C# SYNTAX

2D Point3d[,,] points = new Point3d[n, n, n];


Multidimensional
Arrays for (int i = 0; i < n ; i++)
(Rectangular for(int j = 0; j < n; j++)
Arrays) for(int k = 0; k < n; k++)
are readable, but points[i, j, k] = new Point3d(i * dist, j * dist, k * dist);
only as a flattened
list A = points;
DATATREE
DESCRIPTION C# SYNTAX

DataTree DataTree<Point3d> points = new DataTree<Point3d>();

for (int i = 0; i < n ; i++)


for(int j = 0; j < n; j++)
for(int k = 0; k < n; k++)
GH_Path points.Add(new Point3d(i * dist, j * dist, k * dist), new GH_Path(i, j));

A = points;
NOISE / RANDOM
DESCRIPTION C# SYNTAX

Random Random random = new Random();


int randomInt = random.Next(-1,5);
double randomDouble = random.NextDouble(0.01, 0.05);
Noise private void RunScript(double scale, double increment, double zIncrement, int width, int
height, int step, ref object A, ref object B, ref object C, ref object Noise) {
You must reference int sizeX = (int) (width / step);
int sizeY = (int) (height / step);
SimplexNoise.dll List<Point3d> points = new List<Point3d>();
double xOff = 0.0;
zOff += zIncrement;
//For every x,y coordinate, calculate a noise value and produce circle radius
for(int x = 0; x < sizeX * step; x += step){
xOff += increment;
double yOff = 0.0;
for(int y = 0; y < sizeY * step; y += step){
yOff += increment;
Noise is called here double dist = SimplexNoise.Noise.Generate(Convert.ToSingle(xOff),
by SimplexNoise. Convert.ToSingle(yOff), Convert.ToSingle(zOff)) * scale;
Noise.Generate points.Add(new Point3d(x, y, dist));
} }
//Output
A = points; B = sizeX; C = sizeY;
}
double zOff = 0.0; //Output
SUBDIVISION
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
}

List<Polyline> subdivide(Polyline triangle, double height){


Line[] segments = triangle.GetSegments();
Vector3d normal = Vector3d.CrossProduct(segments[0].Direction, segments[1].Direction);
normal.Unitize();
normal *= height;
List<Point3d> midPts = new List<Point3d>(); //Create Middle points
foreach(Line s in segments){
Vector3d m = (Vector3d) (s.PointAt(0.5));
m += normal;
midPts.Add((Point3d) m);
}
Polyline a = new Polyline(new List<Point3d>{segments[0].From, midPts[0], midPts[2],segments[0].From});
Polyline b = new Polyline(new List<Point3d>{midPts[0], segments[1].From, midPts[1],midPts[0]});
Polyline c = new Polyline(new List<Point3d>{midPts[2], midPts[1], segments[2].From,midPts[2]});
Polyline d = new Polyline(new List<Point3d>{midPts[0], midPts[1], midPts[2],midPts[0]});
return new List<Polyline>{a,b,c,d};
}
RTREE
DESCRIPTION C# SYNTAX

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();

add points to for(int i = 0; i < lineList.Count; i++)


rtree tree.Insert(lineList[i].PointAt(0.5), i);

search tree.Search(new Sphere(lineList[item].PointAt(0.5), radius), method);


A = cl;
B = ci;
}
//Custom Additional code
List<Line> cl = new List<Line>();
List<int> ci = new List<int>();
List<Line> lineList2 = new List<Line>();
private void method(object sender, RTreeEventArgs e){
cl.Add(lineList2[e.Id]);
ci.Add(e.Id);
}
BITMAP
DESCRIPTION C# SYNTAX

Reference .jpg file if (!System.IO.File.Exists(path))


throw new ArgumentException("File does not exist");
// Load the bitmap from file.
Bitmap bitmap = new Bitmap(path);
// Resize it to 200x200 pixels.
bitmap = new Bitmap(bitmap, 200, 200);
// Create a mesh and assign height and color values
int nx = bitmap.Width - 1;
int ny = bitmap.Height - 1;
Interval dx = new Interval(0, 200);
Interval dy = new Interval(0, 200);

Mesh mesh = Mesh.CreateFromPlane(Plane.WorldXY, dx, dy, 200, 200);

Color[] colours = new Color[mesh.Vertices.Count];


for (int i = 0; i < mesh.Vertices.Count; i++)
{
Point3f vertex = mesh.Vertices[i];
int x = (int) vertex.X;
int y = (int) vertex.Y;
colours[i] = bitmap.GetPixel(x, y);
mesh.Vertices.SetVertex(i, vertex.X, vertex.Y, -colours[i].B * scale);
}
mesh.VertexColors.SetColors(colours);
M = mesh;
COMPONENT
VISUAL STUDIO
GRASSHOPPER TEMPLATE
DOWNLOAD TEMPLATE

https://marketplace.visualstudio.com/items?itemName=McNeel.GrasshopperAssemblyforv5
…AND INSTALL IT (YOU MUST HAVE VISUAL STUDIO)
CREATE NEW PROJECT
CREATE NEW PROJECT

1. Select Grasshopper Add-On template 2. Name you component.


Give your project name and directory
COMPONENT CLASS
CHANGE BUILD DIRECTORY
BUILD IT -> RUN IT
RHINO IS LOADING NOW…

RUN GRASSHOPPER AND FIND YOUR COMPONENT


COMPONENT
VISUAL STUDIO
WITHOUT
GRASSHOPPER TEMPLATE
CREATE NEW PROJECT
CREATE CLASS LIBRARY
REFERENCE LIBRARIES

Add 4 references:
1. System.Drawing
2. Grasshopper
3. GH_IO
4. RhinoCommon
SET LOCAL COPY TO FALSE

It prevents copying libraries to your “bin” folder.


CHANGE PROPERTIES

1. Grasshopper libraries folder

2. Rhino.exe directory

3. .dll to .gha type this:


Copy "$(TargetPath)" "$(TargetDir)$(ProjectName).gha"
Erase "$(TargetPath)"
CREATE ASSEMBLY INFO CLASS
using System;
using System.Drawing;
using Grasshopper.Kernel;

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…

RUN GRASSHOPPER AND FIND YOUR COMPONENT

You might also like