CommandMethod - "Unknown Command"

CommandMethod - "Unknown Command"

cadabyss
Participant Participant
2,455 Views
2 Replies
Message 1 of 3

CommandMethod - "Unknown Command"

cadabyss
Participant
Participant

Hello,

I am learning C# and am stuck on getting this simple example code (see below) to work in AutoCAD.  I am using Visual Studio Community 2019 and AutoCAD 2020. 

 

The code contains five different command methods:

ListLayers, CreateLayers, UpdateLayer, SetLayerOnOff, SetLayerFrozenThaw.

 

The problems is with SetLayerOnOFf and SetLayerFrozenThaw.  The other three command work perfectly.

 

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.  The other three commands work fine however.

 

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.  That was already done and we can eliminate that issue.  Some other people said to be sure the folder that the dll is located in is a secure folder via AutoCAD options.  I tried that but it didn't change anything. 

 

Anyone have any ideas on why the commands SetLayerOnOff and SetLayerFrozenThaw (below) do not initialize?

Thanks

 

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
}
0 Likes
Accepted solutions (1)
2,456 Views
2 Replies
Replies (2)
Message 2 of 3

_gile
Consultant
Consultant
Accepted solution

Hi,

 

I copied/pasted your code to make some tests and it worked as expected.

Assuming some commands work and some other does not, it looks like a conflict name on your computer.

 

Anyway, you could improve your code avoiding the whole layer table iteration by using the SymbolTable indexer.

 

if (lyTab.Has("Test"))
{
    LayerTableRecord lytr = trans.GetObject(lyTab["Test"], OpenMode.ForWrite) as LayerTableRecord;
    //...
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 3

cadabyss
Participant
Participant

@_gile wrote:

Hi,

I copied/pasted your code to make some tests and it worked as expected.

Assuming some commands work and some other does not, it looks like a conflict name on your computer.

...


Hi Gile,

 

Thank you for confirming that the code worked.  The key to solve this puzzle was your comment about name conflict.  I tried to rename the two CommandMethods to something I knew wouldn't be a conflict, but the result was the same: "Unknown command". 

 

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.  That experiment worked but I forgot to remove it.

 

So when I netloaded the newer code which contained SetLayerOnOFf and SetLayerFrozenThaw commandmethods, the older version was already loaded.  When I invoked the new commands, they didn't exist because they were not defined in the plugin version.  So, it appears the conflict was AutoCAD protecting the plugin version and ignoring the newer manually netloaded version.  I don't remember seeing any error message about the conflict from AutoCAD when I netloaded the newer version.

 


@_gile wrote:

...

Anyway, you could improve your code avoiding the whole layer table iteration by using the SymbolTable indexer.

 

if (lyTab.Has("Test"))
{
    LayerTableRecord lytr = trans.GetObject(lyTab["Test"], OpenMode.ForWrite) as LayerTableRecord;
    //...
}

 

Yes, the above method is much more efficient and I have incorporated it into my code.  Thanks!

 

-Steve