count objects on layer

Started by perry59, September 15, 2019, 05:47:45 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

perry59

I have made a function to count how many objects reside on a layer, like the "#" column in the layer properties dialog.
It works, but is SLOW, don't want it in my form initialization.
Is there a better way to do this? (code below)



Function countObjects()
        Dim visDoc As Visio.Document
        Dim visPage As Visio.Page
        Dim visShape As Visio.Shape
        Dim visLayer As Visio.Layer
        Dim count As Integer
        Dim i As Integer

        visDoc = Globals.ThisAddIn.Application.ActiveDocument

        Try
            For Each visPage In visDoc.Pages
                For Each visShape In visPage.Shapes
                    If visShape.LayerCount > 0 Then
                        For i = 1 To visShape.LayerCount
                            visLayer = visShape.Layer(i)
                            If visLayer.Name = _Name Then
                                count = count + 1
                            End If
                        Next
                    End If
                Next visShape
            Next visPage
            _objCount = count
        Catch ex As Exception
        End Try
        Return 0
    End Function
what, me worry?

Nikolay

#1
Just a thought. Not sure if it will work.
You could try use "select by layer" (API) and then just returnitem count

In other words, instead of your code something like (please check the syntax, most probably it's wrong, but I hope that the idea is clear

_objCount = ActivePage.CreateSelection(visSelTypeByLayer, , _Name).Count

It could be faster maybe? Note that the shapes are not really selected in the drawing, so it shouldn't hurt users.

perry59

Hey thanks Nikolay!
great idea, that is WAY faster than what I had.
and it only took about 3 lines of code!
what, me worry?