<?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: Create Copy of Leader with annotation in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/create-copy-of-leader-with-annotation/m-p/13089802#M2277</link>
    <description>&lt;P&gt;While&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;&amp;nbsp;offered a very good 'textbook' example of using DeepCloneObjects(), textbook examples are good for learning, but aren't always the most-efficient or easiest way to solve a problem. Like this case, in most use cases where DeepCloneObjects() is used, there needs to be &lt;EM&gt;post-processing&lt;/EM&gt; of the clones. That involves starting a transaction; iterating through the IdMapping,to find each clone; and opening the clone to transform it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That typical use case is a good example of where creative use of Overrules can greatly reduce the amount of code that's needed, and also perform the operation much more efficiently.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In &lt;A href="https://github.com/ActivistInvestor/AcMgdLib/blob/main/AcMgdLib/Overrules/Examples/ObservableDeepCloneExtensions.cs" target="_blank" rel="noopener"&gt;this file&lt;/A&gt;, you'll find the source code needed by the example below. The example copies a selection of objects, and transforms the copies by the user-specified displacement. How it differs from the textbook example presented above, is that it completes the operation faster, without any kind of '&lt;EM&gt;post-processing&lt;/EM&gt;', and does that &lt;EM&gt;in one line of code&lt;/EM&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;/// Included below is the basic example, MyCopyCommand() 
/// method. As can be seen in the example, there's no need 
/// to start a transaction; iterate over an IdMapping; or
/// open each clone to transform it. The delegate passed to
/// the DeepCloneObjects&amp;lt;T&amp;gt;() method does all that's needed, 
/// effectively reducing the task of transforming the clones
/// to a single line of code.
///   
/// Example:
/// 
/// Basic emulation of the COPY command, sans dragging support.
/// &amp;lt;/summary&amp;gt;

[CommandMethod("MYCOPY")]
public static void MyCopyCommand()
{
   var pso = new PromptSelectionOptions();
   pso.RejectObjectsFromNonCurrentSpace = true;
   pso.RejectPaperspaceViewport = true;
   Document doc = Application.DocumentManager.MdiActiveDocument;
   Editor ed = doc.Editor;
   var psr = ed.GetSelection(pso);
   if(psr.Status != PromptStatus.OK)
      return;
   var ppo = new PromptPointOptions("\nBase point: ");
   var ppr = ed.GetPoint(ppo);
   if(ppr.Status != PromptStatus.OK)
      return;
   ppo.Message = "\nDisplacment point: ";
   Point3d from = ppr.Value;
   ppo.BasePoint = from;
   ppo.UseBasePoint = true;
   ppr = ed.GetPoint(ppo);
   if(ppr.Status != PromptStatus.OK)
      return;
   Matrix3d xform = Matrix3d.Displacement(from.GetVectorTo(ppr.Value));
   var ids = new ObjectIdCollection(psr.Value.GetObjectIds());
   ids.CopyObjects&amp;lt;Entity&amp;gt;((source, clone) =&amp;gt; clone.TransformBy(xform));
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 18 Oct 2024 13:48:10 GMT</pubDate>
    <dc:creator>ActivistInvestor</dc:creator>
    <dc:date>2024-10-18T13:48:10Z</dc:date>
    <item>
      <title>Create Copy of Leader with annotation</title>
      <link>https://forums.autodesk.com/t5/net-forum/create-copy-of-leader-with-annotation/m-p/13087157#M2275</link>
      <description>&lt;P&gt;Hello ,I have some leader with mtext. Mtext contain text with reinforcement bar data something like "2-T16+2-T20+4-T25" .&lt;/P&gt;&lt;P&gt;From this leader i want to create 3 separate leader with each leader containining only one bar data like leader 1 with 2-T16 text, leader 2 with 2-T20 text, , leader 3 with 4-T25 text.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wrote some sample code to try to clone existing leader with text annotation but it's not valid input warning&lt;/P&gt;&lt;P&gt;here's my sample code&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;[CommandMethod("CloneLeaderWithMText")]
public void CloneLeaderWithMText()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    Editor ed = doc.Editor;

    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        // Prompt for the leader to clone
        PromptEntityOptions peo = new PromptEntityOptions("\nSelect a leader to clone: ");
        peo.SetRejectMessage("\nSelected entity is not a leader.");
        peo.AddAllowedClass(typeof(Leader), true);
        PromptEntityResult per = ed.GetEntity(peo);

        if (per.Status != PromptStatus.OK)
            return;

        Leader originalLeader = tr.GetObject(per.ObjectId, OpenMode.ForRead) as Leader;

        if (originalLeader == null)
            return;

        // Clone the leader
        Leader clonedLeader = originalLeader.Clone() as Leader;

        if (clonedLeader == null)
            return;

        // Add the cloned leader to the database
        BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
        btr.AppendEntity(clonedLeader);
        tr.AddNewlyCreatedDBObject(clonedLeader, true);

        // Clone the associated MText
        MText originalMText = tr.GetObject(originalLeader.Annotation, OpenMode.ForRead) as MText;

        if (originalMText != null)
        {
            MText clonedMText = originalMText.Clone() as MText;

            if (clonedMText != null)
            {
                btr.AppendEntity(clonedMText);
                tr.AddNewlyCreatedDBObject(clonedMText, true);

                // Associate the cloned MText with the cloned leader
                clonedLeader.Annotation = clonedMText.ObjectId;

                clonedLeader.TransformBy(Matrix3d.Displacement(new Vector3d(1000, 500, 0)));
                clonedMText.TransformBy(Matrix3d.Displacement(new Vector3d(1000, 500, 0)));
            }
        }

        tr.Commit();
    }
}&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 16 Oct 2024 06:07:45 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/create-copy-of-leader-with-annotation/m-p/13087157#M2275</guid>
      <dc:creator>vkpunique</dc:creator>
      <dc:date>2024-10-16T06:07:45Z</dc:date>
    </item>
    <item>
      <title>Re: Create Copy of Leader with annotation</title>
      <link>https://forums.autodesk.com/t5/net-forum/create-copy-of-leader-with-annotation/m-p/13087272#M2276</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What about using &lt;A href="https://help.autodesk.com/view/OARX/2025/ENU/?guid=OARX-ManagedRefGuide-Autodesk_AutoCAD_DatabaseServices_Database_DeepCloneObjects_ObjectIdCollection_ObjectId_IdMapping__MarshalAsUnmanagedType_U1__bool" target="_blank" rel="noopener"&gt;DeepCloneObjects&lt;/A&gt; instead of Clone?&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;[CommandMethod("CloneLeaderWithMText")]
