<?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: eNotApplicable error while doing deep clone. in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/enotapplicable-error-while-doing-deep-clone/m-p/12704757#M4506</link>
    <description>&lt;P&gt;Here's an example of doing an array by deep cloning an existing block reference.&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;public static void BlockArray(
    BlockReference source,
    Transaction tr,
    int numRows,
    double rowHeight, 
    int numColumns, 
    double columnWidth)
{
    var db = source.Database;
    var sourceId = source.ObjectId;
    var ownerid = source.OwnerId;
    var ids = new ObjectIdCollection { sourceId };
    var displaceX = Vector3d.XAxis * columnWidth;
    var displaceY = Vector3d.YAxis * rowHeight;
    for (int i = 0; i &amp;lt; numRows; i++)
    {
        for (int j = 0; j &amp;lt; numColumns; j++)
        {
            if (i == 0 &amp;amp;&amp;amp; j == 0) continue;
            var mapping = new IdMapping();
            db.DeepCloneObjects(ids, ownerid, mapping, false);
            var br = (BlockReference)tr.GetObject(mapping[sourceId].Value, OpenMode.ForWrite);
            br.TransformBy(Matrix3d.Displacement(i * displaceY + j * displaceX));
        }
    }
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But, if as you show in your code, you insert the first block just before doing the array, you do not need to deep clone it simply insert all the block references at different insertion points.&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;public static void BlockArray(
    Database db, 
    string blockName, 
    Point3d startPoint,
    int numRows,
    double rowHeight, 
    int numColumns, 
    double columnWidth)
{
    using (var tr = db.TransactionManager.StartTransaction())
    {
        var blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
        if (!blockTable.Has(blockName)) return;
        var btrId = blockTable[blockName];
        var displaceX = Vector3d.XAxis * columnWidth;
        var displaceY = Vector3d.YAxis * rowHeight;
        var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
        for (int i = 0; i &amp;lt; numRows; i++)
        {
            var startRowPoint = startPoint + i * displaceY;
            for (int j = 0; j &amp;lt; numColumns; j++)
            {
                var insertionPoint = startRowPoint + j * displaceX;
                var br = new BlockReference(insertionPoint, btrId);
                curSpace.AppendEntity(br);
                tr.AddNewlyCreatedDBObject(br, true);
            }
        }
        tr.Commit();
    }
}&lt;/LI-CODE&gt;</description>
    <pubDate>Fri, 12 Apr 2024 14:41:29 GMT</pubDate>
    <dc:creator>_gile</dc:creator>
    <dc:date>2024-04-12T14:41:29Z</dc:date>
    <item>
      <title>eNotApplicable error while doing deep clone.</title>
      <link>https://forums.autodesk.com/t5/net-forum/enotapplicable-error-while-doing-deep-clone/m-p/12704077#M4505</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I'm using the code below (which was posted on this forum, I forgot who posted this.)&lt;/P&gt;&lt;P&gt;When I try to deep clone some blocks, I'm getting eNotApplicable error at line &lt;STRONG&gt;cloned.TransformBy(transform) &lt;/STRONG&gt;for some blocks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've noticed that this does not happen to all blocks.&lt;/P&gt;&lt;P&gt;When the code works, the loop&amp;nbsp;foreach (IdPair pair in mapping) iterates only one time.&lt;/P&gt;&lt;P&gt;When the code fails, foreach loops many more times in one of which it fails. (result view shown when it fails in below image (Image 1).&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've also shown the cloned object which fails.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could any one please help?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public static void DoDatabaseDeepClone(Transaction tran,
ObjectId entId, Matrix3d transform)
{                
    //using (var tran =
    //entId.Database.TransactionManager.StartTransaction())
    //{
    var ent = (Entity)tran.GetObject(entId, OpenMode.ForRead);

    ObjectId ownerId = ent.OwnerId;
    var mapping = new IdMapping();
    ObjectIdCollection ids = new ObjectIdCollection(    
        new ObjectId[] { entId });
    entId.Database.DeepCloneObjects(ids, ownerId, mapping, false);
    foreach (IdPair pair in mapping)
    {
        if (pair.IsCloned)
        {
            var cloned = tran.GetObject(
                pair.Value, OpenMode.ForRead) as Entity;
            if (cloned != null)
            {
                cloned.UpgradeOpen();
                cloned.TransformBy(transform);                            
            }
        }
    }

    // tran.Commit();
    //}
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm calling the method like this.&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;if (bt.Has(blockName))
{
    BlockReference original = BlockMethods.InsertBlock(blockName, startPoint, modelSpace, db, trans);
    //modelSpace.AppendEntity(original);
   // trans.AddNewlyCreatedDBObject(original, true);
    Point3d insertPoint = startPoint;

    for (int i = 0; i &amp;lt; columns; i++)
    {
        insertPoint = startPoint + new Vector3d(1, 0, 0) * 2800 * i;
        Vector3d moveVector = startPoint.GetVectorTo(insertPoint);
        if (i != 0)
        {
            Matrix3d translation = Matrix3d.Displacement(moveVector);
            BlockMethods.DoDatabaseDeepClone(trans, original.ObjectId, translation);
        }

        for (int j = 1; j &amp;lt; rows; j++)
        {
            //insertTower(blockName, insertPoint, modelSpace, db, trans);
            insertPoint = insertPoint + new Vector3d(0, 1, 0) * 950;
            Vector3d moveVec = startPoint.GetVectorTo(insertPoint);
            Matrix3d tranmat = Matrix3d.Displacement(moveVec);
            BlockMethods.DoDatabaseDeepClone(trans, original.ObjectId, tranmat);
        }

    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Image 1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ShricharanaB_0-1712914424182.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1348927i18486612D6687165/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ShricharanaB_0-1712914424182.png" alt="ShricharanaB_0-1712914424182.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Image 2&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ShricharanaB_1-1712914572080.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1348928iB4017EAD4BFF8249/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ShricharanaB_1-1712914572080.png" alt="ShricharanaB_1-1712914572080.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Apr 2024 09:41:24 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/enotapplicable-error-while-doing-deep-clone/m-p/12704077#M4505</guid>
      <dc:creator>ShricharanaB</dc:creator>
      <dc:date>2024-04-12T09:41:24Z</dc:date>
    </item>
    <item>
      <title>Re: eNotApplicable error while doing deep clone.</title>
      <link>https://forums.autodesk.com/t5/net-forum/enotapplicable-error-while-doing-deep-clone/m-p/12704757#M4506</link>
      <description>&lt;P&gt;Here's an example of doing an array by deep cloning an existing block reference.&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;public static void BlockArray(
    BlockReference source,
    Transaction tr,
    int numRows,
    double rowHeight, 
    int numColumns, 
    double columnWidth)
{
    var db = source.Database;
    var sourceId = source.ObjectId;
    var ownerid = source.OwnerId;
    var ids = new ObjectIdCollection { sourceId };
    var displaceX = Vector3d.XAxis * columnWidth;
    var displaceY = Vector3d.YAxis * rowHeight;
    for (int i = 0; i &amp;lt; numRows; i++)
    {
        for (int j = 0; j &amp;lt; numColumns; j++)
        {
            if (i == 0 &amp;amp;&amp;amp; j == 0) continue;
            var mapping = new IdMapping();
            db.DeepCloneObjects(ids, ownerid, mapping, false);
            var br = (BlockReference)tr.GetObject(mapping[sourceId].Value, OpenMode.ForWrite);
            br.TransformBy(Matrix3d.Displacement(i * displaceY + j * displaceX));
        }
    }
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But, if as you show in your code, you insert the first block just before doing the array, you do not need to deep clone it simply insert all the block references at different insertion points.&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;public static void BlockArray(
    Database db, 
    string blockName, 
    Point3d startPoint,
    int numRows,
    double rowHeight, 
    int numColumns, 
    double columnWidth)
{
    using (var tr = db.TransactionManager.StartTransaction())
    {
        var blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
        if (!blockTable.Has(blockName)) return;
        var btrId = blockTable[blockName];
        var displaceX = Vector3d.XAxis * columnWidth;
        var displaceY = Vector3d.YAxis * rowHeight;
        var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
        for (int i = 0; i &amp;lt; numRows; i++)
        {
            var startRowPoint = startPoint + i * displaceY;
            for (int j = 0; j &amp;lt; numColumns; j++)
            {
                var insertionPoint = startRowPoint + j * displaceX;
                var br = new BlockReference(insertionPoint, btrId);
                curSpace.AppendEntity(br);
                tr.AddNewlyCreatedDBObject(br, true);
            }
        }
        tr.Commit();
    }
}&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 12 Apr 2024 14:41:29 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/enotapplicable-error-while-doing-deep-clone/m-p/12704757#M4506</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2024-04-12T14:41:29Z</dc:date>
    </item>
    <item>
      <title>Re: eNotApplicable error while doing deep clone.</title>
      <link>https://forums.autodesk.com/t5/net-forum/enotapplicable-error-while-doing-deep-clone/m-p/12706231#M4507</link>
      <description>&lt;P&gt;Although you should follow&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt; 's advice for your specific purpose, it might help to explain what your code is doing wrong. First, a deep clone operation can clone more than just entities, and can clone other objects along with the ones you specified to be cloned.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It can also clone extension dictionaries, and anything they contain that's attached to the object you're trying to clone, so you can't simply assume that every entry in an IdMapping is an entity, or one of the entities that you're deep cloning. You have to look at the &lt;STRONG&gt;IsPrimary&lt;/STRONG&gt; property to determine if the entry represents one of the objects whose Id was passed to the DeepCloneObjects() method, and act only if that property's value is true (and/or take some other action for non-primary objects that were incidentally cloned along with the primary objects). The 'primary' objects are the ones whose Ids were passed to DeepCloneObjects(), and non-primary objects are the ones that went along for the ride.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;    foreach (IdPair pair in mapping)
    {
        if (pair.IsCloned)
        {
 &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Should be:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;    foreach (IdPair pair in mapping)
    {
        if (pair.IsCloned &amp;amp;&amp;amp; pair.IsPrimary)
        {
 &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 13 Apr 2024 09:39:11 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/enotapplicable-error-while-doing-deep-clone/m-p/12706231#M4507</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2024-04-13T09:39:11Z</dc:date>
    </item>
    <item>
      <title>Re: eNotApplicable error while doing deep clone.</title>
      <link>https://forums.autodesk.com/t5/net-forum/enotapplicable-error-while-doing-deep-clone/m-p/12710994#M4508</link>
      <description>&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;&amp;nbsp;Thank you for the reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I had first tried creating new block reference for each required position. But, creating a new block reference for each position works fast with static blocks but for dynamic blocks I found it significantly slow, since I'll have to modify the parameters for each new block reference (in my case all the dynamic blocks will have same parameter values). This modifying of parameters for each instance makes it very slow compared to deep cloning. That is why I switched to deep cloning.&amp;nbsp;&lt;/P&gt;&lt;P&gt;These blocks are attributed as well.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2024 04:34:30 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/enotapplicable-error-while-doing-deep-clone/m-p/12710994#M4508</guid>
      <dc:creator>ShricharanaB</dc:creator>
      <dc:date>2024-04-16T04:34:30Z</dc:date>
    </item>
    <item>
      <title>Re: eNotApplicable error while doing deep clone.</title>
      <link>https://forums.autodesk.com/t5/net-forum/enotapplicable-error-while-doing-deep-clone/m-p/12711017#M4509</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;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm not sure what the code is doing wrong. As I said in the main post, for some blocks,&amp;nbsp; it is working and for some it gives eNotApplicable error when applying transform (in which case the mapping has multiple values as opposed to cases when the code works - in this case the mapping has only one value). Both are dynamic and has attributes either direct or through nested attributed blocks.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;if (pair.IsCloned &amp;amp;&amp;amp; pair.IsPrimary)
        {&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This seems to have helped in one case where I was getting the error. I'll have to check for other blocks as well.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'll get back after some testing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2024 04:59:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/enotapplicable-error-while-doing-deep-clone/m-p/12711017#M4509</guid>
      <dc:creator>ShricharanaB</dc:creator>
      <dc:date>2024-04-16T04:59:53Z</dc:date>
    </item>
    <item>
      <title>Re: eNotApplicable error while doing deep clone.</title>
      <link>https://forums.autodesk.com/t5/net-forum/enotapplicable-error-while-doing-deep-clone/m-p/12711712#M4510</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/12961036"&gt;@ShricharanaB&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;&amp;nbsp;Thank you for the reply.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I had first tried creating new block reference for each required position. But, creating a new block reference for each position works fast with static blocks but for dynamic blocks I found it significantly slow, since I'll have to modify the parameters for each new block reference (in my case all the dynamic blocks will have same parameter values). This modifying of parameters for each instance makes it very slow compared to deep cloning. That is why I switched to deep cloning.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;These blocks are attributed as well.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The first code I posted does deep cloning and uses mapping[sourceId].Value to get the ObjectId of the cloned block reference as there's one object cloned at once.&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;var mapping = new IdMapping();
db.DeepCloneObjects(ids, ownerid, mapping, false);
var br = (BlockReference)tr.GetObject(mapping[sourceId].Value, OpenMode.ForWrite);
br.TransformBy(Matrix3d.Displacement(i * displaceY + j * displaceX));&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 16 Apr 2024 11:31:26 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/enotapplicable-error-while-doing-deep-clone/m-p/12711712#M4510</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2024-04-16T11:31:26Z</dc:date>
    </item>
  </channel>
</rss>

