AutoLISP: Detach All XREFS

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 &amp;quot;lock&amp;quot; (cdr item))))

(vla-put-freeze

(car item)

(cdr (assoc &amp;quot;freeze&amp;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 &amp;quot;freeze&amp;quot; (vla-get-freeze item))

(cons &amp;quot;lock&amp;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) &amp;quot;*|*&amp;quot;))

) ;_ end of and

(progn

(setq count 0

txt (strcat &amp;quot; Erase Xref and Underlay in &amp;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 &amp;quot; : &amp;quot; (itoa count)))

) ;_ end of if

(if

(and (vlax-write-enabled-p Obj)

(or

(and ;_ XREF

(= (vla-get-ObjectName obj) &amp;quot;AcDbBlockReference&amp;quot;)

(vlax-property-available-p Obj &amp;quot;Path&amp;quot;)

) ;_ end of and

(and ;_ UNDERLAY

(wcmatch (vla-get-ObjectName obj) &amp;quot;*Reference&amp;quot;)

(vlax-property-available-p Obj &amp;quot;UnderlayName&amp;quot;)

) ;_ end of and

(= (vla-get-ObjectName obj) &amp;quot;AcDbRasterImage&amp;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 &amp;quot;ACAD_IMAGE_DICT&amp;quot;, &amp;quot;ACAD_PDFDEFINITIONS&amp;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 ;_ &amp;quot;ACAD_IMAGE_DICT&amp;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 &amp;quot;_-xref&amp;quot; &amp;quot;_d&amp;quot; &amp;quot;*&amp;quot;)

(while (&amp;gt; (getvar &amp;quot;CMDACTIVE&amp;quot;) 0) (command))

(mapcar 'delete-all-dict

(list &amp;quot;ACAD_IMAGE_DICT&amp;quot;

&amp;quot;ACAD_PDFDEFINITIONS&amp;quot;

&amp;quot;ACAD_DWFDEFINITIONS&amp;quot;

&amp;quot;ACAD_DGNDEFINITIONS&amp;quot;

) ;_ end of list

) ;_ end of mapcar

(command &amp;quot;_.regenall&amp;quot;)

(command &amp;quot;_.externalreferences&amp;quot;)

(princ)

) ;_ end of defun

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

~enjoy

Unknown's avatar

About AutoCAD Tips

This blog serves as a knowledge base for myself (and anyone else) so that I can reference tips & tricks that I have learned and also refer others to it as well. I hope that this blog helps you learn at least one tip to make your drafting/design experience better.
This entry was posted in AutoLISP, Modifying, XREFs. Bookmark the permalink.

2 Responses to AutoLISP: Detach All XREFS

  1. Pingback: AutoLISP: XREFs: Remove All Pics | AutoCAD Tips

  2. bla bla's avatar bla bla says:

    wokrs great thank alot for the work guys!

Leave a comment