Announcements
Due to scheduled maintenance, the Autodesk Community will be inaccessible from 10:00PM PDT on Oct 16th for approximately 1 hour. We appreciate your patience during this time.
Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Copy Sketch Symbol sub-folder from .dwg template to current drawing via iLogic

3 REPLIES 3
Reply
Message 1 of 4
chewy_robot
283 Views, 3 Replies

Copy Sketch Symbol sub-folder from .dwg template to current drawing via iLogic

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)

Screenshot_1.jpg Screenshot_2.jpg

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

 

3 REPLIES 3
Message 2 of 4
WCrihfield
in reply to: chewy_robot

Hi @chewy_robot.  Are you aware that we do not need to keep all our SketchedSymbolDefinitions stored within our drawing templates, and that we can use an external SketchedSymbolDefinitionLibraries to store them in.  I used to store tons of SketchedSymbolDefinitions in my template drawings also, but it was a real pain, and often required cleaning out all the ones I do not need later, which I did not like having to do.  But then I discovered how the library system worked for these and that changed everything for the better.  There is now a single DrawingDocument file stored outside of where we store our real templates, but still just under our Project file location, and within that DrawingDocument is this whole collection of SketchedSymbolDefinitions, all sorted and in multiple levels of sub folders.  Our drawing templates now contain almost none of them, unless they are already being used on some sheets, within specialty multi-sheet drawing templates.  Then when I make a new drawing from my template, and want to include / place any SketchedSymbols into any of my sheets, I just right-click on the Sketch Symbols folder (under the Drawing Resources folder in the model browser tree), even if there is nothing in that folder yet, and choose 'Insert'.  That opens a dialog, which lets me either choose a 'local' resource, or one from within my SketchedSymbolDefinitionsLibrary.  As simple as that.  In the end, my new drawing only contains the ones I have inserted directly from the library (which copies the definition to the local document).  And no unused ones to clean-up.  No code required.  Just putting that out there, in case it may be interesting or useful to you.  But I may be able to help with the code side too, if still needed.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 4
chewy_robot
in reply to: WCrihfield

Thanks for the reply and for the info! This makes sense. However I am trying to eliminate clicks and I have this current rule as part of a "drawing updater" so to speak so I wanted to include this in there as part of a drawing clean up process just taking it a step further. Some people on my team are still unfamiliar with some Inventor processes so I am trying to keep things simple for them

Message 4 of 4
WCrihfield
in reply to: chewy_robot

Hi @chewy_robot.  The good news is that I do believe that most, if not all, of what you would like to do is possible to do by code.  However, the bad news is that some of it would be pretty complicated, and would require a whole lot more code, possibly doubling or tripling the size of your code.  The step where you would like to eliminate local resources that are not being used, and are not found in the template sounds relatively easy to do.  But finding sub folders within the model browser tree in the template, then creating those on the local side, then putting specific nodes into it, and making sure they are in alphabetical order is a whole other can of worms.  I am not sure I would have time to tackle that right now myself, while I have several other projects going on.

 

Here is a version of your code which I tweaked slightly, and included the step where it eliminates 'local', unused resources that are not present in the template.  One detail of my 'tweak' is eliminating the use of one code phrase to identify a document while doing the document type check, then using a different code phrase to identify a document when actually setting the value of the document variable to use in the code.  That can lead to both pointing to different documents in some situations.  This version is a little more direct, efficient, and safe.  I reused your variable name though, so it would not mess anything else up.  I just eliminated the Lists, message, and function to shorten the code a bit for posting the added process, since you already have them in your copy.

Sub Main()
    ' Ensure the rule is running in a drawing document
    Dim currentDoc As DrawingDocument = TryCast(ThisDoc.Document, Inventor.DrawingDocument)
	If currentDoc Is Nothing 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)

    ' Copy missing sketch symbols from template to current document
    Dim templateSymbols As SketchedSymbolDefinitions = templateDoc.SketchedSymbolDefinitions
    Dim currentSymbols As SketchedSymbolDefinitions = currentDoc.SketchedSymbolDefinitions

    'Copy symbols from template that are not in the current document
    For Each TSSDef As SketchedSymbolDefinition In templateSymbols
		Dim bFoundLocally As Boolean = False
		For Each LSSDef As SketchedSymbolDefinition In currentSymbols
			If LSSDef.Name = TSSDef.Name Then
				bFoundLocally = True
				Exit For
			End If
		Next LSSDef
		If Not bFoundLocally Then
			TSSDef.CopyTo(currentDoc, True)
		End If
    Next TSSDef

    'Delete unused symbols from current document that are not in the template
	For Each LSSDef As SketchedSymbolDefinition In currentSymbols
		If LSSDef.IsReferenced Then Continue For 'skip to next symbol
		Dim bFoundInTemplate As Boolean = False
		For Each TSSDef As SketchedSymbolDefinition In templateSymbols
			If LSSDef.Name = TSSDef.Name Then
				bFoundInTemplate = True
				Exit For
			End If
		Next TSSDef
		If Not bFoundInTemplate Then LSSDef.Delete
	Next LSSDef
    
	'<<< code here to copy browser sub folders from template to current >>>
	
	' Clean up by closing the template without saving
    templateDoc.Close(False)
	
	'<<< code here to put nodes into browser sub folder >>>
	
	'<<< code here to sort nodes in sub folder alphabetically, if not put in that way >>>
	
    ' Optional: Reactivate the first sheet in the current document
    If currentDoc.Sheets.Count > 0 Then
        currentDoc.Sheets.Item(1).Activate()
    End If
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report