Inventor Vb.Net - Get or Set Custom Property

Inventor Vb.Net - Get or Set Custom Property

isocam
Collaborator Collaborator
341 Views
4 Replies
Message 1 of 5

Inventor Vb.Net - Get or Set Custom Property

isocam
Collaborator
Collaborator

Can anybody help?

 

I am using Vb.Net to create an "AddIn" for Autodesk Inventor.

 

I want to be able to get a custom property's value and display it in a message box.

 

However,

 

If the custom property does not exist I need to automatically create one and set its value.

 

The two string values I am using are:

 

CustomPropertyName & CustomPropertyValue

 

Does anybody know how to do this?

 

Many thanks in advance!

 

Darren

 

 

 

0 Likes
342 Views
4 Replies
Replies (4)
Message 2 of 5

WCrihfield
Mentor
Mentor

Hi @isocam.  I'm not sure if this will help or not, but I will post a code example here, which was created within an iLogic rule, just as a quick example for you to review.  I spread it out into 3 sections, where the 'Sub Main' portion is likely already being handles by other parts of your overall add-in, so is likely not needed, but the other two should translate OK, after either deleting, or replacing the 'iLogic Logger' methods with an equivalent feedback/log method.

Since there were few initial details involved, I kept the code relatively generic also.  In my actual working/production codes, I like to avoid using Try...Catch statements, if there is another way to avoid a potential error, which is why I am using a simple 'For Each' loop to initially find the custom iProperty.  Then I also check the Document.IsModifiable property before getting to the Try...Catch statement which tries to create the new property, to avoid that potential reason for an exception.  I guess the two methods could have been combined into one method, but I like keeping things relatively modular, so that they can be used in more places, or my more other source routines.

I wish Autodesk offered an alternative property, or maybe even a method, besides the 'IsModifiable' property, which would provide more insight or information about 'why' the Document is 'not' modifiable, so we don't have to rely on custom methods.  When the reason for a Document not being modifiable is because of ModelStates, then there is often a way to work around that situation, because it is often a 'temporary' condition/status.  But if the reason is because the file (on disk) that the Document is associated with is 'Read Only' for some reason, is designated as a Content Center component, the file is in a location designated as a 'Library', and such, then there is nothing we can do to change that status.

Sub Main
	oInvApp = ThisApplication
	Dim oDoc As Inventor.Document = oInvApp.ActiveDocument
	ShowMsgWithCustPropValue(oDoc)
End Sub

Dim oInvApp As Inventor.Application

Sub ShowMsgWithCustPropValue(doc As Inventor.Document)
	Dim sCustPropName As String = "My Custom Property Name"
	Dim oVal As Object = GetCustomPropertyValue(doc, sCustPropName, True, "")
	If oVal Is Nothing OrElse oVal.ToString() = String.Empty Then Exit Sub
	MessageBox.Show("Custom Property named '" & sCustPropName & " in the following document:" _
	& vbCrLf & doc.FullDocumentName & vbCrLf & _
	"...had the following value:" & vbCrLf & _
	oVal.ToString())
End Sub

Function GetCustomPropertyValue(doc As Inventor.Document, _
	propertyName As String, _
	Optional CreateIfNotFound As Boolean = False, _
	Optional NewValue As Object = Nothing) As Object
	
	Dim oVal As Object = Nothing
	Dim bFound As Boolean = False
	Dim oCProps As Inventor.PropertySet = doc.PropertySets.Item(4)
	Dim oCProp As Inventor.Property = Nothing
	For Each oCProp In oCProps
		If oCProp.Name = propertyName Then
			oVal = oCProp.Value
			bFound = True
			Exit For
		End If
	Next
	If bFound Then Return oVal
	If CreateIfNotFound Then
		If Not doc.IsModifiable Then
			Logger.Warn("The 'GetCustomPropertyValue' method could not create a new custom property" _
			& " within the Document named:" & vbCrLf & _
			doc.FullDocumentName & vbCrLf & _
			" because its was not modifiable!")
			Return String.Empty
		End If
		Try
			oCProp = oCProps.Add(NewValue, propertyName)
			oVal = NewValue
		Catch ex As Exception
			Logger.Error(ex.ToString)
		End Try
	End If
	Return oVal
End Function

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 5

jjstr8
Collaborator
Collaborator

@isocam - I would recommend the PropertySet.SetPropertyValues Method   

It creates properties if they don't exist. You can just supply it a single element array. I have an extension method (sorry it's C#) that uses that method. It returns True if nothing goes wrong. Looking at it now, I should move the entire if-else into the try-catch. I'm sure there's certain characters that can't be used in property names and would throw an exception in SetPropertyValues.

internal static bool SetPropertyValue(this PropertySet propertySet, string propertyName, object value, bool createIfMissing)
{
    if (propertySet is null || string.IsNullOrEmpty(propertyName) || value is null)
    {
        return false;
    }
    if (createIfMissing)
    {
        propertySet.SetPropertyValues(new string[] { propertyName }, new object[] { value });
        return true;
    }
    else
    {
        try
        {
            Property property = propertySet[propertyName];
            property.Value = value;
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }
}

 

Message 4 of 5

WCrihfield
Mentor
Mentor

Hi @jjstr8.  Thanks for the tip about utilizing the PropertySet.SetPropertyValues method to more easily 'create' a new custom Property, if it may not already exist.  That is a relatively new method that apparently became available in the 2023 release of Inventor, and some folks may have not seen it, or examples of it being used before.  But that method, nor the extension of that method will 'get' the value of an already existing property, which is one of the things the original poster was looking for.  There is also the similarly located PropertySet.GetPropertyInfo method, which is sort of the opposite of the SetPropertyValues method, which could be used to see if that PropertySet 'contains' a Property with the specified name, without throwing an error, and if it is found, we can use the 'Index' of that property name in the returned names array to get the value associated with it in the values array, if any are returned.  Not sure if it would be he most efficient way to do it, but is certainly a valid method that could be used together with the other one.

 

By the way, below is a vb.net equivalent for that extension, complete with some method documentation, just for reference.  Not super useful in my opinion, compared to the API equivalent, but certainly convenient enough, and has potential for reducing overall code required for some tasks, which is always welcome.

''' <summary>
''' Lets you set the value of a specifically named Property within this PropertySet.  
''' Can optionally create that Property, if it does not already exist.
''' </summary>
''' <param name="PropSet"></param>
''' <param name="PropName">The name of the Property.</param>
''' <param name="PropValue">The value you want to set to the Property.</param>
''' <param name="CreateIfNotFound">Do you want the Property to be created, if it is not found? (True = Yes)</param>
''' <returns>A Boolean indicating if this prodeedure was successful</returns>
<System.Runtime.CompilerServices.Extension>
Function SetPropertyValue(PropSet As Inventor.PropertySet, _
	PropName As String, _
	PropValue As Object, _
	Optional CreateIfNotFound As Boolean = False) As Boolean
	If (PropName Is Nothing) OrElse (PropName = String.Empty) Then Return False
	Try
		If CreateIfNotFound Then
			PropSet.SetPropertyValues({PropName }, {PropValue })
		Else 'do not create the property if it does not already exist
			PropSet.Item(PropName).Value = PropValue
		End If
		Return True
	Catch ex As Exception
		Return False
	End Try
	Return False
End Function

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 5

jjstr8
Collaborator
Collaborator

Right you are! I misread the post. I think I would still use your extension as GetPropertyValue. Just have PropValue as an Optional defaulted to Nothing. Let the try-catch figure out if the property exists. If it doesn't and PropValue is not Nothing, create the property and return PropValue; otherwise, return Nothing.

0 Likes