Show an alert if a PDF file being created is already opened

Show an alert if a PDF file being created is already opened

Anonymous
Not applicable
1,421 Views
9 Replies
Message 1 of 10

Show an alert if a PDF file being created is already opened

Anonymous
Not applicable

Hi,

 

After achieve this:

http://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/create-pdf-file-from-some-layouts-onl...

 

We realized it could be the case the PDF is being generated could stay opened and in that case the routine fails.

 

Theses PDFs are in a network file, so it could be another user opens the file and another different user runs the routine to recreate that PDF.

I am looking for a way to detect this network file is already opened and in this case, show an alert telling the user that, finish the routine and get back to the original state of the draw.

 

The most important thing I am looking for is to detect the file is opened, is this posible?

 

I think the rest of the procedure I will get the logic to apply.

 

Thanks for your time.

0 Likes
Accepted solutions (2)
1,422 Views
9 Replies
Replies (9)
Message 2 of 10

hmsilva
Mentor
Mentor

Hi JMVelascoMartinez,

quick test,

 

;by Lee Mac
;http://www.cadtutor.net/forum/showthread.php?61300-I-need-a-way-to-check-if-a-pdf-file-is-open&p=416698&viewfull=1#post416698
(defun _FileOpen-p ( filename )
  (not (vl-file-rename filename filename))
)

 

Hope this helps,
Henrique

EESignature

Message 3 of 10

Anonymous
Not applicable

Thanks again Henrique,

 

I had already found another similar approach that I tested isolated and it worked. 

By the way, when I want to integrate into the routine it fails. I tried with your suggestions and is not working neither, I think I am doing something wrong in the function call.

 

Here is my current approach:

 

;; Function to show a dialog box with the msg passed by parameter
;; mode 0 the message is shown at the command bar
;; mode 1 the message is shown as a popup
(defun display-msg (msg mode / )
  (if (= mode 0)
    (prompt (strcat "\n" msg))
    (alert msg)
  )
 (princ)
)

;; filename = filename of file to test
;; Returns T if specified file is open
(defun _FileOpen-p ( filename / f )
  (if (setq f (open filename "a"))
    (close f)
    t
  )
)

(defun CREATE-PDF-LAYERS(/)
 ... more code ...
                                (setq temp (strcat dir_name dwgname "-" name ".pdf"))
				(setq pdffile (vl-string-subst "PLF" "INT" temp))
				;; Close the file if it is opened
	      		        (if (= (_FileOpen-p pdffile) "T")
				  (progn
				    (display-msg "Open PDF")
				    (setvar "cmddia" 1)
				    (setvar "filedia" 1)
				    (setq DWGNAME nil
              				  LEN nil
              				  NAME nil
              				  PLOTTABLIST nil
	      				  pdffile nil) 				  
				    (exit)
				    )
				  )
...more code ...
)

and the command out i get:

 

1 layer deleted.
Command: tilemode
Enter new value for TILEMODE <0>: 0
Command: -plot Detailed plot configuration? [Yes/No] <No>: Y
Enter a layout name or [?] <MP-PR>: MP-CM Regenerating layout.
Regenerating model - caching viewports.
Enter an output device name or [?] <DWG To PDF.pc3>: Dwg To PDF.pc3 
Enter paper size or [?] <ANSI A (11.00 x 8.50 Inches)>: ISO full bleed A3 (297.00 x 420.00 MM) 
Enter paper units [Inches/Millimeters] <Millimeters>: M 
Enter drawing orientation [Portrait/Landscape] <Landscape>: L 
Plot upside down? [Yes/No] <No>: N
Enter plot area [Display/Extents/Layout/View/Window] <Extents>: E 
Enter plot scale (Plotted Millimeters=Drawing Units) or [Fit] <1=1>: 1:1 
Enter plot offset (x,y) or [Center] <Center>: C Plot with plot styles? [Yes/No] <Yes>: Y 
Enter plot style table name or [?] (enter . for none) <OBS_STB.stb>: OBS_STB.stb 
Plot with lineweights? [Yes/No] <Yes>: Y Scale lineweights with plot scale? [Yes/No] <No>: N 
Plot paper space first? [Yes/No] <No>: N Hide paperspace objects? [Yes/No] <No>: N
Enter file name <C:\Users\jvelasco\Desktop\CAD test\R38 DES MP CAMINT 01-13 - Standard\R38 DES MP CAMINT 01-13-MP-CM.pdf>: O:\Cad\Operators Files - Re\OBS Acad Tools\CAD Dep Stuff\Temporal Batch Folder\R38 DES MP CAMPLF 01-13-MP-CM.pdf
Enter file name <C:\Users\jvelasco\Desktop\CAD test\R38 DES MP CAMINT 01-13 - Standard\R38 DES MP CAMINT 01-13-MP-CM.pdf>: Y 
Save changes to page setup [Yes/No]? <N> Y 
Proceed with plot [Yes/No] <Y>: tilemode
Yes or No, please.
; error: Function cancelled

