Hi.
I'm sending the DWG with the bug. The code I'm using is similar with the code below. I tried to change the block definition of my ordinary blocks to my TEST block and I had the same problem that I reported.
CommandMethod("CBLOCK", CommandFlags.Modal | CommandFlags.NoPaperSpace)]
public static void ChageBlock()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
var pso = new PromptSelectionOptions
{
MessageForAdding = "\nSelect block reference(s)",
AllowDuplicates = false,
AllowSubSelections = false,
ForceSubSelections = false,
RejectObjectsOnLockedLayers = true,
RejectObjectsFromNonCurrentSpace = true,
RejectPaperspaceViewport = true,
};
var selectionResult = ed.GetSelection(pso, new SelectionFilter(new[] { new TypedValue((int)DxfCode.Start, "INSERT"), }));
var options = new PromptEntityOptions("\nSelect block reference");
options.SetRejectMessage("\nSelect only block reference");
options.AddAllowedClass(typeof(BlockReference), false);
if (selectionResult.Status!= PromptStatus.OK)
return;
using (var tx = db.TransactionManager.StartTransaction())
{
//get references
var blkRefs = new ObjectIdCollection(selectionResult.Value.GetObjectIds())
.Cast<ObjectId>()
.Select(objId => tx.GetObject(objId, OpenMode.ForRead))
.OfType<BlockReference>()
.Where(x => x.BlockTableRecord.IsValid)
.ToArray();
//block table record
var blks = ((BlockTable)tx.GetObject(db.BlockTableId, OpenMode.ForRead))
.Cast<ObjectId>()
.Select(objId => tx.GetObject(objId, OpenMode.ForRead))
.OfType<BlockTableRecord>()
.Where(bk => !bk.IsFromExternalReference && (bk.Name == "*Model_Space" || !bk.Name.StartsWith("*")))
.Distinct()
.ToArray();
//Get the target block
var block = blks.First(bk => bk.Name == "TEST");
foreach (var blockReference in blkRefs)
{
blockReference.UpgradeOpen();
//set the id of the test block
blockReference.BlockTableRecord = block.ObjectId;
}
tx.Commit();
}
}