ToolMove.py 9.6 KB

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