Skip to content

Commit

Permalink
Changes in g-code toolpath
Browse files Browse the repository at this point in the history
  • Loading branch information
visose committed Jan 16, 2025
1 parent d6103b5 commit d8cb259
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Product>Extensions</Product>
<Version>1.0.5</Version>
<Version>1.0.6</Version>
<Authors>Extensions Authors</Authors>
<Description>Assorted components for Grasshopper.</Description>
<PackageTags>Grasshopper;Extensions;Robots</PackageTags>
Expand Down
7 changes: 5 additions & 2 deletions src/Extensions.Grasshopper/Toolpaths/GCodeToolpath.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Grasshopper.Kernel;
using Grasshopper.Kernel;
using Rhino.Geometry;
using Robots;
using Robots.Grasshopper;
Expand Down Expand Up @@ -29,6 +29,7 @@ void Inputs(GH_InputParamManager pManager)
pManager.AddTextParameter("File", "F", "GCode file.", GH_ParamAccess.item);
pManager.AddParameter(new TargetParameter(), "Target", "T", "Created targets will use the parameters of this reference target if they're not supplied in the G-code file.", GH_ParamAccess.item);
pManager.AddPointParameter("Point alignment", "P", "Aligns the X axis of the tagets to look towards this point (in world coordinate system). If not supplied it will use the X axis of the reference target.", GH_ParamAccess.item);
pManager.AddBooleanParameter("Add bit", "A", "Add drill bit to tool.", GH_ParamAccess.item);
pManager[2].Optional = true;
}

Expand All @@ -46,16 +47,18 @@ protected override void SolveInstance(IGH_DataAccess DA)
string file = null;
GH_Target target = null;
Point3d? point = null;
bool addBit = false;

if (!DA.GetData(0, ref file)) return;
if (!DA.GetData(1, ref target)) return;
DA.GetData(2, ref point);
if (!DA.GetData(3, ref addBit)) return;

var alignment = Vector3d.XAxis;
if (point.HasValue)
alignment = (Vector3d)point.Value;

var toolpath = new Toolpaths.Milling.GCodeToolpath(file, target.Value as CartesianTarget, alignment);
var toolpath = new Toolpaths.Milling.GCodeToolpath(file, target.Value as CartesianTarget, alignment, addBit);
var (tool, mcs, rapidStarts, ignored) = toolpath.Toolpath;

DA.SetData(0, toolpath);
Expand Down
36 changes: 22 additions & 14 deletions src/Extensions/Toolpaths/Milling/GCodeToolpath.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Rhino.Geometry;
using Rhino.Geometry;
using Robots;
using gs;
using static Extensions.GeometryUtil;
Expand All @@ -9,14 +9,14 @@ public class GCodeToolpath : SimpleToolpath
{
public FiveAxisToRobots Toolpath { get; set; }

public GCodeToolpath(string file, CartesianTarget referenceTarget, Vector3d alignment)
public GCodeToolpath(string file, CartesianTarget referenceTarget, Vector3d alignment, bool addBit)
{
using var reader = File.OpenText(file);

var parser = new GenericGCodeParser();
var code = parser.Parse(reader);

Toolpath = new FiveAxisToRobots(referenceTarget, alignment, code);
Toolpath = new FiveAxisToRobots(referenceTarget, alignment, code, addBit);
_targets = Toolpath.Targets;
}
}
Expand All @@ -43,25 +43,33 @@ public void Deconstruct(out Tool tool, out Frame mcs, out List<int> rapidStarts,
ignored = _ignored;
}

internal FiveAxisToRobots(CartesianTarget refTarget, Vector3d alignment, GCodeFile file)
internal FiveAxisToRobots(CartesianTarget refTarget, Vector3d alignment, GCodeFile file, bool addBit)
{
_refTarget = refTarget;
_alignment = alignment;

var constructionPlane = Rhino.RhinoDoc.ActiveDoc.Views.ActiveView.ActiveViewport.GetConstructionPlane().Plane;
constructionPlane.Origin = Point3d.Origin;
var workPlane = _refTarget.Frame.Plane;
var xform = Transform.PlaneToPlane(Plane.WorldXY, workPlane);
constructionPlane.Transform(xform);
//var xform = Transform.PlaneToPlane(Plane.WorldXY, workPlane);
//var constructionPlane = Rhino.RhinoDoc.ActiveDoc.Views.ActiveView.ActiveViewport.GetConstructionPlane().Plane;
//constructionPlane.Origin = Point3d.Origin;
//constructionPlane.Transform(xform);

_mcs = new Frame(plane: constructionPlane, name: "MCS");
_mcs = new Frame(plane: workPlane, name: "MCS");

_gCodeMap = new Dictionary<(GCodeLine.LType letter, int number), Action<GCodeLine>>
{
{ (GCodeLine.LType.GCode, 0), RapidMove },
{ (GCodeLine.LType.GCode, 1), LinearMove},
{ (GCodeLine.LType.MCode, 6), ToolSet },
};
{
{ (GCodeLine.LType.GCode, 0), RapidMove },
{ (GCodeLine.LType.GCode, 1), LinearMove}
};

if (addBit)
{
_gCodeMap.Add((GCodeLine.LType.MCode, 6), ToolSet);
}
else
{
_tool = _refTarget.Tool;
}

Interpret(file);
_rapidStarts.Add(Targets.Count - 1);
Expand Down

0 comments on commit d8cb259

Please sign in to comment.