Scripting the Trim command

 From:  Michael Gibson
8786.2 In reply to 8786.1 
Hi Brian, the Trim factory is a little awkward to script because it's heavily based on the workflow of the Trim command and has a couple of custom methods that need to be called. Here's an example though that makes a line curve and a point and trims the line. Let me know if trimming an edge instead of a curve isn't working.

code:

var linefactory = moi.command.createFactory( 'line' );
linefactory.setInput( 0, moi.vectorMath.createPoint(0,0,0) );
linefactory.setInput( 1, moi.vectorMath.createPoint(10,0,0) );
var linelist = linefactory.calculate();

var pointfactory = moi.command.createFactory( 'point' );
pointfactory.setInput( 0, moi.vectorMath.createPoint(8,0,0) );
var pointlist = pointfactory.calculate();

/*
Trim factory inputs:

0: ObjectList - Objects to trim.
1: ObjectList - Cutting objects.
2: ObjectList - Selected fragments to keep or remove, can be an empty list to keep all fragments but it can't be left just unset.
3: String - Mode, "remove" or "keep"
4: Boolean - Extend lines
5: Boolean - Use projected intersections
6: List - Optional list of picked points for curve trimming, used by the "Add trim points" function in the Trim command.
*/

var trimfactory = moi.command.createFactory( 'trim' );

trimfactory.setInput( 0, linelist );
trimfactory.setInput( 1, pointlist );
trimfactory.setInput( 2, moi.geometryDatabase.createObjectList() );

/*
Note - the trim factory is heavily based on the trim command workflow and works a little
different than the standard behavior. You need to call the custom functions
.generateFragments() and finishedPickingFragments() for it to finish and because of
the .generateFragments() step puts objects in the geometry database it won't work to
call calculate() to generate "loose" objects that aren't in the geometry database as
is normally possible.
*/

trimfactory.generateFragments();
trimfactory.finishedPickingFragments();

trimfactory.update();

var objs = trimfactory.getCreatedObjects();
moi.ui.alert( objs.length + ' objects created' );

trimfactory.commit();



- Michael