Unable to cast Metadata to StringMetadata

I have a CADModel loaded in HOOPS, and I’m able to get the list of Metadata from the model. I’m trying to iterate over the list of Metadata objects to get the name and value of each metadata object.
I’m writing this in C#.

List<CADModel> cadModels = Factory.GetCADModels().ToList();
Console.WriteLine("Number of CAD Models: " + cadModels.Count);

CADModel cadModel = cadModels[0];

List<Metadata> metadatas = cadModel.GetAllMetadata().ToList();

foreach (Metadata metadata in metadatas)
{
      string name = metadata.GetName();
      
      // this line throws a System.InvalidCastException: "Unable to cast object type HPS.Metadata to type HPS.StringMetadata.
      StringMetadata str = (StringMetadata)cadModel.GetMetadata(name);
      Console.WriteLine(metadata.GetName() + str.GetValue());
}

I’m using the Hoops BIM Viewer Tutorial as an example. In the BIM viewer example, this line of code works fine.

Any advise would be greatly appreciated.

I was able to solve the issue by creating a new StringMetadata object and using the metadata object in the constructor.

CADModel cadModel = cadModels[0];

List<Metadata> metadatas = cadModel.GetAllMetadata().ToList();

foreach (Metadata metadata in metadatas)
{
     string name = metadata.GetName();
                 
     StringMetadata str = new StringMetadata(metadata);
     Console.WriteLine(metadata.GetName() + " " + str.GetValue());
}
1 Like

Glad to hear you were able to solve it! :slight_smile: Thanks for sharing