ObjectIdCollection transform to new point in same drawing

ObjectIdCollection transform to new point in same drawing

manohar2375
Advocate Advocate
1,240 Views
8 Replies
Message 1 of 9

ObjectIdCollection transform to new point in same drawing

manohar2375
Advocate
Advocate

Hello Expertise,

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.

ent.TransformBy(Matrix3d.Displacement(new Vector3d(ptX, ptY, 0)));

But needs help to transform/move list of ObjectIds at a time in the same drawing.

 

Thanks in Advance.

 

 

 

0 Likes
1,241 Views
8 Replies
Replies (8)
Message 2 of 9

_gile
Consultant
Consultant

Hi,

 

Simply use  a foreach loop.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 9

manohar2375
Advocate
Advocate

Hi _gile,

Thanks for your reply with suggestion.

 

But, Is there any possibility to transform list of ObjectIds at a time without use foreach or any other loop.

 

Thanks in Advance.

0 Likes
Message 4 of 9

Ed__Jobe
Mentor
Mentor

Put the foreach in a function method or an extension method and then it acts like a single step.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 5 of 9

manohar2375
Advocate
Advocate

Hi Ed.Jobe,

Thanks for reply with suggestion.

 

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.

For Example:

to capture all elements, will use selection set as below.

PromptSelectionResult res = ed.SelectCrossingPolygon(pnt3dColl, filter);

so, like that, is there any other way.

 

Thanks in Advance.

0 Likes
Message 6 of 9

Ed__Jobe
Mentor
Mentor

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.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 7 of 9

essam-salah
Advisor
Advisor

for loop will be faster than foreach

 

            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 < count; i++)
            {
                entity = ids[i].GetObject(OpenMode.ForWrite) as Entity;
                entity.TransformBy(transform);
            }
0 Likes
Message 8 of 9

_gile
Consultant
Consultant

@essam-salah  a écrit :

for loop will be faster than foreach


Are you sure about this ?

Did you test-it by yourself ?

If so, I should be interseted by how much faster is it ?

 

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.
A direct cast:

entity = (Entity)ids[i].GetObject(OpenMode.ForWrite);

is faster than a 'try cast' (which should only be used in conjunction with a : if (entity != null) ...

entity = ids[i].GetObject(OpenMode.ForWrite) as Entity;

See this dicussion.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 9 of 9

essam-salah
Advisor
Advisor

hi @_gile 

thanks for the tip about Try Cast i always find smth helpful in your posts,

i tried both methods and here is the result:

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

 

        [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 < 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 < 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");
        }
0 Likes