ToolDblSided.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. from PyQt5 import QtWidgets, QtCore
  2. from FlatCAMTool import FlatCAMTool
  3. from flatcamGUI.GUIElements import RadioSet, FCDoubleSpinner, EvalEntry, FCEntry, FCButton
  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 DblSidedTool(FlatCAMTool):
  17. toolName = _("2-Sided PCB")
  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.layout.addWidget(QtWidgets.QLabel(""))
  32. # ## Grid Layout
  33. grid_lay = QtWidgets.QGridLayout()
  34. grid_lay.setColumnStretch(0, 1)
  35. grid_lay.setColumnStretch(1, 0)
  36. self.layout.addLayout(grid_lay)
  37. # Objects to be mirrored
  38. self.m_objects_label = QtWidgets.QLabel("<b>%s:</b>" % _("Mirror Operation"))
  39. self.m_objects_label.setToolTip('%s.' % _("Objects to be mirrored"))
  40. grid_lay.addWidget(self.m_objects_label, 0, 0, 1, 2)
  41. # ## Gerber Object to mirror
  42. self.gerber_object_combo = QtWidgets.QComboBox()
  43. self.gerber_object_combo.setModel(self.app.collection)
  44. self.gerber_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  45. self.gerber_object_combo.setCurrentIndex(1)
  46. self.botlay_label = QtWidgets.QLabel("%s:" % _("GERBER"))
  47. self.botlay_label.setToolTip('%s.' % _("Gerber to be mirrored"))
  48. self.mirror_gerber_button = QtWidgets.QPushButton(_("Mirror"))
  49. self.mirror_gerber_button.setToolTip(
  50. _("Mirrors (flips) the specified object around \n"
  51. "the specified axis. Does not create a new \n"
  52. "object, but modifies it.")
  53. )
  54. self.mirror_gerber_button.setStyleSheet("""
  55. QPushButton
  56. {
  57. font-weight: bold;
  58. }
  59. """)
  60. self.mirror_gerber_button.setMinimumWidth(60)
  61. grid_lay.addWidget(self.botlay_label, 1, 0)
  62. grid_lay.addWidget(self.gerber_object_combo, 2, 0)
  63. grid_lay.addWidget(self.mirror_gerber_button, 2, 1)
  64. # ## Excellon Object to mirror
  65. self.exc_object_combo = QtWidgets.QComboBox()
  66. self.exc_object_combo.setModel(self.app.collection)
  67. self.exc_object_combo.setRootModelIndex(self.app.collection.index(1, 0, QtCore.QModelIndex()))
  68. self.exc_object_combo.setCurrentIndex(1)
  69. self.excobj_label = QtWidgets.QLabel("%s:" % _("EXCELLON"))
  70. self.excobj_label.setToolTip(_("Excellon Object to be mirrored."))
  71. self.mirror_exc_button = QtWidgets.QPushButton(_("Mirror"))
  72. self.mirror_exc_button.setToolTip(
  73. _("Mirrors (flips) the specified object around \n"
  74. "the specified axis. Does not create a new \n"
  75. "object, but modifies it.")
  76. )
  77. self.mirror_exc_button.setStyleSheet("""
  78. QPushButton
  79. {
  80. font-weight: bold;
  81. }
  82. """)
  83. self.mirror_exc_button.setMinimumWidth(60)
  84. grid_lay.addWidget(self.excobj_label, 3, 0)
  85. grid_lay.addWidget(self.exc_object_combo, 4, 0)
  86. grid_lay.addWidget(self.mirror_exc_button, 4, 1)
  87. # ## Geometry Object to mirror
  88. self.geo_object_combo = QtWidgets.QComboBox()
  89. self.geo_object_combo.setModel(self.app.collection)
  90. self.geo_object_combo.setRootModelIndex(self.app.collection.index(2, 0, QtCore.QModelIndex()))
  91. self.geo_object_combo.setCurrentIndex(1)
  92. self.geoobj_label = QtWidgets.QLabel("%s:" % _("GEOMETRY"))
  93. self.geoobj_label.setToolTip(
  94. _("Geometry Obj to be mirrored.")
  95. )
  96. self.mirror_geo_button = QtWidgets.QPushButton(_("Mirror"))
  97. self.mirror_geo_button.setToolTip(
  98. _("Mirrors (flips) the specified object around \n"
  99. "the specified axis. Does not create a new \n"
  100. "object, but modifies it.")
  101. )
  102. self.mirror_geo_button.setStyleSheet("""
  103. QPushButton
  104. {
  105. font-weight: bold;
  106. }
  107. """)
  108. self.mirror_geo_button.setMinimumWidth(60)
  109. # grid_lay.addRow("Bottom Layer:", self.object_combo)
  110. grid_lay.addWidget(self.geoobj_label, 5, 0)
  111. grid_lay.addWidget(self.geo_object_combo, 6, 0)
  112. grid_lay.addWidget(self.mirror_geo_button, 6, 1)
  113. separator_line = QtWidgets.QFrame()
  114. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  115. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  116. grid_lay.addWidget(separator_line, 7, 0, 1, 2)
  117. self.layout.addWidget(QtWidgets.QLabel(""))
  118. # ## Grid Layout
  119. grid_lay1 = QtWidgets.QGridLayout()
  120. grid_lay1.setColumnStretch(0, 0)
  121. grid_lay1.setColumnStretch(1, 1)
  122. self.layout.addLayout(grid_lay1)
  123. # Objects to be mirrored
  124. self.param_label = QtWidgets.QLabel("<b>%s:</b>" % _("Mirror Parameters"))
  125. self.param_label.setToolTip('%s.' % _("Parameters for the mirror operation"))
  126. grid_lay1.addWidget(self.param_label, 0, 0, 1, 3)
  127. # ## Axis
  128. self.mirax_label = QtWidgets.QLabel('%s:' % _("Axis"))
  129. self.mirax_label.setToolTip(_("Mirror vertically (X) or horizontally (Y)."))
  130. self.mirror_axis = RadioSet([{'label': 'X', 'value': 'X'},
  131. {'label': 'Y', 'value': 'Y'}])
  132. grid_lay1.addWidget(self.mirax_label, 2, 0)
  133. grid_lay1.addWidget(self.mirror_axis, 2, 1, 1, 2)
  134. # ## Axis Location
  135. self.axloc_label = QtWidgets.QLabel('%s:' % _("Reference"))
  136. self.axloc_label.setToolTip(
  137. _("The coordinates used as reference for the mirror operation.\n"
  138. "Can be:\n"
  139. "- Point -> a set of coordinates (x,y) around which the object is mirrored\n"
  140. "- Box -> a set of coordinates (x, y) obtained from the center of the\n"
  141. "bounding box of another object selected below")
  142. )
  143. self.axis_location = RadioSet([{'label': _('Point'), 'value': 'point'},
  144. {'label': _('Box'), 'value': 'box'}])
  145. grid_lay1.addWidget(self.axloc_label, 4, 0)
  146. grid_lay1.addWidget(self.axis_location, 4, 1, 1, 2)
  147. # ## Point/Box
  148. self.point_entry = EvalEntry()
  149. # Add a reference
  150. self.add_point_button = QtWidgets.QPushButton(_("Add"))
  151. self.add_point_button.setToolTip(
  152. _("Add the coordinates in format <b>(x, y)</b> through which the mirroring axis \n "
  153. "selected in 'MIRROR AXIS' pass.\n"
  154. "The (x, y) coordinates are captured by pressing SHIFT key\n"
  155. "and left mouse button click on canvas or you can enter the coordinates manually.")
  156. )
  157. self.add_point_button.setStyleSheet("""
  158. QPushButton
  159. {
  160. font-weight: bold;
  161. }
  162. """)
  163. self.add_point_button.setMinimumWidth(60)
  164. grid_lay1.addWidget(self.point_entry, 7, 0, 1, 2)
  165. grid_lay1.addWidget(self.add_point_button, 7, 2)
  166. # ## Grid Layout
  167. grid_lay2 = QtWidgets.QGridLayout()
  168. grid_lay2.setColumnStretch(0, 0)
  169. grid_lay2.setColumnStretch(1, 1)
  170. self.layout.addLayout(grid_lay2)
  171. self.box_type_label = QtWidgets.QLabel('%s:' % _("Reference Object"))
  172. self.box_type_label.setToolTip(
  173. _("It can be of type: Gerber or Excellon or Geometry.\n"
  174. "The coordinates of the center of the bounding box are used\n"
  175. "as reference for mirror operation.")
  176. )
  177. # Type of object used as BOX reference
  178. self.box_type_radio = RadioSet([{'label': _('Gerber'), 'value': 'grb'},
  179. {'label': _('Excellon'), 'value': 'exc'},
  180. {'label': _('Geometry'), 'value': 'geo'}])
  181. self.box_type_label.hide()
  182. self.box_type_radio.hide()
  183. grid_lay2.addWidget(self.box_type_label, 0, 0, 1, 2)
  184. grid_lay2.addWidget(self.box_type_radio, 1, 0, 1, 2)
  185. # Object used as BOX reference
  186. self.box_combo = QtWidgets.QComboBox()
  187. self.box_combo.setModel(self.app.collection)
  188. self.box_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  189. self.box_combo.setCurrentIndex(1)
  190. self.box_combo.hide()
  191. grid_lay2.addWidget(self.box_combo, 3, 0, 1, 2)
  192. separator_line = QtWidgets.QFrame()
  193. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  194. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  195. grid_lay2.addWidget(separator_line, 4, 0, 1, 2)
  196. grid_lay2.addWidget(QtWidgets.QLabel(""), 5, 0, 1, 2)
  197. # ## Title Bounds Values
  198. self.bv_label = QtWidgets.QLabel("<b>%s:</b>" % _('Bounds Values'))
  199. self.bv_label.setToolTip(
  200. _("Select on canvas the object(s)\n"
  201. "for which to calculate bounds values.")
  202. )
  203. grid_lay2.addWidget(self.bv_label, 6, 0, 1, 2)
  204. # Xmin value
  205. self.xmin_entry = FCDoubleSpinner()
  206. self.xmin_entry.set_precision(self.decimals)
  207. self.xmin_entry.set_range(-9999.9999, 9999.9999)
  208. self.xmin_btn = FCButton('%s:' % _("X min"))
  209. self.xmin_btn.setToolTip(
  210. _("Minimum location.")
  211. )
  212. self.xmin_entry.setReadOnly(True)
  213. grid_lay2.addWidget(self.xmin_btn, 7, 0)
  214. grid_lay2.addWidget(self.xmin_entry, 7, 1)
  215. # Ymin value
  216. self.ymin_entry = FCDoubleSpinner()
  217. self.ymin_entry.set_precision(self.decimals)
  218. self.ymin_entry.set_range(-9999.9999, 9999.9999)
  219. self.ymin_btn = FCButton('%s:' % _("Y min"))
  220. self.ymin_btn.setToolTip(
  221. _("Minimum location.")
  222. )
  223. self.ymin_entry.setReadOnly(True)
  224. grid_lay2.addWidget(self.ymin_btn, 8, 0)
  225. grid_lay2.addWidget(self.ymin_entry, 8, 1)
  226. # Xmax value
  227. self.xmax_entry = FCDoubleSpinner()
  228. self.xmax_entry.set_precision(self.decimals)
  229. self.xmax_entry.set_range(-9999.9999, 9999.9999)
  230. self.xmax_btn = FCButton('%s:' % _("X max"))
  231. self.xmax_btn.setToolTip(
  232. _("Maximum location.")
  233. )
  234. self.xmax_entry.setReadOnly(True)
  235. grid_lay2.addWidget(self.xmax_btn, 9, 0)
  236. grid_lay2.addWidget(self.xmax_entry, 9, 1)
  237. # Ymax value
  238. self.ymax_entry = FCDoubleSpinner()
  239. self.ymax_entry.set_precision(self.decimals)
  240. self.ymax_entry.set_range(-9999.9999, 9999.9999)
  241. self.ymax_btn = FCButton('%s:' % _("Y max"))
  242. self.ymax_btn.setToolTip(
  243. _("Maximum location.")
  244. )
  245. self.ymax_entry.setReadOnly(True)
  246. grid_lay2.addWidget(self.ymax_btn, 10, 0)
  247. grid_lay2.addWidget(self.ymax_entry, 10, 1)
  248. # Center point value
  249. self.center_entry = FCEntry()
  250. self.center_btn = FCButton('%s:' % _("Centroid"))
  251. self.center_btn.setToolTip(
  252. _("The center point location for the rectangular\n"
  253. "bounding shape. Centroid. Format is (x, y).")
  254. )
  255. self.center_entry.setReadOnly(True)
  256. grid_lay2.addWidget(self.center_btn, 12, 0)
  257. grid_lay2.addWidget(self.center_entry, 12, 1)
  258. # Calculate Bounding box
  259. self.calculate_bb_button = QtWidgets.QPushButton(_("Calculate Bounds Values"))
  260. self.calculate_bb_button.setToolTip(
  261. _("Calculate the enveloping rectangular shape coordinates,\n"
  262. "for the selection of objects.\n"
  263. "The envelope shape is parallel with the X, Y axis.")
  264. )
  265. self.calculate_bb_button.setStyleSheet("""
  266. QPushButton
  267. {
  268. font-weight: bold;
  269. }
  270. """)
  271. grid_lay2.addWidget(self.calculate_bb_button, 13, 0, 1, 2)
  272. separator_line = QtWidgets.QFrame()
  273. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  274. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  275. grid_lay2.addWidget(separator_line, 14, 0, 1, 2)
  276. grid_lay2.addWidget(QtWidgets.QLabel(""), 15, 0, 1, 2)
  277. # ## Alignment holes
  278. self.alignment_label = QtWidgets.QLabel("<b>%s:</b>" % _('PCB Alignment'))
  279. self.alignment_label.setToolTip(
  280. _("Creates an Excellon Object containing the\n"
  281. "specified alignment holes and their mirror\n"
  282. "images.")
  283. )
  284. grid_lay2.addWidget(self.alignment_label, 25, 0, 1, 2)
  285. # ## Drill diameter for alignment holes
  286. self.dt_label = QtWidgets.QLabel("%s:" % _('Drill Diameter'))
  287. self.dt_label.setToolTip(
  288. _("Diameter of the drill for the alignment holes.")
  289. )
  290. self.drill_dia = FCDoubleSpinner()
  291. self.drill_dia.setToolTip(
  292. _("Diameter of the drill for the alignment holes.")
  293. )
  294. self.drill_dia.set_precision(self.decimals)
  295. self.drill_dia.set_range(0.0000, 9999.9999)
  296. grid_lay2.addWidget(self.dt_label, 26, 0)
  297. grid_lay2.addWidget(self.drill_dia, 26, 1)
  298. # ## Alignment Axis
  299. self.align_ax_label = QtWidgets.QLabel('%s:' % _("Align Axis"))
  300. self.align_ax_label.setToolTip(
  301. _("Mirror vertically (X) or horizontally (Y).")
  302. )
  303. self.align_axis_radio = RadioSet([{'label': 'X', 'value': 'X'},
  304. {'label': 'Y', 'value': 'Y'}])
  305. grid_lay2.addWidget(self.align_ax_label, 27, 0)
  306. grid_lay2.addWidget(self.align_axis_radio, 27, 1)
  307. # ## Alignment Reference Point
  308. self.align_ref_label = QtWidgets.QLabel('%s:' % _("Reference"))
  309. self.align_ref_label.setToolTip(
  310. _("The reference point used to create the second alignment drill\n"
  311. "from the first alignment drill, by doing mirror.\n"
  312. "It can be modified in the Mirror Parameters -> Reference section")
  313. )
  314. self.align_ref_label_val = EvalEntry()
  315. self.align_ref_label_val.setToolTip(
  316. _("The reference point used to create the second alignment drill\n"
  317. "from the first alignment drill, by doing mirror.\n"
  318. "It can be modified in the Mirror Parameters -> Reference section")
  319. )
  320. self.align_ref_label_val.setDisabled(True)
  321. grid_lay2.addWidget(self.align_ref_label, 28, 0)
  322. grid_lay2.addWidget(self.align_ref_label_val, 28, 1)
  323. grid_lay4 = QtWidgets.QGridLayout()
  324. self.layout.addLayout(grid_lay4)
  325. # ## Alignment holes
  326. self.ah_label = QtWidgets.QLabel("%s:" % _('Alignment Drill Coordinates'))
  327. self.ah_label.setToolTip(
  328. _("Alignment holes (x1, y1), (x2, y2), ... "
  329. "on one side of the mirror axis. For each set of (x, y) coordinates\n"
  330. "entered here, a pair of drills will be created:\n\n"
  331. "- one drill at the coordinates from the field\n"
  332. "- one drill in mirror position over the axis selected above in the 'Align Axis'.")
  333. )
  334. self.alignment_holes = EvalEntry()
  335. grid_lay4.addWidget(self.ah_label, 0, 0, 1, 2)
  336. grid_lay4.addWidget(self.alignment_holes, 1, 0, 1, 2)
  337. self.add_drill_point_button = FCButton(_("Add"))
  338. self.add_drill_point_button.setToolTip(
  339. _("Add alignment drill holes coordinates in the format: (x1, y1), (x2, y2), ... \n"
  340. "on one side of the alignment axis.\n\n"
  341. "The coordinates set can be obtained:\n"
  342. "- press SHIFT key and left mouse clicking on canvas. Then click Add.\n"
  343. "- press SHIFT key and left mouse clicking on canvas. Then CTRL+V in the field.\n"
  344. "- press SHIFT key and left mouse clicking on canvas. Then RMB click in the field and click Paste.\n"
  345. "- by entering the coords manually in the format: (x1, y1), (x2, y2), ...")
  346. )
  347. # self.add_drill_point_button.setStyleSheet("""
  348. # QPushButton
  349. # {
  350. # font-weight: bold;
  351. # }
  352. # """)
  353. self.delete_drill_point_button = FCButton(_("Delete Last"))
  354. self.delete_drill_point_button.setToolTip(
  355. _("Delete the last coordinates tuple in the list.")
  356. )
  357. drill_hlay = QtWidgets.QHBoxLayout()
  358. drill_hlay.addWidget(self.add_drill_point_button)
  359. drill_hlay.addWidget(self.delete_drill_point_button)
  360. grid_lay4.addLayout(drill_hlay, 2, 0, 1, 2)
  361. # ## Buttons
  362. self.create_alignment_hole_button = QtWidgets.QPushButton(_("Create Excellon Object"))
  363. self.create_alignment_hole_button.setToolTip(
  364. _("Creates an Excellon Object containing the\n"
  365. "specified alignment holes and their mirror\n"
  366. "images.")
  367. )
  368. self.create_alignment_hole_button.setStyleSheet("""
  369. QPushButton
  370. {
  371. font-weight: bold;
  372. }
  373. """)
  374. self.layout.addWidget(self.create_alignment_hole_button)
  375. self.layout.addStretch()
  376. # ## Reset Tool
  377. self.reset_button = QtWidgets.QPushButton(_("Reset Tool"))
  378. self.reset_button.setToolTip(
  379. _("Will reset the tool parameters.")
  380. )
  381. self.reset_button.setStyleSheet("""
  382. QPushButton
  383. {
  384. font-weight: bold;
  385. }
  386. """)
  387. self.layout.addWidget(self.reset_button)
  388. # ## Signals
  389. self.mirror_gerber_button.clicked.connect(self.on_mirror_gerber)
  390. self.mirror_exc_button.clicked.connect(self.on_mirror_exc)
  391. self.mirror_geo_button.clicked.connect(self.on_mirror_geo)
  392. self.add_point_button.clicked.connect(self.on_point_add)
  393. self.add_drill_point_button.clicked.connect(self.on_drill_add)
  394. self.delete_drill_point_button.clicked.connect(self.on_drill_delete_last)
  395. self.box_type_radio.activated_custom.connect(self.on_combo_box_type)
  396. self.axis_location.group_toggle_fn = self.on_toggle_pointbox
  397. self.point_entry.textChanged.connect(lambda val: self.align_ref_label_val.set_value(val))
  398. self.xmin_btn.clicked.connect(self.on_xmin_clicked)
  399. self.ymin_btn.clicked.connect(self.on_ymin_clicked)
  400. self.xmax_btn.clicked.connect(self.on_xmax_clicked)
  401. self.ymax_btn.clicked.connect(self.on_ymax_clicked)
  402. self.center_btn.clicked.connect(
  403. lambda: self.point_entry.set_value(self.center_entry.get_value())
  404. )
  405. self.create_alignment_hole_button.clicked.connect(self.on_create_alignment_holes)
  406. self.calculate_bb_button.clicked.connect(self.on_bbox_coordinates)
  407. self.reset_button.clicked.connect(self.set_tool_ui)
  408. self.drill_values = ""
  409. def install(self, icon=None, separator=None, **kwargs):
  410. FlatCAMTool.install(self, icon, separator, shortcut='ALT+D', **kwargs)
  411. def run(self, toggle=True):
  412. self.app.report_usage("Tool2Sided()")
  413. if toggle:
  414. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  415. if self.app.ui.splitter.sizes()[0] == 0:
  416. self.app.ui.splitter.setSizes([1, 1])
  417. else:
  418. try:
  419. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  420. # if tab is populated with the tool but it does not have the focus, focus on it
  421. if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
  422. # focus on Tool Tab
  423. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  424. else:
  425. self.app.ui.splitter.setSizes([0, 1])
  426. except AttributeError:
  427. pass
  428. else:
  429. if self.app.ui.splitter.sizes()[0] == 0:
  430. self.app.ui.splitter.setSizes([1, 1])
  431. FlatCAMTool.run(self)
  432. self.set_tool_ui()
  433. self.app.ui.notebook.setTabText(2, _("2-Sided Tool"))
  434. def set_tool_ui(self):
  435. self.reset_fields()
  436. self.point_entry.set_value("")
  437. self.alignment_holes.set_value("")
  438. self.mirror_axis.set_value(self.app.defaults["tools_2sided_mirror_axis"])
  439. self.axis_location.set_value(self.app.defaults["tools_2sided_axis_loc"])
  440. self.drill_dia.set_value(self.app.defaults["tools_2sided_drilldia"])
  441. self.align_axis_radio.set_value(self.app.defaults["tools_2sided_allign_axis"])
  442. self.xmin_entry.set_value(0.0)
  443. self.ymin_entry.set_value(0.0)
  444. self.xmax_entry.set_value(0.0)
  445. self.ymax_entry.set_value(0.0)
  446. self.center_entry.set_value('')
  447. self.align_ref_label_val.set_value('%.*f' % (self.decimals, 0.0))
  448. def on_combo_box_type(self, val):
  449. obj_type = {
  450. 'grb': 0,
  451. 'exc': 1,
  452. 'geo': 2
  453. }[val]
  454. self.box_combo.setRootModelIndex(self.app.collection.index(obj_type, 0, QtCore.QModelIndex()))
  455. self.box_combo.setCurrentIndex(0)
  456. def on_create_alignment_holes(self):
  457. axis = self.align_axis_radio.get_value()
  458. mode = self.axis_location.get_value()
  459. if mode == "point":
  460. try:
  461. px, py = self.point_entry.get_value()
  462. except TypeError:
  463. self.app.inform.emit('[WARNING_NOTCL] %s' % _("'Point' reference is selected and 'Point' coordinates "
  464. "are missing. Add them and retry."))
  465. return
  466. else:
  467. selection_index = self.box_combo.currentIndex()
  468. model_index = self.app.collection.index(selection_index, 0, self.gerber_object_combo.rootModelIndex())
  469. try:
  470. bb_obj = model_index.internalPointer().obj
  471. except AttributeError:
  472. model_index = self.app.collection.index(selection_index, 0, self.exc_object_combo.rootModelIndex())
  473. try:
  474. bb_obj = model_index.internalPointer().obj
  475. except AttributeError:
  476. model_index = self.app.collection.index(selection_index, 0,
  477. self.geo_object_combo.rootModelIndex())
  478. try:
  479. bb_obj = model_index.internalPointer().obj
  480. except AttributeError:
  481. self.app.inform.emit(
  482. '[WARNING_NOTCL] %s' % _("There is no Box reference object loaded. Load one and retry."))
  483. return
  484. xmin, ymin, xmax, ymax = bb_obj.bounds()
  485. px = 0.5 * (xmin + xmax)
  486. py = 0.5 * (ymin + ymax)
  487. xscale, yscale = {"X": (1.0, -1.0), "Y": (-1.0, 1.0)}[axis]
  488. dia = float(self.drill_dia.get_value())
  489. if dia is '':
  490. self.app.inform.emit('[WARNING_NOTCL] %s' %
  491. _("No value or wrong format in Drill Dia entry. Add it and retry."))
  492. return
  493. tools = dict()
  494. tools["1"] = dict()
  495. tools["1"]["C"] = dia
  496. tools["1"]['solid_geometry'] = list()
  497. # holes = self.alignment_holes.get_value()
  498. holes = eval('[{}]'.format(self.alignment_holes.text()))
  499. if not holes:
  500. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There are no Alignment Drill Coordinates to use. "
  501. "Add them and retry."))
  502. return
  503. drills = list()
  504. for hole in holes:
  505. point = Point(hole)
  506. point_mirror = affinity.scale(point, xscale, yscale, origin=(px, py))
  507. drills.append({"point": point, "tool": "1"})
  508. drills.append({"point": point_mirror, "tool": "1"})
  509. tools["1"]['solid_geometry'].append(point)
  510. tools["1"]['solid_geometry'].append(point_mirror)
  511. def obj_init(obj_inst, app_inst):
  512. obj_inst.tools = tools
  513. obj_inst.drills = drills
  514. obj_inst.create_geometry()
  515. obj_inst.source_file = app_inst.export_excellon(obj_name=obj_inst.options['name'], local_use=obj_inst,
  516. filename=None, use_thread=False)
  517. self.app.new_object("excellon", "Alignment Drills", obj_init)
  518. self.drill_values = ''
  519. self.app.inform.emit('[success] %s' % _("Excellon object with alignment drills created..."))
  520. def on_mirror_gerber(self):
  521. selection_index = self.gerber_object_combo.currentIndex()
  522. # fcobj = self.app.collection.object_list[selection_index]
  523. model_index = self.app.collection.index(selection_index, 0, self.gerber_object_combo.rootModelIndex())
  524. try:
  525. fcobj = model_index.internalPointer().obj
  526. except Exception:
  527. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Gerber object loaded ..."))
  528. return
  529. if not isinstance(fcobj, FlatCAMGerber):
  530. self.app.inform.emit('[ERROR_NOTCL] %s' % _("Only Gerber, Excellon and Geometry objects can be mirrored."))
  531. return
  532. axis = self.mirror_axis.get_value()
  533. mode = self.axis_location.get_value()
  534. if mode == "point":
  535. try:
  536. px, py = self.point_entry.get_value()
  537. except TypeError:
  538. self.app.inform.emit('[WARNING_NOTCL] %s' % _("'Point' coordinates missing. "
  539. "Using Origin (0, 0) as mirroring reference."))
  540. px, py = (0, 0)
  541. else:
  542. selection_index_box = self.box_combo.currentIndex()
  543. model_index_box = self.app.collection.index(selection_index_box, 0, self.box_combo.rootModelIndex())
  544. try:
  545. bb_obj = model_index_box.internalPointer().obj
  546. except Exception:
  547. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Box object loaded ..."))
  548. return
  549. xmin, ymin, xmax, ymax = bb_obj.bounds()
  550. px = 0.5 * (xmin + xmax)
  551. py = 0.5 * (ymin + ymax)
  552. fcobj.mirror(axis, [px, py])
  553. self.app.object_changed.emit(fcobj)
  554. fcobj.plot()
  555. self.app.inform.emit('[success] Gerber %s %s...' % (str(fcobj.options['name']), _("was mirrored")))
  556. def on_mirror_exc(self):
  557. selection_index = self.exc_object_combo.currentIndex()
  558. # fcobj = self.app.collection.object_list[selection_index]
  559. model_index = self.app.collection.index(selection_index, 0, self.exc_object_combo.rootModelIndex())
  560. try:
  561. fcobj = model_index.internalPointer().obj
  562. except Exception:
  563. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Excellon object loaded ..."))
  564. return
  565. if not isinstance(fcobj, FlatCAMExcellon):
  566. self.app.inform.emit('[ERROR_NOTCL] %s' % _("Only Gerber, Excellon and Geometry objects can be mirrored."))
  567. return
  568. axis = self.mirror_axis.get_value()
  569. mode = self.axis_location.get_value()
  570. if mode == "point":
  571. try:
  572. px, py = self.point_entry.get_value()
  573. except Exception as e:
  574. log.debug("DblSidedTool.on_mirror_geo() --> %s" % str(e))
  575. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There are no Point coordinates in the Point field. "
  576. "Add coords and try again ..."))
  577. return
  578. else:
  579. selection_index_box = self.box_combo.currentIndex()
  580. model_index_box = self.app.collection.index(selection_index_box, 0, self.box_combo.rootModelIndex())
  581. try:
  582. bb_obj = model_index_box.internalPointer().obj
  583. except Exception as e:
  584. log.debug("DblSidedTool.on_mirror_geo() --> %s" % str(e))
  585. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Box object loaded ..."))
  586. return
  587. xmin, ymin, xmax, ymax = bb_obj.bounds()
  588. px = 0.5 * (xmin + xmax)
  589. py = 0.5 * (ymin + ymax)
  590. fcobj.mirror(axis, [px, py])
  591. self.app.object_changed.emit(fcobj)
  592. fcobj.plot()
  593. self.app.inform.emit('[success] Excellon %s %s...' % (str(fcobj.options['name']), _("was mirrored")))
  594. def on_mirror_geo(self):
  595. selection_index = self.geo_object_combo.currentIndex()
  596. # fcobj = self.app.collection.object_list[selection_index]
  597. model_index = self.app.collection.index(selection_index, 0, self.geo_object_combo.rootModelIndex())
  598. try:
  599. fcobj = model_index.internalPointer().obj
  600. except Exception:
  601. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Geometry object loaded ..."))
  602. return
  603. if not isinstance(fcobj, FlatCAMGeometry):
  604. self.app.inform.emit('[ERROR_NOTCL] %s' % _("Only Gerber, Excellon and Geometry objects can be mirrored."))
  605. return
  606. axis = self.mirror_axis.get_value()
  607. mode = self.axis_location.get_value()
  608. if mode == "point":
  609. px, py = self.point_entry.get_value()
  610. else:
  611. selection_index_box = self.box_combo.currentIndex()
  612. model_index_box = self.app.collection.index(selection_index_box, 0, self.box_combo.rootModelIndex())
  613. try:
  614. bb_obj = model_index_box.internalPointer().obj
  615. except Exception:
  616. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Box object loaded ..."))
  617. return
  618. xmin, ymin, xmax, ymax = bb_obj.bounds()
  619. px = 0.5 * (xmin + xmax)
  620. py = 0.5 * (ymin + ymax)
  621. fcobj.mirror(axis, [px, py])
  622. self.app.object_changed.emit(fcobj)
  623. fcobj.plot()
  624. self.app.inform.emit('[success] Geometry %s %s...' % (str(fcobj.options['name']), _("was mirrored")))
  625. def on_point_add(self):
  626. val = self.app.defaults["global_point_clipboard_format"] % \
  627. (self.decimals, self.app.pos[0], self.decimals, self.app.pos[1])
  628. self.point_entry.set_value(val)
  629. def on_drill_add(self):
  630. self.drill_values += (self.app.defaults["global_point_clipboard_format"] %
  631. (self.decimals, self.app.pos[0], self.decimals, self.app.pos[1])) + ','
  632. self.alignment_holes.set_value(self.drill_values)
  633. def on_drill_delete_last(self):
  634. drill_values_without_last_tupple = self.drill_values.rpartition('(')[0]
  635. self.drill_values = drill_values_without_last_tupple
  636. self.alignment_holes.set_value(self.drill_values)
  637. def on_toggle_pointbox(self):
  638. if self.axis_location.get_value() == "point":
  639. self.point_entry.show()
  640. self.add_point_button.show()
  641. self.box_type_label.hide()
  642. self.box_type_radio.hide()
  643. self.box_combo.hide()
  644. else:
  645. self.point_entry.hide()
  646. self.add_point_button.hide()
  647. self.box_type_label.show()
  648. self.box_type_radio.show()
  649. self.box_combo.show()
  650. self.align_ref_label_val.set_value("Box centroid")
  651. def on_bbox_coordinates(self):
  652. xmin = Inf
  653. ymin = Inf
  654. xmax = -Inf
  655. ymax = -Inf
  656. obj_list = self.app.collection.get_selected()
  657. if not obj_list:
  658. self.app.inform.emit('[ERROR_NOTCL] %s' % _("Failed. No object(s) selected..."))
  659. return
  660. for obj in obj_list:
  661. try:
  662. gxmin, gymin, gxmax, gymax = obj.bounds()
  663. xmin = min([xmin, gxmin])
  664. ymin = min([ymin, gymin])
  665. xmax = max([xmax, gxmax])
  666. ymax = max([ymax, gymax])
  667. except Exception as e:
  668. log.warning("DEV WARNING: Tried to get bounds of empty geometry in DblSidedTool. %s" % str(e))
  669. self.xmin_entry.set_value(xmin)
  670. self.ymin_entry.set_value(ymin)
  671. self.xmax_entry.set_value(xmax)
  672. self.ymax_entry.set_value(ymax)
  673. cx = '%.*f' % (self.decimals, (((xmax - xmin) / 2.0) + xmin))
  674. cy = '%.*f' % (self.decimals, (((ymax - ymin) / 2.0) + ymin))
  675. val_txt = '(%s, %s)' % (cx, cy)
  676. self.center_entry.set_value(val_txt)
  677. self.axis_location.set_value('point')
  678. self.point_entry.set_value(val_txt)
  679. self.app.delete_selection_shape()
  680. def on_xmin_clicked(self):
  681. xmin = self.xmin_entry.get_value()
  682. self.axis_location.set_value('point')
  683. try:
  684. px, py = self.point_entry.get_value()
  685. val = self.app.defaults["global_point_clipboard_format"] % (self.decimals, xmin, self.decimals, py)
  686. except TypeError:
  687. val = self.app.defaults["global_point_clipboard_format"] % (self.decimals, xmin, self.decimals, 0.0)
  688. self.point_entry.set_value(val)
  689. def on_ymin_clicked(self):
  690. ymin = self.ymin_entry.get_value()
  691. self.axis_location.set_value('point')
  692. try:
  693. px, py = self.point_entry.get_value()
  694. val = self.app.defaults["global_point_clipboard_format"] % (self.decimals, px, self.decimals, ymin)
  695. except TypeError:
  696. val = self.app.defaults["global_point_clipboard_format"] % (self.decimals, 0.0, self.decimals, ymin)
  697. self.point_entry.set_value(val)
  698. def on_xmax_clicked(self):
  699. xmax = self.xmax_entry.get_value()
  700. self.axis_location.set_value('point')
  701. try:
  702. px, py = self.point_entry.get_value()
  703. val = self.app.defaults["global_point_clipboard_format"] % (self.decimals, xmax, self.decimals, py)
  704. except TypeError:
  705. val = self.app.defaults["global_point_clipboard_format"] % (self.decimals, xmax, self.decimals, 0.0)
  706. self.point_entry.set_value(val)
  707. def on_ymax_clicked(self):
  708. ymax = self.ymax_entry.get_value()
  709. self.axis_location.set_value('point')
  710. try:
  711. px, py = self.point_entry.get_value()
  712. val = self.app.defaults["global_point_clipboard_format"] % (self.decimals, px, self.decimals, ymax)
  713. except TypeError:
  714. val = self.app.defaults["global_point_clipboard_format"] % (self.decimals, 0.0, self.decimals, ymax)
  715. self.point_entry.set_value(val)
  716. def reset_fields(self):
  717. self.gerber_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  718. self.exc_object_combo.setRootModelIndex(self.app.collection.index(1, 0, QtCore.QModelIndex()))
  719. self.geo_object_combo.setRootModelIndex(self.app.collection.index(2, 0, QtCore.QModelIndex()))
  720. self.box_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  721. self.gerber_object_combo.setCurrentIndex(0)
  722. self.exc_object_combo.setCurrentIndex(0)
  723. self.geo_object_combo.setCurrentIndex(0)
  724. self.box_combo.setCurrentIndex(0)
  725. self.box_type_radio.set_value('grb')
  726. self.drill_values = ""
  727. self.align_ref_label_val.set_value('')