Ilogic to create browser tree folders (ONLY IF NOT EXIST)

Ilogic to create browser tree folders (ONLY IF NOT EXIST)

amarcoc
Advocate Advocate
2,572 Views
13 Replies
Message 1 of 14

Ilogic to create browser tree folders (ONLY IF NOT EXIST)

amarcoc
Advocate
Advocate

Hi.

 

How can I create new folders in Inventor browser tree, using ilogic (ONLY IF NOT EXIST)?

 

Thanks in advance!

0 Likes
Accepted solutions (1)
2,573 Views
13 Replies
Replies (13)
Message 2 of 14

Owner2229
Advisor
Advisor

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

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 3 of 14

amarcoc
Advocate
Advocate

EDIT: check next post 

0 Likes
Message 4 of 14

amarcoc
Advocate
Advocate

The code on first updated code part creates new folder even if exist. The second generates error as in previous post 😞

0 Likes
Message 5 of 14

Owner2229
Advisor
Advisor

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

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 6 of 14

amarcoc
Advocate
Advocate

Thank you.

 

First code works perfect. How about using an array to create multiple folders, if not exist?

 

Thanks

0 Likes
Message 7 of 14

Owner2229
Advisor
Advisor
Accepted solution

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
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 8 of 14

amarcoc
Advocate
Advocate

EDIT:

 

It worked perfect.

 

You're great.

 

Thanks!

0 Likes
Message 9 of 14

Owner2229
Advisor
Advisor

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.

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 10 of 14

amarcoc
Advocate
Advocate

meanwhile I edited my last comment.

 

Worked perfect.

 

Thanks.

0 Likes
Message 11 of 14

amarcoc
Advocate
Advocate

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!

0 Likes
Message 12 of 14

Owner2229
Advisor
Advisor

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
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 13 of 14

amarcoc
Advocate
Advocate

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?

0 Likes
Message 14 of 14

Anonymous
Not applicable

@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?

0 Likes