- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
}
Solved! Go to Solution.