I need a Lisp to copy a number and paste in another object

I need a Lisp to copy a number and paste in another object

mmprakash90
Participant Participant
1,789 Views
20 Replies
Message 1 of 21

I need a Lisp to copy a number and paste in another object

mmprakash90
Participant
Participant

Hi All,

 

I have a scenario where I need a Lisp program to copy and paste within same DWG

 

In a DWG I have a number for

EX :123456

And another object like ADD=XXXXXX 

Now , I want that 123456 in the place of XXXXXX like ADD= 123456 in just two three clicks

 

Need your help

 

 

 

 

0 Likes
Accepted solutions (2)
1,790 Views
20 Replies
Replies (20)
Message 2 of 21

Kent1Cooper
Consultant
Consultant

You don't really "have a number," but some kind of object whose text content is numerical, and another whose text content is partly numerical.  What are the objects?  Text?  Mtext?  Block Attributes?  Dimension text override?  Not always the same kind(s)?  Are they always both the same kind of object as each other?  Is the first one really always only numbers, or might it have other characters like the second one?  Is the format of the second one reliably the same [for example, with the numerical part always at the end, and no numbers before that]?  Etc., etc.

 

A small sample drawing could help, including as many scenarios as you would encounter.  

Kent Cooper, AIA
0 Likes
Message 3 of 21

Moshe-A
Mentor
Mentor

@mmprakash90  hi,

 

check this cpy&pst command

 

enjoy

Moshe

 

