Option Strict Off
Option Explicit On
Friend Class frmMain
	Inherits System.Windows.Forms.Form
	Private objApp As Object
	Private objObject As Object
	Private objAttrSet As Object
	Dim objAttribute As Object
	Private AlreadyExists As Boolean
	
	'
	' Purpose: This subroutine applies or replaces a dynamic attribute on
	'          the selected object.  The dynamic attribute is a single
	'          text attribute.
	'
	' Arguments: No arguments are supplied.  The input string is obtained
	'            from the text box.
	'
	Private Sub cmdApply_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdApply.Click
		' Make sure some text has been keyed in for the attribute.
		If txtAttribute.Text = "" Then
			MsgBox("You must enter some text.", MsgBoxStyle.OKOnly + MsgBoxStyle.Information, "Attribute Sample")
			Exit Sub
		End If
		
		' Make sure there is a single item in the select set and get it.
        If objApp.ActiveDocument.SelectSet.Count <> 1 Then
            MsgBox("Select a single object to add/edit attributes.", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Attribute Sample")
            Exit Sub
        Else
            ' Set a reference to the selected object.  If it fails then an
            ' invalid object has been selected.  Most Solid Edge persistent objects support
            ' dynamic attributes, although there may be some that do not.  You can see
            ' for yourself whether an object supports dynamic attributes by setting a watch
            ' on the object in debug, and then seeing if the object has an AttributeSets collection.
            On Error Resume Next
            objObject = objApp.ActiveDocument.SelectSet(1)
            If Err.Number Then
                MsgBox("You must select a top level part/subassembly.", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Attribute Sample")
                Err.Clear()
                Exit Sub
            End If
        End If
		
        If Not objObject.IsAttributeSetPresent("UserDefined") Then
            ' Create the attribute set called 'UserDefined'.
            objAttrSet = objObject.AttributeSets.Add("UserDefined")

            ' Add the string attribute called 'StringAttribute'.  This sample
            ' just adds a single attribute to the attribute set, but any number
            ' of attributes can be added and they can be of many different types
            ' including: seByte, seInteger, seLong, seSingle, seDouble, seCurrency
            ' seDate, seStringANSI, seStringUnicode, or seBoolean.  These constants
            ' are defined in the Solid Edge Constants type library
            objAttrSet.Add("StringAttribute", SolidEdgeConstants.AttributeTypeConstants.seStringANSI)
            objAttrSet.StringAttribute = txtAttribute.Text
        Else
            ' Edit the existing attribute.
            objAttrSet.StringAttribute = txtAttribute.Text
        End If
	End Sub
	
	Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click
		End
	End Sub
	
	Private Sub cmdReadAttribute_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdReadAttribute.Click
		' Clear out the text field.
		txtAttribute.Text = ""
		
		' Make sure there is a single item in the select set and get it.
        If objApp.ActiveDocument.SelectSet.Count <> 1 Then
            MsgBox("Select a single object to add/edit attributes.")
            Exit Sub
        Else
            ' Set a reference to the selected object.  If it fails then an
            ' invalid object has been selected.  (The only case I know of
            ' is a part that is part of a subassembly.)
            On Error Resume Next
            objObject = objApp.ActiveDocument.SelectSet(1)
            If Err.Number Then
                MsgBox("You must select a Feature or top level part/subassembly.", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Attribute Sample")
                Exit Sub
            End If
        End If

        If objObject.IsAttributeSetPresent("UserDefined") Then
            ' Display the value of the attribute in the text box.
            objAttrSet = objObject.AttributeSets("UserDefined")
            objAttribute = objAttrSet("StringAttribute")
            txtAttribute.Text = objAttribute.Value
        Else
            MsgBox("The selected object does not have the dynamic attribute.", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Attribute Sample")
        End If
	End Sub
	
	Private Sub frmMain_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		' Connect to Solid Edge.
		On Error Resume Next
		objApp = GetObject( , "SolidEdge.Application")
		If Err.Number Then
			MsgBox("Solid Edge must be running", MsgBoxStyle.OKOnly + MsgBoxStyle.Information, "Attribute Sample")
            objApp = Nothing
			End
		End If
		On Error GoTo 0
	End Sub
End Class