Get shape from name of label from STEP/IGES file

How do I get the shape given a name?

I've tried the following with no success for IGES and STEP files:

char shapeName = "M-11";
IGESCAFControl_Reader::GiveList(shapeName);

It is returning a null list and the following message:
Neither Entity Number/Label nor Selection :M-11

The name is shown when viewed from ABViewer. See attached image.

Attachments: 
Ben Cain's picture

I tried the method for getting shapes from the following post:
http://www.opencascade.org/org/forum/thread_13466/?forum=3
But, I keep getting isNull() for the transfer binder.
Handle(Transfer_Binder) binder = tp->Find(ent); // binder is null

Out of couriosty, I tried the following and noticed that the file has no shapes (only entities). The entity names match the names as shown in ABViewer as seen in the attached image.

numEntities: 8988
numShapes: 0
M-11 M-12 M-13 M-14 M-21 M-22 M-23 M-24 M-31 M-32 M-33 M-34 M-41 M-42 M-43 M-44

Here's the code I used ...

TopoDS_Shape Pmcad::getShapeIGES(IGESCAFControl_Reader reader, char * shapeName)
{
const TCollection_AsciiString ascShapeName(shapeName);

const Handle_XSControl_WorkSession & theSession = reader.WS();
const Handle_Interface_InterfaceModel & theModel = theSession->Model();
const Handle_XSControl_TransferReader & aReader = theSession->TransferReader();
const Handle_Transfer_TransientProcess & tp = aReader->TransientProcess();

TopoDS_Shape retShape;

Standard_Integer numEntities = theModel->NbEntities();
Standard_Integer numShapes = reader.NbShapes();

cout << "numEntities: " << numEntities << endl;
cout << "numShapes: " << numShapes << endl;

for(Standard_Integer i=1; i<=numEntities; i++)
{
Handle_IGESData_IGESEntity ent = Handle_IGESData_IGESEntity::DownCast(theModel->Value(i));
if(ent.IsNull())
{
continue;
}
if(ent->HasName() && !ent->NameValue()->String().IsEmpty())
{
cout << ent->NameValue()->String() << " ";
}
Handle_Transfer_Binder binder = tp->Find(ent);
if(binder.IsNull())
{
continue;
}
TopoDS_Shape oneShape = TransferBRep::ShapeResult(binder);
if(oneShape.IsNull())
{
continue;
}
if(ent->HasName() && ent->NameValue()->String().IsEqual(ascShapeName))
{
retShape = oneShape;
}
}

return retShape;
}

Attachments: 
Ben Cain's picture

I've tried converting the entity to a shape with
TopoDS_Shape IGESToBRep_BRepEntity::TransferBRepEntity(...)
but it's returning NULL as well.

I can't find how to retrieve a named part (from IGES or STEP file) anywhere in the documentation.

Ben Cain's picture

The following is working for IGES. I was able to get the shape from the label name.

////////////////////////////////////////////////////////////////////////////////
TopoDS_Shape Pmcad::getShapeFromIges(IGESCAFControl_Reader reader, char * shapeName)
{
TopoDS_Shape retShape;

Handle_TColStd_HSequenceOfTransient shapeList = reader.GiveList("xst-model-roots");
int numShapesTrans = reader.TransferList(shapeList);
retShape = reader.OneShape();

for(int i=1; i<=numShapesTrans; ++i)
{
Handle_Standard_Transient transient = shapeList->Value(i);
Handle_XSControl_WorkSession & theSession = reader.WS();
Handle_XSControl_TransferReader & aReader = theSession->TransferReader();
Handle_Transfer_TransientProcess & tp = aReader->TransientProcess();
TopoDS_Shape shape = TransferBRep::ShapeResult(tp, transient);
if(!shape.IsNull())
{
Handle_Standard_Transient anEntity = aReader->EntityFromShapeResult(shape, 1);
if(!anEntity.IsNull())
{
Handle_IGESData_IGESEntity entity = Handle_IGESData_IGESEntity::DownCast(anEntity);
if(!entity.IsNull())
{
if(entity->NameValue()->String() == shapeName)
{
retShape = shape;
break;
}
}
}
}
}

return retShape;
}

Ben Cain's picture

The following is working for STEP. I was able to get the shape from the label name.

////////////////////////////////////////////////////////////////////////////////
TopoDS_Shape Pmcad::getShapeFromStep(STEPControl_Reader reader, char * shapeName)
{
TopoDS_Shape retShape;

//Handle_TColStd_HSequenceOfTransient shapeList = reader.GiveList("xst-model-roots");
Handle_TColStd_HSequenceOfTransient shapeList = reader.GiveList("xst-model-all");
int numShapesTrans = reader.TransferList(shapeList);
retShape = reader.OneShape();

for(int i=1; i<=numShapesTrans; ++i)
{
Handle_Standard_Transient transient = shapeList->Value(i);
Handle_XSControl_WorkSession & theSession = reader.WS();
Handle_XSControl_TransferReader & aReader = theSession->TransferReader();
Handle_Transfer_TransientProcess & tp = aReader->TransientProcess();
TopoDS_Shape shape = TransferBRep::ShapeResult(tp, transient);
if(!shape.IsNull())
{
Handle_Standard_Transient anEntity = aReader->EntityFromShapeResult(shape, 1);
if(!anEntity.IsNull())
{
Handle_StepRepr_RepresentationItem entity = Handle_StepRepr_RepresentationItem::DownCast(anEntity);
if(!entity.IsNull())
{
if(entity->Name()->String() == shapeName)
{
retShape = shape;
break;
}
}
}
}
}

return retShape;
}

shantanu kumar das's picture

Dear sir, can we get name of shape from the label in opencascade?