best way to process data when iterate each object in modelspace

best way to process data when iterate each object in modelspace

wesbird
Collaborator Collaborator
385 Views
2 Replies
Message 1 of 3

best way to process data when iterate each object in modelspace

wesbird
Collaborator
Collaborator

hi, 

 

I just built a app. now I like to make some re-factory. I found I iterate all objects of whole modelspace in 5 places. it will take lot of time for big drawing. I like to change to scan modelspace once. 

also make the 5 tasks configurable. depend on the config file, I can run 1 task, or 5 tasks. 

how should I design the code? 

 

 

Here is code I use, which should from Scott McFarlane and Tony T.

public static void ForEach<T>(this Database database, Action<T> action)
            where T : Entity
        {
            database.UsingModelSpace((tr, modelSpace) => modelSpace.ForEach(tr, action));
        }

 

 

 

Windows 10 64 bit, AutoCAD (ACA, Map) 2023
0 Likes
386 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor

Assume the argument Action<T> is one of the 5 tasks. Obviously, you need to call 5 times of the Database.ForEach<T>() extension method to get the 5 tasks done with each call needs to loop through the database's ModelSpace. I would not think simply looping through itself takes much time. rather it is what you do in the Action<T> really matters.

 

If you really want to only loop through the modelspace once, the you can change the database extension method signature, by passing in a collection of Action<T>,  like this:

 

public static void ForEach<T>(this Database database, IEnumerable<Action<T>> actions)

{

 ...

}

 

Obviously you also need to modify the extension method 

 

BlockTableRecord.ForEach(this BlockTableRecord space, Transaction tran, IEnumberable<Action<T>> actions)

{

 ...

}

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 3

wesbird
Collaborator
Collaborator

thank you, @norman.yuan:

 

that's what I thought at beginning. but it only work with simple task. for some tasks are really complicate, 

a. before ForEach, I need initialize 

b. during ForEach, I need filter and action, and filter and action are different between task

c. after ForEach, I need do different kind of calculation according to the data I collection in the ForEach

 

what's the best way to solve this? 

 

 

Thank you a million 

Windows 10 64 bit, AutoCAD (ACA, Map) 2023
0 Likes