ToolMove.py 8.6 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, **kwargs)
  23. def run(self):
  24. if self.app.tool_tab_locked is True:
  25. return
  26. self.toggle()
  27. def on_left_click(self, event):
  28. # mouse click will be accepted only if the left button is clicked
  29. # this is necessary because right mouse click and middle mouse click
  30. # are used for panning on the canvas
  31. if event.button == 1:
  32. if self.clicked_move == 0:
  33. pos_canvas = self.app.plotcanvas.vispy_canvas.translate_coords(event.pos)
  34. # if GRID is active we need to get the snapped positions
  35. if self.app.grid_status() == True:
  36. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  37. else:
  38. pos = pos_canvas
  39. if self.point1 is None:
  40. self.point1 = pos
  41. else:
  42. self.point2 = copy(self.point1)
  43. self.point1 = pos
  44. self.app.inform.emit("MOVE: Click on the Destination point ...")
  45. if self.clicked_move == 1:
  46. try:
  47. pos_canvas = self.app.plotcanvas.vispy_canvas.translate_coords(event.pos)
  48. # delete the selection bounding box
  49. self.delete_shape()
  50. # if GRID is active we need to get the snapped positions
  51. if self.app.grid_status() == True:
  52. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  53. else:
  54. pos = pos_canvas
  55. dx = pos[0] - self.point1[0]
  56. dy = pos[1] - self.point1[1]
  57. proc = self.app.proc_container.new("Moving ...")
  58. def job_move(app_obj):
  59. obj_list = self.app.collection.get_selected()
  60. try:
  61. if not obj_list:
  62. self.app.inform.emit("[warning_notcl] No object(s) selected.")
  63. return "fail"
  64. else:
  65. for sel_obj in obj_list:
  66. sel_obj.offset((dx, dy))
  67. sel_obj.plot()
  68. # Update the object bounding box options
  69. a,b,c,d = sel_obj.bounds()
  70. sel_obj.options['xmin'] = a
  71. sel_obj.options['ymin'] = b
  72. sel_obj.options['xmax'] = c
  73. sel_obj.options['ymax'] = d
  74. # self.app.collection.set_active(sel_obj.options['name'])
  75. except Exception as e:
  76. proc.done()
  77. self.app.inform.emit('[error_notcl] '
  78. 'ToolMove.on_left_click() --> %s' % str(e))
  79. return "fail"
  80. proc.done()
  81. # delete the selection bounding box
  82. self.delete_shape()
  83. self.app.worker_task.emit({'fcn': job_move, 'params': [self]})
  84. self.clicked_move = 0
  85. self.toggle()
  86. self.app.inform.emit("[success]Object was moved ...")
  87. return
  88. except TypeError:
  89. self.app.inform.emit('[error_notcl] '
  90. 'ToolMove.on_left_click() --> Error when mouse left click.')
  91. return
  92. self.clicked_move = 1
  93. def on_move(self, event):
  94. pos_canvas = self.app.plotcanvas.vispy_canvas.translate_coords(event.pos)
  95. # if GRID is active we need to get the snapped positions
  96. if self.app.grid_status() == True:
  97. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  98. else:
  99. pos = pos_canvas
  100. if self.point1 is None:
  101. dx = pos[0]
  102. dy = pos[1]
  103. else:
  104. dx = pos[0] - self.point1[0]
  105. dy = pos[1] - self.point1[1]
  106. if self.clicked_move == 1:
  107. self.update_sel_bbox((dx, dy))
  108. def on_key_press(self, event):
  109. if event.key == 'escape':
  110. # abort the move action
  111. self.app.inform.emit("[warning_notcl]Move action cancelled.")
  112. self.toggle()
  113. return
  114. def toggle(self):
  115. if self.isVisible():
  116. self.setVisible(False)
  117. self.app.plotcanvas.vis_disconnect('mouse_move', self.on_move)
  118. self.app.plotcanvas.vis_disconnect('mouse_press', self.on_left_click)
  119. self.app.plotcanvas.vis_disconnect('key_release', self.on_key_press)
  120. self.app.plotcanvas.vis_connect('key_press', self.app.on_key_over_plot)
  121. self.clicked_move = 0
  122. # signal that there is no command active
  123. self.app.command_active = None
  124. # delete the selection box
  125. self.delete_shape()
  126. return
  127. else:
  128. self.setVisible(True)
  129. # signal that there is a command active and it is 'Move'
  130. self.app.command_active = "Move"
  131. if self.app.collection.get_selected():
  132. self.app.inform.emit("MOVE: Click on the Start point ...")
  133. # draw the selection box
  134. self.draw_sel_bbox()
  135. else:
  136. self.setVisible(False)
  137. # signal that there is no command active
  138. self.app.command_active = None
  139. self.app.inform.emit("[warning_notcl]MOVE action cancelled. No object(s) to move.")
  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