diff --git a/src/Mod/CAM/CAMTests/TestRefactoredTestPost.py b/src/Mod/CAM/CAMTests/TestRefactoredTestPost.py index 4cd48772ec..c781bdce76 100644 --- a/src/Mod/CAM/CAMTests/TestRefactoredTestPost.py +++ b/src/Mod/CAM/CAMTests/TestRefactoredTestPost.py @@ -1115,6 +1115,42 @@ G54 ############################################################################# + def test00191(self): + """Make sure postprocessor doesn't crash on blank lines""" + + path = [ + Path.Command("G0 X1"), + Path.Command(""), + Path.Command("G0 X2"), + ] + + self.post.values["OUTPUT_BLANK_LINES"] = True + self.multi_compare( + path, + """G90 +G21 +G54 +G0 X1.000 + +G0 X2.000 +""", + "", + ) + + self.post.values["OUTPUT_BLANK_LINES"] = False + self.multi_compare( + path, + """G90 +G21 +G54 +G0 X1.000 +G0 X2.000 +""", + "", + ) + + ############################################################################# + def test00200(self): """Test Outputting visible arguments. diff --git a/src/Mod/CAM/Path/Op/Gui/Custom.py b/src/Mod/CAM/Path/Op/Gui/Custom.py index fa88d57278..35f93160ba 100644 --- a/src/Mod/CAM/Path/Op/Gui/Custom.py +++ b/src/Mod/CAM/Path/Op/Gui/Custom.py @@ -61,7 +61,7 @@ class TaskPanelOpPage(PathOpGui.TaskPanelPage): return signals def setGCode(self): - self.obj.Gcode = self.form.txtGCode.toPlainText().splitlines() + self.obj.Gcode = self.form.txtGCode.toPlainText().split("\n") Command = PathOpGui.SetupOperation( diff --git a/src/Mod/CAM/Path/Post/UtilsArguments.py b/src/Mod/CAM/Path/Post/UtilsArguments.py index 924d1f4084..fb13aa7574 100644 --- a/src/Mod/CAM/Path/Post/UtilsArguments.py +++ b/src/Mod/CAM/Path/Post/UtilsArguments.py @@ -513,6 +513,10 @@ def init_shared_values(values: Values) -> None: # values["OUTPUT_COMMENTS"] = True # + # If True output blank lines. If False blank lines are suppressed. + # + values["OUTPUT_BLANK_LINES"] = True + # # if False duplicate axis values or feeds are suppressed # if they are the same as the previous line. # diff --git a/src/Mod/CAM/Path/Post/UtilsParse.py b/src/Mod/CAM/Path/Post/UtilsParse.py index 49831fcee0..08d4a6accf 100644 --- a/src/Mod/CAM/Path/Post/UtilsParse.py +++ b/src/Mod/CAM/Path/Post/UtilsParse.py @@ -707,12 +707,18 @@ def parse_a_path(values: Values, gcode: Gcode, pathobj) -> None: command = c.Name command_line = [] + # Skip blank lines if requested + if not command: + if not values["OUTPUT_BLANK_LINES"]: + continue + # Modify the command name if necessary - if command[0] == "(": + if command.startswith("("): if not values["OUTPUT_COMMENTS"]: continue if values["COMMENT_SYMBOL"] != "(" and len(command) > 2: command = create_comment(values, command[1:-1]) + cmd = check_for_an_adaptive_op(values, command, command_line, adaptive_op_variables) if cmd: command = cmd diff --git a/src/Mod/CAM/Path/Post/scripts/KineticNCBeamicon2_post.py b/src/Mod/CAM/Path/Post/scripts/KineticNCBeamicon2_post.py index 3008d6e032..d3334aeff6 100644 --- a/src/Mod/CAM/Path/Post/scripts/KineticNCBeamicon2_post.py +++ b/src/Mod/CAM/Path/Post/scripts/KineticNCBeamicon2_post.py @@ -344,7 +344,7 @@ def parse(pathobj): if command == lastcommand: outstring.pop(0) - if c.Name[0] == "(" and not OUTPUT_COMMENTS: # command is a comment + if c.Name.startswith("(") and not OUTPUT_COMMENTS: # command is a comment continue # Now add the remaining parameters in order diff --git a/src/Mod/CAM/Path/Post/scripts/centroid_post.py b/src/Mod/CAM/Path/Post/scripts/centroid_post.py index 49c58d1137..8bf2660401 100644 --- a/src/Mod/CAM/Path/Post/scripts/centroid_post.py +++ b/src/Mod/CAM/Path/Post/scripts/centroid_post.py @@ -282,7 +282,7 @@ def parse(pathobj): commandlist = [] # list of elements in the command, code and params. command = c.Name # command M or G code or comment string - if command[0] == "(": + if command.startswith("("): command = PostUtils.fcoms(command, COMMENT) commandlist.append(command) diff --git a/src/Mod/CAM/Path/Post/scripts/dynapath_4060_post.py b/src/Mod/CAM/Path/Post/scripts/dynapath_4060_post.py index 84704c7611..98e40c0fd7 100644 --- a/src/Mod/CAM/Path/Post/scripts/dynapath_4060_post.py +++ b/src/Mod/CAM/Path/Post/scripts/dynapath_4060_post.py @@ -394,7 +394,7 @@ def parse(pathobj): if command == lastcommand: outstring.pop(0) - if c.Name[0] == "(" and not OUTPUT_COMMENTS: # command is a comment + if c.Name.startswith("(") and not OUTPUT_COMMENTS: # command is a comment continue # Now add the remaining parameters in order diff --git a/src/Mod/CAM/Path/Post/scripts/fablin_post.py b/src/Mod/CAM/Path/Post/scripts/fablin_post.py index 73c4e640e3..87388d9f11 100644 --- a/src/Mod/CAM/Path/Post/scripts/fablin_post.py +++ b/src/Mod/CAM/Path/Post/scripts/fablin_post.py @@ -199,9 +199,12 @@ def export(objectslist, filename, argstring): print("done postprocessing.") - gfile = pyopen(filename, "w") - gfile.write(final) - gfile.close() + if not filename == "-": + gfile = pyopen(filename, "w") + gfile.write(final) + gfile.close() + + return final def linenumber(): @@ -251,7 +254,7 @@ def parse(pathobj): command = c.Name # fablin does not support parenthesis syntax, so removing that (pocket) in the agnostic gcode - if command[0] == "(": + if command.startswith("("): if not OUTPUT_COMMENTS: pass else: diff --git a/src/Mod/CAM/Path/Post/scripts/fangling_post.py b/src/Mod/CAM/Path/Post/scripts/fangling_post.py index 636bd2bc8b..10eeeb6345 100644 --- a/src/Mod/CAM/Path/Post/scripts/fangling_post.py +++ b/src/Mod/CAM/Path/Post/scripts/fangling_post.py @@ -408,7 +408,7 @@ def parse(pathobj): if command == lastcommand: outstring.pop(0) - if c.Name[0] == "(" and not OUTPUT_COMMENTS: # command is a comment + if c.Name.startswith("(") and not OUTPUT_COMMENTS: # command is a comment continue # Now add the remaining parameters in order diff --git a/src/Mod/CAM/Path/Post/scripts/fanuc_post.py b/src/Mod/CAM/Path/Post/scripts/fanuc_post.py index b708d51a9b..b2e4488a0c 100644 --- a/src/Mod/CAM/Path/Post/scripts/fanuc_post.py +++ b/src/Mod/CAM/Path/Post/scripts/fanuc_post.py @@ -496,7 +496,7 @@ def parse(pathobj): if command == "G80" and lastcommand == nextcommand: continue - if c.Name[0] == "(" and not OUTPUT_COMMENTS: # command is a comment + if c.Name.startswith("(") and not OUTPUT_COMMENTS: # command is a comment continue # Now add the remaining parameters in order diff --git a/src/Mod/CAM/Path/Post/scripts/jtech_post.py b/src/Mod/CAM/Path/Post/scripts/jtech_post.py index 35e12487ea..3fd943a108 100644 --- a/src/Mod/CAM/Path/Post/scripts/jtech_post.py +++ b/src/Mod/CAM/Path/Post/scripts/jtech_post.py @@ -313,7 +313,7 @@ def parse(pathobj): if command == lastcommand: outstring.pop(0) - if c.Name[0] == "(" and not OUTPUT_COMMENTS: # command is a comment + if c.Name.startswith("(") and not OUTPUT_COMMENTS: # command is a comment continue # Now add the remaining parameters in order diff --git a/src/Mod/CAM/Path/Post/scripts/linuxcnc_post.py b/src/Mod/CAM/Path/Post/scripts/linuxcnc_post.py index 358eefe4bf..94adc56c76 100644 --- a/src/Mod/CAM/Path/Post/scripts/linuxcnc_post.py +++ b/src/Mod/CAM/Path/Post/scripts/linuxcnc_post.py @@ -347,7 +347,7 @@ def parse(pathobj): if command == lastcommand: outstring.pop(0) - if c.Name[0] == "(" and not OUTPUT_COMMENTS: # command is a comment + if c.Name.startswith("(") and not OUTPUT_COMMENTS: # command is a comment continue # Now add the remaining parameters in order diff --git a/src/Mod/CAM/Path/Post/scripts/mach3_mach4_post.py b/src/Mod/CAM/Path/Post/scripts/mach3_mach4_post.py index c0eb947fd3..0900e5ffb9 100644 --- a/src/Mod/CAM/Path/Post/scripts/mach3_mach4_post.py +++ b/src/Mod/CAM/Path/Post/scripts/mach3_mach4_post.py @@ -379,7 +379,7 @@ def parse(pathobj): if command == lastcommand: outstring.pop(0) - if c.Name[0] == "(" and not OUTPUT_COMMENTS: # command is a comment + if c.Name.startswith("(") and not OUTPUT_COMMENTS: # command is a comment continue # Now add the remaining parameters in order diff --git a/src/Mod/CAM/Path/Post/scripts/opensbp_post.py b/src/Mod/CAM/Path/Post/scripts/opensbp_post.py index 45fa861a23..7fac68914d 100644 --- a/src/Mod/CAM/Path/Post/scripts/opensbp_post.py +++ b/src/Mod/CAM/Path/Post/scripts/opensbp_post.py @@ -359,7 +359,7 @@ def parse(pathobj): output += scommands[command](c) if c.Parameters: CurrentState.update(c.Parameters) - elif command[0] == "(": + elif command.startswith("("): output += "' " + command + "\n" else: print("I don't know what the hell the command: ", end="") diff --git a/src/Mod/CAM/Path/Post/scripts/philips_post.py b/src/Mod/CAM/Path/Post/scripts/philips_post.py index 9a67cb7957..373c2f3475 100644 --- a/src/Mod/CAM/Path/Post/scripts/philips_post.py +++ b/src/Mod/CAM/Path/Post/scripts/philips_post.py @@ -352,6 +352,7 @@ def linenumberify(GCodeString): def export(objectslist, filename, argstring): + processArguments(argstring) global UNITS global linenr @@ -412,7 +413,7 @@ def export(objectslist, filename, argstring): command = command.replace("G0", "G") # normalize: G01 -> G1 if command != UNITS or UNITS_INCLUDED: - if command[0] == "(": + if command.startswith("("): command = PostUtils.fcoms(command, COMMENT) # the mapping is done for output only! For internal things we # still use the old value. @@ -584,8 +585,16 @@ def export(objectslist, filename, argstring): lastcommand = c.Name gcode = gcode.replace("_", "-") gcode += linenumberify(GCODE_FOOTER) - if SHOW_EDITOR: - PostUtils.editor(gcode) - gfile = pyopen(filename, "w") - gfile.write(gcode) - gfile.close() + + # show the gCode result dialog + if FreeCAD.GuiUp and SHOW_EDITOR: + final = PostUtils.editor(gcode) + else: + final = gcode + + if not filename == "-": + gfile = pyopen(filename, "w") + gfile.write(final) + gfile.close() + + return final diff --git a/src/Mod/CAM/Path/Post/scripts/uccnc_post.py b/src/Mod/CAM/Path/Post/scripts/uccnc_post.py index bad70ad181..0297ea91e6 100644 --- a/src/Mod/CAM/Path/Post/scripts/uccnc_post.py +++ b/src/Mod/CAM/Path/Post/scripts/uccnc_post.py @@ -602,7 +602,7 @@ def parse(pathobj): if command == lastcommand: commandlist.pop(0) - if c.Name[0] == "(" and not OUTPUT_COMMENTS: # command is a comment + if c.Name.startswith("(") and not OUTPUT_COMMENTS: # command is a comment continue # Now add the remaining parameters in order diff --git a/src/Mod/CAM/Path/Post/scripts/wedm_post.py b/src/Mod/CAM/Path/Post/scripts/wedm_post.py index fc745ba8e1..8c1a52ee62 100644 --- a/src/Mod/CAM/Path/Post/scripts/wedm_post.py +++ b/src/Mod/CAM/Path/Post/scripts/wedm_post.py @@ -448,7 +448,7 @@ def parse(pathobj): if command == lastcommand: outstring.pop(0) - if command[0] == "(": # command is a comment + if command.startswith("("): # command is a comment if OUTPUT_COMMENTS: # Edit comment with COMMENT_CHAR outstring.insert(0, COMMENT_CHAR) else: