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

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
(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 "lock" (cdr item))))

(vla-put-freeze

(car item)

(cdr (assoc "freeze" (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 "freeze" (vla-get-freeze item))

(cons "lock" (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) "*|*"))

) ;_ end of and

(progn

(setq count 0

txt (strcat " Erase Xref and Underlay in "

(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 " : " (itoa count)))

) ;_ end of if

(if

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

(or

(and ;_ XREF

(= (vla-get-ObjectName obj) "AcDbBlockReference")

(vlax-property-available-p Obj "Path")

) ;_ end of and

(and ;_ UNDERLAY

(wcmatch (vla-get-ObjectName obj) "*Reference")

(vlax-property-available-p Obj "UnderlayName")

) ;_ end of and

(= (vla-get-ObjectName obj) "AcDbRasterImage") ;_ 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 "ACAD_IMAGE_DICT", "ACAD_PDFDEFINITIONS" ... )

(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 ;_ "ACAD_IMAGE_DICT"

) ;_ 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 "_-xref" "_d" "*")

(while (> (getvar "CMDACTIVE") 0) (command))

(mapcar 'delete-all-dict

(list "ACAD_IMAGE_DICT"

"ACAD_PDFDEFINITIONS"

"ACAD_DWFDEFINITIONS"

"ACAD_DGNDEFINITIONS"

) ;_ end of list

) ;_ end of mapcar

(command "_.regenall")

(command "_.externalreferences")

(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

About these ads

About AutoCAD Tips

As weird as it sounds, AutoCAD is a hobby for me. But I am fortunate enough to have a career doing my hobby. I work for a large engineering firm supporting CAD users in various disciplines. My dream job would be to work for a company like Autodesk or Disney's Imagineering where inspiring others and using innovative approaches and software are adding value to projects and bringing design in all areas up to a new level. This blog serves as a repository or knowledge base for myself 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 experience better.
This entry was posted in AutoLISP, Modifying, XREFs. Bookmark the permalink.

One Response to AutoLISP: Detach All XREFS

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s