<?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: ObjectIdCollection transform to new point in same drawing in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/objectidcollection-transform-to-new-point-in-same-drawing/m-p/11308435#M12315</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;thanks for the tip about Try Cast i always find smth helpful in your posts,&lt;/P&gt;&lt;P&gt;i tried both methods and here is the result:&lt;/P&gt;&lt;LI-CODE lang="general"&gt;Command:
Command: neltao
Unknown command "NELTAO".  Press F1 for help.
Command: NETLOAD
Command: FORVSFOREACH
 entities count: 62,389.00
  try cast:
        foreach1: 436.00
        forloop: 356.00
  without try cast:
        foreach2: 370.00
        forloop2: 362.00
Command: FORVSFOREACH
 entities count: 62,389.00
  try cast:
        foreach1: 422.00
        forloop: 398.00
  without try cast:
        foreach2: 362.00
        forloop2: 365.00
Command: FORVSFOREACH
 entities count: 62,389.00
  try cast:
        foreach1: 413.00
        forloop: 370.00
  without try cast:
        foreach2: 377.00
        forloop2: 351.00
Command: FORVSFOREACH
 entities count: 62,389.00
  try cast:
        foreach1: 377.00
        forloop: 358.00
  without try cast:
        foreach2: 363.00
        forloop2: 402.00
