ToolAlignObjects.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # File Author: Marius Adrian Stanciu (c) #
  4. # Date: 1/13/2020 #
  5. # MIT Licence #
  6. # ##########################################################
  7. from PyQt5 import QtWidgets, QtGui, QtCore
  8. from FlatCAMTool import FlatCAMTool
  9. from flatcamGUI.GUIElements import FCComboBox, RadioSet
  10. import math
  11. from shapely.geometry import Point
  12. from shapely.affinity import translate
  13. import gettext
  14. import FlatCAMTranslation as fcTranslate
  15. import builtins
  16. import logging
  17. fcTranslate.apply_language('strings')
  18. if '_' not in builtins.__dict__:
  19. _ = gettext.gettext
  20. log = logging.getLogger('base')
  21. class AlignObjects(FlatCAMTool):
  22. toolName = _("Align Objects")
  23. def __init__(self, app):
  24. FlatCAMTool.__init__(self, app)
  25. self.app = app
  26. self.decimals = app.decimals
  27. self.canvas = self.app.plotcanvas
  28. # ## Title
  29. title_label = QtWidgets.QLabel("%s" % self.toolName)
  30. title_label.setStyleSheet("""
  31. QLabel
  32. {
  33. font-size: 16px;
  34. font-weight: bold;
  35. }
  36. """)
  37. self.layout.addWidget(title_label)
  38. # Form Layout
  39. grid0 = QtWidgets.QGridLayout()
  40. grid0.setColumnStretch(0, 0)
  41. grid0.setColumnStretch(1, 1)
  42. self.layout.addLayout(grid0)
  43. self.aligned_label = QtWidgets.QLabel('<b>%s</b>' % _("Selection of the WORKING object"))
  44. grid0.addWidget(self.aligned_label, 0, 0, 1, 2)
  45. # Type of object to be aligned
  46. self.type_obj_combo = FCComboBox()
  47. self.type_obj_combo.addItem("Gerber")
  48. self.type_obj_combo.addItem("Excellon")
  49. self.type_obj_combo.setItemIcon(0, QtGui.QIcon(self.app.resource_location + "/flatcam_icon16.png"))
  50. self.type_obj_combo.setItemIcon(1, QtGui.QIcon(self.app.resource_location + "/drill16.png"))
  51. self.type_obj_combo_label = QtWidgets.QLabel('%s:' % _("Object Type"))
  52. self.type_obj_combo_label.setToolTip(
  53. _("Specify the type of object to be aligned.\n"
  54. "It can be of type: Gerber or Excellon.\n"
  55. "The selection here decide the type of objects that will be\n"
  56. "in the Object combobox.")
  57. )
  58. grid0.addWidget(self.type_obj_combo_label, 2, 0)
  59. grid0.addWidget(self.type_obj_combo, 2, 1)
  60. # Object to be aligned
  61. self.object_combo = FCComboBox()
  62. self.object_combo.setModel(self.app.collection)
  63. self.object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  64. self.object_combo.setCurrentIndex(1)
  65. self.object_label = QtWidgets.QLabel('%s:' % _("Object"))
  66. self.object_label.setToolTip(
  67. _("Object to be aligned.")
  68. )
  69. grid0.addWidget(self.object_label, 3, 0)
  70. grid0.addWidget(self.object_combo, 3, 1)
  71. separator_line = QtWidgets.QFrame()
  72. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  73. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  74. grid0.addWidget(separator_line, 4, 0, 1, 2)
  75. self.aligned_label = QtWidgets.QLabel('<b>%s</b>' % _("Selection of the TARGET object"))
  76. self.aligned_label.setToolTip(
  77. _("Object to which the other objects will be aligned to (moved to).")
  78. )
  79. grid0.addWidget(self.aligned_label, 6, 0, 1, 2)
  80. # Type of object to be aligned to = aligner
  81. self.type_aligner_obj_combo = FCComboBox()
  82. self.type_aligner_obj_combo.addItem("Gerber")
  83. self.type_aligner_obj_combo.addItem("Excellon")
  84. self.type_aligner_obj_combo.setItemIcon(0, QtGui.QIcon(self.app.resource_location + "/flatcam_icon16.png"))
  85. self.type_aligner_obj_combo.setItemIcon(1, QtGui.QIcon(self.app.resource_location + "/drill16.png"))
  86. self.type_aligner_obj_combo_label = QtWidgets.QLabel('%s:' % _("Object Type"))
  87. self.type_aligner_obj_combo_label.setToolTip(
  88. _("Specify the type of object to be aligned to.\n"
  89. "It can be of type: Gerber or Excellon.\n"
  90. "The selection here decide the type of objects that will be\n"
  91. "in the Object combobox.")
  92. )
  93. grid0.addWidget(self.type_aligner_obj_combo_label, 7, 0)
  94. grid0.addWidget(self.type_aligner_obj_combo, 7, 1)
  95. # Object to be aligned to = aligner
  96. self.aligner_object_combo = FCComboBox()
  97. self.aligner_object_combo.setModel(self.app.collection)
  98. self.aligner_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  99. self.aligner_object_combo.setCurrentIndex(1)
  100. self.aligner_object_label = QtWidgets.QLabel('%s:' % _("Object"))
  101. self.aligner_object_label.setToolTip(
  102. _("Object to be aligned to. Aligner.")
  103. )
  104. grid0.addWidget(self.aligner_object_label, 8, 0)
  105. grid0.addWidget(self.aligner_object_combo, 8, 1)
  106. separator_line = QtWidgets.QFrame()
  107. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  108. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  109. grid0.addWidget(separator_line, 9, 0, 1, 2)
  110. # Alignment Type
  111. self.a_type_lbl = QtWidgets.QLabel('<b>%s:</b>' % _("Alignment Type"))
  112. self.a_type_lbl.setToolTip(
  113. _("The type of alignment can be:\n"
  114. "- Single Point -> it require a single point of sync, the action will be a translation\n"
  115. "- Dual Point -> it require two points of sync, the action will be translation followed by rotation")
  116. )
  117. self.a_type_radio = RadioSet(
  118. [
  119. {'label': _('Single Point'), 'value': 'sp'},
  120. {'label': _('Dual Point'), 'value': 'dp'}
  121. ],
  122. orientation='horizontal',
  123. stretch=False
  124. )
  125. grid0.addWidget(self.a_type_lbl, 10, 0, 1, 2)
  126. grid0.addWidget(self.a_type_radio, 11, 0, 1, 2)
  127. separator_line = QtWidgets.QFrame()
  128. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  129. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  130. grid0.addWidget(separator_line, 12, 0, 1, 2)
  131. # Buttons
  132. self.align_object_button = QtWidgets.QPushButton(_("Align Object"))
  133. self.align_object_button.setToolTip(
  134. _("Align the specified object to the aligner object.\n"
  135. "If only one point is used then it assumes translation.\n"
  136. "If tho points are used it assume translation and rotation.")
  137. )
  138. self.align_object_button.setStyleSheet("""
  139. QPushButton
  140. {
  141. font-weight: bold;
  142. }
  143. """)
  144. self.layout.addWidget(self.align_object_button)
  145. self.layout.addStretch()
  146. # ## Reset Tool
  147. self.reset_button = QtWidgets.QPushButton(_("Reset Tool"))
  148. self.reset_button.setToolTip(
  149. _("Will reset the tool parameters.")
  150. )
  151. self.reset_button.setStyleSheet("""
  152. QPushButton
  153. {
  154. font-weight: bold;
  155. }
  156. """)
  157. self.layout.addWidget(self.reset_button)
  158. # Signals
  159. self.align_object_button.clicked.connect(self.on_align)
  160. self.type_obj_combo.currentIndexChanged.connect(self.on_type_obj_index_changed)
  161. self.type_aligner_obj_combo.currentIndexChanged.connect(self.on_type_aligner_index_changed)
  162. self.reset_button.clicked.connect(self.set_tool_ui)
  163. self.mr = None
  164. # if the mouse events are connected to a local method set this True
  165. self.local_connected = False
  166. # store the status of the grid
  167. self.grid_status_memory = None
  168. self.aligned_obj = None
  169. self.aligner_obj = None
  170. # this is one of the objects: self.aligned_obj or self.aligner_obj
  171. self.target_obj = None
  172. # here store the alignment points
  173. self.clicked_points = list()
  174. self.new_start = None
  175. self.new_dest = None
  176. self.align_type = None
  177. # old colors of objects involved in the alignment
  178. self.aligner_old_fill_color = None
  179. self.aligner_old_line_color = None
  180. self.aligned_old_fill_color = None
  181. self.aligned_old_line_color = None
  182. def run(self, toggle=True):
  183. self.app.report_usage("ToolAlignObjects()")
  184. if toggle:
  185. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  186. if self.app.ui.splitter.sizes()[0] == 0:
  187. self.app.ui.splitter.setSizes([1, 1])
  188. else:
  189. try:
  190. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  191. # if tab is populated with the tool but it does not have the focus, focus on it
  192. if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
  193. # focus on Tool Tab
  194. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  195. else:
  196. self.app.ui.splitter.setSizes([0, 1])
  197. except AttributeError:
  198. pass
  199. else:
  200. if self.app.ui.splitter.sizes()[0] == 0:
  201. self.app.ui.splitter.setSizes([1, 1])
  202. FlatCAMTool.run(self)
  203. self.set_tool_ui()
  204. self.app.ui.notebook.setTabText(2, _("Align Tool"))
  205. def install(self, icon=None, separator=None, **kwargs):
  206. FlatCAMTool.install(self, icon, separator, shortcut='ALT+A', **kwargs)
  207. def set_tool_ui(self):
  208. self.reset_fields()
  209. self.clicked_points = list()
  210. self.target_obj = None
  211. self.aligned_obj = None
  212. self.aligner_obj = None
  213. self.aligner_old_fill_color = None
  214. self.aligner_old_line_color = None
  215. self.aligned_old_fill_color = None
  216. self.aligned_old_line_color = None
  217. self.a_type_radio.set_value(self.app.defaults["tools_align_objects_align_type"])
  218. if self.local_connected is True:
  219. self.disconnect_cal_events()
  220. def on_type_obj_index_changed(self):
  221. obj_type = self.type_obj_combo.currentIndex()
  222. self.object_combo.setRootModelIndex(self.app.collection.index(obj_type, 0, QtCore.QModelIndex()))
  223. self.object_combo.setCurrentIndex(0)
  224. def on_type_aligner_index_changed(self):
  225. obj_type = self.type_aligner_obj_combo.currentIndex()
  226. self.aligner_object_combo.setRootModelIndex(self.app.collection.index(obj_type, 0, QtCore.QModelIndex()))
  227. self.aligner_object_combo.setCurrentIndex(0)
  228. def on_align(self):
  229. self.app.delete_selection_shape()
  230. obj_sel_index = self.object_combo.currentIndex()
  231. obj_model_index = self.app.collection.index(obj_sel_index, 0, self.object_combo.rootModelIndex())
  232. try:
  233. self.aligned_obj = obj_model_index.internalPointer().obj
  234. except AttributeError:
  235. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no aligned FlatCAM object selected..."))
  236. return
  237. aligner_obj_sel_index = self.aligner_object_combo.currentIndex()
  238. aligner_obj_model_index = self.app.collection.index(
  239. aligner_obj_sel_index, 0, self.aligner_object_combo.rootModelIndex())
  240. try:
  241. self.aligner_obj = aligner_obj_model_index.internalPointer().obj
  242. except AttributeError:
  243. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no aligner FlatCAM object selected..."))
  244. return
  245. self.align_type = self.a_type_radio.get_value()
  246. # disengage the grid snapping since it will be hard to find the drills or pads on grid
  247. if self.app.ui.grid_snap_btn.isChecked():
  248. self.grid_status_memory = True
  249. self.app.ui.grid_snap_btn.trigger()
  250. else:
  251. self.grid_status_memory = False
  252. self.mr = self.canvas.graph_event_connect('mouse_release', self.on_mouse_click_release)
  253. if self.app.is_legacy is False:
  254. self.canvas.graph_event_disconnect('mouse_release', self.app.on_mouse_click_release_over_plot)
  255. else:
  256. self.canvas.graph_event_disconnect(self.app.mr)
  257. self.local_connected = True
  258. self.aligner_old_fill_color = self.aligner_obj.fill_color
  259. self.aligner_old_line_color = self.aligner_obj.outline_color
  260. self.aligned_old_fill_color = self.aligned_obj.fill_color
  261. self.aligned_old_line_color = self.aligned_obj.outline_color
  262. self.app.inform.emit('%s: %s' % (_("First Point"), _("Click on the START point.")))
  263. self.target_obj = self.aligned_obj
  264. self.set_color()
  265. def on_mouse_click_release(self, event):
  266. if self.app.is_legacy is False:
  267. event_pos = event.pos
  268. right_button = 2
  269. self.app.event_is_dragging = self.app.event_is_dragging
  270. else:
  271. event_pos = (event.xdata, event.ydata)
  272. right_button = 3
  273. self.app.event_is_dragging = self.app.ui.popMenu.mouse_is_panning
  274. pos_canvas = self.canvas.translate_coords(event_pos)
  275. if event.button == 1:
  276. click_pt = Point([pos_canvas[0], pos_canvas[1]])
  277. if self.app.selection_type is not None:
  278. # delete previous selection shape
  279. self.app.delete_selection_shape()
  280. self.app.selection_type = None
  281. else:
  282. if self.target_obj.kind.lower() == 'excellon':
  283. for tool, tool_dict in self.target_obj.tools.items():
  284. for geo in tool_dict['solid_geometry']:
  285. if click_pt.within(geo):
  286. center_pt = geo.centroid
  287. self.clicked_points.append(
  288. [
  289. float('%.*f' % (self.decimals, center_pt.x)),
  290. float('%.*f' % (self.decimals, center_pt.y))
  291. ]
  292. )
  293. self.check_points()
  294. elif self.target_obj.kind.lower() == 'gerber':
  295. for apid, apid_val in self.target_obj.apertures.items():
  296. for geo_el in apid_val['geometry']:
  297. if 'solid' in geo_el:
  298. if click_pt.within(geo_el['solid']):
  299. if isinstance(geo_el['follow'], Point):
  300. center_pt = geo_el['solid'].centroid
  301. self.clicked_points.append(
  302. [
  303. float('%.*f' % (self.decimals, center_pt.x)),
  304. float('%.*f' % (self.decimals, center_pt.y))
  305. ]
  306. )
  307. self.check_points()
  308. elif event.button == right_button and self.app.event_is_dragging is False:
  309. self.reset_color()
  310. self.clicked_points = list()
  311. self.disconnect_cal_events()
  312. self.app.inform.emit('[WARNING_NOTCL] %s' % _("Cancelled by user request."))
  313. def check_points(self):
  314. if len(self.clicked_points) == 1:
  315. self.app.inform.emit('%s: %s. %s' % (
  316. _("First Point"), _("Click on the DESTINATION point."), _(" Or right click to cancel.")))
  317. self.target_obj = self.aligner_obj
  318. self.reset_color()
  319. self.set_color()
  320. if len(self.clicked_points) == 2:
  321. if self.align_type == 'sp':
  322. self.align_translate()
  323. self.app.inform.emit('[success] %s' % _("Done."))
  324. self.app.plot_all()
  325. self.disconnect_cal_events()
  326. return
  327. else:
  328. self.app.inform.emit('%s: %s. %s' % (
  329. _("Second Point"), _("Click on the START point."), _(" Or right click to cancel.")))
  330. self.target_obj = self.aligned_obj
  331. self.reset_color()
  332. self.set_color()
  333. if len(self.clicked_points) == 3:
  334. self.app.inform.emit('%s: %s. %s' % (
  335. _("Second Point"), _("Click on the DESTINATION point."), _(" Or right click to cancel.")))
  336. self.target_obj = self.aligner_obj
  337. self.reset_color()
  338. self.set_color()
  339. if len(self.clicked_points) == 4:
  340. self.align_translate()
  341. self.align_rotate()
  342. self.app.inform.emit('[success] %s' % _("Done."))
  343. self.disconnect_cal_events()
  344. self.app.plot_all()
  345. def align_translate(self):
  346. dx = self.clicked_points[1][0] - self.clicked_points[0][0]
  347. dy = self.clicked_points[1][1] - self.clicked_points[0][1]
  348. self.aligned_obj.offset((dx, dy))
  349. # Update the object bounding box options
  350. a, b, c, d = self.aligned_obj.bounds()
  351. self.aligned_obj.options['xmin'] = a
  352. self.aligned_obj.options['ymin'] = b
  353. self.aligned_obj.options['xmax'] = c
  354. self.aligned_obj.options['ymax'] = d
  355. def align_rotate(self):
  356. dx = self.clicked_points[1][0] - self.clicked_points[0][0]
  357. dy = self.clicked_points[1][1] - self.clicked_points[0][1]
  358. new_start_pt = translate(Point(self.clicked_points[2]), xoff=dx, yoff=dy)
  359. self.new_start = (new_start_pt.x, new_start_pt.y)
  360. self.new_dest = self.clicked_points[3]
  361. origin_pt = self.clicked_points[1]
  362. sec_dx = self.new_dest[0] - self.new_start[0]
  363. sec_dy = self.new_dest[1] - self.new_start[1]
  364. rotation_not_needed = (abs(self.new_start[0] - self.new_dest[0]) <= (10 ** -self.decimals)) or \
  365. (abs(self.new_start[1] - self.new_dest[1]) <= (10 ** -self.decimals))
  366. if rotation_not_needed is False:
  367. # calculate rotation angle
  368. angle = math.degrees(math.atan(sec_dy / sec_dx))
  369. self.aligned_obj.rotate(angle=angle, point=origin_pt)
  370. def execute(self):
  371. aligned_name = self.object_combo.currentText()
  372. # Get source object.
  373. try:
  374. aligned_obj = self.app.collection.get_by_name(str(aligned_name))
  375. except Exception as e:
  376. log.debug("AlignObjects.on_align() --> %s" % str(e))
  377. self.app.inform.emit('[ERROR_NOTCL] %s: %s' % (_("Could not retrieve object"), aligned_name))
  378. return "Could not retrieve object: %s" % aligned_name
  379. if aligned_obj is None:
  380. self.app.inform.emit('[ERROR_NOTCL] %s: %s' % (_("Object not found"), aligned_obj))
  381. return "Object not found: %s" % aligned_obj
  382. aligner_name = self.box_combo.currentText()
  383. try:
  384. aligner_obj = self.app.collection.get_by_name(aligner_name)
  385. except Exception as e:
  386. log.debug("AlignObjects.on_align() --> %s" % str(e))
  387. self.app.inform.emit('[ERROR_NOTCL] %s: %s' % (_("Could not retrieve object"), aligner_name))
  388. return "Could not retrieve object: %s" % aligner_name
  389. if aligner_obj is None:
  390. self.app.inform.emit('[ERROR_NOTCL] %s: %s' % (_("Could not retrieve object"), aligner_name))
  391. def align_job():
  392. pass
  393. proc = self.app.proc_container.new(_("Working..."))
  394. def job_thread(app_obj):
  395. try:
  396. align_job()
  397. app_obj.inform.emit('[success] %s' % _("Panel created successfully."))
  398. except Exception as ee:
  399. proc.done()
  400. log.debug(str(ee))
  401. return
  402. proc.done()
  403. self.app.worker_task.emit({'fcn': job_thread, 'params': [self.app]})
  404. def disconnect_cal_events(self):
  405. # restore the Grid snapping if it was active before
  406. if self.grid_status_memory is True:
  407. self.app.ui.grid_snap_btn.trigger()
  408. self.app.mr = self.canvas.graph_event_connect('mouse_release', self.app.on_mouse_click_release_over_plot)
  409. if self.app.is_legacy is False:
  410. self.canvas.graph_event_disconnect('mouse_release', self.on_mouse_click_release)
  411. else:
  412. self.canvas.graph_event_disconnect(self.mr)
  413. self.local_connected = False
  414. self.aligner_old_fill_color = None
  415. self.aligner_old_line_color = None
  416. self.aligned_old_fill_color = None
  417. self.aligned_old_line_color = None
  418. def set_color(self):
  419. new_color = "#15678abf"
  420. new_line_color = new_color
  421. self.target_obj.shapes.redraw(
  422. update_colors=(new_color, new_line_color)
  423. )
  424. def reset_color(self):
  425. self.aligned_obj.shapes.redraw(
  426. update_colors=(self.aligned_old_fill_color, self.aligned_old_line_color)
  427. )
  428. self.aligner_obj.shapes.redraw(
  429. update_colors=(self.aligner_old_fill_color, self.aligner_old_line_color)
  430. )
  431. def reset_fields(self):
  432. self.object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  433. self.aligner_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))