Can I insert attribute in all current opened drawing's layout?

Can I insert attribute in all current opened drawing's layout?

skchui6159
Advocate Advocate
4,790 Views
47 Replies
Message 1 of 48

Can I insert attribute in all current opened drawing's layout?

skchui6159
Advocate
Advocate

(defun reptag (tag newvalue ent / alist )
(if (and (= (type ent) (read "VLA-OBJECT")) newvalue)
(progn
(setq alist ( vlax-invoke ent 'GetAttributes))
(foreach a alist
(if (= (vla-get-tagstring a) tag)
(vlax-put-property a 'TextString newvalue)
);i
);fe
);p
(if (= 'ename (type ent)) (reptag tag newvalue (vlax-ename->vla-object ent)));i
);i
(princ));d

(defun c:OS ( / ent ss1)
;(setq ent (car (entsel)))
(foreach Layout (layoutlist)
(setq dateofinput "07/24")
(setq ss1 (ssget '(788 190 0)))
(setq ent (ssname ss1 0))
(reptag "ORIGINAL" "ORIGINAL" ent)
(reptag "SIGNED" "SIGNED" ent)
(reptag "GSTAUTHDATE" dateofinput ent)
(reptag "GSTFCHECKDATE" dateofinput ent)
(reptag "GST2CHECKDATE" dateofinput ent)
(reptag "GST1CHECKDATE" dateofinput ent)
(reptag "GSTDRAWDATE" dateofinput ent)
); foreach end
(princ))

 

The above lisp can run only single layout, but I want to apply all current opened drawing's? Anyone can help? Thx

0 Likes
4,791 Views
47 Replies
Replies (47)
Message 21 of 48

skchui6159
Advocate
Advocate

The revise doc. are attached.

 

0 Likes
Message 22 of 48

paullimapa
Mentor
Mentor

Also attach your latest code you are using


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 23 of 48

skchui6159
Advocate
Advocate

; OS updates attributes on all blocks found matching given name
; OP:
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/can-i-insert-attribute-in-all-curren...
(defun c:OS ( / dateofinput ent i obj reptag ss1)
(setq blknam "TITLE BLOCK-A"
dateofinput "07/24"
) ; setq
(vl-load-com)
(defun reptag (tag newvalue ent / alist )
(if (and (= (type ent) (read "VLA-OBJECT")) newvalue)
(progn
(setq alist ( vlax-invoke ent 'GetAttributes))
(foreach a alist
(if (= (vla-get-tagstring a) tag)
(vlax-put-property a 'TextString newvalue)
);i
);fe
);p
(if (= 'ename (type ent)) (reptag tag newvalue (vlax-ename->vla-object ent)));i
);i
(princ)
);d
(if(setq ss1 (ssget "_X" (list '(0 . "INSERT") (cons 2 (strcat blknam ",`*U*")) '(66 . 1))))
(progn
(setq i 0)
(repeat (sslength ss1)
(setq ent (ssname ss1 i)
obj (vlax-ename->vla-object ent)
)
(if (eq blknam (vla-get-effectivename obj))
(progn
(reptag "ORIGINAL" "ORIGINAL" ent)
(reptag "SIGNED" "SIGNED" ent)
(reptag "GSTAUTHDATE" dateofinput ent)
(reptag "GSTFCHECKDATE" dateofinput ent)
(reptag "GST2CHECKDATE" dateofinput ent)
(reptag "GST1CHECKDATE" dateofinput ent)
(reptag "GSTDRAWDATE" dateofinput ent)

#add layout name of each layout
#if version is not A-Z version at the end of layout
(if (= (wcmatch (getvar "ctab") "BM-11*[A-Z]")F)
(reptag "GSTDWGNO" strcat ("ABC/1/BL6/S/" getvar("ctab")) ent)
);end if
#if version is the A-Z version at the end of layout
(if (= (wcmatch (getvar "ctab") "BM-11*[A-Z]")T)
(reptag "GSTDWGNO" strcat ("ABC/1/BL6/S/" (substr (getvar "ctab") 1 6) "/" (substr (getvar "ctab") 7 1)) ent)
);end if
) ; progn
) ; if
(setq i (1+ i))
) ; repeat
(princ(strcat"\nUpdated Blocks matching: " blknam))
) ; progn
(princ(strcat"\nThere are no Blocks found matching: " blknam))
) ; if
(princ)
) ; defun

0 Likes
Message 24 of 48

paullimapa
Mentor
Mentor
Accepted solution

I see a couple of issues...

The first one is simple.

Your title block name does not match with what the code is searching for.

Your current code is looking for:

(setq blknam "TITLE BLOCK-A"

But the title block name you included in your test dwg "blank title block ver1.dwg" is actually:

(setq blknam "TITLE BLOCK"

The second one is more complex.

A. You're not searching for the ctab variable because you're not changing the current tab to match with the found title block. But since the code stays in the current layout to loop through all the found blocks, when you use ctab it'll always be the current layout tab name and not the layout tab where the title block is actually found.

B. Don't use number # symbol to represent comments. You have to use semicolon ; symbol.

C. For the if test, you don't need to do an additional = test. You just need to rely on the wcmatch function to prove if what you're testing is true or not.

D. You have to always remember to begin a function like strcat or getvar with an open parenthesis.

E. Use combination of substr and strlen to get the layout name except the last character or only the last character:

So replace the following lines of code:

#add layout name of each layout
#if version is not A-Z version at the end of layout
(if (= (wcmatch (getvar "ctab") "BM-11*[A-Z]")F)
(reptag "GSTDWGNO" strcat ("ABC/1/BL6/S/" getvar("ctab")) ent)
);end if
#if version is the A-Z version at the end of layout
(if (= (wcmatch (getvar "ctab") "BM-11*[A-Z]")T)
(reptag "GSTDWGNO" strcat ("ABC/1/BL6/S/" (substr (getvar "ctab") 1 6) "/" (substr (getvar "ctab") 7 1)) ent)
);end if

with these:

         (setq layoutname (cdr(assoc 410 (entget ent))))
;add layout name of each layout
         (if (wcmatch layoutname "BM-11*[A-Z]")
;then version is the A-Z version at the end of layout
          (reptag "GSTDWGNO" (strcat "ABC/1/BL6/S/" (substr layoutname 1 (1- (strlen layoutname))) "/" (substr layoutname (strlen layoutname))) ent)
;else version is not A-Z version at the end of layout
          (reptag "GSTDWGNO" (strcat "ABC/1/BL6/S/" layoutname) ent)
         );end if

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 25 of 48

skchui6159
Advocate
Advocate

It works great! Thank you very much. 

Sorry, I forgot to use ";" for the lisp to explain of the statement, I forgot it is lisp not Python😅...

Also, thank you very much for the concept for 410 to get the layer name , the strlen function and my apologize for the name of title block was change (Title block is the real block name of the title block).

0 Likes
Message 26 of 48

paullimapa
Mentor
Mentor

Once again glad to have helped…cheers!!!


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 27 of 48

skchui6159
Advocate
Advocate

One more complicated question, the repeated tags can be edited by lisp?

If I want the first orginal signed to Blank (order by top to bottom), it is possible?

 

0 Likes
Message 28 of 48

paullimapa
Mentor
Mentor

I would not change the Tag names otherwise you’ll lose any of the current values. 
when you say blank do you mean nothing like try this

(reptag "ORIGINAL" "" ent)
(reptag "SIGNED" "" ent)

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 29 of 48

skchui6159
Advocate
Advocate

If something like that? It is possible to do? It is only way by changing/update by blockeditor and then change the tag name?

skchui6159_0-1725065826090.png

 

0 Likes
Message 30 of 48

paullimapa
Mentor
Mentor

You can change tag name using lisp. You can read up on it in this thread


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 31 of 48

skchui6159
Advocate
Advocate

I can not run the lisp, it shows error. 

0 Likes
Message 32 of 48

paullimapa
Mentor
Mentor

Include the revised lisp you’re trying to run


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 33 of 48

skchui6159
Advocate
Advocate

Hello, can I also get the boundary coordinate of above block?

0 Likes
Message 34 of 48

paullimapa
Mentor
Mentor

Since I'm not sure what you plan to do, here's the code to get the lower left & the upper right coordinates of your typical title block "TITLE BLOCK" which only works if it's orthogonally placed and not rotated and your UCS is set to World. Here's a section of the code OS.lsp after the blknam has been confirmed which is where you can include the code to get the coordinates:

      (if (eq blknam (vla-get-effectivename obj))
        (progn
          (vla-getBoundingBox obj 'p0 'p1)  
          (setq lowerleft (vlax-safearray->list p0) ; get lower left coordinate
                upperight (vlax-safearray->list p1) ; get upper right coordinate
          )

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 35 of 48

skchui6159
Advocate
Advocate

can also get the top of left corner's and bottom of right corner's coordination? 

0 Likes
Message 36 of 48

paullimapa
Mentor
Mentor
Accepted solution

here you go:

(setq loweright(list(car upperight)(cadr lowerleft)(caddr lowerleft))
      upperleft(list(car lowerleft)(cadr upperight)(caddr upperight))
)

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 37 of 48

skchui6159
Advocate
Advocate

Thank you very much!

0 Likes
Message 38 of 48

paullimapa
Mentor
Mentor

you are welcome...cheers!!!


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 39 of 48

skchui6159
Advocate
Advocate

Can you also help me to change the Width Factor to (for example: 0.7) of the above attribute? Thank you!

skchui6159_2-1737382953851.png

 

 

 

0 Likes
Message 40 of 48

paullimapa
Mentor
Mentor

Could not tell from your screenshot the attribute Tag name. Also do you only want to make this change on one Block with this attribute or all Blocks with this attribute? What is the Block name?


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes