Jump to content
nmulder

Adding defaults to a routine

Recommended Posts

nmulder

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

Share this post


Link to post
Share on other sites
Lee Mac

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))

Share this post


Link to post
Share on other sites
nmulder

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.

Share this post


Link to post
Share on other sites
Lee Mac

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).

Share this post


Link to post
Share on other sites
gile
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)

Share this post


Link to post
Share on other sites
CAB

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

Share this post


Link to post
Share on other sites
Lee Mac
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...

Share this post


Link to post
Share on other sites
hawstom

CAB, your posts here are pretty amazing. Do you have a web site?

Share this post


Link to post
Share on other sites
CAB

Thanks

No web site, Can't afford the overhead. :)

 

So I just hang out here and there.

Share this post


Link to post
Share on other sites
hawstom

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

Share this post


Link to post
Share on other sites
CAB

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

Share this post


Link to post
Share on other sites
hawstom

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?

Share this post


Link to post
Share on other sites
CAB

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

Share this post


Link to post
Share on other sites
hawstom

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

Share this post


Link to post
Share on other sites
CAB

Good talking with you. Let me know if I can help out.

 

See Ya.8)

 

:)

Share this post


Link to post
Share on other sites
Lee Mac
Good talking with you. Let me know if I can help out.

 

See Ya.8)

 

:)

 

Alan, loving that Vector Image of your avatar on your PlotTabs routine :wink:

Share this post


Link to post
Share on other sites
CAB

Thanks, was easy with the vector lisp from Terry. &file=av-BigGrin.gif

Share this post


Link to post
Share on other sites
Lee Mac
Thanks, was easy with the vector lisp from Terry. &file=av-BigGrin.gif

 

Yeah, thought you might have used that :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×