Author Topic: close a drawing that is not current  (Read 4224 times)

0 Members and 1 Guest are viewing this topic.

paul_s

  • Guest
close a drawing that is not current
« on: January 07, 2009, 11:07:07 AM »
Hello,

I've search this site but could'nt find a lisp routine to close an open drawing.
Close with save, and without saving. I know which drawing name to close.
Maybe something like...

(CloseAndSaveDwg "c:\\temp\\test.dwg")
(CloseDontSaveDwg "c:\\temp\\test.dwg")

Thank you in advance.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: close a drawing that is not current
« Reply #1 on: January 07, 2009, 11:44:22 AM »
Once you get the document object, then use the Close method of said document with correct argument for the Save argument of the method.  Since you know the name, then you can get the document object from the Document Collection.  If it is found, then close it.

Its early for me, so something along the lines of ( code written here, and no testing ):

Code: [Select]
(defun CloseAndSave ( dwgName bSave )

    (vlax-for i (vla-get-Documents (vlax-get-Acad-Object))
        (if (= (strcase dwgName) (strcase (vla-get-Name i)))
            (if
                (and
                    bSave
                    (equal (vla-get-ReadOnly i) :vlax-false)
                )
                (vla-Close i :vlax-true)
                (vla-Close i :vlax-false)
            )
        )
    )
)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: close a drawing that is not current
« Reply #2 on: January 07, 2009, 12:03:33 PM »
Try this
Code: [Select]
;;;   Copyright ©2009 by K.E. Blackie
;;;   http://www.resourcecad.com
;;;   kblackie@resourcecad.com
;;;
;;;   AutoCAD 2000+  VisualLISP
;;;
;;;   USAGE
;;;----------------------------------------------------------------------------
;;;
;;;   (CloseDontSaveDwg "filename with path")
;;;   (CloseAndSaveDwg "filename with path")
;;;   (CloseDwg "filename with path" :vlax-true || :vlax-false)
;;;
;;;----------------------------------------------------------------------------

(vl-load-com)

