ToolDblSided.py 36 KB

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