LISP for Cleaning up Drawing

LISP for Cleaning up Drawing

Anonymous
Not applicable
6,445 Views
13 Replies
Message 1 of 14

LISP for Cleaning up Drawing

Anonymous
Not applicable

Hello all,

 

This is my first post so I apologize in advance if this is not in the correct format or place. I'm looking for a LISP that can automate print to pdf, import to pdf, change layer names, and finally chspace with minimal to none user input

 

Background: We have many very large drawings with many xrefs in them that need to be shrunken in size and standalones(no xrefs) to be shipped off to clients. We only want to send the client what the viewport sees in the layout(and border) so we xtrim  to delete everything outside in model space. This is a VERY VERY time consuming process as drawings are very large and require multiple explosions to successfully trim everything.

 

Current Methodology (not in order):

-Extrim

-xplode

-purge

-delete frozen layers

-delete non-visible layers

 

New Proposed Method:

-Export to pdf with layer data

-Import pdf into new drawing

-remove pdf prefix to layer names, rename command

-chspace

Whala! A standalone drawing much quicker!

 

What  I'm looking for:

 

A LISP to automate print to pdf, open pdf, rename layers, and chspace with minimal/none user input

 

Is this possible? Is this the right way to do this? Current macros require too much user input as most of the above commands have pop-up windows

 

 

0 Likes
6,446 Views
13 Replies
Replies (13)
Message 2 of 14

roland.r71
Collaborator
Collaborator

I did something similar using the WMF format, instead of PDF. Worked like a charm.

...except for reducing the size...

 

xrefs & blocks actually help reducing the size, but those are no more.

 

We did this to prevent our chinese clients from using/editing our drawings. Even text got "exploded" into lines.

You won't see a visual difference, until you start to select things 😉

 

Jusr export the view/drawing to WMF, delete everything (or create a blank DWG) import the WMF, et voila.

 

