.NET

Reply
Member
guidorooms
Posts: 4
Registered: ‎03-07-2010
Message 1 of 6 (225 Views)
Accepted Solution

Counting Items in a Collection

225 Views, 5 Replies
06-09-2012 02:36 AM

Hi to all,

 

Since collections like for example the LayerTable have no Count property (as far as I can see, that is), I'm doing the following to get at the number of items:

 

Dim LayerCount as integer = 0

For Each Id as ObjectId in SomeLayerTable

    LayerCount +=1

Next

 

Is this the right way to do it, or is there a more efficient way?

 

Thanks in advance for your reply.

 

 

*Expert Elite*
Alfred.NESWADBA
Posts: 8,867
Registered: ‎06-29-2007
Message 2 of 6 (222 Views)

Re: Counting Items in a Collection

06-09-2012 03:04 AM in reply to: guidorooms

Hi,

 

>> Is this the right way to do it

At least it's one of the ways that work.

You should take care of the .IsErased and .IsHidden property as (depending on how you got the layertable) you may count also layers that the user don't see in the layerdialog.

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Member
guidorooms
Posts: 4
Registered: ‎03-07-2010
Message 3 of 6 (209 Views)

Re: Counting Items in a Collection

06-09-2012 07:44 AM in reply to: Alfred.NESWADBA

 "you may count also layers that the user don't see in the layerdialog"

 

Hadn't thought about that! Goodyou mentioned that.

Thanks a lot.

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 6 (205 Views)

Re: Counting Items in a Collection

06-09-2012 08:16 AM in reply to: guidorooms

Yo could be use Linq also, but it would be

a bit slower, e.g

        'Imports System.Linq
        <CommandMethod("cnt", CommandFlags.Modal Or CommandFlags.Session)> _
        Public Shared Sub GetLayerCount()
            Dim doc As Document = acApp.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim ed As Editor = doc.Editor
            Dim LayerCount As Integer = 0
            Using tr As Transaction = db.TransactionManager.StartTransaction

                Dim lt = CType(tr.GetObject(db.LayerTableId, OpenMode.ForRead), LayerTable)
                Dim cnt = (From id As ObjectId In lt
                         Where Not id.IsNull AndAlso Not id.IsErased
                         Select id).Count
                ed.WriteMessage(vbLf + "Layers in the currrent drawing: {0}", cnt)

            End Using
        End Sub

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Valued Mentor
jeff
Posts: 321
Registered: ‎05-12-2009
Message 5 of 6 (175 Views)

Re: Counting Items in a Collection

06-09-2012 02:44 PM in reply to: Hallex

Writing extension methods that return  IEnumrable can be very helpful

 

A example by Tony T 

http://www.theswamp.org/index.php?topic=41311.msg464457#msg464457

 

 

 

No reason to iterate layertable twice but did for this example

 

  [CommandMethod("LayerCount")]
        public void LayerCount()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;


            using (Transaction trx = db.TransactionManager.StartTransaction())
            {

                LayerTable lt = db.GetLayerTable();

                var layers = lt.GetLayerTableRecords()
                    .Where(layer => !layer.IsHidden);

                var erasedLayers = lt.GetLayerTableRecords(OpenMode.ForRead, true)
                    .Where(layer => layer.IsErased);


                ed.WriteMessage("\nLayers " + layers.Count().ToString());
                ed.WriteMessage("\nHidden Layers " + erasedLayers.Count().ToString());

                trx.Commit();
            

            }

        }

 

 

 

You can also find your answers @ TheSwamp
Valued Mentor
jeff
Posts: 321
Registered: ‎05-12-2009
Message 6 of 6 (165 Views)

Re: Counting Items in a Collection

06-09-2012 05:25 PM in reply to: jeff

Did not think that through as usual but the GetLayerTable extension method probably should have overloads that the parameters indicate to include erased objects, and since a LayerTableRecord can't be on a locked layer it could return all unlocked layers instead to resemble GetObject

You can also find your answers @ TheSwamp

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community