Tools for export multiple layers as pages in a single PDF

2

Comments

  • This is real rough but a prove concept, use a dummy dwg to test, be in model space when running etc. It needs all sorts of adjust re plot parameters but does make one pdf per layer, it checks do objects exist on that layer if not skip. I have join back into one pdf via Ghostscript. Yes does one pdf per layer but saves in a PDF directory under dwg location. Tested on a dwg with 200 layers made 5 pdfs as that was all that existed.

    ; https://forum.bricsys.com/discussion/39584/tools-for-export-multiple-layers-as-pages-in-a-single-pdf#latest
    ; Plot each layer as single pdf By AlanH

    (defun c:wow ( / )

    (setq Layers (vla-get-layers (vla-get-activedocument (vlax-get-Acad-Object))))

    (setq lst '())
    (vlax-for lay layers
    (setq lst (cons (vlax-get lay 'name) lst))
    )

    ; check that pdf directory exists
    (setq dwgpre (strcat (getvar "dwgprefix") "\pdf"))
    (if (= (vl-file-directory-p dwgpre) nil)
    (vl-mkdir dwgpre)
    )

    (setq dwgpre (strcat dwgpre "\" (cadr (fnsplitl (getvar 'dwgname )))))

    (command "-layer" "M" "Dummy" "on" "" "T" "" "")

    (setq pt1 (getpoint "\nPick lower left point ") pt2 (getcorner pt1 "\nPick upper right "))

    (foreach lay lst
    (setq pdfname (strcat dwgpre "-" lay ".pdf"))
    (setq ss (ssget "x" (list (cons 8 lay) (cons 410 (getvar 'ctab)))))
    (if (= ss nil)
    (princ (strcat "\nMiss lay " lay))
    (progn
    (command "-layer" "off" "*" "N" "on" lay "")
    (setvar "textfill" 1)
    (setvar "fillmode" 1)
    (setvar "PLOTTRANSPARENCYOVERRIDE" 2)
    (COMMAND "-PLOT" "Y" "" "Print As PDF.pc3" "ISO A3 (420.00 x 297.00 MM)" "m" "LANDSCAPE" "N" "W" pt1 pt2 "Fit" "C"
    "y" "Default.ctb" "Y" "" pdfName "N" "y")
    )
    )
    )

    (princ)
    )
    (c:wow)

  • Thanks!

    I do: Copy all (starting at ;http..). Paste into Notepad++. Save as wow.lsp in my folder where all other script is. Bricscad - Appload, point to file. Check checkboax (Loaded). Got this:

    Vertikalt: APPLOAD
    Loading C:\ELPROCAD\Användarfiler\Skriptfiler\wow.lsp

    ; ----- Error around expression -----
    ; (AL_EXECUTEADSDEFUN "ICUICMD" REST)
    ;
    ; error : syntax-error <".pdf"> at [read]
    C:\ELPROCAD\Användarfiler\Skriptfiler\wow.lsp loading failed.

  • edited March 3

    Thanks! Now it works.

    359 layer is not normal but as a compare with above:

    Running test.lsp gives 359 pdf,s (not a single as I wanted) takes about 16,5 minute, then to be: [sorted in layer order, not included in time] dragged into pdfarranger, save as "all.pdf", delete temporary pdf,s takes about 19 minutes total. The resulting is good and searchable but takes longer time than other methods.

    Comparing with option to use "DWG to PDF Converter 2021 MX", as above, is that no third party software is needed.

    Maybe "Exportpdf" is faster than "plot"?

    Edit: Modified script using Exportpdf takes 19 minutes, then there is job to merge and so on…

  • I see then it is a bit trickier ☺

  • edited March 3

    I use ghostscript to merge back into one file, it is part of another lisp. I can provide the code it should make 1 pdf very fast but remember your talking 300+ pdf's.

    I tried Exportpdf just make sure you have zoomed the desired window to full screen.

    Change the plot to (command "exportpdf" pdfname)

    Have a look at this code it has the parts you need re making back into one pdf as part of the lisp, you will need to download and install Ghostscript and cahnge code to suit location. It makes a list of the pdfname's then calls ghostscript.

  • Yes, I have already tried Exportpdf, I edited my post above. It takes also long time. Yes 359 layer is not normal but since I started to measure time on that project, I use same as a reference.

  • edited March 4

    When running on a more normal project, when test, with 31 a modified script works very well. Thanks again.

    Merging with like pdfarranger works just fine since we already use it.

    On my wish/question list:
    1. How to modify so the script starts only when entering wow? (to have it autoloaded, "by Appload")
    Edit: found where

    At 98%, the dwg-file is named like 12345. Five digits. When running script, either provided here or as a .scr the resulting files looks like this in explorer:

    12345_1.pdf
    12345_10.pdf
    12345_100.pdf
    12345_105.pdf
    12345_12.pdf

    and so on..
    So then I use a batch-file containing
    rename ?????_1.pdf ?????0001.pdf
    rename ?????_2.pdf ?????0002.pdf

    all way up to 9999.

    I just drag the bat-file in to the pdf-folder and execute.

    Then the files is sorted as I wish so I easily drag & drop in to pdfarranger.

    2 . A smarter way for that, inside the LISP?
    Edit: No need for it! I just change sort order in Total commander!

    Edit:
    3. Handle If PDF already exist. Gives error now.
    Or just delete dir first?

    Edit:
    4. Delete Dummy layer

    Modified as:

    ; https://forum.bricsys.com/discussion/39584/tools-for-export-multiple-layers-as-pages-in-a-single-pdf#latest

    (defun c:wow ()

    (setq Layers (vla-get-layers (vla-get-activedocument (vlax-get-Acad-Object))))

    (setq lst '())
    (vlax-for lay layers
    (setq lst (cons (vlax-get lay 'name) lst))
    )

    ; check that pdf directory exists
    (setq dwgpre (strcat (getvar "dwgprefix") "\pdf"))
    (if (= (vl-file-directory-p dwgpre) nil)
    (vl-mkdir dwgpre)
    )

    (setq dwgpre (strcat dwgpre "\" (cadr (fnsplitl (getvar 'dwgname )))))

    (command "-layer" "M" "Dummy" "on" "" "T" "" "")

    ;(setq pt1 (getpoint "\nPick lower left point ") pt2 (getcorner pt1 "\nPick upper right "))

    (foreach lay lst
    (setq pdfname (strcat dwgpre "_" lay ".pdf"))
    (setq ss (ssget "x" (list (cons 8 lay) (cons 410 (getvar 'ctab)))))
    (if (= ss nil)
    (princ (strcat "\nMiss lay " lay))
    (progn
    (command "-layer" "off" "*" "N" "on" lay "")
    (setvar "textfill" 1)
    (setvar "fillmode" 1)
    (setvar "PLOTTRANSPARENCYOVERRIDE" 2)
    ;(COMMAND "-PLOT" "Y" "" "Print As PDF.pc3" "ISO A3 (420.00 x 297.00 MM)" "m" "LANDSCAPE" "N" "W" pt1 pt2 "Fit" "C"
    ;"y" "Default.ctb" "Y" "" pdfName "N" "y")
    (COMMAND "EXPORTPDF" pdfName)

    )
    )
    )

    (princ)
    )
    ;(c:wow)

  • Update:

    Thanks to @ALANH,s file, a modified version can instead of creating PDF,s create DWG,s

    Again - Not normal with 359 layer but since all measuring of time is done by that many, here is the result:

    359 files took 3:15 minutes to create.
    Including starting up third party software, drag files in to it, convert to PDF as one single file takes 4 minutes.

    A fully acceptable (backup) solution.

    ; https://forum.bricsys.com/discussion/39584/tools-for-export-multiple-layers-as-pages-in-a-single-pdf#latest

    (defun c:wow ( / )

    (setq Layers (vla-get-layers (vla-get-activedocument (vlax-get-Acad-Object))))

    (setq lst '())
    (vlax-for lay layers
    (setq lst (cons (vlax-get lay 'name) lst))
    )

    ; check that dwg directory exists
    (setq dwgpre (strcat (getvar "dwgprefix") "\dwg"))
    (if (= (vl-file-directory-p dwgpre) nil)
    (vl-mkdir dwgpre)
    )

    (setq dwgpre (strcat dwgpre "\" (cadr (fnsplitl (getvar 'dwgname )))))

    (command "-layer" "M" "Dummy" "on" "" "T" "" "")

    ;(setq pt1 (getpoint "\nPick lower left point ") pt2 (getcorner pt1 "\nPick upper right "))

    (foreach lay lst
    (setq wname (strcat dwgpre "-" lay ".dwg"))
    (setq ss (ssget "x" (list (cons 8 lay) (cons 410 (getvar 'ctab)))))
    (if (= ss nil)
    (princ (strcat "\nMiss lay " lay))
    (progn
    (command "-layer" "off" "*" "N" "on" lay "")
    (setvar "textfill" 1)
    (setvar "fillmode" 1)
    (setvar "PLOTTRANSPARENCYOVERRIDE" 2)
    ;(COMMAND "WBLOCK" wname "" "0,0" "W" pt1 pt2 "" "OOPS")
    (COMMAND "WBLOCK" wname "" "0,0" "W" "-1000,-1000" "1000,1000" "" "OOPS")
    )
    )
    )

    (princ)
    )
    (c:wow)

  • "convert to PDF as one single file takes 4 minutes." Sounds odd There may be a better way to swap the current layer I tested on a 200+ layer dwg I noticed a pdf took like 1 second to make. The ghodstscrip took like 3 seconds for 129 pdfs.

    Latest test version.

  • edited March 6

    "convert to PDF as one single file takes 4 minutes."

    Time for saving 359 layer as 359 dwg,s takes 3:15. Converting 359 dwg,s to pdf *) takes 0:45.

    Total time for all = 4:00

    *) as 1 PDF containing 359 pages.

    Thanks, again!

    Maybe it takes shorter time to convert 359 single PDF,s to one 359 pages PDF by using ghostscript but it still takes a lot of time to create 359 pdf files from 359 layer. (Bricscad stops responding)

    When test with a file with 31 layer, test.lisp creates 31 PDF,s pretty fast and also create single file fast but the DUMMY-layer is still there. Okay, if not save the dwg file it is okay.

    The resulting file is not fully searchable, only TTF-fonts are.

    When test with a file with 178 layer, Bricscad stops responding, but continues anyway.

    So far this is the best/easiest solution as a "plan B" if ordinary method fails.

    Little bit curious why stops responding when many layer? Some buffer gets filled? Maybe put in a forced delayed somewhere in the lisp to get it work with no stress?

    Edit: No it seems to stops responding at random time.

  • So far this is the best/easiest solution as a "plan B" if ordinary method fails.

    No it is not! Resulting file is not searchable. Resulting file also contains all layer

    Best is to save each layer as separate dwg,s and then use another tool for convert to PDF.

    Result file is fully searchable. Result file does not contain any layer.

  • edited March 6

    In the code I posted I look for objects on a layer so only plot layers that actually have objects on them so do you really have 359 Layers with Objects ? Maybe do a purge 1st.

    One thing I did find in a dwg I had layers set to invisible so was getting blank pages. Reset those layers. Possibly also need check for no plot layers. More checks needed when making lst2.

    It may well be reaching limits of memory somewhere may need say a loop with do "A*", do "B*" etc resetting variables back to nil look at lst2 then do next layer group. So A-Z, then 0-9 as 1st character. Can use (chr x) so will reset each time. (chr 65) = "A" (chr 48) = "0"

    Will see if can do something.

  • edited March 7

    Yes, my 359 layers drawing contains 359 sheets/pages of circuits.

    Upload here some years old anonymized file containing 145 layer (+ always layer 0). Also upload a PDF created by my ordinary way, with clickable links between the pages.

    Why is it important that the (pdf) file is searchable?

    The dwg with 145 layer is almost unusable with only a DWG-Viewer or even Bricscad as stand alone. It is necessary to use the tool used when creating the drawings. All people at customer may not have this tool.

    I often use a PDF-reader for searching inside the PDF instead of using Bricscad for search inside dwg..

    If a project does not containing just one dwg with 145 layers but instead containing 145 dwg,s, it is difficult to use Bricscad for search in all those files at once. By create one single PDF with 145 pages, it is easy to search.

  • Maybe OT but in this context I want to recommend a good viewer:

    Site favicon de·caff · de·caff Viewer

    Written i Java, just one file. Free!

    I have send request to the developer to handle layers easy and now it is pretty ease to navigate through a multilayer dwg.

    Using the layer list, navigate by arrow key, use + key to thaw actual layer and freeze other.

    I often use it, like when I'm working with Bricscad and in a fast way will have just a look in some other dwg.
    Or just because it starts up very fast.

  • edited March 9

    Give this a try. Tested on your dwg 146 pdf's back into one pdf, took 1 minute 40 secs. Can add delete all pdfs except single one.

  • edited March 9

    Thanks for keep trying!

    ; error : <SETVAR> system variable not available : <CMDEXHO>

    Edit: CMDECHO works?

    145 layer took 4:48 to create, with Bricscad not responding.

    Dummy layer is left in drawing.

    Resulting single PDF is not searchable.

    Edit: To compare. Exporting 145 layer by a scr-script, right click, send to pdfarranger, save: 2 min, 13 sec.
    (and then cleaning temporary files)

  • I also tried this, in Linux Mint:

    Site faviconExtract all the layers of a PDF file into their own files

    I does not understand how to use "template"?

  • I fixed the typo in code above thanks.

    If you use the correct generate pdf software it should result in a searchable pdf. Maybe Cutepdf or Bluebeam. I found could search for text.

    Just add to code at end

    (setvar 'clayer "0")

    (command "-purge" "Layer" "Dummy" "Y")

  • edited March 10

    Thanks. Each created PDF is fully searchable. The merged PDF is not.

    So I'm back with the tools I already got when I started this thread, If I want a secondary way for creating searchable PDF,s, if ordinary way, for some reason, does not work *)

    Either use existing script, export to single PDF, merge with pdfarranger, clean up files
    or
    use existing script, export to single dwg, convert with DWG to PDF Converter, clean up files
    or
    use existing script, plot to priPrinter, save as PDF, do a OCR with Bluebeam. (no files to clean up)

    *) I got a another thread about cleaning all plot setting since i suspect this have something when the "fuzzy" things happens but I have not get any further with the "investigation".

  • "Thanks. Each created PDF is fully searchable. The merged PDF is not." This appears to be a problem with ghostscript I see a flash of an error message so will try to see if can get any answer from ghostscript forums. Maybe a newer version ?

  • I think ghostscript needs tesseract for handle OCR?

    Excellent pdfarranger seem not support commandline(?)


    Site faviconGitHub - pdfarranger/pdfarranger: Small python-gtk application, which helps the user to merge or split PDF documents and rotate, crop and rearrange their pages using an interactive and intuitive graphical interface.

    Post last edited file that works without any error when using Prompt History in Bricscad.

    But only if there is NOT any previous PDF,s created (?)

    (still got "no response" from Bricscad)

  • Is it possible to, instead, use pdfarranger for merge?

    Use as: pdfarranger.exe file1.pdf file2.pdf file3.pdf works when running from command / batch

    Pdfarranger shows up and the manual work is to "Save as" somewhere.

  • edited March 12

    Did you look at the ghostscript call ? Try similar for pdfarranger.

    Not tested, put in correct path

    (setq Pdfarranger "C:\xx\xxx\pdfarranger.exe")
    (startapp (strcat Pdfarranger (KGA_String_Join plotnames "" "")))

  • Yes, I have tried a lot… using my NOT know how…

    ----- LISP : Call Stack -----
    [0]...C:LAYSPDF <<--

    ----- Error around expression -----
    (KGA_STRING_JOIN PLOTNAMES "" "")
    in file :
    C:\Users\mikaelg\Downloads\240312.lsp

    error : too few / too many arguments at [KGA_STRING_JOIN]

  • edited March 14

    Code looks correct, did it make the pdf's ?

    Try this it may need a space after the exe also.

    (setq Pdfarranger "c:\Users\mikaelg\AppData\Local\Programs\pdfarranger\pdfarranger.exe ")
    (setq pdfs (KGA_String_Join plotnames "" ""))
    (startapp (strcat Pdfarranger pdfs))

    I am sorry trying to avoid adding more software that I will never use again.

  • edited March 14

    Thanks!

    Yes, it creates PDF.

    Still got

    ----- LISP : Call Stack -----
    [0]...C:LAYSPDF <<--

    ----- Error around expression -----
    (KGA_STRING_JOIN PLOTNAMES "" "")
    in file :
    C:\Users\mikaelg\Downloads\240312.lsp

    error : too few / too many arguments at [KGA_STRING_JOIN]

    How can I print out "KGA_STRING_JOIN PLOTNAMES" to a dialog (or a plain text file)? So I can verify filenames?

    Edit: found..

  • This is most recent working file. Thanks again for help.

    There seems to be a limitation at PDFArranger. I got a warning:

    After OK - 119 files (of 145) loads in to PDFarranger.

    Also tried with 106 layer (gives 106 files) and it works.

    Still need to remove temporary PDF first.

  • edited March 15

    After OK - 119 files (of 145) loads in to PDFarranger.

    Also tried with 106 layer (gives 106 files) and it works.

    Maybe pdfs is too long, like more than 255 character or something else?

  • Maybe pdfs is too long, like more than 255 character or something else?

    Yes, if change so there is only filenames as layer.pdf I can get more files into PDFarranger. But with 359 layers it wont work anyway..

Welcome!

It looks like you're new here. Sign in or register to get started.

Welcome!

It looks like you're new here. Sign in or register to get started.