ToolMove.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. 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.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] %s' % _("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.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.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. try:
  100. if not obj_list:
  101. self.app.inform.emit('[WARNING_NOTCL] %s' % _("No object(s) selected."))
  102. return "fail"
  103. else:
  104. for sel_obj in obj_list:
  105. # offset solid_geometry
  106. sel_obj.offset((dx, dy))
  107. sel_obj.plot()
  108. try:
  109. sel_obj.replotApertures.emit()
  110. except Exception as e:
  111. pass
  112. # Update the object bounding box options
  113. a, b, c, d = sel_obj.bounds()
  114. sel_obj.options['xmin'] = a
  115. sel_obj.options['ymin'] = b
  116. sel_obj.options['xmax'] = c
  117. sel_obj.options['ymax'] = d
  118. # self.app.collection.set_active(sel_obj.options['name'])
  119. except Exception as e:
  120. proc.done()
  121. self.app.inform.emit('[ERROR_NOTCL] %s --> %s' % (_('ToolMove.on_left_click('), str(e)))
  122. return "fail"
  123. proc.done()
  124. # delete the selection bounding box
  125. self.delete_shape()
  126. self.app.inform.emit('[success] %s %s' % (str(sel_obj.kind).capitalize(),
  127. _('object was moved ...')
  128. )
  129. )
  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 on_move(self, event):
  140. pos_canvas = self.app.plotcanvas.translate_coords(event.pos)
  141. # if GRID is active we need to get the snapped positions
  142. if self.app.grid_status() == True:
  143. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  144. else:
  145. pos = pos_canvas
  146. if self.point1 is None:
  147. dx = pos[0]
  148. dy = pos[1]
  149. else:
  150. dx = pos[0] - self.point1[0]
  151. dy = pos[1] - self.point1[1]
  152. if self.clicked_move == 1:
  153. self.update_sel_bbox((dx, dy))
  154. def on_key_press(self, event):
  155. if event.key == 'escape':
  156. # abort the move action
  157. self.app.inform.emit('[WARNING_NOTCL] %s' % _("Move action cancelled."))
  158. self.toggle()
  159. return
  160. def draw_sel_bbox(self):
  161. xminlist = []
  162. yminlist = []
  163. xmaxlist = []
  164. ymaxlist = []
  165. obj_list = self.app.collection.get_selected()
  166. if not obj_list:
  167. self.app.inform.emit('[WARNING_NOTCL] %s' % _("Object(s) not selected"))
  168. self.toggle()
  169. else:
  170. # if we have an object selected then we can safely activate the mouse events
  171. self.app.plotcanvas.vis_connect('mouse_move', self.on_move)
  172. self.app.plotcanvas.vis_connect('mouse_press', self.on_left_click)
  173. self.app.plotcanvas.vis_connect('key_release', self.on_key_press)
  174. # first get a bounding box to fit all
  175. for obj in obj_list:
  176. xmin, ymin, xmax, ymax = obj.bounds()
  177. xminlist.append(xmin)
  178. yminlist.append(ymin)
  179. xmaxlist.append(xmax)
  180. ymaxlist.append(ymax)
  181. # get the minimum x,y and maximum x,y for all objects selected
  182. xminimal = min(xminlist)
  183. yminimal = min(yminlist)
  184. xmaximal = max(xmaxlist)
  185. ymaximal = max(ymaxlist)
  186. p1 = (xminimal, yminimal)
  187. p2 = (xmaximal, yminimal)
  188. p3 = (xmaximal, ymaximal)
  189. p4 = (xminimal, ymaximal)
  190. self.old_coords = [p1, p2, p3, p4]
  191. self.draw_shape(self.old_coords)
  192. def update_sel_bbox(self, pos):
  193. self.delete_shape()
  194. pt1 = (self.old_coords[0][0] + pos[0], self.old_coords[0][1] + pos[1])
  195. pt2 = (self.old_coords[1][0] + pos[0], self.old_coords[1][1] + pos[1])
  196. pt3 = (self.old_coords[2][0] + pos[0], self.old_coords[2][1] + pos[1])
  197. pt4 = (self.old_coords[3][0] + pos[0], self.old_coords[3][1] + pos[1])
  198. self.draw_shape([pt1, pt2, pt3, pt4])
  199. def delete_shape(self):
  200. self.sel_shapes.clear()
  201. self.sel_shapes.redraw()
  202. def draw_shape(self, coords):
  203. self.sel_rect = Polygon(coords)
  204. if self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().upper() == 'MM':
  205. self.sel_rect = self.sel_rect.buffer(-0.1)
  206. self.sel_rect = self.sel_rect.buffer(0.2)
  207. else:
  208. self.sel_rect = self.sel_rect.buffer(-0.00393)
  209. self.sel_rect = self.sel_rect.buffer(0.00787)
  210. blue_t = Color('blue')
  211. blue_t.alpha = 0.2
  212. self.sel_shapes.add(self.sel_rect, color='blue', face_color=blue_t, update=True, layer=0, tolerance=None)
  213. # end of file