ToolMove.py 8.7 KB

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