ToolExtractDrills.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. # grid_lay.addRow("Bottom Layer:", self.object_combo)
  46. grid_lay.addWidget(self.grb_label, 0, 0)
  47. grid_lay.addWidget(self.gerber_object_combo, 1, 0)
  48. # ## Grid Layout
  49. grid1 = QtWidgets.QGridLayout()
  50. self.layout.addLayout(grid1)
  51. grid1.setColumnStretch(0, 0)
  52. grid1.setColumnStretch(1, 1)
  53. # ## Axis
  54. self.hole_size_radio = RadioSet([{'label': _("Fixed"), 'value': 'fixed'},
  55. {'label': _("Proportional"), 'value': 'prop'}])
  56. self.hole_size_label = QtWidgets.QLabel('%s:' % _("Hole Size"))
  57. self.hole_size_label.setToolTip(
  58. _("The type of hole size. Can be:\n"
  59. "- Fixed -> all holes will have a set size\n"
  60. "- Proprotional -> each hole will havea a variable size\n"
  61. "such as to preserve a set annular ring"))
  62. grid1.addWidget(self.hole_size_label, 3, 0)
  63. grid1.addWidget(self.hole_size_radio, 3, 1)
  64. # grid_lay1.addWidget(QtWidgets.QLabel(''))
  65. separator_line = QtWidgets.QFrame()
  66. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  67. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  68. grid1.addWidget(separator_line, 5, 0, 1, 2)
  69. # Diameter value
  70. self.dia_entry = FCDoubleSpinner()
  71. self.dia_entry.set_precision(self.decimals)
  72. self.dia_entry.set_range(0.0000, 9999.9999)
  73. self.dia_label = QtWidgets.QLabel('%s:' % _("Diameter"))
  74. self.dia_label.setToolTip(
  75. _("Fixed hole diameter.")
  76. )
  77. grid1.addWidget(self.dia_label, 7, 0)
  78. grid1.addWidget(self.dia_entry, 7, 1)
  79. # Annular Ring value
  80. self.ring_entry = FCDoubleSpinner()
  81. self.ring_entry.set_precision(self.decimals)
  82. self.ring_entry.set_range(0.0000, 9999.9999)
  83. self.ring_label = QtWidgets.QLabel('%s:' % _("Annular Ring"))
  84. self.ring_label.setToolTip(
  85. _("The size of annular ring.\n"
  86. "The copper sliver between the drill hole exterior\n"
  87. "and the margin of the copper pad.")
  88. )
  89. grid1.addWidget(self.ring_label, 8, 0)
  90. grid1.addWidget(self.ring_entry, 8, 1)
  91. # Calculate Bounding box
  92. self.e_drills_button = QtWidgets.QPushButton(_("Extract Drills"))
  93. self.e_drills_button.setToolTip(
  94. _("Extract drills from a given Gerber file.")
  95. )
  96. self.e_drills_button.setStyleSheet("""
  97. QPushButton
  98. {
  99. font-weight: bold;
  100. }
  101. """)
  102. self.layout.addWidget(self.e_drills_button)
  103. self.layout.addStretch()
  104. # ## Reset Tool
  105. self.reset_button = QtWidgets.QPushButton(_("Reset Tool"))
  106. self.reset_button.setToolTip(
  107. _("Will reset the tool parameters.")
  108. )
  109. self.reset_button.setStyleSheet("""
  110. QPushButton
  111. {
  112. font-weight: bold;
  113. }
  114. """)
  115. self.layout.addWidget(self.reset_button)
  116. # ## Signals
  117. self.hole_size_radio.activated_custom.connect(self.on_hole_size_toggle)
  118. self.e_drills_button.clicked.connect(self.on_extract_drills_click)
  119. self.reset_button.clicked.connect(self.set_tool_ui)
  120. self.tools = list()
  121. self.drills = dict()
  122. def install(self, icon=None, separator=None, **kwargs):
  123. FlatCAMTool.install(self, icon, separator, shortcut='ALT+E', **kwargs)
  124. def run(self, toggle=True):
  125. self.app.report_usage("Extract Drills()")
  126. if toggle:
  127. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  128. if self.app.ui.splitter.sizes()[0] == 0:
  129. self.app.ui.splitter.setSizes([1, 1])
  130. else:
  131. try:
  132. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  133. # if tab is populated with the tool but it does not have the focus, focus on it
  134. if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
  135. # focus on Tool Tab
  136. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  137. else:
  138. self.app.ui.splitter.setSizes([0, 1])
  139. except AttributeError:
  140. pass
  141. else:
  142. if self.app.ui.splitter.sizes()[0] == 0:
  143. self.app.ui.splitter.setSizes([1, 1])
  144. FlatCAMTool.run(self)
  145. self.set_tool_ui()
  146. self.app.ui.notebook.setTabText(2, _("Extract Drills Tool"))
  147. def set_tool_ui(self):
  148. self.reset_fields()
  149. self.hole_size_radio.set_value(self.app.defaults["tools_edrills_hole_type"])
  150. self.dia_entry.set_value(float(self.app.defaults["tools_edrills_hole_fixed_dia"]))
  151. self.ring_entry.set_value(float(self.app.defaults["tools_edrills_hole_ring"]))
  152. def on_extract_drills_click(self):
  153. selection_index = self.gerber_object_combo.currentIndex()
  154. model_index = self.app.collection.index(selection_index, 0, self.gerber_object_combo.rootModelIndex())
  155. try:
  156. fcobj = model_index.internalPointer().obj
  157. except Exception as e:
  158. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Gerber object loaded ..."))
  159. return
  160. # axis = self.mirror_axis.get_value()
  161. # mode = self.axis_location.get_value()
  162. #
  163. # if mode == "point":
  164. # try:
  165. # px, py = self.point_entry.get_value()
  166. # except TypeError:
  167. # self.app.inform.emit('[WARNING_NOTCL] %s' % _("'Point' coordinates missing. "
  168. # "Using Origin (0, 0) as mirroring reference."))
  169. # px, py = (0, 0)
  170. #
  171. # else:
  172. # selection_index_box = self.box_combo.currentIndex()
  173. # model_index_box = self.app.collection.index(selection_index_box, 0, self.box_combo.rootModelIndex())
  174. # try:
  175. # bb_obj = model_index_box.internalPointer().obj
  176. # except Exception as e:
  177. # self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Box object loaded ..."))
  178. # return
  179. #
  180. # xmin, ymin, xmax, ymax = bb_obj.bounds()
  181. # px = 0.5 * (xmin + xmax)
  182. # py = 0.5 * (ymin + ymax)
  183. #
  184. # fcobj.mirror(axis, [px, py])
  185. # self.app.object_changed.emit(fcobj)
  186. # fcobj.plot()
  187. self.app.inform.emit('[success] Gerber %s %s...' % (str(fcobj.options['name']), _("was mirrored")))
  188. def on_hole_size_toggle(self, val):
  189. if val == "fixed":
  190. self.dia_entry.setDisabled(False)
  191. self.dia_label.setDisabled(False)
  192. self.ring_label.setDisabled(True)
  193. self.ring_entry.setDisabled(True)
  194. else:
  195. self.dia_entry.setDisabled(True)
  196. self.dia_label.setDisabled(True)
  197. self.ring_label.setDisabled(False)
  198. self.ring_entry.setDisabled(False)
  199. def reset_fields(self):
  200. self.gerber_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  201. self.gerber_object_combo.setCurrentIndex(0)