Update block atributtes in Layout by Layout Tabs Names

Update block atributtes in Layout by Layout Tabs Names

LeoManu
Contributor Contributor
391 Views
3 Replies
Message 1 of 4

Update block atributtes in Layout by Layout Tabs Names

LeoManu
Contributor
Contributor
Hello, I have a routine developed that updates the attributes of my title block in numerical order, but I would like to optimize it to work by numbering the attributes in the order in which the tabs are arranged. It should also allow updating the names if I add a new tab in the middle or move one to a different position. My title block has two numbers: the current page number and the next page number. When it reaches the final page, it should display "--" for the next page number.

 

I am attaching a DWG as an example.

 

Thanks!

 

(defun sort-layout-names (layoutList)
  "Sorts layout names in numerical and alphabetical order."
  (vl-sort
    layoutList
    (function (lambda (a b)
                (< (atoi a) (atoi b))
              )
    )
  )
)

(defun c:UpdateBlockAttributesByLayout ( / layoutList layoutName currentLayout nextLayout blkName blkEnt attData attEnt attVal doc layouts)
  (setq blkName "ROTULO") ; Block name
  (setq doc (vla-get-ActiveDocument (vlax-get-acad-object))) ; Get the active document
  (setq layouts (vla-get-Layouts doc)) ; Get the collection of layouts
  (setq layoutList '()) ; Initialize the list of layouts

  ; Get the list of layout names
  (vlax-for layout layouts
    (setq layoutName (vla-get-Name layout))
    (if (/= layoutName "Model") ; Exclude the model workspace
      (setq layoutList (cons layoutName layoutList)))
  )

  ; Sort the list of layout names numerically and alphabetically
  (setq layoutList (sort-layout-names layoutList))

  ; Iterate over each layout and update the block attributes
  (foreach layoutName layoutList
    (setvar "ctab" layoutName)  ; Switch to the current layout
    
    ; Find the block in the current layout
    (setq blkEnt (ssget "X" (list (cons 0 "INSERT") (cons 2 blkName) (cons 410 layoutName))))
    
    (if blkEnt
      (progn
        (setq attData (vlax-invoke (vlax-ename->vla-object (ssname blkEnt 0)) 'getattributes))
        
        (foreach attEnt attData
          (setq attVal (vla-get-tagstring attEnt))
          
          (cond
            ((equal attVal "ATT1") ; Update the attribute "ATT1" with the current layout name
             (vla-put-textstring attEnt layoutName))
            ((equal attVal "ATT2") ; Update the attribute "ATT2" with the name of the next layout or "--" if it is the last one
             (setq nextLayout (cadr (member layoutName layoutList)))
             (if nextLayout
               (vla-put-textstring attEnt nextLayout)
               (vla-put-textstring attEnt "--"))))
        )
      )
    )
  )
  (princ "\nAll attributes updated successfully.")
  (princ)
)
0 Likes
Accepted solutions (1)
392 Views
3 Replies
Replies (3)
Message 2 of 4

Sea-Haven
Mentor
Mentor

I would start with something like this.

 

 

(setq lst '())
(setq tabs (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))))
(vlax-for tab tabs
(setq lst2 '())
(setq lst2 (cons (vlax-get tab 'name) lst2))
(setq lst2 (cons (vla-get-taborder tab ) lst2))
(setq lst (cons lst2 lst))
)

(setq lst (vl-sort lst '(lambda (x y) (< (car x)(car y)))))

 

((0 "Model") (1 "CL2-PROFILE-1") (2 "CL2-PROFILE-2") (3 "CL2-PROFILE-3") (4 "CL2-PROFILE-4") (5 "CL2-PROFILE-5") (6 "CL1-PROFILE-1") (7 "CL1-PROFILE-2") (8 "CL1-PROFILE-3") (9 "CL1-PROFILE-4") (10 "CL1-PROFILE-5") (11 "CL1-PROFILE-6") (12 "CL1-PROFILE-7") (13 "Xsect-CL1-1") (14 "Xsect-CL1-2"))

 

Then you can get next etc 

0 Likes
Message 3 of 4

komondormrex
Mentor
Mentor
Accepted solution

hey there,

check the following

 

(defun c:set_title_atts (/ layout_list)
	(vlax-map-collection (setq layouts (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))))
		'(lambda (layout)
			(setq layout_list (cons (cons (vla-get-taborder layout) (vla-get-name layout)) layout_list))
		 )
	)
	(setq layout_list (vl-sort (vl-remove (assoc 0 layout_list) layout_list) 
							   '(lambda (layout_1 layout_2) (< (car layout_1) (car layout_2)))
					  )
	)     
	(foreach layout layout_list 
		(setq t_block (ssname (ssget "_x" (list (cons 410 (cdr layout)) '(0 . "insert") '(2 . "rotulo"))) 0))
		(setpropertyvalue t_block "att1" (cdr layout))
		(if (not (equal layout (last layout_list)))
				(setpropertyvalue t_block "att2" (cdadr (member layout layout_list)))
				(setpropertyvalue t_block "att2" "--")
		)
	)
	(princ "\nAll attributes updated successfully.")
	(princ)
)

 

0 Likes
Message 4 of 4

LeoManu
Contributor
Contributor

Thank! works great!

0 Likes