From 874ce11d847730f6ce23f87856fa7651b47c5064 Mon Sep 17 00:00:00 2001 From: Marius Stanciu Date: Tue, 26 Feb 2019 17:03:57 +0200 Subject: [PATCH] - work in progress to Toolchange_Custom code replacememnt -> finished the parse and replace function --- FlatCAMApp.py | 2 +- FlatCAMObj.py | 35 ++++--------------- README.md | 1 + camlib.py | 24 +++++++++++++ ..._Probe_general.py => Toolchange_Custom.py} | 33 ++--------------- 5 files changed, 35 insertions(+), 60 deletions(-) rename postprocessors/{Toolchange_Probe_general.py => Toolchange_Custom.py} (89%) diff --git a/FlatCAMApp.py b/FlatCAMApp.py index 89fea344..fb802b49 100644 --- a/FlatCAMApp.py +++ b/FlatCAMApp.py @@ -3829,7 +3829,7 @@ class App(QtCore.QObject): if notebook_widget_name == 'selected_tab': if str(type(self.collection.get_active())) == "": # Tool add works for Geometry only if Advanced is True in Preferences - if self.defaults["global_advanced"] is True: + if self.defaults["global_app_level"] == 'a': tool_add_popup = FCInputDialog(title="New Tool ...", text='Enter a Tool Diameter:', min=0.0000, max=99.9999, decimals=4) diff --git a/FlatCAMObj.py b/FlatCAMObj.py index a939985b..579ba4f3 100644 --- a/FlatCAMObj.py +++ b/FlatCAMObj.py @@ -1343,9 +1343,6 @@ class FlatCAMExcellon(FlatCAMObj, Excellon): self.multigeo = True - # search for toolchange parameters in the Toolchange Custom Code - self.re_toolchange_custom = re.compile(r'^.*%([a-zA-Z0-9]+)%.*') - # Attributes to be included in serialization # Always append to it because it carries contents # from predecessors. @@ -2256,17 +2253,6 @@ class FlatCAMExcellon(FlatCAMObj, Excellon): self.ui.feedrate_probe_entry.setVisible(False) self.ui.feedrate_probe_label.hide() - def parse_custom_toolchange_code(self, data): - - toolchange_gcode = '' - - lines = StringIO(data) - for line in lines: - match = self.re_toolchange_custom.search(line) - if match: - command = match.group(1) - print(globals()[command]) - def on_create_cncjob_button_click(self, *args): self.app.report_usage("excellon_on_create_cncjob_button") self.read_form() @@ -2759,9 +2745,6 @@ class FlatCAMGeometry(FlatCAMObj, Geometry): # the default value is False self.multigeo = False - # search for toolchange parameters in the Toolchange Custom Code - self.re_toolchange_custom = re.compile(r'^.*%([a-zA-Z0-9]+)%.*') - # flag to store if the geometry is part of a special group of geometries that can't be processed by the default # engine of FlatCAM. Most likely are generated by some of tools and are special cases of geometries. self. special_group = None @@ -3866,17 +3849,6 @@ class FlatCAMGeometry(FlatCAMObj, Geometry): self.ui.feedrate_probe_entry.setVisible(False) self.ui.feedrate_probe_label.hide() - def parse_custom_toolchange_code(self, data): - - toolchange_gcode = '' - - lines = StringIO(data) - for line in lines: - match = self.re_toolchange_custom.search(line) - if match: - command = match.group(1) - print(globals()[command]) - def on_generatecnc_button_click(self, *args): self.app.report_usage("geometry_on_generatecnc_button") @@ -5388,6 +5360,13 @@ class FlatCAMCNCjob(FlatCAMObj, CNCjob): g = gcode[:g_idx] + preamble + '\n' + gcode[g_idx:] + postamble + # if toolchange custom is used, replace M6 code with the code from the Toolchange Custom Text box + if self.ui.toolchange_cb.get_value() is True: + # match = self.re_toolchange.search(g) + if 'M6' in g: + m6_code = self.parse_custom_toolchange_code(self.ui.toolchange_text.get_value()) + g = g.replace('M6', m6_code) + # lines = StringIO(self.gcode) lines = StringIO(g) diff --git a/README.md b/README.md index b75d5327..10f7bce3 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ CAD program, and create G-Code for Isolation routing. - added a function to read the parameters from ToolChange macro Text Box (I need to move it from CNCJob to Excellon and Geometry) - fixed the geometry adding to the self.apertures in the case when regions are done without declaring any aperture first (Allegro does that). Now, that geometry will be stored in the '0' aperture with type REG +- work in progress to Toolchange_Custom code replacememnt -> finished the parse and replace function 25.02.2019 diff --git a/camlib.py b/camlib.py index bbc98324..70849666 100644 --- a/camlib.py +++ b/camlib.py @@ -4602,6 +4602,8 @@ class CNCjob(Geometry): self.toolchange_xy = toolchange_xy self.toolchange_xy_type = None + self.toolC = tooldia + self.endz = endz self.depthpercut = depthpercut @@ -4648,6 +4650,12 @@ class CNCjob(Geometry): self.tool = 0.0 + # search for toolchange parameters in the Toolchange Custom Code + self.re_toolchange_custom = re.compile(r'(%[a-zA-Z0-9\-_]+%)') + + # search for toolchange code: M6 + self.re_toolchange = re.compile(r'^\s*(M6)$') + # Attributes to be included in serialization # Always append to it because it carries contents # from Geometry. @@ -4689,6 +4697,22 @@ class CNCjob(Geometry): self.app.log.error('Exception occurred within a postprocessor: ' + traceback.format_exc()) return '' + def parse_custom_toolchange_code(self, data): + text = data + match_list = self.re_toolchange_custom.findall(text) + + if match_list: + for match in match_list: + command = match.strip('%') + try: + value = getattr(self, command) + except AttributeError: + self.app.inform.emit("[ERROR] There is no such parameter: %s" % str(match)) + log.debug("CNCJob.parse_custom_toolchange_code() --> AttributeError ") + return 'fail' + text = text.replace(match, str(value)) + return text + def optimized_travelling_salesman(self, points, start=None): """ As solving the problem in the brute force way is too slow, diff --git a/postprocessors/Toolchange_Probe_general.py b/postprocessors/Toolchange_Custom.py similarity index 89% rename from postprocessors/Toolchange_Probe_general.py rename to postprocessors/Toolchange_Custom.py index 829294bf..01ce7b56 100644 --- a/postprocessors/Toolchange_Probe_general.py +++ b/postprocessors/Toolchange_Custom.py @@ -1,7 +1,7 @@ from FlatCAMPostProc import * -class Toolchange_Probe_general(FlatCAMPostProc): +class Toolchange_Custom(FlatCAMPostProc): coordinate_format = "%.*f" feedrate_format = '%.*f' @@ -97,29 +97,13 @@ class Toolchange_Probe_general(FlatCAMPostProc): if toolchangexy is not None: gcode = """ -M5 -G00 X{toolchangex} Y{toolchangey} -T{tool} M6 -(MSG, Change to Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills}) -M0 """.format(toolchangex=self.coordinate_format % (p.coords_decimals, toolchangex), toolchangey=self.coordinate_format % (p.coords_decimals, toolchangey), tool=int(p.tool), t_drills=no_drills, toolC=toolC_formatted) - else: - gcode = """ -M5 -T{tool} -M6 -(MSG, Change to Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills}) -M0 -""".format(tool=int(p.tool), - t_drills=no_drills, - toolC=toolC_formatted) - if f_plunge is True: gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move) return gcode @@ -127,24 +111,11 @@ M0 else: if toolchangexy is not None: gcode = """ -M5 -G00 X{toolchangex} Y{toolchangey} -T{tool} -M6 -(MSG, Change to Tool Dia = {toolC}) -M0 +M6 """.format(toolchangex=self.coordinate_format % (p.coords_decimals, toolchangex), toolchangey=self.coordinate_format % (p.coords_decimals, toolchangey), tool=int(p.tool), toolC=toolC_formatted) - else: - gcode = """ -M5 -T{tool} -M6 -(MSG, Change to Tool Dia = {toolC}) -M0""".format(tool=int(p.tool), - toolC=toolC_formatted) if f_plunge is True: gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)