using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
[assembly: CommandClass(typeof(Rivilis.LayerHidden))]
namespace Rivilis
{
public class LayerHidden
{
/// <summary>
/// Изменение свойства слою Hidden на противоположное
/// </summary>
[CommandMethod("LayHide")]
public void LayHide()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
PromptStringOptions pr = new PromptStringOptions("\nУкажите имя слоя: ");
pr.AllowSpaces = true;
PromptResult res = ed.GetString(pr);
if (res.Status != PromptStatus.OK) return;
using (LayerTable lt = doc.Database.LayerTableId.Open(OpenMode.ForRead) as LayerTable) {
if (lt.Has(res.StringResult)) {
using (LayerTableRecord ltr = lt[res.StringResult].Open(OpenMode.ForWrite, true) as LayerTableRecord) {
ltr.IsHidden = !ltr.IsHidden;
}
} else if (lt.Has("*"+res.StringResult)) {
using (LayerTableRecord ltr = lt["*"+res.StringResult].Open(OpenMode.ForWrite, true) as LayerTableRecord) {
ltr.IsHidden = !ltr.IsHidden;
}
} else {
using (LayerTableRecord ltr = new LayerTableRecord()) {
lt.UpgradeOpen();
ltr.Name = res.StringResult;
lt.Add(ltr); ltr.IsHidden = true;
lt.DowngradeOpen();
}
}
}
}
/// <summary>
/// Установка примитиву анонимного слоя
/// </summary>
[CommandMethod("SetLay")]
public void SetLay()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
PromptStringOptions pr = new PromptStringOptions("\nУкажите имя слоя: ");
pr.AllowSpaces = true;
PromptResult res = ed.GetString(pr);
if (res.Status != PromptStatus.OK) return;
PromptEntityOptions pre = new PromptEntityOptions("\nВыберите примитив: ");
pre.AllowNone = false;
PromptEntityResult rese = ed.GetEntity(pre);
if (rese.Status != PromptStatus.OK) return;
using (LayerTable lt = doc.Database.LayerTableId.Open(OpenMode.ForRead) as LayerTable) {
if (lt.Has("*" + res.StringResult)) {
ObjectId layId = lt["*" + res.StringResult];
using (Entity ent = rese.ObjectId.Open(OpenMode.ForWrite) as Entity) {
using (LayerTableRecord ltr = layId.Open(OpenMode.ForWrite, true) as LayerTableRecord) {
ltr.IsHidden = false;
}
ent.LayerId = layId;
using (LayerTableRecord ltr = layId.Open(OpenMode.ForWrite, true) as LayerTableRecord) {
ltr.IsHidden = true;
}
}
} else {
ed.WriteMessage("\nНет анонимного слоя <{0}>", res.StringResult);
}
}
}
}
}