ブロックを再定義する(.NET)

任意の Block オブジェクトのメソッドおよびプロパティを使用して、ブロックを再定義します。ブロックを再定義すると、図面内のそのブロックへのすべての参照がすぐに更新され、新しい定義を反映します。

再定義は、再定義以前および以降のすべてのブロックの挿入に影響します。定数属性は失われ、新しい定数属性に置き換えられます。変数属性は、新しいブロックに属性がない場合でも変更されません。

ブロック定義中のオブジェクトを再定義する

次の例では、ブロックを定義し、その定義に円を追加します。次に、ブロック参照としてブロックを図面に挿入します。ブロック定義にある円を更新すると、ブロック参照が自動的に更新されます。

C# の例

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

[CommandMethod("RedefiningABlock")]
public void RedefiningABlock()
{
    // 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("CircleBlock"))
        {
            using (BlockTableRecord acBlkTblRec = new BlockTableRecord())
            {
                acBlkTblRec.Name = "CircleBlock";

                // 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);

                    acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite);
                    acBlkTbl.Add(acBlkTblRec);
                    acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);

                    // Insert the block into the current space
                    using (BlockReference acBlkRef = new BlockReference(new Point3d(0, 0, 0), acBlkTblRec.Id))
                    {
                        BlockTableRecord acModelSpace;
                        acModelSpace = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                        acModelSpace.AppendEntity(acBlkRef);
                        acTrans.AddNewlyCreatedDBObject(acBlkRef, true);

                        Application.ShowAlertDialog("CircleBlock has been created.");
                    }
                }
            }
        }
        else
        {
            // Redefine the block if it exists
            BlockTableRecord acBlkTblRec =
                acTrans.GetObject(acBlkTbl["CircleBlock"], OpenMode.ForWrite) as BlockTableRecord;

            // Step through each object in the block table record
            foreach (ObjectId objID in acBlkTblRec)
            {
                DBObject dbObj = acTrans.GetObject(objID, OpenMode.ForRead) as DBObject;

                // Revise the circle in the block
                if (dbObj is Circle)
                {
                    Circle acCirc = dbObj as Circle;

                    acTrans.GetObject(objID, OpenMode.ForWrite);
                    acCirc.Radius = acCirc.Radius * 2;
                }
            }

            // Update existing block references
            foreach (ObjectId objID in acBlkTblRec.GetBlockReferenceIds(false, true))
            {
                BlockReference acBlkRef = acTrans.GetObject(objID, OpenMode.ForWrite) as BlockReference;
                acBlkRef.RecordGraphicsModified(true);
            }

            Application.ShowAlertDialog("CircleBlock has been revised.");
        }

        // Save the new object to the database
        acTrans.Commit();

        // Dispose of the transaction
    }
}