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

(Not an Autodesk Employee)