ToolMove.py 10 KB

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