ToolMove.py 10 KB

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