Just notes from Opie A commentary on AutoCAD and Civil 3d

User Prompt with Keywords and a Default Option

A lot of routines need to request user input. Fortunately, AutoLISP allows for this through the many getxxx functions. There is even a way to restrict user input to predefined values. The getkword function allows the programmer to specify these predefined values.

The talented Lee Mac explains this method in the tutorial on Prompting with a Default Option. Yet, I would prefer to reuse this method. Thus, we will need a routine which is generic enough for repeat use.

In making this routine, I foresee the need for about three arguments (a list of keywords, a string prompt to the user, and which keyword is the default option).

The first argument, a list of keywords, is simple. Create a list containing each keyword.

	 (setq lOptions '("Alpha" "Beta" "Gamma"))
Select all

Now that we have a list of keywords, we will need to transform them into a combined string. For that, I’ll use the following routine.

	(defun op:implode (data delim / str n)
		;; Join list items into string with delimiter string
		;;_ Copyright © 2013-2016 by Richard Lawrence
		(if (and (= (type data<span class="brkt")</span> 'LIST)
				(= (type delim) 'STR)
				(> (strlen delim) 0)
			)
			(foreach n data
				(if str
					(setq str (strcat str delim n))
					(setq str n)
				)
			)
		)
	)
Select all

This routine requires two arguments (the list data and a string delimiter). The routine will cycle through the list items concatenating each with the delimiter. It will then return a combined string of keywords separated by a delimiter.

Our next argument, the string prompt, is a simple string. We will combine this prompt with the delimited keyword string. But we are not done yet.

We still do not know which keyword is the default option. So, we need our last argument, the default option. We will need this argument specified as an integer. This argument will correspond to one of the keywords in our list.

	(if (and (>= iDefault (length lTypes))
		(<= iDefault 0)
		)
		;; Error check to verify the default
		;; integer is within parameters
		(setq iDefault (1- (length lTypes)))
		(setq iDefault (1- iDefault))
	)
Select all

Unlike most lists which start counting at 0, I chose to make this argument start at 1. If this argument is out of range of the length of the keyword list, it will assign the last keyword as the default.

Now that we have all three arguments, we can create the strings for the initget function and the user prompt.

  (initget (op:implode lTypes " "))
  ;; sets the initget values
  (setq	X (getkword
	    (strcat "\n"
		    sPrompt
		    " ["
		    (op:implode lTypes "/")
		    "] <"
		    (nth iDEFAULT lTypes)
		    ">: "
	    )
	  )
  )
Select all

You can download the completed code below.

;|

OP_KWordPrompt_1.1.lsp

Version History
1.1	April 8, 2016	Reviesed to include implode subroutine
1.0	2005		Initial Release

Request string input from user.

Dependencies:	none
Usage:		(op:KWordPrompt lTypes sPrompt iDefault)
Arguments:	lTypes		<List>		keyword strings
		sPrompt		<String>	prefix for user prompt
		iDefault	<Integer>	represents default keyword, starting at 1
Returns:	string of user specified keyword

Copyright © 2005-2016 by Richard Lawrence

Written permission must be obtained to copy, modify, and distribute 
this software. Permission to use this software for any purpose and 
without fee is hereby granted, provided that the above copyright 
notice appears in all copies and that both the copyright notice and 
the limited warranty and restricted rights notice below appear in 
all supporting documentation.

THIS PROGRAM IS PROVIDED "AS IS" AND WITH ALL FAULTS.  ANY IMPLIED 
WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE ARE 
HEREIN DISCLAIMED. THERE IS NO WARRANTY THAT THE OPERATION OF THE 
PROGRAM WILL BE UNINTERRUPTED OR ERROR FREE.  USAGE OF THIS PROGRAM 
IS AT YOUR OWN RISK.

|;
(defun op:KWordPrompt (lTypes sPrompt iDefault / X $TYPES $TYPES2)
  (defun op:implode (data delim / str n)
    ;; Join list items into string with delimiter string
    (if	(and (= (type data) 'LIST)
	     (= (type delim) 'STR)
	     (> (strlen delim) 0)
	)
      (foreach n data
	(if str
	  (setq str (strcat str delim n))
	  (setq str n)
	)
      )
    )
  )
  (if (or (>= iDefault (length lTypes))
	   (<= iDefault 0)
      )
    ;; Error check to verify the default
    ;; integer is within parameters
    (setq iDefault (1- (length lTypes)))
    (setq iDefault (1- iDefault))
  )
  (initget (op:implode lTypes " "))
  ;; sets the initget values
  (setq	X (getkword
	    (strcat "\n"
		    sPrompt
		    " ["
		    (op:implode lTypes "/")
		    "] <"
		    (nth iDEFAULT lTypes)
		    ">: "
	    )
	  )
  )
  ;; prompts user for selection
  (if (not X)
    (setq X (nth idefault lTYPES))
  )
  X
)
Select allDownload

If you liked this post, you can share it with your followers or follow me on Twitter!

Similar Posts