ToolExtractDrills.py 28 KB

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