Creating Virtual Component

Creating Virtual Component

kmorehouseUCMBR
Explorer Explorer
2,002 Views
11 Replies
Message 1 of 12

Creating Virtual Component

kmorehouseUCMBR
Explorer
Explorer

The code below is used to add Virtual Components.

 

AddByComponentDefinition as used in the code sample below, generates an error after upgrading to Inventor 2023.

 

What would be the proper way to add multiple VC's in the new version of Inventor?

 

'create first instance of the virtual part
Dim virtOcc As ComponentOccurrence
virtOcc = oOccs.AddVirtual(sVirtPart, identity)

'add next instance starting at instance2 (if applicable)
Dim index As Integer 
index = 2
Do While index <= iQTY
oOccs.AddByComponentDefinition(virtOcc.Definition, identity) 
index += 1
Loop

 

 

0 Likes
Accepted solutions (1)
2,003 Views
11 Replies
Replies (11)
Message 2 of 12

WCrihfield
Mentor
Mentor

Hi @kmorehouseUCMBR.  Are you using iLogic shortcut snippets to create/add your 'virtual' components, or Inventor API code to do it?  If using API code, then this following method seems like the right one to use:

Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oOccs As ComponentOccurrences = oADef.Occurrences
Dim oMatrix As Matrix = ThisApplication.TransientGeometry.CreateMatrix
Dim oVirtOcc As ComponentOccurrence = oOccs.AddVirtual("VirtualOccName", oMatrix)

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 3 of 12

Curtis_Waguespack
Consultant
Consultant

Hi @kmorehouseUCMBR 

 

These related links might be of interests as well:

 

http://inventortrenches.blogspot.com/2013/07/ilogic-add-standard-virtual-parts-from_29.html

 

http://inventortrenches.blogspot.com/2013/07/ilogic-add-standard-virtual-parts-from.html

 

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

EESignature

0 Likes
Message 4 of 12

kmorehouseUCMBR
Explorer
Explorer

Both iLogic and Inventor API with the same result. Both worked fine before the upgrade to 2023.

0 Likes
Message 5 of 12

JelteDeJong
Mentor
Mentor

You are not allowed to use the same occurrence name 2 times. To solve this Inventor adds a ":" and a number to names. Some strange things seem to happen when you add/create an occurrence. But I found a workaround. have a look at this script.

Sub Main()

    Dim iQTY = 4
    Dim sVirtPart = "My VC name"

    Dim doc As AssemblyDocument = ThisDoc.Document

    AddVirtualComponent(doc, sVirtPart)

    For i = 2 To iQTY
        AddVirtualComponent(doc, sVirtPart)
    Next

End Sub

Public Sub AddVirtualComponent(doc As AssemblyDocument, name As String)

    Dim occs = doc.ComponentDefinition.Occurrences
    Dim matrix = ThisApplication.TransientGeometry.CreateMatrix()

    If (name.Contains(":")) Then
        name = name.Split(":")(0)
    End If

    Try
        Dim orgName = name
        If (Not orgName.Contains(":")) Then
            orgName = orgName & ":1"
        End If

        Dim oldOcc = occs.ItemByName(orgName)
        occs.AddByComponentDefinition(oldOcc.Definition, matrix)
    Catch ex As Exception
        occs.AddVirtual(name, matrix)
    End Try
End Sub

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 6 of 12

jjstr8
Collaborator
Collaborator

I feel like everyone's misunderstanding the question.  It's a question of adding additional occurrences of a unique virtual component.  The following VBA code runs in 2019.  Could someone with 2023 run this in a empty assembly and confirm that AddByComponentDefinition fails?

 

Public Sub AddVirtuals()

Dim app As Application

Set app = ThisApplication
If app.ActiveDocumentType <> kNoDocument Then
    If app.ActiveDocumentType = kAssemblyDocumentObject Then
        Dim asmDoc As AssemblyDocument
        Set asmDoc = app.ActiveDocument
        Dim virtOcc As ComponentOccurrence
        Dim pos As Matrix
        Set pos = app.TransientGeometry.CreateMatrix
        Set virtOcc = asmDoc.ComponentDefinition.Occurrences.AddVirtual("VirtualComponent", pos)
        asmDoc.ComponentDefinition.Occurrences.AddByComponentDefinition virtOcc.Definition, pos
    End If
End If

End Sub

 

0 Likes
Message 7 of 12

kmorehouseUCMBR
Explorer
Explorer

I've determined that the original code works as expected when applied to a new Inventor assembly, but not an existing assembly that has been migrated to 2023. I tried removing everything from the existing assembly and setting the Model State and Representation to match the new assembly and it still doesn't work. Continuing to test... 

0 Likes
Message 8 of 12

kmorehouseUCMBR
Explorer
Explorer

After extensive testing, I've determined that the original vb.net code works as intended until any model state has been added. Once another Model State exists the AddByComponentDefinition will no longer functions. This was never an issue in Inventor 2021 prior to Model States.

 

See the pictures.

0 Likes
Message 9 of 12

WCrihfield
Mentor
Mentor
Accepted solution

Very interesting situation there.  Any time any new parameter or iProperty is created within a model document that has any custom model states, it effects all model states equally, because that new parameter or iProperty will exist for all of them, and will all have the same initial value.  I was assuming that adding virtual components would be a similar situation, but maybe not yet.  I wander if you set the member edit scope to all members before doing this, if that would make this process work?

 

Edit:  I just did some testing in iLogic (2022.4) and found same situation, and member edit scope did not seem to effect the outcome.  However, as a work-around, I simply used a 'Copy & Paste' routine to accomplish 'virtually' the same thing.😋

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 10 of 12

kmorehouseUCMBR
Explorer
Explorer

Good old, Copy and Paste. Thank you! that was brilliant.

0 Likes
Message 11 of 12

WCrihfield
Mentor
Mentor

One problem with the copy & paste routine is you are not left with a reference to the virtual component you just created.  So, if you want to use a naming scheme/routine, or adjust properties for each one, you might seem out of luck.  However, I believe you should be able to use something like:

oNewVOcc = oOccs.Item(oOccs.Count)

just after each copy paste, to get a reference to the last component it just created, so you could still do your naming or property changes.  It's sort of a last resort way of doing it, but if it works and saves you time, why not.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 12 of 12

MICHAEL.JONES.AMCE
Advocate
Advocate

@WCrihfield  thank you for posting the copy/paste workflow.

 

Resolved our issue with 2024 and placing multiple copies of the same part.

 

Now if we can just get VS 2019 to allow us to make a new Windows Form Dialog project we'll be golden!

 

M.

 

 

0 Likes