Import Drawing Style

Import Drawing Style

j_weber
Mentor Mentor
1,357 Views
6 Replies
Message 1 of 7

Import Drawing Style

j_weber
Mentor
Mentor

Hi everybody

 

I have a question about import a drawing style in an aktive drawing. 

 

I have on my local harddrive a revisiontable style. Now I want import the style in the drawings styles . 

I know I can do it in the style manager with the import button. 

But I want import it with vba. After the import I must change the default style to the new imported style. 

This step works fine. 

 

Is it possible to import a style from the local hard drive or to copy one specific style from a template file to the active drawing ? 

 

thanks and greetz

 

Jörg




Jörg Weber
CAD Systemtechniker für AutoCAD, Inventor, Vault





0 Likes
1,358 Views
6 Replies
Replies (6)
Message 2 of 7

yan.gauthier
Advocate
Advocate

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

0 Likes
Message 3 of 7

j_weber
Mentor
Mentor

Hallo @yan.gauthier 

 

oh my gush, what a code. 

 

I must say, I'm not a programmer but I ask for my college how is a programmer. 

But he is not so often in the board. 

I give him the code and hope he understand it

 

Thanks a lot for your help




Jörg Weber
CAD Systemtechniker für AutoCAD, Inventor, Vault





0 Likes
Message 4 of 7

bradeneuropeArthur
Mentor
Mentor

You could use the Style Library manager from Autodesk for this!

This can be found under START AUTODESK INVENTOR TOOLS >>>>

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 5 of 7

yan.gauthier
Advocate
Advocate

The way my code works is that user need to open a form used to tie our drawings with our ERP's SQL database. On the form.activate, I execute this piece of code to ensure this drawing styles are up to date and not user dependent.

 

This ensure that even a 2 years old drawing reused on a new project will use up to date standards. I have also a way to update TitleBlocks so any drawings printed for production looks as if it was recently made.

0 Likes
Message 6 of 7

j_weber
Mentor
Mentor

Hi @bradeneuropeArthur 

 

I know that I can do it with the Styles Manager, but we want to have a lillte add-in for our customer how can import the style, change the default and change the border on the fly. Thats the reason why for my question. 

Changing the border works well and I found a way to change the objectdefault. The last thing I must find out is to import the style form a local folder to the styles manager




Jörg Weber
CAD Systemtechniker für AutoCAD, Inventor, Vault





0 Likes
Message 7 of 7

yan.gauthier
Advocate
Advocate

For how to save to style library, in my code, where i ConvertToLocal or UpdateFromGlobal, you may also use the SaveToGlobal method.

To add a new style that is not from the library, you must use the Style.Copy method and then change each properties to match your external style (style.Copy creates a copy on the same document as the style used to create the copy).

 

My suggestion is to save to global the styles from your base document and then to convert to local those style in your target document.

 

Best Regards, 

0 Likes