ToolDblSided.py 40 KB

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