@@ -265,12 +265,16 @@ def __repr__(self):
265265
266266
267267from typing import Optional
268+ import re
268269class GCodeLine :
269270 """Encapusates one GCode line, plus print states and a reference to which Printing Object the line belongs
270271 It can contain a calculated `looporder` and concentric loops have the same 'contentric_grop' number
271272 """
272273 __slots__ = ('gcode' , 'previous' , 'current' , 'object' , 'looporder' , 'concentric_group' )
273274
275+ regex_x = re .compile (r'X[-+]?[0-9]*\.?[0-9]+' )
276+ regex_y = re .compile (r'Y[-+]?[0-9]*\.?[0-9]+' )
277+
274278 def __init__ (self , gcode : str , previous : Optional [GCodeState ] = None ,
275279 current : Optional [GCodeState ] = None , object_ref : Optional [ObjectEntry ] = None , looporder : Optional [int ] = None , concentric_group : Optional [int ] = None ):
276280 self .gcode = gcode
@@ -287,13 +291,20 @@ def __repr__(self):
287291 def to_gcode (self ) -> str :
288292 return self .gcode
289293
294+ def update_xy (self , new_x , new_y ):
295+ gcode = self .gcode
296+ gcode = GCodeLine .regex_x .sub (f'X{ new_x } ' , gcode )
297+ gcode = GCodeLine .regex_y .sub (f'Y{ new_y } ' , gcode )
298+ self .gcode = gcode
299+
290300 @staticmethod
291301 def from_gcode (gcode : str , previous : Optional [GCodeState ] = None , current : Optional [GCodeState ] = None , object_ref : Optional [ObjectEntry ] = None ) -> "GCodeLine" :
292302 """Creates a GCodeLine instance from a raw G-code line without modifications. That should inclute a \\ n at the very end"""
293303 return GCodeLine (gcode , previous , current , object_ref )
294304
295305
296306
307+
297308class GCodeFeature :
298309 """
299310 GCodeFeature: A state tracker for parsing G-code and identifying print features.
@@ -1003,9 +1014,16 @@ def travel_to(self, target_state, simulator, feature, loop = None, start_state =
10031014 elif start_state is not None :
10041015 start_pos = start_state
10051016
1017+ move_z = ""
1018+ zhop = self .travel_zhop
1019+ hopping_z = ""
1020+ if z is not None :
1021+ move_z = f" Z{ z :.2f} "
1022+ hopping_z = f" Z{ (z + zhop ):.2f} "
1023+
10061024 if Point .distance_between_points (start_pos , target_state ) < travel_threshold :
10071025 # Simple Travel - no wipe nor retraction (very close travel)
1008- move = from_gcode (f"G1 X{ target_state .x } Y{ target_state .y } F{ int (simulator .travel_speed )} ; BRICK: Travel (no-retraction)\n " ) # Simple Move
1026+ move = from_gcode (f"G1 X{ target_state .x } Y{ target_state .y } { move_z } F{ int (simulator .travel_speed )} ; BRICK: Travel (no-retraction)\n " ) # Simple Move
10091027 gcodes .append (move )
10101028 self .last_travelled_gcode_line = move # last move tracking
10111029 self .last_internalperimeter_xy_line = move
@@ -1020,18 +1038,13 @@ def travel_to(self, target_state, simulator, feature, loop = None, start_state =
10201038 else :
10211039 retraction = simulator .retraction_length * self .retract_before_wipe
10221040 gcodes .append (from_gcode (f"G1 E-{ retraction :.2f} F{ int (simulator .retraction_speed )} ; BRICK: Retraction \n " ))
1023- self .retracted = self .retracted - retraction # Retraction Debt
1041+ self .retracted = self .retracted + retraction # Retraction Debt
10241042
10251043 # Wipe:
10261044 if loop is not None and simulator .wipe_speed > 0 and self .retract_before_wipe < 1 :
10271045 gcodes .extend (self .wipe (loop , simulator , feature ))
10281046
10291047 # Travel:
1030- zhop = self .travel_zhop
1031- hopping_z = ""
1032- z = None
1033- if z is not None :
1034- hopping_z = f" Z{ (z + zhop ):.2f} "
10351048 move = from_gcode (f"G1 X{ target_state .x } Y{ target_state .y } { hopping_z } F{ int (simulator .travel_speed )} ; BRICK: Target Position\n " )
10361049 gcodes .append (move )
10371050 self .last_travelled_gcode_line = move # last move tracking
@@ -1138,7 +1151,7 @@ def wipe(self, loop, simulator, feature):
11381151 self .last_internalperimeter_xy_line = move
11391152 gcodes .append (from_gcode (feature .const_wipe_end )) # ";WIPE_END\n"
11401153
1141- self .retracted = self .retracted - total_extrusion_to_retract # Retraction Debt
1154+ self .retracted = self .retracted + total_extrusion_to_retract # Retraction Debt
11421155
11431156 # TODO: Incorporate the experimental_arcflick
11441157
@@ -1501,6 +1514,7 @@ def generate_deffered_perimeters(self, myline, deffered, extrusion_multiplier, e
15011514 if is_first_perimeter and is_first_loop and is_first_line :
15021515 xy_line_to_adjust = self .last_noninternalperimeter_xy_line
15031516 if xy_line_to_adjust is not None and not xy_line_to_adjust .current .is_extruding :
1517+ #xy_line_to_adjust.update_xy(deffered_line.previous.x, deffered_line.previous.y)
15041518 xy_line_to_adjust .gcode = f"G1 X{ deffered_line .previous .x } Y{ deffered_line .previous .y } Z{ higher_z_formated } F{ int (simulator .travel_speed )} ; BRICK: Travel Fix Up\n "
15051519 self .last_noninternalperimeter_xy_line = None
15061520 else :
@@ -1681,10 +1695,6 @@ def process_gcode(self, gcode_stream):
16811695 #logger.info(f"IP: {feature.internal_perimeter}, JL: {feature.justleft_internal_perimeter} - Line: {line_number}, gcode:{line}")
16821696
16831697
1684- if feature .just_changed_type and feature .last_type in ["Internal Bridge" , "Ironing" , "Bridge" ]:
1685- buffer_lines .append (from_gcode (f"{ feature .const_layer_height } { feature .height } \n " ))
1686- # Fixes a nasty non-related preview glitch on OrcaSlicer and BambuStudio Preview
1687- # Doesn't change anything on actual printing. Just making the preview pretty.
16881698
16891699 # Detecting Speeds from the file:
16901700 if detect_speeds == True : # it must begin as True - there is no default speeds (override down). It changes to False once speeds are detected
@@ -1847,8 +1857,9 @@ def process_gcode(self, gcode_stream):
18471857 if is_first_loop and is_first_line :
18481858 xy_line_to_adjust = self .last_noninternalperimeter_xy_line
18491859 if xy_line_to_adjust is not None and not xy_line_to_adjust .current .is_extruding :
1850- xy_line_to_adjust .gcode = f"G1 X{ kept_line .previous .x } Y{ kept_line .previous .y } F{ int (simulator .travel_speed )} ; BRICK: Travel Fix\n "
1851- #xy_line_to_adjust.gcode = f"G1 X{kept_line.previous.x} Y{kept_line.previous.y} Z{feature.z + 0.4} F{int(simulator.travel_speed)} ; BRICK: Travel Fix\n"
1860+ # This was breaking in some cases
1861+ #xy_line_to_adjust.gcode = f"G1 X{kept_line.previous.x} Y{kept_line.previous.y} F{int(simulator.travel_speed)} ; BRICK: Travel Fix Level\n"
1862+ xy_line_to_adjust .update_xy (kept_line .previous .x , kept_line .previous .y ) # BRICK Travel Fix
18521863 self .last_noninternalperimeter_xy_line = None
18531864 #buffer_lines.append(from_gcode(f"G1 Z{feature.z} F{int(simulator.travel_speed)} ; BRICK: Z-Hop Down\n"))
18541865 buffer_lines .append (from_gcode (feature .internal_perimeter_type ))
@@ -1890,8 +1901,9 @@ def process_gcode(self, gcode_stream):
18901901 # Resets the correct absolute extrusion register for the next feature:
18911902 buffer_lines .append (from_gcode (f"G92 E{ myline .previous .e } ; BRICK: Resets the Extruder absolute position\n " ))
18921903 self .last_internalperimeter_state = calculated_line .current
1893- if myline .previous .width != kept_line .current .width :
1894- buffer_lines .append (from_gcode (f"{ simulator .const_width } { myline .previous .width } \n " ))
1904+ #if myline.previous.width != kept_line.current.width:
1905+ buffer_lines .append (from_gcode (f"{ simulator .const_width } { myline .previous .width } \n " )) # For the Preview
1906+ buffer_lines .append (from_gcode (f"{ feature .const_layer_height } { feature .height :.2f} \n " )) # For the Preview
18951907
18961908
18971909
@@ -1967,6 +1979,12 @@ def process_gcode(self, gcode_stream):
19671979 myline .current = current_state
19681980 self .last_noninternalperimeter_xy_line = myline
19691981
1982+ # Fixes a nasty non-related preview glitch on OrcaSlicer and BambuStudio Preview
1983+ # Doesn't change anything on actual printing. Just making the preview pretty.
1984+ if feature .current_type in ["Internal Bridge" , "Ironing" , "Bridge" , "Sparse infill" ] and line .startswith (feature .DEF_LAYER_HEIGHTS ):
1985+ myline .gcode = f"{ feature .const_layer_height } { feature .height } \n "
1986+ if feature .just_changed_type and feature .current_type in ["Internal Bridge" , "Ironing" , "Bridge" , "Sparse infill" ]:
1987+ buffer_lines .append (from_gcode (f"{ feature .const_layer_height } { feature .height } \n " ))
19701988
19711989 # Exception for pretty visualization on PrusaSlicer and OrcaSlicer preview:
19721990 # Forces a "Width" after an External Perimeter begins, to make them look like they actually ARE.
0 commit comments