using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Data;
using CamBam.CAD;
using CamBam.Geom;
using CamBam.UI;

// Layers demo - EddyCurrent 2015

namespace Collision_Detector
{
    public class LayersDemo
    {
      public static void InitPlugin(CamBamUI ui)
        {
           ToolStripMenuItem LayersDemo = new ToolStripMenuItem();
            LayersDemo.Text = "Layers Demo";
            LayersDemo.Click += new EventHandler(plugin_clicked);
            ui.Menus.mnuPlugins.DropDownItems.Add(LayersDemo);
        }

        public static void plugin_clicked(object sender, EventArgs e)
        {
            LayersDemo2 mp1 = new LayersDemo2();
            mp1.MyMain();
        }
    }
}

    public class LayersDemo2
    {
        public static ICADView view = CamBamUI.MainUI.ActiveView;
        public CADFile file = view.CADFile;
      
        // declare some Layers
        Layer newLayer;
        Layer newLayer2;
        
        Circle circle1 = new Circle();
        Polyline poly1 = new Polyline();
        Point3F point1 = new Point3F(-10,5,0);
 
        public void MyMain()
        {
          // check to see if the named layer exists in current CADFile        
          if (file.HasLayer("Test_Layer1") == true)
            {
                // Each time the plugin runs it loses the handle of any layers
                // so to re-establish the handle, loop through all the layer names
                // until you come to the one you want and use something like; newLayer = exists_layer 
                // to reinstate the handle.
              foreach (Layer exists_layer in file.Layers)
              {
               if (exists_layer.Name == "Test_Layer1")
                  {
                   newLayer = exists_layer;
                   CamBam.ThisApplication.AddLogMessage("newLayer next " + "Name =  " + newLayer.Name + " Hash = " + newLayer.GetHashCode());
                  }
               }
            }
            else
            {
             newLayer = file.CreateLayer("Test_Layer1");
             CamBam.ThisApplication.AddLogMessage("newLayer 1st " + "Name " + newLayer.Name + " Hash " + newLayer.GetHashCode());
            }

         // various Layer methods, should be self explanatory by name. 
         newLayer2 = (Layer)newLayer.Clone();
         newLayer2.Name = "Test_Layer2";
         newLayer2.Color = Color.Cyan;
         newLayer2.PenWidth = 4;
         newLayer2.Tag = "Layer2";
         newLayer2.Visible = true;

        // add a layer to current CADFile
         file.Layers.Add(newLayer2);
         CamBam.ThisApplication.AddLogMessage("newLayer2 = " + "Name " + newLayer2.Name + " Hash " + newLayer2.GetHashCode());
         
         // set layer as the active layer
         file.SetActiveLayer("Test_Layer1");
         draw_circle();
            
         file.SetActiveLayer("Test_Layer2");
         draw_rectangle();
        }

        void draw_circle()
        {
            circle1.Center = point1;
            circle1.Diameter = 10;
            file.Add(circle1);
            view.RefreshView();
        }

        void draw_rectangle()
        {
            poly1.Add(0, 0, 0);
            poly1.Add(20, 0, 0);
            poly1.Add(20, 15, 0);
            poly1.Add(0, 15, 0);
            poly1.Closed = true;
            file.Add(poly1);
            view.RefreshView();
        }
  }