예제 #1
0
def read_iges_file(filename, return_as_shapes=False, verbosity=False):
    """ read the IGES file and returns a compound
    filename: the file path
    return_as_shapes: optional, False by default. If True returns a list of shapes,
                      else returns a single compound
    verbosity: optionl, False by default.
    """
    assert os.path.isfile(filename)

    iges_reader = IGESControl_Reader()
    status = iges_reader.ReadFile(filename)

    _shapes = []

    if status == IFSelect_RetDone:  # check status
        if verbosity:
            failsonly = False
            iges_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity)
            iges_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity)
        iges_reader.TransferRoots()
        nbr = iges_reader.NbRootsForTransfer()
        for n in range(1, nbr + 1):
            nbs = iges_reader.NbShapes()
            if nbs == 0:
                print("At least one shape in IGES cannot be transfered")
            elif nbr == 1 and nbs == 1:
                aResShape = iges_reader.Shape(1)
                if aResShape.IsNull():
                    print("At least one shape in IGES cannot be transferred")
                else:
                    _shapes.append(aResShape)
            else:
                for i in range(1, nbs + 1):
                    aShape = iges_reader.Shape(i)
                    if aShape.IsNull():
                        print(
                            "At least one shape in STEP cannot be transferred")
                    else:
                        _shapes.append(aShape)
    # if not return as shapes
    # create a compound and store all shapes
    # TODO
    if not return_as_shapes:
        builder = BRep_Builder()
        Comp = TopoDS_Compound()
        builder.MakeCompound(Comp)
        for s in _shapes:
            builder.Add(Comp, s)
        _shapes = Comp
    return _shapes
예제 #2
0
 def read_file(self):
     """
     Read the IGES file and stores the result in a list of TopoDS_Shape
     """
     aReader = IGESControl_Reader()
     status = aReader.ReadFile(self._filename)
     if status == IFSelect_RetDone:
         failsonly = False
         aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity)
         nbr = aReader.NbRootsForTransfer()
         aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity)
         # ok = aReader.TransferRoots()
         for n in range(1, nbr + 1):
             self.nbs = aReader.NbShapes()
             if self.nbs == 0:
                 print("At least one shape in IGES cannot be transfered")
             elif nbr == 1 and self.nbs == 1:
                 aResShape = aReader.Shape(1)
                 if aResShape.IsNull():
                     print(
                         "At least one shape in IGES cannot be transferred")
                 self._shapes.append(aResShape)
             else:
                 for i in range(1, self.nbs + 1):
                     aShape = aReader.Shape(i)
                     if aShape.IsNull():
                         print(
                             "At least one shape in STEP cannot be transferred"
                         )
                     else:
                         self._shapes.append(aShape)
         return True
     else:
         print("Error: can't read file %s" % self._filename)
         return False
     return False