Attach XREF for Multiple CAD drawings in a single step

In this post I am going to show you how to attach/ import XREF for multiple AutoCAD drawings in a single click by using LISP.



 If you have many DWG's  in that you need to add the same xreference, then you are in the right place to know the trick to make it simple.

Follow the below steps to attach multiple

1. Add the below code to the lisp or download it from here LISP


2. Type command "mxref"


3. Then select the X ref file in the pop up window as shown below

4. Now select the target folder (which have the drawings) to attach. Then click ok. That's it.

LISP code as below

Created by Imran Lajos ,Latest as on 05/12/2022
(print "=====LISP to Attach Xref to all drawings in Folder loaded sucessfully>>> Use Command 'mxref' ")
(defun c:mxref (/ _getfolder app adoc odbs odbx v xref folder dwg xr)
  (vl-load-com)
  (defun _getfolder ( m / sh f r )
    (setq sh (vla-getinterfaceobject (vlax-get-acad-object) "Shell.Application") f (vlax-invoke-method sh 'browseforfolder 0 m 0))
    (vlax-release-object sh)(if f (progn (setq r (vlax-get-property (vlax-get-property f 'self) 'path))(if (wcmatch r "*\\") r (strcat r "\\")))))
  ; i is 0 (absolute), 1 (relative) of 2 (no) -xref path
  (defun RLXref_SetPathType (i)
    (vl-registry-write (strcat "HKEY_CURRENT_USER\\" (vlax-product-key) "\\Profiles\\" (getvar "cprofile") "\\Dialogs\\XattachDialog") "PathType" i))
  (setq odbs "ObjectDBX.AxDbDocument" v (substr (getvar 'acadver) 1 2) adoc (vla-get-activedocument (setq app (vlax-get-acad-object))))
  (RLXref_SetPathType 1)
  (cond
    ((vl-catch-all-error-p (setq odbx (vl-catch-all-apply 'vla-getinterfaceobject (list app (if (< (atoi v) 16) odbs (strcat odbs "." v))))))
     (princ "\nObject DBX interface not created!"))
    ((not (setq xref (getfiled "Select Xref to attach" "" "dwg" 0))) (alert "No Xref was selected"))
    ((setq folder (_getfolder "Select folder with drawings to attach xref to"))
     (foreach dwg (vl-directory-files folder "*.dwg" 0)
       (setq dwg (strcat folder dwg))
       (if (vl-catch-all-error-p (vl-catch-all-apply 'vla-open (list odbx dwg)))
  (princ (strcase (strcat "\nError opening: " dwg)))
  (progn
    (princ (strcat "\nOpening: " dwg))
    ; attach xref
    (if (vl-catch-all-error-p (setq xr (vl-catch-all-apply 'vla-AttachExternalReference
        (list (vla-get-ModelSpace odbx) xref (vl-filename-base xref) (vlax-3d-point 0 0 0) 1 1 1 0 :vlax-false))))
      (princ (vl-catch-all-error-message xr)))
    ; save drawing
    (vla-saveas odbx (vla-get-name odbx))
  )
       )
     )
    )
  )
  (princ)
(print "<<< XREF File Attached to all drawings sucessfully >>> ")
)

Post a Comment

Facebook