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!
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
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
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
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.
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,