Remove unused file references from drawing

Remove unused file references from drawing

Lott99
Enthusiast Enthusiast
7,070 Views
17 Replies
Message 1 of 18

Remove unused file references from drawing

Lott99
Enthusiast
Enthusiast

I'd like to remove all these "Not Found" file references with a command.

Screenshot 2021-02-18 223612.png

0 Likes
Accepted solutions (3)
7,071 Views
17 Replies
Replies (17)
Message 2 of 18

hak_vz
Advisor
Advisor
Accepted solution

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

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 3 of 18

Lott99
Enthusiast
Enthusiast

Thank you very much! Your time is appreciated. I am glad I found this forum and hopefully I can help out other people eventually.

0 Likes
Message 4 of 18

Sea-Haven
Mentor
Mentor

Only need 1 if

(if (> i 0) or use (or (= i 1)(> i 1))

0 Likes
Message 5 of 18

Lott99
Enthusiast
Enthusiast

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

0 Likes
Message 6 of 18

Lott99
Enthusiast
Enthusiast

@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"

0 Likes
Message 7 of 18

pbejse
Mentor
Mentor

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

Message 8 of 18

pbejse
Mentor
Mentor
Accepted solution

Here, post # 10 or 16

 

Message 9 of 18

hak_vz
Advisor
Advisor

@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

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 10 of 18

hak_vz
Advisor
Advisor
Accepted solution

@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

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 11 of 18

pbejse
Mentor
Mentor

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

 

Message 12 of 18

Lott99
Enthusiast
Enthusiast

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

0 Likes
Message 13 of 18

Lott99
Enthusiast
Enthusiast

@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

0 Likes
Message 14 of 18

Lott99
Enthusiast
Enthusiast

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

0 Likes
Message 15 of 18

S.W.Eng
Contributor
Contributor

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.

0 Likes
Message 16 of 18

hak_vz
Advisor
Advisor

@S.W.Eng 

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

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 17 of 18

yuliya.kuzina
Contributor
Contributor

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! 

0 Likes
Message 18 of 18

pendean
Community Legend
Community Legend
@yuliya.kuzina Try this freeware from the Autodesk App store that can also be scripted and more https://apps.autodesk.com/ACD/en/Detail/Index?id=5474554872375064722&appLang=en&os=Win32_64
0 Likes