General technical questions about v3 Moi's API

 From:  Michael Gibson
8010.8 In reply to 8010.7 
Hi Marco, for the single segment case you need to call .clone() on the parent curve object, not on the segment. Segments are not meant to exist outside of their parent curve, and so they don't respond to obj.clone() directly themselves.

Try this:

code:

function IsMultiSegment( crv )
{
	return crv.getSubObjects().length > 1;
}

function MakeSegmentCurves( crvlist  )
{
	var output = moi.geometryDatabase.createObjectList();

	// Separate input into 2 lists, one for single segment curves, and another for multi-segment curves.

	var single_seg_crvs = moi.geometryDatabase.createObjectList();
	var multi_seg_crvs = moi.geometryDatabase.createObjectList();
	
	for ( var i = 0; i < crvlist.length; ++i )
	{
		var crv = crvlist.item(i);
		
		if ( IsMultiSegment( crv ) )
			multi_seg_crvs.addObject( crv );
		else
			single_seg_crvs.addObject( crv );	
	}
	
	// For single segment curves, call crv.clone() to duplicate it.
	
	for ( var i = 0; i < single_seg_crvs.length; ++i )
	{
		var crv = single_seg_crvs.item(i);
		var newcrv = crv.clone();
		output.addObject( newcrv );
	}
	
	// For multi segment curves, use the separate factory to construct a new curve for each segment.
	
	var factory = moi.command.createFactory( 'separate' );
	factory.setInput( 0, multi_seg_crvs );
	
	var separated_crvs = factory.calculate();
	factory.cancel();
	
	for ( var i = 0; i < separated_crvs.length; ++i )
	{
		var crv = separated_crvs.item(i);
		output.addObject( crv );	
	}
	
	return output;
}

function TestGetSegments()
{
	var output = moi.geometryDatabase.createObjectList();

	var crvs = moi.geometryDatabase.getSelectedObjects().getCurves();
	if ( crvs.length == 0 )
	{
		moi.ui.alert( 'Select a curve before running this command.' );
		return;
	}
	
	var crvsegs = MakeSegmentCurves( crvs );
	
	moi.ui.alert( crvsegs.length );	
}	

TestGetSegments();




Hope this helps!

- Michael