Scripting Path workbench

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!
Russ4262
Posts: 941
Joined: Sat Jun 30, 2018 3:22 pm
Location: Oklahoma
Contact:

Re: Scripting Path workbench

Post by Russ4262 »

wildzero wrote: Sat Apr 15, 2023 12:12 am Any update on this issue? Have exactly the same problem creating a path job via python
Afternoon,
Attached is a module to aid in Job creation and adding tool controllers. It is based on current, restructured code for the Path workbench. No testing has been conducted with the script and pre-restructuring versions of FreeCAD.

Simply place the attached module in your Macros folder to make it accessible to FreeCAD. The module contains four primary public functions:
  1. add_job(models=[], templateFile=None, useGui=False): Add a new job to current document.
  2. add_toolcontroller_by_filename(job, name): Adds a new tool controller based on the name argument to the target job object provided. Get a list of available tool filenames using included 'available_tool_filenames()'.
  3. add_toolcontroller_by_number(job, number): Adds a new tool controller referenced by the number argument to the target job object provided. The number argument is available via the included 'available_tool_filenames()'. If duplicate tool numbers exist across library files, the first instance of the tool number will be used. In this case, I recommend the filename function be used in order to target a specific tool that may exist in multiple library files.
  4. available_tool_filenames(): Prints a list of available tools and associated tool number as referenced in tool libraries identified in FreeCAD preferences. **I do not declare that this function will find all tool library files on your computer. The function is based on existing libary search code in the Path workbench.
In the following examples, the user only needs to import the attached JobUtils module once in a given FreeCAD session.

Usage to identify available tools:
In the python console:

Code: Select all

>>> import JobUtils
>>> JobUtils.available_tool_filenames()
Available tool files:
     1 ::   5mm_Endmill
     2 ::   5mm_Drill
     3 ::   6mm_Ball_End
     4 ::   6mm_Bullnose
     5 ::   60degree_Vbit
     6 ::   45degree_chamfer
     7 ::   slittingsaw
     8 ::   probe
     9 ::   5mm-thread-cutter
     10 ::   dovetail_635mm
     11 ::   Endmill_3mm
 
['5mm_Endmill', '5mm_Drill', '6mm_Ball_End', '6mm_Bullnose', '60degree_Vbit', '45degree_chamfer', 'slittingsaw', 'probe', '5mm-thread-cutter', 'dovetail_635mm', 'Endmill_3mm']
>>> 

Usage to add a Job:
In the python console:

Code: Select all

>>> import JobUtils
# In this example, the name of the model base is provided as 'Body'.
# An actual pointer to the model base object is also permitted in place of the model name.
# The leading 'job = ' is optional to capture a pointer to the new Job object created.
>>> job = JobUtils.add_job(models=['Body'])


Usage to add a tool controller to an existing Job object:
In the python console:

Code: Select all

>>> import JobUtils
# First, get a pointer to the target Job object.
>>> job = FreeCAD.ActiveDocument.getObject('Job')

# Next, add a tool controller by filename
# The tool filename is taken from the list provided from the 'available_tool_filenames()' function above
# The leading 'tc1 = ' is optional to capture a pointer to the new tool controller object created.
>>> tc1 = JobUtils.add_toolcontroller_by_filename(job, 'slittingsaw')

# Or, add a tool controller by tool number
# The tool number is taken from the list provided from the 'available_tool_filenames()' function above
# This example uses number '5 ::   60degree_Vbit' seen above
# The leading 'tc2 = ' is optional to capture a pointer to the new tool controller object created.
>>> tc2 = JobUtils.add_toolcontroller_by_number(job, 5)


EDIT 2023-09-10: Added newer Version 2023-09-10 a few posts below.
EDIT 2024-02-25: Upgraded version 2024-02-25 available further in this thread at Re: Scripting Path workbench. Removed original version here, 2023-04-16, downloaded 57 times.

Cheers,
Russell

Code: Select all

OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.21.0.32821 (Git)
Build type: Release
Branch: master
Hash: eaf9d43f37238c33dd1c47b859a6fdf5b7f629fc
Python 3.10.10, Qt 5.15.8, Coin 4.0.0, Vtk 9.1.0, OCC 7.6.3
Locale: English/United States (en_US)
Installed mods: 
  * FC_SU
  * freecad.gears 1.0.0
  * PathExp
  * Z_MacroStartup
