Create Attribute Example (VBA)
This example shows how to create an instance of attribute and display
that attribute in the FeatureManager design tree.
'-------------------------------------------------------------
'
' Preconditions:
' (1)
Part or assembly is open.
' (2)
Something is selected.
'
' Postconditions: An attribute is added to the selected
entity.
'
' NOTES:
' *
Attribute definition is as follows:
'
' Name
= "TDE_XML_string"
'
' Parameter:
' Name
= "flange_len_dim_name"
' Type
= swParamTypeString
'
' Parameter:
' Name
= "flange_len_value"
' Type
= swParamTypeDouble
'
' *
Attribute is not shown in FeatureManager design tree
' until
you rebuild, because attributes
' are
normally created hidden and refreshing the
' FeatureManager
design tree is an expensive operation.
'
' *
Attributes are allocated by name, and the names must be
' unique
across the model.
'
' *
The selected object must present the Entity interface.
'----------------------------------------------------------------
Option Explicit
Public Enum swParamType_e '
For use with AttributeDef::AddParameter, for example
swParamTypeDouble
= 0
swParamTypeString
= 1
swParamTypeInteger
= 2
swParamTypeDVector
= 3
End Enum
Public Enum swInConfigurationOpts_e
swThisConfiguration
= 1
swAllConfiguration
= 2
swSpecifyConfiguration
= 3
End Enum
Sub main()
' 1
= invisible
' 0
= visible
Const CreateVisible As
Long = 0
Const AttDefName As
String = "TDE_XML_string"
Const AttLenDimName As
String = "flange_len_dim_name"
Const AttLenValueName As
String = "flange_len_value"
Const AttRootName As
String = "TDE"
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swSelMgr As
SldWorks.SelectionMgr
Dim
swFeat As
Object
Dim
swEnt As
SldWorks.entity
Dim
swAttDef As
SldWorks.attributeDef
Dim
swAtt As
SldWorks.Attribute
Dim
swParamName As
SldWorks.Parameter
Dim
swParamValue As
SldWorks.Parameter
Dim
AttName As
String
Dim
i As
Long
Dim
bRet As
Boolean
Set
swApp = CreateObject("SldWorks.Application")
Set
swModel = swApp.ActiveDoc
Set
swSelMgr = swModel.SelectionManager
Set
swFeat = swSelMgr.GetSelectedObject5(1)
Set
swEnt = swFeat
Set
swAttDef = swApp.DefineAttribute(AttDefName)
bRet
= swAttDef.AddParameter(AttLenDimName,
swParamTypeString, 0#, 0)
bRet
= swAttDef.AddParameter(AttLenValueName,
swParamTypeDouble, 0#, 0)
bRet
= swAttDef.Register
While
swAtt Is Nothing
'
Get a unique attribute name
i
= i + 1
AttName
= AttRootName + Str(i)
Set
swAtt = swAttDef.CreateInstance5(swModel,
swEnt, AttName, _
CreateVisible,
swThisConfiguration)
Wend
Set
swParamName = swAtt.GetParameter(AttLenDimName)
Set
swParamValue = swAtt.GetParameter(AttLenValueName)
bRet
= swParamName.SetStringValue2(AttName
& " - " & AttLenDimName, swAllConfiguration, "")
bRet
= swParamValue.SetDoubleValue2(i
* 10, swAllConfiguration, "")
Debug.Print
"File = " & swModel.GetPathName
If
Not swAtt Is Nothing Then
Debug.Print
" "
& AttDefName & "(" & i - 1 & ") = "
& AttName
Else
Debug.Print
" Attribute
not created."
End
If
End Sub
'-------------------------------------------------------------