Adding scripts to a job?

Here's the place for discussion related to CAM/CNC and the development of the Path module.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
WayofWood
Posts: 62
Joined: Mon Nov 26, 2018 4:59 pm

Adding scripts to a job?

Post by WayofWood »

Working mainly with wood I am running here and there into situations where it would be super helpful to produce some specific gcode based on a few parameters. Creating a keyhole is such an example:

Image

You need to select the right tool, move down, forward, backward and up again.

In this thread I found some code to produce an operation and the respective gcode:

Code: Select all

import FreeCAD
import Path
import Part
from FreeCAD import Base

keyhole_depth = 10
keyhole_length = 25

V1 = Base.Vector(0, 0, 0)
V2 = Base.Vector(0, 0, -keyhole_depth)
V3 = Base.Vector(0, keyhole_length, -keyhole_depth)

edge1 = Part.makeLine(V1, V2)
edge2 = Part.makeLine(V2, V3)
edge3 = Part.makeLine(V3, V2)
edge4 = Part.makeLine(V2, V1)
wire = Part.Wire([edge1, edge2, edge3, edge4]) 
shape = Part.Shape([wire])

o = FreeCAD.ActiveDocument.addObject("Path::Feature","KeyHolePath")
o.Path = Path.fromShapes(wire, sort_mode=0, min_dist=1.0, abscissa=3.0, nearest_k=3, orientation=0, direction=0, threshold=2.0, retract_axis=2, retraction=70.0, resume_height=70.0, segmentation=0.0, feedrate=800.0, feedrate_v=400.0, deflection=0.01)
FreeCAD.ActiveDocument.recompute()
p =  o.Path
print(p.toGCode())
Is there a way to attach this (or a similar code) to a job? I tried to create a custom command on the GUI and then set the resulting gcode but somehow did not find a good way how to hand over gcode to the custom object.

Any help or pointers appreciated!
chrisb
Veteran
Posts: 53785
Joined: Tue Mar 17, 2015 9:14 am

Re: Adding scripts to a job?

Post by chrisb »

I like the idea of G-code macros as sort of canned cycles.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
spanner888
Posts: 325
Joined: Tue May 28, 2019 10:51 am

Re: Adding scripts to a job?

Post by spanner888 »

Simple approach is just take gcode output from running your macro, then manually copy it to Path - Supplemental Commands - Custom
bmsaus4ax
Posts: 250
Joined: Sat Nov 14, 2020 9:16 pm
Location: Bargara, Queensland, Australia UTC+10

Re: Adding scripts to a job?

Post by bmsaus4ax »

WayofWood wrote: Tue Jan 18, 2022 5:36 am
Is there a way to attach this (or a similar code) to a job? I tried to create a custom command on the GUI and then set the resulting gcode but somehow did not find a good way how to hand over gcode to the custom object.

Any help or pointers appreciated!
If memory serves me correctly there is a method which may do this in the macro in this post and the discussion around it
https://forum.freecadweb.org/viewtopic. ... 84#p503984
The whole macro will not apply, but the section on creating and filling the Custom Op was something I thought would be ideal for my macro for another project, but I never got back to progressing it.

This is the code block I was thinking of

Code: Select all

    def _createCustom(self, parentJob=None):
        """_createCustom(parentJob=None) ...
        This method returns a PathCustom operation with additional properties
        to contain details about tile location, size, and order."""
        # FreeCAD.Console.PrintMessage("_createCustom()\n")
        if FreeCAD.GuiUp:
            res = PathOpGui.CommandResources(
                    'Custom',
                    PathCustom.Create,
                    PathCustomGui.TaskPanelOpPage,
                    'Path_Custom',
                    PySide.QtCore.QT_TRANSLATE_NOOP("Path_Custom", "Custom"),
                    PySide.QtCore.QT_TRANSLATE_NOOP("Path_Custom", "Create custom gcode snippet"),
                    PathCustom.SetupProperties)
            if not self.useJobGui:
                res.editMode = 5  # No initial Task Panel setup
            if parentJob:
                res.parentJob = parentJob
            op = PathOpGui.Create(res)
        else:
            op = PathCustom.Create('Custom', obj=None)
        op.addProperty("App::PropertyVectorDistance", "ShiftVector", "Tile",
                        PySide.QtCore.QT_TRANSLATE_NOOP("App::Property", "The shift vector applied to the model before sampling the paths for this tile."))
        op.addProperty("App::PropertyVectorDistance", "TileSize", "Tile",
                        PySide.QtCore.QT_TRANSLATE_NOOP("App::Property", "The dimensions of this tile."))
        op.addProperty("App::PropertyString", "SourceOperation", "Tile",
                        PySide.QtCore.QT_TRANSLATE_NOOP("App::Property", "The tile grid ID as X and Y values, regardless of processing direction."))
        op.addProperty("App::PropertyString", "TileDetails", "Tile",
                        PySide.QtCore.QT_TRANSLATE_NOOP("App::Property", "The tile grid ID as X and Y values, regardless of processing direction."))
        op.recompute()
        return op
