ToolProperties.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. # ########################################################## ##
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # http://flatcam.org #
  4. # File Author: Marius Adrian Stanciu (c) #
  5. # Date: 3/10/2019 #
  6. # MIT Licence #
  7. # ########################################################## ##
  8. from PyQt5 import QtGui, QtCore, QtWidgets
  9. from PyQt5.QtCore import Qt
  10. from FlatCAMTool import FlatCAMTool
  11. from FlatCAMObj import *
  12. import gettext
  13. import FlatCAMTranslation as fcTranslate
  14. import builtins
  15. fcTranslate.apply_language('strings')
  16. if '_' not in builtins.__dict__:
  17. _ = gettext.gettext
  18. class Properties(FlatCAMTool):
  19. toolName = _("Properties")
  20. def __init__(self, app):
  21. FlatCAMTool.__init__(self, app)
  22. self.setSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Ignored)
  23. # this way I can hide/show the frame
  24. self.properties_frame = QtWidgets.QFrame()
  25. self.properties_frame.setContentsMargins(0, 0, 0, 0)
  26. self.layout.addWidget(self.properties_frame)
  27. self.properties_box = QtWidgets.QVBoxLayout()
  28. self.properties_box.setContentsMargins(0, 0, 0, 0)
  29. self.properties_frame.setLayout(self.properties_box)
  30. # ## Title
  31. title_label = QtWidgets.QLabel("%s" % self.toolName)
  32. title_label.setStyleSheet("""
  33. QLabel
  34. {
  35. font-size: 16px;
  36. font-weight: bold;
  37. }
  38. """)
  39. self.properties_box.addWidget(title_label)
  40. # self.layout.setMargin(0) # PyQt4
  41. self.properties_box.setContentsMargins(0, 0, 0, 0) # PyQt5
  42. self.vlay = QtWidgets.QVBoxLayout()
  43. self.properties_box.addLayout(self.vlay)
  44. self.treeWidget = QtWidgets.QTreeWidget()
  45. self.treeWidget.setColumnCount(2)
  46. self.treeWidget.setHeaderHidden(True)
  47. self.treeWidget.header().setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
  48. self.treeWidget.setSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Expanding)
  49. self.vlay.addWidget(self.treeWidget)
  50. self.vlay.setStretch(0, 0)
  51. def run(self, toggle=True):
  52. self.app.report_usage("ToolProperties()")
  53. if self.app.tool_tab_locked is True:
  54. return
  55. if toggle:
  56. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  57. if self.app.ui.splitter.sizes()[0] == 0:
  58. self.app.ui.splitter.setSizes([1, 1])
  59. else:
  60. try:
  61. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  62. # if tab is populated with the tool but it does not have the focus, focus on it
  63. if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
  64. # focus on Tool Tab
  65. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  66. else:
  67. self.app.ui.splitter.setSizes([0, 1])
  68. except AttributeError:
  69. pass
  70. else:
  71. if self.app.ui.splitter.sizes()[0] == 0:
  72. self.app.ui.splitter.setSizes([1, 1])
  73. FlatCAMTool.run(self)
  74. self.set_tool_ui()
  75. self.properties()
  76. def install(self, icon=None, separator=None, **kwargs):
  77. FlatCAMTool.install(self, icon, separator, shortcut='P', **kwargs)
  78. def set_tool_ui(self):
  79. # this reset the TreeWidget
  80. self.treeWidget.clear()
  81. self.properties_frame.show()
  82. def properties(self):
  83. obj_list = self.app.collection.get_selected()
  84. if not obj_list:
  85. self.app.inform.emit(_("[ERROR_NOTCL] Properties Tool was not displayed. No object selected."))
  86. self.app.ui.notebook.setTabText(2, _("Tools"))
  87. self.properties_frame.hide()
  88. self.app.ui.notebook.setCurrentWidget(self.app.ui.project_tab)
  89. return
  90. for obj in obj_list:
  91. self.addItems(obj)
  92. self.app.inform.emit(_("[success] Object Properties are displayed."))
  93. self.app.ui.notebook.setTabText(2, _("Properties Tool"))
  94. def addItems(self, obj):
  95. parent = self.treeWidget.invisibleRootItem()
  96. apertures = ''
  97. tools = ''
  98. font = QtGui.QFont()
  99. font.setBold(True)
  100. obj_type = self.addParent(parent, _('TYPE'), expanded=True, color=QtGui.QColor("#000000"), font=font)
  101. obj_name = self.addParent(parent, _('NAME'), expanded=True, color=QtGui.QColor("#000000"), font=font)
  102. dims = self.addParent(parent, _('Dimensions'), expanded=True, color=QtGui.QColor("#000000"), font=font)
  103. units = self.addParent(parent, _('Units'), expanded=True, color=QtGui.QColor("#000000"), font=font)
  104. options = self.addParent(parent, _('Options'), color=QtGui.QColor("#000000"), font=font)
  105. if obj.kind.lower() == 'gerber':
  106. apertures = self.addParent(parent, _('Apertures'), expanded=True, color=QtGui.QColor("#000000"), font=font)
  107. else:
  108. tools = self.addParent(parent, _('Tools'), expanded=True, color=QtGui.QColor("#000000"), font=font)
  109. separator = self.addParent(parent, '')
  110. self.addChild(obj_type, ['%s:' % _('Object Type'), ('%s' % (obj.kind.capitalize()))], True)
  111. try:
  112. self.addChild(obj_type,
  113. ['%s:' % _('Geo Type'),
  114. ('%s' % ({False: _("Single-Geo"), True: _("Multi-Geo")}[obj.multigeo]))],
  115. True)
  116. except Exception as e:
  117. log.debug("Properties.addItems() --> %s" % str(e))
  118. self.addChild(obj_name, [obj.options['name']])
  119. # calculate physical dimensions
  120. try:
  121. xmin, ymin, xmax, ymax = obj.bounds()
  122. except Exception as e:
  123. log.debug("PropertiesTool.addItems() --> %s" % str(e))
  124. return
  125. length = abs(xmax - xmin)
  126. width = abs(ymax - ymin)
  127. self.addChild(dims, ['%s:' % _('Length'), '%.4f %s' % (
  128. length, self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower())], True)
  129. self.addChild(dims, ['%s:' % _('Width'), '%.4f %s' % (
  130. width, self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower())], True)
  131. # calculate and add box area
  132. if self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower() == 'mm':
  133. area = (length * width) / 100
  134. self.addChild(dims, ['%s:' % _('Box Area'), '%.4f %s' % (area, 'cm2')], True)
  135. else:
  136. area = length * width
  137. self.addChild(dims, ['%s:' % _('Box Area'), '%.4f %s' % (area, 'in2')], True)
  138. if not isinstance(obj, FlatCAMCNCjob):
  139. # calculate and add convex hull area
  140. geo = obj.solid_geometry
  141. if isinstance(geo, MultiPolygon):
  142. env_obj = geo.convex_hull
  143. elif (isinstance(geo, MultiPolygon) and len(geo) == 1) or \
  144. (isinstance(geo, list) and len(geo) == 1) and isinstance(geo[0], Polygon):
  145. env_obj = cascaded_union(obj.solid_geometry)
  146. env_obj = env_obj.convex_hull
  147. else:
  148. env_obj = cascaded_union(obj.solid_geometry)
  149. env_obj = env_obj.convex_hull
  150. area_chull = env_obj.area
  151. if self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower() == 'mm':
  152. area_chull = area_chull / 100
  153. self.addChild(dims, ['%s:' % _('Convex_Hull Area'), '%.4f %s' % (area_chull, 'cm2')], True)
  154. else:
  155. area_chull = area_chull
  156. self.addChild(dims, ['%s:' % _('Convex_Hull Area'), '%.4f %s' % (area_chull, 'in2')], True)
  157. self.addChild(units,
  158. ['FlatCAM units:',
  159. {
  160. 'in': _('Inch'),
  161. 'mm': _('Metric')
  162. }
  163. [str(self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower())]
  164. ],
  165. True
  166. )
  167. for option in obj.options:
  168. if option is 'name':
  169. continue
  170. self.addChild(options, [str(option), str(obj.options[option])], True)
  171. if obj.kind.lower() == 'gerber':
  172. temp_ap = dict()
  173. for ap in obj.apertures:
  174. temp_ap.clear()
  175. temp_ap = deepcopy(obj.apertures[ap])
  176. temp_ap.pop('geometry', None)
  177. solid_nr = 0
  178. follow_nr = 0
  179. clear_nr = 0
  180. if 'geometry' in obj.apertures[ap]:
  181. if obj.apertures[ap]['geometry']:
  182. font.setBold(True)
  183. for el in obj.apertures[ap]['geometry']:
  184. if 'solid' in el:
  185. solid_nr += 1
  186. if 'follow' in el:
  187. follow_nr += 1
  188. if 'clear' in el:
  189. clear_nr += 1
  190. else:
  191. font.setBold(False)
  192. temp_ap['Solid_Geo'] = '%s Polygons' % str(solid_nr)
  193. temp_ap['Follow_Geo'] = '%s LineStrings' % str(follow_nr)
  194. temp_ap['Clear_Geo'] = '%s Polygons' % str(clear_nr)
  195. apid = self.addParent(apertures, str(ap), expanded=False, color=QtGui.QColor("#000000"), font=font)
  196. for key in temp_ap:
  197. self.addChild(apid, [str(key), str(temp_ap[key])], True)
  198. elif obj.kind.lower() == 'excellon':
  199. for tool, value in obj.tools.items():
  200. self.addChild(tools, [str(tool), str(value['C'])], True)
  201. elif obj.kind.lower() == 'geometry':
  202. for tool, value in obj.tools.items():
  203. geo_tool = self.addParent(tools, str(tool), expanded=True, color=QtGui.QColor("#000000"), font=font)
  204. for k, v in value.items():
  205. if k == 'solid_geometry':
  206. printed_value = _('Present') if v else _('None')
  207. self.addChild(geo_tool, [str(k), printed_value], True)
  208. elif k == 'data':
  209. tool_data = self.addParent(geo_tool, str(k).capitalize(),
  210. color=QtGui.QColor("#000000"), font=font)
  211. for data_k, data_v in v.items():
  212. self.addChild(tool_data, [str(data_k), str(data_v)], True)
  213. else:
  214. self.addChild(geo_tool, [str(k), str(v)], True)
  215. elif obj.kind.lower() == 'cncjob':
  216. for tool, value in obj.cnc_tools.items():
  217. geo_tool = self.addParent(tools, str(tool), expanded=True, color=QtGui.QColor("#000000"), font=font)
  218. for k, v in value.items():
  219. if k == 'solid_geometry':
  220. printed_value = _('Present') if v else _('None')
  221. self.addChild(geo_tool, [str(k), printed_value], True)
  222. elif k == 'gcode':
  223. printed_value = _('Present') if v != '' else _('None')
  224. self.addChild(geo_tool, [str(k), printed_value], True)
  225. elif k == 'gcode_parsed':
  226. printed_value = _('Present') if v else _('None')
  227. self.addChild(geo_tool, [str(k), printed_value], True)
  228. elif k == 'data':
  229. tool_data = self.addParent(geo_tool, str(k).capitalize(),
  230. color=QtGui.QColor("#000000"), font=font)
  231. for data_k, data_v in v.items():
  232. self.addChild(tool_data, [str(data_k), str(data_v)], True)
  233. else:
  234. self.addChild(geo_tool, [str(k), str(v)], True)
  235. self.addChild(separator, [''])
  236. def addParent(self, parent, title, expanded=False, color=None, font=None):
  237. item = QtWidgets.QTreeWidgetItem(parent, [title])
  238. item.setChildIndicatorPolicy(QtWidgets.QTreeWidgetItem.ShowIndicator)
  239. item.setExpanded(expanded)
  240. if color is not None:
  241. # item.setTextColor(0, color) # PyQt4
  242. item.setForeground(0, QtGui.QBrush(color))
  243. if font is not None:
  244. item.setFont(0, font)
  245. return item
  246. def addChild(self, parent, title, column1=None):
  247. item = QtWidgets.QTreeWidgetItem(parent)
  248. item.setText(0, str(title[0]))
  249. if column1 is not None:
  250. item.setText(1, str(title[1]))
  251. # end of file