ToolProperties.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. self.app.ui.splitter.setSizes([0, 1])
  63. except AttributeError:
  64. pass
  65. else:
  66. if self.app.ui.splitter.sizes()[0] == 0:
  67. self.app.ui.splitter.setSizes([1, 1])
  68. FlatCAMTool.run(self)
  69. self.set_tool_ui()
  70. self.properties()
  71. def install(self, icon=None, separator=None, **kwargs):
  72. FlatCAMTool.install(self, icon, separator, shortcut='P', **kwargs)
  73. def set_tool_ui(self):
  74. # this reset the TreeWidget
  75. self.treeWidget.clear()
  76. self.properties_frame.show()
  77. def properties(self):
  78. obj_list = self.app.collection.get_selected()
  79. if not obj_list:
  80. self.app.inform.emit(_("[ERROR_NOTCL] Properties Tool was not displayed. No object selected."))
  81. self.app.ui.notebook.setTabText(2, _("Tools"))
  82. self.properties_frame.hide()
  83. self.app.ui.notebook.setCurrentWidget(self.app.ui.project_tab)
  84. return
  85. for obj in obj_list:
  86. self.addItems(obj)
  87. self.app.inform.emit(_("[success] Object Properties are displayed."))
  88. self.app.ui.notebook.setTabText(2, _("Properties Tool"))
  89. def addItems(self, obj):
  90. parent = self.treeWidget.invisibleRootItem()
  91. font = QtGui.QFont()
  92. font.setBold(True)
  93. obj_type = self.addParent(parent, 'TYPE', expanded=True, color=QtGui.QColor("#000000"), font=font)
  94. obj_name = self.addParent(parent, 'NAME', expanded=True, color=QtGui.QColor("#000000"), font=font)
  95. dims = self.addParent(parent, 'Dimensions', expanded=True, color=QtGui.QColor("#000000"), font=font)
  96. units = self.addParent(parent, 'Units', expanded=True, color=QtGui.QColor("#000000"), font=font)
  97. options = self.addParent(parent, 'Options', color=QtGui.QColor("#000000"), font=font)
  98. if obj.kind.lower() == 'gerber':
  99. apertures = self.addParent(parent, 'Apertures', expanded=True, color=QtGui.QColor("#000000"), font=font)
  100. else:
  101. tools = self.addParent(parent, 'Tools', expanded=True, color=QtGui.QColor("#000000"), font=font)
  102. separator = self.addParent(parent, '')
  103. self.addChild(obj_type, ['Object Type:', ('%s' % (obj.kind.capitalize()))], True)
  104. try:
  105. self.addChild(obj_type,
  106. ['Geo Type:',
  107. ('%s' % ({False: "Single-Geo", True: "Multi-Geo"}[obj.multigeo]))
  108. ],
  109. True
  110. )
  111. except Exception as e:
  112. log.debug("Properties.addItems() --> %s" % str(e))
  113. self.addChild(obj_name, [obj.options['name']])
  114. # calculate physical dimensions
  115. try:
  116. xmin, ymin, xmax, ymax = obj.bounds()
  117. except Exception as e:
  118. log.debug("PropertiesTool.addItems() --> %s" % str(e))
  119. return
  120. length = abs(xmax - xmin)
  121. width = abs(ymax - ymin)
  122. self.addChild(dims, ['Length:', '%.4f %s' % (
  123. length, self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower())], True)
  124. self.addChild(dims, ['Width:', '%.4f %s' % (
  125. width, self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower())], True)
  126. if self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower() == 'mm':
  127. area = (length * width) / 100
  128. self.addChild(dims, ['Box Area:', '%.4f %s' % (area, 'cm2')], True)
  129. else:
  130. area = length * width
  131. self.addChild(dims, ['Box Area:', '%.4f %s' % (area, 'in2')], True)
  132. self.addChild(units,
  133. ['FlatCAM units:',
  134. {
  135. 'in': 'Inch',
  136. 'mm': 'Metric'
  137. }
  138. [str(self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower())]
  139. ],
  140. True
  141. )
  142. for option in obj.options:
  143. if option is 'name':
  144. continue
  145. self.addChild(options, [str(option), str(obj.options[option])], True)
  146. if obj.kind.lower() == 'gerber':
  147. temp_ap = {}
  148. for ap in obj.apertures:
  149. temp_ap.clear()
  150. temp_ap = deepcopy(obj.apertures[ap])
  151. temp_ap.pop('geometry', None)
  152. if obj.apertures[ap]['geometry']:
  153. solid_nr = 0
  154. follow_nr = 0
  155. clear_nr = 0
  156. for el in obj.apertures[ap]['geometry']:
  157. if 'solid' in el:
  158. solid_nr += 1
  159. if 'follow' in el:
  160. follow_nr += 1
  161. if 'clear' in el:
  162. clear_nr += 1
  163. temp_ap['Solid_Geo'] = '%s Polygons' % str(solid_nr)
  164. temp_ap['Follow_Geo'] = '%s LineStrings' % str(follow_nr)
  165. temp_ap['Clear_Geo'] = '%s Polygons' % str(clear_nr)
  166. apid = self.addParent(apertures, str(ap), expanded=False, color=QtGui.QColor("#000000"), font=font)
  167. for key in temp_ap:
  168. self.addChild(apid, [str(key), str(temp_ap[key])], True)
  169. elif obj.kind.lower() == 'excellon':
  170. for tool, value in obj.tools.items():
  171. self.addChild(tools, [str(tool), str(value['C'])], True)
  172. elif obj.kind.lower() == 'geometry':
  173. for tool, value in obj.tools.items():
  174. geo_tool = self.addParent(tools, str(tool), expanded=True, color=QtGui.QColor("#000000"), font=font)
  175. for k, v in value.items():
  176. if k == 'solid_geometry':
  177. printed_value = 'Present' if v else 'None'
  178. self.addChild(geo_tool, [str(k), printed_value], True)
  179. elif k == 'data':
  180. tool_data = self.addParent(geo_tool, str(k).capitalize(),
  181. color=QtGui.QColor("#000000"), font=font)
  182. for data_k, data_v in v.items():
  183. self.addChild(tool_data, [str(data_k), str(data_v)], True)
  184. else:
  185. self.addChild(geo_tool, [str(k), str(v)], True)
  186. elif obj.kind.lower() == 'cncjob':
  187. for tool, value in obj.cnc_tools.items():
  188. geo_tool = self.addParent(tools, str(tool), expanded=True, color=QtGui.QColor("#000000"), font=font)
  189. for k, v in value.items():
  190. if k == 'solid_geometry':
  191. printed_value = 'Present' if v else 'None'
  192. self.addChild(geo_tool, [str(k), printed_value], True)
  193. elif k == 'gcode':
  194. printed_value = 'Present' if v != '' else 'None'
  195. self.addChild(geo_tool, [str(k), printed_value], True)
  196. elif k == 'gcode_parsed':
  197. printed_value = 'Present' if v else 'None'
  198. self.addChild(geo_tool, [str(k), printed_value], True)
  199. elif k == 'data':
  200. tool_data = self.addParent(geo_tool, str(k).capitalize(),
  201. color=QtGui.QColor("#000000"), font=font)
  202. for data_k, data_v in v.items():
  203. self.addChild(tool_data, [str(data_k), str(data_v)], True)
  204. else:
  205. self.addChild(geo_tool, [str(k), str(v)], True)
  206. self.addChild(separator, [''])
  207. def addParent(self, parent, title, expanded=False, color=None, font=None):
  208. item = QtWidgets.QTreeWidgetItem(parent, [title])
  209. item.setChildIndicatorPolicy(QtWidgets.QTreeWidgetItem.ShowIndicator)
  210. item.setExpanded(expanded)
  211. if color is not None:
  212. # item.setTextColor(0, color) # PyQt4
  213. item.setForeground(0, QtGui.QBrush(color))
  214. if font is not None:
  215. item.setFont(0, font)
  216. return item
  217. def addChild(self, parent, title, column1=None):
  218. item = QtWidgets.QTreeWidgetItem(parent)
  219. item.setText(0, str(title[0]))
  220. if column1 is not None:
  221. item.setText(1, str(title[1]))
  222. # end of file