//Date 09 Feb 2018 Metric
//M31 probing macro modified by John Revill admin@metchit.com.au
//V1.0 Inital Public Release
//V1.01 Change: Do not need to enter 0 if touching off the material surface. Can now leave the field blank and just press enter.

// USER-EDITABLE CONFIG
machineUnitsMM = true;  // Change to false is machine uses Inches.
plateThickness = 12.4;  // Plate thickiness in unit measurements.
probeSpeed = 5;         // Speed at which to lower Z axis.
retractHeight = 5;      // Units to retract above plate after contact.
zMin = -100;            // Maximum plunge depth for Z. Only adjust if neccesary.
// END USER-EDITABLE CONFIG

double Zmin = -100; //Max.Z depth
double FeedrateFast = 300; //Feedrate for probing
double FeedrateSlow = 40; //Feedrate for probing
double SafeZ = 100;
double RetractHeight = 10; //The retract height
double RetractForSecondMeasurement = 2;


// Don't touch this
Button buttonOK = new Button();
buttonOK.Size = new System.Drawing.Size(120, 40);
buttonOK.Location = new System.Drawing.Point(150, 60);
buttonOK.Text = "BEGIN PROBE";
buttonOK.Click += new EventHandler(buttonOK_Click);

Button buttonCancel = new Button();
buttonCancel.Size = new System.Drawing.Size(80, 40);
buttonCancel.Location = new System.Drawing.Point(10, 60);
buttonCancel.Text = "CANCEL";
buttonCancel.Click += new EventHandler(buttonCancel_Click);

Label instruction = new Label();
instruction.Text = "Material Offset:";
instruction.Size = new System.Drawing.Size(115, 30);
instruction.Location = new System.Drawing.Point(5, 5);

materialHeight = new NumericUpDown();
materialHeight.Minimum = 0;
materialHeight.DecimalPlaces = 4;
materialHeight.Size = new System.Drawing.Size(100, 20);
materialHeight.Location = new System.Drawing.Point(120, 5);
// materialHeight.TextChanged += new EventHandler(materialHeightChanged);

materialUnits = new ComboBox();
materialUnits.DropDownStyle = ComboBoxStyle.DropDownList;
materialUnits.Size = new System.Drawing.Size(50, 30);
materialUnits.Location = new System.Drawing.Point(225, 5);
string[] materialUnitsOptions = new string[]{"MM", "IN"};
materialUnits.Items.AddRange(materialUnitsOptions);
if (machineUnitsMM)
{
  materialUnits.SelectedIndex = 0;
}
else
{
  materialUnits.SelectedIndex = 1;
}


dialogForm = new Form();
dialogForm.Text = "Auto-Zero Z-Axis";
dialogForm.Font = new Font("Segoe UI", 11);
dialogForm.Size = new System.Drawing.Size(300, 150);
dialogForm.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
dialogForm.Controls.Add(instruction);
dialogForm.Controls.Add(materialHeight);
dialogForm.Controls.Add(materialUnits);
dialogForm.Controls.Add(buttonCancel);
dialogForm.Controls.Add(buttonOK);
dialogForm.ShowDialog();

#Events

Form dialogForm;
NumericUpDown materialHeight;
ComboBox materialUnits;
bool machineUnitsMM;
double plateThickness;
double probeSpeed;
double retractHeight;

void buttonCancel_Click(object sender, EventArgs e)
{
  dialogForm.Close();
}

void buttonOK_Click(object sender, EventArgs e)
{
  while(exec.IsMoving()){}
  exec.Wait(200);

  double multiplier = 1.00;
  if (machineUnitsMM && materialUnits.SelectedIndex == 1) // Machine in MM but material in IN
  {
    multiplier = 25.4;
  }
  else if (!machineUnitsMM && materialUnits.SelectedIndex == 0) // Machine in IN but material in MM
  {
    multiplier = 1/25.4;
  }

  double materialToAdd = multiplier * Convert.ToDouble(materialHeight.Value);


  // exec.Code("G31 Z" + Convert.ToString(zMin) + " F" + probeSpeed);

  // while(exec.IsMoving()){};
  // exec.Wait(200);

  // double zPos = exec.GetZmachpos();

  // MessageBox.Show(Convert.ToString(zPos));

  dialogForm.Close();
}

// exec.Code("G31 Z" + Zmin + "F" + FeedrateSlow); // Do the Z probing again with Slow Feedrate to get a more accurate reading
// while(exec.IsMoving()){}
// exec.Wait(200);

// newZ = newZ - nominal;

// exec.ChangeaxisDROvalue(2, newZ.ToString()); //Change the DRO value
// exec.Wait(200);  //Safety wait for the UC100 syncronisation

// if(!exec.Ismacrostopped()) // If tool change was not interrupted with a stop only then validate new tool number
// {
//  double Zup = exec.GetZmachpos() + RetractHeight;

//  if(Zup > SafeZ)
//  {
//    Zup = SafeZ;
//  }
//  exec.Code("G00 G53 Z" + Zup); // Move 10mm above probe plate
//  while(exec.IsMoving()){}
//  exec.Wait(1000);
// }



