undo a command (scripting)

 From:  pressure (PEER)
10939.12 In reply to 10939.10 
Hi Michael,

I've had some trouble with undoing scripts that include an object picker. In one case the object picker seemed to interfere with creation of a selection undo unit. Now I've got a situation where I'm using an object picker to get an edit point so that I can move that edit point.

Here's a self-contained snippet. If you run it on a new blank file you'll see:



And then if you hit Undo you'll see an intermediate stage of what the script did:



No matter how many times you hit Undo you'll never get back to an empty environment.

Is there a way for me to clean this up so that it's neatly undoable?

code:
var index = 0;
var destinationPoint = moi.vectorMath.createPoint(1,1,1);

var rect_factory = moi.command.createFactory( 'rectangle' );
rect_factory.setInput( 0, moi.vectorMath.createFrame() );
rect_factory.setInput( 1, null );
rect_factory.setInput( 2, 5 );
rect_factory.setInput( 3, 5 );
rect_factory.setInput( 4, false );
var objList = rect_factory.calculate();
moi.geometryDatabase.addObjects(objList);

var obj = objList.item(0);

obj.showPoints = true; // must show points to make them editable
obj.setEditPointSelected(index, true);

/*
non-interactive object picker with allowEditPoints()
simply to get edit point into an objectList so that it can be manipulated
*/
var objectpicker = moi.ui.createObjectPicker();
objectpicker.allowEditPoints();
objectpicker.done();
var editPointInList = objectpicker.objects;

var basePt = obj.getEditPoint(index);

var factory = moi.command.createFactory('move');
factory.setInput(0, editPointInList);
factory.setInput(1, basePt)
factory.setInput(2, destinationPoint);

// remove the original object, gather up the newly moved object, and add to database
moi.geometryDatabase.removeObject(obj);
var movedObjInList = factory.calculate();
moi.geometryDatabase.addObjects(movedObjInList);

// re-hide the edit points
movedObjInList.item(0).showPoints = false;


- Peer