Esempio n. 1
0
    def import_step(self, fn):
        """
        Import a STEP file generated by the OpenVSP.

        :param str fn: The full path to the file.

        :return: None.

        :raise RuntimeError: If the file cannot be read.
        """
        print('Importing OpenVSP STEP file...')

        # Build a compound for geometric sets
        compound = TopoDS_Compound()
        BRep_Builder().MakeCompound(compound)

        # Read file
        step_reader = STEPControl_Reader()
        status = step_reader.ReadFile(fn)
        if status not in [IFSelect_RetVoid, IFSelect_RetDone]:
            raise RuntimeError("Unable to read OpenVSP STEP file.")

        # TODO Convert to desired units
        # Interface_Static.SetCVal_("xstep.cascade.unit", "SOME UNIT HERE")

        # Transfer
        # OpenVSP STEP files result in one root and one shape (a compound)
        step_reader.TransferRoot(1)
        master_shape = step_reader.Shape(1)

        # Iterate over master shape to find compounds for geometric sets. These
        # sets contain the surfaces that make up the component.
        iterator = TopoDS_Iterator(master_shape, True, True)
        more = True
        while iterator.More() and more:
            print('--Processing a component...')
            # The compound
            compound = iterator.Value()
            # Hack to handle single component for now...
            if compound.ShapeType() != TopAbs_COMPOUND:
                compound = master_shape
                more = False

            solid, is_valid, invalid_shapes = _build_solid(
                compound, self._divide)
            if is_valid:
                print('  Successfully built a solid.')
                self._solids.append(solid)
            else:
                print('  Failed to build a valid solid.')
                self._invalid.append(solid)
                self._invalid_shapes += invalid_shapes

            # Next shape
            iterator.Next()

        print('Finished.\n')
Esempio n. 2
0
class StepRead(object):
    """
    Read a STEP file.

    :param str fn: The file to read.
    """

    def __init__(self, fn):
        self._reader = STEPControl_Reader()
        self._tr = self._reader.WS().TransferReader()

        # Read file
        status = self._reader.ReadFile(fn)
        if status != IFSelect_RetDone:
            raise RuntimeError("Error reading STEP 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 STEP reader object.
        :rtype: OCCT.STEPControl.STEPControl_Reader
        """
        return self._reader

    @property
    def shape(self):
        """
        :return: The main shape.
        :rtype: afem.topology.entities.Shape
        """
        return self._shape

    def name_from_shape(self, shape):
        """
        Attempt to extract the name for the STEP entity that corresponds to the
        shape.

        :param afem.topology.entities.Shape shape: The shape.

        :return: The name or None if not found.
        :rtype: str or None
        """
        item = self._tr.EntityFromShapeResult(shape.object, 1)
        if not item:
            return None
        return item.Name().ToCString()
Esempio n. 3
0
    def read_step(fn):
        """
        Read a STEP file and return as a single shape.

        :param str fn: Filename.

        :return: The shape.
        :rtype: OCCT.TopoDS.TopoDS_Shape
        """
        reader = STEPControl_Reader()
        reader.ReadFile(fn)
        reader.TransferRoots()
        return reader.OneShape()
Esempio n. 4
0
    def __init__(self, fn):
        self._reader = STEPControl_Reader()
        self._tr = self._reader.WS().TransferReader()

        # Read file
        status = self._reader.ReadFile(fn)
        if status != IFSelect_RetDone:
            raise RuntimeError("Error reading STEP 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())
Esempio n. 5
0
def load_stp(filename):
    """ Load a stp model """
    reader = STEPControl_Reader()
    status = reader.ReadFile(filename)
    if status != IFSelect_RetDone:
        raise ValueError("Failed to load: {}".format(filename))
    reader.PrintCheckLoad(False, IFSelect_ItemsByEntity)
    reader.PrintCheckTransfer(False, IFSelect_ItemsByEntity)
    ok = reader.TransferRoot()
    return [TopoShape(shape=reader.Shape(1))]
Esempio n. 6
0
def read_step_file(filename):
    """ read the STEP file and returns a compound
    """
    step_reader = STEPControl_Reader()
    status = step_reader.ReadFile(filename)

    if status == IFSelect_RetDone:  # check status
        failsonly = False
        step_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity)
        step_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity)
        step_reader.TransferRoot(1)
        a_shape = step_reader.Shape(1)
    else:
        print("Error: can't read file.")
        sys.exit(0)
    return a_shape
Esempio n. 7
0
# geometry kernel.
#
# Copyright (C) 2016-2018  Laughlin Research, LLC ([email protected])
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
from OCCT.Graphic3d import Graphic3d_NOM_ALUMINIUM
from OCCT.STEPControl import STEPControl_Reader

from OCCT.Visualization.WxViewer import ViewerWx

reader = STEPControl_Reader()
reader.ReadFile('./models/compressor.step')
reader.TransferRoots()
shape = reader.OneShape()

v = ViewerWx()
v.display_shape(shape, rgb=(0.5, 0.5, 0.5), material=Graphic3d_NOM_ALUMINIUM)
v.start()
Esempio n. 8
0
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
from OCCT.STEPControl import STEPControl_Reader
from OCCT.TopAbs import TopAbs_FACE
from OCCT.TopExp import TopExp_Explorer

from OCCT.Visualization.WxViewer import ShapeViewerWx

# Read the file and get the shape
reader = STEPControl_Reader()
tr = reader.WS().TransferReader()
reader.ReadFile('./models/shape_names.step')
reader.TransferRoots()
shape = reader.OneShape()

gui = ShapeViewerWx()

# Explore the faces of the shape (these are known to be named)
exp = TopExp_Explorer(shape, TopAbs_FACE)
while exp.More():
    rgb = None
    s = exp.Current()
    exp.Next()
    item = tr.EntityFromShapeResult(s, 1)
    name = item.Name().ToCString()