Ejemplo n.º 1
0
    def __init__(self, file_path):
        self.h_doc = TDocStd.Handle_TDocStd_Document()
        self.app = XCAFApp.GetApplication().GetObject()
        self.app.NewDocument(TCollection_ExtendedString("MDTV-XCAF"),
                             self.h_doc)

        self.STEPReader = STEPCAFControl_Reader()
        if not self.STEPReader.ReadFile(file_path.encode("utf-8")):
            raise Exception("OpenCascade could not read STEP file")
        self.STEPReader.Transfer(self.h_doc)
        self.doc = self.h_doc.GetObject()
        self.h_shape_tool = XCAFDoc.XCAFDoc_DocumentTool_ShapeTool(
            self.doc.Main())
        self.h_colors_tool = XCAFDoc.XCAFDoc_DocumentTool_ColorTool(
            self.doc.Main())
        self.shape_tool = self.h_shape_tool.GetObject()
        self.color_tool = self.h_colors_tool.GetObject()
        self.shapes = TDF_LabelSequence()
        self.shape_tool.GetShapes(self.shapes)
        roots = TDF_LabelSequence()
        self.shape_tool.GetFreeShapes(roots)
        ws = self.STEPReader.Reader().WS().GetObject()
        model = ws.Model().GetObject()
        model.Clear()
        for i in range(1, 8):
            ws.ClearData(i)
        ws.ClearFile()
Ejemplo n.º 2
0
def read_step_file_shapes(filename):
    _shapes = []

    # create an handle to a document
    h_doc = Handle_TDocStd_Document()

    # Create the application
    app = XCAFApp_Application.GetApplication().GetObject()
    app.NewDocument(TCollection_ExtendedString("MDTV-CAF"), h_doc)

    # Get root assembly
    doc = h_doc.GetObject()
    h_shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main())

    step_reader = STEPCAFControl_Reader()
    step_reader.SetNameMode(True)

    status = step_reader.ReadFile(filename)
    if status == IFSelect_RetDone:
        step_reader.Transfer(doc.GetHandle())

    labels = TDF_LabelSequence()
    shape_tool = h_shape_tool.GetObject()
    h_shape_tool.GetObject().GetFreeShapes(labels)

    print("Number of shapes at root :%i" % labels.Length())
    for i in range(labels.Length()):
        label = labels.Value(i + 1)
        a_shape = h_shape_tool.GetObject().GetShape(label)
        _shapes.append(a_shape)
    return _shapes
Ejemplo n.º 3
0
    def test_read_step_file(self):
        ''' Reads the previous step file '''
        # create an handle to a document
        h_doc = Handle_TDocStd_Document()
        # Create the application
        app = XCAFApp_Application.GetApplication().GetObject()
        app.NewDocument(TCollection_ExtendedString("MDTV-CAF"), h_doc)
        # Get root assembly
        doc = h_doc.GetObject()
        h_shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main())
        l_colors = XCAFDoc_DocumentTool_ColorTool(doc.Main())
        step_reader = STEPCAFControl_Reader()
        step_reader.SetColorMode(True)
        step_reader.SetLayerMode(True)
        step_reader.SetNameMode(True)
        step_reader.SetMatMode(True)
        status = step_reader.ReadFile("./test_io/test_ocaf.stp")
        if status == IFSelect_RetDone:
            step_reader.Transfer(doc.GetHandle())

        labels = TDF_LabelSequence()
        color_labels = TDF_LabelSequence()

        shape_tool = h_shape_tool.GetObject()
        h_shape_tool.GetObject().GetFreeShapes(labels)

        assert (labels.Length() == 1)
        sub_shapes_labels = TDF_LabelSequence()
        assert (not shape_tool.IsAssembly(labels.Value(1)))
        shape_tool.GetSubShapes(labels.Value(1), sub_shapes_labels)
        assert (sub_shapes_labels.Length() == 0)

        l_colors.GetObject().GetColors(color_labels)
        assert (color_labels.Length() == 1)

        label_shp = labels.Value(1)
        a_shape = h_shape_tool.GetObject().GetShape(label_shp)
        assert (not a_shape.IsNull())
# create an handle to a document
h_doc = Handle_TDocStd_Document()

# Create the application
app = XCAFApp_Application.GetApplication().GetObject()
app.NewDocument(TCollection_ExtendedString("MDTV-CAF"), h_doc)

# Get root assembly
doc = h_doc.GetObject()
h_shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main())
l_colors = XCAFDoc_DocumentTool_ColorTool(doc.Main())
l_layers = XCAFDoc_DocumentTool_LayerTool(doc.Main())
l_materials = XCAFDoc_DocumentTool_MaterialTool(doc.Main())

