ToolMove.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. from FlatCAMTool import FlatCAMTool
  2. from FlatCAMObj import *
  3. from VisPyVisuals import *
  4. from io import StringIO
  5. from copy import copy
  6. class ToolMove(FlatCAMTool):
  7. toolName = "Move"
  8. def __init__(self, app):
  9. FlatCAMTool.__init__(self, app)
  10. self.layout.setContentsMargins(0, 0, 3, 0)
  11. self.setSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Maximum)
  12. self.clicked_move = 0
  13. self.point1 = None
  14. self.point2 = None
  15. # the default state is disabled for the Move command
  16. self.setVisible(False)
  17. self.sel_rect = None
  18. self.old_coords = []
  19. # VisPy visuals
  20. self.sel_shapes = ShapeCollection(parent=self.app.plotcanvas.vispy_canvas.view.scene, layers=1)
  21. def install(self, icon=None, separator=None, **kwargs):
  22. FlatCAMTool.install(self, icon, separator, shortcut='M', **kwargs)
  23. def run(self):
  24. if self.app.tool_tab_locked is True:
  25. return
  26. self.toggle()
  27. def toggle(self):
  28. if self.isVisible():
  29. self.setVisible(False)
  30. self.app.plotcanvas.vis_disconnect('mouse_move', self.on_move)
  31. self.app.plotcanvas.vis_disconnect('mouse_press', self.on_left_click)
  32. self.app.plotcanvas.vis_disconnect('key_release', self.on_key_press)
  33. self.app.plotcanvas.vis_connect('key_press', self.app.on_key_over_plot)
  34. self.clicked_move = 0
  35. # signal that there is no command active
  36. self.app.command_active = None
  37. # delete the selection box
  38. self.delete_shape()
  39. return
  40. else:
  41. self.setVisible(True)
  42. # signal that there is a command active and it is 'Move'
  43. self.app.command_active = "Move"
  44. if self.app.collection.get_selected():
  45. self.app.inform.emit("MOVE: Click on the Start point ...")
  46. # draw the selection box
  47. self.draw_sel_bbox()
  48. else:
  49. self.setVisible(False)
  50. # signal that there is no command active
  51. self.app.command_active = None
  52. self.app.inform.emit("[warning_notcl]MOVE action cancelled. No object(s) to move.")
  53. def on_left_click(self, event):
  54. # mouse click will be accepted only if the left button is clicked
  55. # this is necessary because right mouse click and middle mouse click
  56. # are used for panning on the canvas
  57. if event.button == 1:
  58. if self.clicked_move == 0:
  59. pos_canvas = self.app.plotcanvas.vispy_canvas.translate_coords(event.pos)
  60. # if GRID is active we need to get the snapped positions
  61. if self.app.grid_status() == True:
  62. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  63. else:
  64. pos = pos_canvas
  65. if self.point1 is None:
  66. self.point1 = pos
  67. else:
  68. self.point2 = copy(self.point1)
  69. self.point1 = pos
  70. self.app.inform.emit("MOVE: Click on the Destination point ...")
  71. if self.clicked_move == 1:
  72. try:
  73. pos_canvas = self.app.plotcanvas.vispy_canvas.translate_coords(event.pos)
  74. # delete the selection bounding box
  75. self.delete_shape()
  76. # if GRID is active we need to get the snapped positions
  77. if self.app.grid_status() == True:
  78. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  79. else:
  80. pos = pos_canvas
  81. dx = pos[0] - self.point1[0]
  82. dy = pos[1] - self.point1[1]
  83. proc = self.app.proc_container.new("Moving ...")
  84. def job_move(app_obj):
  85. obj_list = self.app.collection.get_selected()
  86. try:
  87. if not obj_list:
  88. self.app.inform.emit("[warning_notcl] No object(s) selected.")
  89. return "fail"
  90. else:
  91. for sel_obj in obj_list:
  92. sel_obj.offset((dx, dy))
  93. sel_obj.plot()
  94. # Update the object bounding box options
  95. a,b,c,d = sel_obj.bounds()
  96. sel_obj.options['xmin'] = a
  97. sel_obj.options['ymin'] = b
  98. sel_obj.options['xmax'] = c
  99. sel_obj.options['ymax'] = d
  100. # self.app.collection.set_active(sel_obj.options['name'])
  101. except Exception as e:
  102. proc.done()
  103. self.app.inform.emit('[error_notcl] '
  104. 'ToolMove.on_left_click() --> %s' % str(e))
  105. return "fail"
  106. proc.done()
  107. # delete the selection bounding box
  108. self.delete_shape()
  109. self.app.worker_task.emit({'fcn': job_move, 'params': [self]})
  110. self.clicked_move = 0
  111. self.toggle()
  112. self.app.inform.emit("[success]Object was moved ...")
  113. return
  114. except TypeError:
  115. self.app.inform.emit('[error_notcl] '
  116. 'ToolMove.on_left_click() --> Error when mouse left click.')
  117. return
  118. self.clicked_move = 1
  119. def on_move(self, event):
  120. pos_canvas = self.app.plotcanvas.vispy_canvas.translate_coords(event.pos)
  121. # if GRID is active we need to get the snapped positions
  122. if self.app.grid_status() == True:
  123. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  124. else:
  125. pos = pos_canvas
  126. if self.point1 is None:
  127. dx = pos[0]
  128. dy = pos[1]
  129. else:
  130. dx = pos[0] - self.point1[0]
  131. dy = pos[1] - self.point1[1]
  132. if self.clicked_move == 1:
  133. self.update_sel_bbox((dx, dy))
  134. def on_key_press(self, event):
  135. if event.key == 'escape':
  136. # abort the move action
  137. self.app.inform.emit("[warning_notcl]Move action cancelled.")
  138. self.toggle()
  139. return
  140. def draw_sel_bbox(self):
  141. xminlist = []
  142. yminlist = []
  143. xmaxlist = []
  144. ymaxlist = []
  145. obj_list = self.app.collection.get_selected()
  146. if not obj_list:
  147. self.app.inform.emit("[warning_notcl]Object(s) not selected")
  148. self.toggle()
  149. else:
  150. # if we have an object selected then we can safely activate the mouse events
  151. self.app.plotcanvas.vis_connect('mouse_move', self.on_move)
  152. self.app.plotcanvas.vis_connect('mouse_press', self.on_left_click)
  153. self.app.plotcanvas.vis_connect('key_release', self.on_key_press)
  154. # first get a bounding box to fit all
  155. for obj in obj_list:
  156. xmin, ymin, xmax, ymax = obj.bounds()
  157. xminlist.append(xmin)
  158. yminlist.append(ymin)
  159. xmaxlist.append(xmax)
  160. ymaxlist.append(ymax)
  161. # get the minimum x,y and maximum x,y for all objects selected
  162. xminimal = min(xminlist)
  163. yminimal = min(yminlist)
  164. xmaximal = max(xmaxlist)
  165. ymaximal = max(ymaxlist)
  166. p1 = (xminimal, yminimal)
  167. p2 = (xmaximal, yminimal)
  168. p3 = (xmaximal, ymaximal)
  169. p4 = (xminimal, ymaximal)
  170. self.old_coords = [p1, p2, p3, p4]
  171. self.draw_shape(self.old_coords)
  172. def update_sel_bbox(self, pos):
  173. self.delete_shape()
  174. pt1 = (self.old_coords[0][0] + pos[0], self.old_coords[0][1] + pos[1])
  175. pt2 = (self.old_coords[1][0] + pos[0], self.old_coords[1][1] + pos[1])
  176. pt3 = (self.old_coords[2][0] + pos[0], self.old_coords[2][1] + pos[1])
  177. pt4 = (self.old_coords[3][0] + pos[0], self.old_coords[3][1] + pos[1])
  178. self.draw_shape([pt1, pt2, pt3, pt4])
  179. def delete_shape(self):
  180. self.sel_shapes.clear()
  181. self.sel_shapes.redraw()
  182. def draw_shape(self, coords):
  183. self.sel_rect = Polygon(coords)
  184. blue_t = Color('blue')
  185. blue_t.alpha = 0.2
  186. self.sel_shapes.add(self.sel_rect, color='blue', face_color=blue_t, update=True, layer=0, tolerance=None)
  187. # end of file