Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

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

Troubles with nothing selected (while loop

150 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,368
Registered: ‎09-13-2004
Message 2 of 3 (144 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: 173
Registered: ‎11-20-2011
Message 3 of 3 (123 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!!!
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.