ToolProperties.py 25 KB

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