Clone the dynamic block in the same drawing file.

By Virupaksha Aithal

Below code shows the procedure to clone the dynamic block with all its properties to same drawing file.

Steps:
Wblock the present dynamic block to new database using “wblock”.
Use “Insert” API to insert the database (generated in step1) with new block name

[CommandMethod("cloneDynamic")]
static public void cloneDynamic() // This method can have any name 
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Editor ed = doc.Editor;
    Database db = doc.Database;
    using (Transaction tr =
        db.TransactionManager.StartTransaction())
    {
        BlockTable tb = tr.GetObject(db.BlockTableId,
                        OpenMode.ForRead) as BlockTable;
        BlockTableRecord dynamic = tr.GetObject(tb["DynamicBlock"],
                            OpenMode.ForRead) as BlockTableRecord;
 
        Database temDB = db.Wblock(dynamic.ObjectId);
 
        ObjectId copyId = ObjectId.Null;
        using (temDB)
        {
            copyId = db.Insert("DynamicBlockCopy", temDB, true);
        }
        tr.Commit();
 
    }
}

Comments

3 responses to “Clone the dynamic block in the same drawing file.”

  1. Hi, thanks for this post. I’ve been going around the bushes in confusion which db is source and destination in my own code. Can you please go further just to copy the dynamic block into a different name ?
    Thanks,
    Efren

  2. ooops. pls forgive my oversight. can i delete my post?

  3. Hello,
    testing this I run into the “eWasErased” Exception. What am I doing wrong?
    Any hints are appreciated!
    Thank you,
    Daniel

Leave a Reply to EfrenCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading