January 09, 2017

AutoLISP: Pickable Command Line Options

AutoLISP® code has long supported the use of keywords with many of the GETxxx functions, to allow the user to specify a command-line option rather than provide the input expected by the particular GETxxx function being used, just like AutoCAD commands. The INITGET function is used, prior to the GETxxx function, to specify the keywords. Several releases back, Autodesk made enhancements to the AutoCAD® command line, one of which allowed for using the mouse to click on a command-line option instead of typing at the keyboard. This was also supported in AutoLISP, and I had used it multiple times.

Over the weekend, I was working on a "quick" personal project in AutoLISP, and I wanted to include pickable keywords. The problem was, I had forgotten the syntax for setting those up, and it took me longer than it should have to both realize the problem I was having was due to improper syntax and to find the correct syntax. To save myself time when that happens the next time (and, I suspect, it will), I am documenting the proper syntax here. I had remembered that the keywords needed to be enclosed in square brackets, but forgotten about separating them with forward slashes. I wrote a quick test function, to verify that the problem was in fact with the GETxxx function message formatting; here is the final, corrected code with the correct syntax:
(defun C:TestKW ( / sKW)
  (initget 1 "Alpha Beta Delta")
  (setq sKW (getkword "\nChose a keyword [Alpha / Beta / Delta]: "))
  (prompt (strcat "\nKeyword selected is -->" sKW "<-- "))
  (prin1)
) ;_ C:TestKW.

No comments: