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.
Solved! Go to Solution.
Solved by Alfred.NESWADBA. Go to Solution.
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 -
"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.
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'~
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(); } }
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
Can't find what you're looking for? Ask the community or share your knowledge.