<?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 CommandMethod - &amp;quot;Unknown Command&amp;quot; in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/commandmethod-quot-unknown-command-quot/m-p/9107625#M20927</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I am learning C# and am stuck on getting this simple example code (see below) to work in AutoCAD.&amp;nbsp; I am using Visual Studio Community 2019 and AutoCAD 2020.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code contains five different command methods:&lt;/P&gt;&lt;P&gt;ListLayers, CreateLayers, UpdateLayer, SetLayerOnOff, SetLayerFrozenThaw.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problems is with SetLayerOnOFf and SetLayerFrozenThaw.&amp;nbsp; The other three command work perfectly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;After I NETLOAD the dll from a secure folder and then start the command by typing SetLayerOnOff or SetLayerFrozenThaw into the command prompt, AutoCAD returns an "Unknown Command" message.&amp;nbsp; The other three commands work fine however.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have checked the code several times comparing it to the exercise and I'm not seeing what's wrong. I have Googled this issue and some people have said to be sure "Copy Local" is set to False for references to AcCoreMgd, AcDbMgd, and AcMgd.&amp;nbsp; That was already done and we can eliminate that issue.&amp;nbsp; Some other people said to be sure the folder that the dll is located in is a secure folder via AutoCAD options.&amp;nbsp; I tried that but it didn't change anything.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyone have any ideas on why the commands SetLayerOnOff and SetLayerFrozenThaw (below) do not initialize?&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Colors;

namespace LayersLinetypesAndStyles
{
    public class LayersClass
    {
        [CommandMethod("SetLayerFrozenThaw")] // this doesn't work in AutoCAD
        public static void SetLayerFrozenThaw()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                try
                {
                    LayerTable lyTab = trans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    db.Clayer = lyTab["0"];
                    foreach (ObjectId lyID in lyTab)
                    {
                        LayerTableRecord lytr = trans.GetObject(lyID, OpenMode.ForRead) as LayerTableRecord;
                        if (lytr.Name == "Test")
                        {
                            lytr.UpgradeOpen();

                            // Freeze or thaw layer
                            lytr.IsFrozen = true;

                            // Commit transaction
                            trans.Commit();
                            doc.Editor.WriteMessage("\nLayer [" + lytr.Name + "] has been frozen");
                            break;
                        }
                        else
                        {
                            doc.Editor.WriteMessage("\nSkipping layer [" + lytr.Name + "] ");
                        }
                    }

                }
                catch (System.Exception ex)
                {
                    doc.Editor.WriteMessage("Error: " + ex.Message);
                    trans.Abort();
                }
            }
        } // End of SetLayerFrozenThaw

        [CommandMethod("SetLayerOnOff")] // this doesn't work either
        public static void SetLayerOnOff()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                try
                {
                    LayerTable lyTab = trans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    db.Clayer = lyTab["0"];
                    foreach (ObjectId lyID in lyTab)
                    {
                        LayerTableRecord lytr = trans.GetObject(lyID, OpenMode.ForRead) as LayerTableRecord;
                        if (lytr.Name == "Test")
                        {
                            lytr.UpgradeOpen();

                            // Turn layer ON or Off
                            lytr.IsOff = true;

                            // Commit transaction
                            trans.Commit();
                            doc.Editor.WriteMessage("\nLayer [" + lytr.Name + "] has been turned off");
                            break;
                        }
                        else
                        {
                            doc.Editor.WriteMessage("\nSkipping layer [" + lytr.Name + "] ");
                        }
                    }

                }
                catch (System.Exception ex)
                {
                    doc.Editor.WriteMessage("Error: " + ex.Message);
                    trans.Abort();
                }
            }
        } // End of SetLayerOnOff


        [CommandMethod("UpdateLayer")]
        public static void UpdateLayer()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                try
                {
                    LayerTable lyTab = trans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    foreach (ObjectId lyID in lyTab)
                    {
                        LayerTableRecord lytr = trans.GetObject(lyID, OpenMode.ForRead) as LayerTableRecord;
                        if (lytr.Name == "Test")
                        {
                            lytr.UpgradeOpen();

                            // Update color to yellow
                            lytr.Color = Color.FromColorIndex(ColorMethod.ByLayer, 2);

                            //Update linetype to hidden
                            LinetypeTable ltTab = trans.GetObject(db.LinetypeTableId, OpenMode.ForRead) as LinetypeTable;
                            if (ltTab.Has("Hidden") == true)
                            {
                                lytr.LinetypeObjectId = ltTab["Hidden"];
                            }

                            // Commit transaction
                            trans.Commit();
                            doc.Editor.WriteMessage("\nUpdated layer [" + lytr.Name + "] ");
                            break;
                        }
                        else
                        {
                            doc.Editor.WriteMessage("\nSkipping layer [" + lytr.Name + "] ");
                        }
                    }

                }
                catch (System.Exception ex)
                {
                    doc.Editor.WriteMessage("Error: " + ex.Message);
                    trans.Abort();
                }             
            }
        } // End of UpdateLayer


        [CommandMethod("CreateLayer")]
        public static void CreateLayer()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                LayerTable lyTab = trans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                if (lyTab.Has("Test"))
                {
                    doc.Editor.WriteMessage("Layer already exist.");
                    trans.Abort();
                }
                else
                {
                    lyTab.UpgradeOpen();
                    LayerTableRecord ltr = new LayerTableRecord();
                    ltr.Name = "Test";
                    ltr.Color = Color.FromColorIndex(ColorMethod.ByLayer, 1); // Red
                    lyTab.Add(ltr);
                    trans.AddNewlyCreatedDBObject(ltr, true);

                    db.Clayer = lyTab["Test"];

                    doc.Editor.WriteMessage("Layer [" + ltr.Name + "] was created sucessfully.");

                    // Commit the transaction
                    trans.Commit();

                }
                
            }
        } // End of CreateLayer


        [CommandMethod("ListLayers")]
        public static void ListLayers()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                LayerTable lyTab = trans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                foreach (ObjectId lyID in lyTab)
                {
                    LayerTableRecord lytr = trans.GetObject(lyID, OpenMode.ForRead) as LayerTableRecord;
                    doc.Editor.WriteMessage("\nLayer name: " + lytr.Name);
                }

                // Commit the transaction
                trans.Commit();
            }
        }
        
    } // End of ListLayers
}&lt;/PRE&gt;</description>
    <pubDate>Fri, 25 Oct 2019 03:05:20 GMT</pubDate>
    <dc:creator>cadabyss</dc:creator>
    <dc:date>2019-10-25T03:05:20Z</dc:date>
    <item>
      <title>CommandMethod - "Unknown Command"</title>
      <link>https://forums.autodesk.com/t5/net-forum/commandmethod-quot-unknown-command-quot/m-p/9107625#M20927</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I am learning C# and am stuck on getting this simple example code (see below) to work in AutoCAD.&amp;nbsp; I am using Visual Studio Community 2019 and AutoCAD 2020.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code contains five different command methods:&lt;/P&gt;&lt;P&gt;ListLayers, CreateLayers, UpdateLayer, SetLayerOnOff, SetLayerFrozenThaw.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problems is with SetLayerOnOFf and SetLayerFrozenThaw.&amp;nbsp; The other three command work perfectly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;After I NETLOAD the dll from a secure folder and then start the command by typing SetLayerOnOff or SetLayerFrozenThaw into the command prompt, AutoCAD returns an "Unknown Command" message.&amp;nbsp; The other three commands work fine however.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have checked the code several times comparing it to the exercise and I'm not seeing what's wrong. I have Googled this issue and some people have said to be sure "Copy Local" is set to False for references to AcCoreMgd, AcDbMgd, and AcMgd.&amp;nbsp; That was already done and we can eliminate that issue.&amp;nbsp; Some other people said to be sure the folder that the dll is located in is a secure folder via AutoCAD options.&amp;nbsp; I tried that but it didn't change anything.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyone have any ideas on why the commands SetLayerOnOff and SetLayerFrozenThaw (below) do not initialize?&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Colors;

