By Adam Nagy
I have the below code. When the vla-activate part is executed then my LISP code stops and none of the code parts after that is reached.
(command "_sdi" "0") ; the problem is with MDI mode / SDI=0
(vl-load-com)
(setq acadApp (vlax-get-acad-object))
(setq acadActiveDoc (vla-get-ActiveDocument acadApp))
(setq acadDocs (vla-get-documents acadApp))
(setq newDrawing "C:\test.dwg")
(if
(= 0 (getvar "SDI"))
; if sdi = 0
(vla-activate (vla-open acadDocs newDrawing))
; if sdi = 1
(vla-sendcommand acadActiveDoc (
strcat "(command \"_open\")\n" newDrawing "\n"
)
)
)
(print "this part is not reached")
Solution
There are two main contexts in AutoCAD:
- Document context – only works within the boundaries of a document
- Session/Application context – best suited for opening/closing/activating documents
You can find further information about contexts in the Object ARX Reference Guide, <Object ARX SDK>\docs\arxdoc.chm
LISP can only work in Document context – the command or function is bound to the Document that it was started from.
When you activate another document, then the running code's execution stops and only continues once the document it was running in becomes active again.
Also, for similar reasons you cannot close the document that the code is running in.
However, you can open documents in the background and use the API to interact with them – i.e. you open them the same way you're doing it now, but simply do not activate them.
You could also use ARX or .NET instead, which can create commands that run in session context.

Leave a Reply