Skip to content

Commit efb34b6

Browse files
authored
Merge pull request #32 from moethu/develop
Writing Skp files from GH
2 parents 5077730 + d6adbf3 commit efb34b6

2 files changed

Lines changed: 167 additions & 5 deletions

File tree

SketchUpForGrasshopper/SketchUp.cs

Lines changed: 167 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace SketchUpForGrasshopper
4242
/// </summary>
4343
public class SketchUpModel : GH_Component
4444
{
45-
public SketchUpModel() : base("Load SketchUp Model", "Load SketchUp Model", "Loads a SketchUp Model from file", "SketchUpSharp", "Model") { }
45+
public SketchUpModel() : base("Load SketchUp Model", "Load SketchUp Model", "Loads a SketchUp Model from file", "GrassUp", "Model") { }
4646

4747
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
4848
{
@@ -106,12 +106,62 @@ protected override Bitmap Internal_Icon_24x24
106106
}
107107
}
108108

109+
/// <summary>
110+
/// Save SketchUp Model Component
111+
/// </summary>
112+
public class SaveSketchUpModel : GH_Component
113+
{
114+
public SaveSketchUpModel() : base("Save SketchUp Model", "Save SketchUp Model", "Save a SketchUp Model to file", "GrassUp", "Model") { }
115+
116+
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
117+
{
118+
pManager.AddTextParameter("Path", "P", "Path to Sketchup File (skp)", GH_ParamAccess.item);
119+
int a = pManager.AddCurveParameter("Curves", "C", "Curves", GH_ParamAccess.list);
120+
int b = pManager.AddSurfaceParameter("Surfaces", "S", "Surfaces", GH_ParamAccess.list);
121+
pManager[a].Optional = true;
122+
pManager[b].Optional = true;
123+
}
124+
125+
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
126+
{
127+
128+
}
129+
130+
protected override void SolveInstance(IGH_DataAccess DA)
131+
{
132+
GH_String path = new GH_String();
133+
DA.GetData<GH_String>(0, ref path);
134+
135+
List<GH_Surface> surfaces = new List<GH_Surface>();
136+
DA.GetDataList<GH_Surface>(1, surfaces);
137+
List<GH_Curve> curves = new List<GH_Curve>();
138+
DA.GetDataList<GH_Curve>(2, curves);
139+
140+
Geometry.WriteModel(path.Value, surfaces, curves, false);
141+
}
142+
143+
public override Guid ComponentGuid
144+
{
145+
get
146+
{
147+
return new Guid("{5ea8ce3d-d262-4d7f-a733-1573beeb4b5d}");
148+
}
149+
}
150+
protected override Bitmap Internal_Icon_24x24
151+
{
152+
get
153+
{
154+
return Properties.Resources.Skp;
155+
}
156+
}
157+
}
158+
109159
/// <summary>
110160
/// Decomposes a SketchUp Model Instance
111161
/// </summary>
112162
public class SketchUpInstance : GH_Component
113163
{
114-
public SketchUpInstance() : base("Decompose SketchUp Instance", "Decompose SketchUp Instance", "Decomposes a SketchUp Instance", "SketchUpSharp", "Elements") { }
164+
public SketchUpInstance() : base("Decompose SketchUp Instance", "Decompose SketchUp Instance", "Decomposes a SketchUp Instance", "GrassUp", "Elements") { }
115165

116166
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
117167
{
@@ -126,6 +176,7 @@ protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager
126176
pManager.AddBrepParameter("Surfaces", "Sf", "Surfaces", GH_ParamAccess.list);
127177
pManager.AddTextParameter("Parent Name", "PN", "Parent Name", GH_ParamAccess.item);
128178
pManager.AddBrepParameter("Inner", "I", "Inner", GH_ParamAccess.list);
179+
pManager.AddCurveParameter("Curves", "C", "Curves", GH_ParamAccess.list);
129180
}
130181

131182
protected override void SolveInstance(IGH_DataAccess DA)
@@ -139,6 +190,7 @@ protected override void SolveInstance(IGH_DataAccess DA)
139190

140191
List<GH_Brep> surfaces = new List<GH_Brep>();
141192
List<GH_Brep> inner = new List<GH_Brep>();
193+
List<GH_Curve> curves = new List<GH_Curve>();
142194

143195
GH_String parentName = new GH_String("");
144196
SketchUpNET.Component parentComponent = i.Parent as Component;
@@ -152,6 +204,9 @@ protected override void SolveInstance(IGH_DataAccess DA)
152204
surfaces.Add(new GH_Brep(brep));
153205
}
154206
}
207+
208+
foreach (Edge c in parentComponent.Edges)
209+
curves.Add(new GH_Curve(c.ToRhinoGeo().ToNurbsCurve()));
155210
}
156211

