Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to create a folder in Assembly tree and save virtual part there

5 REPLIES 5
Reply
Message 1 of 6
achmidt
1028 Views, 5 Replies

How to create a folder in Assembly tree and save virtual part there

Hello guys,

 

I`m working on the code that reads a list of virtual parts from Excel and adds required qty into the assembly. So far it looks good, I  just need to know how to create a folder in a Assembly view tree and save virtual part in that folder instead of placing it in the root.

 

Dim occs As ComponentOccurrences
occs = asmDoc.ComponentDefinition.Occurrences
Dim identity As Matrix identity = ThisApplication.TransientGeometry.CreateMatrix Dim virtOcc As ComponentOccurrence virtOcc = occs.AddVirtual(sVirtPart, identity)

Any help is appreciated.

Thanks!

Inventor Virtual Parts Addin

http://apps.exchange.autodesk.com/INVNTOR/en/Detail/Index?id=appstore.exchange.autodesk.com%3Avirtualpartsadd-in_windows32and64%3Aen
5 REPLIES 5
Message 2 of 6
achmidt
in reply to: achmidt

Well , I partially resolved this.... Script below creates a folder and moves all virtual parts into that folder.... The only problem I have now is how to check if the folder EXISTS already? if it exists how to add virtual parts in the existing folder?

 

    ' adding virtual part in to the model
    Private Sub btn_AddToTheModel_Click(sender As Object, e As EventArgs) Handles btn_AddToTheModel.Click
        If txt_VPAddQty.Text = "" Then Exit Sub
        If lbox_VParts.SelectedItem Is Nothing Then
            Exit Sub
        End If

        If _invApp.Documents.Count = 0 Then
            MessageBox.Show("Need to open an Assembly document", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return
        End If

        If _invApp.ActiveDocument.DocumentType <> _
             DocumentTypeEnum.kAssemblyDocumentObject Then
            MessageBox.Show("Need to have an Assembly document active", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return
        End If

        sVirtPart = PartNumber(lbox_VParts.SelectedIndex)

        Dim vp_qty As Integer
        vp_qty = CInt(txt_VPAddQty.Text) '("Enter the qty", "qty", 1)

        Dim asmDoc As AssemblyDocument
        asmDoc = _invApp.ActiveDocument

        'define assembly Component Definition
        Dim oAsmCompDef As AssemblyComponentDefinition
        oAsmCompDef = _invApp.ActiveDocument.ComponentDefinition

        'Iterate through all of the occurrences in the assembly
        Dim asmOcc As ComponentOccurrence

        For Each asmOcc In oAsmCompDef.Occurrences
            'get name of occurence only (sees only everything left of the colon)
            Dim oOcc As Object
            oOcc = asmOcc.Name.Split(":")(0)
            'look at only virtual components
            If TypeOf asmOcc.Definition Is VirtualComponentDefinition Then
                'compare name selected from list to the
                'existing virtual parts
                If oOcc = sVirtPart Then
                    'delete existing virtual parts if name matches
                    asmOcc.Delete()
                Else
                End If
            Else
            End If
        Next

        Dim occs As ComponentOccurrences
        occs = asmDoc.ComponentDefinition.Occurrences

        Dim identity As Matrix
        identity = _invApp.TransientGeometry.CreateMatrix

        'create first instance of the virtual part
        Dim virtOcc As ComponentOccurrence
        If vp_qty >= 1 Then
            virtOcc = occs.AddVirtual(sVirtPart, identity)
      Else
            Return
        End If

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

        ' creatin FOLDER and moving virtual part in the folder
         oAsmCompDef = asmDoc.ComponentDefinition
        Dim oPane As BrowserPane
        oPane = asmDoc.BrowserPanes.ActivePane
        Dim oOccurrenceNodes As ObjectCollection
        oOccurrenceNodes = _invApp.TransientObjects.CreateObjectCollection
        For Each oOcc In oAsmCompDef.Occurrences
            Dim oNode As BrowserNode
            oNode = oPane.GetBrowserNodeFromObject(oOcc)
            If TypeOf oOcc.Definition Is VirtualComponentDefinition Then
                oOccurrenceNodes.Add(oNode)
            End If
        Next
        Dim oFolder As BrowserFolder
        oFolder = oPane.AddBrowserFolder("VIRTUAL_PARTS", oOccurrenceNodes)
    End Sub

 

Inventor Virtual Parts Addin

http://apps.exchange.autodesk.com/INVNTOR/en/Detail/Index?id=appstore.exchange.autodesk.com%3Avirtualpartsadd-in_windows32and64%3Aen
Message 3 of 6
Arnold82
in reply to: achmidt

Dim oFolders As BrowserFoldersEnumerator = oPane.TopNode.BrowserFolders
Dim oFolder As BrowserFolder
For Each oFolder In oFolders
If oFolder.Name = "VIRTUAL_PARTS" Then
MsgBox("found")
End If
Next
Message 4 of 6
Arnold82
in reply to: Arnold82

Hmmz, I am also not able to add to an existing folder.

 

I found this forumpost: http://forums.autodesk.com/t5/Autodesk-Inventor-Customization/cannot-add-browser-node-to-empty-brows...

 

But when I try Adam's solution I get an error.

Working in Inventor 2012

Message 5 of 6
Vladimir.Ananyev
in reply to: Arnold82

It looks like the limitation in the Inventor UI also. You may add  normal component to the existing folder  but not the virtual component.

Yoy may consider the following work around:   delete existing folder with virtual components and re-create it with new components set.


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

Message 6 of 6
achmidt
in reply to: achmidt

Here is a code taht works for me.

 

 ' creatin FOLDER and moving virtual part in the folder
            Dim oPane As BrowserPane
            oPane = asmDoc.BrowserPanes.ActivePane
            Dim oTopNode As BrowserNode
            oTopNode = oPane.TopNode()
            Dim i As Integer = 0
            Dim oTopNodeOcc As BrowserFoldersEnumerator
            oTopNodeOcc = oTopNode.BrowserFolders
            Dim oOccurrenceNodes As ObjectCollection
            oOccurrenceNodes = _invApp.TransientObjects.CreateObjectCollection
            Dim oNode As BrowserNode

            For Each oOcc In oAsmCompDef.Occurrences
                If Not TypeOf oOcc.Definition Is VirtualComponentDefinition Then
                    'do nothing
                Else
                    oNode = oPane.GetBrowserNodeFromObject(oOcc)
                    oOccurrenceNodes.Add(oNode)
                End If
            Next

            Dim oFolder As BrowserFolder
            For Each oFolder In oTopNode.BrowserFolders
                i = i + 1
            Next
            If i = 0 Then
                oFolder = oPane.AddBrowserFolder("VIRTUAL_PARTS", oOccurrenceNodes)
            Else
                For Each oFolder In oTopNode.BrowserFolders
                    If oFolder.Name = "VIRTUAL_PARTS" Then
                        For Each oOcc In oAsmCompDef.Occurrences
                            If TypeOf oOcc.Definition Is VirtualComponentDefinition Then
                                oNode = oPane.GetBrowserNodeFromObject(oOcc)
                            Else
                            End If
                            Call oTopNodeOcc(1).Add(oNode)
                        Next

                    Else
                        ' do nothing
                    End If
                Next
            End If

 Thanks,

Inventor Virtual Parts Addin

http://apps.exchange.autodesk.com/INVNTOR/en/Detail/Index?id=appstore.exchange.autodesk.com%3Avirtualpartsadd-in_windows32and64%3Aen

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report