<?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.
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#
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


Leave a Reply