@wtriplett
See attached Drawing file with code contained as an internal rule. You can use this also as an external rule. I have included some error checking. You will need to modify this further to ensure the correct symbols get deleted and added. The for loop for checking the symbol name might need to be used a couple of times to check the contents of the drawing so might be better placed in a separate sub for convenience.
'Source for adding folder then adding symbols to folder
'https://forums.autodesk.com/t5/inventor-customization/copy-sketched-symbols-with-folders/td-p/713142...
Sub Main
'this is the name of the symbol library file,
'the path to this location is specified under
'Tools tab > Application Options button > Files tab
Dim oSymbolDrawingName As String
oSymbolDrawingName = "Title Block Sketch Symbols"'Library'Title Block Sketch Symbols
'check file type
If ThisDoc.Document.DocumentType <> kDrawingDocumentObject Then
Return
End If
'define the drawing document
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
Dim oSketchSymLib As SketchedSymbolDefinitionLibrary
Try
oSketchSymLib = oDrawDoc.SketchedSymbolDefinitions.SketchedSymbolDefinitionLibraries.Item(oSymbolDrawingName)
Catch
MessageBox.Show(oSymbolDrawingName & "-Error with Template File name or Location", "iLogic")
Return
End Try
Dim oSymbol As SketchedSymbol
Dim oFolderTitle As String = "TB Logo"
Dim oSymbolList As New ArrayList
For Each oSymbolDef In oSketchSymLib.SketchedSymbolDefinitions
oSymbolList.Add(oSymbolDef.name)
Next
''[use next line code if you want to pick a symbol from a list in the library]
oName = InputListBox("Pick a Symbol", oSymbolList, oSymbolList(0), "iLogic", "List of Symbols")
If oName = "" Then
MessageBox.Show("Exiting", "Title")
Return'exit rule
ElseIf oName = " " Then
MessageBox.Show("Exiting", "Title")
Return'exit rule
'Tbe symbol name must be same name as project number
ElseIf oName = iProperties.Value("Custom", "ProjectNumber") Then
'Check if there is a symbol in the drawing with oName
For Each oSymbol In oDrawDoc.ActiveSheet.SketchedSymbols
'look for the symbol by name
'deletes all symbols not "oName" Or all symbols = "oName"
'If oSymbol.Name <> oName Or oSymbol.Name = oName Then
'Delete if present to ensure only one is added and it is the most uptodate
If oSymbol.Name = oName Then
'If oSymbol.Name <> Symbol Then
oSymbol.Delete
MessageBox.Show(oName & "-Symbol has been Deleted from all Active Sheet", "iLogic")
End If
Next
Dim oSymDef As SketchedSymbolDefinition
'Add the symbol to the drawing
Try
oSymDef = oDrawDoc.SketchedSymbolDefinitions.AddFromLibrary(oSketchSymLib, oName, True)
'Call the sub to add folder and then symbol/symbols to the folder
Catch
MessageBox.Show("Symbol All Ready Created,Exiting", "iLogic")
End Try
Try
Call AddItemsToFolder(oFolderTitle, oName)
Call SortDwgResourcesFolder
Catch
MessageBox.Show("Folder All Ready Created,Exiting", "iLogic")
End Try
'coordinate units must be converted to centimeters [WET]
Dim oPosition As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(13.875*2.54, 3.275*2.54)
Try
'use this If the symbol has no prompted entry
oSymbol = oDrawDoc.ActiveSheet.SketchedSymbols.Add(oSymDef, oPosition, 0, 1, )
Catch
MessageBox.Show("Symbol All Ready Created,Exiting", "iLogic")
End Try
Else
Try
'Check if there is a symbol in the drawing with oName
For Each oSymbol In oDrawDoc.ActiveSheet.SketchedSymbols
'look for the symbol by name
'deletes all symbols not "oName" Or all symbols = "oName"
'If oSymbol.Name <> oName Or oSymbol.Name = oName Then
'Delete if present to ensure only one is added and it is the most uptodate
If oSymbol.Name = oName Then
'If oSymbol.Name <> Symbol Then
oSymbol.Delete
MessageBox.Show(oName & "-Symbol has been Deleted from all Active Sheet", "iLogic")
End If
Next
'Delete the symbol in the Drawing Resources folder
SymDel = oDrawDoc.SketchedSymbolDefinitions.Item(oName)
SymDel.Delete
MessageBox.Show(oName & "-Symbol has been Deleted from Drawing Resources folder", "iLogic")
Call DeleteItemsFolder(oFolderTitle)
MessageBox.Show(oFolderTitle & "-SubFolder has been Deleted from Drawing Resources Folder", "iLogic")
Catch
MessageBox.Show("Nothing to delete Exiting", "iLogic")
End Try
End If
InventorVb.DocumentUpdate()
End Sub
Sub AddItemsToFolder(oFolderTitle As String, ByVal ParamArray oSketchedSymbolNameArray As String())
Dim oPane As BrowserPane
Dim oNode As BrowserNode
Dim oTopNode As BrowserNode
Dim oDwgResourcesFolder As BrowserNode
Dim oSketchedSymbolsFolder As BrowserNode
Dim oOccurrenceNodes1 As ObjectCollection
oOccurrenceNodes1 = ThisApplication.TransientObjects.CreateObjectCollection
oPane = ThisApplication.ActiveDocument.BrowserPanes("Model")
oDwgResourcesFolder = oPane.TopNode.BrowserNodes.Item("Drawing Resources")
oSketchedSymbolsFolder = oDwgResourcesFolder.BrowserNodes.Item(4)
For Each oNode In oSketchedSymbolsFolder.BrowserNodes
oNodeName = Right(oNode.FullPath, Len(oNode.FullPath) - InStrRev(oNode.FullPath, ":", -1))
For Each oSketchSymbolName In oSketchedSymbolNameArray
If oNodeName = oSketchSymbolName
oOccurrenceNodes1.Add(oNode)
End If
Next
Next
oPane.AddBrowserFolder(oFolderTitle, oOccurrenceNodes1)
End Sub
Sub DeleteItemsFolder(oFolderTitle As String)
Dim oPane As BrowserPane
Dim oNode As BrowserNode
Dim oTopNode As BrowserNode
Dim oDwgResourcesFolder As BrowserNode
Dim oSketchedSymbolsFolder As BrowserNode
oPane = ThisApplication.ActiveDocument.BrowserPanes("Model")
MessageBox.Show(oFolderTitle, "Folder Title")
oDwgResourcesFolder = oPane.TopNode.BrowserNodes.Item("Drawing Resources")
oSketchedSymbolsFolder = oDwgResourcesFolder.BrowserNodes.Item(4)
oSketchedSymbolsSubFolder = oSketchedSymbolsFolder.BrowserFolders.Item(oFolderTitle)
oSketchedSymbolsSubFolder.Delete
End Sub
Sub SortDwgResourcesFolder()
ThisApplication.ActiveDocument.BrowserPanes("Model").TopNode.BrowserNodes.Item("Drawing Resources").BrowserNodes.Item(4).DoSelect
Dim oCommandMgr As CommandManager
oCommandMgr = ThisApplication.CommandManager
Dim oControlDef1 As ControlDefinition
oControlDef1 = oCommandMgr.ControlDefinitions.Item("DrawingResourceSort")
oControlDef1.Execute
End Sub
If this solved a problem, please click (accept) as solution.
Or if this helped you, please, click (like)
Regards
Alan