From fbb59ae712e3ebf2219f480f799f8c2e1f1ca006 Mon Sep 17 00:00:00 2001 From: Eric Trombly Date: Mon, 16 Mar 2020 10:17:32 -0500 Subject: [PATCH 1/6] initial work for modifying path of imported gcode --- src/Mod/Path/PathScripts/PathCustom.py | 18 +++ src/Mod/Path/PathScripts/post/gcode_pre.py | 124 +++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 src/Mod/Path/PathScripts/post/gcode_pre.py diff --git a/src/Mod/Path/PathScripts/PathCustom.py b/src/Mod/Path/PathScripts/PathCustom.py index 74cb93ba1a..a07343731c 100644 --- a/src/Mod/Path/PathScripts/PathCustom.py +++ b/src/Mod/Path/PathScripts/PathCustom.py @@ -25,9 +25,11 @@ import FreeCAD import FreeCADGui import Path from PySide import QtCore +from copy import copy __doc__ = """Path Custom object and FreeCAD command""" +movecommands = ['G0', 'G00', 'G1', 'G01', 'G2', 'G02', 'G3', 'G03'] # Qt translation handling def translate(context, text, disambig=None): @@ -39,6 +41,8 @@ class ObjectCustom: def __init__(self,obj): obj.addProperty("App::PropertyStringList", "Gcode", "Path", QtCore.QT_TRANSLATE_NOOP("PathCustom", "The gcode to be inserted")) obj.addProperty("App::PropertyLink", "ToolController", "Path", QtCore.QT_TRANSLATE_NOOP("PathCustom", "The tool controller that will be used to calculate the path")) + obj.addProperty("App::PropertyBool", "OperationPlacement", "Path", "Use operation placement") + obj.OperationPlacement = False obj.Proxy = self def __getstate__(self): @@ -54,6 +58,20 @@ class ObjectCustom: s += str(l) if s: path = Path.Path(s) + if obj.OperationPlacement: + for x in range(len(path.Commands)): + if path.Commands[x].Name in movecommands: + base = copy(obj.Placement.Base) + new = path.Commands[x] + if 'X' not in path.Commands[x].Parameters: + base[0] = 0.0 + if 'Y' not in path.Commands[x].Parameters: + base[1] = 0.0 + if 'Z' not in path.Commands[x].Parameters: + base[2] = 0.0 + new.Placement.translate(base) + path.deleteCommand(x) + path.insertCommand(new, x) obj.Path = path diff --git a/src/Mod/Path/PathScripts/post/gcode_pre.py b/src/Mod/Path/PathScripts/post/gcode_pre.py new file mode 100644 index 0000000000..1ec9aaa702 --- /dev/null +++ b/src/Mod/Path/PathScripts/post/gcode_pre.py @@ -0,0 +1,124 @@ +# *************************************************************************** +# * (c) Yorik van Havre (yorik@uncreated.net) 2014 * +# * * +# * This file is part of the FreeCAD CAx development system. * +# * * +# * This program is free software; you can redistribute it and/or modify * +# * it under the terms of the GNU Lesser General Public License (LGPL) * +# * as published by the Free Software Foundation; either version 2 of * +# * the License, or (at your option) any later version. * +# * for detail see the LICENCE text file. * +# * * +# * FreeCAD is distributed in the hope that it will be useful, * +# * but WITHOUT ANY WARRANTY; without even the implied warranty of * +# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +# * GNU Lesser General Public License for more details. * +# * * +# * You should have received a copy of the GNU Library General Public * +# * License along with FreeCAD; if not, write to the Free Software * +# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +# * USA * +# * * +# ***************************************************************************/ + + +''' +This is an example preprocessor file for the Path workbench. Its aim is to +open a gcode file, parse its contents, and create the appropriate objects +in FreeCAD. + +Read the Path Workbench documentation to know how to create Path objects +from GCode. +''' + +import os +import Path +import FreeCAD +import PathScripts.PathUtils +import PathScripts.PathLog as PathLog + +# LEVEL = PathLog.Level.DEBUG +LEVEL = PathLog.Level.INFO +PathLog.setLevel(LEVEL, PathLog.thisModule()) + +if LEVEL == PathLog.Level.DEBUG: + PathLog.trackModule(PathLog.thisModule()) + + +# to distinguish python built-in open function from the one declared below +if open.__module__ in ['__builtin__', 'io']: + pythonopen = open + + +def open(filename): + "called when freecad opens a file." + PathLog.track(filename) + docname = os.path.splitext(os.path.basename(filename))[0] + doc = FreeCAD.newDocument(docname) + insert(filename, doc.Name) + + +def insert(filename, docname): + "called when freecad imports a file" + PathLog.track(filename) + gfile = pythonopen(filename) + gcode = gfile.read() + gfile.close() + gcode = parse(gcode) + doc = FreeCAD.getDocument(docname) + obj = FreeCAD.ActiveDocument.addObject("Path::FeaturePython", "Custom") + PathScripts.PathCustom.ObjectCustom(obj) + obj.ViewObject.Proxy = 0 + obj.Gcode = gcode + PathScripts.PathUtils.addToJob(obj) + obj.ToolController = PathScripts.PathUtils.findToolController(obj) + FreeCAD.ActiveDocument.recompute() + + +def parse(inputstring): + "parse(inputstring): returns a parsed output string" + print("preprocessing...") + print(inputstring) + PathLog.track(inputstring) + # split the input by line + lines = inputstring.split("\n") + output = "" + lastcommand = None + print(lines) + + for lin in lines: + # remove any leftover trailing and preceding spaces + lin = lin.strip() + if not lin: + # discard empty lines + continue + if lin[0].upper() in ["N"]: + # remove line numbers + lin = lin.split(" ", 1) + if len(lin) >= 1: + lin = lin[1] + else: + continue + + if lin[0] in ["(", "%", "#", ";"]: + # discard comment and other non strictly gcode lines + continue + if lin[0].upper() in ["G", "M"]: + # found a G or M command: we store it + output += lin + "\n" + last = lin[0].upper() + for c in lin[1:]: + if not c.isdigit(): + break + else: + last += c + lastcommand = last + elif lastcommand: + # no G or M command: we repeat the last one + output += lastcommand + " " + lin + "\n" + + print("done preprocessing.") + return output + + +print(__name__ + " gcode preprocessor loaded.") From 31b17055f1327b2760e0b9e2e39560d1633375fe Mon Sep 17 00:00:00 2001 From: Eric Trombly Date: Mon, 16 Mar 2020 10:17:32 -0500 Subject: [PATCH 2/6] initial work for modifying path of imported gcode --- src/Mod/Path/PathScripts/PathCustom.py | 18 +++ src/Mod/Path/PathScripts/post/gcode_pre.py | 124 +++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 src/Mod/Path/PathScripts/post/gcode_pre.py diff --git a/src/Mod/Path/PathScripts/PathCustom.py b/src/Mod/Path/PathScripts/PathCustom.py index 74cb93ba1a..a07343731c 100644 --- a/src/Mod/Path/PathScripts/PathCustom.py +++ b/src/Mod/Path/PathScripts/PathCustom.py @@ -25,9 +25,11 @@ import FreeCAD import FreeCADGui import Path from PySide import QtCore +from copy import copy __doc__ = """Path Custom object and FreeCAD command""" +movecommands = ['G0', 'G00', 'G1', 'G01', 'G2', 'G02', 'G3', 'G03'] # Qt translation handling def translate(context, text, disambig=None): @@ -39,6 +41,8 @@ class ObjectCustom: def __init__(self,obj): obj.addProperty("App::PropertyStringList", "Gcode", "Path", QtCore.QT_TRANSLATE_NOOP("PathCustom", "The gcode to be inserted")) obj.addProperty("App::PropertyLink", "ToolController", "Path", QtCore.QT_TRANSLATE_NOOP("PathCustom", "The tool controller that will be used to calculate the path")) + obj.addProperty("App::PropertyBool", "OperationPlacement", "Path", "Use operation placement") + obj.OperationPlacement = False obj.Proxy = self def __getstate__(self): @@ -54,6 +58,20 @@ class ObjectCustom: s += str(l) if s: path = Path.Path(s) + if obj.OperationPlacement: + for x in range(len(path.Commands)): + if path.Commands[x].Name in movecommands: + base = copy(obj.Placement.Base) + new = path.Commands[x] + if 'X' not in path.Commands[x].Parameters: + base[0] = 0.0 + if 'Y' not in path.Commands[x].Parameters: + base[1] = 0.0 + if 'Z' not in path.Commands[x].Parameters: + base[2] = 0.0 + new.Placement.translate(base) + path.deleteCommand(x) + path.insertCommand(new, x) obj.Path = path diff --git a/src/Mod/Path/PathScripts/post/gcode_pre.py b/src/Mod/Path/PathScripts/post/gcode_pre.py new file mode 100644 index 0000000000..1ec9aaa702 --- /dev/null +++ b/src/Mod/Path/PathScripts/post/gcode_pre.py @@ -0,0 +1,124 @@ +# *************************************************************************** +# * (c) Yorik van Havre (yorik@uncreated.net) 2014 * +# * * +# * This file is part of the FreeCAD CAx development system. * +# * * +# * This program is free software; you can redistribute it and/or modify * +# * it under the terms of the GNU Lesser General Public License (LGPL) * +# * as published by the Free Software Foundation; either version 2 of * +# * the License, or (at your option) any later version. * +# * for detail see the LICENCE text file. * +# * * +# * FreeCAD is distributed in the hope that it will be useful, * +# * but WITHOUT ANY WARRANTY; without even the implied warranty of * +# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +# * GNU Lesser General Public License for more details. * +# * * +# * You should have received a copy of the GNU Library General Public * +# * License along with FreeCAD; if not, write to the Free Software * +# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +# * USA * +# * * +# ***************************************************************************/ + + +''' +This is an example preprocessor file for the Path workbench. Its aim is to +open a gcode file, parse its contents, and create the appropriate objects +in FreeCAD. + +Read the Path Workbench documentation to know how to create Path objects +from GCode. +''' + +import os +import Path +import FreeCAD +import PathScripts.PathUtils +import PathScripts.PathLog as PathLog + +# LEVEL = PathLog.Level.DEBUG +LEVEL = PathLog.Level.INFO +PathLog.setLevel(LEVEL, PathLog.thisModule()) + +if LEVEL == PathLog.Level.DEBUG: + PathLog.trackModule(PathLog.thisModule()) + + +# to distinguish python built-in open function from the one declared below +if open.__module__ in ['__builtin__', 'io']: + pythonopen = open + + +def open(filename): + "called when freecad opens a file." + PathLog.track(filename) + docname = os.path.splitext(os.path.basename(filename))[0] + doc = FreeCAD.newDocument(docname) + insert(filename, doc.Name) + + +def insert(filename, docname): + "called when freecad imports a file" + PathLog.track(filename) + gfile = pythonopen(filename) + gcode = gfile.read() + gfile.close() + gcode = parse(gcode) + doc = FreeCAD.getDocument(docname) + obj = FreeCAD.ActiveDocument.addObject("Path::FeaturePython", "Custom") + PathScripts.PathCustom.ObjectCustom(obj) + obj.ViewObject.Proxy = 0 + obj.Gcode = gcode + PathScripts.PathUtils.addToJob(obj) + obj.ToolController = PathScripts.PathUtils.findToolController(obj) + FreeCAD.ActiveDocument.recompute() + + +def parse(inputstring): + "parse(inputstring): returns a parsed output string" + print("preprocessing...") + print(inputstring) + PathLog.track(inputstring) + # split the input by line + lines = inputstring.split("\n") + output = "" + lastcommand = None + print(lines) + + for lin in lines: + # remove any leftover trailing and preceding spaces + lin = lin.strip() + if not lin: + # discard empty lines + continue + if lin[0].upper() in ["N"]: + # remove line numbers + lin = lin.split(" ", 1) + if len(lin) >= 1: + lin = lin[1] + else: + continue + + if lin[0] in ["(", "%", "#", ";"]: + # discard comment and other non strictly gcode lines + continue + if lin[0].upper() in ["G", "M"]: + # found a G or M command: we store it + output += lin + "\n" + last = lin[0].upper() + for c in lin[1:]: + if not c.isdigit(): + break + else: + last += c + lastcommand = last + elif lastcommand: + # no G or M command: we repeat the last one + output += lastcommand + " " + lin + "\n" + + print("done preprocessing.") + return output + + +print(__name__ + " gcode preprocessor loaded.") From f1ebaa4cc635f77287e5bbe390b7b8bf30a55780 Mon Sep 17 00:00:00 2001 From: Eric Trombly Date: Fri, 20 Mar 2020 11:21:26 -0500 Subject: [PATCH 3/6] change preprocessor to split lines --- src/Mod/Path/CMakeLists.txt | 1 + src/Mod/Path/PathScripts/PathCustom.py | 34 ++++++++++------------ src/Mod/Path/PathScripts/post/gcode_pre.py | 13 ++++----- 3 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/Mod/Path/CMakeLists.txt b/src/Mod/Path/CMakeLists.txt index b30ca0e472..098bb114dd 100644 --- a/src/Mod/Path/CMakeLists.txt +++ b/src/Mod/Path/CMakeLists.txt @@ -128,6 +128,7 @@ SET(PathScripts_post_SRCS PathScripts/post/comparams_post.py PathScripts/post/dynapath_post.py PathScripts/post/example_pre.py + PathScripts/post/gcode_pre.py PathScripts/post/grbl_post.py PathScripts/post/jtech_post.py PathScripts/post/linuxcnc_post.py diff --git a/src/Mod/Path/PathScripts/PathCustom.py b/src/Mod/Path/PathScripts/PathCustom.py index a07343731c..4be7e94314 100644 --- a/src/Mod/Path/PathScripts/PathCustom.py +++ b/src/Mod/Path/PathScripts/PathCustom.py @@ -52,27 +52,23 @@ class ObjectCustom: return None def execute(self, obj): + commands = [] + newPath = Path.Path if obj.Gcode: - s = "" for l in obj.Gcode: - s += str(l) - if s: - path = Path.Path(s) - if obj.OperationPlacement: - for x in range(len(path.Commands)): - if path.Commands[x].Name in movecommands: - base = copy(obj.Placement.Base) - new = path.Commands[x] - if 'X' not in path.Commands[x].Parameters: - base[0] = 0.0 - if 'Y' not in path.Commands[x].Parameters: - base[1] = 0.0 - if 'Z' not in path.Commands[x].Parameters: - base[2] = 0.0 - new.Placement.translate(base) - path.deleteCommand(x) - path.insertCommand(new, x) - obj.Path = path + newcommand=Path.Command(str(l)) + if newcommand.Name in movecommands and obj.OperationPlacement: + if 'X' in newcommand.Parameters: + newcommand.x += obj.Placement.Base.x + if 'Y' in newcommand.Parameters: + newcommand.y += obj.Placement.Base.y + if 'Z' in newcommand.Parameters: + newcommand.z += obj.Placement.Base.z + + commands.append(newcommand) + newPath.addCommands(commands) + + obj.Path = newPath class CommandPathCustom: diff --git a/src/Mod/Path/PathScripts/post/gcode_pre.py b/src/Mod/Path/PathScripts/post/gcode_pre.py index 1ec9aaa702..0ec421b521 100644 --- a/src/Mod/Path/PathScripts/post/gcode_pre.py +++ b/src/Mod/Path/PathScripts/post/gcode_pre.py @@ -78,13 +78,11 @@ def insert(filename, docname): def parse(inputstring): "parse(inputstring): returns a parsed output string" print("preprocessing...") - print(inputstring) PathLog.track(inputstring) # split the input by line lines = inputstring.split("\n") - output = "" + output = [] #"" lastcommand = None - print(lines) for lin in lines: # remove any leftover trailing and preceding spaces @@ -96,7 +94,7 @@ def parse(inputstring): # remove line numbers lin = lin.split(" ", 1) if len(lin) >= 1: - lin = lin[1] + lin = lin[1].strip() else: continue @@ -105,7 +103,8 @@ def parse(inputstring): continue if lin[0].upper() in ["G", "M"]: # found a G or M command: we store it - output += lin + "\n" + #output += lin + "\n" + output.append(lin) # + "\n" last = lin[0].upper() for c in lin[1:]: if not c.isdigit(): @@ -115,10 +114,10 @@ def parse(inputstring): lastcommand = last elif lastcommand: # no G or M command: we repeat the last one - output += lastcommand + " " + lin + "\n" + output.append(lastcommand + " " + lin) # + "\n" print("done preprocessing.") return output -print(__name__ + " gcode preprocessor loaded.") +print(__name__ + " gcode preprocessor loaded.") \ No newline at end of file From 7f76c5fbc3ad3075b67752bec2a81a7ddc30c8c2 Mon Sep 17 00:00:00 2001 From: Eric Trombly Date: Sun, 22 Mar 2020 12:05:55 -0500 Subject: [PATCH 4/6] split paths on M6 and change placement to use offset --- src/Mod/Path/PathScripts/PathCustom.py | 37 ++++++++++---------- src/Mod/Path/PathScripts/post/example_pre.py | 13 ++++--- src/Mod/Path/PathScripts/post/gcode_pre.py | 25 ++++++++----- 3 files changed, 41 insertions(+), 34 deletions(-) diff --git a/src/Mod/Path/PathScripts/PathCustom.py b/src/Mod/Path/PathScripts/PathCustom.py index 4be7e94314..d78bffe360 100644 --- a/src/Mod/Path/PathScripts/PathCustom.py +++ b/src/Mod/Path/PathScripts/PathCustom.py @@ -25,12 +25,13 @@ import FreeCAD import FreeCADGui import Path from PySide import QtCore -from copy import copy + __doc__ = """Path Custom object and FreeCAD command""" movecommands = ['G0', 'G00', 'G1', 'G01', 'G2', 'G02', 'G3', 'G03'] + # Qt translation handling def translate(context, text, disambig=None): return QtCore.QCoreApplication.translate(context, text, disambig) @@ -38,11 +39,13 @@ def translate(context, text, disambig=None): class ObjectCustom: - def __init__(self,obj): - obj.addProperty("App::PropertyStringList", "Gcode", "Path", QtCore.QT_TRANSLATE_NOOP("PathCustom", "The gcode to be inserted")) - obj.addProperty("App::PropertyLink", "ToolController", "Path", QtCore.QT_TRANSLATE_NOOP("PathCustom", "The tool controller that will be used to calculate the path")) - obj.addProperty("App::PropertyBool", "OperationPlacement", "Path", "Use operation placement") - obj.OperationPlacement = False + def __init__(self, obj): + obj.addProperty("App::PropertyStringList", "Gcode", "Path", + QtCore.QT_TRANSLATE_NOOP("PathCustom", "The gcode to be inserted")) + obj.addProperty("App::PropertyLink", "ToolController", "Path", + QtCore.QT_TRANSLATE_NOOP("PathCustom", "The tool controller that will be used to calculate the path")) + obj.addProperty("App::PropertyPlacement", "Offset", "Path", + "Placement Offset") obj.Proxy = self def __getstate__(self): @@ -52,23 +55,21 @@ class ObjectCustom: return None def execute(self, obj): - commands = [] - newPath = Path.Path + newpath = Path.Path() if obj.Gcode: for l in obj.Gcode: - newcommand=Path.Command(str(l)) - if newcommand.Name in movecommands and obj.OperationPlacement: + newcommand = Path.Command(str(l)) + if newcommand.Name in movecommands: if 'X' in newcommand.Parameters: - newcommand.x += obj.Placement.Base.x + newcommand.x += obj.Offset.Base.x if 'Y' in newcommand.Parameters: - newcommand.y += obj.Placement.Base.y + newcommand.y += obj.Offset.Base.y if 'Z' in newcommand.Parameters: - newcommand.z += obj.Placement.Base.z + newcommand.z += obj.Offset.Base.z - commands.append(newcommand) - newPath.addCommands(commands) + newpath.insertCommand(newcommand) - obj.Path = newPath + obj.Path=newpath class CommandPathCustom: @@ -89,7 +90,7 @@ class CommandPathCustom: FreeCAD.ActiveDocument.openTransaction("Create Custom Path") FreeCADGui.addModule("PathScripts.PathCustom") FreeCADGui.addModule("PathScripts.PathUtils") - FreeCADGui.doCommand('obj = FreeCAD.ActiveDocument.addObject("Path::FeaturePython","Custom")') + FreeCADGui.doCommand('obj = FreeCAD.ActiveDocument.addObject("Path::FeaturePython", "Custom")') FreeCADGui.doCommand('PathScripts.PathCustom.ObjectCustom(obj)') FreeCADGui.doCommand('obj.ViewObject.Proxy = 0') FreeCADGui.doCommand('PathScripts.PathUtils.addToJob(obj)') @@ -100,4 +101,4 @@ class CommandPathCustom: if FreeCAD.GuiUp: # register the FreeCAD command - FreeCADGui.addCommand('Path_Custom', CommandPathCustom()) + FreeCADGui.addCommand('Path_Custom', CommandPathCustom()) \ No newline at end of file diff --git a/src/Mod/Path/PathScripts/post/example_pre.py b/src/Mod/Path/PathScripts/post/example_pre.py index 5928c30301..6e90d556b9 100644 --- a/src/Mod/Path/PathScripts/post/example_pre.py +++ b/src/Mod/Path/PathScripts/post/example_pre.py @@ -73,13 +73,11 @@ def insert(filename, docname): def parse(inputstring): "parse(inputstring): returns a parsed output string" print("preprocessing...") - print(inputstring) PathLog.track(inputstring) # split the input by line lines = inputstring.split("\n") - output = "" - lastcommand = None - print(lines) + output = [] #"" + lastcommand = None for lin in lines: # remove any leftover trailing and preceding spaces @@ -91,7 +89,7 @@ def parse(inputstring): # remove line numbers lin = lin.split(" ", 1) if len(lin) >= 1: - lin = lin[1] + lin = lin[1].strip() else: continue @@ -100,7 +98,8 @@ def parse(inputstring): continue if lin[0].upper() in ["G", "M"]: # found a G or M command: we store it - output += lin + "\n" + #output += lin + "\n" + output.append(lin) # + "\n" last = lin[0].upper() for c in lin[1:]: if not c.isdigit(): @@ -110,7 +109,7 @@ def parse(inputstring): lastcommand = last elif lastcommand: # no G or M command: we repeat the last one - output += lastcommand + " " + lin + "\n" + output.append(lastcommand + " " + lin) # + "\n" print("done preprocessing.") return output diff --git a/src/Mod/Path/PathScripts/post/gcode_pre.py b/src/Mod/Path/PathScripts/post/gcode_pre.py index 0ec421b521..1b3eee4b49 100644 --- a/src/Mod/Path/PathScripts/post/gcode_pre.py +++ b/src/Mod/Path/PathScripts/post/gcode_pre.py @@ -36,6 +36,7 @@ import Path import FreeCAD import PathScripts.PathUtils import PathScripts.PathLog as PathLog +import re # LEVEL = PathLog.Level.DEBUG LEVEL = PathLog.Level.INFO @@ -64,14 +65,20 @@ def insert(filename, docname): gfile = pythonopen(filename) gcode = gfile.read() gfile.close() - gcode = parse(gcode) - doc = FreeCAD.getDocument(docname) - obj = FreeCAD.ActiveDocument.addObject("Path::FeaturePython", "Custom") - PathScripts.PathCustom.ObjectCustom(obj) - obj.ViewObject.Proxy = 0 - obj.Gcode = gcode - PathScripts.PathUtils.addToJob(obj) - obj.ToolController = PathScripts.PathUtils.findToolController(obj) + # split on tool changes + paths = re.split('(?=[mM]+\s?0?6)', gcode) + # if there are any tool changes combine the preamble with the default tool + if len(paths) > 1: + paths = ["\n".join(paths[0:2])] + paths[2:] + for path in paths: + gcode = parse(path) + doc = FreeCAD.getDocument(docname) + obj = FreeCAD.ActiveDocument.addObject("Path::FeaturePython", "Custom") + PathScripts.PathCustom.ObjectCustom(obj) + obj.ViewObject.Proxy = 0 + obj.Gcode = gcode + PathScripts.PathUtils.addToJob(obj) + obj.ToolController = PathScripts.PathUtils.findToolController(obj) FreeCAD.ActiveDocument.recompute() @@ -82,7 +89,7 @@ def parse(inputstring): # split the input by line lines = inputstring.split("\n") output = [] #"" - lastcommand = None + lastcommand = None for lin in lines: # remove any leftover trailing and preceding spaces From f66747ec15eb9fc6200e1ea9c23207d4546f0db7 Mon Sep 17 00:00:00 2001 From: Eric Trombly Date: Sun, 22 Mar 2020 12:15:57 -0500 Subject: [PATCH 5/6] split paths on M6 and change placement to use offset --- src/Mod/Path/PathScripts/post/gcode_pre.py | 41 ---------------------- 1 file changed, 41 deletions(-) diff --git a/src/Mod/Path/PathScripts/post/gcode_pre.py b/src/Mod/Path/PathScripts/post/gcode_pre.py index 80a0753a78..053c39c229 100644 --- a/src/Mod/Path/PathScripts/post/gcode_pre.py +++ b/src/Mod/Path/PathScripts/post/gcode_pre.py @@ -36,10 +36,7 @@ import Path import FreeCAD import PathScripts.PathUtils import PathScripts.PathLog as PathLog -<<<<<<< HEAD import re -======= ->>>>>>> fbb59ae712e3ebf2219f480f799f8c2e1f1ca006 # LEVEL = PathLog.Level.DEBUG LEVEL = PathLog.Level.INFO @@ -68,7 +65,6 @@ def insert(filename, docname): gfile = pythonopen(filename) gcode = gfile.read() gfile.close() -<<<<<<< HEAD # split on tool changes paths = re.split('(?=[mM]+\s?0?6)', gcode) # if there are any tool changes combine the preamble with the default tool @@ -83,37 +79,17 @@ def insert(filename, docname): obj.Gcode = gcode PathScripts.PathUtils.addToJob(obj) obj.ToolController = PathScripts.PathUtils.findToolController(obj) -======= - gcode = parse(gcode) - doc = FreeCAD.getDocument(docname) - obj = FreeCAD.ActiveDocument.addObject("Path::FeaturePython", "Custom") - PathScripts.PathCustom.ObjectCustom(obj) - obj.ViewObject.Proxy = 0 - obj.Gcode = gcode - PathScripts.PathUtils.addToJob(obj) - obj.ToolController = PathScripts.PathUtils.findToolController(obj) ->>>>>>> fbb59ae712e3ebf2219f480f799f8c2e1f1ca006 FreeCAD.ActiveDocument.recompute() def parse(inputstring): "parse(inputstring): returns a parsed output string" print("preprocessing...") -<<<<<<< HEAD PathLog.track(inputstring) # split the input by line lines = inputstring.split("\n") output = [] #"" lastcommand = None -======= - print(inputstring) - PathLog.track(inputstring) - # split the input by line - lines = inputstring.split("\n") - output = "" - lastcommand = None - print(lines) ->>>>>>> fbb59ae712e3ebf2219f480f799f8c2e1f1ca006 for lin in lines: # remove any leftover trailing and preceding spaces @@ -125,11 +101,7 @@ def parse(inputstring): # remove line numbers lin = lin.split(" ", 1) if len(lin) >= 1: -<<<<<<< HEAD lin = lin[1].strip() -======= - lin = lin[1] ->>>>>>> fbb59ae712e3ebf2219f480f799f8c2e1f1ca006 else: continue @@ -138,12 +110,8 @@ def parse(inputstring): continue if lin[0].upper() in ["G", "M"]: # found a G or M command: we store it -<<<<<<< HEAD #output += lin + "\n" output.append(lin) # + "\n" -======= - output += lin + "\n" ->>>>>>> fbb59ae712e3ebf2219f480f799f8c2e1f1ca006 last = lin[0].upper() for c in lin[1:]: if not c.isdigit(): @@ -153,18 +121,9 @@ def parse(inputstring): lastcommand = last elif lastcommand: # no G or M command: we repeat the last one -<<<<<<< HEAD output.append(lastcommand + " " + lin) # + "\n" -======= - output += lastcommand + " " + lin + "\n" ->>>>>>> fbb59ae712e3ebf2219f480f799f8c2e1f1ca006 print("done preprocessing.") return output - -<<<<<<< HEAD print(__name__ + " gcode preprocessor loaded.") -======= -print(__name__ + " gcode preprocessor loaded.") ->>>>>>> fbb59ae712e3ebf2219f480f799f8c2e1f1ca006 From 8756ae8590411f71a343c92d7064567176629a48 Mon Sep 17 00:00:00 2001 From: Eric Trombly Date: Sun, 22 Mar 2020 17:59:16 -0500 Subject: [PATCH 6/6] Change example_pre to use a list of commands instead of one string --- src/Mod/Path/PathScripts/post/example_pre.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Mod/Path/PathScripts/post/example_pre.py b/src/Mod/Path/PathScripts/post/example_pre.py index 6e90d556b9..48ceb433bf 100644 --- a/src/Mod/Path/PathScripts/post/example_pre.py +++ b/src/Mod/Path/PathScripts/post/example_pre.py @@ -99,7 +99,7 @@ def parse(inputstring): if lin[0].upper() in ["G", "M"]: # found a G or M command: we store it #output += lin + "\n" - output.append(lin) # + "\n" + output.append(Path.Command(str(lin))) # + "\n" last = lin[0].upper() for c in lin[1:]: if not c.isdigit(): @@ -109,7 +109,7 @@ def parse(inputstring): lastcommand = last elif lastcommand: # no G or M command: we repeat the last one - output.append(lastcommand + " " + lin) # + "\n" + output.append(Path.Command(str(lastcommand + " " + lin))) # + "\n" print("done preprocessing.") return output