ToolMove.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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(obj=self, app=self.app, name="move")
  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. if self.app.is_legacy is False:
  50. self.app.plotcanvas.graph_event_disconnect('mouse_move', self.on_move)
  51. self.app.plotcanvas.graph_event_disconnect('mouse_press', self.on_left_click)
  52. self.app.plotcanvas.graph_event_disconnect('key_release', self.on_key_press)
  53. self.app.plotcanvas.graph_event_connect('key_press', self.app.ui.keyPressEvent)
  54. else:
  55. self.app.plotcanvas.graph_event_disconnect(self.mm)
  56. self.app.plotcanvas.graph_event_disconnect(self.mp)
  57. self.app.plotcanvas.graph_event_disconnect(self.kr)
  58. self.app.kr = self.app.plotcanvas.graph_event_connect('key_press', self.app.ui.keyPressEvent)
  59. self.clicked_move = 0
  60. # signal that there is no command active
  61. self.app.command_active = None
  62. # delete the selection box
  63. self.delete_shape()
  64. return
  65. else:
  66. self.setVisible(True)
  67. # signal that there is a command active and it is 'Move'
  68. self.app.command_active = "Move"
  69. if self.app.collection.get_selected():
  70. self.app.inform.emit(_("MOVE: Click on the Start point ..."))
  71. # draw the selection box
  72. self.draw_sel_bbox()
  73. else:
  74. self.setVisible(False)
  75. # signal that there is no command active
  76. self.app.command_active = None
  77. self.app.inform.emit('[WARNING_NOTCL] %s' % _("MOVE action cancelled. No object(s) to move."))
  78. def on_left_click(self, event):
  79. # mouse click will be accepted only if the left button is clicked
  80. # this is necessary because right mouse click and middle mouse click
  81. # are used for panning on the canvas
  82. if self.app.is_legacy is False:
  83. event_pos = event.pos
  84. else:
  85. event_pos = (event.xdata, event.ydata)
  86. if event.button == 1:
  87. if self.clicked_move == 0:
  88. pos_canvas = self.app.plotcanvas.translate_coords(event_pos)
  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. if self.point1 is None:
  95. self.point1 = pos
  96. else:
  97. self.point2 = copy(self.point1)
  98. self.point1 = pos
  99. self.app.inform.emit(_("MOVE: Click on the Destination point ..."))
  100. if self.clicked_move == 1:
  101. try:
  102. pos_canvas = self.app.plotcanvas.translate_coords(event_pos)
  103. # delete the selection bounding box
  104. self.delete_shape()
  105. # if GRID is active we need to get the snapped positions
  106. if self.app.grid_status() == True:
  107. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  108. else:
  109. pos = pos_canvas
  110. dx = pos[0] - self.point1[0]
  111. dy = pos[1] - self.point1[1]
  112. obj_list = self.app.collection.get_selected()
  113. def job_move(app_obj):
  114. with self.app.proc_container.new(_("Moving...")) as proc:
  115. try:
  116. if not obj_list:
  117. self.app.inform.emit('[WARNING_NOTCL] %s' % _("No object(s) selected."))
  118. return "fail"
  119. for sel_obj in obj_list:
  120. # offset solid_geometry
  121. sel_obj.offset((dx, dy))
  122. # sel_obj.plot()
  123. try:
  124. sel_obj.replotApertures.emit()
  125. except Exception as e:
  126. pass
  127. # Update the object bounding box options
  128. a, b, c, d = sel_obj.bounds()
  129. sel_obj.options['xmin'] = a
  130. sel_obj.options['ymin'] = b
  131. sel_obj.options['xmax'] = c
  132. sel_obj.options['ymax'] = d
  133. # time to plot the moved objects
  134. self.replot_signal.emit(obj_list)
  135. except Exception as e:
  136. proc.done()
  137. self.app.inform.emit('[ERROR_NOTCL] %s --> %s' % (_('ToolMove.on_left_click()'), str(e)))
  138. return "fail"
  139. proc.done()
  140. # delete the selection bounding box
  141. self.delete_shape()
  142. self.app.inform.emit('[success] %s %s' %
  143. (str(sel_obj.kind).capitalize(), 'object was moved ...'))
  144. self.app.worker_task.emit({'fcn': job_move, 'params': [self]})
  145. self.clicked_move = 0
  146. self.toggle()
  147. return
  148. except TypeError as e:
  149. log.debug("ToolMove.on_left_click() --> %s" % str(e))
  150. self.app.inform.emit('[ERROR_NOTCL] %s' %
  151. _('ToolMove.on_left_click() --> Error when mouse left click.'))
  152. return
  153. self.clicked_move = 1
  154. def replot(self, obj_list):
  155. def worker_task():
  156. with self.app.proc_container.new('%s...' % _("Plotting")):
  157. for sel_obj in obj_list:
  158. sel_obj.plot()
  159. self.app.worker_task.emit({'fcn': worker_task, 'params': []})
  160. def on_move(self, event):
  161. if self.app.is_legacy is False:
  162. event_pos = event.pos
  163. else:
  164. event_pos = (event.xdata, event.ydata)
  165. try:
  166. x = float(event_pos[0])
  167. y = float(event_pos[1])
  168. except TypeError:
  169. return
  170. pos_canvas = self.app.plotcanvas.translate_coords((x, y))
  171. # if GRID is active we need to get the snapped positions
  172. if self.app.grid_status() == True:
  173. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  174. else:
  175. pos = pos_canvas
  176. if self.point1 is None:
  177. dx = pos[0]
  178. dy = pos[1]
  179. else:
  180. dx = pos[0] - self.point1[0]
  181. dy = pos[1] - self.point1[1]
  182. if self.clicked_move == 1:
  183. self.update_sel_bbox((dx, dy))
  184. def on_key_press(self, event):
  185. if event.key == 'escape':
  186. # abort the move action
  187. self.app.inform.emit('[WARNING_NOTCL] %s' % _("Move action cancelled."))
  188. self.toggle()
  189. return
  190. def draw_sel_bbox(self):
  191. xminlist = []
  192. yminlist = []
  193. xmaxlist = []
  194. ymaxlist = []
  195. obj_list = self.app.collection.get_selected()
  196. if not obj_list:
  197. self.app.inform.emit('[WARNING_NOTCL] %s' % _("Object(s) not selected"))
  198. self.toggle()
  199. else:
  200. # if we have an object selected then we can safely activate the mouse events
  201. self.mm = self.app.plotcanvas.graph_event_connect('mouse_move', self.on_move)
  202. self.mp = self.app.plotcanvas.graph_event_connect('mouse_press', self.on_left_click)
  203. self.kr = self.app.plotcanvas.graph_event_connect('key_release', self.on_key_press)
  204. # first get a bounding box to fit all
  205. for obj in obj_list:
  206. xmin, ymin, xmax, ymax = obj.bounds()
  207. xminlist.append(xmin)
  208. yminlist.append(ymin)
  209. xmaxlist.append(xmax)
  210. ymaxlist.append(ymax)
  211. # get the minimum x,y and maximum x,y for all objects selected
  212. xminimal = min(xminlist)
  213. yminimal = min(yminlist)
  214. xmaximal = max(xmaxlist)
  215. ymaximal = max(ymaxlist)
  216. p1 = (xminimal, yminimal)
  217. p2 = (xmaximal, yminimal)
  218. p3 = (xmaximal, ymaximal)
  219. p4 = (xminimal, ymaximal)
  220. self.old_coords = [p1, p2, p3, p4]
  221. self.draw_shape(Polygon(self.old_coords))
  222. if self.app.is_legacy is True:
  223. self.sel_shapes.redraw()
  224. def update_sel_bbox(self, pos):
  225. self.delete_shape()
  226. pt1 = (self.old_coords[0][0] + pos[0], self.old_coords[0][1] + pos[1])
  227. pt2 = (self.old_coords[1][0] + pos[0], self.old_coords[1][1] + pos[1])
  228. pt3 = (self.old_coords[2][0] + pos[0], self.old_coords[2][1] + pos[1])
  229. pt4 = (self.old_coords[3][0] + pos[0], self.old_coords[3][1] + pos[1])
  230. self.draw_shape(Polygon([pt1, pt2, pt3, pt4]))
  231. if self.app.is_legacy is True:
  232. self.sel_shapes.redraw()
  233. def delete_shape(self):
  234. self.sel_shapes.clear()
  235. self.sel_shapes.redraw()
  236. def draw_shape(self, shape):
  237. if self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().upper() == 'MM':
  238. proc_shape = shape.buffer(-0.1)
  239. proc_shape = proc_shape.buffer(0.2)
  240. else:
  241. proc_shape = shape.buffer(-0.00393)
  242. proc_shape = proc_shape.buffer(0.00787)
  243. # face = Color('blue')
  244. # face.alpha = 0.2
  245. face = '#0000FFAF' + str(hex(int(0.2 * 255)))[2:]
  246. outline = '#0000FFAF'
  247. self.sel_shapes.add(proc_shape, color=outline, face_color=face, update=True, layer=0, tolerance=None)
  248. # end of file