ToolMove.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # File Author: Marius Adrian Stanciu (c) #
  4. # Date: 3/10/2019 #
  5. # MIT Licence #
  6. # ##########################################################
  7. from FlatCAMTool import FlatCAMTool
  8. from FlatCAMObj import *
  9. from flatcamGUI.VisPyVisuals import *
  10. from copy import copy
  11. import gettext
  12. import FlatCAMTranslation as fcTranslate
  13. import builtins
  14. fcTranslate.apply_language('strings')
  15. if '_' not in builtins.__dict__:
  16. _ = gettext.gettext
  17. class ToolMove(FlatCAMTool):
  18. toolName = _("Move")
  19. replot_signal = pyqtSignal(list)
  20. def __init__(self, app):
  21. FlatCAMTool.__init__(self, app)
  22. self.layout.setContentsMargins(0, 0, 3, 0)
  23. self.setSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Maximum)
  24. self.clicked_move = 0
  25. self.point1 = None
  26. self.point2 = None
  27. # the default state is disabled for the Move command
  28. self.setVisible(False)
  29. self.sel_rect = None
  30. self.old_coords = []
  31. # VisPy visuals
  32. if self.app.is_legacy is False:
  33. self.sel_shapes = ShapeCollection(parent=self.app.plotcanvas.view.scene, layers=1)
  34. else:
  35. from flatcamGUI.PlotCanvasLegacy import ShapeCollectionLegacy
  36. self.sel_shapes = ShapeCollectionLegacy(obj=self, app=self.app, name="move")
  37. self.mm = None
  38. self.mp = None
  39. self.kr = None
  40. self.replot_signal[list].connect(self.replot)
  41. def install(self, icon=None, separator=None, **kwargs):
  42. FlatCAMTool.install(self, icon, separator, shortcut='M', **kwargs)
  43. def run(self, toggle):
  44. self.app.report_usage("ToolMove()")
  45. if self.app.tool_tab_locked is True:
  46. return
  47. self.toggle()
  48. def toggle(self, toggle=False):
  49. if self.isVisible():
  50. self.setVisible(False)
  51. if self.app.is_legacy is False:
  52. self.app.plotcanvas.graph_event_disconnect('mouse_move', self.on_move)
  53. self.app.plotcanvas.graph_event_disconnect('mouse_press', self.on_left_click)
  54. self.app.plotcanvas.graph_event_disconnect('key_release', self.on_key_press)
  55. self.app.plotcanvas.graph_event_connect('key_press', self.app.ui.keyPressEvent)
  56. else:
  57. self.app.plotcanvas.graph_event_disconnect(self.mm)
  58. self.app.plotcanvas.graph_event_disconnect(self.mp)
  59. self.app.plotcanvas.graph_event_disconnect(self.kr)
  60. self.app.kr = self.app.plotcanvas.graph_event_connect('key_press', self.app.ui.keyPressEvent)
  61. self.clicked_move = 0
  62. # signal that there is no command active
  63. self.app.command_active = None
  64. # delete the selection box
  65. self.delete_shape()
  66. return
  67. else:
  68. self.setVisible(True)
  69. # signal that there is a command active and it is 'Move'
  70. self.app.command_active = "Move"
  71. sel_obj_list = self.app.collection.get_selected()
  72. if sel_obj_list:
  73. self.app.inform.emit(_("MOVE: Click on the Start point ..."))
  74. # if we have an object selected then we can safely activate the mouse events
  75. self.mm = self.app.plotcanvas.graph_event_connect('mouse_move', self.on_move)
  76. self.mp = self.app.plotcanvas.graph_event_connect('mouse_press', self.on_left_click)
  77. self.kr = self.app.plotcanvas.graph_event_connect('key_release', self.on_key_press)
  78. # draw the selection box
  79. self.draw_sel_bbox()
  80. else:
  81. self.toggle()
  82. self.app.inform.emit('[WARNING_NOTCL] %s' % _("MOVE action cancelled. No object(s) to move."))
  83. def on_left_click(self, event):
  84. # mouse click will be accepted only if the left button is clicked
  85. # this is necessary because right mouse click and middle mouse click
  86. # are used for panning on the canvas
  87. if self.app.is_legacy is False:
  88. event_pos = event.pos
  89. else:
  90. event_pos = (event.xdata, event.ydata)
  91. if event.button == 1:
  92. if self.clicked_move == 0:
  93. pos_canvas = self.app.plotcanvas.translate_coords(event_pos)
  94. # if GRID is active we need to get the snapped positions
  95. if self.app.grid_status() == True:
  96. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  97. else:
  98. pos = pos_canvas
  99. if self.point1 is None:
  100. self.point1 = pos
  101. else:
  102. self.point2 = copy(self.point1)
  103. self.point1 = pos
  104. self.app.inform.emit(_("MOVE: Click on the Destination point ..."))
  105. if self.clicked_move == 1:
  106. try:
  107. pos_canvas = self.app.plotcanvas.translate_coords(event_pos)
  108. # delete the selection bounding box
  109. self.delete_shape()
  110. # if GRID is active we need to get the snapped positions
  111. if self.app.grid_status() == True:
  112. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  113. else:
  114. pos = pos_canvas
  115. dx = pos[0] - self.point1[0]
  116. dy = pos[1] - self.point1[1]
  117. # move only the objects selected and plotted and visible
  118. obj_list = [obj for obj in self.app.collection.get_selected()
  119. if obj.options['plot'] and obj.visible is True]
  120. def job_move(app_obj):
  121. with self.app.proc_container.new(_("Moving...")) as proc:
  122. try:
  123. if not obj_list:
  124. self.app.inform.emit('[WARNING_NOTCL] %s' % _("No object(s) selected."))
  125. return "fail"
  126. # remove any mark aperture shape that may be displayed
  127. for sel_obj in obj_list:
  128. # if the Gerber mark shapes are enabled they need to be disabled before move
  129. if isinstance(sel_obj, FlatCAMGerber):
  130. sel_obj.ui.aperture_table_visibility_cb.setChecked(False)
  131. try:
  132. sel_obj.replotApertures.emit()
  133. except Exception as e:
  134. pass
  135. for sel_obj in obj_list:
  136. # offset solid_geometry
  137. sel_obj.offset((dx, dy))
  138. # Update the object bounding box options
  139. a, b, c, d = sel_obj.bounds()
  140. sel_obj.options['xmin'] = a
  141. sel_obj.options['ymin'] = b
  142. sel_obj.options['xmax'] = c
  143. sel_obj.options['ymax'] = d
  144. # time to plot the moved objects
  145. self.replot_signal.emit(obj_list)
  146. except Exception as e:
  147. proc.done()
  148. self.app.inform.emit('[ERROR_NOTCL] %s --> %s' % ('ToolMove.on_left_click()', str(e)))
  149. return "fail"
  150. proc.done()
  151. # delete the selection bounding box
  152. self.delete_shape()
  153. self.app.inform.emit('[success] %s %s' %
  154. (str(sel_obj.kind).capitalize(), 'object was moved ...'))
  155. self.app.worker_task.emit({'fcn': job_move, 'params': [self]})
  156. self.clicked_move = 0
  157. self.toggle()
  158. return
  159. except TypeError as e:
  160. log.debug("ToolMove.on_left_click() --> %s" % str(e))
  161. self.app.inform.emit('[ERROR_NOTCL] ToolMove.on_left_click() --> %s' %
  162. _('Error when mouse left click.'))
  163. return
  164. self.clicked_move = 1
  165. def replot(self, obj_list):
  166. def worker_task():
  167. with self.app.proc_container.new('%s...' % _("Plotting")):
  168. for sel_obj in obj_list:
  169. sel_obj.plot()
  170. self.app.worker_task.emit({'fcn': worker_task, 'params': []})
  171. def on_move(self, event):
  172. if self.app.is_legacy is False:
  173. event_pos = event.pos
  174. else:
  175. event_pos = (event.xdata, event.ydata)
  176. try:
  177. x = float(event_pos[0])
  178. y = float(event_pos[1])
  179. except TypeError:
  180. return
  181. pos_canvas = self.app.plotcanvas.translate_coords((x, y))
  182. # if GRID is active we need to get the snapped positions
  183. if self.app.grid_status() == True:
  184. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  185. else:
  186. pos = pos_canvas
  187. if self.point1 is None:
  188. dx = pos[0]
  189. dy = pos[1]
  190. else:
  191. dx = pos[0] - self.point1[0]
  192. dy = pos[1] - self.point1[1]
  193. if self.clicked_move == 1:
  194. self.update_sel_bbox((dx, dy))
  195. def on_key_press(self, event):
  196. if event.key == 'escape':
  197. # abort the move action
  198. self.app.inform.emit('[WARNING_NOTCL] %s' % _("Move action cancelled."))
  199. self.toggle()
  200. return
  201. def draw_sel_bbox(self):
  202. xminlist = []
  203. yminlist = []
  204. xmaxlist = []
  205. ymaxlist = []
  206. obj_list = self.app.collection.get_selected()
  207. # first get a bounding box to fit all
  208. for obj in obj_list:
  209. # don't move disabled objects, move only plotted objects
  210. if obj.options['plot']:
  211. xmin, ymin, xmax, ymax = obj.bounds()
  212. xminlist.append(xmin)
  213. yminlist.append(ymin)
  214. xmaxlist.append(xmax)
  215. ymaxlist.append(ymax)
  216. # get the minimum x,y and maximum x,y for all objects selected
  217. xminimal = min(xminlist)
  218. yminimal = min(yminlist)
  219. xmaximal = max(xmaxlist)
  220. ymaximal = max(ymaxlist)
  221. p1 = (xminimal, yminimal)
  222. p2 = (xmaximal, yminimal)
  223. p3 = (xmaximal, ymaximal)
  224. p4 = (xminimal, ymaximal)
  225. self.old_coords = [p1, p2, p3, p4]
  226. self.draw_shape(Polygon(self.old_coords))
  227. if self.app.is_legacy is True:
  228. self.sel_shapes.redraw()
  229. def update_sel_bbox(self, pos):
  230. self.delete_shape()
  231. pt1 = (self.old_coords[0][0] + pos[0], self.old_coords[0][1] + pos[1])
  232. pt2 = (self.old_coords[1][0] + pos[0], self.old_coords[1][1] + pos[1])
  233. pt3 = (self.old_coords[2][0] + pos[0], self.old_coords[2][1] + pos[1])
  234. pt4 = (self.old_coords[3][0] + pos[0], self.old_coords[3][1] + pos[1])
  235. self.draw_shape(Polygon([pt1, pt2, pt3, pt4]))
  236. if self.app.is_legacy is True:
  237. self.sel_shapes.redraw()
  238. def delete_shape(self):
  239. self.sel_shapes.clear()
  240. self.sel_shapes.redraw()
  241. def draw_shape(self, shape):
  242. if self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().upper() == 'MM':
  243. proc_shape = shape.buffer(-0.1)
  244. proc_shape = proc_shape.buffer(0.2)
  245. else:
  246. proc_shape = shape.buffer(-0.00393)
  247. proc_shape = proc_shape.buffer(0.00787)
  248. # face = Color('blue')
  249. # face.alpha = 0.2
  250. face = '#0000FF' + str(hex(int(0.2 * 255)))[2:]
  251. outline = '#0000FFAF'
  252. self.sel_shapes.add(proc_shape, color=outline, face_color=face, update=True, layer=0, tolerance=None)
  253. # end of file