Adding CAM simulation to Path workbench

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
User avatar
shaise
Posts: 374
Joined: Thu Jun 11, 2015 8:11 am

Adding CAM simulation to Path workbench

Postby shaise » Sun Oct 15, 2017 5:27 pm

Hi FreeCad team,

For the past few weeks I was playing around with programming CAM simulation to work with Path workbench. I feel this is an important feature as it lets you make sure the rendered gcode indeed delivers what you intended to get.
At first I tried to do it with python and boolean operations. The results were nice, but it was extremely slow. As a test I used one of sliptonic's torture models, and it took about 1 hour and 20 minutes to complete.
(See following thread for images and videos: https://forum.freecadweb.org/viewtopic.php?f=15&t=24422)
I then tried to do the same using external BSP tree boolean library in C++. This helped a bit but still too slow.
Then I decided to go for something like 2.5D voxels (ugly but fast), wrote everything from scratch (no external libraries) and the simulation dropped down to a mere 1 second.
Right now its a stand alone c++ application. I want to integrate it into freecad's path workbench so it can work seamlessly.
Unfortunately as far as I know I can not create an external c++ module, I have to integrate it into FC code. There is lots of info of how to add Python code, but I didnt find enough docs of how to add C++ code. Specially how to write all the python interface to C++ objects. I saw some xml files, suggesting a code generator perhaps?
Any links to docs and samples will be appreciated.


OCC Boolean simulation:
boolSim.png
boolSim.png (46.27 KiB) Viewed 1842 times
C++ 2.5D Voxel sim :
voxSim.png
voxSim.png (144 KiB) Viewed 1842 times
User avatar
PrzemoF
Posts: 3059
Joined: Fri Jul 25, 2014 4:52 pm
Contact:

Re: Adding CAM simulation to Path workbench

Postby PrzemoF » Sun Oct 15, 2017 9:15 pm

Looks impressive! I think voxels might be the way to solve the real life problems where you don't need some crazy angstrom accuracy. Or handling models made in kilometers and micrometers in the same file.

Have you seen this?

https://www.freecadweb.org/wiki/index.p ... e_Creation
https://www.freecadweb.org/wiki/index.p ... h_creation
ickby
Posts: 2985
Joined: Wed Oct 05, 2011 7:36 am

Re: Adding CAM simulation to Path workbench

Postby ickby » Mon Oct 16, 2017 6:02 am

very nice work!

For making an python exposed c++ object I recommend to have a look at Part Workbenchs TopoShape class, it is a very good example of how to do it.

First step is to derive your c++ class from a FreeCAD base object, maybe ComplexGeoData is also appropriate for you. Than your write a xml file, which defines your objects python methods and attributes, like this. Afterwards you cna implement the defined methods in a c++ file like this

In the CMakeList you than call generate_from_xml on the xml file and make sure that the c++ implementation file is also compiled. This creates your python wrapper.
User avatar
shaise
Posts: 374
Joined: Thu Jun 11, 2015 8:11 am

Re: Adding CAM simulation to Path workbench

Postby shaise » Mon Oct 16, 2017 6:11 am

ickby and PrzemoF,

Thanks a lot! this is very helpful.
Lets start coding...
As soon as I have a basic working version I will post the code on github

shai.
chrisb
Posts: 29041
Joined: Tue Mar 17, 2015 9:14 am

Re: Adding CAM simulation to Path workbench

Postby chrisb » Mon Oct 16, 2017 7:50 am

Waiting for it ...
Chris
A Sketcher Lecture with in-depth information is available in English, auf deutsch, en français, en español.
User avatar
shaise
Posts: 374
Joined: Thu Jun 11, 2015 8:11 am

Re: Adding CAM simulation to Path workbench

Postby shaise » Sun Oct 22, 2017 3:03 pm

Hi, Need some experts help here:

I have almost completed the c++ side of the voxel path simulator. Here is a compilation Issue I could not resolve:
I have created a new module: PathSimulator and I am trying to access objects from Path module. I have no issue accessing the object classes but i get unresolved item for the ::Type structs:

Code: Select all

Error	13	error LNK2001: unresolved external symbol "public: static struct _typeobject Path::CommandPy::Type" (?Type@CommandPy@Path@@2U_typeobject@@A)	D:\shai\FreeCAD\dev\build\src\Mod\Path\PathSimulator\App\PathSimPyImp.obj	PathSimulator
Error	12	error LNK2001: unresolved external symbol "public: static struct _typeobject Path::ToolPy::Type" (?Type@ToolPy@Path@@2U_typeobject@@A)	D:\shai\FreeCAD\dev\build\src\Mod\Path\PathSimulator\App\PathSimPyImp.obj	PathSimulator
These objects reside in the Path module. I checked the Path.lib file and listed its contents, and indeed it exports those 2 types:

Code: Select all

                  ?Type@ToolPy@Path@@2U_typeobject@@A (public: static struct _typeobject Path::ToolPy::Type)
                  ?Type@CommandPy@Path@@2U_typeobject@@A (public: static struct _typeobject Path::CommandPy::Type)
I have included the ToolPy.h and CommandPy.h. Libraries are also included in the cMakeLists.
I have no problem accessing objects from Part library including its ::Type objects.

The file: https://github.com/shaise/FreeCAD/blob/ ... mPyImp.cpp

Anyone have a guess why?
User avatar
shaise
Posts: 374
Joined: Thu Jun 11, 2015 8:11 am

Re: Adding CAM simulation to Path workbench

Postby shaise » Mon Oct 23, 2017 6:07 pm

Hi Everyone,

I have finally managed to integrate the C++ code into FreeCAD. Here is the result - this time it is Realtime:
phpBB [video]


right now it still uses external macro, but soon I will embed the Python side as well.
If anyone can help me, I still need answers for my previous post (right now I skip type checking as a workaround)

The C++ code can be found here: https://github.com/shaise/FreeCAD/tree/ ... hSimulator
The Macro is here (With many errors for now): https://github.com/shaise/FreeCAD_Macro ... sh.FCMacro

on for the details....

shai
User avatar
Kunda1
Posts: 8769
Joined: Thu Jan 05, 2017 9:03 pm

Re: Adding CAM simulation to Path workbench

Postby Kunda1 » Mon Oct 23, 2017 6:29 pm

That.Is.Awsome!

@shais this could be a step closer to realizing issue #2091 if one was so inclined
Want to contribute back to FC? Checkout:
#lowhangingfruit | Use the Source, Luke. | How to Help FreeCAD | How to report FC bugs and features
User avatar
microelly2
Posts: 4690
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Adding CAM simulation to Path workbench

Postby microelly2 » Mon Oct 23, 2017 7:20 pm

wow, interesting.
a very good performance for this kind of simulation.
User avatar
shaise
Posts: 374
Joined: Thu Jun 11, 2015 8:11 am

Re: Adding CAM simulation to Path workbench

Postby shaise » Mon Oct 23, 2017 7:49 pm

I feel the simulation is an essential tool to make sure the G Code is valid before actual deployment on the CNC. Thats why its important to have it integrated into the Path workbench.