.NET

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

Counting Items in a Collection

249 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.

 

 

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 -

*Expert Elite*
Alfred.NESWADBA
Posts: 9,583
Registered: ‎06-29-2007
Message 2 of 6 (246 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 (233 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 (229 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: 338
Registered: ‎05-12-2009
Message 5 of 6 (199 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: 338
Registered: ‎05-12-2009
Message 6 of 6 (189 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
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.