Jump to content

Recommended Posts

Posted

Good morning,

 

I am hoping this is a quick and easy one:

In my PDF plot LISP routine I want to add a check whether the file name the user enters into a DCL edit box is currently open or not (if open, send a warning and don't try to plot)

To work out if a file is open I use :

  (if (vl-file-rename filename filename)
    (setq FO "No")  ;;file is closed
    (setq FO "Yes") ;;file is open
  )

filename is (strcat filelocation fn type) 

where fn is filename from edit box, filelocation is the drawings file location and type is just a text string "*.pdf".

 

I've tried with fn being the drawings file name: (vl-filename-base (getvar 'dwgname)) and it all works.. so suspecting that fn is the problem

 

I think the problem is that it is not recognising that the string from my filename edit box should be a file name and not just text.. if that makes sense? Has anyone got any ideas what I could try?

 

Thanks in advance

Posted

Have you tried printing the value of your variable filename to the command-line before attempting the rename operation, so that you can verify that it contains the value you expect?

Posted

Thanks Lee,

That is what is really confusing me, I've done a (princ filename), filepath and so on to show what it is actually doing and as far as I can tell it all looks good.

 

Just tried (vl-file-rename "C:\\Users\\me\\Desktop\\drawing.pdf" "C:\\Users\\me\\Desktop\\drawing.pdf") in the command line (with and without the double \\) and it is returning nil with or without the file drawing being opened.

 

I'm not sure if creating the file name and its path as a string might be doing something weird

 

Posted

I'm not sure if it does - the internet is full of the (vl-file-rename filename filename) method to check if a  file is open (was it Lee's idea for that originally?), so that's what I am using.

 

However I think you've helped me get a bit closer. I have tried it another way, (vl-file-rename filename (strcat filename "qwerty") ) so now the target file doesn't exist and it still isn't working right  - the file isn't renamed.

 

I do have a bit that does work right, when I start the PDF LISP:

 

;;;;;Check if the output file is opened
(defun PDFOpened( PDFOfilename / PDFO)
  (if (vl-file-rename PDFOfilename PDFOfilename)
    (setq PDFO "No")  ;;file is closed
    (setq PDFO "Yes") ;;file is open
  )
  PDFO
)


