Hi Hallex,
I think I found what problem is in my case.
Probably I should mention on the beginning that I need to also change BlockReference.BlockTableRecord once a new BlockTable is created.
I modified your first example and at the end I added line to change BlockReference.
Could you please try create block with some lines and insert at least two blocks into drawing and try run modified CopyXBref command on one block.
When I run REGEN command lines change colour in both (old and new) blocks
Thanks for your examples.
Richard.
[CommandMethod("CopyXBref")]
public void testCopyBlockReference()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
PromptEntityOptions peo = new PromptEntityOptions("\nSelect main block instance to copy: ");
peo.SetRejectMessage("\nMust be a type of the BlockReference!");
peo.AddAllowedClass(typeof(BlockReference), true);
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK) return;
BlockReference bref = (BlockReference)tr.GetObject(per.ObjectId, OpenMode.ForWrite);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bref.BlockTableRecord, OpenMode.ForRead);
ed.WriteMessage("\nBlock Selected with name: {0}", btr.Name);
PromptStringOptions pso = new PromptStringOptions("\nEnter new block name: ");
pso.AllowSpaces = true;
PromptResult sres = ed.GetString(pso);
if (sres.Status != PromptStatus.OK) return;
string newname = sres.StringResult;
if (bt.Has(newname))
{
ed.WriteMessage("\nBlock with name: {0} already exist, try again", newname);
return;
}
BlockTableRecord newbtr = new BlockTableRecord();
bt.UpgradeOpen();
newbtr = (BlockTableRecord)btr.DeepClone(bt, new IdMapping(), true);
newbtr.Name = newname;
bt.Add(newbtr);
//----------------------------------------------------------------//
foreach (ObjectId id in newbtr)
{
Entity ent = tr.GetObject(id, OpenMode.ForRead) as Entity;
if (ent == null) continue;
ent.UpgradeOpen();
ent.ColorIndex = 0;
}
tr.AddNewlyCreatedDBObject(newbtr, true);
//Change BlockReference
bref.BlockTableRecord = newbtr.Id;
tr.Commit();
}
}