.NET

Reply
Active Contributor
absStructural
Posts: 37
Registered: ‎07-15-2010
Message 1 of 7 (155 Views)
Accepted Solution

Prompt from DocumentCreated

155 Views, 6 Replies
06-12-2014 03:15 PM

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");
        }
    }
}

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.

 

Valued Mentor
fieldguy
Posts: 382
Registered: ‎03-31-2005
Message 2 of 7 (116 Views)

Re: Prompt from DocumentCreated

06-13-2014 07:06 AM 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.

Active Contributor
absStructural
Posts: 37
Registered: ‎07-15-2010
Message 3 of 7 (114 Views)

Re: Prompt from DocumentCreated

06-13-2014 07:15 AM 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.

*Expert Elite*
norman.yuan
Posts: 1,064
Registered: ‎04-27-2009
Message 4 of 7 (97 Views)

Re: Prompt from DocumentCreated

06-13-2014 10:09 AM 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.

Active Contributor
absStructural
Posts: 37
Registered: ‎07-15-2010
Message 5 of 7 (97 Views)

Re: Prompt from DocumentCreated

06-13-2014 10:11 AM 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).

*Expert Elite*
norman.yuan
Posts: 1,064
Registered: ‎04-27-2009
Message 6 of 7 (86 Views)

Re: Prompt from DocumentCreated

06-13-2014 10:36 AM 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.

 

Active Contributor
absStructural
Posts: 37
Registered: ‎07-15-2010
Message 7 of 7 (76 Views)

Re: Prompt from DocumentCreated

06-13-2014 11:12 AM 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.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.