Script writing to replace text

Script writing to replace text

Anonymous
Not applicable
4,550 Views
13 Replies
Message 1 of 14

Script writing to replace text

Anonymous
Not applicable

I need to write a script to replace text, I can type the find & replace comand to replace but I've got like 100 dwg's to do every year

I need to change the date in title blocks from 2016 to 2017 figured script would do then all I'd have to do is change date in script and run...

any and all help would be appreciated

0 Likes
4,551 Views
13 Replies
Replies (13)
Message 2 of 14

hmsilva
Mentor
Mentor

@Anonymous wrote:

I need to write a script to replace text, I can type the find & replace comand to replace but I've got like 100 dwg's to do every year

I need to change the date in title blocks from 2016 to 2017 figured script would do then all I'd have to do is change date in script and run...

any and all help would be appreciated


Hi tabmap!

Try to do a search in 'Search This Board' for 'Script to replace text' and a bunch of threads come up, one may do what you're looking for.

 

Henrique

EESignature

Message 3 of 14

Anonymous
Not applicable

Searched could not find any thing that worked, seen some ref'ed SuperFind but it was for like acad 2005 and url page was gone...most everything wanted to go to lsp programing , any more sugestions,

0 Likes
Message 4 of 14

doglips
Advocate
Advocate

Actually lisp, vba, or vb would be good tools for this effort

Message 5 of 14

Kent1Cooper
Consultant
Consultant

Is it too late to change the way the drawings are put together so that the date is [or is part of] an Xref?  This is exactly what those are for -- change it in the source file only, and it's changed in all drawings into which that is Xref'd.  If not for current sets of drawings, at least seriously consider setting up future drawings that way.

Kent Cooper, AIA
Message 6 of 14

Anonymous
Not applicable

drawing been setup for several years the text I need to change is in a block in the title block.  I can use the find command to change the date every year thought if would be faster to setup a script to do it. I have 2 sets of drawing in one set there are 234 and the other in 200, I'm not much on lisp, vba to set it up that way ....  thanks for all the help

 

0 Likes
Message 7 of 14

zph
Collaborator
Collaborator

This is a segment of a routine that does much more, but I think this is the part you need:

 

 

