// ================================================================================================
// Run command(s) on first release of reset
// ================================================================================================

bool ResetNow = exec.GetLED(ResetLED);
string Line;


if (FirstRun994 && !ResetNow)
{
  while (!exec.GetLED(ResetLED))
    Thread.Sleep(10);
  ResetNow = exec.GetLED(ResetLED);
}

FirstRun994 = false;


// ================================================================================================


double currentPos =  exec.GetZpos();
double CurrentMachPos =  exec.GetZmachpos();
double CurrentToolHeight = AS3.Getfielddouble(169);
double zMinMachPos = -272.05;
bool stopped = false;


Console.Write("994 Macropump started");


while(RunMacroPump & !FirstRun994)
{
	Thread.Sleep(10);


	if(!exec.GetLED(25)) //om resetat
	{
		currentPos =  exec.GetZpos();
		CurrentMachPos =  exec.GetZmachpos();
		CurrentToolHeight = AS3.Getfielddouble(169);
		

		/*Console.Write("currentPos " + currentPos);
		Console.Write("CurrentMachPos " + CurrentMachPos);
		Console.Write("CurrentToolHeight " + CurrentToolHeight);*/
		CurrentToolHeight = CurrentToolHeight - CurrentToolHeight - CurrentToolHeight;
		//Console.Write("Toolpos " + (CurrentMachPos - CurrentToolHeight));
		double toolMachPos = CurrentMachPos - CurrentToolHeight;
		//Console.Write("To0lMachpos " + toolMachPos);
		//Console.Write("stopped " + stopped);
		if(toolMachPos <= zMinMachPos && !stopped)
		{
			Console.Write("**********STOOOOOOOP**********");
			exec.Stop();
			//exec.Callbutton(523);
			
			exec.AddStatusmessage( "Lag position!" );
			MessageBox.Show("Lag position!");
			
			
			stopped = true;
		}
		if(toolMachPos > zMinMachPos && stopped)
		{
			stopped = false;
		}
		
	}
	
	if(exec.GetLED(25)) //In reset mode, reseting variables
	{
		
		
	}
	
	
		
}

#Events

static bool RunMacroPump = true;

const int ResetLED = 25;
static bool FirstRun994 = true;
 

