ToolMove.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.ui.keyPressEvent)
  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.inform.emit('[success]%s object was moved ...' %
  111. str(sel_obj.kind).capitalize())
  112. self.app.worker_task.emit({'fcn': job_move, 'params': [self]})
  113. self.clicked_move = 0
  114. self.toggle()
  115. return
  116. except TypeError:
  117. self.app.inform.emit('[ERROR_NOTCL] '
  118. 'ToolMove.on_left_click() --> Error when mouse left click.')
  119. return
  120. self.clicked_move = 1
  121. def on_move(self, event):
  122. pos_canvas = self.app.plotcanvas.vispy_canvas.translate_coords(event.pos)
  123. # if GRID is active we need to get the snapped positions
  124. if self.app.grid_status() == True:
  125. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  126. else:
  127. pos = pos_canvas
  128. if self.point1 is None:
  129. dx = pos[0]
  130. dy = pos[1]
  131. else:
  132. dx = pos[0] - self.point1[0]
  133. dy = pos[1] - self.point1[1]
  134. if self.clicked_move == 1:
  135. self.update_sel_bbox((dx, dy))
  136. def on_key_press(self, event):
  137. if event.key == 'escape':
  138. # abort the move action
  139. self.app.inform.emit("[WARNING_NOTCL]Move action cancelled.")
  140. self.toggle()
  141. return
  142. def draw_sel_bbox(self):
  143. xminlist = []
  144. yminlist = []
  145. xmaxlist = []
  146. ymaxlist = []
  147. obj_list = self.app.collection.get_selected()
  148. if not obj_list:
  149. self.app.inform.emit("[WARNING_NOTCL]Object(s) not selected")
  150. self.toggle()
  151. else:
  152. # if we have an object selected then we can safely activate the mouse events
  153. self.app.plotcanvas.vis_connect('mouse_move', self.on_move)
  154. self.app.plotcanvas.vis_connect('mouse_press', self.on_left_click)
  155. self.app.plotcanvas.vis_connect('key_release', self.on_key_press)
  156. # first get a bounding box to fit all
  157. for obj in obj_list:
  158. xmin, ymin, xmax, ymax = obj.bounds()
  159. xminlist.append(xmin)
  160. yminlist.append(ymin)
  161. xmaxlist.append(xmax)
  162. ymaxlist.append(ymax)
  163. # get the minimum x,y and maximum x,y for all objects selected
  164. xminimal = min(xminlist)
  165. yminimal = min(yminlist)
  166. xmaximal = max(xmaxlist)
  167. ymaximal = max(ymaxlist)
  168. p1 = (xminimal, yminimal)
  169. p2 = (xmaximal, yminimal)
  170. p3 = (xmaximal, ymaximal)
  171. p4 = (xminimal, ymaximal)
  172. self.old_coords = [p1, p2, p3, p4]
  173. self.draw_shape(self.old_coords)
  174. def update_sel_bbox(self, pos):
  175. self.delete_shape()
  176. pt1 = (self.old_coords[0][0] + pos[0], self.old_coords[0][1] + pos[1])
  177. pt2 = (self.old_coords[1][0] + pos[0], self.old_coords[1][1] + pos[1])
  178. pt3 = (self.old_coords[2][0] + pos[0], self.old_coords[2][1] + pos[1])
  179. pt4 = (self.old_coords[3][0] + pos[0], self.old_coords[3][1] + pos[1])
  180. self.draw_shape([pt1, pt2, pt3, pt4])
  181. def delete_shape(self):
  182. self.sel_shapes.clear()
  183. self.sel_shapes.redraw()
  184. def draw_shape(self, coords):
  185. self.sel_rect = Polygon(coords)
  186. blue_t = Color('blue')
  187. blue_t.alpha = 0.2
  188. self.sel_shapes.add(self.sel_rect, color='blue', face_color=blue_t, update=True, layer=0, tolerance=None)
  189. # end of file