.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Prompt from DocumentCreated

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
absStructural
442 Views, 6 Replies

Prompt from DocumentCreated

I have some code that I stripped down to what you see below.  Basically, when a drawing is opened (or a new drawing is created), I need to prompt the user for some stuff.  The problem I'm running across is that when the prompt "Enter text" is shown, I cannot actually type anything in the command prompt.  Basically, this results in AutoCAD becoming unusable because it is waiting for a response to the prompt, but I cannot give one.

 

Any thoughts as to why this occurs or what I can do about it?

public class CommandsClass: IExtensionApplication
{
    /// <summary>
    /// Default constructor
    /// </summary>
    public CommandsClass()
    {
        InitFunctions.AddEventHandlers();
    }
}
public static class InitFunctions
{
    public static void AddEventHandlers()
    {
         Application.DocumentManager.DocumentCreated += new DocumentCollectionEventHandler(DocumentManager_DocumentCreated);
    }
    public static void DocumentManager_DocumentCreated(object sender, DocumentCollectionEventArgs e)
    {
        if(e.Document != null)
        {
            PromptResult result = e.Document.Editor.GetString("Enter text");
        }
    }
}
6 REPLIES 6
Message 2 of 7
fieldguy
in reply to: absStructural

Try this before promptresult:

 

Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView();

 

I also use this when the program is finished.

 

Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();

 

You don't mention a version.  IIRC the "Internal" namespace required a different reference for 2009.

Message 3 of 7
absStructural
in reply to: fieldguy

I tried using this:

Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView();
                PromptResult result = e.Document.Editor.GetString("Type something");
                Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();

 But that yielded the same result.

 

We're using AutoCAD 2012.

Message 4 of 7
norman.yuan
in reply to: absStructural

I do not think you can interact with the document via Editor when in the DocumentCreatedevent handler: The document is just created in the memory, and have not become active/current in AutoCAD.

 

If you need to prompt user something this way, you'd better handle DocumentBecameCurrent or DocumentActivated event when the document becomes current the first time.

Message 5 of 7
absStructural
in reply to: norman.yuan

I suspect you are correct.

 

Is there a good way to check for 'the first time'?  I believe DocumentBecameCurrent and DocumentActivated will fire every time (not just the first).

Message 6 of 7
norman.yuan
in reply to: absStructural

You can create a static collection, such as List<string> to serve as flag of being prompted.

 

That is, whenever DocumentBecameCurrent is fired, you check if the document's name is in the list or not. If not, do the prompting and save the document name into the list. If yes, no prompting, of course.

 

Since a drawing could be closed and open multiple times in the same AutoCAD session, you may also want to handle DocumentDestroyed event to remove the closed document name from the list, so when the drawing is opened again, user get prompted.

 

Message 7 of 7
absStructural
in reply to: norman.yuan

I think that'll do the trick.  The only other caveat I'll add is that the BeginSave event must be monitored for every document.  In that eventhandler, the current database filename must be compared to the new filename (DatabaseIOEventArgs.FileName).  In the event of a SaveAs, the filename will change and the old filename should be removed from the static list.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost