iLogic to create browser node folder and move all iParts and iAssemblies into it

iLogic to create browser node folder and move all iParts and iAssemblies into it

Anonymous
Not applicable
1,276 Views
9 Replies
Message 1 of 10

iLogic to create browser node folder and move all iParts and iAssemblies into it

Anonymous
Not applicable

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

0 Likes
1,277 Views
9 Replies
Replies (9)
Message 2 of 10

Anonymous
Not applicable

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

Message 3 of 10

maxim.teleguz
Advocate
Advocate

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

 

0 Likes
Message 4 of 10

Ryan.Stewart
Participant
Participant
Is there a way to add code to only use the occurrences that are 8 characters long?
0 Likes
Message 5 of 10

maxim.teleguz
Advocate
Advocate

@Ryan.Stewart wrote:
Is there a way to add code to only use the occurrences that are 8 characters long?

that should be really easy to do that, but why? this is a manual click on part in feature tree and it creates a folder for them. 

0 Likes
Message 6 of 10

Ryan.Stewart
Participant
Participant

I am using the rule now to sort my browser and put my 96 parts in a folder and my 97 part in a folder.  But with the wild card  it is also getting some of my files that are project specific but have 96 or 97 in the part name.  They are longer file names similar to this example "123496-00".  But this file name would be moved to the folder also since it has a 96 in the file name.

0 Likes
Message 7 of 10

maxim.teleguz
Advocate
Advocate

@Ryan.Stewart wrote:

I am using the rule now to sort my browser and put my 96 parts in a folder and my 97 part in a folder.  But with the wild card  it is also getting some of my files that are project specific but have 96 or 97 in the part name.  They are longer file names similar to this example "123496-00".  But this file name would be moved to the folder also since it has a 96 in the file name.


ill think on this

0 Likes
Message 8 of 10

maxim.teleguz
Advocate
Advocate

@Ryan.Stewart wrote:

I am using the rule now to sort my browser and put my 96 parts in a folder and my 97 part in a folder.  But with the wild card  it is also getting some of my files that are project specific but have 96 or 97 in the part name.  They are longer file names similar to this example "123496-00".  But this file name would be moved to the folder also since it has a 96 in the file name.


this is my latest updated code. go to feature tree and click on the part you want to be in a folder


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 c Is Nothing Then
'	Exit Sub
'Else
'	d = Component.InventorComponent(c.name).Definition.Document.FullFileName 'get the path and file name of the selected item
'	f = IO.Path.GetFileNameWithoutExtension(d)
'	MsgBox(f)
'	'Exit Sub
'End If



'this works for display name too, keeping just in case for future reference. needs the function below to work

'Dim c As Object = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAllEntitiesFilter, "Select component")

'If c Is Nothing Then
'    Exit Sub
'Else
'    Dim selectedComponent As ComponentOccurrence = TryCast(c, ComponentOccurrence)

'    If selectedComponent IsNot Nothing Then
'        ' For assembly components, get the display name from ComponentOccurrence
'        Dim displayName As String = selectedComponent.Name
'        MsgBox(displayName)
'    Else
'        ' For parts, get the display name from BrowserNodeDefinition.Label
'        Dim partNode As BrowserNode = ThisApplication.ActiveDocument.BrowserPanes.ActivePane.GetBrowserNodeFromObject(c)
'        Dim displayName As String = GetDisplayName(partNode)
'        MsgBox(displayName)
'    End If
'	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

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

'this also works really well to compare like something keeping just in case for future reference.
'	If oOcc.Name.Contains(oOcc.Name Like c.name) 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(oOcc.Name Like c.name) 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

'Private Function GetDisplayName(node As BrowserNode) As String
'    Dim label As String = ""

'    If node.BrowserNodeDefinition.Type = ObjectTypeEnum.kAssemblyComponentDefinitionObject Then
'        ' For assembly components, get the display name from ComponentOccurrence
'        Dim compOcc As ComponentOccurrence = TryCast(node.BrowserNodeDefinition, ComponentOccurrence)
'        If compOcc IsNot Nothing Then
'            label = compOcc.Name
'        End If
'    Else
'        ' For parts, get the display name from BrowserNodeDefinition.Label
'        label = node.BrowserNodeDefinition.Label.ToString()
'    End If

'    Return label
'End Function
0 Likes
Message 9 of 10

Ryan.Stewart
Participant
Participant
Not lazy it saves me time not having to search thru the browser with my common fasteners or purchased part and only work with the project specific parts.
0 Likes
Message 10 of 10

Ryan.Stewart
Participant
Participant

The second ilogic version works great with what I am doing but it gets files that contain the 96 number in it.  I am VERY new to logic, Is there an easy way to limit adding the file to my folder if it is not a library part?  All my library parts are an 8-digit number.

 

0 Likes