using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.Colors;
 
// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(Rivilis.SetSectionSettings))]
 
namespace Rivilis
{
  public class SetSectionSettings
  {
    [CommandMethod("SetSectionColor")]
    public void MyCommand()
    {
      Document doc = Application.DocumentManager.MdiActiveDocument;
      if (doc == null) return;
      Editor ed = doc.Editor;
 
      PromptEntityOptions peo = new PromptEntityOptions("\nВыберите сечение: ");
      peo.SetRejectMessage("\nЭто не сечение!");
      peo.AddAllowedClass(typeof(Section), true);
      PromptEntityResult per = ed.GetEntity(peo);
      if (per.Status != PromptStatus.OK) return;
      ObjectId idSecSets = ObjectId.Null;
      using (Transaction tr = doc.TransactionManager.StartOpenCloseTransaction())
      {
        Section sec = tr.GetObject(per.ObjectId, OpenMode.ForRead) as Section;
        idSecSets = sec.Settings;
        tr.Commit();
      }
      using (Transaction tr = doc.TransactionManager.StartOpenCloseTransaction())
      {
        SectionSettings secset = tr.GetObject(idSecSets, OpenMode.ForWrite) as SectionSettings;
        Color clr = secset.Color(SectionType.LiveSection, SectionGeometry.IntersectionFill);
        ColorDialog cd = new ColorDialog();
        cd.Color = clr;
        System.Windows.Forms.DialogResult dr = cd.ShowDialog();
        if (dr != System.Windows.Forms.DialogResult.OK) return;
        ed.WriteMessage("\nВыбран цвет: " + cd.Color.ToString());
        clr = cd.Color;
        // Определяет цвет чего мы меняем
        secset.SetColor(SectionType.LiveSection, SectionGeometry.IntersectionFill, clr);
        tr.Commit();
      }
      short _cmdecho = (short)Application.GetSystemVariable("CMDECHO");
      Application.SetSystemVariable("CMDECHO", 0);
      ed.Command("_LIVESECTION", per.ObjectId); // Отключаем LIVESECTION
      ed.Command("_LIVESECTION", per.ObjectId); // Включаем LIVESECTION
      Application.SetSystemVariable("CMDECHO", _cmdecho);
    }
  }
}