public void CloneLeaderWithMText()
{
    var doc = Application.DocumentManager.MdiActiveDocument;
    var db = doc.Database;
    var ed = doc.Editor;

    // Prompt for the leader to clone
    var peo = new PromptEntityOptions("\nSelect a leader to clone: ");
    peo.SetRejectMessage("\nSelected entity is not a leader.");
    peo.AddAllowedClass(typeof(Leader), true);
    var per = ed.GetEntity(peo);
    if (per.Status != PromptStatus.OK)
        return;

    using (var tr = db.TransactionManager.StartTransaction())
    {
        var leader = (Leader)tr.GetObject(per.ObjectId, OpenMode.ForRead);

        // Clone the leader and its annotation (if any)
        var ids = new ObjectIdCollection { leader.ObjectId };
        if (!leader.Annotation.IsNull)
            ids.Add(leader.Annotation);
        var mapping = new IdMapping();
        db.DeepCloneObjects(ids, leader.OwnerId, mapping, false);

        // Displace the cloned entitie(s)
        var displace = Matrix3d.Displacement(new Vector3d(1000.0, 500.0, 0.0));
        foreach (IdPair pair in mapping)
        {
            if (pair.IsCloned &amp;amp;&amp;amp; pair.IsPrimary)
            {
                var entity = (Entity)tr.GetObject(pair.Value, OpenMode.ForWrite);
                entity.TransformBy(displace);
            }
        }
        tr.Commit();
    }
}&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 16 Oct 2024 09:17:42 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/create-copy-of-leader-with-annotation/m-p/13087272#M2276</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2024-10-16T09:17:42Z</dc:date>
    </item>
    <item>
      <title>Re: Create Copy of Leader with annotation</title>
      <link>https://forums.autodesk.com/t5/net-forum/create-copy-of-leader-with-annotation/m-p/13089802#M2277</link>
      <description>&lt;P&gt;While&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;&amp;nbsp;offered a very good 'textbook' example of using DeepCloneObjects(), textbook examples are good for learning, but aren't always the most-efficient or easiest way to solve a problem. Like this case, in most use cases where DeepCloneObjects() is used, there needs to be &lt;EM&gt;post-processing&lt;/EM&gt; of the clones. That involves starting a transaction; iterating through the IdMapping,to find each clone; and opening the clone to transform it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That typical use case is a good example of where creative use of Overrules can greatly reduce the amount of code that's needed, and also perform the operation much more efficiently.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In &lt;A href="https://github.com/ActivistInvestor/AcMgdLib/blob/main/AcMgdLib/Overrules/Examples/ObservableDeepCloneExtensions.cs" target="_blank" rel="noopener"&gt;this file&lt;/A&gt;, you'll find the source code needed by the example below. The example copies a selection of objects, and transforms the copies by the user-specified displacement. How it differs from the textbook example presented above, is that it completes the operation faster, without any kind of '&lt;EM&gt;post-processing&lt;/EM&gt;', and does that &lt;EM&gt;in one line of code&lt;/EM&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;/// Included below is the basic example, MyCopyCommand() 