(defun c:cpy&pst (/ _digits ; local function
		    ss0 ss1 txt0 txt1 txt2 sor0 ename elist p)

 ; anonymous function
 (setq _str->digits
	(lambda (s)
	  (apply
	    'strcat
	    (vl-remove-if
	      'not
	      (mapcar
		(function
		  (lambda (n)
		    (if (and (>= n 48) (<= n 57)) (chr n))
		  ); lambda
		); function
		(vl-string->list s)
	      ); mapcar
	    ); vl-remove-if
	  ); apply
	); lambda
 ); setq

  
 (setvar "cmdecho" 0)
 (command "._undo" "_begin")
  
 (if (and
       (not (prompt "\nPick source text..."))
       (setq ss0 (ssget ":s:e+." '((0 . "text,mtext"))))
       (not (prompt "\nSelect target text(s)..."))
       (setq ss1 (ssget '((0 . "text,mtext"))))
    )
  (progn
   (setq txt0 (cdr (assoc '1 (entget (ssname ss0 0)))))
   (setq sor0 (_str->digits txt0))

   (foreach ename (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss1)))
    (setq elist (entget ename))
    (setq txt1 (cdr (assoc '1 elist)))

    (if (setq p (vl-string-position (ascii "=") txt1 0 t))
     (progn
      (setq txt2 (strcat (substr txt1 1 (1+ p)) sor0))
      (entmod (subst (cons '1 txt2) (assoc '1 elist) elist))
     ); progn
    ); if
   ); foreach

  ); progn
 ); if

 (command "._undo" "_end")
 (setvar "cmdecho" 1)
  
 (princ)
)

 

 

0 Likes
Message 4 of 21

mmprakash90
Participant
Participant

1000155633.jpg

 The above objects are constant,

One is numeric which needs to be copied and pasted in alphanumeric ADD=XXXXXX or at the place of only XXXXXX.When I click/select on number it should pick /copy that number and when I click on Other object it should paste that number result as shown in pic

0 Likes
Message 5 of 21

Kent1Cooper
Consultant
Consultant

The remaining crucial question:  object type(s)?

Kent Cooper, AIA
0 Likes
Message 6 of 21

Sea-Haven
Mentor
Mentor

I did no code

Dbl Click 123456, ctrl+a, ctrl+c

esc

Dbl click text, select xxxx, Ctrl+v

Alldone

0 Likes
Message 7 of 21

mmprakash90
Participant
Participant

They are not blocks ,just Texts alphanumeric Texts

0 Likes
Message 8 of 21

mmprakash90
Participant
Participant

We have to do it for 10k addresses daily 

0 Likes
Message 9 of 21

Moshe-A
Mentor
Mentor

@mmprakash90 ,

 

have read message #3?

 

 

0 Likes
Message 10 of 21

Kent1Cooper
Consultant
Consultant

@mmprakash90 wrote:

 The above objects are constant, ....


If "constant" means really constant, that is, the source Text is always six and only six numerical-only digits, and the target object always ends with six capital X's, whether or not there is anything more before them or how much is before them, and if they're both Text objects specifically, then this seems to work:

(defun C:TEST (/ source str1 target str2)
  (if
    (and
      (setq source (car (entsel "\nNumerical source Text object: ")))
      (wcmatch (setq str1 (getpropertyvalue source "TextString")) "*######")
      (not (redraw source 3)); highlight [(not) because it returns nil]
      (setq target (car (entsel "\nText object with XXXXXX to be replaced: ")))
      (wcmatch (setq str2 (getpropertyvalue target "TextString")) "*XXXXXX")
    ); and
    (progn ; then
      (redraw source 4); un-highlight
      (setpropertyvalue target "TextString" (vl-string-subst str1 "XXXXXX" str2))
    ); progn
    (prompt "\nInappropriate object selected.")
  ); if
  (prin1)
)

But that's in simplest terms.  It could be made more sophisticated, to overcome certain possibilities.  For example, it could be made to allow for Mtext [different property name for the contents].  If a target ends with more than six capital X's in a row, the first six of them will be the ones replaced, not the last six.  It currently allows selection of Text on locked Layers, but could prevent that at least for the target [it wouldn't matter for the source, unless you want that removed].  And probably some other things....

Kent Cooper, AIA
0 Likes
Message 11 of 21

komondormrex
Mentor
Mentor
Accepted solution

another one goes

 

(defun c:replace_XXXXXX ( / num_text num_string target_string target_dxf)
  (while (setq num_text (car (entsel "\nPick text with number to get from: ")))
	 (setq num_string (cdr (assoc 1 (entget num_text))))
	 (if (wcmatch (setq target_string (cdr (assoc 1 (setq target_dxf (entget (car (entsel (strcat "\nPick text to replace \"XXXXXX\" with \"" num_string "\": "))))))))
		       "*ADD=XXXXXX*"
	     )
	     (entmod (subst (cons 1 (vl-string-subst (strcat "ADD=" num_string) "ADD=XXXXXX" target_string)) (assoc 1 target_dxf) target_dxf))
	 )
	 (princ)
  )
)

 

Message 12 of 21

ec-cad
Collaborator
Collaborator

Try this Lisp. Function call is SREP. Posted into another post just today.

It is a search-replace type process, will change partial string values for Text, Mtext or Attribute values

within Blocks. Drag / drop the Lisp file into your Acad open drawing type SREP at Command Line,

then Type in 123456, and the replacement # or string e.g 456XXX and it will change them all.

If you have 'unique' numbers for those addresses, it probably won't be of use for that.

But is a handy tool for general use.

 

ECCAD

Message 13 of 21

mmprakash90
Participant
Participant

This is working fine, But I have below scenario like

 

Add=XXXXXX

PB=XXXXXX

TP=XXXXXX

And sometimes only

XXXXXX 

 

These X's only should be replaced with numbers like 123456 or what ever number and resulting below

 

Add = 123456

PB = 123456

TP = 123456

And sometimes only

123456

 

Thanks

PM

 

 

0 Likes
Message 14 of 21

komondormrex
Mentor
Mentor
Accepted solution

then maybe this one

(defun c:replace_XXXXXX ( / num_text num_string target_string target_dxf)
  (while (setq num_text (car (entsel "\nPick text with number to get from: ")))
	 (setq num_string (cdr (assoc 1 (entget num_text))))
	 (if (wcmatch (setq target_string (cdr (assoc 1 (setq target_dxf (entget (car (entsel (strcat "\nPick text to replace \"XXXXXX\" with \"" num_string "\": "))))))))
		       "*XXXXXX*"
	     )
	     (entmod (subst (cons 1 (vl-string-subst num_string "XXXXXX" target_string)) (assoc 1 target_dxf) target_dxf))
	 )
	 (princ)
  )
)
0 Likes
Message 15 of 21

Sea-Haven
Mentor
Mentor

If its just "XXXXXX" then why not use FIND.

SeaHaven_0-1732595861441.png

If needed could get the "123456" from text, copy to clipboard and paste into the Find command.

 

0 Likes
Message 16 of 21

mmprakash90
Participant
Participant

Yes ,it works better

 

Thanks 

0 Likes
Message 17 of 21

mmprakash90
Participant
Participant

Yes its working, But its only working for 6 digits what if the digits very, from 6 to 5 or I may have to replace 7 digits or 4 digits.How to do at that time 

0 Likes
Message 18 of 21

komondormrex
Mentor
Mentor

if you happened to have full autocad with et installed you may try the following adaptation of the code

(defun c:replace_X-X ( / num_text num_string start_x_pos end_x_pos target_string target_dxf)
	(while (setq num_text (car (entsel "\nPick text with number to get from: ")))
	 	   (setq num_string (cdr (assoc 1 (entget num_text))))
	 		(while (null (setq start_x_pos (acet-str-find "[xX]+" 
	 			 								  		   (setq target_string 
	 											  				  	(cdr (assoc 1 (setq target_dxf 
	 											  										(entget (car (entsel (strcat "\nPick text to replace \"X...X\" with \"" 
	 											  																	  num_string 
	 											  																	  "\": "
	 											  															 )
	 											  													 )
	 											  												)
	 											  										)
	 											  								  )
	 											  						 )
	 											  					)
	 											  		   ) t t
	 										)
	 							end_x_pos start_x_pos 
	 			 		)
					)
			  )
	 		  (entmod (subst (cons 1 (vl-string-subst 
	 				 					num_string 
	 				 					(substr target_string 
	 				 							start_x_pos
	 											(progn
	 												  (while (or (= "X" (substr target_string (setq end_x_pos (1+ end_x_pos)) 1)) 
	 															 (= "x" (substr target_string end_x_pos 1)) 
	 														 )
	 												  )
	 												  (- end_x_pos start_x_pos)
	 											)
	 									)
	 									target_string
	 				 				)
	 						) 
	 						(assoc 1 target_dxf) 
	 						target_dxf
	 				 )
	 		  )
	 )
	 (princ)
)
0 Likes
Message 19 of 21

mmprakash90
Participant
Participant

Is it possible for Multileader text /callot as shown in pic, it should paste by suffix ID=123456

1000163729.jpg

1000163728.jpg

 Pls help with this sir

0 Likes
Message 20 of 21

mmprakash90
Participant
Participant

@komondormrex Its working fine for normal text, if we have Multileader texts or call outs its not working .Can you please help with similar code for multileader texts. I need to replace XXXXXX in below text and result the ID=123456(or some other number)

1000171590.jpg

0 Likes