By Adam Nagy
In case of Geometry objects you do not have the Type/ObjectType properties that you could check the type with. Instead you can use Windows API functionality to find information about that. In this sample we'll use the QueryInterface function (could use CComQIPtr<> as well) and ITypeInfo to get the type name of the object.
// Check type using QueryInterface
// In VB this would be "TypeOf x Is y"
static void CheckType(CComPtr<IDispatch> geom)
{
void * ptr;
if (SUCCEEDED(
geom->QueryInterface(__uuidof(LineSegment), &ptr)))
MessageBox(NULL, _T("LineSegment"), _T("Type Info"), MB_OK);
else if (
SUCCEEDED(geom->QueryInterface(__uuidof(Circle), &ptr)))
MessageBox(NULL, _T("Circle"), _T("Type Info"), MB_OK);
else
MessageBox(NULL, _T("Unknown type"), _T("Type Info"), MB_OK);
}
// Get the type name
// In VB this would be "TypeName"
static void ShowTypeName(CComPtr<IDispatch> geom)
{
ITypeInfo * info;
geom->GetTypeInfo(0, 0, &info);
BSTR name, doc, help;
DWORD context = -1;
info->GetDocumentation(-1, &name, &doc, &context, &help);
MessageBox(NULL, name, _T("Type Name"), MB_OK);
}
static void TypeCheckTest(CComPtr<Application> app)
{
CComQIPtr<PartDocument> doc = app->ActiveDocument;
CComPtr<IDispatch> geom =
doc->ComponentDefinition->SurfaceBodies->
Item[1]->Edges->Item[1]->Geometry;
ShowTypeName(geom);
CheckType(geom);
}