Add Multiple Virtual Components

Add Multiple Virtual Components

Anonymous
Not applicable
3,027 Views
10 Replies
Message 1 of 11

Add Multiple Virtual Components

Anonymous
Not applicable

I have a VBA macro that places selected virtual components into an assembly.  I can place virtual components with different part numbers into the assembly with no problem.  When I try to place the second instance of a virtual component with the same part number I get an error.

 

For example: If I place a virtual component with the part number C123A456 into an assembly it shows in the tree as C123A456:1 If I try to place C123A456 again it errors at this line.

 

'Add selected line as a virtual part

Set oOcc = oOccs.AddVirtual(sPartNumber, oMatrix)

 

I can place multiple virtual parts if I append -1, -2, -3 etc to the part number but I would like to let Inventor use the :1, :2, :3 to keep track.

 

How is this handled?

0 Likes
Accepted solutions (1)
3,028 Views
10 Replies
Replies (10)
Message 2 of 11

Anonymous
Not applicable

Just tried it myself and indeed it seems you cannot add 2 virtual components with the same name. I'm not sure, but I guess this just isn't possible.

 

Hopefully someone can prove me wrong.

 

Daniël

0 Likes
Message 3 of 11

mrattray
Advisor
Advisor
Can you create a pattern of them? That's how I would do it with the UI...
Mike (not Matt) Rattray

0 Likes
Message 4 of 11

Anonymous
Not applicable

I am not sure what you mean by creating a pattern since a virtual part only exists in the tree.  Can you provide an example of how you would do this?

0 Likes
Message 5 of 11

mrattray
Advisor
Advisor

I use this all the time for misc. purchased parts that I can't or don't want to draw.

 

Capture.PNG

 

Capture2.JPG

Mike (not Matt) Rattray

0 Likes
Message 6 of 11

Anonymous
Not applicable

You can accomplish what I am trying to do with the following manual workflow.  Have not found way to do it in code yet.

 

1.png

 

 

0 Likes
Message 7 of 11

jdkriek
Advisor
Advisor

@mrattray wrote:
Can you create a pattern of them? That's how I would do it with the UI...

 

Great idea! Decided to write a VBA macro to do just that 😉

 

Sub CreateVirtPattern()
' JDK 2013 - Create virtual component then pattern it
    Dim oAssDoc As AssemblyDocument
    Set oAssDoc = ThisApplication.ActiveDocument
    Dim oAssDef As AssemblyComponentDefinition
    Set oAssDef = oAssDoc.ComponentDefinition
    Dim oMatrix As Matrix
    Set oMatrix = ThisApplication.TransientGeometry.CreateMatrix
    
    ' Create virtual component
    Dim sName As String
    sName = "VirtualComp"
    Set oNewOcc = oAssDef.Occurrences.AddVirtual(sName, oMatrix)

    Dim oOccurrence As ComponentOccurrence
    For Each oOccurrence In oAssDef.Occurrences
    
        ' Look for virtual components
        If TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then
' Look for name match sNameS = Split(oOccurrence.name, ":") If sName & ":" & sNameS(1) = oOccurrence.name Then Dim XAxis As WorkAxis Set XAxis = oAssDef.WorkAxes(1) ' Create object collection Dim objCol As ObjectCollection Set objCol = ThisApplication _ .TransientObjects.CreateObjectCollection ' Add virtual comp to object collection objCol.Add oAssDef.Occurrences.Item(1) ' How many components in pattern Dim iNum As Integer iNum = 3 ' Create pattern Call oAssDef.OccurrencePatterns _ .AddRectangularPattern(objCol, _ XAxis, True, 10, iNum) End If End If Next End Sub

 

Here's the iLogic version as well...

 

' JDK 2013 - Create virtual component then pattern it
Dim oAssDoc As AssemblyDocument = ThisDoc.Document
Dim oAssDef As AssemblyComponentDefinition = oAssDoc.ComponentDefinition
Dim oMatrix As Matrix: oMatrix = ThisApplication.TransientGeometry.CreateMatrix
    
' Create virtual component
sName = "VirtualComp"
oNewOcc = oAssDef.Occurrences.AddVirtual(sName, oMatrix)

Dim oOccurrence As ComponentOccurrence
    For Each oOccurrence In oAssDef.Occurrences
	
        ' Look for virtual components
        If TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then
		
            ' Look for name match
            sNameS = Split(oOccurrence.Name, ":")
            If sName & ":" & sNameS(1) = oOccurrence.Name Then
                Dim XAxis As WorkAxis: XAxis = oAssDef.WorkAxes(1)
				
                ' Create object collection
                Dim objCol As ObjectCollection
                objCol = ThisApplication _
                .TransientObjects.CreateObjectCollection
				
                ' Add virtual comp to object collection
                objCol.Add(oAssDef.Occurrences.Item(1))
                
                ' How many components in pattern
                Dim iNum As Integer
                iNum = 3
                
                ' Create pattern
                Call oAssDef.OccurrencePatterns _
                .AddRectangularPattern(objCol, _
                XAxis, True, 10, iNum)
            End If
        End If
    Next
Jonathan D. Kriek
MFG Solutions Engineer
KETIV Technologies, Inc.


Message 8 of 11

ekinsb
Alumni
Alumni
Accepted solution

Give this a try:

 

Public Sub AddVirtual()
    Dim asmDoc As AssemblyDocument
    Set asmDoc = ThisApplication.ActiveDocument
   
    Dim occs As ComponentOccurrences
    Set occs = asmDoc.ComponentDefinition.Occurrences
   
    Dim identity As Matrix
    Set identity = ThisApplication.TransientGeometry.CreateMatrix
   
    Dim virtOcc As ComponentOccurrence
    Set virtOcc = occs.AddVirtual("TestVirtual", identity)
   
    Call occs.AddByComponentDefinition(virtOcc.Definition, identity)
    Call occs.AddByComponentDefinition(virtOcc.Definition, identity)
    Call occs.AddByComponentDefinition(virtOcc.Definition, identity)
    Call occs.AddByComponentDefinition(virtOcc.Definition, identity)
    Call occs.AddByComponentDefinition(virtOcc.Definition, identity)
End Sub


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 9 of 11

Anonymous
Not applicable

Thanks Brian.  That is what I was looking for.  Didn't think to use the AddByComponentDefinition for virtual part additions after the first.

 

Here is the code I used if it helps anyone.

 

 

'Function to add virtual parts to the assembly
Public Sub AddVirtualPart_Click()

'Variable declarations
Dim oAssemblyDoc As AssemblyDocument
Dim oAssemblyCompDef As AssemblyComponentDefinition
Dim oVirtualCompDef As VirtualComponentDefinition
Dim oTransientGeometry As TransientGeometry
Dim oMatrix As Matrix
Dim oOccs As ComponentOccurrences
Dim oOcc As ComponentOccurrence
Dim sLine As String
Dim sPartNumber As String
Dim sDescription As String
Dim iListIndex As Integer
Dim i As Integer


'Set the list index
iListIndex = Me.ItemToAddListBox.ListIndex

'Check if something was selected
If iListIndex = -1 Then
    MsgBox "Please select a part to add!"
Else
    'Set the assembly document
    Set oAssemblyDoc = ThisApplication.ActiveDocument

    'Set the assembly component definition
    Set oAssemblyCompDef = oAssemblyDoc.ComponentDefinition

    'Set the transient geometry
    Set oTransientGeometry = ThisApplication.TransientGeometry

    'Set the matrix
    Set oMatrix = oTransientGeometry.CreateMatrix

    'Set selected line
    sLine = Me.ItemToAddListBox.Text

    'Set the part number
    sPartNumber = Left(sLine, 20)
    
    'Remove spaces from the part number
    sPartNumber = RemoveSpaces(sPartNumber)
    
    'Set the occurrences collection
    Set oOccs = oAssemblyCompDef.Occurrences
    
    'Initialize
    i = 1

    'Check for other occurrences
    For Each oOcc In oOccs
        If Not InStr(oOcc.Name, sPartNumber) = 0 Then
            i = i + 1
        Else
        End If
    Next

    'Check if this is the first occurrence
    If i = 1 Then
        'Add the virtual part
        Set oOcc = oOccs.AddVirtual(sPartNumber, oMatrix)
        
        'Set the virtual part component definition
        Set oVirtualCompDef = oOcc.Definition
    Else
        'Set the virtual part component definition from one of the occurrences
        For Each oOcc In oOccs
            If Not InStr(oOcc.Name, sPartNumber) = 0 Then
                Set oVirtualCompDef = oOcc.Definition
                Exit For
            Else
            End If
        Next
        
        'Add the virtual part
        Call oOccs.AddByComponentDefinition(oVirtualCompDef, oMatrix)
    End If
    
    'Set the description
    sDescription = Mid(sLine, 21, Len(sLine) - 21)
    
    'Remove spaces
    sDescription = RemoveSpaces(sDescription)
    
    'Set the description
    oVirtualCompDef.PropertySets.Item("Design Tracking Properties").Item("Description").Value = sDescription
    
    'Set the part number
    oVirtualCompDef.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value = sPartNumber

End If


'Unload objects
Set oAssemblyDoc = Nothing
Set oAssemblyCompDef = Nothing
Set oTransientGeometry = Nothing
Set oMatrix = Nothing
Set oOcc = Nothing

End Sub

 

0 Likes
Message 10 of 11

jdkriek
Advisor
Advisor

Thanks Brian, didn't think about that either.

Jonathan D. Kriek
MFG Solutions Engineer
KETIV Technologies, Inc.


0 Likes
Message 11 of 11

Anonymous
Not applicable

to add inside the model within the browser tree you add the name:1, name:2 and so on,  I would think you would do the same if adding via VBA.

 

 

0 Likes