Get Properties of Type Class

Get Properties of Type Class

h.schkorwaga
Advocate Advocate
746 Views
6 Replies
Message 1 of 7

Get Properties of Type Class

h.schkorwaga
Advocate
Advocate

Hello together,

 

I'm trying to get properties from an object of type DrawingView for example. (see screenshot attached)

 

This should be a general subroutine for all Inventor types later.

        private void CreateFinalList<T>(T obj)
        {
            if (obj == null) return;

            List<PropertyInfo> properties = typeof(T).GetProperties().ToList();
            foreach (PropertyInfo propertyInfo in properties)
            {
                string name = propertyInfo.Name;
            }
        }

 

Properties list keep count = 0.

 

What am I doing wrong?

 

Thank you in advance.

 

0 Likes
Accepted solutions (1)
747 Views
6 Replies
Replies (6)
Message 2 of 7

JelteDeJong
Mentor
Mentor

If I test your code it works fine. Maybe it's not your method that is not working?

JelteDeJong_0-1676931966751.png

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 3 of 7

h.schkorwaga
Advocate
Advocate

Hello Jelte,

 

thank you for testing. Yes this works for a specified type (DrawingView)

 

The obj parameter comes from a foreach iterating through selected entities which are delivered as objects. So no special type here.

 

        public void OSelectEvents_OnSelect(ObjectsEnumerator JustSelectedEntities, SelectionDeviceEnum SelectionDevice, Point ModelPosition, Point2d ViewPosition, Inventor.View View)
        {
            if (JustSelectedEntities.Count == 0) return;

            foreach (object item in JustSelectedEntities)
            {               
                CreateFinalList(item);
            }

            StandardAddInServer.oInteractEvents.Stop();
        }

 

 

Any ideas how this could work?

 

Thank you!

0 Likes
Message 4 of 7

jjstr8
Collaborator
Collaborator

The issue is that 'item' is a System.__ComObject.  You can use Microsoft.VisualBasic.Information.TypeName(item) to get the type as a string, but you can't use Convert.ChangeType because it needs to implement IConvertible.  I'm not sure what object types are in your selection set, but you may have to have a (potentially) large if/elseif statement to cast 'item' to all types you're interested in then call CreateFinalList with a specific type.

0 Likes
Message 5 of 7

h.schkorwaga
Advocate
Advocate

Yes that's the Problem, I don't want to have such large if or switch statements to handle each possible type (everything that is selectable) with casting. That's why I want to have a general working function that can handle all.

 

This snippet is already implemented in the OnPreselect event to check what type the preselected entity is to ensure highlighting.

Microsoft.VisualBasic.Information.TypeName(item)

 

        public void OSelectEvents_OnPreSelect(ref object PreSelectEntity, out bool DoHighlight, ref ObjectCollection MorePreSelectEntities, SelectionDeviceEnum SelectionDevice, Point ModelPosition, Point2d ViewPosition, Inventor.View View)
        {
            string PreSelTypeName = Microsoft.VisualBasic.Information.TypeName(PreSelectEntity);
            List<string> typenameList = new List<string>();

            foreach (Type type in SelectionType)
                typenameList.Add(type.Name);
                
            if (!(typenameList.Contains(PreSelTypeName)))
            {
                DoHighlight = false;
            }
            else
            {
                DrawingView drawingView = (DrawingView)PreSelectEntity;
                DoHighlight = true;
            }
        }

 

 

But I want to have access to the properties of the recognized type in my "CreateFinalList" function to make a cast, or maybe there is another solution(?).

 

 

0 Likes
Message 6 of 7

jjstr8
Collaborator
Collaborator
Accepted solution

With a little more digging, this is what I could come up with.  Note that 'InvApp' is the Inventor.Application object.  I don't know if you're just after property names, but be aware that property values that are reference types will come back as System.__ComObject.  I didn't do any additional testing to see if you can drill down further.

 

AppDomain appDomain = AppDomain.CurrentDomain;
Assembly[] appDomainAssemblies = appDomain.GetAssemblies();
System.Reflection.Assembly inventor = appDomainAssemblies.FirstOrDefault(a => a.FullName.StartsWith("Autodesk.Inventor.Interop"));
if (inventor != null)
{
    Type[] types = inventor.GetTypes();
    foreach (object item in InvApp.ActiveDocument.SelectSet)
    {
        string typeName = Microsoft.VisualBasic.Information.TypeName(item);
        Type invType = types.FirstOrDefault(t => t.Name == typeName);
        if (invType != null)
        {
            List<PropertyInfo> properties = invType.GetProperties().ToList();
            foreach (PropertyInfo propertyInfo in properties)
            {
                string name = propertyInfo.Name;
                try
                {
                    object propValue = propertyInfo.GetValue(item);
                }
                catch (Exception)
                {
                    // Do something when you can't get the value
                }
            } 
        }
    }
}

 

 

0 Likes
Message 7 of 7

h.schkorwaga
Advocate
Advocate

Hi,

 

thank you very much - this works perfect.

 

Not needed atm but to drill down the mentionend generic System.__ComObject-types a recursive function could be written, I guess.

 

BR

0 Likes