属性定義および属性参照を作成する(.NET)

属性定義を作成するには、まず、AppendEntity メソッドを使用して AttributeDefinition オブジェクトを作成し、BlockTableRecord オブジェクトに属性定義を作成する必要があります。属性定義を定義する場合は、属性テキストの高さ、属性モード、プロンプト、タグ文字列、挿入点、既定の属性値を指定する必要があります。

属性定義のモードは、以下のプロパティを使ってコントロールします。

Constant
挿入するブロックの属性に、固定値を設定します。
Invisible
ブロックを挿入したときに、属性値は表示も印刷もされません。ATTDISP[属性表示]コマンドは、すべての属性の非表示モードよりも優先されます。
IsMTextAttributeDefinition
属性値に複数の文字行を含めるかどうかを指定することができます。このオプションを選択すると、属性の境界幅を指定できます。
LockPositionInBlock
ブロック参照内で属性の位置を固定します。ロックを解除すると、グリップ編集を使用して、残りのブロックを基準に属性を移動したり、マルチ テキスト属性のサイズを変更することができます。
[プリセット(P)]
プリセット属性を持つブロックを挿入したときに、属性が既定値に設定されます。
Verifiable
ブロックを挿入するときに、属性値が正しいかどうかを確認するためのプロンプトが表示されます。

プロンプト文字列は、属性を含むブロックが挿入され、Prompt プロパティを使用して設定されている場合に表示されます。属性のデフォルト値は、TextString プロパティで設定します。Constant プロパティが True に設定されている場合、属性定義を含むブロックが挿入されると、新しい値の入力を求めるプロンプトは無効になります。

タグ文字列は属性の各対象を識別し、Tag プロパティを使って割り当てられます。スペースと感嘆符以外の任意の文字を使用することができます。小文字は自動的に大文字に変更されます。

ブロック内の属性定義の作成後は、INSERT コマンドを使ってブロックを挿入するたびに、定数として定義されていない属性参照ごとに異なる値を指定できます。BlockReference オブジェクトを作成すると、BlockTableRecord オブジェクトで定義する属性は、AppendAttribute メソッドを使用して BlockReference オブジェクトに追加するまで、オブジェクトには含まれません。

属性を BlockReference オブジェクトに追加する前に、SetAttributeFromBlock メソッドを使って AttributeDefinition オブジェクトのプロパティを AttributeReference オブジェクトにコピーします。HasAttributeDefinitions プロパティをブロック テーブル レコードに使用し、そのレコードに属性定義が含まれているかを確認できます。

モデル空間またはペーパー空間に関して作成された属性定義は、ブロックには関連付けられません。

属性定義を定義する

次の例では、ブロックを作成し、そのブロックに属性を追加します。

C# の例

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
    }
}

属性を持つブロックを挿入する

この例では、属性を持つブロックを作成し、現在のスペースにそのブロックを挿入します。

C# の例

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
    }
}