﻿
InitializeGUI ();
dr = new DialogResult ();

exec.Stopspin();



int maxToolNumber = Convert.ToInt32(exec.Readkey ("GP Proto CNC", "maxToolNumber", "0"));

int Newtool = 3;
int Currenttool = exec.Getcurrenttool();

if(Newtool <1 || Newtool > maxToolNumber) // Tool number is out of range, so we need to stop here...
{
	textBoxDro.Visible = false;
	btnYES.Visible = false;
	btnNO.Visible = false;
	btnOK.Visible = true;
	textBoxStatus.Clear ();
	textBoxStatus.AppendText (" \n");
	textBoxStatus.AppendText ("Utensile diverso dai valori consentiti!");
	System.Media.SystemSounds.Exclamation.Play();
	dr = myForm.ShowDialog ();
	if (dr == DialogResult.OK)
	{
		myForm.Close();
	}

	return;
}
if(Newtool == Currenttool) // Same tool was selected, so do nothing, stop here...
{ 
	textBoxDro.Visible = false;
	btnYES.Visible = false;
	btnNO.Visible = false;
	btnOK.Visible = true;
	textBoxStatus.Clear ();
	textBoxStatus.AppendText ("Utensile in mandrino e No." + Newtool + " uguale all'utensile richiamato.");
	System.Media.SystemSounds.Exclamation.Play();
	dr = myForm.ShowDialog ();
	if (dr == DialogResult.OK)
	{
		myForm.Close();
	}
	
	return; 
}
 
if(!exec.GetLED(56)||!exec.GetLED(57)||!exec.GetLED(58)) // If machine was not homed then it is unsafe to move in machine coordinates, stop here...
{
	textBoxDro.Visible = false;
	btnYES.Visible = false;
	btnNO.Visible = false;
	btnOK.Visible = true;
	textBoxStatus.Clear ();
	textBoxStatus.AppendText ("Errore! Cambio utensile non effettuabile se non è stata effettuata l'origine macchina!");
	System.Media.SystemSounds.Exclamation.Play();
	dr = myForm.ShowDialog ();
	if (dr == DialogResult.OK)
	{
		myForm.Close();
	}
  
	exec.Stop();
	return;
}


if (Currenttool==0) 
{	
	InitializeGUI ();
	textBoxDro.Visible = false;
	btnYES.Visible = true;
	btnNO.Visible = true;
	btnOK.Visible = false;
	textBoxStatus.Clear ();
	textBoxStatus.AppendText ("Attenzione utensile No.0. Vuoi forzare il valore?");
	System.Media.SystemSounds.Exclamation.Play();
	dr = myForm.ShowDialog ();
	if (dr == DialogResult.Yes)
	{
		myForm.Close();
		InitializeGUI ();
		pictureBoxUI.Image = System.Drawing.Image.FromFile (Application.StartupPath + @"\Flashscreen\BMP\Bluescreenset\MessageBox1.png");
		textBoxDro.Visible = true;
		btnYES.Visible = false;
		btnNO.Visible = false;
		btnOK.Visible = true;
		textBoxStatus.Clear ();
		textBoxStatus.AppendText ("Inserisci il numero utensile in mandrino nel DRO.");
		System.Media.SystemSounds.Exclamation.Play();
		dr = myForm.ShowDialog ();
		if (dr == DialogResult.OK)
		{
			int tool_set = Convert.ToInt32(textBoxDro.Text);
			myForm.Close();
			exec.Setcurrenttool(tool_set);
		}
		
	}
	else if (dr == DialogResult.No)
	{
		myForm.Close ();
		InitializeGUI ();
		textBoxDro.Visible = false;
		btnYES.Visible = false;
		btnNO.Visible = false;
		btnOK.Visible = true;
		textBoxStatus.Clear ();
		textBoxStatus.AppendText ("Procedura terminata.");
		System.Media.SystemSounds.Exclamation.Play();
		dr = myForm.ShowDialog ();
		if (dr == DialogResult.OK)
		{
			myForm.Close();
		}
		
		return;
	}
						
}

    exec.Code ("M6T" + Newtool);
	while(exec.IsMoving()){}
	AS3.Clearlist (2);
	exec.AddStatusmessage ("Utensile caricato : " + Newtool);

if(!exec.Ismacrostopped()) // If tool change was not interrupted with a stop only then validate new tool number
{
	exec.Setcurrenttool(Newtool); //Set the current tool -> the new tool
    
}
else
{
	exec.StopWithDeccel();
  
	InitializeGUI ();
	textBoxDro.Visible = false;
	btnYES.Visible = false;
	btnNO.Visible = false;
	btnOK.Visible = true;
	textBoxStatus.Clear ();
	textBoxStatus.AppendText ("Operazio interrotta. Verificare corrispondenza utensile in mandrino!");
	System.Media.SystemSounds.Exclamation.Play();
	dr = myForm.ShowDialog ();
	if (dr == DialogResult.OK)
	{
		myForm.Close();
	}
 
}


#Events

Form myForm;
Button btnYES;
Button btnNO;
Button btnOK;
TextBox textBoxStatus;
PictureBox pictureBoxUI;
TextBox textBoxDro;
DialogResult dr;

