Lisp creat script batch publish Multiple File

Lisp creat script batch publish Multiple File

Vuxvix
Advocate Advocate
2,023 Views
28 Replies
Message 1 of 29

Lisp creat script batch publish Multiple File

Vuxvix
Advocate
Advocate

Hi!
I'm trying to fix the attached lisp to do publishing for all files in the folder. 
Is there any other function to open Pop-up that can paste the path link instead of selecting the path like lisp is using. It will save more time.
Looking forward to helping. Thanks

0 Likes
Accepted solutions (2)
2,024 Views
28 Replies
Replies (28)
Message 2 of 29

paullimapa
Mentor
Mentor

Would this help

http://www.lee-mac.com/directorydialog.html


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 3 of 29

Vuxvix
Advocate
Advocate
Hi
Can you give more specific instructions?
I run lisp with the bits in the list. Then what needs to be done?
0 Likes
Message 4 of 29

paullimapa
Mentor
Mentor

What function do you have in mind that will let you get the path where the drawings are so you can paste it into your lisp code?

Currently your code shows this to pop up a window to select a folder:

  (setq folderName
  (browsefolder "Select folder to perform batch job: ")
  )

If you know exactly what the path should be then just replace the above with this:

; if you know the path will always be C:\Autodesk
  (setq folderName "C:\\Autodesk\\")

If you want the opportunity to enter the path at the command line then change to this:

  (setq dwgprefix (getvar"dwgprefix")) ; set default path to current dwg path
  (while (not folderName)
   (setq folderName (getstring (strcat "\nEnter folder to perform batch job <" dwgprefix ">: ")))
   (if (eq folderName "") ; if enter 
     (setq folderName dwgprefix) ; then use default path
     (if (not (vl-file-directory-p folderName)) ; else check if path entered exists
       (setq folderName nil) ; if no path found then start over
       (if(not(eq "\\" (substr folderName (strlen folderName)))) ; else check if slash at end
         (setq folderName (strcat folderName "\\")) ; if not then add
       )
     ) ; if folderName
   ) ; if enter
  ) ; while

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 5 of 29

Ar_vxv
Enthusiast
Enthusiast

Hi!
Within my own limited abilities. I combined Lee-Mac's lisp to be able to paste path links.

 

(setq folderName
  (LM:OFD "Select folder to perform batch job: " nil 16)
  )

 

I noticed this is "Open folder dialog". The desired window in my question is "Open File dialog".
Here is the version: Incorporate your lisp hint. but something is wrong. so lisp doesn't work.

 

(defun c:PAD2 (/ dwgs file dwgName scrFile folderName)
  (setq dwgprefix (getvar"dwgprefix")) ; set default path to current dwg path
  (while (not folderName)
   (setq folderName (getstring (strcat "\nEnter folder to perform batch job <" dwgprefix ">: ")))
   (if (eq folderName "") ; if enter 
     (setq folderName dwgprefix) ; then use default path
     (if (not (vl-file-directory-p folderName)) ; else check if path entered exists
       (setq folderName nil) ; if no path found then start over
       (if(not(eq "\\" (substr folderName (strlen folderName)))) ; else check if slash at end
         (setq folderName (strcat folderName "\\")) ; if not then add
       )
     ) ; if folderName
   ) ; if enter
  ) ; while
  (setq scrFile (open (strcat folderName "\\PAD.scr") "w"))
  (foreach file dwgs
 (setq dwgName (strcat "\"" folderName "\\" file "\""))
 (write-line ".Open" scrFile) 
 (write-line dwgName scrFile)
 (write-line "_.Autopublish  " scrFile)
 (write-line "(command)" scrFile)
 (write-line ".zoom e" scrFile)
 (write-line "_.CLOSE _Y " scrFile)
 )  
  (close scrFile)  
  (command ".script" (strcat folderName "\\PAD.scr"))
  (princ)
)

 

 

0 Likes
Message 6 of 29

paullimapa
Mentor
Mentor

Ok, found the error because I added a slash at end of folderName

Remove this section of code:

  (setq dwgprefix (getvar"dwgprefix")) ; set default path to current dwg path
  (while (not folderName)
   (setq folderName (getstring (strcat "\nEnter folder to perform batch job <" dwgprefix ">: ")))
   (if (eq folderName "") ; if enter 
     (setq folderName dwgprefix) ; then use default path
     (if (not (vl-file-directory-p folderName)) ; else check if path entered exists
       (setq folderName nil) ; if no path found then start over
       (if(not(eq "\\" (substr folderName (strlen folderName)))) ; else check if slash at end
         (setq folderName (strcat folderName "\\")) ; if not then add
       )
     ) ; if folderName
   ) ; if enter
  ) ; while

& replace with this:

  (setq dwgprefix (getvar"dwgprefix")) ; set default path to current dwg path
  (setq dwgprefix (substr dwgprefix 1 (1- (strlen dwgprefix)))) ; drop slashes at end
  (while (not folderName)
   (setq folderName (getstring (strcat "\nEnter folder to perform batch job <" dwgprefix ">: ")))
   (if (eq folderName "") ; if enter 
     (setq folderName dwgprefix) ; then use default path
     (if (not (vl-file-directory-p folderName)) ; else check if path entered exists
       (setq folderName nil) ; if no path found then start over
       (if(eq "\\" (substr folderName (strlen folderName))) ; else check if slash at end
         (setq folderName (substr folderName 1 (1- (strlen folderName)))) ; if so then drop
       )
     ) ; if folderName
   ) ; if enter
  ) ; while

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 7 of 29

Ar_vxv
Enthusiast
Enthusiast

error: File load canceled

;;; Function to browse folder
;;; This code was originally posted by kpblc2000 in AUGI AutoLisp Forum

