ToolPunchGerber.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # File Author: Marius Adrian Stanciu (c) #
  4. # Date: 1/24/2020 #
  5. # MIT Licence #
  6. # ##########################################################
  7. from PyQt5 import QtGui, QtCore, QtWidgets
  8. from FlatCAMTool import FlatCAMTool
  9. from flatcamGUI.GUIElements import RadioSet, FCDoubleSpinner, FCCheckBox, \
  10. OptionalHideInputSection, OptionalInputSection, FCComboBox
  11. from copy import deepcopy
  12. import logging
  13. from shapely.geometry import Polygon, MultiPolygon, Point
  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 ToolPunchGerber(FlatCAMTool):
  22. toolName = _("Punch Gerber")
  23. def __init__(self, app):
  24. FlatCAMTool.__init__(self, app)
  25. self.decimals = self.app.decimals
  26. # Title
  27. title_label = QtWidgets.QLabel("%s" % self.toolName)
  28. title_label.setStyleSheet("""
  29. QLabel
  30. {
  31. font-size: 16px;
  32. font-weight: bold;
  33. }
  34. """)
  35. self.layout.addWidget(title_label)
  36. # Punch Drill holes
  37. self.layout.addWidget(QtWidgets.QLabel(""))
  38. # ## Grid Layout
  39. grid_lay = QtWidgets.QGridLayout()
  40. self.layout.addLayout(grid_lay)
  41. grid_lay.setColumnStretch(0, 1)
  42. grid_lay.setColumnStretch(1, 0)
  43. # ## Gerber Object
  44. self.gerber_object_combo = QtWidgets.QComboBox()
  45. self.gerber_object_combo.setModel(self.app.collection)
  46. self.gerber_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  47. self.gerber_object_combo.setCurrentIndex(1)
  48. self.grb_label = QtWidgets.QLabel("<b>%s:</b>" % _("GERBER"))
  49. self.grb_label.setToolTip('%s.' % _("Gerber into which to punch holes"))
  50. grid_lay.addWidget(self.grb_label, 0, 0, 1, 2)
  51. grid_lay.addWidget(self.gerber_object_combo, 1, 0, 1, 2)
  52. separator_line = QtWidgets.QFrame()
  53. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  54. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  55. grid_lay.addWidget(separator_line, 2, 0, 1, 2)
  56. self.padt_label = QtWidgets.QLabel("<b>%s</b>" % _("Processed Pads Type"))
  57. self.padt_label.setToolTip(
  58. _("The type of pads shape to be processed.\n"
  59. "If the PCB has many SMD pads with rectangular pads,\n"
  60. "disable the Rectangular aperture.")
  61. )
  62. grid_lay.addWidget(self.padt_label, 3, 0, 1, 2)
  63. # Select all
  64. self.select_all_cb = FCCheckBox('%s' % _("ALL"))
  65. grid_lay.addWidget(self.select_all_cb)
  66. # Circular Aperture Selection
  67. self.circular_cb = FCCheckBox('%s' % _("Circular"))
  68. self.circular_cb.setToolTip(
  69. _("Create drills from circular pads.")
  70. )
  71. grid_lay.addWidget(self.circular_cb, 5, 0, 1, 2)
  72. # Oblong Aperture Selection
  73. self.oblong_cb = FCCheckBox('%s' % _("Oblong"))
  74. self.oblong_cb.setToolTip(
  75. _("Create drills from oblong pads.")
  76. )
  77. grid_lay.addWidget(self.oblong_cb, 6, 0, 1, 2)
  78. # Square Aperture Selection
  79. self.square_cb = FCCheckBox('%s' % _("Square"))
  80. self.square_cb.setToolTip(
  81. _("Create drills from square pads.")
  82. )
  83. grid_lay.addWidget(self.square_cb, 7, 0, 1, 2)
  84. # Rectangular Aperture Selection
  85. self.rectangular_cb = FCCheckBox('%s' % _("Rectangular"))
  86. self.rectangular_cb.setToolTip(
  87. _("Create drills from rectangular pads.")
  88. )
  89. grid_lay.addWidget(self.rectangular_cb, 8, 0, 1, 2)
  90. # Others type of Apertures Selection
  91. self.other_cb = FCCheckBox('%s' % _("Others"))
  92. self.other_cb.setToolTip(
  93. _("Create drills from other types of pad shape.")
  94. )
  95. grid_lay.addWidget(self.other_cb, 9, 0, 1, 2)
  96. separator_line = QtWidgets.QFrame()
  97. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  98. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  99. grid_lay.addWidget(separator_line, 10, 0, 1, 2)
  100. # Grid Layout
  101. grid0 = QtWidgets.QGridLayout()
  102. self.layout.addLayout(grid0)
  103. grid0.setColumnStretch(0, 0)
  104. grid0.setColumnStretch(1, 1)
  105. self.method_label = QtWidgets.QLabel('<b>%s:</b>' % _("Method"))
  106. self.method_label.setToolTip(
  107. _("The punch hole source can be:\n"
  108. "- Excellon Object-> the Excellon object drills center will serve as reference.\n"
  109. "- Fixed Diameter -> will try to use the pads center as reference adding fixed diameter holes.\n"
  110. "- Fixed Annular Ring -> will try to keep a set annular ring.\n"
  111. "- Proportional -> will make a Gerber punch hole having the diameter a percentage of the pad diameter.\n")
  112. )
  113. self.method_punch = RadioSet(
  114. [
  115. {'label': _('Excellon'), 'value': 'exc'},
  116. {'label': _("Fixed Diameter"), 'value': 'fixed'},
  117. {'label': _("Fixed Annular Ring"), 'value': 'ring'},
  118. {'label': _("Proportional"), 'value': 'prop'}
  119. ],
  120. orientation='vertical',
  121. stretch=False)
  122. grid0.addWidget(self.method_label, 0, 0, 1, 2)
  123. grid0.addWidget(self.method_punch, 1, 0, 1, 2)
  124. separator_line = QtWidgets.QFrame()
  125. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  126. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  127. grid0.addWidget(separator_line, 2, 0, 1, 2)
  128. self.exc_label = QtWidgets.QLabel('<b>%s</b>' % _("Excellon"))
  129. self.exc_label.setToolTip(
  130. _("Remove the geometry of Excellon from the Gerber to create the holes in pads.")
  131. )
  132. self.exc_combo = QtWidgets.QComboBox()
  133. self.exc_combo.setModel(self.app.collection)
  134. self.exc_combo.setRootModelIndex(self.app.collection.index(1, 0, QtCore.QModelIndex()))
  135. self.exc_combo.setCurrentIndex(1)
  136. grid0.addWidget(self.exc_label, 3, 0, 1, 2)
  137. grid0.addWidget(self.exc_combo, 4, 0, 1, 2)
  138. separator_line = QtWidgets.QFrame()
  139. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  140. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  141. grid0.addWidget(separator_line, 5, 0, 1, 2)
  142. # Fixed Dia
  143. self.fixed_label = QtWidgets.QLabel('<b>%s</b>' % _("Fixed Diameter"))
  144. grid0.addWidget(self.fixed_label, 6, 0, 1, 2)
  145. # Diameter value
  146. self.dia_entry = FCDoubleSpinner()
  147. self.dia_entry.set_precision(self.decimals)
  148. self.dia_entry.set_range(0.0000, 9999.9999)
  149. self.dia_label = QtWidgets.QLabel('%s:' % _("Value"))
  150. self.dia_label.setToolTip(
  151. _("Fixed hole diameter.")
  152. )
  153. grid0.addWidget(self.dia_label, 8, 0)
  154. grid0.addWidget(self.dia_entry, 8, 1)
  155. separator_line = QtWidgets.QFrame()
  156. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  157. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  158. grid0.addWidget(separator_line, 9, 0, 1, 2)
  159. self.ring_frame = QtWidgets.QFrame()
  160. self.ring_frame.setContentsMargins(0, 0, 0, 0)
  161. grid0.addWidget(self.ring_frame, 10, 0, 1, 2)
  162. self.ring_box = QtWidgets.QVBoxLayout()
  163. self.ring_box.setContentsMargins(0, 0, 0, 0)
  164. self.ring_frame.setLayout(self.ring_box)
  165. # Annular Ring value
  166. self.ring_label = QtWidgets.QLabel('<b>%s</b>' % _("Fixed Annular Ring"))
  167. self.ring_label.setToolTip(
  168. _("The size of annular ring.\n"
  169. "The copper sliver between the drill hole exterior\n"
  170. "and the margin of the copper pad.")
  171. )
  172. self.ring_box.addWidget(self.ring_label)
  173. # ## Grid Layout
  174. self.grid1 = QtWidgets.QGridLayout()
  175. self.grid1.setColumnStretch(0, 0)
  176. self.grid1.setColumnStretch(1, 1)
  177. self.ring_box.addLayout(self.grid1)
  178. # Circular Annular Ring Value
  179. self.circular_ring_label = QtWidgets.QLabel('%s:' % _("Circular"))
  180. self.circular_ring_label.setToolTip(
  181. _("The size of annular ring for circular pads.")
  182. )
  183. self.circular_ring_entry = FCDoubleSpinner()
  184. self.circular_ring_entry.set_precision(self.decimals)
  185. self.circular_ring_entry.set_range(0.0000, 9999.9999)
  186. self.grid1.addWidget(self.circular_ring_label, 3, 0)
  187. self.grid1.addWidget(self.circular_ring_entry, 3, 1)
  188. # Oblong Annular Ring Value
  189. self.oblong_ring_label = QtWidgets.QLabel('%s:' % _("Oblong"))
  190. self.oblong_ring_label.setToolTip(
  191. _("The size of annular ring for oblong pads.")
  192. )
  193. self.oblong_ring_entry = FCDoubleSpinner()
  194. self.oblong_ring_entry.set_precision(self.decimals)
  195. self.oblong_ring_entry.set_range(0.0000, 9999.9999)
  196. self.grid1.addWidget(self.oblong_ring_label, 4, 0)
  197. self.grid1.addWidget(self.oblong_ring_entry, 4, 1)
  198. # Square Annular Ring Value
  199. self.square_ring_label = QtWidgets.QLabel('%s:' % _("Square"))
  200. self.square_ring_label.setToolTip(
  201. _("The size of annular ring for square pads.")
  202. )
  203. self.square_ring_entry = FCDoubleSpinner()
  204. self.square_ring_entry.set_precision(self.decimals)
  205. self.square_ring_entry.set_range(0.0000, 9999.9999)
  206. self.grid1.addWidget(self.square_ring_label, 5, 0)
  207. self.grid1.addWidget(self.square_ring_entry, 5, 1)
  208. # Rectangular Annular Ring Value
  209. self.rectangular_ring_label = QtWidgets.QLabel('%s:' % _("Rectangular"))
  210. self.rectangular_ring_label.setToolTip(
  211. _("The size of annular ring for rectangular pads.")
  212. )
  213. self.rectangular_ring_entry = FCDoubleSpinner()
  214. self.rectangular_ring_entry.set_precision(self.decimals)
  215. self.rectangular_ring_entry.set_range(0.0000, 9999.9999)
  216. self.grid1.addWidget(self.rectangular_ring_label, 6, 0)
  217. self.grid1.addWidget(self.rectangular_ring_entry, 6, 1)
  218. # Others Annular Ring Value
  219. self.other_ring_label = QtWidgets.QLabel('%s:' % _("Others"))
  220. self.other_ring_label.setToolTip(
  221. _("The size of annular ring for other pads.")
  222. )
  223. self.other_ring_entry = FCDoubleSpinner()
  224. self.other_ring_entry.set_precision(self.decimals)
  225. self.other_ring_entry.set_range(0.0000, 9999.9999)
  226. self.grid1.addWidget(self.other_ring_label, 7, 0)
  227. self.grid1.addWidget(self.other_ring_entry, 7, 1)
  228. separator_line = QtWidgets.QFrame()
  229. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  230. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  231. grid0.addWidget(separator_line, 11, 0, 1, 2)
  232. # Proportional value
  233. self.prop_label = QtWidgets.QLabel('<b>%s</b>' % _("Proportional Diameter"))
  234. grid0.addWidget(self.prop_label, 12, 0, 1, 2)
  235. # Diameter value
  236. self.factor_entry = FCDoubleSpinner(suffix='%')
  237. self.factor_entry.set_precision(self.decimals)
  238. self.factor_entry.set_range(0.0000, 100.0000)
  239. self.factor_entry.setSingleStep(0.1)
  240. self.factor_label = QtWidgets.QLabel('%s:' % _("Value"))
  241. self.factor_label.setToolTip(
  242. _("Proportional Diameter.\n"
  243. "The drill diameter will be a fraction of the pad size.")
  244. )
  245. grid0.addWidget(self.factor_label, 13, 0)
  246. grid0.addWidget(self.factor_entry, 13, 1)
  247. separator_line3 = QtWidgets.QFrame()
  248. separator_line3.setFrameShape(QtWidgets.QFrame.HLine)
  249. separator_line3.setFrameShadow(QtWidgets.QFrame.Sunken)
  250. grid0.addWidget(separator_line3, 14, 0, 1, 2)
  251. # Buttons
  252. self.punch_object_button = QtWidgets.QPushButton(_("Punch Gerber"))
  253. self.punch_object_button.setToolTip(
  254. _("Create a Gerber object from the selected object, within\n"
  255. "the specified box.")
  256. )
  257. self.punch_object_button.setStyleSheet("""
  258. QPushButton
  259. {
  260. font-weight: bold;
  261. }
  262. """)
  263. self.layout.addWidget(self.punch_object_button)
  264. self.layout.addStretch()
  265. # ## Reset Tool
  266. self.reset_button = QtWidgets.QPushButton(_("Reset Tool"))
  267. self.reset_button.setToolTip(
  268. _("Will reset the tool parameters.")
  269. )
  270. self.reset_button.setStyleSheet("""
  271. QPushButton
  272. {
  273. font-weight: bold;
  274. }
  275. """)
  276. self.layout.addWidget(self.reset_button)
  277. self.units = self.app.defaults['units']
  278. # self.cb_items = [
  279. # self.grid1.itemAt(w).widget() for w in range(self.grid1.count())
  280. # if isinstance(self.grid1.itemAt(w).widget(), FCCheckBox)
  281. # ]
  282. self.circular_ring_entry.setEnabled(False)
  283. self.oblong_ring_entry.setEnabled(False)
  284. self.square_ring_entry.setEnabled(False)
  285. self.rectangular_ring_entry.setEnabled(False)
  286. self.other_ring_entry.setEnabled(False)
  287. self.dia_entry.setDisabled(True)
  288. self.dia_label.setDisabled(True)
  289. self.factor_label.setDisabled(True)
  290. self.factor_entry.setDisabled(True)
  291. # ## Signals
  292. self.method_punch.activated_custom.connect(self.on_method)
  293. self.reset_button.clicked.connect(self.set_tool_ui)
  294. self.punch_object_button.clicked.connect(self.on_generate_object)
  295. self.circular_cb.stateChanged.connect(
  296. lambda state:
  297. self.circular_ring_entry.setDisabled(False) if state else self.circular_ring_entry.setDisabled(True)
  298. )
  299. self.oblong_cb.stateChanged.connect(
  300. lambda state:
  301. self.oblong_ring_entry.setDisabled(False) if state else self.oblong_ring_entry.setDisabled(True)
  302. )
  303. self.square_cb.stateChanged.connect(
  304. lambda state:
  305. self.square_ring_entry.setDisabled(False) if state else self.square_ring_entry.setDisabled(True)
  306. )
  307. self.rectangular_cb.stateChanged.connect(
  308. lambda state:
  309. self.rectangular_ring_entry.setDisabled(False) if state else self.rectangular_ring_entry.setDisabled(True)
  310. )
  311. self.other_cb.stateChanged.connect(
  312. lambda state:
  313. self.other_ring_entry.setDisabled(False) if state else self.other_ring_entry.setDisabled(True)
  314. )
  315. def run(self, toggle=True):
  316. self.app.report_usage("ToolPunchGerber()")
  317. if toggle:
  318. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  319. if self.app.ui.splitter.sizes()[0] == 0:
  320. self.app.ui.splitter.setSizes([1, 1])
  321. else:
  322. try:
  323. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  324. # if tab is populated with the tool but it does not have the focus, focus on it
  325. if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
  326. # focus on Tool Tab
  327. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  328. else:
  329. self.app.ui.splitter.setSizes([0, 1])
  330. except AttributeError:
  331. pass
  332. else:
  333. if self.app.ui.splitter.sizes()[0] == 0:
  334. self.app.ui.splitter.setSizes([1, 1])
  335. FlatCAMTool.run(self)
  336. self.set_tool_ui()
  337. self.app.ui.notebook.setTabText(2, _("Punch Tool"))
  338. def install(self, icon=None, separator=None, **kwargs):
  339. FlatCAMTool.install(self, icon, separator, shortcut='ALT+H', **kwargs)
  340. def set_tool_ui(self):
  341. self.reset_fields()
  342. self.ui_connect()
  343. self.method_punch.set_value('exc')
  344. self.select_all_cb.set_value(True)
  345. def on_select_all(self, state):
  346. self.ui_disconnect()
  347. if state:
  348. self.circular_cb.setChecked(True)
  349. self.oblong_cb.setChecked(True)
  350. self.square_cb.setChecked(True)
  351. self.rectangular_cb.setChecked(True)
  352. self.other_cb.setChecked(True)
  353. else:
  354. self.circular_cb.setChecked(False)
  355. self.oblong_cb.setChecked(False)
  356. self.square_cb.setChecked(False)
  357. self.rectangular_cb.setChecked(False)
  358. self.other_cb.setChecked(False)
  359. self.ui_connect()
  360. def on_method(self, val):
  361. self.exc_label.setEnabled(False)
  362. self.exc_combo.setEnabled(False)
  363. self.fixed_label.setEnabled(False)
  364. self.dia_label.setEnabled(False)
  365. self.dia_entry.setEnabled(False)
  366. self.ring_frame.setEnabled(False)
  367. self.prop_label.setEnabled(False)
  368. self.factor_label.setEnabled(False)
  369. self.factor_entry.setEnabled(False)
  370. if val == 'exc':
  371. self.exc_label.setEnabled(True)
  372. self.exc_combo.setEnabled(True)
  373. elif val == 'fixed':
  374. self.fixed_label.setEnabled(True)
  375. self.dia_label.setEnabled(True)
  376. self.dia_entry.setEnabled(True)
  377. elif val == 'ring':
  378. self.ring_frame.setEnabled(True)
  379. elif val == 'prop':
  380. self.prop_label.setEnabled(True)
  381. self.factor_label.setEnabled(True)
  382. self.factor_entry.setEnabled(True)
  383. def ui_connect(self):
  384. self.select_all_cb.stateChanged.connect(self.on_select_all)
  385. def ui_disconnect(self):
  386. try:
  387. self.select_all_cb.stateChanged.disconnect()
  388. except (AttributeError, TypeError):
  389. pass
  390. def on_generate_object(self):
  391. # get the Gerber file who is the source of the punched Gerber
  392. selection_index = self.gerber_object_combo.currentIndex()
  393. model_index = self.app.collection.index(selection_index, 0, self.gerber_object_combo.rootModelIndex())
  394. try:
  395. grb_obj = model_index.internalPointer().obj
  396. except Exception:
  397. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Gerber object loaded ..."))
  398. return
  399. name = grb_obj.options['name'].rpartition('.')[0]
  400. outname = name + "_punched"
  401. punch_method = self.method_punch.get_value()
  402. if punch_method == 'exc':
  403. # get the Excellon file whose geometry will create the punch holes
  404. selection_index = self.exc_combo.currentIndex()
  405. model_index = self.app.collection.index(selection_index, 0, self.exc_combo.rootModelIndex())
  406. try:
  407. exc_obj = model_index.internalPointer().obj
  408. except Exception:
  409. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Excellon object loaded ..."))
  410. return
  411. # this is the punching geometry
  412. exc_solid_geometry = MultiPolygon(exc_obj.solid_geometry)
  413. if isinstance(grb_obj.solid_geometry, list):
  414. grb_solid_geometry = MultiPolygon(grb_obj.solid_geometry)
  415. else:
  416. grb_solid_geometry = grb_obj.solid_geometry
  417. # create the punched Gerber solid_geometry
  418. punched_solid_geometry = grb_solid_geometry.difference(exc_solid_geometry)
  419. new_apertures = dict()
  420. new_apertures = deepcopy(grb_obj.apertures)
  421. holes_apertures = dict()
  422. for apid, val in new_apertures.items():
  423. for elem in val['geometry']:
  424. # make it work only for Gerber Flashes who are Points in 'follow'
  425. if 'solid' in elem and isinstance(elem['follow'], Point):
  426. for drill in exc_obj.drills:
  427. clear_apid = exc_obj.tools[drill['tool']]['C']
  428. exc_poly = drill['point'].buffer(clear_apid / 2.0)
  429. if exc_poly.within(elem['solid']):
  430. if clear_apid not in holes_apertures or holes_apertures[clear_apid]['type'] != 'C':
  431. holes_apertures[clear_apid] = dict()
  432. holes_apertures[clear_apid]['type'] = 'C'
  433. holes_apertures[clear_apid]['size'] = clear_apid
  434. holes_apertures[clear_apid]['geometry'] = list()
  435. geo_elem = dict()
  436. geo_elem['clear'] = exc_poly
  437. geo_elem['follow'] = exc_poly.centroid
  438. holes_apertures[clear_apid]['geometry'].append(deepcopy(geo_elem))
  439. elem['clear'] = exc_poly.centroid
  440. for apid, val in new_apertures.items():
  441. for clear_apid, clear_val in holes_apertures.items():
  442. if round(clear_apid, self.decimals) == round(val['size'], self.decimals):
  443. geo_elem = dict()
  444. val['geometry'].append(geo_elem)
  445. def init_func(new_obj, app_obj):
  446. new_obj.options.update(grb_obj.options)
  447. new_obj.options['name'] = outname
  448. new_obj.fill_color = deepcopy(grb_obj.fill_color)
  449. new_obj.outline_color = deepcopy(grb_obj.outline_color)
  450. new_obj.apertures = deepcopy(new_apertures)
  451. new_obj.solid_geometry = deepcopy(punched_solid_geometry)
  452. new_obj.source_file = self.app.export_gerber(obj_name=outname, filename=None,
  453. local_use=new_obj, use_thread=False)
  454. self.app.new_object('gerber', outname, init_func)
  455. elif punch_method == 'fixed':
  456. punch_size = float(self.dia_entry.get_value())
  457. punching_geo = list()
  458. for apid in grb_obj.apertures:
  459. if grb_obj.apertures[apid]['type'] == 'C':
  460. if punch_size >= float(grb_obj.apertures[apid]['size']):
  461. self.app.inform.emit('[ERROR_NOTCL] %s' %
  462. _(" Could not generate punched hole Gerber because the punch hole size"
  463. "is bigger than some of the apertures in the Gerber object."))
  464. return 'fail'
  465. else:
  466. for elem in grb_obj.apertures[apid]['geometry']:
  467. if 'follow' in elem:
  468. if isinstance(elem['follow'], Point):
  469. punching_geo.append(elem['follow'].buffer(punch_size / 2))
  470. else:
  471. if punch_size >= float(grb_obj.apertures[apid]['width']) or \
  472. punch_size >= float(grb_obj.apertures[apid]['height']):
  473. self.app.inform.emit('[ERROR_NOTCL] %s' %
  474. _("Could not generate punched hole Gerber because the punch hole size"
  475. "is bigger than some of the apertures in the Gerber object."))
  476. return 'fail'
  477. else:
  478. for elem in grb_obj.apertures[apid]['geometry']:
  479. if 'follow' in elem:
  480. if isinstance(elem['follow'], Point):
  481. punching_geo.append(elem['follow'].buffer(punch_size / 2))
  482. punching_geo = MultiPolygon(punching_geo)
  483. if isinstance(grb_obj.solid_geometry, list):
  484. temp_solid_geometry = MultiPolygon(grb_obj.solid_geometry)
  485. else:
  486. temp_solid_geometry = grb_obj.solid_geometry
  487. punched_solid_geometry = temp_solid_geometry.difference(punching_geo)
  488. if punched_solid_geometry == temp_solid_geometry:
  489. self.app.inform.emit('[WARNING_NOTCL] %s' %
  490. _("Could not generate punched hole Gerber because the newly created object "
  491. "geometry is the same as the one in the source object geometry..."))
  492. return 'fail'
  493. def init_func(new_obj, app_obj):
  494. new_obj.solid_geometry = deepcopy(punched_solid_geometry)
  495. self.app.new_object('gerber', outname, init_func)
  496. elif punch_method == 'ring':
  497. pass
  498. elif punch_method == 'prop':
  499. pass
  500. def reset_fields(self):
  501. self.gerber_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  502. self.exc_combo.setRootModelIndex(self.app.collection.index(1, 0, QtCore.QModelIndex()))
  503. self.ui_disconnect()