Replace Object Defaults Style

<?xml encoding=”UTF-8″>By Adam Nagy

In the Style and Standard Editor dialog you can do a Replace Style on an Object Defaults Style. This will go through all the standards and check if any of them uses the selected style as the Active Object Defaults. If so, then it will activate the other Object Defaults Style that you provide in the Replace Style dialog. Once it’s not used anywhere you may as well purge it / delete it.

ReplaceStyle

You can achieve the above using the API as well.

VBA

Sub ReplaceObjectDefaultsStyle( _
ByVal stylesMgr As DrawingStylesManager, _
ByVal replaceStyle As ObjectDefaultsStyle, _
ByVal withStyle As ObjectDefaultsStyle, _
purge As Boolean)
' Go through each standard and replace the
' Active Defaults with withStyle if currently
' replaceStyle is being used as Active Defaults
Dim oDwgStd As DrawingStandardStyle
For Each oDwgStd In stylesMgr.StandardStyles
If oDwgStd.ActiveObjectDefaults Is replaceStyle Then
oDwgStd.ActiveObjectDefaults = withStyle
End If
Next
' If it is (or is also) local then we can delete it
' i.e. not library only
If purge And replaceStyle.StyleLocation <> kLibraryStyleLocation _
Then
replaceStyle.Delete
End If
End Sub
Sub TestReplace()
Dim oDwg As DrawingDocument
Set oDwg = ThisApplication.ActiveDocument
Dim oStlMgr As DrawingStylesManager
Set oStlMgr = oDwg.StylesManager
' The style we want to replace
Dim oDefStyle1 As ObjectDefaultsStyle
Set oDefStyle1 = oStlMgr.ObjectDefaultsStyles(3)
' The style we want to replace the active style with
Dim oDefStyle2 As ObjectDefaultsStyle
Set oDefStyle2 = oStlMgr.ObjectDefaultsStyles(1)
ReplaceObjectDefaultsStyle oStlMgr, oDefStyle1, oDefStyle2, True
End Sub

C#

static void ReplaceObjectDefaultsStyle(

  DrawingStylesManager stylesManager,

  ObjectDefaultsStyle replaceStyle,

  ObjectDefaultsStyle withStyle,

  bool purge)

{

  foreach (DrawingStandardStyle standardStyle

    in stylesManager.StandardStyles)

  {

    if (standardStyle.ActiveObjectDefaults == replaceStyle)

      standardStyle.ActiveObjectDefaults = withStyle;

  }

 

  if (purge && replaceStyle.StyleLocation !=

    StyleLocationEnum.kLibraryStyleLocation)

    replaceStyle.Delete();

}

 

static private void test()

{

  Application app = (Application)

    System.Runtime.InteropServices.Marshal.

    GetActiveObject(“Inventor.Application”);

 

  DrawingDocument dwg = (DrawingDocument)app.ActiveDocument;

  DrawingStylesManager stylesManager = dwg.StylesManager;

  ObjectDefaultsStyle style1 = stylesManager.ObjectDefaultsStyles[1];

  ObjectDefaultsStyle style2 = stylesManager.ObjectDefaultsStyles[2];

  ReplaceObjectDefaultsStyle(stylesManager, style1, style2, true);

}

 

And you could also do it from an iLogic Rule – see Convert VBA to .NET / iLogic

Sub Main()
Dim oDwg As DrawingDocument
oDwg = ThisApplication.ActiveDocument
Dim oStlMgr As DrawingStylesManager
oStlMgr = oDwg.StylesManager
' The style we want to replace
Dim oDefStyle1 As ObjectDefaultsStyle
oDefStyle1 = oStlMgr.ObjectDefaultsStyles(3)
' The style we want to replace the active style with
Dim oDefStyle2 As ObjectDefaultsStyle
oDefStyle2 = oStlMgr.ObjectDefaultsStyles(1)
ReplaceObjectDefaultsStyle(oStlMgr, oDefStyle1, oDefStyle2, True)
End Sub
Sub ReplaceObjectDefaultsStyle(
stylesMgr As DrawingStylesManager,
replaceStyle As ObjectDefaultsStyle,
withStyle As ObjectDefaultsStyle,
purge As Boolean)
' Go through each standard and replace the
' Active Defaults with withStyle if currently
' replaceStyle is being used as Active Defaults
Dim oDwgStd As DrawingStandardStyle
For Each oDwgStd In stylesMgr.StandardStyles
If oDwgStd.ActiveObjectDefaults Is replaceStyle Then
oDwgStd.ActiveObjectDefaults = withStyle
End If
Next
' If it is (or is also) local then we can delete it
' i.e. not library only
If purge And replaceStyle.StyleLocation <> kLibraryStyleLocation _
Then
replaceStyle.Delete
End If
End Sub

