Copy Sketch Symbol sub-folder from .dwg template to current drawing via iLogic
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have an iLogic rule that can run in an out of date .dwg file and it will cross-check with the .dwg template file for any missing sketch symbols but I need to add some more functionality if possible.
What I want it to be able to do is to also copy over any folders containing sketch symbols in them over as well (see screenshot_1 from the template file vs screenshot_2 when the code is run). This code will copy the sketch symbols located in the sub folders however not the actual folder itself. Is this possible? The reason being - I will have over 20+ in this sub folder that I want to keep tidy. Some other things I would like to add -
- Can these be sorted in alphabetical order without deleting them all and re-adding them alphabetically?
-Can sketch symbols not found in the template be deleted out of the current .dwg file if they are also not being used on the current .dwg file (as to not delete the sketch symbol off the sheet if it is being used)
Below is my code currently:
Sub Main()
' Ensure the rule is running in a drawing document
If ThisApplication.ActiveDocument.DocumentType <> kDrawingDocumentObject Then
MessageBox.Show("This rule only works for Drawing Documents. Exiting.", "WRONG DOCUMENT TYPE", MessageBoxButtons.OK)
Return
End If
' Load the template drawing with updated sketch symbols
Dim templatePath As String = "TEMPLATE PATH HERE.dwg"
Dim templateDoc As DrawingDocument = ThisApplication.Documents.Open(templatePath, False)
Dim currentDoc As DrawingDocument = ThisDoc.Document
' Copy missing sketch symbols from template to current document
Dim templateSymbols As SketchedSymbolDefinitions = templateDoc.SketchedSymbolDefinitions
Dim currentSymbols As SketchedSymbolDefinitions = currentDoc.SketchedSymbolDefinitions
Dim missingSymbols As New List(Of String)
Dim importedSymbols As New List(Of String)
' Identify symbols that are in the template but not in the current document
For Each symDef In templateSymbols
If Not SymbolExists(currentSymbols, symDef.Name) Then
missingSymbols.Add(symDef.Name)
End If
Next
' Copy missing symbols to current document
For Each symName In missingSymbols
Dim symDef As SketchedSymbolDefinition = templateSymbols.Item(symName)
symDef.CopyTo(currentDoc, True)
importedSymbols.Add(symName)
Next
' Notify about imported symbols
If importedSymbols.Count > 0 Then
Dim message As String = "Imported symbols:" & vbCrLf & String.Join(vbCrLf, importedSymbols)
MessageBox.Show(message, "Symbols Imported")
Else
' MessageBox.Show("No new symbols were imported.", "Update Complete")
End If
' Clean up by closing the template without saving
templateDoc.Close(False)
' Optional: Reactivate the first sheet in the current document
If currentDoc.Sheets.Count > 0 Then
currentDoc.Sheets.Item(1).Activate()
End If
End Sub
Function SymbolExists(symbols As SketchedSymbolDefinitions, name As String) As Boolean
Try
Dim symbol As SketchedSymbolDefinition = symbols.Item(name)
Return symbol IsNot Nothing
Catch ex As Exception
Return False
End Try
End Function