-
Notifications
You must be signed in to change notification settings - Fork 1
/
ClipShapes.cs
169 lines (140 loc) · 5.24 KB
/
ClipShapes.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
168
169
using Clipper2Lib;
using GH_IO.Serialization;
using Grasshopper.Kernel;
using Rhino.Geometry;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace ClipperTwo
{
public class ClipShapesComponent : GH_Component
{
public ClipShapesComponent()
: base("Clipper2 Rectangle Clip", "C2RectClip",
"Don't rotate the rectagnle",
"Curve", "Clipper2")
{
}
int precision = 4;
public override void AppendAdditionalMenuItems(ToolStripDropDown menu)
{
base.AppendAdditionalMenuItems(menu);
Menu_AppendSeparator(menu);
#region precision
Menu_AppendSeparator(menu);
TableLayoutPanel tableLayoutPanel = new TableLayoutPanel
{
ColumnCount = 2,
AutoSize = true,
Height = 30,
Width = 140,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
BackColor = Color.Transparent
};
Label label = new Label
{
Text = "Precision ",
AutoSize = true,
Anchor = AnchorStyles.Right
};
NumericUpDown numericUpDown = new NumericUpDown
{
Minimum = 2,
Maximum = 8,
DecimalPlaces = 0,
Value = precision,
Increment = 1,
Width = 60,
Anchor = AnchorStyles.Left
};
tableLayoutPanel.Controls.Add(label, 0, 0);
tableLayoutPanel.Controls.Add(numericUpDown, 1, 0);
Menu_AppendCustomItem(menu, tableLayoutPanel);
numericUpDown.MouseWheel += (sender, e) =>
{
((HandledMouseEventArgs)e).Handled = true;
};
numericUpDown.ValueChanged += (sender, e) =>
{
precision = (int)numericUpDown.Value;
ExpireSolution(true);
};
#endregion
}
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
pManager.AddCurveParameter("Polylines", "", "", GH_ParamAccess.list);
pManager.AddRectangleParameter("Rectangle", "", "", GH_ParamAccess.item);
//pManager.AddBooleanParameter("Convex", "", "", GH_ParamAccess.item, false);
pManager[0].Optional = true;
pManager[1].Optional = true;
}
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.AddGenericParameter("Result", "", "Result", GH_ParamAccess.list);
}
protected override void SolveInstance(IGH_DataAccess DA)
{
List<Curve> curves = new List<Curve>();
Rectangle3d rectangle = Rectangle3d.Unset;
//bool convexOnly = false;
if (!DA.GetDataList(0, curves)) return;
foreach (Curve curve in curves)
{
if (!curve.IsPolyline() || !curve.IsValid)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Choose a valid polylines");
return;
}
}
if (!DA.GetData(1, ref rectangle)) return;
//if (!DA.GetData(2, ref convexOnly)) return;
List<Curve> newcurves = new List<Curve>();
var mirror = Transform.Mirror(Plane.WorldZX);
rectangle.Transform(mirror);
foreach (Curve curve in curves)
{
curve.Transform(mirror);
newcurves.Add(curve);
}
ClipShapesGh(newcurves, rectangle);
List<Curve> newresultCurve = new List<Curve>();
foreach (Curve curve in resultCurve)
{
curve.Transform(mirror);
newresultCurve.Add(curve);
}
DA.SetDataList(0, newresultCurve);
}
List<Curve> resultCurve = new List<Curve>();
void ClipShapesGh(List<Curve> curves, Rectangle3d rectangle)
{
resultCurve.Clear();
PathsD paths = Converter.ConvertPolylinesA1(curves);
RectD rect = Converter.ConvertRectangle(rectangle);
PathsD cliprect;
cliprect = Clipper.RectClip(rect, paths, precision);
foreach (var path in cliprect)
{
Polyline polyline = new Polyline(path.Select(p => new Point3d(p.x, p.y, 0)));
polyline.Add(polyline[0]);
resultCurve.Add(polyline.ToNurbsCurve());
}
}
protected override System.Drawing.Bitmap Icon => Properties.Resources.clipshapes;
public override Guid ComponentGuid => new Guid("DA58CEAB-42FA-4AF8-9FCC-75792B068131");
public override bool Read(GH_IReader reader)
{
if (reader.ItemExists("Precision"))
precision = reader.GetInt32("Precision");
return base.Read(reader);
}
public override bool Write(GH_IWriter writer)
{
writer.SetInt32("Precision", precision);
return base.Write(writer);
}
}
}