ToolMove.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. # remove any mark aperture shape that may be displayed
  120. for sel_obj in obj_list:
  121. # if the Gerber mark shapes are enabled they need to be disabled before move
  122. if isinstance(sel_obj, FlatCAMGerber):
  123. sel_obj.ui.aperture_table_visibility_cb.setChecked(False)
  124. try:
  125. sel_obj.replotApertures.emit()
  126. except Exception as e:
  127. pass
  128. for sel_obj in obj_list:
  129. # offset solid_geometry
  130. sel_obj.offset((dx, dy))
  131. # Update the object bounding box options
  132. a, b, c, d = sel_obj.bounds()
  133. sel_obj.options['xmin'] = a
  134. sel_obj.options['ymin'] = b
  135. sel_obj.options['xmax'] = c
  136. sel_obj.options['ymax'] = d
  137. # time to plot the moved objects
  138. self.replot_signal.emit(obj_list)
  139. except Exception as e:
  140. proc.done()
  141. self.app.inform.emit('[ERROR_NOTCL] %s --> %s' % ('ToolMove.on_left_click()', str(e)))
  142. return "fail"
  143. proc.done()
  144. # delete the selection bounding box
  145. self.delete_shape()
  146. self.app.inform.emit('[success] %s %s' %
  147. (str(sel_obj.kind).capitalize(), 'object was moved ...'))
  148. self.app.worker_task.emit({'fcn': job_move, 'params': [self]})
  149. self.clicked_move = 0
  150. self.toggle()
  151. return
  152. except TypeError as e:
  153. log.debug("ToolMove.on_left_click() --> %s" % str(e))
  154. self.app.inform.emit('[ERROR_NOTCL] ToolMove.on_left_click() --> %s' %
  155. _('Error when mouse left click.'))
  156. return
  157. self.clicked_move = 1
  158. def replot(self, obj_list):
  159. def worker_task():
  160. with self.app.proc_container.new('%s...' % _("Plotting")):
  161. for sel_obj in obj_list:
  162. sel_obj.plot()
  163. self.app.worker_task.emit({'fcn': worker_task, 'params': []})
  164. def on_move(self, event):
  165. if self.app.is_legacy is False:
  166. event_pos = event.pos
  167. else:
  168. event_pos = (event.xdata, event.ydata)
  169. try:
  170. x = float(event_pos[0])
  171. y = float(event_pos[1])
  172. except TypeError:
  173. return
  174. pos_canvas = self.app.plotcanvas.translate_coords((x, y))
  175. # if GRID is active we need to get the snapped positions
  176. if self.app.grid_status() == True:
  177. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  178. else:
  179. pos = pos_canvas
  180. if self.point1 is None:
  181. dx = pos[0]
  182. dy = pos[1]
  183. else:
  184. dx = pos[0] - self.point1[0]
  185. dy = pos[1] - self.point1[1]
  186. if self.clicked_move == 1:
  187. self.update_sel_bbox((dx, dy))
  188. def on_key_press(self, event):
  189. if event.key == 'escape':
  190. # abort the move action
  191. self.app.inform.emit('[WARNING_NOTCL] %s' % _("Move action cancelled."))
  192. self.toggle()
  193. return
  194. def draw_sel_bbox(self):
  195. xminlist = []
  196. yminlist = []
  197. xmaxlist = []
  198. ymaxlist = []
  199. obj_list = self.app.collection.get_selected()
  200. if not obj_list:
  201. self.app.inform.emit('[WARNING_NOTCL] %s' % _("Object(s) not selected"))
  202. self.toggle()
  203. else:
  204. # if we have an object selected then we can safely activate the mouse events
  205. self.mm = self.app.plotcanvas.graph_event_connect('mouse_move', self.on_move)
  206. self.mp = self.app.plotcanvas.graph_event_connect('mouse_press', self.on_left_click)
  207. self.kr = self.app.plotcanvas.graph_event_connect('key_release', self.on_key_press)
  208. # first get a bounding box to fit all
  209. for obj in obj_list:
  210. xmin, ymin, xmax, ymax = obj.bounds()
  211. xminlist.append(xmin)
  212. yminlist.append(ymin)
  213. xmaxlist.append(xmax)
  214. ymaxlist.append(ymax)
  215. # get the minimum x,y and maximum x,y for all objects selected
  216. xminimal = min(xminlist)
  217. yminimal = min(yminlist)
  218. xmaximal = max(xmaxlist)
  219. ymaximal = max(ymaxlist)
  220. p1 = (xminimal, yminimal)
  221. p2 = (xmaximal, yminimal)
  222. p3 = (xmaximal, ymaximal)
  223. p4 = (xminimal, ymaximal)
  224. self.old_coords = [p1, p2, p3, p4]
  225. self.draw_shape(Polygon(self.old_coords))
  226. if self.app.is_legacy is True:
  227. self.sel_shapes.redraw()
  228. def update_sel_bbox(self, pos):
  229. self.delete_shape()
  230. pt1 = (self.old_coords[0][0] + pos[0], self.old_coords[0][1] + pos[1])
  231. pt2 = (self.old_coords[1][0] + pos[0], self.old_coords[1][1] + pos[1])
  232. pt3 = (self.old_coords[2][0] + pos[0], self.old_coords[2][1] + pos[1])
  233. pt4 = (self.old_coords[3][0] + pos[0], self.old_coords[3][1] + pos[1])
  234. self.draw_shape(Polygon([pt1, pt2, pt3, pt4]))
  235. if self.app.is_legacy is True:
  236. self.sel_shapes.redraw()
  237. def delete_shape(self):
  238. self.sel_shapes.clear()
  239. self.sel_shapes.redraw()
  240. def draw_shape(self, shape):
  241. if self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().upper() == 'MM':
  242. proc_shape = shape.buffer(-0.1)
  243. proc_shape = proc_shape.buffer(0.2)
  244. else:
  245. proc_shape = shape.buffer(-0.00393)
  246. proc_shape = proc_shape.buffer(0.00787)
  247. # face = Color('blue')
  248. # face.alpha = 0.2
  249. face = '#0000FF' + str(hex(int(0.2 * 255)))[2:]
  250. outline = '#0000FFAF'
  251. self.sel_shapes.add(proc_shape, color=outline, face_color=face, update=True, layer=0, tolerance=None)
  252. # end of file