(i'll see if i can still find some code for it...been years ago)

0 Likes
Message 3 of 14

roland.r71
Collaborator
Collaborator

Found it.

 

But the code is part of a bigger collection, using my own script engine & file selection scripts.

So, i'll limit it to just the used commands:

 

; ! WARNING: Do NOT use the original DWG for this, use a copy!

(setq dwgname (getvar "DWGNAME"))
(setq path          (getvar "DWGPREFIX"))

; set name for wmf file, based on dwg
(setq wmfname (strcat (substr dwgname 1 (- (strlen dwgname) 3)) "wmf"))

; set path for wmf file, based on dwg path
(setq wmfexp (strcat path wmfname))

; export the wmf file
(command "_export" wmfexp "all" "")

; clean up the dwg
(command "_erase" "all" "")

; import the wmf file
(command "_import" wmfexp)

; explode it
(command "_explode" "all")

; purge the drawing
(command "_purge" "all" "*" "N")

; zoom it
(command "_zoom" "_e")

; Delete the wmf file
 ; NOTE: for this to work you need doslib or an alternative function
(DOS_DELETE wmfexp)

; save the flattened/mamed DWG
(command "_save" "")
Message 4 of 14

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... 

New Proposed Method:

-Export to pdf with layer data

-Import pdf into new drawing

.... 


That part is something you won't be able to do with an AutoLisp routine -- they can't start in one drawing and continue in another.

 

A suggested approach:

 

UNDO Mark.

Make all the strip-down changes -- deleting Layers, changing space, Purging, etc.

SAVE [not  QSAVE, not  SAVEAS] to a new external drawing file -- no need to deal with prefixes added to Layer names, etc.  Or just export to .PDF if that's your desired end result [I admit to not understanding why you want a .DWG  file that apparently contains only an imported .PDF  file].

UNDO Back to restore your current drawing as it was.

 

The SAVE command makes a new drawing external to but exactly like the current drawing, but [unlike SAVEAS] leaves you in the current drawing.  So an AutoLisp routine can continue after the SAVE.

 

Would that work for you?  It could be done with an AutoLisp routine.

Kent Cooper, AIA
0 Likes
Message 5 of 14

roland.r71
Collaborator
Collaborator

@Kent1Cooper wrote:

@Anonymous wrote:

.... 

New Proposed Method:

-Export to pdf with layer data

-Import pdf into new drawing

.... 


That part is something you won't be able to do with an AutoLisp routine -- they can't start in one drawing and continue in another.

 

 


Yes & No.

 

You can, if working from SDI mode, using a *.scr file.

Sequentially open the drawing, export, create new drawing, import.

& repeat for each drawing.

 

Of course, any lisp routine should be (re)loaded after each time you open or create a drawing. Any info to be 'transfered' can be stored inside a ini file (for example)

 

As i said, i created my own script engine (to write the *.scr file) & file selection tool, to do this.

 

edit:

I see i still have an old .scr file.

Here's how it looks, for the first 3 drawings (the .scr file contains over 800! )

 

; ------------------------------------------------------------------------------
;                   This script was generated by ACAD M-Tools v2.0 RC 1
;                       Copyleft ;) 2004-2007 | R. Rothenhöfer
; ------------------------------------------------------------------------------
(setvar "FILEDIA" 0)
(load "D:/ACAD_M-Tools_v2.0/MODULES/MOpen.lsp")
(c:MOpen "Q:/Tmp/4Roland/Crane4.dwg")
(if (= skippit nil)
   (progn
      (setq dwgname (getvar "DWGNAME"))
      (setq path    (getvar "DWGPREFIX"))
      (setq wmfname (strcat (substr dwgname 1 (- (strlen dwgname) 3)) "wmf"))
      (setq wmfexp  (strcat path wmfname))
      (command "_model")
      (command "_zoom" "_e")
      (command "_export" wmfexp "all" "")
      (command "_erase" "all" "")
      (command "_import" wmfexp)
      (command "_explode" "all")
      (command "_purge" "all" "*" "N")
      (command "_zoom" "_e")
      (DOS_DELETE wmfexp)
      (command "_save" (strcat path dwgname))
))
(load "D:/ACAD_M-Tools_v2.0/MODULES/MOpen.lsp")
(c:MOpen "Q:/Tmp/4Roland/P4-0A1-01.dwg")
(if (= skippit nil)
   (progn
      (setq dwgname (getvar "DWGNAME"))
      (setq path    (getvar "DWGPREFIX"))
      (setq wmfname (strcat (substr dwgname 1 (- (strlen dwgname) 3)) "wmf"))
      (setq wmfexp  (strcat path wmfname))
      (command "_model")
      (command "_zoom" "_e")
      (command "_export" wmfexp "all" "")
      (command "_erase" "all" "")
      (command "_import" wmfexp)
      (command "_explode" "all")
      (command "_purge" "all" "*" "N")
      (command "_zoom" "_e")
      (DOS_DELETE wmfexp)
      (command "_save" (strcat path dwgname))
))
(load "D:/ACAD_M-Tools_v2.0/MODULES/MOpen.lsp")
(c:MOpen "Q:/Tmp/4Roland/P4-0A1-02.dwg")
(if (= skippit nil)
   (progn
      (setq dwgname (getvar "DWGNAME"))
      (setq path    (getvar "DWGPREFIX"))
      (setq wmfname (strcat (substr dwgname 1 (- (strlen dwgname) 3)) "wmf"))
      (setq wmfexp  (strcat path wmfname))
      (command "_model")
      (command "_zoom" "_e")
      (command "_export" wmfexp "all" "")
      (command "_erase" "all" "")
      (command "_import" wmfexp)
      (command "_explode" "all")
      (command "_purge" "all" "*" "N")
      (command "_zoom" "_e")
      (DOS_DELETE wmfexp)
      (command "_save" (strcat path dwgname))
))
(load "D:/ACAD_M-Tools_v2.0/MODULES/MOpen.lsp")
(c:MOpen "D:/ACAD_M-Tools_v2.0/DATA/temp.dwg")
(setq msg "MDWG : Operation Completed")
(setq countr (DOS_GETINI "MOpen" "num_skipped" "D:/ACAD_M-Tools_v2.0/M-Tools.ini"))
(if (/= countr "0")
   (progn
      (setq msg (strcat msg "\n" countr " file(s) skipped"))
      (if (= (DOS_GETINI "MOpen" "log_skipdfile" "D:/ACAD_M-Tools_v2.0/M-Tools.ini") "1")
         (progn
            (setq confdia (DOS_MSGBOX (strcat msg "\nWould you like to view the logfile ?") "Done" 2 1))
            (if (/= confdia 1)
               (DOS_SHELLEXE "notepad.exe" (DOS_GETINI "MOpen" "logfile" "D:/ACAD_M-Tools_v2.0/M-Tools.ini"))
            )
         )
         (progn
            (DOS_MSGBOX msg "Done" 1 1)
         )
      )
   )
   (progn
      (DOS_MSGBOX msg "Done" 1 1)
   )
)
(setvar "FILEDIA" 1)
Message 6 of 14

