Dxf hole patterns straight to Helical GCode

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
CoderMusashi
Posts: 92
Joined: Mon Nov 19, 2018 8:26 pm

Dxf hole patterns straight to Helical GCode

Post by CoderMusashi »

This Script takes all the holes imported from a DXF and generates GCode for helical milling cycles.I created this for doing counter bores mostly.
Step1. Create a new Document
Step2. Import A DXF file
Step3 Run the HelicalDxfToGcode macro and it will create the GCode
NOTE: This is for DXF files with HOLES / Circles Only not meant to deal with other features like lines slim down your DXF to just holes

The Cincinnati Acramatic A2100 Cnc mill I use has a helical can cycle like most CNC controllers do. My Post for my CadCam software does not do helical boring but drops down the zdepth cuts the circular path then drops down again and cuts the circular path. This creates a lot of code and just is not cool. I have a variable program I leave in my machine just for helical boring making life easier for quick tasks. After doing a bunch of holes using my CadCam software I thought it would be nice to be able to import a DXF file with just the holes, run a script and generate the GCode for using my machines helical milling capabilities. No worries FreeCAD to the rescue again. Freecad really is not suited for working with DXF and 2D tool paths generally. Freecad is for 3D modeling and is geared towards creating tool paths from 3D models and not simple 2D stuff. Freecad is so amazing we can do amazing things like Create a new freecad document, import a DXF file making sure your Scale factor is set to 25.4 for inch users then run a script to generate g code from the imported dxf. The Script can be modified to work with most machines. In my case the difference between cutting a circle and helical boring is a matter of adding a z and k value to my G3 line of code. Hope the community finds this helpful. Note Change the file variable in the attached FreeCad macro to save your Gcode where you want it. I also attached a dxf file to import and try the script on. When importing a DXF the X0 Y0 origin will be based on the software that created the dxf. In my case the upper left hand corner is usually my X0,Y0 and Z 0 is top of my part. While you are at it check out the real power of FeatureArea https://forum.freecadweb.org/viewtopic.php?f=36&t=41781

Code: Select all

from PySide import QtCore, QtGui
import sys

#Change the file variable for your needs
#variable that defines our filename and location
file = "C:\\Users\\NameofUser\\Documents\\helix.NC"

#create a simple input dialog box to get zdepth
zd, okPressed = QtGui.QInputDialog.getText(None, 'Z Cut Depth', '.1')
#create a simple input dialog box to get cuts per pass
cperp, okPressed = QtGui.QInputDialog.getText(None, 'Cut Depth per pass', '.015')
#create a simple input dialog box to get the tool diameter
td, okPressed = QtGui.QInputDialog.getText(None, 'Tool Diameter', '.25')

#variables for GCode stuff
zdepth = float(zd)
cutperpass = float(cperp)
tooldia = float(td)/ 2

#variable to hold all objects in the active document
objects = App.ActiveDocument.Objects

#set up list variables to hold x location y location and hole radius size
x=[]
y=[]
rads =[]

#loop through all the objects setting the data for the obj lists
for obj in objects:
    #loc holds 3 values x, y, z vectors of current object being iterated through
    loc = obj.Shape.Curve.Location
    #rads holds 1 vlaue the radius of the current object being iterated through
    rads.append(obj.Shape.Curve.Radius)

    #check to make sure tool is not bigger than the hole diameter
    for r in rads:
        if r < tooldia:
          print("tool diameter is to big for helical bore")
          sys.exit()
        else:
            pass
   #i is for counting to as we go through the 3 values loc holds
    i=0
    for v in loc:
        if i == 0:
            #add this objects x location to our x list variable
            x.append (v)
        elif i == 1:
            #add this objects y location to our y list variable
            y.append(v)
        else:
            # we dont care about the objects z location so pass and do nothing
            pass
       #add 1 to i so we can keep track of which 3 values of the loc variable we are looping through
        i = i+1

