// 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 14, 1997
//
//  Description:
//      This script runs rebuild curve on the selection list.
//


global proc performRebuildCurveSet( int $doHistory, int $replaceOriginal,
									int $rebuildType, float $globalTol,
									int $crvNumSpans, int $crvDegree,
									int $endKnots,
									int $keepParmRange, int $keepControlPoints,
									int $keepEndPts, int $keepTan,
									int $crvUseGlobalTol, float $crvLocalTol)
{
	// Get a list of each type of acceptable object type - 
	// curves, and curves-on-surface.
	//
	global int $gSelectNurbsCurvesBit;
	global int $gSelectCurvesOnSurfacesBit;
	string $curveList[] = `filterExpand -ex true -sm $gSelectNurbsCurvesBit`;
	string $cosList[] = `filterExpand -ex true -sm $gSelectCurvesOnSurfacesBit`;

	int $i;

	if( $rebuildType == 2 ) {
		if( (size($curveList) + size($cosList)) < 2 ) {
			error( "Select at least two NURBS curves to " +
				   "rebuild with match knots option.") ;
			return;
		}
	}
	else {
		if( (size($curveList) + size($cosList)) < 1 ) {
			error( "Nothing was selected to rebuild.  You must select " +
				   "NURBS curves or curve on surfaces.");
			return;
		}
	}

	// Execute rebuildCurve on all active curves.
	//
	$cmd = "rebuildCurve" + " -ch " + $doHistory + 
		   " -rpo " + $replaceOriginal+
		   " -rt " + $rebuildType + 
		   " -end " + $endKnots +
		   " -kr " + $keepParmRange +
		   " -kcp " + $keepControlPoints +
		   " -kep " + $keepEndPts + 
		   " -kt " + $keepTan;
	$cmd += " -s " + $crvNumSpans + " -d " + $crvDegree;
	if( $crvUseGlobalTol == 0 ) {	// Use globl tolerance vs. local tolerance
		$cmd += " -tol " + $crvLocalTol;
	} else {
		$cmd += " -tol " + $globalTol;
	}

	string $cosCmd;
	$cosCmd = $cmd;

	string $curveResults[] ;

	if( $rebuildType == 2 ) {
		int $l = size($curveList) ;
		if( $l > 2 ) {
			warning( "Rebuilding all but the last curve to match knots " +
					 "of the last curve in the selection list" );
		}
		if( $l >= 2 ) {
			int $nitems = 2 ;
			$cmd = appendToCmdPlaceHoldersForSelectionItems( $cmd, $nitems ) ;
			string $curvePair[2] ;

			for( $i=0; $i<($l-1); $i+=1 ) {
				$curvePair[0] = $curveList[$i] ;	
				$curvePair[1] = $curveList[$l-1];
				string $oneRes[] = executeCmdOnItems($cmd,$curvePair);
				appendStringArray( $curveResults, $oneRes, size($oneRes));
			}
		}
	} else {
		$cmd += " %s;";
		$curveResults = executeForEachObject( $curveList, $cmd );
	}

	// Execute rebuildCurve on all active curves-on-surface.
	//
	// $cosCmd +=  " -cos on ";
	string $cosResults[];
	if( $rebuildType == 2 ) {
		int $l = size($cosList) ;
		if( $l > 2 ) {
			warning( "Rebuilding all but the last curve to match knots " +
					 "of the last curve in the selection list" );
		}
		if( $l >= 2  ) {
			int $nitems = 2 ;
			$cosCmd = appendToCmdPlaceHoldersForSelectionItems( $cosCmd, $nitems ) ;
			for( $i=0; $i<($l-1); $i+=1 ) {
				$curvePair[0] = $cosList[$i] ;	
				$curvePair[1] = $cosList[$l-1];
				string $oneRes[] = executeCmdOnItems($cmd,$curvePair);
				appendStringArray( $curveResults, $oneRes, size($oneRes));
			}
		}
	} else {
		$cosCmd += " %s;" ;
		$cosResults = executeForEachObject( $cosList, $cosCmd );
	}


	if( (size($curveResults)+size($cosResults)) == 0 ) {
		int $l = size($curveList) ;
		if( $rebuildType == 2  && (size($curveList) < 2) ) {
			error( "Select at least two NURBS curves to rebuild " +
				   "with match knots.");
		}
		else if( 0 == (size($curveList) + size($cosList)) ) {
			error( "Nothing was selected to rebuild.  You must select " +
				   "NURBS curves.");
		}
		else {
			error( "Rebuild curve failed on the current valid selection." );
		}
	} else {
		// Select all the results with one select command
		//
		select -r $curveResults $cosResults;
	}
}

