Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
danny.lewisA9QBW
1223 Views, 8 Replies

Check if parameter is multivalue list and if so, copy the multivalue list to a new parameter

I'm making code to transfer the key parameters from one file to another (via ilogic, not xml) and I can't find how to check if a parameter is a multi-value parameter and to copy it over to a new parameter if it is.

J-Camper
in reply to: danny.lewisA9QBW

Check the ExpressionList Property  of each parameter. 

 

Non-list Parameters have ExpressionList.Count = 0

 

Sample:

For Each p As Parameter In ThisDoc.Document.ComponentDefinition.Parameters
	If p.ExpressionList.Count > 0
		Logger.Trace(p.Name & "  List QTY:  " & p.ExpressionList.Count)
	End If
Next

 

Run rule in Log Level Trace to check sample code results.

 

Hi @danny.lewisA9QBW .

See if this code does what you're trying to do:

Sub Main
	'define source document
	Dim oSourceDoc As Document = ThisApplication.ActiveDocument
	'get its parameters
	Dim oSParams As Inventor.Parameters = GetParams(oSourceDoc)
	Dim oSParam As Inventor.Parameter
	
	'define target document
	Dim oTargetDoc As Document
	Dim oTargetDocName As String = GetTargetDoc
	If oTargetDocName = "" Then Exit Sub
	oTargetDoc = ThisApplication.Documents.Open(GetTargetDoc, True)
	'get its parameters
	Dim oTParams As Inventor.Parameters = GetParams(oTargetDoc)
	Dim oTParam As Inventor.Parameter
	
	For Each oSParam In oSParams
		If Not oSParam.IsKey Then Continue For
		Try
			oTParam = oTParams.Item(oSParam.Name)
		Catch
			If oSParam.ParameterType = ParameterTypeEnum.kModelParameter Then
				oTParam = oTParams.ModelParameters.AddByExpression(oSParam.Expression, oSParam.Units, oSParam.Name)
				If oSParam.ExpressionList IsNot Nothing AndAlso oSParam.ExpressionList.Count > 0 Then
					Dim oExps() As String = oSParam.ExpressionList.GetExpressionList
					oTParam.ExpressionList.SetExpressionList(oExps)
				End If
			ElseIf oSParam.ParameterType = ParameterTypeEnum.kUserParameter Then
				oTParam = oTParams.UserParameters.AddByExpression(oSParam.Expression, oSParam.Units, oSParam.Name)
				If oSParam.ExpressionList IsNot Nothing AndAlso oSParam.ExpressionList.Count > 0 Then
					Dim oExps() As String = oSParam.ExpressionList.GetExpressionList
					oTParam.ExpressionList.SetExpressionList(oExps)
				End If
			End If
		Catch
			MsgBox("Failed to copy source param named '" & oSParam.Name & "' to target document.",,"")
		End Try
	Next
End Sub

Public Function GetParams(oDoc As Document) As Inventor.Parameters
	'check doc type
	Dim oDocType As DocumentTypeEnum = oDoc.DocumentType
	'get its Params
	Dim oPrms As Inventor.Parameters
	If oDocType = DocumentTypeEnum.kPartDocumentObject Or _
		oDocType = DocumentTypeEnum.kAssemblyDocumentObject Then
		oPrms = ThisApplication.ActiveDocument.ComponentDefinition.Parameters
	ElseIf oDocType = DocumentTypeEnum.kDrawingDocumentObject Then
		oPrms = ThisDrawing.Document.Parameters
	End If
	Return oPrms
End Function

Public Function GetTargetDoc() As String
	'use Open File Dialog to get target document
	Dim oFileName As String
	Dim oDoc As Inventor.Document
	Dim oFileDialog As Inventor.FileDialog
	ThisApplication.CreateFileDialog(oFileDialog)
	oFileDialog.DialogTitle = "Select 'Target' file."
	oFileDialog.InitialDirectory = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
	oFileDialog.Filter = "Autodesk Inventor Files (*.iam;*.dwg;*.idw;*.ipt:*.ipn;*.ide) | *.iam;*.dwg;*.idw;*ipt;*.ipn;*.ide | All files (*.*)|*.*"
	oFileDialog.MultiSelectEnabled = False
	oFileDialog.OptionsEnabled = False
	oFileDialog.CancelError = True
	oFileDialog.InsertMode = False
	On Error Resume Next
	oFileDialog.ShowOpen
	If Err.Number <> 0 Then
		MsgBox("Dialog was canceled. Exiting.", vbOKOnly, "CANCELED")
		Return ""
	Else	
		If oFileDialog.FileName <> vbNullString Then
			oFileName = oFileDialog.FileName
			Return oFileName
		Else
			MsgBox("No file was selected. Exiting.", vbOKOnly + vbExclamation, "FILE NOT SELECTED")
			Return ""
		End If
	End If
End Function

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) :thumbs_up:.

