This is incorrect:
- Code: Select all
exec.Code("G91");
exec.Code("G31 Z" + ZPos + " F5.0" ); // Probes the tool length.
Since it's a relative move, the Z value needs to be the distance (and direction) to travel, not the absolute position. In my example, I showed it this way:
- Code: Select all
// Probe using relative distance (not dependent on coordinate system)
exec.Code("G91");
exec.Code("G31 Z-" + ZMove + " F5.0" ); // Probes the tool length
I used Zmove, which in your macro is the distance you want to travel. I placed a minus just before it, in the "G31 Z-", based on your example subtracting it from the starting Z position.
You're checking:
- Code: Select all
if(ZPos == OldZpos) // Check if the the tool positon was found.
{
MessageBox.Show("ERROR! The tool did not hit the probe and DRO was not set");
exec.Stop();
return;
}
... and (Zpos == OldZpos) is not likely to ever happen. That is, if the probe fails it doesn't retract to the starting position: it dies where it dies. I included a test for probe failure in my example.
As for the rest of the macro, I'm not sure what you're trying to accomplish. It looks like you're wanting to save the tool length into the tool table, but I don't see where Zshift is being defined. I don't think the calculation you're doing for Toffset will necessarily result in the length of the tool.