1

https://stackoverflow.com/a/9495670/12407473

From the question above, when I try to work the code I get

"Error: no function definition: STR".

Can anyone tell me why it does not work for me?? Thank you!

(with-open-file (str "/.../filename.txt"
                     :direction :output
                     :if-exists :supersede
                     :if-does-not-exist :create)
  (format str "write anything ~%"))
Lin Du
  • 88,126
  • 95
  • 281
  • 483
Marcel Watson
  • 13
  • 1
  • 3
  • 2
    `with-open-file` is Common Lisp, not AutoLisp. – Barmar Nov 21 '19 at 03:11
  • [file handling functions in AutoLisp](https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2017/ENU/AutoCAD-AutoLISP-Reference/files/GUID-F70DECFC-DBE1-4F04-A64C-B3F869A636A2-htm.html) – Barmar Nov 21 '19 at 03:13
  • Here's a tutorial: http://www.jefferypsanders.com/autolispintr_rw.html – Barmar Nov 21 '19 at 03:14
  • 1
    I found both those by googling "autolisp write file" – Barmar Nov 21 '19 at 03:15
  • Thanks! I guess my problem was I was googling "LSIP create text file". That and I'm dumb!! ;D – Marcel Watson Nov 21 '19 at 03:49
  • 2
    You need to remember that AutoCad uses their own, proprietary Lisp dialect. Generic Lisp answers won't always work. – Barmar Nov 21 '19 at 03:50
  • 2
    And, so, onto the why. Since AutoLISP has no `with-open-file` macro, it treats that expression as a function call. Which means that the syntax `(str ..)` looks like an argument expression to produce the first argument of the nonexistent `with-open-file` function. AutoLISP is evidently evaluating the arguments of a function before looking it up that function itself, so it discovers first that a function named `str` doesn't exist. A more mature Lisp dialect can produce static warnings about all undefined functions and variables in the expression before evaluating it. – Kaz Nov 21 '19 at 04:30

1 Answers1

0

As noted by others in the comments, the example code that you are using is Common LISP, not AutoLISP (the dialect of LISP used by AutoCAD). As such, functions such as str, with-open-file, and format are not defined in the AutoLISP dialect.

In AutoLISP, the general approach would be as follows:

(if (setq des (open "C:\\YourFolder\\YourFile.txt" "w"))
    (progn
        (write-line "Your text string" des)
        (close des)
    )
)

Commented, that is:

;; If the following expression returns a non-nil value
(if
    ;; Assign the result of the following expression to the symbol 'des'
    (setq des
        ;; Attempt to acquire a file descriptor for a file with the supplied
        ;; filepath. The open mode argument of "w" will automatically create
        ;; a new file if it doesn't exist, and will replace an existing file
        ;; if it does exist. If a file cannot be created or opened for writing,
        ;; open will return nil.
        (open "C:\\YourFolder\\YourFile.txt" "w")
    ) ;; end setq

    ;; Here, 'progn' merely evaluates the following set of expressions and
    ;; returns the result of the last evaluated expression. This enables
    ;; us to pass a set of expressions as a single 'then' argument to the
    ;; if function.
    (progn

        ;; Write the string "Your text string" to the file
        ;; The write-line function will automatically append a new-line
        ;; to the end of the string.
        (write-line "Your text string" des)

        ;; Close the file descriptor
        (close des)
    ) ;; end progn

    ;; Else the file could not be opened for writing

) ;; end if
Lee Mac
  • 15,615
  • 6
  • 32
  • 80
  • Great thank you for all the descriptions!! How on earth does it know where to save the text file after opening from thin air though?? I have (open "xref_list.txt" "w"), and when i close it, no matter what dwg i have open it always finds its way back to the same folder. what if i want it saved relevant to the dwg i have open? – Marcel Watson Nov 29 '19 at 01:41
  • If you do not specify the full path name of the file, `open` assumes you are referring to the AutoCAD default drawing directory (which is typically the My Documents folder on fresh installations). If you want to create the file in a specific location, simply supply the full filepath - for example, to create the file in the work directory, you might use `(open (strcat (getvar 'dwgprefix) "xref_list.txt") "w")` – Lee Mac Nov 29 '19 at 12:09