ToolMove.py 12 KB

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