ToolProperties.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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 math
  13. import logging
  14. import gettext
  15. import FlatCAMTranslation as fcTranslate
  16. import builtins
  17. fcTranslate.apply_language('strings')
  18. if '_' not in builtins.__dict__:
  19. _ = gettext.gettext
  20. log = logging.getLogger('base')
  21. class Properties(FlatCAMTool):
  22. toolName = _("Properties")
  23. calculations_finished = QtCore.pyqtSignal(float, float, float, float, float, object)
  24. def __init__(self, app):
  25. FlatCAMTool.__init__(self, app)
  26. self.setSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Ignored)
  27. self.decimals = self.app.decimals
  28. # this way I can hide/show the frame
  29. self.properties_frame = QtWidgets.QFrame()
  30. self.properties_frame.setContentsMargins(0, 0, 0, 0)
  31. self.layout.addWidget(self.properties_frame)
  32. self.properties_box = QtWidgets.QVBoxLayout()
  33. self.properties_box.setContentsMargins(0, 0, 0, 0)
  34. self.properties_frame.setLayout(self.properties_box)
  35. # ## Title
  36. title_label = QtWidgets.QLabel("%s" % self.toolName)
  37. title_label.setStyleSheet("""
  38. QLabel
  39. {
  40. font-size: 16px;
  41. font-weight: bold;
  42. }
  43. """)
  44. self.properties_box.addWidget(title_label)
  45. # self.layout.setMargin(0) # PyQt4
  46. self.properties_box.setContentsMargins(0, 0, 0, 0) # PyQt5
  47. self.vlay = QtWidgets.QVBoxLayout()
  48. self.properties_box.addLayout(self.vlay)
  49. self.treeWidget = QtWidgets.QTreeWidget()
  50. self.treeWidget.setColumnCount(2)
  51. self.treeWidget.setHeaderHidden(True)
  52. self.treeWidget.header().setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
  53. self.treeWidget.setSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Expanding)
  54. self.vlay.addWidget(self.treeWidget)
  55. self.vlay.setStretch(0, 0)
  56. self.calculations_finished.connect(self.show_area_chull)
  57. def run(self, toggle=True):
  58. self.app.report_usage("ToolProperties()")
  59. if self.app.tool_tab_locked is True:
  60. return
  61. if toggle:
  62. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  63. if self.app.ui.splitter.sizes()[0] == 0:
  64. self.app.ui.splitter.setSizes([1, 1])
  65. else:
  66. try:
  67. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  68. # if tab is populated with the tool but it does not have the focus, focus on it
  69. if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
  70. # focus on Tool Tab
  71. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  72. else:
  73. self.app.ui.splitter.setSizes([0, 1])
  74. except AttributeError:
  75. pass
  76. else:
  77. if self.app.ui.splitter.sizes()[0] == 0:
  78. self.app.ui.splitter.setSizes([1, 1])
  79. FlatCAMTool.run(self)
  80. self.set_tool_ui()
  81. self.properties()
  82. def install(self, icon=None, separator=None, **kwargs):
  83. FlatCAMTool.install(self, icon, separator, shortcut='P', **kwargs)
  84. def set_tool_ui(self):
  85. # this reset the TreeWidget
  86. self.treeWidget.clear()
  87. self.properties_frame.show()
  88. def properties(self):
  89. obj_list = self.app.collection.get_selected()
  90. if not obj_list:
  91. self.app.inform.emit('[ERROR_NOTCL] %s' % _("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. # delete the selection shape, if any
  97. try:
  98. self.app.delete_selection_shape()
  99. except Exception as e:
  100. log.debug("ToolProperties.Properties.properties() --> %s" % str(e))
  101. # populate the properties items
  102. for obj in obj_list:
  103. self.addItems(obj)
  104. self.app.inform.emit('[success] %s' % _("Object Properties are displayed."))
  105. self.app.ui.notebook.setTabText(2, _("Properties Tool"))
  106. def addItems(self, obj):
  107. parent = self.treeWidget.invisibleRootItem()
  108. apertures = ''
  109. tools = ''
  110. drills = ''
  111. slots = ''
  112. others = ''
  113. font = QtGui.QFont()
  114. font.setBold(True)
  115. # main Items categories
  116. obj_type = self.addParent(parent, _('TYPE'), expanded=True, color=QtGui.QColor("#000000"), font=font)
  117. obj_name = self.addParent(parent, _('NAME'), expanded=True, color=QtGui.QColor("#000000"), font=font)
  118. dims = self.addParent(parent, _('Dimensions'), expanded=True, color=QtGui.QColor("#000000"), font=font)
  119. units = self.addParent(parent, _('Units'), expanded=True, color=QtGui.QColor("#000000"), font=font)
  120. options = self.addParent(parent, _('Options'), color=QtGui.QColor("#000000"), font=font)
  121. if obj.kind.lower() == 'gerber':
  122. apertures = self.addParent(parent, _('Apertures'), expanded=True, color=QtGui.QColor("#000000"), font=font)
  123. else:
  124. tools = self.addParent(parent, _('Tools'), expanded=True, color=QtGui.QColor("#000000"), font=font)
  125. if obj.kind.lower() == 'excellon':
  126. drills = self.addParent(parent, _('Drills'), expanded=True, color=QtGui.QColor("#000000"), font=font)
  127. slots = self.addParent(parent, _('Slots'), expanded=True, color=QtGui.QColor("#000000"), font=font)
  128. if obj.kind.lower() == 'cncjob':
  129. others = self.addParent(parent, _('Others'), expanded=True, color=QtGui.QColor("#000000"), font=font)
  130. separator = self.addParent(parent, '')
  131. self.addChild(obj_type, ['%s:' % _('Object Type'), ('%s' % (obj.kind.upper()))], True, font=font, font_items=1)
  132. try:
  133. self.addChild(obj_type,
  134. ['%s:' % _('Geo Type'),
  135. ('%s' % ({False: _("Single-Geo"), True: _("Multi-Geo")}[obj.multigeo]))],
  136. True)
  137. except Exception as e:
  138. log.debug("Properties.addItems() --> %s" % str(e))
  139. self.addChild(obj_name, [obj.options['name']])
  140. def job_thread(obj_prop):
  141. proc = self.app.proc_container.new(_("Calculating dimensions ... Please wait."))
  142. length = 0.0
  143. width = 0.0
  144. area = 0.0
  145. copper_area = 0.0
  146. geo = obj_prop.solid_geometry
  147. if geo:
  148. # calculate physical dimensions
  149. try:
  150. xmin, ymin, xmax, ymax = obj_prop.bounds()
  151. length = abs(xmax - xmin)
  152. width = abs(ymax - ymin)
  153. except Exception as e:
  154. log.debug("PropertiesTool.addItems() -> calculate dimensions --> %s" % str(e))
  155. # calculate box area
  156. if self.app.defaults['units'].lower() == 'mm':
  157. area = (length * width) / 100
  158. else:
  159. area = length * width
  160. if obj_prop.kind.lower() == 'gerber':
  161. # calculate copper area
  162. try:
  163. for geo_el in geo:
  164. copper_area += geo_el.area
  165. except TypeError:
  166. copper_area += geo.area
  167. copper_area /= 100
  168. else:
  169. xmin = []
  170. ymin = []
  171. xmax = []
  172. ymax = []
  173. if obj_prop.kind.lower() == 'cncjob':
  174. try:
  175. for tool_k in obj_prop.exc_cnc_tools:
  176. x0, y0, x1, y1 = cascaded_union(obj_prop.exc_cnc_tools[tool_k]['solid_geometry']).bounds
  177. xmin.append(x0)
  178. ymin.append(y0)
  179. xmax.append(x1)
  180. ymax.append(y1)
  181. except Exception as ee:
  182. log.debug("PropertiesTool.addItems() --> %s" % str(ee))
  183. try:
  184. for tool_k in obj_prop.cnc_tools:
  185. x0, y0, x1, y1 = cascaded_union(obj_prop.cnc_tools[tool_k]['solid_geometry']).bounds
  186. xmin.append(x0)
  187. ymin.append(y0)
  188. xmax.append(x1)
  189. ymax.append(y1)
  190. except Exception as ee:
  191. log.debug("PropertiesTool.addItems() --> %s" % str(ee))
  192. else:
  193. try:
  194. for tool_k in obj_prop.tools:
  195. x0, y0, x1, y1 = cascaded_union(obj_prop.tools[tool_k]['solid_geometry']).bounds
  196. xmin.append(x0)
  197. ymin.append(y0)
  198. xmax.append(x1)
  199. ymax.append(y1)
  200. except Exception as ee:
  201. log.debug("PropertiesTool.addItems() --> %s" % str(ee))
  202. try:
  203. xmin = min(xmin)
  204. ymin = min(ymin)
  205. xmax = max(xmax)
  206. ymax = max(ymax)
  207. length = abs(xmax - xmin)
  208. width = abs(ymax - ymin)
  209. # calculate box area
  210. if self.app.defaults['units'].lower() == 'mm':
  211. area = (length * width) / 100
  212. else:
  213. area = length * width
  214. if obj_prop.kind.lower() == 'gerber':
  215. # calculate copper area
  216. # create a complete solid_geometry from the tools
  217. geo_tools = list()
  218. for tool_k in obj_prop.tools:
  219. if 'solid_geometry' in obj_prop.tools[tool_k]:
  220. for geo_el in obj_prop.tools[tool_k]['solid_geometry']:
  221. geo_tools.append(geo_el)
  222. try:
  223. for geo_el in geo_tools:
  224. copper_area += geo_el.area
  225. except TypeError:
  226. copper_area += geo_tools.area
  227. copper_area /= 100
  228. except Exception as e:
  229. log.debug("Properties.addItems() --> %s" % str(e))
  230. area_chull = 0.0
  231. if obj_prop.kind.lower() != 'cncjob':
  232. # calculate and add convex hull area
  233. if geo:
  234. if isinstance(geo, MultiPolygon):
  235. env_obj = geo.convex_hull
  236. elif (isinstance(geo, MultiPolygon) and len(geo) == 1) or \
  237. (isinstance(geo, list) and len(geo) == 1) and isinstance(geo[0], Polygon):
  238. env_obj = cascaded_union(obj_prop.solid_geometry)
  239. env_obj = env_obj.convex_hull
  240. else:
  241. env_obj = cascaded_union(obj_prop.solid_geometry)
  242. env_obj = env_obj.convex_hull
  243. area_chull = env_obj.area
  244. else:
  245. try:
  246. area_chull = []
  247. for tool_k in obj_prop.tools:
  248. area_el = cascaded_union(obj_prop.tools[tool_k]['solid_geometry']).convex_hull
  249. area_chull.append(area_el.area)
  250. area_chull = max(area_chull)
  251. except Exception as e:
  252. area_chull = None
  253. log.debug("Properties.addItems() --> %s" % str(e))
  254. if self.app.defaults['units'].lower() == 'mm' and area_chull:
  255. area_chull = area_chull / 100
  256. if area_chull is None:
  257. area_chull = 0
  258. self.calculations_finished.emit(area, length, width, area_chull, copper_area, dims)
  259. self.app.worker_task.emit({'fcn': job_thread, 'params': [obj]})
  260. # Units items
  261. f_unit = {'in': _('Inch'), 'mm': _('Metric')}[str(self.app.defaults['units'].lower())]
  262. self.addChild(units, ['FlatCAM units:', f_unit], True)
  263. o_unit = {
  264. 'in': _('Inch'),
  265. 'mm': _('Metric'),
  266. 'inch': _('Inch'),
  267. 'metric': _('Metric')
  268. }[str(obj.units_found.lower())]
  269. self.addChild(units, ['Object units:', o_unit], True)
  270. # Options items
  271. for option in obj.options:
  272. if option is 'name':
  273. continue
  274. self.addChild(options, [str(option), str(obj.options[option])], True)
  275. # Items that depend on the object type
  276. if obj.kind.lower() == 'gerber':
  277. temp_ap = dict()
  278. for ap in obj.apertures:
  279. temp_ap.clear()
  280. temp_ap = deepcopy(obj.apertures[ap])
  281. temp_ap.pop('geometry', None)
  282. solid_nr = 0
  283. follow_nr = 0
  284. clear_nr = 0
  285. if 'geometry' in obj.apertures[ap]:
  286. if obj.apertures[ap]['geometry']:
  287. font.setBold(True)
  288. for el in obj.apertures[ap]['geometry']:
  289. if 'solid' in el:
  290. solid_nr += 1
  291. if 'follow' in el:
  292. follow_nr += 1
  293. if 'clear' in el:
  294. clear_nr += 1
  295. else:
  296. font.setBold(False)
  297. temp_ap['Solid_Geo'] = '%s Polygons' % str(solid_nr)
  298. temp_ap['Follow_Geo'] = '%s LineStrings' % str(follow_nr)
  299. temp_ap['Clear_Geo'] = '%s Polygons' % str(clear_nr)
  300. apid = self.addParent(apertures, str(ap), expanded=False, color=QtGui.QColor("#000000"), font=font)
  301. for key in temp_ap:
  302. self.addChild(apid, [str(key), str(temp_ap[key])], True)
  303. elif obj.kind.lower() == 'excellon':
  304. tot_drill_cnt = 0
  305. tot_slot_cnt = 0
  306. for tool, value in obj.tools.items():
  307. toolid = self.addParent(tools, str(tool), expanded=False, color=QtGui.QColor("#000000"), font=font)
  308. drill_cnt = 0 # variable to store the nr of drills per tool
  309. slot_cnt = 0 # variable to store the nr of slots per tool
  310. # Find no of drills for the current tool
  311. for drill in obj.drills:
  312. if drill['tool'] == tool:
  313. drill_cnt += 1
  314. tot_drill_cnt += drill_cnt
  315. # Find no of slots for the current tool
  316. for slot in obj.slots:
  317. if slot['tool'] == tool:
  318. slot_cnt += 1
  319. tot_slot_cnt += slot_cnt
  320. self.addChild(
  321. toolid,
  322. [
  323. _('Diameter'),
  324. '%.*f %s' % (self.decimals, value['C'], self.app.defaults['units'].lower())
  325. ],
  326. True
  327. )
  328. self.addChild(toolid, [_('Drills number'), str(drill_cnt)], True)
  329. self.addChild(toolid, [_('Slots number'), str(slot_cnt)], True)
  330. self.addChild(drills, [_('Drills total number:'), str(tot_drill_cnt)], True)
  331. self.addChild(slots, [_('Slots total number:'), str(tot_slot_cnt)], True)
  332. elif obj.kind.lower() == 'geometry':
  333. for tool, value in obj.tools.items():
  334. geo_tool = self.addParent(tools, str(tool), expanded=True, color=QtGui.QColor("#000000"), font=font)
  335. for k, v in value.items():
  336. if k == 'solid_geometry':
  337. printed_value = _('Present') if v else _('None')
  338. self.addChild(geo_tool, [str(k), printed_value], True)
  339. elif k == 'data':
  340. tool_data = self.addParent(geo_tool, str(k).capitalize(),
  341. color=QtGui.QColor("#000000"), font=font)
  342. for data_k, data_v in v.items():
  343. self.addChild(tool_data, [str(data_k), str(data_v)], True)
  344. else:
  345. self.addChild(geo_tool, [str(k), str(v)], True)
  346. elif obj.kind.lower() == 'cncjob':
  347. # for cncjob objects made from gerber or geometry
  348. for tool, value in obj.cnc_tools.items():
  349. geo_tool = self.addParent(tools, str(tool), expanded=True, color=QtGui.QColor("#000000"), font=font)
  350. for k, v in value.items():
  351. if k == 'solid_geometry':
  352. printed_value = _('Present') if v else _('None')
  353. self.addChild(geo_tool, [_("Solid Geometry"), printed_value], True)
  354. elif k == 'gcode':
  355. printed_value = _('Present') if v != '' else _('None')
  356. self.addChild(geo_tool, [_("GCode Text"), printed_value], True)
  357. elif k == 'gcode_parsed':
  358. printed_value = _('Present') if v else _('None')
  359. self.addChild(geo_tool, [_("GCode Geometry"), printed_value], True)
  360. elif k == 'data':
  361. tool_data = self.addParent(geo_tool, _("Data"), color=QtGui.QColor("#000000"), font=font)
  362. for data_k, data_v in v.items():
  363. self.addChild(tool_data, [str(data_k).capitalize(), str(data_v)], True)
  364. else:
  365. self.addChild(geo_tool, [str(k), str(v)], True)
  366. # for cncjob objects made from excellon
  367. for tool_dia, value in obj.exc_cnc_tools.items():
  368. exc_tool = self.addParent(
  369. tools, str(value['tool']), expanded=False, color=QtGui.QColor("#000000"), font=font
  370. )
  371. self.addChild(
  372. exc_tool,
  373. [
  374. _('Diameter'),
  375. '%.*f %s' % (self.decimals, tool_dia, self.app.defaults['units'].lower())
  376. ],
  377. True
  378. )
  379. for k, v in value.items():
  380. if k == 'solid_geometry':
  381. printed_value = _('Present') if v else _('None')
  382. self.addChild(exc_tool, [_("Solid Geometry"), printed_value], True)
  383. elif k == 'nr_drills':
  384. self.addChild(exc_tool, [_("Drills number"), str(v)], True)
  385. elif k == 'nr_slots':
  386. self.addChild(exc_tool, [_("Slots number"), str(v)], True)
  387. else:
  388. pass
  389. self.addChild(
  390. exc_tool,
  391. [
  392. _("Depth of Cut"),
  393. '%.*f %s' % (
  394. self.decimals,
  395. (obj.z_cut - abs(obj.tool_offset[tool_dia])),
  396. self.app.defaults['units'].lower()
  397. )
  398. ],
  399. True
  400. )
  401. self.addChild(
  402. exc_tool,
  403. [
  404. _("Clearance Height"),
  405. '%.*f %s' % (
  406. self.decimals,
  407. obj.z_move,
  408. self.app.defaults['units'].lower()
  409. )
  410. ],
  411. True
  412. )
  413. self.addChild(
  414. exc_tool,
  415. [
  416. _("Feedrate"),
  417. '%.*f %s/min' % (
  418. self.decimals,
  419. obj.feedrate,
  420. self.app.defaults['units'].lower()
  421. )
  422. ],
  423. True
  424. )
  425. r_time = obj.routing_time
  426. if r_time > 1:
  427. units_lbl = 'min'
  428. else:
  429. r_time *= 60
  430. units_lbl = 'sec'
  431. r_time = math.ceil(float(r_time))
  432. self.addChild(
  433. others,
  434. [
  435. '%s:' % _('Routing time'),
  436. '%.*f %s' % (self.decimals, r_time, units_lbl)],
  437. True
  438. )
  439. self.addChild(
  440. others,
  441. [
  442. '%s:' % _('Travelled distance'),
  443. '%.*f %s' % (self.decimals, obj.travel_distance, self.app.defaults['units'].lower())
  444. ],
  445. True
  446. )
  447. self.addChild(separator, [''])
  448. def addParent(self, parent, title, expanded=False, color=None, font=None):
  449. item = QtWidgets.QTreeWidgetItem(parent, [title])
  450. item.setChildIndicatorPolicy(QtWidgets.QTreeWidgetItem.ShowIndicator)
  451. item.setExpanded(expanded)
  452. if color is not None:
  453. # item.setTextColor(0, color) # PyQt4
  454. item.setForeground(0, QtGui.QBrush(color))
  455. if font is not None:
  456. item.setFont(0, font)
  457. return item
  458. def addChild(self, parent, title, column1=None, font=None, font_items=None):
  459. item = QtWidgets.QTreeWidgetItem(parent)
  460. item.setText(0, str(title[0]))
  461. if column1 is not None:
  462. item.setText(1, str(title[1]))
  463. if font and font_items:
  464. try:
  465. for fi in font_items:
  466. item.setFont(fi, font)
  467. except TypeError:
  468. item.setFont(font_items, font)
  469. def show_area_chull(self, area, length, width, chull_area, copper_area, location):
  470. # add dimensions
  471. self.addChild(
  472. location,
  473. ['%s:' % _('Length'), '%.*f %s' % (self.decimals, length, self.app.defaults['units'].lower())],
  474. True
  475. )
  476. self.addChild(
  477. location,
  478. ['%s:' % _('Width'), '%.*f %s' % (self.decimals, width, self.app.defaults['units'].lower())],
  479. True
  480. )
  481. # add box area
  482. if self.app.defaults['units'].lower() == 'mm':
  483. self.addChild(location, ['%s:' % _('Box Area'), '%.*f %s' % (self.decimals, area, 'cm2')], True)
  484. self.addChild(
  485. location,
  486. ['%s:' % _('Convex_Hull Area'), '%.*f %s' % (self.decimals, chull_area, 'cm2')],
  487. True
  488. )
  489. else:
  490. self.addChild(location, ['%s:' % _('Box Area'), '%.*f %s' % (self.decimals, area, 'in2')], True)
  491. self.addChild(
  492. location,
  493. ['%s:' % _('Convex_Hull Area'), '%.*f %s' % (self.decimals, chull_area, 'in2')],
  494. True
  495. )
  496. # add copper area
  497. if self.app.defaults['units'].lower() == 'mm':
  498. self.addChild(location, ['%s:' % _('Copper Area'), '%.*f %s' % (self.decimals, copper_area, 'cm2')], True)
  499. else:
  500. self.addChild(location, ['%s:' % _('Copper Area'), '%.*f %s' % (self.decimals, copper_area, 'in2')], True)
  501. # end of file