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
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 6:52 pm 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).
Exactly there are a number of features that appear in the real-world part that aren't worth modelling in CAD. We want to deburr an edge to remove the sharp stuff but it probably isn't worth adding a chamfer just so we can select it to make a deburr operation. So we just select the edges.

A keyhole might be similar.

I would implement this from the bottom up:

First write a generator. This is a python module in the Generators/ subdirectory. It will have a function called generate() that takes all inputs required to calculate a valid keyhole toolpath. It will return a list of path commands. The generator does not need to add feed rate to the commands, that will be added later. Just brainstorming, this generator needs to know the center position of the 'hole', depth, which direction to cut laterally, and distance.

That can probably all be simplified into a startPoint, and endPoint.

The generate() function can assume the machine is already at the startPoint. It should calculate the toolpath moving down, forward, back, and rapid up.

This generator will be quite simple. It's meant to be. So we'll also write a set of unit tests in the PathTests/ directory to thoroughly test it. I would make just that the first Pull Request. We now have a useful function that can be used in a macro and implemented into a full operation.

Next would be the operation. There's a lot of boilerplate code we can use. There might be some unique stuff depending on what you want the user to select or input. Regardless, it will be the operation's job to get the user input, verify correctness, call the generator, add the feed rates, and store the results.

Start with step one. I'll help as needed.
chrisb
Veteran
Posts: 53785
Joined: Tue Mar 17, 2015 9:14 am

Re: Adding scripts to a job?

Post by chrisb »

To me this looks like a slot with a special start point and a special return path.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
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 »

chrisb wrote: Tue Jan 18, 2022 9:24 pm To me this looks like a slot with a special start point and a special return path.
I wouldn't try to combine them. A slot never retracts over it's previous path and a keyhole always does. A keyhole always feeds vertically on entry, a slot may or may not. Enough different that they should be different things, at least at the generator level
User avatar
onekk
Veteran
Posts: 6094
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Adding scripts to a job?

Post by onekk »

It is not clear what the cutter is doing.

Keyholes are usually made by two circles and two lines.

if you think of the key cylinder.

The cutter you have shown seem to have a big diameter and a smaller diameter cutting edge.

So I suppose that it will machine the big hole, step down, and cut the smaller part.

It remains to set the starting point, if the working is as above maybe the center of the bug circle will be a viable starting point?

@sliptonic

I've roughly catch the generator methods working.

no need to model an operation, maybe adding a "special operation" marked as script and make come conventional text, or even putting directly a GCode on it will be easy.

you will add the new operation and choose a Macro that will generate the code, but the file will "retain" only the generated GCode (no need to have all the Macro Code in the FCStd file).

I have seen some CAM that even use a macro language like move(x, y, z) with some variable capabilities.

so you vould even code a script that will create based on the starting point the path.

So if the script have to cut the keyhole you have to code maybe

Code: Select all

a = mem(pos)
g1(z -10)
g1(y -20)
g0(a.x, a.y)
g0(a.z)

a sort of macro language with some memory on it.

