From 7c301922db1180e838aeab611a10fa673fc3c4e9 Mon Sep 17 00:00:00 2001 From: Marius Stanciu Date: Wed, 23 Jan 2019 00:31:48 +0200 Subject: [PATCH] - added a new postprocessor file named 'line_xyz' which have x, y, z values on the same GCode line - fixed calculation of total path for Excellon Gcode file --- README.md | 7 +- camlib.py | 59 +++++++++----- postprocessors/line_xyz.py | 156 +++++++++++++++++++++++++++++++++++++ 3 files changed, 201 insertions(+), 21 deletions(-) create mode 100644 postprocessors/line_xyz.py diff --git a/README.md b/README.md index 267e1d56..66f0fdc2 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,11 @@ CAD program, and create G-Code for Isolation routing. ================================================= +23.01.2019 + +- added a new postprocessor file named 'line_xyz' which have x, y, z values on the same GCode line +- fixed calculation of total path for Excellon Gcode file + 21.01.2019 - changed some tooltips @@ -16,7 +21,7 @@ CAD program, and create G-Code for Isolation routing. - in Excellon Tool Table the columns are now only selectable by clicking on the header (sorting is done automatically) - if CNCJob from Excellon then hide the CNC tools table in CNCJob Object - + 20.01.2019 - fixed the HPGL code geometry rendering when travel diff --git a/camlib.py b/camlib.py index 0c469bcd..675f1b4b 100644 --- a/camlib.py +++ b/camlib.py @@ -4416,7 +4416,7 @@ class CNCjob(Geometry): returnvalue = fun(attributes) return returnvalue except Exception as e: - self.app.log.error('Exception ocurred inside a postprocessor: ' + traceback.format_exc()) + self.app.log.error('Exception occurred within a postprocessor: ' + traceback.format_exc()) return '' def optimized_travelling_salesman(self, points, start=None): @@ -4435,6 +4435,7 @@ class CNCjob(Geometry): >>> optimized_travelling_salesman([[0,0],[10,0],[6,0]]) [[0, 0], [6, 0], [10, 0]] """ + if start is None: start = points[0] must_visit = points @@ -4541,7 +4542,7 @@ class CNCjob(Geometry): gcode = self.doformat(p.start_code) gcode += self.doformat(p.feedrate_code) gcode += self.doformat(p.lift_code, x=0, y=0) - gcode += self.doformat(p.startz_code) + gcode += self.doformat(p.startz_code, x=0, y=0) # Distance callback class CreateDistanceCallback(object): @@ -4575,6 +4576,10 @@ class CNCjob(Geometry): locations.append((point.coords.xy[0][0], point.coords.xy[1][0])) return locations + oldx = 0 + oldy = 0 + measured_distance = 0 + current_platform = platform.architecture()[0] if current_platform == '64bit': if excellon_optimization_type == 'M': @@ -4600,7 +4605,8 @@ class CNCjob(Geometry): # Set search time limit in milliseconds. if float(self.app.defaults["excellon_search_time"]) != 0: - search_parameters.time_limit_ms = int(float(self.app.defaults["excellon_search_time"]) * 1000) + search_parameters.time_limit_ms = int( + float(self.app.defaults["excellon_search_time"]) * 1000) else: search_parameters.time_limit_ms = 3000 @@ -4636,7 +4642,7 @@ class CNCjob(Geometry): if tool in points: # Tool change sequence (optional) if toolchange: - gcode += self.doformat(p.toolchange_code) + gcode += self.doformat(p.toolchange_code,toolchangexy=(oldx, oldy)) gcode += self.doformat(p.spindle_code) # Spindle start if self.dwell is True: gcode += self.doformat(p.dwell_code) # Dwell time @@ -4646,12 +4652,10 @@ class CNCjob(Geometry): gcode += self.doformat(p.dwell_code) # Dwell time # Drillling! - oldx = 0 - oldy = 0 - measured_distance = 0 for k in node_list: locx = locations[k][0] locy = locations[k][1] + gcode += self.doformat(p.rapid_code, x=locx, y=locy) gcode += self.doformat(p.down_code, x=locx, y=locy) gcode += self.doformat(p.up_to_zero_code, x=locx, y=locy) @@ -4659,7 +4663,7 @@ class CNCjob(Geometry): measured_distance += abs(distance_euclidian(locx, locy, oldx, oldy)) oldx = locx oldy = locy - log.debug("The total travel distance with Metaheuristics is: %s" % str(measured_distance) + '\n') + log.debug("The total travel distance with OR-TOOLS Metaheuristics is: %s" % str(measured_distance)) elif excellon_optimization_type == 'B': log.debug("Using OR-Tools Basic drill path optimization.") for tool in tools: @@ -4712,7 +4716,7 @@ class CNCjob(Geometry): if tool in points: # Tool change sequence (optional) if toolchange: - gcode += self.doformat(p.toolchange_code) + gcode += self.doformat(p.toolchange_code,toolchangexy=(oldx, oldy)) gcode += self.doformat(p.spindle_code) # Spindle start) if self.dwell is True: gcode += self.doformat(p.dwell_code) # Dwell time @@ -4722,9 +4726,6 @@ class CNCjob(Geometry): gcode += self.doformat(p.dwell_code) # Dwell time # Drillling! - oldx = 0 - oldy = 0 - measured_distance = 0 for k in node_list: locx = locations[k][0] locy = locations[k][1] @@ -4735,7 +4736,7 @@ class CNCjob(Geometry): measured_distance += abs(distance_euclidian(locx, locy, oldx, oldy)) oldx = locx oldy = locy - log.debug("The total travel distance with Basic Algorithm is: %s" % str(measured_distance) + '\n') + log.debug("The total travel distance with OR-TOOLS Basic Algorithm is: %s" % str(measured_distance)) else: self.app.inform.emit("[error_notcl] Wrong optimization type selected.") return @@ -4749,7 +4750,7 @@ class CNCjob(Geometry): if tool in points: # Tool change sequence (optional) if toolchange: - gcode += self.doformat(p.toolchange_code) + gcode += self.doformat(p.toolchange_code, toolchangexy=(oldx, oldy)) gcode += self.doformat(p.spindle_code) # Spindle start) if self.dwell is True: gcode += self.doformat(p.dwell_code) # Dwell time @@ -4768,10 +4769,17 @@ class CNCjob(Geometry): gcode += self.doformat(p.down_code, x=point[0], y=point[1]) gcode += self.doformat(p.up_to_zero_code, x=point[0], y=point[1]) gcode += self.doformat(p.lift_code, x=point[0], y=point[1]) + measured_distance += abs(distance_euclidian(point[0], point[1], oldx, oldy)) + oldx = point[0] + oldy = point[1] + log.debug("The total travel distance with Travelling Salesman Algorithm is: %s" % str(measured_distance)) gcode += self.doformat(p.spindle_stop_code) # Spindle stop gcode += self.doformat(p.end_code, x=0, y=0) + measured_distance += abs(distance_euclidian(oldx, oldy, 0, 0)) + log.debug("The total travel distance including travel to end position is: %s" % + str(measured_distance) + '\n') self.gcode = gcode def generate_from_multitool_geometry(self, geometry, append=True, @@ -4887,10 +4895,14 @@ class CNCjob(Geometry): self.gcode += self.doformat(p.feedrate_code) # sets the feed rate self.gcode += self.doformat(p.lift_code, x=0, y=0) # Move (up) to travel height - self.gcode += self.doformat(p.startz_code) + self.gcode += self.doformat(p.startz_code, x=0, y=0) if toolchange: - self.gcode += self.doformat(p.toolchange_code) + if "line_xyz" in self.pp_geometry_name: + self.gcode += self.doformat(p.toolchange_code, x=self.toolchange_xy[0], y=self.toolchange_xy[1]) + else: + self.gcode += self.doformat(p.toolchange_code) + self.gcode += self.doformat(p.spindle_code) # Spindle start if self.dwell is True: self.gcode += self.doformat(p.dwell_code) # Dwell time @@ -5063,10 +5075,14 @@ class CNCjob(Geometry): self.gcode += self.doformat(p.feedrate_code) # sets the feed rate self.gcode += self.doformat(p.lift_code, x=0, y=0) # Move (up) to travel height - self.gcode += self.doformat(p.startz_code) + self.gcode += self.doformat(p.startz_code, x=0, y=0) if toolchange: - self.gcode += self.doformat(p.toolchange_code) + if "line_xyz" in self.pp_geometry_name: + self.gcode += self.doformat(p.toolchange_code, x=self.toolchange_xy[0], y=self.toolchange_xy[1]) + else: + self.gcode += self.doformat(p.toolchange_code) + self.gcode += self.doformat(p.spindle_code) # Spindle start if self.dwell is True: self.gcode += self.doformat(p.dwell_code) # Dwell time @@ -5278,8 +5294,11 @@ class CNCjob(Geometry): elif 'hpgl' in self.pp_excellon_name or 'hpgl' in self.pp_geometry_name: pass elif ('X' in gobj or 'Y' in gobj) and gobj['Z'] != current['Z']: - log.warning("Non-orthogonal motion: From %s" % str(current)) - log.warning(" To: %s" % str(gobj)) + if self.pp_geometry_name == 'line_xyz' or self.pp_excellon_name == 'line_xyz': + pass + else: + log.warning("Non-orthogonal motion: From %s" % str(current)) + log.warning(" To: %s" % str(gobj)) current['Z'] = gobj['Z'] # Store the path into geometry and reset path diff --git a/postprocessors/line_xyz.py b/postprocessors/line_xyz.py new file mode 100644 index 00000000..2f01e492 --- /dev/null +++ b/postprocessors/line_xyz.py @@ -0,0 +1,156 @@ +from FlatCAMPostProc import * + + +class line_xyz(FlatCAMPostProc): + + coordinate_format = "%.*f" + feedrate_format = '%.*f' + + def start_code(self, p): + units = ' ' + str(p['units']).lower() + coords_xy = p['toolchange_xy'] + gcode = '' + + if str(p['options']['type']) == 'Geometry': + gcode += '(TOOL DIAMETER: ' + str(p['options']['tool_dia']) + units + ')\n' + + gcode += '(Feedrate: ' + str(p['feedrate']) + units + '/min' + ')\n' + + if str(p['options']['type']) == 'Geometry': + gcode += '(Feedrate_Z: ' + str(p['feedrate_z']) + units + '/min' + ')\n' + + gcode += '(Feedrate rapids ' + str(p['feedrate_rapid']) + units + '/min' + ')\n' + '\n' + gcode += '(Z_Cut: ' + str(p['z_cut']) + units + ')\n' + + if str(p['options']['type']) == 'Geometry': + if p['multidepth'] is True: + gcode += '(DepthPerCut: ' + str(p['depthpercut']) + units + ' <=>' + \ + str(math.ceil(abs(p['z_cut']) / p['depthpercut'])) + ' passes' + ')\n' + + gcode += '(Z_Move: ' + str(p['z_move']) + units + ')\n' + gcode += '(Z Toolchange: ' + str(p['toolchangez']) + units + ')\n' + gcode += '(X,Y Toolchange: ' + "%.4f, %.4f" % (coords_xy[0], coords_xy[1]) + units + ')\n' + gcode += '(Z Start: ' + str(p['startz']) + units + ')\n' + gcode += '(Z End: ' + str(p['endz']) + units + ')\n' + gcode += '(Steps per circle: ' + str(p['steps_per_circle']) + ')\n' + + if str(p['options']['type']) == 'Excellon' or str(p['options']['type']) == 'Excellon Geometry': + gcode += '(Postprocessor Excellon: ' + str(p['pp_excellon_name']) + ')\n' + else: + gcode += '(Postprocessor Geometry: ' + str(p['pp_geometry_name']) + ')\n' + + gcode += '(Spindle Speed: %s RPM)\n' % str(p['spindlespeed']) + + gcode += ('G20\n' if p.units.upper() == 'IN' else 'G21\n') + gcode += 'G90\n' + gcode += 'G94\n' + + return gcode + + def startz_code(self, p): + if p.startz is not None: + g = 'G00 ' + 'X' + self.coordinate_format%(p.coords_decimals, p.x) + \ + ' Y' + self.coordinate_format%(p.coords_decimals, p.y) + \ + ' Z' + self.coordinate_format%(p.coords_decimals, p.startz) + return g + else: + return '' + + def lift_code(self, p): + g = 'G00 ' + 'X' + self.coordinate_format % (p.coords_decimals, p.x) + \ + ' Y' + self.coordinate_format % (p.coords_decimals, p.y) + \ + ' Z' + self.coordinate_format % (p.coords_decimals, p.z_move) + return g + + def down_code(self, p): + g = 'G01 ' + 'X' + self.coordinate_format % (p.coords_decimals, p.x) + \ + ' Y' + self.coordinate_format % (p.coords_decimals, p.y) + \ + ' Z' + self.coordinate_format % (p.coords_decimals, p.z_cut) + return g + + def toolchange_code(self, p): + toolchangez = p.toolchangez + toolchangexy = p.toolchange_xy + toolchangex = toolchangexy[0] + toolchangey = toolchangexy[1] + + no_drills = 1 + + if int(p.tool) == 1 and p.startz is not None: + toolchangez = p.startz + + if p.units.upper() == 'MM': + toolC_formatted = format(p.toolC, '.2f') + else: + toolC_formatted = format(p.toolC, '.4f') + + if str(p['options']['type']) == 'Excellon': + for i in p['options']['Tools_in_use']: + if i[0] == p.tool: + no_drills = i[2] + return """G00 X{toolchangex} Y{toolchangey} Z{toolchangez} +T{tool} +M5 +M6 +(MSG, Change to Tool Dia = {toolC}, Total drills for current tool = {t_drills}) +M0""".format(toolchangex=self.coordinate_format%(p.coords_decimals, toolchangex), + toolchangey=self.coordinate_format % (p.coords_decimals, toolchangey), + toolchangez=self.coordinate_format % (p.coords_decimals, toolchangez), + tool=int(p.tool), + t_drills=no_drills, + toolC=toolC_formatted) + else: + return """G00 X{toolchangex} Y{toolchangey} Z{toolchangez} +T{tool} +M5 +M6 +(MSG, Change to Tool Dia = {toolC}) +M0""".format(toolchangex=self.coordinate_format%(p.coords_decimals, toolchangex), + toolchangey=self.coordinate_format % (p.coords_decimals, toolchangey), + toolchangez=self.coordinate_format % (p.coords_decimals, toolchangez), + tool=int(p.tool), + toolC=toolC_formatted) + + def up_to_zero_code(self, p): + g = 'G01 ' + 'X' + self.coordinate_format % (p.coords_decimals, p.x) + \ + ' Y' + self.coordinate_format % (p.coords_decimals, p.y) + \ + ' Z0' + return g + + def position_code(self, p): + return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \ + (p.coords_decimals, p.x, p.coords_decimals, p.y) + + def rapid_code(self, p): + g = ('G00 ' + self.position_code(p)).format(**p) + g += ' Z' + self.coordinate_format % (p.coords_decimals, p.z_move) + return g + + def linear_code(self, p): + g = ('G01 ' + self.position_code(p)).format(**p) + g += ' Z' + self.coordinate_format % (p.coords_decimals, p.z_cut) + return g + + def end_code(self, p): + g = ('G00 ' + self.position_code(p)).format(**p) + g += ' Z' + self.coordinate_format % (p.coords_decimals, p.endz) + return g + + def feedrate_code(self, p): + return 'G01 F' + str(self.feedrate_format %(p.fr_decimals, p.feedrate)) + + def feedrate_z_code(self, p): + return 'G01 F' + str(self.feedrate_format %(p.fr_decimals, p.feedrate_z)) + + def spindle_code(self, p): + if p.spindlespeed: + return 'M03 S' + str(p.spindlespeed) + else: + return 'M03' + + def dwell_code(self, p): + if p.dwelltime: + return 'G4 P' + str(p.dwelltime) + + def spindle_stop_code(self,p): + return 'M05'