C# .Net Access Properties by Key (to save time)

C# .Net Access Properties by Key (to save time)

Robert.MislerXEDWT
Participant Participant
938 Views
3 Replies
Message 1 of 4

C# .Net Access Properties by Key (to save time)

Robert.MislerXEDWT
Participant
Participant

Dear Inventors,

 

while coding my Inventor-Plugin I hit a problem:

 

In order to identify parts in my company, there is an ID in the "Inventor User Defined Properties".

Now in order to extract the ID, I want to access the PropertySet using my property name. In VBA no problem with the .Item() method. But this doesn't exist in the .net API?

 

The only method I found that works is:

 

foreach (Property _p in (Inventor.PropertySet)_ps)
                {
                    try
                    {
                        if (_p.Name == "PART_ID")
                        {
                            id = _p.Value.ToString();
                            return id;
                        }
                    }
                    catch { }
                }

 

 

But that is way too slow for my application (I have to iterate 1000+ parts).

So then I fount the GetPropertyInfo() function of the PropertySet - but using that gives me a "Object reference not set to an instance..." error?

 

Inventor.PropertySet _psc = ((Inventor.PropertySet)_ps);
                    _psc.GetPropertyInfo(out int[] pids, out string[] Names, out object[] Values);
// fails with Object reference not set to an instance... error
                    if (pids != null)
                    {
                        id = Values[Array.IndexOf(Names, "PART_ID")].ToString();

                    }

 

 

So what is your advice to most efficiently get one Property from a parts PropertySet in C#?

 

Thank you so much,

 

Robert

0 Likes
Accepted solutions (1)
939 Views
3 Replies
Replies (3)
Message 2 of 4

Robert.MislerXEDWT
Participant
Participant
Accepted solution

Ok sorry for my post, maybe it'll be helpful to others at least.

ChatGPT solved it:

What is the most efficient way to get a Property by its Name in a PropertySet in the Inventor C# API?
 

Answer:

The most efficient way to get a Property by its Name in a PropertySet in the Inventor C# API is to use the Item property of the PropertySet, which uses an indexer to access the properties in the set. The indexer takes a string parameter representing the name of the property you want to retrieve. For example, if you have a PropertySet object named "myPropSet" and you want to retrieve a property named "MyProp", you can use the following code:

 

Inventor.Property myProp = myPropSet["MyProp"];
 

This will retrieve the property with the name "MyProp" from the "myPropSet" PropertySet, if it exists. If the property does not exist in the set, an exception will be thrown.

0 Likes
Message 3 of 4

Michael.Navara
Advisor
Advisor

You can access iProperties directly. Here is sample how to access Part Number and your user defined iProperty

 

 

doc.PropertySets["{32853F0F-3444-11D1-9E93-0060B03C1CA6}"]["Part Number"].Value
doc.PropertySets["{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"]["PART_ID"].Value

 

Available PropertySet IDs

 

{F29F85E0-4FF9-1068-AB91-08002B27B3D9}	Inventor Summary Information    
{D5CDD502-2E9C-101B-9397-08002B2CF9AE}	Inventor Document Summary Information   
{32853F0F-3444-11D1-9E93-0060B03C1CA6}	Design Tracking Properties  
{D5CDD505-2E9C-101B-9397-08002B2CF9AE}	Inventor User Defined Properties    

 

 

0 Likes
Message 4 of 4

Robert.MislerXEDWT
Participant
Participant

Thank you so much!!! It works great. Somehow I wasn't able to find that information by myself...

 

 

0 Likes