// Copyright (C) 1997-2004 Alias Systems Corp.
// 
// The information in this file is provided for the exclusive use of the
// licensees of Alias.  Such users have the right to use, modify,
// and incorporate this code into other products for purposes authorized
// by the Alias license agreement, without fee.
// 
// ALIAS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
// INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
// EVENT SHALL ALIAS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
// DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

//
//  Alias Script File
//  MODIFY THIS AT YOUR OWN RISK
//
//  Creation Date:  Mar 20, 1997
//
//  Description:
//      The intersectPreset() procedure executes a intersect operation on 
//      a pair of surfaces based on the intersect option vars. In general if
//		you have n surfaces selected, (n-1) surface intersect operations would 
//		be carried out.
//
//  Input Arguments:
//      None.
//
//  Return Value:
//      None.
//

proc string pieceTogetherACmd(
	int $cosOnlyOnFirstSurface,
	int $cosAs2D,
	int $history,
    float $tol )
//
//	Description :
//		piece together an intersect command.
//
{
	string $cmd;
	$cmd = "intersect " ;

	// construction history.
	//
	$cmd = $cmd + " -ch " ;

	if( $history == 1 ) {
		$cmd = $cmd + "true" ;
	} else {
		$cmd = $cmd + "false" ;
	}

	$cmd = $cmd + " -fs " ;
	$cmd = $cmd + $cosOnlyOnFirstSurface ;

	$cmd = $cmd + " -cos " ;
	$cmd = $cmd + $cosAs2D ;

	$cmd = $cmd + " -tol " + $tol ;

	return $cmd ;

}

global proc intersectPreset(
 int $cosOnlyOnFirstSurface,
 int $cosAs2D,
 int $history,
 float $tol )
//
//	Intersect with the preset options.
//	Use this proc when operation dragged to Shelf.
//
{
	string $cmd ;
	$cmd = pieceTogetherACmd( $cosOnlyOnFirstSurface, $cosAs2D, $history, $tol ) ;

	int $nitems = 2 ;
	$cmd = appendToCmdPlaceHoldersForSelectionItems($cmd,$nitems) ;

	// Get the list of nurbs surfaces selected.
	//
	global int $gSelectNurbsSurfacesBit;
	string $surfaceList[] = `filterExpand -ex true -sm $gSelectNurbsSurfacesBit`;
	int $i ;

	int $surfaceCount = size($surfaceList) ;
	if( $surfaceCount < 2 ) {
	   error "Select at least two NURBS surfaces to intersect" ; 
	} else {
		// all (n-1) combinations
		//
		string $intersectResults[] ;
		int $nr = size($intersectResults) ;
		for( $i = 0 ; $i < $surfaceCount-1 ; $i++ ) {
			string $surfacePair[2] ;
			$surfacePair[0] = $surfaceList[$i] ;
			$surfacePair[1] = $surfaceList[$surfaceCount-1] ;
			string $results[] = executeCmdOnItems( $cmd, $surfacePair );	
			$intersectResults = stringArrayCatenate( $intersectResults, $results ) ;
		} // for $i	

		// select the results.
		//
		int $resultCount = size($intersectResults) ;

		if( $resultCount > 0 ) {
			string $selectString;
        	$selectString = "select -r";

			// select all results to begin with
			//
			$resultCount = size($intersectResults) ;
			for( $i = 0 ; $i < $resultCount ; $i++ ) {
				$selectString += " ";
				$selectString += $intersectResults[$i] ;
			}
			$selectString += ";";
			eval($selectString) ;
		}
	}
}
