<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Hijacking new button functionality in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/hijacking-new-button-functionality/m-p/12532634#M5720</link>
    <description>&lt;P&gt;Thanks again for your help,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think I found the problem. If I create a new document while another document exists the command that is run is NEW, which as you mentioned I can easily veto and then run whatever code I want.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I try to create a new document from the starting page, before opening any document, the command that is run is DOCINIT rather than NEW. When I veto DOCINIT is when I get the error I mentioned in my previous post. Is there any especial consideration to veto DOCINIT?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ideally the solution I am after should work the same whether or not a document already exists.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;        private void OnLockModeChange(object sender, DocumentLockModeChangedEventArgs e)
        {
            var commandName = e.GlobalCommandName;
            //This case happens if a doc is already open
            if (commandName == "NEW" || commandName == "QNEW")
            {
                //Here I would show my form etc...

                //Veto runs without problem
                e.Veto();
            }

            //This happends if try to create doc from start page
            if (commandName == "DOCINIT")
            {
                //Here I would show my form etc...
                e.Veto();
                //Here is where things get ugly
            }
        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 31 Jan 2024 19:45:34 GMT</pubDate>
    <dc:creator>p.alvarez.rio</dc:creator>
    <dc:date>2024-01-31T19:45:34Z</dc:date>
    <item>
      <title>Hijacking new button functionality</title>
      <link>https://forums.autodesk.com/t5/net-forum/hijacking-new-button-functionality/m-p/12529012#M5716</link>
      <description>&lt;P&gt;I am trying to create a plugin with the following behaviour: &amp;nbsp;When a user runs the new command (either writing new on the command line or clicking on File/New) they will get a custom WPF window where they can select a template from our cloud storage. After selecting the template a new document with said template will be created.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To do that I am subscribing to DocumentCreateStarted event and launching my custom WPF window from there. At this point I would like to cancel the “standard” file creation process so my application can take care of creation of the new project, unfortunately I cannot find a way of doing it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another option I considered was using the EnterModal event and try to detect when a window with the name “Select Template” is going to be displayed so I can then show my custom window. The problem with this approach is that in the event args I have no information about what window is going to be shown.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We have implemented something similar in Revit subscribing to DialogBoxShowingEventArgs, checking the dialog corresponds to the new project window, overriding the event and then running our own custom code, but I am struggling to do the same in Autocad.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be greatly appreciated!&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jan 2024 12:48:31 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/hijacking-new-button-functionality/m-p/12529012#M5716</guid>
      <dc:creator>p.alvarez.rio</dc:creator>
      <dc:date>2024-01-30T12:48:31Z</dc:date>
    </item>
    <item>
      <title>Re: Hijacking new button functionality</title>
      <link>https://forums.autodesk.com/t5/net-forum/hijacking-new-button-functionality/m-p/12529180#M5717</link>
      <description>&lt;P&gt;Do you really want to strip the capability for CAD user to use standard AutoCAD commands? I'd prefer to have explicitly my own custom command besides the standard command. In your case, you can add custom menu items to the big "A" button (on top-left corner of AutoCAD main window) and the quick assess button panel for executing your custom commands, so user has the same UI experience, but knows exactly/explicitly what command is to use.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if you really want to "hijack" an existing command, you can:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. The simplest way to do it is to redefine the existing command "New" to use your custom command for opening new file. Say, you can use acad.lsp to load something like&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(command "redefine" "MyNewCommand")&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. The .NET API solution is to handle DocumentCollection.DocumentLockModeChanged event (not DocumentCreateStarted event), in this event handler, you can test the command by its name, if it is "NEW", you can veto the command and in the DocumentLockModeChangeVetoed event handler, you can start your own command by calling SendStringToExecute("myNewCommand",.....). You may consider to pop up an option dialog before the "NEW" command to be vetoed for user to choose continuing with standard "NEW" command, or using your alternate command.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jan 2024 14:03:32 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/hijacking-new-button-functionality/m-p/12529180#M5717</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2024-01-30T14:03:32Z</dc:date>
    </item>
    <item>
      <title>Re: Hijacking new button functionality</title>
      <link>https://forums.autodesk.com/t5/net-forum/hijacking-new-button-functionality/m-p/12531267#M5718</link>
      <description>&lt;P&gt;Thanks for your help Norman.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The plan is to include a button in the custom WPF window to let the user to continue with the standard document creation process.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I followed your suggestion of using DocumentLockModeChanged event, in there I show my custom window and depending on the user action I veto the DOCINIT command or continue normally. The problem I found is when I veto the command the “Select Template” window still shows up, and if I select a template there I get an error message:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="AcadError.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1320464i6B28BDD00243FDEB/image-size/medium?v=v2&amp;amp;px=400" role="button" title="AcadError.png" alt="AcadError.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It seems to be that is trying to load a doc that is not property created because I veto the command in the middle of the process.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I will probably have to go with the option of adding a custom button rather than using the standard new button.&amp;nbsp;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2024 09:34:19 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/hijacking-new-button-functionality/m-p/12531267#M5718</guid>
      <dc:creator>p.alvarez.rio</dc:creator>
      <dc:date>2024-01-31T09:34:19Z</dc:date>
    </item>
    <item>
      <title>Re: Hijacking new button functionality</title>
      <link>https://forums.autodesk.com/t5/net-forum/hijacking-new-button-functionality/m-p/12532294#M5719</link>
      <description>&lt;P&gt;Since you did not show your code of how the command vetoing is handled, I cannot comment on why you got that the error, other than be certain that your code must be wrong.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;While I prefer simply providing short-cut (customizing the big "A" button, quick access buttons, or custom ribbon items...) to the customized open drawing command to "hijacking" existing command(s), as I stated in my previous reply, vetoing existing command is also rather easy to do. Following is simplified code for your case (as you described in the OP):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;        private static bool _runMyNewCommand = false;
        [CommandMethod("ForceMyTemplate")]
        public static void HandleDocumentLockChange()
        {
            // you probably want to place this line of code
            // in IExtenstionApplication.Initialize()
            CadApp.DocumentManager.DocumentLockModeChanged += DocumentManager_DocumentLockModeChanged;
        }

        private static void DocumentManager_DocumentLockModeChanged(object sender, DocumentLockModeChangedEventArgs e)
        {
            var cmd = e.GlobalCommandName.ToUpper();
            if (cmd == "NEW" || cmd == "QNEW")
            {
                var msg = "Open new drawing with project-specific template?";
                var res = MessageBox.Show(msg, "Open New Drawing Option",
                    MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (res == DialogResult.Yes || res == DialogResult.Cancel)
                {
                    CadApp.DocumentManager.DocumentLockModeChangeVetoed += DocumentManager_DocumentLockModeChangeVetoed;
                    if (res == DialogResult.Yes)
                    {
                        _runMyNewCommand = true;
                    }
                    e.Veto();
                }
            }
        }

        private static void DocumentManager_DocumentLockModeChangeVetoed(object sender, DocumentLockModeChangeVetoedEventArgs e)
        {
            CadApp.DocumentManager.DocumentLockModeChangeVetoed -= DocumentManager_DocumentLockModeChangeVetoed;
            if (_runMyNewCommand)
            {
                e.Document.SendStringToExecute("MyNewCommand\n", false, false, false);
            }
        }

        [CommandMethod("MyNewCommand", CommandFlags.NoHistory)]
        public static void OpenNewDrawingWithTemplate()
        {
            _runMyNewCommand = false;
            MessageBox.Show("This is UI for opening new drawing with\nproject-specific templates");
        }&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since the code is rather easy to follow, I omit to post a companion video clip to prove it works as expected.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2024 17:03:51 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/hijacking-new-button-functionality/m-p/12532294#M5719</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2024-01-31T17:03:51Z</dc:date>
    </item>
    <item>
      <title>Re: Hijacking new button functionality</title>
      <link>https://forums.autodesk.com/t5/net-forum/hijacking-new-button-functionality/m-p/12532634#M5720</link>
      <description>&lt;P&gt;Thanks again for your help,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think I found the problem. If I create a new document while another document exists the command that is run is NEW, which as you mentioned I can easily veto and then run whatever code I want.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I try to create a new document from the starting page, before opening any document, the command that is run is DOCINIT rather than NEW. When I veto DOCINIT is when I get the error I mentioned in my previous post. Is there any especial consideration to veto DOCINIT?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ideally the solution I am after should work the same whether or not a document already exists.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;        private void OnLockModeChange(object sender, DocumentLockModeChangedEventArgs e)
        {
            var commandName = e.GlobalCommandName;
            //This case happens if a doc is already open
            if (commandName == "NEW" || commandName == "QNEW")
            {
                //Here I would show my form etc...

                //Veto runs without problem
                e.Veto();
            }

            //This happends if try to create doc from start page
            if (commandName == "DOCINIT")
            {
                //Here I would show my form etc...
                e.Veto();
                //Here is where things get ugly
            }
        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2024 19:45:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/hijacking-new-button-functionality/m-p/12532634#M5720</guid>
      <dc:creator>p.alvarez.rio</dc:creator>
      <dc:date>2024-01-31T19:45:34Z</dc:date>
    </item>
    <item>
      <title>Re: Hijacking new button functionality</title>
      <link>https://forums.autodesk.com/t5/net-forum/hijacking-new-button-functionality/m-p/12534847#M5721</link>
      <description>&lt;P&gt;I looked into the "DOCINIT" issue. Here are what I found:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. When start "New" in "Start" tab of AUtoCAD, the AutoCAD may have o or multiple drawings open. and the DocumentLockModeChanged event indeed fires as the trigger command is "DOCINIT";&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. While it seems strange when the command starts from "Start" Tab, not from an active document, the DocumentLockModeChanged event still fires with its argument having a Document property. Originally, I though the e.Document property might be null, but it turns out the Document property is not null, but e.Document.Database is null. So, I assume in order to fire the DocumentLockModeChanged event, the API developer has to create a Document object without database as place holder. This would explain the error prompt after "DOCINIT" is vetoed about "damaged drawing" (I found that if I check the "Always recover the drawing file", then the message would not pop up again when "DOCINIT" is vetoed again).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3. The "DOCINIT" is not a "regular" command that one can run at command line. That would be the reason that vetoes it would cause issues: besides the "Damaged drawing" popup message (which can be gotten rid of after first show by checking the check box, as I just mentioned above), there is no action allowed right after the vetoing. That is, this command is not designed to be "fully veto-able" as other regular command.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;4. With what I found, for the business case as you required, there is easy workaround to handle this. See the following code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;private static bool _runMyNewCommand = false;
private static bool _isDocInitCommand = false;
[CommandMethod("ForceMyTemplate")]
public static void HandleDocumentLockChange()
{
    CadApp.DocumentManager.DocumentLockModeChanged += DocumentManager_DocumentLockModeChanged;
}

private static void DocumentManager_DocumentLockModeChanged(object sender, DocumentLockModeChangedEventArgs e)
{
    if (_runMyNewCommand) return;

    var cmd = e.GlobalCommandName.ToUpper();
    if (cmd == "NEW" || cmd == "QNEW" || cmd == "DOCINIT")
    {
        var msg = "Open new drawing with project-specific template?";
        var res = MessageBox.Show(msg, "Open New Drawing Option",
            MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
        if (res == DialogResult.Yes || res == DialogResult.Cancel)
        {
            CadApp.DocumentManager.DocumentLockModeChangeVetoed += DocumentManager_DocumentLockModeChangeVetoed;
            if (res == DialogResult.Yes)
            {
                _runMyNewCommand = true;
                if (cmd =="DOCINIT")
                {
                    _isDocInitCommand = true;
                }
            }
            e.Veto();
        }
    }
}

private static void DocumentManager_DocumentLockModeChangeVetoed(object sender, DocumentLockModeChangeVetoedEventArgs e)
{
    CadApp.DocumentManager.DocumentLockModeChangeVetoed -= DocumentManager_DocumentLockModeChangeVetoed;
    if (_runMyNewCommand)
    {
        if (!_isDocInitCommand &amp;amp;&amp;amp; CadApp.DocumentManager.MdiActiveDocument != null)
        {
            e.Document.SendStringToExecute("MyNewCommand\n", false, false, false);
        }
        else 
        {
            CadApp.Idle += CadApp_Idle;
        }
    }
}

private static void CadApp_Idle(object sender, EventArgs e)
{
    if (_runMyNewCommand)
    {
        CadApp.Idle -= CadApp_Idle;
        OpenNewDrawingWithTemplate();
    }
}

[CommandMethod("MyNewCommand", CommandFlags.NoHistory)]
public static void OpenNewDrawingWithTemplate()
{
            
    MessageBox.Show("This is UI for opening new drawing with\nproject-specific templates");
    CadApp.DocumentManager.Add(@"C:\Temp\acad.dwt");
    _runMyNewCommand = false;
    _isDocInitCommand = false;
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Feb 2024 17:16:09 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/hijacking-new-button-functionality/m-p/12534847#M5721</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2024-02-01T17:16:09Z</dc:date>
    </item>
  </channel>
</rss>

