From 8ba202a79caf7fe00264b5eb7de1053f9cd55432 Mon Sep 17 00:00:00 2001 From: Marius Stanciu Date: Wed, 21 Oct 2020 22:17:23 +0300 Subject: [PATCH] - optimized FlatCAMObj.add_properties_items() method --- CHANGELOG.md | 1 + appGUI/ObjectUI.py | 32 +++++++++++++----- appObjects/FlatCAMObj.py | 73 +++++++++++++++++++++------------------- 3 files changed, 63 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ebbbed8..88679770 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ CHANGELOG for FlatCAM beta - compacted the UI for the 2Sided Tool - added a button in Excellon UI that will hide/show the milling section - optimized a bit the UI for Gerber/Excellon/Geometry objects +- optimized FlatCAMObj.add_properties_items() method 20.10.2020 diff --git a/appGUI/ObjectUI.py b/appGUI/ObjectUI.py index 71f59567..f4ca5d07 100644 --- a/appGUI/ObjectUI.py +++ b/appGUI/ObjectUI.py @@ -1763,6 +1763,22 @@ class GeometryObjectUI(ObjectUI): ) self.grid4.addWidget(self.tools_label, 28, 0, 1, 2) + # Milling Tool - will create GCode for slot holes + self.milling_button = QtWidgets.QPushButton(_('Milling Tool')) + self.milling_button.setIcon(QtGui.QIcon(self.app.resource_location + '/milling_tool32.png')) + self.milling_button.setToolTip( + _("Generate a CNCJob by milling a Geometry.") + ) + self.milling_button.setStyleSheet(""" + QPushButton + { + font-weight: bold; + } + """) + self.grid4.addWidget(self.milling_button, 30, 0, 1, 2) + # FIXME: until the Milling Tool is ready, this get disabled + self.milling_button.setDisabled(True) + # Paint Button self.paint_tool_button = QtWidgets.QPushButton(_('Paint Tool')) self.paint_tool_button.setIcon(QtGui.QIcon(self.app.resource_location + '/paint20_1.png')) @@ -1774,13 +1790,13 @@ class GeometryObjectUI(ObjectUI): "to click on the desired polygon." ) ) - self.paint_tool_button.setStyleSheet(""" - QPushButton - { - font-weight: bold; - } - """) - self.grid4.addWidget(self.paint_tool_button, 30, 0, 1, 2) + # self.paint_tool_button.setStyleSheet(""" + # QPushButton + # { + # font-weight: bold; + # } + # """) + self.grid4.addWidget(self.paint_tool_button, 32, 0, 1, 2) # NCC Tool self.generate_ncc_button = QtWidgets.QPushButton(_('NCC Tool')) @@ -1795,7 +1811,7 @@ class GeometryObjectUI(ObjectUI): # font-weight: bold; # } # """) - self.grid4.addWidget(self.generate_ncc_button, 32, 0, 1, 2) + self.grid4.addWidget(self.generate_ncc_button, 34, 0, 1, 2) class CNCObjectUI(ObjectUI): diff --git a/appObjects/FlatCAMObj.py b/appObjects/FlatCAMObj.py index 78a7c0b9..624fbf7b 100644 --- a/appObjects/FlatCAMObj.py +++ b/appObjects/FlatCAMObj.py @@ -523,7 +523,7 @@ class FlatCAMObj(QtCore.QObject): length = abs(xmax - xmin) width = abs(ymax - ymin) except Exception as ee: - log.debug("FlatCAMObj.addItems() -> calculate dimensions --> %s" % str(ee)) + log.debug("FlatCAMObj.add_properties_items() -> calculate dimensions --> %s" % str(ee)) # calculate box area if self.app.defaults['units'].lower() == 'mm': @@ -531,7 +531,7 @@ class FlatCAMObj(QtCore.QObject): else: area = length * width - if obj_prop.kind.lower() == 'gerber': + if obj_prop.kind.lower() == 'gerber' and geo: # calculate copper area try: for geo_el in geo: @@ -554,7 +554,7 @@ class FlatCAMObj(QtCore.QObject): xmax.append(x1) ymax.append(y1) except Exception as ee: - log.debug("FlatCAMObj.addItems() --> %s" % str(ee)) + log.debug("FlatCAMObj.add_properties_items() cncjob --> %s" % str(ee)) try: for tool_k in obj_prop.cnc_tools: @@ -564,19 +564,24 @@ class FlatCAMObj(QtCore.QObject): xmax.append(x1) ymax.append(y1) except Exception as ee: - log.debug("FlatCAMObj.addItems() --> %s" % str(ee)) + log.debug("FlatCAMObj.add_properties_items() cncjob --> %s" % str(ee)) else: try: - for tool_k in obj_prop.tools: - x0, y0, x1, y1 = unary_union(obj_prop.tools[tool_k]['solid_geometry']).bounds - xmin.append(x0) - ymin.append(y0) - xmax.append(x1) - ymax.append(y1) + if obj_prop.tools: + for tool_k in obj_prop.tools: + t_geo = obj_prop.tools[tool_k]['solid_geometry'] + try: + x0, y0, x1, y1 = unary_union(t_geo).bounds + except Exception: + continue + xmin.append(x0) + ymin.append(y0) + xmax.append(x1) + ymax.append(y1) except Exception as ee: - log.debug("FlatCAMObj.addItems() --> %s" % str(ee)) + log.debug("FlatCAMObj.add_properties_items() not cncjob tools --> %s" % str(ee)) - try: + if xmin and ymin and xmax and ymax: xmin = min(xmin) ymin = min(ymin) xmax = max(xmax) @@ -591,24 +596,20 @@ class FlatCAMObj(QtCore.QObject): else: area = length * width - if obj_prop.kind.lower() == 'gerber': - # calculate copper area + if obj_prop.kind.lower() == 'gerber' and obj_prop.tools: + # calculate copper area - # create a complete solid_geometry from the tools - geo_tools = [] - for tool_k in obj_prop.tools: - if 'solid_geometry' in obj_prop.tools[tool_k]: - for geo_el in obj_prop.tools[tool_k]['solid_geometry']: - geo_tools.append(geo_el) + # create a complete solid_geometry from the tools + geo_tools = [] + for tool_k in obj_prop.tools: + if 'solid_geometry' in obj_prop.tools[tool_k]: + for geo_el in obj_prop.tools[tool_k]['solid_geometry']: + geo_tools.append(geo_el) - try: - for geo_el in geo_tools: - copper_area += geo_el.area - except TypeError: - copper_area += geo_tools.area - copper_area /= 100 - except Exception as err: - log.debug("FlatCAMObj.addItems() --> %s" % str(err)) + for geo_el in geo_tools: + copper_area += geo_el.area + # in cm2 + copper_area /= 100 area_chull = 0.0 if obj_prop.kind.lower() != 'cncjob': @@ -630,14 +631,16 @@ class FlatCAMObj(QtCore.QObject): area_chull = 0 else: try: - area_chull = [] - for tool_k in obj_prop.tools: - area_el = unary_union(obj_prop.tools[tool_k]['solid_geometry']).convex_hull - area_chull.append(area_el.area) - area_chull = max(area_chull) + area_chull = None + if obj_prop.tools: + area_chull_list = [] + for tool_k in obj_prop.tools: + area_el = unary_union(obj_prop.tools[tool_k]['solid_geometry']).convex_hull + area_chull_list.append(area_el.area) + area_chull = max(area_chull_list) except Exception as er: area_chull = None - log.debug("FlatCAMObj.addItems() --> %s" % str(er)) + log.debug("FlatCAMObj.add_properties_items() area chull--> %s" % str(er)) if self.app.defaults['units'].lower() == 'mm' and area_chull: area_chull = area_chull / 100 @@ -656,7 +659,7 @@ class FlatCAMObj(QtCore.QObject): self.treeWidget.addChild(options, [str(option), str(obj.options[option])], True) # Items that depend on the object type - if obj.kind.lower() == 'gerber': + if obj.kind.lower() == 'gerber' and obj.apertures: temp_ap = {} for ap in obj.apertures: temp_ap.clear()