ToolProperties.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # File Author: Marius Adrian Stanciu (c) #
  4. # Date: 3/10/2019 #
  5. # MIT Licence #
  6. # ##########################################################
  7. from PyQt5 import QtGui, QtCore, QtWidgets
  8. from FlatCAMTool import FlatCAMTool
  9. from shapely.geometry import MultiPolygon, Polygon
  10. from shapely.ops import cascaded_union
  11. from copy import deepcopy
  12. import logging
  13. import gettext
  14. import FlatCAMTranslation as fcTranslate
  15. import builtins
  16. fcTranslate.apply_language('strings')
  17. if '_' not in builtins.__dict__:
  18. _ = gettext.gettext
  19. log = logging.getLogger('base')
  20. class Properties(FlatCAMTool):
  21. toolName = _("Properties")
  22. calculations_finished = QtCore.pyqtSignal(float, float, float, float, float, object)
  23. def __init__(self, app):
  24. FlatCAMTool.__init__(self, app)
  25. self.setSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Ignored)
  26. self.decimals = 4
  27. # this way I can hide/show the frame
  28. self.properties_frame = QtWidgets.QFrame()
  29. self.properties_frame.setContentsMargins(0, 0, 0, 0)
  30. self.layout.addWidget(self.properties_frame)
  31. self.properties_box = QtWidgets.QVBoxLayout()
  32. self.properties_box.setContentsMargins(0, 0, 0, 0)
  33. self.properties_frame.setLayout(self.properties_box)
  34. # ## Title
  35. title_label = QtWidgets.QLabel("%s" % self.toolName)
  36. title_label.setStyleSheet("""
  37. QLabel
  38. {
  39. font-size: 16px;
  40. font-weight: bold;
  41. }
  42. """)
  43. self.properties_box.addWidget(title_label)
  44. # self.layout.setMargin(0) # PyQt4
  45. self.properties_box.setContentsMargins(0, 0, 0, 0) # PyQt5
  46. self.vlay = QtWidgets.QVBoxLayout()
  47. self.properties_box.addLayout(self.vlay)
  48. self.treeWidget = QtWidgets.QTreeWidget()
  49. self.treeWidget.setColumnCount(2)
  50. self.treeWidget.setHeaderHidden(True)
  51. self.treeWidget.header().setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
  52. self.treeWidget.setSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Expanding)
  53. self.vlay.addWidget(self.treeWidget)
  54. self.vlay.setStretch(0, 0)
  55. self.calculations_finished.connect(self.show_area_chull)
  56. def run(self, toggle=True):
  57. self.app.report_usage("ToolProperties()")
  58. if self.app.tool_tab_locked is True:
  59. return
  60. if toggle:
  61. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  62. if self.app.ui.splitter.sizes()[0] == 0:
  63. self.app.ui.splitter.setSizes([1, 1])
  64. else:
  65. try:
  66. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  67. # if tab is populated with the tool but it does not have the focus, focus on it
  68. if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
  69. # focus on Tool Tab
  70. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  71. else:
  72. self.app.ui.splitter.setSizes([0, 1])
  73. except AttributeError:
  74. pass
  75. else:
  76. if self.app.ui.splitter.sizes()[0] == 0:
  77. self.app.ui.splitter.setSizes([1, 1])
  78. FlatCAMTool.run(self)
  79. self.set_tool_ui()
  80. self.properties()
  81. def install(self, icon=None, separator=None, **kwargs):
  82. FlatCAMTool.install(self, icon, separator, shortcut='P', **kwargs)
  83. def set_tool_ui(self):
  84. # this reset the TreeWidget
  85. self.treeWidget.clear()
  86. self.properties_frame.show()
  87. def properties(self):
  88. obj_list = self.app.collection.get_selected()
  89. if not obj_list:
  90. self.app.inform.emit('[ERROR_NOTCL] %s' %
  91. _("Properties Tool was not displayed. No object selected."))
  92. self.app.ui.notebook.setTabText(2, _("Tools"))
  93. self.properties_frame.hide()
  94. self.app.ui.notebook.setCurrentWidget(self.app.ui.project_tab)
  95. return
  96. for obj in obj_list:
  97. self.addItems(obj)
  98. self.app.inform.emit('[success] %s' %
  99. _("Object Properties are displayed."))
  100. self.app.ui.notebook.setTabText(2, _("Properties Tool"))
  101. def addItems(self, obj):
  102. parent = self.treeWidget.invisibleRootItem()
  103. apertures = ''
  104. tools = ''
  105. font = QtGui.QFont()
  106. font.setBold(True)
  107. obj_type = self.addParent(parent, _('TYPE'), expanded=True, color=QtGui.QColor("#000000"), font=font)
  108. obj_name = self.addParent(parent, _('NAME'), expanded=True, color=QtGui.QColor("#000000"), font=font)
  109. dims = self.addParent(parent, _('Dimensions'), expanded=True, color=QtGui.QColor("#000000"), font=font)
  110. units = self.addParent(parent, _('Units'), expanded=True, color=QtGui.QColor("#000000"), font=font)
  111. options = self.addParent(parent, _('Options'), color=QtGui.QColor("#000000"), font=font)
  112. if obj.kind.lower() == 'gerber':
  113. apertures = self.addParent(parent, _('Apertures'), expanded=True, color=QtGui.QColor("#000000"), font=font)
  114. else:
  115. tools = self.addParent(parent, _('Tools'), expanded=True, color=QtGui.QColor("#000000"), font=font)
  116. separator = self.addParent(parent, '')
  117. self.addChild(obj_type, ['%s:' % _('Object Type'), ('%s' % (obj.kind.capitalize()))], True)
  118. try:
  119. self.addChild(obj_type,
  120. ['%s:' % _('Geo Type'),
  121. ('%s' % ({False: _("Single-Geo"), True: _("Multi-Geo")}[obj.multigeo]))],
  122. True)
  123. except Exception as e:
  124. log.debug("Properties.addItems() --> %s" % str(e))
  125. self.addChild(obj_name, [obj.options['name']])
  126. def job_thread(obj_prop):
  127. proc = self.app.proc_container.new(_("Calculating dimensions ... Please wait."))
  128. length = 0.0
  129. width = 0.0
  130. area = 0.0
  131. copper_area = 0.0
  132. geo = obj_prop.solid_geometry
  133. if geo:
  134. # calculate physical dimensions
  135. try:
  136. xmin, ymin, xmax, ymax = obj_prop.bounds()
  137. length = abs(xmax - xmin)
  138. width = abs(ymax - ymin)
  139. except Exception as e:
  140. log.debug("PropertiesTool.addItems() -> calculate dimensions --> %s" % str(e))
  141. # calculate box area
  142. if self.app.defaults['units'].lower() == 'mm':
  143. area = (length * width) / 100
  144. else:
  145. area = length * width
  146. if obj_prop.kind.lower() == 'gerber':
  147. # calculate copper area
  148. try:
  149. for geo_el in geo:
  150. copper_area += geo_el.area
  151. except TypeError:
  152. copper_area += geo.area
  153. copper_area /= 100
  154. else:
  155. xmin = []
  156. ymin = []
  157. xmax = []
  158. ymax = []
  159. for tool_k in obj_prop.tools:
  160. try:
  161. x0, y0, x1, y1 = cascaded_union(obj_prop.tools[tool_k]['solid_geometry']).bounds
  162. xmin.append(x0)
  163. ymin.append(y0)
  164. xmax.append(x1)
  165. ymax.append(y1)
  166. except Exception as ee:
  167. log.debug("PropertiesTool.addItems() --> %s" % str(ee))
  168. try:
  169. xmin = min(xmin)
  170. ymin = min(ymin)
  171. xmax = max(xmax)
  172. ymax = max(ymax)
  173. length = abs(xmax - xmin)
  174. width = abs(ymax - ymin)
  175. # calculate box area
  176. if self.app.defaults['units'].lower() == 'mm':
  177. area = (length * width) / 100
  178. else:
  179. area = length * width
  180. if obj_prop.kind.lower() == 'gerber':
  181. # calculate copper area
  182. # create a complete solid_geometry from the tools
  183. geo_tools = list()
  184. for tool_k in obj_prop.tools:
  185. if 'solid_geometry' in obj_prop.tools[tool_k]:
  186. for geo_el in obj_prop.tools[tool_k]['solid_geometry']:
  187. geo_tools.append(geo_el)
  188. try:
  189. for geo_el in geo_tools:
  190. copper_area += geo_el.area
  191. except TypeError:
  192. copper_area += geo_tools.area
  193. copper_area /= 100
  194. except Exception as e:
  195. log.debug("Properties.addItems() --> %s" % str(e))
  196. area_chull = 0.0
  197. if obj_prop.kind.lower() != 'cncjob':
  198. # calculate and add convex hull area
  199. if geo:
  200. if isinstance(geo, MultiPolygon):
  201. env_obj = geo.convex_hull
  202. elif (isinstance(geo, MultiPolygon) and len(geo) == 1) or \
  203. (isinstance(geo, list) and len(geo) == 1) and isinstance(geo[0], Polygon):
  204. env_obj = cascaded_union(obj_prop.solid_geometry)
  205. env_obj = env_obj.convex_hull
  206. else:
  207. env_obj = cascaded_union(obj_prop.solid_geometry)
  208. env_obj = env_obj.convex_hull
  209. area_chull = env_obj.area
  210. else:
  211. try:
  212. area_chull = []
  213. for tool_k in obj_prop.tools:
  214. area_el = cascaded_union(obj_prop.tools[tool_k]['solid_geometry']).convex_hull
  215. area_chull.append(area_el.area)
  216. area_chull = max(area_chull)
  217. except Exception as e:
  218. area_chull = None
  219. log.debug("Properties.addItems() --> %s" % str(e))
  220. if self.app.defaults['units'].lower() == 'mm':
  221. area_chull = area_chull / 100
  222. self.calculations_finished.emit(area, length, width, area_chull, copper_area, dims)
  223. self.app.worker_task.emit({'fcn': job_thread, 'params': [obj]})
  224. f_unit = {'in': _('Inch'), 'mm': _('Metric')}[str(self.app.defaults['units'].lower())]
  225. self.addChild(units, ['FlatCAM units:', f_unit], True)
  226. for option in obj.options:
  227. if option is 'name':
  228. continue
  229. self.addChild(options, [str(option), str(obj.options[option])], True)
  230. if obj.kind.lower() == 'gerber':
  231. temp_ap = dict()
  232. for ap in obj.apertures:
  233. temp_ap.clear()
  234. temp_ap = deepcopy(obj.apertures[ap])
  235. temp_ap.pop('geometry', None)
  236. solid_nr = 0
  237. follow_nr = 0
  238. clear_nr = 0
  239. if 'geometry' in obj.apertures[ap]:
  240. if obj.apertures[ap]['geometry']:
  241. font.setBold(True)
  242. for el in obj.apertures[ap]['geometry']:
  243. if 'solid' in el:
  244. solid_nr += 1
  245. if 'follow' in el:
  246. follow_nr += 1
  247. if 'clear' in el:
  248. clear_nr += 1
  249. else:
  250. font.setBold(False)
  251. temp_ap['Solid_Geo'] = '%s Polygons' % str(solid_nr)
  252. temp_ap['Follow_Geo'] = '%s LineStrings' % str(follow_nr)
  253. temp_ap['Clear_Geo'] = '%s Polygons' % str(clear_nr)
  254. apid = self.addParent(apertures, str(ap), expanded=False, color=QtGui.QColor("#000000"), font=font)
  255. for key in temp_ap:
  256. self.addChild(apid, [str(key), str(temp_ap[key])], True)
  257. elif obj.kind.lower() == 'excellon':
  258. for tool, value in obj.tools.items():
  259. self.addChild(tools, [str(tool), str(value['C'])], True)
  260. elif obj.kind.lower() == 'geometry':
  261. for tool, value in obj.tools.items():
  262. geo_tool = self.addParent(tools, str(tool), expanded=True, color=QtGui.QColor("#000000"), font=font)
  263. for k, v in value.items():
  264. if k == 'solid_geometry':
  265. printed_value = _('Present') if v else _('None')
  266. self.addChild(geo_tool, [str(k), printed_value], True)
  267. elif k == 'data':
  268. tool_data = self.addParent(geo_tool, str(k).capitalize(),
  269. color=QtGui.QColor("#000000"), font=font)
  270. for data_k, data_v in v.items():
  271. self.addChild(tool_data, [str(data_k), str(data_v)], True)
  272. else:
  273. self.addChild(geo_tool, [str(k), str(v)], True)
  274. elif obj.kind.lower() == 'cncjob':
  275. for tool, value in obj.cnc_tools.items():
  276. geo_tool = self.addParent(tools, str(tool), expanded=True, color=QtGui.QColor("#000000"), font=font)
  277. for k, v in value.items():
  278. if k == 'solid_geometry':
  279. printed_value = _('Present') if v else _('None')
  280. self.addChild(geo_tool, [str(k), printed_value], True)
  281. elif k == 'gcode':
  282. printed_value = _('Present') if v != '' else _('None')
  283. self.addChild(geo_tool, [str(k), printed_value], True)
  284. elif k == 'gcode_parsed':
  285. printed_value = _('Present') if v else _('None')
  286. self.addChild(geo_tool, [str(k), printed_value], True)
  287. elif k == 'data':
  288. tool_data = self.addParent(geo_tool, str(k).capitalize(),
  289. color=QtGui.QColor("#000000"), font=font)
  290. for data_k, data_v in v.items():
  291. self.addChild(tool_data, [str(data_k), str(data_v)], True)
  292. else:
  293. self.addChild(geo_tool, [str(k), str(v)], True)
  294. self.addChild(separator, [''])
  295. def addParent(self, parent, title, expanded=False, color=None, font=None):
  296. item = QtWidgets.QTreeWidgetItem(parent, [title])
  297. item.setChildIndicatorPolicy(QtWidgets.QTreeWidgetItem.ShowIndicator)
  298. item.setExpanded(expanded)
  299. if color is not None:
  300. # item.setTextColor(0, color) # PyQt4
  301. item.setForeground(0, QtGui.QBrush(color))
  302. if font is not None:
  303. item.setFont(0, font)
  304. return item
  305. def addChild(self, parent, title, column1=None):
  306. item = QtWidgets.QTreeWidgetItem(parent)
  307. item.setText(0, str(title[0]))
  308. if column1 is not None:
  309. item.setText(1, str(title[1]))
  310. def show_area_chull(self, area, length, width, chull_area, copper_area, location):
  311. # add dimensions
  312. self.addChild(
  313. location,
  314. ['%s:' % _('Length'), '%.*f %s' % (self.decimals, length, self.app.defaults['units'].lower())],
  315. True
  316. )
  317. self.addChild(
  318. location,
  319. ['%s:' % _('Width'), '%.*f %s' % (self.decimals, width, self.app.defaults['units'].lower())],
  320. True
  321. )
  322. # add box area
  323. if self.app.defaults['units'].lower() == 'mm':
  324. self.addChild(location, ['%s:' % _('Box Area'), '%.*f %s' % (self.decimals, area, 'cm2')], True)
  325. self.addChild(
  326. location,
  327. ['%s:' % _('Convex_Hull Area'), '%.*f %s' % (self.decimals, chull_area, 'cm2')],
  328. True
  329. )
  330. else:
  331. self.addChild(location, ['%s:' % _('Box Area'), '%.*f %s' % (self.decimals, area, 'in2')], True)
  332. self.addChild(
  333. location,
  334. ['%s:' % _('Convex_Hull Area'), '%.*f %s' % (self.decimals, chull_area, 'in2')],
  335. True
  336. )
  337. # add copper area
  338. if self.app.defaults['units'].lower() == 'mm':
  339. self.addChild(location, ['%s:' % _('Copper Area'), '%.*f %s' % (self.decimals, copper_area, 'cm2')], True)
  340. else:
  341. self.addChild(location, ['%s:' % _('Copper Area'), '%.*f %s' % (self.decimals, copper_area, 'in2')], True)
  342. # end of file