//TAVOLA PCB v1.0 21-03-2019 
// porto l'utensile all'interno dello scodellino e muovendomi prima su X poi su Y vado a tastare con G31 per trovare il centro e memorizzo le quote 
// trovato il centro dello scodellino mi alzo in Z e vado sul tastatore per azzarare la quota zero di Z
// terminato il setting mi scosto delle coordinate relative al punto di origine PCB  e della distanza tra punto di origine tastatore 
// Z e superficie PCB
double DeltaZ = Convert.ToInt32(exec.Readkey ("GP Proto CNC", "DeltaZ", "0"));
exec.AddStatusmessage ("-->  DELTA Z :" + DeltaZ);
InitializeGUI ();
dr = new DialogResult ();

exec.Stopspin();


					
		myForm.Close();
		InitializeGUI ();
		pictureBoxUI.Image = System.Drawing.Image.FromFile (Application.StartupPath + @"\Flashscreen\BMP\Bluescreenset\MessageBox.png");
		textBoxDro.Visible = false;
		btnYES.Visible = true;
		btnNO.Visible = true;
		btnOK.Visible = false;
		textBoxStatus.Clear ();
		textBoxStatus.AppendText ("TAVOLA PCB , l'utensile deve essere all'interno dello scodellino di tastatura");
		System.Media.SystemSounds.Exclamation.Play();
		dr = myForm.ShowDialog ();
		if (dr == DialogResult.Yes)
		{



					
exec.Callbutton(100);
exec.Callbutton(101);
exec.Callbutton(102);

// Legge la posizione corrente di XY delle ccordinate macchina per ritornare in posizione a termina macro

double Xoriginalpos = exec.GetXmachpos();
double Yoriginalpos = exec.GetYmachpos();
double Zoriginalpos = exec.GetZmachpos();

// inizio misurazione 2 punti su X

	exec.Code ("F" + fminZ);
	
	exec.Code ("G31 X" + minX);
	while (exec.IsMoving ()) {}
	X_P1 = exec.Getvar (5061);	
	Y_P1 = exec.Getvar (5062);	
	exec.Callbutton(100); //azzero DRO X
	exec.Code ("G1 X" + 2 + "F" + fminZ);
	while (exec.IsMoving ()) {}

	exec.Code ("G31 X" + maxX + "F" + fminZ);
	while (exec.IsMoving ()) {}
	X_P2 = exec.Getvar (5061);	
	Y_P2 = exec.Getvar (5062);	
	
	DeltaX = (X_P2) /2;
	exec.AddStatusmessage ("-->  X_P1 :" + X_P1);
	exec.AddStatusmessage ("-->  DELTA X :" + DeltaX);
	exec.Code ("G1 X" + DeltaX + "F" + fmin);
	while (exec.IsMoving ()) {}
	
//  inizio misurazione 2 punti su Y	

	exec.Code ("G31 Y" + minY+ "F" + fminZ);
	while (exec.IsMoving ()) {}
	X_P3 = exec.Getvar (5061);	
	Y_P3 = exec.Getvar (5062);	
	exec.Callbutton(101); //azzero DRO Y
		exec.Code ("G1 Y" + 2 + "F" + fminZ);
	while (exec.IsMoving ()) {}
	

	exec.Code ("G31 Y" + maxY + "F" + fminZ);
	while (exec.IsMoving ()) {}
	X_P4 = exec.Getvar (5061);	
	Y_P4 = exec.Getvar (5062);	
	
	DeltaY = (Y_P4) /2;
	exec.AddStatusmessage ("-->  DELTA Y :" + DeltaY);
	exec.Code ("G1 Y" + DeltaY + "F" + fmin);
	while (exec.IsMoving ()) {}
	
	exec.Callbutton(100); //azzero DRO X
	exec.Callbutton(101); //azzero DRO Y
	X_PC = exec.GetXmachpos();	// quote centro scodellino
	Y_PC = exec.GetYmachpos();
	
	
	// Mi alzo in Z e vado sul  centro tastatore 
	exec.Code ("G1 Z" + 20 + "F" + fminZ);
	while (exec.IsMoving ()) {}
	exec.Code ("G1 X" + 49.9 + "F" + fminZ);
	while (exec.IsMoving ()) {}
	exec.Callbutton(100); //azzero DRO X
	// G31 Z (5063)
	exec.Code ("G31 Z" + zmin);
	while (exec.IsMoving ()) {}
	exec.Callbutton(102); //azzero DRO Z
	
	// Mi alzo in Z e vado sul  punto origine PCB
	exec.Code ("G1 Z" + 20 + "F" + fmin);
	while (exec.IsMoving ()) {}
	exec.Code ("G1 X" + -99.9 + "F" + fmin);
	while (exec.IsMoving ()) {}
	exec.Code ("G1 Y" + 19.9 + "F" + fmin);
	while (exec.IsMoving ()) {}
	// abbasso Z del delta
	DeltaZ=5;
		exec.Code ("G1 Z" + DeltaZ + "F" + fminZ);
	while (exec.IsMoving ()) {}
	
		exec.Callbutton(100); //azzero DRO X
		exec.Callbutton(101); //azzero DRO Y
		exec.Callbutton(102); //azzero DRO Z
	
	InitializeGUI ();
	textBoxDro.Visible = false;
	btnYES.Visible = false;
	btnNO.Visible = false;
	btnOK.Visible = true;
	textBoxStatus.Clear ();
	textBoxStatus.AppendText ("Orignine PCB terminata , caricare ed eseguire il programma di fresatura");
	System.Media.SystemSounds.Exclamation.Play();
	dr = myForm.ShowDialog ();
	if (dr == DialogResult.OK)
	{
		myForm.Close();
	}
		exec.AddStatusmessage ("-->  FINE PROCEDURA");
		
		
	}
	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;
	}
	
	if(!exec.Ismacrostopped()) //
{
    
}
else
{
	exec.StopWithDeccel();
  
	InitializeGUI ();
	textBoxDro.Visible = false;
	btnYES.Visible = false;
	btnNO.Visible = false;
	btnOK.Visible = true;
	textBoxStatus.Clear ();
	textBoxStatus.AppendText ("Operazio interrotta dal'utente!");
	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;

// Start global variables 

int NewTool = 0;
int ToolUp = -5; 		// Z Hieght to Rapid from tool to tool
int ToolMove= -45; 		// movimento mentre cambia
int fminZ = 200;
int fmin = 1000;
int fmax = 5000;
int zmin = -200;
int counter = 0;
int minX = -20;
int maxX = 40;
int minY = -20;
int maxY = 40;


double OldTool;			//Utensile in mandrino
double X_palp;			//posizione X del palpatore (Settings -> Tool Change Location)
double Y_palp;			//posizione Y del palpatore
double X_UT1;
double Y_UT1;
double X_P1;
double Y_P1;
double X_P2;
double Y_P2;
double X_P3;
double Y_P3;
double X_P4;
double Y_P4;
double X_PC;		// coordinate del centro scodellino
double Y_PC;		// coordinate del centro scodellino
double DeltaX;
double DeltaY;


double ToolY;   		// Y fuori rastelliera
double ToolDown; 		// DISTANZA DISCESA SU PORTAUTENSILE
double UTpres;			// posizione maassima presetting
double XPos, YPos;

double offsetToolPosition;
int maxToolNumber;
int chuckPort;
int chuckPin;

double Zzero = 0;
double Zuno = 0;
double Zdelta = 0;

static bool checkButton = true;


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;
}

// End global variables