Can someone assist with adapting this iLogic code?

Can someone assist with adapting this iLogic code?

chris
Advisor Advisor
443 Views
6 Replies
Message 1 of 7

Can someone assist with adapting this iLogic code?

chris
Advisor
Advisor

The attached code is something I found that copies user-defined parameters from parts in an assembly to the assembly user-defined parameters, but I was hoping someone could help adapt this so that instead of pulling "all" user-defined parameters from "all" the parts in an assembly, it could be changed to ask which part or parts to pull from?

 

If I'm being very specific, it would be nice if this could be told to reference a specific part(s) and if the rule is ran and additional user parameters are added to those parts at a later time that the assembly rule can be ran again and if ran again it would only add user define parameters not already found in the assembly parameters. 

 

Here's the iLogic code for the part parameters to be copied to the assembly, thanks to @JhoelForshav 

 

Dim asmDoc As Inventor.AssemblyDocument = ThisDoc.Document	

For Each refDoc As Inventor.Document In asmDoc.AllReferencedDocuments
Dim refDocUserParams As UserParameters = asmDoc.ComponentDefinition.Parameters.UserParameters
	'Look for part documents.
	If refDoc.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then
    	Dim partDoc As Inventor.PartDocument = refDoc
        'Add the part parameters to the assembly.
        For Each partUserParam As UserParameter In refDoc.ComponentDefinition.Parameters.UserParameters
            'Check to see if the parameter already exists.
            Dim checkParam As UserParameter = Nothing
            Try
            	checkParam = refDocUserParams.Item(partUserParam.Name)
            Catch
            	checkParam = Nothing
            End Try

            If checkParam Is Nothing Then
				'Create the missing parameter.
				Try
            	refDocUserParams.AddByExpression(partUserParam.Name, partUserParam.Expression, partUserParam.Units)
				Catch
				refDocUserParams.AddByValue(partUserParam.Name, partUserParam.Value, partUserParam.Units)
				End Try
			Else
            	'Update the value of the existing parameter.
				Try
            	checkParam.Expression = partUserParam.Expression
				Catch
				checkParam.Value = partUserParam.Value
			End Try
            End If
		Next
	End If
Next

 

 

 

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

WCrihfield
Mentor
Mentor

Hi @chris.  So, how do you want to specify which parts to copy UserParameters from?  Should the code collect a list of the file names of every referenced part, then present those to you in a list, and let you choose one (or more) from that list?  Or would hard-coding component names (or possibly component paths, if they are in sub assemblies), or hard-coding FullFileNames into the code be OK for which parts to target?  Or maybe pre-selecting assembly components before the rule starts, or maybe using the 'Pick' function after rule starts to select some assembly components to target?  FileDialog?

 

This type of process can also cause errors, because if the source UserParameter had an equation in it that was referring to another parameter name, but that other parameter was not already present in the 'destination' document before this one is copied, that will cause an error, because it can not find what that term in the equation is referring to.  That can be very difficult to detect or prevent.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 7

chris
Advisor
Advisor

@WCrihfield 

 

If it could pull a list of included parts from the assembly and allow the ability to choose that would be great... would it only see parts one level down, or can it see into sub-assemblies as well? It's mainly to reference parameters within a skeleton model to copy to the top level to control that skel from the top instead of digging in to make changes

 

like this: start at (1:04:23)

 

https://youtu.be/PUlDOiSZzEE?si=Zx3xiijKqAdDm7mA&t=3863

0 Likes
Message 4 of 7

WCrihfield
Mentor
Mentor
Accepted solution

Hi @chris.  On my way out, but I have this code that you can try out.  It lacks a little polish right now, but I was in a hurry.  I'll check back tomorrow (if I remember).

Sub Main
	Dim oADoc As AssemblyDocument = TryCast(ThisDoc.Document, Inventor.AssemblyDocument)
	If oADoc Is Nothing Then Return
	Dim oRefDocs As DocumentsEnumerator = oADoc.AllReferencedDocuments
	If oRefDocs.Count = 0 Then Return
	Dim oPartFiles As New List(Of String)
	For Each oRefDoc As Inventor.Document In oRefDocs
		If TypeOf oRefDoc Is PartDocument Then
			oPartFiles.Add(oRefDoc.FullDocumentName)
		End If
	Next 'oRefDoc
	Dim sSelectedPart As String = InputListBox("Select Part File.", oPartFiles, "", "Part Files", "List Of Part Files")
	If sSelectedPart = "" Then Return
	oAsmUParams = oADoc.ComponentDefinition.Parameters.UserParameters
'	For Each oRefDoc As Inventor.Document In oRefDocs
'		CopyUParams(oRefDoc)
'	Next 'oRefDoc
	Dim oSelectedPart As Inventor.Document = ThisApplication.Documents.ItemByName(sSelectedPart)
	CopyUParams(oSelectedPart)
	oADoc.Update2(True)
	'oADoc.Save2(True)
End Sub

Dim oAsmUParams As UserParameters

Sub CopyUParams(oSource As Inventor.Document)
	Dim oSourceUParams As UserParameters = Nothing
	Try : oSourceUParams = oSource.ComponentDefinition.Parameters.UserParameters : Catch : End Try
	If oSourceUParams Is Nothing OrElse oSourceUParams.Count = 0 Then Return
	For Each oSourceUParam As UserParameter In oSourceUParams
		Dim oAsmUParam As UserParameter = Nothing
		Try : oAsmUParam = oAsmUParams.Item(oSourceUParam.Name) : Catch : End Try
		If oAsmUParam Is Nothing Then
			Try
				oAsmUParam = oAsmUParams.AddByExpression( _
				oSourceUParam.Name, oSourceUParam.Expression, oSourceUParam.Units)
			Catch : End Try
		Else 'this UserParameter exists in the assembly, so update its Expression
			Try : oAsmUParam.Expression = oSourceUParam.Expression : Catch : End Try
		End If
	Next 'oSourceUParam
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) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 7

chris
Advisor
Advisor
Accepted solution

@WCrihfield This works great, Thank you!  I don't understand why this functionality isn't already built into the Assembly templates, it should be a button in the RMB options

0 Likes
Message 6 of 7

chris
Advisor
Advisor

@WCrihfield  The only other thing I can think of is adding the functionality to be able to select multiple parts at once. As it is now, it gives you a list of available parts to copy from, but you can only pick one at a time.

 

0 Likes
Message 7 of 7

WCrihfield
Mentor
Mentor

Hi @chris.  This thought also crossed my mind yesterday, before posting, but I did not have time to include it at that time.  Below is a link to another forum post that I created just to offer a new way to use the InputListBox, which allows for multiple selections at one time.  I called it a 'MultiSelectListBox'

https://forums.autodesk.com/t5/inventor-programming-ilogic/multiselectlistbox-based-on-ilogic-s-inpu... 

You could integrate that functionality into the code above, to replace the regular InputListBox functionality.  It can either be used as a separate Function as shown there, or it can be used directly within the 'Main' code, as seen in the example at the Link in Message 3 of that topic.  Plus, you can switch the 'MultiSimple' term out for 'MultiExtended' if you want, as pointed out by Maxim-CADman77 in Message 4 of that topic.

If you think you may need help with that, just let me know.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes