ToolMove.py 9.5 KB

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