Following up from the previous post about iProperties, I want to discuss how to work with custom iProperties. In the user-interface you access custom properties using the “Custom” tab of the iProperties dialog, as shown below.
Custom iProperties are accessed the same as other iProperties. It’s just that there is a specific property set that contains them. The property set is called:
“Inventor User Defined Properties”
Getting iProperty Values
The name of the property is the name that was assigned when it was created. You use the name to access a specific property. The sample VBA code below illustrates getting the custom property named “Sample1” and displaying its value.
Public Sub GetCustomiProperty()
' Get the active document.
Dim doc As Document
Set doc = ThisApplication.ActiveDocument
' Get the custom property set.
Dim customPropSet As PropertySet
Set customPropSet = doc.PropertySets.Item( _
"Inventor User Defined Properties")
' Get the property named "Sample1".
Dim customProp As Property
Set customProp = customPropSet.Item("Sample1")
' Display the value of the iProperty.
MsgBox "Sample1 = " & customProp.Value
End Sub
This results in displaying the dialog shown below.
Setting iProperty Values
In the example above the Value property of the Property object is used to get the current value of the iProperty. You can also set the value of the iProperty using the Value property as demonstrated in the code below.
' Set the value of the property.
customProp.Value = "Here's a new value."
When setting the value of an iProperty you are also controlling the property type (Text, Number, Date, or Yes/No). This is implicitly defined by the type of the value you assign to the property. If the value is a String then the property will be a Text property type. If you assign any number type (Integer, Long, Double, etc.) then the result will be a Number property type. If you assign a Date type then the result will be a Date property type. Finally, if you assign a Boolean type then the result will be a Yes/No property type. This also applies when creating iProperties and is demonstrated in the section below.
Creating Custom iProperties
Another difference between custom iProperties and the standard iProperties is that you can create new ones and delete existing ones. For the standard iProperties you’re limited to only getting and setting their values. To create a new iProperty you use the Add method of the PropertySet object. It has three arguments, the value, the name, and the ID. Besides its name, the ID is another way to identify a specific iProperty. This argument is optional and Inventor will automatically assign an ID if you don’t provide one, which is what I always do.
The sample VBA code below demonstrates creating several new iProperties. The type of iProperty created (Text, Number, Date, or Yes/No) is implicitly defined by the type of the value you supply.
Public Sub CreateCustomiProperty()
' Get the active document.
Dim doc As Document
Set doc = ThisApplication.ActiveDocument
' Get the custom property set.
Dim customPropSet As PropertySet
Set customPropSet = doc.PropertySets.Item( _
"Inventor User Defined Properties")
' Create a new text property.
Dim textValue As String
textValue = "Here's some text."
Call customPropSet.Add(textValue, "TextProp")
' Create a new number property.
Dim numberValue As Double
numberValue = 3.14159265358979
Call customPropSet.Add(numberValue, "Pi")
' Create a new date property.
Dim dateValue As Date
dateValue = Now
Call customPropSet.Add(dateValue, "Right Now")
' Create a new boolean property.
yesNoValue As Boolean
yesNoValue = True
Call customPropSet.Add(yesNoValue, "Chocolate is good")
End Sub
Creating or Updating Custom iProperties
If you run the above sample code twice, it will fail the second time. That’s because it’s attempting to create iProperties with names that already exist. Each iProperty must have a unique name with respect to other iProperties in that property set. A common workflow is to update the value of an iProperty, but if it doesn’t exist to create it. The VBA sample code below demonstrates a function that does this. It relies on error handling to determine if an iProperty already exists. If it exists it updates its value. If it doesn’t exist then it creates it. Below this sample I’ve also listed VB.Net, and C# samples since there are differences in syntax and error handling, even though they all make the same Inventor API calls.
' Get the active document.
Dim Doc As Document
Set Doc = ThisApplication.ActiveDocument
' Update or create the custom iProperty.
Call UpdateCustomiProperty(Doc, "Test", "Some Text")
End Sub
Public Sub UpdateCustomiProperty(ByRef Doc As Document, _
ByRef PropertyName As String, _
ByRef PropertyValue As Variant)
' Get the custom property set.
Dim customPropSet As PropertySet
Set customPropSet = Doc.PropertySets.Item( _
"Inventor User Defined Properties")
' Get the existing property, if it exists.
Dim prop As Property
On Error Resume Next
Set prop = customPropSet.Item(PropertyName)
' Check to see if the above call failed. If it failed
' then the property doesn't exist.
If Err.Number <> 0 Then
' Failed to get the existing property so create a new one.
Set prop = customPropSet.Add(PropertyValue, PropertyName)
Else
' Change the value of the existing property.
prop.Value = PropertyValue
End If
End Sub
VB.Net
' Watch out for the wrapped line.
Dim invApp As Inventor.Application
invApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")
' Get the active document.
Dim Doc As Inventor.Document
Doc = invApp.ActiveDocument
' Update or create the custom iProperty.
UpdateCustomiProperty(Doc, "Test", "Some Text")
End Sub
Private Sub UpdateCustomiProperty(ByRef Doc As Inventor.Document, _
ByRef PropertyName As String, _
ByRef PropertyValue As Object)
' Get the custom property set.
Dim customPropSet As Inventor.PropertySet
customPropSet = Doc.PropertySets.Item( _
"Inventor User Defined Properties")
' Get the existing property, if it exists.
Dim prop As Inventor.Property = Nothing
Dim propExists As Boolean = True
Try
prop = customPropSet.Item(PropertyName)
Catch ex As Exception
propExists = False
End Try
' Check to see if the property was successfully obtained.
If Not propExists Then
' Failed to get the existing property so create a new one.
prop = customPropSet.Add(PropertyValue, PropertyName)
Else
' Change the value of the existing property.
prop.Value = PropertyValue
End If
End Sub
C#
{
// Connect to a running instance of Inventor.
// Watch out for the wrapped line.
Inventor.Application invApp = null;
invApp = (Inventor.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application");
// Get the active document.
Inventor.Document doc = invApp.ActiveDocument;
// Update or create the custom iProperty.
UpdateCustomiProperty(doc, "Test2", "Some updated text.");
}
private void UpdateCustomiProperty(Inventor.Document Doc,
String PropertyName,
Object PropertyValue)
{
// Get the custom property set.
// Watch out for the wrapped line.
Inventor.PropertySet customPropSet;
customPropSet = Doc.PropertySets["Inventor User Defined Properties"];
// Get the existing property, if it exists.
Inventor.Property prop = null;
Boolean propExists = true;
try
{
prop = customPropSet[PropertyName];
}
catch (Exception ex)
{
propExists = false;
}
// Check to see if the property was successfully obtained.
if (!propExists)
{
// Failed to get the existing property so create a new one.
prop = customPropSet.Add(PropertyValue, PropertyName, null);
}
else
{
// Change the value of the existing property.
prop.Value = PropertyValue;
}
}
Deleting iProperties
To delete an iProperty you just call the Delete method of the Property object.