def read_step_file(filename, return_as_shapes=False, verbosity=True):
    """ read the STEP 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: optional, False by default.
    """
    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)

        shapes_to_return = []

        for root_num in range(1, step_reader.NbRootsForTransfer() + 1):
            transfer_result = step_reader.TransferRoot(root_num)

            if not transfer_result:
                raise AssertionError("Transfer failed.")

            shape = step_reader.Shape(root_num)

            if shape.IsNull():
                raise AssertionError("Shape is null.")

            shapes_to_return.append(shape)

        if return_as_shapes:
            return shapes_to_return

        builder = BRep_Builder()
        compound_shape = TopoDS_Compound()
        builder.MakeCompound(compound_shape)

        for shape in shapes_to_return:
            builder.Add(compound_shape, shape)

        return compound_shape

    else:
        raise AssertionError("Error: can't read file.")
Ejemplo n.º 2
0
    def read_file(self):
        """
        Read the STEP file and stores the result in a _shapes list
        """
        stepcontrol_reader = STEPControl_Reader()
        status = stepcontrol_reader.ReadFile(self._filename)

        if status == IFSelect_RetDone:
            stepcontrol_reader.PrintCheckLoad(False, IFSelect_ItemsByEntity)
            nb_roots = stepcontrol_reader.NbRootsForTransfer()
            logger.info("%i root(s)" % nb_roots)
            if nb_roots == 0:
                msg = "No root for transfer"
                logger.error(msg)
                raise StepFileReadException(msg)

            stepcontrol_reader.PrintCheckTransfer(False,
                                                  IFSelect_ItemsByEntity)

            self._number_of_shapes = stepcontrol_reader.NbShapes()

            for n in range(1, nb_roots + 1):
                logger.info("Root index %i" % n)
                ok = stepcontrol_reader.TransferRoot(n)
                logger.info("TransferRoots status : %i" % ok)

                if ok:
                    # for i in range(1, self.nb_shapes + 1):
                    a_shape = stepcontrol_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.info("Appending a %s to list of shapes" %
                                    topo_types_dict[a_shape.ShapeType()])
                else:
                    msg = "One shape could not be transferred"
                    logger.warning(msg)
                    warnings.warn(msg)
            return True
        else:
            msg = "Status is not IFSelect_RetDone"
            logger.error(msg)
            raise StepFileReadException(msg)
Ejemplo n.º 3
0
def importStep(fileName):
    """
        Accepts a file name and loads the STEP file into a cadquery shape
        :param fileName: The path and name of the STEP file to be imported
    """
    # Now read and return the shape
    reader = STEPControl_Reader()
    readStatus = reader.ReadFile(fileName)
    if readStatus != OCC.Core.IFSelect.IFSelect_RetDone:
        raise ValueError("STEP File could not be loaded")
    for i in range(reader.NbRootsForTransfer()):
        reader.TransferRoot(i + 1)

    occ_shapes = []
    for i in range(reader.NbShapes()):
        occ_shapes.append(reader.Shape(i + 1))

    # Make sure that we extract all the solids
    solids = []
    for shape in occ_shapes:
        solids.append(Shape.cast(shape))

    return cadquery.Workplane("XY").newObject(solids)