Extract Attribute Information

You can extract attribute information from a drawing using the GetAttributes and GetConstantAttributes methods. The GetAttributes method returns an array of the attribute references attached to a block, along with their current values. The GetConstantAttributes method returns an array of constant attributes attached to the block or external reference. The attributes returned by this method are the constant attribute definitions, not attribute references.

You do not need template files to extract attribute information, and no attribute information files are created. Simply iterate the array of attribute references, using the TagString and TextString properties of the attribute -reference to examine the attribute information.

The TagString property represents the individual tag for the attribute reference. The TextString property contains the value for the attribute reference.

For more information on extracting attribute information, see “Extract Data from Block Attributes” in the User's Guide.

Get attribute reference information

This example creates a block and then adds an attribute to the block. The block is then inserted into the drawing. The attribute data is then returned and displayed using a message box. The attribute data is then updated for the block reference, and once again the attribute data is returned and displayed.

Sub Ch10_GettingAttributes()
	' Create the block
	Dim blockObj As AcadBlock
	Dim insertionPnt(0 To 2) As Double
	insertionPnt(0) = 0
	insertionPnt(1) = 0
	insertionPnt(2) = 0
	Set blockObj = ThisDrawing.Blocks.Add _
						(insertionPnt, "TESTBLOCK")

	' Define the attribute definition
	Dim attributeObj As AcadAttribute
	Dim height As Double
	Dim mode As Long
	Dim prompt As String
	Dim insertionPoint(0 To 2) As Double
	Dim tag As String
	Dim value As String
	height = 1#
	mode = acAttributeModeVerify
	prompt = "Attribute Prompt"
	insertionPoint(0) = 5
	insertionPoint(1) = 5
	insertionPoint(2) = 0
	tag = "Attribute Tag"
	value = "Attribute Value"

	' Create the attribute definition object on the block
	Set attributeObj = blockObj.AddAttribute _
						(height, mode, prompt, _
						insertionPoint, tag, value)

	' Insert the block
	Dim blockRefObj As AcadBlockReference
	insertionPnt(0) = 2
	insertionPnt(1) = 2

	insertionPnt(2) = 0
	Set blockRefObj = ThisDrawing.ModelSpace.InsertBlock _
					 (insertionPnt, "TESTBLOCK", 1, 1, 1, 0)

	' Get the attributes for the block reference
	Dim varAttributes As Variant
	varAttributes = blockRefObj.GetAttributes

	' Move the attribute tags and values into a
	' string to be displayed in a Msgbox
	Dim strAttributes As String
	strAttributes = ""
	Dim I As Integer
	For I = LBound(varAttributes) To UBound(varAttributes)
		strAttributes = strAttributes + "  Tag: " + _
					varAttributes(I).TagString + vbCrLf + _
					"   Value: " + varAttributes(I).textString
	MsgBox "The attributes for blockReference " + _
				 blockRefObj.Name & " are: " & vbCrLf _
				 & strAttributes

	' Change the value of the attribute
	' Note: There is no SetAttributes. Once you have the
	' variant array, you have the objects.
	' Changing them changes the objects in the drawing.
	varAttributes(0).textString = "NEW VALUE!"

	' Get the attributes again
	Dim newvarAttributes As Variant
	newvarAttributes = blockRefObj.GetAttributes

	' Again, display the tags and values
	strAttributes = ""
	For I = LBound(varAttributes) To UBound(varAttributes)
		strAttributes = strAttributes + "  Tag: " + _
					newvarAttributes(I).TagString + vbCrLf + _
					"   Value: " + newvarAttributes(I).textString
	MsgBox "The attributes for blockReference " & _
				blockRefObj.Name & " are: " & vbCrLf _
				& strAttributes
End Sub