Author Topic: Insert titleblock onto several layouts  (Read 2185 times)

0 Members and 1 Guest are viewing this topic.

Rabbit

  • Guest
Insert titleblock onto several layouts
« on: June 05, 2012, 04:52:00 PM »
I'm trying to write some code to be able to take a titleblock from layout tab A and place it on layouts B, C, D, E, etc....
The titleblock has atributes so I've included my trusty attribute read write sub-routines.
All I really want to do is copy the titleblock on the first layout directly to the other layouts, with out any changes to the attributes.

The main code I'm working on is (defun C:TC ().........

Thanks for any help,
Rabbit
Code: [Select]

;;;------------------------------------------------------------------------------------------------
;;;Thanks to J. Roper for this section:
;;---------------------------------------------------------
;ATT:GETS - Utility routine to get all attributes for
;an insert object (EN).
;;
;;You pass this function an entity name, like something that would be returned from this example:
;;(setq EN (car (entsel "\nSelect block")))
;;an entity name looks like this when returned on a command line  --><Entity name: 4119ab90>
;;You use this function like this:  (setq ATTLIST (ATT:GETS EN))
;;it returns a list like this ---> (("TAG1 . "VALUE1")("TAG2" . "VALUE2")....)
(defun ATT:GETS (EN / EL RES)
  ;entnext goes on to the next entity.  If what you passed is an attributed block, the next entity will be an attribute.
  (setq EN (entnext EN)
        EL (entget EN));entget "breaks open" the entity name to reveal the entity list
  (while (= "ATTRIB" (cdr (assoc 0 EL)));we check the entity list to see if it is an attribute
    (setq RES (cons
                (cons (cdr (assoc 2 EL));if it is, we create a list of its tag and value, dxf code 2 and 1, using CONS
                      (cdr (assoc 1 EL))) RES)
          EN (entnext EN);go to the next entity
          EL (entget EN)));get the next entities list
  (reverse RES);reverse the list to put it in proper order.
  )

;;This is the compliment to ATT:GETS.  It puts stuff back in a block
;;---------------------------------------------------------
;ATT:REPLACE - Replace attributes in entity (EN)
;from the list (ATTS).  The list can be a sequential
;list of strings to be replaced or a list containing
;tag names and attribute values to be replaced specifically.
;
 (defun ATT:REPLACE (EN ATTS / EN1 EL)
   (setq EN1 EN)
   (if (atom (car ATTS)) ;;is first list element an atom?
      ;;update sequential
      (foreach ATT ATTS
         (setq EN (entnext EN)  ;;get the next entity
               EL (entget EN)   ;;it is an attribute
               EL (subst (cons 1 ATT)  ;;substitute the value
                         (assoc 1 EL) EL)
         )
         (entmod EL) ;;update it in the drawing database
      )
      ;;else update by tag names
      (while (= "ATTRIB" (cdr (assoc 0 (setq EL (entget (setq EN (entnext EN)))))))
         (if (assoc (cdr (assoc 2 EL)) ATTS)
            (entmod (subst (cons 1 (cdr ;;use CDR if dotted pairs, CADR for non-dotted
                    (assoc (cdr (assoc 2 EL)) ATTS)))
                    (assoc 1 EL) EL)
            )
         )
      )
   )
   ;;
   ;; Regenerate the block insert and attributes.
   ;;
   (entupd EN1)
)

(defun delatom (a l / tmp)
(while l
(if (not (equal a (car l) 1e-11))
(setq tmp (cons (car l) tmp)))
(setq l (cdr l)))
(reverse tmp))

;;;main code starts here - this is what I'm trying to figure out
 
(defun c:TC ()
  (vla-startundomark (vla-get-activedocument (vlax-get-acad-object)))
  (setq ActiveDocument (vla-get-activedocument (vlax-get-acad-object)))
  (setq Space (vla-get-paperspace ActiveDocument))
  (setq TBlock (cdr (assoc 331 (entget (vlax-vla-object->ename (vla-item (vla-get-blocks ActiveDocument) "prb2436a"))))))
  (setq AttributeInfo (ATT:GETS TBlock));get attribute value

  (setq LayList (layoutlist))
  (setq LayList (delatom "K1" LayList)
  (setq LayoutObjects (vla-get-layouts ActiveDocument))
  (repeat (length LayList)
    (setq count 0)
    (vla-insertblock (get_item (nth count LayList) LayoutObjects) (vlax-3d-point  2.78388 1.10378 0.0) "prb2436a" 1.0 1.0 1.0 0)
    (ATT:REPLACE (entlast) (list (cons TEMATTTag TEMNewAttribute)))


    ;;;gotta finish the code in here
   
   
  (vla-endundomark (vla-get-activedocument (vlax-get-acad-object)))
)

ronjonp

  • Needs a day job
  • Posts: 7533
Re: Insert titleblock onto several layouts
« Reply #1 on: June 05, 2012, 05:39:31 PM »
See if this helps you ... it will copy selected objects to all paperspace layouts:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test (/ doc ss)
  2.   (and (setq ss (ssget))
  3.        (setq ss (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))))
  4.        (vlax-for x (vla-get-layouts doc)
  5.          (if (zerop (vlax-get x 'modeltype))
  6.            (vlax-invoke doc 'copyobjects ss (vla-get-block x) nil)
  7.          )
  8.        )
  9.   )
  10.   (princ)
  11. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Rabbit

  • Guest
Re: Insert titleblock onto several layouts
« Reply #3 on: June 05, 2012, 06:15:48 PM »
WOW!  That is impressive.  Super quick too.  I had thought about trying to modify it so that I can remove the selection set, but mapcar and some VL stuff still gives me headaches.  Maybe I can deconstruct it and learn from it.

MUCH THANKS!!

Rabbit

WAIT!!!!!  I just figured out that the selection set means that there is more than one selection.  I was focusing on a single block.  Now I'm really impressed.

kruuger

  • Swamp Rat
  • Posts: 637
Re: Insert titleblock onto several layouts
« Reply #4 on: June 05, 2012, 06:28:39 PM »
try also this one.
kruuger

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: Insert titleblock onto several layouts
« Reply #5 on: June 05, 2012, 08:08:08 PM »