PlotCanvasLegacy.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. ############################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # http://caram.cl/software/flatcam #
  4. # Author: Juan Pablo Caram (c) #
  5. # Date: 2/5/2014 #
  6. # MIT Licence #
  7. # Modified by Marius Stanciu 09/21/2019 #
  8. ############################################################
  9. from PyQt5 import QtGui, QtCore, QtWidgets
  10. # Prevent conflict with Qt5 and above.
  11. from matplotlib import use as mpl_use
  12. mpl_use("Qt5Agg")
  13. from matplotlib.figure import Figure
  14. from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
  15. from matplotlib.backends.backend_agg import FigureCanvasAgg
  16. from matplotlib.widgets import Cursor
  17. # needed for legacy mode
  18. # Used for solid polygons in Matplotlib
  19. from descartes.patch import PolygonPatch
  20. from shapely.geometry import Polygon, LineString, LinearRing, Point, MultiPolygon, MultiLineString
  21. import FlatCAMApp
  22. from copy import deepcopy
  23. import logging
  24. import gettext
  25. import FlatCAMTranslation as fcTranslate
  26. import builtins
  27. fcTranslate.apply_language('strings')
  28. if '_' not in builtins.__dict__:
  29. _ = gettext.gettext
  30. log = logging.getLogger('base')
  31. class CanvasCache(QtCore.QObject):
  32. """
  33. Case story #1:
  34. 1) No objects in the project.
  35. 2) Object is created (new_object() emits object_created(obj)).
  36. on_object_created() adds (i) object to collection and emits
  37. (ii) new_object_available() then calls (iii) object.plot()
  38. 3) object.plot() creates axes if necessary on
  39. app.collection.figure. Then plots on it.
  40. 4) Plots on a cache-size canvas (in background).
  41. 5) Plot completes. Bitmap is generated.
  42. 6) Visible canvas is painted.
  43. """
  44. # Signals:
  45. # A bitmap is ready to be displayed.
  46. new_screen = QtCore.pyqtSignal()
  47. def __init__(self, plotcanvas, app, dpi=50):
  48. super(CanvasCache, self).__init__()
  49. self.app = app
  50. self.plotcanvas = plotcanvas
  51. self.dpi = dpi
  52. self.figure = Figure(dpi=dpi)
  53. self.axes = self.figure.add_axes([0.0, 0.0, 1.0, 1.0], alpha=1.0)
  54. self.axes.set_frame_on(False)
  55. self.axes.set_xticks([])
  56. self.axes.set_yticks([])
  57. self.canvas = FigureCanvasAgg(self.figure)
  58. self.cache = None
  59. def run(self):
  60. log.debug("CanvasCache Thread Started!")
  61. self.plotcanvas.update_screen_request.connect(self.on_update_req)
  62. def on_update_req(self, extents):
  63. """
  64. Event handler for an updated display request.
  65. :param extents: [xmin, xmax, ymin, ymax, zoom(optional)]
  66. """
  67. # log.debug("Canvas update requested: %s" % str(extents))
  68. # Note: This information below might be out of date. Establish
  69. # a protocol regarding when to change the canvas in the main
  70. # thread and when to check these values here in the background,
  71. # or pass this data in the signal (safer).
  72. # log.debug("Size: %s [px]" % str(self.plotcanvas.get_axes_pixelsize()))
  73. # log.debug("Density: %s [units/px]" % str(self.plotcanvas.get_density()))
  74. # Move the requested screen portion to the main thread
  75. # and inform about the update:
  76. self.new_screen.emit()
  77. # Continue to update the cache.
  78. # def on_new_object_available(self):
  79. #
  80. # log.debug("A new object is available. Should plot it!")
  81. class PlotCanvasLegacy(QtCore.QObject):
  82. """
  83. Class handling the plotting area in the application.
  84. """
  85. # Signals:
  86. # Request for new bitmap to display. The parameter
  87. # is a list with [xmin, xmax, ymin, ymax, zoom(optional)]
  88. update_screen_request = QtCore.pyqtSignal(list)
  89. double_click = QtCore.pyqtSignal(object)
  90. def __init__(self, container, app):
  91. """
  92. The constructor configures the Matplotlib figure that
  93. will contain all plots, creates the base axes and connects
  94. events to the plotting area.
  95. :param container: The parent container in which to draw plots.
  96. :rtype: PlotCanvas
  97. """
  98. super(PlotCanvasLegacy, self).__init__()
  99. self.app = app
  100. # Options
  101. self.x_margin = 15 # pixels
  102. self.y_margin = 25 # Pixels
  103. # Parent container
  104. self.container = container
  105. # Plots go onto a single matplotlib.figure
  106. self.figure = Figure(dpi=50) # TODO: dpi needed?
  107. self.figure.patch.set_visible(False)
  108. # These axes show the ticks and grid. No plotting done here.
  109. # New axes must have a label, otherwise mpl returns an existing one.
  110. self.axes = self.figure.add_axes([0.05, 0.05, 0.9, 0.9], label="base", alpha=0.0)
  111. self.axes.set_aspect(1)
  112. self.axes.grid(True)
  113. self.axes.axhline(color=(0.70, 0.3, 0.3), linewidth=2)
  114. self.axes.axvline(color=(0.70, 0.3, 0.3), linewidth=2)
  115. # The canvas is the top level container (FigureCanvasQTAgg)
  116. self.canvas = FigureCanvas(self.figure)
  117. self.canvas.setFocusPolicy(QtCore.Qt.ClickFocus)
  118. self.canvas.setFocus()
  119. self.native = self.canvas
  120. self.adjust_axes(-10, -10, 100, 100)
  121. # self.canvas.set_can_focus(True) # For key press
  122. # Attach to parent
  123. # self.container.attach(self.canvas, 0, 0, 600, 400) # TODO: Height and width are num. columns??
  124. self.container.addWidget(self.canvas) # Qt
  125. # Copy a bitmap of the canvas for quick animation.
  126. # Update every time the canvas is re-drawn.
  127. self.background = self.canvas.copy_from_bbox(self.axes.bbox)
  128. # ## Bitmap Cache
  129. self.cache = CanvasCache(self, self.app)
  130. self.cache_thread = QtCore.QThread()
  131. self.cache.moveToThread(self.cache_thread)
  132. # super(PlotCanvas, self).connect(self.cache_thread, QtCore.SIGNAL("started()"), self.cache.run)
  133. self.cache_thread.started.connect(self.cache.run)
  134. self.cache_thread.start()
  135. self.cache.new_screen.connect(self.on_new_screen)
  136. # Events
  137. self.mp = self.graph_event_connect('button_press_event', self.on_mouse_press)
  138. self.mr = self.graph_event_connect('button_release_event', self.on_mouse_release)
  139. self.mm = self.graph_event_connect('motion_notify_event', self.on_mouse_move)
  140. # self.canvas.connect('configure-event', self.auto_adjust_axes)
  141. self.aaa = self.graph_event_connect('resize_event', self.auto_adjust_axes)
  142. # self.canvas.add_events(Gdk.EventMask.SMOOTH_SCROLL_MASK)
  143. # self.canvas.connect("scroll-event", self.on_scroll)
  144. self.osc = self.graph_event_connect('scroll_event', self.on_scroll)
  145. # self.graph_event_connect('key_press_event', self.on_key_down)
  146. # self.graph_event_connect('key_release_event', self.on_key_up)
  147. self.odr = self.graph_event_connect('draw_event', self.on_draw)
  148. self.mouse = [0, 0]
  149. self.key = None
  150. self.pan_axes = []
  151. self.panning = False
  152. # signal is the mouse is dragging
  153. self.is_dragging = False
  154. # signal if there is a doubleclick
  155. self.is_dblclk = False
  156. def graph_event_connect(self, event_name, callback):
  157. """
  158. Attach an event handler to the canvas through the Matplotlib interface.
  159. :param event_name: Name of the event
  160. :type event_name: str
  161. :param callback: Function to call
  162. :type callback: func
  163. :return: Connection id
  164. :rtype: int
  165. """
  166. if event_name == 'mouse_move':
  167. event_name = 'motion_notify_event'
  168. if event_name == 'mouse_press':
  169. event_name = 'button_press_event'
  170. if event_name == 'mouse_release':
  171. event_name = 'button_release_event'
  172. if event_name == 'mouse_double_click':
  173. return self.double_click.connect(callback)
  174. if event_name == 'key_press':
  175. event_name = 'key_press_event'
  176. return self.canvas.mpl_connect(event_name, callback)
  177. def graph_event_disconnect(self, cid):
  178. """
  179. Disconnect callback with the give id.
  180. :param cid: Callback id.
  181. :return: None
  182. """
  183. # self.double_click.disconnect(cid)
  184. self.canvas.mpl_disconnect(cid)
  185. def on_new_screen(self):
  186. pass
  187. # log.debug("Cache updated the screen!")
  188. def new_cursor(self, axes=None):
  189. # if axes is None:
  190. # c = MplCursor(axes=self.axes, color='black', linewidth=1)
  191. # else:
  192. # c = MplCursor(axes=axes, color='black', linewidth=1)
  193. c = FakeCursor()
  194. return c
  195. def on_key_down(self, event):
  196. """
  197. :param event:
  198. :return:
  199. """
  200. FlatCAMApp.App.log.debug('on_key_down(): ' + str(event.key))
  201. self.key = event.key
  202. def on_key_up(self, event):
  203. """
  204. :param event:
  205. :return:
  206. """
  207. self.key = None
  208. def connect(self, event_name, callback):
  209. """
  210. Attach an event handler to the canvas through the native Qt interface.
  211. :param event_name: Name of the event
  212. :type event_name: str
  213. :param callback: Function to call
  214. :type callback: function
  215. :return: Nothing
  216. """
  217. self.canvas.connect(event_name, callback)
  218. def clear(self):
  219. """
  220. Clears axes and figure.
  221. :return: None
  222. """
  223. # Clear
  224. self.axes.cla()
  225. try:
  226. self.figure.clf()
  227. except KeyError:
  228. FlatCAMApp.App.log.warning("KeyError in MPL figure.clf()")
  229. # Re-build
  230. self.figure.add_axes(self.axes)
  231. self.axes.set_aspect(1)
  232. self.axes.grid(True)
  233. self.axes.axhline(color=(0.70, 0.3, 0.3), linewidth=2)
  234. self.axes.axvline(color=(0.70, 0.3, 0.3), linewidth=2)
  235. self.adjust_axes(-10, -10, 100, 100)
  236. # Re-draw
  237. self.canvas.draw_idle()
  238. def redraw(self):
  239. """
  240. Created only to serve for compatibility with the VisPy plotcanvas (the other graphic engine, 3D)
  241. :return:
  242. """
  243. self.clear()
  244. def adjust_axes(self, xmin, ymin, xmax, ymax):
  245. """
  246. Adjusts all axes while maintaining the use of the whole canvas
  247. and an aspect ratio to 1:1 between x and y axes. The parameters are an original
  248. request that will be modified to fit these restrictions.
  249. :param xmin: Requested minimum value for the X axis.
  250. :type xmin: float
  251. :param ymin: Requested minimum value for the Y axis.
  252. :type ymin: float
  253. :param xmax: Requested maximum value for the X axis.
  254. :type xmax: float
  255. :param ymax: Requested maximum value for the Y axis.
  256. :type ymax: float
  257. :return: None
  258. """
  259. # FlatCAMApp.App.log.debug("PC.adjust_axes()")
  260. if not self.app.collection.get_list():
  261. xmin = -10
  262. ymin = -10
  263. xmax = 100
  264. ymax = 100
  265. width = xmax - xmin
  266. height = ymax - ymin
  267. try:
  268. r = width / height
  269. except ZeroDivisionError:
  270. FlatCAMApp.App.log.error("Height is %f" % height)
  271. return
  272. canvas_w, canvas_h = self.canvas.get_width_height()
  273. canvas_r = float(canvas_w) / canvas_h
  274. x_ratio = float(self.x_margin) / canvas_w
  275. y_ratio = float(self.y_margin) / canvas_h
  276. if r > canvas_r:
  277. ycenter = (ymin + ymax) / 2.0
  278. newheight = height * r / canvas_r
  279. ymin = ycenter - newheight / 2.0
  280. ymax = ycenter + newheight / 2.0
  281. else:
  282. xcenter = (xmax + xmin) / 2.0
  283. newwidth = width * canvas_r / r
  284. xmin = xcenter - newwidth / 2.0
  285. xmax = xcenter + newwidth / 2.0
  286. # Adjust axes
  287. for ax in self.figure.get_axes():
  288. if ax._label != 'base':
  289. ax.set_frame_on(False) # No frame
  290. ax.set_xticks([]) # No tick
  291. ax.set_yticks([]) # No ticks
  292. ax.patch.set_visible(False) # No background
  293. ax.set_aspect(1)
  294. ax.set_xlim((xmin, xmax))
  295. ax.set_ylim((ymin, ymax))
  296. ax.set_position([x_ratio, y_ratio, 1 - 2 * x_ratio, 1 - 2 * y_ratio])
  297. # Sync re-draw to proper paint on form resize
  298. self.canvas.draw()
  299. # #### Temporary place-holder for cached update #####
  300. self.update_screen_request.emit([0, 0, 0, 0, 0])
  301. def auto_adjust_axes(self, *args):
  302. """
  303. Calls ``adjust_axes()`` using the extents of the base axes.
  304. :rtype : None
  305. :return: None
  306. """
  307. xmin, xmax = self.axes.get_xlim()
  308. ymin, ymax = self.axes.get_ylim()
  309. self.adjust_axes(xmin, ymin, xmax, ymax)
  310. def fit_view(self):
  311. self.auto_adjust_axes()
  312. def zoom(self, factor, center=None):
  313. """
  314. Zooms the plot by factor around a given
  315. center point. Takes care of re-drawing.
  316. :param factor: Number by which to scale the plot.
  317. :type factor: float
  318. :param center: Coordinates [x, y] of the point around which to scale the plot.
  319. :type center: list
  320. :return: None
  321. """
  322. factor = 1 / factor
  323. xmin, xmax = self.axes.get_xlim()
  324. ymin, ymax = self.axes.get_ylim()
  325. width = xmax - xmin
  326. height = ymax - ymin
  327. if center is None or center == [None, None]:
  328. center = [(xmin + xmax) / 2.0, (ymin + ymax) / 2.0]
  329. # For keeping the point at the pointer location
  330. relx = (xmax - center[0]) / width
  331. rely = (ymax - center[1]) / height
  332. new_width = width / factor
  333. new_height = height / factor
  334. xmin = center[0] - new_width * (1 - relx)
  335. xmax = center[0] + new_width * relx
  336. ymin = center[1] - new_height * (1 - rely)
  337. ymax = center[1] + new_height * rely
  338. # Adjust axes
  339. for ax in self.figure.get_axes():
  340. ax.set_xlim((xmin, xmax))
  341. ax.set_ylim((ymin, ymax))
  342. # Async re-draw
  343. self.canvas.draw_idle()
  344. # #### Temporary place-holder for cached update #####
  345. self.update_screen_request.emit([0, 0, 0, 0, 0])
  346. def pan(self, x, y):
  347. xmin, xmax = self.axes.get_xlim()
  348. ymin, ymax = self.axes.get_ylim()
  349. width = xmax - xmin
  350. height = ymax - ymin
  351. # Adjust axes
  352. for ax in self.figure.get_axes():
  353. ax.set_xlim((xmin + x * width, xmax + x * width))
  354. ax.set_ylim((ymin + y * height, ymax + y * height))
  355. # Re-draw
  356. self.canvas.draw_idle()
  357. # #### Temporary place-holder for cached update #####
  358. self.update_screen_request.emit([0, 0, 0, 0, 0])
  359. def new_axes(self, name):
  360. """
  361. Creates and returns an Axes object attached to this object's Figure.
  362. :param name: Unique label for the axes.
  363. :return: Axes attached to the figure.
  364. :rtype: Axes
  365. """
  366. return self.figure.add_axes([0.05, 0.05, 0.9, 0.9], label=name)
  367. def on_scroll(self, event):
  368. """
  369. Scroll event handler.
  370. :param event: Event object containing the event information.
  371. :return: None
  372. """
  373. # So it can receive key presses
  374. # self.canvas.grab_focus()
  375. self.canvas.setFocus()
  376. # Event info
  377. # z, direction = event.get_scroll_direction()
  378. if self.key is None:
  379. if event.button == 'up':
  380. self.zoom(1 / 1.5, self.mouse)
  381. else:
  382. self.zoom(1.5, self.mouse)
  383. return
  384. if self.key == 'shift':
  385. if event.button == 'up':
  386. self.pan(0.3, 0)
  387. else:
  388. self.pan(-0.3, 0)
  389. return
  390. if self.key == 'control':
  391. if event.button == 'up':
  392. self.pan(0, 0.3)
  393. else:
  394. self.pan(0, -0.3)
  395. return
  396. def on_mouse_press(self, event):
  397. self.is_dragging = True
  398. # Check for middle mouse button press
  399. if self.app.defaults["global_pan_button"] == '2':
  400. pan_button = 3 # right button for Matplotlib
  401. else:
  402. pan_button = 2 # middle button for Matplotlib
  403. if event.button == pan_button:
  404. # Prepare axes for pan (using 'matplotlib' pan function)
  405. self.pan_axes = []
  406. for a in self.figure.get_axes():
  407. if (event.x is not None and event.y is not None and a.in_axes(event) and
  408. a.get_navigate() and a.can_pan()):
  409. a.start_pan(event.x, event.y, 1)
  410. self.pan_axes.append(a)
  411. # Set pan view flag
  412. if len(self.pan_axes) > 0:
  413. self.panning = True
  414. if event.dblclick:
  415. self.double_click.emit(event)
  416. def on_mouse_release(self, event):
  417. self.is_dragging = False
  418. # Check for middle mouse button release to complete pan procedure
  419. # Check for middle mouse button press
  420. if self.app.defaults["global_pan_button"] == '2':
  421. pan_button = 3 # right button for Matplotlib
  422. else:
  423. pan_button = 2 # middle button for Matplotlib
  424. if event.button == pan_button:
  425. for a in self.pan_axes:
  426. a.end_pan()
  427. # Clear pan flag
  428. self.panning = False
  429. def on_mouse_move(self, event):
  430. """
  431. Mouse movement event hadler. Stores the coordinates. Updates view on pan.
  432. :param event: Contains information about the event.
  433. :return: None
  434. """
  435. try:
  436. x = float(event.xdata)
  437. y = float(event.ydata)
  438. except TypeError:
  439. return
  440. self.mouse = [event.xdata, event.ydata]
  441. self.canvas.restore_region(self.background)
  442. # Update pan view on mouse move
  443. if self.panning is True:
  444. # x_pan, y_pan = self.app.geo_editor.snap(event.xdata, event.ydata)
  445. # self.app.app_cursor.set_data(event, (x_pan, y_pan))
  446. for a in self.pan_axes:
  447. a.drag_pan(1, event.key, event.x, event.y)
  448. # Async re-draw (redraws only on thread idle state, uses timer on backend)
  449. self.canvas.draw_idle()
  450. # #### Temporary place-holder for cached update #####
  451. self.update_screen_request.emit([0, 0, 0, 0, 0])
  452. x, y = self.app.geo_editor.snap(x, y)
  453. if self.app.app_cursor.enabled is True:
  454. # Pointer (snapped)
  455. elements = self.axes.plot(x, y, 'k+', ms=40, mew=2, animated=True)
  456. for el in elements:
  457. self.axes.draw_artist(el)
  458. self.canvas.blit(self.axes.bbox)
  459. def translate_coords(self, position):
  460. """
  461. This does not do much. It's just for code compatibility
  462. :param position: Mouse event position
  463. :return: Tuple with mouse position
  464. """
  465. return position[0], position[1]
  466. def on_draw(self, renderer):
  467. # Store background on canvas redraw
  468. self.background = self.canvas.copy_from_bbox(self.axes.bbox)
  469. def get_axes_pixelsize(self):
  470. """
  471. Axes size in pixels.
  472. :return: Pixel width and height
  473. :rtype: tuple
  474. """
  475. bbox = self.axes.get_window_extent().transformed(self.figure.dpi_scale_trans.inverted())
  476. width, height = bbox.width, bbox.height
  477. width *= self.figure.dpi
  478. height *= self.figure.dpi
  479. return width, height
  480. def get_density(self):
  481. """
  482. Returns unit length per pixel on horizontal
  483. and vertical axes.
  484. :return: X and Y density
  485. :rtype: tuple
  486. """
  487. xpx, ypx = self.get_axes_pixelsize()
  488. xmin, xmax = self.axes.get_xlim()
  489. ymin, ymax = self.axes.get_ylim()
  490. width = xmax - xmin
  491. height = ymax - ymin
  492. return width / xpx, height / ypx
  493. class FakeCursor:
  494. """
  495. This is a fake cursor to ensure compatibility with the OpenGL engine (VisPy).
  496. This way I don't have to chane (disable) things related to the cursor all over when
  497. using the low performance Matplotlib 2D graphic engine.
  498. """
  499. def __init__(self):
  500. self._enabled = True
  501. @property
  502. def enabled(self):
  503. return True if self._enabled else False
  504. @enabled.setter
  505. def enabled(self, value):
  506. self._enabled = value
  507. def set_data(self, pos, **kwargs):
  508. """Internal event handler to draw the cursor when the mouse moves."""
  509. pass
  510. class MplCursor(Cursor):
  511. """
  512. Unfortunately this gets attached to the current axes and if a new axes is added
  513. it will not be showed until that axes is deleted.
  514. Not the kind of behavior needed here so I don't use it anymore.
  515. """
  516. def __init__(self, axes, color='red', linewidth=1):
  517. super().__init__(ax=axes, useblit=True, color=color, linewidth=linewidth)
  518. self._enabled = True
  519. self.axes = axes
  520. self.color = color
  521. self.linewidth = linewidth
  522. self.x = None
  523. self.y = None
  524. @property
  525. def enabled(self):
  526. return True if self._enabled else False
  527. @enabled.setter
  528. def enabled(self, value):
  529. self._enabled = value
  530. self.visible = self._enabled
  531. self.canvas.draw()
  532. def onmove(self, event):
  533. pass
  534. def set_data(self, event, pos):
  535. """Internal event handler to draw the cursor when the mouse moves."""
  536. self.x = pos[0]
  537. self.y = pos[1]
  538. if self.ignore(event):
  539. return
  540. if not self.canvas.widgetlock.available(self):
  541. return
  542. if event.inaxes != self.ax:
  543. self.linev.set_visible(False)
  544. self.lineh.set_visible(False)
  545. if self.needclear:
  546. self.canvas.draw()
  547. self.needclear = False
  548. return
  549. self.needclear = True
  550. if not self.visible:
  551. return
  552. self.linev.set_xdata((self.x, self.x))
  553. self.lineh.set_ydata((self.y, self.y))
  554. self.linev.set_visible(self.visible and self.vertOn)
  555. self.lineh.set_visible(self.visible and self.horizOn)
  556. self._update()
  557. class ShapeCollectionLegacy:
  558. """
  559. This will create the axes for each collection of shapes and will also
  560. hold the collection of shapes into a dict self._shapes.
  561. This handles the shapes redraw on canvas.
  562. """
  563. def __init__(self, obj, app, name=None, annotation_job=None):
  564. """
  565. :param obj: this is the object to which the shapes collection is attached and for
  566. which it will have to draw shapes
  567. :param app: this is the FLatCAM.App usually, needed because we have to access attributes there
  568. :param name: this is the name given to the Matplotlib axes; it needs to be unique due of Matplotlib requurements
  569. :param annotation_job: make this True if the job needed is just for annotation
  570. """
  571. self.obj = obj
  572. self.app = app
  573. self.annotation_job = annotation_job
  574. self._shapes = dict()
  575. self.shape_dict = dict()
  576. self.shape_id = 0
  577. self._color = None
  578. self._face_color = None
  579. self._visible = True
  580. self._update = False
  581. self._alpha = None
  582. self._tool_tolerance = None
  583. self._tooldia = None
  584. self._obj = None
  585. self._gcode_parsed = None
  586. if name is None:
  587. axes_name = self.obj.options['name']
  588. else:
  589. axes_name = name
  590. # Axes must exist and be attached to canvas.
  591. if axes_name not in self.app.plotcanvas.figure.axes:
  592. self.axes = self.app.plotcanvas.new_axes(axes_name)
  593. def add(self, shape=None, color=None, face_color=None, alpha=None, visible=True,
  594. update=False, layer=1, tolerance=0.01, obj=None, gcode_parsed=None, tool_tolerance=None, tooldia=None):
  595. """
  596. This function will add shapes to the shape collection
  597. :param shape: the Shapely shape to be added to the shape collection
  598. :param color: edge color of the shape, hex value
  599. :param face_color: the body color of the shape, hex value
  600. :param alpha: level of transparency of the shape [0.0 ... 1.0]; Float
  601. :param visible: if True will allow the shapes to be added
  602. :param update: not used; just for compatibility with VIsPy canvas
  603. :param layer: just for compatibility with VIsPy canvas
  604. :param tolerance: just for compatibility with VIsPy canvas
  605. :param obj: not used
  606. :param gcode_parsed: not used; just for compatibility with VIsPy canvas
  607. :param tool_tolerance: just for compatibility with VIsPy canvas
  608. :param tooldia:
  609. :return:
  610. """
  611. self._color = color[:-2] if color is not None else None
  612. self._face_color = face_color[:-2] if face_color is not None else None
  613. self._alpha = int(face_color[-2:], 16) / 255 if face_color is not None else 0.75
  614. if alpha is not None:
  615. self._alpha = alpha
  616. self._visible = visible
  617. self._update = update
  618. # CNCJob object related arguments
  619. self._obj = obj
  620. self._gcode_parsed = gcode_parsed
  621. self._tool_tolerance = tool_tolerance
  622. self._tooldia = tooldia
  623. # if self._update:
  624. # self.clear()
  625. try:
  626. for sh in shape:
  627. self.shape_id += 1
  628. self.shape_dict.update({
  629. 'color': self._color,
  630. 'face_color': self._face_color,
  631. 'alpha': self._alpha,
  632. 'shape': sh
  633. })
  634. self._shapes.update({
  635. self.shape_id: deepcopy(self.shape_dict)
  636. })
  637. except TypeError:
  638. self.shape_id += 1
  639. self.shape_dict.update({
  640. 'color': self._color,
  641. 'face_color': self._face_color,
  642. 'alpha': self._alpha,
  643. 'shape': shape
  644. })
  645. self._shapes.update({
  646. self.shape_id: deepcopy(self.shape_dict)
  647. })
  648. return self.shape_id
  649. def clear(self, update=None):
  650. """
  651. Clear the canvas of the shapes.
  652. :param update:
  653. :return: None
  654. """
  655. self._shapes.clear()
  656. self.shape_id = 0
  657. self.axes.cla()
  658. self.app.plotcanvas.auto_adjust_axes()
  659. if update is True:
  660. self.redraw()
  661. def redraw(self):
  662. """
  663. This draw the shapes in the shapes collection, on canvas
  664. :return: None
  665. """
  666. path_num = 0
  667. local_shapes = deepcopy(self._shapes)
  668. try:
  669. obj_type = self.obj.kind
  670. except AttributeError:
  671. obj_type = 'utility'
  672. if self._visible:
  673. for element in local_shapes:
  674. if obj_type == 'excellon':
  675. # Plot excellon (All polygons?)
  676. if self.obj.options["solid"] and isinstance(local_shapes[element]['shape'], Polygon):
  677. patch = PolygonPatch(local_shapes[element]['shape'],
  678. facecolor="#C40000",
  679. edgecolor="#750000",
  680. alpha=local_shapes[element]['alpha'],
  681. zorder=3)
  682. self.axes.add_patch(patch)
  683. else:
  684. x, y = local_shapes[element]['shape'].exterior.coords.xy
  685. self.axes.plot(x, y, 'r-')
  686. for ints in local_shapes[element]['shape'].interiors:
  687. x, y = ints.coords.xy
  688. self.axes.plot(x, y, 'o-')
  689. elif obj_type == 'geometry':
  690. if type(local_shapes[element]['shape']) == Polygon:
  691. x, y = local_shapes[element]['shape'].exterior.coords.xy
  692. self.axes.plot(x, y, local_shapes[element]['color'], linestyle='-')
  693. for ints in local_shapes[element]['shape'].interiors:
  694. x, y = ints.coords.xy
  695. self.axes.plot(x, y, local_shapes[element]['color'], linestyle='-')
  696. elif type(local_shapes[element]['shape']) == LineString or \
  697. type(local_shapes[element]['shape']) == LinearRing:
  698. x, y = local_shapes[element]['shape'].coords.xy
  699. self.axes.plot(x, y, local_shapes[element]['color'], linestyle='-')
  700. elif obj_type == 'gerber':
  701. if self.obj.options["multicolored"]:
  702. linespec = '-'
  703. else:
  704. linespec = 'k-'
  705. if self.obj.options["solid"]:
  706. try:
  707. patch = PolygonPatch(local_shapes[element]['shape'],
  708. facecolor=local_shapes[element]['face_color'],
  709. edgecolor=local_shapes[element]['color'],
  710. alpha=local_shapes[element]['alpha'],
  711. zorder=2)
  712. self.axes.add_patch(patch)
  713. except AssertionError:
  714. FlatCAMApp.App.log.warning("A geometry component was not a polygon:")
  715. FlatCAMApp.App.log.warning(str(element))
  716. else:
  717. x, y = local_shapes[element]['shape'].exterior.xy
  718. self.axes.plot(x, y, linespec)
  719. for ints in local_shapes[element]['shape'].interiors:
  720. x, y = ints.coords.xy
  721. self.axes.plot(x, y, linespec)
  722. elif obj_type == 'cncjob':
  723. if local_shapes[element]['face_color'] is None:
  724. linespec = '--'
  725. linecolor = local_shapes[element]['color']
  726. # if geo['kind'][0] == 'C':
  727. # linespec = 'k-'
  728. x, y = local_shapes[element]['shape'].coords.xy
  729. self.axes.plot(x, y, linespec, color=linecolor)
  730. else:
  731. path_num += 1
  732. if isinstance(local_shapes[element]['shape'], Polygon):
  733. self.axes.annotate(str(path_num), xy=local_shapes[element]['shape'].exterior.coords[0],
  734. xycoords='data', fontsize=20)
  735. else:
  736. self.axes.annotate(str(path_num), xy=local_shapes[element]['shape'].coords[0],
  737. xycoords='data', fontsize=20)
  738. patch = PolygonPatch(local_shapes[element]['shape'],
  739. facecolor=local_shapes[element]['face_color'],
  740. edgecolor=local_shapes[element]['color'],
  741. alpha=local_shapes[element]['alpha'], zorder=2)
  742. self.axes.add_patch(patch)
  743. elif obj_type == 'utility':
  744. # not a FlatCAM object, must be utility
  745. if local_shapes[element]['face_color']:
  746. try:
  747. patch = PolygonPatch(local_shapes[element]['shape'],
  748. facecolor=local_shapes[element]['face_color'],
  749. edgecolor=local_shapes[element]['color'],
  750. alpha=local_shapes[element]['alpha'],
  751. zorder=2)
  752. self.axes.add_patch(patch)
  753. except Exception as e:
  754. log.debug("ShapeCollectionLegacy.redraw() --> %s" % str(e))
  755. else:
  756. if isinstance(local_shapes[element]['shape'], Polygon):
  757. x, y = local_shapes[element]['shape'].exterior.xy
  758. self.axes.plot(x, y, local_shapes[element]['color'], linestyle='-')
  759. for ints in local_shapes[element]['shape'].interiors:
  760. x, y = ints.coords.xy
  761. self.axes.plot(x, y, local_shapes[element]['color'], linestyle='-')
  762. else:
  763. x, y = local_shapes[element]['shape'].coords.xy
  764. self.axes.plot(x, y, local_shapes[element]['color'], linestyle='-')
  765. self.app.plotcanvas.auto_adjust_axes()
  766. def set(self, text, pos, visible=True, font_size=16, color=None):
  767. """
  768. This will set annotations on the canvas.
  769. :param text: a list of text elements to be used as annotations
  770. :param pos: a list of positions for showing the text elements above
  771. :param visible: if True will display annotations, if False will clear them on canvas
  772. :param font_size: the font size or the annotations
  773. :param color: color of the annotations
  774. :return: None
  775. """
  776. if color is None:
  777. color = "#000000FF"
  778. if visible is not True:
  779. self.clear()
  780. return
  781. if len(text) != len(pos):
  782. self.app.inform.emit('[ERROR_NOTCL] %s' % _("Could not annotate due of a difference between the number "
  783. "of text elements and the number of text positions."))
  784. return
  785. for idx in range(len(text)):
  786. try:
  787. self.axes.annotate(text[idx], xy=pos[idx], xycoords='data', fontsize=font_size, color=color)
  788. except Exception as e:
  789. log.debug("ShapeCollectionLegacy.set() --> %s" % str(e))
  790. self.app.plotcanvas.auto_adjust_axes()
  791. @property
  792. def visible(self):
  793. return self._visible
  794. @visible.setter
  795. def visible(self, value):
  796. if value is False:
  797. self.axes.cla()
  798. self.app.plotcanvas.auto_adjust_axes()
  799. else:
  800. if self._visible is False:
  801. self.redraw()
  802. self._visible = value
  803. @property
  804. def enabled(self):
  805. return self._visible
  806. @enabled.setter
  807. def enabled(self, value):
  808. if value is False:
  809. self.axes.cla()
  810. self.app.plotcanvas.auto_adjust_axes()
  811. else:
  812. if self._visible is False:
  813. self.redraw()
  814. self._visible = value