Пример #1
0
    def generate_solid(self):
        """
        Generate an assembled solid shaft using the BRepBuilderAPI_MakeSolid  
        algorithm. This method requires PythonOCC to be installed.

        :raises RuntimeError: if the assembling of the solid shaft is not 
            completed successfully
        :return: solid shaft
        :rtype: OCC.Core.TopoDS.TopoDS_Solid
        """
        ext = os.path.splitext(self.filename)[1][1:]
        if ext == 'stl':
            shaft_compound = read_stl_file(self.filename)
        elif ext == 'iges':
            iges_reader = IGESControl_Reader()
            iges_reader.ReadFile(self.filename)
            iges_reader.TransferRoots()
            shaft_compound = iges_reader.Shape()
        else:
            raise Exception('The shaft file is not in iges/stl formats')
        sewer = BRepBuilderAPI_Sewing(1e-2)
        sewer.Add(shaft_compound)
        sewer.Perform()
        result_sewed_shaft = sewer.SewedShape()
        shaft_solid_maker = BRepBuilderAPI_MakeSolid()
        shaft_solid_maker.Add(OCC.Core.TopoDS.topods_Shell(result_sewed_shaft))
        if not shaft_solid_maker.IsDone():
            raise RuntimeError('Unsuccessful assembling of solid shaft')
        shaft_solid = shaft_solid_maker.Solid()
        return shaft_solid
Пример #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
Пример #3
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
Пример #4
0
    def __init__(self, fn):
        self._reader = IGESControl_Reader()

        # Read file
        status = self._reader.ReadFile(fn)
        if status != IFSelect_RetDone:
            raise RuntimeError("Error reading IGES file.")

        # Convert to desired units
        Interface_Static.SetCVal("xstep.cascade.unit", Settings.units)

        # Transfer
        nroots = self._reader.TransferRoots()
        if nroots > 0:
            self._shape = Shape.wrap(self._reader.OneShape())
Пример #5
0
    def load_shape_from_file(self, filename):
        """
        This class method loads a shape from the file `filename`.

        :param string filename: name of the input file.
            It should have proper extension (.iges or .igs)

        :return: shape: loaded shape
        :rtype: TopoDS_Shape
        """
        self._check_filename_type(filename)
        self._check_extension(filename)
        reader = IGESControl_Reader()
        return_reader = reader.ReadFile(filename)
        # check status
        if return_reader == IFSelect_RetDone:
            return_transfer = reader.TransferRoots()
            if return_transfer:
                # load all shapes in one
                shape = reader.OneShape()

        return shape
Пример #6
0
class IgesRead(object):
    """
    Read an IGES file.

    :param str fn: The file to read.
    """
    def __init__(self, fn):
        self._reader = IGESControl_Reader()

        # Read file
        status = self._reader.ReadFile(fn)
        if status != IFSelect_RetDone:
            raise RuntimeError("Error reading IGES file.")

        # Convert to desired units
        Interface_Static.SetCVal("xstep.cascade.unit", Settings.units)

        # Transfer
        nroots = self._reader.TransferRoots()
        if nroots > 0:
            self._shape = Shape.wrap(self._reader.OneShape())

    @property
    def object(self):
        """
        :return: The IGES reader object.
        :rtype: OCC.Core.IGESControl.IGESControl_Reader
        """
        return self._reader

    @property
    def shape(self):
        """
        :return: The main shape.
        :rtype: afem.topology.entities.Shape
        """
        return self._shape
Пример #7
0
def read_iges(file_name):
    iges_reader = IGESControl_Reader()
    status = iges_reader.ReadFile(file_name)

    if status == IFSelect_RetDone:
        failsonly = False
        iges_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity)
        iges_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity)
        ok = iges_reader.TransferRoots()
        IgesShape = iges_reader.OneShape()
        return IgesShape
    else:
        print("Error: can't read file.")
        sys.exit(0)
Пример #8
0
    def read_file(self):
        """
        Read the IGES file and stores the result in a list of TopoDS_Shape

        """
        igescontrol_reader = IGESControl_Reader()
        status = igescontrol_reader.ReadFile(self._filename)
        igescontrol_reader.PrintCheckLoad(False, IFSelect_ItemsByEntity)
        nb_roots = igescontrol_reader.NbRootsForTransfer()
        logger.info("Nb roots for transfer : %i" % nb_roots)

        if status == IFSelect_RetDone and nb_roots != 0:

            igescontrol_reader.PrintCheckTransfer(False,
                                                  IFSelect_ItemsByEntity)
            ok = igescontrol_reader.TransferRoots()
            logger.info("TransferRoots status : %i" % ok)
            self.nb_shapes = igescontrol_reader.NbShapes()

            for n in range(1, nb_roots + 1):

                logger.debug("Root index %i" % n)

                # for i in range(1, self.nb_shapes + 1):
                a_shape = igescontrol_reader.Shape(n)
                if a_shape.IsNull():
                    msg = "At least one shape in IGES cannot be transferred"
                    logger.warning(msg)
                else:
                    self._shapes.append(a_shape)
                    logger.debug("Appending a %s to list of shapes" %
                                 topo_types_dict[a_shape.ShapeType()])
        else:
            msg = "Status is not IFSelect_RetDone or No root for transfer"
            logger.error(msg)
            raise IgesFileReadException(msg)
Пример #9
0
def read_iges_file(filename,
                   return_as_shapes=False,
                   verbosity=False,
                   visible_only=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.
    """
    if not os.path.isfile(filename):
        raise FileNotFoundError(f"{filename} not found.")

    IGESControl_Controller.Init()

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

    _shapes = []

    builder = BRep_Builder()
    compound = TopoDS_Compound()
    builder.MakeCompound(compound)
    empty_compound = True

    if status != IFSelect_RetDone:  # check status
        raise IOError("Cannot read IGES file")

    if verbosity:
        failsonly = False
        iges_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity)
        iges_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity)
    iges_reader.ClearShapes()
    iges_reader.TransferRoots()
    nbr = iges_reader.NbShapes()

    for i in range(1, nbr + 1):
        a_shp = iges_reader.Shape(i)
        if not a_shp.IsNull():
            if a_shp.ShapeType() in [
                    TopAbs_SOLID, TopAbs_SHELL, TopAbs_COMPOUND
            ]:
                _shapes.append(a_shp)
            else:  # other shape types are merged into a compound
                builder.Add(compound, a_shp)
                empty_compound = False

    if not empty_compound:
        _shapes.append(compound)

    # create a compound and store all shapes
    if not return_as_shapes:
        builder_2 = BRep_Builder()
        compound_2 = TopoDS_Compound()
        builder_2.MakeCompound(compound_2)
        for s in _shapes:
            builder_2.Add(compound_2, s)
        _shapes = compound_2

    return _shapes