<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to create a copy of a block definition? in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/how-to-create-a-copy-of-a-block-definition/m-p/12711799#M4445</link>
    <description>&lt;P&gt;Here's an example:&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;public static ObjectId CopyBlockDefintion(Database db, string sourceBlockName, string newBlockName)
{
    ObjectId newBlockId;
    ObjectIdCollection ids;
    using (var tr = db.TransactionManager.StartOpenCloseTransaction())
    {
        var blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
        if (!blockTable.Has(sourceBlockName))
            return ObjectId.Null;
        var sourceDefintion = (BlockTableRecord)tr.GetObject(blockTable[sourceBlockName], OpenMode.ForRead);
        ids = new ObjectIdCollection(sourceDefintion.Cast&amp;lt;ObjectId&amp;gt;().ToArray());
        blockTable.UpgradeOpen();
        var newBlock = new BlockTableRecord();
        newBlock.Name = newBlockName;
        newBlockId = blockTable.Add(newBlock);
        tr.AddNewlyCreatedDBObject(newBlock, true);
        tr.Commit();
    }
    var mapping = new IdMapping();
    db.DeepCloneObjects(ids, newBlockId, mapping, false);
    return newBlockId;
}&lt;/LI-CODE&gt;</description>
    <pubDate>Tue, 16 Apr 2024 12:05:44 GMT</pubDate>
    <dc:creator>_gile</dc:creator>
    <dc:date>2024-04-16T12:05:44Z</dc:date>
    <item>
      <title>How to create a copy of a block definition?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-create-a-copy-of-a-block-definition/m-p/12711394#M4443</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create a duplicate of an existing block definition in BlockTable. How would I go about doing that?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Lets say I have a block called B1 in the block table, which has nested dynamic / static blocks, dynamic properties and attributes.&lt;/P&gt;&lt;P&gt;I want to create block B2 which is same as B1. So that I can then modify the nested dynamic block parameters in the new block, which I can then insert where ever I need.&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I achieve this?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit: Manually it can be done using Save Block As within the block editor. Can something similar be done through c#?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2024 09:04:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-create-a-copy-of-a-block-definition/m-p/12711394#M4443</guid>
      <dc:creator>ShricharanaB</dc:creator>
      <dc:date>2024-04-16T09:04:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a copy of a block definition?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-create-a-copy-of-a-block-definition/m-p/12711717#M4444</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;Simply create a new BlockTableRecord named B2, add it to the BlockTable and deep clone the contents of B1 into B2.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2024 11:34:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-create-a-copy-of-a-block-definition/m-p/12711717#M4444</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2024-04-16T11:34:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a copy of a block definition?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-create-a-copy-of-a-block-definition/m-p/12711799#M4445</link>
      <description>&lt;P&gt;Here's an example:&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;public static ObjectId CopyBlockDefintion(Database db, string sourceBlockName, string newBlockName)
{
    ObjectId newBlockId;
    ObjectIdCollection ids;
    using (var tr = db.TransactionManager.StartOpenCloseTransaction())
    {
        var blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
        if (!blockTable.Has(sourceBlockName))
            return ObjectId.Null;
        var sourceDefintion = (BlockTableRecord)tr.GetObject(blockTable[sourceBlockName], OpenMode.ForRead);
        ids = new ObjectIdCollection(sourceDefintion.Cast&amp;lt;ObjectId&amp;gt;().ToArray());
        blockTable.UpgradeOpen();
        var newBlock = new BlockTableRecord();
        newBlock.Name = newBlockName;
        newBlockId = blockTable.Add(newBlock);
        tr.AddNewlyCreatedDBObject(newBlock, true);
        tr.Commit();
    }
    var mapping = new IdMapping();
    db.DeepCloneObjects(ids, newBlockId, mapping, false);
    return newBlockId;
}&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 16 Apr 2024 12:05:44 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-create-a-copy-of-a-block-definition/m-p/12711799#M4445</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2024-04-16T12:05:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a copy of a block definition?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-create-a-copy-of-a-block-definition/m-p/12712000#M4446</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for the reply. I tried your code on a dynamic block, it does create a new block. But it is a static block not dynamic.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2024 13:16:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-create-a-copy-of-a-block-definition/m-p/12712000#M4446</guid>
      <dc:creator>ShricharanaB</dc:creator>
      <dc:date>2024-04-16T13:16:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a copy of a block definition?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-create-a-copy-of-a-block-definition/m-p/12712456#M4447</link>
      <description>&lt;P&gt;You can try deep cloning the block defifnition in a temporary Database, changing its name, and deep cloning back the block definition from the side Database.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;public static ObjectId CopyBlockDefintion(Database db, string sourceBlockName, string newBlockName)
{
    ObjectId sourceBlockId;
    using (var tr = new OpenCloseTransaction())
    {
        var blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
        if (blockTable.Has(sourceBlockName))
            sourceBlockId = blockTable[sourceBlockName];
        else
            return ObjectId.Null;
        tr.Commit();
    }

    ObjectId newBlockId;
    using (var tempDb = new Database(true, true))
    {
        var ids = new ObjectIdCollection { sourceBlockId };
        var mapping = new IdMapping();
        tempDb.WblockCloneObjects(ids, tempDb.BlockTableId, mapping, DuplicateRecordCloning.Replace, false);
        var tempBlockId = mapping[sourceBlockId].Value;
        using (var tr = new OpenCloseTransaction())
        {
            var btr = (BlockTableRecord)tr.GetObject(tempBlockId, OpenMode.ForWrite);
            btr.Name = newBlockName;
        }
        ids = new ObjectIdCollection { tempBlockId };
        mapping = new IdMapping();
        db.WblockCloneObjects(ids, db.BlockTableId, mapping, DuplicateRecordCloning.Replace, false);
        newBlockId = mapping[tempBlockId].Value;
    }
    return newBlockId;
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2024 16:32:49 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-create-a-copy-of-a-block-definition/m-p/12712456#M4447</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2024-04-16T16:32:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a copy of a block definition?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-create-a-copy-of-a-block-definition/m-p/12712661#M4448</link>
      <description>&lt;P&gt;You have to clone the BlockTableRecord itself rather than just the entities it contains.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cloning objects across databases can introduce other problems.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another way to clone a block definition without having to use another database is to use an ObjectOverrule that overrides the DeepClone() method and in the override you can change the name of the Clone of the&amp;nbsp; block before it is appended to the block table&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2024 17:53:31 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-create-a-copy-of-a-block-definition/m-p/12712661#M4448</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2024-04-16T17:53:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a copy of a block definition?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-create-a-copy-of-a-block-definition/m-p/12713621#M4449</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again for the reply.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your code works! If I understand correctly, this is pretty much same as copying block definitions from another drawing right? Just a step added to copy to another temp database?&lt;/P&gt;&lt;P&gt;Or are there any fundamental differences?&lt;/P&gt;</description>
      <pubDate>Wed, 17 Apr 2024 04:54:58 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-create-a-copy-of-a-block-definition/m-p/12713621#M4449</guid>
      <dc:creator>ShricharanaB</dc:creator>
      <dc:date>2024-04-17T04:54:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a copy of a block definition?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-create-a-copy-of-a-block-definition/m-p/12713631#M4450</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/4476837"&gt;@ActivistInvestor&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;Thank you for the reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you please let me know what kind of problems copying to other databases can introduce?&lt;/P&gt;&lt;P&gt;I've been copying across databases for a while now as a method of importing blocks when ever I create something. It&amp;nbsp; works the same way as gile's code.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, I'm assuming inserting blocks through tool palettes works the same way as well. (When you add the block to tool palette and when you click on the palette it imports the block to the current drawing from the original and lets you add it to the selected point)&lt;/P&gt;&lt;P&gt;Please correct me where I'm wrong.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And I don't understand how to write the override method for DeepClone() with ObjectOverride. Could you please help?&lt;/P&gt;</description>
      <pubDate>Wed, 17 Apr 2024 05:02:33 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-create-a-copy-of-a-block-definition/m-p/12713631#M4450</guid>
      <dc:creator>ShricharanaB</dc:creator>
      <dc:date>2024-04-17T05:02:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a copy of a block definition?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-create-a-copy-of-a-block-definition/m-p/12715417#M4451</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;I've been copying across databases for a while now as a method of importing blocks when ever I create something. It&amp;nbsp; works the same way as gile's code.&amp;nbsp;&lt;P class=""&gt;&amp;nbsp;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;After 12 or-so years, I don't remember the exact details, other than it is related to annotative objects.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you're not dealing with annotative objects in blocks, You should probably do it the way&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;&amp;nbsp;suggests, because using an ObjectOverrule to do it involves more code than using an external database.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Apr 2024 18:03:00 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-create-a-copy-of-a-block-definition/m-p/12715417#M4451</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2024-04-17T18:03:00Z</dc:date>
    </item>
  </channel>
</rss>

