Auto Folder for Components (FAST) ilogic code

Auto Folder for Components (FAST) ilogic code

maxim.teleguz
Advocate Advocate
1,251 Views
10 Replies
Message 1 of 11

Auto Folder for Components (FAST) ilogic code

maxim.teleguz
Advocate
Advocate

finally after utilizing snippets of the best code out there I created an auto folder tool that is blazingly fast. I suggest Autodesk implements this as it is so quick. 

there are a few issues with it that I hope some beautiful people can help me solve. 

Component patterns placed in folders are causing them to go above the model states folder which I didn't even think was possible. 

also if there was a way without sacrificing speed to only place components if there is more than one in the folder. currently it places all unique ones into a folder. 

 

 

Dim oDoc1 As Document
oDoc1 = ThisApplication.ActiveDocument

Dim oTopNode3 As BrowserNode
oTopNode3 = oDoc1.BrowserPanes.ActivePane.TopNode
Dim oNode3 As BrowserNode

For Each oNode3 In oTopNode3.BrowserNodes
	oNode3.DoSelect
	If oNode3.BrowserNodeDefinition.Label.Contains(":") = True Then
		If oNode3.BrowserNodeDefinition.Label.Contains("Model States") = False Then
			If oNode3.BrowserNodeDefinition.Label.Contains("Component Pattern") = False Then
			oNode3.DoSelect
			


			Try
				'define assembly doc
				Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
				'define assembly Component Definition
				Dim oAsmCompDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
				'Dim itemName As ComponentOccurrence
			
			
				startagain :
				SendKeys.SendWait("{ESC}")
				AppActivate(ThisApplication.Caption)
			
				c = ThisApplication.ActiveDocument.SelectSet.Item(1)
			'c = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAllEntitiesFilter, "Select component")
				If c Is Nothing Then
					Exit Sub
				Else
					'get the path and file name of the selected item
					d = Component.InventorComponent(c.name).Definition.Document.FullFileName
					f = IO.Path.GetFileNameWithoutExtension(d)
					'MsgBox(f)
					'exit sub
				End If
			
				'f = InputBox("Input Wildcard of Item you want to put in the folder", "BrowserNodeItem Name", "****")
				'sFolder = f
				
				If f = "" Then
					Exit Sub
				Else If f = "** Item Name**"
					MsgBox("please enter item number")
					Exit Sub
				End If
				
				
				'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(f)
					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(f) 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(f) 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(f, oCollection)
					
				Catch
					MessageBox.Show("Error", "ilogic")
				End Try
				oFolder.BrowserNode.DoSelect
				GoTo startagain
				'SOURCECODE: https://forums.autodesk.com/t5/inventor-forum/ilogic-to-create-browser-node-folder-and-move-all-iparts-and/td-p/9000788
			
			Catch
			End Try
			
			End If
		End If
	End If
Next

 

 

 

0 Likes
1,252 Views
10 Replies
Replies (10)
Message 2 of 11

maxim.teleguz
Advocate
Advocate

Please let me know how this works for you as the previous occurrence builder add-in is broken for ages now. 

0 Likes
Message 3 of 11

WCrihfield
Mentor
Mentor

Hi @maxim.teleguz.  If I am correct in my assessment of what your design intent here is, you want to:

  1. Select a top level assembly component before running the rule.
  2. Run the rule.
  3. The code gets the base name of the selected component (usually file name, but is customizable).
  4. If a BrowserFolder already exists with that name, delete it.
  5. Find any / all other top level components with that same base name (or same file name), and add their BrowserNode to an ObjectCollection.
  6. Create a new BrowserFolder and put all those component BrowserNodes into it.

Correct?

If that is correct, then I do not see the logic in iterating every BrowserNode under the TopNode.BrowserNodes, and I do not see the logic in checking the ComponentDefinition Type of every component.  Also, there is no need to redefine the variable for the main assembly or the variables for the BrowserPane & top BrowserNode down within the loop of every BrowserNode...those can be established before the loop, because they should not change.  I have attached a couple of alternate iLogic rule codes as text files for you to review and test with.  In those examples, I am using StartsWith() instead of Contains(), and I am just getting the base name from the first part of the component's name, to save processing effort, and I have incorporated 1 or more transactions, so that these actions can easily be undone.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 11

WCrihfield
Mentor
Mentor

Hi @maxim.teleguz.  I dabbled with this idea a little more, with the plan in mind to bundle all top level components into browser folders if there are more than one of them referencing the same file, and they are not 'pattern elements'.  After some brief testing, I see what you mean about some of the folders being created just under the model BrowserPane's top node.  That is definitely odd.  I attempted to exclude any components that were 'pattern elements', and that seemed to help a little, but did not eliminate the problem.  I also tried using the BrowserPane.Reorder method to move the newly created BrowserFolder to a location either just below the 'Origin' folder, or at the end of the list.  Those reorder attempts all failed.  Not sure why yet.  Attached is a text file containing the iLogic code I was testing with for this task.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 11

maxim.teleguz
Advocate
Advocate

Thank you for testing it, I truly believe this occurrence bundler could help a lot of new ideas come to fruition which is why I want to make sure this works really well first. I will take a look at and try to understand your examples.

My next idea will be to look for occurrences and which part they are constrained to and add them to the folder with that part. 

0 Likes
Message 6 of 11

JelteDeJong
Mentor
Mentor

I did see your review in the app store on my addon "OccurrenceBundler". I was surprised by your comment:

"I created illogic code for this instead and it is free."

The add-in is also free, even the source code is free available. You can find it on GitHub. If there is something you don't like, you can alter it or add features and create a pull request. 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 7 of 11

maxim.teleguz
Advocate
Advocate

thank you for sharing the github link, i will study how you did your work. BTW you can use: https://converter.telerik.com/ to convert your work to vb and test variations of it and convert it back to cs. 

0 Likes
Message 8 of 11

maxim.teleguz
Advocate
Advocate

here is the updated code to work with 2023 and to use the displayed part name instead:

Sub main

Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument 'define assembly doc
Dim oAsmCompDef As AssemblyComponentDefinition = oDoc.ComponentDefinition 'define assembly Component Definition

startagain :
SendKeys.SendWait("{ESC}")
AppActivate(ThisApplication.Caption)

c = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAllEntitiesFilter, "Select component")

If c Is Nothing Then
	Exit Sub
Else
	'get displayed string since that is what some content center parts show as.
	Dim originalString As String = c.name
	Dim symbol As String = ":"
	' Find the position of the symbol
	Dim indexOfSymbol As Integer = originalString.IndexOf(symbol)
	
	' Check if the symbol is present in the string
	If indexOfSymbol <> -1 Then
	    ' Extract the portion of the string before the symbol
	    Dim resultString As String = originalString.Substring(0, indexOfSymbol)
	    'MsgBox(resultString)
		f = resultString
	Else
	    ' Symbol not found in the string
	    'MsgBox("Symbol not found in the string.")
	End If
End If

If f = "" Then
	Exit Sub
Else If f = "** Item Name**"
MsgBox("please enter item number")
Exit Sub
End If

Dim oPane As BrowserPane = oDoc.BrowserPanes.ActivePane 'get browser pane
Dim oTopNode As BrowserNode = oPane.TopNode 'get browser pane top node
Dim oFolder As BrowserFolder

Try 'try to get existing folder by name and delete it
	oFolder = oPane.TopNode.BrowserFolders.Item(f)'filenamewithoutextension
	oFolder.Delete
Catch 'catch error if not found
End Try

Dim oCollection As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection 'create a bucket to put stuff in
Dim oOcc As ComponentOccurrence 'Iterate through all of the occurrences in the assembly

Dim part As String = "Part"
Dim assembly As String = "Assembly"

For Each oOcc In oAsmCompDef.Occurrences

	If oOcc.Name.Contains(f) And Inventor.DocumentTypeEnum.kPartDocumentObject Then
		If TypeOf oOcc.Definition Is PartComponentDefinition Then
			Dim oNode As BrowserNode = oPane.GetBrowserNodeFromObject(oOcc) 'get browser node of occurence
			oCollection.Add(oNode) 'add node to collection
			'MsgBox(f)
		End If
	End If

	If oOcc.Name.Contains(f) And Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
		If TypeOf oOcc.Definition Is AssemblyComponentDefinition Then
			Dim oNode1 As BrowserNode = oPane.GetBrowserNodeFromObject(oOcc) 'get browser node of occurence
			oCollection.Add(oNode1) 'add node to collection
		End If
	End If

Next

Try
	oFolder = oPane.AddBrowserFolder(f, oCollection) 'recreate folder & add collection To folder
Catch
	MessageBox.Show("Error", "ilogic")
End Try

oFolder.BrowserNode.DoSelect 'select folder that was created so that it you can stay in place.
GoTo startagain
'SOURCECODE: https://forums.autodesk.com/t5/inventor-forum/ilogic-to-create-browser-node-folder-and-move-all-iparts-and/td-p/9000788

End Sub
Message 9 of 11

442780782
Advocate
Advocate

Hello! Whether the code is implemented in Autodesk® Inventor® 2016 or not

Message 10 of 11

maxim.teleguz
Advocate
Advocate

@442780782 wrote:

Hello! Whether the code is implemented in Autodesk® Inventor® 2016 or not


why would you ever use 2016? 2020-2023 is so much faster. 2024 introduced GPU processing too. 

0 Likes
Message 11 of 11

442780782
Advocate
Advocate

Hello! Whether the code can be modified to be available in 2016
Run error Public member "name" of type "FaceProxy" not found.

0 Likes