Jump to content

Adding defaults to a routine


nmulder

Recommended Posts

I can't figure out how to add defaults to a while loop. Below is the code that I have working (which is code I got from Tim Spangler and modified myself):

 

(

if (= defstyle nil) (setq defstyle "Square"))
	(initget 6 "Square s S Round r R")
	(setq DuctStyle (getkword (strcat "\n Enter duct style (Square/Round)<"defstyle">: ")))
		(if (= DuctStyle nil) (setq DuctStyle defstyle) (setq defstyle DuctStyle))

 

Below is the while loop that I would also like to add defaults to:

 

;; Get duct width
	(if(= (strcase DuctStyle) "SQUARE")
  (while(null(setq DuctWidth (getint "\n Enter duct width: ")))
  	(princ "\n Enter duct width: ")
  )
  
	(while(null(setq DuctWidth (getint "\n Enter duct diameter: ")))
		(princ "\n Enter duct diameter:: ")
	)
)
;; Get duct height
(if(= (strcase DuctStyle) "SQUARE")
	(while(null(setq DuctHeight (getint "\n Enter duct height: ")))
			(princ "\n Enter duct height: ")
	)
	(setq DuctHeight DuctWidth)
)

 

I attached the full routine as well.

 

As an aside--how much more difficult would it be to have the routine "act" like the filet command? So that in any subsequent runs, it automatically uses the last inputs and only provides the option for the usre to change inputs. That way if the routine is run again and all inputs are the same, you just issue the routine and hit enter.

 

I'm pretty new to the world of LISP programming--learning as much as I can here and there.

 

Thanks. Any help is very appreciated.

RWB-DUCT.LSP

Link to comment
Share on other sites

I would usually approach it this way:

 

(or *def* (setq *def* 10))  ;; First time default setup

(setq tmp (getint (strcat "\nSpecify Integer <" (itoa *def*) "> : ")))
(or (not tmp) (setq *def* tmp))

Link to comment
Share on other sites

  • 4 weeks later...

Lee,

 

Thanks for your help. I've been busy and am just now getting back to figuring out this routine. Your code suggestion worked...however, I can't for the life of me get the default setup code within the previous "if" statement.

 

Right now the code checks to see if the user selected "Square" or "Round". If square, then it prompts for width and height. If round, it prompts for diameter. Then in subsequent issues I want it to provide the last user input respectively.

 

Maybe I'm missing something obvious here, but I'm stuck. Any more help you can offer would be greatly appreciated.

 

Thanks.

Link to comment
Share on other sites

You cannot use this logic with an IF statement - most of the getXXX functions return nil upon null user input (with the exception of getstring).

 

Yes you can, it's a little more verbose but more explicit (by my side I often use or, and statements)

(or *default* (setq *default* 10.0))

works the same as:

(if (not *default*) (setq *default* 10.0))

except the first return T the second 10.0

 

An example:

 

;; initialize defaults as global variables
(if (not *DuctStyle*)
 (setq *DuctStyle* "Square")
)
(if (not *DuctWidth*)
 (setq *DuctWidth* 20.0)
)
(if (not *DuctHeight*)
 (setq *DuctHeight* 40.0)
)
(if (not *DuctDiameter*)
 (setq *DuctDiameter* 30.0)
)
(initget "Square Round")
(if (not (setq DuctStyle
               (getkword
                 (strcat "\nEnter duct style [Round Square] <" *DuctStyle* ">: ")
               )
        )
   )
 (setq DuctStyle *DuctStyle*)
 (setq *DuctStyle* Ducstyle) ;_ save default
)
(if (= DuctStyle "Square")
 (progn
   (if (not (setq width
                   (getdist (strcat "\nSpecify duct width <" (rtos *DuctWidth*) ">: "))
            )
       )
     (setq width *DuctWidth*)
     (setq *DuctWidth* width) ;_ save default
   )
   (if (not (setq height
                   (getdist (strcat "\nSpecify duct height <" (rtos *DuctHeight*) ">: "))
            )
       )
     (setq height *DuctHeight*)
     (setq *DuctHeight* height) ;_ save default
   )
 )
 (if (not (setq diameter
                 (getdist (strcat "\nSpecify duct diameter <" (rtos *DuctDiameter*) ">: "))
          )
     )
   (setq diameter *DuctDiameter*)
   (setq *DuctDiameter* diameter) ;_ save default
 )
)

: Corrected mistake: "BTW gile needed vl-princ-to-string in strcat" (CAB)

Link to comment
Share on other sites

If you wan to get fancy you can add a Range test like this:

  ;; initialize defaults as global variables
 (if (not *DuctStyle*) (setq *DuctStyle* "Square"))
 (if (not *DuctWidth*) (setq *DuctWidth* 20.0))
 (if (not *DuctHeight*) (setq *DuctHeight* 40.0))
 (if (not *DuctDiameter*) (setq *DuctDiameter* 30.0))

 ;;  Set Min & Max Size allowed
 (setq DuctWidthRange '(6 60))
 (setq DuctHeightRange '(6 60))
 (setq DuctDiameterRange '(6 60))
 
 (initget "Square Round")
 (if (setq DuctStyle
            (getkword (strcat "\nEnter duct style [Round Square] <"
                              (vl-princ-to-string *DuctStyle*) ">: ")))
   (setq *DuctStyle* Ducstyle) ;_ save default
   (setq DuctStyle *DuctStyle*)
 )
 (if (= DuctStyle "Square")
   (progn
     (while
       (cond
         ((setq width (getdist (strcat "\nSpecify duct width <"
                                       (vl-princ-to-string *DuctWidth*) ">: ")))
          (if (< (apply 'min DuctWidthRange) width (apply 'max DuctWidthRange)) 
            (null (setq *DuctWidth* width)) ;_ save default, reurn nil to exit
            (princ (strcat "\nError out of range. " (vl-princ-to-string DuctWidthRange)))
         ))
         ((setq width *DuctWidth*) nil)
       )
     )
     (while
       (cond
         ((setq height (getdist (strcat "\nSpecify duct height <"
                                        (vl-princ-to-string *DuctHeight*) ">: ")))
          (if (< (apply 'min DuctHeightRange) Height (apply 'max DuctHeightRange)) 
            (null (setq *DuctHeight* Height)) ;_ save default, reurn nil to exit
            (princ (strcat "\nError out of range. " (vl-princ-to-string DuctHeightRange)))
         ))
         ((setq Height *DuctHeight*) nil)
       )
     )
    )

     (while
       (cond
         ((setq diameter (getdist (strcat "\nSpecify duct diameter <"
                                          (vl-princ-to-string *DuctDiameter*) ">: ")))
          (if (< (apply 'min DuctDiameterRange) Diameter (apply 'max DuctDiameterRange)) 
            (null (setq *DuctDiameter* Diameter)) ;_ save default, reurn nil to exit
            (princ (strcat "\nError out of range. " (vl-princ-to-string DuctDiameterRange)))
         ))
         ((setq Diameter *DuctDiameter*) nil)
       )
     )
    )

 

BTW gile needed vl-princ-to-string in strcat

Link to comment
Share on other sites

You cannot use this logic with an IF statement - most of the getXXX functions return nil upon null user input (with the exception of getstring).

 

Not sure what planet I was on when I wrote that ^^ :oops: As you correctly point out Gile, my or statement depends upon the getXXX functions returning nil...

Link to comment
Share on other sites

That's really neat. I have been programming AutoCAD using mainly AutoLISP since 1995. I have done a little VBA and I'm just now getting into the Visual LISP ActiveX stuff. My name is Tom Haws. Where do you hang out most? Do you always go by CAB? What is your real name? I'm 43 years old, and I am trying to make as many programmer acquaintances as I can now that I'm getting older and I'm frankly a bit tired of being a lone ranger programmer. I would rather find somebody I can team up with on projects here and there. Anyway, I hope to hear more from you. Feel free to look me up on Google and drop me an email.

 

Tom

Link to comment
Share on other sites

I go by CAB or CAB2k, most of my time is spent at TheSwamp.org

Lisping since 2003, started in ACAD2000 in 2001 or 2002.

Closed up my Plumbing company & thought myself ACAD from a book & the help files.

Was a lot of fun so I started designing custom houses for the contractors I used to work for.

Funny how things work out. I gave a GC friend of mine my sketches for my current house and

as a favor he was going to produce a permit set of plans. Well he got so busy he did not have

time to complete them. My wife lit a fire under me & I got the files from him and finished the plans

myself just using the help files. Put on my builder hat & with two employees built most of the

house. When finished & moved in I bought a book ACAD and a new career was born.

Unfortunately house building in Florida sucks so drafting is not paying the bills at the moment.

Thanks goodness my wife has a good job. :)

 

Alan

Link to comment
Share on other sites

Well, you do well with Visual Lisp, from what I can see. Lately I have been doing most of my new programming as Open Source stuff, though I have a bunch of legacy stuff I still maintain and sell. If you ever get a hankering for some abuse, maybe you could help us figure out and prettify the Turning Path Tracker TURN.LSP. Or contribute some code to the AutoCAD Wiki at wikia.

 

What other programming experience do you have besides AutoCAD/Visual LISP, Alan?

Link to comment
Share on other sites

Well I'm getting a little Long in the Tooth at 61, long forgotten is my first

Commodore 64 then an Apple II computer with a cassette player as a storage device.

I purchased one of there first floppies with a serial number of #80. With that

machine I did quite a bit of Apple Basic and 6502 machine coding. Also picked

up a CPM machine with duel 8" floppies and did some dBase and 8088 - Z80 machine

language. Along came IBM which meant more BASIC and in the 80's I used FoxBase

which evolved to FoxPro etc, to create my Billing, Inventory and Scheduling

program for my plumbing business. In 1989 I wrote a routine in FoxBase template

language, a program that writes programs, and made a few bucks selling it before

FoxPro came out and provided radio buttons and check boxes & made my routine

obsolete. Got into fishing & diving and forgot my old hobby until ACAD came

along & I started making a living doing that. I think Lisp is a natural

progression using ACAD. The little routines I have written with the help here

have saved me a lot of time drawing but I reinvest the time into programing.

 

With LISP, Visual Lisp & ObjectDBX I have not seen any need for me to venture into the

other languages ACAD supports.

 

There you have it.

 

If you are a Swamp member you may use this link to review some of my programs.

Just look at the ones started by CAB.

Search CAB

Link to comment
Share on other sites

theswamp . org seems like an amazing forum. I am surprised it is news to me. Perhaps that is what I get for not paying enough attention to personal networking over the years. I have registered there, and perhaps I will run into you there.

 

Your age shows: your programming is clean and readable and methodical.

 

Having the acquaintance of good AutoCAD programmers like you is something I value. Thanks for chatting with me.

 

Tom

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...