Hi all!
So I made great progress on my "Cycle remaining time" timer.
Most of the work is done but I've hit a few snags.
The code is split in three macros to make this work... the M99998 constructor macro, the M30 macro and a macro loop.
I created two fields... One for the time of the previous cycle and one for the remaining time during a cycle.
Now a look at the code.
The M99998 constructor macro
- Code: Select all
// Take the strig from the "Cycle Time" field and save it in field 3000
string LastCycleTime = AS3.Getfield(898);
AS3.Setfieldtext(LastCycleTime, 3000);
exec.Callbutton(218); // Calls the "Zero Cycle Time" button and reset Cycle time field
//This code should also be run on "Rewind File" and "Load File"
This code runs fine but I would need it to run before the macro loop is called. Unfortunately, it seems the macroloop is called first and the fields have not been set.
Then the M30
- Code: Select all
// THIS CODE ISN'T CALLED AT THE END OF A CYCLE? BUT IT RUNS IF M30 IS CALLED FROM THE MDI???
// Take the strig from the "Cycle Time" field and save it in field 3000
string LastCycleTime = AS3.Getfield(898);
AS3.Setfieldtext(LastCycleTime, 3000);
exec.Callbutton(218); // Calls the "Zero Cycle Time" button and reset Cycle time field
//This code should also be run on "Rewind File" and "Load File"
This code also work but only if I call it from MDI. It doesn't work when called at the end of a cycle like you would expect???
And lastly the macroloop
- Code: Select all
// Cycle time Remaining (WORK IN PROGRESS)
string LastCycleTime = AS3.Getfield(3000); // Field 3000 is a container for the time of the last cycle
if (string.IsNullOrEmpty(LastCycleTime)){ // Check if the field is empty (DOES NOT WORK!)
LastCycleTime = "00:00:00"; // Set previous cycle and Remaining fields to 00:00:00
AS3.Setfieldtext("00:00:00", 3000);
AS3.Setfieldtext("00:00:00", 3001);
exec.Callbutton(218);
}
// Split the last Cicle time into h, m, s integers for calculations
string[] LastCycleTimeArray = LastCycleTime.Split(':');
int Hours = Int32.Parse(LastCycleTimeArray[0]);
int Minutes = Int32.Parse(LastCycleTimeArray[1]);
int Seconds = Int32.Parse(LastCycleTimeArray[2]);
// Calculate the numbers of seconds in the last cycle time and put it in a variable as an integer for calculations
int LastCycleTimeInSeconds = (Hours * 60 * 60) + (Minutes * 60) + Seconds;
// Split the current Cycle time into h, m, s integers for calculations
string CurentCycleTime = AS3.Getfield(898);
string[] CurentCycleTimeArray = CurentCycleTime.Split(':');
int CurentHours = Int32.Parse(CurentCycleTimeArray[0]);
int CurentMinutes = Int32.Parse(CurentCycleTimeArray[1]);
int CurentSeconds = Int32.Parse(CurentCycleTimeArray[2]);
// Calculate the numbers of seconds in the last cycle time and put it in a variable as an int
int CurentCycleTimeInSeconds = (CurentHours * 60 * 60) + (CurentMinutes * 60) + CurentSeconds;
//Substract current cycle time from last cycle time to get remaining time
int RemainingInSeconds = LastCycleTimeInSeconds - CurentCycleTimeInSeconds;
// Convert it back as a string formated as time hh:mm:ss
if(RemainingInSeconds >= 0){ // Test if value is larger than zero
TimeSpan t = TimeSpan.FromSeconds( RemainingInSeconds );
string answer = string.Format("{0:D2}:{1:D2}:{2:D2}",
t.Hours,
t.Minutes,
t.Seconds); // Format into hh:mm:ss string
AS3.Setfieldtext(answer, 3001); // set remaining time with the result
}else{
AS3.Setfieldtext("00:00:00", 3001); // else Set to 00:00:00
}
This code also work great except for the if statement (Second line from the beginning). This if statement should be true only the first time the loop runs.
Any help would be greatly appreciated.
- Blaise