Xref rename issue

Xref rename issue

andreas7ZYXQ
Advocate Advocate
954 Views
6 Replies
Message 1 of 7

Xref rename issue

andreas7ZYXQ
Advocate
Advocate

I got some help to develop a code but i found out it doesnt do as i want some times.

It will change my name to 64CCBA.

But

It will change my path to..
.\64CCBA

It cuts out the .dwg..  Any ideas about this? So far the code saved alot of time since i run this on hundreds of files.

 



(Defun C:F64 ( / StringtoFind StringtoPut)
  (setq StringtoFind "61";(getstring "\nEnter string in Xref name: ")
	StringtoPut "64CCBA";(getstring "\nEnter new string here:  ")
	);setq
  
 (vlax-for MyXref (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)));<--go through all block objects
   (if (and
	 (vl-string-search (strcase StringtoFind) (strcase (vla-get-name MyXref)));<---if the name contains your string
	 (= (vla-get-isxref MyXref) :vlax-true);<---if it's an xref
	   )
     (progn
       (vla-put-name myxref (vl-string-subst StringtoPut;<---Change the name
			      (substr (vla-get-name MyXref)
				      (1+ (vl-string-search (strcase StringtoFind) (strcase (vla-get-name MyXref))))
				      (strlen StringtoPut));substr
			      (vla-get-name myxref)
			      );string subst
	 );vla-put-name
       (vla-put-path myxref (vl-string-subst StringtoPut;<---Change the Path
			      (substr (vla-get-path MyXref)
				      (1+ (vl-string-search (strcase StringtoFind) (strcase (vla-get-path MyXref))))
				      (strlen StringtoPut));substr
			      (vla-get-path myxref)
			      );string subst
	 );vla-put-path
       (vla-reload myxref)
       );progn
     );if
   );vlax-for
  (princ)
);defun
0 Likes
955 Views
6 Replies
Replies (6)
Message 2 of 7

john.uhden
Mentor
Mentor

The first thing I see is that there is no checking on whether the new path\\name exists.

The second thing is if the StringtoPut is longer than StringtoFind then you could be losing path delimiters or part of the name extension.

The third thing is if there are multiple occurrences of StringtoFind in the path, e.g. C:\\ABC\\ABC\\ABC\\.  Which level are you intending to replace?

John F. Uhden

0 Likes
Message 3 of 7

andreas7ZYXQ
Advocate
Advocate

i see.. 
I just want to replace the last one wich is the file name. 

 

Do you have any idea how to change the code or someone else out there who are good at this 😛 

0 Likes
Message 4 of 7

john.uhden
Mentor
Mentor

Is it just the name you want to change?  Meaning you want to change the Xref to a different file, or do you want to reference the same file but change its name?  Your code appears to try to be changing the path as well.  Is that not true?

John F. Uhden

0 Likes
Message 5 of 7

andreas7ZYXQ
Advocate
Advocate

My goal is to change part of a filename and also its path.

For example


E61-101-1.dwg
..\A\B\C\E61-101-1.dwg

to

E64CBBA-101-1.dwg
..\A\B\C\E64CBBA-101-1.dwg (64CBBA string can be 64 / 64A and so on. its not always the same number of characters in the string.)

with the code

(Defun C:F64 ( / StringtoFind StringtoPut)
  (setq StringtoFind "61";(getstring "\nEnter string in Xref name: ")
	StringtoPut "64CCBA";(getstring "\nEnter new string here:  ")
	);setq..........
......
.....
....
...



0 Likes
Message 6 of 7

john.uhden
Mentor
Mentor

If the string lengths are not equal, then you can't use vl-string-translate.

John F. Uhden

0 Likes
Message 7 of 7

andreas7ZYXQ
Advocate
Advocate

Actually i tweaked it a bit. Seems to work much better, didnt get any problems as before. 

(Defun C:F64 ( / StringtoFind StringtoPut)
  (setq StringtoFind "61";(getstring "\nEnter string in Xref name: ")
	StringtoPut "64CCBA";(getstring "\nEnter new string here:  ")
	);setq
  
 (vlax-for MyXref (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)));<--go through all block objects
   (if (and
	 (vl-string-search (strcase StringtoFind) (strcase (vla-get-name MyXref)));<---if the name contains your string
	 (= (vla-get-isxref MyXref) :vlax-true);<---if it's an xref
	   )
     (progn
       (vla-put-name myxref (vl-string-subst StringtoPut;<---Change the name
			      (substr (vla-get-name MyXref)
				      (1+ (vl-string-search (strcase StringtoFind) (strcase (vla-get-name MyXref))))
				      (strlen StringtoFind));substr
			      (vla-get-name myxref)
			      );string subst
	 );vla-put-name
       (vla-put-path myxref (vl-string-subst StringtoPut;<---Change the Path
			      (substr (vla-get-path MyXref)
				      (1+ (vl-string-search (strcase StringtoFind) (strcase (vla-get-path MyXref))))
				      (strlen StringtoFind));substr
			      (vla-get-path myxref)
			      );string subst
	 );vla-put-path
       (vla-reload myxref)
       );progn
     );if
   );vlax-for
  (princ)
;(command "qsave")
;(command "close")  
);defun

 

 

0 Likes