Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Xref Lisp Rename

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
andreas7ZYXQ
4169 Views, 8 Replies

Xref Lisp Rename

Im working on a lisp that will change my xref name and its directory. But i want to tweak it abit but i have no clue how to do it.

 

The problem i have is ive got multiple drawings. and the 100 numers change to 200 300 etc

I dont want to make a new command for every new combination of numbers so i just want to match The first 5 letters in this case E63.1 and change it to E64.1

final result will be E63.1-100 to E64.1-100.  Any ideas out there??

 

 

(defun c:mk64_01 ()
(command "-rename" "b" "E63.1-100*" "E64.1-100*")
(command "-xref" "p" "E64.1-100" "..\\Modell\\E64.1-100.dwg")
(command "-rename" "b" "F63" "F64")
(command "-xref" "p" "F64" "..\\Kompl\\F64.dwg")
)

 

8 REPLIES 8
Message 2 of 9
Shneuph
in reply to: andreas7ZYXQ

After reading very carefully a few times I think I understand what you need to do.

 

Give this a try:

 

(Defun C:Xref_Rename ( / StringtoFind StringtoPut)
  (setq StringtoFind (getstring "\nEnter String in Xref Name: ")
	StringtoPut (getstring "\nEnter New String:  ")
	);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

It will find and replace any string in your xref name and path. I made samples and did:

Command: XREF_RENAME

Enter String in Xref Name: 3.1

Enter New String: 4.1

 

and it seems to work well.

---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
Message 3 of 9
Sandervp
in reply to: andreas7ZYXQ

Hello Andreas7ZYXO,

 

I'm using a lisp that will clean up my drawings. It's also renaming the current .dwg name.

The new .dwg name becomes xref-example.dwg instead of example.dwg.

 

The location of this xref is the same as the original drawing. I do not know how to change this.

 

But maybe this will help you: 

 

 

(defun c:xfile (/ file)
    (setq file (strcat (getvar 'dwgprefix) "XREF-" (getvar 'dwgname)))
    (if (findfile file)
        (command "_.-wblock" file "Y" "*")
        (command "_.-wblock" file "*")
    )
)

 

Greetings Sander

Tags (1)
Message 4 of 9
andreas7ZYXQ
in reply to: Shneuph

Whoa, it works as i want. But i would like to have it run automatic. For example i want to search for E61 and change it to E63. 

 

What happends if its more than one matching file? it will rename both? 

 

Is it also possible to tweak it to search and replace 2 files at a time? 
For example
E61 to E63

F61 to F63

 

(Defun C:Xref_Rename ( / StringtoFind StringtoPut)
  (setq StringtoFind (getstring "\nEnter string in Xref name: ")
	StringtoPut (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

 

Message 5 of 9
Shneuph
in reply to: andreas7ZYXQ

If you want it to run and change the same thing every time.you can hard code your strings into it like this:

 

(Defun C:Xref_Rename ( / StringtoFind StringtoPut)
  (setq StringtoFind "E6.1";(getstring "\nEnter string in Xref name: ")<--this is commented out so you 
	StringtoPut "E6.3" ;(getstring "\nEnter new string here:  ")    --can use it later if you like.
	);setq

It will change every xref name and path in your drawing that it finds your your "stringtofind" variable.  If the new xref does not exist it will probably error out/crash.

 

"Is it also possible to tweak it to search and replace 2 files at a time? 
For example
E61 to E63

F61 to F63"

 

It should do this as is if you make stringtofind "61" and "stringtoput" 63.  Just be careful that it doesn't grab unwanted xrefs because the search string is too general.

 

Also, to clarify.  It's not renaming any files.  The new xref file must be in the same directory as the existing one.  It merely points the xref in your drawing to a different file. 

---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
Message 6 of 9
andreas7ZYXQ
in reply to: Shneuph

Thanks alot, it achives the things i want!  Great, and many thanks

Message 7 of 9
andreas7ZYXQ
in reply to: andreas7ZYXQ

I found some issues with this code.

For example if i search for string E61 and want to change it to E6123456789 it will change the name but not the path correctly.

The path i get as a result is (..\Modell\E6123456789wg)

 

Anyone have an idea about this?


I had an idea, is it possible to work with a "list" in the variable stringtoifind. for example if i want to match E12 E13 E14 E89 E56 and so on..

Message 8 of 9
cyberflow
in reply to: andreas7ZYXQ

This thread changed my life.

Thank you for the lisp !
Gona be usefull !

Frank Freitas

CAE/CAD/BIM Coordinator & Support Specialist

LinkedIn
Message 9 of 9
Anonymous
in reply to: andreas7ZYXQ

In search of the program I need...

 

Instead (strlen StringtoPut) must be (strlen StringtoFind)

To times.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report