Files
Add or read attributes on document
Code
'Demonstration of storing information on a document using attributes.
'Preconditions: model is open
'Result: if no attribute called "test" exists, request parameter string value from user and _
create a hidden attribute containing that string parameter. If an attribute called "test" _
does exist, display a message box containing the string paraemter value
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAttDef As SldWorks.AttributeDef
Dim swAtt As SldWorks.Attribute
Dim swParam As SldWorks.Parameter
Dim swFeat As SldWorks.Feature
Dim strValue As String
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
'Determine if an attribute called "test" already exists
Set swFeat = swModel.FirstFeature
While Not swFeat Is Nothing
If swFeat.GetTypeName2 = "Attribute" Then
If swFeat.Name = "test" Then
'if it exists then get the contents of the parameter "StringParam", display _
it, and then end the macro
Set swAtt = swFeat.GetSpecificFeature2
Set swParam = swAtt.GetParameter("StringParam")
MsgBox "Attribute contains a string containing this text:" & vbCrLf & _
swParam.GetStringValue
End
End If
End If
Set swFeat = swFeat.GetNextFeature
Wend
'if "test" attribute did not exist, add it with a parameter called "StringParam" _
containing text specified by user
Set swAttDef = swApp.DefineAttribute("template")
strValue = InputBox("Attribute does not exist. Specify parameter value.")
If strValue = "" Then End
swAttDef.AddParameter "StringParam", swParamTypeString, Empty, 0
swAttDef.Register
Set swAtt = swAttDef.CreateInstance5(swModel, Nothing, "test", 1, swAllConfiguration)
Set swParam = swAtt.GetParameter("StringParam")
swParam.SetStringValue2 strValue, swAllConfiguration, Empty
End Sub
'An attribute is a user-defined container of custom variables stored on the SolidWorks _
model or on geometry by a programmer. The custom variables are called parameters. If _
stored on the geometry, they can be traversed. Attributes are saved with the document.
'Notes:
' -Do not use definition names containing the word "attribute"
' -The Options argument in IAttributeDefinition::CreateInstance5 controls whether the _
attribute is hidden. 0 = visible, 1 = hidden