I found this a little while ago and noticed that there is no header – so I don’t know who wrote it. Whoever it was – thank you!!
Update: I found the original postings of this code:
This little routine quickly detaches all XREFS from a drawing. XREFS are not limited to other drawing files. Pictures and PDFs are also considered XREFS. This routine will detach all of these as well.
Here’s how:
- DETACHALL <enter>
- That’s it. the routine will remove all XREFs
;;; BY VVA found at cadtutor.net ;;; Also posted by Azarko at https://forums.augi.com/showthread.php?131322-LISP-to-detach-all-XREF-PDF-IMAGE-ALL-UNDERLAYS/page2 (defun C:Detachall (/ *error* mip:layer-status-restore mip:layer-status-save delete-xref-img-underlay delete-all-dict ) (vl-load-com) (defun *error* (msg) (mip:layer-status-restore) (princ msg) (princ) ) ;_ end of defun (defun mip:layer-status-restore () (foreach item *PD_LAYER_LST* (if (not (vlax-erased-p (car item))) (vl-catch-all-apply '(lambda () (vla-put-lock (car item) (cdr (assoc &quot;lock&quot; (cdr item)))) (vla-put-freeze (car item) (cdr (assoc &quot;freeze&quot; (cdr item))) ) ;_ end of vla-put-freeze ) ;_ end of lambda ) ;_ end of vl-catch-all-apply ) ;_ end of if ) ;_ end of foreach (setq *PD_LAYER_LST* nil) ) ;_ end of defun (defun mip:layer-status-save () (setq *PD_LAYER_LST* nil) (vlax-for item (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)) ) ;_ end of vla-get-layers (setq *PD_LAYER_LST* (cons (list item (cons &quot;freeze&quot; (vla-get-freeze item)) (cons &quot;lock&quot; (vla-get-lock item)) ) ;_ end of cons *PD_LAYER_LST* ) ;_ end of cons ) ;_ end of setq (vla-put-lock item :vlax-false) (if (= (vla-get-freeze item) :vlax-true) (vl-catch-all-apply '(lambda () (vla-put-freeze item :vlax-false)) ) ;_ end of vl-catch-all-apply ) ;_ end of if ) ;_ end of vlax-for ) ;_ end of defun (defun delete-xref-img-underlay (/ count txt) (mip:layer-status-save) (vlax-for Blk (vla-get-Blocks (vla-get-activedocument (vlax-get-acad-object)) ) ;_ end of vla-get-Blocks (if (and (= (vla-get-IsXref Blk) :vlax-false) (not (wcmatch (vla-get-name Blk) &quot;*|*&quot;)) ) ;_ end of and (progn (setq count 0 txt (strcat &quot; Erase Xref and Underlay in &quot; (vla-get-name Blk) ) ;_ end of strcat ) ;_ end of setq (grtext -1 txt) (vlax-for Obj Blk (setq count (1+ count)) (if (zerop (rem count 10)) (grtext -1 (strcat txt &quot; : &quot; (itoa count))) ) ;_ end of if (if (and (vlax-write-enabled-p Obj) (or (and ;_ XREF (= (vla-get-ObjectName obj) &quot;AcDbBlockReference&quot;) (vlax-property-available-p Obj &quot;Path&quot;) ) ;_ end of and (and ;_ UNDERLAY (wcmatch (vla-get-ObjectName obj) &quot;*Reference&quot;) (vlax-property-available-p Obj &quot;UnderlayName&quot;) ) ;_ end of and (= (vla-get-ObjectName obj) &quot;AcDbRasterImage&quot;) ;_ IMAGE ) ;_ end of or ) ;_ end of and (VL-CATCH-ALL-APPLY 'vla-Delete (list Obj)) ) ;_ end of if ) ;_ end of vlax-for ) ;_ end of progn ) ;_ end of if ) ;_ end of vlax-for (mip:layer-status-restore) ) ;_ end of defun (defun delete-all-dict (dict) ;;; dict - dict name (like &quot;ACAD_IMAGE_DICT&quot;, &quot;ACAD_PDFDEFINITIONS&quot; ... ) (vl-catch-all-apply '(lambda () (vlax-map-Collection (vla-item (vla-get-dictionaries (vla-get-activedocument (vlax-get-acad-object)) ) ;_ end of vla-get-dictionaries dict ;_ &quot;ACAD_IMAGE_DICT&quot; ) ;_ end of vla-Item 'vla-delete ) ;_ end of vlax-map-Collection ) ;_ end of lambda ) ;_ end of vl-catch-all-apply ) ;_ end of defun (vl-load-com) (delete-xref-img-underlay) (command &quot;_-xref&quot; &quot;_d&quot; &quot;*&quot;) (while (&gt; (getvar &quot;CMDACTIVE&quot;) 0) (command)) (mapcar 'delete-all-dict (list &quot;ACAD_IMAGE_DICT&quot; &quot;ACAD_PDFDEFINITIONS&quot; &quot;ACAD_DWFDEFINITIONS&quot; &quot;ACAD_DGNDEFINITIONS&quot; ) ;_ end of list ) ;_ end of mapcar (command &quot;_.regenall&quot;) (command &quot;_.externalreferences&quot;) (princ) ) ;_ end of defunOpen the External Reference Dialog/palette by entering XREF or XR at the command line.
This Picture shows the XREFs in the drawing. There is a JPEG (picture), PDF, & drawing file.
In the picture below:
To detach an xref manually – Select the XREF from the list > right click > select “Detach”Note: Selecting an xref from the list will highlight that XREF in the drawing area
In the Picture below:
This is how the XREF dialog/Palette looks after running the DETACHALL.lsp or by manually detaching the XREFs from the drawing.




Pingback: AutoLISP: XREFs: Remove All Pics | AutoCAD Tips
wokrs great thank alot for the work guys!