Jump to content

Replacing an Xref with a new Xref Using LISP Routine


ADSK2007

Recommended Posts

Hello,

 

I am in search of a lisp routine that can replace an Xref inside a drawing with a new Xref located in a different location. Here is the situation. Our company has been creating AutoCAD drawings for many years. As a new project comes in, we use to create a new folder and copy the border onto this folder and use it as an xref. This was the OLD way until now.

 

What I need to accomplish is, If a draft person opens an old drawing, I want him/her to be able to select the old Xref and replace it with a specific Xref. I already know the name of the Xref, I also know the path. These towo thing will never change.

 

I found this lisp routine which will do what I want and I would like to thank who ever wrote it. The only thing I like to change is - Instead of asking the user for a new name, Automatically enter the name "XREF_1117.dwg"

and Automatically enter the location of the Xref "//srvfile/CAD/AutoCAD 2010/Xref".

 

I have tried many times to do this but my knowledge of lisp is not allowing me to accomplish this. Could someone please take a look at this code and let me know what I could do to make it work? Thank you in advance for your time.

 

=======================================================

 

(defun crch (/ OLDERR B XRLIST XRCHANGE XRPATH XRNAME )

 

(setq olderr *error*)

 

(defun *error* (msg)

(if (/= msg "Function cancelled")

(princ (strcat "Error: " msg)))

(setvar "cmdecho" 1)

(graphscr)

(setq *error* olderr)

(princ)

); end error

 

 

(setvar "cmdecho" 0)

(setq B (tblnext "block" 1))

(while B

(if (assoc 1 B) ;find all xrefs

(setq XRLIST (cons (cdr (assoc 2 B)) XRLIST))) ;in drawing

(setq B (tblnext "block")))

(while (not XRCHANGE)

(textscr)

(repeat 10

(terpri))

(princ "**External References in Drawing**\n") ;print out a list

(princ "----------------------------------") ;of xrefs for the

(mapcar 'print XRLIST) ;the user to see

(repeat 5

(terpri))

(setq XRCHANGE (getstring "\nEnter old external reference name: "))

(if (tblsearch "block" XRCHANGE)

(progn

(while (not XRPATH)

(setq XRNAME (getstring "\nEnter new external reference name: ")) ;our folder structure is setup

(setq XRPATH (strcat (substr (getvar "dwgprefix") 1 1 XRNAME ".dwg")) ;that the xrefs reside one folder

(if (findfile XRPATH) ;level up from drawing

(progn

(command ".-xref" "p" XRCHANGE XRPATH) ;sets the new path

(command ".-rename" "b" XRCHANGE XRNAME) ;sets the new name

);progn

(progn

(princ (strcat "\nCould not find " (strcase XRNAME) " in search path.")) ;not found in xref folder

(setq XRNAME nil)

(setq XRPATH nil)

);progn

);if

);while

);progn

(progn

(princ (strcat "\nCould not find " (strcase XRCHANGE) " in drawing.")) ;not an xref in drawing

(setq XRCHANGE nil)

);progn

);if

);while

(graphscr)

(setvar "cmdecho" 1)

(setq *error* olderr)

(princ)

);eof

Link to comment
Share on other sites

Sorry about that. Here is the LISP with Tags

 

 
(defun c:xrch (/ OLDERR B XRLIST XRCHANGE XRPATH XRNAME )
(setq olderr *error*)
 (defun *error* (msg)
   (if (/= msg "Function cancelled") 
     (princ (strcat "Error: " msg)))
   (setvar "cmdecho" 1)
   (graphscr)
   (setq *error* olderr)
   (princ)
   ); end error

 (setvar "cmdecho" 0)
 (setq B (tblnext "block" 1))
 (while B
   (if (assoc 1 B)     ;find all xrefs
     (setq XRLIST (cons (cdr (assoc 2 B)) XRLIST))) ;in drawing
   (setq B (tblnext "block")))
 (while (not XRCHANGE)     
   (textscr)
   (repeat 10
     (terpri))
   (princ "**External References in Drawing**\n") ;print out a list
   (princ "----------------------------------") ;of xrefs for the
   (mapcar 'print XRLIST)    ;the user to see
   (repeat 5
     (terpri))
   (setq XRCHANGE (getstring "\nEnter old external reference name: "))
   (if (tblsearch "block" XRCHANGE)
     (progn
       (while (not XRPATH)          
         (setq XRNAME (getstring "\nEnter new external reference name: "))  ;our folder structure is setup
         (setq XRPATH (strcat (substr (getvar "dwgprefix") 1 18) XRNAME ".dwg"))  ;that the xrefs reside one folder 
         (if (findfile XRPATH)             ;level up from drawing
           (progn              
             (command ".-xref" "p" XRCHANGE XRPATH) ;sets the new path
             (command ".-rename" "b" XRCHANGE XRNAME) ;sets the new name
             );progn
           (progn
             (princ (strcat "\nCould not find " (strcase XRNAME) " in search path.")) ;not found in xref folder
             (setq XRNAME nil)
             (setq XRPATH nil)
             );progn
           );if
         );while
       );progn
       (progn
         (princ (strcat "\nCould not find " (strcase XRCHANGE) " in drawing.")) ;not an xref in drawing
         (setq XRCHANGE nil)
         );progn
       );if        
     );while
   (graphscr)
   (setvar "cmdecho" 1)
   (setq *error* olderr)
   (princ)
   );eof

Link to comment
Share on other sites

I finally got it resolved. Below is the correct routine. Just fill the areas in RED.

For the file name, you only need to type the file name without the extension.

 

 
(defun c:xrch (/ OLDERR B XRLIST XRCHANGE XRPATH XRNAME )
(setq olderr *error*)
 (defun *error* (msg)
   (if (/= msg "Function cancelled") 
     (princ (strcat "Error: " msg)))
   (setvar "cmdecho" 1)
   (graphscr)
   (setq *error* olderr)
   (princ)
   ); end error

 (setvar "cmdecho" 0)
 (setq B (tblnext "block" 1))
 (while B
   (if (assoc 1 B)     ;find all xrefs
     (setq XRLIST (cons (cdr (assoc 2 B)) XRLIST))) ;in drawing
   (setq B (tblnext "block")))
 (while (not XRCHANGE)     
   (textscr)
   (repeat 10
     (terpri))
   (princ "**External References in Drawing**\n") ;print out a list
   (princ "----------------------------------") ;of xrefs for the
   (mapcar 'print XRLIST)    ;the user to see
   (repeat 5
     (terpri))
   (setq XRCHANGE (getstring "\nEnter old external reference name: "))
   (if (tblsearch "block" XRCHANGE)
     (progn
       (while (not XRPATH)          
         (setq XRNAME "[color=red]TYPE XREF FILE NAME[/color]")          
         (setq XRPATH (strcat "[color=red]TYPE THE NETWORK PATH[/color]"xrname".dwg"))     
         (if (findfile XRPATH)                 
           (progn               
             (command ".-xref" "p" XRCHANGE XRPATH) ;sets the new path
             (command ".-rename" "b" XRCHANGE XRNAME) ;sets the new name
             );progn
           (progn
             (princ (strcat "\nCould not find " (strcase XRNAME) " in search path.")) ;not found in xref folder
             (setq XRNAME nil)
             (setq XRPATH nil)
             );progn
           );if
         );while
       );progn
       (progn
         (princ (strcat "\nCould not find " (strcase XRCHANGE) " in drawing.")) ;not an xref in drawing
         (setq XRCHANGE nil)
         );progn
       );if        
     );while
   (graphscr)
   (setvar "cmdecho" 1)
   (setq *error* olderr)
   (princ)
   );eof

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...