Jump to content

Detaching a XREF and deleting corresponding file


Ament

Recommended Posts

Hello together,

 

The following sceanrio I'd like to achive:

Pre: I have a rawing with several XREFs attached, selected one of them on screen and run the code.

  1. It should remind the path to the attached file
  2. detach the file
  3. and delete the file in the path stored before

 

I thought I was done with the code I'll attach below, but the issue seems to be that even if I detach the XREF it still seems to be in the memory so that the file can't be deleted before closing the original drawing. So my question to you is if there is a way to "close" the referenced drawing competly during the macro to be able to delete it from the hard drive.

(defun C:DELXREF ( / FILENAME FILEPATH XREFVLNAME)
	(setq FILENAME (cdr (assoc 2 (entget (ssname (ssget "_I") 0)))))
	(setq FILEPATH (cdr (assoc 1 (tblsearch "Block" FILENAME))))
	(while (wcmatch FILEPATH "*\\*") ;Replacing backslash with regular slash as the vl-delete-function is expecting it that way (reason?)
		(setq FILEPATH (vl-string-subst "//" "\\" FILEPATH))
	);while
	(if (and (not (vl-catch-all-error-p (setq XREFVLNAME (vl-catch-all-apply 'vla-item (list (vla-get-blocks 
				(vla-get-activedocument (vlax-get-acad-object))) FILENAME))))) 
		(= :vlax-true (vla-get-isxref XREFVLNAME)))
   (vla-detach XREFVLNAME)
   (vl-file-delete FILEPATH)
   );if
(princ)
);defun

 

Link to comment
Share on other sites

Feel free to change the (princ) messages if you want less melo drama :)

 



;; based a bit on https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/a-lisp-to-make-all-entities-color-quot-by-layer-quot/td-p/4840793
(defun remove_xref (xrefname path deleteFile / d b )
    (vlax-for b (vla-get-blocks (setq d (vla-get-activedocument (vlax-get-acad-object))))
        (if (= (vla-get-name b) xrefname)
            (progn
            
              (vla-detach b)
              (princ "\nDetactched XREF: ")
              (princ xrefname)
              (princ "\n")
              
              (if (and deleteFile path) (progn
                (vl-file-delete path)
                
                (princ "\nFile removed from the hard disc: \n")
                (princ path)
                (princ "\n")
                
              ))
            )
        )
    )
    (vla-regen d acallviewports)
    (princ)
)

;;;;;;;;;;;;;;;;;;;;;;;

(vl-load-com)

;; RXF for Remove Xref File
(defun c:rxf ( / ent obj xrefname xrefpath conf)
 
  (setq ent (car (entsel "\nSelect XREF to detach and delete the file: ")))
  (setq xrefname (cdr (assoc 2 (entget ent))))
  (setq obj (vlax-ename->vla-object ent))
 
  (if (setq xrefpath (vla-get-path obj)) (progn
    
    (princ "\nXREF selected: ")
    (princ xrefname)
    (princ "\nXREF path: \n")
    (princ xrefpath)
    
    (setq conf (getstring "\nType Y if you are absolutely sure you want to detach the XREF and delete the file from your hard disc for ever: "))
    (if (or (= "Y" conf) (= conf "y") ) (progn
      (remove_xref xrefname xrefpath T)
    ))
  ))
 
  (princ)
)

 

Link to comment
Share on other sites

Hello Emmanuel,

when reading this piece of code I can't find any difference in comands used and their sequence. So I doubt it does anything different than I did expect it is a bit more cumbersome to read.

Best regards,

Ament

Link to comment
Share on other sites

Maybe look at this part:

(vl-string-subst "//" "\\" FILEPATH)

You are inserting DOUBLE forward slashes here. The resulting path will therefore be incorrect.

Link to comment
Share on other sites

For me moving the file to a junk directory would be safer, allow overwrite, clean up every now and then. I can just see a castrophe using a delete file picked wrong one. Picking rubbish bun on email accidently when about to read at least its in the delete pile.

Link to comment
Share on other sites

Thank you for your responses.

@Roy_043 I just doublechecked it with a file test.txt on my desktop and the following command is working fine.

(vl-file-delete "C://Users//XXX//Desktop//test.txt")

Also for the substitute it looks fine.

Before:

"C:\\Users\\XXX\\Desktop\\DRWNAME.dwg"

After:

"C://Users//XXX//Desktop//DRWNAME.dwg"

So I actually think I'm correct in here. Or am I missing the point? Maybe I don't see the forrest for the trees?

 

@BIGAL You're right. I might change it that way. But the issue seems to be the same. As the file I'd like to move to the dumb directory is still in runtime use I'll not be able to move it. :(

 

Best regards,

Ament

Edited by Ament
Typo
Link to comment
Share on other sites

7 minutes ago, Ament said:

 


"C://Users//XXX//Desktop//DRWNAME.dwg"

So I actually think I'm correct in here. Or am I missing the point? Maybe I don't see the forrest for the trees?

 

 

 

Command: (findfile "c://temp//plot.log")
nil

Command: (findfile "c:/temp/plot.log")
"C:\\temp\\plot.log"

so yes , you're missing the point 😅

Link to comment
Share on other sites

Actually "\\" is a single character. The first backslash functions as an escape character.

Edited by Roy_043
Link to comment
Share on other sites

Well, two things. It didn't matter if I use one or two slashes in the path.

Command: (vl-file-delete "C://temp//test1.txt")
T
Command: (vl-file-delete "C:/temp/test2.txt")
T

Both of them are working well.

 

In the meantime I found out that the variable XLOADCTL was set to 1 in my environment which caused the issue that my xref file was in runtime use and couldn't be shifted/deleted.

So I changed it to 2 which results in opening a temporary copy, not the original. Now the deletion of the original file is possible by manually triggering the (vl-file-delete VAR) command but the macro is still struggeling :(

Link to comment
Share on other sites

:facepalm:

Sorry. After reviewing it I found the issue. Beginners issue:

In my if loop I didn't make sure it runs through both commands using (progn). And because I had just two commands in there it interpreted the delete function as "ELSE" condition.

:facepalm::facepalm::facepalm:

I just quickly doublechecked if this was the only issue. But infact even if I was fine from the beginning with the (progn) still the XLOADCTL would have blocked the execution.

Now it is working well.

 

Anyhow I'll integrate your suggestion @BIGAL. You're correct. Not all is gone if you accidently selected the wrong xref. As I know my colleagues working with it I'm sure it will become quiet handy to have the backup ;)

 

Thanks all of you,

Ament

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...