Hi Guys....
I found a good code doing what i need but only for:
' If TypeOf oOcc.Definition Is VirtualComponentDefinition Then
' If TypeOf oOcc.Definition Is PartComponentDefinition Then
... how is it possible do to same now for iPart and iAssembly?
This code I am using now:
'define folder name to use sFolder = "Parts" 'define assembly doc Dim oDoc As AssemblyDocument oDoc = ThisApplication.ActiveDocument 'define assembly Component Definition Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = oDoc.ComponentDefinition 'get browser pane Dim oPane As BrowserPane oPane = oDoc.BrowserPanes.ActivePane 'try to get existing folder by name and delete it 'catch error if not found Dim oFolder As BrowserFolder Try oFolder = oPane.TopNode.BrowserFolders(sFolder) oFolder.Delete Catch End Try Dim oCollection As ObjectCollection oCollection = ThisApplication.TransientObjects.CreateObjectCollection 'Iterate through all of the occurrences in the assembly Dim oOcc As ComponentOccurrence For Each oOcc In oAsmCompDef.Occurrences 'look at only virtual components ' If TypeOf oOcc.Definition Is VirtualComponentDefinition Then If TypeOf oOcc.Definition Is PartComponentDefinition Then 'get browser node of occurence Dim oNode As BrowserNode oNode = oPane.GetBrowserNodeFromObject(oOcc) 'add node to collection oCollection.Add(oNode) End If Next
Can someone help me here please . Thanks
UPDATE:
I managed now to define the folder name via input - define the part section via name(wildcard) and in the end select the definition type from a list window. Works great so far but what I can't manage currently is WorkPlanes. Did someone know how to this? This is my code so far:
'define folder name to use oMyParameter = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters Try 'Change value of param Parameter("TypeOfItem") = "" Catch 'Create Param as it doesn't exist oParameter=oMyParameter.AddByValue("TypeOfItem", "", UnitsTypeEnum.kTextUnits) End Try sFolder = InputBox("Input Folder Name", "BrowserNodeFolder Name", "** Folder Name**") itemName = InputBox("Input Wildcard of Item you want to put in the folder", "BrowserNodeItem Name", "** Item Name**") MultiValue.SetList("TypeOfItem", "Part", "Assembly", "VirtualItem") ItemType = InputListBox("Choose Item Type Definition", MultiValue.List("TypeOfItem"),ItemType,Title := "Type of Item Definition", ListName := "Available Standard Item Types") 'define assembly doc Dim oDoc As AssemblyDocument oDoc = ThisApplication.ActiveDocument 'define assembly Component Definition Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = oDoc.ComponentDefinition 'get browser pane Dim oPane As BrowserPane oPane = oDoc.BrowserPanes.ActivePane 'try to get existing folder by name and delete it 'catch error if not found Dim oFolder As BrowserFolder Try oFolder = oPane.TopNode.BrowserFolders(sFolder) oFolder.Delete Catch End Try Dim oCollection As ObjectCollection oCollection = ThisApplication.TransientObjects.CreateObjectCollection 'Iterate through all of the occurrences in the assembly Dim oOcc As ComponentOccurrence Dim part As String = "Part" Dim assembly As String = "Assembly" Dim virtual As String = "VirtualItem" 'part = TypeOf oOcc.Definition Is PartComponentDefinition 'assembly = TypeOf oOcc.Definition Is AssemblyComponentDefinition 'virtual = TypeOf oOcc.Definition Is AssemblyComponentDefinition For Each oOcc In oAsmCompDef.Occurrences 'look at only virtual components ' If TypeOf oOcc.Definition Is VirtualComponentDefinition Then ' If TypeOf oOcc.Definition Is AssemblyComponentDefinition Then If oOcc.Name.Contains(itemName) And ItemType = part Then If TypeOf oOcc.Definition Is PartComponentDefinition Then 'get browser node of occurence Dim oNode As BrowserNode oNode = oPane.GetBrowserNodeFromObject(oOcc) 'add node to collection oCollection.Add(oNode) End If End If If oOcc.Name.Contains(itemName) And ItemType = assembly Then If TypeOf oOcc.Definition Is AssemblyComponentDefinition Then 'get browser node of occurence Dim oNode1 As BrowserNode oNode1 = oPane.GetBrowserNodeFromObject(oOcc) 'add node to collection oCollection.Add(oNode1) End If End If If oOcc.Name.Contains(itemName) And ItemType = virtual Then If TypeOf oOcc.Definition Is AssemblyComponentDefinition Then 'get browser node of occurence Dim oNode2 As BrowserNode oNode2 = oPane.GetBrowserNodeFromObject(oOcc) 'add node to collection oCollection.Add(oNode2) End If End If Next ' recreate folder 'And add collection To folder Try oFolder = oPane.AddBrowserFolder(sFolder,oCollection) Catch MessageBox.Show("Error happens here", "ilogic") End Try
As well if I type same folder name it will not add to already existing folder it will swap - means already selected items getting moved back to browser node and new selected items getting moved to browser node folder.
Thanks
Jens
The code is much better faster and places the folders on the bottom even if you accidently press cancel or type in a number of a part that doesn't exist (it would be nice to have a check but I think that would take too long):
itemName = InputBox("Input Wildcard of Item you want to put in the folder", "BrowserNodeItem Name", "** Item Name**")
sFolder = itemName
If sFolder = "" Then
Exit Sub
Else If sFolder = "** Item Name**"
MsgBox("please enter item number")
Exit Sub
End If
'define assembly doc
Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
'define assembly Component Definition
Dim oAsmCompDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
'get browser pane
'Dim oPane As BrowserPane = oDoc.BrowserPanes.ActivePane
Dim oPane As BrowserPane = oDoc.BrowserPanes("Model")
Dim oTopNode As BrowserNode = oPane.TopNode
'try to get existing folder by name and delete it
'catch error if not found
Dim oFolder As BrowserFolder
Try
oFolder = oPane.TopNode.BrowserFolders.Item(sFolder)
oFolder.Delete
Catch
End Try
Dim oCollection As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
'Iterate through all of the occurrences in the assembly
Dim oOcc As ComponentOccurrence
Dim part As String = "Part"
Dim assembly As String = "Assembly"
For Each oOcc In oAsmCompDef.Occurrences
If oOcc.Name.Contains(itemName) And Inventor.DocumentTypeEnum.kPartDocumentObject Then
If TypeOf oOcc.Definition Is PartComponentDefinition Then
'get browser node of occurence
Dim oNode As BrowserNode = oPane.GetBrowserNodeFromObject(oOcc)
'add node to collection
oCollection.Add(oNode)
End If
End If
If oOcc.Name.Contains(itemName) And Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
If TypeOf oOcc.Definition Is AssemblyComponentDefinition Then
'get browser node of occurence
Dim oNode1 As BrowserNode = oPane.GetBrowserNodeFromObject(oOcc)
'add node to collection
oCollection.Add(oNode1)
End If
End If
Next
' recreate folder
'And add collection To folder
Try
oFolder = oPane.AddBrowserFolder(sFolder, oCollection)
Catch
MessageBox.Show("Error", "ilogic")
End Try
Can't find what you're looking for? Ask the community or share your knowledge.