FlatCAMTool.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. from PyQt4 import QtGui, QtCore
  2. from shapely.geometry import Point
  3. from shapely import affinity
  4. from math import sqrt
  5. from GUIElements import *
  6. from FlatCAMObj import *
  7. class FlatCAMTool(QtGui.QWidget):
  8. toolName = "FlatCAM Generic Tool"
  9. def __init__(self, app, parent=None):
  10. """
  11. :param app: The application this tool will run in.
  12. :type app: App
  13. :param parent: Qt Parent
  14. :return: FlatCAMTool
  15. """
  16. QtGui.QWidget.__init__(self, parent)
  17. # self.setSizePolicy(QtGui.QSizePolicy.Maximum, QtGui.QSizePolicy.Maximum)
  18. self.layout = QtGui.QVBoxLayout()
  19. self.setLayout(self.layout)
  20. self.app = app
  21. self.menuAction = None
  22. def install(self):
  23. self.menuAction = self.app.ui.menutool.addAction(self.toolName)
  24. self.menuAction.triggered.connect(self.run)
  25. def run(self):
  26. # Remove anything else in the GUI
  27. self.app.ui.tool_scroll_area.takeWidget()
  28. # Put ourself in the GUI
  29. self.app.ui.tool_scroll_area.setWidget(self)
  30. # Switch notebook to tool page
  31. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  32. self.show()
  33. class DblSidedTool(FlatCAMTool):
  34. toolName = "Double-Sided PCB Tool"
  35. def __init__(self, app):
  36. FlatCAMTool.__init__(self, app)
  37. ## Title
  38. title_label = QtGui.QLabel("<font size=4><b>%s</b></font>" % self.toolName)
  39. self.layout.addWidget(title_label)
  40. ## Form Layout
  41. form_layout = QtGui.QFormLayout()
  42. self.layout.addLayout(form_layout)
  43. ## Layer to mirror
  44. self.object_combo = QtGui.QComboBox()
  45. self.object_combo.setModel(self.app.collection)
  46. form_layout.addRow("Bottom Layer:", self.object_combo)
  47. ## Axis
  48. self.mirror_axis = RadioSet([{'label': 'X', 'value': 'X'},
  49. {'label': 'Y', 'value': 'Y'}])
  50. form_layout.addRow("Mirror Axis:", self.mirror_axis)
  51. ## Axis Location
  52. self.axis_location = RadioSet([{'label': 'Point', 'value': 'point'},
  53. {'label': 'Box', 'value': 'box'}])
  54. form_layout.addRow("Axis Location:", self.axis_location)
  55. ## Point/Box
  56. self.point_box_container = QtGui.QVBoxLayout()
  57. form_layout.addRow("Point/Box:", self.point_box_container)
  58. self.point = EvalEntry()
  59. self.point_box_container.addWidget(self.point)
  60. self.box_combo = QtGui.QComboBox()
  61. self.box_combo.setModel(self.app.collection)
  62. self.point_box_container.addWidget(self.box_combo)
  63. self.box_combo.hide()
  64. ## Alignment holes
  65. self.alignment_holes = EvalEntry()
  66. form_layout.addRow("Alignment Holes:", self.alignment_holes)
  67. ## Drill diameter for alignment holes
  68. self.drill_dia = LengthEntry()
  69. form_layout.addRow("Drill diam.:", self.drill_dia)
  70. ## Buttons
  71. hlay = QtGui.QHBoxLayout()
  72. self.layout.addLayout(hlay)
  73. hlay.addStretch()
  74. self.create_alignment_hole_button = QtGui.QPushButton("Create Alignment Drill")
  75. self.mirror_object_button = QtGui.QPushButton("Mirror Object")
  76. hlay.addWidget(self.create_alignment_hole_button)
  77. hlay.addWidget(self.mirror_object_button)
  78. self.layout.addStretch()
  79. ## Signals
  80. self.create_alignment_hole_button.clicked.connect(self.on_create_alignment_holes)
  81. self.mirror_object_button.clicked.connect(self.on_mirror)
  82. self.axis_location.group_toggle_fn = self.on_toggle_pointbox
  83. ## Initialize form
  84. self.mirror_axis.set_value('X')
  85. self.axis_location.set_value('point')
  86. def on_create_alignment_holes(self):
  87. axis = self.mirror_axis.get_value()
  88. mode = self.axis_location.get_value()
  89. if mode == "point":
  90. px, py = self.point.get_value()
  91. else:
  92. selection_index = self.box_combo.currentIndex()
  93. bb_obj = self.app.collection.object_list[selection_index] # TODO: Direct access??
  94. xmin, ymin, xmax, ymax = bb_obj.bounds()
  95. px = 0.5*(xmin+xmax)
  96. py = 0.5*(ymin+ymax)
  97. xscale, yscale = {"X": (1.0, -1.0), "Y": (-1.0, 1.0)}[axis]
  98. dia = self.drill_dia.get_value()
  99. tools = {"1": {"C": dia}}
  100. holes = self.alignment_holes.get_value()
  101. drills = []
  102. for hole in holes:
  103. point = Point(hole)
  104. point_mirror = affinity.scale(point, xscale, yscale, origin=(px, py))
  105. drills.append({"point": point, "tool": "1"})
  106. drills.append({"point": point_mirror, "tool": "1"})
  107. def obj_init(obj_inst, app_inst):
  108. obj_inst.tools = tools
  109. obj_inst.drills = drills
  110. obj_inst.create_geometry()
  111. self.app.new_object("excellon", "Alignment Drills", obj_init)
  112. def on_mirror(self):
  113. selection_index = self.object_combo.currentIndex()
  114. fcobj = self.app.collection.object_list[selection_index]
  115. # For now, lets limit to Gerbers and Excellons.
  116. # assert isinstance(gerb, FlatCAMGerber)
  117. if not isinstance(fcobj, FlatCAMGerber) and not isinstance(fcobj, FlatCAMExcellon):
  118. self.info("ERROR: Only Gerber and Excellon objects can be mirrored.")
  119. return
  120. axis = self.mirror_axis.get_value()
  121. mode = self.axis_location.get_value()
  122. if mode == "point":
  123. px, py = self.point.get_value()
  124. else:
  125. selection_index = self.box_combo.currentIndex()
  126. bb_obj = self.app.collection.object_list[selection_index] # TODO: Direct access??
  127. xmin, ymin, xmax, ymax = bb_obj.bounds()
  128. px = 0.5*(xmin+xmax)
  129. py = 0.5*(ymin+ymax)
  130. fcobj.mirror(axis, [px, py])
  131. fcobj.plot()
  132. def on_toggle_pointbox(self):
  133. if self.axis_location.get_value() == "point":
  134. self.point.show()
  135. self.box_combo.hide()
  136. else:
  137. self.point.hide()
  138. self.box_combo.show()
  139. class Measurement(FlatCAMTool):
  140. toolName = "Measurement Tool"
  141. def __init__(self, app):
  142. FlatCAMTool.__init__(self, app)
  143. # self.setContentsMargins(0, 0, 0, 0)
  144. self.layout.setMargin(0)
  145. self.layout.setContentsMargins(0, 0, 3, 0)
  146. self.setSizePolicy(QtGui.QSizePolicy.Ignored, QtGui.QSizePolicy.Maximum)
  147. self.point1 = None
  148. self.point2 = None
  149. self.label = QtGui.QLabel("Click on a reference point ...")
  150. self.label.setFrameStyle(QtGui.QFrame.StyledPanel | QtGui.QFrame.Plain)
  151. self.label.setMargin(3)
  152. self.layout.addWidget(self.label)
  153. # self.layout.setMargin(0)
  154. self.setVisible(False)
  155. self.click_subscription = None
  156. self.move_subscription = None
  157. def install(self):
  158. FlatCAMTool.install(self)
  159. self.app.ui.right_layout.addWidget(self)
  160. self.app.plotcanvas.mpl_connect('key_press_event', self.on_key_press)
  161. def run(self):
  162. self.toggle()
  163. def on_click(self, event):
  164. if self.point1 is None:
  165. self.point1 = (event.xdata, event.ydata)
  166. else:
  167. self.point2 = copy(self.point1)
  168. self.point1 = (event.xdata, event.ydata)
  169. self.on_move(event)
  170. def on_key_press(self, event):
  171. if event.key == 'r':
  172. self.toggle()
  173. def toggle(self):
  174. if self.isVisible():
  175. self.setVisible(False)
  176. self.app.plotcanvas.mpl_disconnect(self.move_subscription)
  177. self.app.plotcanvas.mpl_disconnect(self.click_subscription)
  178. else:
  179. self.setVisible(True)
  180. self.move_subscription = self.app.plotcanvas.mpl_connect('motion_notify_event', self.on_move)
  181. self.click_subscription = self.app.plotcanvas.mpl_connect('button_press_event', self.on_click)
  182. def on_move(self, event):
  183. if self.point1 is None:
  184. self.label.setText("Click on a reference point...")
  185. else:
  186. try:
  187. dx = event.xdata - self.point1[0]
  188. dy = event.ydata - self.point1[1]
  189. d = sqrt(dx**2 + dy**2)
  190. self.label.setText("D = %.4f D(x) = %.4f D(y) = %.4f" % (d, dx, dy))
  191. except TypeError:
  192. pass
  193. if self.update is not None:
  194. self.update()