Author Topic: Count selection real-time  (Read 3155 times)

0 Members and 1 Guest are viewing this topic.

Eycki

  • Guest
Count selection real-time
« on: December 02, 2011, 07:33:48 AM »
Hello, I'm new here to the forum

I have a question: Is it possible to count your selection realtime (when making a fence in AutoCAD) ?
For instance, if you do a copy command in AutoCAD, you will see real-time on the command line how many you have selected.
e.g.:
1 found
2 found, 3 total

I would like to add these counted elements to a progressbar value.

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Count selection real-time
« Reply #1 on: December 02, 2011, 09:16:36 AM »
Welcome.  Editor.SelectionAdded Event  might give you what you need

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Count selection real-time
« Reply #2 on: December 02, 2011, 10:16:16 AM »
Code: [Select]
[CommandMethod("ssetwithcount")]
        static public void SSet()
        {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            ed.SelectionAdded += new SelectionAddedEventHandler(sAdded);
            PromptSelectionOptions SSO = new PromptSelectionOptions();
            SSO.AllowDuplicates = false;
            SSO.MessageForAdding = "Select:";
            PromptSelectionResult PSR = ed.GetSelection(SSO);
            if (PSR.Status != PromptStatus.OK) return;
            SelectionSet SS = PSR.Value;
            int i=0;


            foreach (SelectedObject so in SS)
                i++;
            ed.WriteMessage("You selected " + i.ToString() + " objects");
         
           ed.SelectionAdded -= new SelectionAddedEventHandler(sAdded);
        }

        private static void sAdded(object sender, SelectionAddedEventArgs e)
        {
            SelectionSet ss = e.AddedObjects;
            int i = 0;
            foreach (SelectedObject so in ss)
                i++;

            acadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("You added " + i.ToString() + " objects");
           
        }

Eycki

  • Guest
Re: Count selection real-time
« Reply #3 on: December 09, 2011, 07:12:22 AM »
Thanks a lot for the code, I converted it to VB.NET, it all works fine exept that "SelectionAdded" doesn't seem to be a part of the editor :s  What am I doing wrong?   If I type it straight after doc.editor.selectionadded it does exist but not like ed.selectionadded.

Look my code below in VB.NET
Thx in advance!
Code: [Select]
<CommandMethod("ssetwithcount")> _
    Public Sub SSet()
        Dim doc = Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        ed.SelectionAdded += New SelectionAddedEventHandler(AddressOf sAdded)
        Dim SSO As New PromptSelectionOptions()
        SSO.AllowDuplicates = False
        SSO.MessageForAdding = "Select:"
        Dim PSR As PromptSelectionResult = ed.GetSelection(SSO)
        If PSR.Status <> PromptStatus.OK Then
            Return
        End If
        Dim SS As SelectionSet = PSR.Value
        Dim i As Integer = 0


        For Each so As SelectedObject In SS
            i += 1
        Next
        ed.WriteMessage("You selected " & i.ToString() & " objects")

        ed.SelectionAdded -= New SelectionAddedEventHandler(AddressOf sAdded)
    End Sub

    Private Sub sAdded(ByVal sender As Object, ByVal e As SelectionAddedEventArgs)
        Dim ss As SelectionSet = e.AddedObjects
        Dim i As Integer = 0
        For Each so As SelectedObject In ss
            i += 1
        Next

        Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("You added " & i.ToString() & " objects")

    End Sub

kaefer

  • Guest
Re: Count selection real-time
« Reply #4 on: December 09, 2011, 07:49:18 AM »
What am I doing wrong?

Code: [Select]
        AddHandler ed.SelectionAdded, New SelectionAddedEventHandler(AddressOf sAdded)
        Try
            ... do your thing here ...
        Finally
            RemoveHandler ed.SelectionAdded, New SelectionAddedEventHandler(AddressOf sAdded)
        End Try

And BTW, maybe you'd like to look into the SelectionRemoved event too.

Eycki

  • Guest
Re: Count selection real-time
« Reply #5 on: December 09, 2011, 09:50:44 AM »
What am I doing wrong?

Code: [Select]
        AddHandler ed.SelectionAdded, New SelectionAddedEventHandler(AddressOf sAdded)
        Try
            ... do your thing here ...
        Finally
            RemoveHandler ed.SelectionAdded, New SelectionAddedEventHandler(AddressOf sAdded)
        End Try

And BTW, maybe you'd like to look into the SelectionRemoved event too.

That's it!  Thank you so much for the help.