- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm trying to append string data to a browser folder. the purpose of this is so that I can make another code that prompts the user to make the occurrences placed in the browser folder inherit the string values as custom iproperties. this is what I have so far, but it's saying that the object I'm trying to use isn't supported. could someone help me fix this code? I've included another code that allows the user to read attributes from the folder selected, but I don't think either are working properly.
Sub Main()
Call MainLogic()
End Sub
Sub MainLogic()
Dim oDoc As Document = ThisDoc.Document
Dim oBrowserPanes As BrowserPanes = oDoc.BrowserPanes
Dim oModelBrowserPane As BrowserPane = oBrowserPanes.Item("Model")
Dim oBrowserFolders As BrowserFoldersEnumerator = oModelBrowserPane.TopNode.BrowserFolders
Dim folderNames As New List(Of String)
' Populate the list of folder names
For i As Integer = 1 To oBrowserFolders.Count
folderNames.Add(oBrowserFolders.Item(i).Name)
Next
' Prompt the user to choose a folder
If folderNames.Count > 0 Then
Dim selectedFolderName As String = InputListBox("Please select a browser folder:", folderNames, folderNames(0), "Browser Folder Selection")
If Not String.IsNullOrEmpty(selectedFolderName) Then
' Find the selected folder
Dim selectedFolder As BrowserFolder = Nothing
For i As Integer = 1 To oBrowserFolders.Count
Dim oFolder As BrowserFolder = oBrowserFolders.Item(i)
If oFolder.Name = selectedFolderName Then
selectedFolder = oFolder
Exit For
End If
Next
If Not selectedFolder Is Nothing Then
' Get the object represented by the browser folder
Dim obj As Object = selectedFolder.BrowserNode.NativeObject
' Prompt the user to enter a string
Dim userString As String = InputBox("Enter a string to append as an attribute:", "String Input")
If Not String.IsNullOrEmpty(userString) Then
Try
' Attempt to add the attribute
AddAttributeToObject(obj, "CustomAttributes", "UserInput", userString)
MessageBox.Show("Attribute added successfully to object in folder: " & selectedFolderName, "Success")
Catch ex As Exception
MessageBox.Show("Error adding attribute: " & ex.Message, "Error")
End Try
Else
MessageBox.Show("No string entered.", "Cancelled")
End If
Else
MessageBox.Show("Selected folder not found.", "Error")
End If
Else
MessageBox.Show("No folder selected.", "Cancelled")
End If
Else
MessageBox.Show("No folders found in the browser.", "Error")
End If
End Sub
Sub AddAttributeToObject(obj As Object, attributeSetName As String, attributeName As String, attributeValue As String)
If obj Is Nothing Then
Throw New ArgumentNullException("obj")
End If
Dim oAttribSets As AttributeSets
Try
oAttribSets = obj.AttributeSets
Catch ex As Exception
Throw New Exception("Object does not support attributes.")
End Try
Dim oAttribSet As AttributeSet
If Not oAttribSets.NameIsUsed(attributeSetName) Then
oAttribSet = oAttribSets.Add(attributeSetName)
Else
oAttribSet = oAttribSets.Item(attributeSetName)
End If
If Not oAttribSet.NameIsUsed(attributeName) Then
oAttribSet.Add(attributeName, ValueTypeEnum.kStringType, attributeValue)
Else
oAttribSet.Item(attributeName).Value = attributeValue
End If
End Sub
Sub Main() Call MainLogic() End Sub Sub MainLogic() Dim oDoc As Document = ThisDoc.Document Dim oBrowserPanes As BrowserPanes = oDoc.BrowserPanes Dim oModelBrowserPane As BrowserPane = oBrowserPanes.Item("Model") Dim oBrowserFolders As BrowserFoldersEnumerator = oModelBrowserPane.TopNode.BrowserFolders Dim folderNames As New List(Of String) ' Populate the list of folder names For i As Integer = 1 To oBrowserFolders.Count folderNames.Add(oBrowserFolders.Item(i).Name) Next ' Prompt the user to choose a folder If folderNames.Count > 0 Then Dim selectedFolderName As String = InputListBox("Please select a browser folder:", folderNames, folderNames(0), "Browser Folder Selection") If Not String.IsNullOrEmpty(selectedFolderName) Then ' Find the selected folder Dim selectedFolder As BrowserFolder = Nothing For i As Integer = 1 To oBrowserFolders.Count Dim oFolder As BrowserFolder = oBrowserFolders.Item(i) If oFolder.Name = selectedFolderName Then selectedFolder = oFolder Exit For End If Next If Not selectedFolder Is Nothing Then ' Get the object represented by the browser folder Dim obj As Object = selectedFolder.BrowserNode.NativeObject ' Get attributes for the object Dim attributeInfo As String = GetAttributeInfo(obj) ' Display the attribute information If Not String.IsNullOrEmpty(attributeInfo) Then MessageBox.Show(attributeInfo, "Folder Attributes") Else MessageBox.Show("No attributes found for this folder.", "Folder Attributes") End If Else MessageBox.Show("Selected folder not found.", "Error") End If Else MessageBox.Show("No folder selected.", "Cancelled") End If Else MessageBox.Show("No folders found in the browser.", "Error") End If End Sub Function GetAttributeInfo(obj As Object) As String If obj Is Nothing Then Return Nothing Dim attributeInfo As String = "" Dim oSets As Inventor.AttributeSets = Nothing Try oSets = obj.AttributeSets Catch Return Nothing End Try If oSets Is Nothing OrElse oSets.Count = 0 Then Return Nothing For Each oSet As Inventor.AttributeSet In oSets attributeInfo &= "Attribute Set: " & oSet.Name & vbNewLine If oSet.Count = 0 Then Continue For For Each oAtt As Inventor.Attribute In oSet attributeInfo &= " - " & oAtt.Name & ": " & oAtt.Value.ToString & vbNewLine Next attributeInfo &= vbNewLine Next Return attributeInfo End Function
Solved! Go to Solution.