Message 1 of 14
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi.
How can I create new folders in Inventor browser tree, using ilogic (ONLY IF NOT EXIST)?
Thanks in advance!
Solved! Go to Solution.
Hi.
How can I create new folders in Inventor browser tree, using ilogic (ONLY IF NOT EXIST)?
Thanks in advance!
Solved! Go to Solution.
EDIT: I just saw the "only if not exists"...
EDIT2: Fixed the ComponentDefinition
Here you go:
Dim oFolderName As String = "NewFolder"
Dim oDoc As Document = ThisApplication.ActiveDocument Dim oPane As BrowserPane = oDoc.BrowserPanes.ActivePane Dim oFolder As BrowserFolder = Nothing Try oFolder = oPane.BrowserFolders(oFolderName) Catch oFolder = oPane.AddBrowserFolder(oFolderName, Nothing) End Try
If in assembly, you can pass it occurrences you want to be added to the folder:
Dim oFolderName As String = "NewFolder"
Dim oDoc As Document = ThisApplication.ActiveDocument Dim oNodes As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection Dim oPane As BrowserPane = oDoc.BrowserPanes.ActivePane For Each oOcc As ComponentOccurrence In oDoc.ComponentDefinition.Occurrences Dim oNode As BrowserNode = oPane.GetBrowserNodeFromObject(oOcc) oNodes.Add(oNode) Next
Dim oFolder As BrowserFolder = Nothing Try oFolder = oPane.BrowserFolders(oFolderName) Catch oFolder = oPane.AddBrowserFolder(oFolderName, oNodes) End Try
The code on first updated code part creates new folder even if exist. The second generates error as in previous post 😞
Alright, so there's a little bit more into that than I thought. Here's the correction:
Dim oFolderName As String = "NewFolder"
Dim oDoc As Document = ThisApplication.ActiveDocument Dim oPane As BrowserPane = oDoc.BrowserPanes.ActivePane Dim oFolder As BrowserFolder = Nothing Try oFolder = oPane.TopNode.BrowserFolders(oFolderName) Catch oFolder = oPane.AddBrowserFolder(oFolderName, Nothing) End Try
The Assembly one:
Dim oFolderName As String = "NewFolder"
Dim oDoc As Document = ThisApplication.ActiveDocument Dim oNodes As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection Dim oPane As BrowserPane = oDoc.BrowserPanes.ActivePane For Each oOcc As ComponentOccurrence In oDoc.ComponentDefinition.Occurrences Dim oNode As BrowserNode = oPane.GetBrowserNodeFromObject(oOcc) oNodes.Add(oNode) Next
Dim oFolder As BrowserFolder = Nothing Try oFolder = oPane.TopNode.BrowserFolders(oFolderName) Catch oFolder = oPane.AddBrowserFolder(oFolderName, oNodes) End Try
Thank you.
First code works perfect. How about using an array to create multiple folders, if not exist?
Thanks
Sure thing:
Dim oFN() As String = {"NewFolder", "OldFolder", "AnotherFolder"} Dim oDoc As Document = ThisApplication.ActiveDocument Dim oPane As BrowserPane = oDoc.BrowserPanes.ActivePane For Each oFolderName As String In oFN Dim oFolder As BrowserFolder = Nothing Try oFolder = oPane.TopNode.BrowserFolders(oFolderName) Catch oFolder = oPane.AddBrowserFolder(oFolderName, Nothing) End Try Next
Hmm, that's strange. I've just tested it and it worked.
Why did you enclose it in a Sub() ?
Do you call it from somewhere? If not, either change it to "Sub Main()" or remove it.
As always, you're welcomed.
Hi again
I need a small update on this ilogic if possible: if a folder with name "XYZ" is found, then rename it to "ABC".
Could you help me on this?
Thanks!
Sure, here it is in the current rule:
Dim oFN() As String = {"XYZ", "OldFolder", "AnotherFolder"} Dim oDoc As Document = ThisApplication.ActiveDocument Dim oPane As BrowserPane = oDoc.BrowserPanes.ActivePane For Each oFolderName As String In oFN Dim oFolder As BrowserFolder = Nothing Try oFolder = oPane.TopNode.BrowserFolders(oFolderName) Catch oFolder = oPane.AddBrowserFolder(oFolderName, Nothing) End Try If oFolderName = "XYZ" Then oFolder.Name = "ABC" Next
As a standalone one:
Dim oFolderName As String = "XYZ" Dim oNewName As String = "ABC" Dim oDoc As Document = ThisApplication.ActiveDocument Dim oPane As BrowserPane = oDoc.BrowserPanes.ActivePane Try Dim oFolder As BrowserFolder = oPane.TopNode.BrowserFolders(oFolderName) oFolder.Name = oNewName Catch End Try
Again, thank you.
Older folder is not renamed.
Its renamed only if I move the rename line, as this:
Dim oFN() As String = {"XYZ", "OldFolder", "AnotherFolder"} Dim oDoc As Document = ThisApplication.ActiveDocument Dim oPane As BrowserPane = oDoc.BrowserPanes.ActivePane For Each oFolderName As String In oFN Dim oFolder As BrowserFolder = Nothing Try oFolder = oPane.TopNode.BrowserFolders(oFolderName) If oFolderName = "XYZ" Then oFolder.Name = "ABC" Catch oFolder = oPane.AddBrowserFolder(oFolderName, Nothing) End Try Next
But even this dont work well on first try.
Any idea?
@Owner2229 wrote:Sure thing:
Dim oFN() As String = {"NewFolder", "OldFolder", "AnotherFolder"} Dim oDoc As Document = ThisApplication.ActiveDocument Dim oPane As BrowserPane = oDoc.BrowserPanes.ActivePane For Each oFolderName As String In oFN Dim oFolder As BrowserFolder = Nothing Try oFolder = oPane.TopNode.BrowserFolders(oFolderName) Catch oFolder = oPane.AddBrowserFolder(oFolderName, Nothing) End Try Next
Top notch!
Is there a code for deleting those 3 folders in your code? So that you could go back and forth?