Extract Entity Names from STEP/IGES files

Identifying a specific shape inside an IGES/STEP file is - assuming you're not using a GUI - solely possible if you provide a method to identify the shapes via names. Entries in IGES and STEP files can be assigned names. Take for example an excerpt from a STEP file

#379=CARTESIAN_POINT('centre point',(-19.756658525,0.0,0.0));

The functionality to extract those entity names is undocumented. Open CASCADE XCAF framework only provides methods to extract product names but not entity names!

The original code for IGES entities was provided by Matthias Weber. Thanks a lot!
The only external requirement of the following code snippets are a IGESReader or STEPReader object of name "reader".

     //get shape for label
     TopoDS_Shape getShape(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();
 
          Standard_Integer nb = theModel->NbEntities();
          TopoDS_Shape retShape;
          for(Standard_Integer i=1; i<=nb; i++)
          {
           Handle(IGESData_IGESEntity) ent =
               Handle(IGESData_IGESEntity)::DownCast
               (theModel->Value(i));
           if (ent.IsNull()) continue;
           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;
     }
 
     //dump all labels
     void dumpLabels()
     {
          const Handle(XSControl_WorkSession)& theSession = reader.WS();
          const Handle(Interface_InterfaceModel)& theModel = theSession->Model();
 
          Standard_Integer nb = theModel->NbEntities();
          for(Standard_Integer i=1; i<=nb; i++)
          {
           Handle(IGESData_IGESEntity) ent =
               Handle(IGESData_IGESEntity)::DownCast
               (theModel->Value(i));
           if (ent.IsNull()) continue;
 
           if (ent->HasName())
           {
               std::cout << ent->NameValue()->String().ToCString() << std::endl;
           }
          }
     }

Here's the same code for STEP files

TopoDS_Shape getShape(char* shapeName)
        {
            const TCollection_AsciiString ascShapeName(shapeName);
 
            const Handle(XSControl_WorkSession)& theSession = WS();
            const Handle(Interface_InterfaceModel)& theModel = theSession->Model();
            const Handle(XSControl_TransferReader)& aReader = theSession->TransferReader();
            const Handle(Transfer_TransientProcess)& tp = aReader->TransientProcess();
 
            Standard_Integer nb = theModel->NbEntities();
            TopoDS_Shape retShape;
            for(Standard_Integer i=1; i<=nb; i++)
            {
                Handle(StepRepr_Representation) ent =
                Handle(StepRepr_Representation)::DownCast
                (theModel->Value(i));
 
                if (ent.IsNull()) continue;
                Handle(Transfer_Binder) binder = tp->Find(ent);
                if (binder.IsNull()) continue;
                TopoDS_Shape oneShape = TransferBRep::ShapeResult(binder);
                if (oneShape.IsNull()) continue;
 
                if (ent->Name().IsNull())
                    continue;
 
                if(ent->Name()->String().IsEqual(ascShapeName))
                {
                    retShape = oneShape;
                }
            }
 
            return retShape;
        }
 
        //dump all labels
        void dumpLabels()
        {
            const Handle(XSControl_WorkSession)& theSession = WS();
            const Handle(Interface_InterfaceModel)& theModel = theSession->Model();
 
            Standard_Integer nb = theModel->NbEntities();
            for(Standard_Integer i=1; i<=nb; i++)
            {
                Handle(StepRepr_Representation) ent =
                Handle(StepRepr_Representation)::DownCast
                (theModel->Value(i));
                if (ent.IsNull()) continue;
                if (ent->Name().IsNull()) continue;
                std::cout << ent->Name()->ToCString() << std::endl;
            }
        }