#Create GCode Header
with open(file, 'w+') as f:
    f.write("(MSG, Created with FreeCAD)\n")
    f.write(":G90 G70 G40 G94 \n")
    f.write(":T20 M6 \n")
    f.write("M3 S1800 \n")
    f.close
#Majority of the helix bore GCode
#Loop through our x, y and radius list
for i in range(len(x)):
    #setup our variable to temporarily hold values from our list x,y,and radius variables
    xcenter = x[i]
    ycenter = y[i]
    radius = rads[i]
    xstart = (radius-tooldia) + xcenter
    with open(file, 'a+') as f:
        # .3f formats the number to round to 3 decimal places
        f.write("G0 X{0:.3f} Y{1:.3f} \n".format(xstart,ycenter))
        f.write("G0 Z.1 \n")
        f.write("G1 Z.005 \n")
        f.write("G3 X{0:.3f} Y{1:.3f} Z-{2:.3f} K{3:.3f} I{4:.3f} J{5:.3f} F20 \n".format(xstart,ycenter,zdepth,cutperpass,xcenter,ycenter))
        #once at the bottom go to the hole center and make a clean z level circular path before exiting the hole
        f.write("G1 X{0:.3f} Y{1:.3f} \n".format(xcenter,ycenter))
        f.write("G1 X{0:.3f} Y{1:.3f} \n".format(xstart,ycenter))
        f.write("G3 X{0:.3f} Y{1:.3f} I{2:.3f} J{3:.3f} F20 \n".format(xstart,ycenter,xcenter,ycenter))
        f.write("G1 X{0:.3f} Y{1:.3f} \n".format(xcenter,ycenter))
        f.write("G0 Z1.0 \n")
        f.close

#End of GCode
with open(file, 'a+') as f:
    f.write("M9 M5 \n")
    f.write("M26 \n")
    f.write("G0 Y8. \n")
    f.write("M0 \n")
    f.close
HelicalDxfToGCode.FCMacro
(3.35 KiB) Downloaded 53 times
testholes.dxf
(40.36 KiB) Downloaded 50 times
Last edited by CoderMusashi on Wed Dec 18, 2019 9:07 am, edited 3 times in total.
chrisb
Veteran
Posts: 53934
Joined: Tue Mar 17, 2015 9:14 am

Re: Dxf hole patterns straight to Helical GCode

Post by chrisb »

I have opened a dxf file, which was converted to several shapes. I selected one of the circular shapes and called the macro. I confirmed all the proposed defaults and got finally an error:

Code: Select all

Traceback (most recent call last):
  File "/tmp/HelicalDxfToGCode.FCMacro", line 16, in <module>
    zdepth = float(zd)
<class 'ValueError'>: could not convert string to float: 
OS: macOS High Sierra (10.13)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.18811 (Git)
Build type: Release
Branch: master
Hash: b809867021deb37be1039dab37b9fe1cafab254e
Python version: 3.7.3
Qt version: 5.12.5
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/Germany (en_DE)
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
CoderMusashi
Posts: 92
Joined: Mon Nov 19, 2018 8:26 pm

Re: Dxf hole patterns straight to Helical GCode

Post by CoderMusashi »

Thanks chrisb for checking this out. You do not need to select any of the circles the code is designed to select them all automatically. Did you use a negative number for your z depth? Use all positive numbers and do not add thou, mm or inch at the end of the number like you would in other interfaces. Also the tool diameter needs to be less than .350 if your trying out the attached dxf file. I am not sure why python can't convert the returned str into a floating point. This was created and tested using this version. I also imported the dxf into a new document instead of just using the open file dialog. I also tried this using version 19 on a ubuntu linux machine as well as a windows machine. I do not have a mac to test it on.

OS: Windows 10
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.16110 (Git)
Build type: Release
Branch: (HEAD detached at upstream/releases/FreeCAD-0-18)
Hash: f7dccfaa909e5b9da26bf50c4a22ccca9bb10c40
Python version: 3.6.6
Qt version: 5.6.2
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/UnitedStates (en_US)
chrisb
Veteran
Posts: 53934
Joined: Tue Mar 17, 2015 9:14 am