I have continued to use the process referred to by "spanner888" i.e., copy and paste gcode result into a Custom op manually.
User avatar
WayofWood
Posts: 62
Joined: Mon Nov 26, 2018 4:59 pm

Re: Adding scripts to a job?

Post by WayofWood »

bmsaus4ax wrote: Tue Jan 18, 2022 11:09 am This is the code block I was thinking of
I can't test it right now but it looks very promising. Thanks a lot!
User avatar
WayofWood
Posts: 62
Joined: Mon Nov 26, 2018 4:59 pm

Re: Adding scripts to a job?

Post by WayofWood »

chrisb wrote: Tue Jan 18, 2022 9:24 am I like the idea of G-code macros as sort of canned cycles.
Let me brainstorm an idea of what would be an ideal solution for quite a few problems:

We would have something like a GenericPathGenerator that would accept the following parameters:
  • A set of shapes
  • A propertybag of generic parameters
  • The toolbit to be used
  • Switch if all shapes or each shape is presented
  • G-Code supporting template-toolkit or similar syntax
Implementing my keyhole feature would then be quite simple:
  • Add a toolbit.
  • Add some circles as shapes
  • Add keyholedepth and keyholelength to the propertybag
  • Add a script

    Code: Select all

    G1 F[%toolbit.VertFeed%] [% shape.CenterOfMass %] 
    G1 F[%toolbit.VertFeed%] [% shape.CenterOfMass.x %] [% shape.CenterOfMass.y %] [% shape.CenterOfMass.z - keyholedepth %]
    G1 F[%toolbit.HorizFeed%] [% shape.CenterOfMass.x %] [% shape.CenterOfMass.y + keyholelength %] [% shape.CenterOfMass.z - keyholedepth %]
    G1 F[%toolbit.HorizFeed%] [% shape.CenterOfMass.x %] [% shape.CenterOfMass.y %] [% shape.CenterOfMass.z - keyholedepth %]
    G1 F[%toolbit.VertFeed%] [% shape.CenterOfMass %] 
    
What do you think - would such a thing be useful for others than myself?
User avatar
onekk
Veteran
Posts: 6094
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Adding scripts to a job?

Post by onekk »

WayofWood wrote: Tue Jan 18, 2022 2:36 pm Let me brainstorm an idea of what would be an ideal solution for quite a few problems:

We would have something like a GenericPathGenerator that would accept the following parameters:
  • A set of shapes
  • A propertybag of generic parameters
  • The toolbit to be used
  • Switch if all shapes or each shape is presented
  • G-Code supporting template-toolkit or similar syntax
Implementing my keyhole feature would then be quite simple:
  • Add a toolbit.
  • Add some circles as shapes
  • Add keyholedepth and keyholelength to the propertybag
  • Add a script

    Code: Select all

    G1 F[%toolbit.VertFeed%] [% shape.CenterOfMass %] 
    G1 F[%toolbit.VertFeed%] [% shape.CenterOfMass.x %] [% shape.CenterOfMass.y %] [% shape.CenterOfMass.z - keyholedepth %]
    G1 F[%toolbit.HorizFeed%] [% shape.CenterOfMass.x %] [% shape.CenterOfMass.y + keyholelength %] [% shape.CenterOfMass.z - keyholedepth %]
    G1 F[%toolbit.HorizFeed%] [% shape.CenterOfMass.x %] [% shape.CenterOfMass.y %] [% shape.CenterOfMass.z - keyholedepth %]
    G1 F[%toolbit.VertFeed%] [% shape.CenterOfMass %] 
    