(defun c:DATEUP ( / NewDate titlePage nextPages ssTTLpageBLKdate i4 obj ssTTLBLKdate i5)

(if (= 1(getvar "TILEMODE"))  ;;switches to last active paper space layout
(setvar "TILEMODE" 0)
) ;if

(setvar "cmdecho" 0)

;***************** Define block name variables *****************;
(setq titlePage "_SP_TITLE_PAGE_08SEP15")			; title page block name
(setq nextPages "_SP-Border2-F28X40_08SEP2015")			; subsequent pages block name
;***************************************************************;

(setq ReadNewDate (open "MAPtoYOURtxtFILEwithTHEnameRevDate.txt/RevDate.txt" "r"))
(setq NewDate (read-line ReadNewRevDate))			; Date format is:  "DDMMMYYYY"
(close ReadNewRevDate)						

(if
	(setq ssTTLpageBLKdate 
		(ssget "_X" 
			(list '(0 . "INSERT")
			  (cons 2 titlePage)
			  '(66 . 1))				
	)) ; ssTTLpageBLKdate

	(repeat (setq i4 (sslength ssTTLpageBLKdate))
		(setq obj    (vlax-ename->vla-object (ssname ssTTLpageBLKdate (setq i4 (1- i4))))
		attlst (vlax-invoke obj 'GetAttributes)
	)
	
	(foreach att attlst
		(if (= (vla-get-TagString att) "DRAWINGDATE")
		(vla-put-TextString att NewDate)      		;;; if specific value is needed, replace NewDate with "new value"
		) ;if
	)) ;fe ;re
) ;if

(if
	(setq ssTTLBLKdate 
		(ssget "_X" 
			(list '(0 . "INSERT")
			  (cons 2 nextPages)
			  '(66 . 1))				
	)) ; ssTTLBLKdate

	(repeat (setq i5 (sslength ssTTLBLKdate))
		(setq obj    (vlax-ename->vla-object (ssname ssTTLBLKdate (setq i5 (1- i5))))
		attlst (vlax-invoke obj 'GetAttributes)
	)
	
	(foreach att attlst
		(if (= (vla-get-TagString att) "DRAWINGDATE")
		(vla-put-TextString att NewDate)      		;;; if specific value is needed, replace NewDate with "new value"
		) ;if
	)) ;fe ;re
) ;if						

(princ "\n\033\n *** Dates updated! ***")
(setvar "cmdecho" 1)
(princ)
) ;DATEUP

 

NOTE:  It does require to set up and map to the .txt file.  In the text file you type your desired date.  You'll need to change the block names from mine (_SP_TITLE...) to yours and do the same with the attribute tag name (DRAWINGDATE).  This routine also assumes that your title sheet is different from all the others and also has a different block name.

 

You say you prefer scripts, but LISP routines have done much to kept me sainer over the years by giving me the option to not have to do *everything* manually...especially the very mundane tasks...like updating revision numbers and dates...1000's of times.

Message 8 of 14

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.. the text I need to change is in a block in the title block. ....


Is it an ordinary Text [or perhaps Mtext] object in the Block, or an Attribute?

 

If it's just Text, and if that block is a .dwg file in a Support File Search Path location somewhere, it could be as simple as changing the date in that source drawing, and using something like Scriptpro [Search the Forums for examples/links] on the drawings, with a Script that just updates the Block definition to that of the drawing with the same name, with the BlockName= approach, i.e.:

 

-INSERT YourBlockName= Yes ^C

 

It could even be without that "Yes" answer, if you set the EXPERT System Variable accordingly [to 2 or higher].

Kent Cooper, AIA
Message 9 of 14

Anonymous
Not applicable

attached is one of  my title blocks, when the drawing was setup these were inserted in the layouts of the dwg and the drawing was broke up in views for the layouts, and one drawing my have from 1 to 20+ layouts...  you can double click in the bottom right of the title block and it will open an editor then you can change the info Section, Map Number ect..

about the only thing that changes in bulk is the tax year    again thanks for all the help  

0 Likes
Message 10 of 14

hmsilva
Mentor
Mentor

@Anonymous wrote:

attached is one of  my title blocks, when the drawing was setup these were inserted in the layouts of the dwg and the drawing was broke up in views for the layouts, and one drawing my have from 1 to 20+ layouts...  you can double click in the bottom right of the title block and it will open an editor then you can change the info Section, Map Number ect..

about the only thing that changes in bulk is the tax year    again thanks for all the help  


Hi tabmap,

your titleblock have attributes, most likely a Script '*.scr' file, might not be enough to change an attribute value in all layouts.

I would suggest the use of an  Auto/Visual Lisp routine to change the attribute value , and a '*,scr' file to open, run the lisp file, save close, open the next one....

And if you don't mind to lose de dwg preview until the next time you open and save the dwg, I would suggest ObjectDBX to open, change and save  all/every dwg...

 

Henrique

EESignature

Message 11 of 14

Kent1Cooper
Consultant
Consultant

So it's an Attribute in that Block, in which case my suggestion is not going to do it.  Does @zph's routine work for you, if you edit it to reflect your Block name, Attribute tag, etc.?

Kent Cooper, AIA
Message 12 of 14

doglips
Advocate
Advocate

Maybe something like this:

You could just give it a .scr extension

 

(setq TbBlockSearch (ssget "X" '((2 . "titleblock name"))))
 
 (setq BlockSearch (ssname TbBlockSearch 0))  ;; Names the entity 0 of the selection set

 (defun designsearch() ;;function to cycle through all attributes in block starting from the first
  (setq DesignEntity BlockSearch)
    (setq DesigntagSearch (entnext DesignEntity))
 );;defun

  
 (designsearch)

 (setq b (cdr(assoc 2 (entget DesigntagSearch))))
 (while (/= b "attribute tag");;while not the attribute tag you're looking for,
      (setq DesigntagSearch (entnext DesigntagSearch));; go to the next entity
      (setq b (cdr(assoc 2 (entget DesigntagSearch))));; and check the tag
 );;while  ;;found it
     
     (setq old (assoc 1 (entget DesigntagSearch))) ;;gets original assoc 1 value
     (setq new (cons 1 "new value"));;creates the new assoc value
     (setq newvalu (subst new old (entget DesigntagSearch))) ;;substitutes it
     (entmod newvalu) ;;modifies the entity
     (entupd (cdr(assoc -1 (entget DesigntagSearch)))) ;; updates the object
     

0 Likes
Message 13 of 14

zph
Collaborator
Collaborator

Tabmap, thank you for the example.  The code below works in the drawing you provided:

 

 

(defun c:DATEUP ( / NewDate tBlock nextPages ssTTLpageBLKdate i4 obj)

(if (= 1(getvar "TILEMODE"))  ;;switches to last active paper space layout
(setvar "TILEMODE" 0)
) ;if

(setvar "cmdecho" 0)

;*********************** Define variables **********************;
(setq tBlock "greene_leg")					; title block name
(setq NewDate "newDate")					; you can hard code the new date here if you'd prefer
;*************************************************​**************;

;;;commented txt file code for hard coded date ^^^ up there
;(setq ReadNewDate (open "MAPtoYOURtxtFILEwithTHEnameRevDate.txt/RevDate.tx​t" "r"))
;(setq NewDate (read-line ReadNewRevDate))			; Date format is:  "DDMMMYYYY"
;(close ReadNewRevDate)	

(if
	(setq ssTTLpageBLKdate 
		(ssget "_X" 
			(list '(0 . "INSERT")
			  (cons 2 tBlock)
			  '(66 . 1))				
	)) ; ssTTLpageBLKdate

	(repeat (setq i4 (sslength ssTTLpageBLKdate))
		(setq obj (vlax-ename->vla-object (ssname ssTTLpageBLKdate (setq i4 (1- i4))))
		attlst (vlax-invoke obj 'GetAttributes)
	)
	
	(foreach att attlst
		(if (= (vla-get-TagString att) "DATE_FLOWN")
		(vla-put-TextString att NewDate)      		;;; if specific value is needed, replace NewDate with "new value"
		) ;if
	)) ;fe ;re
) ;if

(princ "\n\033\n *** Dates updated! ***")
(setvar "cmdecho" 1)
(princ)
) ;DATEUP

 

 Good luck!

~zph

Message 14 of 14

Anonymous
Not applicable
Thanks for all the help everybody has given...
0 Likes