157212
DA.SetData(0, location);
@@ -160,6 +215,7 @@ protected override void SolveInstance(IGH_DataAccess DA)
160215
DA.SetDataList(3, surfaces);
161216
DA.SetData(4, parentName);
162217
DA.SetDataList(5, inner);
218+
DA.SetDataList(6, curves);
163219
}
164220

165221
public override Guid ComponentGuid
@@ -183,6 +239,8 @@ protected override Bitmap Internal_Icon_24x24
183239
/// </summary>
184240
public static class Geometry
185241
{
242+
public static string DefaultLayer = "Default";
243+
186244
/// <summary>
187245
/// Converts a SketchUp Vertex to a Rhino Point
188246
/// </summary>
@@ -197,6 +255,22 @@ public static Rhino.Geometry.Point3d ToRhinoGeo(this SketchUpNET.Vertex v, Trans
197255
}
198256
}
199257

258+
/// <summary>
259+
/// Converts a Rhino Point to a SketchUp Point
260+
/// </summary>
261+
public static SketchUpNET.Vertex ToSkpGeo(this Rhino.Geometry.Point3d v)
262+
{
263+
return new SketchUpNET.Vertex(v.X, v.Y, v.Z);
264+
}
265+
266+
/// <summary>
267+
/// Converts a Rhino Vector to a SketchUp Vector
268+
/// </summary>
269+
public static SketchUpNET.Vector ToSkpGeo(this Rhino.Geometry.Vector3d v)
270+
{
271+
return new SketchUpNET.Vector(v.X, v.Y, v.Z);
272+
}
273+
200274
/// <summary>
201275
/// Converts a SketchUp Vector to a Rhino Vector
202276
/// </summary>
@@ -213,17 +287,71 @@ public static Rhino.Geometry.Line ToRhinoGeo(this SketchUpNET.Edge v, Transform
213287
return new Rhino.Geometry.Line(v.Start.ToRhinoGeo(t), v.End.ToRhinoGeo(t));
214288
}
215289