If you have time, please... Vote For My IDEAS :light_bulb:or you can Explore My CONTRIBUTIONS

Inventor 2021 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

danny.lewisA9QBW
in reply to: J-Camper

that's most of what I need, but not sure how to have it copy over the multivalue list to the new parameter.

@danny.lewisA9QBW 

 

If you are using iLogic below code will help you.

CopyParam is a parameter in which you will copy the multivalue list.

SourceParam is a parameter in which there is already a multivalue list.

 

MultiValue.List("CopyParam") = MultiValue.List("SourceParam")
RuleParametersOutput
InventorVb.DocumentUpdate

 See if this helps.

If this answer has solved your problem please ACCEPT SOLUTION and hit like if you found it helpful..!


Regards,
Dutt Thakar
LinkedIn

The code I posted already contained code to copy the multi-value list from the source parameter to the target parameter.  That's what the following block of code, that appears twice within it, was for:

If oSParam.ExpressionList IsNot Nothing AndAlso oSParam.ExpressionList.Count > 0 Then
	Dim oExps() As String = oSParam.ExpressionList.GetExpressionList
	oTParam.ExpressionList.SetExpressionList(oExps)
End If

It doesn't just copy a list of 'Values', it copies the list of 'Expressions', in case you want to maintain a list of possible equations or similar non-simple entries.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

While I think your code chunk would work; here's a bigger picture of the relevant code so people have a better idea of what's going on:

 

Dim oDoc As PartDocument = ThisApplication.ActiveDocument
Dim AssyDoc As AssemblyDocument = ThisApplication.Documents.Add(kAssemblyDocumentObject, "{source Assy filepath}", True)

Dim oUParams As UserParameters = oDoc.ComponentDefinition.Parameters.UserParameters
Dim iUParamCount As Integer = oUParams.Count

Dim Cycle As Integer
Dim uParamName As String

For Cycle = 1 To iUParamCount
	uParamName = oUParams(Cycle).Name
	If oUParams(Cycle).IsKey = True Then
		If oUParams(Cycle).Units = "ul" Then
			AssyDoc.ComponentDefinition.Parameters.UserParameters.AddByExpression(uParamName, oUParams(Cycle).Value, "ul").IsKey = True
'	If oUParams(Cycle).ExpressionList.Count > 0 Then
'			AssyDoc.ComponentDefinition.Parameters.UserParameters.AddByExpression(uParamName, MultiValue.List(uParamName), "ul").IsKey = True
'				End If
Else If oUParams(Cycle).Units = "Text" Then
	AssyDoc.ComponentDefinition.Parameters.UserParameters.AddByValue(uParamName, oUParams(Cycle).Value, kTextUnits).IsKey = True
Else
			AssyDoc.ComponentDefinition.Parameters.UserParameters.AddByValue(uParamName, oUParams(Cycle).Value, kMillimeterLengthUnits).IsKey = True
		End If
	End If
Next

 

It's because I'm trying to transfer the parameters between the source (part) file to the destination (part or assy) file that I'm tripping up.

 

OK. Maybe try it this way then.

 

Sub Main
	'source document
	Dim oSDoc As PartDocument = ThisApplication.ActiveDocument
	Dim oSUParams As UserParameters = oSDoc.ComponentDefinition.Parameters.UserParameters
	Dim oSUParam As UserParameter

	'target document
	Dim oTDoc As AssemblyDocument = ThisApplication.Documents.Add(kAssemblyDocumentObject, "{source Assy filepath}", True)
	Dim oTUParams As UserParameters = oTDoc.ComponentDefinition.Parameters.UserParameters
	Dim oTUParam As UserParameter

	For Each oSUParam In oSUParams
		If Not oSUParam.IsKey Then Continue For 'so it will only process Key params
		If oSUParam.Units = "ul" Then
			oTUParam = oTUParams.AddByExpression(oSUParam.Name, oSUParam.Value, "ul")
			oTUParam.IsKey = True
			CopyExpList(oSUParam,oTUParam)
		ElseIf oSUParam.Units = "Text" Then
			oTUParam = oTUParams.AddByValue(oSUParam.Name, oSUParam.Value, kTextUnits)
			oTUParam.IsKey = True
			CopyExpList(oSUParam,oTUParam)
		Else
			oTUParam = oTUParams.AddByValue(oSUParam.Name, oSUParam.Value, kMillimeterLengthUnits)
			oTUParam.IsKey = True
			CopyExpList(oSUParam,oTUParam)
		End If
	Next
End Sub

Public Sub CopyExpList(oSourceP As UserParameter, oTargetP As UserParameter)
	If oSourceP.ExpressionList IsNot Nothing AndAlso oSourceP.ExpressionList.Count > 0 Then
		oTargetP.ExpressionList.SetExpressionList(oSourceP.ExpressionList.GetExpressionList)
	End If
End Sub

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) :thumbs_up:.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

You sir.... are a scholar and a gentleman. Thank you!

 

success.JPG