Save the current "selection set" and restore later

 From:  Michael Gibson
11708.4 In reply to 11708.3 
Hi Frederick,

re:
> Does MoI have the abillity to write and read text files?

Yes, see examples in ImportPointFile and SavePointFile at:
http://moi3d.com/download/scripts/PetrsMoiPage/PetrsMoiPage.htm

read:
code:
var file = moi.filesystem.openFileStream( 'filename', 'r' );
while ( !file.atEOF )
{
    var line = file.readLine();
    // do something with line of text.
}


write:
code:
var file = moi.filesystem.openFileStream( 'filename', 'w' );
file.writeLine( 'Line of text' );
file.close();


But it's probably not going to work very well to store it as an id value in a file instead of as an object property
like a name. It will break if you make any changes like just moving the object.

re:
> They already have names that are meaningful.

Ok, then just use obj.setUserText( key, value ) instead of obj.name .

For example - Set selection set 0:
code:
var objs = moi.geometryDatabase.getSelectedObjects();
for ( var i = 0; i < objs.length; ++i )
{
    var obj = objs.item(i);
    obj.setUserText( 'saved_selection_set_0', '1' );
}


Restore selection set 0:
code:
var objs = moi.geometryDatabase.getObjects();
for ( var i = 0; i < objs.length; ++i )
{
    var obj = objs.item(i);
    if ( obj.getUserText( 'saved_selection_set_0' ) == '1' )
        obj.selected = true;

    var subobjs = obj.getSubObjects();
    for ( var j = 0; j < subobjs.length; ++j )
    {
        var subobj = subobjs.item(j);
        if ( subobj.getUserText( 'saved_selection_set_0' ) == '1' )
            subobj.selected = true;
    }
}


Unlike storing id values in a separate file, the user text will inherit down to transformed
objects so it won't break so easily. And it will be saved directly in the .3dm file so you
won't have to manage separate files.

For the store part it might be good to clear that mark off of unselected objects unless you want it to be cumulative.

So this is probably better for the store part:
code:
/* Clear user text with key = 'saved_selection_set_0' off of all objects and sub objects. */
var all_objs = moi.geometryDatabase.getObjects();
for ( var i = 0; i < all_objs.length; ++i )
{
    var obj = all_objs.item(i);
    var clear_objs = obj.getSubObjects();
    clear_objs.addObject( obj );
    for ( var j = 0; j < clear_objs.length; ++j )
    {
         var clear_obj = clear_objs.item(j);
         clear_obj.removeUserText( 'saved_selection_set_0' );
    }
}

/* Store user text with key 'saved_selection_set_0' on selected objects. */

var objs = moi.geometryDatabase.getSelectedObjects();
for ( var i = 0; i < objs.length; ++i )
{
    var obj = objs.item(i);
    obj.setUserText( 'saved_selection_set_0', '1' );
}


- Michael

EDITED: 13 Apr by MICHAEL GIBSON