ToolMove.py 9.9 KB

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