Copy user parameter from one assembly file to another assembly file

Copy user parameter from one assembly file to another assembly file

karram
Advocate Advocate
986 Views
6 Replies
Message 1 of 7

Copy user parameter from one assembly file to another assembly file

karram
Advocate
Advocate

Using ilogic how to copy user parameters from one assembly file to another assembly files.

For Example Assembly file (Test_A.iam) contains 10 user parameter. using ilogic i need to copy automatically all user parameters to another assembly file (Test_B.iam).

0 Likes
Accepted solutions (3)
987 Views
6 Replies
Replies (6)
Message 2 of 7

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

Hi @karram . Try this code. You need to select 2 assemblage either in the model or in the browser. The first click on the assembly from which you will copy parameters, the second click on the assembly in which you will create parameters.

Sub main
	Dim oDoc As Document = ThisApplication.ActiveDocument
	If TypeOf oDoc Is AssemblyDocument Then
		Dim oCM As CommandManager = ThisApplication.CommandManager
		Dim oTm As TransactionManager = ThisApplication.TransactionManager
		Dim oADoc As AssemblyDocument = oDoc
		Dim oCompCopy, oCompPaste As ComponentOccurrence
		Do
			oCompCopy = oCM.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select assembly copy params...")
			If oCompCopy Is Nothing Then Exit Sub
		Loop While oCompCopy.DefinitionDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject
		Do
			oCompPaste = oCM.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select assembly paste params...")
			If oCompPaste Is Nothing Then Exit Sub
		Loop While oCompCopy.DefinitionDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject
		Dim newTM As Transaction = oTm.StartTransaction(oADoc, "CreateUserParameters")
		Dim listParams As New List(Of UserParameter)	
		listParams = GetListParams(oCompCopy.Definition.Parameters)
		Call CreateParamsInAssembly(oCompPaste.Definition.Parameters, listParams)
		newTM.End()
		oADoc.Update()
	Else
		MessageBox.Show("Active document is not AssemblyDocument.", "Error!",MessageBoxButtons.OK,MessageBoxIcon.Error)
	End If
End Sub

Private Function CreateParamsInAssembly(oParams As Parameters, ByVal listParams As List(Of UserParameter))
	Dim bCreatParam As Boolean = True
	For i As Integer = 1 To listParams.Count
		For Each oParam As UserParameter In oParams.UserParameters
			If oParam.Name = listParams.Item(i - 1).Name Then
				bCreatParam = False
				Try
					oParam.Value = listParams.Item(i - 1).Value
				Catch
					oParam.Expression = listParams.Item(i - 1).Expression
				End Try
			End If
		Next
		If bCreatParam Then
			oParams.UserParameters.AddByExpression(listParams.Item(i - 1).Name, listParams.Item(i - 1).Expression, listParams.Item(i - 1).Units)
		End If
	Next i
	
End Function

Private Function GetListParams(ByVal oParams As Parameters) As List(Of UserParameter)
	Dim listParams As New List(Of UserParameter)
	For Each oParam As UserParameter In oParams.UserParameters
		listParams.Add(oParam)
	Next
	Return listParams
End Function

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 3 of 7

karram
Advocate
Advocate

Above code works perfect, Thanks.

Is there any option how to copy user parameters from one Part to another parts and also copy user parameters from one Part  to Assembly like same above code.

 

0 Likes
Message 4 of 7

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

Hi @karram . Try this code. It can copy in any combination: Assembly - Assembly, Assembly - Detail, Detail - Detail, Detail - Assembly.

 

 

