diff --git a/src/Mod/Path/Path/Post/UtilsArguments.py b/src/Mod/Path/Path/Post/UtilsArguments.py index b770b33384..923de4b4dd 100644 --- a/src/Mod/Path/Path/Post/UtilsArguments.py +++ b/src/Mod/Path/Path/Post/UtilsArguments.py @@ -36,10 +36,6 @@ import shlex from FreeCAD import Units -# to distinguish python built-in open function from the one declared below -if open.__module__ in ["__builtin__", "io"]: - pythonopen = open - def add_flag_type_arguments( argument_group, @@ -50,6 +46,7 @@ def add_flag_type_arguments( false_help, visible=True, ): + """Create an argument specification for an argument that is a flag.""" if visible: if default_flag: true_help += " (default)" @@ -103,13 +100,11 @@ def init_arguments_visible(arguments_visible): def init_shared_arguments(values, argument_defaults, arguments_visible): - """Initialize the shared arguments for postprocessors.""" + """Initialize the arguments for postprocessors.""" parser = argparse.ArgumentParser( prog=values["MACHINE_NAME"], usage=argparse.SUPPRESS, add_help=False ) - shared = parser.add_argument_group( - "Arguments that are shared with all postprocessors" - ) + shared = parser.add_argument_group("Arguments that are commonly used") add_flag_type_arguments( shared, argument_defaults["metric_inches"], @@ -272,8 +267,8 @@ def init_shared_arguments(values, argument_defaults, arguments_visible): argument_defaults["translate_drill"], "--translate_drill", "--no-translate_drill", - "Translate drill cycles G81, G82 & G83 into G0/G1 movements", - "Don't translate drill cycles G81, G82 & G83 into G0/G1 movements", + "Translate drill cycles G73, G81, G82 & G83 into G0/G1 movements", + "Don't translate drill cycles G73, G81, G82 & G83 into G0/G1 movements", arguments_visible["translate_drill"], ) if arguments_visible["wait-for-spindle"]: @@ -581,20 +576,14 @@ def process_shared_arguments(values, parser, argstring, all_visible, filename): if args.output_all_arguments: argument_text = all_visible.format_help() if not filename == "-": - gfile = pythonopen( - filename, "w", newline=values["END_OF_LINE_CHARACTERS"] - ) - gfile.write(argument_text) - gfile.close() + with open(filename, "w", newline=values["END_OF_LINE_CHARACTERS"]) as f: + f.write(argument_text) return (False, argument_text) if args.output_visible_arguments: argument_text = parser.format_help() if not filename == "-": - gfile = pythonopen( - filename, "w", newline=values["END_OF_LINE_CHARACTERS"] - ) - gfile.write(argument_text) - gfile.close() + with open(filename, "w", newline=values["END_OF_LINE_CHARACTERS"]) as f: + f.write(argument_text) return (False, argument_text) # Default to metric unless an argument overrides it values["UNITS"] = "G21" @@ -711,7 +700,7 @@ def process_shared_arguments(values, parser, argstring, all_visible, filename): # # G53 G00|G01 or G00|G01 G53 X Y Z # -# G73, G74, G81 to G86, G89 (X Y Z) or (U V W) R Q L P F K $ +# G73, G74, G81 to G86, G89 "X Y Z" | "U V W" R Q L P F K $ # # G76 P Z I J R K Q H E L $ # diff --git a/src/Mod/Path/Path/Post/UtilsExport.py b/src/Mod/Path/Path/Post/UtilsExport.py index 1fad862599..3b76e7dd3f 100644 --- a/src/Mod/Path/Path/Post/UtilsExport.py +++ b/src/Mod/Path/Path/Post/UtilsExport.py @@ -35,11 +35,6 @@ import Path.Post.Utils as PostUtils import Path.Post.UtilsParse as PostUtilsParse import Path.Tool.Controller as PathToolController - -# to distinguish python built-in open function from the one declared below -if open.__module__ in ["__builtin__", "io"]: - pythonopen = open - # # This routine processes things in the following order: # @@ -301,8 +296,7 @@ def export_common(values, objectslist, filename): print("done postprocessing.") if not filename == "-": - gfile = pythonopen(filename, "w", newline=values["END_OF_LINE_CHARACTERS"]) - gfile.write(final) - gfile.close() + with open(filename, "w", newline=values["END_OF_LINE_CHARACTERS"]) as gfile: + gfile.write(final) return final diff --git a/src/Mod/Path/Path/Post/UtilsParse.py b/src/Mod/Path/Path/Post/UtilsParse.py index 61cce29547..72919f66aa 100644 --- a/src/Mod/Path/Path/Post/UtilsParse.py +++ b/src/Mod/Path/Path/Post/UtilsParse.py @@ -46,9 +46,6 @@ def create_comment(comment_string, comment_symbol): def drill_translate(values, outstring, cmd, params): """Translate drill cycles.""" - axis_precision_string = f'.{str(values["AXIS_PRECISION"])}f' - feed_precision_string = f'.{str(values["FEED_PRECISION"])}f' - trBuff = "" nl = "\n" @@ -100,44 +97,26 @@ def drill_translate(values, outstring, cmd, params): if values["MOTION_MODE"] == "G91": # force absolute coordinates during cycles trBuff += f"{linenumber(values)}G90{nl}" - num = format( - float(RETRACT_Z.getValueAs(values["UNIT_FORMAT"])), - axis_precision_string, - ) - strG0_RETRACT_Z = f"G0 Z{num}{nl}" - num = format( - float(drill_feedrate.getValueAs(values["UNIT_SPEED_FORMAT"])), - feed_precision_string, - ) - strF_Feedrate = f" F{num}{nl}" + strG0_RETRACT_Z = f"G0 Z{format_for_axis(values, RETRACT_Z)}{nl}" + strF_Feedrate = f" F{format_for_feed(values, drill_feedrate)}{nl}" # print(strF_Feedrate) # preliminary movement(s) if values["CURRENT_Z"] < RETRACT_Z: trBuff += f"{linenumber(values)}{strG0_RETRACT_Z}" - num_x = format( - float(drill_X.getValueAs(values["UNIT_FORMAT"])), axis_precision_string - ) - num_y = format( - float(drill_Y.getValueAs(values["UNIT_FORMAT"])), axis_precision_string - ) + num_x = format_for_axis(values, drill_X) + num_y = format_for_axis(values, drill_Y) trBuff += f"{linenumber(values)}G0 X{num_x} Y{num_y}{nl}" if values["CURRENT_Z"] > RETRACT_Z: # NIST GCODE 3.5.16.1 Preliminary and In-Between Motion says G0 to RETRACT_Z # Here use G1 since retract height may be below surface ! - num_z = format( - float(RETRACT_Z.getValueAs(values["UNIT_FORMAT"])), - axis_precision_string, - ) + num_z = format_for_axis(values, RETRACT_Z) trBuff += f"{linenumber(values)}G1 Z{num_z}{strF_Feedrate}" last_Stop_Z = RETRACT_Z # drill moves if cmd in ("G81", "G82"): - num_z = format( - float(drill_Z.getValueAs(values["UNIT_FORMAT"])), - axis_precision_string, - ) + num_z = format_for_axis(values, drill_Z) trBuff += f"{linenumber(values)}G1 Z{num_z}{strF_Feedrate}" # pause where applicable if cmd == "G82": @@ -149,41 +128,25 @@ def drill_translate(values, outstring, cmd, params): if last_Stop_Z != RETRACT_Z: # rapid move to just short of last drilling depth clearance_depth = last_Stop_Z + a_bit - num_z = format( - float(clearance_depth.getValueAs(values["UNIT_FORMAT"])), - axis_precision_string, - ) + num_z = format_for_axis(values, clearance_depth) trBuff += f"{linenumber(values)}G0 Z{num_z}{nl}" next_Stop_Z = last_Stop_Z - drill_Step if next_Stop_Z > drill_Z: - num_z = format( - float(next_Stop_Z.getValueAs(values["UNIT_FORMAT"])), - axis_precision_string, - ) + num_z = format_for_axis(values, next_Stop_Z) trBuff += f"{linenumber(values)}G1 Z{num_z}{strF_Feedrate}" if cmd == "G73": # Rapid up "a small amount". chip_breaker_height = ( next_Stop_Z + values["CHIPBREAKING_AMOUNT"] ) - num_z = format( - float( - chip_breaker_height.getValueAs( - values["UNIT_FORMAT"] - ) - ), - axis_precision_string, - ) + num_z = format_for_axis(values, chip_breaker_height) trBuff += f"{linenumber(values)}G0 Z{num_z}{nl}" elif cmd == "G83": # Rapid up to the retract height trBuff += f"{linenumber(values)}{strG0_RETRACT_Z}" last_Stop_Z = next_Stop_Z else: - num_z = format( - float(drill_Z.getValueAs(values["UNIT_FORMAT"])), - axis_precision_string, - ) + num_z = format_for_axis(values, drill_Z) trBuff += f"{linenumber(values)}G1 Z{num_z}{strF_Feedrate}" trBuff += f"{linenumber(values)}{strG0_RETRACT_Z}" break @@ -196,6 +159,22 @@ def drill_translate(values, outstring, cmd, params): return trBuff +def format_for_axis(values, number): + """Format a number using the precision for an axis value.""" + return format( + float(number.getValueAs(values["UNIT_FORMAT"])), + f'.{str(values["AXIS_PRECISION"])}f', + ) + + +def format_for_feed(values, number): + """Format a number using the precision for a feed rate.""" + return format( + float(number.getValueAs(values["UNIT_SPEED_FORMAT"])), + f'.{str(values["FEED_PRECISION"])}f', + ) + + def format_outstring(values, strTable): """Construct the line for the final output.""" s = "" @@ -221,8 +200,6 @@ def parse(values, pathobj): nl = "\n" out = "" lastcommand = None - axis_precision_string = f'.{str(values["AXIS_PRECISION"])}f' - feed_precision_string = f'.{str(values["FEED_PRECISION"])}f' currLocation = {} # keep track for no doubles firstmove = Path.Command("G0", {"X": -1, "Y": -1, "Z": -1, "F": 0.0}) @@ -316,12 +293,7 @@ def parse(values, pathobj): if command not in values["RAPID_MOVES"]: speed = Units.Quantity(c.Parameters["F"], Units.Velocity) if speed.getValueAs(values["UNIT_SPEED_FORMAT"]) > 0.0: - param_num = format( - float( - speed.getValueAs(values["UNIT_SPEED_FORMAT"]) - ), - feed_precision_string, - ) + param_num = format_for_feed(values, speed) outstring.append(f"{param}{param_num}") else: continue @@ -332,10 +304,7 @@ def parse(values, pathobj): outstring.append(f"{param}{str(int(c.Parameters[param]))}") elif command in ("G41.1", "G42.1"): pos = Units.Quantity(c.Parameters[param], Units.Length) - param_num = format( - float(pos.getValueAs(values["UNIT_FORMAT"])), - axis_precision_string, - ) + param_num = format_for_axis(values, pos) outstring.append(f"{param}{param_num}") elif command in ("G96", "G97"): param_num = PostUtils.fmt( @@ -367,10 +336,7 @@ def parse(values, pathobj): ) elif command in ("G5", "G05", "G64"): pos = Units.Quantity(c.Parameters[param], Units.Length) - param_num = format( - float(pos.getValueAs(values["UNIT_FORMAT"])), - axis_precision_string, - ) + param_num = format_for_axis(values, pos) outstring.append(f"{param}{param_num}") else: # anything else that is supported outstring.append(f"{param}{str(c.Parameters[param])}") @@ -379,10 +345,7 @@ def parse(values, pathobj): outstring.append(f"{param}{str(int(c.Parameters[param]))}") elif command in ("G64", "G73", "G83"): pos = Units.Quantity(c.Parameters[param], Units.Length) - param_num = format( - float(pos.getValueAs(values["UNIT_FORMAT"])), - axis_precision_string, - ) + param_num = format_for_axis(values, pos) outstring.append(f"{param}{param_num}") elif param == "S": param_num = PostUtils.fmt( @@ -398,10 +361,7 @@ def parse(values, pathobj): continue else: pos = Units.Quantity(c.Parameters[param], Units.Length) - param_num = format( - float(pos.getValueAs(values["UNIT_FORMAT"])), - axis_precision_string, - ) + param_num = format_for_axis(values, pos) outstring.append(f"{param}{param_num}") if ( @@ -412,16 +372,10 @@ def parse(values, pathobj): and opVertRapid ): if "Z" not in c.Parameters: - param_num = format( - float(opHorizRapid.getValueAs(values["UNIT_SPEED_FORMAT"])), - axis_precision_string, - ) + param_num = format_for_axis(values, opHorizRapid) outstring.append(f"F{param_num}") else: - param_num = format( - float(opVertRapid.getValueAs(values["UNIT_SPEED_FORMAT"])), - axis_precision_string, - ) + param_num = format_for_axis(values, opVertRapid) outstring.append(f"F{param_num}") # store the latest command diff --git a/src/Mod/Path/PathTests/TestRefactoredTestPost.py b/src/Mod/Path/PathTests/TestRefactoredTestPost.py index 546aa494d5..b75a0e7a6b 100644 --- a/src/Mod/Path/PathTests/TestRefactoredTestPost.py +++ b/src/Mod/Path/PathTests/TestRefactoredTestPost.py @@ -21,8 +21,6 @@ # * * # *************************************************************************** -from importlib import reload - import FreeCAD import Path @@ -192,7 +190,7 @@ G21 Empty path. Outputs all arguments. """ - expected = """Arguments that are shared with all postprocessors: + expected = """Arguments that are commonly used: --metric Convert output for Metric mode (G21) (default) --inches Convert output for US imperial mode (G20) --axis-modal Don't output axis values if they are the same as the @@ -246,10 +244,10 @@ G21 --tool_change Insert M6 and any other tool change G-code for all tool changes (default) --no-tool_change Convert M6 to a comment for all tool changes - --translate_drill Translate drill cycles G81, G82 & G83 into G0/G1 + --translate_drill Translate drill cycles G73, G81, G82 & G83 into G0/G1 movements - --no-translate_drill Don't translate drill cycles G81, G82 & G83 into G0/G1 - movements (default) + --no-translate_drill Don't translate drill cycles G73, G81, G82 & G83 into + G0/G1 movements (default) --wait-for-spindle WAIT_FOR_SPINDLE Time to wait (in seconds) after M3, M4 (default = 0.0) """ @@ -363,7 +361,9 @@ G21 def test00160(self): """Test inches.""" + # c = Path.Command("G0 X10 Y20 Z30 A10 B20 C30 U10 V20 W30") + self.docobj.Path = Path.Path([c]) postables = [self.docobj] args = "--inches" @@ -889,7 +889,7 @@ G52 X0.0000 Y0.0000 Z0.0000 A0.0000 B0.0000 C0.0000 U0.0000 V0.0000 W0.0000 # # Some gcode interpreters use G59 P- to select additional # work coordinate systems. This is considered somewhat - # obsolete and is being replaces by G54.1 P- instead. + # obsolete and is being replaced by G54.1 P- instead. # self.compare_third_line("G59 P2.34567", "G59 P2", "")