ToolMove.py 12 KB

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