Making an autolisp that copies layouts from an other drawing.

Making an autolisp that copies layouts from an other drawing.

n.k.mackenzie
Explorer Explorer
2,266 Views
9 Replies
Message 1 of 10

Making an autolisp that copies layouts from an other drawing.

n.k.mackenzie
Explorer
Explorer

Hello, 

 

I'm not that familiar with lisp yet, but I would like to copy layouts from other drawings into existing drawings. I have a lisp but it doesn't work. can someone tell me what i am doing wrong?

 

(defun c:layouts (/ fname)

  (and (setq fname (getfiled "Select Drawing" "-- -- 2 -- 01 " "dwg" 0))

       (imp_layouts fname)

       )                                       

  (princ)

  )

 

(defun imp_layouts (source / *acad* doc l_import odbx Doctest

                                     path lays l_list)

  (vl-load-com)

  (setq *acad* (vlax-get-acad-object)

                doc (vla-get-activedocument *acad*))

  ;;**** Check if source file is open

  (vlax-for oDoc (vla-get-documents *acad*)

    (if (= (vla-get-fullname oDoc) source)

      (setq Doctest t)))

  (if (and (not Doctest) (findfile source))

      (progn ;; Ensure that ObjectDBX is available

                (if (> (setq versn (atoi (getvar "AcadVer"))) 15)

                  (setq oDBX (vla-GetInterfaceObject

                                      *acad* (strcat "ObjectDBX.AxDbDocument." versn)))

                  (progn

                    (if (not (vl-registry-read

                                      "HKEY_CLASSES_ROOT\\ObjectDBX.AxDbDocument\\CLSID"))

                      (startapp "regsvr32.exe"

                                               (strcat "/s \"" (findfile "axdb15.dll") "\""))

                      );;if

                    (setq oDBX (vla-GetInterfaceObject

                                                *acad* "ObjectDBX.AxDbDocument"))

                    );;progn

                  );;if

                (vla-open oDBX source);; "opens" source for our use

                (setq lays (vla-get-layouts oDBX));; source layout table

                (if (> (vla-get-count lays) 1);;ensure there are layouts to import

                  (progn

                    (vlax-for x lays ;; cycle thru table & check if name

                              ;; exists in this drawing, omit if so

                      (if (not (or (= "Model" (vla-get-name x))

                                                  (member (vla-get-name x) (layoutlist))))

                               (setq l_list (append (list x) l_list )

                                     b_list (append (list (vla-get-block x)) b_list))

                               );if

                      );for

                    (setvar "ctab" "Model")

                    (if l_list

                      (progn

                               (setq l_import (vlax-safearray-fill

                                                               (vlax-make-safearray vlax-vbObject

                                                                 (cons 0 (- (length l_list) 1))

                                                                 ) l_list)

                                     b_import (vlax-safearray-fill

                                                               (vlax-make-safearray vlax-vbObject

                                                                 (cons 0 (- (length b_list) 1))

                                                                 ) b_list)

                                     );; create safearray for use in ActiveX method

                               (vla-copyobjects oDBX l_import (vla-get-layouts doc))

                               (vla-copyobjects oDBX b_import (vla-get-blocks doc))

                                   (foreach x (layoutlist)

                                     (cond ((= x "Layout1")(vla-delete (vla-item (vla-get-layouts doc) x)))

                                                   ((= x "Layout2")(vla-delete (vla-item (vla-get-layouts doc) x)))

                                                   )

                                     )

                               (princ (strcat "\nLayouts imported successfully: "

                                                      (itoa (length l_list))))

                               ;;import the layouts

                               );progn

                      (princ "\No new layouts to import from the source drawing...")

                      );if

                    );progn

                  (princ "\nSorry, no layouts found in source drawing, try again.")

                  );if

                (vlax-release-object oDBX);; we're done with it

                );progn

    (progn

      (if doctest

                (princ "\nYou tried to use a file in use by Autocad, please close it and try again....")

                (princ "\nSource file not found, please try again....")

                );;if

      );;progn

    );if

  (princ);; exit quietly

  );defun

 

;;;(princ "Import layouts from source drawing to this drawing,")

;;;(princ "type \"(imp_layouts \"source path & file\")\" to run.")

;;;(princ)

 

 

0 Likes
2,267 Views
9 Replies
Replies (9)
Message 2 of 10

Anonymous
Not applicable

Hello @n.k.mackenzie  Do you really need a .lsp for this ?? with the command ADCENTER you get this result.

0 Likes
Message 3 of 10

n.k.mackenzie
Explorer
Explorer
Hello  Frjuniornogueira,



we have several thousand drawings and all these drawings should get these layouts. It takes us too much time to open the drawings 1 by 1. maybe it works with adcenter but im new with that too.
0 Likes
Message 4 of 10

ВeekeeCZ
Consultant
Consultant

@n.k.mackenzie wrote:

Hello, 

 

I'm not that familiar with lisp yet, but I would like to copy layouts from other drawings into existing drawings. I have a lisp but it doesn't work. can someone tell me what i am doing wrong?

 

(defun c:layouts (/ fname)

  (and (setq fname (getfiled "Select Drawing" "-- -- 2 -- 01 " "dwg" 0))

       (imp_layouts fname)

       )                                       

  (princ)

  )

 