Re: Dxf hole patterns straight to Helical GCode

Post by chrisb »

I had used the dxf files attached to this post: https://forum.freecadweb.org/viewtopic.php?f=3&t=41437.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
chrisb
Veteran
Posts: 53934
Joined: Tue Mar 17, 2015 9:14 am

Re: Dxf hole patterns straight to Helical GCode

Post by chrisb »

Same situation with your testfile. On first try I simply used the defaults. Then I retried with a smaller diameter, to no avail. If you say that the diameter should be smaller than 0.35, then it would be very convenient if you either change the default or the example, so that at least that example work with the defaults.

You should also consider different locales, such as the german, where the decimal separator is a comma. Perhaps that is the culprit of the parse error.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
CoderMusashi
Posts: 92
Joined: Mon Nov 19, 2018 8:26 pm

Re: Dxf hole patterns straight to Helical GCode

Post by CoderMusashi »

I set the default tool diameter but forgot the dxf i was testing with had holes less than the diameter of the tool. I will correct it in the code section but not in the uploaded file. Not sure how the comma or period thing works for the german version. Also this was not made for DXF files with other features but DXF with just holes / circles. I also made another version for just doing a line or edge (simple profile) but have not released it until I see how people got along with this particular macro.
chrisb
Veteran
Posts: 53934
Joined: Tue Mar 17, 2015 9:14 am

Re: Dxf hole patterns straight to Helical GCode

Post by chrisb »

Instead of investing more effort in your macro you should join the development of Path workbench. As far as I can see can the profile operation with a ramp dressup already do what you develop here. And it works.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
CoderMusashi
Posts: 92
Joined: Mon Nov 19, 2018 8:26 pm

Re: Dxf hole patterns straight to Helical GCode

Post by CoderMusashi »

Path workbench is great and does need more development. I am not doing anything else to this macro and it took me like 2 hours to create it while my machines are running. I use Matercam, Solidworks, Fushion360 (python included) and BobCad on top of playing with FreeCAD and none of them are the best and are good for different uses. I also use them all when VXelements does not do what I want when 3d Scanning stuff with my HandyScan 700. Currently Freecad and the other software I use do not post exactly for my needs but this script does every time and I can modify it on the fly. I get what you are saying but when Profile based on edges is broken in your pathworkbench and all you need is a quick simple 2D operation Macros like this are the way to go in my honest opinion. I don't want to spend 10 minutes plus when all I need is a quick tool path. I have several variable programs like this macro in my machine that I use religiously to square stock, cut a quick circle or counterbore instead of using my CAD/CAM software. Change a few variable and the same program i used for a 6" block is now facing a 4" block with a smaller diameter tool etc... This was just me killing time at work and thought I would share it with the community. I'd like to help with the path workbench but I am not a cpp fan but love, love, love python. Any suggestions on what and where I can help the Path Developers just PM me.
User avatar
freman
Veteran
Posts: 2201
Joined: Tue Nov 27, 2018 10:30 pm

Re: Dxf hole patterns straight to Helical GCode

Post by freman »

most of the Path WB tools are 2.5D capable and are based on a common "AreaOp" .

Good news: all of this is very clear, well-written code done by sliptonic and is written in python, so if you are comfortable with that it should be no problem.

Having been confused by some of the behaviour of these tools for a while I've just realised that how they work is to project whatever area is selected onto the horizontal plane; take the lowest point of the selected area to define the z of the projected plane. The various functions ( contour, profile,pocket ... ) then operated based on this projected plane.

If the user selected face/surface/edges are not all in the horizontal this can produce some odd results. I reality this is just for simple 2.5D cuts on horizontal planes.

Understanding that in advance may help you see what is going on there. Though getting to grips with a fairly substantial block of code will probably involve a fair amount of time before being able to modify it to do something new.
Post Reply