Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Mentor
smaher12
Posts: 191
Registered: ‎11-20-2011
Message 1 of 10 (329 Views)
Accepted Solution

while loop with prompt

329 Views, 9 Replies
11-19-2013 08:10 AM

I am trying to put together a loop to enter a number greater than the previous number entered. Below is my mockup and I just cannot get it.

 

(defun c:22 ()
  (while
    (setq d (getreal "\nSpecify diameter: "))
    (setq $d d)
  (while
    (if (<= d $d)
      (progn
        (prompt "\nSpecify greater diameter.")
          (setq d (getreal "\nSpecify diameter: "))
      )
    )
   )
  (setq p1 (getpoint "\nSpecify point: "))
  (command "CIRCLE" p1 d)
  )
)

 

 

Perhaps something along these lines:

 

(defun c:22 ( / $d d p1 )
(while
(progn
(initget 6)
(setq d (getdist "\nSpecify diameter <exit>: "))
)
(if (and (= 'real (type $d)) (<= d $d))
(prompt (strcat "\nDiameter must be greater than " (rtos $d)))
(if (setq p1 (getpoint "\nSpecify center <back>: "))
(progn
(command "_.circle" "_non" p1 d)
(setq $d d)
)
)
)
)
(princ)
)

 

*Expert Elite*
hmsilva
Posts: 3,371
Registered: ‎12-17-2004
Message 2 of 10 (323 Views)

Re: while loop with prompt

11-19-2013 08:23 AM in reply to: smaher12

smaher12 wrote:

I am trying to put together a loop to enter a number greater than the previous number entered. Below is my mockup and I just cannot get it.

 

 

 


Try

(defun c:22 ( / $D D P1)
  (if (and (setq d (getreal "\nSpecify diameter: "))
	   (setq $d d)
      )
    (while
      (> d $d)
       (setq p1 (getpoint "\nSpecify point: "))
       (command "CIRCLE" p1 d)
       (setq $d d)
       (prompt "\nSpecify greater diameter.")
       (setq d (getreal "\nSpecify diameter: "))
    )
  )
  (princ)
)

 Henrique

*Expert Elite*
Lee_Mac
Posts: 1,270
Registered: ‎12-29-2009
Message 3 of 10 (318 Views)

Re: while loop with prompt

11-19-2013 08:29 AM in reply to: smaher12

Perhaps something along these lines:

 

(defun c:22 ( / $d d p1 )
    (while
        (progn
            (initget 6)
            (setq d (getdist "\nSpecify diameter <exit>: "))
        )
        (if (and (= 'real (type $d)) (<= d $d))
            (prompt (strcat "\nDiameter must be greater than " (rtos $d)))
            (if (setq p1 (getpoint "\nSpecify center <back>: "))
                (progn
                    (command "_.circle" "_non" p1 d)
                    (setq $d d)
                )
            )
        )
    )
    (princ)
)

 

Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)
Expert Elite
With Mathematics there is the possibility of perfect rigour, so why settle for less?
*Expert Elite*
Lee_Mac
Posts: 1,270
Registered: ‎12-29-2009
Message 4 of 10 (311 Views)

Re: while loop with prompt

11-19-2013 08:34 AM in reply to: Lee_Mac

Alternatively:

 

(defun c:22 ( / $d d p1 )
    (while
        (progn
            (initget 6)
            (setq d (getdist "\nSpecify diameter <exit>: "))
            (cond
                (   (null d) nil)
                (   (and (= 'real (type $d)) (<= d $d))
                    (princ (strcat "\nDiameter must be greater than " (rtos $d)))
                )
                (   (setq p1 (getpoint "\nSpecify center <exit>: "))
                    (command "_.circle" "_non" p1 d)
                    (setq $d d)
                )
            )
        )
    )
    (princ)
)

 

Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)
Expert Elite
With Mathematics there is the possibility of perfect rigour, so why settle for less?
*Expert Elite*
hmsilva
Posts: 3,371
Registered: ‎12-17-2004
Message 5 of 10 (301 Views)

Re: while loop with prompt

11-19-2013 08:42 AM in reply to: Lee_Mac
Nicely done, Lee

Cheers
Henrique
*Expert Elite*
Lee_Mac
Posts: 1,270
Registered: ‎12-29-2009
Message 6 of 10 (296 Views)

Re: while loop with prompt

11-19-2013 09:02 AM in reply to: hmsilva

hmsilva wrote:
Nicely done, Lee

Thank you Henrique! :smileyhappy:

Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)
Expert Elite
With Mathematics there is the possibility of perfect rigour, so why settle for less?
Mentor
smaher12
Posts: 191
Registered: ‎11-20-2011
Message 7 of 10 (260 Views)

Re: while loop with prompt

11-19-2013 07:05 PM in reply to: Lee_Mac

Many thanks Lee. Well, I applied the mock up to my model and it opened another can of worms.

 

I am trying to add a cond if the strata depth is greater than the boring depth (princ Strata depth is greater than boring depth.

Also, when I exit Strata depth I still need to select a hatch pattern and hatch the remaining section.

 

 

(defun c:22 ()
  (or $sc (setq $sc 60))
    (setq sc (getreal (strcat "\nSpecify scale [Inches = Foot] <"(rtos $sc 2 1)">: ")))
      (if (not sc) (setq sc $sc))
        (setq $sc sc)

  (or $bd (setq $bd 7))
    (setq bd (getreal (strcat "\nSpecify boring depth in feet <"(rtos $bd 2 1)">: ")))
      (if (not bd) (setq bd $bd))
        (setq $bd bd)

  (setq p0 (getpoint "\nSpecify starting point: ")
	p1 (list (- (car p0) 9) (cadr p0))
        p2 (list (+ 9 (car p0)) (cadr p0))
	p3 (list (car p2) (- (cadr p2)(* bd sc)))
        p4 (list (car p1) (cadr p3)))
  (command "pline" p1 p2 p3 p4 "C")
  (setq p9 p1)
  (setq P10 p4)

 (setq $d 0)
    (while
        (progn
            (initget 6)
            (setq d (getdist "\nSpecify strata depth <exit>: "))
            (cond
                (   (null d) nil)
                (   (and (= 'real (type $d)) (<= d $d))
                    (princ (strcat "\nDepth must be greater than " (rtos $d)))
                )
                (   
    (setq pn (getstring (strcat "\nSpecify pattern name <" (getvar "hpname") ">: ")))
      (if pn (= nil) (setq pn (getvar "hpname")))

    (setq d1 (list (car p9) (- (cadr p9) (* d sc))))
    (setq d2 (list (+ 18 (car d1)) (cadr d1)))
    (command "line" d1 d2 "")
    (command "hatch" "p" pn "80" "w" "" "" d1 d2 p2 p1 "c" "")
                    (setq $d d)
                )
            )
        )
    )
 (princ)
)

 

*Expert Elite*
doni49
Posts: 2,019
Registered: ‎12-22-2012
Message 8 of 10 (241 Views)

Re: while loop with prompt

11-20-2013 06:31 AM in reply to: smaher12

smaher12 wrote: 

I am trying to add a cond if the strata depth is greater than the boring depth (princ Strata depth is greater than boring depth.


And exactly what is the problem?  Are you getting an error message when you attempt it?  What is the error?  What is not working right (or makes you think it's not working right)?  Is there any portion that DOES work?  What?

 

Don Ireland



If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.






Forum links:
AutoCAD 2013/2014/2015
AutoCAD 2010/2011/2012

AutoLISP, VB and other AutoCAD customization


Civil 3D General
Civil 3d Customization


Other Helpful links:
My File is too large to post or I can't post it due to privacy issues!
Mentor
marko_ribar
Posts: 190
Registered: ‎12-04-2011
Message 9 of 10 (226 Views)

Re: while loop with prompt

11-20-2013 09:24 AM in reply to: doni49
Change this :
(if pn (= nil) (setq pn (getvar "hpname")))

to :
(if (= pn "") (setq pn (getvar "hpname")))
Marko Ribar, d.i.a. (graduated engineer of architecture)
Mentor
smaher12
Posts: 191
Registered: ‎11-20-2011
Message 10 of 10 (159 Views)

Re: while loop with prompt

01-21-2014 01:31 PM in reply to: Lee_Mac

Okay, the following code does what I need it to. I was just hoping someone can take a look at it and see if the condition section can be a little more efficient.  Any suggestions are helpful. Thanks.

 

(defun c:22 ()

  (or $scale (setq $scale 60))
    (setq scale (getreal (strcat "\nSpecify scale <"(rtos $scale 2 1)">: ")))
      (if (not scale) (setq scale $scale))
        (setq $scale scale)

  (or $depth (setq $depth 4))
    (setq depth (getreal (strcat "\nSpecify depth in feet <"(rtos $depth 2 1)">: ")))
      (if (not depth) (setq depth $depth))
        (setq $depth depth)

  (setq p0 (getpoint "\nSpecify starting point: ")
	p1 (list (- (car p0) 9) (cadr p0))
        p2 (list (+ 9 (car p0)) (cadr p0))
	p3 (list (car p2) (- (cadr p2)(* depth scale)))
        p4 (list (car p1) (cadr p3))
	p5 (list (- (car p0) 9) (cadr p0))
  )
  (setvar 'osmode 0)
  (command "pline" p2 p3 p4 p1 "C")

  (setq $sdepth 0)
  (while
    (progn
      (initget 6)
      (setq sdepth (getdist "\nSpecify strata depth <exit>: "))

      (cond
	((and (not sdepth))(wrapup)
	)
	((and (= 'real (type sdepth)) (= sdepth depth)) (wrapup)
	)
	((and (= 'real (type sdepth)) (> sdepth depth))
	 (princ (strcat "\nStrata depth must be less than boring depth " (rtos depth 2 1)))
        )
	((and (= 'real (type $sdepth)) (<= sdepth $sdepth))
	 (princ (strcat "\nStrata depth must be greater than " (rtos $sdepth 2 1)))
        )
	((and (= 'real (type $sdepth)) (> sdepth depth))
	 (princ (strcat "\nStrata depth must be less than boring depth " (rtos depth 2 1)))
	)
	((and (= 'real (type $sdepth)) (= sdepth depth)) (wrapup)
	)

	(; then do this
	(setq d1 (list (car p5) (- (cadr p5) (* sdepth scale)))
	      d2 (list (+ 18 (car d1)) (cadr d1))
	)
	(command "line" d1 d2 "") 

         (initget 1)
	 (setq no (getint "\nSpecify strata number: "))
	   (command
;	     "-insert" "sn" (polar p2 (* pi 1.5) (/ (distance p2 d2) 2)) "" "" "" no ""
	     "-HATCH" "P" "Ansi31" "80" "" "DR" "B" "W" "N" D1 D2 P2 P1 "C" "" ""
	   )
	 (setq $sdepth sdepth)
	 (setq p1 d1)
	 (setq p2 d2)
        ); end then
      ); cond
    ); progn
  ); while
 (princ)
); 22

(defun wrapup ()
  (initget 1)
  (setq no (getint "\nSpecify FINAL strata number: "))
    (command
;      "-insert" "sn" (polar p2 (* pi 1.5) (/ (distance p2 p3) 2)) "" "" "" no ""
      "-HATCH" "P" "Ansi33" "80" "" "DR" "B" "W" "N" P4 P3 P2 P1 "C" "" ""
    )
)

 

 

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.