The command keeps waiting for the "Proceed plot"question.

 

 

I tried similar apporach with the function you suggested, and I still can't get it.

 

Can you see my mistake?

 

Thank you so much, I am starting to feel an autolisp beginner thanks to your assistance.

 

0 Likes
Message 4 of 10

hmsilva
Mentor
Mentor
Accepted solution

 

(if (_FileOpen-p pdffile)
    (progn
       ; is open stuff
        )
    (progn
        ; is not open stuff
        )
    )

 

 

Henrique

EESignature

0 Likes
Message 5 of 10

Anonymous
Not applicable
Accepted solution

Once again, thanks!

0 Likes
Message 6 of 10

hmsilva
Mentor
Mentor

@Anonymous wrote:

Once again, thanks!


You're welcome!
Glad I could help

Henrique

EESignature

0 Likes
Message 7 of 10

Anonymous
Not applicable

Hi again Henrique, my apologies but indeed I didn't check the routine when the pdf file is not opened, and in this case the behavieur is the same as it is open. Maybe I did something wrong in the function to check if the file is open.

 

Here is the code we have so far.

Can you see why is always taken as file open?

 

;; Command to create a PDF copy of all the layouts 
;; except those what hold MICs (named as STAMIC*)
(defun CREATE-PDF-LAYERS(/)
	(setvar "cmddia" 0)
	(setvar "filedia" 0)
    ;; Define target directory
  	(setq dir_name "O:\\Cad\\Operators Files - Re\\OBS Acad Tools\\CAD Dep Stuff\\Temporal Batch Folder\\")

    ;; Get the draw name without the extension
	(setq dwgname (GETVAR "dwgname"))
	(setq len (strlen dwgname))
	(setq dwgname (substr dwgname 1 (- len 4)))

    ;; Create a list with all the layouts
	(setq plottablist (acad_strlsort (layoutlist)))

    (foreach name plottablist
		;; Filter layers to skip Model Layer and all named as STAMIC*
		(if (not (wcmatch name "STAMIC*"))
                    (progn
				;; Define the target PDF filename with path
				(setq temp (strcat dir_name dwgname "-" name ".pdf"))
				(setq pdffile (vl-string-subst "PLF" "INT" temp))
				;; Show alert if the PDF is open and not create it
	      		(if (_FileOpen-p pdffile)
					(progn
						; is open stuff
						(display-msg (strcat "The following file is open:\n\n" pdffile "\n\nThis PDF file will not be created, press OK to continue.") 1)
						)
					(progn
						; is not open stuff
						;; Create PDF
						(command "tilemode" "0")
						(command "-plot" "Y" name "Dwg To PDF.pc3" "ISO full bleed A3 (297.00 x 420.00 MM)" "M" "L" "N" "E" "1:1" "C" "Y" "OBS_STB.stb" "Y" "N" "N" "N" pdffile "Y" "Y")
						)
					)
				    
				
			)
		)
    )
	
    ;; Restore configuration
	(setvar "cmddia" 1)
	(setvar "filedia" 1)
	(setq DWGNAME nil
              LEN nil
              NAME nil
              PLOTTABLIST nil
	      pdffile nil) 
	(princ)
)

;; Function to show a dialog box with the msg passed by parameter
;; mode 0 the message is shown at the command bar
;; mode 1 the message is shown as a popup
(defun display-msg (msg mode / )
  (if (= mode 0)
    (prompt (strcat "\n" msg))
    (alert msg)
  )
 (princ)
)

;by Lee Mac
;http://www.cadtutor.net/forum/showthread.php?61300-I-need-a-way-to-check-if-a-pdf-file-is-open&p=416698&viewfull=1#post416698
(defun _FileOpen-p ( filename )
  (not (vl-file-rename filename filename))
)

Thanks for your time.

0 Likes
Message 8 of 10

hmsilva
Mentor
Mentor

The test will error, if the fila does not exist, will return T

Pseudo code:

 

if findfile x

    if _FileOpen-p x

        the file is open, alert! and exit

        is not open, delete the file

not find file, or the file is deleted, do the plot...

 

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 9 of 10

Anonymous
Not applicable

Hi Henrique,

 

Hope you have spent a great weekend.

 

My collaboration with the Cad department has finished. It is a shame becasue I was starting to love autolist, in fact, I remember to love Lisp at university.

I have applied your last suggestion and I have given the script to my collegue to test it. I think it will be fine. Otherwise I will come back.

 

Thanks for your valuable collaboration!

0 Likes
Message 10 of 10

hmsilva
Mentor
Mentor

You're welcome,  JMVelascoMartinez!

Thank you for your kind words.

All the best,

Henrique

EESignature

0 Likes