Hi,
It can be done without too much effort in VBA
From your drawing document, you can access the DrawingDocument.StylesManager object which will give you access to each style groupe (DimensionStyles, StandardStyles, ObjectDefaultStyles, etc...)
In inventor, styles are either in your drawing or in your library (located in you Design Data Folder)
- Each styles can be tested with Style.StyleLocation which can be kLibraryStyleLocation, kLocalStyleLocation or kBothStyleLocation
- Local Styles can also be tested with Style.UptoDate to check if the library version has changed
- you can then manipulate a style with method like ConvertToLocal to get a style from library, or UpdateFromGlobal to update an existing style.
At my company, what I do is open the template drawing, gather a list of every local style from the template and then compare with styles in the active document to update them or get missing styles from library.
Here is how I do it. ScanStyles put styles in dictionnaries (a collection type that support .exists() method) and then UpdateStyles do the actual updating. You could change the ScanStyles function to a sub and just use the ThisApplication.ActiveDocument to get access to your open drawing Document. This my code as is. Some Conditions are specific to my need, you will need to edit it.
Sub UpdateStyles(StyleDic As Object, StandardStyleDic As Object, DefaultStyleDic As Object)
Dim invapp As Inventor.Application
Dim oStylesManager As StylesManager
Dim oDoc As Document
Dim oDrawDoc As DrawingDocument
Dim oStyle As DimensionStyle
Dim oStandardStyle As DrawingStandardStyle
Dim oDefaultStyle As ObjectDefaultsStyle
On Error GoTo UpdateStylesErr
'Loop through all dimension styles from the document and update them if needed
Set invapp = ThisApplication
Set oDoc = ThisApplication.ActiveDocument
If TypeOf oDoc Is DrawingDocument Then
Set oDrawDoc = oDoc
Else
MsgBox "Active Document has to be a Drawing Document", vbMsgBoxSetForeground, "UpdateStyles"
Exit Sub
End If
For Each oStyle In oDrawDoc.StylesManager.DimensionStyles
If oStyle.StyleLocation = kLibraryStyleLocation Then
'If style is not local to this drawing, but in the template, get it.
If StyleDic.exists(oStyle.name) Then
oStyle.ConvertToLocal
End If
Else
'Style is in local document, check if it needs to be updated
If Not oStyle.UpToDate Then
oStyle.UpdateFromGlobal
End If
End If
Next oStyle
For Each oDefaultStyle In oDrawDoc.StylesManager.ObjectDefaultsStyles
'If style is not local to this drawing, but in the template, get it.
If oDefaultStyle.StyleLocation = kLibraryStyleLocation Then
If DefaultStyleDic.exists(oDefaultStyle.name) Then
oDefaultStyle.ConvertToLocal
End If
Else
'Style is in local document, check if it needs to be updated
If Not oDefaultStyle.UpToDate Then
oDefaultStyle.UpdateFromGlobal
End If
End If
Next oDefaultStyle
For Each oStandardStyle In oDrawDoc.StylesManager.StandardStyles
'If style is not local to this drawing, but in the template, get it.
If oStandardStyle.StyleLocation = kLibraryStyleLocation Then
If StandardStyleDic.exists(oStandardStyle.name) Then
oStandardStyle.ConvertToLocal
End If
Else
'Style is in local document, check if it needs to be updated
If Not oStandardStyle.UpToDate Then
oStandardStyle.UpdateFromGlobal
End If
End If
Next oStandardStyle
UpdateStylesErr:
If Err Then
MsgBox "unexpected error: " & Err.Description, vbMsgBoxSetForeground, "UpdateStyles"
Err.Clear
End If
End Sub
Function ScanStyles(oDrawDoc As DrawingDocument) As Boolean
Dim invapp As Inventor.Application
Dim oStylesManager As StylesManager
Dim oDoc As Document
Dim OutdatedStyles As String
Dim oTemplateDrawDoc As DrawingDocument
Dim oStyle As DimensionStyle
Dim StyleDic As Object
Dim templatepath As String
Dim fso As Object
Dim StandardStyleDic As Object
Dim DefaultStyleDic As Object
Dim oStandardStyle As DrawingStandardStyle
Dim oDefaultStyle As ObjectDefaultsStyle
On Error GoTo ScanStylesErr
'Loop through all dimension styles from the document and update them if needed
Set invapp = ThisApplication
Set oDoc = ThisApplication.ActiveDocument
ScanStyles = True
Set fso = CreateObject("Scripting.FileSystemObject")
Set StyleDic = CreateObject("Scripting.Dictionary")
Set StandardStyleDic = CreateObject("Scripting.Dictionary")
Set DefaultStyleDic = CreateObject("Scripting.Dictionary")
'Path to template to compare styles varies if drawing uses MÉTRIQUE template
If InStr(1, oDrawDoc.StylesManager.ActiveStandardStyle.name, "métrique", vbTextCompare) > 0 Then
templatepath = invapp.FileLocations.TemplatesPath & "GENIK_dessin_métrique 2020.idw"
Else
templatepath = invapp.FileLocations.TemplatesPath & "GENIK_dessin_imperial 2020.idw"
End If
If fso.fileExists(templatepath) Then
Set oTemplateDrawDoc = invapp.Documents.Open(templatepath, False)
Else
MsgBox "Le fichier template à jour n'a pas été trouvé.", vbOKOnly, "Erreur"
Exit Function
End If
'Gather all local styles from template
For Each oStyle In oTemplateDrawDoc.StylesManager.DimensionStyles
If oStyle.StyleLocation = kBothStyleLocation Or oStyle.StyleLocation = kLocalStyleLocation Then
If Not StyleDic.exists(oStyle.name) Then
StyleDic.add oStyle.name, True
End If
End If
Next oStyle
'Gather all standard styles from template
For Each oStandardStyle In oTemplateDrawDoc.StylesManager.StandardStyles
If InStr(1, oStandardStyle.name, "genik", vbTextCompare) > 0 And (oStandardStyle.StyleLocation = kBothStyleLocation Or oStandardStyle.StyleLocation = kLocalStyleLocation) Then 'only check custom standards
If Not StandardStyleDic.exists(oStandardStyle.name) Then
StandardStyleDic.add oStandardStyle.name, True
End If
End If
Next oStandardStyle
'Gather all ObjectDefault style from template
For Each oDefaultStyle In oTemplateDrawDoc.StylesManager.ObjectDefaultsStyles
If InStr(1, oDefaultStyle.name, "genik", vbTextCompare) > 0 And (oDefaultStyle.StyleLocation = kBothStyleLocation Or oDefaultStyle.StyleLocation = kLocalStyleLocation) Then 'only check custom standards
If Not DefaultStyleDic.exists(oDefaultStyle.name) Then
DefaultStyleDic.add oDefaultStyle.name, True
End If
End If
Next oDefaultStyle
'Close template
oTemplateDrawDoc.Close True
Set oTemplateDrawDoc = Nothing
'search for styles needing an update
For Each oStyle In oDrawDoc.StylesManager.DimensionStyles
If oStyle.StyleLocation <> kLibraryStyleLocation Then
If Not oStyle.UpToDate Then 'Style is local and not up to date
OutdatedStyles = OutdatedStyles & vbCrLf & oStyle.name
ScanStyles = False
End If
ElseIf oStyle.StyleLocation = kLibraryStyleLocation Then
If StyleDic.exists(oStyle.name) Then
'Style is not local, but is local to template, needs to be added
OutdatedStyles = OutdatedStyles & vbCrLf & oStyle.name
ScanStyles = False
End If
End If
Next oStyle
For Each oDefaultStyle In oDrawDoc.StylesManager.ObjectDefaultsStyles
If oDefaultStyle.StyleLocation <> kLibraryStyleLocation Then
If Not oDefaultStyle.UpToDate Then
OutdatedStyles = OutdatedStyles & vbCrLf & oDefaultStyle.name
ScanStyles = True
End If
ElseIf oDefaultStyle.StyleLocation = kLibraryStyleLocation Then
If DefaultStyleDic.exists(oDefaultStyle.name) Then
'Style is not local, but is local to template, needs to be added
OutdatedStyles = OutdatedStyles & vbCrLf & oDefaultStyle.name
ScanStyles = False
End If
End If
Next oDefaultStyle
For Each oStandardStyle In oDrawDoc.StylesManager.StandardStyles
If oStandardStyle.StyleLocation <> kLibraryStyleLocation Then
If Not oStandardStyle.UpToDate Then 'Style is local and not up to date
OutdatedStyles = OutdatedStyles & vbCrLf & oStandardStyle.name
ScanStyles = False
End If
ElseIf oStandardStyle.StyleLocation = kLibraryStyleLocation Then
If StandardStyleDic.exists(oStandardStyle.name) Then
'Style is not local, but is local to template, needs to be added
OutdatedStyles = OutdatedStyles & vbCrLf & oStandardStyle.name
ScanStyles = False
End If
End If
Next oStandardStyle
'Check Standard style
If Not ScanStyles Then
If MsgBox("Les styles de dimensions suivant ne sont pas à jour." & vbCrLf & OutdatedStyles & vbCrLf & vbCrLf & "Voulez-vous mettre à jour les styles ?", vbYesNo + vbMsgBoxSetForeground, "ScanStyles") = vbYes Then
UpdateStyles StyleDic, StandardStyleDic, DefaultStyleDic
End If
End If
ScanStylesErr:
If Err Then
MsgBox "unexpected error: " & Err.Description, vbMsgBoxSetForeground, "ScanStyles"
Err.Clear
End If
End Function
ObjectDefaultStyles gives you the opportunity to change default style used when creating dimensions/annotations. StandardStyle Chose which ObjectDefaultStyle is active.
Cheers