;;;;Starting the PDF LISP
;;;;Checks if the DWG file has a PDF with the same name that is open
(Defun PDFgetfilename ( / myfnloc myfn myf)
  (setq myfnloc (getvar "dwgprefix"))
  (setq myfn (vl-filename-base (getvar 'dwgname)))
  (setq myf (strcat myfnloc myfn ".pdf"))
  (if (= "Yes" (PDFOpened myf))
    (alert (strcat myfn " is open\nPlease close it or rename the output file"))
  )
  myfn
)
;;;;This bit works as it should




;;;; When the DCL bos is open, I can change the file name,
;;;;Check if the new filename is open as a PDF:
(defun PDFFileNameCheck ( / myfnloc myfn myf)
  (setq myfnloc (get_tile "Location")) ;;a text, "DWG location" or "Desktop" - I can replace that from above and no change

;;this is my problem i think
  (setq myfn (vl-princ-to-string (get_tile "afilename")) ) ;;tile 'afilename' is an edit box, so is just text


  (setq myf (strcat myfnloc myfn ".pdf"))

  (if (= "Yes" (PDFOpened myf))
    (alert (strcat myf "\nis open. Please use a different name or close the PDF file."))
  ) ;end if
  myfn
)

 

 

Thanks for your help so far

 

Posted

Pehaps (vl-princ-to-string) is the problem. What do these produce

(setq myf (strcat myfnloc (get_tile "afilename") ".pdf"))

or

 

(setq myfn (get_tile "afilename"))

 

Posted

Yes, I should have taken that out - that was left over as I was trying something else. Whoops.

 

Still no good - there is something odd.

Still playing with this. If I use, say

(setq myfn "Drawinga")

in the checking LISP it all seams to work as I expect (without getting the file name from the DCL box). Pretty sure it is something to do with grabbing the wrong sort of text from the DCL if that makes sense? (I know that's not a proper description but hope you know what I mean).

 

Again thanks for taking the time to help out so far

Posted (edited)

Maybe autocad quirk... you could work aroud using vl-file-copy like if and (setq source (findfile org)) and not findfile target vl-file-copy source target + if findfile target vl-file-delete source. It aint pretty and maybe neither am I , but still I get the job done 😉

Edited by rlx
Posted

Firstly, to clarify: there is no difference in the data type of the data returned by a get_tile expression for an edit_box DCL tile (or equivalently the $value symbol referenced as part of an action_tile statement), and the string used to represent a filename and/or filepath - both are merely strings.

 

To help you diagnose the issue, I would suggest using the following function:

;; Check if the output file is opened
(defun PDFOpened ( PDFOfilename )
    (vl-file-rename PDFOfilename PDFOfilename)
)

;; Starting the PDF LISP
;; Checks if the DWG file has a PDF with the same name that is open
(defun PDFgetfilename ( / fn )
    (setq fn (strcat (getvar "dwgprefix") (vl-filename-base (getvar 'dwgname)) ".pdf"))
    (prin1 fn)
    
    (if (PDFOpened fn)
        (alert (strcat fn " is open\nPlease close it or rename the output file"))
    )
    fn
)

;; When the DCL bos is open, I can change the file name,
;; Check if the new filename is open as a PDF:
(defun PDFFileNameCheck ( / fn )
    (setq fn (strcat (get_tile "Location") (get_tile "afilename") ".pdf"))
    (prin1 fn)

    (if (PDFOpened fn)
        (alert (strcat fn "\nis open. Please use a different name or close the PDF file."))
    )
    fn
)

The use of the prin1 function shows you the raw AutoLISP data, rather than the output rendered by the string interpreter.

  • Like 1
Posted

I know there is no difference but it just feels that way at the moment.

 

Thanks. I'll try the Prin1 function in the morning

 

RlX will try that also - not pretty maybe but does the job

Posted

when handling files the findfile function can make sure / verify you have the correct file(name) like (if (setq file (findfile file)).... and of course you must make sure , as suggested by master Lee that a file is not in use / locked and also you have write permission , both on the file and in the folder. Good luck & happy coding.

  • Like 1
Posted

Happy codig... wasn't happy earlier when it wasn't working...

  • Funny 2
Posted

Thanks Lee and RLX, I've tried this just now, but no luck.

 

- I tried your code Lee - looks nicer than mine so I will also keep it if that's OK?, still the same thing

- The prin1 output (for your version and my version) shows the file name and path are the same in each case

- The folder is 'read only', however 'PDFgetfilename' check works, the 'PDFFilenameCheck' doesn't work (same files, same location, same read only folder, should do the same thing)

- I tried (findfile ) function to check, shows files to rename exist and so on...

 

So I am going to move on for now and come back to this later with fresh eyes and brain. It might be there is an error somewhere else that hasn't become obvious until now messing with things (that is very possible with me). The first part warning me that a PDF file is opened with the same name as the DWG looks like it is working, just not the check that a new name isn't also open and I think I can live with that for now.

 

 

Thanks again for your help, and as always on here, I learnt a little bit more. If I work out what is wrong on the next week or so I'll let you know what it was.

 

Posted

Thanks, I am having a look at that, see if I can do anything with that

Posted

Thanks everyone, I think I got it working, even though I said I was going to leave it (and do paid work for a change)

 

I added a findfile :

  (if (findfile myf)
    (if (= "Yes" (PDFOpened myf)) ;; - Lee, I am about to change this to your seggestion in a second
      (alert (strcat myf "\nis open. Please use a different name or close the PDF file."))
    ) ; end if
  ) ; end if

 

No idea why, but its sort of working for now

 

 

Posted
12 hours ago, Steven P said:

- I tried your code Lee - looks nicer than mine so I will also keep it if that's OK?

 

Certainly - glad you were able to get your program working in the end.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...