290+
/// <summary>
291+
/// Converts a Rhino Line to a SketchUp Edge
292+
/// </summary>
293+
public static SketchUpNET.Edge ToSkpGeo(this Rhino.Geometry.Line v)
294+
{
295+
return new SketchUpNET.Edge(v.PointAt(0).ToSkpGeo(), v.PointAt(1.0).ToSkpGeo(), DefaultLayer);
296+
}
297+
298+
299+
/// <summary>
300+
/// Converts a Rhino Curve to a SketchUp Curve
301+
/// </summary>
302+
public static SketchUpNET.Curve ToSkpGeo(this Rhino.Geometry.Curve v)
303+
{
304+
List<Edge> edges = new List<Edge>();
305+
if (v.IsLinear())
306+
{
307+
edges.Add(new Edge(v.PointAt(0).ToSkpGeo(), v.PointAt(1.0).ToSkpGeo(), DefaultLayer));
308+
}
309+
else
310+
{
311+
for (double i = 0; i < 1.0; i = i + 0.1)
312+
{
313+
edges.Add(new Edge(v.PointAt(i).ToSkpGeo(), v.PointAt(i + 0.1).ToSkpGeo(), DefaultLayer));
314+
}
315+
}
316+
return new SketchUpNET.Curve(edges,v.IsArc());
317+
}
318+
319+
/// <summary>
320+
/// Converts a Rhino Surface to a SketchUp Surface
321+
/// </summary>
322+
public static SketchUpNET.Surface ToSkpGeo(this Rhino.Geometry.Brep surface)
323+
{
324+
Surface srf = new Surface();
325+
srf.Vertices = new List<Vertex>();
326+
327+
foreach (var curve in surface.Edges)
328+
{
329+
if (curve.IsLinear())
330+
{
331+
srf.Vertices.Add(curve.PointAt(0.0).ToSkpGeo());
332+
}
333+
else
334+
{
335+
for (double i = 0; i < 1.0; i = i + 0.1)
336+
{
337+
srf.Vertices.Add(curve.PointAt(i).ToSkpGeo());
338+
}
339+
}
340+
}
341+
return srf;
342+
}
343+
216344
/// <summary>
217345
/// Converts a SketchUp Surface to a Rhino Surface
218346
/// </summary>
219347
public static Rhino.Geometry.Brep[] ToRhinoGeo(this SketchUpNET.Surface v, Transform t = null)
220348
{
221349
List<Rhino.Geometry.Curve> curves = new List<Rhino.Geometry.Curve>();
222-
350+
var tol = Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance;
223351
foreach (SketchUpNET.Edge c in v.OuterEdges.Edges)
224352
curves.Add(c.ToRhinoGeo(t).ToNurbsCurve());
225353

226-
Rhino.Geometry.Brep[] b = Rhino.Geometry.Brep.CreatePlanarBreps(curves,0.1);
354+
Rhino.Geometry.Brep[] b = Rhino.Geometry.Brep.CreatePlanarBreps(curves,tol);
227355
if (b == null) return new Rhino.Geometry.Brep[] { };
228356

229357
List<Rhino.Geometry.Brep> breps = v.InnerLoops(t);
@@ -243,12 +371,13 @@ public static Rhino.Geometry.Brep[] ToRhinoGeo(this SketchUpNET.Surface v, Trans
243371
public static List<Rhino.Geometry.Brep> InnerLoops(this SketchUpNET.Surface v, Transform t = null)
244372
{
245373
List<Rhino.Geometry.Brep> breps = new List<Rhino.Geometry.Brep>();
374+
var tol = Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance;
246375

247376
foreach (Loop loop in v.InnerEdges)
248377
{
249378
List<Rhino.Geometry.Curve> curves = new List<Rhino.Geometry.Curve>();
250379
foreach (SketchUpNET.Edge c in loop.Edges) curves.Add(c.ToRhinoGeo(t).ToNurbsCurve());
251-
Rhino.Geometry.Brep[] b = Rhino.Geometry.Brep.CreatePlanarBreps(curves, 0.1);
380+
Rhino.Geometry.Brep[] b = Rhino.Geometry.Brep.CreatePlanarBreps(curves,tol);
252381
if (b != null)
253382
{
254383
foreach (var brep in b)
@@ -257,5 +386,38 @@ public static Rhino.Geometry.Brep[] ToRhinoGeo(this SketchUpNET.Surface v, Trans
257386
}
258387
return breps;
259388
}
389+
390+
public static void WriteModel(string path, List<GH_Surface> surfaces = null, List<GH_Curve> curves = null, bool append = false)
391+
{
392+
SketchUpNET.SketchUp skp = new SketchUpNET.SketchUp();
393+
skp.Surfaces = new List<Surface>();
394+
skp.Edges = new List<Edge>();
395+
skp.Curves = new List<Curve>();
396+
397+
if (curves != null)
398+
foreach (var c in curves)
399+
{
400+
var curve = c.Value;
401+
if (curve.IsLinear())
402+
{
403+
var line = new SketchUpNET.Edge(curve.PointAt(0).ToSkpGeo(), curve.PointAt(1.0).ToSkpGeo(), DefaultLayer);
404+
skp.Edges.Add(line);
405+
}
406+
else
407+
{
408+
skp.Curves.Add(curve.ToSkpGeo());
409+
}
410+
}
411+
412+
if (surfaces != null)
413+
foreach (var surface in surfaces)
414+
skp.Surfaces.Add(surface.Value.ToSkpGeo());
415+
416+
if (System.IO.File.Exists(path) && append)
417+
skp.AppendToModel(path);
418+
else
419+
skp.WriteNewModel(path);
420+
}
421+
260422
}
261423
}

Testfiles/TestDefinition.gh

-124 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)