ToolDblSided.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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.drill_dia.setToolTip(
  236. _("Diameter of the drill for the "
  237. "alignment holes.")
  238. )
  239. grid0.addWidget(self.drill_dia, 1, 0, 1, 2)
  240. # ## Buttons
  241. self.create_alignment_hole_button = QtWidgets.QPushButton(_("Create Excellon Object"))
  242. self.create_alignment_hole_button.setToolTip(
  243. _("Creates an Excellon Object containing the\n"
  244. "specified alignment holes and their mirror\n"
  245. "images.")
  246. )
  247. self.create_alignment_hole_button.setStyleSheet("""
  248. QPushButton
  249. {
  250. font-weight: bold;
  251. }
  252. """)
  253. self.layout.addWidget(self.create_alignment_hole_button)
  254. separator_line = QtWidgets.QFrame()
  255. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  256. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  257. self.layout.addWidget(separator_line)
  258. self.layout.addWidget(QtWidgets.QLabel(''))
  259. grid1 = QtWidgets.QGridLayout()
  260. self.layout.addLayout(grid1)
  261. grid1.setColumnStretch(0, 0)
  262. grid1.setColumnStretch(1, 1)
  263. # Xmin value
  264. self.xmin_entry = FCDoubleSpinner()
  265. self.xmin_entry.set_precision(self.decimals)
  266. self.xmin_entry.set_range(-9999.9999, 9999.9999)
  267. self.xmin_label = QtWidgets.QLabel('%s:' % _("X min"))
  268. self.xmin_label.setToolTip(
  269. _("Minimum location.")
  270. )
  271. self.xmin_entry.setReadOnly(True)
  272. grid1.addWidget(self.xmin_label, 1, 0)
  273. grid1.addWidget(self.xmin_entry, 1, 1)
  274. # Ymin value
  275. self.ymin_entry = FCDoubleSpinner()
  276. self.ymin_entry.set_precision(self.decimals)
  277. self.ymin_entry.set_range(-9999.9999, 9999.9999)
  278. self.ymin_label = QtWidgets.QLabel('%s:' % _("Y min"))
  279. self.ymin_label.setToolTip(
  280. _("Minimum location.")
  281. )
  282. self.ymin_entry.setReadOnly(True)
  283. grid1.addWidget(self.ymin_label, 2, 0)
  284. grid1.addWidget(self.ymin_entry, 2, 1)
  285. # Xmax value
  286. self.xmax_entry = FCDoubleSpinner()
  287. self.xmax_entry.set_precision(self.decimals)
  288. self.xmax_entry.set_range(-9999.9999, 9999.9999)
  289. self.xmax_label = QtWidgets.QLabel('%s:' % _("X max"))
  290. self.xmax_label.setToolTip(
  291. _("Maximum location.")
  292. )
  293. self.xmax_entry.setReadOnly(True)
  294. grid1.addWidget(self.xmax_label, 3, 0)
  295. grid1.addWidget(self.xmax_entry, 3, 1)
  296. # Ymax value
  297. self.ymax_entry = FCDoubleSpinner()
  298. self.ymax_entry.set_precision(self.decimals)
  299. self.ymax_entry.set_range(-9999.9999, 9999.9999)
  300. self.ymax_label = QtWidgets.QLabel('%s:' % _("Y max"))
  301. self.ymax_label.setToolTip(
  302. _("Maximum location.")
  303. )
  304. self.ymax_entry.setReadOnly(True)
  305. grid1.addWidget(self.ymax_label, 4, 0)
  306. grid1.addWidget(self.ymax_entry, 4, 1)
  307. # Center point value
  308. self.center_entry = FCEntry()
  309. self.center_label = QtWidgets.QLabel('%s:' % _("Centroid"))
  310. self.center_label.setToolTip(
  311. _("The center point location for the rectangular\n"
  312. "bounding shape. Centroid. Format is (x, y).")
  313. )
  314. self.center_entry.setReadOnly(True)
  315. grid1.addWidget(self.center_label, 5, 0)
  316. grid1.addWidget(self.center_entry, 5, 1)
  317. # Calculate Bounding box
  318. self.calculate_bb_button = QtWidgets.QPushButton(_("Calculate Bounds Values"))
  319. self.calculate_bb_button.setToolTip(
  320. _("Calculate the enveloping rectangular shape coordinates,\n"
  321. "for the selection of objects.\n"
  322. "The envelope shape is parallel with the X, Y axis.")
  323. )
  324. self.calculate_bb_button.setStyleSheet("""
  325. QPushButton
  326. {
  327. font-weight: bold;
  328. }
  329. """)
  330. self.layout.addWidget(self.calculate_bb_button)
  331. self.layout.addStretch()
  332. # ## Reset Tool
  333. self.reset_button = QtWidgets.QPushButton(_("Reset Tool"))
  334. self.reset_button.setToolTip(
  335. _("Will reset the tool parameters.")
  336. )
  337. self.reset_button.setStyleSheet("""
  338. QPushButton
  339. {
  340. font-weight: bold;
  341. }
  342. """)
  343. self.layout.addWidget(self.reset_button)
  344. # ## Signals
  345. self.mirror_gerber_button.clicked.connect(self.on_mirror_gerber)
  346. self.mirror_exc_button.clicked.connect(self.on_mirror_exc)
  347. self.mirror_geo_button.clicked.connect(self.on_mirror_geo)
  348. self.add_point_button.clicked.connect(self.on_point_add)
  349. self.add_drill_point_button.clicked.connect(self.on_drill_add)
  350. self.box_combo_type.currentIndexChanged.connect(self.on_combo_box_type)
  351. self.axis_location.group_toggle_fn = self.on_toggle_pointbox
  352. self.create_alignment_hole_button.clicked.connect(self.on_create_alignment_holes)
  353. self.calculate_bb_button.clicked.connect(self.on_bbox_coordinates)
  354. self.reset_button.clicked.connect(self.set_tool_ui)
  355. self.drill_values = ""
  356. def install(self, icon=None, separator=None, **kwargs):
  357. FlatCAMTool.install(self, icon, separator, shortcut='ALT+D', **kwargs)
  358. def run(self, toggle=True):
  359. self.app.report_usage("Tool2Sided()")
  360. if toggle:
  361. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  362. if self.app.ui.splitter.sizes()[0] == 0:
  363. self.app.ui.splitter.setSizes([1, 1])
  364. else:
  365. try:
  366. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  367. # if tab is populated with the tool but it does not have the focus, focus on it
  368. if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
  369. # focus on Tool Tab
  370. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  371. else:
  372. self.app.ui.splitter.setSizes([0, 1])
  373. except AttributeError:
  374. pass
  375. else:
  376. if self.app.ui.splitter.sizes()[0] == 0:
  377. self.app.ui.splitter.setSizes([1, 1])
  378. FlatCAMTool.run(self)
  379. self.set_tool_ui()
  380. self.app.ui.notebook.setTabText(2, _("2-Sided Tool"))
  381. def set_tool_ui(self):
  382. self.reset_fields()
  383. self.point_entry.set_value("")
  384. self.alignment_holes.set_value("")
  385. self.mirror_axis.set_value(self.app.defaults["tools_2sided_mirror_axis"])
  386. self.axis_location.set_value(self.app.defaults["tools_2sided_axis_loc"])
  387. self.drill_dia.set_value(self.app.defaults["tools_2sided_drilldia"])
  388. self.xmin_entry.set_value(0.0)
  389. self.ymin_entry.set_value(0.0)
  390. self.xmax_entry.set_value(0.0)
  391. self.ymax_entry.set_value(0.0)
  392. self.center_entry.set_value('')
  393. def on_combo_box_type(self):
  394. obj_type = self.box_combo_type.currentIndex()
  395. self.box_combo.setRootModelIndex(self.app.collection.index(obj_type, 0, QtCore.QModelIndex()))
  396. self.box_combo.setCurrentIndex(0)
  397. def on_create_alignment_holes(self):
  398. axis = self.mirror_axis.get_value()
  399. mode = self.axis_location.get_value()
  400. if mode == "point":
  401. try:
  402. px, py = self.point_entry.get_value()
  403. except TypeError:
  404. self.app.inform.emit('[WARNING_NOTCL] %s' % _("'Point' reference is selected and 'Point' coordinates "
  405. "are missing. Add them and retry."))
  406. return
  407. else:
  408. selection_index = self.box_combo.currentIndex()
  409. model_index = self.app.collection.index(selection_index, 0, self.gerber_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, self.exc_object_combo.rootModelIndex())
  414. try:
  415. bb_obj = model_index.internalPointer().obj
  416. except AttributeError:
  417. model_index = self.app.collection.index(selection_index, 0,
  418. self.geo_object_combo.rootModelIndex())
  419. try:
  420. bb_obj = model_index.internalPointer().obj
  421. except AttributeError:
  422. self.app.inform.emit(
  423. '[WARNING_NOTCL] %s' % _("There is no Box reference object loaded. Load one and retry."))
  424. return
  425. xmin, ymin, xmax, ymax = bb_obj.bounds()
  426. px = 0.5 * (xmin + xmax)
  427. py = 0.5 * (ymin + ymax)
  428. xscale, yscale = {"X": (1.0, -1.0), "Y": (-1.0, 1.0)}[axis]
  429. dia = float(self.drill_dia.get_value())
  430. if dia is '':
  431. self.app.inform.emit('[WARNING_NOTCL] %s' %
  432. _("No value or wrong format in Drill Dia entry. Add it and retry."))
  433. return
  434. tools = {"1": {"C": dia}}
  435. # holes = self.alignment_holes.get_value()
  436. holes = eval('[{}]'.format(self.alignment_holes.text()))
  437. if not holes:
  438. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There are no Alignment Drill Coordinates to use. "
  439. "Add them and retry."))
  440. return
  441. drills = list()
  442. for hole in holes:
  443. point = Point(hole)
  444. point_mirror = affinity.scale(point, xscale, yscale, origin=(px, py))
  445. drills.append({"point": point, "tool": "1"})
  446. drills.append({"point": point_mirror, "tool": "1"})
  447. if 'solid_geometry' not in tools["1"]:
  448. tools["1"]['solid_geometry'] = list()
  449. else:
  450. tools["1"]['solid_geometry'].append(point)
  451. tools["1"]['solid_geometry'].append(point_mirror)
  452. def obj_init(obj_inst, app_inst):
  453. obj_inst.tools = tools
  454. obj_inst.drills = drills
  455. obj_inst.create_geometry()
  456. self.app.new_object("excellon", "Alignment Drills", obj_init)
  457. self.drill_values = ''
  458. self.app.inform.emit('[success] %s' % _("Excellon object with alignment drills created..."))
  459. def on_mirror_gerber(self):
  460. selection_index = self.gerber_object_combo.currentIndex()
  461. # fcobj = self.app.collection.object_list[selection_index]
  462. model_index = self.app.collection.index(selection_index, 0, self.gerber_object_combo.rootModelIndex())
  463. try:
  464. fcobj = model_index.internalPointer().obj
  465. except Exception as e:
  466. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Gerber object loaded ..."))
  467. return
  468. if not isinstance(fcobj, FlatCAMGerber):
  469. self.app.inform.emit('[ERROR_NOTCL] %s' % _("Only Gerber, Excellon and Geometry objects can be mirrored."))
  470. return
  471. axis = self.mirror_axis.get_value()
  472. mode = self.axis_location.get_value()
  473. if mode == "point":
  474. try:
  475. px, py = self.point_entry.get_value()
  476. except TypeError:
  477. self.app.inform.emit('[WARNING_NOTCL] %s' % _("'Point' coordinates missing. "
  478. "Using Origin (0, 0) as mirroring reference."))
  479. px, py = (0, 0)
  480. else:
  481. selection_index_box = self.box_combo.currentIndex()
  482. model_index_box = self.app.collection.index(selection_index_box, 0, self.box_combo.rootModelIndex())
  483. try:
  484. bb_obj = model_index_box.internalPointer().obj
  485. except Exception as e:
  486. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Box object loaded ..."))
  487. return
  488. xmin, ymin, xmax, ymax = bb_obj.bounds()
  489. px = 0.5 * (xmin + xmax)
  490. py = 0.5 * (ymin + ymax)
  491. fcobj.mirror(axis, [px, py])
  492. self.app.object_changed.emit(fcobj)
  493. fcobj.plot()
  494. self.app.inform.emit('[success] Gerber %s %s...' % (str(fcobj.options['name']), _("was mirrored")))
  495. def on_mirror_exc(self):
  496. selection_index = self.exc_object_combo.currentIndex()
  497. # fcobj = self.app.collection.object_list[selection_index]
  498. model_index = self.app.collection.index(selection_index, 0, self.exc_object_combo.rootModelIndex())
  499. try:
  500. fcobj = model_index.internalPointer().obj
  501. except Exception as e:
  502. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Excellon object loaded ..."))
  503. return
  504. if not isinstance(fcobj, FlatCAMExcellon):
  505. self.app.inform.emit('[ERROR_NOTCL] %s' % _("Only Gerber, Excellon and Geometry objects can be mirrored."))
  506. return
  507. axis = self.mirror_axis.get_value()
  508. mode = self.axis_location.get_value()
  509. if mode == "point":
  510. try:
  511. px, py = self.point_entry.get_value()
  512. except Exception as e:
  513. log.debug("DblSidedTool.on_mirror_geo() --> %s" % str(e))
  514. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There are no Point coordinates in the Point field. "
  515. "Add coords and try again ..."))
  516. return
  517. else:
  518. selection_index_box = self.box_combo.currentIndex()
  519. model_index_box = self.app.collection.index(selection_index_box, 0, self.box_combo.rootModelIndex())
  520. try:
  521. bb_obj = model_index_box.internalPointer().obj
  522. except Exception as e:
  523. log.debug("DblSidedTool.on_mirror_geo() --> %s" % str(e))
  524. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Box object loaded ..."))
  525. return
  526. xmin, ymin, xmax, ymax = bb_obj.bounds()
  527. px = 0.5 * (xmin + xmax)
  528. py = 0.5 * (ymin + ymax)
  529. fcobj.mirror(axis, [px, py])
  530. self.app.object_changed.emit(fcobj)
  531. fcobj.plot()
  532. self.app.inform.emit('[success] Excellon %s %s...' % (str(fcobj.options['name']), _("was mirrored")))
  533. def on_mirror_geo(self):
  534. selection_index = self.geo_object_combo.currentIndex()
  535. # fcobj = self.app.collection.object_list[selection_index]
  536. model_index = self.app.collection.index(selection_index, 0, self.geo_object_combo.rootModelIndex())
  537. try:
  538. fcobj = model_index.internalPointer().obj
  539. except Exception as e:
  540. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Geometry object loaded ..."))
  541. return
  542. if not isinstance(fcobj, FlatCAMGeometry):
  543. self.app.inform.emit('[ERROR_NOTCL] %s' % _("Only Gerber, Excellon and Geometry objects can be mirrored."))
  544. return
  545. axis = self.mirror_axis.get_value()
  546. mode = self.axis_location.get_value()
  547. if mode == "point":
  548. px, py = self.point_entry.get_value()
  549. else:
  550. selection_index_box = self.box_combo.currentIndex()
  551. model_index_box = self.app.collection.index(selection_index_box, 0, self.box_combo.rootModelIndex())
  552. try:
  553. bb_obj = model_index_box.internalPointer().obj
  554. except Exception as e:
  555. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Box object loaded ..."))
  556. return
  557. xmin, ymin, xmax, ymax = bb_obj.bounds()
  558. px = 0.5 * (xmin + xmax)
  559. py = 0.5 * (ymin + ymax)
  560. fcobj.mirror(axis, [px, py])
  561. self.app.object_changed.emit(fcobj)
  562. fcobj.plot()
  563. self.app.inform.emit('[success] Geometry %s %s...' % (str(fcobj.options['name']), _("was mirrored")))
  564. def on_point_add(self):
  565. val = self.app.defaults["global_point_clipboard_format"] % (self.app.pos[0], self.app.pos[1])
  566. self.point_entry.set_value(val)
  567. def on_drill_add(self):
  568. self.drill_values += (self.app.defaults["global_point_clipboard_format"] %
  569. (self.app.pos[0], self.app.pos[1])) + ','
  570. self.alignment_holes.set_value(self.drill_values)
  571. def on_toggle_pointbox(self):
  572. if self.axis_location.get_value() == "point":
  573. self.point_entry.show()
  574. self.box_combo.hide()
  575. self.box_combo_type.hide()
  576. self.add_point_button.setDisabled(False)
  577. else:
  578. self.point_entry.hide()
  579. self.box_combo.show()
  580. self.box_combo_type.show()
  581. self.add_point_button.setDisabled(True)
  582. def on_bbox_coordinates(self):
  583. xmin = Inf
  584. ymin = Inf
  585. xmax = -Inf
  586. ymax = -Inf
  587. obj_list = self.app.collection.get_selected()
  588. if not obj_list:
  589. self.app.inform.emit('[ERROR_NOTCL] %s' % _("Failed. No object(s) selected..."))
  590. return
  591. for obj in obj_list:
  592. try:
  593. gxmin, gymin, gxmax, gymax = obj.bounds()
  594. xmin = min([xmin, gxmin])
  595. ymin = min([ymin, gymin])
  596. xmax = max([xmax, gxmax])
  597. ymax = max([ymax, gymax])
  598. except Exception as e:
  599. log.warning("DEV WARNING: Tried to get bounds of empty geometry in DblSidedTool. %s" % str(e))
  600. self.xmin_entry.set_value(xmin)
  601. self.ymin_entry.set_value(ymin)
  602. self.xmax_entry.set_value(xmax)
  603. self.ymax_entry.set_value(ymax)
  604. cx = '%.*f' % (self.decimals, (((xmax - xmin) / 2.0) + xmin))
  605. cy = '%.*f' % (self.decimals, (((ymax - ymin) / 2.0) + ymin))
  606. val_txt = '(%s, %s)' % (cx, cy)
  607. self.center_entry.set_value(val_txt)
  608. self.axis_location.set_value('point')
  609. self.point_entry.set_value(val_txt)
  610. def reset_fields(self):
  611. self.gerber_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  612. self.exc_object_combo.setRootModelIndex(self.app.collection.index(1, 0, QtCore.QModelIndex()))
  613. self.geo_object_combo.setRootModelIndex(self.app.collection.index(2, 0, QtCore.QModelIndex()))
  614. self.box_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  615. self.gerber_object_combo.setCurrentIndex(0)
  616. self.exc_object_combo.setCurrentIndex(0)
  617. self.geo_object_combo.setCurrentIndex(0)
  618. self.box_combo.setCurrentIndex(0)
  619. self.box_combo_type.setCurrentIndex(0)
  620. self.drill_values = ""