ToolDblSided.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 FlatCAM object) through \n"
  99. "the center."
  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 Reference:</b>")
  109. self.pb_label.setToolTip(
  110. "If 'Point' is selected above it store the coordinates (x, y) through which\n"
  111. "the mirroring axis passes.\n"
  112. "If 'Box' is selected above, select here a FlatCAM object (Gerber, Exc or Geo).\n"
  113. "Through the center of this object pass the mirroring axis selected above."
  114. )
  115. self.add_point_button = QtWidgets.QPushButton("Add")
  116. self.add_point_button.setToolTip(
  117. "Add the coordinates in format <b>(x, y)</b> through which the mirroring axis \n "
  118. "selected in 'MIRROR AXIS' pass.\n"
  119. "The (x, y) coordinates are captured by pressing SHIFT key\n"
  120. "and left mouse button click on canvas or you can enter the coords manually."
  121. )
  122. self.add_point_button.setFixedWidth(40)
  123. grid_lay.addWidget(self.pb_label, 10, 0)
  124. grid_lay.addLayout(self.point_box_container, 11, 0, 1, 3)
  125. grid_lay.addWidget(self.add_point_button, 11, 3)
  126. self.point_entry = EvalEntry()
  127. self.point_box_container.addWidget(self.point_entry)
  128. self.box_combo = QtWidgets.QComboBox()
  129. self.box_combo.setModel(self.app.collection)
  130. self.box_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  131. self.box_combo.setCurrentIndex(1)
  132. self.box_combo_type = QtWidgets.QComboBox()
  133. self.box_combo_type.addItem("Gerber Reference Box Object")
  134. self.box_combo_type.addItem("Excellon Reference Box Object")
  135. self.box_combo_type.addItem("Geometry Reference Box Object")
  136. self.point_box_container.addWidget(self.box_combo_type)
  137. self.point_box_container.addWidget(self.box_combo)
  138. self.box_combo.hide()
  139. self.box_combo_type.hide()
  140. ## Alignment holes
  141. self.ah_label = QtWidgets.QLabel("<b>Alignment Drill Coordinates:</b>")
  142. self.ah_label.setToolTip(
  143. "Alignment holes (x1, y1), (x2, y2), ... "
  144. "on one side of the mirror axis. For each set of (x, y) coordinates\n"
  145. "entered here, a pair of drills will be created:\n\n"
  146. "- one drill at the coordinates from the field\n"
  147. "- one drill in mirror position over the axis selected above in the 'Mirror Axis'."
  148. )
  149. self.layout.addWidget(self.ah_label)
  150. grid_lay1 = QtWidgets.QGridLayout()
  151. self.layout.addLayout(grid_lay1)
  152. self.alignment_holes = EvalEntry()
  153. self.add_drill_point_button = QtWidgets.QPushButton("Add")
  154. self.add_drill_point_button.setToolTip(
  155. "Add alignment drill holes coords in the format: (x1, y1), (x2, y2), ... \n"
  156. "on one side of the mirror axis.\n\n"
  157. "The coordinates set can be obtained:\n"
  158. "- press SHIFT key and left mouse clicking on canvas. Then click Add.\n"
  159. "- press SHIFT key and left mouse clicking on canvas. Then CTRL+V in the field.\n"
  160. "- press SHIFT key and left mouse clicking on canvas. Then RMB click in the field and click Paste.\n"
  161. "- by entering the coords manually in the format: (x1, y1), (x2, y2), ..."
  162. )
  163. self.add_drill_point_button.setFixedWidth(40)
  164. grid_lay1.addWidget(self.alignment_holes, 0, 0, 1, 2)
  165. grid_lay1.addWidget(self.add_drill_point_button, 0, 3)
  166. ## Drill diameter for alignment holes
  167. self.dt_label = QtWidgets.QLabel("<b>Alignment Drill Diameter</b>:")
  168. self.dt_label.setToolTip(
  169. "Diameter of the drill for the "
  170. "alignment holes."
  171. )
  172. self.layout.addWidget(self.dt_label)
  173. grid_lay2 = QtWidgets.QGridLayout()
  174. self.layout.addLayout(grid_lay2)
  175. self.drill_dia = FCEntry()
  176. self.dd_label = QtWidgets.QLabel("Drill diam.:")
  177. self.dd_label.setToolTip(
  178. "Diameter of the drill for the "
  179. "alignment holes."
  180. )
  181. grid_lay2.addWidget(self.dd_label, 0, 0)
  182. grid_lay2.addWidget(self.drill_dia, 0, 1)
  183. ## Buttons
  184. self.create_alignment_hole_button = QtWidgets.QPushButton("Create Excellon Object")
  185. self.create_alignment_hole_button.setToolTip(
  186. "Creates an Excellon Object containing the\n"
  187. "specified alignment holes and their mirror\n"
  188. "images.")
  189. # self.create_alignment_hole_button.setFixedWidth(40)
  190. grid_lay2.addWidget(self.create_alignment_hole_button, 1,0, 1, 2)
  191. self.reset_button = QtWidgets.QPushButton("Reset")
  192. self.reset_button.setToolTip(
  193. "Resets all the fields.")
  194. self.reset_button.setFixedWidth(40)
  195. grid_lay2.addWidget(self.reset_button, 1, 2)
  196. self.layout.addStretch()
  197. ## Signals
  198. self.create_alignment_hole_button.clicked.connect(self.on_create_alignment_holes)
  199. self.mirror_gerber_button.clicked.connect(self.on_mirror_gerber)
  200. self.mirror_exc_button.clicked.connect(self.on_mirror_exc)
  201. self.mirror_geo_button.clicked.connect(self.on_mirror_geo)
  202. self.add_point_button.clicked.connect(self.on_point_add)
  203. self.add_drill_point_button.clicked.connect(self.on_drill_add)
  204. self.reset_button.clicked.connect(self.reset_fields)
  205. self.box_combo_type.currentIndexChanged.connect(self.on_combo_box_type)
  206. self.axis_location.group_toggle_fn = self.on_toggle_pointbox
  207. self.drill_values = ""
  208. def install(self, icon=None, separator=None, **kwargs):
  209. FlatCAMTool.install(self, icon, separator, shortcut='ALT+D', **kwargs)
  210. def run(self):
  211. self.app.report_usage("Tool2Sided()")
  212. FlatCAMTool.run(self)
  213. self.set_tool_ui()
  214. # if the splitter us hidden, display it
  215. if self.app.ui.splitter.sizes()[0] == 0:
  216. self.app.ui.splitter.setSizes([1, 1])
  217. self.app.ui.notebook.setTabText(2, "2-Sided Tool")
  218. def set_tool_ui(self):
  219. self.reset_fields()
  220. self.point_entry.set_value("")
  221. self.alignment_holes.set_value("")
  222. self.mirror_axis.set_value(self.app.defaults["tools_2sided_mirror_axis"])
  223. self.axis_location.set_value(self.app.defaults["tools_2sided_axis_loc"])
  224. self.drill_dia.set_value(self.app.defaults["tools_2sided_drilldia"])
  225. def on_combo_box_type(self):
  226. obj_type = self.box_combo_type.currentIndex()
  227. self.box_combo.setRootModelIndex(self.app.collection.index(obj_type, 0, QtCore.QModelIndex()))
  228. self.box_combo.setCurrentIndex(0)
  229. def on_create_alignment_holes(self):
  230. axis = self.mirror_axis.get_value()
  231. mode = self.axis_location.get_value()
  232. if mode == "point":
  233. try:
  234. px, py = self.point_entry.get_value()
  235. except TypeError:
  236. self.app.inform.emit("[WARNING_NOTCL] 'Point' reference is selected and 'Point' coordinates "
  237. "are missing. Add them and retry.")
  238. return
  239. else:
  240. selection_index = self.box_combo.currentIndex()
  241. model_index = self.app.collection.index(selection_index, 0, self.gerber_object_combo.rootModelIndex())
  242. try:
  243. bb_obj = model_index.internalPointer().obj
  244. except AttributeError:
  245. model_index = self.app.collection.index(selection_index, 0, self.exc_object_combo.rootModelIndex())
  246. try:
  247. bb_obj = model_index.internalPointer().obj
  248. except AttributeError:
  249. model_index = self.app.collection.index(selection_index, 0,
  250. self.geo_object_combo.rootModelIndex())
  251. try:
  252. bb_obj = model_index.internalPointer().obj
  253. except AttributeError:
  254. self.app.inform.emit(
  255. "[WARNING_NOTCL] There is no Box reference object loaded. Load one and retry.")
  256. return
  257. xmin, ymin, xmax, ymax = bb_obj.bounds()
  258. px = 0.5 * (xmin + xmax)
  259. py = 0.5 * (ymin + ymax)
  260. xscale, yscale = {"X": (1.0, -1.0), "Y": (-1.0, 1.0)}[axis]
  261. dia = self.drill_dia.get_value()
  262. if dia is '':
  263. self.app.inform.emit("[WARNING_NOTCL]No value or wrong format in Drill Dia entry. Add it and retry.")
  264. return
  265. tools = {"1": {"C": dia}}
  266. # holes = self.alignment_holes.get_value()
  267. holes = eval('[{}]'.format(self.alignment_holes.text()))
  268. if not holes:
  269. self.app.inform.emit("[WARNING_NOTCL] There are no Alignment Drill Coordinates to use. Add them and retry.")
  270. return
  271. drills = []
  272. for hole in holes:
  273. point = Point(hole)
  274. point_mirror = affinity.scale(point, xscale, yscale, origin=(px, py))
  275. drills.append({"point": point, "tool": "1"})
  276. drills.append({"point": point_mirror, "tool": "1"})
  277. if 'solid_geometry' not in tools:
  278. tools["1"]['solid_geometry'] = []
  279. else:
  280. tools["1"]['solid_geometry'].append(point_mirror)
  281. def obj_init(obj_inst, app_inst):
  282. obj_inst.tools = tools
  283. obj_inst.drills = drills
  284. obj_inst.create_geometry()
  285. self.app.new_object("excellon", "Alignment Drills", obj_init)
  286. self.drill_values = ''
  287. self.app.inform.emit("[success] Excellon object with alignment drills created...")
  288. def on_mirror_gerber(self):
  289. selection_index = self.gerber_object_combo.currentIndex()
  290. # fcobj = self.app.collection.object_list[selection_index]
  291. model_index = self.app.collection.index(selection_index, 0, self.gerber_object_combo.rootModelIndex())
  292. try:
  293. fcobj = model_index.internalPointer().obj
  294. except Exception as e:
  295. self.app.inform.emit("[WARNING_NOTCL] There is no Gerber object loaded ...")
  296. return
  297. if not isinstance(fcobj, FlatCAMGerber):
  298. self.app.inform.emit("[ERROR_NOTCL] Only Gerber, Excellon and Geometry objects can be mirrored.")
  299. return
  300. axis = self.mirror_axis.get_value()
  301. mode = self.axis_location.get_value()
  302. if mode == "point":
  303. try:
  304. px, py = self.point_entry.get_value()
  305. except TypeError:
  306. self.app.inform.emit("[WARNING_NOTCL] 'Point' coordinates missing. "
  307. "Using Origin (0, 0) as mirroring reference.")
  308. px, py = (0, 0)
  309. else:
  310. selection_index_box = self.box_combo.currentIndex()
  311. model_index_box = self.app.collection.index(selection_index_box, 0, self.box_combo.rootModelIndex())
  312. try:
  313. bb_obj = model_index_box.internalPointer().obj
  314. except Exception as e:
  315. self.app.inform.emit("[WARNING_NOTCL] There is no Box object loaded ...")
  316. return
  317. xmin, ymin, xmax, ymax = bb_obj.bounds()
  318. px = 0.5 * (xmin + xmax)
  319. py = 0.5 * (ymin + ymax)
  320. fcobj.mirror(axis, [px, py])
  321. self.app.object_changed.emit(fcobj)
  322. fcobj.plot()
  323. self.app.inform.emit("[success] Gerber %s was mirrored..." % str(fcobj.options['name']))
  324. def on_mirror_exc(self):
  325. selection_index = self.exc_object_combo.currentIndex()
  326. # fcobj = self.app.collection.object_list[selection_index]
  327. model_index = self.app.collection.index(selection_index, 0, self.exc_object_combo.rootModelIndex())
  328. try:
  329. fcobj = model_index.internalPointer().obj
  330. except Exception as e:
  331. self.app.inform.emit("[WARNING_NOTCL] There is no Excellon object loaded ...")
  332. return
  333. if not isinstance(fcobj, FlatCAMExcellon):
  334. self.app.inform.emit("[ERROR_NOTCL] Only Gerber, Excellon and Geometry objects can be mirrored.")
  335. return
  336. axis = self.mirror_axis.get_value()
  337. mode = self.axis_location.get_value()
  338. if mode == "point":
  339. try:
  340. px, py = self.point_entry.get_value()
  341. except Exception as e:
  342. log.debug("DblSidedTool.on_mirror_geo() --> %s" % str(e))
  343. self.app.inform.emit("[WARNING_NOTCL] There are no Point coordinates in the Point field. "
  344. "Add coords and try again ...")
  345. return
  346. else:
  347. selection_index_box = self.box_combo.currentIndex()
  348. model_index_box = self.app.collection.index(selection_index_box, 0, self.box_combo.rootModelIndex())
  349. try:
  350. bb_obj = model_index_box.internalPointer().obj
  351. except Exception as e:
  352. log.debug("DblSidedTool.on_mirror_geo() --> %s" % str(e))
  353. self.app.inform.emit("[WARNING_NOTCL] There is no Box object loaded ...")
  354. return
  355. xmin, ymin, xmax, ymax = bb_obj.bounds()
  356. px = 0.5 * (xmin + xmax)
  357. py = 0.5 * (ymin + ymax)
  358. fcobj.mirror(axis, [px, py])
  359. self.app.object_changed.emit(fcobj)
  360. fcobj.plot()
  361. self.app.inform.emit("[success] Excellon %s was mirrored..." % str(fcobj.options['name']))
  362. def on_mirror_geo(self):
  363. selection_index = self.geo_object_combo.currentIndex()
  364. # fcobj = self.app.collection.object_list[selection_index]
  365. model_index = self.app.collection.index(selection_index, 0, self.geo_object_combo.rootModelIndex())
  366. try:
  367. fcobj = model_index.internalPointer().obj
  368. except Exception as e:
  369. self.app.inform.emit("[WARNING_NOTCL] There is no Geometry object loaded ...")
  370. return
  371. if not isinstance(fcobj, FlatCAMGeometry):
  372. self.app.inform.emit("[ERROR_NOTCL] Only Gerber, Excellon and Geometry objects can be mirrored.")
  373. return
  374. axis = self.mirror_axis.get_value()
  375. mode = self.axis_location.get_value()
  376. if mode == "point":
  377. px, py = self.point_entry.get_value()
  378. else:
  379. selection_index_box = self.box_combo.currentIndex()
  380. model_index_box = self.app.collection.index(selection_index_box, 0, self.box_combo.rootModelIndex())
  381. try:
  382. bb_obj = model_index_box.internalPointer().obj
  383. except Exception as e:
  384. self.app.inform.emit("[WARNING_NOTCL] There is no Box object loaded ...")
  385. return
  386. xmin, ymin, xmax, ymax = bb_obj.bounds()
  387. px = 0.5 * (xmin + xmax)
  388. py = 0.5 * (ymin + ymax)
  389. fcobj.mirror(axis, [px, py])
  390. self.app.object_changed.emit(fcobj)
  391. fcobj.plot()
  392. self.app.inform.emit("[success] Geometry %s was mirrored..." % str(fcobj.options['name']))
  393. def on_point_add(self):
  394. val = self.app.defaults["global_point_clipboard_format"] % (self.app.pos[0], self.app.pos[1])
  395. self.point_entry.set_value(val)
  396. def on_drill_add(self):
  397. self.drill_values += (self.app.defaults["global_point_clipboard_format"] %
  398. (self.app.pos[0], self.app.pos[1])) + ','
  399. self.alignment_holes.set_value(self.drill_values)
  400. def on_toggle_pointbox(self):
  401. if self.axis_location.get_value() == "point":
  402. self.point_entry.show()
  403. self.box_combo.hide()
  404. self.box_combo_type.hide()
  405. self.add_point_button.setDisabled(False)
  406. else:
  407. self.point_entry.hide()
  408. self.box_combo.show()
  409. self.box_combo_type.show()
  410. self.add_point_button.setDisabled(True)
  411. def reset_fields(self):
  412. self.gerber_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  413. self.exc_object_combo.setRootModelIndex(self.app.collection.index(1, 0, QtCore.QModelIndex()))
  414. self.geo_object_combo.setRootModelIndex(self.app.collection.index(2, 0, QtCore.QModelIndex()))
  415. self.box_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  416. self.gerber_object_combo.setCurrentIndex(0)
  417. self.exc_object_combo.setCurrentIndex(0)
  418. self.geo_object_combo.setCurrentIndex(0)
  419. self.box_combo.setCurrentIndex(0)
  420. self.box_combo_type.setCurrentIndex(0)
  421. self.drill_values = ""