//#define __LIGHT_VERSION__
#if (__LIGHT_VERSION__)
	using Kompas6LTAPI5;
#else
	using Kompas6API5;
#endif

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Kompas6Constants;
using Kompas6Constants3D;


namespace Steps.NET
{
	public class FrmActiveX : System.Windows.Forms.Form
	{
		#region Designer declarations
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Panel leftpanel;
		private System.Windows.Forms.Splitter splitter;
		private System.Windows.Forms.Panel rightpanel;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button btn3dApply;
		private System.Windows.Forms.Button btn3dStop;
		private System.Windows.Forms.Button btn2dStop;
		private System.Windows.Forms.Button btn2dApply;
		private System.Windows.Forms.Button btn3dRunCommand;
		private System.Windows.Forms.Button btnShowAll;
		private System.Windows.Forms.Button btnOrientation;
		private System.Windows.Forms.Button btn2dRunCommand;
		private System.Windows.Forms.Button btnRotate;
		private System.Windows.Forms.Button btnPanorama;
		private System.Windows.Forms.Button btnMove;
		private System.Windows.Forms.Button btnShaded;
		private System.Windows.Forms.Button btnHiddenRemoved;
    private AxKGAXLib.AxKGAX axKGAX3D;
    private AxKGAXLib.AxKGAX axKGAX2D;
		private System.Windows.Forms.Button btnWireframe;
		#endregion

		#region Instance etc...
		private FrmActiveX()
		{
			InitializeComponent();
			
			axKGAX2D.OnKgMouseDown += new AxKGAXLib._DKGAXEvents_OnKgMouseDownEventHandler(axKGAX2D_OnKgMouseDown);
			axKGAX3D.OnKgStopCurrentProcess += new EventHandler(KGAX3D_OnKgStopCurrentProcess);
			axKGAX3D.OnKgMouseDown += new AxKGAXLib._DKGAXEvents_OnKgMouseDownEventHandler(axKGAX2D_OnKgMouseDown);
		}

		private static FrmActiveX instance;
		public static FrmActiveX Instance
		{
			get
			{
				if (instance == null)
					instance = new FrmActiveX();
				return instance;

			}
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FrmActiveX));
      this.leftpanel = new System.Windows.Forms.Panel();
      this.axKGAX3D = new AxKGAXLib.AxKGAX();
      this.btnShaded = new System.Windows.Forms.Button();
      this.btnHiddenRemoved = new System.Windows.Forms.Button();
      this.btnWireframe = new System.Windows.Forms.Button();
      this.btnRotate = new System.Windows.Forms.Button();
      this.btnPanorama = new System.Windows.Forms.Button();
      this.btnMove = new System.Windows.Forms.Button();
      this.btnOrientation = new System.Windows.Forms.Button();
      this.btnShowAll = new System.Windows.Forms.Button();
      this.btn3dRunCommand = new System.Windows.Forms.Button();
      this.btn3dStop = new System.Windows.Forms.Button();
      this.label1 = new System.Windows.Forms.Label();
      this.btn3dApply = new System.Windows.Forms.Button();
      this.splitter = new System.Windows.Forms.Splitter();
      this.rightpanel = new System.Windows.Forms.Panel();
      this.axKGAX2D = new AxKGAXLib.AxKGAX();
      this.btn2dStop = new System.Windows.Forms.Button();
      this.label2 = new System.Windows.Forms.Label();
      this.btn2dApply = new System.Windows.Forms.Button();
      this.btn2dRunCommand = new System.Windows.Forms.Button();
      this.leftpanel.SuspendLayout();
      ((System.ComponentModel.ISupportInitialize)(this.axKGAX3D)).BeginInit();
      this.rightpanel.SuspendLayout();
      ((System.ComponentModel.ISupportInitialize)(this.axKGAX2D)).BeginInit();
      this.SuspendLayout();
      // 
      // leftpanel
      // 
      this.leftpanel.Controls.Add(this.axKGAX3D);
      this.leftpanel.Controls.Add(this.btnShaded);
      this.leftpanel.Controls.Add(this.btnHiddenRemoved);
      this.leftpanel.Controls.Add(this.btnWireframe);
      this.leftpanel.Controls.Add(this.btnRotate);
      this.leftpanel.Controls.Add(this.btnPanorama);
      this.leftpanel.Controls.Add(this.btnMove);
      this.leftpanel.Controls.Add(this.btnOrientation);
      this.leftpanel.Controls.Add(this.btnShowAll);
      this.leftpanel.Controls.Add(this.btn3dRunCommand);
      this.leftpanel.Controls.Add(this.btn3dStop);
      this.leftpanel.Controls.Add(this.label1);
      this.leftpanel.Controls.Add(this.btn3dApply);
      this.leftpanel.Dock = System.Windows.Forms.DockStyle.Left;
      this.leftpanel.Location = new System.Drawing.Point(0, 0);
      this.leftpanel.Name = "leftpanel";
      this.leftpanel.Size = new System.Drawing.Size(344, 493);
      this.leftpanel.TabIndex = 0;
      // 
      // axKGAX3D
      // 
      this.axKGAX3D.ContainingControl = this;
      this.axKGAX3D.Enabled = true;
      this.axKGAX3D.Location = new System.Drawing.Point(16, 64);
      this.axKGAX3D.Name = "axKGAX3D";
      this.axKGAX3D.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axKGAX3D.OcxState")));
      this.axKGAX3D.Size = new System.Drawing.Size(320, 296);
      this.axKGAX3D.TabIndex = 13;
      // 
      // btnShaded
      // 
      this.btnShaded.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
      this.btnShaded.Location = new System.Drawing.Point(224, 432);
      this.btnShaded.Name = "btnShaded";
      this.btnShaded.Size = new System.Drawing.Size(88, 23);
      this.btnShaded.TabIndex = 12;
      this.btnShaded.Text = "Shaded";
      this.btnShaded.Click += new System.EventHandler(this.btnShaded_Click);
      // 
      // btnHiddenRemoved
      // 
      this.btnHiddenRemoved.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
      this.btnHiddenRemoved.Location = new System.Drawing.Point(120, 432);
      this.btnHiddenRemoved.Name = "btnHiddenRemoved";
      this.btnHiddenRemoved.Size = new System.Drawing.Size(88, 23);
      this.btnHiddenRemoved.TabIndex = 11;
      this.btnHiddenRemoved.Text = "HiddenRemoved";
      this.btnHiddenRemoved.Click += new System.EventHandler(this.btnHiddenRemoved_Click);
      // 
      // btnWireframe
      // 
      this.btnWireframe.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
      this.btnWireframe.Location = new System.Drawing.Point(16, 432);
      this.btnWireframe.Name = "btnWireframe";
      this.btnWireframe.Size = new System.Drawing.Size(88, 23);
      this.btnWireframe.TabIndex = 10;
      this.btnWireframe.Text = "Wireframe";
      this.btnWireframe.Click += new System.EventHandler(this.btnWireframe_Click);
      // 
      // btnRotate
      // 
      this.btnRotate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
      this.btnRotate.Location = new System.Drawing.Point(224, 464);
      this.btnRotate.Name = "btnRotate";
      this.btnRotate.Size = new System.Drawing.Size(88, 23);
      this.btnRotate.TabIndex = 9;
      this.btnRotate.Text = "Rotate";
      this.btnRotate.Click += new System.EventHandler(this.btnRotate_Click);
      // 
      // btnPanorama
      // 
      this.btnPanorama.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
      this.btnPanorama.Location = new System.Drawing.Point(120, 464);
      this.btnPanorama.Name = "btnPanorama";
      this.btnPanorama.Size = new System.Drawing.Size(88, 23);
      this.btnPanorama.TabIndex = 8;
      this.btnPanorama.Text = "Panorama";
      this.btnPanorama.Click += new System.EventHandler(this.btnPanorama_Click);
      // 
      // btnMove
      // 
      this.btnMove.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
      this.btnMove.Location = new System.Drawing.Point(16, 464);
      this.btnMove.Name = "btnMove";
      this.btnMove.Size = new System.Drawing.Size(88, 23);
      this.btnMove.TabIndex = 7;
      this.btnMove.Text = "Move";
      this.btnMove.Click += new System.EventHandler(this.btnMove_Click);
      // 
      // btnOrientation
      // 
      this.btnOrientation.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
      this.btnOrientation.Location = new System.Drawing.Point(224, 384);
      this.btnOrientation.Name = "btnOrientation";
      this.btnOrientation.Size = new System.Drawing.Size(88, 23);
      this.btnOrientation.TabIndex = 6;
      this.btnOrientation.Text = "Orientation";
      this.btnOrientation.Click += new System.EventHandler(this.btnOrientation_Click);
      // 
      // btnShowAll
      // 
      this.btnShowAll.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
      this.btnShowAll.Location = new System.Drawing.Point(120, 384);
      this.btnShowAll.Name = "btnShowAll";
      this.btnShowAll.Size = new System.Drawing.Size(88, 23);
      this.btnShowAll.TabIndex = 5;
      this.btnShowAll.Text = "Show all";
      this.btnShowAll.Click += new System.EventHandler(this.btnShowAll_Click);
      // 
      // btn3dRunCommand
      // 
      this.btn3dRunCommand.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
      this.btn3dRunCommand.Location = new System.Drawing.Point(16, 384);
      this.btn3dRunCommand.Name = "btn3dRunCommand";
      this.btn3dRunCommand.Size = new System.Drawing.Size(88, 23);
      this.btn3dRunCommand.TabIndex = 4;
      this.btn3dRunCommand.Text = "Run command";
      this.btn3dRunCommand.Click += new System.EventHandler(this.btn3dRunCommand_Click);
      // 
      // btn3dStop
      // 
      this.btn3dStop.Location = new System.Drawing.Point(152, 8);
      this.btn3dStop.Name = "btn3dStop";
      this.btn3dStop.Size = new System.Drawing.Size(48, 23);
      this.btn3dStop.TabIndex = 3;
      this.btn3dStop.Text = "";
      this.btn3dStop.Click += new System.EventHandler(this.button2_Click);
      // 
      // label1
      // 
      this.label1.Location = new System.Drawing.Point(16, 8);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(24, 23);
      this.label1.TabIndex = 2;
      this.label1.Text = "3D";
      this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
      // 
      // btn3dApply
      // 
      this.btn3dApply.Location = new System.Drawing.Point(48, 8);
      this.btn3dApply.Name = "btn3dApply";
      this.btn3dApply.Size = new System.Drawing.Size(88, 23);
      this.btn3dApply.TabIndex = 1;
      this.btn3dApply.Text = "";
      this.btn3dApply.Click += new System.EventHandler(this.btn3dApply_Click);
      // 
      // splitter
      // 
      this.splitter.Location = new System.Drawing.Point(344, 0);
      this.splitter.Name = "splitter";
      this.splitter.Size = new System.Drawing.Size(5, 493);
      this.splitter.TabIndex = 1;
      this.splitter.TabStop = false;
      // 
      // rightpanel
      // 
      this.rightpanel.Controls.Add(this.axKGAX2D);
      this.rightpanel.Controls.Add(this.btn2dStop);
      this.rightpanel.Controls.Add(this.label2);
      this.rightpanel.Controls.Add(this.btn2dApply);
      this.rightpanel.Controls.Add(this.btn2dRunCommand);
      this.rightpanel.Dock = System.Windows.Forms.DockStyle.Fill;
      this.rightpanel.Location = new System.Drawing.Point(349, 0);
      this.rightpanel.Name = "rightpanel";
      this.rightpanel.Size = new System.Drawing.Size(355, 493);
      this.rightpanel.TabIndex = 2;
      // 
      // axKGAX2D
      // 
      this.axKGAX2D.ContainingControl = this;
      this.axKGAX2D.Enabled = true;
      this.axKGAX2D.Location = new System.Drawing.Point(16, 64);
      this.axKGAX2D.Name = "axKGAX2D";
      this.axKGAX2D.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axKGAX2D.OcxState")));
      this.axKGAX2D.Size = new System.Drawing.Size(312, 296);
      this.axKGAX2D.TabIndex = 8;
      this.axKGAX2D.OnKgStopCurrentProcess += new System.EventHandler(this.axKGAX2D_OnKgStopCurrentProcess);
      this.axKGAX2D.OnKgMouseDown += new AxKGAXLib._DKGAXEvents_OnKgMouseDownEventHandler(this.axKGAX2D_OnKgMouseDown);
      // 
      // btn2dStop
      // 
      this.btn2dStop.Location = new System.Drawing.Point(152, 8);
      this.btn2dStop.Name = "btn2dStop";
      this.btn2dStop.Size = new System.Drawing.Size(48, 23);
      this.btn2dStop.TabIndex = 6;
      this.btn2dStop.Text = "";
      this.btn2dStop.Click += new System.EventHandler(this.btn2dStop_Click);
      // 
      // label2
      // 
      this.label2.Location = new System.Drawing.Point(16, 8);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(24, 23);
      this.label2.TabIndex = 5;
      this.label2.Text = "2D";
      this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
      // 
      // btn2dApply
      // 
      this.btn2dApply.Location = new System.Drawing.Point(48, 8);
      this.btn2dApply.Name = "btn2dApply";
      this.btn2dApply.Size = new System.Drawing.Size(88, 23);
      this.btn2dApply.TabIndex = 4;
      this.btn2dApply.Text = "";
      this.btn2dApply.Click += new System.EventHandler(this.btn2dApply_Click);
      // 
      // btn2dRunCommand
      // 
      this.btn2dRunCommand.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
      this.btn2dRunCommand.Location = new System.Drawing.Point(16, 384);
      this.btn2dRunCommand.Name = "btn2dRunCommand";
      this.btn2dRunCommand.Size = new System.Drawing.Size(88, 23);
      this.btn2dRunCommand.TabIndex = 7;
      this.btn2dRunCommand.Text = "Run command";
      this.btn2dRunCommand.Click += new System.EventHandler(this.btn2dRunCommand_Click);
      // 
      // FrmActiveX
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(704, 493);
      this.Controls.Add(this.rightpanel);
      this.Controls.Add(this.splitter);
      this.Controls.Add(this.leftpanel);
      this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
      this.Name = "FrmActiveX";
      this.Text = "Kompas ActiveX Container";
      this.leftpanel.ResumeLayout(false);
      ((System.ComponentModel.ISupportInitialize)(this.axKGAX3D)).EndInit();
      this.rightpanel.ResumeLayout(false);
      ((System.ComponentModel.ISupportInitialize)(this.axKGAX2D)).EndInit();
      this.ResumeLayout(false);

    }
		#endregion

		private void btn3dApply_Click(object sender, System.EventArgs e)
		{
			axKGAX3D.StopCurrentProcess(false);
		}

		#endregion

		private void button2_Click(object sender, System.EventArgs e)
		{
			axKGAX3D.StopCurrentProcess(true);
		}

		private void btn3dRunCommand_Click(object sender, System.EventArgs e)
		{
			KompasObject kompas = axKGAX3D.GetKompasObject();
			if (kompas == null)
			{
				MessageBox.Show("BAD KompasObject");
				return;
			}

			ksDocument3D doc = (ksDocument3D)kompas.ActiveDocument3D();
			if (doc == null)
			{
				MessageBox.Show("BAD Document3D");
				return;
			}

			ksPart part = (ksPart)doc.GetPart((short)Part_Type.pNew_Part);	// 
			if (part == null)
			{
				MessageBox.Show("BAD Part");
				return;
			}

			ksEntity entitySketch = (ksEntity)part.NewEntity((short)Obj3dType.o3d_sketch);
			if (entitySketch == null)
			{
				MessageBox.Show("BAD EntitySketch");
				return;
			}

			//   
			ksSketchDefinition sketchDef = (ksSketchDefinition)entitySketch.GetDefinition();
			if (sketchDef == null)
			{
				MessageBox.Show("BAD SketchDefinition");
				return;
			}

			//     XOY
			ksEntity basePlane = (ksEntity)part.GetDefaultEntity((short)Obj3dType.o3d_planeXOY);
			sketchDef.SetPlane(basePlane);	//   XOY   
			sketchDef.angle = 45;			//   
			entitySketch.Create();			//  

			//   
			ksDocument2D sketchEdit = (ksDocument2D)sketchDef.BeginEdit();
			//    - 
			sketchEdit.ksLineSeg(50, 50, -50, 50, 1);
            sketchEdit.ksLineSeg(50, -50, -50, -50, 1);
            sketchEdit.ksLineSeg(50, -50, 50, 50, 1);
            sketchEdit.ksLineSeg(-50, -50, -50, 50, 1);
			sketchDef.EndEdit();	//  

			ksEntity entityExtr = (ksEntity)part.NewEntity((short)Obj3dType.o3d_baseExtrusion);
			if (entityExtr == null)
			{
				MessageBox.Show("BAD EntityExtrusion");
				return;
			}

			//     
			ksBaseExtrusionDefinition extrusionDef = (ksBaseExtrusionDefinition)entityExtr.GetDefinition();
			if (extrusionDef == null)
			{
				MessageBox.Show("BAD ExtrusionDefinition");
				return;
			}

			extrusionDef.directionType = (short)Direction_Type.dtNormal;	//  
			extrusionDef.SetSideParam(true, (short)End_Type.etBlind, 200, 0, false);
			extrusionDef.SetThinParam(true, (short)Direction_Type.dtBoth, 10, 10);		//     
			extrusionDef.SetSketch(entitySketch);					//   
			entityExtr.Create();									//  
		}

		private void btnShowAll_Click(object sender, System.EventArgs e)
		{
			axKGAX3D.ZoomEntireDocument();
		}

		private void btnOrientation_Click(object sender, System.EventArgs e)
		{
			axKGAX3D.OrientationDocument();
		}

		private void btnWireframe_Click(object sender, System.EventArgs e)
		{
			axKGAX3D.Document3DDrawMode = KGAXLib.KDocument3DDrawMode.vt_WireframeMode;
		}

		private void btnHiddenRemoved_Click(object sender, System.EventArgs e)
		{
			axKGAX3D.Document3DDrawMode = KGAXLib.KDocument3DDrawMode.vt_HiddenRemovedMode;
		}

		private void btnShaded_Click(object sender, System.EventArgs e)
		{
			axKGAX3D.Document3DDrawMode = KGAXLib.KDocument3DDrawMode.vt_ShadedMode;
		}

		private void btnMove_Click(object sender, System.EventArgs e)
		{
			axKGAX3D.MoveViewDocument();
		}

		private void btnPanorama_Click(object sender, System.EventArgs e)
		{
			axKGAX3D.PanoramaViewDocument();
		}

		private void btnRotate_Click(object sender, System.EventArgs e)
		{
			axKGAX3D.RotateViewDocument();
		}

		private void btn2dApply_Click(object sender, System.EventArgs e)
		{
			axKGAX2D.StopCurrentProcess(false);
		}

		private void btn2dStop_Click(object sender, System.EventArgs e)
		{
			axKGAX2D.StopCurrentProcess(true);
		}

		private void btn2dRunCommand_Click(object sender, System.EventArgs e)
		{
			Kompas6API5.Application kompas = (Kompas6API5.Application)axKGAX2D.GetKompasObject();
			if (kompas == null)
			{
				MessageBox.Show("BAD KompasObject");
				return;
			}

			ksDocument2D doc = (ksDocument2D)kompas.ActiveDocument2D();
			if (kompas == null)
			{
				MessageBox.Show("BAD Document2D");
				return;
			}

			doc.ksLineSeg(100, 100, 200, 200, 1);
			doc.ksLineSeg(200, 100, 100, 200, 0);
		}


		private void KGAX3D_OnKgStopCurrentProcess(object sender, EventArgs e)
		{
			MessageBox.Show(this, "KGAX_OnKgStopCurrentProcess", "");
		}

    private void axKGAX2D_OnKgMouseDown(object sender, AxKGAXLib._DKGAXEvents_OnKgMouseDownEvent e)
    {
      if (e.nButton != 2)
        e.proceed = true;
      else
      {
        MessageBox.Show(this, "KgMouseDown", "");
      }
  
    }

    private void axKGAX2D_OnKgStopCurrentProcess(object sender, System.EventArgs e)
    {
      MessageBox.Show(this, "KGAX_OnKgStopCurrentProcess", "");
 
    }
	}
}
