Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Mentor
smaher12
Posts: 191
Registered: ‎11-20-2011
Message 1 of 3 (152 Views)

Troubles with nothing selected (while loop

152 Views, 2 Replies
10-11-2012 01:49 PM

I seem to be having troubles my (while looping. My issue is if I select nothing and I select anthing other than a hatch pattern and go back and select nothing again the loop stops. I kind of know why but I'm not having any luck getting it work. Any ideas?

 

(defun c:hhh ()
  (setq ent (car (entsel "\nSelect a hatch whose properties will become current: ")))

 

   (while
     (= ent nil)
      (princ "\nNothing was selected")
       (setq ent (car (entsel "\nSelect a hatch whose properties will become current: ")))
   )

 (setq ent2 (entget ent))
 (setq type (cdr (assoc 0 ent2)))

 

  (while
    (/= "HATCH" type)
     (progn
       (princ "\nHatch pattern was not selected")
        (setq ent nil)
        (setq ent (car (entsel "\nSelect a hatch whose properties will become current: ")))
        (setq ent2 (entget ent))
        (setq type (cdr (assoc 0 ent2)))
     )
  )

 

  (setq hpn (cdr (assoc '2 ent2)))

 

  (if (= hpn "SOLID")
   (progn
      (setvar "hpname" (cdr (assoc '2 ent2)))
        (princ (strcat "\n" "Pattern = " (getvar "hpname") "  is set to current."))
          (princ)
   )
  )

 

  (if (/= hpn "SOLID")
   (progn
    (setvar "hpang" (cdr (assoc '52 ent2)))
     (setvar "hpname" (cdr (assoc '2 ent2)))
      (setvar "hpscale" (cdr (assoc '41 ent2)))
       (princ (strcat "\n" "Pattern = " (getvar "hpname") "  Scale = " (rtos (getvar "hpscale") 2 2) "  Angle = " (rtos (getvar "hpang") 2 2) "  is set to current."))
         (princ)
   )
  )
(princ)
)

*Expert Elite*
Kent1Cooper
Posts: 5,779
Registered: ‎09-13-2004
Message 2 of 3 (146 Views)

Re: Troubles with nothing selected (while loop

10-11-2012 02:39 PM in reply to: smaher12

smaher12 wrote:

I seem to be having troubles my (while looping. My issue is if I select nothing and I select anthing other than a hatch pattern and go back and select nothing again the loop stops. I kind of know why but I'm not having any luck getting it work. Any ideas?

....


Without digging too deeply into the possible reason(s) for that, I would suggest a somewhat different approach, which can, among other benefits, spare you both the need to spell out the same prompt twice and the double (while) loops:

 

(defun c:hhh (/ esel edata)

  (while

    (not

      (and

        (setq esel (entsel "\nSelect a hatch whose properties will become current: "))

        (setq edata (entget (car esel)))

        (= (cdr (assoc 0 edata)) "HATCH")

      ); and

    ); not 

    (prompt "\nNothing selected, or not a Hatch pattern.")
  ); while

  (setq hpn (cdr (assoc 2 edata)))

... etc. ...

 

You don't need the apostrophes before the numbers in (assoc) [it may work with them, but they're expendable].

 

And you're setting the HPNAME System Variable to what's in your hpn variable in any case, so you could just omit that setting in the two subsequent cases, and instead follow the last line above with:

 

  (setvar 'hpname hpn)

Kent Cooper
Mentor
smaher12
Posts: 191
Registered: ‎11-20-2011
Message 3 of 3 (125 Views)

Re: Troubles with nothing selected (while loop

10-13-2012 12:57 PM in reply to: Kent1Cooper
Kent1Cooper - I like it. That works great. Thank you so much!!!
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!