<?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 Deep clone associative hatch to polyline in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/deep-clone-associative-hatch-to-polyline/m-p/8827767#M22285</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I'm trying to override ObjectOverrule.DeepClone&amp;nbsp;&lt;SPAN&gt;behavior to copy ployline and it's associative hatches.&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Associative Hatch" style="width: 993px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/643146i610EE51127D8F50C/image-size/large?v=v2&amp;amp;px=999" role="button" title="TTSlab.png" alt="Associative Hatch" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Associative Hatch&lt;/span&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;the following&amp;nbsp;code throws exception when trying to copy hatches.&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;public override DBObject DeepClone(DBObject dbObject, DBObject ownerObject, IdMapping idMap, bool isPrimary)
{
&amp;nbsp; &amp;nbsp; &amp;nbsp;Document document = Application.DocumentManager.GetDocument(dbObject.Database);
&amp;nbsp; &amp;nbsp; &amp;nbsp;DBObject clonedObject = base.DeepClone(dbObject, ownerObject, idMap, isPrimary);
&amp;nbsp; &amp;nbsp; &amp;nbsp; //Copy Hatch
&amp;nbsp;&amp;nbsp; &amp;nbsp; ObjectIdCollection persistentID = dbObject.GetPersistentReactorIds();
&amp;nbsp; &amp;nbsp; &amp;nbsp;IdMapping map = new IdMapping();
&amp;nbsp; &amp;nbsp; &amp;nbsp;document.Database.DeepCloneObjects(persistentID, document.Database.CurrentSpaceId, map, false);&amp;nbsp; &amp;nbsp;
&amp;nbsp; &amp;nbsp; &amp;nbsp;return clonedObject;
}&amp;nbsp;&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Exception" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/643147iCB28EAE877952278/image-size/large?v=v2&amp;amp;px=999" role="button" title="temp.png" alt="Exception" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Exception&lt;/span&gt;&lt;/span&gt;Thanks in advance.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 02 Jun 2019 09:18:37 GMT</pubDate>
    <dc:creator>mahmoudali9316</dc:creator>
    <dc:date>2019-06-02T09:18:37Z</dc:date>
    <item>
      <title>Deep clone associative hatch to polyline</title>
      <link>https://forums.autodesk.com/t5/net-forum/deep-clone-associative-hatch-to-polyline/m-p/8827767#M22285</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I'm trying to override ObjectOverrule.DeepClone&amp;nbsp;&lt;SPAN&gt;behavior to copy ployline and it's associative hatches.&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Associative Hatch" style="width: 993px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/643146i610EE51127D8F50C/image-size/large?v=v2&amp;amp;px=999" role="button" title="TTSlab.png" alt="Associative Hatch" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Associative Hatch&lt;/span&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;the following&amp;nbsp;code throws exception when trying to copy hatches.&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;public override DBObject DeepClone(DBObject dbObject, DBObject ownerObject, IdMapping idMap, bool isPrimary)
{
&amp;nbsp; &amp;nbsp; &amp;nbsp;Document document = Application.DocumentManager.GetDocument(dbObject.Database);
&amp;nbsp; &amp;nbsp; &amp;nbsp;DBObject clonedObject = base.DeepClone(dbObject, ownerObject, idMap, isPrimary);
&amp;nbsp; &amp;nbsp; &amp;nbsp; //Copy Hatch
&amp;nbsp;&amp;nbsp; &amp;nbsp; ObjectIdCollection persistentID = dbObject.GetPersistentReactorIds();
&amp;nbsp; &amp;nbsp; &amp;nbsp;IdMapping map = new IdMapping();
&amp;nbsp; &amp;nbsp; &amp;nbsp;document.Database.DeepCloneObjects(persistentID, document.Database.CurrentSpaceId, map, false);&amp;nbsp; &amp;nbsp;
&amp;nbsp; &amp;nbsp; &amp;nbsp;return clonedObject;
}&amp;nbsp;&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Exception" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/643147iCB28EAE877952278/image-size/large?v=v2&amp;amp;px=999" role="button" title="temp.png" alt="Exception" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Exception&lt;/span&gt;&lt;/span&gt;Thanks in advance.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Jun 2019 09:18:37 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/deep-clone-associative-hatch-to-polyline/m-p/8827767#M22285</guid>
      <dc:creator>mahmoudali9316</dc:creator>
      <dc:date>2019-06-02T09:18:37Z</dc:date>
    </item>
    <item>
      <title>Re: Deep clone associative hatch to polyline</title>
      <link>https://forums.autodesk.com/t5/net-forum/deep-clone-associative-hatch-to-polyline/m-p/8835786#M22286</link>
      <description>&lt;P&gt;You are calling&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ObjectIdCollection persistentID = dbObject.GetPersistentReactorIds();