(defun imp_layouts (source / *acad* doc l_import odbx Doctest

                                     path lays l_list)

  (vl-load-com)

  (setq *acad* (vlax-get-acad-object)

                doc (vla-get-activedocument *acad*))

  ;;**** Check if source file is open

  (vlax-for oDoc (vla-get-documents *acad*)

    (if (= (vla-get-fullname oDoc) source)

      (setq Doctest t)))

  (if (and (not Doctest) (findfile source))

      (progn ;; Ensure that ObjectDBX is available

                (if (> (setq versn (atoi (getvar "AcadVer"))) 15)

                  (setq oDBX (vla-GetInterfaceObject

                                      *acad* (strcat "ObjectDBX.AxDbDocument." (substr (getvar 'acadver) 1 2))))

                  (progn

                    (if (not (vl-registry-read

                                      "HKEY_CLASSES_ROOT\\ObjectDBX.AxDbDocument\\CLSID"))

                      (startapp "regsvr32.exe"

                                               (strcat "/s \"" (findfile "axdb15.dll") "\""))

                      );;if

                    (setq oDBX (vla-GetInterfaceObject

                                                *acad* "ObjectDBX.AxDbDocument"))

                    );;progn

                  );;if

                (vla-open oDBX source);; "opens" source for our use

                (setq lays (vla-get-layouts oDBX));; source layout table

                (if (> (vla-get-count lays) 1);;ensure there are layouts to import

                  (progn

                    (vlax-for x lays ;; cycle thru table & check if name

                              ;; exists in this drawing, omit if so

                      (if (not (or (= "Model" (vla-get-name x))

                                                  (member (vla-get-name x) (layoutlist))))

                               (setq l_list (append (list x) l_list )

                                     b_list (append (list (vla-get-block x)) b_list))

                               );if

                      );for

                    (setvar "ctab" "Model")

                    (if l_list

                      (progn

                               (setq l_import (vlax-safearray-fill

                                                               (vlax-make-safearray vlax-vbObject

                                                                 (cons 0 (- (length l_list) 1))

                                                                 ) l_list)

                                     b_import (vlax-safearray-fill

                                                               (vlax-make-safearray vlax-vbObject

                                                                 (cons 0 (- (length b_list) 1))

                                                                 ) b_list)

                                     );; create safearray for use in ActiveX method

                               (vla-copyobjects oDBX l_import (vla-get-layouts doc))

                               (vla-copyobjects oDBX b_import (vla-get-blocks doc))

                                   (foreach x (layoutlist)

                                     (cond ((= x "Layout1")(vla-delete (vla-item (vla-get-layouts doc) x)))

                                                   ((= x "Layout2")(vla-delete (vla-item (vla-get-layouts doc) x)))

                                                   )

                                     )

                               (princ (strcat "\nLayouts imported successfully: "

                                                      (itoa (length l_list))))

                               ;;import the layouts

                               );progn

                      (princ "\No new layouts to import from the source drawing...")

                      );if

                    );progn

                  (princ "\nSorry, no layouts found in source drawing, try again.")

                  );if

                (vlax-release-object oDBX);; we're done with it

                );progn

    (progn

      (if doctest

                (princ "\nYou tried to use a file in use by Autocad, please close it and try again....")

                (princ "\nSource file not found, please try again....")

                );;if

      );;progn

    );if

  (princ);; exit quietly

  );defun

 

;;;(princ "Import layouts from source drawing to this drawing,")

;;;(princ "type \"(imp_layouts \"source path & file\")\" to run.")

;;;(princ)

 

 


 

Try now...

Message 5 of 10

n.k.mackenzie
Explorer
Explorer

Hi Z9E3zK5E!

Thanks, i think it's 90% working now. the only thing left to do is that the file opens automatically instead of manually

nkmackenzie_0-1642077148479.png

 

0 Likes
Message 6 of 10

ВeekeeCZ
Consultant
Consultant

Use this, one line per file. Note double flashes.

 

(imp_layouts "G:\\Projects\\City\\2021_XYroute\\Filename.dwg")

Message 7 of 10

n.k.mackenzie
Explorer
Explorer
Sorry , where in the code should i add this?
0 Likes
Message 8 of 10

ВeekeeCZ
Consultant
Consultant

(defun c:ImportAllLayouts ()

(imp_layouts "G:\\Projects\\City\\2021_XYroute\\Filename1.dwg")

(imp_layouts "G:\\Projects\\City\\2021_XYroute\\Filename2.dwg")

(imp_layouts "G:\\Projects\\City\\2021_XYroute\\Filename3.dwg")

)

Message 9 of 10

Sea-Haven
Mentor
Mentor

Would Accoreconsole be a better approach can do entire directory with out knowing dwg names.

 

If you you know DOS yes the old system can use CMD bottom left in Windows, go to directory and do Dir *.dwg /b >dwglst.txt this will make a text file with all your dwg names use replace and add the (imp_layouts ", replace / with \\ and add ") to end of line. Note Notepad++ and Word support search/replace end of line so that is how to add the bits at start and end. 

 

 

Message 10 of 10

deanbourke69
Participant
Participant

For anybody visiting this post, I had some issues getting the imp_layouts code to work efficiently, then I realised that this can do it without hassle:

(command ".layout" "t" filename "*") (just wrap it in a loop thru file names)

Then if you end up with unwanted layouts (and you use doslib):

(defun c:layouts_delete ( / x l)
(if (setq x (dos_multilist "Layouts" "Select to DELETE" (layoutlist) ))
(foreach l x (command ".layout" "DELETE" l))
)
(princ)
)

0 Likes