Add PropertySet and Property in C#

By Xiaodong Liang

When you try to add add a new propertySet to a PropertSets in C#, you will get an exception

'’System.Reflection.TargetInvocationException.TargetInvocationException’

If the code is written like: oPropertySet = oPropertySets.Add("test","internaltestName"); The solution is to use the internal name with the format of GUID.

The sample below demonstrates how to add PropertySet and Property in C#. Please note: PropId of PropertySet.Add specifies the PropertyID of the Property to add to the set. Valid propids are 2 through 254 and 256 through 0x80000000. Other values are reserved.

private void test()
{
        string progId = "Inventor.Application";
        Type inventorApplicationType = 
                Type.GetTypeFromProgID(progId);
 
        Inventor.Application InvApp = 
      (Inventor.Application)Marshal.GetActiveObject(progId);
 
        PartDocument oDoc = (PartDocument)
                      InvApp.ActiveDocument;
        PropertySets oPropertySets = oDoc.PropertySets;
        Inventor.PropertySet oPropertySet;
 
        try
        {
            //oPropertySet =  
            //oPropertySets.Add("test", "internaltestName");
 
            int index = 2;
            oPropertySet = oPropertySets.Add("test", 
                "{22345678-1234-1234-1234-123456789012}");
            Property oPropertyString = oPropertySet.Add
                 ("MyPropertyStringValue", 
                  "MyPropertyString", index++);
            Property oPropertyInteger = oPropertySet.Add(123, 
                  "MyPropertyInteger", index++);
            Property oPropertyFloat = oPropertySet.Add
                   (123.456, "MyPropertyFloat", index++);
 
            System.Windows.Forms.MessageBox.Show("Add 
                         Properties Succeed!");
 
            //iterate the properties
            string msg = "";
            foreach (Inventor.Property prop in oPropertySet)
            {
 
                msg += prop.PropId.ToString();
                msg += " ";
                msg += prop.Name;
                msg += " ";
                msg += prop.Value.ToString();
                msg += "nn";
            }
 
            System.Windows.Forms.MessageBox.Show(msg);
        }
        catch (System.Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
 
    }
}
Download UserProject1

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading