Skip to content

Commit

Permalink
Merge pull request #6 from mishaelnuh/dev
Browse files Browse the repository at this point in the history
Fix scaling when using other units
  • Loading branch information
mishaelnuh authored Dec 30, 2019
2 parents be38bdd + 42781b2 commit c339bbf
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
5 changes: 3 additions & 2 deletions RodSteward/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,13 @@ public Dictionary<int, List<Mesh>> 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();
Expand Down
26 changes: 24 additions & 2 deletions RodSteward/OutputJointSTL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
{
}
Expand Down Expand Up @@ -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)
Expand All @@ -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();

Expand Down
36 changes: 29 additions & 7 deletions RodSteward/OutputRodCutSVG.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
{
}
Expand Down Expand Up @@ -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);

Expand All @@ -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")))
Expand Down

0 comments on commit c339bbf

Please sign in to comment.