ToolCopperThieving.py 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # File Author: Marius Adrian Stanciu (c) #
  4. # Date: 10/25/2019 #
  5. # MIT Licence #
  6. # ##########################################################
  7. from PyQt5 import QtWidgets, QtCore
  8. import FlatCAMApp
  9. from FlatCAMTool import FlatCAMTool
  10. from flatcamGUI.GUIElements import FCDoubleSpinner, RadioSet
  11. from FlatCAMObj import FlatCAMGerber, FlatCAMGeometry, FlatCAMExcellon
  12. import shapely.geometry.base as base
  13. from shapely.ops import cascaded_union, unary_union
  14. from shapely.geometry import Polygon, MultiPolygon, Point
  15. from shapely.geometry import box as box
  16. import logging
  17. from copy import deepcopy
  18. import numpy as np
  19. from collections import Iterable
  20. import gettext
  21. import FlatCAMTranslation as fcTranslate
  22. import builtins
  23. fcTranslate.apply_language('strings')
  24. if '_' not in builtins.__dict__:
  25. _ = gettext.gettext
  26. log = logging.getLogger('base')
  27. class ToolCopperThieving(FlatCAMTool):
  28. toolName = _("Copper Thieving Tool")
  29. def __init__(self, app):
  30. FlatCAMTool.__init__(self, app)
  31. self.app = app
  32. self.canvas = self.app.plotcanvas
  33. self.decimals = 4
  34. self.units = ''
  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.layout.addWidget(title_label)
  45. self.layout.addWidget(QtWidgets.QLabel(''))
  46. # ## Grid Layout
  47. i_grid_lay = QtWidgets.QGridLayout()
  48. self.layout.addLayout(i_grid_lay)
  49. i_grid_lay.setColumnStretch(0, 0)
  50. i_grid_lay.setColumnStretch(1, 1)
  51. self.grb_object_combo = QtWidgets.QComboBox()
  52. self.grb_object_combo.setModel(self.app.collection)
  53. self.grb_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  54. self.grb_object_combo.setCurrentIndex(1)
  55. self.grbobj_label = QtWidgets.QLabel("<b>%s:</b>" % _("GERBER"))
  56. self.grbobj_label.setToolTip(
  57. _("Gerber Object to which will be added a copper thieving.")
  58. )
  59. i_grid_lay.addWidget(self.grbobj_label, 0, 0)
  60. i_grid_lay.addWidget(self.grb_object_combo, 0, 1, 1, 2)
  61. i_grid_lay.addWidget(QtWidgets.QLabel(''), 1, 0)
  62. # ## Grid Layout
  63. grid_lay = QtWidgets.QGridLayout()
  64. self.layout.addLayout(grid_lay)
  65. grid_lay.setColumnStretch(0, 0)
  66. grid_lay.setColumnStretch(1, 1)
  67. self.copper_fill_label = QtWidgets.QLabel('<b>%s</b>' % _('Parameters'))
  68. self.copper_fill_label.setToolTip(
  69. _("Parameters used for this tool.")
  70. )
  71. grid_lay.addWidget(self.copper_fill_label, 0, 0, 1, 2)
  72. # CLEARANCE #
  73. self.clearance_label = QtWidgets.QLabel('%s:' % _("Clearance"))
  74. self.clearance_label.setToolTip(
  75. _("This set the distance between the copper thieving components\n"
  76. "(the polygon fill may be split in multiple polygons)\n"
  77. "and the copper traces in the Gerber file.")
  78. )
  79. self.clearance_entry = FCDoubleSpinner()
  80. self.clearance_entry.set_range(0.00001, 9999.9999)
  81. self.clearance_entry.set_precision(self.decimals)
  82. self.clearance_entry.setSingleStep(0.1)
  83. grid_lay.addWidget(self.clearance_label, 1, 0)
  84. grid_lay.addWidget(self.clearance_entry, 1, 1)
  85. # MARGIN #
  86. self.margin_label = QtWidgets.QLabel('%s:' % _("Margin"))
  87. self.margin_label.setToolTip(
  88. _("Bounding box margin.")
  89. )
  90. self.margin_entry = FCDoubleSpinner()
  91. self.margin_entry.set_range(0.0, 9999.9999)
  92. self.margin_entry.set_precision(self.decimals)
  93. self.margin_entry.setSingleStep(0.1)
  94. grid_lay.addWidget(self.margin_label, 2, 0)
  95. grid_lay.addWidget(self.margin_entry, 2, 1)
  96. # Reference #
  97. self.reference_radio = RadioSet([
  98. {'label': _('Itself'), 'value': 'itself'},
  99. {"label": _("Area Selection"), "value": "area"},
  100. {'label': _("Reference Object"), 'value': 'box'}
  101. ], orientation='vertical', stretch=False)
  102. self.reference_label = QtWidgets.QLabel(_("Reference:"))
  103. self.reference_label.setToolTip(
  104. _("- 'Itself' - the copper thieving extent is based on the object that is copper cleared.\n "
  105. "- 'Area Selection' - left mouse click to start selection of the area to be filled.\n"
  106. "- 'Reference Object' - will do copper thieving within the area specified by another object.")
  107. )
  108. grid_lay.addWidget(self.reference_label, 3, 0)
  109. grid_lay.addWidget(self.reference_radio, 3, 1)
  110. self.box_combo_type_label = QtWidgets.QLabel('%s:' % _("Ref. Type"))
  111. self.box_combo_type_label.setToolTip(
  112. _("The type of FlatCAM object to be used as copper thieving reference.\n"
  113. "It can be Gerber, Excellon or Geometry.")
  114. )
  115. self.box_combo_type = QtWidgets.QComboBox()
  116. self.box_combo_type.addItem(_("Reference Gerber"))
  117. self.box_combo_type.addItem(_("Reference Excellon"))
  118. self.box_combo_type.addItem(_("Reference Geometry"))
  119. grid_lay.addWidget(self.box_combo_type_label, 4, 0)
  120. grid_lay.addWidget(self.box_combo_type, 4, 1)
  121. self.box_combo_label = QtWidgets.QLabel('%s:' % _("Ref. Object"))
  122. self.box_combo_label.setToolTip(
  123. _("The FlatCAM object to be used as non copper clearing reference.")
  124. )
  125. self.box_combo = QtWidgets.QComboBox()
  126. self.box_combo.setModel(self.app.collection)
  127. self.box_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  128. self.box_combo.setCurrentIndex(1)
  129. grid_lay.addWidget(self.box_combo_label, 5, 0)
  130. grid_lay.addWidget(self.box_combo, 5, 1)
  131. self.box_combo.hide()
  132. self.box_combo_label.hide()
  133. self.box_combo_type.hide()
  134. self.box_combo_type_label.hide()
  135. # Bounding Box Type #
  136. self.bbox_type_radio = RadioSet([
  137. {'label': _('Rectangular'), 'value': 'rect'},
  138. {"label": _("Minimal"), "value": "min"}
  139. ], stretch=False)
  140. self.bbox_type_label = QtWidgets.QLabel(_("Box Type:"))
  141. self.bbox_type_label.setToolTip(
  142. _("- 'Rectangular' - the bounding box will be of rectangular shape.\n "
  143. "- 'Minimal' - the bounding box will be the convex hull shape.")
  144. )
  145. grid_lay.addWidget(self.bbox_type_label, 6, 0)
  146. grid_lay.addWidget(self.bbox_type_radio, 6, 1)
  147. self.bbox_type_label.hide()
  148. self.bbox_type_radio.hide()
  149. separator_line = QtWidgets.QFrame()
  150. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  151. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  152. grid_lay.addWidget(separator_line, 7, 0, 1, 2)
  153. # Fill Type
  154. self.fill_type_radio = RadioSet([
  155. {'label': _('Solid'), 'value': 'solid'},
  156. {"label": _("Dots Grid"), "value": "dot"},
  157. {"label": _("Squares Grid"), "value": "square"},
  158. {"label": _("Lines Grid"), "value": "line"}
  159. ], orientation='vertical', stretch=False)
  160. self.fill_type_label = QtWidgets.QLabel(_("Fill Type:"))
  161. self.fill_type_label.setToolTip(
  162. _("- 'Solid' - copper thieving will be a solid polygon.\n "
  163. "- 'Dots Grid' - the empty area will be filled with a pattern of dots.\n"
  164. "- 'Squares Grid' - the empty area will be filled with a pattern of squares.\n"
  165. "- 'Lines Grid' - the empty area will be filled with a pattern of lines.")
  166. )
  167. grid_lay.addWidget(self.fill_type_label, 8, 0)
  168. grid_lay.addWidget(self.fill_type_radio, 8, 1)
  169. # DOTS FRAME
  170. self.dots_frame = QtWidgets.QFrame()
  171. self.dots_frame.setContentsMargins(0, 0, 0, 0)
  172. self.layout.addWidget(self.dots_frame)
  173. dots_grid = QtWidgets.QGridLayout()
  174. dots_grid.setColumnStretch(0, 0)
  175. dots_grid.setColumnStretch(1, 1)
  176. dots_grid.setContentsMargins(0, 0, 0, 0)
  177. self.dots_frame.setLayout(dots_grid)
  178. self.dots_frame.hide()
  179. self.dots_label = QtWidgets.QLabel('<b>%s</b>:' % _("Dots Grid Parameters"))
  180. dots_grid.addWidget(self.dots_label, 0, 0, 1, 2)
  181. # Dot diameter #
  182. self.dotdia_label = QtWidgets.QLabel('%s:' % _("Dia"))
  183. self.dotdia_label.setToolTip(
  184. _("Dot diameter in Dots Grid.")
  185. )
  186. self.dot_dia_entry = FCDoubleSpinner()
  187. self.dot_dia_entry.set_range(0.0, 9999.9999)
  188. self.dot_dia_entry.set_precision(self.decimals)
  189. self.dot_dia_entry.setSingleStep(0.1)
  190. dots_grid.addWidget(self.dotdia_label, 1, 0)
  191. dots_grid.addWidget(self.dot_dia_entry, 1, 1)
  192. # Dot spacing #
  193. self.dotspacing_label = QtWidgets.QLabel('%s:' % _("Spacing"))
  194. self.dotspacing_label.setToolTip(
  195. _("Distance between each two dots in Dots Grid.")
  196. )
  197. self.dot_spacing_entry = FCDoubleSpinner()
  198. self.dot_spacing_entry.set_range(0.0, 9999.9999)
  199. self.dot_spacing_entry.set_precision(self.decimals)
  200. self.dot_spacing_entry.setSingleStep(0.1)
  201. dots_grid.addWidget(self.dotspacing_label, 2, 0)
  202. dots_grid.addWidget(self.dot_spacing_entry, 2, 1)
  203. # SQUARES FRAME
  204. self.squares_frame = QtWidgets.QFrame()
  205. self.squares_frame.setContentsMargins(0, 0, 0, 0)
  206. self.layout.addWidget(self.squares_frame)
  207. squares_grid = QtWidgets.QGridLayout()
  208. squares_grid.setColumnStretch(0, 0)
  209. squares_grid.setColumnStretch(1, 1)
  210. squares_grid.setContentsMargins(0, 0, 0, 0)
  211. self.squares_frame.setLayout(squares_grid)
  212. self.squares_frame.hide()
  213. self.squares_label = QtWidgets.QLabel('<b>%s</b>:' % _("Squares Grid Parameters"))
  214. squares_grid.addWidget(self.squares_label, 0, 0, 1, 2)
  215. # Square Size #
  216. self.square_size_label = QtWidgets.QLabel('%s:' % _("Size"))
  217. self.square_size_label.setToolTip(
  218. _("Square side size in Squares Grid.")
  219. )
  220. self.square_size_entry = FCDoubleSpinner()
  221. self.square_size_entry.set_range(0.0, 9999.9999)
  222. self.square_size_entry.set_precision(self.decimals)
  223. self.square_size_entry.setSingleStep(0.1)
  224. squares_grid.addWidget(self.square_size_label, 1, 0)
  225. squares_grid.addWidget(self.square_size_entry, 1, 1)
  226. # Squares spacing #
  227. self.squares_spacing_label = QtWidgets.QLabel('%s:' % _("Spacing"))
  228. self.squares_spacing_label.setToolTip(
  229. _("Distance between each two squares in Squares Grid.")
  230. )
  231. self.squares_spacing_entry = FCDoubleSpinner()
  232. self.squares_spacing_entry.set_range(0.0, 9999.9999)
  233. self.squares_spacing_entry.set_precision(self.decimals)
  234. self.squares_spacing_entry.setSingleStep(0.1)
  235. squares_grid.addWidget(self.squares_spacing_label, 2, 0)
  236. squares_grid.addWidget(self.squares_spacing_entry, 2, 1)
  237. # LINES FRAME
  238. self.lines_frame = QtWidgets.QFrame()
  239. self.lines_frame.setContentsMargins(0, 0, 0, 0)
  240. self.layout.addWidget(self.lines_frame)
  241. lines_grid = QtWidgets.QGridLayout()
  242. lines_grid.setColumnStretch(0, 0)
  243. lines_grid.setColumnStretch(1, 1)
  244. lines_grid.setContentsMargins(0, 0, 0, 0)
  245. self.lines_frame.setLayout(lines_grid)
  246. self.lines_frame.hide()
  247. self.lines_label = QtWidgets.QLabel('<b>%s</b>:' % _("Lines Grid Parameters"))
  248. lines_grid.addWidget(self.lines_label, 0, 0, 1, 2)
  249. # Square Size #
  250. self.line_size_label = QtWidgets.QLabel('%s:' % _("Size"))
  251. self.line_size_label.setToolTip(
  252. _("Line thickness size in Lines Grid.")
  253. )
  254. self.line_size_entry = FCDoubleSpinner()
  255. self.line_size_entry.set_range(0.0, 9999.9999)
  256. self.line_size_entry.set_precision(self.decimals)
  257. self.line_size_entry.setSingleStep(0.1)
  258. lines_grid.addWidget(self.line_size_label, 1, 0)
  259. lines_grid.addWidget(self.line_size_entry, 1, 1)
  260. # Lines spacing #
  261. self.lines_spacing_label = QtWidgets.QLabel('%s:' % _("Spacing"))
  262. self.lines_spacing_label.setToolTip(
  263. _("Distance between each two lines in Lines Grid.")
  264. )
  265. self.lines_spacing_entry = FCDoubleSpinner()
  266. self.lines_spacing_entry.set_range(0.0, 9999.9999)
  267. self.lines_spacing_entry.set_precision(self.decimals)
  268. self.lines_spacing_entry.setSingleStep(0.1)
  269. lines_grid.addWidget(self.lines_spacing_label, 2, 0)
  270. lines_grid.addWidget(self.lines_spacing_entry, 2, 1)
  271. # ## Insert Copper Thieving
  272. self.fill_button = QtWidgets.QPushButton(_("Insert Copper thieving"))
  273. self.fill_button.setToolTip(
  274. _("Will add a polygon (may be split in multiple parts)\n"
  275. "that will surround the actual Gerber traces at a certain distance.")
  276. )
  277. self.layout.addWidget(self.fill_button)
  278. self.layout.addStretch()
  279. # Objects involved in Copper thieving
  280. self.grb_object = None
  281. self.ref_obj = None
  282. self.sel_rect = list()
  283. # Events ID
  284. self.mr = None
  285. self.mm = None
  286. # Mouse cursor positions
  287. self.mouse_is_dragging = False
  288. self.cursor_pos = (0, 0)
  289. self.first_click = False
  290. self.area_method = False
  291. # Tool properties
  292. self.clearance_val = None
  293. self.margin_val = None
  294. self.geo_steps_per_circle = 128
  295. # SIGNALS
  296. self.fill_button.clicked.connect(self.execute)
  297. self.box_combo_type.currentIndexChanged.connect(self.on_combo_box_type)
  298. self.reference_radio.group_toggle_fn = self.on_toggle_reference
  299. self.fill_type_radio.activated_custom.connect(self.on_thieving_type)
  300. def run(self, toggle=True):
  301. self.app.report_usage("ToolCopperThieving()")
  302. if toggle:
  303. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  304. if self.app.ui.splitter.sizes()[0] == 0:
  305. self.app.ui.splitter.setSizes([1, 1])
  306. else:
  307. try:
  308. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  309. # if tab is populated with the tool but it does not have the focus, focus on it
  310. if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
  311. # focus on Tool Tab
  312. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  313. else:
  314. self.app.ui.splitter.setSizes([0, 1])
  315. except AttributeError:
  316. pass
  317. else:
  318. if self.app.ui.splitter.sizes()[0] == 0:
  319. self.app.ui.splitter.setSizes([1, 1])
  320. FlatCAMTool.run(self)
  321. self.set_tool_ui()
  322. self.app.ui.notebook.setTabText(2, _("Copper Thieving Tool"))
  323. def install(self, icon=None, separator=None, **kwargs):
  324. FlatCAMTool.install(self, icon, separator, shortcut='ALT+F', **kwargs)
  325. def set_tool_ui(self):
  326. self.units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value()
  327. self.clearance_entry.set_value(float(self.app.defaults["tools_copper_thieving_clearance"]))
  328. self.margin_entry.set_value(float(self.app.defaults["tools_copper_thieving_margin"]))
  329. self.reference_radio.set_value(self.app.defaults["tools_copper_thieving_reference"])
  330. self.bbox_type_radio.set_value(self.app.defaults["tools_copper_thieving_box_type"])
  331. self.fill_type_radio.set_value(self.app.defaults["tools_copper_thieving_fill_type"])
  332. self.geo_steps_per_circle = int(self.app.defaults["tools_copper_thieving_circle_steps"])
  333. self.dot_dia_entry.set_value(self.app.defaults["tools_copper_thieving_dots_dia"])
  334. self.dot_spacing_entry.set_value(self.app.defaults["tools_copper_thieving_dots_spacing"])
  335. self.square_size_entry.set_value(self.app.defaults["tools_copper_thieving_squares_size"])
  336. self.squares_spacing_entry.set_value(self.app.defaults["tools_copper_thieving_squares_spacing"])
  337. self.line_size_entry.set_value(self.app.defaults["tools_copper_thieving_lines_size"])
  338. self.lines_spacing_entry.set_value(self.app.defaults["tools_copper_thieving_lines_spacing"])
  339. self.area_method = False
  340. def on_combo_box_type(self):
  341. obj_type = self.box_combo_type.currentIndex()
  342. self.box_combo.setRootModelIndex(self.app.collection.index(obj_type, 0, QtCore.QModelIndex()))
  343. self.box_combo.setCurrentIndex(0)
  344. def on_toggle_reference(self):
  345. if self.reference_radio.get_value() == "itself" or self.reference_radio.get_value() == "area":
  346. self.box_combo.hide()
  347. self.box_combo_label.hide()
  348. self.box_combo_type.hide()
  349. self.box_combo_type_label.hide()
  350. else:
  351. self.box_combo.show()
  352. self.box_combo_label.show()
  353. self.box_combo_type.show()
  354. self.box_combo_type_label.show()
  355. if self.reference_radio.get_value() == "itself":
  356. self.bbox_type_label.show()
  357. self.bbox_type_radio.show()
  358. else:
  359. self.bbox_type_label.hide()
  360. self.bbox_type_radio.hide()
  361. def on_thieving_type(self, choice):
  362. if choice == 'solid':
  363. self.dots_frame.hide()
  364. self.squares_frame.hide()
  365. self.lines_frame.hide()
  366. elif choice == 'dot':
  367. self.dots_frame.show()
  368. self.squares_frame.hide()
  369. self.lines_frame.hide()
  370. elif choice == 'square':
  371. self.dots_frame.hide()
  372. self.squares_frame.show()
  373. self.lines_frame.hide()
  374. else:
  375. self.dots_frame.hide()
  376. self.squares_frame.hide()
  377. self.lines_frame.show()
  378. def execute(self):
  379. self.app.call_source = "copper_thieving_tool"
  380. self.clearance_val = self.clearance_entry.get_value()
  381. self.margin_val = self.margin_entry.get_value()
  382. reference_method = self.reference_radio.get_value()
  383. # get the Gerber object on which the Copper thieving will be inserted
  384. selection_index = self.grb_object_combo.currentIndex()
  385. model_index = self.app.collection.index(selection_index, 0, self.grb_object_combo.rootModelIndex())
  386. try:
  387. self.grb_object = model_index.internalPointer().obj
  388. except Exception as e:
  389. log.debug("ToolCopperThieving.execute() --> %s" % str(e))
  390. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Gerber object loaded ..."))
  391. return 'fail'
  392. if reference_method == 'itself':
  393. bound_obj_name = self.grb_object_combo.currentText()
  394. # Get reference object.
  395. try:
  396. self.ref_obj = self.app.collection.get_by_name(bound_obj_name)
  397. except Exception as e:
  398. self.app.inform.emit('[ERROR_NOTCL] %s: %s' % (_("Could not retrieve object"), str(e)))
  399. return "Could not retrieve object: %s" % self.obj_name
  400. self.on_copper_thieving(
  401. thieving_obj=self.grb_object,
  402. c_val=self.clearance_val,
  403. margin=self.margin_val
  404. )
  405. elif reference_method == 'area':
  406. self.app.inform.emit('[WARNING_NOTCL] %s' % _("Click the start point of the area."))
  407. self.area_method = True
  408. if self.app.is_legacy is False:
  409. self.app.plotcanvas.graph_event_disconnect('mouse_press', self.app.on_mouse_click_over_plot)
  410. self.app.plotcanvas.graph_event_disconnect('mouse_move', self.app.on_mouse_move_over_plot)
  411. self.app.plotcanvas.graph_event_disconnect('mouse_release', self.app.on_mouse_click_release_over_plot)
  412. else:
  413. self.app.plotcanvas.graph_event_disconnect(self.app.mp)
  414. self.app.plotcanvas.graph_event_disconnect(self.app.mm)
  415. self.app.plotcanvas.graph_event_disconnect(self.app.mr)
  416. self.mr = self.app.plotcanvas.graph_event_connect('mouse_release', self.on_mouse_release)
  417. self.mm = self.app.plotcanvas.graph_event_connect('mouse_move', self.on_mouse_move)
  418. elif reference_method == 'box':
  419. bound_obj_name = self.box_combo.currentText()
  420. # Get reference object.
  421. try:
  422. self.ref_obj = self.app.collection.get_by_name(bound_obj_name)
  423. except Exception as e:
  424. self.app.inform.emit('[ERROR_NOTCL] %s: %s' % (_("Could not retrieve object"), bound_obj_name))
  425. return "Could not retrieve object: %s. Error: %s" % (bound_obj_name, str(e))
  426. self.on_copper_thieving(
  427. thieving_obj=self.grb_object,
  428. ref_obj=self.ref_obj,
  429. c_val=self.clearance_val,
  430. margin=self.margin_val
  431. )
  432. # To be called after clicking on the plot.
  433. def on_mouse_release(self, event):
  434. if self.app.is_legacy is False:
  435. event_pos = event.pos
  436. # event_is_dragging = event.is_dragging
  437. right_button = 2
  438. else:
  439. event_pos = (event.xdata, event.ydata)
  440. # event_is_dragging = self.app.plotcanvas.is_dragging
  441. right_button = 3
  442. event_pos = self.app.plotcanvas.translate_coords(event_pos)
  443. # do clear area only for left mouse clicks
  444. if event.button == 1:
  445. if self.first_click is False:
  446. self.first_click = True
  447. self.app.inform.emit('[WARNING_NOTCL] %s' % _("Click the end point of the filling area."))
  448. self.cursor_pos = self.app.plotcanvas.translate_coords(event_pos)
  449. if self.app.grid_status() is True:
  450. self.cursor_pos = self.app.geo_editor.snap(event_pos[0], event_pos[1])
  451. else:
  452. self.app.inform.emit(_("Zone added. Click to start adding next zone or right click to finish."))
  453. self.app.delete_selection_shape()
  454. if self.app.grid_status() is True:
  455. curr_pos = self.app.geo_editor.snap(event_pos[0], event_pos[1])
  456. else:
  457. curr_pos = (event_pos[0], event_pos[1])
  458. x0, y0 = self.cursor_pos[0], self.cursor_pos[1]
  459. x1, y1 = curr_pos[0], curr_pos[1]
  460. pt1 = (x0, y0)
  461. pt2 = (x1, y0)
  462. pt3 = (x1, y1)
  463. pt4 = (x0, y1)
  464. new_rectangle = Polygon([pt1, pt2, pt3, pt4])
  465. self.sel_rect.append(new_rectangle)
  466. # add a temporary shape on canvas
  467. self.draw_tool_selection_shape(old_coords=(x0, y0), coords=(x1, y1))
  468. self.first_click = False
  469. return
  470. elif event.button == right_button and self.mouse_is_dragging is False:
  471. self.area_method = False
  472. self.first_click = False
  473. self.delete_tool_selection_shape()
  474. if self.app.is_legacy is False:
  475. self.app.plotcanvas.graph_event_disconnect('mouse_release', self.on_mouse_release)
  476. self.app.plotcanvas.graph_event_disconnect('mouse_move', self.on_mouse_move)
  477. else:
  478. self.app.plotcanvas.graph_event_disconnect(self.mr)
  479. self.app.plotcanvas.graph_event_disconnect(self.mm)
  480. self.app.mp = self.app.plotcanvas.graph_event_connect('mouse_press',
  481. self.app.on_mouse_click_over_plot)
  482. self.app.mm = self.app.plotcanvas.graph_event_connect('mouse_move',
  483. self.app.on_mouse_move_over_plot)
  484. self.app.mr = self.app.plotcanvas.graph_event_connect('mouse_release',
  485. self.app.on_mouse_click_release_over_plot)
  486. if len(self.sel_rect) == 0:
  487. return
  488. self.sel_rect = cascaded_union(self.sel_rect)
  489. if not isinstance(self.sel_rect, Iterable):
  490. self.sel_rect = [self.sel_rect]
  491. self.on_copper_thieving(
  492. thieving_obj=self.grb_object,
  493. ref_obj=self.sel_rect,
  494. c_val=self.clearance_val,
  495. margin=self.margin_val
  496. )
  497. # called on mouse move
  498. def on_mouse_move(self, event):
  499. if self.app.is_legacy is False:
  500. event_pos = event.pos
  501. event_is_dragging = event.is_dragging
  502. # right_button = 2
  503. else:
  504. event_pos = (event.xdata, event.ydata)
  505. event_is_dragging = self.app.plotcanvas.is_dragging
  506. # right_button = 3
  507. curr_pos = self.app.plotcanvas.translate_coords(event_pos)
  508. # detect mouse dragging motion
  509. if event_is_dragging is True:
  510. self.mouse_is_dragging = True
  511. else:
  512. self.mouse_is_dragging = False
  513. # update the cursor position
  514. if self.app.grid_status() is True:
  515. # Update cursor
  516. curr_pos = self.app.geo_editor.snap(curr_pos[0], curr_pos[1])
  517. self.app.app_cursor.set_data(np.asarray([(curr_pos[0], curr_pos[1])]),
  518. symbol='++', edge_color=self.app.cursor_color_3D,
  519. size=self.app.defaults["global_cursor_size"])
  520. # update the positions on status bar
  521. self.app.ui.position_label.setText("&nbsp;&nbsp;&nbsp;&nbsp;<b>X</b>: %.4f&nbsp;&nbsp; "
  522. "<b>Y</b>: %.4f" % (curr_pos[0], curr_pos[1]))
  523. if self.cursor_pos is None:
  524. self.cursor_pos = (0, 0)
  525. dx = curr_pos[0] - float(self.cursor_pos[0])
  526. dy = curr_pos[1] - float(self.cursor_pos[1])
  527. self.app.ui.rel_position_label.setText("<b>Dx</b>: %.4f&nbsp;&nbsp; <b>Dy</b>: "
  528. "%.4f&nbsp;&nbsp;&nbsp;&nbsp;" % (dx, dy))
  529. # draw the utility geometry
  530. if self.first_click:
  531. self.app.delete_selection_shape()
  532. self.app.draw_moving_selection_shape(old_coords=(self.cursor_pos[0], self.cursor_pos[1]),
  533. coords=(curr_pos[0], curr_pos[1]))
  534. def on_copper_thieving(self, thieving_obj, ref_obj=None, c_val=None, margin=None, run_threaded=True):
  535. """
  536. :param thieving_obj:
  537. :param ref_obj:
  538. :param c_val:
  539. :param margin:
  540. :param run_threaded:
  541. :return:
  542. """
  543. if run_threaded:
  544. proc = self.app.proc_container.new('%s ...' % _("Copper thieving"))
  545. else:
  546. self.app.proc_container.view.set_busy('%s ...' % _("Copper thieving"))
  547. QtWidgets.QApplication.processEvents()
  548. # #####################################################################
  549. # ####### Read the parameters #########################################
  550. # #####################################################################
  551. log.debug("Copper Thieving Tool started. Reading parameters.")
  552. self.app.inform.emit(_("Copper Thieving Tool started. Reading parameters."))
  553. ref_selected = self.reference_radio.get_value()
  554. if c_val is None:
  555. c_val = float(self.app.defaults["tools_copperfill_clearance"])
  556. if margin is None:
  557. margin = float(self.app.defaults["tools_copperfill_margin"])
  558. fill_type = self.fill_type_radio.get_value()
  559. dot_dia = self.dot_dia_entry.get_value()
  560. dot_spacing = self.dot_spacing_entry.get_value()
  561. square_size = self.square_size_entry.get_value()
  562. square_spacing = self.squares_spacing_entry.get_value()
  563. line_size = self.line_size_entry.get_value()
  564. line_spacing = self.lines_spacing_entry.get_value()
  565. # make sure that the source object solid geometry is an Iterable
  566. if not isinstance(self.grb_object.solid_geometry, Iterable):
  567. self.grb_object.solid_geometry = [self.grb_object.solid_geometry]
  568. # #########################################################################################
  569. # Prepare isolation polygon. This will create the clearance over the Gerber features ######
  570. # #########################################################################################
  571. log.debug("Copper Thieving Tool. Preparing isolation polygons.")
  572. self.app.inform.emit(_("Copper Thieving Tool. Preparing isolation polygons."))
  573. # variables to display the percentage of work done
  574. geo_len = 0
  575. try:
  576. for pol in self.grb_object.solid_geometry:
  577. geo_len += 1
  578. except TypeError:
  579. geo_len = 1
  580. old_disp_number = 0
  581. pol_nr = 0
  582. clearance_geometry = []
  583. try:
  584. for pol in self.grb_object.solid_geometry:
  585. if self.app.abort_flag:
  586. # graceful abort requested by the user
  587. raise FlatCAMApp.GracefulException
  588. clearance_geometry.append(
  589. pol.buffer(c_val, int(int(self.geo_steps_per_circle) / 4))
  590. )
  591. pol_nr += 1
  592. disp_number = int(np.interp(pol_nr, [0, geo_len], [0, 100]))
  593. if old_disp_number < disp_number <= 100:
  594. self.app.proc_container.update_view_text(' %s ... %d%%' %
  595. (_("Buffering"), int(disp_number)))
  596. old_disp_number = disp_number
  597. except TypeError:
  598. # taking care of the case when the self.solid_geometry is just a single Polygon, not a list or a
  599. # MultiPolygon (not an iterable)
  600. clearance_geometry.append(
  601. self.grb_object.solid_geometry.buffer(c_val, int(int(self.geo_steps_per_circle) / 4))
  602. )
  603. self.app.proc_container.update_view_text(' %s' % _("Buffering"))
  604. clearance_geometry = unary_union(clearance_geometry)
  605. # #########################################################################################
  606. # Prepare the area to fill with copper. ###################################################
  607. # #########################################################################################
  608. log.debug("Copper Thieving Tool. Preparing areas to fill with copper.")
  609. self.app.inform.emit(_("Copper Thieving Tool. Preparing areas to fill with copper."))
  610. try:
  611. if ref_obj is None or ref_obj == 'itself':
  612. working_obj = thieving_obj
  613. else:
  614. working_obj = ref_obj
  615. except Exception as e:
  616. log.debug("ToolCopperThieving.on_copper_thieving() --> %s" % str(e))
  617. return 'fail'
  618. bounding_box = None
  619. if ref_selected == 'itself':
  620. geo_n = working_obj.solid_geometry
  621. try:
  622. if self.bbox_type_radio.get_value() == 'min':
  623. if isinstance(geo_n, MultiPolygon):
  624. env_obj = geo_n.convex_hull
  625. elif (isinstance(geo_n, MultiPolygon) and len(geo_n) == 1) or \
  626. (isinstance(geo_n, list) and len(geo_n) == 1) and isinstance(geo_n[0], Polygon):
  627. env_obj = cascaded_union(geo_n)
  628. else:
  629. env_obj = cascaded_union(geo_n)
  630. env_obj = env_obj.convex_hull
  631. else:
  632. if isinstance(geo_n, Polygon) or \
  633. (isinstance(geo_n, list) and len(geo_n) == 1) or \
  634. (isinstance(geo_n, MultiPolygon) and len(geo_n) == 1):
  635. env_obj = geo_n.buffer(0, join_style=base.JOIN_STYLE.mitre).exterior
  636. elif isinstance(geo_n, MultiPolygon):
  637. x0, y0, x1, y1 = geo_n.bounds
  638. geo = box(x0, y0, x1, y1)
  639. env_obj = geo.buffer(0, join_style=base.JOIN_STYLE.mitre)
  640. else:
  641. self.app.inform.emit(
  642. '[ERROR_NOTCL] %s: %s' % (_("Geometry not supported for bounding box"), type(geo_n))
  643. )
  644. return 'fail'
  645. bounding_box = env_obj.buffer(distance=margin, join_style=base.JOIN_STYLE.mitre)
  646. except Exception as e:
  647. log.debug("ToolCopperFIll.on_copper_thieving() 'itself' --> %s" % str(e))
  648. self.app.inform.emit('[ERROR_NOTCL] %s' % _("No object available."))
  649. return 'fail'
  650. elif ref_selected == 'area':
  651. geo_n = cascaded_union(working_obj)
  652. try:
  653. __ = iter(geo_n)
  654. except Exception as e:
  655. log.debug("ToolCopperFIll.on_copper_thieving() 'area' --> %s" % str(e))
  656. geo_n = [geo_n]
  657. geo_buff_list = []
  658. for poly in geo_n:
  659. if self.app.abort_flag:
  660. # graceful abort requested by the user
  661. raise FlatCAMApp.GracefulException
  662. geo_buff_list.append(poly.buffer(distance=margin, join_style=base.JOIN_STYLE.mitre))
  663. bounding_box = cascaded_union(geo_buff_list)
  664. elif ref_selected == 'box':
  665. geo_n = working_obj.solid_geometry
  666. if isinstance(working_obj, FlatCAMGeometry):
  667. try:
  668. __ = iter(geo_n)
  669. except Exception as e:
  670. log.debug("ToolCopperFIll.on_copper_thieving() 'box' --> %s" % str(e))
  671. geo_n = [geo_n]
  672. geo_buff_list = []
  673. for poly in geo_n:
  674. if self.app.abort_flag:
  675. # graceful abort requested by the user
  676. raise FlatCAMApp.GracefulException
  677. geo_buff_list.append(poly.buffer(distance=margin, join_style=base.JOIN_STYLE.mitre))
  678. bounding_box = cascaded_union(geo_buff_list)
  679. elif isinstance(working_obj, FlatCAMGerber):
  680. geo_n = cascaded_union(geo_n).convex_hull
  681. bounding_box = cascaded_union(thieving_obj.solid_geometry).convex_hull.intersection(geo_n)
  682. bounding_box = bounding_box.buffer(distance=margin, join_style=base.JOIN_STYLE.mitre)
  683. else:
  684. self.app.inform.emit('[ERROR_NOTCL] %s' % _("The reference object type is not supported."))
  685. return 'fail'
  686. log.debug("Copper Thieving Tool. Finished creating areas to fill with copper.")
  687. self.app.inform.emit(_("Copper Thieving Tool. Appending new geometry and buffering."))
  688. # #########################################################################################
  689. # ########## Generate filling geometry. ###################################################
  690. # #########################################################################################
  691. if fill_type == 'solid':
  692. new_solid_geometry = bounding_box.difference(clearance_geometry)
  693. elif fill_type == 'dots':
  694. dot_geo = Point((0, 0)).buffer(dot_dia / 2.0)
  695. try:
  696. for bb in bounding_box:
  697. pass
  698. except TypeError:
  699. pass
  700. geo_list = self.grb_object.solid_geometry
  701. if isinstance(self.grb_object.solid_geometry, MultiPolygon):
  702. geo_list = list(self.grb_object.solid_geometry.geoms)
  703. if '0' not in self.grb_object.apertures:
  704. self.grb_object.apertures['0'] = dict()
  705. self.grb_object.apertures['0']['geometry'] = list()
  706. self.grb_object.apertures['0']['type'] = 'REG'
  707. self.grb_object.apertures['0']['size'] = 0.0
  708. try:
  709. for poly in new_solid_geometry:
  710. # append to the new solid geometry
  711. geo_list.append(poly)
  712. # append into the '0' aperture
  713. geo_elem = dict()
  714. geo_elem['solid'] = poly
  715. geo_elem['follow'] = poly.exterior
  716. self.grb_object.apertures['0']['geometry'].append(deepcopy(geo_elem))
  717. except TypeError:
  718. # append to the new solid geometry
  719. geo_list.append(new_solid_geometry)
  720. # append into the '0' aperture
  721. geo_elem = dict()
  722. geo_elem['solid'] = new_solid_geometry
  723. geo_elem['follow'] = new_solid_geometry.exterior
  724. self.grb_object.apertures['0']['geometry'].append(deepcopy(geo_elem))
  725. self.grb_object.solid_geometry = MultiPolygon(geo_list).buffer(0.0000001).buffer(-0.0000001)
  726. # update the source file with the new geometry:
  727. self.grb_object.source_file = self.app.export_gerber(obj_name=self.grb_object.options['name'], filename=None,
  728. local_use=self.grb_object, use_thread=False)
  729. self.on_exit()
  730. self.app.inform.emit('[success] %s' % _("Copper Thieving Tool done."))
  731. def replot(self, obj):
  732. def worker_task():
  733. with self.app.proc_container.new('%s...' % _("Plotting")):
  734. obj.plot()
  735. self.app.worker_task.emit({'fcn': worker_task, 'params': []})
  736. def on_exit(self):
  737. # plot the object
  738. self.replot(obj=self.grb_object)
  739. # update the bounding box values
  740. try:
  741. a, b, c, d = self.grb_object.bounds()
  742. self.grb_object.options['xmin'] = a
  743. self.grb_object.options['ymin'] = b
  744. self.grb_object.options['xmax'] = c
  745. self.grb_object.options['ymax'] = d
  746. except Exception as e:
  747. log.debug("ToolCopperThieving.on_exit() bounds error --> %s" % str(e))
  748. # reset the variables
  749. self.grb_object = None
  750. self.ref_obj = None
  751. self.sel_rect = list()
  752. # Events ID
  753. self.mr = None
  754. self.mm = None
  755. # Mouse cursor positions
  756. self.mouse_is_dragging = False
  757. self.cursor_pos = (0, 0)
  758. self.first_click = False
  759. # if True it means we exited from tool in the middle of area adding therefore disconnect the events
  760. if self.area_method is True:
  761. self.app.delete_selection_shape()
  762. self.area_method = False
  763. if self.app.is_legacy is False:
  764. self.app.plotcanvas.graph_event_disconnect('mouse_release', self.on_mouse_release)
  765. self.app.plotcanvas.graph_event_disconnect('mouse_move', self.on_mouse_move)
  766. else:
  767. self.app.plotcanvas.graph_event_disconnect(self.mr)
  768. self.app.plotcanvas.graph_event_disconnect(self.mm)
  769. self.app.mp = self.app.plotcanvas.graph_event_connect('mouse_press',
  770. self.app.on_mouse_click_over_plot)
  771. self.app.mm = self.app.plotcanvas.graph_event_connect('mouse_move',
  772. self.app.on_mouse_move_over_plot)
  773. self.app.mr = self.app.plotcanvas.graph_event_connect('mouse_release',
  774. self.app.on_mouse_click_release_over_plot)
  775. self.app.call_source = "app"
  776. self.app.inform.emit('[success] %s' % _("Copper Thieving Tool exit."))