Hello
You can try to work around the error message when modelstates exist. With model states it is possible to create user parameters with different values for the factory document or one of the states. Adding a user parameter needs to be done in factory document afaik.
Standard parts and other write protected could be skipped silently with a mark in debug console.
Try this:
Public Sub Main()
CopyUserParams()
End Sub
Private Sub CopyUserParams()
If ThisDoc.Document.DocumentType <> Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
MsgBox("The active document must be an assembly.")
Return
End If
break
Dim asmDoc As Inventor.AssemblyDocument = ThisDoc.Document
For Each refDoc As Inventor.Document In asmDoc.AllReferencedDocuments
' Look for part documents.
If refDoc.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then
Dim partDoc As Inventor.PartDocument = refDoc
Dim refDocUserParams As UserParameters
Try
refDocUserParams = partDoc.ComponentDefinition.FactoryDocument.componentdefinition.Parameters.UserParameters
Catch ex As Exception
refDocUserParams = partDoc.ComponentDefinition.Parameters.UserParameters
End Try
Try
' Add the assembly parameters to the part.
For Each asmUserParam As UserParameter In asmDoc.ComponentDefinition.Parameters.UserParameters
' Check to see if the parameter already exists.
Dim checkParam As UserParameter = Nothing
Try
checkParam = refDocUserParams.Item(asmUserParam.Name)
Catch ex As Exception
checkParam = Nothing
End Try
Dim svalue As String
Dim bvalue As Boolean
If checkParam Is Nothing Then
' Create the missing parameter.
If asmUserParam.Units = "Text" Then
svalue = Replace(asmUserParam.Value, Chr(34), "")
Call refDocUserParams.AddByValue(asmUserParam.Name, svalue, asmUserParam.Units)
ElseIf asmUserParam.Units = "Boolean" Then
bvalue = Replace(asmUserParam.Value, Chr(34), "")
Call refDocUserParams.AddByValue(asmUserParam.Name, CBool(bvalue), asmUserParam.Units)
Else
Call refDocUserParams.AddByExpression(asmUserParam.Name, asmUserParam.Expression.ToString , asmUserParam.Units)
End If
Else
' Update the value of the existing parameter.
If asmUserParam.Units = "Text" Then
svalue = Replace(asmUserParam.Value, Chr(34), "")
checkParam.Value = svalue
ElseIf asmUserParam.Units = "Boolean" Then
bvalue = Replace(asmUserParam.Value, Chr(34), "")
checkParam.Value = bvalue
Else
checkParam.Expression = asmUserParam.Expression
End If
End If
Next
Catch ex As Exception
Logger.Debug("Skipped file: " & partDoc.FullFileName)
End Try
End If
Next
End Sub
R. Krieg
RKW Solutions GmbH
www.rkw-solutions.com