ToolExtractDrills.py 28 KB

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