// Copyright (C) 1997-2006 Autodesk, Inc., and/or its licensors.
// All rights reserved.
//
// The coded instructions, statements, computer programs, and/or related
// material (collectively the "Data") in these files contain unpublished
// information proprietary to Autodesk, Inc. ("Autodesk") and/or its licensors,
// which is protected by U.S. and Canadian federal copyright law and by
// international treaties.
//
// The Data is provided for use exclusively by You. You have the right to use,
// modify, and incorporate this Data into other products for purposes authorized 
// by the Autodesk software license agreement, without fee.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND. AUTODESK
// DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTIES
// INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF NON-INFRINGEMENT,
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, OR ARISING FROM A COURSE 
// OF DEALING, USAGE, OR TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS
// LICENSORS BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL,
// DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK AND/OR ITS
// LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY OR PROBABILITY OF SUCH DAMAGES.

//
//  Alias Script File
//  MODIFY THIS AT YOUR OWN RISK
//
//  Creation Date: Dec 1998
//
// Description:
//    Simplify the curves based on percentage of current cvs 
//

global proc simplifyStrokes( float $skip )
{
	int $i, $j, $k, $l;
	
	string $curves[] =  `ls -dag -sl -typ nurbsCurve`;
	string $strokes[] = `getStrokes 2`;
	string $allCurves[];
	int    $aCIdx = 0;

	// For each stroke on the pick list, find it's associated curve.
	for ($i = 0; $i < size( $strokes ); $i++) {
		string $curveList[] = `listConnections ($strokes[$i] + ".pathCurve")`;
		for ($j = 0; $j < size( $curveList ); $j++) {
			if (`nodeType $curveList[$j]` == "curveFromSurfaceCoS") {
				// Need to find the nurbsCurve(s) from the curve on surface.
				string $transforms[] = `listConnections ($curveList[$j] + ".curveOnSurface")`;
				for ($l = 0; $l < size( $transforms ); $l++) {
					string $moreChildren[] = `listRelatives -c -s -f $transforms[$l]`;
					for ($k = 0; $k < size( $moreChildren ); $k++) {
						$allCurves[ $aCIdx ] = $moreChildren[ $k ];
						$aCIdx++;
					}
				}
			}
			else if (`nodeType $curveList[$j]` == "curveFromMeshCoM") {
				// Need to find the nurbsCurve(s) from the curve on surface.
				string $transforms[] = `listConnections ($curveList[$j] + ".curveOnMesh")`;
				for ($l = 0; $l < size( $transforms ); $l++) {
					string $moreChildren[] = `listRelatives -c -s -f $transforms[$l]`;
					for ($k = 0; $k < size( $moreChildren ); $k++) {
						$allCurves[ $aCIdx ] = $moreChildren[ $k ];
						$aCIdx++;
					}
				}
			} else {
				if (`nodeType $curveList[$j]` == "transform") {
					string $moreChildren[] = `listRelatives -c -s -f $curveList[$j]`;
					for ($l = 0; $l < size( $moreChildren ); $l++) {
						if (`nodeType $moreChildren[$l]` == "nurbsCurve") {
							$allCurves[ $aCIdx ] = $moreChildren[ $l ];
							$aCIdx++;
						}
					}
				}
				else if (`nodeType $curveList[$j]` == "nurbsCurve") {
					$allCurves[ $aCIdx ] = $curveList[ $j ];
					$aCIdx++;
				}
			}
		}
	}
	// Now add the selected curves to $allCurves.
	for ( $i = 0; $i < size( $curves ); $i++ ) {
		$allCurves[ $aCIdx ] = $curves[ $i ];
		$aCIdx++;
	}

	// Finally, remove any duplicate curves from this list.
	string $uniqueCurves[];
	int    $curveSize = size( $allCurves );
	int    $idx;
	if ( $curveSize != 0) {
		// Now filter out any duplicates.
		string $sortedCurves[] = `sort $allCurves`;
		int $i;
		int $idx = 0;
		$uniqueCurves[ $idx ] = $sortedCurves[ 0 ];
		for ($i = 1; $i < $curveSize; ++$i) {
			if ($sortedCurves[ $i ] != $uniqueCurves[ $idx ]) {
				// It is a unique entry.
				++$idx;
				$uniqueCurves[ $idx ] = $sortedCurves[ $i ];
			}
		}
	}

	for( $i = 0; $i < size( $uniqueCurves ); $i++ )
	{
		int $spans = `getAttr ($uniqueCurves[$i] + ".spans")`;
		$spans = (float)$spans/(float)$skip;
		rebuildCurve -ch 1 -rpo 1 -rt 0 -kr 0 -kcp 0 
					-kep 1 -kt 0 -s $spans -d 3 -tol 1.0 $uniqueCurves[$i];
	}
}

