-
Notifications
You must be signed in to change notification settings - Fork 331
/
Program.cs
133 lines (119 loc) · 5.67 KB
/
Program.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
using System.Collections.Generic;
using System.Text;
using Rhino.Geometry;
using Rhino.Compute;
namespace CircleIntersection
{
class Program
{
static void Main(string[] args)
{
ComputeServer.WebAddress = "http://localhost:8081/";
// ComputeServer.ApiKey = "";
// Create a couple circles using a local copy of Rhino3dmIo
var c1 = new Circle(new Point3d(0, 0, 0), 100);
var c2 = new Circle(new Point3d(30, 30, 0), 70);
// Call compute to perform a curve boolean operation
var intersectionCurves = Rhino.Compute.CurveCompute.CreateBooleanIntersection(c1.ToNurbsCurve(), c2.ToNurbsCurve());
Mesh[] intersectionMeshes = null;
if (intersectionCurves != null)
{
// Use local Rhino3dm to create a Brep from the curves
var brep = Brep.CreateTrimmedPlane(c1.Plane, intersectionCurves);
// Call compute to mesh the Brep
intersectionMeshes = Rhino.Compute.MeshCompute.CreateFromBrep(brep, MeshingParameters.FastRenderMesh);
}
// just some helper routines to create an SVG file of the results so we can see what was generated
string path = "circle_intersection.svg";
WriteSvgFile(path, c1, c2, intersectionMeshes);
System.Diagnostics.Process.Start(path);
}
/// <summary>
/// Very basic SVG file creation routine to make an SVG the displays two circles and a mesh.
/// Kept simple just for this sample; there are much better SVG toolkits available that could
/// be used for general purpose routines.
/// </summary>
/// <param name="path"></param>
/// <param name="circle1"></param>
/// <param name="circle2"></param>
/// <param name="intersectionMeshes"></param>
static void WriteSvgFile(string path, Circle circle1, Circle circle2, Mesh[] intersectionMeshes)
{
var doc = new System.Xml.XmlDocument();
doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", "no"));
doc.XmlResolver = null;
doc.AppendChild(doc.CreateDocumentType("svg", "-//W3C//DTD SVG 1.1//EN", "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd", null));
var svgroot = doc.CreateElement("svg");
var bbox = circle1.BoundingBox;
bbox.Union(circle2.BoundingBox);
var length = (bbox.Max - bbox.Min).Length;
bbox.Inflate(length * 0.05);
AppendAttribute(doc, svgroot, "viewBox", $"{bbox.Min.X} {bbox.Min.Y} {bbox.Max.X - bbox.Min.X} {bbox.Max.Y - bbox.Min.Y}");
AppendAttribute(doc, svgroot, "version", "1.1");
AppendAttribute(doc, svgroot, "xmlns", "http://www.w3.org/2000/svg");
doc.AppendChild(svgroot);
if (intersectionMeshes != null)
{
foreach (var mesh in intersectionMeshes)
{
foreach (var face in mesh.Faces)
{
var elem = doc.CreateElement("polygon");
var attrib = doc.CreateAttribute("fill");
attrib.Value = "#008000"; // green
elem.Attributes.Append(attrib);
attrib = doc.CreateAttribute("stroke");
attrib.Value = "#008000"; // green
elem.Attributes.Append(attrib);
attrib = doc.CreateAttribute("points");
StringBuilder sb = new StringBuilder();
var points = new List<Rhino.Geometry.Point3d>();
points.Add(mesh.Vertices[face.A]);
points.Add(mesh.Vertices[face.B]);
points.Add(mesh.Vertices[face.C]);
if (face.IsQuad)
points.Add(mesh.Vertices[face.D]);
for (int i = 0; i < points.Count; i++)
{
if (i > 0)
sb.Append(", ");
sb.Append($"{points[i].X}, {points[i].Y}");
}
attrib.Value = sb.ToString().Trim();
elem.Attributes.Append(attrib);
svgroot.AppendChild(elem);
}
}
}
svgroot.AppendChild(SvgCircle(circle1, doc));
svgroot.AppendChild(SvgCircle(circle2, doc));
doc.Save(path);
}
static void AppendAttribute(System.Xml.XmlDocument doc, System.Xml.XmlElement element, string name, string value)
{
var attr = doc.CreateAttribute(name);
attr.Value = value;
element.Attributes.Append(attr);
}
static System.Xml.XmlElement SvgCircle(Rhino.Geometry.Circle c, System.Xml.XmlDocument doc)
{
var elem = doc.CreateElement("circle");
var attrib = doc.CreateAttribute("cx");
attrib.Value = $"{c.Center.X}";
elem.Attributes.Append(attrib);
attrib = doc.CreateAttribute("cy");
attrib.Value = $"{c.Center.Y}";
elem.Attributes.Append(attrib);
attrib = doc.CreateAttribute("r");
attrib.Value = $"{c.Radius}";
elem.Attributes.Append(attrib);
attrib = doc.CreateAttribute("fill-opacity");
attrib.Value = "0";
elem.Attributes.Append(attrib);
attrib = doc.CreateAttribute("stroke");
attrib.Value = "#000000";
elem.Attributes.Append(attrib);
return elem;
}
}
}