Using the Add Attribute Method

Use the AddAttribute method of a PaperSpace, ModelSpace, or Block object to create a new Attribute object. This attribute definition specifies the corresponding AttributeReference's characteristics. The most important characteristics are passed as parameters to this method, including Prompt to display when inserting a block, Tag to identify the attribute, and Value (the default) to assign the attribute. This method has the following syntax:

Set AttributeObject = Object.AddAttribute(Height, Mode, Prompt, _ InsertionPoint, Tag, Value)

Table 13-8 explains this method's attributes.

Table 13-8. The AddAttribute Method's Parameters


Data Type




The attribute definition's text size in the current drawing units.



How the Attribute object behaves when a block containing this attribute definition is inserted. Table 13-9 lists this parameter's values.



The method displays this prompt when a block containing this attribute definition is inserted. If null, the method displays the Tag parameter instead.



A three-element array of doubles that specifies the attribute's location in the block or drawing. It is a 3-D WCS coordinate.



An identifier used to access a specific attribute. It may contain any characters except spaces and exclamation points. AutoCAD converts it to uppercase.



The attribute's default value.

AutoCAD provides several AcAttributeMode constants for the Mode parameter, which you can specify in any combination. Table 13-9 lists these values.

Table 13-9. The Mode Parameter's Values






The default mode, in which none of the other modes is applied.



When the block is inserted, the attribute's values aren't visible. The ATTDISP system variable overrides this mode setting.



Each inserted block's attribute values are fixed. AutoCAD does not prompt for the attribute.



When you insert the block, AutoCAD asks you to verify that the attribute value is correct.



AutoCAD inserts the block with its default attribute values. You can't edit these values.

The user can establish a preferred global mode for new attributes. The AFLAGS system variable stores this preferred global mode. You can access and set it using the GetVariable and SetVariable methods, which Appendix C discusses.

The following example creates a simple block containing an attribute of each type and then interactively inserts it:

Public Sub TestAddAttribute() Dim dblOrigin(2) As Double Dim dblEnt(2) As Double Dim dblHeight As Double Dim lngMode As Long Dim strTag As String Dim strPrompt As String Dim strValue As String Dim objBlock As AcadBlock Dim objEnt As AcadEntity

'' create the block dblOrigin(0) = 0: dblOrigin(1) = 0: dblOrigin(2) = 0

Set objBlock = ThisDrawing.Blocks.Add(dblOrigin, "Affirmations")

'' delete existing entities (in case we've run before) For Each objEnt In objBlock objEnt.Delete


'' create an ellipse in the block dblEnt(0) = 4: dblEnt(1) = 0: dblEnt(2) = 0 objBlock.AddEllipse dblOrigin, dblEnt, 0.5

'' set the height for all attributes dblHeight = 0.25

'' create a regular attribute lngMode = acAttributeModeNormal strTag = "Regular" strPrompt = "Enter a value" strValue = "I'm regular" dblEnt(1) = 1

objBlock.AddAttribute dblHeight, lngMode, strPrompt, dblEnt, strTag, strValue

'' create an invisible attribute lngMode = acAttributeModeInvisible strTag = "Invisible" strPrompt = "Enter a hidden value" strValue = "I'm invisible" dblEnt(1) = 0.5

objBlock.AddAttribute dblHeight, lngMode, strPrompt, dblEnt, strTag, _ strValue

'' create a constant attribute lngMode = acAttributeModeConstant strTag = "Constant" strPrompt = "Don't bother" strValue = "I'm set" dblEnt(1) = 0

objBlock.AddAttribute dblHeight, lngMode, strPrompt, dblEnt, strTag, _ strValue

'' create a verify attribute lngMode = acAttributeModeVerify strTag = "Verify"

strPrompt = "Enter an important value" strValue = "I'm important" dblEnt(1) = -0.5

objBlock.AddAttribute dblHeight, lngMode, strPrompt, dblEnt, strTag, _ strValue

'' create a preset attribute lngMode = acAttributeModePreset strTag = "Preset" strPrompt = "No question" strValue = "I've got values" dblEnt(1) = -1

objBlock.AddAttribute dblHeight, lngMode, strPrompt, dblEnt, strTag, _ strValue

'' now insert block interactively using sendcommand ThisDrawing.SendCommand "._-insert" & vbCr & "Affirmations" & vbCr End Sub

Neither constant nor preset attributes prompt for values. The Verify attribute prompt repeats. Only four attributes are visible in the drawing, as shown in Figure 13-5. The Invisible attribute is present but hidden.

I'm set VIP

I've got values

Figure 13-5. Viewing the code output

If, after you perform an insertion, you run the DDATTE command to review these values in the Edit Attributes dialog box, four of them are editable, as shown in Figure 13-6. The Constant attribute is not available, but you can change both the invisible values and the preset values.

Figure 13-6. Editing the four attributes

In AutoCAD 2005 and 2006, after you perform the insertion, you can double-click the block to invoke the Enhanced Attribute Editor, as shown in Figure 13-7. You can then review the attribute values and make changes appropriately.

Figure 13-7. Editing attributes in AutoCAD 2005/2006

Was this article helpful?

0 0

Post a comment