Author Topic: Improving on entsel  (Read 14891 times)

0 Members and 1 Guest are viewing this topic.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Improving on entsel
« Reply #30 on: February 28, 2008, 03:21:55 PM »
Don't know what I was doing wrong, but that works nice, Ron. Thanks.

You're welcome  :-) (although Chuck Gabriel did all the work)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

whdjr

  • Guest
Re: Improving on entsel
« Reply #31 on: February 28, 2008, 04:06:20 PM »
I'm late to the party as always (or uninvited).
Here's my version.

Code: [Select]
;;;---------------------------------------------------------------------;;;
;;;A rewrite of the entsel function.                                    ;;;
;;;---------------------------------------------------------------------;;;
  (defun ent_sel (msg / ent)
    (while (not ent)
      (cond ((setq ent (entsel msg)))
    ((= (getvar "ErrNo") 7)
     (princ "\nSelection missed.  Please try again.")
    )
    ((= (getvar "ErrNo") 52) (exit))
      )
    )
    ent
  )

daron

  • Guest
Re: Improving on entsel
« Reply #32 on: February 28, 2008, 04:22:13 PM »
Don't know what I was doing wrong, but that works nice, Ron. Thanks.

You're welcome  :-) (although Chuck Gabriel did all the work)
I'm aware, but I was thanking you for answering my part of the question. It was seemingly overlooked.

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
Re: Improving on entsel
« Reply #33 on: February 28, 2008, 05:00:28 PM »
Im a bit late and unprepaird (i didnt read this thread so far -- you guys talk a lot!).

Whats wrong with a simple recursive test?!

Code: [Select]
(defun entse7 ( msg )
  ;; EntSe7
  ;; dont allow null pick
  ;; EX:
  ;;    (entse7 "Select something or you DIE! ")
  ;;  >: ent picked
  (cond
    ( (entsel (strcat "\n" msg)))
    ( T (entse7 msg))) )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Chuck Gabriel

  • Guest
Re: Improving on entsel
« Reply #34 on: February 28, 2008, 06:12:23 PM »
Im a bit late and unprepaird (i didnt read this thread so far -- you guys talk a lot!).

Whats wrong with a simple recursive test?!

Code: [Select]
(defun entse7 ( msg )
  ;; EntSe7
  ;; dont allow null pick
  ;; EX:
  ;;    (entse7 "Select something or you DIE! ")
  ;;  >: ent picked
  (cond
    ( (entsel (strcat "\n" msg)))
    ( T (entse7 msg))) )

I don't recall.  Does AutoLISP do tail call optimization.  If it does, then I guess recursion is just as good as iteration.  However, some of us, like myself, just reach for the looping constructs out of habit.  Probably baggage from other programming languages.

Incidentally, no one has asked me why I used the vl-catch-all-apply, but I noticed many of the other solutions posted don't.  I did it because I want to be able to recover and return something meaningful to the calling function if the user presses the escape key.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Improving on entsel
« Reply #35 on: February 28, 2008, 06:50:22 PM »

Nice clean solution Chuck.

My method is posted (or similar) to the thread that CAB referenced, and a couple of other places here . .. allows for nested selection (optional) and allows for an eType list ("LINE" "ARC")
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Chuck Gabriel

  • Guest
Re: Improving on entsel
« Reply #36 on: February 28, 2008, 06:54:36 PM »
Thanks Kerry.

