def __init__(self, parent=None): super(QOpenCascadeWidget, self).__init__(parent) # Qt settings self.setBackgroundRole(QPalette.NoRole) self.setMouseTracking(True) # Values for mouse movement self._x0, self._y0 = 0., 0. # Some default settings self._white = Quantity_Color(Quantity_NOC_WHITE) self._black = Quantity_Color(Quantity_NOC_BLACK) # Get window handle self.window_handle = self.winId() # Display connection self.display_connect = Aspect_DisplayConnection() # Graphics driver self._graphics_driver = OpenGl_GraphicDriver(self.display_connect) # Create viewer and view self.my_viewer = V3d_Viewer(self._graphics_driver) self.my_view = self.my_viewer.CreateView() self.wind = Xw_Window(self.display_connect, self.window_handle) self.my_view.SetWindow(self.wind) # Map window if not self.wind.IsMapped(): self.wind.Map() # AIS interactive context self.my_context = AIS_InteractiveContext(self.my_viewer) self.my_context.SetAutomaticHilight(True) self.my_viewer.SetDefaultLights() self.my_viewer.SetLightOn() self.my_view.SetBackgroundColor(Quantity_TOC_RGB, 0.5, 0.5, 0.5) self.my_context.SetDisplayMode(AIS_Shaded, True) self.my_drawer = self.my_context.DefaultDrawer() self.my_drawer.SetFaceBoundaryDraw(True) self.my_view.TriedronDisplay(Aspect_TOTP_RIGHT_LOWER, self._black, 0.08) self.my_view.SetProj(V3d_TypeOfOrientation.V3d_XposYposZpos) # Mesh display mode self._mesh_mode = 1
def _init_wind(self, fit, background): """ Under Linux, wxPython's GetHandle() only returns a valid handle if called after the MainLoop has started. This function completes the parts of the initialisation which require the window handle. It is called from start() using wx.CallAfter() """ # Do only if my_view is empty if self._my_view.IsEmpty(): # Suitable handle to window. Returns a HWND in Windows and an X # Window identifier in Linux hwnd = self.GetHandle() if sys.platform.startswith('win'): import ctypes from OCCT.WNT import WNT_Window ctypes.pythonapi.PyCapsule_New.restype = ctypes.py_object ctypes.pythonapi.PyCapsule_New.argtypes = [ ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p ] capsule = ctypes.pythonapi.PyCapsule_New(hwnd, None, None) # WNT window wind = WNT_Window(capsule) elif sys.platform.startswith('linux'): from OCCT.Xw import Xw_Window # Xw window wind = Xw_Window(self.display_connect, hwnd) else: raise NotImplementedError( 'Support platform not found for visualization.') # Map window if not wind.IsMapped(): wind.Map() # Set the window self._my_view.SetWindow(wind) # Fit if desired if fit: self.fit() # If running in background, just continue. Otherwise, show the window. if background: self._continue() else: self.show()
def __init__(self, parent=None): super(QOpenCascadeWidget, self).__init__(parent) # Qt settings self.setBackgroundRole(QPalette.NoRole) self.setMouseTracking(True) # Values for mouse movement self._x0, self._y0 = 0., 0. # Some default settings self._white = Quantity_Color(Quantity_NOC_WHITE) self._black = Quantity_Color(Quantity_NOC_BLACK) # Display connection self.display_connect = Aspect_DisplayConnection() # Graphics driver self._graphics_driver = OpenGl_GraphicDriver(self.display_connect) # Create viewer and view self.my_viewer = V3d_Viewer(self._graphics_driver) self.my_view = self.my_viewer.CreateView() hwnd = self.winId() if sys.platform.startswith('win'): import ctypes from OCCT.WNT import WNT_Window ctypes.pythonapi.PyCapsule_New.restype = ctypes.py_object ctypes.pythonapi.PyCapsule_New.argtypes = [ ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p ] hwnd = ctypes.pythonapi.PyCapsule_New(hwnd, None, None) window = WNT_Window(hwnd) elif sys.platform.startswith('darwin'): from OCCT.Cocoa import Cocoa_Window window = Cocoa_Window(hwnd) elif sys.platform.startswith('linux'): from OCCT.Xw import Xw_Window window = Xw_Window(self.display_connection, hwnd) else: raise NotImplementedError( 'Support platform not found for visualization.') self.wind = window self.my_view.SetWindow(self.wind) # Map window if not self.wind.IsMapped(): self.wind.Map() # AIS interactive context self.my_context = AIS_InteractiveContext(self.my_viewer) self.my_context.SetAutomaticHilight(True) self.my_viewer.SetDefaultLights() self.my_viewer.SetLightOn() self.my_view.SetBackgroundColor(Quantity_TOC_RGB, 0.5, 0.5, 0.5) self.my_context.SetDisplayMode(AIS_Shaded, True) self.my_drawer = self.my_context.DefaultDrawer() self.my_drawer.SetFaceBoundaryDraw(True) self.my_view.TriedronDisplay(Aspect_TOTP_RIGHT_LOWER, self._black, 0.08) self.my_view.SetProj(V3d_TypeOfOrientation.V3d_XposYposZpos) # Mesh display mode self._mesh_mode = 1
class QOpenCascadeWidget(QOpenGLWidget): """ View for displaying shapes. :param PySide.QtGui.QWidget parent: The parent widget. """ def __init__(self, parent=None): super(QOpenCascadeWidget, self).__init__(parent) # Qt settings self.setBackgroundRole(QPalette.NoRole) self.setMouseTracking(True) # Values for mouse movement self._x0, self._y0 = 0., 0. # Some default settings self._white = Quantity_Color(Quantity_NOC_WHITE) self._black = Quantity_Color(Quantity_NOC_BLACK) # Get window handle self.window_handle = self.winId() # Display connection self.display_connect = Aspect_DisplayConnection() # Graphics driver self._graphics_driver = OpenGl_GraphicDriver(self.display_connect) # Create viewer and view self.my_viewer = V3d_Viewer(self._graphics_driver) self.my_view = self.my_viewer.CreateView() self.wind = Xw_Window(self.display_connect, self.window_handle) self.my_view.SetWindow(self.wind) # Map window if not self.wind.IsMapped(): self.wind.Map() # AIS interactive context self.my_context = AIS_InteractiveContext(self.my_viewer) self.my_context.SetAutomaticHilight(True) self.my_viewer.SetDefaultLights() self.my_viewer.SetLightOn() self.my_view.SetBackgroundColor(Quantity_TOC_RGB, 0.5, 0.5, 0.5) self.my_context.SetDisplayMode(AIS_Shaded, True) self.my_drawer = self.my_context.DefaultDrawer() self.my_drawer.SetFaceBoundaryDraw(True) self.my_view.TriedronDisplay(Aspect_TOTP_RIGHT_LOWER, self._black, 0.08) self.my_view.SetProj(V3d_TypeOfOrientation.V3d_XposYposZpos) # Mesh display mode self._mesh_mode = 1 def paintEvent(self, *args, **kwargs): self.my_view.Redraw() def resizeEvent(self, *args, **kwargs): self.my_view.MustBeResized() def wheelEvent(self, e): if e.delta() > 0: zoom = 1.5 else: zoom = 0.75 self.my_view.SetZoom(zoom) def mousePressEvent(self, e): pos = e.pos() x, y = pos.x(), pos.y() self._x0, self._y0 = x, y self.my_view.StartRotation(x, y) def mouseMoveEvent(self, e): pos = e.pos() x, y = pos.x(), pos.y() button = e.buttons() # Rotate if button == QtCore.Qt.LeftButton: self.my_view.Rotation(x, y) # Pan elif button in [QtCore.Qt.MidButton, QtCore.Qt.RightButton]: dx, dy = x - self._x0, y - self._y0 self._x0, self._y0 = x, y self.my_view.Pan(dx, -dy) def fit(self): """ Fit the contents. :return: None. """ self.my_view.FitAll() self.my_view.ZFitAll() self.my_view.Redraw() def display(self, ais_shape, update=True): """ Display an AIS_Shape. :param OCCT.AIS.AIS_Shape ais_shape: The AIS shape. :param bool update: Option to update the viewer. :return: None. """ self.my_context.Display(ais_shape, update) def display_shape(self, shape, rgb=None, transparency=None, material=Graphic3d_NOM_DEFAULT): """ Display a shape. :param OCCT.TopoDS.TopoDS_Shape shape: The shape. :param rgb: The RGB color (r, g, b). :type rgb: collections.Sequence[float] or OCCT.Quantity.Quantity_Color :param float transparency: The transparency (0 to 1). :param OCCT.Graphic3d.Graphic3d_NameOfMaterial material: The material. :return: The AIS_Shape created for the part. :rtype: OCCT.AIS.AIS_Shape """ ais_shape = AIS_Shape(shape) if isinstance(rgb, (tuple, list)): r, g, b = rgb if r > 1.: r /= 255. if g > 1.: g /= 255. if b > 1.: b /= 255. color = Quantity_Color(r, g, b, Quantity_TOC_RGB) ais_shape.SetColor(color) elif isinstance(rgb, Quantity_Color): ais_shape.SetColor(rgb) if transparency is not None: ais_shape.SetTransparency(transparency) ma = Graphic3d_MaterialAspect(material) ais_shape.SetMaterial(ma) self.my_context.Display(ais_shape, True) return ais_shape def display_geom(self, geom, rgb=None, transparency=None, material=Graphic3d_NOM_DEFAULT): """ Display a geometric entity. :param geom: The geometry. :type geom: OCCT.gp.gp_Pnt or OCCT.Geom.Geom_Curve or OCCT.Geom.Geom_Surface :param rgb: The RGB color (r, g, b). :type rgb: collections.Sequence[float] or OCCT.Quantity.Quantity_Color :param float transparency: The transparency (0 to 1). :param OCCT.Graphic3d.Graphic3d_NameOfMaterial material: The material. :return: The AIS_Shape created for the geometry. Returns *None* if the entity cannot be converted to a shape. :rtype: OCCT.AIS.AIS_Shape or None """ if isinstance(geom, gp_Pnt): shape = BRepBuilderAPI_MakeVertex(geom).Vertex() elif isinstance(geom, Geom_Curve): shape = BRepBuilderAPI_MakeEdge(geom).Edge() elif isinstance(geom, Geom_Surface): shape = BRepBuilderAPI_MakeFace(geom, 1.0e-7).Face() else: return None return self.display_shape(shape, rgb, transparency, material) def display_mesh(self, mesh, mode=None): """ Display a mesh. :param OCCT.SMESH.SMESH_Mesh mesh: The mesh. :param int mode: Display mode for mesh elements (1=wireframe, 2=solid). :return: The MeshVS_Mesh created for the mesh. :rtype: OCCT.MeshVS.MeshVS_Mesh """ if mode is None: mode = self._mesh_mode vs_link = SMESH_MeshVSLink(mesh) mesh_vs = MeshVS_Mesh() mesh_vs.SetDataSource(vs_link) prs_builder = MeshVS_MeshPrsBuilder(mesh_vs) mesh_vs.AddBuilder(prs_builder) mesh_vs_drawer = mesh_vs.GetDrawer() mesh_vs_drawer.SetBoolean(MeshVS_DA_DisplayNodes, False) mesh_vs_drawer.SetColor(MeshVS_DA_EdgeColor, self._black) mesh_vs.SetDisplayMode(mode) self.my_context.Display(mesh_vs, True) return mesh_vs def set_bg_color(self, r, g, b): """ Set the background color. :param float r: The r-value. :param float g: The g-value. :param float b: The b-value. :return: None. """ self.my_view.SetBackgroundColor(Quantity_TOC_RGB, r, g, b) def set_white_background(self): """ Set the background color to white. :return: None. """ self.my_view.SetBackgroundColor(Quantity_TOC_RGB, 1., 1., 1.) def set_mesh_wireframe(self): """ Set the default mesh view to wireframe. :return: None. """ self._mesh_mode = 1 def set_mesh_shaded(self): """ Set the default mesh view to shaded. :return: None. """ self._mesh_mode = 2 def set_display_mode(self, mode='s'): if mode == 's': self.my_context.SetDisplayMode(AIS_Shaded, True) else: self.my_context.SetDisplayMode(AIS_WireFrame, True) def view_iso(self): """ Isometric view. :return: None. """ self.my_view.SetProj(V3d_TypeOfOrientation.V3d_XposYposZpos) def view_top(self): """ Top view. :return: None. """ self.my_view.SetProj(V3d_TypeOfOrientation.V3d_Zpos) def view_bottom(self): """ Bottom view. :return: None. """ self.my_view.SetProj(V3d_TypeOfOrientation.V3d_Zneg) def view_front(self): """ Front view. :return: None. """ self.my_view.SetProj(V3d_TypeOfOrientation.V3d_Xneg) def view_rear(self): """ Rear view. :return: None. """ self.my_view.SetProj(V3d_TypeOfOrientation.V3d_Xpos) def view_left(self): """ Left view. :return: None. """ self.my_view.SetProj(V3d_TypeOfOrientation.V3d_Yneg) def view_right(self): """ Right view. :return: None. """ self.my_view.SetProj(V3d_TypeOfOrientation.V3d_Ypos) def capture(self, fn): """ Capture the screen contents and save to a file. The type of file will be determined by the extension. :param str fn: The filename. :return: None. """ self.my_view.Dump(fn) def remove_all(self): """ Remove all items from the context. :return: None. """ self.my_context.RemoveAll(True) def export_pdf(self, fn): """ Export the screen contents to PDF. :param str fn: The filename. :return: None. """ raise NotImplemented('Need gl2ps library.')