GCode Importing To a ToolPath

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

GCode Importing To a ToolPath

Post by CoderMusashi »

Here is some code to take unknown or known GCode from a file and then create a tool path from that gcode. This Script has a FileDialog box pop up so you can select your .txt .ngc .nc or whatever you named your file. Then it turns that files gcode into a tool path.

NOTE Create a new file in freecad then run the script. It needs an Active Document to add to this code wont check to see if you have a Document created or open.

I had to zoom in on my tool Path because it is in English and FreeCad seem to be doing some strange metric thing with its GCode.

Importing GCode to Path Script

Code: Select all

import sys
from PySide import QtGui , QtCore
dialog = QtGui.QFileDialog()
dialog.setFileMode(dialog.AnyFile)
dialog.setViewMode(dialog.Detail)
if dialog.exec_():
    fileName = dialog.selectedFiles()

fname = str(fileName[0])
#end of file dialog creation section

with open(fname, 'r') as data:
    gcode = data.readlines()
    data.close()
    
#turn our gcode varible that is a list into a variable called lines that is a string
lines = ''.join(gcode)

#create a tool path
p = Path.Path()
p.setFromGCode(lines)
o = App.ActiveDocument.addObject("Path::Feature", "myPath") #rename mypath as you see fit
o.Path = p
Just a side not I have to run this script to clean up my gcode created by other software. If your GCode does not have G0, G1, G2's and 3's in every line of code freecad does not like it. It seems every line needs those along with any x,y or z. So if the previous line starts with a G0 and the next line is not a G1, G2 or G3 this script will add a G0 to the line and go to the next line and check again.

Code: Select all

import sys
from PySide import QtGui , QtCore
dialog = QtGui.QFileDialog()
dialog.setFileMode(dialog.AnyFile)
dialog.setViewMode(dialog.Detail)
if dialog.exec_():
    fileNames = dialog.selectedFiles()

fname = str(fileNames[0])
#end of file dialog creation section

#make our new file the same name as old file with a 1 added to the end
fname2 = fname + "1"
with open(fname, 'r') as f:
    with open(fname2, 'a') as updatedFile:
        data = f.readlines()
        f.close()

        for line in data:
            if line.startswith('('):
                pass
            elif "T" in line:
                #print(line)
                pass
            elif line.startswith('G0'):
                lgcode = 'G0'
                updatedFile.write(line)
            elif line.startswith('G1'):
                lgcode = 'G1'
                updatedFile.write(line)
            elif line.startswith('G2'):
                lgcode = 'G2'
                updatedFile.write(line)
            elif line.startswith('G3'):
                lgcode = 'G3'
                updatedFile.write(line)
                #pass
            elif line.startswith('X'):
                #add the last G Code to our line of Gcode 
                updatedFile.write(lgcode + " " + line)
            elif line.startswith('Y'):
                updatedFile.write(lgcode + " " + line)
            elif line.startswith('Z'):
                updatedFile.write(lgcode + " " + line)
Last edited by CoderMusashi on Tue Dec 24, 2019 9:05 am, edited 1 time in total.
chrisb
Veteran
Posts: 54150
Joined: Tue Mar 17, 2015 9:14 am

Re: GCode Importing To a ToolPath

Post by chrisb »

Is this different from the File->Import of GCode?
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: GCode Importing To a ToolPath

Post by CoderMusashi »

It is very much the same but it is still different. The biggest difference is I can't control the one and the other one is code. Plus when you use FreeCads import from the file menu you get to choose a processor and the example_pre is the only one that seems to generate anything visible on my screen. This is a Script and it is in the Python Scripting and macros forum. I am glad FreeCad has both options available to the user.
chrisb
Veteran
Posts: 54150
Joined: Tue Mar 17, 2015 9:14 am

Re: GCode Importing To a ToolPath

Post by chrisb »

I usually don't use the import. I gave it a try with "none" and it worked out of the box.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
sliptonic
Veteran
Posts: 3459
Joined: Tue Oct 25, 2011 10:46 pm
Location: Columbia, Missouri
Contact:

Re: GCode Importing To a ToolPath

Post by sliptonic »

CoderMusashi wrote: Tue Dec 24, 2019 9:14 am It is very much the same but it is still different. The biggest difference is I can't control the one and the other one is code. Plus when you use FreeCads import from the file menu you get to choose a processor and the example_pre is the only one that seems to generate anything visible on my screen. This is a Script and it is in the Python Scripting and macros forum. I am glad FreeCad has both options available to the user.
I appreciate the effort but would honestly prefer an improvement to the import method rather than a new macro that will get lost on the forum. Why not improve the importer?
CoderMusashi
Posts: 92
Joined: Mon Nov 19, 2018 8:26 pm

Re: GCode Importing To a ToolPath

Post by CoderMusashi »

I will give out my python code all day long but I do not want to ever compile another piece of software ever in my life. I don't even know where i would begin. Honestly though I don't feel I am up to the task of helping contribute to the code under the hood of FreeCad. I wish I could personally thank each and every member of this community for the work they have done to keep it alive and thriving. Each and every poster to the forum and the people working on their own little slice of this very robust software.
Post Reply