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