From c5e4d72db8689aaeb9c18bd9552c8ff32fc77000 Mon Sep 17 00:00:00 2001 From: Marius Stanciu Date: Fri, 28 Feb 2020 17:59:15 +0200 Subject: [PATCH] - solved issue #381 where there was an error when trying to generate CNCJob out of an Excellon file that have a tool with only slots and no drills - solved some issues in the preprocessors regarding the newly introduced feature that allow control of the final move X,Y positions --- README.md | 2 + camlib.py | 88 +++++++++++++++-------- preprocessors/Berta_CNC.py | 2 +- preprocessors/GRBL_laser.py | 2 +- preprocessors/ISEL_CNC.py | 2 +- preprocessors/Marlin.py | 2 +- preprocessors/Marlin_laser_FAN_pin.py | 2 +- preprocessors/Marlin_laser_Spindle_pin.py | 2 +- preprocessors/Paste_1.py | 2 +- preprocessors/Repetier.py | 2 +- preprocessors/Toolchange_Custom.py | 2 +- preprocessors/Toolchange_Manual.py | 2 +- preprocessors/Toolchange_Probe_MACH3.py | 2 +- preprocessors/default.py | 2 +- preprocessors/grbl_11.py | 2 +- preprocessors/line_xyz.py | 2 +- 16 files changed, 73 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index df9ef53c..b0e69180 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,8 @@ CAD program, and create G-Code for Isolation routing. 28.02.2020 - some small changes in preprocessors +- solved issue #381 where there was an error when trying to generate CNCJob out of an Excellon file that have a tool with only slots and no drills +- solved some issues in the preprocessors regarding the newly introduced feature that allow control of the final move X,Y positions 25.02.2020 diff --git a/camlib.py b/camlib.py index b56d65f5..a1ca1375 100644 --- a/camlib.py +++ b/camlib.py @@ -2647,8 +2647,8 @@ class CNCjob(Geometry): if self.xy_toolchange == '': self.xy_toolchange = None else: - self.xy_toolchange = [float(eval(a)) for a in self.xy_toolchange.split(",")] - if len(self.xy_toolchange) < 2: + self.xy_toolchange = [float(eval(a)) for a in self.xy_toolchange.split(",") if self.xy_toolchange != ''] + if self.xy_toolchange and len(self.xy_toolchange) < 2: self.app.inform.emit('[ERROR]%s' % _("The Toolchange X,Y field in Edit -> Preferences has to be " "in the format (x, y) \nbut now there is only one value, not two. ")) @@ -2657,8 +2657,8 @@ class CNCjob(Geometry): log.debug("camlib.CNCJob.generate_from_excellon_by_tool() --> %s" % str(e)) pass - self.xy_end = [float(eval(a)) for a in self.xy_end.split(",")] - if len(self.xy_end) < 2: + self.xy_end = [float(eval(a)) for a in self.xy_end.split(",") if self.xy_end != ''] + if self.xy_end and len(self.xy_end) < 2: self.app.inform.emit('[ERROR] %s' % _("The End Move X,Y field in Edit -> Preferences has to be " "in the format (x, y) but now there is only one value, not two.")) return 'fail' @@ -2783,23 +2783,25 @@ class CNCjob(Geometry): class CreateDistanceCallback(object): """Create callback to calculate distances between points.""" - def __init__(self): + def __init__(self, tool): """Initialize distance array.""" - locations = create_data_array() - size = len(locations) + locations = create_data_array(tool) self.matrix = dict() - for from_node in range(size): - self.matrix[from_node] = {} - for to_node in range(size): - if from_node == to_node: - self.matrix[from_node][to_node] = 0 - else: - x1 = locations[from_node][0] - y1 = locations[from_node][1] - x2 = locations[to_node][0] - y2 = locations[to_node][1] - self.matrix[from_node][to_node] = distance_euclidian(x1, y1, x2, y2) + if locations: + size = len(locations) + + for from_node in range(size): + self.matrix[from_node] = {} + for to_node in range(size): + if from_node == to_node: + self.matrix[from_node][to_node] = 0 + else: + x1 = locations[from_node][0] + y1 = locations[from_node][1] + x2 = locations[to_node][0] + y2 = locations[to_node][1] + self.matrix[from_node][to_node] = distance_euclidian(x1, y1, x2, y2) # def Distance(self, from_node, to_node): # return int(self.matrix[from_node][to_node]) @@ -2810,11 +2812,15 @@ class CNCjob(Geometry): return self.matrix[from_node][to_node] # Create the data. - def create_data_array(): - locations = [] + def create_data_array(tool): + loc_list = list() + + if tool not in points: + return None + for point in points[tool]: - locations.append((point.coords.xy[0][0], point.coords.xy[1][0])) - return locations + loc_list.append((point.coords.xy[0][0], point.coords.xy[1][0])) + return loc_list if self.xy_toolchange is not None: self.oldx = self.xy_toolchange[0] @@ -2884,7 +2890,12 @@ class CNCjob(Geometry): # ############################################### node_list = [] - locations = create_data_array() + locations = create_data_array(tool=tool) + + # if there are no locations then go to the next tool + if not locations: + continue + tsp_size = len(locations) num_routes = 1 # The number of routes, which is 1 in the TSP. # Nodes are indexed from 0 to tsp_size - 1. The depot is the starting node of the route. @@ -2906,7 +2917,12 @@ class CNCjob(Geometry): # Callback to the distance function. The callback takes two # arguments (the from and to node indices) and returns the distance between them. - dist_between_locations = CreateDistanceCallback() + dist_between_locations = CreateDistanceCallback(tool=tool) + + # if there are no distances then go to the next tool + if not dist_between_locations: + continue + dist_callback = dist_between_locations.Distance transit_callback_index = routing.RegisterTransitCallback(dist_callback) routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index) @@ -3088,7 +3104,7 @@ class CNCjob(Geometry): old_zcut = deepcopy(self.z_cut) self.z_move = exobj.tools[tool]['data']['travelz'] - print(self.z_move) + self.spindlespeed = exobj.tools[tool]['data']['spindlespeed'] self.dwell = exobj.tools[tool]['data']['dwell'] self.dwelltime = exobj.tools[tool]['data']['dwelltime'] @@ -3100,7 +3116,12 @@ class CNCjob(Geometry): # ############################################### node_list = [] - locations = create_data_array() + locations = create_data_array(tool=tool) + + # if there are no locations then go to the next tool + if not locations: + continue + tsp_size = len(locations) num_routes = 1 # The number of routes, which is 1 in the TSP. @@ -3115,7 +3136,12 @@ class CNCjob(Geometry): # Callback to the distance function. The callback takes two # arguments (the from and to node indices) and returns the distance between them. - dist_between_locations = CreateDistanceCallback() + dist_between_locations = CreateDistanceCallback(tool=tool) + + # if there are no distances then go to the next tool + if not dist_between_locations: + continue + dist_callback = dist_between_locations.Distance transit_callback_index = routing.RegisterTransitCallback(dist_callback) routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index) @@ -3519,8 +3545,8 @@ class CNCjob(Geometry): self.startz = float(startz) if startz is not None else None self.z_end = float(endz) if endz is not None else None - self.xy_end = [float(eval(a)) for a in endxy.split(",")] - if len(self.xy_end) < 2: + self.xy_end = [float(eval(a)) for a in endxy.split(",") if endxy != ''] + if self.xy_end and len(self.xy_end) < 2: self.app.inform.emit('[ERROR] %s' % _("The End Move X,Y field in Edit -> Preferences has to be " "in the format (x, y) but now there is only one value, not two.")) return 'fail' @@ -3887,8 +3913,8 @@ class CNCjob(Geometry): self.startz = float(startz) if startz is not None else self.app.defaults["geometry_startz"] self.z_end = float(endz) if endz is not None else self.app.defaults["geometry_endz"] self.xy_end = endxy if endxy != '' else self.app.defaults["geometry_endxy"] - self.xy_end = [float(eval(a)) for a in self.xy_end.split(",")] - if len(self.xy_end) < 2: + self.xy_end = [float(eval(a)) for a in self.xy_end.split(",") if self.xy_end != ''] + if self.xy_end and len(self.xy_end) < 2: self.app.inform.emit('[ERROR] %s' % _("The End Move X,Y field in Edit -> Preferences has to be " "in the format (x, y) but now there is only one value, not two.")) return 'fail' diff --git a/preprocessors/Berta_CNC.py b/preprocessors/Berta_CNC.py index 59ed4e20..8239d0e9 100644 --- a/preprocessors/Berta_CNC.py +++ b/preprocessors/Berta_CNC.py @@ -230,7 +230,7 @@ M0""".format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolcha coords_xy = p['xy_end'] gcode = ('G00 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + "\n") - if coords_xy != '': + if coords_xy and coords_xy != '': gcode += 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n" gcode += '(Berta)\n' diff --git a/preprocessors/GRBL_laser.py b/preprocessors/GRBL_laser.py index c6ddb280..9caf4f39 100644 --- a/preprocessors/GRBL_laser.py +++ b/preprocessors/GRBL_laser.py @@ -86,7 +86,7 @@ class GRBL_laser(FlatCAMPostProc): coords_xy = p['xy_end'] gcode = ('G00 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + "\n") - if coords_xy != '': + if coords_xy and coords_xy != '': gcode += 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n" return gcode diff --git a/preprocessors/ISEL_CNC.py b/preprocessors/ISEL_CNC.py index 805cf35a..8c7c9ed0 100644 --- a/preprocessors/ISEL_CNC.py +++ b/preprocessors/ISEL_CNC.py @@ -160,7 +160,7 @@ M01""".format(tool=int(p.tool), toolC=toolC_formatted) coords_xy = p['xy_end'] gcode = ('G00 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + "\n") - if coords_xy != '': + if coords_xy and coords_xy != '': gcode += 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n" return gcode diff --git a/preprocessors/Marlin.py b/preprocessors/Marlin.py index 2f46183f..d53a257f 100644 --- a/preprocessors/Marlin.py +++ b/preprocessors/Marlin.py @@ -218,7 +218,7 @@ G0 Z{z_toolchange} coords_xy = p['xy_end'] gcode = ('G0 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + " " + self.feedrate_rapid_code(p) + "\n") - if coords_xy != '': + if coords_xy and coords_xy != '': gcode += 'G0 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + " " + self.feedrate_rapid_code(p) + "\n" return gcode diff --git a/preprocessors/Marlin_laser_FAN_pin.py b/preprocessors/Marlin_laser_FAN_pin.py index fb515d5f..2d06d23d 100644 --- a/preprocessors/Marlin_laser_FAN_pin.py +++ b/preprocessors/Marlin_laser_FAN_pin.py @@ -88,7 +88,7 @@ class Marlin_laser_FAN_pin(FlatCAMPostProc): coords_xy = p['xy_end'] gcode = ('G0 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + " " + self.feedrate_rapid_code(p) + "\n") - if coords_xy != '': + if coords_xy and coords_xy != '': gcode += 'G0 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + " " + self.feedrate_rapid_code(p) + "\n" return gcode diff --git a/preprocessors/Marlin_laser_Spindle_pin.py b/preprocessors/Marlin_laser_Spindle_pin.py index 370d0c33..151e9dc4 100644 --- a/preprocessors/Marlin_laser_Spindle_pin.py +++ b/preprocessors/Marlin_laser_Spindle_pin.py @@ -89,7 +89,7 @@ class Marlin_laser_Spindle_pin(FlatCAMPostProc): coords_xy = p['xy_end'] gcode = ('G0 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + " " + self.feedrate_rapid_code(p) + "\n") - if coords_xy != '': + if coords_xy and coords_xy != '': gcode += 'G0 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + " " + self.feedrate_rapid_code(p) + "\n" return gcode diff --git a/preprocessors/Paste_1.py b/preprocessors/Paste_1.py index b12d0f67..cbb0b19f 100644 --- a/preprocessors/Paste_1.py +++ b/preprocessors/Paste_1.py @@ -125,7 +125,7 @@ G00 Z{z_toolchange} coords_xy = [float(eval(a)) for a in p['xy_end'].split(",") if a != ''] gcode = ('G00 Z' + self.feedrate_format % (p.fr_decimals, float(p['z_toolchange'])) + "\n") - if coords_xy != '': + if coords_xy and coords_xy != '': gcode += 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n" return gcode diff --git a/preprocessors/Repetier.py b/preprocessors/Repetier.py index 4bdca261..bb17f9ee 100644 --- a/preprocessors/Repetier.py +++ b/preprocessors/Repetier.py @@ -209,7 +209,7 @@ G0 Z{z_toolchange} coords_xy = p['xy_end'] gcode = ('G0 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + " " + self.feedrate_rapid_code(p) + "\n") - if coords_xy != '': + if coords_xy and coords_xy != '': gcode += 'G0 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + " " + self.feedrate_rapid_code(p) + "\n" return gcode diff --git a/preprocessors/Toolchange_Custom.py b/preprocessors/Toolchange_Custom.py index 39468d5d..1c2eeffc 100644 --- a/preprocessors/Toolchange_Custom.py +++ b/preprocessors/Toolchange_Custom.py @@ -176,7 +176,7 @@ M6 coords_xy = p['xy_end'] gcode = ('G00 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + "\n") - if coords_xy != '': + if coords_xy and coords_xy != '': gcode += 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n" return gcode diff --git a/preprocessors/Toolchange_Manual.py b/preprocessors/Toolchange_Manual.py index 432a0013..27aaec22 100644 --- a/preprocessors/Toolchange_Manual.py +++ b/preprocessors/Toolchange_Manual.py @@ -238,7 +238,7 @@ M0 coords_xy = p['xy_end'] gcode = ('G00 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + "\n") - if coords_xy != '': + if coords_xy and coords_xy != '': gcode += 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n" return gcode diff --git a/preprocessors/Toolchange_Probe_MACH3.py b/preprocessors/Toolchange_Probe_MACH3.py index 1cfa0baa..f5f8c9c3 100644 --- a/preprocessors/Toolchange_Probe_MACH3.py +++ b/preprocessors/Toolchange_Probe_MACH3.py @@ -275,7 +275,7 @@ M0 coords_xy = p['xy_end'] gcode = ('G00 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + "\n") - if coords_xy != '': + if coords_xy and coords_xy != '': gcode += 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n" return gcode diff --git a/preprocessors/default.py b/preprocessors/default.py index f7fadddc..7c5cf1c6 100644 --- a/preprocessors/default.py +++ b/preprocessors/default.py @@ -220,7 +220,7 @@ G00 Z{z_toolchange} end_coords_xy = p['xy_end'] gcode = ('G00 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + "\n") - if end_coords_xy != '': + if end_coords_xy and end_coords_xy != '': gcode += 'G00 X{x} Y{y}'.format(x=end_coords_xy[0], y=end_coords_xy[1]) + "\n" return gcode diff --git a/preprocessors/grbl_11.py b/preprocessors/grbl_11.py index 1bd28189..b002b2b2 100644 --- a/preprocessors/grbl_11.py +++ b/preprocessors/grbl_11.py @@ -221,7 +221,7 @@ G00 Z{z_toolchange} coords_xy = p['xy_end'] gcode = ('G00 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + "\n") - if coords_xy != '': + if coords_xy and coords_xy != '': gcode += 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n" return gcode diff --git a/preprocessors/line_xyz.py b/preprocessors/line_xyz.py index 6187c1ae..900d7fab 100644 --- a/preprocessors/line_xyz.py +++ b/preprocessors/line_xyz.py @@ -207,7 +207,7 @@ M0""".format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolcha def end_code(self, p): coords_xy = p['xy_end'] - if coords_xy != '': + if coords_xy and coords_xy != '': g = 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n" else: g = ('G00 ' + self.position_code(p)).format(**p)