ToolDblSided.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. from PyQt5 import QtWidgets, QtCore, QtGui
  2. from appTool import AppTool
  3. from appGUI.GUIElements import RadioSet, FCDoubleSpinner, FCButton, FCComboBox, NumericalEvalTupleEntry, FCLabel
  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 = {1: {}}
  141. tools[1]["tooldia"] = dia
  142. tools[1]['drills'] = []
  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'] += [point, point_mirror]
  155. def obj_init(obj_inst, app_inst):
  156. obj_inst.tools = tools
  157. obj_inst.create_geometry()
  158. obj_inst.source_file = app_inst.f_handlers.export_excellon(obj_name=obj_inst.options['name'],
  159. local_use=obj_inst,
  160. filename=None,
  161. use_thread=False)
  162. ret_val = self.app.app_obj.new_object("excellon", _("Alignment Drills"), obj_init)
  163. self.drill_values = ''
  164. if not ret_val == 'fail':
  165. self.app.inform.emit('[success] %s' % _("Excellon object with alignment drills created..."))
  166. def on_pick_hole(self):
  167. # get the Excellon file whose geometry will contain the desired drill hole
  168. selection_index = self.ui.exc_combo.currentIndex()
  169. model_index = self.app.collection.index(selection_index, 0, self.ui.exc_combo.rootModelIndex())
  170. try:
  171. self.exc_hole_obj = model_index.internalPointer().obj
  172. except Exception:
  173. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Excellon object loaded ..."))
  174. return
  175. # disengage the grid snapping since it will be hard to find the drills or pads on grid
  176. if self.app.ui.grid_snap_btn.isChecked():
  177. self.grid_status_memory = True
  178. self.app.ui.grid_snap_btn.trigger()
  179. else:
  180. self.grid_status_memory = False
  181. self.local_connected = True
  182. self.app.inform.emit('%s.' % _("Click on canvas within the desired Excellon drill hole"))
  183. self.mr = self.canvas.graph_event_connect('mouse_release', self.on_mouse_click_release)
  184. if self.app.is_legacy is False:
  185. self.canvas.graph_event_disconnect('mouse_release', self.app.on_mouse_click_release_over_plot)
  186. else:
  187. self.canvas.graph_event_disconnect(self.app.mr)
  188. def on_mouse_click_release(self, event):
  189. if self.app.is_legacy is False:
  190. event_pos = event.pos
  191. right_button = 2
  192. self.app.event_is_dragging = self.app.event_is_dragging
  193. else:
  194. event_pos = (event.xdata, event.ydata)
  195. right_button = 3
  196. self.app.event_is_dragging = self.app.ui.popMenu.mouse_is_panning
  197. pos_canvas = self.canvas.translate_coords(event_pos)
  198. if event.button == 1:
  199. click_pt = Point([pos_canvas[0], pos_canvas[1]])
  200. if self.app.selection_type is not None:
  201. # delete previous selection shape
  202. self.app.delete_selection_shape()
  203. self.app.selection_type = None
  204. else:
  205. if self.exc_hole_obj.kind.lower() == 'excellon':
  206. for tool, tool_dict in self.exc_hole_obj.tools.items():
  207. for geo in tool_dict['solid_geometry']:
  208. if click_pt.within(geo):
  209. center_pt = geo.centroid
  210. center_pt_coords = (
  211. self.app.dec_format(center_pt.x, self.decimals),
  212. self.app.dec_format(center_pt.y, self.decimals)
  213. )
  214. self.app.delete_selection_shape()
  215. self.ui.axis_location.set_value('point')
  216. # set the reference point for mirror
  217. self.ui.point_entry.set_value(center_pt_coords)
  218. self.app.inform.emit('[success] %s' % _("Mirror reference point set."))
  219. elif event.button == right_button and self.app.event_is_dragging is False:
  220. self.app.delete_selection_shape()
  221. self.disconnect_events()
  222. self.app.inform.emit('[WARNING_NOTCL] %s' % _("Cancelled by user request."))
  223. def disconnect_events(self):
  224. self.app.mr = self.canvas.graph_event_connect('mouse_release', self.app.on_mouse_click_release_over_plot)
  225. if self.app.is_legacy is False:
  226. self.canvas.graph_event_disconnect('mouse_release', self.on_mouse_click_release)
  227. else:
  228. self.canvas.graph_event_disconnect(self.mr)
  229. self.local_connected = False
  230. def on_mirror(self):
  231. selection_index = self.ui.object_combo.currentIndex()
  232. # fcobj = self.app.collection.object_list[selection_index]
  233. model_index = self.app.collection.index(selection_index, 0, self.ui.object_combo.rootModelIndex())
  234. try:
  235. fcobj = model_index.internalPointer().obj
  236. except Exception:
  237. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Gerber object loaded ..."))
  238. return
  239. if fcobj.kind not in ['gerber', 'geometry', 'excellon']:
  240. self.app.inform.emit('[ERROR_NOTCL] %s' % _("Only Gerber, Excellon and Geometry objects can be mirrored."))
  241. return
  242. axis = self.ui.mirror_axis.get_value()
  243. mode = self.ui.axis_location.get_value()
  244. if mode == "box":
  245. selection_index_box = self.ui.box_combo.currentIndex()
  246. model_index_box = self.app.collection.index(selection_index_box, 0, self.ui.box_combo.rootModelIndex())
  247. try:
  248. bb_obj = model_index_box.internalPointer().obj
  249. except Exception:
  250. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Box object loaded ..."))
  251. return
  252. xmin, ymin, xmax, ymax = bb_obj.bounds()
  253. px = 0.5 * (xmin + xmax)
  254. py = 0.5 * (ymin + ymax)
  255. else:
  256. try:
  257. px, py = self.ui.point_entry.get_value()
  258. except TypeError:
  259. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There are no Point coordinates in the Point field. "
  260. "Add coords and try again ..."))
  261. return
  262. fcobj.mirror(axis, [px, py])
  263. self.app.app_obj.object_changed.emit(fcobj)
  264. fcobj.plot()
  265. self.app.inform.emit('[success] %s: %s' % (_("Object was mirrored"), str(fcobj.options['name'])))
  266. def on_point_add(self):
  267. val = self.app.defaults["global_point_clipboard_format"] % \
  268. (self.decimals, self.app.pos[0], self.decimals, self.app.pos[1])
  269. self.ui.point_entry.set_value(val)
  270. def on_drill_add(self):
  271. self.drill_values += (self.app.defaults["global_point_clipboard_format"] %
  272. (self.decimals, self.app.pos[0], self.decimals, self.app.pos[1])) + ','
  273. self.ui.alignment_holes.set_value(self.drill_values)
  274. def on_drill_delete_last(self):
  275. drill_values_without_last_tupple = self.drill_values.rpartition('(')[0]
  276. self.drill_values = drill_values_without_last_tupple
  277. self.ui.alignment_holes.set_value(self.drill_values)
  278. def on_toggle_pointbox(self):
  279. val = self.ui.axis_location.get_value()
  280. if val == "point":
  281. self.ui.point_entry.show()
  282. self.ui.add_point_button.show()
  283. self.ui.box_type_label.hide()
  284. self.ui.box_type_radio.hide()
  285. self.ui.box_combo.hide()
  286. self.ui.exc_hole_lbl.hide()
  287. self.ui.exc_combo.hide()
  288. self.ui.pick_hole_button.hide()
  289. self.ui.align_ref_label_val.set_value(self.ui.point_entry.get_value())
  290. elif val == 'box':
  291. self.ui.point_entry.hide()
  292. self.ui.add_point_button.hide()
  293. self.ui.box_type_label.show()
  294. self.ui.box_type_radio.show()
  295. self.ui.box_combo.show()
  296. self.ui.exc_hole_lbl.hide()
  297. self.ui.exc_combo.hide()
  298. self.ui.pick_hole_button.hide()
  299. self.ui.align_ref_label_val.set_value("Box centroid")
  300. elif val == 'hole':
  301. self.ui.point_entry.show()
  302. self.ui.add_point_button.hide()
  303. self.ui.box_type_label.hide()
  304. self.ui.box_type_radio.hide()
  305. self.ui.box_combo.hide()
  306. self.ui.exc_hole_lbl.show()
  307. self.ui.exc_combo.show()
  308. self.ui.pick_hole_button.show()
  309. def on_bbox_coordinates(self):
  310. xmin = Inf
  311. ymin = Inf
  312. xmax = -Inf
  313. ymax = -Inf
  314. obj_list = self.app.collection.get_selected()
  315. if not obj_list:
  316. self.app.inform.emit('[ERROR_NOTCL] %s %s' % (_("Failed."), _("No object is selected.")))
  317. return
  318. for obj in obj_list:
  319. try:
  320. gxmin, gymin, gxmax, gymax = obj.bounds()
  321. xmin = min([xmin, gxmin])
  322. ymin = min([ymin, gymin])
  323. xmax = max([xmax, gxmax])
  324. ymax = max([ymax, gymax])
  325. except Exception as e:
  326. log.warning("DEV WARNING: Tried to get bounds of empty geometry in DblSidedTool. %s" % str(e))
  327. self.ui.xmin_entry.set_value(xmin)
  328. self.ui.ymin_entry.set_value(ymin)
  329. self.ui.xmax_entry.set_value(xmax)
  330. self.ui.ymax_entry.set_value(ymax)
  331. cx = '%.*f' % (self.decimals, (((xmax - xmin) / 2.0) + xmin))
  332. cy = '%.*f' % (self.decimals, (((ymax - ymin) / 2.0) + ymin))
  333. val_txt = '(%s, %s)' % (cx, cy)
  334. self.ui.center_entry.set_value(val_txt)
  335. self.ui.axis_location.set_value('point')
  336. self.ui.point_entry.set_value(val_txt)
  337. self.app.delete_selection_shape()
  338. def on_xmin_clicked(self):
  339. xmin = self.ui.xmin_entry.get_value()
  340. self.ui.axis_location.set_value('point')
  341. try:
  342. px, py = self.ui.point_entry.get_value()
  343. val = self.app.defaults["global_point_clipboard_format"] % (self.decimals, xmin, self.decimals, py)
  344. except TypeError:
  345. val = self.app.defaults["global_point_clipboard_format"] % (self.decimals, xmin, self.decimals, 0.0)
  346. self.ui.point_entry.set_value(val)
  347. def on_ymin_clicked(self):
  348. ymin = self.ui.ymin_entry.get_value()
  349. self.ui.axis_location.set_value('point')
  350. try:
  351. px, py = self.ui.point_entry.get_value()
  352. val = self.app.defaults["global_point_clipboard_format"] % (self.decimals, px, self.decimals, ymin)
  353. except TypeError:
  354. val = self.app.defaults["global_point_clipboard_format"] % (self.decimals, 0.0, self.decimals, ymin)
  355. self.ui.point_entry.set_value(val)
  356. def on_xmax_clicked(self):
  357. xmax = self.ui.xmax_entry.get_value()
  358. self.ui.axis_location.set_value('point')
  359. try:
  360. px, py = self.ui.point_entry.get_value()
  361. val = self.app.defaults["global_point_clipboard_format"] % (self.decimals, xmax, self.decimals, py)
  362. except TypeError:
  363. val = self.app.defaults["global_point_clipboard_format"] % (self.decimals, xmax, self.decimals, 0.0)
  364. self.ui.point_entry.set_value(val)
  365. def on_ymax_clicked(self):
  366. ymax = self.ui.ymax_entry.get_value()
  367. self.ui.axis_location.set_value('point')
  368. try:
  369. px, py = self.ui.point_entry.get_value()
  370. val = self.app.defaults["global_point_clipboard_format"] % (self.decimals, px, self.decimals, ymax)
  371. except TypeError:
  372. val = self.app.defaults["global_point_clipboard_format"] % (self.decimals, 0.0, self.decimals, ymax)
  373. self.ui.point_entry.set_value(val)
  374. def reset_fields(self):
  375. self.ui.object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  376. self.ui.box_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  377. self.ui.object_combo.setCurrentIndex(0)
  378. self.ui.box_combo.setCurrentIndex(0)
  379. self.ui.box_type_radio.set_value('grb')
  380. self.drill_values = ""
  381. self.ui.align_ref_label_val.set_value('')
  382. class DsidedUI:
  383. toolName = _("2-Sided PCB")
  384. def __init__(self, layout, app):
  385. self.app = app
  386. self.decimals = self.app.decimals
  387. self.layout = layout
  388. # ## Title
  389. title_label = FCLabel("%s" % self.toolName)
  390. title_label.setStyleSheet("""
  391. QLabel
  392. {
  393. font-size: 16px;
  394. font-weight: bold;
  395. }
  396. """)
  397. self.layout.addWidget(title_label)
  398. self.layout.addWidget(FCLabel(""))
  399. # ## Grid Layout
  400. grid_lay = QtWidgets.QGridLayout()
  401. grid_lay.setColumnStretch(0, 1)
  402. grid_lay.setColumnStretch(1, 0)
  403. self.layout.addLayout(grid_lay)
  404. # Objects to be mirrored
  405. self.m_objects_label = FCLabel("<b>%s:</b>" % _("Source Object"))
  406. self.m_objects_label.setToolTip('%s.' % _("Objects to be mirrored"))
  407. grid_lay.addWidget(self.m_objects_label, 0, 0, 1, 2)
  408. # Type of object to be cutout
  409. self.type_obj_combo_label = FCLabel('%s:' % _("Type"))
  410. self.type_obj_combo_label.setToolTip(
  411. _("Select the type of application object to be processed in this tool.")
  412. )
  413. self.object_type_radio = RadioSet([
  414. {"label": _("Gerber"), "value": "grb"},
  415. {"label": _("Geometry"), "value": "geo"},
  416. {"label": _("Excellon"), "value": "exc"}
  417. ])
  418. grid_lay.addWidget(self.type_obj_combo_label, 2, 0)
  419. grid_lay.addWidget(self.object_type_radio, 2, 1)
  420. # ## Gerber Object to mirror
  421. self.object_combo = FCComboBox()
  422. self.object_combo.setModel(self.app.collection)
  423. self.object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  424. self.object_combo.is_last = True
  425. grid_lay.addWidget(self.object_combo, 4, 0, 1, 2)
  426. separator_line = QtWidgets.QFrame()
  427. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  428. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  429. grid_lay.addWidget(separator_line, 7, 0, 1, 2)
  430. # #############################################################################################################
  431. # ########## BOUNDS OPERATION ###########################################################################
  432. # #############################################################################################################
  433. grid0 = QtWidgets.QGridLayout()
  434. grid0.setColumnStretch(0, 0)
  435. grid0.setColumnStretch(1, 1)
  436. self.layout.addLayout(grid0)
  437. # ## Title Bounds Values
  438. self.bv_label = FCLabel("<b>%s:</b>" % _('Bounds Values'))
  439. self.bv_label.setToolTip(
  440. _("Select on canvas the object(s)\n"
  441. "for which to calculate bounds values.")
  442. )
  443. grid0.addWidget(self.bv_label, 6, 0, 1, 2)
  444. # Xmin value
  445. self.xmin_entry = FCDoubleSpinner(callback=self.confirmation_message)
  446. self.xmin_entry.set_precision(self.decimals)
  447. self.xmin_entry.set_range(-10000.0000, 10000.0000)
  448. self.xmin_btn = FCButton('%s:' % _("X min"))
  449. self.xmin_btn.setToolTip(
  450. _("Minimum location.")
  451. )
  452. self.xmin_entry.setReadOnly(True)
  453. grid0.addWidget(self.xmin_btn, 7, 0)
  454. grid0.addWidget(self.xmin_entry, 7, 1)
  455. # Ymin value
  456. self.ymin_entry = FCDoubleSpinner(callback=self.confirmation_message)
  457. self.ymin_entry.set_precision(self.decimals)
  458. self.ymin_entry.set_range(-10000.0000, 10000.0000)
  459. self.ymin_btn = FCButton('%s:' % _("Y min"))
  460. self.ymin_btn.setToolTip(
  461. _("Minimum location.")
  462. )
  463. self.ymin_entry.setReadOnly(True)
  464. grid0.addWidget(self.ymin_btn, 8, 0)
  465. grid0.addWidget(self.ymin_entry, 8, 1)
  466. # Xmax value
  467. self.xmax_entry = FCDoubleSpinner(callback=self.confirmation_message)
  468. self.xmax_entry.set_precision(self.decimals)
  469. self.xmax_entry.set_range(-10000.0000, 10000.0000)
  470. self.xmax_btn = FCButton('%s:' % _("X max"))
  471. self.xmax_btn.setToolTip(
  472. _("Maximum location.")
  473. )
  474. self.xmax_entry.setReadOnly(True)
  475. grid0.addWidget(self.xmax_btn, 9, 0)
  476. grid0.addWidget(self.xmax_entry, 9, 1)
  477. # Ymax value
  478. self.ymax_entry = FCDoubleSpinner(callback=self.confirmation_message)
  479. self.ymax_entry.set_precision(self.decimals)
  480. self.ymax_entry.set_range(-10000.0000, 10000.0000)
  481. self.ymax_btn = FCButton('%s:' % _("Y max"))
  482. self.ymax_btn.setToolTip(
  483. _("Maximum location.")
  484. )
  485. self.ymax_entry.setReadOnly(True)
  486. grid0.addWidget(self.ymax_btn, 10, 0)
  487. grid0.addWidget(self.ymax_entry, 10, 1)
  488. # Center point value
  489. self.center_entry = NumericalEvalTupleEntry(border_color='#0069A9')
  490. self.center_entry.setPlaceholderText(_("Center point coordinates"))
  491. self.center_btn = FCButton('%s:' % _("Centroid"))
  492. self.center_btn.setToolTip(
  493. _("The center point location for the rectangular\n"
  494. "bounding shape. Centroid. Format is (x, y).")
  495. )
  496. self.center_entry.setReadOnly(True)
  497. grid0.addWidget(self.center_btn, 12, 0)
  498. grid0.addWidget(self.center_entry, 12, 1)
  499. # Calculate Bounding box
  500. self.calculate_bb_button = FCButton(_("Calculate Bounds Values"))
  501. self.calculate_bb_button.setToolTip(
  502. _("Calculate the enveloping rectangular shape coordinates,\n"
  503. "for the selection of objects.\n"
  504. "The envelope shape is parallel with the X, Y axis.")
  505. )
  506. self.calculate_bb_button.setStyleSheet("""
  507. QPushButton
  508. {
  509. font-weight: bold;
  510. }
  511. """)
  512. grid0.addWidget(self.calculate_bb_button, 13, 0, 1, 2)
  513. separator_line = QtWidgets.QFrame()
  514. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  515. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  516. grid0.addWidget(separator_line, 14, 0, 1, 2)
  517. # #############################################################################################################
  518. # ########## MIRROR OPERATION ###########################################################################
  519. # #############################################################################################################
  520. grid1 = QtWidgets.QGridLayout()
  521. grid1.setColumnStretch(0, 0)
  522. grid1.setColumnStretch(1, 1)
  523. self.layout.addLayout(grid1)
  524. self.param_label = FCLabel("<b>%s:</b>" % _("Mirror Operation"))
  525. self.param_label.setToolTip('%s.' % _("Parameters for the mirror operation"))
  526. grid1.addWidget(self.param_label, 0, 0, 1, 2)
  527. # ## Axis
  528. self.mirax_label = FCLabel('%s:' % _("Axis"))
  529. self.mirax_label.setToolTip(_("Mirror vertically (X) or horizontally (Y)."))
  530. self.mirror_axis = RadioSet(
  531. [
  532. {'label': 'X', 'value': 'X'},
  533. {'label': 'Y', 'value': 'Y'}
  534. ],
  535. orientation='vertical',
  536. stretch=False
  537. )
  538. grid1.addWidget(self.mirax_label, 2, 0)
  539. grid1.addWidget(self.mirror_axis, 2, 1, 1, 2)
  540. # ## Axis Location
  541. self.axloc_label = FCLabel('%s:' % _("Reference"))
  542. self.axloc_label.setToolTip(
  543. _("The coordinates used as reference for the mirror operation.\n"
  544. "Can be:\n"
  545. "- Point -> a set of coordinates (x,y) around which the object is mirrored\n"
  546. "- Box -> a set of coordinates (x, y) obtained from the center of the\n"
  547. "bounding box of another object selected below\n"
  548. "- Hole Snap -> a point defined by the center of a drill hole in a Excellon object")
  549. )
  550. self.axis_location = RadioSet(
  551. [
  552. {'label': _('Point'), 'value': 'point'},
  553. {'label': _('Box'), 'value': 'box'},
  554. {'label': _('Hole Snap'), 'value': 'hole'},
  555. ]
  556. )
  557. grid1.addWidget(self.axloc_label, 4, 0)
  558. grid1.addWidget(self.axis_location, 4, 1, 1, 2)
  559. # ## Point/Box
  560. self.point_entry = NumericalEvalTupleEntry(border_color='#0069A9')
  561. self.point_entry.setPlaceholderText(_("Point coordinates"))
  562. # Add a reference
  563. self.add_point_button = FCButton(_("Add"))
  564. self.add_point_button.setIcon(QtGui.QIcon(self.app.resource_location + '/plus16.png'))
  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 = FCLabel('%s:' % _("Excellon"))
  581. self.exc_hole_lbl.setToolTip(
  582. _("Object that holds holes that can be picked as reference for mirroring.")
  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 = FCLabel('%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 = FCButton(_("Mirror"))
  627. self.mirror_button.setIcon(QtGui.QIcon(self.app.resource_location + '/doubleside16.png'))
  628. self.mirror_button.setToolTip(
  629. _("Mirrors (flips) the specified object around \n"
  630. "the specified axis. Does not create a new \n"
  631. "object, but modifies it.")
  632. )
  633. self.mirror_button.setStyleSheet("""
  634. QPushButton
  635. {
  636. font-weight: bold;
  637. }
  638. """)
  639. grid1.addWidget(self.mirror_button, 16, 0, 1, 3)
  640. separator_line = QtWidgets.QFrame()
  641. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  642. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  643. grid1.addWidget(separator_line, 18, 0, 1, 3)
  644. # #############################################################################################################
  645. # ########## ALIGNMENT OPERATION ########################################################################
  646. # #############################################################################################################
  647. grid4 = QtWidgets.QGridLayout()
  648. grid4.setColumnStretch(0, 0)
  649. grid4.setColumnStretch(1, 1)
  650. self.layout.addLayout(grid4)
  651. # ## Alignment holes
  652. self.alignment_label = FCLabel("<b>%s:</b>" % _('PCB Alignment'))
  653. self.alignment_label.setToolTip(
  654. _("Creates an Excellon Object containing the\n"
  655. "specified alignment holes and their mirror\n"
  656. "images.")
  657. )
  658. grid4.addWidget(self.alignment_label, 0, 0, 1, 2)
  659. # ## Drill diameter for alignment holes
  660. self.dt_label = FCLabel("%s:" % _('Drill Dia'))
  661. self.dt_label.setToolTip(
  662. _("Diameter of the drill for the alignment holes.")
  663. )
  664. self.drill_dia = FCDoubleSpinner(callback=self.confirmation_message)
  665. self.drill_dia.setToolTip(
  666. _("Diameter of the drill for the alignment holes.")
  667. )
  668. self.drill_dia.set_precision(self.decimals)
  669. self.drill_dia.set_range(0.0000, 10000.0000)
  670. grid4.addWidget(self.dt_label, 2, 0)
  671. grid4.addWidget(self.drill_dia, 2, 1)
  672. # ## Alignment Axis
  673. self.align_ax_label = FCLabel('%s:' % _("Axis"))
  674. self.align_ax_label.setToolTip(
  675. _("Mirror vertically (X) or horizontally (Y).")
  676. )
  677. self.align_axis_radio = RadioSet(
  678. [
  679. {'label': 'X', 'value': 'X'},
  680. {'label': 'Y', 'value': 'Y'}
  681. ],
  682. orientation='vertical',
  683. stretch=False
  684. )
  685. grid4.addWidget(self.align_ax_label, 4, 0)
  686. grid4.addWidget(self.align_axis_radio, 4, 1)
  687. # ## Alignment Reference Point
  688. self.align_ref_label = FCLabel('%s:' % _("Reference"))
  689. self.align_ref_label.setToolTip(
  690. _("The reference point used to create the second alignment drill\n"
  691. "from the first alignment drill, by doing mirror.\n"
  692. "It can be modified in the Mirror Parameters -> Reference section")
  693. )
  694. self.align_ref_label_val = NumericalEvalTupleEntry(border_color='#0069A9')
  695. self.align_ref_label_val.setToolTip(
  696. _("The reference point used to create the second alignment drill\n"
  697. "from the first alignment drill, by doing mirror.\n"
  698. "It can be modified in the Mirror Parameters -> Reference section")
  699. )
  700. self.align_ref_label_val.setDisabled(True)
  701. grid4.addWidget(self.align_ref_label, 6, 0)
  702. grid4.addWidget(self.align_ref_label_val, 6, 1)
  703. grid5 = QtWidgets.QGridLayout()
  704. self.layout.addLayout(grid5)
  705. # ## Alignment holes
  706. self.ah_label = FCLabel("%s:" % _('Alignment Drill Coordinates'))
  707. self.ah_label.setToolTip(
  708. _("Alignment holes (x1, y1), (x2, y2), ... "
  709. "on one side of the mirror axis. For each set of (x, y) coordinates\n"
  710. "entered here, a pair of drills will be created:\n\n"
  711. "- one drill at the coordinates from the field\n"
  712. "- one drill in mirror position over the axis selected above in the 'Align Axis'.")
  713. )
  714. self.alignment_holes = NumericalEvalTupleEntry(border_color='#0069A9')
  715. self.alignment_holes.setPlaceholderText(_("Drill coordinates"))
  716. grid5.addWidget(self.ah_label, 0, 0, 1, 2)
  717. grid5.addWidget(self.alignment_holes, 1, 0, 1, 2)
  718. self.add_drill_point_button = FCButton(_("Add"))
  719. self.add_drill_point_button.setIcon(QtGui.QIcon(self.app.resource_location + '/plus16.png'))
  720. self.add_drill_point_button.setToolTip(
  721. _("Add alignment drill holes coordinates in the format: (x1, y1), (x2, y2), ... \n"
  722. "on one side of the alignment axis.\n\n"
  723. "The coordinates set can be obtained:\n"
  724. "- press SHIFT key and left mouse clicking on canvas. Then click Add.\n"
  725. "- press SHIFT key and left mouse clicking on canvas. Then Ctrl+V in the field.\n"
  726. "- press SHIFT key and left mouse clicking on canvas. Then RMB click in the field and click Paste.\n"
  727. "- by entering the coords manually in the format: (x1, y1), (x2, y2), ...")
  728. )
  729. # self.add_drill_point_button.setStyleSheet("""
  730. # QPushButton
  731. # {
  732. # font-weight: bold;
  733. # }
  734. # """)
  735. self.delete_drill_point_button = FCButton(_("Delete Last"))
  736. self.delete_drill_point_button.setIcon(QtGui.QIcon(self.app.resource_location + '/trash32.png'))
  737. self.delete_drill_point_button.setToolTip(
  738. _("Delete the last coordinates tuple in the list.")
  739. )
  740. drill_hlay = QtWidgets.QHBoxLayout()
  741. drill_hlay.addWidget(self.add_drill_point_button)
  742. drill_hlay.addWidget(self.delete_drill_point_button)
  743. grid5.addLayout(drill_hlay, 2, 0, 1, 2)
  744. # ## Buttons
  745. self.create_alignment_hole_button = FCButton(_("Create Excellon Object"))
  746. self.create_alignment_hole_button.setIcon(QtGui.QIcon(self.app.resource_location + '/drill32.png'))
  747. self.create_alignment_hole_button.setToolTip(
  748. _("Creates an Excellon Object containing the\n"
  749. "specified alignment holes and their mirror\n"
  750. "images.")
  751. )
  752. self.create_alignment_hole_button.setStyleSheet("""
  753. QPushButton
  754. {
  755. font-weight: bold;
  756. }
  757. """)
  758. self.layout.addWidget(self.create_alignment_hole_button)
  759. self.layout.addStretch()
  760. # ## Reset Tool
  761. self.reset_button = FCButton(_("Reset Tool"))
  762. self.reset_button.setIcon(QtGui.QIcon(self.app.resource_location + '/reset32.png'))
  763. self.reset_button.setToolTip(
  764. _("Will reset the tool parameters.")
  765. )
  766. self.reset_button.setStyleSheet("""
  767. QPushButton
  768. {
  769. font-weight: bold;
  770. }
  771. """)
  772. self.layout.addWidget(self.reset_button)
  773. # #################################### FINSIHED GUI ###########################
  774. # #############################################################################
  775. def confirmation_message(self, accepted, minval, maxval):
  776. if accepted is False:
  777. self.app.inform[str, bool].emit('[WARNING_NOTCL] %s: [%.*f, %.*f]' % (_("Edited value is out of range"),
  778. self.decimals,
  779. minval,
  780. self.decimals,
  781. maxval), False)
  782. else:
  783. self.app.inform[str, bool].emit('[success] %s' % _("Edited value is within limits."), False)
  784. def confirmation_message_int(self, accepted, minval, maxval):
  785. if accepted is False:
  786. self.app.inform[str, bool].emit('[WARNING_NOTCL] %s: [%d, %d]' %
  787. (_("Edited value is out of range"), minval, maxval), False)
  788. else:
  789. self.app.inform[str, bool].emit('[success] %s' % _("Edited value is within limits."), False)