Author Topic: Change filtering criteria during the selection  (Read 1154 times)

0 Members and 1 Guest are viewing this topic.

Offline (gile)

  • C#
  • *
  • Posts: 87
  • Karma: +8/-0
  • Gender: Male
    • prefered language: F
    • Prog expertise: Good
    • View Profile
Change filtering criteria during the selection
« on: June 30, 2013, 10:04:34 AM »
This is just a example to show how it is possible to change the filtering criteria during the selection using Editor.SelectionAdded event.

In this example, the selection starts filtering lines and allows the user to change the filtered entities to circles or polylines by specifying keywords.

C#
Code: [Select]
using System.Collections.Generic;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

namespace SelectionSample
{
    public class CommandMethods
    {
        private string keyWord;

        [CommandMethod("Test")]
        public void Test()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

            this.keyWord = "LIne";

            PromptSelectionOptions opt = new PromptSelectionOptions();
            opt.SetKeywords("[CIrcle/LIne/POlyline]", "CIrcle LIne POlyline");
            opt.MessageForAdding = "\nSelect objects or " + opt.Keywords.GetDisplayString(true);
            opt.KeywordInput += onKeywordInput;

            ed.SelectionAdded += onSelectionAdded;
            PromptSelectionResult psr = ed.GetSelection(opt);
            ed.SelectionAdded -= onSelectionAdded;
            if (psr.Status == PromptStatus.OK)
                ed.SetImpliedSelection(psr.Value);
        }

        private void onKeywordInput(object sender, SelectionTextInputEventArgs e)
        {
            this.keyWord = e.Input;
        }

        private void onSelectionAdded(object sender, SelectionAddedEventArgs e)
        {
            RXClass rxc;
            switch (this.keyWord)
            {
                case "POlyline": rxc = RXClass.GetClass(typeof(Polyline)); break;
                case "CIrcle": rxc = RXClass.GetClass(typeof(Circle)); break;
                default: rxc = RXClass.GetClass(typeof(Line)); break;
            }
            ObjectId[] ids = e.AddedObjects.GetObjectIds();
            for (int i = 0; i < ids.Length; i++)
            {
                if (ids[i].ObjectClass != rxc)
                    e.Remove(i);
            }
        }
    }
}

VB
Code: [Select]
Imports System.Collections.Generic
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime

Namespace SelectionSample

    Public Class CommandMethods

        Private keyWord As String

        <CommandMethod("Test")> _
        Public Sub Test()
            Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor

            Me.keyWord = "LIne"

            Dim opt As New PromptSelectionOptions()
            opt.SetKeywords("[CIrcle/LIne/POlyline]", "CIrcle LIne POlyline")
            opt.MessageForAdding = vbLf & "Select objects or " & opt.Keywords.GetDisplayString(True)
            AddHandler opt.KeywordInput, AddressOf onKeywordInput

            AddHandler ed.SelectionAdded, AddressOf onSelectionAdded
            Dim psr As PromptSelectionResult = ed.GetSelection(opt)
            RemoveHandler ed.SelectionAdded, AddressOf onSelectionAdded
            If psr.Status = PromptStatus.OK Then
                ed.SetImpliedSelection(psr.Value)
            End If
        End Sub

        Private Sub onKeywordInput(sender As Object, e As SelectionTextInputEventArgs)
            Me.keyWord = e.Input
        End Sub

        Private Sub onSelectionAdded(sender As Object, e As SelectionAddedEventArgs)
            Dim rxc As RXClass
            Select Case Me.keyWord
                Case "POlyline"
                    rxc = RXClass.GetClass(GetType(Polyline))
                    Exit Select
                Case "CIrcle"
                    rxc = RXClass.GetClass(GetType(Circle))
                    Exit Select
                Case Else
                    rxc = RXClass.GetClass(GetType(Line))
                    Exit Select
            End Select
            Dim ids As ObjectId() = e.AddedObjects.GetObjectIds()
            For i As Integer = 0 To ids.Length - 1
                If ids(i).ObjectClass <> rxc Then
                    e.Remove(i)
                End If
            Next
        End Sub

    End Class

End Namespace

F#
Code: [Select]
module SelectionSample

open System.Collections.Generic
open Autodesk.AutoCAD.ApplicationServices
open Autodesk.AutoCAD.DatabaseServices
open Autodesk.AutoCAD.EditorInput
open Autodesk.AutoCAD.Runtime

[<CommandMethod("Test")>]
let test () =
    let ed = Application.DocumentManager.MdiActiveDocument.Editor

    let keyWord = ref "LIne"

    let opt = new PromptSelectionOptions()
    opt.SetKeywords("[CIrcle/LIne/POlyline]", "CIrcle LIne POlyline")
    opt.MessageForAdding <- "\nSelect objects or " + opt.Keywords.GetDisplayString(true)
    opt.KeywordInput.Add(fun (e: SelectionTextInputEventArgs) -> keyWord := e.Input)

    let onSelectionAdded =
        new SelectionAddedEventHandler(fun _ e ->
            let rxc =
                RXClass.GetClass(
                    match !keyWord with
                    | "CIrcle"   -> typeof<Circle>
                    | "POlyline" -> typeof<Polyline>
                    | _          -> typeof<Line>)
            e.AddedObjects.GetObjectIds()
            |> Array.iteri(fun i id -> if id.ObjectClass <> rxc then e.Remove(i)))

    ed.SelectionAdded.AddHandler(onSelectionAdded)
    let psr = ed.GetSelection(opt)
    ed.SelectionAdded.RemoveHandler(onSelectionAdded)
    if psr.Status = PromptStatus.OK then
        ed.SetImpliedSelection(psr.Value)
« Last Edit: June 30, 2013, 10:11:16 AM by (gile) »