Select If

Function Syntax (LM:SelectIf <msg> [pred] <func> [keyw])
Current Version 1.0
Arguments
Symbol Type Description
msg String Selection prompt String
pred Function [Optional] Predicate function taking selection list argument
func Function Selection function: entsel, nentsel or enentselp
keyw List [Optional] Initget argument list
Returns
Type Description
List / String Selection List, Keyword, or nil

Program Description

This function continously prompts the user for a selection until either a predicate function is validated or a valid keyword is supplied.

The predicate function should require a single argument: the list returned by the selection function argument (either entsel, nentsel, or nentselp). Upon validation of the predicate function, the entity selection list is returned.

A keyword will be returned if the 'keyw' argument is supplied and the user enters a keyword appearing in the initget argument list.

Select all
;;---------------------=={ Select if }==----------------------;;
;;                                                            ;;
;;  Provides continuous selection prompts until either a      ;;
;;  predicate function is validated or a keyword is supplied. ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright  2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  msg  - prompt string                                      ;;
;;  pred - optional predicate function [selection list arg]   ;;
;;  func - selection function to invoke                       ;;
;;  keyw - optional initget argument list                     ;;
;;------------------------------------------------------------;;
;;  Returns:  Entity selection list, keyword, or nil          ;;
;;------------------------------------------------------------;;

(defun LM:SelectIf ( msg pred func keyw / sel ) (setq pred (eval pred))  
  (while
    (progn (setvar 'ERRNO 0) (if keyw (apply 'initget keyw)) (setq sel (func msg))
      (cond
        ( (= 7 (getvar 'ERRNO))
          (princ "\nMissed, Try again.")
        )
        ( (eq 'STR (type sel))
          nil
        )
        ( (vl-consp sel)
          (if (and pred (not (pred sel)))
            (princ "\nInvalid Object Selected.")
          )
        )
      )
    )
  )
  sel
)

Example Function Calls

Example 1

This first example continously prompts the user until a Line entity is selected. Following a valid selection, the handle of the selected line is printed to the command line.

Select all
(defun c:test1 ( / entity )
  (if
    (setq entity
      (car
        (LM:SelectIf "\nSelect a Line: "
          (lambda ( x ) (eq "LINE" (cdr (assoc 0 (entget (car x)))))) entsel nil
        )
      )
    )
    (princ (strcat "\nHandle of Selected Line: " (cdr (assoc 5 (entget entity)))))
  )
  (princ)
)

Example 2

In this second example, the user may either select a Circle whose centre point will be printed to the command line, or choose to pick an arbitrary point to be printed by specifying the 'Point' keyword at the prompt.

Select all
(defun c:test2 ( / select pt )
  (if
    (and
      (setq select
        (LM:SelectIf "\nSelect Circle [Point]: "
          (lambda ( x ) (eq "CIRCLE" (cdr (assoc 0 (entget (car x)))))) entsel '("Point")
        )
      )
      (or
        (and
          (listp select) (setq pt (cdr (assoc 10 (entget (car select)))))
        )
        (setq pt (getpoint "\nSpecify Point: "))
      )
    )
    (princ (apply 'strcat (mapcar 'strcat '("\nPoint: " "," ",") (mapcar 'rtos pt))))
  )
  (princ)
)

textsize

increase · reset · decrease

Designed & Created by Lee Mac © 2010