ToolDblSided.py 30 KB

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