Dialog File Access with autolisp in AutoCAD

Sometimes it helps to bring up a dialog box instead of having the user type in information. The (getfiled) function brings up a file dialog where you can specify the default extension. Other dialog boxes that might help in your work are the color dialog, (acad_colordlg), and alert box, (alert).

The (getfiled) function

This function displays the standard AutoCAD file dialog box. It returns the name of the file selected by the user, or nil if no file was selected.

(getfiled title filename extension flags)

The title argument specifies the text in the title bar of the dialog. The filename argument specifies a default filename for the dialog. This value can be an empty string. The extension argument specifies the default file extension used to filter the files–if a null string “”, it defaults to * (all file types). The flags argument is the sum of specific values shown below.

Example

(setq FILENAME (getfiled “LISP FILES” “” “lsp” (+ 1 4)))

The (acad_colordlg) function

If your user needs to choose a color in the middle of an AutoLISP routine you can also call up the color dialog box.

(acad_colordlg color_number)

The color_number argument is the default color choosen.

Example

Instead of using a list of colors to choose from like we did in our box routine earlier:

See also  Every List is Evaluated in the Same Specific Manner in Autolisp

(initget 1 “Red Yellow Blue”)
(setq CLR (getkword “\nSelect Red/Yellow/Blue: “))

You could use a dialog choice:
(setvar “cecolor” (itoa(acad_colordlg 4)))

  • Notice that we had to translate the integer supplied by the color dialog box to an ASCII character that the system variable “cecolor” could read.

The (alert) function

Another way to make sure users know what is going on in an AutoLISP routine is to post an alert dialog with a single OK button.

(alert string)

  • The string argument is displayed as the warning message in the alert.
  • Text in the alert box is left-justified.

Practice

Open READ.LSP and save it to a file called READ-BOM.LSP. Then add the file dialog in place of the existing prompt to name the file.

(defun C:READ ()
(setq CE-SAVE (getvar “cmdecho”))
(setvar “cmdecho” 0)
(setq FILENAME (getfiled “Bill of Materials Files” “” “bom” 4))
(setq FILE (open FILENAME “r”))
(setq PT1 (getpoint “\nStarting Point: “))
(setq HT (getdist PT1 “\nHeight: “))
(setq SPACE (* HT 1.667))
(while (setq TEXTLINE (read-line FILE))
(command “TEXT” pt1 ht “0” TEXTLINE)
(setq PT1 (list (car PT1) (- (cadr PT1) SPACE)))
)
(close FILE)
(setvar “cmdecho” CE-SAVE)
(princ)
)

Tip: Finding Files
When a routine calls for an external file, it is often inconvenient or impossible for the user to supply the full pathname. AutoLISP provides the capability to check for the path and returns the full pathname and filename as a string.

(findfile filename)

  • If the file is not found, (findfile) returns nil.
  • If multiple copies of the file exist, the first copy found is returned.

The (findfile) function checks the current directory, the ACAD.EXE directory, and the support files search path set in the OPTIONS dialog box, in that order. If a directory path is supplied, (findfile) searches only the designated directory.

Example
Command: (findfile “acad.mnu”)
“C:\\PROGRAM FILES\\AUTOCAD 2000\\support\\acad.mnu”

A function that requires an AutoCAD support file, such as ACAD.MNU or ACAD.PGP, could include a (findfile) function to locate the file on any machine.

Back to top button