Last edited by Russ4262 on Mon Feb 26, 2024 4:20 am, edited 4 times in total.
spanner888
Posts: 326
Joined: Tue May 28, 2019 10:51 am

Re: Scripting Path workbench

Post by spanner888 »

Russ4262 wrote: Sun Apr 16, 2023 8:11 pm Attached is a module to aid in Job creation and adding tool controllers.....
thanks Russ, that nicely gives us a great start and gets us up to date after recent changes.
MrFitz
Posts: 5
Joined: Sat May 27, 2023 2:00 am

Re: Scripting Path workbench

Post by MrFitz »

This script is very helpful for job creation and tool controllers. Thank you!

Can it be further extended to include path operations as well?

Blessings on your life and work!
Russ4262
Posts: 941
Joined: Sat Jun 30, 2018 3:22 pm
Location: Oklahoma
Contact:

Re: Scripting Path workbench

Post by Russ4262 »

MrFitz wrote: Fri Jun 02, 2023 11:20 pm ... Can it be further extended to include path operations as well? ...
Evening.
Sure. The Path workbench and FreeCAD in general are fairly scriptable.

Blessing well received. Have a great week.

Russell
MrFitz
Posts: 5
Joined: Sat May 27, 2023 2:00 am

Re: Scripting Path workbench

Post by MrFitz »

Hello @Russ4262 ! I've been using this script with thankfulness. I have not been able to figure out how to do something similar in creating an operation, for example, a profile or pocket with a boundary dressup. Have you extended these utilities into path operations? If so, would you mind sharing an example?

Blessings and increase in your life!
Russ4262
Posts: 941
Joined: Sat Jun 30, 2018 3:22 pm
Location: Oklahoma
Contact:

Re: Scripting Path workbench

Post by Russ4262 »

MrFitz wrote: Mon Sep 04, 2023 6:41 pm ... Have you extended these utilities into path operations? If so, would you mind sharing an example? ...
Afternoon.
Took a break from work for some FreeCAD fiddling. Attached is the updated version with two additional functions, and an associated test function.

As with the orginal version above, place the attached module in your Macros folder to make it accessible to FreeCAD. This version module contains these additional primary public functions:
  1. add_profile_operation(job, baseGeom=[], useGui=False): Add a new profile operation to the job provided as the argument.
  2. add_dressup_boundary(baseOp, extendValues={"xNeg": 1.0, "xPos": 1.0, "yNeg": 1.0, "yPos": 1.0, "zNeg": 1.0, "zPos": 1.0,}, useGui=False, ): Adds a new boundary dressup on the baseOp operation provided as the argument.
Included in this version are some improvements to the testing function, splitting into multiple test functions. One dedicated to testing the tool controller functions, and the second dedicated to testing the new profile and boundary dressup creation functions. These are test_00() and test_01() respectively. These two do not verify output in the traditional sense of testing, but provide example usage.

No changes were made to existing functions within the module, other than the test function.

EDIT: 2024-02-25 Upgraded version 2024-02-25 at Re: Scripting Path workbench. Removed version, 2023-09-10, downloaded 32 times.

Have a blessed day.
Russell
Last edited by Russ4262 on Mon Feb 26, 2024 4:19 am, edited 2 times in total.
MrFitz
Posts: 5
Joined: Sat May 27, 2023 2:00 am

Re: Scripting Path workbench

Post by MrFitz »

Brilliant!

Blessed indeed. Thank you.
EdsWorkshop
Posts: 4
Joined: Sun Feb 18, 2024 12:25 pm

Re: Scripting Path workbench

Post by EdsWorkshop »

I've got to CNC 70 wooden nameplates for table settings at a wedding this summer, so I'm building a script to generate 70 .nc files from a list of names. Heartfelt thanks to Russ4262, I've been picking through your utils and used them as a manual for scripting the Path workbench. I'm a beginner at both FreeCAD and Python and this thread and your utils has given me a way in. This is my first post on this forum.

I'm in FreeCAD 0.21 on Windows.

So after 3 days struggle I've written the attached, which seems to work pretty well, albeit I've probably abused the API terribly and would welcome any comments on that.

I have just one issue - I can't get the toolbit to start the Vcarve operation from the Origin. I want to do something functionally the same as 'Set Origin' in the Job dialog, and to achieve that I'm currently shifting the Placement of the Job so the vertex that I want to be the origin is at (0, 0, 0). However nothing I've tried alters the place that the toolbit starts from. I'm probably taking completely the wrong approach and would welcome any pointers from anyone please.

