ToolMove.py 11 KB

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