Find Attribute Example (VBA)
This example shows how to find
an attribute on the selected entity.
'-----------------------------------------------
'
' Preconditions:
' (1)
Part or assembly is open.
' (2)
Something is selected.
' (3)
An attribute has been added to the selected entity
' (see
Create Atrribute Example (VB).
' (4)
Attribute definition has been created in a
' certain
format. See NOTES.
'
' Postconditions: None
'
' NOTES:
' (1)
Attribute definition is as follows:
'
' Name
= "TDE_XML_string"
'
' Parameter:
' Name
= "flange_len_dim_name"
' Type
= swParamTypeString
'
' Parameter:
' Name
= "flange_len_value"
' Type
= swParamTypeDouble
'
' (2)
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
Public Enum swAssociatedEntityStates_e
swIsEntityInvalid
= 0
swIsEntitySuppressed
= 1
swIsEntityAmbiguous
= 2
swIsEntityDeleted
= 3
End Enum
Const AttDefName As
String = "TDE_XML_string"
Const AttLenDimName As
String = "flange_len_dim_name"
Const AttLenValueName As
String = "flange_len_value"
Sub main()
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swSelMgr As
SldWorks.SelectionMgr
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
i As
Long
Dim
bRet As
Boolean
Set
swApp = CreateObject("SldWorks.Application")
Set
swModel = swApp.ActiveDoc
Set
swSelMgr = swModel.SelectionManager
Set
swEnt = swSelMgr.GetSelectedObject5(1)
Set
swAttDef = swApp.DefineAttribute(AttDefName)
bRet
= swAttDef.AddParameter(AttLenDimName,
swParamTypeString, 0#, 0)
bRet
= swAttDef.AddParameter(AttLenValueName,
swParamTypeDouble, 0#, 0)
bRet
= swAttDef.Register
'
Limit search for instances because an attribute
'
on the entity may not exist
While
swAtt Is Nothing And i < 300
Set
swAtt = swEnt.FindAttribute(swAttDef,
i)
i
= i + 1
Wend
Debug.Print
"File = " & swModel.GetPathName
If
Not swAtt Is Nothing Then
If
False = swAtt.GetEntityState(swIsEntityInvalid)
And _
False
= swAtt.GetEntityState(swIsEntitySuppressed)
And _
False
= swAtt.GetEntityState(swIsEntityAmbiguous)
And _
False
= swAtt.GetEntityState(swIsEntityDeleted)
Then
Set
swParamName = swAtt.GetParameter(AttLenDimName)
Set
swParamValue = swAtt.GetParameter(AttLenValueName)
Debug.Print
" "
& AttDefName & "(" & i - 1 & ") = "
& swAtt.GetName
Debug.Print
" Name
=
" & swParamName.GetStringValue
Debug.Print
" Value
= "
& swParamValue.GetDoubleValue
Else
Debug.Print
" Attribute
retrieved but problems exist."
End
If
Else
Debug.Print
" Attribute
not retrieved."
End
If
End Sub
'-------------------------------------------------------------