COPY ALL USER PARAMETERS FROM MAIN ASSEMBLY TO LOWER ASSEMBLIES AND PARTS

COPY ALL USER PARAMETERS FROM MAIN ASSEMBLY TO LOWER ASSEMBLIES AND PARTS

_bmiller_
Contributor Contributor
670 Views
5 Replies
Message 1 of 6

COPY ALL USER PARAMETERS FROM MAIN ASSEMBLY TO LOWER ASSEMBLIES AND PARTS

_bmiller_
Contributor
Contributor

I need to match all user properties from top assembly to lower assemblys and all parts in unit. this must include all types of user properties (num, text, multi text, true/false.) this is so I can drive parts and add properties to large assemblys

 

this is what i have so far

 

' iLogic Rule to Copy/Create User Parameters in Parts and Assemblies


Sub Main()

' Define the assembly document as the active document

Dim asmDoc As AssemblyDocument
asmDoc = ThisApplication.ActiveDocument

' Iterate through all components in the assembly
For Each comp As ComponentOccurrence In asmDoc.ComponentDefinition.Occurrences
	' Get the document for the component
	Dim compDoc As Document
	compDoc = comp.Definition.Document


	Dim mainParams As UserParameters
	mainParams = asmDoc.ComponentDefinition.Parameters.UserParameters

	' Loop through all user parameters in the main assembly
	For Each mainParam As UserParameter In mainParams
		' Check if the parameter already exists in the component
		If Not ParameterExists(compDoc, mainParam.Name) Then
			' Create the parameter in the component if it doesn't exist
			CreateUserParameter(compDoc, mainParam.Name, mainParam.Value, mainParam.Units)
		End If

		' Copy the parameter value from the main assembly to the component
		CopyParameterValue(asmDoc, compDoc, mainParam.Name)
	Next

Next
End Sub

Function ParameterExists(doc As Document, paramName As String) As Boolean
	' Check if a parameter with the given name exists in the document
	Try
		Dim param As Parameter
		param = doc.ComponentDefinition.Parameters.UserParameters.Item(paramName)
		Return True
	Catch ex As Exception
		Return False
	End Try
End Function

Sub CreateUserParameter(doc As Document, paramName As String, paramValue As Object, paramUnits As UnitsTypeEnum)
	' Create a new user parameter in the document
	Try
		Dim param As UserParameter
		param = doc.ComponentDefinition.Parameters.UserParameters.AddByValue(paramName, paramValue, paramUnits)
	Catch ex As Exception
		' Handle any errors that may occur when creating the parameter
	End Try
End Sub

Sub CopyParameterValue(sourceDoc As Document, targetDoc As Document, paramName As String)
	' Copy the parameter value from the source document to the target document
	Try
		Dim sourceParam As Parameter
		sourceParam = sourceDoc.ComponentDefinition.Parameters.UserParameters.Item(paramName)

		Dim targetParam As Parameter
		targetParam = targetDoc.ComponentDefinition.Parameters.UserParameters.Item(paramName)

		If sourceParam.Value.GetType() Is GetType(String) Then
			targetParam.Expression = sourceParam.Expression
		ElseIf sourceParam.Value.GetType() Is GetType(Boolean) Then
			targetParam.Expression = IIf (CBool(sourceParam.Value), "True", "False")
		Else
			targetParam.Value = sourceParam.Value
		End If
	Catch ex As Exception
		' Handle any errors that may occur when copying the parameter value
	End Try
End Sub
and this is the error I'm fighting with
Conversion from string "Text" to type 'Integer' is not valid.

 

0 Likes
Accepted solutions (1)
671 Views
5 Replies
Replies (5)
Message 2 of 6

Preston_Reed
Advocate
Advocate

Try this 

 

' iLogic Rule to Copy/Create User Parameters in Parts and Assemblies

Sub Main()

' Define the assembly document as the active document

Dim asmDoc As AssemblyDocument
asmDoc = ThisApplication.ActiveDocument

' Iterate through all components in the assembly
For Each comp As ComponentOccurrence In asmDoc.ComponentDefinition.Occurrences
	' Get the document for the component
	Dim compDoc As Document
	compDoc = comp.Definition.Document


	Dim mainParams As UserParameters
	mainParams = asmDoc.ComponentDefinition.Parameters.UserParameters

	' Loop through all user parameters in the main assembly
	For Each mainParam As UserParameter In mainParams
		' Check if the parameter already exists in the component
		If Not ParameterExists(compDoc, mainParam.Name) Then
			' Create the parameter in the component if it doesn't exist
