ToolDblSided.py 36 KB

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