From 55c71e3060efae04b934ad263ebb1abd2775e0db Mon Sep 17 00:00:00 2001 From: Mishael Nuh Date: Mon, 30 Dec 2019 17:48:01 -0500 Subject: [PATCH 1/2] Fix issue with ConvexHull when vertice values are too small --- RodSteward/Model.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/RodSteward/Model.cs b/RodSteward/Model.cs index 9abab05..464d60d 100644 --- a/RodSteward/Model.cs +++ b/RodSteward/Model.cs @@ -355,12 +355,13 @@ public Dictionary> GenerateJointMeshes(bool label = true) { try { - var convHullRes = ConvexHull.Create(kvp.Value, Tolerance); + var scaling = Math.Floor(1000 / kvp.Value.SelectMany(p => p).Max()); + var convHullRes = ConvexHull.Create(kvp.Value.Select(p => p.Select(pi => pi * scaling).ToArray()).ToList()); var hullPoints = convHullRes.Result.Points.ToList(); var hullFaces = convHullRes.Result.Faces.ToList(); var newMesh = new Mesh(); - newMesh.Vertices.AddVertices(hullPoints.Select(p => new Point3d(p.Position[0], p.Position[1], p.Position[2]))); + newMesh.Vertices.AddVertices(hullPoints.Select(p => new Point3d(p.Position[0] / scaling, p.Position[1] / scaling, p.Position[2] / scaling))); newMesh.Faces.AddFaces(hullFaces.Select(f => new MeshFace(hullPoints.IndexOf(f.Vertices[0]), hullPoints.IndexOf(f.Vertices[1]), hullPoints.IndexOf(f.Vertices[2])))); newMesh.Normals.ComputeNormals(); newMesh.UnifyNormals(); From 42781b211407d47b7ae30adea61dfdc9cf69423a Mon Sep 17 00:00:00 2001 From: Mishael Nuh Date: Mon, 30 Dec 2019 17:57:06 -0500 Subject: [PATCH 2/2] Force output to be mm if input is in another unit. --- RodSteward/OutputJointSTL.cs | 26 +++++++++++++++++++++++-- RodSteward/OutputRodCutSVG.cs | 36 ++++++++++++++++++++++++++++------- 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/RodSteward/OutputJointSTL.cs b/RodSteward/OutputJointSTL.cs index 6f3f503..5ea853d 100644 --- a/RodSteward/OutputJointSTL.cs +++ b/RodSteward/OutputJointSTL.cs @@ -11,7 +11,7 @@ public class OutputJointSTL : GH_Component private string outputMessage = ""; public OutputJointSTL() : base("OutputJointSTL", "RSOutputJointSTL", - "Outputs joint mesh STLs to the target directory", + "Outputs joint mesh STLs in [mm] to the target directory", "RodSteward", "Output") { } @@ -58,6 +58,28 @@ protected override void SolveInstance(IGH_DataAccess DA) foreach (var file in dirFiles.Where(f => f.Contains("RS_Joint_Mesh_"))) File.Delete(file); + // Get unit scaling + var unit = Rhino.RhinoDoc.ActiveDoc.ModelUnitSystem; + var scale = 1.0f; + switch (unit) + { + case Rhino.UnitSystem.Millimeters: + scale = 1.0f; + break; + case Rhino.UnitSystem.Centimeters: + scale = 10; + break; + case Rhino.UnitSystem.Meters: + scale = 1000; + break; + case Rhino.UnitSystem.Inches: + scale = 25.4f; + break; + case Rhino.UnitSystem.Feet: + scale = 304.8f; + break; + } + int fileCounter = 0; foreach(var kvp in model.JointMeshes) @@ -67,7 +89,7 @@ protected override void SolveInstance(IGH_DataAccess DA) foreach(var m in kvp.Value) { - var vertices = m.Vertices.Select(v => new StlVertex(v.X, v.Y, v.Z)).ToList(); + var vertices = m.Vertices.Select(v => new StlVertex(v.X * scale, v.Y * scale, v.Z * scale)).ToList(); var faces = m.Faces; var normals = m.FaceNormals.Select(n => new StlNormal(n.X, n.Y, n.Z)).ToList(); diff --git a/RodSteward/OutputRodCutSVG.cs b/RodSteward/OutputRodCutSVG.cs index 02cde45..be226eb 100644 --- a/RodSteward/OutputRodCutSVG.cs +++ b/RodSteward/OutputRodCutSVG.cs @@ -18,7 +18,7 @@ public class OutputRodCutSVG : GH_Component public OutputRodCutSVG() : base("OutputRodCutSVG", "RSOutputRodCutSVG", - "Outputs rod lengths into rod cutting plan as SVG", + "Outputs rod lengths into rod cutting plan as SVG in [mm]", "RodSteward", "Output") { } @@ -108,10 +108,32 @@ protected override void SolveInstance(IGH_DataAccess DA) foreach (var file in dirFiles.Where(f => f.Contains("RS_Laser_Cut_Plan"))) File.Delete(file); + // Get unit scaling + var unit = Rhino.RhinoDoc.ActiveDoc.ModelUnitSystem; + var scale = 1.0f; + switch (unit) + { + case Rhino.UnitSystem.Millimeters: + scale = 1.0f; + break; + case Rhino.UnitSystem.Centimeters: + scale = 10; + break; + case Rhino.UnitSystem.Meters: + scale = 1000; + break; + case Rhino.UnitSystem.Inches: + scale = 25.4f; + break; + case Rhino.UnitSystem.Feet: + scale = 304.8f; + break; + } + var svgFile = new SvgDocument() { - Width = new SvgUnit(SvgUnitType.None, (float)(stockLength + DOC_PADDING * 2)), - Height = new SvgUnit(SvgUnitType.None, (float)(model.Radius * 2 * binLengths.Count() + ROD_PADDING * (binLengths.Count() - 1) + DOC_PADDING * 2)), + Width = new SvgUnit(SvgUnitType.None, (float)(stockLength * scale + DOC_PADDING * 2)), + Height = new SvgUnit(SvgUnitType.None, (float)(model.Radius * 2 * binLengths.Count() * scale + ROD_PADDING * (binLengths.Count() - 1) + DOC_PADDING * 2)), }; svgFile.ViewBox = new SvgViewBox(0, 0, svgFile.Width, svgFile.Height); @@ -124,24 +146,24 @@ protected override void SolveInstance(IGH_DataAccess DA) StartX = (float)(offset_x), EndX = (float)(offset_x), StartY = (float)(offset_y), - EndY = (float)(offset_y + model.Radius * 2), + EndY = (float)(offset_y + model.Radius * 2 * scale), Stroke = new SvgColourServer(System.Drawing.Color.Red), StrokeWidth = 2, }); foreach (var l in b) { - offset_x += (float)l; + offset_x += (float)l * scale; svgFile.Children.Add(new SvgLine() { StartX = (float)(offset_x), EndX = (float)(offset_x), StartY = (float)(offset_y), - EndY = (float)(offset_y + model.Radius * 2), + EndY = (float)(offset_y + model.Radius * 2 * scale), Stroke = new SvgColourServer(System.Drawing.Color.Red), StrokeWidth = 2, }); } - offset_y += model.Radius * 2 + ROD_PADDING; + offset_y += model.Radius * 2 * scale + ROD_PADDING; } using (FileStream fs = File.Create(Path.Combine(dir, "RS_Laser_Cut_Plan.svg")))