How to use python to create a model-based processing path and simulate

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
maxwang531
Posts: 21
Joined: Sun Sep 19, 2021 5:25 pm

How to use python to create a model-based processing path and simulate

Post by maxwang531 »

I am a novice to python. At present, I hope to use some functions of FreeCAD to create model-based CNC path through python. After learning part of the code in the forum, based on this post "https://forum.freecadweb.org/viewtopic.php?f=15&t=53849" , I can already import the model in Python and create the most basic profile path. I put the code in the attachment "Without GUI Import Test.py".

But at present I have encountered a problem about the path simulator. I don't know how to call the path simulator through python to complete the simulation. In fact, what I need is the Cutmaterial model after the simulation. In python, PathSimulator can be called by the code "Gui.runCommand('Path_Simulator',0)". But the simulation will not proceed, but will get stuck on this screen.
PathSimulator.PNG
PathSimulator.PNG (102 KiB) Viewed 2328 times
Does anyone know how to make the simulation go on through python code? My purpose is to compare the Cutmaterial model with the original model.
Attachments
Without GUI Import Test.py
(1.83 KiB) Downloaded 38 times
User avatar
gbroques
Posts: 167
Joined: Thu Jan 23, 2020 3:28 am
Location: St. Louis, Missouri

Re: How to use python to create a model-based processing path and simulate

Post by gbroques »

maxwang531 wrote: Wed Oct 20, 2021 3:23 pm Does anyone know how to make the simulation go on through python code? My purpose is to compare the Cutmaterial model with the original model.
I have never used the Path workbench before.

However, I would take a look at the PathScripts module in the Python console (ensure View > Panels > Python console is checked).

For example, this looks promising:

Code: Select all

import PathScripts
PathScripts.PathSimulatorGui.pathSimulation
If you type "." after a symbol in the Python console, then you should see a context window containing the methods available to you.

Then cycle through the methods with the up and down arrow keys reading the description of each.

You may need someone familiar with the Path workbench in Python to help, but I hope this gives some ideas of things to try.
CoderMusashi
Posts: 92
Joined: Mon Nov 19, 2018 8:26 pm

Re: How to use python to create a model-based processing path and simulate

Post by CoderMusashi »

Here is some code to get you further on your quest.

Code: Select all

Gui.runCommand('Path_Simulator',0)

import PathScripts.PathSimulatorGui as psim
psim.pathSimulation.SetupSimulation()
psim.pathSimulation.SimPlay()

# code for accepting and closing the dialog box
psim.pathSimulation.accept()
FreeCADGui.Control.closeDialog()
maxwang531
Posts: 21
Joined: Sun Sep 19, 2021 5:25 pm

Re: How to use python to create a model-based processing path and simulate

Post by maxwang531 »

CoderMusashi wrote: Thu Oct 21, 2021 4:43 am Here is some code to get you further on your quest.

Code: Select all

Gui.runCommand('Path_Simulator',0)

import PathScripts.PathSimulatorGui as psim
psim.pathSimulation.SetupSimulation()
psim.pathSimulation.SimPlay()

# code for accepting and closing the dialog box
psim.pathSimulation.accept()
FreeCADGui.Control.closeDialog()
First of all, thank you for your reply. In fact, the code you gave has been confirmed by me to be effective, but there is still a problem about the simulation time.
If you put the code in a python program and run it together, the simulation will not actually run, and the resulting Cutmaterial is just a cube.
CutMaterial.PNG
CutMaterial.PNG (108.93 KiB) Viewed 2071 times
And if the code is entered line by line, the simulation can indeed artificially determine whether it has been completed. So one of my current thoughts is whether it is possible to add a running time to the line of psim.pathSimulation.SimFF(). I don't know if this is feasible.
CoderMusashi
Posts: 92
Joined: Mon Nov 19, 2018 8:26 pm

Re: How to use python to create a model-based processing path and simulate

Post by CoderMusashi »

I am not sure what your trying to accomplish. I was bored at work and took a stab at what you were working on for fun. I notice you were just calling up the GUI using the command

Code: Select all

Gui.runCommand('Path_Simulator',0)
I knew you had to hit the play button or the simulation wouldn't run so I took a look at how this was done by opening up \Mod\Path\PathScripts\ PathSimulatorGui.py. After looking at the code I quickly came up this the code I posted here by testing in the FreeCAD python console on a generic job. Honestly you don't need to run the GUI to do a simulation. All the code used to create the MESH cut material is right there in PathSimulatorGui.py which is what you are after plus any other things that come from doing a simulation. You can copy whole sections of that code for your own use or call it like any other module.
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to use python to create a model-based processing path and simulate

Post by onekk »

Main concern, using Python and Path module, is that actually, at least in 0.19 and probably in 0.20 Path module, is not very "python friendly".

So it is not scriptable, without doing some "magic".

Maybe it is better to ask at @sliptonic and see what is his answer.

When I've tried Path scriptability. I have had some problems with Jobs, and Operations, so after some discussion with Sliptonic the result is that the "full scriptability" of Path is a future goal.

But this discussion has been done at leaste six months ago, so I don't know if something is already implemented.

I have tested some experimental code for 0.20 about some modification about Path.Job to make creation of Job using Python more easy but this code was experimental and I've replaced original Path code with development code.

I suspect that not much is implemented yet as from some discussion I have seen two or three months ago priority is not Python usability, as development is concentrated on GUI workflow, as scriptability is used by too few peoples.

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/
spanner888
Posts: 326
Joined: Tue May 28, 2019 10:51 am

Re: How to use python to create a model-based processing path and simulate

Post by spanner888 »

Also be aware that the path Simulator does not always (often?) return an accurate representation of the Cutmaterial model. There are plenty of mentions in the forum and my own experience confirms.
maxwang531
Posts: 21
Joined: Sun Sep 19, 2021 5:25 pm

Re: How to use python to create a model-based processing path and simulate

Post by maxwang531 »

First of all, thank you for your various suggestions. Finally, after a part of the trial, you only need to add some conditional statements between the start and end of the simulation to complete the automatic simulation and the export of CutMaterial. The specific code is as follows:

Code: Select all

from PathScripts import PathSimulatorGui as a
from time import sleep
Gui.runCommand('Path_Simulator',0)

a.pathSimulation.SetupSimulation()  
a.pathSimulation.SimFF()

while a.pathSimulation.iprogress < a.pathSimulation.numCommands :
    sleep(1)
a.pathSimulation.accept()
FreeCADGui.Control.closeDialog()

Post Reply