Anonymous
Not applicable

Roland,

 

Thankyou for this! I'm new to this,  but I believe I got it working. The LISP does not export whats in the viewport, only the objects in the paper space (LISP called from paperspace). Is there a way to get the wmf export whats seen in the viewport? Also is there anychange to export to pdf instead of wmf, I tried to change it but had no success.

 

Thanks again!

0 Likes
Message 7 of 14

Anonymous
Not applicable

Kent,

 

Its fine if you cannot open another drawing with a lisp, I will just make a copy before running the lisp, and then run the lisp in the new drawing. 

 

The reasoning for making a drawing with just an imported pdf is because by exporting to pdf it automatically binds, and trims the model space into the viewport. ( no need for exploding and then extrim which are very time consuming on large drawings). The client wants a dwg and not a pdf, because its easier for them to measure and inspect items within the drawing.

 

Thankyou again!!

0 Likes
Message 8 of 14

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... 

The reasoning for making a drawing with just an imported pdf is because by exporting to pdf it automatically binds, and trims the model space into the viewport. ( no need for exploding and then extrim which are very time consuming on large drawings). The client wants a dwg and not a pdf, because its easier for them to measure and inspect items within the drawing.

....


That makes some sense, but I would do some careful evaluation of the results of importing the .PDF as to measurements -- I would worry that some dimensional inaccuracies could result, though I haven't tried it, and maybe they wouldn't be beyond acceptable tolerances for your client.

 

Unfortunately, commands that call for object selection [the EXPORT in that code, and also the ERASE and EXPLODE] can only "see" things in the current space.  So some variant approach would be needed.  My suggested SAVE approach would get it all, but it would also include other Layouts if there are any, all the Model Space information however far that may extend beyond the viewport, etc.

Kent Cooper, AIA
0 Likes
Message 9 of 14

roland.r71
Collaborator
Collaborator

@Anonymous wrote:

Roland,
 
Thankyou! I'm new to this,  but I believe I got it working. The LISP does not export whats in the viewport, only the objects in the paper space (LISP called from paperspace). Is there a way to get the wmf export whats seen in the viewport? Also is there any chance to export to pdf instead of wmf, I tried to change it but had no success.
 
Thanks again!


I never tried it with viewports, but your right. It will only export the objects in current space. Ignoring all modelspace objects, in this case.

Tried a few things with selection sets and other formats, but no succes.

 

I did find 1 'solution', which requires a bit more work.

Using exportlayout you can export current layout to a new DWG where a visual representation of the layout will be created in modelspace.

Run the routine on those files & it works.

 

