ToolExtractDrills.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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 = {"1": {"C": drill_dia}}
  348. for apid, apid_value in fcobj.apertures.items():
  349. ap_type = apid_value['type']
  350. if ap_type == 'C':
  351. if self.circular_cb.get_value() is False:
  352. continue
  353. elif ap_type == 'O':
  354. if self.oblong_cb.get_value() is False:
  355. continue
  356. elif ap_type == 'R':
  357. width = float(apid_value['width'])
  358. height = float(apid_value['height'])
  359. # if the height == width (float numbers so the reason for the following)
  360. if round(width, self.decimals) == round(height, self.decimals):
  361. if self.square_cb.get_value() is False:
  362. continue
  363. else:
  364. if self.rectangular_cb.get_value() is False:
  365. continue
  366. else:
  367. if self.other_cb.get_value() is False:
  368. continue
  369. for geo_el in apid_value['geometry']:
  370. if 'follow' in geo_el and isinstance(geo_el['follow'], Point):
  371. drills.append({"point": geo_el['follow'], "tool": "1"})
  372. if 'solid_geometry' not in tools["1"]:
  373. tools["1"]['solid_geometry'] = []
  374. else:
  375. tools["1"]['solid_geometry'].append(geo_el['follow'])
  376. if 'solid_geometry' not in tools["1"] or not tools["1"]['solid_geometry']:
  377. self.app.inform.emit('[WARNING_NOTCL] %s' % _("No drills extracted. Try different parameters."))
  378. return
  379. elif mode == 'ring':
  380. drills_found = set()
  381. for apid, apid_value in fcobj.apertures.items():
  382. ap_type = apid_value['type']
  383. dia = None
  384. if ap_type == 'C':
  385. if self.circular_cb.get_value():
  386. dia = float(apid_value['size']) - (2 * circ_r_val)
  387. elif ap_type == 'O':
  388. width = float(apid_value['width'])
  389. height = float(apid_value['height'])
  390. if self.oblong_cb.get_value():
  391. if width > height:
  392. dia = float(apid_value['height']) - (2 * oblong_r_val)
  393. else:
  394. dia = float(apid_value['width']) - (2 * oblong_r_val)
  395. elif ap_type == 'R':
  396. width = float(apid_value['width'])
  397. height = float(apid_value['height'])
  398. # if the height == width (float numbers so the reason for the following)
  399. if abs(float('%.*f' % (self.decimals, width)) - float('%.*f' % (self.decimals, height))) < \
  400. (10 ** -self.decimals):
  401. if self.square_cb.get_value():
  402. dia = float(apid_value['height']) - (2 * square_r_val)
  403. else:
  404. if self.rectangular_cb.get_value():
  405. if width > height:
  406. dia = float(apid_value['height']) - (2 * rect_r_val)
  407. else:
  408. dia = float(apid_value['width']) - (2 * rect_r_val)
  409. else:
  410. if self.other_cb.get_value():
  411. try:
  412. dia = float(apid_value['size']) - (2 * other_r_val)
  413. except KeyError:
  414. if ap_type == 'AM':
  415. pol = apid_value['geometry'][0]['solid']
  416. x0, y0, x1, y1 = pol.bounds
  417. dx = x1 - x0
  418. dy = y1 - y0
  419. if dx <= dy:
  420. dia = dx - (2 * other_r_val)
  421. else:
  422. dia = dy - (2 * other_r_val)
  423. # if dia is None then none of the above applied so we skip the following
  424. if dia is None:
  425. continue
  426. tool_in_drills = False
  427. for tool, tool_val in tools.items():
  428. if abs(float('%.*f' % (self.decimals, tool_val["C"])) - float('%.*f' % (self.decimals, dia))) < \
  429. (10 ** -self.decimals):
  430. tool_in_drills = tool
  431. if tool_in_drills is False:
  432. if tools:
  433. new_tool = max([int(t) for t in tools]) + 1
  434. tool_in_drills = str(new_tool)
  435. else:
  436. tool_in_drills = "1"
  437. for geo_el in apid_value['geometry']:
  438. if 'follow' in geo_el and isinstance(geo_el['follow'], Point):
  439. if tool_in_drills not in tools:
  440. tools[tool_in_drills] = {"C": dia}
  441. drills.append({"point": geo_el['follow'], "tool": tool_in_drills})
  442. if 'solid_geometry' not in tools[tool_in_drills]:
  443. tools[tool_in_drills]['solid_geometry'] = []
  444. else:
  445. tools[tool_in_drills]['solid_geometry'].append(geo_el['follow'])
  446. if tool_in_drills in tools:
  447. if 'solid_geometry' not in tools[tool_in_drills] or not tools[tool_in_drills]['solid_geometry']:
  448. drills_found.add(False)
  449. else:
  450. drills_found.add(True)
  451. if True not in drills_found:
  452. self.app.inform.emit('[WARNING_NOTCL] %s' % _("No drills extracted. Try different parameters."))
  453. return
  454. else:
  455. drills_found = set()
  456. for apid, apid_value in fcobj.apertures.items():
  457. ap_type = apid_value['type']
  458. dia = None
  459. if ap_type == 'C':
  460. if self.circular_cb.get_value():
  461. dia = float(apid_value['size']) * prop_factor
  462. elif ap_type == 'O':
  463. width = float(apid_value['width'])
  464. height = float(apid_value['height'])
  465. if self.oblong_cb.get_value():
  466. if width > height:
  467. dia = float(apid_value['height']) * prop_factor
  468. else:
  469. dia = float(apid_value['width']) * prop_factor
  470. elif ap_type == 'R':
  471. width = float(apid_value['width'])
  472. height = float(apid_value['height'])
  473. # if the height == width (float numbers so the reason for the following)
  474. if abs(float('%.*f' % (self.decimals, width)) - float('%.*f' % (self.decimals, height))) < \
  475. (10 ** -self.decimals):
  476. if self.square_cb.get_value():
  477. dia = float(apid_value['height']) * prop_factor
  478. else:
  479. if self.rectangular_cb.get_value():
  480. if width > height:
  481. dia = float(apid_value['height']) * prop_factor
  482. else:
  483. dia = float(apid_value['width']) * prop_factor
  484. else:
  485. if self.other_cb.get_value():
  486. try:
  487. dia = float(apid_value['size']) * prop_factor
  488. except KeyError:
  489. if ap_type == 'AM':
  490. pol = apid_value['geometry'][0]['solid']
  491. x0, y0, x1, y1 = pol.bounds
  492. dx = x1 - x0
  493. dy = y1 - y0
  494. if dx <= dy:
  495. dia = dx * prop_factor
  496. else:
  497. dia = dy * prop_factor
  498. # if dia is None then none of the above applied so we skip the following
  499. if dia is None:
  500. continue
  501. tool_in_drills = False
  502. for tool, tool_val in tools.items():
  503. if abs(float('%.*f' % (self.decimals, tool_val["C"])) - float('%.*f' % (self.decimals, dia))) < \
  504. (10 ** -self.decimals):
  505. tool_in_drills = tool
  506. if tool_in_drills is False:
  507. if tools:
  508. new_tool = max([int(t) for t in tools]) + 1
  509. tool_in_drills = str(new_tool)
  510. else:
  511. tool_in_drills = "1"
  512. for geo_el in apid_value['geometry']:
  513. if 'follow' in geo_el and isinstance(geo_el['follow'], Point):
  514. if tool_in_drills not in tools:
  515. tools[tool_in_drills] = {"C": dia}
  516. drills.append({"point": geo_el['follow'], "tool": tool_in_drills})
  517. if 'solid_geometry' not in tools[tool_in_drills]:
  518. tools[tool_in_drills]['solid_geometry'] = []
  519. else:
  520. tools[tool_in_drills]['solid_geometry'].append(geo_el['follow'])
  521. if tool_in_drills in tools:
  522. if 'solid_geometry' not in tools[tool_in_drills] or not tools[tool_in_drills]['solid_geometry']:
  523. drills_found.add(False)
  524. else:
  525. drills_found.add(True)
  526. if True not in drills_found:
  527. self.app.inform.emit('[WARNING_NOTCL] %s' % _("No drills extracted. Try different parameters."))
  528. return
  529. def obj_init(obj_inst, app_inst):
  530. obj_inst.tools = tools
  531. obj_inst.drills = drills
  532. obj_inst.create_geometry()
  533. obj_inst.source_file = self.app.export_excellon(obj_name=outname, local_use=obj_inst, filename=None,
  534. use_thread=False)
  535. self.app.app_obj.new_object("excellon", outname, obj_init)
  536. def on_hole_size_toggle(self, val):
  537. if val == "fixed":
  538. self.fixed_label.setDisabled(False)
  539. self.dia_entry.setDisabled(False)
  540. self.dia_label.setDisabled(False)
  541. self.ring_frame.setDisabled(True)
  542. self.prop_label.setDisabled(True)
  543. self.factor_label.setDisabled(True)
  544. self.factor_entry.setDisabled(True)
  545. elif val == "ring":
  546. self.fixed_label.setDisabled(True)
  547. self.dia_entry.setDisabled(True)
  548. self.dia_label.setDisabled(True)
  549. self.ring_frame.setDisabled(False)
  550. self.prop_label.setDisabled(True)
  551. self.factor_label.setDisabled(True)
  552. self.factor_entry.setDisabled(True)
  553. elif val == "prop":
  554. self.fixed_label.setDisabled(True)
  555. self.dia_entry.setDisabled(True)
  556. self.dia_label.setDisabled(True)
  557. self.ring_frame.setDisabled(True)
  558. self.prop_label.setDisabled(False)
  559. self.factor_label.setDisabled(False)
  560. self.factor_entry.setDisabled(False)
  561. def reset_fields(self):
  562. self.gerber_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  563. self.gerber_object_combo.setCurrentIndex(0)