// 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
//
//  Description:
//      Functions called from "Subdiv Tessellate".
//

//
//  Procedure Name:
//      doSubdivCollapseArgList
//
//  Description:
//		This is the actual function that gets called from "Collapse Hierarchy" 
//      option box.
//
//  Input Arguments:
//	    $version: The version of this option box.  This is used to know how to 
//	              interpret the $args array.
//
//      history (args[0])
//      level (args[1])
//      orig object (args[2])
//
//  Return Value:
//      None.
//
//  Note:
//
global proc int doSubdivCollapseArgList( string $version, string $args[] )
{
	int $status = 0;

	if( size($args) < 3 ) {
		error("Incorrect doSubdivCollapseArgList \"1\" number of arguments.");
		return $status;
	}

	global int $gSelectSubdivSurface;

	// collapse all subd's
	string $list[] = `filterExpand -ex 1 -fp 1 -sm $gSelectSubdivSurface`; 
	int $len = size($list);

	if ($len == 0) {
		error ("Select a subdivision surface " +
			   "whose hierarchy you want to collapse.");
		return $status;
	}

	int $origObjectAction = $args[2]; 
	int $globalHist = $args[0];

	string $cmd = "subdCollapse";

	switch ($origObjectAction) {
	  case 1:
		// replace original object, forces history off, but delete
		// will take care of that...
		$cmd = $cmd + " -ch off ";
		break;

	  case 2:
		// hide original object, forces history on
		$cmd = $cmd + " -ch on ";
		if( !$globalHist ) {
			warning( "Forcing construction history to on." );
		}
		break;

	  case 3:
	  default:
		// show original object, respects global construction history
		$cmd = $cmd + " -ch " + $globalHist + " ";
		break;
	}

	$cmd += (" -level " + $args[1] + " " );

	//  Convert each subdiv, one at a time so we can catch errors
	//
  	string $polyRes[];
	string $selList = "";

	for($i = 0; $i < $len; $i++) {
		$retVal = catch ($polyRes = evalEcho ($cmd + $list[$i]));

		// if there's no error, do post work and add results 
		if ($retVal != 1){
			switch ($origObjectAction){
			  case 1:
				evalEcho ("delete "  + $list[$i]);
				break;
			  case 2:
				evalEcho ("hide " + $list[$i]);	  
				break;
			  default:
				break;
			}

			for ($s in $polyRes) {
				$selList += ($s + " ");
			}
		}
	}

	if (size($selList) > 0){
  		$selList += ";";
		eval ("select -r " + $selList);
	}

	return $status;
}


