Code Red > AutoLISP (Vanilla / Visual)

Trying to get filename to use but there is error

<< < (2/2)

Keith™:
Upon closer examination, the whole issue is with COND and getstring and the evaluation order.

Consider this:

--- Code - Auto/Visual Lisp: ---    (setq fileprefix ;;THIS IS THE SECOND TIME fileprefix IS SET TO A VALUE        (cond ;; this designates that there are several conditions to test against             ;; Begin COND1            (                 ;;CAPTURE USER INPUT                 (getstring                      ;;USING A CONCATENATED STRING PROMPT                      (strcat                            "\nEnter filename prefix: < " ;;WITH THIS STRING                             ;; PLUS THE RETURN VALUE OF THIS EVALUATION                             (setq fileprefix ;;THIS IS THE FIRST TIME fileprefix IS SET TO A VALUE                                   ;;IF WE DON'T HAVE A FILENAME WITHOUT EXTENSION                                   (if (not fileprefix)                                         ;;JUST USE THE FILENAME WITH EXTENSION                                           filename)                              );; END SETQ                               " >?: " ;;AND THIS STRING TO BUILD OUR PROMPT                      );; END STRCAT                 );; END GETSTRING            );;END COND1            ;;BEGIN COND2 - FAILOVER RETURN fileprefix to user            ;;This code will never be executed because getstring will always pause             ;;and wait for user input. pressing enter at getstring prompt returns             ;;returns an empty string thus fulfilling the COND as it is NOT NIL            (t fileprefix)        );;END COND    );;END SETQ
Of course the fix is rather simple:

--- Code - Auto/Visual Lisp: ---    ;;corrections in CAPITALS    (setq fileprefix        (cond            ((getstring (strcat "\nEnter filename prefix: < "(setq fileprefix (if (not fileprefix) filename FILEPREFIX)) " >?: " )) FILEPREFIX)            (t fileprefix)        )    )
All that being said, a COND is really unnecessary, but it does work

roy_043:
The getstring function will return "" instead of nil if the user presses Enter.

--- Code - Auto/Visual Lisp: ---(setq filepath (getvar "dwgname"))(if (not fileprefix) (setq fileprefix (vl-filename-base filepath)))(if (/= "" (setq tmp (getstring (strcat "\nEnter filename prefix: < " fileprefix " >?: "))))  (setq fileprefix tmp))

HasanCAD:

--- Quote from: Keith™ on August 07, 2020, 01:42:03 PM ---
--- Code - Auto/Visual Lisp: ---;;corrections in CAPITALS    (setq fileprefix        (cond            ((getstring (strcat "\nEnter filename prefix: < "(setq fileprefix (if (not fileprefix) filename FILEPREFIX)) " >?: " )) FILEPREFIX)            (t fileprefix)        )    )
--- End quote ---

Thanks Working perfect

roy_043:

--- Quote from: HasanCAD on August 09, 2020, 04:06:53 AM ---Thanks Working perfect

--- End quote ---
Are you sure? :-o
Keith's code completely ignores the getstring user input.

Keith™:

--- Quote from: roy_043 on August 09, 2020, 06:19:29 AM ---
--- Quote from: HasanCAD on August 09, 2020, 04:06:53 AM ---Thanks Working perfect

--- End quote ---
Are you sure? :-o
Keith's code completely ignores the getstring user input.

--- End quote ---

You are correct

His initial problem was that pressing enter in GETSTRING returns an empty string thus an empty string was always returned.
Pressing enter will now return the name of the drawing, however, it will not return the value captured by GETSTRING because it is never stored.

In order to resolve this issue, the function would need to be altered.

I wouldn't have attempted to write it in this manner.

I would have written it something like this:


--- Code - Auto/Visual Lisp: ---(defun c:test (/ fn pr pf)  (setq fn (vl-filename-base (getvar "dwgname")))  (setq pr (strcat "\nEnter filename prefix: < " fn " >?: "))  (setq pf (getstring pr))  (if (= pf "")    (setq pf fn)  )  pf)

Navigation

[0] Message Index

[*] Previous page

Go to full version