copy objects in one DWG files with lots of layouts (sheets)

copy objects in one DWG files with lots of layouts (sheets)

mruPRQUJ
Advocate Advocate
1,112 Views
13 Replies
Message 1 of 14

copy objects in one DWG files with lots of layouts (sheets)

mruPRQUJ
Advocate
Advocate

Hi there,

 

There is one DWG files with lots of layouts (sheets). Is it possible to create a lisp to copy objects from one layout to all other layouts? thank you very much in advance! 🙂

0 Likes
Accepted solutions (3)
1,113 Views
13 Replies
Replies (13)
Message 2 of 14

LDShaw
Collaborator
Collaborator

This is when I wish there was a remove post.
Sorry I did not read your post well enough.  Mine will just copy the layouts.

I did that not too long ago for someone. 
Try this. 

 

 

 

;;; ==============================================================================
;;; Lisp Name: foo.lsp
;;; Author: Lonnie
;;; Date Created: 2024-07-02
;;; Last Edited: [Insert Last Edit Date]
;;;
;;; DESCRIPTION:
;;; A routine to set up page configurations by importing them from a specified DWG file.
;;;
;;; Usage:
;;; 1. Load the Lisp routine.
;;; 2. Run the command "foo" in AutoCAD.
;;;
;;; Parameters:
;;; None explicitly, but internally uses the DWG path and page setup name.
;;;
;;; Returns:
;;; None
;;;
;;; Notes:
;;; - This routine imports the "3624 PDF" page setup from the specified DWG file 
;;;   ("x:\\xx\\Page Setups.dwg") and applies it to the current drawing.
;;;
;;; Related URL:
;;; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/copy-objects-in-one-dwg-files-with-lots-of-layouts-sheets/td-p/12949888
;;; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/set-a-page-setup-current-with-lsp/td-p/12917953
;;;
;;; ---------------------------- Main Program --------------------------------

(defun C:foo ()
  (command "._-PSETUPIN" "x:\\xx\\Page Setups.dwg" "3624 PDF")
  (vlax-for x
    (vla-get-plotconfigurations
      (setq d (vla-get-activedocument (vlax-get-acad-object))))
    (if (= (strcase "3624 PDF") (strcase (vla-get-name x)))
      (vl-catch-all-apply 'vla-copyfrom (list (vla-get-activelayout d) x))
    )
  )
)

 

 

 


When I made that I kept messing up on the names so I created this one to list all the names.

 

 

 

(defun C:3624PageSetup ()
(command "._-PSETUPIN" "x:\\xx\\trash1.dwg" "3624 PDF")

(defun lm:psetup2tab (/ d found setup-list)
(setq found nil) ; Initialize a variable to track if the page setup is found
(setq setup-list "") ; Initialize a string to collect page setup names

(vlax-for x (vla-get-plotconfigurations
(setq d (vla-get-activedocument (vlax-get-acad-object))))
(setq setup-list (strcat setup-list (vla-get-name x) "\n")) ; Append each page setup name to setup-list
(if (= (strcase "3624 PDF") (strcase (vla-get-name x)))
(progn
(setq found T) ; Set found to true if the page setup is found
(vl-catch-all-apply 'vla-copyfrom (list (vla-get-activelayout d) x))
)
)
)

(if (not found) ; If the page setup is not found, display an alert
(alert (strcat "The page setup '3624 PDF' was not found.\nAvailable page setups:\n" setup-list))
)
)

(lm:psetup2tab)
)

 

 

 

Message 3 of 14

Sea-Haven
Mentor
Mentor

There is no problem copy objects to other layouts use Copybase or copyclip, just make sure when looping through all layouts that you skip the one you started in.

Message 4 of 14

Moshe-A
Mentor
Mentor
Accepted solution

@mruPRQUJ hi,

 

Check this quick CPY2LAY command.

The command is based on COPYBASE & PASTECLIP at 0,0,0. Your layouts must be synchronize in order this successfully work.

 

enjoy

Moshe

 

 

(defun c:cpy2lay (/ sorLay ss lay)
 (setvar "cmdecho" 0)
 (command "._undo" "_begin")
  
 (setvar "tilemode" 0)         ; move to layouts
 (command "._pspace")  	       ; activate paper space
 (setq sorLay (getvar "ctab")) ; save current layout name

 (if (setq ss (ssget))
  (progn
   (command "._copybase" "0,0,0" "_si" ss)
   (foreach lay (layoutlist)

    (if (/= lay sorLay)
     (progn
      (setvar "ctab" lay)
      (command "._pasteclip" "0,0,0")
     ); progn
    ); if
     
   ); foreach
  ); progn
 ); if

 (setvar "ctab" sorLay) ; restore layout

 (command "._undo" "_end")
 (setvar "cmdecho" 1)
  
 (princ)
); c:cpylay

 

 

 

Message 5 of 14

mruPRQUJ
Advocate
Advocate

Hi there,

 

Sorry to reply to you so late as I am busy with my work. Could you please provide a little more information how to use this lisp? After it was loaded, please see the information below,

mruPRQUJ_0-1723495244347.png

 

Thanks again! 🙂

0 Likes
Message 6 of 14

mruPRQUJ
Advocate
Advocate

Hi there,

 

Sorry to reply to you so late as I am busy with my work. It works, but there is information below,

"error: extra right paren on input"

 

Could you please correct it? thank you very much! 🙂

0 Likes
Message 7 of 14

ronjonp
Mentor
Mentor

@mruPRQUJ wrote:

Hi there,

 

Sorry to reply to you so late as I am busy with my work. It works, but there is information below,

"error: extra right paren on input"

 

Could you please correct it? thank you very much! 🙂


@Moshe-A code loads correctly for me. Perhaps you did not copy the entire snip.

Message 8 of 14

mruPRQUJ
Advocate
Advocate

Yes, I made a mistake. It works perfect now. Thanks a million! 🙂

0 Likes
Message 9 of 14

mruPRQUJ
Advocate
Advocate

The import page setup is very useful as well. But it did not work.   Please see the lisp and information below:

Could you please provide some advice to me? Many thanks. 🙂

 

information:

FOO
._-PSETUPIN Enter file name: C:UUsersmruDesktopBLOCKSPage Setups.dwg
"UUsersmruDesktopBLOCKSPage Setups.dwg": Can't find file.
Command: 3624 PDF Unknown command "3624 PDF". Press F1 for help.

 

Lisp:

(defun C:foo ()
(command "._-PSETUPIN" "C:\Users\mru\Desktop\BLOCKS\Page Setups.dwg" "3624 PDF")
(vlax-for x
(vla-get-plotconfigurations
(setq d (vla-get-activedocument (vlax-get-acad-object))))
(if (= (strcase "3624 PDF") (strcase (vla-get-name x)))
(vl-catch-all-apply 'vla-copyfrom (list (vla-get-activelayout d) x))
)
)
)

0 Likes
Message 10 of 14

ronjonp
Mentor
Mentor
Accepted solution

@mruPRQUJ You can also try this version. It does not need to cycle through the tabs.

(defun c:ctabs (/ ad ch f fn fno id l lyts r s x)
  ;; RJP » 2019-08-21
  ;; Original idea: http://www.theswamp.org/index.php?topic=3012.msg37650#msg37650
  (vlax-for x
	    (setq lyts (vla-get-layouts (setq ad (vla-get-activedocument (vlax-get-acad-object)))))
    (setq l (cons (list (vla-get-taborder x) (vla-get-name x)) l))
  )
  ;; RJP » 2022-01-19 (layoutlist) returns an odd order this is better
  (setq l (mapcar 'cadr (cdr (vl-sort l '(lambda (r j) (< (car r) (car j)))))))
  (cond
    ((null (setq l (vl-remove (getvar 'ctab) l))) (princ "\nHahahaha..."))
    ((or (> (getvar 'cvport) 1) (= (getvar 'tilemode) 1))
     (princ "\nPlease go to a paperspace tab...")
    )
    ((not (setq s (ssget ":L"))) (princ "\nBye..."))
    ((and (setq fn (vl-filename-mktemp nil nil ".dcl")) (setq fno (open fn "w")))
     (foreach x	(list "ctabs : dialog"
		      "{"
		      "    label = \"Available Layouts\";"
		      "    :boxed_column"
		      "    {"
		      "        label = \"Select layouts to copy objects to:\";"
		      "        : list_box"
		      "        {"
		      (strcat "            key = \"layouts\";"
			      "            height = "
			      (itoa (+ 2 (length l)))
			      ";"
		      )
		      "            multiple_select = true;"
		      "        }"
		      "    }"
		      "    : row"
		      "    {"
		      "        : button"
		      "        {"
		      "            label = \"&Select...\";"
		      "            key = \"select\";"
		      "        }"
		      "        : button"
		      "        {"
		      "            label = \"&All...\";"
		      "            key = \"all\";"
		      "        }"
		      "        : button"
		      "        {"
		      "            label = \"&Cancel\";"
		      "            is_cancel = true;"
		      "            key = \"cancel\";"
		      "        }"
		      "    }"
		      "}"
		)
       (write-line x fno)
     )
     (close fno)
     (setq id (load_dialog fn))
     (new_dialog "ctabs" id)
     (start_list "layouts")
     (mapcar 'add_list l)
     (end_list)
     (mode_tile "select" 1)
     (action_tile "layouts" "(mode_tile \"select\" 0)(setq ch $value)")
     (action_tile
       "select"
       "(setq r (mapcar '(lambda (x) (nth x l)) (read (strcat \"(\" ch \")\")))) (done_dialog 1)"
     )
     (action_tile "all" "(setq r l) (done_dialog 1)")
     (setq f (start_dialog))
     (unload_dialog id)
     (vl-file-delete fn)
     (cond
       (r
	(setq s (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))))
	(foreach x r
	  (vlax-invoke ad 'copyobjects s (vla-get-block (vla-item lyts x)) nil)
	  (princ (strcat "\n" (itoa (length s)) " items copied to tab: " x))
	)
       )
     )
    )
    ((print "Something went wrong..."))
  )
  (princ)
)
Message 11 of 14

mruPRQUJ
Advocate
Advocate

It works great! Thanks a lot! 🙂

0 Likes
Message 12 of 14

mruPRQUJ
Advocate
Advocate

Hi there,

 

It works before. But it did not work now. Please refer to info below

mruPRQUJ_0-1724862658563.png

 

Could you please provide some advice to me? thank you very much in advance. 🙂

0 Likes
Message 13 of 14

ronjonp
Mentor
Mentor
Accepted solution

@mruPRQUJ 

That means you only have one paperspace tab in the drawing. The code is intended to copy to other paperspace tabs and since there aren't any it quits.

Message 14 of 14

mruPRQUJ
Advocate
Advocate

thanks a lot! 🙂

0 Likes