By Wayne Brill
You may be having trouble setting a new ObjectDefaultsStyle in a Drawing. This could be because the ObjectDefaultsStyle can not be activated directly. It can via a DrawingStandardStyle however. Each DrawingStandardStyle has only one ObjectDefaultsStyle. When you activate a standard, you also set the active ObjectDefaultsStyle. These VBA and VB.NET examples create a new standard and a new ObjectDefaultsStyle for it. The Copy method needs to be used to create new standard and style.
VBA
Sub SetObjectDefaultsStyle()
Const kStandardName = "My Standard"
Const kObjDefaultsName = "My Defaults"
Dim oDoc As DrawingDocument
On Error Resume Next
Set oDoc = ThisApplication.ActiveDocument
Dim oStylesMgr As DrawingStylesManager
Set oStylesMgr = oDoc.StylesManager
Dim oStandard As DrawingStandardStyle
Set oStandard = oStylesMgr.StandardStyles _
.Item(kStandardName)
If oStandard Is Nothing Then
Set oStandard = oStylesMgr.StandardStyles _
.Item(1).Copy(kStandardName)
End If
' create object defaults
Dim oObjDefaults As ObjectDefaultsStyle
Set oObjDefaults = oStylesMgr. _
ObjectDefaultsStyles.Item(kObjDefaultsName)
If oObjDefaults Is Nothing Then
Set oObjDefaults = oStylesMgr. _
ObjectDefaultsStyles.Item(1). _
Copy(kObjDefaultsName)
End If
oStandard.ActiveObjectDefaults = oObjDefaults
' activate standard
oStylesMgr.ActiveStandardStyle = oStandard
End Sub
VB.NET
Public Class Form1
Dim m_inventorApp As Inventor.Application _
= Nothing
Private Sub Button1_Click(ByVal sender As _
System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
' Get an active instance of Inventor
Try
m_inventorApp = System.Runtime. _
InteropServices.Marshal. _
GetActiveObject("Inventor.Application")
Catch 'Inventor not started
System.Windows.Forms.MessageBox. _
Show("Start an Inventor session")
Exit Sub
End Try
'Call the Sub
SetObjectDefaultsStyle()
End Sub
Public Sub SetObjectDefaultsStyle()
Try
Const kStandardName As String = _
"MyStandard"
Const kObjDefaultsName As String = _
"My Defaults"
Dim oDoc As DrawingDocument = _
m_inventorApp.ActiveDocument
Dim oStylesMgr As DrawingStylesManager = _
oDoc.StylesManager
Dim oStandard As DrawingStandardStyle = _
Nothing
For Each style As DrawingStandardStyle _
In oStylesMgr.StandardStyles
If String.Compare _
(style.Name, kStandardName) = 0 Then
oStandard = style
Exit For
End If
Next
If oStandard Is Nothing Then
oStandard = oStylesMgr. _
StandardStyles(1)._Copy(kStandardName)
End If
Dim oObjDefaults As _
ObjectDefaultsStyle = Nothing
For Each DefaultsStyle As _
ObjectDefaultsStyle _
In oStylesMgr.ObjectDefaultsStyles
If String.Compare _
(DefaultsStyle.Name, kObjDefaultsName) _
= 0 Then
oObjDefaults = DefaultsStyle
Exit For
End If
Next
If oObjDefaults Is Nothing Then
oObjDefaults = oStylesMgr. _
ObjectDefaultsStyles(1).Copy _
(kObjDefaultsName)
End If
oStandard.ActiveObjectDefaults = _
oObjDefaults
oStylesMgr.ActiveStandardStyle = _
oStandard
Catch
End Try
End Sub
End Class