Command Options in AutoLISP

Command Options in AutoLISP

Anonymous
Not applicable
3,356 Views
9 Replies
Message 1 of 10

Command Options in AutoLISP

Anonymous
Not applicable

Hey community, so I'm diving into (getkword) and playing with it in a command that prompts the user for point elevation information and calculates the slope/vertical change/etc between two points. 

 

It's not all complete, but when I try testing it I receive that my (setq *op*) has too few arguments, but when I test each line, I get the right return values. 

 

General process for this command prompts the user for a point and elevation value. Then the user has some options/combinations of specifying percent slope, distance, elevation, or vertical change. It's a work in progress, but these conditionals are just setting variables to be used for the return values and seem to be tripping up somehow.

 

Any help is appreciated! 

 

 


(defun c:gtools () (setq clty (getvar 'celtype) ccol (getvar 'cecolor) clay (getvar 'clayer) dims (if (= (getvar 'measurement) 0) "2017" "2017-M" ) dimb (strcat @blk "dim_p_" dims ".dwg") ) (if (not *ffe*) (setq *ffe* 0.00)) ;default FFE (if (not *elv*) (setq *ffe* 0.00)) ;default elevation @ first point (if (not *elv2*) (setq *elv* 0.00)) ;default elevation @ second point (if (not *m*) (setq *m* 2)) ;default percent slope (if (not *run*) (setq *run* 6)) ;default run distance (if (not *vrt*) (setq *vrt* (* *run* (/ *m* 100)))) ;default vertical change (if (not *op*) (setq *op* "eXit")) ;default option value (if (not *mld*) (setq *mld* "None")) ;default leader option (if (not *ans*) (setq *ans* "Yes")) ;default erase last option (if (not *mem*) (setq *mem* "Yes")) ;default memory option (if (tblsearch "style" dims) (command "-dimstyle" "r" dims) (progn (command "-insert" dimb "0,0" "1" "1" "0") (command "erase" "l" "") (command "-dimstyle" "r" dims) ) ) (initget 128 "Options") (setq pt (getpoint (strcat "\nSelect first point or [Options] < >: "))) (cond ((= pt "Options") (initget "eleVation Slope Distance Leader Erase Memory eXit") (setq *op* (cond ((getkword (strcat "\nSet default value for [eleVation/Slope/Distance/Leader/Erase/Memory/eXit] <" *op* ">: "))) (*op*))) )) (cond ((= *op* "eleVation") (initget 128 "Back") (setq *ffe* (cond ((getkword (strcat "\nSet default elevation (FFE) or [Back] <" *ffe* ">: "))) (*ffe*))) ) ((= *op* "Slope") (initget 128 "Back") (setq *m* (cond ((getkword (strcat "\nSet default percent slope or [Back] <" *m* "%>: "))) (*m*))) ) ((= *op* "Distance") (initget 128 "Back") (setq *run* (cond ((getkword (strcat "\nSet default run or [Back] <" *run* ">: "))) (*run*))) ) ((= *op* "Leader") (initget "Leader Spot None Back") (setq *mld* (cond ((getkword (strcat "\nSelect default leader type [Leader/Spot/None/Back] <" *mld* ">: "))) (*mld*))) ) ((= *op* "Erase") (initget "Yes No Back") (setq *ans* (cond ((getkword (strcat "\nErase slope line? [Yes/No/Back] <" *ans* ">: "))) (*ans*))) ) ((= *op* "Memory") (initget "Yes No Back") (setq *mem* (cond ((getkword (strcat "\nRemember erase last? [Yes/No/Back] <" *mem* ">: "))) (*mem*))) ) ((= *op* "eXit") (initget 128 "Options") (setq pt (getpoint "\nSelect first point or [Options] < >: ")) ) ) (cond ((= *ffe* "Back") (initget "eleVation Slope Distance Leader Erase Memory eXit") (setq *op* (cond ((getkword (strcat "\nSet default value for [eleVation/Slope/Distance/Leader/Erase/Memory/eXit] <" *op* ">: ")))) (*op*)) ) ((= *m* "Back") (initget "eleVation Slope Distance Leader Erase Memory eXit") (setq *op* (cond ((getkword (strcat "\nSet default value for [eleVation/Slope/Distance/Leader/Erase/Memory/eXit] <" *op* ">: "))) (*op*))) ) ((= *run* "Back") (initget "eleVation Slope Distance Leader Erase Memory eXit") (setq *op* (cond ((getkword (strcat "\nSet default value for [eleVation/Slope/Distance/Leader/Erase/Memory/eXit] <" *op* ">: "))) (*op*))) ) ((= *mld* "Back") (initget "eleVation Slope Distance Leader Erase Memory eXit") (setq *op* (cond ((getkword (strcat "\nSet default value for [eleVation/Slope/Distance/Leader/Erase/Memory/eXit] <" *op* ">: "))) (*op*))) ) ((= *ans* "Back") (initget "eleVation Slope Distance Leader Erase Memory eXit") (setq *op* (cond ((getkword (strcat "\nSet default value for [eleVation/Slope/Distance/Leader/Erase/Memory/eXit] <" *op* ">: "))) (*op*))) ) ((= *mem* "Back") (initget "eleVation Slope Distance Leader Erase Memory eXit") (setq *op* (cond ((getkword (strcat "\nSet default value for [eleVation/Slope/Distance/Leader/Erase/Memory/eXit] <" *op* ">: "))) (*op*))) ) );;end options ;;begin second point (cond ((numberp pt) (initget 128 "Ffe") (setq *elv* (cond ((getkword (strcat "\nEnter elevation at first point or [Ffe] <" *elv* ">: "))) (*elv*))) (initget 128 "Run Vertical Slope") (setq pt2 (getpoint (strcat "\nSelect second point or enter [Run/Vertical change/Slope] < >: "))) ) ((= pt2 "Run") (initget 128 "Back") (setq *run* (cond ((getkword (strcat "\nEnter run distance or [Back] <" *run* ">: "))) (*run*))) ) ((numberp *run*) (setvar 'celtype "HIDDEN2") (setvar 'cecolor "20") (command "circle" pt *run*) (setq cir (entlast)) (initcommandversion) (command "dimradius" cir) (while (> (getvar 'cmdactive) 0) (command pause) ) (initget 128 "Ffe") (setq *elv2* (cond ((getkword (strcat "\nEnter elevation at second point or [Ffe] <" *elv2* ">: "))) (*elv2*)) *vrt* (- *elv* *elv2*) dimt (strcat *run* " @ " *m* " <" *vrt* ">") entd (subst (cons 1 dimt) (assoc 1 entd) entd) ) (entmod entd) ) ((= *run* "Back") (initget 128 "Run Vertical Slope") (setq pt2 (getpoint (strcat "\nSelect second point or enter [Run/Vertical change/Slope] < >: "))) ) ((= pt2 "Vertical") (initget 128 "Back") (setq *vrt* (cond ((getkword (strcat "\nEnter vertical change or [Back] <" *vrt* ">: "))) (*vrt*))) ) ((= pt2 "Slope") ) ((numberp pt2) (initget 128 "Verical Elevation") (setq *run* (getdist (list pt pt2)) *m* (cond ((getkword (strcat "\nEnter percent slope or [Vertical change/Elevation] <" *m* "%>: "))) (*m*)) ) ) ((= *m* "Vertical") (initget 128 "Back") (setq *vrt* (cond ((getkword (strcat "\nEnter vertical change or [Back] <" *vrt* ">: "))) (*vrt*)) *m* (/ *vrt* *run* ) ) ) ((= *vrt* "Back") (initget 128 "Vertical Elevation") (setq *run* (getdist (list pt pt2)) *m* (cond ((getkword (strcat "\nEnter percent slope or [Vertical change/Elevation] <" *m* "%>: "))) (*m*)) ) ) ((= *m* "Elevation") (initget 128 "Back") (setq *elv2* (cond ((getkword (strcat "\nEnter elevation at second point or [Back] <" *elv2* ">: "))) (*elv2*)) *vrt* (- *elv* *elv2*) ) ) ((= *elv2* "Back") (initget 128 "Vertical Elevation") (setq *m* (cond ((getkword (strcat "\nEnter percent slope or [Vertical change/Elevation] <" *m* "%>: "))) (*m*)) ) ) );;end cond (setvar 'celtype clty) (setvar 'cecolor ccol) (setvar 'clayer clay) );end defun

 

Thanks, 

0 Likes
3,357 Views
9 Replies
Replies (9)
Message 2 of 10

Ranjit_Singh
Advisor
Advisor

see below reply

0 Likes
Message 3 of 10

Ranjit_Singh
Advisor
Advisor

OK. Parantheses in wrong place

(setq	*op*	(cond ((getkword (strcat "\nSet default value for [eleVation/Slope/Distance/Leader/Erase/Memory/eXit] <" *op* ">: ")))) (*op*))

should be

(setq	*op*	(cond ((getkword (strcat "\nSet default value for [eleVation/Slope/Distance/Leader/Erase/Memory/eXit] <" *op* ">: "))) (*op*)))
0 Likes
Message 4 of 10

ВeekeeCZ
Consultant
Consultant

Couple of tips for you.

 

(if (not *m*) (setq *m* 2))				;default percent slope
(if (not *run*) (setq *run* 6))				;default run distance
(if (not *vrt*) (setq *vrt* (* *run* (/ *m* 100.))))	;default vertical change

(setvar 'celtype "HIDDEN2")

(command "circle" "_none" pt *run*)

- an integer divided by an integer is still an integer - (/ 2 100) = 0! You might want to 100 as a real number 100., with a dot at least.

- make sure that HIDDEN2 linetype is loaded. See Kent's recent posts HERE and HERE

- if you setting pt in command, make sure that OSNAP is off. The simplest way is use "_none" for a temp osnap. Otherwise it could catch some of running osnaps...

0 Likes
Message 5 of 10

Kent1Cooper
Consultant
Consultant

Without delving too deeply into the exact workings of things, I think you're going to need to re-structure some elements of the code.  Read up on the (cond) function.  For instance, in this part:

 

(cond
	((numberp pt)
		(initget 128 "Ffe")
		(setq	*elv*	(cond ((getkword (strcat "\nEnter elevation at first point or [Ffe] <" *elv* ">: "))) (*elv*)))
		(initget 128 "Run Vertical Slope")
		(setq	pt2 (getpoint (strcat "\nSelect second point or enter [Run/Vertical change/Slope] < >: ")))
	)
	((= pt2 "Run")
		(initget 128 "Back")
		(setq *run* (cond ((getkword (strcat "\nEnter run distance or [Back] <" *run* ">: "))) (*run*)))
	)

 

First of all, as I read earlier code, the pt variable is going to be either a picked point or the word "Options", not a number.  So I think that first conditional test should be:

 

    ((listp pt)

 

But if that's correct [and corrected], if the User has picked a point, then it will proceed to ask for *elv* and pt2 values.  It will not then go on to look at the second condition, about what the pt2 variable contains.  The nature of (cond) is that as soon as it finds any condition satisifed, it does what is asked for there, and does not consider any of the other conditions., but skips to the end of the overall (cond) function, and the routine continues on with whatever may follow that.

 

I think you're going to need to pull a lot of the things you have together in extended (cond) functions out, into separate (cond) [or in some cases (if) would do] functions, so that it will go through them all and can apply the results of earlier ones in the evaluations in later ones.

 

Another suggestion, for a shorter way of setting all those initial default values except for the *vrt* one:

(foreach pair
  '(("*ffe*" 0.0) ("*elv*" 0.0) ("*elv2*" 0.0) ("*m*" 2) ("*run*" 6)
  ("*op*" "eXit") ("*mld*" "None") ("*ans*" "Yes") ("*mem*" "Yes"))
  (if (not (eval (read (car pair)))) (set (read (car pair)) (cadr pair)))
)

 

The *vrt* variable can't be included in there, because a 'quoted' list like that can't have anything in it that needs evaluation, like the calculation involved in setting *vrt*.  [That's also why the variable names are in double-quotes, and extracted with (read) functions.]

Kent Cooper, AIA
0 Likes
Message 6 of 10

Anonymous
Not applicable

Thanks Kent, you're correct on breaking out the conditionals. This code is early in development and I came to the same conclusion. Really broke it down today, so we'll see where this goes. 

 

20161202_153230.jpg

 

 

Going to work on it this weekend, and likely repost some code next week. 

0 Likes
Message 7 of 10

Anonymous
Not applicable

Been racking my brain on this for the last week. Figured out the general structure, but having a problem with the (getpoint) function and getting a string return. I'm unsure know what to use to have the option to prompt for a real number, but have the option to return a string...

 

Any help would be great!

 

Updated code:

 

(defun c:gtools ()

(if (not *ffe*) (setq *ffe* 0.00))			;default FFE
(if (not (numberp *elv*)) (setq *elv* *ffe*))			;default elevation @ first point
(if (not *elv2*) (setq *elv2* *elv*))			;default elevation @ second point
(if (not *ang*) (setq *elv2* 0.0))			;default horizontal direction
(if (not (numberp *m*)) (setq *m* 2.0))					;default percent slope
(if (not *run*) (setq *run* 72))				;default run distance
(if (not *vrt*) (setq *vrt* (* *run* (/ *m* 100))))	;default vertical change
(setq *op* "eXit")			;default option value
(if (not *mld*) (setq *mld* "None"))		;default leader option
(if (not *ans*) (setq *ans* "Yes"))			;default erase last option
(if (not *mem*) (setq *mem* "Yes"))			;default memory option
(if (not *dir*) (setq *dir* "Down"))		;default vertical direction

;;begin
(initget "Options")
(setq pt1 (getpoint (strcat "\nSelect first point or [Options] < >: ")))

;;options
(cond
	((= pt1 "Options")
		(initget "eleVation Slope Leader diRection Erase Memory eXit")
		(setq *op* (cond ((getkword (strcat "\nSet default value for [eleVation/Slope/Leader style/diRection/Erase last/Memory/eXit] <" *op* ">: "))) (*op*)))
	)
)

;;options menu
(cond
	((and (= *op* "eleVation") (= pt1 "Options"))
		(setq *ffe* (getreal (strcat "\nSet default elevation (FFE) <" (if (numberp *ffe*) (rtos *ffe* 2 2) *ffe*) ">: ")))
	)
	((and (= *op* "Slope") (= pt1 "Options"))
		(setq *m* (getreal (strcat "\nSet default percent slope <" (if (numberp *m*) (strcat (rtos *m* 2 2) "%") *m*) ">: ")))
	)
	((and (= *op* "Distance") (= pt1 "Options"))
		(setq *run* (getreal (strcat "\nSet default run <" (if (numberp *run*) (rtos *run* 2 2) *run*) ">: ")))
	)
	((and (= *op* "Leader") (= pt1 "Options"))
		(initget "Leader Spot None")
		(setq *mld* (cond ((getkword (strcat "\nSelect default leader type [Leader/Spot/None] <" *mld* ">: "))) (*mld*)))
	)
	((and (= *op* "diRection") (= pt1 "Options"))
		(initget "Up Down")
		(setq *dir* (cond ((getkword (strcat "\nSelect default slope direction [Up/Down] <" *dir* ">: "))) (*dir*)))
	)
	((and (= *op* "Erase") (= pt1 "Options"))
		(initget "Yes No")
		(setq *ans* (cond ((getkword (strcat "\nErase slope line? [Yes/No] <" *ans* ">: "))) (*ans*)))
	)
	((and (= *op* "Memory") (= pt1 "Options"))
		(initget 128 "Yes No")
		(setq *mem* (cond ((getkword (strcat "\nRemember erase last? [Yes/No] <" *mem* ">: "))) (*mem*)))
	)
	((and (= *op* "eXit") (= pt1 "Options"))
		(setq pt1 (getpoint "\nSelect first point: "))
	)
)

;;elevation at first point
(if (listp pt)
	(setq *elv* (getreal (strcat "\nEnter elevation at first point <" (rtos *elv* 2 2) ">: ")))
)

;;begin choas
(initget 320 "verTical Slope")
(setq pt2 (getpoint pt1 (strcat "\nSelect second point or enter [verTical change/Slope]: ")))

;;
(cond 
	((= *run* "verTical")
		(setq *vrt* (getreal (strcat "\nEnter vertical change <" (if (numberp *vrt*) (rtos *vrt* 2 3) (*vrt*)) ">: ")))
		(initget "seconD")
		(setq *m* (getreal (strcat "\nEnter percent slope or select [seconD point] <" (if (numberp *m*) (strcat (rtos *m* 2 1) "%") (strcat (*m*))) ">: ")))
	)

	((= *run* "Slope")
		(setq *m* (getreal (strcat "\nEnter percent slope <" (if (numberp *m*) (strcat (rtos *m* 2 1) "%") (strcat (*m*))) ">: ")))
		(initget "verTical eleVation")
		(setq pt2 (getpoint pt1 (strcat "\nSelect second point or enter [verTical change/eleVation at second point]: ")))
	)

	((listp pt2)
		(initget 128 "verTical eleVation")
		(setq *m* (getreal (strcat "\nEnter percent slope or [verTical change/eleVation at second point] <" (rtos *m* 2 1) ">: ")))
	)
)

;;calculations
(cond

	((and (= *run* "verTical") (= *m* "seconD"))
		(setq pt2 (getpoint pt1 (strcat "\nSelect second point: ")))
		(setq *run* (distance pt1 pt2));calc run
		(setq *run* (if (= (getvar 'measurement) 0) (/ *run* 12) *run*));set run to feet or mm
		(setq *m* (/ *vrt* *run*));calc slope
		(setq *elv2* (if (= *dir* "up") (+ *elv* *vrt*) (- *elv* *vrt*)));calc elvation at second point
	)

	((and (= *run* "verTical") (numberp *m*))
		(setq *ang* (getangle pt1 "\nSelect slope direction: "))
		(setq *ang* (* 180.0 (/ *ang* pi)));set direction in degrees
		(setq *run* (/ *vrt* *m*));calc run
		(setq *run* (if (= (getvar 'measurement) 0) (/ *run* 12) *run*));set run to feet or mm
		(setq pt2 (polar pt1 *ang* *run*));set second point
		(setq *elv2* (if (= *dir* "up") (+ *elv* *vrt*) (- *elv* *vrt*)));calc elvation at second point

	)


	((and (= *run* "Slope") (listp pt2))
		(setq *run* (distance pt1 pt2));calc run
		(setq *run* (if (= (getvar 'measurement) 0) (/ *run* 12) *run*));set run to feet or mm
		(setq *m* (/ *m* 100));set slope to decimal
		(setq *vrt* (* *m* *run*));calc vertical change
		(setq *m* (* *m* 100));set slope to percent
		(setq *elv2* (if (= *dir* "up") (+ *elv* *vrt*) (- *elv* *vrt*)));calc elvation at second point
	)

	((and (= *run* "Slope") (= pt2 "verTical"))
		(setq *vrt* (getreal (strcat "\nEnter vertical change <" (if (numberp *vrt*) (rtos *vrt* 2 3) (*vrt*)) ">: ")))
		(setq *ang* (getangle pt1 "\nSelect slope direction: "))
		(setq *m* (/ *m* 100));set slope to decimal
		(setq *run* (/ *vrt* *m*));calc run
		(setq *run* (if (= (getvar 'measurement) 0) (/ *run* 12) *run*));set run to feet or mm
		(setq *m* (* *m* 100));set slope to percent
		(setq pt2 (polar pt1 *ang* *run*));set second point
		(setq *elv2* (if (= *dir* "up") (+ *elv* *vrt*) (- *elv* *vrt*)));calc elvation at second point
	)

	((and (= *run* "Slope") (= pt2 "eleVation"))
		(setq *elv2* (getreal (strcat "\nEnter elevation at second point: ")))
		(setq *ang* (getangle pt1 "\nSelect slope direction: "))
		(setq *vrt* (if (> *elv* *elv2*) (- *elv* *elv2*) (- *elv2* *elv*)));calc vertical change
		(setq *m* (/ *m* 100));set slope to decimal
		(setq *run* (/ *vrt* *m*));calc run
		(setq *run* (if (= (getvar 'measurement) 0) (/ *run* 12) *run*));set run to feet or mm
		(setq *m* (* *m* 100));set slope to percent
	)


	((and (listp pt2) (= *m* "verTical"))
		(setq *vrt* (getreal (strcat "\nEnter vertical change <" (if (numberp *vrt*) (rtos *vrt* 2 3) (*vrt*)) ">: ")))
		(setq *elv2* (if (= *dir* "up") (+ *elv* *vrt*) (- *elv* *vrt*)));calc elvation at second point
		(setq *run* (distance pt1 pt2));calc run
		(setq *run* (if (= (getvar 'measurement) 0) (/ *run* 12) *run*));set run to feet or mm
		(setq *m* (/ *vrt* *run*));calc slope
	)

	((and (listp pt2) (= *m* "eleVation"))
		(setq *elv2* (getreal (strcat "\nEnter elevation at second point: ")))
		(setq *run* (distance pt1 pt2));calc run
		(setq *run* (if (= (getvar 'measurement) 0) (/ *run* 12) *run*));set run to feet or mm
		(setq *vrt* (if (> *elv* *elv2*) (- *elv* *elv2*) (- *elv2* *elv*)));calc vertical change
		(setq *m* (/ *vrt* *run*));calc slope
	)

	((and (listp pt2) (numberp *m*))
		(setq *run* (distance pt1 pt2));calc run
		(setq *run* (if (= (getvar 'measurement) 0) (/ *run* 12) *run*));set run to feet or mm
		(setq *m* (/ *m* 100));set slope to decimal
		(setq *vrt* (* *run* *m*))
		(setq *m* (* *m* 100));set slope to percent
		(setq *elv2* (if (= *dir* "up") (+ *elv* *vrt*) (- *elv* *vrt*)));calc elvation at second point
	)
)

(setq info (strcat "Starting elevation: " (rtos *elv* 2 2) "\n\nRun is: " (rtos *run* 2 2) "\n\nSlope is: " (rtos *m* 2 2) "%" "\n\nEnding elevation: " (rtos *elv2* 2 2)))
(alert info)
(setq *elv* *elv2*)

);defun

 

0 Likes
Message 8 of 10

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... but having a problem with the (getpoint) function and getting a string return. I'm unsure know what to use to have the option to prompt for a real number, but have the option to return a string...

.... 


I'm confused.  What do you want to do with the string?  Since (getpoint) will return a list of 3 numbers, that's not the function to use if you want to prompt for a real number.  And by "option to return a string" do you mean what the function would return [that's an easy conversion from what is returned by something like (getreal) or (getdist) or (getint)], or the option for the User to type in a string?  If the latter, precede something like (getpoint) with (initget 128) to allow "arbitrary input" [read about it in Help] and the User can type in a string in response to a (getpoint) [or (getreal) or (getdist) or (getint) if those are better for your purposes] prompt.

Kent Cooper, AIA
0 Likes
Message 9 of 10

Anonymous
Not applicable

Kent, thank you for the help on (initget). I got most of the code functioning the way I want, but there's one last piece I don't know how to fix. Having the option for the user to enter the last used variable would quicken the user entering all these prompts. For instance, the *elv* variable is set to *elv2* at the end of the command so that the next time the user uses the command, they can use the last calculated value for the elevation at their new first point.

 

However, when I hit enter to return the value that's shown in the prompt the value doesn't stick. I'm assuming it's returning a string when it's expecting a real number. Is this something I missed with (initget)? Any help is appreciated! 

 

(cond 
	((listp pt2)
		(initget 128 "verTical eleVation")
		(setq *m* (getreal (strcat "\nEnter percent slope or [verTical change/eleVation at second point] <" (rtos *m* 2 1) ">: ")))
	)

	((= pt2 "verTical")
		(setq *vrt* (getreal (strcat "\nEnter vertical change <" (rtos *vrt* 2 3) ">: ")))
		(initget 128 "seconD")
		(setq *m* (getreal (strcat "\nEnter percent slope or select [seconD point] <" (rtos *m* 2 1) ">: ")))
		)

	((= pt2 "Slope")
		(initget 128)
		(setq *m* (getreal (strcat "\nEnter percent slope <" (rtos *m* 2 1) ">: ")))
		(initget 128 "seconD eleVation")
		(setq *vrt* (getreal (strcat "\nEnter vertical change or [eleVation at second point/select seconD point]: ")))
	)
)
0 Likes
Message 10 of 10

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... when I hit enter to return the value that's shown in the prompt the value doesn't stick. I'm assuming it's returning a string when it's expecting a real number.  .... 

 

(cond 
	((listp pt2)
		(initget 128 "verTical eleVation")
		(setq *m* (getreal (strcat "\nEnter percent slope or [verTical change/eleVation at second point] <" (rtos *m* 2 1) ">: ")))
	)
....

No, it's returning nil if the User hits Enter -- (getreal) always does that.  Merely putting a default value into a prompt string is not enough to have it take effect on Enter.  You need to give it something to do if the returned value is nil.  Also, if you're specifying options in (initget), you don't need the arbitrary-input 128 argument -- an included option will be accepted without that, and with that, the User could type some invalid entry and it would be accepted, rather than the routine asking them to try again.  Here's how I would do it for that first condition [untested]:

 

(initget (if *m* 6 7) "verTical eleVation"); no 0, no negative, no Enter unless prior value present
(setq *m*
(cond
( (getreal ; User picks point or types option [nil on Enter if permitted by (initget)]
(strcat
"\nEnter percent slope or [verTical change/eleVation at second point]"
(if *m* (strcat " <" (rtos *m* 2 1) ">") "")
; offer default value only if present; otherwise don't add any text here
": "
); strcat
); getreal
); User-input [other than Enter] condition
(*m*); use default value on Enter [because first condition test returned nil]
); cond
); setq

If you have a standard default value you would like to offer on first use, that's also possible without the need for setting it at the beginning with something like your

 

    (if (not (numberp *m*)) (setq *m* 2.0))

 

line [the code above assumes this approach, so that there would not yet be a *m* value on first use]:

 

(initget 6 "verTical eleVation"); no 0, no negative, allow Enter whether or not *m* exists yet
(setq *m*
(cond
( (getreal ; User picks point or types option [nil on Enter]
(strcat
"\nEnter percent slope or [verTical change/eleVation at second point] <"
(if *m* (rtos *m* 2 1) "2.0")
; offer prior-value default value if present; otherwise offer 2.0
">: "
); strcat
); getreal
); User-input [other than Enter] condition
(*m*); default prior value on Enter if present
(2.0); initial default value if no prior value present
); cond
); setq

 

Kent Cooper, AIA
0 Likes