ToolMove.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. ############################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # http://flatcam.org #
  4. # File Author: Marius Adrian Stanciu (c) #
  5. # Date: 3/10/2019 #
  6. # MIT Licence #
  7. ############################################################
  8. from FlatCAMTool import FlatCAMTool
  9. from FlatCAMObj import *
  10. from flatcamGUI.VisPyVisuals import *
  11. from copy import copy
  12. import gettext
  13. import FlatCAMTranslation as fcTranslate
  14. fcTranslate.apply_language('strings')
  15. import builtins
  16. if '_' not in builtins.__dict__:
  17. _ = gettext.gettext
  18. class ToolMove(FlatCAMTool):
  19. toolName = _("Move")
  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. self.sel_shapes = ShapeCollection(parent=self.app.plotcanvas.vispy_canvas.view.scene, layers=1)
  33. def install(self, icon=None, separator=None, **kwargs):
  34. FlatCAMTool.install(self, icon, separator, shortcut='M', **kwargs)
  35. def run(self, toggle):
  36. self.app.report_usage("ToolMove()")
  37. if self.app.tool_tab_locked is True:
  38. return
  39. self.toggle()
  40. def toggle(self, toggle=False):
  41. if self.isVisible():
  42. self.setVisible(False)
  43. self.app.plotcanvas.vis_disconnect('mouse_move', self.on_move)
  44. self.app.plotcanvas.vis_disconnect('mouse_press', self.on_left_click)
  45. self.app.plotcanvas.vis_disconnect('key_release', self.on_key_press)
  46. self.app.plotcanvas.vis_connect('key_press', self.app.ui.keyPressEvent)
  47. self.clicked_move = 0
  48. # signal that there is no command active
  49. self.app.command_active = None
  50. # delete the selection box
  51. self.delete_shape()
  52. return
  53. else:
  54. self.setVisible(True)
  55. # signal that there is a command active and it is 'Move'
  56. self.app.command_active = "Move"
  57. if self.app.collection.get_selected():
  58. self.app.inform.emit(_("MOVE: Click on the Start point ..."))
  59. # draw the selection box
  60. self.draw_sel_bbox()
  61. else:
  62. self.setVisible(False)
  63. # signal that there is no command active
  64. self.app.command_active = None
  65. self.app.inform.emit(_("[WARNING_NOTCL] MOVE action cancelled. No object(s) to move."))
  66. def on_left_click(self, event):
  67. # mouse click will be accepted only if the left button is clicked
  68. # this is necessary because right mouse click and middle mouse click
  69. # are used for panning on the canvas
  70. if event.button == 1:
  71. if self.clicked_move == 0:
  72. pos_canvas = self.app.plotcanvas.vispy_canvas.translate_coords(event.pos)
  73. # if GRID is active we need to get the snapped positions
  74. if self.app.grid_status() == True:
  75. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  76. else:
  77. pos = pos_canvas
  78. if self.point1 is None:
  79. self.point1 = pos
  80. else:
  81. self.point2 = copy(self.point1)
  82. self.point1 = pos
  83. self.app.inform.emit(_("MOVE: Click on the Destination point ..."))
  84. if self.clicked_move == 1:
  85. try:
  86. pos_canvas = self.app.plotcanvas.vispy_canvas.translate_coords(event.pos)
  87. # delete the selection bounding box
  88. self.delete_shape()
  89. # if GRID is active we need to get the snapped positions
  90. if self.app.grid_status() == True:
  91. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  92. else:
  93. pos = pos_canvas
  94. dx = pos[0] - self.point1[0]
  95. dy = pos[1] - self.point1[1]
  96. proc = self.app.proc_container.new(_("Moving ..."))
  97. def job_move(app_obj):
  98. obj_list = self.app.collection.get_selected()
  99. try:
  100. if not obj_list:
  101. self.app.inform.emit(_("[WARNING_NOTCL] No object(s) selected."))
  102. return "fail"
  103. else:
  104. for sel_obj in obj_list:
  105. # offset
  106. sel_obj.offset((dx, dy))
  107. sel_obj.plot()
  108. try:
  109. sel_obj.replotApertures.emit()
  110. except:
  111. pass
  112. # Update the object bounding box options
  113. a,b,c,d = sel_obj.bounds()
  114. sel_obj.options['xmin'] = a
  115. sel_obj.options['ymin'] = b
  116. sel_obj.options['xmax'] = c
  117. sel_obj.options['ymax'] = d
  118. # self.app.collection.set_active(sel_obj.options['name'])
  119. except Exception as e:
  120. proc.done()
  121. self.app.inform.emit(_('[ERROR_NOTCL] '
  122. 'ToolMove.on_left_click() --> %s') % str(e))
  123. return "fail"
  124. proc.done()
  125. # delete the selection bounding box
  126. self.delete_shape()
  127. self.app.inform.emit(_('[success]%s object was moved ...') %
  128. str(sel_obj.kind).capitalize())
  129. self.app.worker_task.emit({'fcn': job_move, 'params': [self]})
  130. self.clicked_move = 0
  131. self.toggle()
  132. return
  133. except TypeError:
  134. self.app.inform.emit(_('[ERROR_NOTCL] '
  135. 'ToolMove.on_left_click() --> Error when mouse left click.'))
  136. return
  137. self.clicked_move = 1
  138. def on_move(self, event):
  139. pos_canvas = self.app.plotcanvas.vispy_canvas.translate_coords(event.pos)
  140. # if GRID is active we need to get the snapped positions
  141. if self.app.grid_status() == True:
  142. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  143. else:
  144. pos = pos_canvas
  145. if self.point1 is None:
  146. dx = pos[0]
  147. dy = pos[1]
  148. else:
  149. dx = pos[0] - self.point1[0]
  150. dy = pos[1] - self.point1[1]
  151. if self.clicked_move == 1:
  152. self.update_sel_bbox((dx, dy))
  153. def on_key_press(self, event):
  154. if event.key == 'escape':
  155. # abort the move action
  156. self.app.inform.emit(_("[WARNING_NOTCL]Move action cancelled."))
  157. self.toggle()
  158. return
  159. def draw_sel_bbox(self):
  160. xminlist = []
  161. yminlist = []
  162. xmaxlist = []
  163. ymaxlist = []
  164. obj_list = self.app.collection.get_selected()
  165. if not obj_list:
  166. self.app.inform.emit(_("[WARNING_NOTCL]Object(s) not selected"))
  167. self.toggle()
  168. else:
  169. # if we have an object selected then we can safely activate the mouse events
  170. self.app.plotcanvas.vis_connect('mouse_move', self.on_move)
  171. self.app.plotcanvas.vis_connect('mouse_press', self.on_left_click)
  172. self.app.plotcanvas.vis_connect('key_release', self.on_key_press)
  173. # first get a bounding box to fit all
  174. for obj in obj_list:
  175. xmin, ymin, xmax, ymax = obj.bounds()
  176. xminlist.append(xmin)
  177. yminlist.append(ymin)
  178. xmaxlist.append(xmax)
  179. ymaxlist.append(ymax)
  180. # get the minimum x,y and maximum x,y for all objects selected
  181. xminimal = min(xminlist)
  182. yminimal = min(yminlist)
  183. xmaximal = max(xmaxlist)
  184. ymaximal = max(ymaxlist)
  185. p1 = (xminimal, yminimal)
  186. p2 = (xmaximal, yminimal)
  187. p3 = (xmaximal, ymaximal)
  188. p4 = (xminimal, ymaximal)
  189. self.old_coords = [p1, p2, p3, p4]
  190. self.draw_shape(self.old_coords)
  191. def update_sel_bbox(self, pos):
  192. self.delete_shape()
  193. pt1 = (self.old_coords[0][0] + pos[0], self.old_coords[0][1] + pos[1])
  194. pt2 = (self.old_coords[1][0] + pos[0], self.old_coords[1][1] + pos[1])
  195. pt3 = (self.old_coords[2][0] + pos[0], self.old_coords[2][1] + pos[1])
  196. pt4 = (self.old_coords[3][0] + pos[0], self.old_coords[3][1] + pos[1])
  197. self.draw_shape([pt1, pt2, pt3, pt4])
  198. def delete_shape(self):
  199. self.sel_shapes.clear()
  200. self.sel_shapes.redraw()
  201. def draw_shape(self, coords):
  202. self.sel_rect = Polygon(coords)
  203. if self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().upper() == 'MM':
  204. self.sel_rect = self.sel_rect.buffer(-0.1)
  205. self.sel_rect = self.sel_rect.buffer(0.2)
  206. else:
  207. self.sel_rect = self.sel_rect.buffer(-0.00393)
  208. self.sel_rect = self.sel_rect.buffer(0.00787)
  209. blue_t = Color('blue')
  210. blue_t.alpha = 0.2
  211. self.sel_shapes.add(self.sel_rect, color='blue', face_color=blue_t, update=True, layer=0, tolerance=None)
  212. # end of file