ToolDblSided.py 40 KB

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