Close the Active Drawing for Next and Previous Commands

Discussion in 'AutoCAD' started by Peter Farrell, Apr 8, 2004.

  1. Hey all,
    I'm only about 4 hours into trying to understand VLA, VLAX, etc. Objects
    etc. So this may be pretty ugly codewise, but
    I managed to get a working version of NEXT and PREV drawings (ie. open the
    next or previous drawing in the project folder and close the Active
    Document).

    This has been addressed a number of times on the Newsgroup - but I have yet
    to see a complete answer. The problem with this one is that after about 5
    uses, my drawing CRASHES! No damage done, but I assume this is due to some
    clogging of memory? I don't want to resort to SDI, to solve this - that
    would be the easy way out.

    Here's what I have so far...

    ;;;
    (defun c:NEXT ( / acadapp docs activedoc path fnam dwgs nextfnam nextdwg)
    (setq acadapp (vlax-get-acad-object))
    (setq docs (vla-get-documents acadapp))
    (setq activedoc (vla-get-ActiveDocument acadapp))
    (setq path (vla-get-Path activedoc))
    (setq fnam (vla-get-Name activedoc))
    (setq dwgs (acad_strlsort (vl-directory-files path "*.dwg" 1)))
    (setq nextfnam (cadr (member fnam dwgs)))
    (if (/= nextfnam nil)
    (progn
    (setq nextdwg (vla-Open docs (strcat path "\\" nextfnam)))
    (command "vbastmt" "activedocument.close false")
    )
    )
    (prin1)
    )
    ;;;
    (defun c:pREV ( / acadapp docs activedoc path fnam dwgs nextfnam nextdwg)
    (setq acadapp (vlax-get-acad-object))
    (setq docs (vla-get-documents acadapp))
    (setq activedoc (vla-get-ActiveDocument acadapp))
    (setq path (vla-get-Path activedoc))
    (setq fnam (vla-get-Name activedoc))
    (setq dwgs (reverse (acad_strlsort (vl-directory-files path "*.dwg"
    1)))) ;;;reverse for previous
    (setq nextfnam (cadr (member fnam dwgs)))
    (if (/= nextfnam nil)
    (progn
    (setq nextdwg (vlax-invoke-method docs "Open" (strcat path "\\"
    nextfnam)))
    (command "vbastmt" "activedocument.close false")
    )
    )
    (prin1)
    )
    ;;;;

    Thanks for Any Help.

    P. Farrell
     
    Peter Farrell, Apr 8, 2004
    #1
  2. Well That Flew right over my head - but not as much as it would have
    Yesterday =).
    In your example it appears "Previous" drawing is the Base drawing you return
    to from your Xref, but the Xref stays open and so does the base drawing, and
    I think I've already accomplished that, though crudely. I want to go one
    step further to actually close the "base" drawing in your example, but
    that's where I'm stuck - With my versions of Next and Previous I can step
    through the directory of drawings, but only get 4 or 5 "Nexts" or
    "Previouses" before I crash. Which either means I'm not doing something
    right with the VL-stuff, or the VBA line, or I'm reaching some memory issue
    or something else. Does this has something to do with needing to "release"
    objects?

    As for the opendwg part, Previously I was using a VBA snipet from somebody
    on this newsgroup, and occasionallyy it would choke on a file name - it
    would say the file name wasn't available to open when it clearly was. It
    seems similar to what you have in your utilities lisp - is vl-cmdf is a
    better way to go than command?

    (defun cmdOpen (name)
    (command "._VBAStmt"
    (strcat "AcadApplication.Documents.Open \"" name "\"")
    )
    )

    THANKS for the Pointer to your site - Lots to digest there.

    a
     
    Peter Farrell, Apr 8, 2004
    #2
  3. Peter Farrell

    Gary J. Orr Guest

    Without knowing the details of what you're up to I would ask:
    Why are you opening the next drawing, then closing the previous? Why not
    close the current drawing, then open the next? (this is a stupid question if
    you're insuring that at least one drawing stays open, but begs to be asked)
    Also, Why are you opening a drawing, then closing it without saving it?
    <snip>
    (command "vbastmt" "activedocument.close false")
    <snip>
    And more, are you trying to do a batch process or are you wanting the
    drawing to remain open until you are ready to move on to the next?
    Is there a particular reason that you use the "(command "vbastmt"" over the
    vla-open and vla-close statements? (other than the fact that it might make
    translations a bit easier)

    If you're after a batch process I've just built a decent little routine that
    launches a second session of AutoCAD and sends commands to it from the first
    session. I have been able to batch process 30+ drawings in a given folder,
    and all of it's subfolders, with lisp routines (some that required user
    input).

    I kept hitting dead ends when I was trying to run the process from the same
    session of AutoCAD.

    --
    Gary J. Orr
    CADD Administrator
    (218) 279-2421


    LHB, Inc
    21 West Superior Street, Suite 500
    Duluth, Mn 55802
    (218) 727-8446
    www.LHBcorp.com
     
    Gary J. Orr, Apr 9, 2004
    #3
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.