For export to pdf use: exportpdf instead. You can export current or all layouts to PDF.

BUT you can not import a pdf, like a vector file (dwg, 3ds, wmf, dwf etc.) instead it gets attached as document.

Unless you use a PDF 2 DWG converter. (afaik)

 

0 Likes
Message 10 of 14

roland.r71
Collaborator
Collaborator

If your client does not want PDF but DWG for measurements, they will most likely not be happy with a DWG containing a PDF.

You should be extra carefull anyway, as kent pointed out, the measurements can be off at the slightest mistake.

(+ they will likely want selectable lines/points to measure from. )

 

Converting the PDF is no option in this case. Way to inaccurate. Imho.

 

Exporting layouts to DWG isn't working for that either.

 

I created a rectangle: 588x1016 in modelspace

 

If i measure it on the layout, i get those same numbers.

 

After the export it becomes: 117x203

 

0 Likes
Message 11 of 14

Anonymous
Not applicable

 

I'm not convinced yet,

 

pdf export ("epdf") (not plot dwg to pdf.pc3) has precision options for up to 1/1000, the imported pdf still retains layer data (pdf exports layer data) and lines and arcs are still selectable (but you do loose static/dynamic blocks). In your experiment you said you went from 588x1016 to 117x203, they still retain aspect ratio just different scale (probably cause your viewport scale not 1:1)

 

How do you do "Epdf" command in lisp? what arguments must you give to it? Sorry I'm new to this, is there a good resource for this?

 

Thanks again! Really appreciate it!

 

P.S. Autocad 2017

 

0 Likes
Message 12 of 14

roland.r71
Collaborator
Collaborator

@Anonymous wrote:

 

I'm not convinced yet,

 

pdf export ("epdf") (not plot dwg to pdf.pc3) has precision options for up to 1/1000, the imported pdf still retains layer data (pdf exports layer data) and lines and arcs are still selectable (but you do loose static/dynamic blocks). In your experiment you said you went from 588x1016 to 117x203, they still retain aspect ratio just different scale (probably cause your viewport scale not 1:1)

 

How do you do "Epdf" command in lisp? what arguments must you give to it? Sorry I'm new to this, is there a good resource for this?

 

Thanks again! Really appreciate it!

 

P.S. Autocad 2017

 


The scaling occured using exportlayout. Which creates a new dwg with a 'visual representation' of the layout in modelspace. No good for precision.

 

epdf (or exportpdf) doesn't seem to be working without a dialog to save the file (it doesn't 'listen' to the filedia var)

use export instead.

 

e.g:

(setq filedia_start (getvar "FILEDIA"))
(setvar "FILEDIA" 0)
(command "-export" 
         "P"      ; Pdf
         "C"      ; Current layout 
         "N"      ; No (detailed plot cfg) 
         ""       ; Name of file (automatic naming if blank (DWGname-layout.pdf))
         "")
(setvar "FILEDIA" filedia_start)

This will export the active layout to a pdf. (you can easily test different options from the commandline)

0 Likes
Message 13 of 14

roland.r71
Collaborator
Collaborator

I gave the exportlayout another try as it promisses to do exactly what you need/want, but somehow i got the scale mixed up.

 

I was comparing the wrong measurements.

 

Currently my A4 papersize (210mm x 297mm) becomes 194 x 256 ... probably minus some non-printable area

Everything else has the same size as it has on the layout.

Any measurements in acad should be the same as on paper. (using the correct scale factors)

 

So thats still worth looking into.

In fact, i just tried a more complex drawing with dynamic blocks & all... (no viewports though)

Everything that was visible on the layout got retained, including the layers, dynamic blocks 🙂

Everything else is gone.

 

0 Likes
Message 14 of 14

Anonymous
Not applicable

WOW! I had no idea export layout was a thing!!!! Its almost perfect!! I'm looking into that now, checking scale and layer properties, etc I'll let you know how it turns out!

0 Likes