What do you think - would such a thing be useful for others than myself?
The main concern is the way job and operations are actually made.

You could generate operation, even shapes, but you have to code many things, sometimes, it will be less a problem to directly code operation in a script, that will create all the things without having to fiddle with expression and Spreadsheets.

If you have to code something it is more easy to start from scratch.

It is not difficult to create a script that will create:

- A Job
. A toolcontroller
- Profile and pocketing ops
- and also produce G-Code.

This is roughly an example, with some concepts, developed in a script.

https://forum.freecadweb.org/viewtopic. ... 48#p554648

Obviously it lacks of the "GUI" part that could be easily done creating an input dialog where you have to put your desired value, or even to retrieve the data from a Spreadsheet. Sorry but I haven't used Spreadsheets, so I don't know the related code to eventually create one.

Take in account that "direct Path creation" in other word the use of Path.fromShapes is considered not the most easy way to go, as this comment from Russ4262 seems to tell https://forum.freecadweb.org/viewtopic. ... 19#p559319.

Around there are some other experiments, (mostly done by me) there are also some old code in German forum, but as Russ4262 kindly have modified the Path code, following some discussion I have had with him, I think that the most "recent way of doing things" is the code in first link, as it uses 0.20, (Maybe even 0.19.3) Path code modification.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
sliptonic
Veteran
Posts: 3453
Joined: Tue Oct 25, 2011 10:46 pm
Location: Columbia, Missouri
Contact:

Re: Adding scripts to a job?

Post by sliptonic »

WayofWood wrote: Tue Jan 18, 2022 5:36 am Working mainly with wood I am running here and there into situations where it would be super helpful to produce some specific gcode based on a few parameters. Creating a keyhole is such an example:
Great use-case! How many more of these do you have? If you've got dozens and new unique ones all the time, then maybe a generic script-y sort of solution is needed. But keyhole is pretty common. I've used it myself and hand-coded the gcode. So my first thought be a new operation type.
User avatar
sliptonic
Veteran
Posts: 3453
Joined: Tue Oct 25, 2011 10:46 pm
Location: Columbia, Missouri
Contact:

Re: Adding scripts to a job?

Post by sliptonic »

By the way, a keyhole cutter is a tool shape that we don't have. A new operation/generator could check if that a proper tool is being used.
User avatar
WayofWood
Posts: 62
Joined: Mon Nov 26, 2018 4:59 pm

Re: Adding scripts to a job?

Post by WayofWood »

sliptonic wrote: Tue Jan 18, 2022 4:30 pm
WayofWood wrote: Tue Jan 18, 2022 5:36 am Working mainly with wood I am running here and there into situations where it would be super helpful to produce some specific gcode based on a few parameters. Creating a keyhole is such an example:
Great use-case! How many more of these do you have? If you've got dozens and new unique ones all the time, then maybe a generic script-y sort of solution is needed. But keyhole is pretty common. I've used it myself and hand-coded the gcode. So my first thought be a new operation type.
It's maybe rather 4-5 than 20-30. Most likely a new operation type would be the cleanest way to go. How would you go about implementing that feature in the Path Design philosophy? Most of the Path features are based on an actual geometric model (except the Deburr operation).
Bildschirmfoto vom 2022-01-18 18-25-53.png
Bildschirmfoto vom 2022-01-18 18-25-53.png (15.88 KiB) Viewed 2841 times
For a keyhole it would be a bit more difficult to select the right elements:

(1) The easy mode would be to just select a circle as a proxy and then provide the direction of the keyhole (left hand side)
(2) Selecting the entry circle and the bottom would make it possible to create a keyhole in line with the actual model but one could not create multiple keyholes in one go
(3) The most elegant way would be to only select the half-circle at the bottom of the model and the operation would figure out the direction of the keyhole on it's own. However while I am not sure if my limited development skills are sufficient to tackle (1) or (2) this approach would definitely be beyond my ability ;-)
Post Reply