NP CAD Page | Articles | Russian

Sergey I. Vasiliev. "Little Tricks of Default Directory"

This article contains an exercise showing unusual behaviour of some AutoLISP functions handling files in AutoCAD version 2002 or higher (OPEN function is being studied). [NP's NOTE. This was found in Russian version but it must be present in all languages.]

AutoLISP documentation gives the following OPEN function format for reading operation: (OPEN <file name> "r"), where <file name> can be put with the full path (e.g., "c:\\razv_nn\\1451\\korsht") or with incomplete (relative) path, e.g. korsht. The last case relies on the so called "AutoCAD default drawing directory". Russian books usually call it "current folder".

To demonstrate some peculiarities of the OPEN function unzip from archive to your computer folders FOLDER1 and FOLDER2, with the files:
FOLDER1: acad.dwg, korsht1, 1-1.dwg
FOLDER2: korsht2, 1-2.dwg

1. Start AutoCAD by double-clicking ACAD.DWG file. It is clear that the current folder will be FOLDER1.

2. Enter in the command line:
Command: (open "korsht1" "r")
#<file "korsht1">

The last line denotes that KORSHT1 is found and opened.

3. Press "Up button" to call the last command and modify file name from KORSHT1 to KORSHT2. Press ENTER.
Command: (open "korsht2" "r")
nil

Everything is correct, the latest nil means that the KORSHT2 file is not found in the current folder.

4. Perform insertion of 1-2.dwg file as block:
Insert => Block => FOLDER2 => 1-2.dwg
Command: _insert
Specify insertion point or [Basepoint/Scale/X/Y/Z/Rotate]:
Specify scale factor for XYZ axes <1>:

5. Selecting "Up button" check OPEN function work:
Command: (open "korsht1" "r")
#<file "korsht">

Command: (open "korsht2" "r")
nil

Yet no changes can be seen.

6. Repeat 1-2.dwg file insertion and check OPEN function once more (the second time one should insert 1-2 block with redefinition of the existing block with the same name):
Command: (open "korsht1" "r")
nil

Command: (open "korsht2" "r")
#<file "korsht2">

ATTENTION! The result is opposite to the previous one! The KORSHT1 file is not found but the KORSHT2 file is suddenly became accessible for opening, that is current folder has changed to FOLDER2.

7. How can one provide reading the KORSHT1 file? Here is one of the possible ways:
Command: (open (strcat (getvar "dwgprefix") "korsht1") "r")
#<file "C:\\Razv_14.nn\\folder1\\korsht1">

We used DWGPREFIX system variable containing the current drawing path.

8. One can restore the current folder to FOLDER1 with twice insertion of 1-1.dwg block (for the second time you should make insertion with redefinition of the existing 1-1 block):
Command: _insert
Specify insertion point or [Basepoint/Scale/X/Y/Z/Rotate]:
Specify scale factor for XYZ axes <1>:

Command: _insert
Specify insertion point or [Basepoint/Scale/X/Y/Z/Rotate]:
Specify scale factor for XYZ axes <1>:

9. With "Up button" check the OPEN function work:
Command: (open "korsht1" "r")
#<file "korsht1">

Command: (open "korsht2" "r")
nil

All have returned to their places.

CONCLUSIONS:

1. Some AutoCAD commands working with files (e.g. _INSERT command for block insertion) change the variable storing default directory, i.e. current folder. The same problems arise after DWG external reference attachment command (_XATTACH). It is very likely that some other AutoCAD commands bring similar problems up.

2. The behaviour of AutoLISP functions working with files (OPEN, VL-FILE-DELETE, VL-FILE-COPY etc.) is UNPREDICTABLE (N.Poleshchuk's formulation).

3. While programming in AutoLISP one should avoid using functions with incomplete path file names.

4. One of the methods to resolve this problem is using system variables (e.g. DWGPREFIX).

Sergey Izotovich Vasiliev, Nizhny Novgorod, Russia
Tel. +7 8312 64-57-45
E-mail: razv_nn@mail.ru

NP CAD Page | Articles | Russian