Ejemplo n.º 1
0
    def sigint_handler(signum, frame):
        """ Install handler for the SIGINT signal. To kill app through shell.

        :param signum:
        :param frame:
        :return:
        """
        QApplication.exit()
Ejemplo n.º 2
0
class ViewerQt(QMainWindow):
    """
    Simple class for viewing items using Qt.

    :param int width: Window width.
    :param int height: Window height.
    :param PySide.QtGui.QWidget parent: The parent widget.
    """
    def __init__(self, width=800, height=600, parent=None):
        # Start app
        self._app = QApplication.instance()
        if self._app is None:
            self._app = QApplication([])
        super(ViewerQt, self).__init__(parent)

        # Window settings
        self.setWindowTitle('pyOCCT')
        _icon = os.path.dirname(__file__) + '/_resources/icon.png'
        _qicon = QIcon(_icon)
        self.setWindowIcon(_qicon)

        # Create the OCCT view
        frame = QFrame(self)
        layout = QVBoxLayout(frame)
        layout.setContentsMargins(0, 0, 0, 0)
        self._the_view = QOpenCascadeWidget()
        layout.addWidget(self._the_view)
        self.setCentralWidget(frame)
        self.show()
        self.resize(width, height)
        self.view.my_view.MustBeResized()

    @property
    def view(self):
        return self._the_view

    def _continue(self, *args):
        self._app.exit()

    def keyPressEvent(self, e):
        if e.key() == ord('F'):
            self.view.fit()
        elif e.key() == ord('0'):
            self.view.view_iso()
        elif e.key() == ord('1'):
            self.view.view_front()
        elif e.key() == ord('2'):
            self.view.view_top()
        elif e.key() == ord('3'):
            self.view.view_right()
        elif e.key() == ord('4'):
            self.view.view_rear()
        elif e.key() == ord('5'):
            self.view.view_bottom()
        elif e.key() == ord('6'):
            self.view.view_left()
        elif e.key() == ord('S'):
            self.view.set_display_mode('s')
        elif e.key() == ord('W'):
            self.view.set_display_mode('w')
        elif e.key() == ord('C'):
            self._continue()
        else:
            print('Key is not mapped to anything.')

    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
        """
        return self.view.display_shape(shape, rgb, transparency, material)

    def display_mesh(self,
                     mesh,
                     mode=2,
                     group=None,
                     display_nodes=False,
                     node_size=1,
                     node_color=(1, 1, 1),
                     display_edges=True,
                     edge_size=1,
                     edge_color=(0.5, 0.5, 0.5),
                     beam_size=2,
                     beam_color=(1, 1, 0),
                     face_color=(0, 0, 0.5),
                     back_face_color=None):
        """
        Display a mesh.

        :param mesh: The mesh.
        :type mesh: OCCT.SMESH_SMESH_Mesh or OCCT.SMESH_SMESH_subMesh
        :param int mode: Display mode for mesh elements (1=wireframe, 2=solid).
        :param group: Option to display a group of mesh elements.
        :type group: None or OCCT.SMESH.SMESH_Group group
        :param bool display_nodes: Option to display mesh nodes or not. If a group of nodes is
            provided, then this option is turned on by default.
        :param float node_size: An option to scale the size of the node markers.
        :param node_color: The RGB values for the node markers between 0 and 1.
        :type node_color: tuple(float, float, float)
        :param bool display_edges: An option to display the edges of faces and beams.
        :param float edge_size: An option to scale the size of the edges.
        :param edge_color: The RGB values for the edges between 0 and 1.
        :type edge_color: tuple(float, float, float)
        :param float beam_size: An option to scale the size of the beams.
        :param beam_color: The RGB values for the beams between 0 and 1.
        :type beam_color: tuple(float, float, float)
        :param face_color: The RGB values for the faces between 0 and 1.
        :type face_color: tuple(float, float, float)
        :param back_face_color: The RGB values of the back side of the faces between 0 and 1. If not
            provided, then the back faces are colored the same as the faces.
        :type back_face_color: None or tuple(float, float, float)

        :return: The MeshVS_Mesh created for the mesh.
        :rtype: OCCT.MeshVS.MeshVS_Mesh
        """
        return self.view.display_mesh(mesh, mode, group, display_nodes,
                                      node_size, node_color, display_edges,
                                      edge_size, edge_color, beam_size,
                                      beam_color, face_color, back_face_color)

    def add(self,
            entity,
            rgb=None,
            transparency=None,
            material=Graphic3d_NOM_DEFAULT,
            mode=None):
        """
        Add an entity to the view.

        :param entity: The entity.
        :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.
        :param int mode: Display mode for mesh elements (1=wireframe, 2=solid).

        :return: The AIS_Shape created for the entity. Returns *None* if the
            entity cannot be converted to a shape.
        :rtype: OCCT.AIS.AIS_Shape or None
        """
        if isinstance(entity, TopoDS_Shape):
            return self.view.display_shape(entity, rgb, transparency, material)
        elif isinstance(entity, (gp_Pnt, Geom_Curve, Geom_Surface)):
            return self.view.display_geom(entity, rgb, transparency, material)
        elif HAS_SMESH and isinstance(entity, SMESH_Mesh):
            return self.view.display_mesh(entity, mode)
        else:
            return None

    def clear(self):
        """
        Clear contents of the view.

        :return: None.
        """

        self.view.remove_all()

    def start(self, fit=True):
        """
        Start the viewer.

        :param bool fit: Option to fit contents.

        :return: None.
        """
        print('Press \"c\" to continue...')

        if fit:
            self.view.fit()
        self._app.exec_()
Ejemplo n.º 3
0
 def on_quitApp_clicked(self):
     if os.getenv('DESIGNER', False):
         self.accept()
     else:
         QApplication.exit()
Ejemplo n.º 4
0
class ModalTester(object):
    """
    Helper class for testing modal widgets (dialogs).
    """
    def __init__(self, creator, tester):
        """
        Initialise ModalTester.
        :param creator: Function without arguments that creates a modal widget. Can be a class name.
            A modal widget must have exec_() method.
        :param tester: A function taking a widget as its argument that does testing.
        """
        self.app = QApplication.instance()
        if self.app is None:
            self.app = QApplication([''])
        self.creator = creator
        self.tester = tester
        self.widget = None
        self.timer = None
        self.passed = False

    def __call__(self):
        """
        Initialise testing.
        """
        try:
            self.widget = self.creator()
        except:
            traceback.print_exc()
            self.app.exit(0)
        if self.widget is not None:
            self.widget.exec_()

    def _idle(self):
        """
        This function runs every time in QApplication's event loop.
        Call the testing function.
        """
        modal_widget = self.app.activeModalWidget()
        if modal_widget is not None:
            if self.widget is modal_widget:
                try:
                    self.tester(self.widget)
                    self.passed = True
                except:
                    traceback.print_exc()
            if modal_widget.isVisible():
                modal_widget.close()

    def start(self):
        """
        Start this tester.
        """
        self.timer = QTimer()
        # Connecting self.idle to a QTimer with 0 time delay
        # makes it called when there are no events to process
        self.timer.timeout.connect(self._idle)
        self.timer.start()
        # This calls __call__() method
        QTimer.singleShot(0, self)
        # Start the event loop
        self.app.exec_()
Ejemplo n.º 5
0
class ShapeViewerQt(QMainWindow):
    """
    Simple class for viewing items using Qt.

    :param int width: Window width.
    :param int height: Window height.
    :param PySide.QtGui.QWidget parent: The parent widget.
    """
    def __init__(self, width=800, height=600, parent=None):
        # Start app
        self._app = QApplication.instance()
        if self._app is None:
            self._app = QApplication([])
        super(ShapeViewerQt, self).__init__(parent)

        # Window settings
        self.setWindowTitle('pyOCCT')
        _icon = os.path.dirname(__file__) + '/_resources/icon.png'
        _qicon = QIcon(_icon)
        self.setWindowIcon(_qicon)

        # Create the OCCT view
        frame = QFrame(self)
        layout = QVBoxLayout(frame)
        layout.setContentsMargins(0, 0, 0, 0)
        self._the_view = QOpenCascadeWidget(self)
        layout.addWidget(self._the_view)
        self.setCentralWidget(frame)
        self.show()
        self.resize(width, height)
        self.view.my_view.MustBeResized()

    @property
    def view(self):
        return self._the_view

    def _continue(self, *args):
        self._app.exit()

    def keyPressEvent(self, e):
        if e.key() == ord('F'):
            self.view.fit()
        elif e.key() == ord('0'):
            self.view.view_iso()
        elif e.key() == ord('1'):
            self.view.view_front()
        elif e.key() == ord('2'):
            self.view.view_top()
        elif e.key() == ord('3'):
            self.view.view_right()
        elif e.key() == ord('4'):
            self.view.view_rear()
        elif e.key() == ord('5'):
            self.view.view_bottom()
        elif e.key() == ord('6'):
            self.view.view_left()
        elif e.key() == ord('S'):
            self.view.set_display_mode('s')
        elif e.key() == ord('W'):
            self.view.set_display_mode('w')
        elif e.key() == ord('C'):
            self._continue()
        else:
            print('Key is not mapped to anything.')

    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
        """
        return self.view.display_shape(shape, rgb, transparency, material)

    def add(self,
            entity,
            rgb=None,
            transparency=None,
            material=Graphic3d_NOM_DEFAULT):
        """
        Add an entity to the view.

        :param entity: The entity.
        :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 entity. Returns *None* if the
            entity cannot be converted to a shape.
        :rtype: OCCT.AIS.AIS_Shape or None
        """
        if isinstance(entity, TopoDS_Shape):
            return self.view.display_shape(entity, rgb, transparency, material)
        elif isinstance(entity, (gp_Pnt, Geom_Curve, Geom_Surface)):
            return self.view.display_geom(entity, rgb, transparency, material)
        else:
            return None

    def clear(self):
        """
        Clear contents of the view.

        :return: None.
        """

        self.view.remove_all()

    def start(self, fit=True):
        """
        Start the viewer.

        :param bool fit: Option to fit contents.

        :return: None.
        """
        print('Press \"c\" to continue...')

        if fit:
            self.view.fit()
        self._app.exec_()