'			CreateUserParameter(compDoc, mainParam.Name, mainParam.Value, mainParam.Units)
			
			oMyParameter=ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters
			' Creating top level assembly user parameters
			oParameter = oMyParameter.AddByValue("MainParam", "", UnitsTypeEnum.kTextUnits)

			
			
		End If

		' Copy the parameter value from the main assembly to the component
		CopyParameterValue(asmDoc, compDoc, mainParam.Name)
	Next

Next
End Sub

Function ParameterExists(doc As Document, paramName As String) As Boolean
	' Check if a parameter with the given name exists in the document
	Try
		Dim param As Parameter
		param = doc.ComponentDefinition.Parameters.UserParameters.Item(paramName)
		Return True
	Catch ex As Exception
		Return False
	End Try
End Function

Sub CreateUserParameter(doc As Document, paramName As String, paramValue As Object, paramUnits As UnitsTypeEnum)
	' Create a new user parameter in the document
	Try
		Dim param As UserParameter
		param = doc.ComponentDefinition.Parameters.UserParameters.AddByValue(paramName, paramValue, paramUnits)
	Catch ex As Exception
		' Handle any errors that may occur when creating the parameter
	End Try
End Sub

Sub CopyParameterValue(sourceDoc As Document, targetDoc As Document, paramName As String)
	' Copy the parameter value from the source document to the target document
	Try
		Dim sourceParam As Parameter
		sourceParam = sourceDoc.ComponentDefinition.Parameters.UserParameters.Item(paramName)

		Dim targetParam As Parameter
		targetParam = targetDoc.ComponentDefinition.Parameters.UserParameters.Item(paramName)

		If sourceParam.Value.GetType() Is GetType(String) Then
			targetParam.Expression = sourceParam.Expression
		ElseIf sourceParam.Value.GetType() Is GetType(Boolean) Then
			targetParam.Expression = IIf (CBool(sourceParam.Value), "True", "False")
		Else
			targetParam.Value = sourceParam.Value
		End If
	Catch ex As Exception
		' Handle any errors that may occur when copying the parameter value
	End Try
End Sub
'And this Is the Error I'm fighting with
'Conversion From String "Text" To Type 'Integer' is not valid.
0 Likes
Message 3 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Hi @_bmiller_.  This can be a tricky task to complete entirely as you would like in some situations.  I believe that the error you mentioned at the end of your code window is due to the lines of code within your 'CopyParameterValue' Sub routine which are dealing with Boolean type UserParameter values.  You seem to be attempting to supply String type values to its Expression, instead of supplying true Boolean type values to its Value property directly, which may be what is causing the problem.  It is common for Boolean type expressions to be evaluated as either a 0 (zero) or 1, which can then be directly related to Boolean, where zero = False, and 1 = True...hence the Integer type being mentioned in the conversion issue.  Just a my theory.

 

I have provided an alternate iLogic rule code for the same task within the attached text file that you can review, and or test with.  However, there are still some factors that neither code is dealing with at this time.  What if the source UserParameter is a multi-value text type?  Do you need that whole list of potential values copied down to all the other referenced documents, or just the current value?  And what about if the source UserParameters Expression contained an equation?  Do you need that equation copied to the Expression of all the others?  If so, and the equation contains some local parameter names, then you may need to ensure that the other parameters mentioned in its equation are already present in all the other documents, before attempting to copy that equation down to them.  Then the final, but perhaps most complicated factor is...do you have any custom ModelStates present within any of those referenced documents?  If so, then you may need to incorporate more code to deal with that added factor.

 

Edit:  Oops.  I was a little late with my post.  Didn't see the other one when I posted this.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 6

_bmiller_
Contributor
Contributor

LOL LOCKED UP

0 Likes
Message 5 of 6

_bmiller_
Contributor
Contributor

Don't need whole list as long as it updates it. will try code as soon as i can get running again

0 Likes
Message 6 of 6

_bmiller_
Contributor
Contributor

this one worked, thank you

0 Likes