Object Type Casting in C#

By Xiaodong Liang

In some applications, it allows users to select objects from a part (edges, surfaces, features) but it does explicitly setting the selection filter. Using if/switch results in a lot of codes. One thought is to use reflection to create a variable of the type selected. However it isn’t possible to use reflection as it always returns System._ComObject because Inventor API is still based on COM. The workaround is: every Inventor object has Type property which returns ObjectTypeEnum enumeration. This can be used to find out specific Inventor object type, for example:

 

private void objType()    {        string progId = "Inventor.Application";        Type inventorApplicationType =            Type.GetTypeFromProgID(progId);             Inventor.Application InvApp =             (Inventor.Application)Marshal.GetActiveObject(progId);             PartDocument oDoc =             (PartDocument)InvApp.ActiveDocument;             // assume an entity is selected        object obj = oDoc.SelectSet[1];        System.Type invokeType = obj.GetType();              object tmp =             invokeType.InvokeMember("Type",                                    BindingFlags.GetProperty,                                     null,                                     obj,                                     null);             Inventor.ObjectTypeEnum objType =             (Inventor.ObjectTypeEnum)tmp;             // check if this is an edge        if (objType == ObjectTypeEnum.kEdgeObject)        {            Edge edge = obj as Edge;        }

}


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading