How to use python to create a model-based processing path and simulate
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
-
- Posts: 21
- Joined: Sun Sep 19, 2021 5:25 pm
How to use python to create a model-based processing path and simulate
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. 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.
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. 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 39 times
Re: How to use python to create a model-based processing path and simulate
I have never used the Path workbench before.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.
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
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.
-
- Posts: 92
- Joined: Mon Nov 19, 2018 8:26 pm
Re: How to use python to create a model-based processing path and simulate
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()
-
- Posts: 21
- Joined: Sun Sep 19, 2021 5:25 pm
Re: How to use python to create a model-based processing path and simulate
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.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()
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. 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.
-
- Posts: 92
- Joined: Mon Nov 19, 2018 8:26 pm
Re: How to use python to create a model-based processing path and simulate
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
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.
Code: Select all
Gui.runCommand('Path_Simulator',0)
Re: How to use python to create a model-based processing path and simulate
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.
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/
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/
-
- Posts: 327
- Joined: Tue May 28, 2019 10:51 am
Re: How to use python to create a model-based processing path and simulate
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.
-
- Posts: 21
- Joined: Sun Sep 19, 2021 5:25 pm
Re: How to use python to create a model-based processing path and simulate
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()