ToolDblSided.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. from PyQt5 import QtGui
  2. from GUIElements import RadioSet, EvalEntry, LengthEntry
  3. from FlatCAMTool import FlatCAMTool
  4. from FlatCAMObj import *
  5. from shapely.geometry import Point
  6. from shapely import affinity
  7. from PyQt5 import QtCore
  8. class DblSidedTool(FlatCAMTool):
  9. toolName = "2-Sided PCB"
  10. def __init__(self, app):
  11. FlatCAMTool.__init__(self, app)
  12. ## Title
  13. title_label = QtWidgets.QLabel("<font size=4><b>%s</b></font>" % self.toolName)
  14. self.layout.addWidget(title_label)
  15. self.empty_lb = QtWidgets.QLabel("")
  16. self.layout.addWidget(self.empty_lb)
  17. ## Grid Layout
  18. grid_lay = QtWidgets.QGridLayout()
  19. self.layout.addLayout(grid_lay)
  20. ## Gerber Object to mirror
  21. self.gerber_object_combo = QtWidgets.QComboBox()
  22. self.gerber_object_combo.setModel(self.app.collection)
  23. self.gerber_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  24. self.gerber_object_combo.setCurrentIndex(1)
  25. self.botlay_label = QtWidgets.QLabel("<b>GERBER:</b>")
  26. self.botlay_label.setToolTip(
  27. "Gerber to be mirrored."
  28. )
  29. self.mirror_gerber_button = QtWidgets.QPushButton("Mirror")
  30. self.mirror_gerber_button.setToolTip(
  31. "Mirrors (flips) the specified object around \n"
  32. "the specified axis. Does not create a new \n"
  33. "object, but modifies it."
  34. )
  35. self.mirror_gerber_button.setFixedWidth(40)
  36. # grid_lay.addRow("Bottom Layer:", self.object_combo)
  37. grid_lay.addWidget(self.botlay_label, 0, 0)
  38. grid_lay.addWidget(self.gerber_object_combo, 1, 0, 1, 2)
  39. grid_lay.addWidget(self.mirror_gerber_button, 1, 3)
  40. ## Excellon Object to mirror
  41. self.exc_object_combo = QtWidgets.QComboBox()
  42. self.exc_object_combo.setModel(self.app.collection)
  43. self.exc_object_combo.setRootModelIndex(self.app.collection.index(1, 0, QtCore.QModelIndex()))
  44. self.exc_object_combo.setCurrentIndex(1)
  45. self.excobj_label = QtWidgets.QLabel("<b>EXCELLON:</b>")
  46. self.excobj_label.setToolTip(
  47. "Excellon Object to be mirrored."
  48. )
  49. self.mirror_exc_button = QtWidgets.QPushButton("Mirror")
  50. self.mirror_exc_button.setToolTip(
  51. "Mirrors (flips) the specified object around \n"
  52. "the specified axis. Does not create a new \n"
  53. "object, but modifies it."
  54. )
  55. self.mirror_exc_button.setFixedWidth(40)
  56. # grid_lay.addRow("Bottom Layer:", self.object_combo)
  57. grid_lay.addWidget(self.excobj_label, 2, 0)
  58. grid_lay.addWidget(self.exc_object_combo, 3, 0, 1, 2)
  59. grid_lay.addWidget(self.mirror_exc_button, 3, 3)
  60. ## Geometry Object to mirror
  61. self.geo_object_combo = QtWidgets.QComboBox()
  62. self.geo_object_combo.setModel(self.app.collection)
  63. self.geo_object_combo.setRootModelIndex(self.app.collection.index(2, 0, QtCore.QModelIndex()))
  64. self.geo_object_combo.setCurrentIndex(1)
  65. self.geoobj_label = QtWidgets.QLabel("<b>GEOMETRY</b>:")
  66. self.geoobj_label.setToolTip(
  67. "Geometry Obj to be mirrored."
  68. )
  69. self.mirror_geo_button = QtWidgets.QPushButton("Mirror")
  70. self.mirror_geo_button.setToolTip(
  71. "Mirrors (flips) the specified object around \n"
  72. "the specified axis. Does not create a new \n"
  73. "object, but modifies it."
  74. )
  75. self.mirror_geo_button.setFixedWidth(40)
  76. # grid_lay.addRow("Bottom Layer:", self.object_combo)
  77. grid_lay.addWidget(self.geoobj_label, 4, 0)
  78. grid_lay.addWidget(self.geo_object_combo, 5, 0, 1, 2)
  79. grid_lay.addWidget(self.mirror_geo_button, 5, 3)
  80. ## Axis
  81. self.mirror_axis = RadioSet([{'label': 'X', 'value': 'X'},
  82. {'label': 'Y', 'value': 'Y'}])
  83. self.mirax_label = QtWidgets.QLabel("Mirror Axis:")
  84. self.mirax_label.setToolTip(
  85. "Mirror vertically (X) or horizontally (Y)."
  86. )
  87. # grid_lay.addRow("Mirror Axis:", self.mirror_axis)
  88. self.empty_lb1 = QtWidgets.QLabel("")
  89. grid_lay.addWidget(self.empty_lb1, 6, 0)
  90. grid_lay.addWidget(self.mirax_label, 7, 0)
  91. grid_lay.addWidget(self.mirror_axis, 7, 1)
  92. ## Axis Location
  93. self.axis_location = RadioSet([{'label': 'Point', 'value': 'point'},
  94. {'label': 'Box', 'value': 'box'}])
  95. self.axloc_label = QtWidgets.QLabel("Axis Ref:")
  96. self.axloc_label.setToolTip(
  97. "The axis should pass through a <b>point</b> or cut\n "
  98. "a specified <b>box</b> (in a Geometry object) in \n"
  99. "the middle."
  100. )
  101. # grid_lay.addRow("Axis Location:", self.axis_location)
  102. grid_lay.addWidget(self.axloc_label, 8, 0)
  103. grid_lay.addWidget(self.axis_location, 8, 1)
  104. self.empty_lb2 = QtWidgets.QLabel("")
  105. grid_lay.addWidget(self.empty_lb2, 9, 0)
  106. ## Point/Box
  107. self.point_box_container = QtWidgets.QVBoxLayout()
  108. self.pb_label = QtWidgets.QLabel("<b>Point/Box:</b>")
  109. self.pb_label.setToolTip(
  110. "Specify the point (x, y) through which the mirror axis \n "
  111. "passes or the Geometry object containing a rectangle \n"
  112. "that the mirror axis cuts in half."
  113. )
  114. # grid_lay.addRow("Point/Box:", self.point_box_container)
  115. self.add_point_button = QtWidgets.QPushButton("Add")
  116. self.add_point_button.setToolTip(
  117. "Add the <b>point (x, y)</b> through which the mirror axis \n "
  118. "passes or the Object containing a rectangle \n"
  119. "that the mirror axis cuts in half.\n"
  120. "The point is captured by pressing SHIFT key\n"
  121. "and left mouse clicking on canvas or you can enter them manually."
  122. )
  123. self.add_point_button.setFixedWidth(40)
  124. grid_lay.addWidget(self.pb_label, 10, 0)
  125. grid_lay.addLayout(self.point_box_container, 11, 0, 1, 3)
  126. grid_lay.addWidget(self.add_point_button, 11, 3)
  127. self.point_entry = EvalEntry()
  128. self.point_box_container.addWidget(self.point_entry)
  129. self.box_combo = QtWidgets.QComboBox()
  130. self.box_combo.setModel(self.app.collection)
  131. self.box_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  132. self.box_combo.setCurrentIndex(1)
  133. self.box_combo_type = QtWidgets.QComboBox()
  134. self.box_combo_type.addItem("Gerber Reference Box Object")
  135. self.box_combo_type.addItem("Excellon Reference Box Object")
  136. self.box_combo_type.addItem("Geometry Reference Box Object")
  137. self.point_box_container.addWidget(self.box_combo_type)
  138. self.point_box_container.addWidget(self.box_combo)
  139. self.box_combo.hide()
  140. self.box_combo_type.hide()
  141. ## Alignment holes
  142. self.ah_label = QtWidgets.QLabel("<b>Alignment Drill Coordinates:</b>")
  143. self.ah_label.setToolTip(
  144. "Alignment holes (x1, y1), (x2, y2), ... "
  145. "on one side of the mirror axis. For each set of (x, y) coordinates\n"
  146. "entered here, a pair of drills will be created: one on the\n"
  147. "coordinates entered and one in mirror position over the axis\n"
  148. "selected above in the 'Mirror Axis'."
  149. )
  150. self.layout.addWidget(self.ah_label)
  151. grid_lay1 = QtWidgets.QGridLayout()
  152. self.layout.addLayout(grid_lay1)
  153. self.alignment_holes = EvalEntry()
  154. self.add_drill_point_button = QtWidgets.QPushButton("Add")
  155. self.add_drill_point_button.setToolTip(
  156. "Add alignment drill holes coords (x1, y1), (x2, y2), ... \n"
  157. "on one side of the mirror axis.\n"
  158. "The point(s) can be captured by pressing SHIFT key\n"
  159. "and left mouse clicking on canvas. Or you can enter them manually."
  160. )
  161. self.add_drill_point_button.setFixedWidth(40)
  162. grid_lay1.addWidget(self.alignment_holes, 0, 0, 1, 2)
  163. grid_lay1.addWidget(self.add_drill_point_button, 0, 3)
  164. ## Drill diameter for alignment holes
  165. self.dt_label = QtWidgets.QLabel("<b>Alignment Drill Creation</b>:")
  166. self.dt_label.setToolTip(
  167. "Create a set of alignment drill holes\n"
  168. "with the specified diameter,\n"
  169. "at the specified coordinates."
  170. )
  171. self.layout.addWidget(self.dt_label)
  172. grid_lay2 = QtWidgets.QGridLayout()
  173. self.layout.addLayout(grid_lay2)
  174. self.drill_dia = LengthEntry()
  175. self.dd_label = QtWidgets.QLabel("Drill diam.:")
  176. self.dd_label.setToolTip(
  177. "Diameter of the drill for the "
  178. "alignment holes."
  179. )
  180. grid_lay2.addWidget(self.dd_label, 0, 0)
  181. grid_lay2.addWidget(self.drill_dia, 0, 1)
  182. ## Buttons
  183. self.create_alignment_hole_button = QtWidgets.QPushButton("Create Excellon Object")
  184. self.create_alignment_hole_button.setToolTip(
  185. "Creates an Excellon Object containing the\n"
  186. "specified alignment holes and their mirror\n"
  187. "images.")
  188. # self.create_alignment_hole_button.setFixedWidth(40)
  189. grid_lay2.addWidget(self.create_alignment_hole_button, 1,0, 1, 2)
  190. self.reset_button = QtWidgets.QPushButton("Reset")
  191. self.reset_button.setToolTip(
  192. "Resets all the fields.")
  193. self.reset_button.setFixedWidth(40)
  194. grid_lay2.addWidget(self.reset_button, 1, 2)
  195. self.layout.addStretch()
  196. ## Signals
  197. self.create_alignment_hole_button.clicked.connect(self.on_create_alignment_holes)
  198. self.mirror_gerber_button.clicked.connect(self.on_mirror_gerber)
  199. self.mirror_exc_button.clicked.connect(self.on_mirror_exc)
  200. self.mirror_geo_button.clicked.connect(self.on_mirror_geo)
  201. self.add_point_button.clicked.connect(self.on_point_add)
  202. self.add_drill_point_button.clicked.connect(self.on_drill_add)
  203. self.reset_button.clicked.connect(self.reset_fields)
  204. self.box_combo_type.currentIndexChanged.connect(self.on_combo_box_type)
  205. self.axis_location.group_toggle_fn = self.on_toggle_pointbox
  206. self.drill_values = ""
  207. ## Initialize form
  208. self.mirror_axis.set_value('X')
  209. self.axis_location.set_value('point')
  210. self.drill_dia.set_value(1)
  211. def install(self, icon=None, separator=None, **kwargs):
  212. FlatCAMTool.install(self, icon, separator, shortcut='ALT+D', **kwargs)
  213. def on_combo_box_type(self):
  214. obj_type = self.box_combo_type.currentIndex()
  215. self.box_combo.setRootModelIndex(self.app.collection.index(obj_type, 0, QtCore.QModelIndex()))
  216. self.box_combo.setCurrentIndex(0)
  217. def on_create_alignment_holes(self):
  218. axis = self.mirror_axis.get_value()
  219. mode = self.axis_location.get_value()
  220. if mode == "point":
  221. try:
  222. px, py = self.point_entry.get_value()
  223. except TypeError:
  224. self.app.inform.emit("[warning_notcl] 'Point' reference is selected and 'Point' coordinates "
  225. "are missing. Add them and retry.")
  226. return
  227. else:
  228. selection_index = self.box_combo.currentIndex()
  229. model_index = self.app.collection.index(selection_index, 0, self.gerber_object_combo.rootModelIndex())
  230. bb_obj = model_index.internalPointer().obj
  231. xmin, ymin, xmax, ymax = bb_obj.bounds()
  232. px = 0.5 * (xmin + xmax)
  233. py = 0.5 * (ymin + ymax)
  234. xscale, yscale = {"X": (1.0, -1.0), "Y": (-1.0, 1.0)}[axis]
  235. dia = self.drill_dia.get_value()
  236. tools = {"1": {"C": dia}}
  237. # holes = self.alignment_holes.get_value()
  238. holes = eval('[{}]'.format(self.alignment_holes.text()))
  239. if not holes:
  240. self.app.inform.emit("[warning_notcl] There are no Alignment Drill Coordinates to use. Add them and retry.")
  241. return
  242. drills = []
  243. for hole in holes:
  244. point = Point(hole)
  245. point_mirror = affinity.scale(point, xscale, yscale, origin=(px, py))
  246. drills.append({"point": point, "tool": "1"})
  247. drills.append({"point": point_mirror, "tool": "1"})
  248. def obj_init(obj_inst, app_inst):
  249. obj_inst.tools = tools
  250. obj_inst.drills = drills
  251. obj_inst.create_geometry()
  252. self.app.new_object("excellon", "Alignment Drills", obj_init)
  253. self.drill_values = ''
  254. def on_mirror_gerber(self):
  255. selection_index = self.gerber_object_combo.currentIndex()
  256. # fcobj = self.app.collection.object_list[selection_index]
  257. model_index = self.app.collection.index(selection_index, 0, self.gerber_object_combo.rootModelIndex())
  258. try:
  259. fcobj = model_index.internalPointer().obj
  260. except Exception as e:
  261. self.app.inform.emit("[warning_notcl] There is no Gerber object loaded ...")
  262. return
  263. if not isinstance(fcobj, FlatCAMGerber):
  264. self.app.inform.emit("[error_notcl] Only Gerber, Excellon and Geometry objects can be mirrored.")
  265. return
  266. axis = self.mirror_axis.get_value()
  267. mode = self.axis_location.get_value()
  268. if mode == "point":
  269. try:
  270. px, py = self.point_entry.get_value()
  271. except TypeError:
  272. self.app.inform.emit("[warning_notcl] 'Point' coordinates missing. "
  273. "Using Origin (0, 0) as mirroring reference.")
  274. px, py = (0, 0)
  275. else:
  276. selection_index_box = self.box_combo.currentIndex()
  277. model_index_box = self.app.collection.index(selection_index_box, 0, self.box_combo.rootModelIndex())
  278. try:
  279. bb_obj = model_index_box.internalPointer().obj
  280. except Exception as e:
  281. self.app.inform.emit("[warning_notcl] There is no Box object loaded ...")
  282. return
  283. xmin, ymin, xmax, ymax = bb_obj.bounds()
  284. px = 0.5 * (xmin + xmax)
  285. py = 0.5 * (ymin + ymax)
  286. fcobj.mirror(axis, [px, py])
  287. self.app.object_changed.emit(fcobj)
  288. fcobj.plot()
  289. def on_mirror_exc(self):
  290. selection_index = self.exc_object_combo.currentIndex()
  291. # fcobj = self.app.collection.object_list[selection_index]
  292. model_index = self.app.collection.index(selection_index, 0, self.exc_object_combo.rootModelIndex())
  293. try:
  294. fcobj = model_index.internalPointer().obj
  295. except Exception as e:
  296. self.app.inform.emit("[warning_notcl] There is no Excellon object loaded ...")
  297. return
  298. if not isinstance(fcobj, FlatCAMExcellon):
  299. self.app.inform.emit("[error_notcl] Only Gerber, Excellon and Geometry objects can be mirrored.")
  300. return
  301. axis = self.mirror_axis.get_value()
  302. mode = self.axis_location.get_value()
  303. if mode == "point":
  304. px, py = self.point_entry.get_value()
  305. else:
  306. selection_index_box = self.box_combo.currentIndex()
  307. model_index_box = self.app.collection.index(selection_index_box, 0, self.box_combo.rootModelIndex())
  308. try:
  309. bb_obj = model_index_box.internalPointer().obj
  310. except Exception as e:
  311. self.app.inform.emit("[warning_notcl] There is no Box object loaded ...")
  312. return
  313. xmin, ymin, xmax, ymax = bb_obj.bounds()
  314. px = 0.5 * (xmin + xmax)
  315. py = 0.5 * (ymin + ymax)
  316. fcobj.mirror(axis, [px, py])
  317. self.app.object_changed.emit(fcobj)
  318. fcobj.plot()
  319. def on_mirror_geo(self):
  320. selection_index = self.geo_object_combo.currentIndex()
  321. # fcobj = self.app.collection.object_list[selection_index]
  322. model_index = self.app.collection.index(selection_index, 0, self.geo_object_combo.rootModelIndex())
  323. try:
  324. fcobj = model_index.internalPointer().obj
  325. except Exception as e:
  326. self.app.inform.emit("[warning_notcl] There is no Geometry object loaded ...")
  327. return
  328. if not isinstance(fcobj, FlatCAMGeometry):
  329. self.app.inform.emit("[error_notcl] Only Gerber, Excellon and Geometry objects can be mirrored.")
  330. return
  331. axis = self.mirror_axis.get_value()
  332. mode = self.axis_location.get_value()
  333. if mode == "point":
  334. px, py = self.point_entry.get_value()
  335. else:
  336. selection_index_box = self.box_combo.currentIndex()
  337. model_index_box = self.app.collection.index(selection_index_box, 0, self.box_combo.rootModelIndex())
  338. try:
  339. bb_obj = model_index_box.internalPointer().obj
  340. except Exception as e:
  341. self.app.inform.emit("[warning_notcl] There is no Box object loaded ...")
  342. return
  343. xmin, ymin, xmax, ymax = bb_obj.bounds()
  344. px = 0.5 * (xmin + xmax)
  345. py = 0.5 * (ymin + ymax)
  346. fcobj.mirror(axis, [px, py])
  347. self.app.object_changed.emit(fcobj)
  348. fcobj.plot()
  349. def on_point_add(self):
  350. val = self.app.defaults["global_point_clipboard_format"] % (self.app.pos[0], self.app.pos[1])
  351. self.point_entry.set_value(val)
  352. def on_drill_add(self):
  353. self.drill_values += (self.app.defaults["global_point_clipboard_format"] %
  354. (self.app.pos[0], self.app.pos[1])) + ','
  355. self.alignment_holes.set_value(self.drill_values)
  356. def on_toggle_pointbox(self):
  357. if self.axis_location.get_value() == "point":
  358. self.point_entry.show()
  359. self.box_combo.hide()
  360. self.box_combo_type.hide()
  361. self.add_point_button.setDisabled(False)
  362. else:
  363. self.point_entry.hide()
  364. self.box_combo.show()
  365. self.box_combo_type.show()
  366. self.add_point_button.setDisabled(True)
  367. def reset_fields(self):
  368. self.gerber_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  369. self.exc_object_combo.setRootModelIndex(self.app.collection.index(1, 0, QtCore.QModelIndex()))
  370. self.geo_object_combo.setRootModelIndex(self.app.collection.index(2, 0, QtCore.QModelIndex()))
  371. self.box_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  372. self.gerber_object_combo.setCurrentIndex(0)
  373. self.exc_object_combo.setCurrentIndex(0)
  374. self.geo_object_combo.setCurrentIndex(0)
  375. self.box_combo.setCurrentIndex(0)
  376. self.box_combo_type.setCurrentIndex(0)
  377. self.drill_values = ""
  378. self.point_entry.set_value("")
  379. self.alignment_holes.set_value("")
  380. ## Initialize form
  381. self.mirror_axis.set_value('X')
  382. self.axis_location.set_value('point')
  383. self.drill_dia.set_value(1)
  384. def run(self):
  385. FlatCAMTool.run(self)
  386. self.app.ui.notebook.setTabText(2, "2-Sided Tool")
  387. self.reset_fields()