Active Document problems

Discussion in 'AutoCAD' started by Shane-W, Dec 2, 2004.

  1. Shane-W

    Shane-W Guest

    Im getting an error from a lisp routine im writing. The lisp takes all open drawings and creates an index of all the open drawings in alpha/numerical order. When the command is issued the drawing will switch to the next DWG in line alphabetically. This all works fine however.

    The problem is that I get the error:
    ; error: Exception occurred: 0xC0000005 (Access
    Violation)
    ; error: Unknown exception occurred
    ; warning: unwind skipped on unknown exception

    I have seen this problem posted before and the reason that I repost this error is that it seems to be caused by another error i get if I try to EXIT Acad with drawings open that have been activated by this code. I get this statement from Acad (alert box):
    AutoCad cannot close xx1.dwg; because there is a command still active. Please complete the command and try agian.

    My question (finally) is this: Is there a way for me to complete the command in the "background".

    The command not completing is from the lisp routine. If I switch back to the drawing that still has an active command it will pass 2 empty command lines:
    Command: (Index_Scroll "D")
    Command:
    Command:

    Ocassionally, enough of these errors will crash cad without warning .. poof no cad/no save :(

    Below is the code snipet, Its a work in progress and is incomplete but functional.

    (defun Gen_Index ( )
    (setq acadobject (vlax-get-Acad-Object))
    (setq documentcollection (vla-get-documents acadobject))
    (SETQ DOCUMENTCOUNT (VLA-GET-COUNT DOCUMENTCOLLECTION))
    (SETQ TMPCNT 0)
    (SETQ DOC_LIST NIL)
    (SETQ DOC_LIST_INDEX NIL)
    (WHILE (< TMPCNT DOCUMENTCOUNT)
    (SETQ DOC_LIST (APPEND DOC_LIST (LIST (VLA-GET-NAME (vla-item documentcollection TMPCNT)))))
    (SETQ DOC_LIST_INDEX (APPEND DOC_LIST_INDEX (LIST (CONS TMPCNT (VLA-GET-NAME (vla-item documentcollection TMPCNT))))))
    (SETQ TMPCNT (1+ TMPCNT))
    )
    (SETQ DOC_LIST_INDEX DOC_LIST )
    (SETQ DOC_LIST (VL-SORT DOC_LIST '<))
    (SETQ DOC_LIST_CNTR 0)
    (SETQ TEMP_INDEX NIL)
    (FOREACH DWG_NAME DOC_LIST
    (SETQ TMP_CNTR 0)
    (WHILE (/= DWG_NAME DWG_INDEX)
    (SETQ DWG_INDEX (NTH TMP_CNTR DOC_LIST_INDEX))
    (SETQ TMP_CNTR (1+ TMP_CNTR))
    )
    (SETQ TEMP_INDEX (APPEND TEMP_INDEX (LIST (NTH DOC_LIST_CNTR DOC_LIST) (1- TMP_CNTR))))
    (SETQ DOC_LIST_CNTR (1+ DOC_LIST_CNTR))
    )
    (vl-propagate 'temp_index)
    (vl-propagate 'ACADOBJECT)
    (vl-propagate 'documentcollection)
    )
    ; move down code BELOW
    (defun Go_Dn ()
    (SETQ DOCUMENT_NUMBER (NTH 3 (MEMBER (getvar 'dwgname) TEMP_INDEX)))
    (VLA-PUT-ACTIVEDOCUMENT ACADOBJECT (vla-item documentcollection DOCUMENT_NUMBER))
    (PRINC)
    )
    ;move up code
    (defun Go_Up ()
    (SETQ DOCUMENT_NUMBER (NTH (1- (vl-position (getvar 'dwgname) TEMP_INDEX)) TEMP_INDEX))
    (VLA-PUT-ACTIVEDOCUMENT ACADOBJECT (vla-item documentcollection DOCUMENT_NUMBER))
    (PRINC)
    )
    ; trigger code below
    (defun Index_Scroll (Direction)
    (Gen_Index)
    (if (EQUAL (strcase direction) "U") (Go_Up))
    (if (EQUAL (strcase direction) "D") (Go_Dn))
    (PRINC)
    )
     
    Shane-W, Dec 2, 2004
    #1
  2. Shane-W

    James Allen Guest

    Hi Shane,
    Try using vlax-get ... instead of vla-get-... ; vla-get occasionally throws
    this kind of error when vlax-get will not. I do not know why. A google
    search of these groups for that error should yield more info. Good luck.
    --
    James Allen, EIT
    Malicoat-Winslow Engineers, P.C.
    Columbia, MO


    open drawings and creates an index of all the open drawings in
    alpha/numerical order. When the command is issued the drawing will switch to
    the next DWG in line alphabetically. This all works fine however.
    error is that it seems to be caused by another error i get if I try to EXIT
    Acad with drawings open that have been activated by this code. I get this
    statement from Acad (alert box):
    Please complete the command and try agian.
    the drawing that still has an active command it will pass 2 empty command
    lines:
    (VLA-GET-NAME (vla-item documentcollection TMPCNT))))))
     
    James Allen, Dec 2, 2004
    #2
  3. Shane-W

    Shane-W Guest

    Thanks for the input James. But It was no help.

    I run Acad 2000i on XP (sp2 probablly) the only valid vlax-get was vlax-get-Acad-Object.

    Thanks agian however for your response.
    Shane
     
    Shane-W, Dec 2, 2004
    #3
  4. Shane-W

    Jürg Menzi Guest

    Hi Shane-W

    Never propagate objects! Localize variables...

    This can be a possible solution:
    Code:
    (defun Index_Scroll (Dir / AcaObj DocCol DocLst DocNum DocObj MaxPos MovDir)
    (vl-load-com)
    (setq AcaObj (vlax-get-Acad-Object)
    DocCol (vla-get-Documents AcaObj)
    )
    (vlax-for DocObj DocCol
    (setq DocLst (cons (vla-get-Name DocObj) DocLst))
    )
    (setq DocLst (vl-sort
    DocLst
    (function (lambda (a b) (< (strcase a) (strcase b))))
    )
    MaxPos (1- (length DocLst))
    MovDir (eq (strcase Dir) "D")
    DocNum (vl-position (getvar 'DWGNAME) DocLst)
    DocNum (cond
    ((and MovDir (< DocNum MaxPos)) (1+ DocNum))
    ((and MovDir (= DocNum MaxPos)) 0)
    ((> DocNum 0) (1- DocNum))
    (MaxPos)
    )
    )
    (vla-put-ActiveDocument AcaObj (vla-Item DocCol (nth DocNum DocLst)))
    (princ)
    )
    
    Cheers
     
    Jürg Menzi, Dec 3, 2004
    #4
  5. Shane-W

    Shane-W Guest

    Great deal works perfect Jürg.

    One question however, Where did I go wrong? The activate document code is nearly identical, your list sort is super clean although I wouldnt think how I got a var name would matter a great deal.

    Any after thoughts will be greatly appreciated :)
    Thank You Jürg
    Shane
     
    Shane-W, Dec 3, 2004
    #5
  6. Shane-W

    Shane-W Guest

    Doh I thought it had gone away but the error still happens once I start to close the drawings. Thats when the unwind error happens.

    Thanks all the same however.
    Shane
     
    Shane-W, Dec 3, 2004
    #6
  7. Shane-W

    GaryDF Guest

    Try these.......

    Gary


    ;;;The NEXT.LSP function that opens the next drawing in a directory.
    ;;;http://www.cad-code.com/archive.htm
    ;;;Peter Jamtgaard

    (defun NEXT-IT (/ CNT FILELIST NEXTDWG objDWG acadObject)
    (vl-load-com)
    (setq FILELIST (acad_strlsort (vl-directory-files (getvar "dwgprefix") "*.dwg"))
    CNT (length (member (getvar "dwgname") (reverse FILELIST)))
    )
    (if (< CNT (length FILELIST))
    (progn
    (setq NEXTDWG (strcat (getvar "dwgprefix") (nth CNT FILELIST)))
    (if (IsItOpen NEXTDWG)
    (princ)
    (command "vbastmt" (strcat "AcadApplication.Documents.Open \"" NEXTDWG "\""))
    )
    )
    (alert "Error: You are in the last\nDrawing in this Directory!")
    )
    (prin1)
    (if objDwg
    (progn
    (setq acadObject (vlax-get-acad-object))
    (vlax-put-property acadObject 'Activedocument objDWG)
    )
    )
    )
    (defun c:n2 ()
    (NEXT-IT)
    )
    (defun c:nc2 ()
    (Close_All)
    (NEXT-IT)
    )
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;;;;
    (defun PREV-IT (/ CNT FILELIST NEXTDWG objDWG acadObject)
    (vl-load-com)
    (setq FILELIST (acad_strlsort (vl-directory-files (getvar "dwgprefix") "*.dwg"))
    CNT (length (member (getvar "dwgname") (reverse FILELIST)))
    )
    (if (< CNT (length FILELIST))
    (progn
    (setq NEXTDWG (strcat (getvar "dwgprefix") (nth (- CNT 2) FILELIST)))
    (if (IsItOpen NEXTDWG)
    (princ)
    (command "vbastmt" (strcat "AcadApplication.Documents.Open \"" NEXTDWG "\""))
    )
    )
    (alert "Error: You are in the last\nDrawing in this Directory!")
    )
    (prin1)
    (if objDwg
    (progn
    (setq acadObject (vlax-get-acad-object))
    (vlax-put-property acadObject 'Activedocument objDWG)
    )
    )
    )

    (defun c:p2 ()
    (PREV-IT)
    )
    (defun c:pc2 ()
    (Close_All)
    (PREV-IT)
    )
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;;;;
    ;;;IsItOpen by Bill Kramer
    (defun IsItOpen (DwgName / DWGS DWG Flag)
    (setq DWGS (vlax-get-property (vlax-get-acad-object) "Documents"))
    (setq objDWG nil)
    (vlax-for DWG DWGS
    (if (or
    (= (strcase (vlax-get-property DWG "Name"))
    (strcase DwgName))
    (= (strcase (vlax-get-property DWG "FullName"))
    (strcase DwgName)))
    (progn
    (setq objDWG DWG)
    (setq Flag 'T)
    )
    )
    )
    Flag
    )
    ;;;Peter Jamtgaard
    (defun Close_All (/ CLOSE1 FULL)
    (vl-load-com)
    (vlax-for
    DOCOBJ (vla-get-documents (vlax-get-acad-object))
    (setq FULL (strcat (vla-get-path DOCOBJ) "\\" (vla-get-name DOCOBJ)))
    (if (= (vla-get-active DOCOBJ) :vlax-false)
    (if (vl-catch-all-error-p
    (vl-catch-all-apply
    '(lambda (X) (vla-close X :vlax-true FULL))
    (list DOCOBJ)))
    (princ (strcat "\nCan not close drawing "
    FULL
    " with active command in progress:")))))
    (princ))


    drawings and creates an index of all the open drawings in alpha/numerical order.
    When the command is issued the drawing will switch to the next DWG in line
    alphabetically. This all works fine however.
    is that it seems to be caused by another error i get if I try to EXIT Acad with
    drawings open that have been activated by this code. I get this statement from
    Acad (alert box):
    complete the command and try agian.
    drawing that still has an active command it will pass 2 empty command lines:
    (vla-item documentcollection TMPCNT))))))
     
    GaryDF, Dec 3, 2004
    #7
  8. Shane-W

    Shane-W Guest

    Gary thanks for the input. Those seem to work. I guees its the way that the dwg is selected for the statement : (vlax-put-property acadObject 'Activedocument objDWG)

    I tried to just put that in the code I use now but still no go. However, the progams you supplied work perfectly.

    Ill have to figure out the differences between the two methods. Just scanning over the code it seems that all 3 pieces of code (mine jurgs and yours) are pretty much the same, except for the method of generating the list and selecting the next drawing.

    Anyways, thanks agian. This will help on a couple of mass editing routines im working on.
    Shane
     
    Shane-W, Dec 3, 2004
    #8
  9. Shane-W

    GaryDF Guest

    Your welcome....thanks goes to Bill Kramer and Peter Jamtgaard.

    Gary

    is selected for the statement : (vlax-put-property acadObject 'Activedocument
    objDWG)
    progams you supplied work perfectly.
    over the code it seems that all 3 pieces of code (mine jurgs and yours) are
    pretty much the same, except for the method of generating the list and selecting
    the next drawing.
     
    GaryDF, Dec 6, 2004
    #9
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.