ToolExtractDrills.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. from PyQt5 import QtWidgets, QtCore
  2. from FlatCAMTool import FlatCAMTool
  3. from flatcamGUI.GUIElements import RadioSet, FCDoubleSpinner, EvalEntry, FCEntry
  4. from FlatCAMObj import FlatCAMGerber, FlatCAMExcellon, FlatCAMGeometry
  5. from numpy import Inf
  6. from shapely.geometry import Point
  7. from shapely import affinity
  8. import logging
  9. import gettext
  10. import FlatCAMTranslation as fcTranslate
  11. import builtins
  12. fcTranslate.apply_language('strings')
  13. if '_' not in builtins.__dict__:
  14. _ = gettext.gettext
  15. log = logging.getLogger('base')
  16. class ToolExtractDrills(FlatCAMTool):
  17. toolName = _("Extract Drills")
  18. def __init__(self, app):
  19. FlatCAMTool.__init__(self, app)
  20. self.decimals = self.app.decimals
  21. # ## Title
  22. title_label = QtWidgets.QLabel("%s" % self.toolName)
  23. title_label.setStyleSheet("""
  24. QLabel
  25. {
  26. font-size: 16px;
  27. font-weight: bold;
  28. }
  29. """)
  30. self.layout.addWidget(title_label)
  31. self.empty_lb = QtWidgets.QLabel("")
  32. self.layout.addWidget(self.empty_lb)
  33. # ## Grid Layout
  34. grid_lay = QtWidgets.QGridLayout()
  35. self.layout.addLayout(grid_lay)
  36. grid_lay.setColumnStretch(0, 1)
  37. grid_lay.setColumnStretch(1, 0)
  38. # ## Gerber Object
  39. self.gerber_object_combo = QtWidgets.QComboBox()
  40. self.gerber_object_combo.setModel(self.app.collection)
  41. self.gerber_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  42. self.gerber_object_combo.setCurrentIndex(1)
  43. self.grb_label = QtWidgets.QLabel("<b>%s:</b>" % _("GERBER"))
  44. self.grb_label.setToolTip('%s.' % _("Gerber from which to extract drill holes"))
  45. self.mirror_gerber_button.setStyleSheet("""
  46. QPushButton
  47. {
  48. font-weight: bold;
  49. }
  50. """)
  51. self.mirror_gerber_button.setMinimumWidth(60)
  52. # grid_lay.addRow("Bottom Layer:", self.object_combo)
  53. grid_lay.addWidget(self.grb_label, 0, 0)
  54. grid_lay.addWidget(self.gerber_object_combo, 1, 0)
  55. # ## Grid Layout
  56. grid_lay1 = QtWidgets.QGridLayout()
  57. self.layout.addLayout(grid_lay1)
  58. # ## Axis
  59. self.hole_size_radio = RadioSet([{'label': _("Fixed"), 'value': 'fixed'},
  60. {'label': _("Proportional"), 'value': 'prop'}])
  61. self.hole_size_label = QtWidgets.QLabel('%s:' % _("Hole Size"))
  62. self.hole_size_label.setToolTip(
  63. _("The type of hole size. Can be:\n"
  64. "- Fixed -> all holes will have a set size\n"
  65. "- Proprotional -> each hole will havea a variable size\n"
  66. "such as to preserve a set annular ring"))
  67. grid_lay1.addWidget(self.hole_size_label, 3, 0)
  68. grid_lay1.addWidget(self.hole_size_radio, 3, 1)
  69. self.layout.addWidget(QtWidgets.QLabel(''))
  70. separator_line = QtWidgets.QFrame()
  71. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  72. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  73. self.layout.addWidget(separator_line)
  74. grid1 = QtWidgets.QGridLayout()
  75. self.layout.addLayout(grid1)
  76. grid1.setColumnStretch(0, 0)
  77. grid1.setColumnStretch(1, 1)
  78. # Diameter value
  79. self.dia_entry = FCDoubleSpinner()
  80. self.dia_entry.set_precision(self.decimals)
  81. self.dia_entry.set_range(0.0000, 9999.9999)
  82. self.dia_label = QtWidgets.QLabel('%s:' % _("Diameter"))
  83. self.dia_label.setToolTip(
  84. _("Fixed hole diameter.")
  85. )
  86. grid1.addWidget(self.dia_label, 1, 0)
  87. grid1.addWidget(self.dia_entry, 1, 1)
  88. # Annular Ring value
  89. self.ring_entry = FCDoubleSpinner()
  90. self.ring_entry.set_precision(self.decimals)
  91. self.ring_entry.set_range(0.0000, 9999.9999)
  92. self.ring_label = QtWidgets.QLabel('%s:' % _("Annular Ring"))
  93. self.ring_label.setToolTip(
  94. _("The size of annular ring.\n"
  95. "The copper sliver between the drill hole exterior\n"
  96. "and the margin of the copper pad.")
  97. )
  98. grid1.addWidget(self.ring_label, 2, 0)
  99. grid1.addWidget(self.ring_entry, 2, 1)
  100. # Calculate Bounding box
  101. self.e_drills_button = QtWidgets.QPushButton(_("Extract Drills"))
  102. self.e_drills_button.setToolTip(
  103. _("Extract drills from a given Gerber file.")
  104. )
  105. self.e_drills_button.setStyleSheet("""
  106. QPushButton
  107. {
  108. font-weight: bold;
  109. }
  110. """)
  111. self.layout.addWidget(self.e_drills_button)
  112. self.layout.addStretch()
  113. # ## Reset Tool
  114. self.reset_button = QtWidgets.QPushButton(_("Reset Tool"))
  115. self.reset_button.setToolTip(
  116. _("Will reset the tool parameters.")
  117. )
  118. self.reset_button.setStyleSheet("""
  119. QPushButton
  120. {
  121. font-weight: bold;
  122. }
  123. """)
  124. self.layout.addWidget(self.reset_button)
  125. # ## Signals
  126. self.hole_size_radio.activated_custom(self.on_hole_size_toggle)
  127. self.e_drills_button.clicked.connect(self.on_extract_drills_click)
  128. self.reset_button.clicked.connect(self.set_tool_ui)
  129. self.tools = list()
  130. self.drills = dict()
  131. def install(self, icon=None, separator=None, **kwargs):
  132. FlatCAMTool.install(self, icon, separator, shortcut='ALT+D', **kwargs)
  133. def run(self, toggle=True):
  134. self.app.report_usage("Extract Drills()")
  135. if toggle:
  136. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  137. if self.app.ui.splitter.sizes()[0] == 0:
  138. self.app.ui.splitter.setSizes([1, 1])
  139. else:
  140. try:
  141. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  142. # if tab is populated with the tool but it does not have the focus, focus on it
  143. if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
  144. # focus on Tool Tab
  145. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  146. else:
  147. self.app.ui.splitter.setSizes([0, 1])
  148. except AttributeError:
  149. pass
  150. else:
  151. if self.app.ui.splitter.sizes()[0] == 0:
  152. self.app.ui.splitter.setSizes([1, 1])
  153. FlatCAMTool.run(self)
  154. self.set_tool_ui()
  155. self.app.ui.notebook.setTabText(2, _("Extract Drills Tool"))
  156. def set_tool_ui(self):
  157. self.reset_fields()
  158. self.hole_size_radio.set_value(self.app.defaults["tools_edrills_hole_type"])
  159. self.dia_entry.set_value(float(self.app.defaults["tools_edrills_hole_fixed_dia"]))
  160. self.ring_entry.set_value(float(self.app.defaults["tools_edrills_hole_ring"]))
  161. def on_extract_drills_click(self):
  162. selection_index = self.gerber_object_combo.currentIndex()
  163. # fcobj = self.app.collection.object_list[selection_index]
  164. model_index = self.app.collection.index(selection_index, 0, self.gerber_object_combo.rootModelIndex())
  165. try:
  166. fcobj = model_index.internalPointer().obj
  167. except Exception as e:
  168. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Gerber object loaded ..."))
  169. return
  170. if not isinstance(fcobj, FlatCAMGerber):
  171. self.app.inform.emit('[ERROR_NOTCL] %s' % _("Only Gerber, Excellon and Geometry objects can be mirrored."))
  172. return
  173. axis = self.mirror_axis.get_value()
  174. mode = self.axis_location.get_value()
  175. if mode == "point":
  176. try:
  177. px, py = self.point_entry.get_value()
  178. except TypeError:
  179. self.app.inform.emit('[WARNING_NOTCL] %s' % _("'Point' coordinates missing. "
  180. "Using Origin (0, 0) as mirroring reference."))
  181. px, py = (0, 0)
  182. else:
  183. selection_index_box = self.box_combo.currentIndex()
  184. model_index_box = self.app.collection.index(selection_index_box, 0, self.box_combo.rootModelIndex())
  185. try:
  186. bb_obj = model_index_box.internalPointer().obj
  187. except Exception as e:
  188. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Box object loaded ..."))
  189. return
  190. xmin, ymin, xmax, ymax = bb_obj.bounds()
  191. px = 0.5 * (xmin + xmax)
  192. py = 0.5 * (ymin + ymax)
  193. fcobj.mirror(axis, [px, py])
  194. self.app.object_changed.emit(fcobj)
  195. fcobj.plot()
  196. self.app.inform.emit('[success] Gerber %s %s...' % (str(fcobj.options['name']), _("was mirrored")))
  197. def on_hole_size_toggle(self, val):
  198. if val == "fixed":
  199. self.dia_entry.show()
  200. self.dia_label.show()
  201. self.ring_label.hide()
  202. self.ring_entry.hide()
  203. else:
  204. self.dia_entry.hide()
  205. self.dia_label.hide()
  206. self.ring_label.show()
  207. self.ring_entry.show()
  208. def reset_fields(self):
  209. self.gerber_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  210. self.gerber_object_combo.setCurrentIndex(0)