maybe adfing a circle() that will calculate a toolpath using proper tool diameter (that will be a property of Script op, like other machining data feed and speedl

ar even a box() maybe with an option to clear the path to transform in pocket.

so surfacing a piece wil be easily done positioning the tool at a starting point (maybe lower left corner) and creating a script operstion eith.

Code: Select all

a = mem(pos)
g1(z -1.0)
box(300, 200)
g0(a)
Simply enough?

But this implies to have some documentation or help in how to implement an operation from scratch.

the parser will be not so difficult, there are some python code around that generates GCode as output.

it will be a matter of reading the gcode property and process it when the script operation is processed and the resulting gcode will be put in the Gcode file.


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 »

onekk wrote: Wed Jan 19, 2022 7:50 am
@sliptonic

I've roughly catch the generator methods working.

no need to model an operation, maybe adding a "special operation" marked as script and make come conventional text, or even putting directly a GCode on it will be easy.

you will add the new operation and choose a Macro that will generate the code, but the file will "retain" only the generated GCode (no need to have all the Macro Code in the FCStd file).
Slow down. First things first.
Let's do the generator first and then consider the next step.

Dropping the macro code and only storing the gcode creates a special kind of 'custom'. The problem is that it cannot be recomputed. If the user changes the origin in the job setup or wants to change a depth, you lose the ability to recompute the keyhole (or whatever) relative to the rest of the job. Anyway, that's getting ahead of ourselves.
User avatar
onekk
Veteran
Posts: 6094
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Adding scripts to a job?

Post by onekk »

sliptonic wrote: Wed Jan 19, 2022 3:00 pm
Slow down. First things first.
Let's do the generator first and then consider the next step.

Dropping the macro code and only storing the gcode creates a special kind of 'custom'. The problem is that it cannot be recomputed. If the user changes the origin in the job setup or wants to change a depth, you lose the ability to recompute the keyhole (or whatever) relative to the rest of the job. Anyway, that's getting ahead of ourselves.

Ok to do the generator, but how?

- making a wire that with supplied infos could be transformed in a Path?

Code: Select all

def keyhole_generator(main_dia, length, depth):
    some code here
    return wire # or wires as a list or tuple of wire
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 »

onekk wrote: Wed Jan 19, 2022 3:11 pm
sliptonic wrote: Wed Jan 19, 2022 3:00 pm
Slow down. First things first.
Let's do the generator first and then consider the next step.

Dropping the macro code and only storing the gcode creates a special kind of 'custom'. The problem is that it cannot be recomputed. If the user changes the origin in the job setup or wants to change a depth, you lose the ability to recompute the keyhole (or whatever) relative to the rest of the job. Anyway, that's getting ahead of ourselves.

Ok to do the generator, but how?

- making a wire that with supplied infos could be transformed in a Path?

Code: Select all

def keyhole_generator(main_dia, length, depth):
    some code here
    return wire # or wires as a list or tuple of wire
What do you need a wire for? Everything you need can be derived from the startpoint and endpoint.
So the generator is really simple. More like this:

Code: Select all


def generate(startPoint, endPoint):

    #test any assumptions about the inputs and raise exceptions

    commands = []
    #figure out the bottom of the vertical move.
    command.append(Path.Command("G1" {<params for end of plunge>}))

    #Figure out the end point of the horizontal move.
    command.append(Path.Command("G1" {<params for end of cut>}))

    # Retract on the horizontal
    command.append(Path.Command("G0" {<params for end of horizontal retract>}))

    # Retract on the vertical
    command.append(Path.Command("G0" {<params for end of vertical retract>}))

    return commands


GeneFC
Veteran
Posts: 5373
Joined: Sat Mar 19, 2016 3:36 pm
Location: Punta Gorda, FL

Re: Adding scripts to a job?

Post by GeneFC »

onekk wrote: Wed Jan 19, 2022 7:50 am It is not clear what the cutter is doing.

Keyholes are usually made by two circles and two lines.

if you think of the key cylinder.

The cutter you have shown seem to have a big diameter and a smaller diameter cutting edge.

So I suppose that it will machine the big hole, step down, and cut the smaller part.
The photo in the first post is a standard keyhole cutter. It moves down, cutting the entry hole. Then it moves laterally cutting both the smaller slot and the wider buried slot for the screw head.

It is for hanging pictures and the like. Nothing to do with real keys or locks.

Gene
User avatar
onekk
Veteran
Posts: 6094
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Adding scripts to a job?

Post by onekk »

GeneFC wrote: Wed Jan 19, 2022 3:48 pm
The photo in the first post is a standard keyhole cutter. It moves down, cutting the entry hole. Then it moves laterally cutting both the smaller slot and the wider buried slot for the screw head.

It is for hanging pictures and the like. Nothing to do with real keys or locks.

Gene

OK it seems me strange, so in the case it suffice to state starting point and move accordingly,

like:

suppose you are at 1mm from the stock surface, and in the center of the entry hole.

Code: Select all

G0 Z 0
G1 Z_depth 
G1 Y-10 (to length) # here the cutter will make the groove
G1 Y0  # return to the initial position)
G0 Z start
This will be the pseudocode.

to generate a proper hole we need the length of the groove and the depth of the cut (in term of Z)

Code: Select all

def generate(length, depth, StartPoint):

    #test any assumptions about the inputs and raise exceptions

    commands = []
    command.append(Path.Command("G0" {X StartPoint.X Y StartPoint.Y Z 0}))

    command.append(Path.Command("G1" {X StartPoint.X Y StartPoint.Y Z depth}))

    #Figure out the end point of the horizontal move.
    command.append(Path.Command("G1" {X StartPoint.X Y StartPoint.Y - length  Z depth}))

    # Retract on the horizontal
    command.append(Path.Command("G0" {X StartPoint.X Y StartPoint.Y Z depth}))

    # Retract on the vertical
    command.append(Path.Command("G0" {X StartPoint.X Y StartPoint.Y Z StartPoint.Z}))

    return commands
but how to supply variables to:

Code: Select all

  command.append(Path.Command("G0" { }))
is a dict so if I supply

Code: Select all

pt1 = {"X": StartPoint.X,  "Y": StartPoint.Y,  "Z": 0 }
will work for the inital postion?

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 »

onekk wrote: Wed Jan 19, 2022 4:21 pm
suppose you are at 1mm from the stock surface, and in the center of the entry hole.
For the generators, you can assume you're at the starting position already. The generator shouldn't have to make any assumptions about what comes before or after it. It'll be the responsibility of the operation code to get the machine to the correct starting point.

Code: Select all

G0 Z 0
G1 Z_depth 
G1 Y-10 (to length) # here the cutter will make the groove
G1 Y0  # return to the initial position)
G0 Z start
I would prefer that the generators return Path commands with all the coordinates explicitly stated. The post-processors can suppress this later for efficiency. We don't want any hidden assumptions though.

The retract moves both horizontal and vertical can be done as rapids.
to generate a proper hole we need the length of the groove and the depth of the cut (in term of Z)
Not really, The depth is derived as the difference between the startPoint.z and the endPoint.z
The length is implied by moving from the startPoint.x,y to the endPoint.x,y

At the operation level, it might be nice to allow the user to only specify a start point and a direction/length or maybe select two points, or something else. Then the operation will turn it into two points for the generator.
but how to supply variables to:
Take a look at the drill_generator (already merged) or the helix_generator (pr pending)
Post Reply