Hi all,
I'm trying to copy a dynamic block definition, so I can change the layer of a hatch without affecting the original block definition. My first approach was using deepclone:
private string bordcolor(string bname) { Document acadDoc = default(Document); acadDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database acadDB = acadDoc.Database; if (BordColorCombobox.SelectedIndex == 1) { using (Transaction acadTrans = acadDB.TransactionManager.StartTransaction()) { BlockTable bt = (BlockTable)acadTrans.GetObject(acadDB.BlockTableId, OpenMode.ForRead); if (!bt.Has(bname + "wit")) { BlockTableRecord tmpblockDef = bt[bname].GetObject(OpenMode.ForRead) as BlockTableRecord; IdMapping map = new IdMapping(); BlockTableRecord newblockDef = tmpblockDef.DeepClone(bt, map, false) as BlockTableRecord; bname = bname + "wit"; newblockDef.Name = bname; bt.UpgradeOpen(); bt.Add(newblockDef); acadTrans.AddNewlyCreatedDBObject(newblockDef, true); bt.DowngradeOpen(); foreach (ObjectId idt in newblockDef) { Hatch h = acadTrans.GetObject(idt, OpenMode.ForRead) as Hatch; if (h != null && h.Layer == "wy-kleur-050") { h.UpgradeOpen(); h.Layer = "wy-kleur-255"; h.DowngradeOpen(); } } } else { bname = bname + "wit"; } acadTrans.Commit(); } } return bname; }
This makes a perfect clone with the hatch on the correct layer, but it also changes the layer of the hatch in the original block definition if there is no block reference in the drawing.
Another issue is when the code (not shown here) inserts the new block reference. I set all of the dynamic properties through code, but none of them seem to be applied. When stepping through the code I see de values being updated, but these changes are not visible in the drawing after insertion.
So I tried DeepCloneObjects:
private string bordcolor(string bname) { Document acadDoc = default(Document); acadDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database acadDB = acadDoc.Database; if (BordColorCombobox.SelectedIndex == 1) { using (Transaction acadTrans = acadDB.TransactionManager.StartTransaction()) { BlockTable bt = (BlockTable)acadTrans.GetObject(acadDB.BlockTableId, OpenMode.ForRead); if (!bt.Has(bname + "wit")) { BlockTableRecord tmpblockDef = bt[bname].GetObject(OpenMode.ForRead) as BlockTableRecord; ObjectIdCollection ids = new ObjectIdCollection(); foreach (ObjectId id in tmpblockDef) { ids.Add(id); } BlockTableRecord newblockDef = new BlockTableRecord(); bname = bname + "wit"; newblockDef.Name = bname; bt.UpgradeOpen(); bt.Add(newblockDef); acadTrans.AddNewlyCreatedDBObject(newblockDef, true); IdMapping map = new IdMapping(); acadDB.DeepCloneObjects(ids, newblockDef.ObjectId, map, true); bt.DowngradeOpen(); foreach (ObjectId idt in newblockDef) { Hatch h = acadTrans.GetObject(idt, OpenMode.ForRead) as Hatch; if (h != null && h.Layer == "wy-kleur-050") { h.UpgradeOpen(); h.Layer = "wy-kleur-255"; h.DowngradeOpen(); } } } else { bname = bname + "wit"; } acadTrans.Commit(); } } return bname; }
This also makes a perfect copy, but without the dynamic properties. Which for me is useless.
Is there a way the do this?