-
Notifications
You must be signed in to change notification settings - Fork 330
/
SampleGhTaskAreaComponent.cs
167 lines (144 loc) · 4.53 KB
/
SampleGhTaskAreaComponent.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.Threading.Tasks;
using Rhino.Geometry;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Types;
namespace SampleGhTaskCapable.Components
{
public class SampleGhTaskAreaComponent : GH_TaskCapableComponent<SampleGhTaskAreaComponent.SolveResults>
{
public SampleGhTaskAreaComponent()
: base("Task Area", "TArea", "Task compute the area breps, meshes and planar closed curves.", "Sample", "Task")
{
}
protected override void RegisterInputParams(GH_Component.GH_InputParamManager input)
{
input.AddGeometryParameter("Geometry", "G", "Brep, mesh or planar closed curve for area computation", GH_ParamAccess.item);
}
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager output)
{
output.AddNumberParameter("Area", "A", "Area of geometry", GH_ParamAccess.item);
}
public class SolveResults
{
public double Area { get; set; }
}
private SolveResults ComputeArea(IGH_GeometricGoo geometry)
{
if (null == geometry)
return null;
bool rc = false;
Brep brep = null;
Mesh mesh = null;
Curve curve = null;
if (!rc)
{
var gh_brep = geometry as GH_Brep;
rc = (null != gh_brep && null != gh_brep.Value);
if (rc)
brep = gh_brep.Value;
}
if (!rc)
{
var gh_surface = geometry as GH_Surface;
rc = (null != gh_surface && null != gh_surface.Value);
if (rc)
brep = gh_surface.Value;
}
if (!rc)
{
var gh_mesh = geometry as GH_Mesh;
rc = (null != gh_mesh && null != gh_mesh.Value);
if (rc)
mesh = gh_mesh.Value;
}
if (!rc)
{
var gh_curve = geometry as GH_Curve;
rc = (null != gh_curve && null != gh_curve.Value);
if (rc)
curve = gh_curve.Value;
}
if (!rc)
{
var gh_circle = geometry as GH_Circle;
if (null != gh_circle)
{
var circle = gh_circle.Value;
return new SolveResults { Area = Math.PI * circle.Radius * circle.Radius };
}
}
if (!rc)
{
var gh_rectangle = geometry as GH_Rectangle;
if (null != gh_rectangle)
{
var rectangle = gh_rectangle.Value;
return new SolveResults { Area = Math.Abs(rectangle.Width * rectangle.Height) };
}
}
// Indirect casts
if (!rc)
rc = GH_Convert.ToBrep(geometry, ref brep, GH_Conversion.Both);
if (!rc)
rc = GH_Convert.ToCurve(geometry, ref curve, GH_Conversion.Both);
if (!rc)
rc = GH_Convert.ToMesh(geometry, ref mesh, GH_Conversion.Both);
if (rc)
{
AreaMassProperties mp = null;
if (null != brep)
{
mp = AreaMassProperties.Compute(brep, true, false, false, false);
}
else if (null != mesh)
{
mp = AreaMassProperties.Compute(mesh, true, false, false, false);
}
else if (null != curve)
{
if (!curve.IsClosed)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Curve must be closed for Area calculation");
return null;
}
if (!curve.IsPlanar(DocumentTolerance()))
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Curve must be planar for Area calculation");
return null;
}
var geom = new GeometryBase[] { curve };
mp = AreaMassProperties.Compute(geom, true, false, false, false);
}
if (null != mp)
return new SolveResults { Area = mp.Area };
else
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Area could not be computed.");
}
return null;
}
protected override void SolveInstance(IGH_DataAccess data)
{
if (InPreSolve)
{
IGH_GeometricGoo geometry = null;
data.GetData(0, ref geometry);
TaskList.Add(Task.Run(() => ComputeArea(geometry), CancelToken));
return;
}
if (!GetSolveResults(data, out SolveResults result))
{
IGH_GeometricGoo geometry = null;
if (!data.GetData(0, ref geometry))
return;
result = ComputeArea(geometry);
}
if (null != result)
{
data.SetData(0, result.Area);
}
}
protected override System.Drawing.Bitmap Icon => Properties.Resources.SampleGhTaskAreaComponent_24x24;
public override Guid ComponentGuid => new Guid("689C4320-BA81-4D6A-92D5-053CFA2F7CB4");
}
}