Those are good ideas (nested selection and eType list).  I hadn't looked beyond my immediate needs, but I can definitely imagine putting those features to work.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Improving on entsel
« Reply #37 on: February 28, 2008, 07:34:08 PM »
This one needs a list of etypes & honors wcmatch characters.
AND has feed back messages. 8-)
Code: [Select]
(defun getEntity (msg ; string - select message
                  etypes ; list pf types '("LINE" "CIRCLE")
                  /
                  ent
                 )
  (setvar "ERRNO" 0)
  (if
    (vl-catch-all-error-p
      (vl-catch-all-apply
        '(lambda ()
           (while
             (and
               (/= 52 (getvar "ERRNO"))
               (or
                 (and (null (setq ent (entsel msg)))
                      (princ "\nMissed, Try Again.")
                 )
                 (and
                   etypes
                   (and
                     (not (vl-some
                            '(lambda(x)
                               (wcmatch (cdr (assoc 0 (entget (car ent)))) x))
                                      (mapcar 'strcase etypes)))
                     (princ "\nWrong entity type, Try Again.")
                   )
                 )
               )
             )
           )
         )
      )
    )
     nil
     ent
  )
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
Re: Improving on entsel
« Reply #38 on: February 28, 2008, 07:44:17 PM »
I don't recall.  Does AutoLISP do tail call optimization.  If it does, then I guess recursion is just as good as iteration.  However, some of us, like myself, just reach for the looping constructs out of habit.  Probably baggage from other programming languages.

Incidentally, no one has asked me why I used the vl-catch-all-apply, but I noticed many of the other solutions posted don't.  I did it because I want to be able to recover and return something meaningful to the calling function if the user presses the escape key.
*blink-blink*
If the user chews up his or her stack with the task of selecting an object, then that is their problem, not mine.

IMO, recursion is the simplest form of a loop.

But seriously though, i was just offering a suggestion of simplicity. You know more about what your doing then i do. You guys are already way over my head.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Chuck Gabriel

  • Guest
Re: Improving on entsel
« Reply #39 on: February 28, 2008, 08:38:58 PM »
*blink-blink*
If the user chews up his or her stack with the task of selecting an object, then that is their problem, not mine.

IMO, recursion is the simplest form of a loop.

But seriously though, i was just offering a suggestion of simplicity. You know more about what your doing then i do. You guys are already way over my head.

It was a perfectly good suggestion.  It's funny though how recursion feels simpler to some people, while others feel more comfortable with looping constructs.  I'm pretty much equally at home with either, but I seem to have a natural tendency to go with a looping construct for some reason.

Chuck Gabriel

  • Guest
Re: Improving on entsel
« Reply #40 on: February 28, 2008, 08:45:25 PM »
This one needs a list of etypes & honors wcmatch characters.
AND has feed back messages. 8-)

That's a good one.  I like the fact that you included feedback.  I tend to be lazy about that sort of thing because I forget that people other than me might use my programs at some point.  In other words my programs are only user-friendly if the user is me. :D

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Improving on entsel
« Reply #41 on: February 28, 2008, 09:24:17 PM »

Chuck, this is probably surplus to your needs .. :-)
Code: [Select]
;;;------------------------------------------------------------------
;;;------------------------------------------------------------------
;;;
;;; kwb 20021103
;;; KDUB:entsel (msg kwd def typelist selectflag lockflag

;;; Arguments:
;;; msg : The prompt string.
;;; kwd : Initget keywords string.
;;; def : Value to return if response is <enter>.
;;; typelist  : Stringlist of entity types allowed for selection. If nil select anything.
;;; selectflag : If true nentsel permitted , otherwise use entsel.
;;; lockflag  : If true dont allow selection from locked layers.
;;;
;;; Note : Arguments may be set to nil
;;;
;;; Return output from (n)entsel, a key word, the default argument, or nil.
;;;
;; example1 : (KDUB:EntSel "Select Arc Object" nil nil (list "ARC" "CIRCLE") nil T)
;;  ==>  (<Entity name: 40bcd540> (-28175.1 154575.0 1250.0))
;; example2 : (KDUB:EntSel "Select Datum Line" nil nil (list "LINE") T T) ; line in block
;;  ==>  (<Entity name: 4022c680> (-21613.1 142392.0 0.0)
;;  ((70.0 0.0 0.0) (0.0 70.0 0.0) (0.0 0.0 70.0) (-21611.9 142635.0 0.0))
;;          (<Entity name: 4022c6b8>)  )

(defun kdub:entsel (msg        kwd        def        typelist
                    selectflag lockflag   /          pickok
                    returnvalue           tmp
                   )
  (setq msg (strcat "\n"
                    (cond (msg)
                          ("Select object")
                    )
                    " : "
            )
  )
  (while (not pickok)
    (setvar "ERRNO" 0)
    (if kwd
      (initget kwd)
    )
    (setq returnvalue (if selectflag
                        (nentsel msg)
                        (entsel msg)
                      )
    )
    (cond
      ((= (getvar "ERRNO") 52)               ; enter
       (if def
         (setq returnvalue def)
       )
       ;; skip out
       (setq pickok t)
      )
      ((= (getvar "ERRNO") 7)
       (princ "Nothing found at selectedpoint. ")
      )
      ((= (type returnvalue) 'str) (setq pickok t)) ; keyword
      ((and
         (setq tmp (entget (car returnvalue))) ; object type
         typelist
         (not (member (cdr (assoc 0 tmp)) (mapcar 'strcase typelist)))
       )                                     ; wrong type
       (alert
         (strcat "Selected object is not"
                 "\na "
                 (apply 'strcat
                        (cons (car typelist)
                              (mapcar '(lambda (x) (strcat "\nor " x))
                                      (cdr typelist)
                              )
                        )
                 )
                 ". "
         )
       )
      )
      ((and lockflag                         ;Locked Layer Not Permitted
            (setq                            ;layer name
              tmp (entget (tblobjname "LAYER" (cdr (assoc 8 tmp))))
            )
            (= (logand 4 (cdr (assoc 70 tmp))) 4) ;is layer locked
       )
       (princ "Selected object is on a locked layer. ")
      )
      ;; skip out
      ((setq pickok t))
    )
  )
  returnvalue
)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

hermanm

  • Guest
Re: Improving on entsel
« Reply #42 on: February 28, 2008, 11:53:39 PM »
Here is something  I wrote recently.

Code: [Select]
;;function to mark checked annotation objects
;;supports ATTRIB,TEXT,MTEXT,& MTEXT in DIMENSIONs
;;
(defun C:OK ( /
                code
                count
                etype
                obj
                retval
                stypes)
  (setq stypes '("ATTRIB" "MTEXT" "TEXT");define supported types
        count 0)
  (prompt "\nMark as Checked <U to Undo, any other key to exit>:")
  ;;grread to acquire points
  (while (or(> (setq code (car (setq retval (grread nil 14 2))))2);not keyboard
            (and (= 2 code) (member (cadr retval) '(85 117))));key u or U
     (cond ((= 2 code);undo one step
            (if (> count 0)
              (progn
                (remove_image (entlast))
                (setq count (1- count))
              );progn
                (princ "\nNothing to Undo.")
            );if
           );pred
        ((= 3 code);point selected
          (and
          (setq obj (car (nentselp (cadr retval))));object found
          (member (setq etype (cdr (assoc 0 (setq obj(entget obj))))) stypes);supported type
          ;;process it
          (mark_as_checked obj)
          (setq count (1+ count))
          );and
        );pred
     );cond
  );while
  (princ)
);end OK

taner

  • Guest
Re: Improving on entsel
« Reply #43 on: February 29, 2008, 03:09:55 AM »
(defun uentsel (msg / en)
  (while (null (setq en (entsel (strcat "\nPichk up " msg ":"))))
    (princ "\nNothing selected,Try again.")
  )
  en
)

Chuck Gabriel

  • Guest
Re: Improving on entsel
« Reply #44 on: February 29, 2008, 08:49:17 AM »
I know we've just about beaten this one to death, but I thought I'd throw out on more thing:

Code: [Select]
(defun cci:getEntity (msg eTypes / ent)
  (or
    (null eTypes)
    (listp eTypes)
    (setq eTypes (list eTypes))
  )
  (setvar "ERRNO" 0)
  (if
    (vl-catch-all-error-p
      (vl-catch-all-apply
'(lambda ()
   (while
     (and
       (/= 52 (getvar "ERRNO"))
       (or
(and (null (setq ent (entsel msg)))
      (or
(= 52 (getvar "ERRNO"))
(princ "\nMissed. Try again.")
      )
)
(and
   eTypes
   (and
     (not
       (vl-some
'(lambda (pattern)
    (wcmatch
      (cdr (assoc 0 (entget (car ent))))
      pattern
    )
  )
(mapcar 'strcase etypes)
       )
     )
     (princ "\nWrong entity type. Try Again.")
   )
)
       )
     )
   )
)
      )
    )
     nil
     ent
  )
)

I just took the last version CAB posted, added some code to allow the caller to pass eTypes as a single string when a list isn't called for, and added a couple of lines to prevent the missed pick feedback from printing when the user terminates by pressing enter.

For now, I'll leave it up to the caller to deal with locked layers and dig into complex entity data.


Thanks again everybody for all your input.