-
Notifications
You must be signed in to change notification settings - Fork 0
/
WSCSharpJazzComponent.cs
152 lines (132 loc) · 6.81 KB
/
WSCSharpJazzComponent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using Grasshopper;
using Grasshopper.Kernel;
using Rhino.Geometry;
using System;
using System.Collections.Generic;
namespace WSCSharpJazz
{
public class WSCSharpJazzComponent : GH_Component
{
/// <summary>
/// Each implementation of GH_Component must provide a public
/// constructor without any arguments.
/// Category represents the Tab in which the component will appear,
/// Subcategory the panel. If you use non-existing tab or panel names,
/// new tabs/panels will automatically be created.
/// </summary>
public WSCSharpJazzComponent()
: base("TestJazz", "ASpi",
"Construct an Archimedean, or arithmetic, spiral given its radii and number of turns.",
"Curve", "Primitive")
{
}
/// <summary>
/// Registers all the input parameters for this component.
/// </summary>
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
// Use the pManager object to register your input parameters.
// You can often supply default values when creating parameters.
// All parameters must have the correct access type. If you want
// to import lists or trees of values, modify the ParamAccess flag.
pManager.AddPlaneParameter("Plane", "P", "Base plane for spiral", GH_ParamAccess.item, Plane.WorldXY);
pManager.AddNumberParameter("Inner Radius", "R0", "Inner radius for spiral", GH_ParamAccess.item, 1.0);
pManager.AddNumberParameter("Outer Radius", "R1", "Outer radius for spiral", GH_ParamAccess.item, 10.0);
pManager.AddIntegerParameter("Turns", "T", "Number of turns between radii", GH_ParamAccess.item, 10);
// If you want to change properties of certain parameters,
// you can use the pManager instance to access them by index:
//pManager[0].Optional = true;
}
/// <summary>
/// Registers all the output parameters for this component.
/// </summary>
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
// Use the pManager object to register your output parameters.
// Output parameters do not have default values, but they too must have the correct access type.
pManager.AddCurveParameter("Spiral", "S", "Spiral curve", GH_ParamAccess.item);
// Sometimes you want to hide a specific parameter from the Rhino preview.
// You can use the HideParameter() method as a quick way:
//pManager.HideParameter(0);
}
/// <summary>
/// This is the method that actually does the work.
/// </summary>
/// <param name="DA">The DA object can be used to retrieve data from input parameters and
/// to store data in output parameters.</param>
protected override void SolveInstance(IGH_DataAccess DA)
{
// First, we need to retrieve all data from the input parameters.
// We'll start by declaring variables and assigning them starting values.
Plane plane = Plane.WorldXY;
double radius0 = 0.0;
double radius1 = 0.0;
int turns = 0;
// Then we need to access the input parameters individually.
// When data cannot be extracted from a parameter, we should abort this method.
if (!DA.GetData(0, ref plane)) return;
if (!DA.GetData(1, ref radius0)) return;
if (!DA.GetData(2, ref radius1)) return;
if (!DA.GetData(3, ref turns)) return;
// We should now validate the data and warn the user if invalid data is supplied.
if (radius0 < 0.0)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Inner radius must be bigger than or equal to zero");
return;
}
if (radius1 <= radius0)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Outer radius must be bigger than the inner radius");
return;
}
if (turns <= 0)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Spiral turn count must be bigger than or equal to one");
return;
}
// We're set to create the spiral now. To keep the size of the SolveInstance() method small,
// The actual functionality will be in a different method:
Curve spiral = CreateSpiral(plane, radius0, radius1, turns);
// Finally assign the spiral to the output parameter.
DA.SetData(0, spiral);
}
Curve CreateSpiral(Plane plane, double r0, double r1, Int32 turns)
{
Line l0 = new Line(plane.Origin + r0 * plane.XAxis, plane.Origin + r1 * plane.XAxis);
Line l1 = new Line(plane.Origin - r0 * plane.XAxis, plane.Origin - r1 * plane.XAxis);
Point3d[] p0;
Point3d[] p1;
l0.ToNurbsCurve().DivideByCount(turns, true, out p0);
l1.ToNurbsCurve().DivideByCount(turns, true, out p1);
PolyCurve spiral = new PolyCurve();
for (int i = 0; i < p0.Length - 1; i++)
{
Arc arc0 = new Arc(p0[i], plane.YAxis, p1[i + 1]);
Arc arc1 = new Arc(p1[i + 1], -plane.YAxis, p0[i + 1]);
spiral.Append(arc0);
spiral.Append(arc1);
}
return spiral;
}
/// <summary>
/// The Exposure property controls where in the panel a component icon
/// will appear. There are seven possible locations (primary to septenary),
/// each of which can be combined with the GH_Exposure.obscure flag, which
/// ensures the component will only be visible on panel dropdowns.
/// </summary>
public override GH_Exposure Exposure => GH_Exposure.primary;
/// <summary>
/// Provides an Icon for every component that will be visible in the User Interface.
/// Icons need to be 24x24 pixels.
/// You can add image files to your project resources and access them like this:
/// return Resources.IconForThisComponent;
/// </summary>
protected override System.Drawing.Bitmap Icon => null;
/// <summary>
/// Each component must have a unique Guid to identify it.
/// It is vital this Guid doesn't change otherwise old ghx files
/// that use the old ID will partially fail during loading.
/// </summary>
public override Guid ComponentGuid => new Guid("5B64DD84-3929-467E-858A-6B775F12AC74");
}
}