Below code shows the procedure to modify the block definition for block reference. Code, prompts the user to select a block reference and changes its definition so that block reference becomes reference for new block definition,
[CommandMethod("chageBlock")]
public static void chageBlock()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityOptions options =
new PromptEntityOptions("nSelect block reference");
options.SetRejectMessage("nSelect only block reference");
options.AddAllowedClass(typeof(BlockReference), false);
PromptEntityResult acSSPrompt = ed.GetEntity(options);
if (acSSPrompt.Status != PromptStatus.OK)
return;
using (Transaction Tx = db.TransactionManager.StartTransaction())
{
//get the reference
BlockReference blockRef = Tx.GetObject(acSSPrompt.ObjectId,
OpenMode.ForRead) as BlockReference;
//block table record...
BlockTable blockTable = Tx.GetObject(db.BlockTableId,
OpenMode.ForRead) as BlockTable;
//set the test as new block if present
if (blockTable.Has("TEST"))
{
blockRef.UpgradeOpen();
//set the id of the test block
blockRef.BlockTableRecord = blockTable["TEST"];
}
Tx.Commit();
}
}

Leave a Reply to Gerrit van DiepenCancel reply