To create an attribute definition, first you must create an AttributeDefinition object and then append it to a BlockTableRecord object using the AppendEntity method. When defining an attribute definition, you should specify the height of the attribute text, the attribute modes, a prompt and tag string, the insertion point, and the default attribute value.
The modes for the attribute definition are controlled with the following properties:
The prompt string appears when a block containing the attribute is inserted and is set using the Prompt property. The default value for the attribute is set with the TextString property. If the Constant property is set to True, prompting for a new value is disabled when the block with the attribute definition is inserted.
The tag string identifies each occurrence of the attribute and is assigned with the Tag property. You can use any characters except spaces or exclamation points. AutoCAD changes lowercase letters to uppercase.
Once the attribute definition is defined in a block, whenever the block is inserted using the INSERT command, you can specify a different value for each attribute reference that is not defined as being constant. When a BlockReference object is created, the object does not contain any attributes that are defined in the BlockTableRecord object until they are appended to the BlockReference object using the AppendAttribute method.
Before appending the attribute to the BlockReference object, use the SetAttributeFromBlock method to copy the properties of an AttributeDefinition object to an AttributeReference object. The HasAttributeDefinitions property can be used on a block table record to see if it contains attribute definitions.
Attribute definitions created on model space or paper space are not considered attached to any given block.
This example creates a block and then adds an attribute to the block.
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
[CommandMethod("AddingAttributeToABlock")]
public void AddingAttributeToABlock()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
if (!acBlkTbl.Has("CircleBlockWithAttributes"))
{
using (BlockTableRecord acBlkTblRec = new BlockTableRecord())
{
acBlkTblRec.Name = "CircleBlockWithAttributes";
// Set the insertion point for the block
acBlkTblRec.Origin = new Point3d(0, 0, 0);
// Add a circle to the block
using (Circle acCirc = new Circle())
{
acCirc.Center = new Point3d(0, 0, 0);
acCirc.Radius = 2;
acBlkTblRec.AppendEntity(acCirc);
// Add an attribute definition to the block
using (AttributeDefinition acAttDef = new AttributeDefinition())
{
acAttDef.Position = new Point3d(0, 0, 0);
acAttDef.Verifiable = true;
acAttDef.Prompt = "Door #: ";
acAttDef.Tag = "Door#";
acAttDef.TextString = "DXX";
acAttDef.Height = 1;
acAttDef.Justify = AttachmentPoint.MiddleCenter;
acBlkTblRec.AppendEntity(acAttDef);
acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite);
acBlkTbl.Add(acBlkTblRec);
acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);
}
}
}
}
// Save the new object to the database
acTrans.Commit();
// Dispose of the transaction
}
}
This example creates a block with attributes, and then inserts the block in the current space.
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
[CommandMethod("InsertingBlockWithAnAttribute")]
public void InsertingBlockWithAnAttribute()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
ObjectId blkRecId = ObjectId.Null;
if (!acBlkTbl.Has("CircleBlockWithAttributes"))
{
using (BlockTableRecord acBlkTblRec = new BlockTableRecord())
{
acBlkTblRec.Name = "CircleBlockWithAttributes";
// Set the insertion point for the block
acBlkTblRec.Origin = new Point3d(0, 0, 0);
// Add a circle to the block
using (Circle acCirc = new Circle())
{
acCirc.Center = new Point3d(0, 0, 0);
acCirc.Radius = 2;
acBlkTblRec.AppendEntity(acCirc);
// Add an attribute definition to the block
using (AttributeDefinition acAttDef = new AttributeDefinition())
{
acAttDef.Position = new Point3d(0, 0, 0);
acAttDef.Prompt = "Door #: ";
acAttDef.Tag = "Door#";
acAttDef.TextString = "DXX";
acAttDef.Height = 1;
acAttDef.Justify = AttachmentPoint.MiddleCenter;
acBlkTblRec.AppendEntity(acAttDef);
acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite);
acBlkTbl.Add(acBlkTblRec);
acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);
}
}
blkRecId = acBlkTblRec.Id;
}
}
else
{
blkRecId = acBlkTbl["CircleBlockWithAttributes"];
}
// Insert the block into the current space
if (blkRecId != ObjectId.Null)
{
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord;
// Create and insert the new block reference
using (BlockReference acBlkRef = new BlockReference(new Point3d(2, 2, 0), blkRecId))
{
BlockTableRecord acCurSpaceBlkTblRec;
acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
acCurSpaceBlkTblRec.AppendEntity(acBlkRef);
acTrans.AddNewlyCreatedDBObject(acBlkRef, true);
// Verify block table record has attribute definitions associated with it
if (acBlkTblRec.HasAttributeDefinitions)
{
// Add attributes from the block table record
foreach (ObjectId objID in acBlkTblRec)
{
DBObject dbObj = acTrans.GetObject(objID, OpenMode.ForRead) as DBObject;
if (dbObj is AttributeDefinition)
{
AttributeDefinition acAtt = dbObj as AttributeDefinition;
if (!acAtt.Constant)
{
using (AttributeReference acAttRef = new AttributeReference())
{
acAttRef.SetAttributeFromBlock(acAtt, acBlkRef.BlockTransform);
acAttRef.Position = acAtt.Position.TransformBy(acBlkRef.BlockTransform);
acAttRef.TextString = acAtt.TextString;
acBlkRef.AttributeCollection.AppendAttribute(acAttRef);
acTrans.AddNewlyCreatedDBObject(acAttRef, true);
}
}
}
}
}
}
}
// Save the new object to the database
acTrans.Commit();
// Dispose of the transaction
}
}