.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Copy a dynamic block definition

0 REPLIES 0
Reply
Message 1 of 1
marcwalen
318 Views, 0 Replies

Copy a dynamic block definition

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?

0 REPLIES 0

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost