Best way to add multiple iParts with different variations

Best way to add multiple iParts with different variations

DavidTunnard
Collaborator Collaborator
348 Views
3 Replies
Message 1 of 4

Best way to add multiple iParts with different variations

DavidTunnard
Collaborator
Collaborator

I need to be able to add multiple versions of the same iPart in the same assembly file.

 

I have many holes which can be either true/false. they have around 10 different variations for the type of fitting that can be fit into them.

 

ie.

Hole 1 = True, and is 1"

meaning I need the 1" row of the iPart

 

Hole 2 = True, and is 1.5"

meaning I need the 1.5" row of the iPart

 

etc..

 

This is what I have so far, but just for inserting one iPart and changing its row. I don't know how best to carry on with this for multiple instances of the same iPart.

Sub Main()

'creating a list of options for iPart 1
Dim iPartList As New ArrayList()
iPartList.Add("Option A")
iPartList.Add("Option B")

'defining which multi value parameter(s) should use the above list
MultiValue.List("iPART_SELECT") = iPartList


If iPART = True Then
Call Add_iPart
Call Row_Select
End If
Call Delete_iPart

End Sub

Sub Add_iPart ()

	Dim componentA = Components.AddiPart("First iPart:1", "iPart Test.ipt", row := 1, position := Nothing, grounded := False, visible := True, appearance := Nothing)

End Sub

Sub Row_Select()
	
	If iPART_SELECT = "Option A" Then
		iPart.ChangeRow("First iPart:1", "Row A")
		
	ElseIf iPART_SELECT = "Option B" Then
		iPart.ChangeRow("First iPart:1", "Row B")
		
	End If
End Sub

Sub Delete_iPart()
	If iPART = False Then
		Components.Delete("First iPart:1")
	End If
End Sub

 

Thanks

0 Likes
349 Views
3 Replies
Replies (3)
Message 2 of 4

A.Acheson
Mentor
Mentor

What you can do is to take the occurrence name directly from the occurrence added this way it will self index (take internal count of the occurrences). For the deletion of the occurrences I have used the Leaf occurrences method of the API. There might be another ilogic API equivalent. 

 

Dim asmDoc As AssemblyDocument = ThisDoc.Document
Dim asmDef As AssemblyComponentDefinition = asmDoc.ComponentDefinition

'creating a list of options for iPart 1
Dim iPartList As New List (Of String)
iPartList.Add("Option A")
iPartList.Add("Option B")

'defining which multi value parameter(s) should use the above list
MultiValue.List("iPART_SELECT") = iPartList

Dim partName as String = "iPart Test.ipt"
If iPART = True Then
	For i = 1 to 5
		Dim componentA = Components.AddiPart("",partName, row := 1, position := Nothing, grounded := False, visible := True, appearance := Nothing)
		
		If iPART_SELECT = "Option B" Then
			iPart.ChangeRow(componentA.Name, "Row A")
		ElseIf iPART_SELECT = "Option B" Then
			iPart.ChangeRow(componentA.Name, "Row B")
		End If
	Next

ElseIf iPART = False Then

	' Get all of the leaf occurrences of the assembly.
    Dim leafOccs As ComponentOccurrencesEnumerator = oAsmDef.Occurrences.AllLeafOccurrences

    ' Iterate through the occurrences and print the name. 
    For Each occ As ComponentOccurrence In leafOccs
		 If occ.Definition.Document.FullFileName.Contains(partName) Then
			occ.Delete
		 End If
    Next
	
End If

 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 4

DavidTunnard
Collaborator
Collaborator

Hi Alan

 

Thanks very much for the suggestion. However, I have never used leaf occurrences before. would you be able to explain a bit more how the following lines work as I don't really know what is happening/how to adapt them for my needs?

Dim partName as String = "iPart Test.ipt"
If iPART = True Then
	For i = 1 to 5
		Dim componentA = Components.AddiPart("",partName, row := 1, position := Nothing, grounded := False, visible := True, appearance := Nothing)
		
		If iPART_SELECT = "Option B" Then
			iPart.ChangeRow(componentA.Name, "Row A")
		ElseIf iPART_SELECT = "Option B" Then
			iPart.ChangeRow(componentA.Name, "Row B")
		End If
	Next

ElseIf iPART = False Then

	' Get all of the leaf occurrences of the assembly.
    Dim leafOccs As ComponentOccurrencesEnumerator = oAsmDef.Occurrences.AllLeafOccurrences

    ' Iterate through the occurrences and print the name. 
    For Each occ As ComponentOccurrence In leafOccs
		 If occ.Definition.Document.FullFileName.Contains(partName) Then
			occ.Delete
		 End If

 

David

0 Likes
Message 4 of 4

A.Acheson
Mentor
Mentor

Hi @DavidTunnard 

The code posted for the most part is how you had laid it out. If you place "" in as the occurrence name in add ipart the occurrence name becomes system generated and will selve index example "iPart Test:1", when the next is added then "iPart Test:2" etc. 

componentA.Name will allow you to retrieve the occurrence name and use it for the ipart change row function. 

 

If you want to create your own occurrence name you will need to put a string together that is unique say "Part" & i etc. This will work as long as you delete these occurrences before trying to add them back in so as not to trip over the index number allready assigned. 

 

The leaf occurrences is needed to delete all the iparts you added (if required). It will loop through all occurrence parts only in the assembly. The if statement set up will compare the string filename of the occurrence against the part filename you want and will delete all occurrences created from the part file. If you want to just target specific occurrence then you can check the occurrence name and perform an operation on them. 

 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes