Preventing AutoCAD from entering a zero doc state

By Virupaksha Aithal

When AutoCAD is in a zero document state, you have very limited access to AutoCAD’s functionality. One straightforward method of solving this problem is to prevent AutoCAD from entering the zero document state.

For this, handle the “DocumentCollection.DocumentToBeDestroyed” event, in this event handler; make a call to “ExecuteInApplicationContext” passing a callback. Add a new document, when AutoCAD calls the callback function.

static bool bBeginQuit = false;
 
[CommandMethod("ZeroDocState")]
public static void ZeroDocState()
{
    Application.BeginQuit +=new EventHandler(Application_BeginQuit);
    DocumentCollection docMg = Application.DocumentManager;
    docMg.DocumentToBeDestroyed +=
        new DocumentCollectionEventHandler(docMg_DocumentToBeDestroyed);
}
 
 
static void Application_BeginQuit(object sender, EventArgs e)
{
    bBeginQuit = true;
}
 
static void docMg_DocumentToBeDestroyed(object sender,
                                    DocumentCollectionEventArgs e)
{
    DocumentCollection docman = (DocumentCollection)sender;
 
    //1 - means, after closing the document AutoCAD will 
    //be in zero document state, so call ExecuteInApplicationContext
    if (docman.Count == 1)
    {
        Application.DocumentManager.ExecuteInApplicationContext(
                                      ApplicationContextAdd, null);
    }
}
 
static void ApplicationContextAdd(object data)
{
    try
    {
        if(!bBeginQuit)
            Application.DocumentManager.Add("");
    }
    catch
    {
 
    }
}

Comments

8 responses to “Preventing AutoCAD from entering a zero doc state”

  1. Tony Tanzillo Avatar
    Tony Tanzillo

    It looks like you haven’t bothered to test your solution.
    AutoCAD will enter zero document state when the user issues the QUIT command.
    What then?

  2. Tony Tanzillo Avatar
    Tony Tanzillo

    On second thought… what possible legitimate reason can there be to prevent AutoCAD from entering zero-document state?
    “When AutoCAD is in a zero document state, you have very limited access to AutoCAD’s functionality”..
    That is kind of like saying “when you park your car and turn off the ignition, you have very limited access to your car’s functionality”.
    Duh

  3. Tony – The analogy you used answers your own question, so I’m not sure the reason for your ‘duh’. And this is a question that is frequently asked by ADN partners, so many people clearly do want this behavior.
    Its very kind of you to pop onto this blog occasionally to give us the benefit of your knowledge – our whole community benefits if we all share with each other – but please keep your comments constructive when you do visit. If you can’t post a comment without being condescending, then please don’t post comments.

  4. Tony Tanzillo Avatar
    Tony Tanzillo

    ADN partners that don’t understand why preventing AutoCAD from entering a zero-document state is not not a legitimate requirement which they shouldn’t do, should be made to understand why, rather than thoughtlessly accommodated, without regards for whether the need is legitimate and/or interferes with the normal intended use of the product.
    If a user closes all open documents, they did it for a reason. If the ADN partner doesn’t know how to display their own UI in zero-document state (which is usually the reason why they want to prevent the user from getting there) then they should be shown how to do that, rather than shown how to implement a kludge that interferes with the normal use of the product, and the use of other extensions that may display a different UI when AutoCAD enters zero-document state.

  5. Thank you for expanding on your previous comment, Tony. I agree that its always important to understand the underlying reason why someone is asking for a specific functionality. But we’ll have to agree to disagree on the ‘legitimacy’ of this one. We have a large community of programmers who customize AutoCAD for many reasons, and in very different environments. They know what they and their customers want and need – not us. To say its ‘not a legitimate requirement’ is using too broad a brush. A reductio ad absurdum of this is that any AutoCAD customization changes the behavior some AutoCAD user has come to expect, ergo all customization is illegitimate.

  6. Tony Tanzillo Avatar
    Tony Tanzillo

    The solution posted crashes AutoCAD when the QUIT command is used, which I believe does qualify as interference.

  7. Thank you Tony. My previous responses were to your second comment – I hadn’t referred to your first comment until now. It appears that you now accept that this is a legitimate use case, and your only beef is that Viru didn’t explicitly demonstrate in this post how an exiting app should clean up an event handler it has registered. That’s not surprising, as this blog is mostly focused on code snippets rather than full-blown apps – we don’t cover every caveat in every post. But it is a good suggestion for a future post – probably after we’ve finished with AU and our Developer Days conferences – and I’m sure Viru will address the specific point for this post now you’ve raised it (and once he returns from vacation).

  8. Thanks for the comments. I included a check for quit scenario. The modified code is now on the blog.
    -Viru

Leave a Reply to Tony TanzilloCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading