-
Notifications
You must be signed in to change notification settings - Fork 1
/
ClipperOffset.cs
446 lines (372 loc) · 14.9 KB
/
ClipperOffset.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
using Clipper2Lib;
using GH_IO.Serialization;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Parameters;
using Rhino.Geometry;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Windows.Forms;
namespace ClipperTwo
{
public class ClipperOffsetComponent : GH_Component
{
public ClipperOffsetComponent()
: base("Clipper2 Offset", "C2Offset",
"",
"Curve", "Clipper2")
{
}
int precision = 4;
int svgWidth = 800;
int svgHeight = 600;
double thickness = 1;
Color color = Color.Black;
public string filename = "";
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 ",
Width = 60,
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
#region width
Menu_AppendSeparator(menu);
TableLayoutPanel tableLayoutPanelwidth = new TableLayoutPanel
{
ColumnCount = 2,
AutoSize = true,
Height = 30,
Width = 140,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
BackColor = Color.Transparent
};
Label widthlabel = new Label
{
Text = "Width ",
Width = 60,
Anchor = AnchorStyles.Right
};
NumericUpDown numericUpDownW = new NumericUpDown
{
Minimum = 100,
Maximum = 4000,
DecimalPlaces = 0,
Value = svgWidth,
Increment = 100,
Width = 60,
Anchor = AnchorStyles.Left
};
tableLayoutPanelwidth.Controls.Add(widthlabel, 0, 0);
tableLayoutPanelwidth.Controls.Add(numericUpDownW, 1, 0);
Menu_AppendCustomItem(menu, tableLayoutPanelwidth);
numericUpDownW.MouseWheel += (sender, e) =>
{
((HandledMouseEventArgs)e).Handled = true;
};
numericUpDownW.ValueChanged += (sender, e) =>
{
svgWidth = (int)numericUpDownW.Value;
ExpireSolution(true);
};
#endregion
#region height
TableLayoutPanel tableLayoutPanelheight = new TableLayoutPanel
{
ColumnCount = 2,
AutoSize = true,
Height = 30,
Width = 140,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
BackColor = Color.Transparent
};
Label heightlabel = new Label
{
Text = "Height ",
Width = 60,
Anchor = AnchorStyles.Right
};
NumericUpDown numericUpDownH = new NumericUpDown
{
Minimum = 100,
Maximum = 4000,
DecimalPlaces = 0,
Value = svgHeight,
Increment = 100,
Width = 60,
Anchor = AnchorStyles.Left
};
tableLayoutPanelheight.Controls.Add(heightlabel, 0, 0);
tableLayoutPanelheight.Controls.Add(numericUpDownH, 1, 0);
Menu_AppendCustomItem(menu, tableLayoutPanelheight);
numericUpDownH.MouseWheel += (sender, e) =>
{
((HandledMouseEventArgs)e).Handled = true;
};
numericUpDownH.ValueChanged += (sender, e) =>
{
svgHeight = (int)numericUpDownW.Value;
ExpireSolution(true);
};
#endregion
#region SVG Save Button
ToolStripButton saveAsSvgButton = new ToolStripButton
{
Text = "Save as SVG",
DisplayStyle = ToolStripItemDisplayStyle.ImageAndText,
TextAlign = ContentAlignment.MiddleCenter,
BackColor = Color.FromArgb(245, 245, 245),
AutoSize = false,
Width = 80,
Height = 24,
AutoToolTip = false,
};
// Handle the Paint event
saveAsSvgButton.Paint += (sender, e) =>
{
// Draw the rounded rectangle (border)
using (GraphicsPath path = GetRoundedRectangle(0, 0, saveAsSvgButton.Width - 1, saveAsSvgButton.Height - 1, 8))
using (Pen borderPen = new Pen(Color.Gray, 1))
{
e.Graphics.DrawPath(borderPen, path);
}
};
saveAsSvgButton.Click += (sender, e) =>
{
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
{
saveFileDialog.Filter = "SVG files (*.svg)|*.svg";
saveFileDialog.FilterIndex = 1;
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
filename = saveFileDialog.FileName;
SvgWriter svg = new SvgWriter();
SvgUtils.AddSolution(svg, svgpath, color, thickness, false);
SvgUtils.SaveToFile(svg, filename, FillRule.EvenOdd, svgWidth, svgHeight, 20);
}
}
};
menu.Items.Add(saveAsSvgButton);
#endregion
#region BIN Save Button
Menu_AppendSeparator(menu);
ToolStripButton saveAsBinButton = new ToolStripButton
{
Text = "Save as BIN",
DisplayStyle = ToolStripItemDisplayStyle.ImageAndText,
TextAlign = ContentAlignment.MiddleCenter,
BackColor = Color.FromArgb(245, 245, 245),
AutoSize = false,
Width = 80,
Height = 24,
AutoToolTip = false,
};
// Handle the Paint event
saveAsBinButton.Paint += (sender, e) =>
{
// Draw the rounded rectangle (border)
using (GraphicsPath path = GetRoundedRectangle(0, 0, saveAsBinButton.Width - 1, saveAsBinButton.Height - 1, 8))
using (Pen borderPen = new Pen(Color.Gray, 1))
{
e.Graphics.DrawPath(borderPen, path);
}
};
saveAsBinButton.Click += SaveFile;
menu.Items.Add(saveAsBinButton);
#endregion
}
private GraphicsPath GetRoundedRectangle(int x, int y, int width, int height, int radius)
{
GraphicsPath path = new GraphicsPath();
path.AddArc(x, y, radius * 2, radius * 2, 180, 90); // Top-left corner
path.AddArc(width - 2 * radius, y, radius * 2, radius * 2, 270, 90); // Top-right corner
path.AddArc(width - 2 * radius, height - 2 * radius, radius * 2, radius * 2, 0, 90); // Bottom-right corner
path.AddArc(x, height - 2 * radius, radius * 2, radius * 2, 90, 90); // Bottom-left corner
path.CloseFigure();
return path;
}
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
pManager.AddCurveParameter("Polylines", "", "", GH_ParamAccess.list);
pManager.AddNumberParameter("Distance", "", "", GH_ParamAccess.item, 0.0);
pManager.AddIntegerParameter("JoinType", "", "", GH_ParamAccess.item, 0);
pManager.AddIntegerParameter("EndType", "", "", GH_ParamAccess.item, 0);
pManager.AddNumberParameter("MiterLimit", "", "", GH_ParamAccess.item, 2);
pManager[0].Optional = true;
pManager[1].Optional = true;
if (!(pManager[2] is Param_Integer paramInteger1))
return;
paramInteger1.AddNamedValue("Miter", 0);
paramInteger1.AddNamedValue("Square", 1);
paramInteger1.AddNamedValue("Bevel", 2);
paramInteger1.AddNamedValue("Round", 3);
if (!(pManager[3] is Param_Integer paramInteger2))
return;
paramInteger2.AddNamedValue("Butt", 0);
paramInteger2.AddNamedValue("Joined", 1);
paramInteger2.AddNamedValue("Square", 2);
paramInteger2.AddNamedValue("Round", 3);
paramInteger2.AddNamedValue("Polygon", 4);
}
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.AddGenericParameter("Holes", "", "Holes bounds", GH_ParamAccess.list);
pManager.AddGenericParameter("Outer", "", "Outer bounds", GH_ParamAccess.list);
}
PathsD pp = new PathsD();
PathsD svgpath = new PathsD();
protected override void BeforeSolveInstance()
{
pp.Clear();
svgpath = new PathsD();
}
protected override void SolveInstance(IGH_DataAccess DA)
{
List<Curve> curves = new List<Curve>();
double distance = 0;
int jointtype = 0;
int endtype = 0;
double miter = 2;
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 distance)) return;
if (!DA.GetData(2, ref jointtype)) return;
if (!DA.GetData(3, ref endtype)) return;
if (!DA.GetData(4, ref miter)) return;
endtype %= 5;
jointtype %= 4;
ClipperOffsetPoly(curves, distance * 2, jointtype, endtype, miter);
DA.SetDataList(0, holeCurves);
DA.SetDataList(1, outCurves);
}
List<Curve> holeCurves = new List<Curve>();
List<Curve> outCurves = new List<Curve>();
void ClipperOffsetPoly(List<Curve> curves, double distance, int jointype, int endtype, double miterLimit)
{
holeCurves.Clear();
outCurves.Clear();
//PathsD pathsD = Converter.ConvertPolylinesA1(curves);
var result = Converter.ConvertPolylinesA2(curves);
PathsD closedPaths = result.closedPathsD;
PathsD openedPaths = result.openedPathsD;
//offset closed curves
PathsD solutionDA = new PathsD();
solutionDA = Clipper.InflatePaths(closedPaths, distance, JoinTypes[jointype], EndTypes[1], miterLimit, precision);
// offset opened curves
PathsD solutionDB = new PathsD();
solutionDB = Clipper.InflatePaths(openedPaths, distance, JoinTypes[jointype], EndTypes[endtype], miterLimit, precision);
PathsD solutionD = Clipper.Union(solutionDA, solutionDB, FillRule.EvenOdd, precision);
foreach (PathD path in solutionD)
{
PathD newPath = new PathD();
foreach (PointD point in path)
{
newPath.Add(new PointD(point.x, -point.y));
}
Polyline polyline = new Polyline(path.Select(p => new Point3d(p.x, p.y, 0)));
polyline.Add(polyline[0]);
if (Clipper.IsPositive(path))
outCurves.Add(polyline.ToNurbsCurve());
else
holeCurves.Add(polyline.ToNurbsCurve());
pp.Add(path);
svgpath.Add(newPath);
}
}
void SaveFile(object sender, System.EventArgs e)
{
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
{
saveFileDialog.Filter = "BIN files (*.bin)|*.bin";
saveFileDialog.FilterIndex = 1;
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
filename = saveFileDialog.FileName;
Clipper2Lib.ClipperFileIO.SaveToBinFile(filename, pp);
}
}
}
List<JoinType> JoinTypes = new List<JoinType>()
{
JoinType.Miter,
JoinType.Square,
JoinType.Bevel,
JoinType.Round
};
List<EndType> EndTypes = new List<EndType>()
{
EndType.Butt,
EndType.Joined,
EndType.Square,
EndType.Round,
EndType.Polygon,
};
protected override System.Drawing.Bitmap Icon => Properties.Resources.clipper;
public override Guid ComponentGuid => new Guid("ECC627E4-F10E-4013-81D9-8278C4D5F8E1");
public override bool Read(GH_IReader reader)
{
if (reader.ItemExists("Precision"))
precision = reader.GetInt32("Precision");
if (reader.ItemExists("SVGWidth"))
svgWidth = reader.GetInt32("SVGWidth");
if (reader.ItemExists("SVGHeight"))
svgHeight = reader.GetInt32("SVGHeight");
return base.Read(reader);
}
public override bool Write(GH_IWriter writer)
{
writer.SetInt32("Precision", precision);
writer.SetInt32("SVGWidth", svgWidth);
writer.SetInt32("SVGHeight", svgHeight);
return base.Write(writer);
}
}
}