Author Topic: How to stop while loop?  (Read 2639 times)

0 Members and 1 Guest are viewing this topic.

HasanCAD

  • Swamp Rat
  • Posts: 1422
How to stop while loop?
« on: October 09, 2013, 07:02:46 AM »
I coaded this lisp but cant stop while loop.
Please help
Code: [Select]
(defun c:ctxt ( / _value data txt1 txtn )
  (vl-load-com)
  (setq data " ")

  (defun _value (str / ATTr)
    (while
      (not
(if (and (setq ATTr (car (nentsel str)))
(member (vla-get-objectname (setq ATTr (vlax-ename->vla-object ATTr))) '("AcDbAttribute" "AcDbText")))
  ATTr
  (progn (princ str) nil)))) ATTr)
 
  (if (setq txt1 (vla-get-textstring (_value "\nSelect First attribute/Text : ")))
    (progn
      (while
(setq txtn (vla-get-textstring (_value "\nSelect Next attribute/Text : ")))
(setq data (strcat data " + " txtn)))
      (setq data (strcat txt1 data ))))
 
  (setq ip (getpoint "\nPick Text insertion point"))

   (entmakex (list (cons 0 "TEXT")
                  (cons 10  ip)
                  (cons 40 (getvar "textsize"))
                  (cons 1  data)))
  )

(princ " Type   CTXT    to invoke the lisp")

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: How to stop while loop?
« Reply #1 on: October 09, 2013, 08:52:05 AM »
I think I would do the routine like this: (quickly done)
Code - Auto/Visual Lisp: [Select]
  1. (defun c:ctxt ( / done ent ip string1 string2)
  2.  
  3.   (defun _getString (ent / obj subent)
  4.     (setq obj (vlax-ename->vla-object (car ent)))
  5.     (cond
  6.       ((vlax-property-available-p obj 'textstring) (vla-get-textstring obj))
  7.       ((null (setq subent (nentselp (cadr ent)))) nil)
  8.       ((and subent
  9.             (setq obj (vlax-ename->vla-object (car subent))))
  10.        (if (vlax-property-available-p obj 'textstring)
  11.          (vla-get-textstring obj)
  12.        )
  13.       )
  14.     )
  15.   )
  16.  
  17. (while (not done)
  18.   (cond
  19.     ((and (null string1)
  20.           (setq ent (entsel "\nSelect First attribute/Text : ")))
  21.       (setq string1 (_getString ent))
  22.      )
  23.     ((and string1 (null string2)
  24.           (setq ent (entsel "\nSelect Next attribute/Text : "))
  25.           (setq string2 (_getString ent)))
  26.      )
  27.     ((and string1 string2 (setq ip (getpoint "\nPick Text insertion point")))
  28.       (entmakex (list (cons 0 "TEXT")
  29.                   (cons 10  ip)
  30.                   (cons 40 (getvar "textsize"))
  31.                   (cons 1  (strcat string1 " + " string2))))
  32.      (setq done t)
  33.      )
  34.    ) ; cond
  35. ) ; while
  36.   (princ)
  37. )
  38.  
  39. (princ " Type   CTXT    to invoke the lisp")
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.

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: How to stop while loop?
« Reply #2 on: October 09, 2013, 09:06:57 AM »
I think I would do the routine like this: (quickly done)
Working good but the routine alow to select one more text
I want to pick more than one text
see attached


Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: How to stop while loop?
« Reply #3 on: October 09, 2013, 09:08:22 AM »
Try something like this for your loop
Code: [Select]
(while (not ATTr)
  (if (not (and (setq ATTr (car (nentsel str)))
(member
  (vla-get-objectname
    (setq ATTr (vlax-ename->vla-object ATTr))
  )
  '("AcDbAttribute" "AcDbText")
)
   )
      )
    (setq ATTr nil)
  )
)

As long as ATTr is nil, the loop will continue to run, once there is a valid value in ATTr the loop will exit.

You will need to ensure that ATTr is always nil upon entering the loop.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: How to stop while loop?
« Reply #4 on: October 09, 2013, 09:17:51 AM »
Another quick draft:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:ctxt ( / _entsel ins nxt str )
  2.    
  3.     (defun _entsel ( msg / ent )
  4.         (while
  5.             (progn
  6.                 (setvar 'errno 0)
  7.                 (setq ent (car (nentsel msg)))
  8.                 (cond
  9.                     (   (= 7 (getvar 'errno))
  10.                         (princ "\nMissed, try again.")
  11.                     )
  12.                     (   (= 'ename (type ent))
  13.                         (if (not (wcmatch (cdr (assoc 0 (entget ent))) "TEXT,ATTRIB"))
  14.                             (princ "\nInvalid object selected.")
  15.                         )
  16.                     )
  17.                 )
  18.             )
  19.         )
  20.         (if ent (cdr (assoc 1 (entget ent))))
  21.     )
  22.  
  23.     (if (setq str (_entsel "\nSelect first attribute/text: "))
  24.         (progn
  25.             (while (setq nxt (_entsel "\nSelect next attribute/text <Done>: "))
  26.                 (setq str (strcat str " + " nxt))
  27.             )
  28.             (if (setq ins (getpoint "\nPick text insertion point: "))
  29.                 (entmake
  30.                     (list
  31.                        '(0 . "TEXT")
  32.                         (cons 10 (trans ins 1 0))
  33.                         (cons 40 (getvar 'textsize))
  34.                         (cons 01 str)
  35.                     )
  36.                 )
  37.             )
  38.         )
  39.     )
  40.     (princ)
  41. )

Only compatible in UCS parallel to WCS.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: How to stop while loop?
« Reply #5 on: October 09, 2013, 09:24:40 AM »
My rework:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:ctxt ( / done ent ip string1 string2)
  2.  
  3.   (defun _getString (ent / obj subent)
  4.     (setq obj (vlax-ename->vla-object (car ent)))
  5.     (cond
  6.       ((vlax-property-available-p obj 'textstring) (vla-get-textstring obj))
  7.       ((null (setq subent (nentselp (cadr ent)))) nil)
  8.       ((and subent
  9.             (setq obj (vlax-ename->vla-object (car subent))))
  10.        (if (vlax-property-available-p obj 'textstring)
  11.          (vla-get-textstring obj)
  12.        )
  13.       )
  14.     )
  15.   )
  16.  
  17. (setvar "errno" 0) ; must pre set the errno to 0
  18. (while (not done)
  19.   (cond
  20.     ((and (null string1)
  21.           (setq ent (entsel "\nSelect First attribute/Text : ")))
  22.       (setq string1 (_getString ent))
  23.      )
  24.     ((and string1 (null string2)
  25.           (setq ent (entsel "\nSelect Next attribute/Text : "))
  26.           (setq string2 (_getString ent)))
  27.      (setq string1 (strcat string1 " + " string2)
  28.            string2 nil)
  29.      )
  30.     ((= (getvar "errno") 52) ; exit if user pressed ENTER
  31.      
  32.      (if (and string1
  33.               (setq ip (getpoint "\nPick Text insertion point")))
  34.       (entmakex (list (cons 0 "TEXT")
  35.                   (cons 10  ip)
  36.                   (cons 40 (getvar "textsize"))
  37.                   (cons 1  string1)))
  38.      )
  39.      (setq done t)
  40.      )
  41.    ) ; cond
  42. ) ; while
  43.   (princ)
  44. )
  45.  
  46. (princ " Type   CTXT    to invoke the lisp")
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.

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: How to stop while loop?
« Reply #6 on: October 09, 2013, 10:31:59 AM »
working perfect

Thnax CAB
Thanx LEE

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: How to stop while loop?
« Reply #7 on: October 09, 2013, 10:49:31 AM »
Thnax CAB
Thanx LEE

*Thanks Keith™
  :wink:



You're welcome Hasan  :-)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: How to stop while loop?
« Reply #8 on: October 09, 2013, 11:28:10 AM »
That poor cat.  :)
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.

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: How to stop while loop?
« Reply #9 on: October 10, 2013, 02:51:44 AM »
Try something like this for your loop

Special thanks for Keith™  :-D  :-)