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 post.py in HeeksCNC) that is the main script for a gcode file.
post.py : 
import area
area.set_units(25.4)
import kurve_funcs
from nc.nc import *
import nc.emc2

output('/tmp/test.tap')
program_begin(123, 'Test program')
absolute()
imperial()
set_plane(0)

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)

workplane(1)
comment('tool change to 1/4 inch Carbide End Mill')
tool_change( id=3)
spindle(7000)
feedrate_hv(33.07086614, 7)
flush_nc()
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
comment('Sketch')
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 )
absolute()
program_end()  
This script is processed like this:


post.py 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.

kurve_funcs.py 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 post.py and in kurve_funcs.py, 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:

 




5 comments:

  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.

    http://kungfumachinist.blogspot.com/

    ReplyDelete
  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.

    ReplyDelete
  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.
    Thanks.

    ReplyDelete
  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.

    BR,

    Alan

    ReplyDelete
  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:
    https://github.com/DavidNicholls/FreeCAD_sf_master/commits/nightly

    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.

    ReplyDelete