document.Database.DeepCloneObjects(persistentID, document.Database.CurrentSpaceId, ..);   &lt;/PRE&gt;
&lt;P&gt;without checking&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;the number of persistent reactors in &lt;FONT face="courier new,courier"&gt;persistentID&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;the owners and classes of the reactors in &lt;FONT face="courier new,courier"&gt;persistentID&lt;/FONT&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;I suppose the exception is caused, because you are cloning objects to &lt;FONT face="courier new,courier"&gt;document.Database.CurrentSpaceId&lt;/FONT&gt;, (which is either the model space or paper space), that are no entities. Or maybe because &lt;FONT face="courier new,courier"&gt;persistentID&lt;/FONT&gt; is empty.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I checked the hatch of a hatched polyline with the ArxDbg.arx (from &amp;lt;ARX&amp;gt;\samples\database\ARXDBG) and it had no persistent reactors at all. Only the polyline had the hatch as persistent reactor.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jun 2019 08:31:47 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/deep-clone-associative-hatch-to-polyline/m-p/8835786#M22286</guid>
      <dc:creator>tbrammer</dc:creator>
      <dc:date>2019-06-06T08:31:47Z</dc:date>
    </item>
    <item>
      <title>Re: Deep clone associative hatch to polyline</title>
      <link>https://forums.autodesk.com/t5/net-forum/deep-clone-associative-hatch-to-polyline/m-p/8840869#M22287</link>
      <description>&lt;P&gt;I have written command method to check Ids of hatches before copying polyline as in the following image.&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="PrintIdPolyline.png" style="width: 701px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/645495i8588B724F60C57A6/image-size/large?v=v2&amp;amp;px=999" role="button" title="PrintIdPolyline.png" alt="PrintIdPolyline.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and then I called copy command to copy polyline and checked number of PersistentReactorIds as in the following image.&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="persistenceId.png" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/645496iC9BC8985E42FD248/image-size/large?v=v2&amp;amp;px=999" role="button" title="persistenceId.png" alt="persistenceId.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;notice that :&lt;/P&gt;&lt;P&gt;-persistentID.Count=2.&lt;/P&gt;&lt;P&gt;-it has same ids.&lt;/P&gt;</description>
      <pubDate>Sun, 09 Jun 2019 08:27:07 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/deep-clone-associative-hatch-to-polyline/m-p/8840869#M22287</guid>
      <dc:creator>mahmoudali9316</dc:creator>
      <dc:date>2019-06-09T08:27:07Z</dc:date>
    </item>
    <item>
      <title>Re: Deep clone associative hatch to polyline</title>
      <link>https://forums.autodesk.com/t5/net-forum/deep-clone-associative-hatch-to-polyline/m-p/8840977#M22288</link>
      <description>&lt;P&gt;I Solved the problem by opening a transaction then looping through&amp;nbsp;persistentIDs to clone hatches and then remove hatch loop and assign new cloned Polyline to it. (as in following code)&lt;/P&gt;&lt;PRE&gt;      public override DBObject DeepClone(DBObject dbObject, DBObject ownerObject, IdMapping idMap, bool isPrimary)
        {
            var document = Application.DocumentManager.GetDocument(dbObject.Database);
            DBObject clonedObject = base.DeepClone(dbObject, ownerObject, idMap, isPrimary);
            //Copy Hatch
            ObjectIdCollection persistentIDs = clonedObject.GetPersistentReactorIds();
            if (persistentIDs.Count &amp;gt; 0)
            {
                using (Transaction trans = document.Database.TransactionManager.StartTransaction())
                {
                    BlockTableRecord currentSpace = trans.GetObject(document.Database.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                    Polyline clonedPoly = trans.GetObject(clonedObject.Id, OpenMode.ForWrite) as Polyline;
                    ObjectIdCollection clonedPolyIds = new ObjectIdCollection { clonedPoly.ObjectId };
                    foreach (ObjectId ObjID in persistentIDs)
                    {
                        IdMapping map = new IdMapping();
                        document.Database.DeepCloneObjects(new ObjectIdCollection { ObjID }, currentSpace.Id, map, false);
                        foreach (IdPair idPair in map)
                        {
                            Hatch clonedHatch = trans.GetObject(idPair.Value, OpenMode.ForWrite) as Hatch;
                            if (clonedHatch != null)
                            {
                                clonedHatch.Associative = false;
                                clonedHatch.RemoveAssociatedObjectIds();
                                for (int i = 0; i &amp;lt; clonedHatch.NumberOfLoops; ++i)
                                    clonedHatch.RemoveLoopAt(i);

                                clonedHatch.Associative = true;
                                clonedHatch.AppendLoop(HatchLoopTypes.External, clonedPolyIds);
                            }
                        }
                    }
                    trans.Commit();
                }
            }
            return clonedObject;
        }&lt;/PRE&gt;&lt;P&gt;however, there is another problem.&lt;/P&gt;&lt;P&gt;After copying polyline I need to call Regen command to show hatch within polyline (1 hatch).&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Untitled.png" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/645519iEAF8BFE57424F161/image-size/large?v=v2&amp;amp;px=999" role="button" title="Untitled.png" alt="Untitled.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;to show transparent hatch, i need to move polyline.&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Untitled.png" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/645520iB24AD1984E0407CD/image-size/large?v=v2&amp;amp;px=999" role="button" title="Untitled.png" alt="Untitled.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;How can I solve this problem using API?&lt;/P&gt;</description>
      <pubDate>Sun, 09 Jun 2019 11:52:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/deep-clone-associative-hatch-to-polyline/m-p/8840977#M22288</guid>
      <dc:creator>mahmoudali9316</dc:creator>
      <dc:date>2019-06-09T11:52:20Z</dc:date>
    </item>
    <item>
      <title>Re: Deep clone associative hatch to polyline</title>
      <link>https://forums.autodesk.com/t5/net-forum/deep-clone-associative-hatch-to-polyline/m-p/8841815#M22289</link>
      <description>&lt;P&gt;I solved the problem by using IdMapping of Deep Clone function instead of defining new instance.&lt;/P&gt;&lt;PRE&gt; public override DBObject DeepClone(DBObject dbObject, DBObject ownerObject, IdMapping idMap, bool isPrimary)
        {
            var document = Application.DocumentManager.GetDocument(dbObject.Database);
            DBObject clonedObject = base.DeepClone(dbObject, ownerObject, idMap, isPrimary);
            //Copy Hatch
            ObjectIdCollection persistentIDs = clonedObject.GetPersistentReactorIds();
            if (persistentIDs.Count &amp;gt; 0)
            {
                using (Transaction trans = document.Database.TransactionManager.StartTransaction())
                {
                    BlockTableRecord currentSpace = trans.GetObject(document.Database.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                    Polyline clonedPoly = trans.GetObject(clonedObject.Id, OpenMode.ForWrite) as Polyline;
                    ObjectIdCollection clonedPolyIds = new ObjectIdCollection { clonedPoly.ObjectId };
                    foreach (ObjectId ObjID in persistentIDs)
                    {&lt;BR /&gt;                         //&lt;SPAN&gt;Never create a new instance of&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;AcDbIdMapping&lt;SPAN&gt;&amp;nbsp;to pass to the cloning function of the dependent reference.&lt;/SPAN&gt;
                        //IdMapping map = new IdMapping();
                        document.Database.DeepCloneObjects(new ObjectIdCollection { ObjID }, currentSpace.Id, idMap, false);
                        foreach (IdPair idPair in idMap)
                        {
                            Hatch clonedHatch = trans.GetObject(idPair.Value, OpenMode.ForWrite) as Hatch;
                            if (clonedHatch != null)
                            {
                                clonedHatch.Associative = false;
                                clonedHatch.RemoveAssociatedObjectIds();
                                for (int i = 0; i &amp;lt; clonedHatch.NumberOfLoops; ++i)
                                    clonedHatch.RemoveLoopAt(i);

                                clonedHatch.Associative = true;
                                clonedHatch.AppendLoop(HatchLoopTypes.External, clonedPolyIds);
                            }
                        }
                    }
                    trans.Commit();
                }
            }
            return clonedObject;
        }&lt;/PRE&gt;&lt;P&gt;See ObjectARX: Developer's Guide -&amp;gt; Advanced topic -&amp;gt;Deep cloning-&amp;gt;Implementing deepClone() for custom classes.&lt;/P&gt;&lt;P&gt;there is note says that :&amp;nbsp;&lt;SPAN&gt;Never create a new instance of&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;AcDbIdMapping&lt;SPAN&gt;&amp;nbsp;to pass to the cloning function of the dependent reference.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jun 2019 09:07:12 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/deep-clone-associative-hatch-to-polyline/m-p/8841815#M22289</guid>
      <dc:creator>mahmoudali9316</dc:creator>
      <dc:date>2019-06-10T09:07:12Z</dc:date>
    </item>
  </channel>
</rss>