namespace LayersLinetypesAndStyles
{
    public class LayersClass
    {
        [CommandMethod("SetLayerFrozenThaw")] // this doesn't work in AutoCAD
        public static void SetLayerFrozenThaw()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                try
                {
                    LayerTable lyTab = trans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    db.Clayer = lyTab["0"];
                    foreach (ObjectId lyID in lyTab)
                    {
                        LayerTableRecord lytr = trans.GetObject(lyID, OpenMode.ForRead) as LayerTableRecord;
                        if (lytr.Name == "Test")
                        {
                            lytr.UpgradeOpen();

                            // Freeze or thaw layer
                            lytr.IsFrozen = true;

                            // Commit transaction
                            trans.Commit();
                            doc.Editor.WriteMessage("\nLayer [" + lytr.Name + "] has been frozen");
                            break;
                        }
                        else
                        {
                            doc.Editor.WriteMessage("\nSkipping layer [" + lytr.Name + "] ");
                        }
                    }

                }
                catch (System.Exception ex)
                {
                    doc.Editor.WriteMessage("Error: " + ex.Message);
                    trans.Abort();
                }
            }
        } // End of SetLayerFrozenThaw

        [CommandMethod("SetLayerOnOff")] // this doesn't work either
        public static void SetLayerOnOff()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                try
                {
                    LayerTable lyTab = trans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    db.Clayer = lyTab["0"];
                    foreach (ObjectId lyID in lyTab)
                    {
                        LayerTableRecord lytr = trans.GetObject(lyID, OpenMode.ForRead) as LayerTableRecord;
                        if (lytr.Name == "Test")
                        {
                            lytr.UpgradeOpen();

                            // Turn layer ON or Off
                            lytr.IsOff = true;

                            // Commit transaction
                            trans.Commit();
                            doc.Editor.WriteMessage("\nLayer [" + lytr.Name + "] has been turned off");
                            break;
                        }
                        else
                        {
                            doc.Editor.WriteMessage("\nSkipping layer [" + lytr.Name + "] ");
                        }
                    }

                }
                catch (System.Exception ex)
                {
                    doc.Editor.WriteMessage("Error: " + ex.Message);
                    trans.Abort();
                }
            }
        } // End of SetLayerOnOff


        [CommandMethod("UpdateLayer")]
        public static void UpdateLayer()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                try
                {
                    LayerTable lyTab = trans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    foreach (ObjectId lyID in lyTab)
                    {
                        LayerTableRecord lytr = trans.GetObject(lyID, OpenMode.ForRead) as LayerTableRecord;
                        if (lytr.Name == "Test")
                        {
                            lytr.UpgradeOpen();

                            // Update color to yellow
                            lytr.Color = Color.FromColorIndex(ColorMethod.ByLayer, 2);

                            //Update linetype to hidden
                            LinetypeTable ltTab = trans.GetObject(db.LinetypeTableId, OpenMode.ForRead) as LinetypeTable;
                            if (ltTab.Has("Hidden") == true)
                            {
                                lytr.LinetypeObjectId = ltTab["Hidden"];
                            }

                            // Commit transaction
                            trans.Commit();
                            doc.Editor.WriteMessage("\nUpdated layer [" + lytr.Name + "] ");
                            break;
                        }
                        else
                        {
                            doc.Editor.WriteMessage("\nSkipping layer [" + lytr.Name + "] ");
                        }
                    }

                }
                catch (System.Exception ex)
                {
                    doc.Editor.WriteMessage("Error: " + ex.Message);
                    trans.Abort();
                }             
            }
        } // End of UpdateLayer


        [CommandMethod("CreateLayer")]
        public static void CreateLayer()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                LayerTable lyTab = trans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                if (lyTab.Has("Test"))
                {
                    doc.Editor.WriteMessage("Layer already exist.");
                    trans.Abort();
                }
                else
                {
                    lyTab.UpgradeOpen();
                    LayerTableRecord ltr = new LayerTableRecord();
                    ltr.Name = "Test";
                    ltr.Color = Color.FromColorIndex(ColorMethod.ByLayer, 1); // Red
                    lyTab.Add(ltr);
                    trans.AddNewlyCreatedDBObject(ltr, true);

                    db.Clayer = lyTab["Test"];

                    doc.Editor.WriteMessage("Layer [" + ltr.Name + "] was created sucessfully.");

                    // Commit the transaction
                    trans.Commit();

                }
                
            }
        } // End of CreateLayer


        [CommandMethod("ListLayers")]
        public static void ListLayers()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                LayerTable lyTab = trans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                foreach (ObjectId lyID in lyTab)
                {
                    LayerTableRecord lytr = trans.GetObject(lyID, OpenMode.ForRead) as LayerTableRecord;
                    doc.Editor.WriteMessage("\nLayer name: " + lytr.Name);
                }

                // Commit the transaction
                trans.Commit();
            }
        }
        
    } // End of ListLayers
}&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Oct 2019 03:05:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/commandmethod-quot-unknown-command-quot/m-p/9107625#M20927</guid>
      <dc:creator>cadabyss</dc:creator>
      <dc:date>2019-10-25T03:05:20Z</dc:date>
    </item>
    <item>
      <title>Re: CommandMethod - "Unknown Command"</title>
      <link>https://forums.autodesk.com/t5/net-forum/commandmethod-quot-unknown-command-quot/m-p/9107726#M20928</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I copied/pasted your code to make some tests and it worked as expected.&lt;/P&gt;
