ToolMove.py 9.4 KB

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