Friday, October 5, 2012

Python toolpath generation -HeeksCNC method

In this post, I want to explain the basics of how HeeksCNC uses python to generate toolpaths. The method that Dan Heeks came up with for doing this was brilliant. I have used bits and pieces of this method with different applications a lot and am just starting to use it with FreeCAD. Here is a python script (Dan Heeks named it in HeeksCNC) that is the main script for a gcode file. : 
import area
import kurve_funcs
from import *
import nc.emc2

program_begin(123, 'Test program')

comment('Feeds and Speeds set for machining Please select a material to machine')

tool_defn( id=3, name='1/4 inch Carbide End Mill', radius=0.125, length=1.25, gradient=-0.1)
#(0.188 inch Carbide End Mill)

comment('tool change to 1/4 inch Carbide End Mill')
tool_change( id=3)
feedrate_hv(33.07086614, 7)
clearance = float(0.1968503937)
rapid_safety_space = float(0.07874015748)
start_depth = float(0)
step_down = float(0.035)
final_depth = float(-0.07)
tool_diameter = float(0.25)
cutting_edge_angle = float(0)
#absolute() mode
roll_radius = float(0.125)
offset_extra = 0
curve = area.Curve()
curve.append(area.Point(1.25, -0.7))
curve.append(area.Point(-0.65, -0.7))
curve.append(area.Vertex(-1, area.Point(-1.4, 0.05), area.Point(-0.65, 0.05)))
curve.append(area.Point(-1.4, 0.45))
curve.append(area.Vertex(-1, area.Point(-0.65, 1.2), area.Point(-0.65, 0.45)))
curve.append(area.Point(1.25, 1.2))
curve.append(area.Vertex(-1, area.Point(2, 0.45), area.Point(1.25, 0.45)))
curve.append(area.Point(2, 0.05))
curve.append(area.Vertex(-1, area.Point(1.25, -0.7), area.Point(1.25, 0.05)))

roll_on = 'auto'
roll_off = 'auto'
extend_at_start= 0
extend_at_end= 0
lead_in_line_len= 0.25
lead_out_line_len= 0
kurve_funcs.profile(curve, 'left', tool_diameter/2, offset_extra, roll_radius, roll_on, roll_off, rapid_safety_space, clearance, start_depth, step_down, final_depth,extend_at_start,extend_at_end,lead_in_line_len,lead_out_line_len )
This script is processed like this: reaches out to other scripts/modules in the heekscnc world for processing and formatting. These other scripts need to be in the python path, as you would expect.
libarea can be had from many places now, although I prefer to use the 'New BSD' licensed version that can be found here. is used for processing toolpaths for milling profiles. It depends on libarea too. I think we could probably untangle the dual dependency of calling 'import area' in and in, if we looked into it.

the line 'import nc.emc2' calls upon a user editable script that formats gcode for the emc2 (linuxcnc now) milling machine. There are a lot of other scripts in the nc directory that let one format the gcode for other machines.

Here is a summary of the components of this system again:



  1. Looking forward to seeing more on this, both for FreeCAD and possibly for including the same technique in my system. Of course if your work makes it in to FreeCAD I won't need to put it in mine!

    Keep at it.

  2. Dan, is there any help you can give me with compiling libarea? This post was quite helpful for understanding how it's to be used. So it's a Python module to be compiled and installed and then called from Python... I've written Python before but never compiled a native library for Python before. I'm asking you because I've pulled the git code with the new license. I could ask Dan Heeks. Thanks.

  3. Hi Daryl. That's kind of a deep subject for the comments section of a blog :) What OS are you running? Did you get a log of the errors, if you tried compiling it and didn't get it to work? I will try my best,but hopefully we end up integrating it with FreeCAD and it becomes a non-issue.

  4. Hi Dan,
    Do you still planing to integrate it on FreeCAD?

    I looked at the FreeCAD repository and noticed there is native CAM module, but I don't know how complete it is currently.

    I want to test all options, including exporting STL to use HeeksCAD or BlenderCAD, but a completely integrated solution is better.



  5. Hi Alan,
    Sorry, I haven't made much progress a CAM workbench here, other than learning a lot about the FreeCAD internals. David Nichols has made the most progress and you can look at his git repo here:

    The CAM workbench that is currently included with FreeCAD isn't being actively developed. The new one is referred to as 'CAM2'.
    One thing that FreeCAD has helped me with is learning about software development in general. I've done a lot of experiments with it and even set up a FreeCAD based server that I'm playing with for manipulating drawing views. It's a very good platform for development.

    1. Hi Dan,
      I just began to read your FreeCAD [How-To] book, I find it very interesting. What I'm looking for is a CAM software that goes in between FreeCAD and LinuxCNC. I heard about such a module for FreeCAD, but I find it nowhere.
      I don't know if I could contribute to this work, I like Python programming but I am not a CAD or CAM expert, my main field of interest is electronics.
      If you think there is something I could do, please tell me.
      With friendly greetings,

    2. Hi Jean-Claude,
      We actually have a new CAM workbench in FreeCAD that we have named 'Path' (for tool path). It is in the latest source. It is still rough, but I think we have the ground work in it. Try it out.
      We also have a sub forum on the FreeCAD forum for CAM here: