Beispiel #1
0
    def load_shape_from_file(self, filename):
        """
        This method loads a shape from the file `filename`.

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

        :return: shape: loaded shape
        :rtype: TopoDS_Shape
        """
        self._check_filename_type(filename)
        self._check_extension(filename)
        reader = STEPControl_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
            else:
                raise RuntimeError("Shapes not loaded.")
        else:
            raise RuntimeError("Cannot read the file.")
Beispiel #2
0
def read_step_file(filename, as_compound=True, verbosity=True):
    """ read the STEP file and returns a compound
    filename: the file path
    verbosity: optional, False by default.
    as_compound: True by default. If there are more than one shape at root,
    gather all shapes into one compound. Otherwise returns a list of shapes.
    """
    if not os.path.isfile(filename):
        raise FileNotFoundError("%s not found." % filename)

    step_reader = STEPControl_Reader()
    status = step_reader.ReadFile(filename)

    if status == IFSelect_RetDone:  # check status
        if verbosity:
            failsonly = False
            step_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity)
            step_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity)
        transfer_result = step_reader.TransferRoots()
        if not transfer_result:
            raise AssertionError("Transfer failed.")
        _nbs = step_reader.NbShapes()
        if _nbs == 0:
            raise AssertionError("No shape to transfer.")
        elif _nbs == 1:  # most cases
            return step_reader.Shape(1)
        elif _nbs > 1:
            print("Number of shapes:", _nbs)
            shps = []
            # loop over root shapes
            for k in range(1, _nbs + 1):
                new_shp = step_reader.Shape(k)
                if not new_shp.IsNull():
                    shps.append(new_shp)
            if as_compound:
                builder = BRep_Builder()
                compound = TopoDS_Compound()
                builder.MakeCompound(compound)
                for s in shps:
                    builder.Add(compound, s)
                # shps = compound
                # compound, result = list_of_shapes_to_compound(shps)
                # if not result:
                #    print("Warning: all shapes were not added to the compound")
                return compound
            else:
                print("Warning, returns a list of shapes.")
                return shps
    else:
        raise AssertionError("Error: can't read file.")
    return None
Beispiel #3
0
    def load_shape_from_file(self, filename):
        """
		This method loads a shape from the file `filename`.

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

		:return: shape: loaded shape
		:rtype: TopoDS_Shape
		"""
        self._check_filename_type(filename)
        self._check_extension(filename)
        reader = STEPControl_Reader()
        reader.ReadFile(filename)
        reader.TransferRoots()
        shape = reader.Shape()
        return shape
Beispiel #4
0
from occlib.Topology import Topo
from occlib.EdgeParse import EdgeOnSurface
from occlib.BoundingBox import get_boundingbox
from occlib.Scene import Line3D, Arc3D, BSpline3D

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

if __name__ == "__main__":

    # Read the file and get the shape
    reader = STEPControl_Reader()
    tr = reader.WS().GetObject().TransferReader().GetObject()
    reader.ReadFile(
        os.path.abspath(os.path.join('.', 'models', 'TPI_PH_CNF95XX.STEP')))
    reader.TransferRoots()
    shape = reader.OneShape()

    # Get bounding box
    xmin, ymin, zmin, xmax, ymax, zmax = get_boundingbox(shape)

    # Build section plane
    XYZ = (1, 0, 1)
    lim_coord1 = (xmin, xmax)
    lim_coord2 = (zmin, zmax)

    section_width = ymin + 1e-3
    # A horizontal plane is created from which a face is constructed to intersect with
    # the building. The face is transparently displayed along with the building.
    section_plane = gp_Pln(gp_Pnt(xmax + xmin, section_width, 0.0),
                           gp_Dir(0, 1, 0))