ToolProperties.py 24 KB

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