step_reader = STEPCAFControl_Reader()
step_reader.SetColorMode(True)
step_reader.SetLayerMode(True)
step_reader.SetNameMode(True)
step_reader.SetMatMode(True)

status = step_reader.ReadFile(filename)
if status == IFSelect_RetDone:
    step_reader.Transfer(doc.GetHandle())

labels = TDF_LabelSequence()
color_labels = TDF_LabelSequence()

shape_tool = h_shape_tool.GetObject()
h_shape_tool.GetObject().GetFreeShapes(labels)
Ejemplo n.º 5
0
    def __init__(self, file_path, id=None):

        self.file = file_path.encode("utf-8")
        self.id = id
        self.shapes_simples = []
        self.main_product = None
        basename = os.path.basename(self.file)
        self.fileName = os.path.splitext(basename)[0]

        self.STEPReader = STEPCAFControl_Reader()

        if self.STEPReader.ReadFile(self.file) != 1:
            raise OCCReadingStepError

        self.h_doc = TDocStd.Handle_TDocStd_Document()
        self.app = XCAFApp.GetApplication().GetObject()
        self.app.NewDocument(TCollection_ExtendedString("MDTV-XCAF"),
                             self.h_doc)

        self.STEPReader.Transfer(self.h_doc)

        self.doc = self.h_doc.GetObject()
        self.h_shape_tool = XCAFDoc.XCAFDoc_DocumentTool_ShapeTool(
            self.doc.Main())
        self.h_colors_tool = XCAFDoc.XCAFDoc_DocumentTool_ColorTool(
            self.doc.Main())
        self.shape_tool = self.h_shape_tool.GetObject()
        self.color_tool = self.h_colors_tool.GetObject()

        self.shapes = TDF_LabelSequence()
        self.shape_tool.GetShapes(self.shapes)
        for i in range(self.shapes.Length()):
            shape = self.shapes.Value(i + 1)
            if self.shape_tool.IsSimpleShape(shape):
                compShape = self.shape_tool.GetShape(shape)
                t = Topo(compShape)
                if t.number_of_vertices() > 0:
                    label = get_label_name(shape)
                    color = find_color(shape, self.color_tool, self.shape_tool)
                    self.shapes_simples.append(
                        SimpleShape(label, compShape, color))

        roots = TDF_LabelSequence()
        self.shape_tool.GetFreeShapes(roots)
        self.thumbnail_valid = False
        if roots.Length() == 1:
            shape = self.shape_tool.GetShape(roots.Value(1))
            t = Topo(shape)
            if t.number_of_vertices() > 0:
                bbox = Bnd_Box()
                gap = 0
                bbox.SetGap(gap)

                BRepMesh_Mesh(shape, get_mesh_precision(shape, 1))
                faces_iterator = Topo(shape).faces()
                for F in faces_iterator:
                    face_location = TopLoc_Location()
                    BRep_Tool_Triangulation(F, face_location)
                BRepBndLib_Add(shape, bbox)
                x_min, y_min, z_min, x_max, y_max, z_max = bbox.Get()
                diagonal = max(x_max - x_min, y_max - y_min, z_max - z_min)
                if diagonal > 0:
                    self.scale = 200. / diagonal
                    self.trans = ((x_max - x_min) / 2. - x_max,
                                  (y_max - y_min) / 2. - y_max,
                                  (z_max - z_min) / 2. - z_max)
                    self.thumbnail_valid = True

        ws = self.STEPReader.Reader().WS().GetObject()
        model = ws.Model().GetObject()
        model.Clear()
