Here's some code to:
- allow you to enter a name into an InputBox
- searches all styles within the active document for a style by that name to get the Style object
- Saves that style to the global repository (style library)
- Finds all drawing template files within the current projects templates location
- Opens each one, then searches for a style by that name within it, then checks to see if it is located globally, locally, or both.
- If it is located globally only, it creates a local copy of it within the template file
- If it is located either locally or both, it lets you know and asks you if you want to replace the existing local one within the template.
- If yes, it replaces it with the one from your current document.
- Saves, then closes the template document
With several checks in there to help eliminate possible error messages.
See if this will work for you.
Dim oDDoc As DrawingDocument = ThisDrawing.Document
'Get Style name from user
Enter :
Dim oStyleName As String = InputBox("Enter the Name of the Style you want to copy to the Templates.", "STYLE NAME")
If oStyleName = "" Then
oAns = MsgBox("Nothing was entered. Try again?", vbYesNo + vbQuestion, "NO ENTRY")
If oAns = vbYes Then GoTo Enter Else Return
End If
'Search for this style in the local drawing document, to get the actual Style object (early check)
Dim oFound As Boolean = False
Dim oStyle As Style
For Each oDStyle As Style In oDDoc.StylesManager.Styles
If oDStyle.Name = oStyleName Or oDStyle.InternalName = oStyleName Then
oFound = True
oStyle = oDStyle
oStyle.SaveToGlobal
End If
Next
If oFound = False Then
oTryAgain = MsgBox("No Style was found with that name. Try again?", vbYesNo + vbQuestion, "STYLE NOT FOUND")
If oTryAgain = vbYes Then GoTo Enter Else Return
End If
'Get all the Drawing Template files
Dim oTPath As String = ThisApplication.DesignProjectManager.ActiveDesignProject.TemplatesPath
Dim oFileNames() As String = System.IO.Directory.GetFiles(oTPath, "*.idw", IO.SearchOption.TopDirectoryOnly)
'Check if multiple template files were found.
If oFileNames.Length = 0 Then 'it did not find any
MsgBox("No template files were found matching your search criteria. Exiting.", vbOKOnly, "NOTHING FOUND")
Return
ElseIf oFileNames.Length > 0 Then 'it found some
a = InputListBox("There were " & oFileNames.Length & " files found matching your search criteria." & vbCrLf & "Here is the list:", oFileNames)
End If
'Open each drawing template file as a drawing document, then copy specified style library styles to local.
Dim oFName As String
Dim oTDDoc As DrawingDocument
Dim oTStyle As Style
For Each oFName In oFileNames
oTDDoc = ThisApplication.Documents.Open(oFName, False)
For Each oTStyle In oTDDoc.StylesManager.Styles
If oTStyle.Name = oStyleName Or oTStyle.InternalName = oStyleName Then
If oTStyle.StyleLocation = StyleLocationEnum.kLibraryStyleLocation Then
oTStyle.ConvertToLocal
ElseIf oTStyle.StyleLocation = StyleLocationEnum.kBothStyleLocation Or _
oTStyle.StyleLocation = StyleLocationEnum.kLocalStyleLocation Then
oReplace = MsgBox("That style already exists locally within this template file." & vbCrLf & _
"Do you want to replace the existing one?", vbYesNo + vbQuestion, "STYLE EXISTS")
If oReplace = vbYes Then
Dim oTSCol As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
oTSCol.Add(oTStyle)
oTDDoc.StylesManager.ReplaceStyles(oTSCol, oStyle, True) 'True purges old version
End If
End If
End If
Next
oTDDoc.Save
oTDDoc.Close
Next
Wesley Crihfield

(Not an Autodesk Employee)