.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Counting Items in a Collection

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
guidorooms
540 Views, 5 Replies

Counting Items in a Collection

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.

 

 

5 REPLIES 5
Message 2 of 6

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 ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 3 of 6

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

Message 4 of 6
Hallex
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
Message 5 of 6
jeff
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
Message 6 of 6
jeff
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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost