Not fully tested but it should remove all delete xrefs in a drawing.
(defun c:removeUnusedXRefs ( / *error* adoc xrefs ss unresolved i)
;hak_vz 19.02.2021
(defun *error* ( msg )
(if (not (member msg '("Function cancelled" "quit / exit abort" "Automation Error. Object was erased")))
(princ (strcat "\nError: " msg))
)
(if adoc (vla-endundomark adoc))
(princ)
)
(setq adoc (vla-get-activedocument (vlax-get-acad-object)))
(vlax-for blk (vla-get-blocks adoc)
(if (equal (vla-get-IsXref blk) :vlax-true)
(setq xrefs (cons (list (vla-get-Name blk) blk) xrefs))
)
)
(setq i 0)
(foreach e xrefs
(setq ss (ssget "X" (list(cons 0 "Insert")(cons 2 (car e)))))
(cond
((not ss)
(setq unresolved (cadr e) i (1+ i))
(if (and unresolved)(vlax-invoke-method unresolved 'Detach))
)
)
)
(if (= i 1)(princ (strcat "\n" (itoa i) " unreferenced xref removed!")))
(if (> i 1)(princ (strcat "\n" (itoa i) " unreferenced xrefs removed!")))
(princ)
)
(princ "\n Command removeUnusedXRefs detaches unreferenced external references!")
(princ)
Miljenko Hatlak
Thank you very much! Your time is appreciated. I am glad I found this forum and hopefully I can help out other people eventually.
@Sea-Haven I am not sure if I understand. I believe the two if statements are just to print either a singular or plural sentence detailing how many XREFs were removed.
@Lott99 wrote:
I'd like to remove all these "Not Found" file references with a command.
Are those names on the list drawing files [ xref dwg ]?
(defun c:XrefDetach ()
(vlax-for Blk (vla-get-Blocks
(vla-get-activedocument (vlax-get-acad-object))
)
(if (= (vla-get-IsXref Blk) :vlax-true)
(if
(vl-catch-all-error-p
(vl-catch-all-apply 'vla-get-XrefDatabase (list Blk))
)
(vla-Detach Blk)
)
)
)
(princ)
)
or something else?
EDIT: ohhhh i see, image files.
@Lott99I've tested it with my test samples and it worked ok. All xrefs that have been deleted in a drawing but were present in xref manager (status unreferenced) have been removed from a list. This were xrefs inserted with relative path. Since there are other options to insert xref there may be a problem.
Miljenko Hatlak
@Lott99 wrote:@hak_vz I know you said it wasn't fully tested, but did you get it to work? I have not been able to. I am testing it on three image files that are either "Not Found" or "Unreferenced"
In you original file you have asked for a code to detach unreferenced files in external references. My code works on inserted external files (dwg) that are presented in a file as a xref block and it wont work with images.
To detach images check this link
Miljenko Hatlak
@Lott99 wrote:
I am testing it on three image files that are either "Not Found" or "Unreferenced"
(defun c:detachImage (/ mspcoll dictcoll)
(vl-load-com)
(vlax-for obj (vla-get-modelspace
(vla-get-activedocument
(vlax-get-acad-object)))
(if
(and
(eq (vla-get-objectname obj)
"AcDbRasterImage")
(not (findfile (vla-get-ImageFile obj)))
)
(vl-cmdf "_.-image" "_detach" (vla-get-name obj))
)
)
(princ)
)
If you want all of them remove regardless, use the code from the link on my previous post.
@pbejse Thank you for linking this. This is exactly what I need. I will try to combine them so I can remove all types of file references. I was going to apologize for missing that post but then I realized that it was brand new as well.
@hak_vz Sorry for not being more specific. Thank you for the code you did write; I will use it as well. I am trying to completely clean up a lot of drawings and I'll eventually need to remove dwg XREF attachments
This might actually be more what I need. I meant to mark this one as the solution. https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/purge-unused-raster-image-reference/...
HI everyone , is this something that would work on Autocad lt, I have a problem where a file was used as a templete and it had a bunch of xrefs , since that was the original the rest of the files made after all have Xrefs that are not needed.
I think it is causing files to take forever to load, another issue is that after the file finally loads and you multi task and go to another window, for example chrome or outlook, whewn you come back to autocad, it takes forever for it to load again. It seems that it trys to load everytime you come back to it.
No, this lisp will not work since there is no autolisp in Autocad LT. You can use command PURGE to remove all unused objects in particular drawing.
Miljenko Hatlak
Hi, I am inserting this into a script and it's not working. I am trying to detach all (images, pdfs, xrefs etc)
Would appreciate an advise. Thanks!