how to terminate a script

 From:  pressure (PEER)
10847.3 In reply to 10847.2 
Michael,

Thank you for the extensive description of what the Esc key does and for pointing me in the direction of createOrientationPicker(). I'll give that a try.

Having methods with a cancel button return false if canceled sounds good.

I'm still not clear on a couple of things.

First, how can I avoid the infinite loop that results if a script is set to repeat, but the script throws an alert and then exits as soon as the alert button is clicked? I ran into this with IsoAtPoints http://moi3d.com/forum/lmessages.php?webtag=MOI&msg=7978.6 and had to force quit MoI while I had unsaved work on a project that I cared about. Now I'm leary of ticking the Repeat checkbox for any script, even though it would be handy to use repeat sometimes. Here's what happens:



Second, how can I safely re-prompt a user for input? If I run the script below, set the CPlane at the wrong orientation, and then hit Esc on the next iteration through the While loop it goes into an infite loop. It's like the line moi.View.setCPLaneInteractive(); is getting skipped over, but the While loop is still running.



code:
var box;
while ( true ){

	// user picks orientation of new CPlane
	moi.View.setCPLaneInteractive();
	
	// get bounding box of selected object in frame set by user
	box = moi.ui.PropertiesPanel.highAccuracyBoundingBox;
	
	var tol = 0.0001; //numerical tolerance
	
	// check the just-set CPlane to see if it has the correct orientation
	// relative to the selected object
	if ( compareNumbers( box.zLength, 0, tol ) != 0 ){t
		
		moi.ui.alert( "CPlane XY must coincide with face" );
	} else {
		break;
	}
}

// Returns 1 if a > b, 0 if a = b, and -1 if a < b within some positive tolerance, t  
function compareNumbers(a, b, t) {
    var out = undefined;

    var dif = a - b;

    // a > b
    if (dif > 0 && Math.abs(dif) > t) {

        out = 1;

    }

    // a = b
    if (Math.abs(dif) < t) {

        out = 0;

    }

    // a < b
    if (dif < 0 && Math.abs(dif) > t) {

        out = -1;

    }

    return out;

}

- Peer

EDITED: 17 Nov 2024 by PEER