Conditional Selection Filter for Entities on Multiple Layers

By Madhukar Moogala

This post shows a simple way of drafting your selection filter, if you want to select a single entity of type A on specific layer L, and another entity of type B on several layers L1, L2, L3…. so on.

Lisp Code:
(defun C:FCTest ()
  (setq sel1
    (ssget "_X"
      '(
        (-4 . "")
          (-4 . "")
          (-4 . "AND>")
        (-4 . "OR>")
      )
    )
  )
  (sslength sel1)
)
.NET code
public static void FC()
{
    // Get the current document editor
    Editor acDocEd = Autodesk.AutoCAD.ApplicationServices.Core.Application
                                .DocumentManager.MdiActiveDocument.Editor;

    // Create a TypedValue array to define the filter criteria
    // All objects meeting any of the Four criteria will get selected
    TypedValue[] acTypValAr = {
        new TypedValue((int)DxfCode.Operator, ""),
        // Criteria 2
        new TypedValue((int)DxfCode.Operator, ""),
        new TypedValue((int)DxfCode.Operator, "and>"),
        new TypedValue((int)DxfCode.Operator, "or>")
    };

    // Assign the filter criteria to a SelectionFilter object
    SelectionFilter acSelFtr = new SelectionFilter(acTypValAr);

    // Request for objects to be selected in the drawing area
    PromptSelectionResult acSSPrompt;
    acSSPrompt = acDocEd.GetSelection(acSelFtr);

    // If the prompt status is OK, objects were selected
    if (acSSPrompt.Status == PromptStatus.OK)
    {
        SelectionSet acSSet = acSSPrompt.Value;
        Autodesk.AutoCAD.ApplicationServices.Core.Application
            .ShowAlertDialog("Number of objects selected: " + acSSet.Count.ToString());
    }
    else
    {
        Autodesk.AutoCAD.ApplicationServices.Core.Application
            .ShowAlertDialog("Number of objects selected: 0");
    }
}

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading