ToolDblSided.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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(_("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 coords 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:' % _("Object Type"))
  172. self.box_type_label.setToolTip(
  173. _("It can be of type: Gerber or Excellon or Geometry.\n"
  174. "The selection here decide the type of objects that will be\n"
  175. "in the Object combobox.")
  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. self.box_object_label = QtWidgets.QLabel('%s:' % _("Object"))
  186. self.box_object_label.setToolTip(
  187. _("Object to be used as mirror reference.")
  188. )
  189. # Object used as BOX reference
  190. self.box_combo = QtWidgets.QComboBox()
  191. self.box_combo.setModel(self.app.collection)
  192. self.box_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  193. self.box_combo.setCurrentIndex(1)
  194. self.box_object_label.hide()
  195. self.box_combo.hide()
  196. grid_lay2.addWidget(self.box_object_label, 2, 0, 1, 2)
  197. grid_lay2.addWidget(self.box_combo, 3, 0, 1, 2)
  198. separator_line = QtWidgets.QFrame()
  199. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  200. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  201. self.layout.addWidget(separator_line)
  202. self.layout.addWidget(QtWidgets.QLabel(""))
  203. # ## Alignment holes
  204. self.alignment_label = QtWidgets.QLabel("<b>%s:</b>" % _('Alignment Excellon object'))
  205. self.alignment_label.setToolTip(
  206. _("Creates an Excellon Object containing the\n"
  207. "specified alignment holes and their mirror\n"
  208. "images.")
  209. )
  210. self.layout.addWidget(self.alignment_label)
  211. # ## Alignment holes
  212. self.ah_label = QtWidgets.QLabel("%s:" % _('Alignment Drill Coordinates'))
  213. self.ah_label.setToolTip(
  214. _("Alignment holes (x1, y1), (x2, y2), ... "
  215. "on one side of the mirror axis. For each set of (x, y) coordinates\n"
  216. "entered here, a pair of drills will be created:\n\n"
  217. "- one drill at the coordinates from the field\n"
  218. "- one drill in mirror position over the axis selected above in the 'Mirror Axis'.")
  219. )
  220. self.layout.addWidget(self.ah_label)
  221. grid_lay3 = QtWidgets.QGridLayout()
  222. self.layout.addLayout(grid_lay3)
  223. self.alignment_holes = EvalEntry()
  224. grid_lay3.addWidget(self.alignment_holes, 0, 0, 1, 2)
  225. self.add_drill_point_button = FCButton(_("Add"))
  226. self.add_drill_point_button.setToolTip(
  227. _("Add alignment drill holes coords in the format: (x1, y1), (x2, y2), ... \n"
  228. "on one side of the mirror axis.\n\n"
  229. "The coordinates set can be obtained:\n"
  230. "- press SHIFT key and left mouse clicking on canvas. Then click Add.\n"
  231. "- press SHIFT key and left mouse clicking on canvas. Then CTRL+V in the field.\n"
  232. "- press SHIFT key and left mouse clicking on canvas. Then RMB click in the field and click Paste.\n"
  233. "- by entering the coords manually in the format: (x1, y1), (x2, y2), ...")
  234. )
  235. self.add_drill_point_button.setStyleSheet("""
  236. QPushButton
  237. {
  238. font-weight: bold;
  239. }
  240. """)
  241. self.delete_drill_point_button = FCButton(_("Delete Last"))
  242. self.delete_drill_point_button.setToolTip(
  243. _("Delete the last coordinates tupple in the list.")
  244. )
  245. drill_hlay = QtWidgets.QHBoxLayout()
  246. drill_hlay.addWidget(self.add_drill_point_button)
  247. drill_hlay.addWidget(self.delete_drill_point_button)
  248. grid_lay3.addLayout(drill_hlay, 1, 0, 1, 2)
  249. grid0 = QtWidgets.QGridLayout()
  250. self.layout.addLayout(grid0)
  251. grid0.setColumnStretch(0, 0)
  252. grid0.setColumnStretch(1, 1)
  253. # ## Drill diameter for alignment holes
  254. self.dt_label = QtWidgets.QLabel("%s:" % _('Alignment Drill Diameter'))
  255. self.dt_label.setToolTip(
  256. _("Diameter of the drill for the "
  257. "alignment holes.")
  258. )
  259. grid0.addWidget(self.dt_label, 0, 0, 1, 2)
  260. # Drill diameter value
  261. self.drill_dia = FCDoubleSpinner()
  262. self.drill_dia.set_precision(self.decimals)
  263. self.drill_dia.set_range(0.0000, 9999.9999)
  264. self.drill_dia.setToolTip(
  265. _("Diameter of the drill for the "
  266. "alignment holes.")
  267. )
  268. grid0.addWidget(self.drill_dia, 1, 0, 1, 2)
  269. # ## Buttons
  270. self.create_alignment_hole_button = QtWidgets.QPushButton(_("Create Excellon Object"))
  271. self.create_alignment_hole_button.setToolTip(
  272. _("Creates an Excellon Object containing the\n"
  273. "specified alignment holes and their mirror\n"
  274. "images.")
  275. )
  276. self.create_alignment_hole_button.setStyleSheet("""
  277. QPushButton
  278. {
  279. font-weight: bold;
  280. }
  281. """)
  282. self.layout.addWidget(self.create_alignment_hole_button)
  283. separator_line = QtWidgets.QFrame()
  284. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  285. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  286. self.layout.addWidget(separator_line)
  287. self.layout.addWidget(QtWidgets.QLabel(''))
  288. grid1 = QtWidgets.QGridLayout()
  289. self.layout.addLayout(grid1)
  290. grid1.setColumnStretch(0, 0)
  291. grid1.setColumnStretch(1, 1)
  292. # ## Title Bounds Values
  293. self.bv_label = QtWidgets.QLabel("<b>%s:</b>" % _('Bounds Values'))
  294. self.bv_label.setToolTip(
  295. _("Select on canvas the object(s)\n"
  296. "for which to calculate bounds values.")
  297. )
  298. grid1.addWidget(self.bv_label, 0, 0, 1, 2)
  299. # Xmin value
  300. self.xmin_entry = FCDoubleSpinner()
  301. self.xmin_entry.set_precision(self.decimals)
  302. self.xmin_entry.set_range(-9999.9999, 9999.9999)
  303. self.xmin_label = QtWidgets.QLabel('%s:' % _("X min"))
  304. self.xmin_label.setToolTip(
  305. _("Minimum location.")
  306. )
  307. self.xmin_entry.setReadOnly(True)
  308. grid1.addWidget(self.xmin_label, 1, 0)
  309. grid1.addWidget(self.xmin_entry, 1, 1)
  310. # Ymin value
  311. self.ymin_entry = FCDoubleSpinner()
  312. self.ymin_entry.set_precision(self.decimals)
  313. self.ymin_entry.set_range(-9999.9999, 9999.9999)
  314. self.ymin_label = QtWidgets.QLabel('%s:' % _("Y min"))
  315. self.ymin_label.setToolTip(
  316. _("Minimum location.")
  317. )
  318. self.ymin_entry.setReadOnly(True)
  319. grid1.addWidget(self.ymin_label, 2, 0)
  320. grid1.addWidget(self.ymin_entry, 2, 1)
  321. # Xmax value
  322. self.xmax_entry = FCDoubleSpinner()
  323. self.xmax_entry.set_precision(self.decimals)
  324. self.xmax_entry.set_range(-9999.9999, 9999.9999)
  325. self.xmax_label = QtWidgets.QLabel('%s:' % _("X max"))
  326. self.xmax_label.setToolTip(
  327. _("Maximum location.")
  328. )
  329. self.xmax_entry.setReadOnly(True)
  330. grid1.addWidget(self.xmax_label, 3, 0)
  331. grid1.addWidget(self.xmax_entry, 3, 1)
  332. # Ymax value
  333. self.ymax_entry = FCDoubleSpinner()
  334. self.ymax_entry.set_precision(self.decimals)
  335. self.ymax_entry.set_range(-9999.9999, 9999.9999)
  336. self.ymax_label = QtWidgets.QLabel('%s:' % _("Y max"))
  337. self.ymax_label.setToolTip(
  338. _("Maximum location.")
  339. )
  340. self.ymax_entry.setReadOnly(True)
  341. grid1.addWidget(self.ymax_label, 4, 0)
  342. grid1.addWidget(self.ymax_entry, 4, 1)
  343. # Center point value
  344. self.center_entry = FCEntry()
  345. self.center_label = QtWidgets.QLabel('%s:' % _("Centroid"))
  346. self.center_label.setToolTip(
  347. _("The center point location for the rectangular\n"
  348. "bounding shape. Centroid. Format is (x, y).")
  349. )
  350. self.center_entry.setReadOnly(True)
  351. grid1.addWidget(self.center_label, 5, 0)
  352. grid1.addWidget(self.center_entry, 5, 1)
  353. # Calculate Bounding box
  354. self.calculate_bb_button = QtWidgets.QPushButton(_("Calculate Bounds Values"))
  355. self.calculate_bb_button.setToolTip(
  356. _("Calculate the enveloping rectangular shape coordinates,\n"
  357. "for the selection of objects.\n"
  358. "The envelope shape is parallel with the X, Y axis.")
  359. )
  360. self.calculate_bb_button.setStyleSheet("""
  361. QPushButton
  362. {
  363. font-weight: bold;
  364. }
  365. """)
  366. self.layout.addWidget(self.calculate_bb_button)
  367. self.layout.addStretch()
  368. # ## Reset Tool
  369. self.reset_button = QtWidgets.QPushButton(_("Reset Tool"))
  370. self.reset_button.setToolTip(
  371. _("Will reset the tool parameters.")
  372. )
  373. self.reset_button.setStyleSheet("""
  374. QPushButton
  375. {
  376. font-weight: bold;
  377. }
  378. """)
  379. self.layout.addWidget(self.reset_button)
  380. # ## Signals
  381. self.mirror_gerber_button.clicked.connect(self.on_mirror_gerber)
  382. self.mirror_exc_button.clicked.connect(self.on_mirror_exc)
  383. self.mirror_geo_button.clicked.connect(self.on_mirror_geo)
  384. self.add_point_button.clicked.connect(self.on_point_add)
  385. self.add_drill_point_button.clicked.connect(self.on_drill_add)
  386. self.delete_drill_point_button.clicked.connect(self.on_drill_delete_last)
  387. self.box_type_radio.activated_custom.connect(self.on_combo_box_type)
  388. self.axis_location.group_toggle_fn = self.on_toggle_pointbox
  389. self.create_alignment_hole_button.clicked.connect(self.on_create_alignment_holes)
  390. self.calculate_bb_button.clicked.connect(self.on_bbox_coordinates)
  391. self.reset_button.clicked.connect(self.set_tool_ui)
  392. self.drill_values = ""
  393. def install(self, icon=None, separator=None, **kwargs):
  394. FlatCAMTool.install(self, icon, separator, shortcut='ALT+D', **kwargs)
  395. def run(self, toggle=True):
  396. self.app.report_usage("Tool2Sided()")
  397. if toggle:
  398. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  399. if self.app.ui.splitter.sizes()[0] == 0:
  400. self.app.ui.splitter.setSizes([1, 1])
  401. else:
  402. try:
  403. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  404. # if tab is populated with the tool but it does not have the focus, focus on it
  405. if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
  406. # focus on Tool Tab
  407. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  408. else:
  409. self.app.ui.splitter.setSizes([0, 1])
  410. except AttributeError:
  411. pass
  412. else:
  413. if self.app.ui.splitter.sizes()[0] == 0:
  414. self.app.ui.splitter.setSizes([1, 1])
  415. FlatCAMTool.run(self)
  416. self.set_tool_ui()
  417. self.app.ui.notebook.setTabText(2, _("2-Sided Tool"))
  418. def set_tool_ui(self):
  419. self.reset_fields()
  420. self.point_entry.set_value("")
  421. self.alignment_holes.set_value("")
  422. self.mirror_axis.set_value(self.app.defaults["tools_2sided_mirror_axis"])
  423. self.axis_location.set_value(self.app.defaults["tools_2sided_axis_loc"])
  424. self.drill_dia.set_value(self.app.defaults["tools_2sided_drilldia"])
  425. self.xmin_entry.set_value(0.0)
  426. self.ymin_entry.set_value(0.0)
  427. self.xmax_entry.set_value(0.0)
  428. self.ymax_entry.set_value(0.0)
  429. self.center_entry.set_value('')
  430. def on_combo_box_type(self, val):
  431. obj_type = {
  432. 'grb': 0,
  433. 'exc': 1,
  434. 'geo': 2
  435. }[val]
  436. self.box_combo.setRootModelIndex(self.app.collection.index(obj_type, 0, QtCore.QModelIndex()))
  437. self.box_combo.setCurrentIndex(0)
  438. def on_create_alignment_holes(self):
  439. axis = self.mirror_axis.get_value()
  440. mode = self.axis_location.get_value()
  441. if mode == "point":
  442. try:
  443. px, py = self.point_entry.get_value()
  444. except TypeError:
  445. self.app.inform.emit('[WARNING_NOTCL] %s' % _("'Point' reference is selected and 'Point' coordinates "
  446. "are missing. Add them and retry."))
  447. return
  448. else:
  449. selection_index = self.box_combo.currentIndex()
  450. model_index = self.app.collection.index(selection_index, 0, self.gerber_object_combo.rootModelIndex())
  451. try:
  452. bb_obj = model_index.internalPointer().obj
  453. except AttributeError:
  454. model_index = self.app.collection.index(selection_index, 0, self.exc_object_combo.rootModelIndex())
  455. try:
  456. bb_obj = model_index.internalPointer().obj
  457. except AttributeError:
  458. model_index = self.app.collection.index(selection_index, 0,
  459. self.geo_object_combo.rootModelIndex())
  460. try:
  461. bb_obj = model_index.internalPointer().obj
  462. except AttributeError:
  463. self.app.inform.emit(
  464. '[WARNING_NOTCL] %s' % _("There is no Box reference object loaded. Load one and retry."))
  465. return
  466. xmin, ymin, xmax, ymax = bb_obj.bounds()
  467. px = 0.5 * (xmin + xmax)
  468. py = 0.5 * (ymin + ymax)
  469. xscale, yscale = {"X": (1.0, -1.0), "Y": (-1.0, 1.0)}[axis]
  470. dia = float(self.drill_dia.get_value())
  471. if dia is '':
  472. self.app.inform.emit('[WARNING_NOTCL] %s' %
  473. _("No value or wrong format in Drill Dia entry. Add it and retry."))
  474. return
  475. tools = dict()
  476. tools["1"] = dict()
  477. tools["1"]["C"] = dia
  478. tools["1"]['solid_geometry'] = list()
  479. # holes = self.alignment_holes.get_value()
  480. holes = eval('[{}]'.format(self.alignment_holes.text()))
  481. if not holes:
  482. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There are no Alignment Drill Coordinates to use. "
  483. "Add them and retry."))
  484. return
  485. drills = list()
  486. for hole in holes:
  487. point = Point(hole)
  488. point_mirror = affinity.scale(point, xscale, yscale, origin=(px, py))
  489. drills.append({"point": point, "tool": "1"})
  490. drills.append({"point": point_mirror, "tool": "1"})
  491. tools["1"]['solid_geometry'].append(point)
  492. tools["1"]['solid_geometry'].append(point_mirror)
  493. def obj_init(obj_inst, app_inst):
  494. obj_inst.tools = tools
  495. obj_inst.drills = drills
  496. obj_inst.create_geometry()
  497. obj_inst.source_file = self.app.export_excellon(obj_name=obj_inst.options['name'], local_use=obj_inst,
  498. filename=None, use_thread=False)
  499. self.app.new_object("excellon", "Alignment Drills", obj_init)
  500. self.drill_values = ''
  501. self.app.inform.emit('[success] %s' % _("Excellon object with alignment drills created..."))
  502. def on_mirror_gerber(self):
  503. selection_index = self.gerber_object_combo.currentIndex()
  504. # fcobj = self.app.collection.object_list[selection_index]
  505. model_index = self.app.collection.index(selection_index, 0, self.gerber_object_combo.rootModelIndex())
  506. try:
  507. fcobj = model_index.internalPointer().obj
  508. except Exception:
  509. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Gerber object loaded ..."))
  510. return
  511. if not isinstance(fcobj, FlatCAMGerber):
  512. self.app.inform.emit('[ERROR_NOTCL] %s' % _("Only Gerber, Excellon and Geometry objects can be mirrored."))
  513. return
  514. axis = self.mirror_axis.get_value()
  515. mode = self.axis_location.get_value()
  516. if mode == "point":
  517. try:
  518. px, py = self.point_entry.get_value()
  519. except TypeError:
  520. self.app.inform.emit('[WARNING_NOTCL] %s' % _("'Point' coordinates missing. "
  521. "Using Origin (0, 0) as mirroring reference."))
  522. px, py = (0, 0)
  523. else:
  524. selection_index_box = self.box_combo.currentIndex()
  525. model_index_box = self.app.collection.index(selection_index_box, 0, self.box_combo.rootModelIndex())
  526. try:
  527. bb_obj = model_index_box.internalPointer().obj
  528. except Exception:
  529. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Box object loaded ..."))
  530. return
  531. xmin, ymin, xmax, ymax = bb_obj.bounds()
  532. px = 0.5 * (xmin + xmax)
  533. py = 0.5 * (ymin + ymax)
  534. fcobj.mirror(axis, [px, py])
  535. self.app.object_changed.emit(fcobj)
  536. fcobj.plot()
  537. self.app.inform.emit('[success] Gerber %s %s...' % (str(fcobj.options['name']), _("was mirrored")))
  538. def on_mirror_exc(self):
  539. selection_index = self.exc_object_combo.currentIndex()
  540. # fcobj = self.app.collection.object_list[selection_index]
  541. model_index = self.app.collection.index(selection_index, 0, self.exc_object_combo.rootModelIndex())
  542. try:
  543. fcobj = model_index.internalPointer().obj
  544. except Exception:
  545. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Excellon object loaded ..."))
  546. return
  547. if not isinstance(fcobj, FlatCAMExcellon):
  548. self.app.inform.emit('[ERROR_NOTCL] %s' % _("Only Gerber, Excellon and Geometry objects can be mirrored."))
  549. return
  550. axis = self.mirror_axis.get_value()
  551. mode = self.axis_location.get_value()
  552. if mode == "point":
  553. try:
  554. px, py = self.point_entry.get_value()
  555. except Exception as e:
  556. log.debug("DblSidedTool.on_mirror_geo() --> %s" % str(e))
  557. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There are no Point coordinates in the Point field. "
  558. "Add coords and try again ..."))
  559. return
  560. else:
  561. selection_index_box = self.box_combo.currentIndex()
  562. model_index_box = self.app.collection.index(selection_index_box, 0, self.box_combo.rootModelIndex())
  563. try:
  564. bb_obj = model_index_box.internalPointer().obj
  565. except Exception as e:
  566. log.debug("DblSidedTool.on_mirror_geo() --> %s" % str(e))
  567. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Box object loaded ..."))
  568. return
  569. xmin, ymin, xmax, ymax = bb_obj.bounds()
  570. px = 0.5 * (xmin + xmax)
  571. py = 0.5 * (ymin + ymax)
  572. fcobj.mirror(axis, [px, py])
  573. self.app.object_changed.emit(fcobj)
  574. fcobj.plot()
  575. self.app.inform.emit('[success] Excellon %s %s...' % (str(fcobj.options['name']), _("was mirrored")))
  576. def on_mirror_geo(self):
  577. selection_index = self.geo_object_combo.currentIndex()
  578. # fcobj = self.app.collection.object_list[selection_index]
  579. model_index = self.app.collection.index(selection_index, 0, self.geo_object_combo.rootModelIndex())
  580. try:
  581. fcobj = model_index.internalPointer().obj
  582. except Exception:
  583. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Geometry object loaded ..."))
  584. return
  585. if not isinstance(fcobj, FlatCAMGeometry):
  586. self.app.inform.emit('[ERROR_NOTCL] %s' % _("Only Gerber, Excellon and Geometry objects can be mirrored."))
  587. return
  588. axis = self.mirror_axis.get_value()
  589. mode = self.axis_location.get_value()
  590. if mode == "point":
  591. px, py = self.point_entry.get_value()
  592. else:
  593. selection_index_box = self.box_combo.currentIndex()
  594. model_index_box = self.app.collection.index(selection_index_box, 0, self.box_combo.rootModelIndex())
  595. try:
  596. bb_obj = model_index_box.internalPointer().obj
  597. except Exception:
  598. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Box object loaded ..."))
  599. return
  600. xmin, ymin, xmax, ymax = bb_obj.bounds()
  601. px = 0.5 * (xmin + xmax)
  602. py = 0.5 * (ymin + ymax)
  603. fcobj.mirror(axis, [px, py])
  604. self.app.object_changed.emit(fcobj)
  605. fcobj.plot()
  606. self.app.inform.emit('[success] Geometry %s %s...' % (str(fcobj.options['name']), _("was mirrored")))
  607. def on_point_add(self):
  608. val = self.app.defaults["global_point_clipboard_format"] % (self.app.pos[0], self.app.pos[1])
  609. self.point_entry.set_value(val)
  610. def on_drill_add(self):
  611. self.drill_values += (self.app.defaults["global_point_clipboard_format"] %
  612. (self.app.pos[0], self.app.pos[1])) + ','
  613. self.alignment_holes.set_value(self.drill_values)
  614. def on_drill_delete_last(self):
  615. drill_values_without_last_tupple = self.drill_values.rpartition('(')[0]
  616. self.drill_values = drill_values_without_last_tupple
  617. self.alignment_holes.set_value(self.drill_values)
  618. def on_toggle_pointbox(self):
  619. if self.axis_location.get_value() == "point":
  620. self.point_entry.show()
  621. self.add_point_button.show()
  622. self.box_type_label.hide()
  623. self.box_type_radio.hide()
  624. self.box_object_label.hide()
  625. self.box_combo.hide()
  626. else:
  627. self.point_entry.hide()
  628. self.add_point_button.hide()
  629. self.box_type_label.show()
  630. self.box_type_radio.show()
  631. self.box_object_label.show()
  632. self.box_combo.show()
  633. def on_bbox_coordinates(self):
  634. xmin = Inf
  635. ymin = Inf
  636. xmax = -Inf
  637. ymax = -Inf
  638. obj_list = self.app.collection.get_selected()
  639. if not obj_list:
  640. self.app.inform.emit('[ERROR_NOTCL] %s' % _("Failed. No object(s) selected..."))
  641. return
  642. for obj in obj_list:
  643. try:
  644. gxmin, gymin, gxmax, gymax = obj.bounds()
  645. xmin = min([xmin, gxmin])
  646. ymin = min([ymin, gymin])
  647. xmax = max([xmax, gxmax])
  648. ymax = max([ymax, gymax])
  649. except Exception as e:
  650. log.warning("DEV WARNING: Tried to get bounds of empty geometry in DblSidedTool. %s" % str(e))
  651. self.xmin_entry.set_value(xmin)
  652. self.ymin_entry.set_value(ymin)
  653. self.xmax_entry.set_value(xmax)
  654. self.ymax_entry.set_value(ymax)
  655. cx = '%.*f' % (self.decimals, (((xmax - xmin) / 2.0) + xmin))
  656. cy = '%.*f' % (self.decimals, (((ymax - ymin) / 2.0) + ymin))
  657. val_txt = '(%s, %s)' % (cx, cy)
  658. self.center_entry.set_value(val_txt)
  659. self.axis_location.set_value('point')
  660. self.point_entry.set_value(val_txt)
  661. self.app.delete_selection_shape()
  662. def reset_fields(self):
  663. self.gerber_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  664. self.exc_object_combo.setRootModelIndex(self.app.collection.index(1, 0, QtCore.QModelIndex()))
  665. self.geo_object_combo.setRootModelIndex(self.app.collection.index(2, 0, QtCore.QModelIndex()))
  666. self.box_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  667. self.gerber_object_combo.setCurrentIndex(0)
  668. self.exc_object_combo.setCurrentIndex(0)
  669. self.geo_object_combo.setCurrentIndex(0)
  670. self.box_combo.setCurrentIndex(0)
  671. self.box_type_radio.set_value('grb')
  672. self.drill_values = ""