<?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 c# - detect command input to set correct current layer in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/c-detect-command-input-to-set-correct-current-layer/m-p/9418561#M19896</link>
    <description>&lt;P&gt;Very simplified, what i've got is the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A userform:&lt;BR /&gt;At one place in the form you can set the layer for lines.&amp;nbsp;&lt;/P&gt;&lt;P&gt;At another place in the form you can set the layer for circles.&amp;nbsp;&lt;BR /&gt;At another place in the form you can set the for blocks.&amp;nbsp;&lt;/P&gt;&lt;P&gt;and so on....&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now, what I want is: (when a certain checkbox is checked so the userform should be active) the form detecting when the Autocad-user is clicking the 'line' button or the 'circle'-button from the standard Autocad Toolbars, or maybe types "insert" to insert a block, so the current layer can be set to wanted layer.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I created a while loop which is active as long as the checkbox is checked. In this while-loop I get the last inserted command (by&amp;nbsp;&lt;SPAN&gt;AutoCADAppServices.&lt;/SPAN&gt;&lt;SPAN&gt;Application&lt;/SPAN&gt;&lt;SPAN&gt;.GetSystemVariable(&lt;/SPAN&gt;&lt;SPAN&gt;"CMDNAMES"&lt;/SPAN&gt;&lt;SPAN&gt;), convert it to a string, and do the 'switch' lines on the different cases (last command is _line then set layer to a, last command is _circle then set layer to b, last command is _insert then set layer to c).&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2 questions:&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1) is the 'switch'way the right way to do this?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2) (maybe not fully autocad-related): how can i prevent the code from getting stuck in the while-loop, so I cant click any commands anymore (nor uncheck the checkbox or do anything else).&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Any help would be appreciated, I'm a little stuck on this the last days, can't get it to work nor I can check if at least i'm getting closer (because of while-loop getting stuck (which by the way makes perfect sense to me but I don't know how to prevent it)).&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 02 Apr 2020 19:33:07 GMT</pubDate>
    <dc:creator>stefanveurink68AXD</dc:creator>
    <dc:date>2020-04-02T19:33:07Z</dc:date>
    <item>
      <title>c# - detect command input to set correct current layer</title>
      <link>https://forums.autodesk.com/t5/net-forum/c-detect-command-input-to-set-correct-current-layer/m-p/9418561#M19896</link>
      <description>&lt;P&gt;Very simplified, what i've got is the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A userform:&lt;BR /&gt;At one place in the form you can set the layer for lines.&amp;nbsp;&lt;/P&gt;&lt;P&gt;At another place in the form you can set the layer for circles.&amp;nbsp;&lt;BR /&gt;At another place in the form you can set the for blocks.&amp;nbsp;&lt;/P&gt;&lt;P&gt;and so on....&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now, what I want is: (when a certain checkbox is checked so the userform should be active) the form detecting when the Autocad-user is clicking the 'line' button or the 'circle'-button from the standard Autocad Toolbars, or maybe types "insert" to insert a block, so the current layer can be set to wanted layer.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I created a while loop which is active as long as the checkbox is checked. In this while-loop I get the last inserted command (by&amp;nbsp;&lt;SPAN&gt;AutoCADAppServices.&lt;/SPAN&gt;&lt;SPAN&gt;Application&lt;/SPAN&gt;&lt;SPAN&gt;.GetSystemVariable(&lt;/SPAN&gt;&lt;SPAN&gt;"CMDNAMES"&lt;/SPAN&gt;&lt;SPAN&gt;), convert it to a string, and do the 'switch' lines on the different cases (last command is _line then set layer to a, last command is _circle then set layer to b, last command is _insert then set layer to c).&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2 questions:&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1) is the 'switch'way the right way to do this?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2) (maybe not fully autocad-related): how can i prevent the code from getting stuck in the while-loop, so I cant click any commands anymore (nor uncheck the checkbox or do anything else).&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Any help would be appreciated, I'm a little stuck on this the last days, can't get it to work nor I can check if at least i'm getting closer (because of while-loop getting stuck (which by the way makes perfect sense to me but I don't know how to prevent it)).&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Apr 2020 19:33:07 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/c-detect-command-input-to-set-correct-current-layer/m-p/9418561#M19896</guid>
      <dc:creator>stefanveurink68AXD</dc:creator>
      <dc:date>2020-04-02T19:33:07Z</dc:date>
    </item>
    <item>
      <title>Re: c# - detect command input to set correct current layer</title>
      <link>https://forums.autodesk.com/t5/net-forum/c-detect-command-input-to-set-correct-current-layer/m-p/9418789#M19897</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe you can handle the Editor.CommandWillStart event to set the layer according to the CommandEventArgs.GlobalCommandName value and handle the Editor.CommandEnded / CommandCancelled / CommandFailed to restore the previuous current layer.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Apr 2020 21:19:48 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/c-detect-command-input-to-set-correct-current-layer/m-p/9418789#M19897</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2020-04-02T21:19:48Z</dc:date>
    </item>
    <item>
      <title>Re: c# - detect command input to set correct current layer</title>
      <link>https://forums.autodesk.com/t5/net-forum/c-detect-command-input-to-set-correct-current-layer/m-p/9421153#M19898</link>
      <description>&lt;P&gt;Seems very helpfull, including&amp;nbsp;&lt;A href="https://forums.autodesk.com/t5/net/addhandler-commandwillstart-removehandler-commandwillstart-not/td-p/5867570" target="_blank" rel="noopener"&gt;this&lt;/A&gt;&amp;nbsp;topic a found as well as the code that's referred to by Norman Yuan&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: CommandClass(typeof(CommandWillStartHandler.Commands))]
[assembly: ExtensionApplication(typeof(CommandWillStartHandler.Commands))]

namespace CommandWillStartHandler
{
    public class Commands : IExtensionApplication
    {
        private static string _savedLayer = null;
        private const string LINE_LAYER = "MyLine";

        #region IExtensionApplication Interface

        public void Initialize()
        {
            Document dwg = CadApp.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;

            try
            {
                ed.WriteMessage("\nInitialising custom add-in...");
                AddCommandHandler();
                ed.WriteMessage("\nInitializing custom add-in completed.");
            }
            catch
            {
                ed.WriteMessage("\nInitializing custom add-in failed");
            }
            finally
            {
                Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
            }
        }

        public void Terminate()
        {

        }

        private void AddCommandHandler()
        {
            CadApp.DocumentManager.DocumentCreated += DocumentManager_DocumentCreated;

            foreach (Document d in CadApp.DocumentManager)
            {
                d.CommandWillStart += Document_CommandWillStart;
                d.CommandEnded += Document_CommandEnded;
                d.CommandCancelled += Document_CommandCancelled;
            }
        }

        private void DocumentManager_DocumentCreated(object sender, DocumentCollectionEventArgs e)
        {
            e.Document.CommandWillStart += Document_CommandWillStart;
            e.Document.CommandEnded += Document_CommandEnded;
            e.Document.CommandCancelled += Document_CommandCancelled;
        }

        private void Document_CommandEnded(object sender, CommandEventArgs e)
        {
            Document doc = CadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;

            ed.WriteMessage("\nCommand {0} ended.", e.GlobalCommandName);

            if (!string.IsNullOrEmpty(_savedLayer))
            {
                CadApp.SetSystemVariable("CLAYER", _savedLayer);
                _savedLayer = null;
            }
        }

        private void Document_CommandCancelled(object sender, CommandEventArgs e)
        {
            Document doc = CadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;

            ed.WriteMessage("\nCommand {0} cancelled.", e.GlobalCommandName);

            if (!string.IsNullOrEmpty(_savedLayer))
            {
                CadApp.SetSystemVariable("CLAYER", _savedLayer);
                _savedLayer = null;
            }
        }

        private void Document_CommandWillStart(object sender, CommandEventArgs e)
        {
            Document doc = CadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;

            ed.WriteMessage("\nCommand {0} will start.", e.GlobalCommandName);

            //Whenever "LINE" command starts, the active layer is set to
            //LINE_LAYER so that the line is drawn on that layer
            if (e.GlobalCommandName.ToUpper() == "LINE")
            {
                _savedLayer = CadApp.GetSystemVariable("CLAYER").ToString();
                CadApp.SetSystemVariable("CLAYER", LINE_LAYER);
            }
        }

        #endregion
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However when I literally insert this code into a new project and start debugging, nothing happens, saying, when I draw a line its not on the asked layer.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any idea what could be wrong?&lt;/P&gt;</description>
      <pubDate>Fri, 03 Apr 2020 20:24:40 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/c-detect-command-input-to-set-correct-current-layer/m-p/9421153#M19898</guid>
      <dc:creator>stefanveurink68AXD</dc:creator>
      <dc:date>2020-04-03T20:24:40Z</dc:date>
    </item>
    <item>
      <title>Re: c# - detect command input to set correct current layer</title>
      <link>https://forums.autodesk.com/t5/net-forum/c-detect-command-input-to-set-correct-current-layer/m-p/9421231#M19899</link>
      <description>&lt;P&gt;This works for me:&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;

using System;

using AcAp = Autodesk.AutoCAD.ApplicationServices.Core.Application;

namespace RegionToPolyline
{
    public class Initialization : IExtensionApplication
    {
        string prevLayer;
        const string LINE_LAYER = "MyLine";

        public void Initialize()
        {
            AcAp.Idle += OnIdle;
        }

        public void Terminate()
        { }

        private void OnIdle(object sender, EventArgs e)
        {
            AcAp.Idle -= OnIdle;
            var docs = AcAp.DocumentManager;
            foreach (Document doc in docs)
            {
                AddHandlers(doc);
            }
            docs.DocumentCreated += DocumentCreated;
        }

        private void AddHandlers(Document doc)
        {
            doc.CommandEnded += CommandEnded;
            doc.CommandCancelled += CommandEnded;
            doc.CommandFailed += CommandEnded;
            doc.CommandWillStart += CommandWillStart;
        }

        private void DocumentCreated(object sender, DocumentCollectionEventArgs e)
        {
            AddHandlers(e.Document);
        }

        private void CommandWillStart(object sender, CommandEventArgs e)
        {
            if (e.GlobalCommandName == "LINE")
            {
                prevLayer = AcAp.GetSystemVariable("clayer").ToString();
                AcAp.SetSystemVariable("clayer", LINE_LAYER);
            }
        }

        private void CommandEnded(object sender, CommandEventArgs e)
        {
            if (!string.IsNullOrEmpty(prevLayer))
            {
                AcAp.SetSystemVariable("clayer", prevLayer);
                prevLayer = null;
            }
        }
    }
}
&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 03 Apr 2020 21:18:10 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/c-detect-command-input-to-set-correct-current-layer/m-p/9421231#M19899</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2020-04-03T21:18:10Z</dc:date>
    </item>
    <item>
      <title>Re: c# - detect command input to set correct current layer</title>
      <link>https://forums.autodesk.com/t5/net-forum/c-detect-command-input-to-set-correct-current-layer/m-p/9422325#M19900</link>
      <description>&lt;P&gt;Must be something wrong with my C# skills then, I'm just starting to learn this language.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I did was put your code into a new class file in the project, with same namespace as the others.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then in the main function I create an instance of your class and call the instance.initialize(); function from your class.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;then at debugging, start autocad, netload to load the .dll, and startup the form. but nothing happens when drawing a line. layer stays zero. What am I forgetting?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Is this what I should do? Cause it still isn't working with me. Neither is just start an entire new project with your code because this give all kind of errors (expecting formload and stuff like that).&amp;nbsp; (By the way i also combined the initialize() and terminate() with the checkbox but didn't work either)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 04 Apr 2020 18:26:00 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/c-detect-command-input-to-set-correct-current-layer/m-p/9422325#M19900</guid>
      <dc:creator>stefanveurink68AXD</dc:creator>
      <dc:date>2020-04-04T18:26:00Z</dc:date>
    </item>
    <item>
      <title>Re: c# - detect command input to set correct current layer</title>
      <link>https://forums.autodesk.com/t5/net-forum/c-detect-command-input-to-set-correct-current-layer/m-p/9422365#M19901</link>
      <description>&lt;P&gt;You do not have to create an instance of the class which implements IExtensionApplication. The Initialize() method of this class runs automatically when the application is loaded, i.e. it adds the handlers. Note thats this is not realted to your form.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a more robust implementation in case the "MyLine" layer does not exists.&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;

using AcAp = Autodesk.AutoCAD.ApplicationServices.Core.Application;

namespace CommandWillStartHandler
{
    public class Initialization : IExtensionApplication
    {
        string prevLayer;
        const string LINE_LAYER = "MyLine";

        public void Initialize()
        {
            var docs = AcAp.DocumentManager;
            foreach (Document doc in docs)
            {
                AddHandlers(doc);
            }
            docs.DocumentCreated += DocumentCreated;
        }

        public void Terminate()
        { }

        private void DocumentCreated(object sender, DocumentCollectionEventArgs e)
        {
            AddHandlers(e.Document);
        }

        private void CommandWillStart(object sender, CommandEventArgs e)
        {
            if (e.GlobalCommandName == "LINE")
            {
                prevLayer = (string)AcAp.GetSystemVariable("clayer");
                try { AcAp.SetSystemVariable("clayer", LINE_LAYER); }
                catch { prevLayer = null; }
            }
        }

        private void CommandEnded(object sender, CommandEventArgs e)
        {
            if (!string.IsNullOrEmpty(prevLayer))
            {
                AcAp.SetSystemVariable("clayer", prevLayer);
                prevLayer = null;
            }
        }

        private void AddHandlers(Document doc)
        {
            doc.CommandEnded += CommandEnded;
            doc.CommandCancelled += CommandEnded;
            doc.CommandFailed += CommandEnded;
            doc.CommandWillStart += CommandWillStart;
        }
    }
}
&lt;/LI-CODE&gt;</description>
      <pubDate>Sat, 04 Apr 2020 18:54:27 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/c-detect-command-input-to-set-correct-current-layer/m-p/9422365#M19901</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2020-04-04T18:54:27Z</dc:date>
    </item>
    <item>
      <title>Re: c# - detect command input to set correct current layer</title>
      <link>https://forums.autodesk.com/t5/net-forum/c-detect-command-input-to-set-correct-current-layer/m-p/9422382#M19902</link>
      <description>&lt;P&gt;awesome! thanks a lot!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Indeed it seems like I don't have to create an instance of the class. One last short question therefore, if I want to put this into a checkbox, can I still do this by initialize when checked and terminate when not checked == false? Cause if I don't have to create an instance.... i don't see how it's turned on and off?&lt;/P&gt;</description>
      <pubDate>Sat, 04 Apr 2020 19:07:24 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/c-detect-command-input-to-set-correct-current-layer/m-p/9422382#M19902</guid>
      <dc:creator>stefanveurink68AXD</dc:creator>
      <dc:date>2020-04-04T19:07:24Z</dc:date>
    </item>
    <item>
      <title>Re: c# - detect command input to set correct current layer</title>
      <link>https://forums.autodesk.com/t5/net-forum/c-detect-command-input-to-set-correct-current-layer/m-p/9422394#M19903</link>
      <description>&lt;P&gt;If you want to activate / deactivate the event handlers, you must not do all this stuff from the Initialze() method but from the handler of your CheckBox.&lt;/P&gt;
&lt;P&gt;Something like this:&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;        private void CheckBox1_CheckedChanged(Object sender, EventArgs e)
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            if (checkBox1.Checked)
            {
                doc.CommandEnded += CommandEnded;
                doc.CommandCancelled += CommandEnded;
                doc.CommandFailed += CommandEnded;
                doc.CommandWillStart += CommandWillStart;
            }
            else
            {
                doc.CommandEnded -= CommandEnded;
                doc.CommandCancelled -= CommandEnded;
                doc.CommandFailed -= CommandEnded;
                doc.CommandWillStart -= CommandWillStart;
            }
        }&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 04 Apr 2020 19:31:00 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/c-detect-command-input-to-set-correct-current-layer/m-p/9422394#M19903</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2020-04-04T19:31:00Z</dc:date>
    </item>
    <item>
      <title>Re: c# - detect command input to set correct current layer</title>
      <link>https://forums.autodesk.com/t5/net-forum/c-detect-command-input-to-set-correct-current-layer/m-p/9422444#M19904</link>
      <description>&lt;P&gt;Works Perfect. Thanks!&lt;/P&gt;</description>
      <pubDate>Sat, 04 Apr 2020 20:18:42 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/c-detect-command-input-to-set-correct-current-layer/m-p/9422444#M19904</guid>
      <dc:creator>stefanveurink68AXD</dc:creator>
      <dc:date>2020-04-04T20:18:42Z</dc:date>
    </item>
  </channel>
</rss>

