Jump to content

Recommended Posts

Posted

Hi Guys

 

I'm looking to run a lisp routine to rename all xrefs within a folder with a prefix of the main drawing filename and then re-path the xrefs. i have scripts to do the two steps independently but am struggling to merge the two into one process. Ideally i'll be able to run this over a number of files as a batch. Currently each drawing sits within its own folder with all of the xrefs residing in the same folder

 

Example folder looks something like this:

 

mainfilename.dwg

xref.dwg

zref2.dwg

 

Afte running the process it would then look like:

 

mainfilname.dwg

mainfilename_xref.dwg

mainfilename_xref2.dwg

 

I would appreciate any input you may have

 

Cheers! 

Posted (edited)

That was surprisingly doable.

I started with function repath, a generic "change from -> to",

but found it easiest to put most of the functionality in that function, since it already loops through all xrefs, so I renamed it repath2.

 

Command RXRS

 



(vl-load-com)

 

(defun rename_file ( old-filename new-filename / )
  ;; https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2016/ENU/AutoCAD-AutoLISP/files/GUID-188B957C-4CDF-41C7-99BE-8080FA587F74-htm.html
  (if (findfile old-filename)
    (vl-file-rename old-filename new-filename)
  )
)

;; https://www.cadtutor.net/forum/topic/39458-lisp-to-reload-xrefs-in-all-open-drawings/
(defun reloadall nil
   (vlax-for b (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
       (if (eq :vlax-true (vla-get-isxref b)) (vla-reload b))
   )
   (princ)
)

;; change the name and path properties of the xref
;; pattern: change all XREFS to (strcat main_dwg_name "_" xref_name)
(defun repath2 (  / frm to path main_dwg objDefs)

  (setq main_dwg (vl-filename-base (getvar "dwgname")))  ;; main dwg name, not including path or extension
  (princ "\n")
  (princ main_dwg)
  (setq objDefs (vla-Get-Blocks (vla-Get-ActiveDocument (vlax-Get-Acad-Object))))
  (vlax-for objDef objDefs
    (if (= (vla-Get-IsXref objDef) :vlax-True)
      (progn
        (setq path (strcat (vl-Filename-Directory (vla-Get-Path objDef))))
        (setq frm (vla-Get-Name objDef) )
        (setq to (strcat main_dwg "_" frm ) )
        
        (rename_file
          (strcat path "\\" frm ".dwg")
          (strcat path "\\" to  ".dwg")
        )
        (princ (strcat "\nXREF file " frm " renamed to " to "\n" ) )
        
        (vla-Put-Path objDef (strcat path "\\" to ".dwg"))
        (vla-Put-Name objDef to)
        (princ (strcat "\nXREF properties " frm " changed to " to "\n" ) )

      )
    )
  )
)

;; for Repath XRefS
(defun c:rxrs ( /  )
  (repath2)
    ;;  reload xrefs
  (reloadall)
  (princ)
)

 

 

 

FYI, function repath + test function, not including all functions that you can find in the previous code block


;; change the name and path properties of the xref
(defun repath ( frm to change_file / )
  (setq objDefs (vla-Get-Blocks (vla-Get-ActiveDocument (vlax-Get-Acad-Object))))
  (vlax-for objDef objDefs
    (if (= (vla-Get-IsXref objDef) :vlax-True)
      (progn
        (setq path (vl-Filename-Directory (vla-Get-Path objDef)))
        (if (= frm (vla-Get-Name objDef)) (progn
        
          (if change_file (progn
            (rename_file
              (strcat path "\\" frm ".dwg")
              (strcat path "\\" to ".dwg")
            )
            (princ (strcat "\nXREF file " frm " renamed to " to "\n" ) )
          ))
          
          (vla-Put-Path objDef (strcat path "\\" to ".dwg"))
          (vla-Put-Name objDef to)
          (princ (strcat "\nXREF properties " frm " changed to " to "\n" ) )
        ))
      )
    )
  )
)

;; test
(defun c:rxr ( / frm to )

  (setq frm "xref")
  (setq to "mainfilename_xref")
 
  ;; change the filename of the xref
  ;; change the (name and path) properties of the xref object
  (repath frm to T)
 
  ;;  reload xrefs
  (reloadall)
  (princ)
)

 

Edited by Emmanuel Delay
  • Like 1
Posted

Fantastic!!

 

Thank you so much Emmanuel. it works perfectly. Exactly what i needed

  • Like 1
  • 1 year later...
Posted

I have a similar issue. I just need to rename the prefix of the xref name to a new prefix.

I already change the filename prefix, the path stays the same.

 

But since the filename prefix was change, i need to reload them all again one by one.

using the Reference Manager doesn't help coz the filename was changed.

 

can the above code be modified for this purpose?

 

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...