;;; main function
(defun CloseDwg (filename save)
  (setq acad (vlax-get-acad-object)
docs (vla-get-documents acad)
  )
  (vlax-for doc docs
    (if (= filename (vlax-get-property doc 'fullname))
      (vla-close doc save)
    )
  )
)

;;; close drawing don't save
(defun CloseDontSaveDwg (filename)
  (CloseDwg filename :vlax-false)
)

;;; close drawing and save
(defun CloseAndSaveDwg (filename)
  (CloseDwg filename :vlax-true)
)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

paul_s

  • Guest
Re: close a drawing that is not current
« Reply #3 on: January 07, 2009, 12:23:58 PM »
Hi Tim & Keith,

Thank you guys. I love this site!

paul_s

  • Guest
Re: close a drawing that is not current
« Reply #4 on: January 07, 2009, 12:41:41 PM »

One more thing if possible. I sometimes need to open drawings (in a read-only mode) that was already
opened by another person in the network to check or grabbed things (wblock).

Can the code be updated to close (of course without saving) a read-only that I opened but is not my current?

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: close a drawing that is not current
« Reply #5 on: January 07, 2009, 12:45:51 PM »
I believe Tim's code does that
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

T.Willey

  • Needs a day job
  • Posts: 5251
Re: close a drawing that is not current
« Reply #6 on: January 07, 2009, 12:47:42 PM »
Both codes have that ability already.  You can use the ' CloseDontSaveDwg ' routine within Keith's post.  Mine will do it without anything else needed.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

paul_s

  • Guest
Re: close a drawing that is not current
« Reply #7 on: January 07, 2009, 01:10:28 PM »
Tim,

I tried Keith's "CloseDontSaveDwg" for a ready-only drawing but it did not work. It worked fine
with non read-only files.

In your code what would be the second argument be (bSave)?
...(CloseAndSave "c:\\temp\\test.dwg" ? )

T.Willey

  • Needs a day job
  • Posts: 5251
Re: close a drawing that is not current
« Reply #8 on: January 07, 2009, 01:52:08 PM »
In mine it will be either ' T ' ( or any no nil value ) for save or ' nil ' for no save.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

paul_s

  • Guest
Re: close a drawing that is not current
« Reply #9 on: January 07, 2009, 02:41:46 PM »

Hmmm. It did not work. Even though the first guy closed out the drawing.
Anyway, thank you again guys.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: close a drawing that is not current
« Reply #10 on: January 07, 2009, 03:17:11 PM »
If you have it open as read only in your session, then mine will close it without saving no matter what argument you pass it.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

cadmoogle

  • Guest
Re: close a drawing that is not current
« Reply #11 on: January 07, 2009, 03:37:36 PM »
Don't laugh at my code mangling skills  8-)

Code: [Select]
;;Close all drawing without saving
;;Original code by someone on Autodesk forums
;;Modified by cadmoogle to 'Close last drawing'
(defun doc:CloseAllButActive (TrueOrFalse / cnt)
(setq cnt 0)
(vlax-for Item (vla-get-Documents (vlax-get-acad-object))
(if (= (vla-get-Active Item) :vlax-False)
(progn
(vla-close Item TrueOrFalse)
(setq cnt (1+ cnt))
)
)
)
cnt
); end defun

(defun close (/) ;added by cadmoogle to close the last drawing without saving
(command "close" "Y")
(princ)
); end defun

(defun c:cad ( / cnt) ;Closes all drawings except active
(setq cnt (doc:CloseAllButActive :vlax-False))
(if (> cnt 0)
(princ (strcat "\n(" (itoa cnt) ") document" (if (> cnt 1) "s" "") "
closed without saving."))
(princ "\nNo other documents to close.")
)
(close)
(princ)
); end defun



Code: [Select]
(defun doc:CloseAllButActive (TrueOrFalse / cnt)
(setq cnt 0)
(vlax-for Item (vla-get-Documents (vlax-get-acad-object))
(if (= (vla-get-Active Item) :vlax-False)
(progn
(vla-close Item TrueOrFalse)
(setq cnt (1+ cnt))
)
)
)
cnt
)

(defun c:cns ( / cnt) ;CLOSES WITH NO SAVE ALL DOCUMENTS EXCEPT THE ACTIVE
(setq cnt (doc:CloseAllButActive :vlax-False))
(if (> cnt 0)
(princ (strcat "\n(" (itoa cnt) ") document" (if (> cnt 1) "s" "") "
closed without saving."))
(princ "\nNo documents to close.")
)
(princ)
)




kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2152
  • class keyThumper<T>:ILazy<T>
Re: close a drawing that is not current
« Reply #12 on: January 07, 2009, 04:54:24 PM »
It would be really nice if you would format the code before posting it.
some people won't even bother to try to read what you've posted.

... but please don't stop posting :)
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

taner

  • Guest
Re: close a drawing that is not current
« Reply #13 on: February 06, 2009, 07:38:30 PM »
Once you get the document object, then use the Close method of said document with correct argument for the Save argument of the method.  Since you know the name, then you can get the document object from the Document Collection.  If it is found, then close it.

Its early for me, so something along the lines of ( code written here, and no testing ):

Code: [Select]
(defun CloseAndSave ( dwgName bSave )

    (vlax-for i (vla-get-Documents (vlax-get-Acad-Object))
        (if (= (strcase dwgName) (strcase (vla-get-Name i)))
            (if
                (and
                    bSave
                    (equal (vla-get-ReadOnly i) :vlax-false)
                )
                (vla-Close i :vlax-true)
                (vla-Close i :vlax-false)
            )
        )
    )
)

The above code can not colse the active drawing.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: close a drawing that is not current
« Reply #14 on: February 09, 2009, 12:02:53 PM »
Once you get the document object, then use the Close method of said document with correct argument for the Save argument of the method.  Since you know the name, then you can get the document object from the Document Collection.  If it is found, then close it.

Its early for me, so something along the lines of ( code written here, and no testing ):

Code: [Select]
(defun CloseAndSave ( dwgName bSave )

    (vlax-for i (vla-get-Documents (vlax-get-Acad-Object))
        (if (= (strcase dwgName) (strcase (vla-get-Name i)))
            (if
                (and
                    bSave
                    (equal (vla-get-ReadOnly i) :vlax-false)
                )
                (vla-Close i :vlax-true)
                (vla-Close i :vlax-false)
            )
        )
    )
)

The above code can not colse the active drawing.

Nope.  You might have to pass a VBA statement because you don't do it with Lisp.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

mr_nick

  • Guest
Re: close a drawing that is not current
« Reply #15 on: July 15, 2009, 03:20:57 AM »
I'm going to revive this topic because it's exactly the process I want to carry out but it has caused me grief for many years and I'm now stuck as to how to get past my problem.

I approached this task a few years back using Vlisp but found that there was a flaw which caused the vlisp system to shutdown and stop all further commands running until AutoCAD was restarted. I then used a VBA command to overcome this glitch and it's been working just fine for the last few years. Now with the advent of 2010 and the demise of VBA, I now need to find a replacement for the VBA command:

(command "VBASTMT" (strcat "AutoCAD.Application.Documents.Item(\"temp.dwg\").Close") )

In order to demonstrate the problem I have when using the Vlisp approach, following these steps creates the error each and every time I try it so presumably it will do the same for anybody wishing to have a nosey:

Open a new AutoCAD session so that you have 'Drawing1' open
Open a new drawing so you now have 'Drawing2' open.
Enter the following commands to close 'Drawing1':
(vl-load-com)
(setq docs (vla-get-documents (vlax-get-acad-object)))
(vla-close (vla-item docs "Drawing1.dwg") :vlax-false)


You should now just have 'Drawing2' left open. Now use the following command and see what happens:
(command "_.line" "0,0" "10,10" "")

In my case I get the following error and no further lisp or vlisp will run until I restart CAD:
AecRcpLispSupport::getArgIgnore() got null."

And that's where I'm now stuck. I have re-tried all the above code but continually get the aforementioned error. I'm currently running ACA2010 but I've had this problem on versions as far back as 2006 when I initially tried to get it working. Would I be correct in thinking that nobody else out there has experienced the same glitch?