Peter at Eds Workshop
Attachments
TextGenerator.py
(7.83 KiB) Downloaded 8 times
EdsWorkshop
Posts: 4
Joined: Sun Feb 18, 2024 12:25 pm

Re: Scripting Path workbench

Post by EdsWorkshop »

Hi @Russ4262, apologies if mentioning you directly in the hope of help is not the right thing to do, this is only the second time I've ever posted on any forum, and I don't know if cold calling is allowed. There's not many people scripting against the path workbench and I've learned a lot from reading your code posted above. I've spent another weekend scouring the forum for an answer to my question and trying things out.

What's the scripting equivalent of pressing the 'Set Origin' button in the Job dialog, please?

In my attached script I've tried shifting the Placement of the Job so that my chosen vertex of the stock is on the origin. That appears to have the right effect, but isn't functionally the same. When I add the Tool it doesn't get placed on the origin (even though the Placement in the GUI is 0,0,0), and when I add the Vcarve operation the initial move doesn't start from the origin either.

My guess is the 'Set origin' button does something more global than just move the Placement of the Job, or perhaps it does multiple things. I'm a FreeCAD beginner so getting really confused reading about attachments, transformations, placements, global and local coordinates.

Thanks
Peter from Ed's Workshop
Attachments
TextGenerator.py
(6.42 KiB) Downloaded 2 times
Russ4262
Posts: 941
Joined: Sat Jun 30, 2018 3:22 pm
Location: Oklahoma
Contact:

Re: Scripting Path workbench

Post by Russ4262 »

EdsWorkshop wrote: Sun Feb 25, 2024 12:26 pm ... this is only the second time I've ever posted on any forum, ...
What's the scripting equivalent of pressing the 'Set Origin' button in the Job dialog, please?
...
Good afternoon,
Welcome to the forum! Glad to have another FreeCAD user, and involved in the forum. As to your question about scripting behind the 'Set Origin' button, I have updated the JobUtils module to include a function to do just that.

As with the original version above, place the attached module in your Macros folder to make it accessible to FreeCAD. This version contains an additional public function:
  1. set_job_origin_to_point(job, pnt): Sets the origin of the 'job' object to the given 'pnt' argument.
Included in this version are some improvements to the testing functions with a new test targeting the new function mentioned above. Also included are additional comments added to some of the existing functions for documentation and support.

Pertaining to your specific code, I have downloaded and taken a look. I made two primary changes on my end, after converting the file extension to .FCMacro so I can run/edit as a macro. One, I grouped your set of instructions into specific functions to be called as a means to isolate specific actions. Two, I added a simple one-line function using the JobUtils module to add the stock 60 degree vbit to the Job object since I don't have your exact toolbit file on my computer. I have attached this new file (macro version). Simply change the file extension to '.py' to convert back to a module(proper python script file).

Back to your 'Set Origin' question...
I need some clarification. Is your intention to move the origin to a particular point on your work piece as a means to set the zero point for the Job? Or, are you wanting to set the point at which the machine begins making the VCarve cut - referred to as a Start Point in the Path workbench? The new function added to the JobUtils above is for the former, setting the actual origin, or zero point. As for the latter, I do not believe that the VCarve operation allows for the provision of a Start Point.

Also, it appears from my running of your code that your ShapeString is larger than your stock. Also, great job with the comments in your code. I will send a PM to you with additional contact information.

Have a blessed day.
Russell

```
OS: Windows 10 build 19045
Word size of FreeCAD: 64-bit
Version: 0.22.0dev.36117 (Git)
Build type: Release
Branch: main
Hash: 8a3407aee6dff8218282d9cdc3f4e32e7ad958d4
Python 3.10.13, Qt 5.15.8, Coin 4.0.2, Vtk 9.2.6, OCC 7.6.3
Locale: English/United States (en_US)
Installed mods:
* FC_SU
* freecad.gears 1.0.0
* freecad.pathred
* Z_MacroStartup
```
Attachments
TextGenerator.FCMacro
Macro version of modified TextGenerator script
(7.52 KiB) Downloaded 5 times
JobUtils.py
JobUtils module, version 2024-02-25
(17.07 KiB) Downloaded 10 times
Post Reply