Writing a script to modify a date in a title block

Writing a script to modify a date in a title block

Anonymous
Not applicable
1,593 Views
9 Replies
Message 1 of 10

Writing a script to modify a date in a title block

Anonymous
Not applicable

AutoCAD Mechanical, 2018.

 

I'm going through it all on the command line before putting it into a script. Obviously trying to do it all via keyboard as mouse-clicks don't work in a script.

 

-ATTEDIT [ENTER]

 

Edit attributes one at a time?: Y [ENTER]

 

Enter block name specification: ASI MECH TB [ENTER]

 

Enter attribute tag specification: DATE [ENTER]

 

Enter attribute value specification: * [ENTER]

 

Select attributes

 

 

I can't type anything at this point. It will allow me to mouse-click on the date attribute in the title block, but not on anything else. It clearly knows that I'm after the DATE, since it's the only thing it will allow me to click on, but it still requires me to click on it. After I click it, I can use the keyboard for the rest, to choose replace, and modify the value.

 

 

I found this script on the forums in response to an almost identical question, but it doesn't work.

 

; This script will change to the WD_TB attribute
; of a Title Block. Follow the instructions in
; comments below. I suggest you turn off Word
; Wrap (Format > Word wrap).
-attedit
n
n
; Change this line to the name of the Title Block
TITLE_BLOCK
WD_TB
; Change this line to match the value stored in the WD_TB attribute.
AAA
; This is the new value you want.
BBB
QSAVE

 

Bonus question: I'd like to modify the first four characters of the drawing # attribute. One of them, for example, is 1683ME-TT117. I want to change the first four numbers to 1713, but keep everything else the same. There are about fifty drawings, with different drawing numbers. For instance there's also 1683ME-TT146. Is there any way to modify only those first 4 digits? Maybe set the cursor at the start of the line, activate delete 4 times, and add 1713?

 

Thanks a ton for any help!

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

pendean
Community Legend
Community Legend
FIND command' replace option not a simpler solution?
0 Likes
Message 3 of 10

Anonymous
Not applicable

It is not, because the date isn't always the same in each drawing. I would have to go through and make a list of all existing dates, then have it search for each date and replace it. It would be faster to just change them manually.

0 Likes
Message 4 of 10

pendean
Community Legend
Community Legend
You need to be posting in the LISP forum then https://forums.autodesk.com/t5/autocad-customization/ct-p/AutoCADTopic1
0 Likes
Message 5 of 10

pbejse
Mentor
Mentor

@Anonymous wrote:

It is not, because the date isn't always the same in each drawing. I would have to go through and make a list of all existing dates, then have it search for each date and replace it. It would be faster to just change them manually.


Is that change the string value of the DATE tag regardless of the current value? Or are you wanting to change only the latest of the DATE tag (Assuming there are multiple DATE tag on "ASI MECH TB"  block so the need to collect all existing dates?

 

 

0 Likes
Message 6 of 10

Anonymous
Not applicable

I want to change the value of the DATE tag, regardless of the value. There is only one DATE tag. These are drawings that have been worked on at various times over months or years, so the dates are all different. They all use the same format title block, however, so a script that works for one should work for all.

0 Likes
Message 7 of 10

Anonymous
Not applicable

Tried that one too, but it looks like that can't be put into a script. I want to be able to run the script on maybe 30 files at a time.

0 Likes
Message 8 of 10

pbejse
Mentor
Mentor

@Anonymous wrote:

I want to change the value of the DATE tag, regardless of the value. There is only one DATE tag. These are drawings that have been worked on at various times over months or years, so the dates are all different. They all use the same format title block, however, so a script that works for one should work for all.


 

(defun _ReplaceAttValues ( blkname newvalueS / ss i obj attbutevalues dn dwgno)
     (setq dn (Cadadr newvalueS))
	(if	(setq ss (ssget "_X" (list '(0 . "INSERT")(cons 2 blkname))))
	       (repeat (setq i (sslength ss))
	         	(Setq obj (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
			(setq attbutevalues
	                      (mapcar '(lambda (at)
	                                 (list (Vla-get-tagstring at)
	                                       (Vla-get-textstring at)
	                                       at
	                                 )
	                               )
	                              (vlax-invoke obj 'Getattributes)
	                      )
	               )
	         (if
	               (Vl-every '(lambda (tag) (assoc tag attbutevalues))
	                         (mapcar 'car newvalueS)
	               )
		       (progn	         	
		          (setq dwgno (Cadr (assoc (caadr newvalueS) attbutevalues)))	          
		          (foreach itm (list (car newvalueS)
		                             (list (caadr newvalueS) (strcat dn (substr dwgno (1+ (strlen dn))))))
		            	(vla-put-textstring (last (assoc (Car itm) attbutevalues)) (cadr itm)))
		         )
	           )
	         )
       
       )
  (princ)
  )

 

 

On your script

 

 


(_ReplaceAttValues "ASI MECH TB" '(("DATE" "03-23-2018") ("DWGNO" "1713")))

 

 

HTH

 

0 Likes
Message 9 of 10

devitg
Advisor
Advisor

Instead using a SCRIPT file  .scr , maybe you could use a LISP file .lsp , handling ObjectDBX , no need to open each dwg 

0 Likes
Message 10 of 10

pbejse
Mentor
Mentor

@devitg wrote:

Instead using a SCRIPT file  .scr , maybe you could use a LISP file .lsp , handling ObjectDBX , no need to open each dwg 


Agreed. in that case use this version instead

 

(defun _ReplaceAttValues ( blkname newvalueS / obj attbutevalues dn dwgno)
     (setq dn (Cadadr newvalueS))
     (vlax-for layout (vla-get-layouts (vla-get-ActiveDocument (vlax-get-acad-object)))
       (vlax-for obj (vla-get-block layout)
         	(if (and
	            (= (vla-get-objectname obj) "AcDbBlockReference")
	            (= (strcase (vla-get-effectivename obj)) (strcase blkname))
                 (minusp (vlax-get obj 'HasAttributes))
	          )
	        	(progn
				(setq attbutevalues
		                      (mapcar '(lambda (at)
		                                 (list (Vla-get-tagstring at)
		                                       (Vla-get-textstring at)
		                                       at
		                                 )
		                               )
		                              (vlax-invoke obj 'Getattributes)
		                      )
		               )
		         (if
		               (Vl-every '(lambda (tag) (assoc tag attbutevalues))
		                         (mapcar 'car newvalueS)
		               )
			       (progn	         	
			          (setq dwgno (Cadr (assoc (caadr newvalueS) attbutevalues)))	          
			          (foreach itm (list (car newvalueS)
			                             (list (caadr newvalueS) (strcat dn (substr dwgno (1+ (strlen dn))))))
			            	(vla-put-textstring (last (assoc (Car itm) attbutevalues)) (cadr itm)))
			         )
		           )
	         )
       	)
       )
       )
  (princ)
  )

Holler if you need help putting all this together.

 

 

0 Likes