public Point mouseLocation;

private void pictureBoxUI_MouseDown(object sender, MouseEventArgs e)
{
	mouseLocation = new Point(-e.X, -e.Y);
}

private void pictureBoxUI_MouseMove(object sender, MouseEventArgs e)
{
	if (e.Button == MouseButtons.Left)
	{
		Point mousePos = Control.MousePosition;
		mousePos.Offset(mouseLocation.X, mouseLocation.Y);
		myForm.Location = mousePos;
	}
}

private void InitializeGUI ()
{
btnYES = new Button ();
btnYES.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(164)))), ((int)(((byte)(211)))), ((int)(((byte)(118)))));
btnYES.DialogResult = System.Windows.Forms.DialogResult.Yes;
btnYES.FlatAppearance.BorderSize = 0;
btnYES.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
btnYES.Font = new System.Drawing.Font("Aero Matics", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
btnYES.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(52)))), ((int)(((byte)(63)))));
btnYES.Location = new System.Drawing.Point(236, 112);
btnYES.Size = new System.Drawing.Size(70, 22);
btnYES.TabIndex = 1;
btnYES.Text = "YES";
btnYES.UseVisualStyleBackColor = false;

btnNO = new Button ();
btnNO.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(164)))), ((int)(((byte)(211)))), ((int)(((byte)(118)))));
btnNO.DialogResult = System.Windows.Forms.DialogResult.No;
btnNO.FlatAppearance.BorderSize = 0;
btnNO.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
btnNO.Font = new System.Drawing.Font("Aero Matics", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
btnNO.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(52)))), ((int)(((byte)(63)))));
btnNO.Location = new System.Drawing.Point(315, 112);
btnNO.Size = new System.Drawing.Size(70, 22);
btnNO.TabIndex = 2;
btnNO.Text = "NO";
btnNO.UseVisualStyleBackColor = false;

btnOK = new Button ();
btnOK.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(164)))), ((int)(((byte)(211)))), ((int)(((byte)(118)))));
btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
btnOK.FlatAppearance.BorderSize = 0;
btnOK.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
btnOK.Font = new System.Drawing.Font("Aero Matics", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
btnOK.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(52)))), ((int)(((byte)(63)))));
btnOK.Location = new System.Drawing.Point(315, 112);
btnOK.Size = new System.Drawing.Size(70, 22);
btnOK.TabIndex = 3;
btnOK.Text = "OK";
btnOK.UseVisualStyleBackColor = false;

textBoxStatus = new TextBox (); 
textBoxStatus.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(52)))), ((int)(((byte)(63)))));
textBoxStatus.BorderStyle = System.Windows.Forms.BorderStyle.None;
textBoxStatus.Font = new System.Drawing.Font("Aero Matics", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
textBoxStatus.ForeColor = System.Drawing.Color.LightGray;
textBoxStatus.Location = new System.Drawing.Point(83, 39);
textBoxStatus.Multiline = true;
textBoxStatus.ReadOnly = true;
textBoxStatus.Size = new System.Drawing.Size(302, 54);
textBoxStatus.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
textBoxStatus.TabIndex = 4;
textBoxStatus.TabStop = false;

textBoxDro = new TextBox ();
textBoxDro.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(164)))), ((int)(((byte)(211)))), ((int)(((byte)(118)))));
textBoxDro.BorderStyle = System.Windows.Forms.BorderStyle.None;
textBoxDro.Font = new System.Drawing.Font("Aero Matics", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
textBoxDro.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(52)))), ((int)(((byte)(63)))));
textBoxDro.Location = new System.Drawing.Point(19, 114);
textBoxDro.MaxLength = 2;
textBoxDro.Size = new System.Drawing.Size(54, 18);
textBoxDro.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
textBoxDro.TabIndex = 5;
textBoxDro.TabStop = false;

pictureBoxUI = new PictureBox ();
pictureBoxUI.BackColor = System.Drawing.Color.Transparent;
pictureBoxUI.Image = System.Drawing.Image.FromFile (Application.StartupPath + @"\Flashscreen\BMP\Bluescreenset\MessageBox.png");
pictureBoxUI.Location = new System.Drawing.Point(0, 0);
pictureBoxUI.Size = new System.Drawing.Size(400, 150);
pictureBoxUI.Controls.Add(btnYES);
pictureBoxUI.Controls.Add(btnNO);
pictureBoxUI.Controls.Add(btnOK);
pictureBoxUI.Controls.Add(textBoxDro);
pictureBoxUI.Controls.Add(textBoxStatus);
pictureBoxUI.TabIndex = 0;
pictureBoxUI.MouseDown += new System.Windows.Forms.MouseEventHandler(pictureBoxUI_MouseDown);
pictureBoxUI.MouseMove += new System.Windows.Forms.MouseEventHandler(pictureBoxUI_MouseMove);

myForm = new Form ();
myForm.BackColor = System.Drawing.Color.DarkRed;
myForm.ClientSize = new System.Drawing.Size(400, 150);
myForm.Controls.Add(pictureBoxUI);
myForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
myForm.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
myForm.TransparencyKey = System.Drawing.Color.DarkRed;
myForm.TopMost = true;
}