Command: FORVSFOREACH
 entities count: 62,389.00
  try cast:
        foreach1: 365.00
        forloop: 350.00
  without try cast:
        foreach2: 370.00
        forloop2: 373.00&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;        [CommandMethod("ForVsForeach")]
        public void ForAndForeach()
        {
            #region curr doc
            var adoc = Application.DocumentManager.MdiActiveDocument;
            var adb = adoc.Database;
            var editor = adoc.Editor;
            var cdoc = cApp.CivilApplication.ActiveDocument;
            #endregion
            Stopwatch stopWatch = new Stopwatch();


            var ids = editor.SelectAll().Value.GetObjectIds();
            int count = ids.Length;
            Entity entity;
            var transform = Matrix3d.Displacement(new Vector3d(22.22, 33.33, 0));
            editor.WriteMessage($"\n entities count: {count,0:#,0.00} \n");
            
            //////////////////////////////// try cast /////////////////////////////////////////////
            editor.WriteMessage($"\n\t try cast: \n");
            stopWatch.Reset();
            using (var tr = adb.TransactionManager.StartTransaction())
            {
                stopWatch.Start();
                foreach (var id in ids)
                {
                    entity = id.GetObject(OpenMode.ForWrite) as Entity;
                    entity.TransformBy(transform);
                }

                stopWatch.Stop();
                //tr.Commit();
            }
            editor.WriteMessage($"\n\t\t\t foreach1: {stopWatch.ElapsedMilliseconds,0:#,0.00}\n");

            stopWatch.Reset();
            using (var tr = adb.TransactionManager.StartTransaction())
            {
                stopWatch.Start();
                for (int i = 0; i &amp;lt; count; i++)
                {
                    entity = ids[i].GetObject(OpenMode.ForWrite) as Entity;
                    entity.TransformBy(transform);
                }
                stopWatch.Stop();
                //tr.Commit();
            }
            editor.WriteMessage($"\n\n\t\t\t forloop: {stopWatch.ElapsedMilliseconds,0:#,0.00}\n");

            ////////////////////////////////without try cast/////////////////////////////////////////////
            editor.WriteMessage($"\n\t without try cast: \n");
            stopWatch.Reset();
            using (var tr = adb.TransactionManager.StartTransaction())
            {
                stopWatch.Start();
                foreach (var id in ids)
                {
                    entity = (Entity)id.GetObject(OpenMode.ForWrite);
                    entity.TransformBy(transform);
                }

                stopWatch.Stop();
                //tr.Commit();
            }
            editor.WriteMessage($"\n\n\t\t\t foreach2: {stopWatch.ElapsedMilliseconds,0:#,0.00}\n");

            stopWatch.Reset();
            using (var tr = adb.TransactionManager.StartTransaction())
            {
                stopWatch.Start();
                for (int i = 0; i &amp;lt; count; i++)
                {
                    entity = (Entity)ids[i].GetObject(OpenMode.ForWrite);
                    entity.TransformBy(transform);
                }
                stopWatch.Stop();
                //tr.Commit();
            }
            editor.WriteMessage($"\n\n\t\t\t forloop2: {stopWatch.ElapsedMilliseconds,0:#,0.00}\n\n");
        }&lt;/LI-CODE&gt;</description>
    <pubDate>Wed, 20 Jul 2022 10:48:03 GMT</pubDate>
    <dc:creator>essam-salah</dc:creator>
    <dc:date>2022-07-20T10:48:03Z</dc:date>
    <item>
      <title>ObjectIdCollection transform to new point in same drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/objectidcollection-transform-to-new-point-in-same-drawing/m-p/11304498#M12307</link>
      <description>&lt;P&gt;Hello Expertise,&lt;/P&gt;&lt;P&gt;Could you please help me with sample code to capture all ObjectIds within the Closed Polyline and transform/move them to new position at a time instead of one by one ObjectId in the same drawing. I know to transform one by one entity as below.&lt;/P&gt;&lt;P&gt;ent.TransformBy(Matrix3d.Displacement(new Vector3d(ptX, ptY, 0)));&lt;/P&gt;&lt;P&gt;But&amp;nbsp;needs help to transform/move list of ObjectIds at a time in the same drawing.&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jul 2022 16:44:57 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/objectidcollection-transform-to-new-point-in-same-drawing/m-p/11304498#M12307</guid>
      <dc:creator>manohar2375</dc:creator>
      <dc:date>2022-07-18T16:44:57Z</dc:date>
    </item>
    <item>
      <title>Re: ObjectIdCollection transform to new point in same drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/objectidcollection-transform-to-new-point-in-same-drawing/m-p/11304571#M12308</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Simply use&amp;nbsp; a &lt;A href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/statements/iteration-statements#the-foreach-statement" target="_blank" rel="noopener"&gt;foreach&lt;/A&gt; loop.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jul 2022 17:17:46 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/objectidcollection-transform-to-new-point-in-same-drawing/m-p/11304571#M12308</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2022-07-18T17:17:46Z</dc:date>
    </item>
    <item>
      <title>Re: ObjectIdCollection transform to new point in same drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/objectidcollection-transform-to-new-point-in-same-drawing/m-p/11304604#M12309</link>
      <description>&lt;P&gt;Hi _gile,&lt;/P&gt;&lt;P&gt;Thanks for your reply with suggestion.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But, Is there any possibility to transform list of ObjectIds at a time without use foreach or any other loop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in Advance.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jul 2022 17:30:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/objectidcollection-transform-to-new-point-in-same-drawing/m-p/11304604#M12309</guid>
      <dc:creator>manohar2375</dc:creator>
      <dc:date>2022-07-18T17:30:14Z</dc:date>
    </item>
    <item>
      <title>Re: ObjectIdCollection transform to new point in same drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/objectidcollection-transform-to-new-point-in-same-drawing/m-p/11304622#M12310</link>
      <description>&lt;P&gt;Put the foreach in a function method or an extension method and then it acts like a single step.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jul 2022 17:35:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/objectidcollection-transform-to-new-point-in-same-drawing/m-p/11304622#M12310</guid>
      <dc:creator>Ed__Jobe</dc:creator>
      <dc:date>2022-07-18T17:35:50Z</dc:date>
    </item>
    <item>
      <title>Re: ObjectIdCollection transform to new point in same drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/objectidcollection-transform-to-new-point-in-same-drawing/m-p/11304650#M12311</link>
      <description>&lt;P&gt;Hi Ed.Jobe,&lt;/P&gt;&lt;P&gt;Thanks for reply with suggestion.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But i am thinking that, drawing will get heavy load and get impact on performance if we use foreach, so thinking that, is there any other way to transform all at a time.&lt;/P&gt;&lt;P&gt;For Example:&lt;/P&gt;&lt;P&gt;to capture all elements, will use selection set as below.&lt;/P&gt;&lt;P&gt;PromptSelectionResult res = ed.SelectCrossingPolygon(pnt3dColl, filter);&lt;/P&gt;&lt;P&gt;so, like that, is there any other way.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in Advance.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jul 2022 17:48:37 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/objectidcollection-transform-to-new-point-in-same-drawing/m-p/11304650#M12311</guid>
      <dc:creator>manohar2375</dc:creator>
      <dc:date>2022-07-18T17:48:37Z</dc:date>
    </item>
    <item>
      <title>Re: ObjectIdCollection transform to new point in same drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/objectidcollection-transform-to-new-point-in-same-drawing/m-p/11304657#M12312</link>
      <description>&lt;P&gt;Even if you found an AutoCAD api method that did it, how do you think they would do it? Somehow, at a base level, you have to process them one at a time. A foreach is the simplest method.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jul 2022 17:52:01 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/objectidcollection-transform-to-new-point-in-same-drawing/m-p/11304657#M12312</guid>
      <dc:creator>Ed__Jobe</dc:creator>
      <dc:date>2022-07-18T17:52:01Z</dc:date>
    </item>
    <item>
      <title>Re: ObjectIdCollection transform to new point in same drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/objectidcollection-transform-to-new-point-in-same-drawing/m-p/11305866#M12313</link>
      <description>&lt;P&gt;&lt;EM&gt;for loop&lt;/EM&gt; will be faster than &lt;EM&gt;foreach&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;            var ids = res.Value.GetObjectIds();
            int count = ids.Length;
            Entity entity;

            var transform = Matrix3d.Displacement(new Vector3d(0, 0, 0));
            for (int i = 0; i &amp;lt; count; i++)
            {
                entity = ids[i].GetObject(OpenMode.ForWrite) as Entity;
                entity.TransformBy(transform);
            }&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 19 Jul 2022 09:47:54 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/objectidcollection-transform-to-new-point-in-same-drawing/m-p/11305866#M12313</guid>
      <dc:creator>essam-salah</dc:creator>
      <dc:date>2022-07-19T09:47:54Z</dc:date>
    </item>
    <item>
      <title>Re: ObjectIdCollection transform to new point in same drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/objectidcollection-transform-to-new-point-in-same-drawing/m-p/11306051#M12314</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/3650456"&gt;@essam-salah&lt;/a&gt;&amp;nbsp; a écrit&amp;nbsp;:&lt;BR /&gt;
