Hi @mohandis2. As you may, or may not know, when multiple ModelStates are involved, the code gets much more complicated. Below is another version of an iLogic rule for this task, but it also includes some extra code for when the view is of a model that may (or may not) have 2 or more ModelStates in it, and you just want to change the value of a custom iProperty that already exists. I like to use the ModelStateTable for changing values of things that already exist, but it will not allow us to add new columns to it by code for when we may want to create a new UserParameter or custom iProperty. There are various ways to do this, but they are all a lot more complex than they used to be before ModelStates were involved.
Dim oView As DrawingView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Select a drawing view of part.")
If oView Is Nothing Then Return
Dim oSheet As Inventor.Sheet = oView.Parent
Dim oDDoc As DrawingDocument = oSheet.Parent
If oDDoc.RequiresUpdate Then oDDoc.Update2(True)
oSheet.Update
Dim oRDD As DocumentDescriptor = oView.ReferencedDocumentDescriptor
If oRDD.ReferenceMissing OrElse oRDD.ReferenceDisabled Then
MsgBox("This view's reference is either missing or disabled. Exiting routine.", vbCritical, "iLogic")
Return
End If
'If oRDD.ReferencedModelState = ModelStateTypeEnum.kCustomModelStateType Then
Dim oViewDoc As Inventor.Document = oRDD.ReferencedDocument
If oViewDoc.RequiresUpdate Then oViewDoc.Update2(True)
Dim sViewMS As String = oView.ActiveModelState 'can be Null
If String.IsNullOrEmpty(sViewMS) Then
Logger.Info("This view's active ModelState specification is either null or empty.")
End If
Dim oMSs As ModelStates = Nothing
Try : oMSs = oViewDoc.ComponentDefinition.ModelStates : Catch : End Try
If oMSs Is Nothing OrElse oMSs.Count < 2 Then
'there are no ModelSates, or only the 1 original involved, so do things normally
Dim oViewDocCProps As Inventor.PropertySet = oViewDoc.PropertySets.Item(4)
Dim oViewDocCProp As Inventor.Property = Nothing
Try
oViewDocCProp = oViewDocCProps.Item("CustomPropertyName")
oViewDocCProp.Value = "CustomPropertyValue"
Catch
'oViewDocCProp = oViewDocCProps.Add("Value Of Custom iProperty", "Name Of Custom iProperty")
Logger.Error("Error getting custom iProperty, or setting its value.")
End Try
Else 'there are 2 or more ModelSates involved, so do things the hard way
'make sure we have a reference to the 'factory' document (the only version that is not ReadOnly)
Try : If oViewDoc.ComponentDefinition.IsModelStateMember Then _
oViewDoc = oViewDoc.ComponentDefinition.FactoryDocument
Catch : End Try
Dim oMSTable As ModelStateTable = oMSs.ModelStateTable
Try
'get the row for the view's active ModelState
Dim oMSRow As ModelStateTableRow = oMSTable.TableRows.Item(sViewMS)
'get the column for the custom iProperty (we must include which PropertySet it is in)
Dim oMSCol As ModelStateTableColumn = oMSTable.TableColumns.Item("CustomPropertyName [Custom]")
'set the value of the Cell at the intersection of that row and that column
oMSRow.Item(oMSCol).Value = "CustomPropertyValue"
Catch
Logger.Error("Error editing ModelStateTable to change custom iProperty value.")
End Try
End If
If oViewDoc.RequiresUpdate Then oViewDoc.Update2(True)
If oDDoc.RequiresUpdate Then oDDoc.Update2(True)
oSheet.Update
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)