Author Topic: Trying to get filename to use but there is error  (Read 2003 times)

0 Members and 1 Guest are viewing this topic.

HasanCAD

  • Swamp Rat
  • Posts: 1421
Trying to get filename to use but there is error
« on: August 07, 2020, 04:39:41 AM »
I am trying to use file name as a prefix but when press enter result is not the filename

Thanks

Code - Auto/Visual Lisp: [Select]
  1. (setq filepath (getvar "dwgname"))
  2. (setq filename (VL-FILENAME-BASE filepath))
  3.  
  4. (setq fileprefix
  5.        (cond ((getstring (strcat "\nEnter filename prefix: < "
  6.                                  (setq fileprefix
  7.                                         (cond (fileprefix)
  8.                                               (filename)
  9.                                         )
  10.                                  )
  11.                                  " >?: "
  12.                          )
  13.               )
  14.              )
  15.              (fileprefix)
  16.        )
  17. )


The result is
Quote
""

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Trying to get filename to use but there is error
« Reply #1 on: August 07, 2020, 08:22:37 AM »
Check DWGTITLED
If it is 0 no path is included in DWGNAME
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Dlanor

  • Bull Frog
  • Posts: 263
Re: Trying to get filename to use but there is error
« Reply #2 on: August 07, 2020, 08:27:57 AM »
Your condition (cond) is incorrect

Perhaps
Code - Auto/Visual Lisp: [Select]
  1.  
  2.     (setq filepath (getvar "dwgname"))
  3.     (setq filename (VL-FILENAME-BASE filepath))
  4.      
  5.     (setq fileprefix
  6.       (cond ( (getstring (strcat "\nEnter filename prefix: < " (setq fileprefix (if (not fileprefix) filename)) " >?: ")))
  7.             (t fileprefix)
  8.       )
  9.     )
  10.  
  11.  
  12.  


HasanCAD

  • Swamp Rat
  • Posts: 1421
Re: Trying to get filename to use but there is error
« Reply #3 on: August 07, 2020, 10:43:54 AM »
Thanks Keith

Thanks Dlaner but gives the same result
In command line shows the filename but when click Enter gives this
Code: [Select]
"222-Layout2.dwg"
"222-Layout2"
""

Dlanor

  • Bull Frog
  • Posts: 263
Re: Trying to get filename to use but there is error
« Reply #4 on: August 07, 2020, 01:19:46 PM »
What are you expecting to get?

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Trying to get filename to use but there is error
« Reply #5 on: August 07, 2020, 01:42:03 PM »
Upon closer examination, the whole issue is with COND and getstring and the evaluation order.

Consider this:
Code - Auto/Visual Lisp: [Select]
  1.     (setq fileprefix ;;THIS IS THE SECOND TIME fileprefix IS SET TO A VALUE
  2.         (cond ;; this designates that there are several conditions to test against
  3.             ;; Begin COND1
  4.             (
  5.                  ;;CAPTURE USER INPUT
  6.                  (getstring
  7.                       ;;USING A CONCATENATED STRING PROMPT
  8.                       (strcat
  9.                             "\nEnter filename prefix: < " ;;WITH THIS STRING
  10.                              ;; PLUS THE RETURN VALUE OF THIS EVALUATION
  11.                              (setq fileprefix ;;THIS IS THE FIRST TIME fileprefix IS SET TO A VALUE
  12.                                    ;;IF WE DON'T HAVE A FILENAME WITHOUT EXTENSION
  13.                                    (if (not fileprefix)
  14.                                          ;;JUST USE THE FILENAME WITH EXTENSION  
  15.                                          filename)
  16.                               );; END SETQ
  17.                                " >?: " ;;AND THIS STRING TO BUILD OUR PROMPT
  18.                       );; END STRCAT
  19.                  );; END GETSTRING
  20.             );;END COND1
  21.             ;;BEGIN COND2 - FAILOVER RETURN fileprefix to user
  22.             ;;This code will never be executed because getstring will always pause
  23.             ;;and wait for user input. pressing enter at getstring prompt returns
  24.             ;;returns an empty string thus fulfilling the COND as it is NOT NIL
  25.             (t fileprefix)
  26.         );;END COND
  27.     );;END SETQ

Of course the fix is rather simple:
Code - Auto/Visual Lisp: [Select]
  1.     ;;corrections in CAPITALS
  2.     (setq fileprefix
  3.         (cond
  4.             ((getstring (strcat "\nEnter filename prefix: < "(setq fileprefix (if (not fileprefix) filename FILEPREFIX)) " >?: " )) FILEPREFIX)
  5.             (t fileprefix)
  6.         )
  7.     )

All that being said, a COND is really unnecessary, but it does work
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Trying to get filename to use but there is error
« Reply #6 on: August 07, 2020, 02:09:54 PM »
The getstring function will return "" instead of nil if the user presses Enter.
Code - Auto/Visual Lisp: [Select]
  1. (setq filepath (getvar "dwgname"))
  2. (if (not fileprefix) (setq fileprefix (vl-filename-base filepath)))
  3. (if (/= "" (setq tmp (getstring (strcat "\nEnter filename prefix: < " fileprefix " >?: "))))
  4.   (setq fileprefix tmp)
  5. )

HasanCAD

  • Swamp Rat
  • Posts: 1421
Re: Trying to get filename to use but there is error
« Reply #7 on: August 09, 2020, 04:06:53 AM »
Code - Auto/Visual Lisp: [Select]
  1. ;;corrections in CAPITALS
  2.     (setq fileprefix
  3.         (cond
  4.             ((getstring (strcat "\nEnter filename prefix: < "(setq fileprefix (if (not fileprefix) filename FILEPREFIX)) " >?: " )) FILEPREFIX)
  5.             (t fileprefix)
  6.         )
  7.     )

Thanks Working perfect

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Trying to get filename to use but there is error
« Reply #8 on: August 09, 2020, 06:19:29 AM »
Thanks Working perfect
Are you sure? :-o
Keith's code completely ignores the getstring user input.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Trying to get filename to use but there is error
« Reply #9 on: August 10, 2020, 01:02:24 PM »
Thanks Working perfect
Are you sure? :-o
Keith's code completely ignores the getstring user input.

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: [Select]
  1. (defun c:test (/ fn pr pf)
  2.   (setq fn (vl-filename-base (getvar "dwgname")))
  3.   (setq pr (strcat "\nEnter filename prefix: < " fn " >?: "))
  4.   (setq pf (getstring pr))
  5.   (if (= pf "")
  6.     (setq pf fn)
  7.   )
  8.   pf
  9. )


Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie