Use any of the Block object methods and properties to redefine a block. When you redefine a block, all the references to that block in the drawing are immediately updated to reflect the new definition.
Redefinition affects previous and future insertions of a block. Constant attributes are lost and replaced by any new constant attributes. Variable attributes remain unchanged, even if the new block has no attributes.
This example creates a block and adds a circle to the definition of the block. The block is then inserted into the drawing as a block reference. The circle in the block definition is updated, and the block reference is updated automatically.
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
}
}