// 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:  28 December 1999
//
//  Description:
//      This implements the Polygon and Subdiv selection
//		conversion operation.
//

global proc PolySelectConvert(int $toType)
{
	string	$converted[];
	string	$convertedSubd[];
	int		$numConvertedSubdFaces = 0;
	string	$tokens[];
	string	$evalStr;
	string	$selList[];
	string	$addToSelList[];
	int		$i;

	if( `isTrue "SubdivUIExists"` ) {
		//
		// First replace all subd shapes (or their transforms)
		// in the selection list with
		// all their faces at the current display level.
		// This is necessary because subd component conversion only occurs
		// among component selection types.
		//
		$selList = `ls -sl`;
		int $numSelected = size($selList);
		string $selType[];
		string	$subdivShapesToConvert[];
		string	$itemsToDeselect[];
		string	$children[];
		string	$childType[];
		int			$numChildren;
		int			$child;
		int			$found;
		int			$numToConvert = 0;
		int			$numToDeselect = 0;
		int			$isIntermediate = 0;

		//
		// Take a pass through the selection list, keepting track of
		// subdiv shapes found (they need to be replaced with a list
		// of all their faces at the current display level).
		// For those that are represented by their transform parent,
		// keep a list of those, as those transfors will need to
		// be removed from the selection list.
		for ($i = 0; $i < $numSelected; $i++) {
			$selType = `ls -st $selList[$i]`;
			if ($selType[1] == "transform") {
				$children = `listRelatives -children $selList[$i]`;
				$numChildren = size($children);
				$found = 0;
				for ($child = 0; $child < $numChildren && !$found; $child++) {
					$childType = `ls -st $children[$child]`;
					if ($childType[1] == "subdiv") {
						string $getAttrCmd = "getAttr " + $children[$child]
											+ ".intermediateObject";
						$isIntermediate = eval($getAttrCmd);

						//
						// Ignore intermediateObjects (those that
						// are the hidden inputs to a deformation).
						if ($isIntermediate == 0) {
							//
							// Add this child to the list of subdiv shapes
							// to convert to faces.
							$subdivShapesToConvert[$numToConvert]
								= $children[$child];
							$numToConvert++;

							$itemsToDeselect[$numToDeselect] = $selList[$i];
							$numToDeselect++;
							$found = 1;
						}
					}
				}
			} else if ($selType[1] == "subdiv") {
				//
				// Add this shape to the list of subdiv shapes
				// to convert to faces.
				$subdivShapesToConvert[$numToConvert] = $selList[$i];
				$numToConvert++;
				$itemsToDeselect[$numToDeselect] = $selList[$i];
				$numToDeselect++;

			}
		}
		for ($i = 0; $i < $numToDeselect; $i++) {
			select -toggle $itemsToDeselect[$i];
		}
		$selList = `ls -sl`;
		for ($i = 0; $i < $numToConvert; $i++) {
			querySubdiv -a 4 $subdivShapesToConvert[$i];
			$addToSelList = `ls -sl`;
			select $selList $addToSelList;
			$selList = `ls -sl`;
		}
	}

	// To Face
	if ($toType == 1)
	{
		$converted = `polyListComponentConversion -fv -fe -fuv -fvf -tf`;
		$convertedSubd = `subdListComponentConversion -fv -fe -fuv -ff -tf`;
	}

	// To Contained Face
	if ($toType == 10)
	{
		$converted = `polyListComponentConversion -fv -fe -fuv -fvf -tf -in`;
		$convertedSubd = `subdListComponentConversion -fv -fe -fuv -ff -tf -in`;
	}

	// To Edge
	else if ($toType == 2)
	{
		$converted = `polyListComponentConversion -fv -ff -fuv -fvf -te`;
		$convertedSubd = `subdListComponentConversion -fv -ff -fuv -te`;
	}

	// To Contained Edge
	else if ($toType == 20)
	{
		$converted = `polyListComponentConversion -fv -ff -fuv -fvf -te -in`;
		$convertedSubd = `subdListComponentConversion -fv -ff -fuv -te -in`;
	}

	// To Vertex
	else if ($toType == 3)
	{
		$converted = `polyListComponentConversion -ff -fe -fuv -fvf -tv`;
		$convertedSubd = `subdListComponentConversion -ff -fe -fuv -tv`;
	}

	// To UV
	else if ($toType == 4)
	{
		$converted = `polyListComponentConversion -fv -fe -ff -fvf -tuv`;
		$convertedSubd = `subdListComponentConversion -fv -fe -ff -tuv`;
	}

	
	// To vertexFace 
	else if ($toType == 5) 
	{ 
		$converted = `polyListComponentConversion -fv -fe -ff -fuv -tvf`; 
		//no vertex faces for subds 
	}

	int	$n = size($converted);
	int $ns = size($convertedSubd);
	if ( ($n > 0) || ($ns > 0) )
	{
		$evalStr = "select -r";

		for ( $i=0; $i<$n; $i++)
		{
			$evalStr += " \"";
			$evalStr += $converted[$i];
			$evalStr += "\"";
		}

		$n = size($convertedSubd);
		for ( $i=0; $i<$n; $i++)
		{
			$evalStr += " \"";
			$evalStr += $convertedSubd[$i];
			$evalStr += "\"";
		}
		eval $evalStr;
	}
}