Comments

6 responses to “Replace Object Defaults Style”

  1. Denis Becker Avatar
    Denis Becker

    I test it in Inventor 2015 SP2.
    I get a failure.
    run-time error ‘-2147467259(80004005)’:
    Method ‘ActiveObjectDefaults’ of object ‘DrwaingStandrdStyle’ failed
    What can I do?
    Please help.

  2. Hi Denis,
    Which programming language?
    Can you reproduce it with any drawing?
    Can you do it through the UI?
    Are you sure the program selects the correct ObjectDefaultsStyles?
    Are you running the exact same code shown above or you modified something in it?
    Best might be if you logged this on the Inventor Customization forum and provide as much info as you can.
    Cheers,
    Adam

  3. Denis Becker Avatar
    Denis Becker

    I test the VBA-Code with Inventor his own VBA-editor.
    The code was not modified.
    How can I test witch ObjectDefaultsStyles is selected?
    I’m not a programmer. It is only my hobby.
    Thanks

  4. Both these lines select specific styles you want to work with:
    Set oDefStyle1 = oStlMgr.ObjectDefaultsStyles(3)
    Set oDefStyle2 = oStlMgr.ObjectDefaultsStyles(1)
    Did you debug into the code to see what they select? Maybe you have to modify the values…

  5. Denis Becker Avatar
    Denis Becker

    its were false numbers.
    Thanks for the help

  6. Denis Becker Avatar
    Denis Becker

    What do you think about this code?
    It overwrite all object styles with the active style from standard sytle.
    Sub ReplaceObjectDefaultsStyle( _
    ByVal stylesMgr As DrawingStylesManager, _
    ByVal replaceStyle As ObjectDefaultsStyle, _
    ByVal withStyle As ObjectDefaultsStyle, _
    purge As Boolean)
    ‘ Go through each standard and replace the
    ‘ Active Defaults with withStyle if currently
    ‘ replaceStyle is being used as Active Defaults
    Dim oDwgStd As DrawingStandardStyle
    For Each oDwgStd In stylesMgr.StandardStyles
    If oDwgStd.ActiveObjectDefaults Is replaceStyle Then
    oDwgStd.ActiveObjectDefaults = withStyle
    End If
    Next
    ‘ If it is (or is also) local then we can delete it
    ‘ i.e. not library only
    If purge And replaceStyle.StyleLocation <> kLibraryStyleLocation _
    Then
    replaceStyle.Delete
    End If
    End Sub
    Sub Stile_ersetzen()
    Dim oDwg As DrawingDocument
    Set oDwg = ThisApplication.ActiveDocument
    Dim oStlMgr As DrawingStylesManager
    Set oStlMgr = oDwg.StylesManager
    Dim aStSty As DrawingStandardStyle
    Set aStSty = oDwg.StylesManager.ActiveStandardStyle
    ‘ The style we want to replace the active style with
    Dim oDefStyleAktive As ObjectDefaultsStyle
    Set oDefStyleAktive = aStSty.ActiveObjectDefaults
    Dim StCount As ObjectDefaultsStylesEnumerator
    Set StCount = oStlMgr.ObjectDefaultsStyles
    Debug.Print StCount.Count
    For i = 1 To StCount.Count
    On Error Resume Next
    ‘ The style we want to replace
    Dim oDefStyleX As ObjectDefaultsStyle
    Set oDefStyleX = oStlMgr.ObjectDefaultsStyles(i)
    Debug.Print i
    ReplaceObjectDefaultsStyle oStlMgr, oDefStyleX, oDefStyleAktive, False
    If Err Then fehler = True
    Next
    End Sub

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading