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: OCC.Core.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()
import numpy as np from OCC.Core.STEPControl import STEPControl_Reader from OCC.Core.TopAbs import TopAbs_FACE from OCC.Core.TopExp import TopExp_Explorer from OCC.Core.StepRepr import StepRepr_RepresentationItem from OCC.Core.BRepAdaptor import BRepAdaptor_Surface from OCC.Core.GeomAbs import GeomAbs_Cylinder from OCC.Core.TopoDS import topods # Read the file and get the shape reader = STEPControl_Reader() tr = reader.WS().TransferReader() reader.ReadFile('geometry_names.stp') reader.TransferRoots() shape = reader.OneShape() # Explore the faces of the shape (these are known to be named) exp = TopExp_Explorer(shape, TopAbs_FACE) while exp.More(): s = exp.Current() exp.Next() # Converting TopoDS_Shape to TopoDS_Face face = topods.Face(s) # Working on the geometry face_adaptor = BRepAdaptor_Surface(face) face_type = face_adaptor.GetType() if face_type == GeomAbs_Cylinder: cylinder = face_adaptor.Cylinder() entity = {}
def read_step_file_withnames( filename, breadface=False, breadedge=False ): #Read a step file with names attached to solid, faces (can be extended to edges) """""[Read a step file with names attached to solid, faces (can be extended to edges)] Arguments: filename {[type]} -- [path to the .stp file] Keyword Arguments: breadface {bool} -- [read the faces' names] (default: {False}) breadedge {bool} -- [read the edges' names] (default: {False}) Returns: (dSolids, dFaces, dEdges) -- [two dicts with name (int) as key and aShape as value] """ "" reader = STEPControl_Reader() #tr = reader.WS().GetObject().TransferReader().GetObject() tr = reader.WS().GetObject().TransferReader() reader.ReadFile(filename) reader.TransferRoots() shape = reader.OneShape() dSolids = dict( ) #solids initial as a dict with name (int) as key and aShape as value dFaces = dict( ) #faces initial as a dict with name (int) as key and aShape as value dEdges = dict( ) #edges initial as a dict with name (int) as key and aShape as value #read the solid names exp = TopExp_Explorer(shape, TopAbs_SOLID) while exp.More(): s = exp.Current() exp.Next() #Converting TopoDS_Shape to TopoDS_Face solid = topods.Solid(s) #Working on the name item = tr.EntityFromShapeResult(s, 1) if item == None: continue #item = Handle_StepRepr_RepresentationItem.DownCast(item).GetObject() item = StepRepr_RepresentationItem.DownCast(item) name = item.Name().ToCString() if name: print('Found entity named: {}: {}.'.format(name, s)) nameid = int(name.split('_')[-1]) dSolids[nameid] = solid # read the face names if breadface: exp = TopExp_Explorer(shape, TopAbs_FACE) while exp.More(): s = exp.Current() exp.Next() #Converting TopoDS_Shape to TopoDS_Face face = topods.Face(s) #Working on the name item = tr.EntityFromShapeResult(s, 1) if item.IsNull(): continue #item = Handle_StepRepr_RepresentationItem.DownCast(item).GetObject() item = StepRepr_RepresentationItem.DownCast(item) name = item.Name().GetObject().ToCString() if name: # print('Found entity named: {}: {}.'.format(name, s)) nameid = int(name.split('_')[-1]) dFaces[nameid] = face # read the edge names if breadedge: exp = TopExp_Explorer(shape, TopAbs_EDGE) while exp.More(): s = exp.Current() exp.Next() #Converting TopoDS_Shape to TopoDS_Face edge = topods.Edge(s) #Working on the name item = tr.EntityFromShapeResult(s, 1) if item.IsNull(): continue #item = Handle_StepRepr_RepresentationItem.DownCast(item).GetObject() item = StepRepr_RepresentationItem.DownCast(item) name = item.Name().GetObject().ToCString() if name: # print('Found entity named: {}: {}.'.format(name, s)) nameid = int(name.split('_')[-1]) dEdges[nameid] = edge ret = (dSolids, dFaces, dEdges) return ret