&lt;P&gt;Assuming some commands work and some other does not, it looks like a conflict name on your computer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyway, you could improve your code avoiding the whole layer table iteration by using the SymbolTable &lt;A href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/indexers/" target="_blank" rel="noopener"&gt;indexer&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;if (lyTab.Has("Test"))
{
    LayerTableRecord lytr = trans.GetObject(lyTab["Test"], OpenMode.ForWrite) as LayerTableRecord;
    //...
}&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Oct 2019 05:33:24 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/commandmethod-quot-unknown-command-quot/m-p/9107726#M20928</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2019-10-25T05:33:24Z</dc:date>
    </item>
    <item>
      <title>Re: CommandMethod - "Unknown Command"</title>
      <link>https://forums.autodesk.com/t5/net-forum/commandmethod-quot-unknown-command-quot/m-p/9108741#M20929</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I copied/pasted your code to make some tests and it worked as expected.&lt;/P&gt;&lt;P&gt;Assuming some commands work and some other does not, it looks like a conflict name on your computer.&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Hi Gile,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for confirming that the code worked.&amp;nbsp; The key to solve this puzzle was your comment about name conflict.&amp;nbsp; I tried to rename the two CommandMethods to something I knew wouldn't be a conflict, but the result was the same: "Unknown command".&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then it dawned on me that a couple days ago, I had experimented with using AutoLoader and ApplicationPlugins folder to automatically load an earlier version of this dll file that did not contain the SetLayerOnOFf and SetLayerFrozenThaw commandmethods.&amp;nbsp; That experiment worked but I forgot to remove it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So when I netloaded the newer code which contained SetLayerOnOFf and SetLayerFrozenThaw commandmethods, the older version was already loaded.&amp;nbsp; When I invoked the new commands, they didn't exist because they were not defined in the plugin version.&amp;nbsp; So, it appears the conflict was AutoCAD protecting the plugin version and ignoring the newer manually netloaded version.&amp;nbsp; I don't remember seeing any error message about the conflict from AutoCAD when I netloaded the newer version.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;Anyway, you could improve your code avoiding the whole layer table iteration by using the SymbolTable &lt;A href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/indexers/" target="_blank" rel="noopener"&gt;indexer&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;if (lyTab.Has("Test"))
{
    LayerTableRecord lytr = trans.GetObject(lyTab["Test"], OpenMode.ForWrite) as LayerTableRecord;
    //...
}&lt;/PRE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes, the above method is much more efficient and I have incorporated it into my code.&amp;nbsp; Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;-Steve&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Fri, 25 Oct 2019 13:34:13 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/commandmethod-quot-unknown-command-quot/m-p/9108741#M20929</guid>
      <dc:creator>cadabyss</dc:creator>
      <dc:date>2019-10-25T13:34:13Z</dc:date>
    </item>
  </channel>
</rss>

