Hello
Sorry, I'm currently not here every day.
I assume the source file is not always the same? I added a file open dialog to select the part file to use as source. The version with a hard coded source file is included, but commented.
I'm not able to test it, so please be careful and test it with a test assembly or copy first.
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
'Variant A - hard coded SourceFile
'Dim oSourceDoc As PartDocument = ThisApplication.Documents.Open(ThisDoc.PathAndFileName(False).Replace( 100 ,"stuurschets")& ".ipt", False)
'Variant B - Select SourceFile in a dialog
Dim oDlg As Inventor.FileDialog
ThisApplication.CreateFileDialog(oDlg)
oDlg.CancelError=False
oDlg.DialogTitle = "Select parameter source file..."
oDlg.Filter="Inventor Part Files (*.ipt)|*.ipt"
oDlg.InitialDirectory=ThisDoc.Path
oDlg.MultiSelectEnabled = False
oDlg.OptionsEnabled = False
oDlg.ShowOpen
Dim oSourceDoc As PartDocument
If oDlg.FileName <> "" Then
MsgBox("File " & oDlg.FileName & " was selected.")
oSourceDoc = ThisApplication.Documents.Open(oDlg.FileName, False)
Else
Exit Sub
End If
'End of Variant B
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 part parameters of source file to the part.
For Each sourceUserParam As UserParameter In oSourceDoc.ComponentDefinition.Parameters.UserParameters
' Check to see if the parameter already exists.
Dim checkParam As UserParameter = Nothing
Try
checkParam = refDocUserParams.Item(sourceUserParam.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 sourceUserParam.Units = "Text" Then
svalue = Replace(sourceUserParam.Value, Chr(34), "")
Call refDocUserParams.AddByValue(sourceUserParam.Name, svalue, sourceUserParam.Units)
ElseIf sourceUserParam.Units = "Boolean" Then
bvalue = Replace(sourceUserParam.Value, Chr(34), "")
Call refDocUserParams.AddByValue(sourceUserParam.Name, CBool(bvalue), sourceUserParam.Units)
Else
Call refDocUserParams.AddByExpression(sourceUserParam.Name, sourceUserParam.Expression.ToString , sourceUserParam.Units)
End If
Else
' Update the value of the existing parameter.
If sourceUserParam.Units = "Text" Then
svalue = Replace(sourceUserParam.Value, Chr(34), "")
checkParam.Value = svalue
ElseIf sourceUserParam.Units = "Boolean" Then
bvalue = Replace(sourceUserParam.Value, Chr(34), "")
checkParam.Value = bvalue
Else
checkParam.Expression = sourceUserParam.Expression
End If
End If
Next
Catch ex As Exception
Logger.Debug("Skipped file: " & partDoc.FullFileName)
End Try
End If
Next
If oSourceDoc IsNot Nothing Then
If oSourceDoc.Views.Count=0 Then
oSourceDoc.Close(True)
End If
End If
End Sub
R. Krieg
RKW Solutions
www.rkw-solutions.com