PlotCanvas.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. # ########################################################## ##
  8. from PyQt5 import QtCore
  9. import logging
  10. from flatcamGUI.VisPyCanvas import VisPyCanvas, time
  11. from flatcamGUI.VisPyVisuals import ShapeGroup, ShapeCollection, TextCollection, TextGroup, Cursor
  12. from vispy.scene.visuals import InfiniteLine, Line
  13. import numpy as np
  14. from vispy.geometry import Rect
  15. log = logging.getLogger('base')
  16. class PlotCanvas(QtCore.QObject, VisPyCanvas):
  17. """
  18. Class handling the plotting area in the application.
  19. """
  20. def __init__(self, container, fcapp):
  21. """
  22. The constructor configures the VisPy figure that
  23. will contain all plots, creates the base axes and connects
  24. events to the plotting area.
  25. :param container: The parent container in which to draw plots.
  26. :rtype: PlotCanvas
  27. """
  28. super(PlotCanvas, self).__init__()
  29. VisPyCanvas.__init__(self)
  30. # VisPyCanvas does not allow new attributes. Override.
  31. self.unfreeze()
  32. self.fcapp = fcapp
  33. # Parent container
  34. self.container = container
  35. # workspace lines; I didn't use the rectangle because I didn't want to add another VisPy Node,
  36. # which might decrease performance
  37. self.b_line, self.r_line, self.t_line, self.l_line = None, None, None, None
  38. # <VisPyCanvas>
  39. self.create_native()
  40. self.native.setParent(self.fcapp.ui)
  41. # <QtCore.QObject>
  42. self.container.addWidget(self.native)
  43. # ## AXIS # ##
  44. self.v_line = InfiniteLine(pos=0, color=(0.70, 0.3, 0.3, 1.0), vertical=True,
  45. parent=self.view.scene)
  46. self.h_line = InfiniteLine(pos=0, color=(0.70, 0.3, 0.3, 1.0), vertical=False,
  47. parent=self.view.scene)
  48. # draw a rectangle made out of 4 lines on the canvas to serve as a hint for the work area
  49. # all CNC have a limited workspace
  50. self.draw_workspace()
  51. # if self.app.defaults['global_workspace'] is True:
  52. # if self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().upper() == 'MM':
  53. # self.wkspace_t = Line(pos=)
  54. self.shape_collections = []
  55. self.shape_collection = self.new_shape_collection()
  56. self.fcapp.pool_recreated.connect(self.on_pool_recreated)
  57. self.text_collection = self.new_text_collection()
  58. # TODO: Should be setting to show/hide CNC job annotations (global or per object)
  59. self.text_collection.enabled = True
  60. # Keep VisPy canvas happy by letting it be "frozen" again.
  61. self.freeze()
  62. # draw a rectangle made out of 4 lines on the canvas to serve as a hint for the work area
  63. # all CNC have a limited workspace
  64. def draw_workspace(self):
  65. a = np.empty((0, 0))
  66. a4p_in = np.array([(0, 0), (8.3, 0), (8.3, 11.7), (0, 11.7)])
  67. a4l_in = np.array([(0, 0), (11.7, 0), (11.7, 8.3), (0, 8.3)])
  68. a3p_in = np.array([(0, 0), (11.7, 0), (11.7, 16.5), (0, 16.5)])
  69. a3l_in = np.array([(0, 0), (16.5, 0), (16.5, 11.7), (0, 11.7)])
  70. a4p_mm = np.array([(0, 0), (210, 0), (210, 297), (0, 297)])
  71. a4l_mm = np.array([(0, 0), (297, 0), (297,210), (0, 210)])
  72. a3p_mm = np.array([(0, 0), (297, 0), (297, 420), (0, 420)])
  73. a3l_mm = np.array([(0, 0), (420, 0), (420, 297), (0, 297)])
  74. if self.fcapp.ui.general_defaults_form.general_app_group.units_radio.get_value().upper() == 'MM':
  75. if self.fcapp.defaults['global_workspaceT'] == 'A4P':
  76. a = a4p_mm
  77. elif self.fcapp.defaults['global_workspaceT'] == 'A4L':
  78. a = a4l_mm
  79. elif self.fcapp.defaults['global_workspaceT'] == 'A3P':
  80. a = a3p_mm
  81. elif self.fcapp.defaults['global_workspaceT'] == 'A3L':
  82. a = a3l_mm
  83. else:
  84. if self.fcapp.defaults['global_workspaceT'] == 'A4P':
  85. a = a4p_in
  86. elif self.fcapp.defaults['global_workspaceT'] == 'A4L':
  87. a = a4l_in
  88. elif self.fcapp.defaults['global_workspaceT'] == 'A3P':
  89. a = a3p_in
  90. elif self.fcapp.defaults['global_workspaceT'] == 'A3L':
  91. a = a3l_in
  92. self.delete_workspace()
  93. self.b_line = Line(pos=a[0:2], color=(0.70, 0.3, 0.3, 1.0),
  94. antialias= True, method='agg', parent=self.view.scene)
  95. self.r_line = Line(pos=a[1:3], color=(0.70, 0.3, 0.3, 1.0),
  96. antialias= True, method='agg', parent=self.view.scene)
  97. self.t_line = Line(pos=a[2:4], color=(0.70, 0.3, 0.3, 1.0),
  98. antialias= True, method='agg', parent=self.view.scene)
  99. self.l_line = Line(pos=np.array((a[0], a[3])), color=(0.70, 0.3, 0.3, 1.0),
  100. antialias= True, method='agg', parent=self.view.scene)
  101. if self.fcapp.defaults['global_workspace'] is False:
  102. self.delete_workspace()
  103. # delete the workspace lines from the plot by removing the parent
  104. def delete_workspace(self):
  105. try:
  106. self.b_line.parent = None
  107. self.r_line.parent = None
  108. self.t_line.parent = None
  109. self.l_line.parent = None
  110. except Exception as e:
  111. pass
  112. # redraw the workspace lines on the plot by readding them to the parent view.scene
  113. def restore_workspace(self):
  114. try:
  115. self.b_line.parent = self.view.scene
  116. self.r_line.parent = self.view.scene
  117. self.t_line.parent = self.view.scene
  118. self.l_line.parent = self.view.scene
  119. except Exception as e:
  120. pass
  121. def vis_connect(self, event_name, callback):
  122. return getattr(self.events, event_name).connect(callback)
  123. def vis_disconnect(self, event_name, callback=None):
  124. if callback is None:
  125. getattr(self.events, event_name).disconnect()
  126. else:
  127. getattr(self.events, event_name).disconnect(callback)
  128. def zoom(self, factor, center=None):
  129. """
  130. Zooms the plot by factor around a given
  131. center point. Takes care of re-drawing.
  132. :param factor: Number by which to scale the plot.
  133. :type factor: float
  134. :param center: Coordinates [x, y] of the point around which to scale the plot.
  135. :type center: list
  136. :return: None
  137. """
  138. self.view.camera.zoom(factor, center)
  139. def new_shape_group(self, shape_collection=None):
  140. if shape_collection:
  141. return ShapeGroup(shape_collection)
  142. return ShapeGroup(self.shape_collection)
  143. def new_shape_collection(self, **kwargs):
  144. # sc = ShapeCollection(parent=self.view.scene, pool=self.app.pool, **kwargs)
  145. # self.shape_collections.append(sc)
  146. # return sc
  147. return ShapeCollection(parent=self.view.scene, pool=self.fcapp.pool, **kwargs)
  148. def new_cursor(self):
  149. c = Cursor(pos=np.empty((0, 2)), parent=self.view.scene)
  150. c.antialias = 0
  151. return c
  152. def new_text_group(self, collection=None):
  153. if collection:
  154. return TextGroup(collection)
  155. else:
  156. return TextGroup(self.text_collection)
  157. def new_text_collection(self, **kwargs):
  158. return TextCollection(parent=self.view.scene, **kwargs)
  159. def fit_view(self, rect=None):
  160. # Lock updates in other threads
  161. self.shape_collection.lock_updates()
  162. if not rect:
  163. rect = Rect(-1, -1, 20, 20)
  164. try:
  165. rect.left, rect.right = self.shape_collection.bounds(axis=0)
  166. rect.bottom, rect.top = self.shape_collection.bounds(axis=1)
  167. except TypeError:
  168. pass
  169. # adjust the view camera to be slightly bigger than the bounds so the shape colleaction can be seen clearly
  170. # otherwise the shape collection boundary will have no border
  171. rect.left *= 0.96
  172. rect.bottom *= 0.96
  173. rect.right *= 1.01
  174. rect.top *= 1.01
  175. self.view.camera.rect = rect
  176. self.shape_collection.unlock_updates()
  177. def fit_center(self, loc, rect=None):
  178. # Lock updates in other threads
  179. self.shape_collection.lock_updates()
  180. if not rect:
  181. try:
  182. rect = Rect(loc[0]-20, loc[1]-20, 40, 40)
  183. except TypeError:
  184. pass
  185. self.view.camera.rect = rect
  186. self.shape_collection.unlock_updates()
  187. def clear(self):
  188. pass
  189. def redraw(self):
  190. self.shape_collection.redraw([])
  191. self.text_collection.redraw()
  192. def on_pool_recreated(self, pool):
  193. self.shape_collection.pool = pool