Author Topic: subfunction entsel  (Read 1804 times)

0 Members and 1 Guest are viewing this topic.

amc.dicsac

  • Newt
  • Posts: 109
  • Autocad 2008
subfunction entsel
« on: May 05, 2016, 05:11:11 PM »
Hello such, I want to create a subfunction with entsel but I can not exit the loop.

Code - Auto/Visual Lisp: [Select]
  1. (defun *2ApLEnt* (Mnsj Elemento / ent Entype)
  2.   (while (setq ent  (car (entsel Mnsj)))
  3.     (setq Entype (cdr (assoc 0 (entget ent))))
  4.       (/= Entype Elemento) (prompt "\nError in the selection, try again. ")))
  5.  
  6.  
  7. (defun c:peu ()
  8.   (*2ApLEnt* "\nSelect object: " "CIRCLE"))
<a href="http:/http://axprogramlisp.blogspot.pe" class="bbc_link" target="_blank">By Alexander Castro</a>

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: subfunction entsel
« Reply #1 on: May 05, 2016, 05:16:11 PM »
The current test expression for your while loop causes the loop to continue evaluation if (car (entsel)) is non-nil, i.e. if the user has selected an object, regardless of any other condition.

You will therefore need to include the expression (/= Entype Elemento) as part of the test expression:
Code - Auto/Visual Lisp: [Select]
  1. (defun *2ApLEnt* ( Mnsj Elemento / ent )
  2.     (while
  3.         (and
  4.             (setq ent (car (entsel Mnsj)))
  5.             (/= (cdr (assoc 0 (entget ent))) Elemento)
  6.         )
  7.         (prompt "\nError in the selection, try again. ")
  8.     )
  9.     ent
  10. )

amc.dicsac

  • Newt
  • Posts: 109
  • Autocad 2008
Re: subfunction entsel
« Reply #2 on: May 05, 2016, 05:22:46 PM »
Thank you very much for the help Lee 
<a href="http:/http://axprogramlisp.blogspot.pe" class="bbc_link" target="_blank">By Alexander Castro</a>

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: subfunction entsel
« Reply #3 on: May 06, 2016, 03:23:01 AM »
Try this:
Code: [Select]
; Marc'Antonio Alessi, Italy - http://xoomer.virgilio.it/alessi
;
; Function: ALE_EntSelFilter
;
; Version 1.00 - 22/02/2005
; Version 1.01 - 04/03/2006 - added (setvar "ERRNO" 0)
;
; Description:
;   Ssget for one entity with filter list
;
; Arguments:
;   PrmStr = User prompt [STR]
;   FltLst = ssget filter list [LIST]
;
; Example:
;  (ALE_EntSelFilter "Select attibuted block" '((0 . "INSERT") (66 . 1)))
;
; Return Values:
;   [ENAME] Entity name
;   nil if user press 'Return' or 'Space'
;
(defun ALE_EntSelFilter (PrmStr FltLst / FlgSlt EntNam)
    (setvar "ERRNO" 0)
    (princ "\n_ ")
    (prompt (setq PrmStr (strcat "\n" PrmStr ": ")))
    (if
      (while (not FlgSlt)
        (if (setq EntNam (ssget "_:E:S" FltLst))
          (not (setq FlgSlt T))
          (if (= 52 (getvar "ERRNO"))
            (setq FlgSlt T)
;|e|;       (prompt (strcat "\nNo objects selected or valid, try again!" PrmStr))
          )
        )
      )
;|e|; (not (princ "\nFunction cancelled.  "))
      (ssname EntNam 0)
    )
)
;
; Marc'Antonio Alessi, Italy - http://xoomer.virgilio.it/alessi
;
; Function: ALE_N-EntSelEntity
;
; Version 1.00 - 22/02/2005
; Version 1.01 - 04/03/2006 - added (setvar "ERRNO" 0)
;
; Description:
;   enhanced EntSel or NentSel
;
; Arguments:
;   PrmStr = User prompt    [STR]
;   SelTyp = Selection type [SYM]: 'entsel or 'nentsel
;   WcMStr = Wcmatch string [STR]
;       ex.: "Line,*Polyline,Arc,Circle,Ellipse,Spline"
;
; Example:
;  (ALE_N-EntSelEntity "Select block" 'entsel "Insert")
;  (ALE_N-EntSelEntity "Select text or attrib (also nested)" 'nentsel "Text,Attrib")
;
; Return Values:
;   [LIST]: see entsel or nentsel
;   nil if user press 'Return' or 'Space'
;
(defun ALE_N-EntSelEntity (PrmStr SelTyp WcMStr / FlgSlt SelLst)
    (setvar "ERRNO" 0)
    (princ "\n_ ")
    (setq PrmStr (strcat "\n" PrmStr ": "))
    (if
      (while (not FlgSlt)
        (if
          (and
            (setq SelLst ((eval SelTyp) PrmStr))
            (wcmatch (DXF 0 (entget (car SelLst))) (strcase WcMStr))
          )
          (not (setq FlgSlt T))
          (if (= 52 (getvar "ERRNO"))
            (setq FlgSlt T)
;|e|;       (princ (strcat "\nNo entity selected or it is not a: " WcMStr "!"))
          )
        )
      )
;|e|; (not (princ "\nFunction cancelled.  "))
      SelLst
    )
)

ChrisCarlson

  • Guest
Re: subfunction entsel
« Reply #4 on: May 06, 2016, 08:05:05 AM »
This is probably my favorite routine

http://www.lee-mac.com/selectif.html

Code - Auto/Visual Lisp: [Select]
  1. ;;---------------------=={ Select if }==----------------------;;
  2. ;;                                                            ;;
  3. ;;  Provides continuous selection prompts until either a      ;;
  4. ;;  predicate function is validated or a keyword is supplied. ;;
  5. ;;------------------------------------------------------------;;
  6. ;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
  7. ;;------------------------------------------------------------;;
  8. ;;  Arguments:                                                ;;
  9. ;;  msg  - prompt string                                      ;;
  10. ;;  pred - optional predicate function [selection list arg]   ;;
  11. ;;  func - selection function to invoke                       ;;
  12. ;;  keyw - optional initget argument list                     ;;
  13. ;;------------------------------------------------------------;;
  14. ;;  Returns:  Entity selection list, keyword, or nil          ;;
  15. ;;------------------------------------------------------------;;
  16.  
  17. (defun LM:SelectIf ( msg pred func keyw / sel ) (setq pred (eval pred))  
  18.   (while
  19.     (progn (setvar 'ERRNO 0) (if keyw (apply 'initget keyw)) (setq sel (func msg))
  20.       (cond
  21.         ( (= 7 (getvar 'ERRNO))
  22.           (princ "\nMissed, Try again.")
  23.         )
  24.         ( (eq 'STR (type sel))
  25.           nil
  26.         )
  27.         ( (vl-consp sel)
  28.           (if (and pred (not (pred sel)))
  29.             (princ "\nInvalid Object Selected.")
  30.           )
  31.         )
  32.       )
  33.     )
  34.   )
  35.   sel
  36. )

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test1 ( / entity )
  2.   (if
  3.     (setq entity
  4.       (car
  5.         (LM:SelectIf "\nSelect a Line: "
  6.           (lambda ( x ) (eq "LINE" (cdr (assoc 0 (entget (car x)))))) entsel nil
  7.         )
  8.       )
  9.     )
  10.     (princ (strcat "\nHandle of Selected Line: " (cdr (assoc 5 (entget entity)))))
  11.   )
  12.   (princ)
  13. )