iLogic, function for virtual components

iLogic, function for virtual components

justinparfitt
Participant Participant
442 Views
4 Replies
Message 1 of 5

iLogic, function for virtual components

justinparfitt
Participant
Participant

Hi,

Could you please help me out and advise, where do I miss additional code with ComponentOccurance  or Add.Virtual item.

When configurator is run and door reach height of 2500mm, I would like to add virtual item "FLUSHBOLT EXTENDED ROD"

 

If leaf_height >= 2500 Then
		
AddAdditionalItems(oAsmDoc,1, "FLUSHBOLT EXTENDED ROD", "FLUSHBOLT EXTENDED ROD")

 It works, but then I have another instance where, if another parameter is true then:

AddAdditionalItems(oAsmDoc,1, "PACKER FOR EXTERNAL DEVICE", "PACKER FOR EXTERNAL DEVICE")

And at this point both virtual items are PACKER FOR EXTERNAL DEVICE:1 and PACKER FOR EXTERNAL DEVICE:2

Please see code below:

 

Function AddAdditionalItems(asmDoc As AssemblyDocument, ItemQuantity As Double, ItemNameOnDrawing As String, ItemNameDescription As String) As String
'
	QTY = ItemQuantity
	
	'Dim asmDoc As AssemblyDocument
    asmDoc = ThisDoc.ModelDocument

    Dim oOccs As ComponentOccurrences
    oOccs = asmDoc.ComponentDefinition.Occurrences

    ' Create collection to hold virtual parts
    Dim oVPs As ObjectCollection
    oVPs = ThisApplication.TransientObjects.CreateObjectCollection

    ' Add to collection of virtual parts

    For Each oOcc As ComponentOccurrence In oOccs

        If TypeOf oOcc.Definition Is VirtualComponentDefinition Then

            oVPs.Add(oOcc)

        End If

    Next

    Dim identity As Matrix = ThisApplication.TransientGeometry.CreateMatrix

    Dim virtOcc As ComponentOccurrence

    If oVPs.Count > 0 Then

        virtOcc = oVPs.Item(1)

    Else

        virtOcc = oOccs.AddVirtual(ItemNameOnDrawing, identity)
	
        ' Set properties of virtual part

        'iProperties.Value(virtOcc.Name, "Project", "Part Number") = "INSERT PART NUMBER HERE"

        iProperties.Value(virtOcc.Name, "Project", "Description") = ItemNameDescription

        'iProperties.Value(virtOcc.Name, "Project", "Stock Number") = "INSERT STOCK NUMBER HERE"

        oVPs.Add(virtOcc)
		

    End If
	
	  ' Compare actual to required

    If QTY > oVPs.Count Then ' Add more virtual parts

        For aaa = 1 To QTY - oVPs.Count

            Call oOccs.AddByComponentDefinition(virtOcc.Definition, identity)

        Next

    Else If QTY < oVPs.Count ' Remove virtual parts

        RemoveQTY = oVPs.Count - QTY

        For bbb = oVPs.Count To QTY + 1 Step -1

            oVPs.Item(bbb).Delete

        Next   

    End If
		
End Function

Thanks...

 

 

0 Likes
Accepted solutions (1)
443 Views
4 Replies
Replies (4)
Message 2 of 5

Zach.Stauffer
Advocate
Advocate

I couldn't quite figure out why your code wasn't working, the logic is a little confusing. I think you are selecting any virtual part instead of selecting only virtual parts that have the same name as the one you are trying to add. However I wasn't sure of that so I tried to simplify your function to do what I think you want. Give it a try and see:

Sub Main
	Dim assemblyDoc = ThisApplication.ActiveDocument
	AddAdditionalItems(assemblyDoc, 4, "FLUSHBOLT", "FLUSHBOLT")
	AddAdditionalItems(assemblyDoc, 2, "PACKER", "PACKER")
End Sub

Function AddAdditionalItems(assemblyDoc As AssemblyDocument, ItemQuantity As Double, ItemNameOnDrawing As String, ItemNameDescription As String) As String

    Dim assemblyOccurrences As ComponentOccurrences = assemblyDoc.ComponentDefinition.Occurrences

	Dim numVirtualParts As Integer = 0
	Dim existingVirtualPart As ComponentOccurrence
	Dim identity As Matrix = ThisApplication.TransientGeometry.CreateMatrix
			
	For Each componentOcc As ComponentOccurrence In assemblyOccurrences
		If componentOcc.Name.Contains(ItemNameOnDrawing) Then
			existingVirtualPart = componentOcc
			numVirtualParts += 1
		End If
	Next
	
	If numVirtualParts = 0 Then
		existingVirtualPart = assemblyOccurrences.AddVirtual(ItemNameOnDrawing, identity)
		numVirtualParts += 1
	End If
		
	If numVirtualParts < ItemQuantity

		Dim nextPartNumber As Integer = numVirtualParts + 1
		Dim finalPartNumber As Integer = ItemQuantity
		
		For i As Integer = nextPartNumber To finalPartNumber Step 1
    		Dim virtOcc As ComponentOccurrence = assemblyOccurrences.AddByComponentDefinition(existingVirtualPart.Definition, identity)
        	iProperties.Value(virtOcc.Name, "Project", "Description") = ItemNameDescription
		Next
	End If
	
	If numVirtualParts > ItemQuantity Then
		While numVirtualParts > ItemQuantity
			For i = 1 To assemblyOccurrences.Count
				If assemblyOccurrences(i).Name.Contains(ItemNameOnDrawing)Then
					assemblyOccurrences(i).Delete()
					numVirtualParts -= 1
					Exit For
				End If
			Next
		End While
	End If
		
End Function

 

Instead of adding multiple virtual parts you could investigate changing the BOM to reflect the correct quantity, that might be a better approach in the long run.

0 Likes
Message 3 of 5

justinparfitt
Participant
Participant

Hi Zach,

 

Thank you very much for re-writing the function. 

The items are coming now as individual components, but the iProperty description do not get filled in.

When item quantity is set to 2 and more the description is bang on, but it doesn't do it with 1 item.

 

Could you please have a look, when you got a minute?

 

Thanks...

0 Likes
Message 4 of 5

Zach.Stauffer
Advocate
Advocate
Accepted solution

Just forgot to add that part where it adds the description for single parts:

 

If numVirtualParts = 0 Then
	existingVirtualPart = assemblyOccurrences.AddVirtual(ItemNameOnDrawing, identity)
	iProperties.Value(existingVirtualPart.Name, "Project", "Description") = ItemNameDescription
	numVirtualParts += 1
End If
0 Likes
Message 5 of 5

justinparfitt
Participant
Participant

Thank you very much!

0 Likes