Hi Bravlin, I'm not noticing any lag over here with your script.
But a couple of things that could improve performance though - first thing is a script that does selection type stuff is better to run as an "instant script" than as a command.
A command in MoI handles various things related to creating and deleting objects like setting up undo units, clearing selection lock, loading UI files, etc...
If you run it as instant script you will bypass all that stuff. Also you can trigger instant script while running inside of a command. If it's run as a command it will exit any currently running command since there can only be one "command" running at a time.
One way to run things as an instant script is to have the script code pasted directly into the Command field of the Shortcut key editor, prefixed with "script:" . But it is also possible to set up instant script from a .js file. To do that, make a directory named "scripts" as a sibling of "commands" in the moi appdata directory and put it in there. Then for your shortcut key use the name of the script file including the .js file extension like: SaveSelectToStyle.js . You can also put it in any directory and put the whole path to it in the shortcut key like c:\scripts\SaveSelectToStyle.js .
Also a couple other things would be to use geometryDatabase.findStyle() to get the style, and also line 23 if(obj.type = 7) looks like a bug, first you would need == there but also type number 7 is a mesh object will only be around when you're exporting to a mesh format. The objects that you're working with in TEST.3dm are BRep objects so you could use obj.isBRep . I would kind of think that trying to assign to the read only obj.type property would trigger an exception but I guess not, that might be a bug.
So give this one a try with it set up to run as immediate script, does it behave any better for you? :
code:
function DoSaveSelectToStyle()
{
var gd = moi.geometryDatabase;
var activstyleidx = moi.geometryDatabase.activeStyle;
var objects = gd.getObjects();
var savedobjsnum = 0;
var si = moi.geometryDatabase.findStyle( 'selectset', true /*CreateIfNotFound*/ ).index;
// reselect all selectset objs
for ( var i = 0; i < objects.length; ++i )
{
var obj = objects.item(i);
if(obj.styleIndex == si)
{
obj.styleIndex=activstyleidx;
obj.selected=true;
savedobjsnum +=1;
}
else
{
if(obj.isBRep)
{
var subs = obj.getSubObjects();
for ( var j = 0; j < subs.length; ++j )
{
var sub = subs.item(j);
if(sub.styleIndex == si)
{
sub.styleIndex=activstyleidx;
sub.selected=true;
savedobjsnum +=1;
}
}
}
}
}
// if no objs in style selectset
if(savedobjsnum == 0)
{
var selobjects = gd.getSelectedObjects();
selobjects.setProperty( 'styleIndex', si );
}
}
DoSaveSelectToStyle();
- Michael
|