/// method. As can be seen in the example, there's no need 
/// to start a transaction; iterate over an IdMapping; or
/// open each clone to transform it. The delegate passed to
/// the DeepCloneObjects&amp;lt;T&amp;gt;() method does all that's needed, 
/// effectively reducing the task of transforming the clones
/// to a single line of code.
///   
/// Example:
/// 
/// Basic emulation of the COPY command, sans dragging support.
/// &amp;lt;/summary&amp;gt;

[CommandMethod("MYCOPY")]
public static void MyCopyCommand()
{
   var pso = new PromptSelectionOptions();
   pso.RejectObjectsFromNonCurrentSpace = true;
   pso.RejectPaperspaceViewport = true;
   Document doc = Application.DocumentManager.MdiActiveDocument;
   Editor ed = doc.Editor;
   var psr = ed.GetSelection(pso);
   if(psr.Status != PromptStatus.OK)
      return;
   var ppo = new PromptPointOptions("\nBase point: ");
   var ppr = ed.GetPoint(ppo);
   if(ppr.Status != PromptStatus.OK)
      return;
   ppo.Message = "\nDisplacment point: ";
   Point3d from = ppr.Value;
   ppo.BasePoint = from;
   ppo.UseBasePoint = true;
   ppr = ed.GetPoint(ppo);
   if(ppr.Status != PromptStatus.OK)
      return;
   Matrix3d xform = Matrix3d.Displacement(from.GetVectorTo(ppr.Value));
   var ids = new ObjectIdCollection(psr.Value.GetObjectIds());
   ids.CopyObjects&amp;lt;Entity&amp;gt;((source, clone) =&amp;gt; clone.TransformBy(xform));
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2024 13:48:10 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/create-copy-of-leader-with-annotation/m-p/13089802#M2277</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2024-10-18T13:48:10Z</dc:date>
    </item>
    <item>
      <title>Re: Create Copy of Leader with annotation</title>
      <link>https://forums.autodesk.com/t5/net-forum/create-copy-of-leader-with-annotation/m-p/13089945#M2278</link>
      <description>db.DeepCloneObjects&amp;lt;Entity&amp;gt;(ids, (source, clone) =&amp;gt; clone.TransformBy(xform));&lt;BR /&gt;deepclone method giving error&lt;BR /&gt;Error : CS0308 The non-generic method 'Database.DeepCloneObjects(ObjectIdCollection, ObjectId, IdMapping, bool)' cannot be used with type arguments CadApp&lt;BR /&gt;</description>
      <pubDate>Thu, 17 Oct 2024 04:42:49 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/create-copy-of-leader-with-annotation/m-p/13089945#M2278</guid>
      <dc:creator>vkpunique</dc:creator>
      <dc:date>2024-10-17T04:42:49Z</dc:date>
    </item>
    <item>
      <title>Re: Create Copy of Leader with annotation</title>
      <link>https://forums.autodesk.com/t5/net-forum/create-copy-of-leader-with-annotation/m-p/13091009#M2279</link>
      <description>&lt;P&gt;If you read my reply, you would have seen the link to a code file that's needed.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Oct 2024 12:50:55 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/create-copy-of-leader-with-annotation/m-p/13091009#M2279</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2024-10-17T12:50:55Z</dc:date>
    </item>
  </channel>
</rss>

