ToolMove.py 12 KB

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