ToolMove.py 11 KB

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