Sub main
	Dim oDoc As Document = ThisApplication.ActiveDocument
	If TypeOf oDoc Is AssemblyDocument Then
		Dim oCM As CommandManager = ThisApplication.CommandManager
		Dim oTm As TransactionManager = ThisApplication.TransactionManager
		Dim oADoc As AssemblyDocument = oDoc
		Dim oCompCopy, oCompPaste As ComponentOccurrence
		Do
			oCompCopy = oCM.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select component copy params...")
			If oCompCopy Is Nothing Then Exit Sub
		Loop While Not oCompCopy.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Or _
			oCompCopy.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject
		Do
			oCompPaste = oCM.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select component paste params...")
			If oCompPaste Is Nothing Then Exit Sub
		Loop While Not oCompPaste.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Or _
			oCompPaste.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject
		Dim newTM As Transaction = oTm.StartTransaction(oADoc, "CopingUserParameters")
		Call CreateParamsInAssembly(oCompPaste.Definition.Parameters, oCompCopy.Definition.Parameters)
		newTM.End()
		oADoc.Update()
	Else
		MessageBox.Show("Active document is not AssemblyDocument.", "Error!",MessageBoxButtons.OK,MessageBoxIcon.Error)
	End If
End Sub

Private Function CreateParamsInAssembly(oParams As Parameters, ByVal copyParams As Parameters)
	Dim bCreatParam As Boolean
	For Each copyParam As UserParameter In copyParams.UserParameters
		bCreatParam = True
		For Each oParam As UserParameter In oParams.UserParameters
			If oParam.Name = copyParam.Name Then
				bCreatParam = False
				Try
					oParam.Value = copyParam.Value
				Catch
					oParam.Expression = copyParam.Expression
				End Try
			End If
		Next
		If bCreatParam Then
			oParams.UserParameters.AddByExpression(copyParam.Name, copyParam.Expression, copyParam.Units)
		End If
	Next
End Function

  

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 5 of 7

karram
Advocate
Advocate

Above code woks only Assembly-Assembly combination.

Above code Not works for following combination Parts - Parts, Part -Assembly, Assembly-parts.

0 Likes
Message 6 of 7

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

Try this code.

 

 

Sub main
	Dim oDoc As Document = ThisApplication.ActiveDocument
	If TypeOf oDoc Is AssemblyDocument Then
		Dim oCM As CommandManager = ThisApplication.CommandManager
		Dim oTm As TransactionManager = ThisApplication.TransactionManager
		Dim oADoc As AssemblyDocument = oDoc
		Dim oCompCopy, oCompPaste As ComponentOccurrence
		Do
			oCompCopy = oCM.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select component copy params...")
			If oCompCopy Is Nothing Then Exit Sub
			If oCompCopy.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Or _
			oCompCopy.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then GoTo Step1Rule
		Loop
		Step1Rule :
		Do
			oCompPaste = oCM.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select component paste params...")
			If oCompPaste Is Nothing Then Exit Sub
			If oCompPaste.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Or _
			oCompPaste.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then GoTo Step2Rule
		Loop
		Step2Rule :
		Dim newTM As Transaction = oTm.StartTransaction(oADoc, "CopingUserParameters")
		Call CreateParamsInAssembly(oCompPaste.Definition.Parameters, oCompCopy.Definition.Parameters)
		newTM.End()
		oADoc.Update()
	Else
		MessageBox.Show("Active document is not AssemblyDocument.", "Error!",MessageBoxButtons.OK,MessageBoxIcon.Error)
	End If
End Sub

Private Function CreateParamsInAssembly(oParams As Parameters, ByVal copyParams As Parameters)
	Dim bCreatParam As Boolean
	For Each copyParam As UserParameter In copyParams.UserParameters
		bCreatParam = True
		For Each oParam As UserParameter In oParams.UserParameters
			If oParam.Name = copyParam.Name Then
				bCreatParam = False
				Try
					oParam.Value = copyParam.Value
				Catch
					oParam.Expression = copyParam.Expression
				End Try
			End If
		Next
		If bCreatParam Then
			oParams.UserParameters.AddByExpression(copyParam.Name, copyParam.Expression, copyParam.Units)
		End If
	Next
End Function

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 7 of 7

karram
Advocate
Advocate

Runs perfectly. Thanks.

 

 

0 Likes