Ejemplo n.º 6
0
class StepImporter(object):
    """

    :param file_path: Path of the file **.stp** to analyzing
    :param id: For the generation of the :class:`.Product`, **id** is assigned like product.doc_id for every node of :class:`.Product`. For the generation of the files of geometry **.geo**, **id** , together with an index generated by the class, are used to identify the content of every file



    Generates from a path of :class:`~django.core.files.File` **.stp**:


    -A set of files **.geo** that represents the geometry of the different simple products that are useful to realize the visualization 3D across the web browser.

    -A structure of information that represents the arborescencse of the different assemblies,represented in a  :class:`.Product` . (Including his spatial location and orientation and his label of reference (:class:`.OCC.TDF.TDF_Label`))


    This class is invoked from three different subprocesses related to the functionality of pythonOCC(generate3D.py , generateComposition.py , generateDecomposition.py).

   """
    def __init__(self, file_path, id=None):

        self.file = file_path.encode("utf-8")
        self.id = id
        self.shapes_simples = []
        self.main_product = None
        basename = os.path.basename(self.file)
        self.fileName = os.path.splitext(basename)[0]

        self.STEPReader = STEPCAFControl_Reader()

        if self.STEPReader.ReadFile(self.file) != 1:
            raise OCCReadingStepError

        self.h_doc = TDocStd.Handle_TDocStd_Document()
        self.app = XCAFApp.GetApplication().GetObject()
        self.app.NewDocument(TCollection_ExtendedString("MDTV-XCAF"),
                             self.h_doc)

        self.STEPReader.Transfer(self.h_doc)

        self.doc = self.h_doc.GetObject()
        self.h_shape_tool = XCAFDoc.XCAFDoc_DocumentTool_ShapeTool(
            self.doc.Main())
        self.h_colors_tool = XCAFDoc.XCAFDoc_DocumentTool_ColorTool(
            self.doc.Main())
        self.shape_tool = self.h_shape_tool.GetObject()
        self.color_tool = self.h_colors_tool.GetObject()

        self.shapes = TDF_LabelSequence()
        self.shape_tool.GetShapes(self.shapes)
        for i in range(self.shapes.Length()):
            shape = self.shapes.Value(i + 1)
            if self.shape_tool.IsSimpleShape(shape):
                compShape = self.shape_tool.GetShape(shape)
                t = Topo(compShape)
                if t.number_of_vertices() > 0:
                    label = get_label_name(shape)
                    color = find_color(shape, self.color_tool, self.shape_tool)
                    self.shapes_simples.append(
                        SimpleShape(label, compShape, color))

        roots = TDF_LabelSequence()
        self.shape_tool.GetFreeShapes(roots)
        self.thumbnail_valid = False
        if roots.Length() == 1:
            shape = self.shape_tool.GetShape(roots.Value(1))
            t = Topo(shape)
            if t.number_of_vertices() > 0:
                bbox = Bnd_Box()
                gap = 0
                bbox.SetGap(gap)

                BRepMesh_Mesh(shape, get_mesh_precision(shape, 1))
                faces_iterator = Topo(shape).faces()
                for F in faces_iterator:
                    face_location = TopLoc_Location()
                    BRep_Tool_Triangulation(F, face_location)
                BRepBndLib_Add(shape, bbox)
                x_min, y_min, z_min, x_max, y_max, z_max = bbox.Get()
                diagonal = max(x_max - x_min, y_max - y_min, z_max - z_min)
                if diagonal > 0:
                    self.scale = 200. / diagonal
                    self.trans = ((x_max - x_min) / 2. - x_max,
                                  (y_max - y_min) / 2. - y_max,
                                  (z_max - z_min) / 2. - z_max)
                    self.thumbnail_valid = True

        ws = self.STEPReader.Reader().WS().GetObject()
        model = ws.Model().GetObject()
        model.Clear()

    def compute_geometries(self, root_path, pov_dir):
        """

        :param root_path: Path where to store the files **.geo** generated

        When we generate a new :class:`.StepImporter` we will refill a list(**shapes_simples**) whit the :class:`.SimpleShape` contained in the file **.stp**

        For each :class:`.SimpleShape` in the list **shapes_simples**:

            We call the method :func:`.write_geometries` to generate a file **.geo** representative of its geometry,the content of the file is identified by the index+1 (>0) of the position of the :class:`.SimpleShape` in the list of **SimpleShapes**  and by the attribue id of :class:`.StepImporter`

        Returns the list of the path of the generated **.geo** files

        """
        files_index = ""
        self.povs = []

        for index, shape in enumerate(self.shapes_simples):
            name = get_available_name(root_path, self.fileName + ".geo")
            path = os.path.join(root_path, name)
            identifier = "_" + str(index + 1) + "_" + str(self.id)
            writer = GeometryWriter(shape, 0.3)
            pov_filename = os.path.join(pov_dir,
                                        os.path.basename(path + ".inc"))
            writer.write_geometries(identifier, path, pov_filename)
            files_index += "GEO:" + name + " , " + str(index + 1) + "\n"
            if writer.triangle_count:
                self.povs.append((os.path.basename(name + ".inc"), identifier))

        return files_index

    def generate_product_arbre(self):
        """

        Generates a :class:`.Product` relative to the assemblies of the file **.stp**, for every node of the :class:`.Product` it includes a label (:class:`.OCC.TDF.TDF_Label`) that represents and identifies the node , openPLM can only work whit a single root **.stp** files

        """

        roots = TDF_LabelSequence()
        self.shape_tool.GetFreeShapes(roots)
        if roots.Length() != 1:
            raise MultiRootError

        deep = 0
        product_id = [1]
        root = roots.Value(1)
        name = get_label_name(root)
        self.main_product = Product(name, deep, root, self.id, product_id[0],
                                    self.file)
        product_id[0] += 1
        expand_product(self.shape_tool, self.main_product, self.shapes_simples,
                       deep + 1, self.id, self.main_product, product_id,
                       self.file)

        return self.main_product