ToolExtractDrills.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # File Author: Marius Adrian Stanciu (c) #
  4. # Date: 1/10/2020 #
  5. # MIT Licence #
  6. # ##########################################################
  7. from PyQt5 import QtWidgets, QtCore
  8. from appTool import AppTool
  9. from appGUI.GUIElements import RadioSet, FCDoubleSpinner, FCCheckBox, FCComboBox
  10. from shapely.geometry import Point
  11. import logging
  12. import gettext
  13. import appTranslation as fcTranslate
  14. import builtins
  15. fcTranslate.apply_language('strings')
  16. if '_' not in builtins.__dict__:
  17. _ = gettext.gettext
  18. log = logging.getLogger('base')
  19. class ToolExtractDrills(AppTool):
  20. toolName = _("Extract Drills")
  21. def __init__(self, app):
  22. AppTool.__init__(self, app)
  23. self.decimals = self.app.decimals
  24. # ## Title
  25. title_label = QtWidgets.QLabel("%s" % self.toolName)
  26. title_label.setStyleSheet("""
  27. QLabel
  28. {
  29. font-size: 16px;
  30. font-weight: bold;
  31. }
  32. """)
  33. self.layout.addWidget(title_label)
  34. self.layout.addWidget(QtWidgets.QLabel(""))
  35. # ## Grid Layout
  36. grid_lay = QtWidgets.QGridLayout()
  37. self.layout.addLayout(grid_lay)
  38. grid_lay.setColumnStretch(0, 1)
  39. grid_lay.setColumnStretch(1, 0)
  40. # ## Gerber Object
  41. self.gerber_object_combo = FCComboBox()
  42. self.gerber_object_combo.setModel(self.app.collection)
  43. self.gerber_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  44. self.gerber_object_combo.is_last = True
  45. self.gerber_object_combo.obj_type = "Gerber"
  46. self.grb_label = QtWidgets.QLabel("<b>%s:</b>" % _("GERBER"))
  47. self.grb_label.setToolTip('%s.' % _("Gerber from which to extract drill holes"))
  48. # grid_lay.addRow("Bottom Layer:", self.object_combo)
  49. grid_lay.addWidget(self.grb_label, 0, 0, 1, 2)
  50. grid_lay.addWidget(self.gerber_object_combo, 1, 0, 1, 2)
  51. self.padt_label = QtWidgets.QLabel("<b>%s</b>" % _("Processed Pads Type"))
  52. self.padt_label.setToolTip(
  53. _("The type of pads shape to be processed.\n"
  54. "If the PCB has many SMD pads with rectangular pads,\n"
  55. "disable the Rectangular aperture.")
  56. )
  57. grid_lay.addWidget(self.padt_label, 2, 0, 1, 2)
  58. # Circular Aperture Selection
  59. self.circular_cb = FCCheckBox('%s' % _("Circular"))
  60. self.circular_cb.setToolTip(
  61. _("Process Circular Pads.")
  62. )
  63. grid_lay.addWidget(self.circular_cb, 3, 0, 1, 2)
  64. # Oblong Aperture Selection
  65. self.oblong_cb = FCCheckBox('%s' % _("Oblong"))
  66. self.oblong_cb.setToolTip(
  67. _("Process Oblong Pads.")
  68. )
  69. grid_lay.addWidget(self.oblong_cb, 4, 0, 1, 2)
  70. # Square Aperture Selection
  71. self.square_cb = FCCheckBox('%s' % _("Square"))
  72. self.square_cb.setToolTip(
  73. _("Process Square Pads.")
  74. )
  75. grid_lay.addWidget(self.square_cb, 5, 0, 1, 2)
  76. # Rectangular Aperture Selection
  77. self.rectangular_cb = FCCheckBox('%s' % _("Rectangular"))
  78. self.rectangular_cb.setToolTip(
  79. _("Process Rectangular Pads.")
  80. )
  81. grid_lay.addWidget(self.rectangular_cb, 6, 0, 1, 2)
  82. # Others type of Apertures Selection
  83. self.other_cb = FCCheckBox('%s' % _("Others"))
  84. self.other_cb.setToolTip(
  85. _("Process pads not in the categories above.")
  86. )
  87. grid_lay.addWidget(self.other_cb, 7, 0, 1, 2)
  88. separator_line = QtWidgets.QFrame()
  89. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  90. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  91. grid_lay.addWidget(separator_line, 8, 0, 1, 2)
  92. # ## Grid Layout
  93. grid1 = QtWidgets.QGridLayout()
  94. self.layout.addLayout(grid1)
  95. grid1.setColumnStretch(0, 0)
  96. grid1.setColumnStretch(1, 1)
  97. self.method_label = QtWidgets.QLabel('<b>%s</b>' % _("Method"))
  98. self.method_label.setToolTip(
  99. _("The method for processing pads. Can be:\n"
  100. "- Fixed Diameter -> all holes will have a set size\n"
  101. "- Fixed Annular Ring -> all holes will have a set annular ring\n"
  102. "- Proportional -> each hole size will be a fraction of the pad size"))
  103. grid1.addWidget(self.method_label, 2, 0, 1, 2)
  104. # ## Holes Size
  105. self.hole_size_radio = RadioSet(
  106. [
  107. {'label': _("Fixed Diameter"), 'value': 'fixed'},
  108. {'label': _("Fixed Annular Ring"), 'value': 'ring'},
  109. {'label': _("Proportional"), 'value': 'prop'}
  110. ],
  111. orientation='vertical',
  112. stretch=False)
  113. grid1.addWidget(self.hole_size_radio, 3, 0, 1, 2)
  114. # grid_lay1.addWidget(QtWidgets.QLabel(''))
  115. separator_line = QtWidgets.QFrame()
  116. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  117. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  118. grid1.addWidget(separator_line, 5, 0, 1, 2)
  119. # Annular Ring
  120. self.fixed_label = QtWidgets.QLabel('<b>%s</b>' % _("Fixed Diameter"))
  121. grid1.addWidget(self.fixed_label, 6, 0, 1, 2)
  122. # Diameter value
  123. self.dia_entry = FCDoubleSpinner(callback=self.confirmation_message)
  124. self.dia_entry.set_precision(self.decimals)
  125. self.dia_entry.set_range(0.0000, 9999.9999)
  126. self.dia_label = QtWidgets.QLabel('%s:' % _("Value"))
  127. self.dia_label.setToolTip(
  128. _("Fixed hole diameter.")
  129. )
  130. grid1.addWidget(self.dia_label, 8, 0)
  131. grid1.addWidget(self.dia_entry, 8, 1)
  132. separator_line = QtWidgets.QFrame()
  133. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  134. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  135. grid1.addWidget(separator_line, 9, 0, 1, 2)
  136. self.ring_frame = QtWidgets.QFrame()
  137. self.ring_frame.setContentsMargins(0, 0, 0, 0)
  138. self.layout.addWidget(self.ring_frame)
  139. self.ring_box = QtWidgets.QVBoxLayout()
  140. self.ring_box.setContentsMargins(0, 0, 0, 0)
  141. self.ring_frame.setLayout(self.ring_box)
  142. # ## Grid Layout
  143. grid2 = QtWidgets.QGridLayout()
  144. grid2.setColumnStretch(0, 0)
  145. grid2.setColumnStretch(1, 1)
  146. self.ring_box.addLayout(grid2)
  147. # Annular Ring value
  148. self.ring_label = QtWidgets.QLabel('<b>%s</b>' % _("Fixed Annular Ring"))
  149. self.ring_label.setToolTip(
  150. _("The size of annular ring.\n"
  151. "The copper sliver between the hole exterior\n"
  152. "and the margin of the copper pad.")
  153. )
  154. grid2.addWidget(self.ring_label, 0, 0, 1, 2)
  155. # Circular Annular Ring Value
  156. self.circular_ring_label = QtWidgets.QLabel('%s:' % _("Circular"))
  157. self.circular_ring_label.setToolTip(
  158. _("The size of annular ring for circular pads.")
  159. )
  160. self.circular_ring_entry = FCDoubleSpinner(callback=self.confirmation_message)
  161. self.circular_ring_entry.set_precision(self.decimals)
  162. self.circular_ring_entry.set_range(0.0000, 9999.9999)
  163. grid2.addWidget(self.circular_ring_label, 1, 0)
  164. grid2.addWidget(self.circular_ring_entry, 1, 1)
  165. # Oblong Annular Ring Value
  166. self.oblong_ring_label = QtWidgets.QLabel('%s:' % _("Oblong"))
  167. self.oblong_ring_label.setToolTip(
  168. _("The size of annular ring for oblong pads.")
  169. )
  170. self.oblong_ring_entry = FCDoubleSpinner(callback=self.confirmation_message)
  171. self.oblong_ring_entry.set_precision(self.decimals)
  172. self.oblong_ring_entry.set_range(0.0000, 9999.9999)
  173. grid2.addWidget(self.oblong_ring_label, 2, 0)
  174. grid2.addWidget(self.oblong_ring_entry, 2, 1)
  175. # Square Annular Ring Value
  176. self.square_ring_label = QtWidgets.QLabel('%s:' % _("Square"))
  177. self.square_ring_label.setToolTip(
  178. _("The size of annular ring for square pads.")
  179. )
  180. self.square_ring_entry = FCDoubleSpinner(callback=self.confirmation_message)
  181. self.square_ring_entry.set_precision(self.decimals)
  182. self.square_ring_entry.set_range(0.0000, 9999.9999)
  183. grid2.addWidget(self.square_ring_label, 3, 0)
  184. grid2.addWidget(self.square_ring_entry, 3, 1)
  185. # Rectangular Annular Ring Value
  186. self.rectangular_ring_label = QtWidgets.QLabel('%s:' % _("Rectangular"))
  187. self.rectangular_ring_label.setToolTip(
  188. _("The size of annular ring for rectangular pads.")
  189. )
  190. self.rectangular_ring_entry = FCDoubleSpinner(callback=self.confirmation_message)
  191. self.rectangular_ring_entry.set_precision(self.decimals)
  192. self.rectangular_ring_entry.set_range(0.0000, 9999.9999)
  193. grid2.addWidget(self.rectangular_ring_label, 4, 0)
  194. grid2.addWidget(self.rectangular_ring_entry, 4, 1)
  195. # Others Annular Ring Value
  196. self.other_ring_label = QtWidgets.QLabel('%s:' % _("Others"))
  197. self.other_ring_label.setToolTip(
  198. _("The size of annular ring for other pads.")
  199. )
  200. self.other_ring_entry = FCDoubleSpinner(callback=self.confirmation_message)
  201. self.other_ring_entry.set_precision(self.decimals)
  202. self.other_ring_entry.set_range(0.0000, 9999.9999)
  203. grid2.addWidget(self.other_ring_label, 5, 0)
  204. grid2.addWidget(self.other_ring_entry, 5, 1)
  205. grid3 = QtWidgets.QGridLayout()
  206. self.layout.addLayout(grid3)
  207. grid3.setColumnStretch(0, 0)
  208. grid3.setColumnStretch(1, 1)
  209. separator_line = QtWidgets.QFrame()
  210. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  211. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  212. grid3.addWidget(separator_line, 1, 0, 1, 2)
  213. # Annular Ring value
  214. self.prop_label = QtWidgets.QLabel('<b>%s</b>' % _("Proportional Diameter"))
  215. grid3.addWidget(self.prop_label, 2, 0, 1, 2)
  216. # Diameter value
  217. self.factor_entry = FCDoubleSpinner(callback=self.confirmation_message, suffix='%')
  218. self.factor_entry.set_precision(self.decimals)
  219. self.factor_entry.set_range(0.0000, 100.0000)
  220. self.factor_entry.setSingleStep(0.1)
  221. self.factor_label = QtWidgets.QLabel('%s:' % _("Value"))
  222. self.factor_label.setToolTip(
  223. _("Proportional Diameter.\n"
  224. "The hole diameter will be a fraction of the pad size.")
  225. )
  226. grid3.addWidget(self.factor_label, 3, 0)
  227. grid3.addWidget(self.factor_entry, 3, 1)
  228. # Extract drills from Gerber apertures flashes (pads)
  229. self.e_drills_button = QtWidgets.QPushButton(_("Extract Drills"))
  230. self.e_drills_button.setToolTip(
  231. _("Extract drills from a given Gerber file.")
  232. )
  233. self.e_drills_button.setStyleSheet("""
  234. QPushButton
  235. {
  236. font-weight: bold;
  237. }
  238. """)
  239. self.layout.addWidget(self.e_drills_button)
  240. self.layout.addStretch()
  241. # ## Reset Tool
  242. self.reset_button = QtWidgets.QPushButton(_("Reset Tool"))
  243. self.reset_button.setToolTip(
  244. _("Will reset the tool parameters.")
  245. )
  246. self.reset_button.setStyleSheet("""
  247. QPushButton
  248. {
  249. font-weight: bold;
  250. }
  251. """)
  252. self.layout.addWidget(self.reset_button)
  253. self.circular_ring_entry.setEnabled(False)
  254. self.oblong_ring_entry.setEnabled(False)
  255. self.square_ring_entry.setEnabled(False)
  256. self.rectangular_ring_entry.setEnabled(False)
  257. self.other_ring_entry.setEnabled(False)
  258. self.dia_entry.setDisabled(True)
  259. self.dia_label.setDisabled(True)
  260. self.factor_label.setDisabled(True)
  261. self.factor_entry.setDisabled(True)
  262. self.ring_frame.setDisabled(True)
  263. # ## Signals
  264. self.hole_size_radio.activated_custom.connect(self.on_hole_size_toggle)
  265. self.e_drills_button.clicked.connect(self.on_extract_drills_click)
  266. self.reset_button.clicked.connect(self.set_tool_ui)
  267. self.circular_cb.stateChanged.connect(
  268. lambda state:
  269. self.circular_ring_entry.setDisabled(False) if state else self.circular_ring_entry.setDisabled(True)
  270. )
  271. self.oblong_cb.stateChanged.connect(
  272. lambda state:
  273. self.oblong_ring_entry.setDisabled(False) if state else self.oblong_ring_entry.setDisabled(True)
  274. )
  275. self.square_cb.stateChanged.connect(
  276. lambda state:
  277. self.square_ring_entry.setDisabled(False) if state else self.square_ring_entry.setDisabled(True)
  278. )
  279. self.rectangular_cb.stateChanged.connect(
  280. lambda state:
  281. self.rectangular_ring_entry.setDisabled(False) if state else self.rectangular_ring_entry.setDisabled(True)
  282. )
  283. self.other_cb.stateChanged.connect(
  284. lambda state:
  285. self.other_ring_entry.setDisabled(False) if state else self.other_ring_entry.setDisabled(True)
  286. )
  287. def install(self, icon=None, separator=None, **kwargs):
  288. AppTool.install(self, icon, separator, shortcut='Alt+I', **kwargs)
  289. def run(self, toggle=True):
  290. self.app.defaults.report_usage("Extract Drills()")
  291. if toggle:
  292. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  293. if self.app.ui.splitter.sizes()[0] == 0:
  294. self.app.ui.splitter.setSizes([1, 1])
  295. else:
  296. try:
  297. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  298. # if tab is populated with the tool but it does not have the focus, focus on it
  299. if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
  300. # focus on Tool Tab
  301. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  302. else:
  303. self.app.ui.splitter.setSizes([0, 1])
  304. except AttributeError:
  305. pass
  306. else:
  307. if self.app.ui.splitter.sizes()[0] == 0:
  308. self.app.ui.splitter.setSizes([1, 1])
  309. AppTool.run(self)
  310. self.set_tool_ui()
  311. self.app.ui.notebook.setTabText(2, _("Extract Drills Tool"))
  312. def set_tool_ui(self):
  313. self.reset_fields()
  314. self.hole_size_radio.set_value(self.app.defaults["tools_edrills_hole_type"])
  315. self.dia_entry.set_value(float(self.app.defaults["tools_edrills_hole_fixed_dia"]))
  316. self.circular_ring_entry.set_value(float(self.app.defaults["tools_edrills_circular_ring"]))
  317. self.oblong_ring_entry.set_value(float(self.app.defaults["tools_edrills_oblong_ring"]))
  318. self.square_ring_entry.set_value(float(self.app.defaults["tools_edrills_square_ring"]))
  319. self.rectangular_ring_entry.set_value(float(self.app.defaults["tools_edrills_rectangular_ring"]))
  320. self.other_ring_entry.set_value(float(self.app.defaults["tools_edrills_others_ring"]))
  321. self.circular_cb.set_value(self.app.defaults["tools_edrills_circular"])
  322. self.oblong_cb.set_value(self.app.defaults["tools_edrills_oblong"])
  323. self.square_cb.set_value(self.app.defaults["tools_edrills_square"])
  324. self.rectangular_cb.set_value(self.app.defaults["tools_edrills_rectangular"])
  325. self.other_cb.set_value(self.app.defaults["tools_edrills_others"])
  326. self.factor_entry.set_value(float(self.app.defaults["tools_edrills_hole_prop_factor"]))
  327. def on_extract_drills_click(self):
  328. drill_dia = self.dia_entry.get_value()
  329. circ_r_val = self.circular_ring_entry.get_value()
  330. oblong_r_val = self.oblong_ring_entry.get_value()
  331. square_r_val = self.square_ring_entry.get_value()
  332. rect_r_val = self.rectangular_ring_entry.get_value()
  333. other_r_val = self.other_ring_entry.get_value()
  334. prop_factor = self.factor_entry.get_value() / 100.0
  335. drills = []
  336. tools = {}
  337. selection_index = self.gerber_object_combo.currentIndex()
  338. model_index = self.app.collection.index(selection_index, 0, self.gerber_object_combo.rootModelIndex())
  339. try:
  340. fcobj = model_index.internalPointer().obj
  341. except Exception:
  342. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Gerber object loaded ..."))
  343. return
  344. outname = fcobj.options['name'].rpartition('.')[0]
  345. mode = self.hole_size_radio.get_value()
  346. if mode == 'fixed':
  347. tools = {
  348. 1: {
  349. "tooldia": drill_dia,
  350. "drills": [],
  351. "slots": []
  352. }
  353. }
  354. for apid, apid_value in fcobj.apertures.items():
  355. ap_type = apid_value['type']
  356. if ap_type == 'C':
  357. if self.circular_cb.get_value() is False:
  358. continue
  359. elif ap_type == 'O':
  360. if self.oblong_cb.get_value() is False:
  361. continue
  362. elif ap_type == 'R':
  363. width = float(apid_value['width'])
  364. height = float(apid_value['height'])
  365. # if the height == width (float numbers so the reason for the following)
  366. if round(width, self.decimals) == round(height, self.decimals):
  367. if self.square_cb.get_value() is False:
  368. continue
  369. else:
  370. if self.rectangular_cb.get_value() is False:
  371. continue
  372. else:
  373. if self.other_cb.get_value() is False:
  374. continue
  375. for geo_el in apid_value['geometry']:
  376. if 'follow' in geo_el and isinstance(geo_el['follow'], Point):
  377. tools[1]["drills"].append(geo_el['follow'])
  378. if 'solid_geometry' not in tools[1]:
  379. tools[1]['solid_geometry'] = []
  380. else:
  381. tools[1]['solid_geometry'].append(geo_el['follow'])
  382. if 'solid_geometry' not in tools[1] or not tools[1]['solid_geometry']:
  383. self.app.inform.emit('[WARNING_NOTCL] %s' % _("No drills extracted. Try different parameters."))
  384. return
  385. elif mode == 'ring':
  386. drills_found = set()
  387. for apid, apid_value in fcobj.apertures.items():
  388. ap_type = apid_value['type']
  389. dia = None
  390. if ap_type == 'C':
  391. if self.circular_cb.get_value():
  392. dia = float(apid_value['size']) - (2 * circ_r_val)
  393. elif ap_type == 'O':
  394. width = float(apid_value['width'])
  395. height = float(apid_value['height'])
  396. if self.oblong_cb.get_value():
  397. if width > height:
  398. dia = float(apid_value['height']) - (2 * oblong_r_val)
  399. else:
  400. dia = float(apid_value['width']) - (2 * oblong_r_val)
  401. elif ap_type == 'R':
  402. width = float(apid_value['width'])
  403. height = float(apid_value['height'])
  404. # if the height == width (float numbers so the reason for the following)
  405. if abs(float('%.*f' % (self.decimals, width)) - float('%.*f' % (self.decimals, height))) < \
  406. (10 ** -self.decimals):
  407. if self.square_cb.get_value():
  408. dia = float(apid_value['height']) - (2 * square_r_val)
  409. else:
  410. if self.rectangular_cb.get_value():
  411. if width > height:
  412. dia = float(apid_value['height']) - (2 * rect_r_val)
  413. else:
  414. dia = float(apid_value['width']) - (2 * rect_r_val)
  415. else:
  416. if self.other_cb.get_value():
  417. try:
  418. dia = float(apid_value['size']) - (2 * other_r_val)
  419. except KeyError:
  420. if ap_type == 'AM':
  421. pol = apid_value['geometry'][0]['solid']
  422. x0, y0, x1, y1 = pol.bounds
  423. dx = x1 - x0
  424. dy = y1 - y0
  425. if dx <= dy:
  426. dia = dx - (2 * other_r_val)
  427. else:
  428. dia = dy - (2 * other_r_val)
  429. # if dia is None then none of the above applied so we skip the following
  430. if dia is None:
  431. continue
  432. tool_in_drills = False
  433. for tool, tool_val in tools.items():
  434. if abs(float('%.*f' % (
  435. self.decimals,
  436. tool_val["tooldia"])) - float('%.*f' % (self.decimals, dia))) < (10 ** -self.decimals):
  437. tool_in_drills = tool
  438. if tool_in_drills is False:
  439. if tools:
  440. new_tool = max([int(t) for t in tools]) + 1
  441. tool_in_drills = new_tool
  442. else:
  443. tool_in_drills = 1
  444. for geo_el in apid_value['geometry']:
  445. if 'follow' in geo_el and isinstance(geo_el['follow'], Point):
  446. if tool_in_drills not in tools:
  447. tools[tool_in_drills] = {
  448. "tooldia": dia,
  449. "drills": [],
  450. "slots": []
  451. }
  452. tools[tool_in_drills]['drills'].append(geo_el['follow'])
  453. if 'solid_geometry' not in tools[tool_in_drills]:
  454. tools[tool_in_drills]['solid_geometry'] = []
  455. else:
  456. tools[tool_in_drills]['solid_geometry'].append(geo_el['follow'])
  457. if tool_in_drills in tools:
  458. if 'solid_geometry' not in tools[tool_in_drills] or not tools[tool_in_drills]['solid_geometry']:
  459. drills_found.add(False)
  460. else:
  461. drills_found.add(True)
  462. if True not in drills_found:
  463. self.app.inform.emit('[WARNING_NOTCL] %s' % _("No drills extracted. Try different parameters."))
  464. return
  465. else:
  466. drills_found = set()
  467. for apid, apid_value in fcobj.apertures.items():
  468. ap_type = apid_value['type']
  469. dia = None
  470. if ap_type == 'C':
  471. if self.circular_cb.get_value():
  472. dia = float(apid_value['size']) * prop_factor
  473. elif ap_type == 'O':
  474. width = float(apid_value['width'])
  475. height = float(apid_value['height'])
  476. if self.oblong_cb.get_value():
  477. if width > height:
  478. dia = float(apid_value['height']) * prop_factor
  479. else:
  480. dia = float(apid_value['width']) * prop_factor
  481. elif ap_type == 'R':
  482. width = float(apid_value['width'])
  483. height = float(apid_value['height'])
  484. # if the height == width (float numbers so the reason for the following)
  485. if abs(float('%.*f' % (self.decimals, width)) - float('%.*f' % (self.decimals, height))) < \
  486. (10 ** -self.decimals):
  487. if self.square_cb.get_value():
  488. dia = float(apid_value['height']) * prop_factor
  489. else:
  490. if self.rectangular_cb.get_value():
  491. if width > height:
  492. dia = float(apid_value['height']) * prop_factor
  493. else:
  494. dia = float(apid_value['width']) * prop_factor
  495. else:
  496. if self.other_cb.get_value():
  497. try:
  498. dia = float(apid_value['size']) * prop_factor
  499. except KeyError:
  500. if ap_type == 'AM':
  501. pol = apid_value['geometry'][0]['solid']
  502. x0, y0, x1, y1 = pol.bounds
  503. dx = x1 - x0
  504. dy = y1 - y0
  505. if dx <= dy:
  506. dia = dx * prop_factor
  507. else:
  508. dia = dy * prop_factor
  509. # if dia is None then none of the above applied so we skip the following
  510. if dia is None:
  511. continue
  512. tool_in_drills = False
  513. for tool, tool_val in tools.items():
  514. if abs(float('%.*f' % (
  515. self.decimals,
  516. tool_val["tooldia"])) - float('%.*f' % (self.decimals, dia))) < (10 ** -self.decimals):
  517. tool_in_drills = tool
  518. if tool_in_drills is False:
  519. if tools:
  520. new_tool = max([int(t) for t in tools]) + 1
  521. tool_in_drills = new_tool
  522. else:
  523. tool_in_drills = 1
  524. for geo_el in apid_value['geometry']:
  525. if 'follow' in geo_el and isinstance(geo_el['follow'], Point):
  526. if tool_in_drills not in tools:
  527. tools[tool_in_drills] = {
  528. "tooldia": dia,
  529. "drills": [],
  530. "slots": []
  531. }
  532. tools[tool_in_drills]['drills'].append(geo_el['follow'])
  533. if 'solid_geometry' not in tools[tool_in_drills]:
  534. tools[tool_in_drills]['solid_geometry'] = []
  535. else:
  536. tools[tool_in_drills]['solid_geometry'].append(geo_el['follow'])
  537. if tool_in_drills in tools:
  538. if 'solid_geometry' not in tools[tool_in_drills] or not tools[tool_in_drills]['solid_geometry']:
  539. drills_found.add(False)
  540. else:
  541. drills_found.add(True)
  542. if True not in drills_found:
  543. self.app.inform.emit('[WARNING_NOTCL] %s' % _("No drills extracted. Try different parameters."))
  544. return
  545. def obj_init(obj_inst, app_inst):
  546. obj_inst.tools = tools
  547. obj_inst.drills = drills
  548. obj_inst.create_geometry()
  549. obj_inst.source_file = self.app.export_excellon(obj_name=outname, local_use=obj_inst, filename=None,
  550. use_thread=False)
  551. self.app.app_obj.new_object("excellon", outname, obj_init)
  552. def on_hole_size_toggle(self, val):
  553. if val == "fixed":
  554. self.fixed_label.setDisabled(False)
  555. self.dia_entry.setDisabled(False)
  556. self.dia_label.setDisabled(False)
  557. self.ring_frame.setDisabled(True)
  558. self.prop_label.setDisabled(True)
  559. self.factor_label.setDisabled(True)
  560. self.factor_entry.setDisabled(True)
  561. elif val == "ring":
  562. self.fixed_label.setDisabled(True)
  563. self.dia_entry.setDisabled(True)
  564. self.dia_label.setDisabled(True)
  565. self.ring_frame.setDisabled(False)
  566. self.prop_label.setDisabled(True)
  567. self.factor_label.setDisabled(True)
  568. self.factor_entry.setDisabled(True)
  569. elif val == "prop":
  570. self.fixed_label.setDisabled(True)
  571. self.dia_entry.setDisabled(True)
  572. self.dia_label.setDisabled(True)
  573. self.ring_frame.setDisabled(True)
  574. self.prop_label.setDisabled(False)
  575. self.factor_label.setDisabled(False)
  576. self.factor_entry.setDisabled(False)
  577. def reset_fields(self):
  578. self.gerber_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  579. self.gerber_object_combo.setCurrentIndex(0)