&lt;P&gt;&lt;EM&gt;for loop&lt;/EM&gt; will be faster than &lt;EM&gt;foreach&lt;/EM&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Are you sure about this ?&lt;/P&gt;
&lt;P&gt;Did you test-it by yourself ?&lt;/P&gt;
&lt;P&gt;If so, I should be interseted by how much faster is it ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Rather than worrying about whether 'for' is faster than 'foreach', it is better to start by optimizing what you repeat at each iteration inside the loop.&lt;BR /&gt;A direct cast:&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;entity = (Entity)ids[i].GetObject(OpenMode.ForWrite);&lt;/LI-CODE&gt;
&lt;P&gt;is faster than a 'try cast' (which should only be used in conjunction with a : if (entity != null) ...&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;entity = ids[i].GetObject(OpenMode.ForWrite) as Entity;&lt;/LI-CODE&gt;
&lt;P&gt;See &lt;A href="https://stackoverflow.com/questions/2483/casting-newtype-vs-object-as-newtype" target="_blank" rel="noopener"&gt;this dicussion&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jul 2022 11:42:54 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/objectidcollection-transform-to-new-point-in-same-drawing/m-p/11306051#M12314</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2022-07-19T11:42:54Z</dc:date>
    </item>
    <item>
      <title>Re: ObjectIdCollection transform to new point in same drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/objectidcollection-transform-to-new-point-in-same-drawing/m-p/11308435#M12315</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;thanks for the tip about Try Cast i always find smth helpful in your posts,&lt;/P&gt;&lt;P&gt;i tried both methods and here is the result:&lt;/P&gt;&lt;LI-CODE lang="general"&gt;Command:
Command: neltao
Unknown command "NELTAO".  Press F1 for help.
Command: NETLOAD
Command: FORVSFOREACH
 entities count: 62,389.00
  try cast:
        foreach1: 436.00
        forloop: 356.00
  without try cast:
        foreach2: 370.00
        forloop2: 362.00
Command: FORVSFOREACH
 entities count: 62,389.00
  try cast:
        foreach1: 422.00
        forloop: 398.00
  without try cast:
        foreach2: 362.00
        forloop2: 365.00
Command: FORVSFOREACH
 entities count: 62,389.00
  try cast:
        foreach1: 413.00
        forloop: 370.00
  without try cast:
        foreach2: 377.00
        forloop2: 351.00
Command: FORVSFOREACH
 entities count: 62,389.00
  try cast:
        foreach1: 377.00
        forloop: 358.00
  without try cast:
        foreach2: 363.00
        forloop2: 402.00
Command: FORVSFOREACH
 entities count: 62,389.00
  try cast:
        foreach1: 365.00
        forloop: 350.00
  without try cast:
        foreach2: 370.00
        forloop2: 373.00&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;        [CommandMethod("ForVsForeach")]
        public void ForAndForeach()
        {
            #region curr doc
            var adoc = Application.DocumentManager.MdiActiveDocument;
            var adb = adoc.Database;
            var editor = adoc.Editor;
            var cdoc = cApp.CivilApplication.ActiveDocument;
            #endregion
            Stopwatch stopWatch = new Stopwatch();


            var ids = editor.SelectAll().Value.GetObjectIds();
            int count = ids.Length;
            Entity entity;
            var transform = Matrix3d.Displacement(new Vector3d(22.22, 33.33, 0));
            editor.WriteMessage($"\n entities count: {count,0:#,0.00} \n");
            
            //////////////////////////////// try cast /////////////////////////////////////////////
            editor.WriteMessage($"\n\t try cast: \n");
            stopWatch.Reset();
            using (var tr = adb.TransactionManager.StartTransaction())
            {
                stopWatch.Start();
                foreach (var id in ids)
                {
                    entity = id.GetObject(OpenMode.ForWrite) as Entity;
                    entity.TransformBy(transform);
                }

                stopWatch.Stop();
                //tr.Commit();
            }
            editor.WriteMessage($"\n\t\t\t foreach1: {stopWatch.ElapsedMilliseconds,0:#,0.00}\n");

            stopWatch.Reset();
            using (var tr = adb.TransactionManager.StartTransaction())
            {
                stopWatch.Start();
                for (int i = 0; i &amp;lt; count; i++)
                {
                    entity = ids[i].GetObject(OpenMode.ForWrite) as Entity;
                    entity.TransformBy(transform);
                }
                stopWatch.Stop();
                //tr.Commit();
            }
            editor.WriteMessage($"\n\n\t\t\t forloop: {stopWatch.ElapsedMilliseconds,0:#,0.00}\n");

            ////////////////////////////////without try cast/////////////////////////////////////////////
            editor.WriteMessage($"\n\t without try cast: \n");
            stopWatch.Reset();
            using (var tr = adb.TransactionManager.StartTransaction())
            {
                stopWatch.Start();
                foreach (var id in ids)
                {
                    entity = (Entity)id.GetObject(OpenMode.ForWrite);
                    entity.TransformBy(transform);
                }

                stopWatch.Stop();
                //tr.Commit();
            }
            editor.WriteMessage($"\n\n\t\t\t foreach2: {stopWatch.ElapsedMilliseconds,0:#,0.00}\n");

            stopWatch.Reset();
            using (var tr = adb.TransactionManager.StartTransaction())
            {
                stopWatch.Start();
                for (int i = 0; i &amp;lt; count; i++)
                {
                    entity = (Entity)ids[i].GetObject(OpenMode.ForWrite);
                    entity.TransformBy(transform);
                }
                stopWatch.Stop();
                //tr.Commit();
            }
            editor.WriteMessage($"\n\n\t\t\t forloop2: {stopWatch.ElapsedMilliseconds,0:#,0.00}\n\n");
        }&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 20 Jul 2022 10:48:03 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/objectidcollection-transform-to-new-point-in-same-drawing/m-p/11308435#M12315</guid>
      <dc:creator>essam-salah</dc:creator>
      <dc:date>2022-07-20T10:48:03Z</dc:date>
    </item>
  </channel>
</rss>