(defun browsefolder (title / shlobj folder fldobj)
  (vl-load-com)
  (setq
 shlobj (vla-getinterfaceobject
   (vlax-get-acad-object)
   "Shell.Application"
 )
 folder (vlax-invoke-method shlobj 'OFD 0 title 0)
  )
  (vlax-release-object shlobj)
  (if folder
 (progn
   (setq
 fldobj (vlax-get-property folder 'self)
 folderName (vlax-get-property fldobj 'path)
   )
   (vlax-release-object folder)
   (vlax-release-object fldobj)
   folderName
 )
  )
)
(princ "\nBatchJob is loaded, Type BatchJob to run.")
(princ)

;;; Main program, command: batchjob                            ;;
;;; old version of line 106:                                   ;;
;;; (browsefolder "Select folder to perform batch job: ")      ;;

(defun c:PAD2 (/ dwgs file dwgName scrFile folderName)
 (setq dwgprefix (getvar"dwgprefix")) ; set default path to current dwg path
 (setq dwgprefix (substr dwgprefix 1 (1- (strlen dwgprefix)))) ; drop slashes at end
  (while (not folderName)
   (setq folderName (getstring (strcat "\nEnter folder to perform batch job <" dwgprefix ">: ")))
   (if (eq folderName "") ; if enter 
     (setq folderName dwgprefix) ; then use default path
     (if (not (vl-file-directory-p folderName)) ; else check if path entered exists
       (setq folderName nil) ; if no path found then start over
       (if(eq "\\" (substr folderName (strlen folderName))) ; else check if slash at end
         (setq folderName (substr folderName 1 (1- (strlen folderName)))) ; if so then drop
       )
     ) ; if folderName
   ) ; if enter
  ) ; while
  (setq scrFile (open (strcat folderName "\\PAD.scr") "w"))
  (foreach file dwgs
 (setq dwgName (strcat "\"" folderName "\\" file "\""))
 (write-line ".Open" scrFile) 
 (write-line dwgName scrFile)
 (write-line "_.Autopublish  " scrFile)
 (write-line "(command)" scrFile)
 (write-line ".zoom e" scrFile)
 (write-line "_.CLOSE _Y " scrFile)
 )  
  (close scrFile)  
  (command ".script" (strcat folderName "\\PAD.scr"))
  (princ)
)

 

0 Likes
Message 8 of 29

paullimapa
Mentor
Mentor
Accepted solution

You missed the line of code that collects the dwgs from the folderName path

Also I dropped the browsefolder routine since you no longer need this:

 

 

; pad2 enter dwg path, create & run script file 
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-creat-script-batch-publish-multiple-file/m-p/12217609#M454238
(princ "\nPAD2 is loading...")
(defun c:PAD2 (/ dwgprefix dwgs file dwgName scrFile folderName)
 (setq dwgprefix (getvar"dwgprefix")) ; set default path to current dwg path
 (setq dwgprefix (substr dwgprefix 1 (1- (strlen dwgprefix)))) ; drop slashes at end
  (while (not folderName)
   (setq folderName (getstring (strcat "\nEnter folder to perform batch job <" dwgprefix ">: ")))
   (if (eq folderName "") ; if enter 
     (setq folderName dwgprefix) ; then use default path
     (if (not (vl-file-directory-p folderName)) ; else check if path entered exists
       (setq folderName nil) ; if no path found then start over
       (if(eq "\\" (substr folderName (strlen folderName))) ; else check if slash at end
         (setq folderName (substr folderName 1 (1- (strlen folderName)))) ; if so then drop
       )
     ) ; if folderName
   ) ; if enter
  ) ; while
  ; need to include the code below to get list of dwg files
  (setq dwgs (vl-directory-files folderName "*.dwg"))
  ;
  (setq scrFile (open (strcat folderName "\\PAD.scr") "w"))
  (foreach file dwgs
   (setq dwgName (strcat "\"" folderName "\\" file "\""))
   (write-line ".Open" scrFile) 
   (write-line dwgName scrFile)
   (write-line "_.Autopublish  " scrFile)
   (write-line "(command)" scrFile)
   (write-line ".zoom e" scrFile)
   (write-line "_.CLOSE _Y " scrFile)
  )  
  (close scrFile)  
; (startapp "Notepad" (strcat folderName "\\PAD.scr")) ; if needed open script file for editing 
  (command ".script" (strcat folderName "\\PAD.scr"))
  (princ)
) ; defun
(princ "\nSuccessfully loaded. Type PAD2 to run.")
(princ)

 

 

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 9 of 29

Ar_vxv
Enthusiast
Enthusiast
Hi Paul!
Although it is a "filedia 0" format, it is sufficient for this lisp. Thanks a lot!
0 Likes
Message 10 of 29

paullimapa
Mentor
Mentor

Glad to have helped…cheers!!!


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 11 of 29

Ar_vxv
Enthusiast
Enthusiast

I encountered an issue while using this lisp to export PDFs for multiple drawings. Some of the drawings have layouts that do not have a "Page setup" assigned. Consequently, the script stops running.

How to edit the script - Let it override a "Page setup" (which may need to be imported from a template file since the dwg file does not have any Page setup) when exporting the PDF file.

Currently, I am using the "Autopublish" command to automatically export the drawing to PDF without modifying any settings. Should I consider an another way?

...
_.Autopublish  
(command)
.zoom e
_.CLOSE _Y 
0 Likes
Message 12 of 29

paullimapa
Mentor
Mentor

here's a link to a web page with a number of pagesetup lisp functions:

you can use getPageSetupName to check if there's a named pagesetup in the current layout.

if not, then use (command "._-PSETUPIN" filename "*") to import all from another dwg or a specific pagesetup before proceeding with autopublish

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 13 of 29

Ar_vxv
Enthusiast
Enthusiast

Hi! Please help me fixed it
1-I use this lisp to apply  Page Setup for all layouts after Psetupin to . But it doesn't work.

; name - the Page Setup Name
; all - a flag, T for all nil for current layout
(defun SetNamePageSetupAllLayouts (name all / lst)
   (or adoc (setq adoc (vla-get-ActiveDocument (vlax-get-acad-object))))
   (if (vl-position
          name
          (vlax-for pltcfg (vla-get-plotconfigurations adoc)
             (setq lst (cons (vlax-get pltcfg 'Name) lst))
          )
       )
      (progn
         (vlax-for layt (vla-get-Layouts adoc)
            (if (/= (vla-get-name layt) "Model")
               (if all
                  (vla-copyfrom layt (vla-item (vla-get-PlotConfigurations adoc) name))
                  (if (= (vla-get-name layt) (getvar 'ctab))
                     (vla-copyfrom layt (vla-item (vla-get-PlotConfigurations adoc) name))
                  )
               )
            )
         )
         (vla-Regen adoc acActiveViewport)
      )
   )
)
 

To call the main function:
; to change to PageSetup eg:"PDF.A1" all layouts
(defun c:PS2A nil
   (SetNamePageSetupAllLayouts "PDF.A1" T)
   (princ)
)

; to change to PageSetup eg:"PDF.A1" just the active layout
(defun c:PA21 nil
   (SetNamePageSetupAllLayouts "PDF.A1" nil)
   (princ)
)

 

2- In line 2: "*" do not insert all page setup. Does it only work with specific page setup?

...
 (write-line "(command "._-PSETUPIN" Yaesu.dwt "*")" scrFile)
 (write-line "PS2A " scrFile)
 (write-line "_.Autopublish  " scrFile)
 (write-line "(command)" scrFile)
 (write-line ".zoom e" scrFile)
 (write-line "_.CLOSE _Y " scrFile)
 )  

 3-How can I incorporate Lisp-1 into lisp-2. To be able to put the values: template file, page setup at the beginning of the lisp file
look like

; to change to PageSetup eg:"PDF.A1" all layouts
(defun c:PS2A nil
   (SetNamePageSetupAllLayouts "PDF.A1" T)
   (princ)
)

; to change to PageSetup eg:"PDF.A1" just the active layout
(defun c:PA21 nil
   (SetNamePageSetupAllLayouts "PDF.A1" nil)
   (princ)
)

Thanks

0 Likes
Message 14 of 29

paullimapa
Mentor
Mentor

(SetNamePageSetupAllLayouts "PDF.A1" T) works for me so I'm not sure what's not working for you.

Perhaps attach your Yaesu.dwt so I can see if you actually have a pagesetup named PDF.A1

Next change this:

(write-line "(command "._-PSETUPIN" Yaesu.dwt "*")" scrFile)

to this:

(write-line "(command \"._-PSETUPIN\" \"Yaesu.dwt\" \"*\")" scrFile)

Also change this:

(write-line "PS2A " scrFile)

to this:

(write-line "(c:PS2A)" scrFile)

finally the first write-line statement should be this:

(write-line "(load\"SetNamePageSetupAllLayouts\")" scrFile)

Just make sure SetNamePageSetupAllLayouts.lsp is saved in one of the AutoCAD's Support File Search PathTrusted Locations so load function will work.

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 15 of 29

Ar_vxv
Enthusiast
Enthusiast
In case some Drawings have Page setup, when loading this script
Page setup "PDF.A1" already exists. Redefine it? [Yes/No] <N>: *Cancel*
how to Yes or No for all !?
0 Likes
Message 16 of 29

paullimapa
Mentor
Mentor

One way is to test if pagesetup PDF.A1 is already in the current dwg and only run psetupin if it's not 

here's a function (get_pgsetup_lst) which returns a list of all pagesetup names in current dwg:

 

(defun get_pgsetup_lst (/ pgsob1 pgsob2)
  (setq pgsob1(cdr(assoc -1(dictsearch(namedobjdict)"ACAD_PlotSettings"))))
  (setq pgslst '())
  (while (setq pgsob2 (dictnext pgsob1 (not pgsob2)))
      (setq pgslst(cons (cdr(assoc 1 pgsob2)) pgslst))
  )
  (if(> (length pgslst) 1)(setq pgslst (aec_sort_lst pgslst <)))  
 pgslst
)

you can then use this to test if "PDF.A1" page setup is already in the dwg:

(member "PDF.A1"(get_pgsetup_lst))

 

the above will return a list if "PDF.A1" is found or nil if not.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 17 of 29

Vuxvix
Advocate
Advocate

Hi!
Sorry for the late reply when I'm the one who needs help. Anyway, work still comes first.
I created another lisp to load the lisp you wrote: "Page SetupList"

 (write-line ".Open" scrFile) 
 (write-line dwgName scrFile)
 (write-line "(load\"PageSetupList\") " scrFile)
 (write-line "(load\"PS2A-PageSetup2AllLayout\") " scrFile)
 (write-line "(member "PDF.A1"(get_pgsetup_lst))" scrFile)
 (write-line "(command \"._-PSETUPIN\" \"Yaesu.dwt\" \"*\")" scrFile)
 (write-line "(c:PS2A)" scrFile)
 (write-line "_.Autopublish  " scrFile)
 (write-line "(command)" scrFile)
 (write-line ".zoom e" scrFile)
 (write-line "_.CLOSE _Y " scrFile)
 )  
  (close scrFil)

However, line No.5 is wrong. I have tried adding "\" in multiple locations. But it's all wrong. So lisp doesn't work.
I also noticed that you are giving me the problem to complete. Thank you for your goodwill.
With my little knowledge, there are definitely many mistakes.
if you are comfortable with your time and mind. Please look into it for me.
Thanks

0 Likes
Message 18 of 29

paullimapa
Mentor
Mentor

**modified with updates**

For line 5 whenever you're writing a statement and it contains a quote " then you have to precede it with a slash \:

 

(write-line "(member \"PDF.A1\"(get_pgsetup_lst))" scrFile)

 

But instead of what you're doing I would include as much as possible inside the single lisp file such as testing if

"PDF.A1" is there or not and if not then bring it in 
PS2A-PageSetup2AllLayout.lsp:

 

; PS2A-PageSetup2AllLayout.lsp
; name - the Page Setup Name
; dwt - drawing template that contains the page setup name
; all - a flag, T for all nil for current layout
(defun SetNamePageSetupAllLayouts (name dwt all / get_pgsetup_lst lst)
 (defun get_pgsetup_lst (/ pgsob1 pgsob2)
  (setq pgsob1(cdr(assoc -1(dictsearch(namedobjdict)"ACAD_PlotSettings"))))
  (setq pgslst '())
  (while (setq pgsob2 (dictnext pgsob1 (not pgsob2)))
      (setq pgslst(cons (cdr(assoc 1 pgsob2)) pgslst))
  )
  (if(> (length pgslst) 1)(setq pgslst (aec_sort_lst pgslst <)))  
  pgslst
 ) 
 ; first test to see if pagesetup name is already in dwg
   (if(not(member name (get_pgsetup_lst)))
    (command "._-PSETUPIN" dwt name) ; if not then insert from template 
   )
   (or adoc (setq adoc (vla-get-ActiveDocument (vlax-get-acad-object))))
   (if (vl-position
          name
          (vlax-for pltcfg (vla-get-plotconfigurations adoc)
             (setq lst (cons (vlax-get pltcfg 'Name) lst))
          )
       )
      (progn
         (vlax-for layt (vla-get-Layouts adoc)
            (if (/= (vla-get-name layt) "Model")
               (if all
                  (vla-copyfrom layt (vla-item (vla-get-PlotConfigurations adoc) name))
                  (if (= (vla-get-name layt) (getvar 'ctab))
                     (vla-copyfrom layt (vla-item (vla-get-PlotConfigurations adoc) name))
                  )
               )
            )
         )
         (vla-Regen adoc acActiveViewport)
      )
   )
)


To call the main function:
; to change to PageSetup eg:"PDF.A1" all layouts
(defun c:PS2A nil
   (SetNamePageSetupAllLayouts "PDF.A1" "Yaesu.dwt" T)
   (princ)
)

; to change to PageSetup eg:"PDF.A1" just the active layout
(defun c:PA21 nil
   (SetNamePageSetupAllLayouts "PDF.A1" "Yaesu.dwt" nil)
   (princ)
)

 

Then the one that writes the script file will just need to include the following lines to load the lisp & run either (c:PS2A) or (c:PS21):

 

 (write-line ".Open" scrFile) 
 (write-line dwgName scrFile)
 (write-line "(load\"PS2A-PageSetup2AllLayout\")" scrFile)
 (write-line "(c:PS2A)" scrFile)  ; for applying name to all layouts
; (write-line "(c:PS21)" scrFile) ; for applying name to current layouts
 (write-line "_.Autopublish  " scrFile)
 (write-line "(command)" scrFile)
 (write-line ".zoom e" scrFile)
 (write-line "_.CLOSE _Y " scrFile)
 )  
  (close scrFil)

 

 

 

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 19 of 29

Vuxvix
Advocate
Advocate

1-with PS2A.lsp: Besides, Looks sound like when opening new file via script. Load PS2A lisp need confirm "allway load/ load once/.."

 

Command: PS2A
._-PSETUPIN Enter file name: ; error: bad argument value: AutoCAD command: T

 

2-With PAD.lsp:

  • Line is not needed anymore!?

 

(write-line "(command \"._-PSETUPIN\" \"Yaesu.dwt\" \"*\")" scrFile)

 

  •  

 

Command: PAD
; error: bad argument type: streamp nil
​



 

 

0 Likes
Message 20 of 29

paullimapa
Mentor
Mentor

that error occurs because I had made a modification update to the lisp which you probably didn't pick up.

This line was this:

(defun SetNamePageSetupAllLayouts (name all dwt / get_pgsetup_lst lst)

I changed it to this:

(defun SetNamePageSetupAllLayouts (name dwt all / get_pgsetup_lst lst)

So now PSA.lsp should work properly:

; PS2A.lsp
; name - the Page Setup Name
; all - a flag, T for all nil for current layout
;; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-creat-script-batch-publish-multiple-file/m-p/12288211#M455772

(defun SetNamePageSetupAllLayouts (name dwt all / get_pgsetup_lst lst)
 (defun get_pgsetup_lst (/ pgsob1 pgsob2)
  (setq pgsob1(cdr(assoc -1(dictsearch(namedobjdict)"ACAD_PlotSettings"))))
  (setq pgslst '())
  (while (setq pgsob2 (dictnext pgsob1 (not pgsob2)))
      (setq pgslst(cons (cdr(assoc 1 pgsob2)) pgslst))
  )
  (if(> (length pgslst) 1)(setq pgslst (aec_sort_lst pgslst <)))  
  pgslst
 ) 
 ; first test to see if pagesetup name is already in dwg
   (if(not(member name (get_pgsetup_lst)))
    (command "._-PSETUPIN" dwt name) ; if not then insert from template 
   )
   (or adoc (setq adoc (vla-get-ActiveDocument (vlax-get-acad-object))))
   (if (vl-position
          name
          (vlax-for pltcfg (vla-get-plotconfigurations adoc)
             (setq lst (cons (vlax-get pltcfg 'Name) lst))
          )
       )
      (progn
         (vlax-for layt (vla-get-Layouts adoc)
            (if (/= (vla-get-name layt) "Model")
               (if all
                  (vla-copyfrom layt (vla-item (vla-get-PlotConfigurations adoc) name))
                  (if (= (vla-get-name layt) (getvar 'ctab))
                     (vla-copyfrom layt (vla-item (vla-get-PlotConfigurations adoc) name))
                  )
               )
            )
         )
         (vla-Regen adoc acActiveViewport)
      )
   )
)


To call the main function:
; to change to PageSetup eg:"PDF.A1" all layouts
(defun c:PS2A nil
   (SetNamePageSetupAllLayouts "PDF.A1" "Yaesu.dwt" T)
   (princ)
)

; to change to PageSetup eg:"PDF.A1" just the active layout
(defun c:PA21 nil
   (SetNamePageSetupAllLayouts "PDF.A1" "Yaesu.dwt" nil)
   (princ)
)

Give it a try again.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes