/*
script om een serie locator te verbinden met lijnen, met point constraints
Sander Mulders
(c)2006
XXL-workshop
*/
 

global proc connectLocators() {
// retrieve the selected locators
  
  string $locators[] = `ls -sl`;
  int $numLocators = size($locators);
  
  for ( $i = 0; $i < ($numLocators-1); $i++) {
	  
	  //retrieve the position of the locator and the next one
	  
	  $pos1 = `pointPosition $locators[$i]`;
	  $pos2 = `pointPosition $locators[$i+1]`;

	  //creates a curve between the 2 locators
	  
	  $lijn = `curve -degree 1 -p $pos1[0] $pos1[1] $pos1[2] -p $pos2[0] $pos2[1] $pos2[2]`;

	  //create a clusterhandle on each end of the curve
	  
	  $clus_1 = `cluster -rel ($lijn+".cv[0]")`;
	  $clus_2 = `cluster -rel ($lijn+".cv[1]")`;
	  
	  //centers the pivot of the 2 locators to ensure correct constraining
	  
	  select -r $locators[$i] ;
	  CenterPivot;
	  select -r $locators[$i+1] ;
	  CenterPivot;
	  
	  //point constrain the curve-clusters to the locator
	  
	  pointConstraint -o 0 0 0 $locators[$i] $clus_1;
	  pointConstraint -o 0 0 0 $locators[$i+1] $clus_2;
	  
	  hide $clus_1;
	  hide $clus_2;
	  
	float $diaConnector = `floatField -query -value connectionDiameter`; //locator size
	
	if ($diaConnector != 0) {
	
		string $diameter[];
		$diameter = `circle -r ($diaConnector/2)`;
		extrude -ch true -rn false -po 0 -et 2 -ucp 1 -fpt 1 -upn 1 -rotation 0 -scale 1 -rsp 1 $diameter[0] $lijn;
		hide $diameter;
	}

  }
}


global proc newLocator() {
	
	$locator = `spaceLocator -p 0 0 0`;
	
	float $size = `floatField -query -value locatorDiameter`; //locator size
	
	if ($size != 0) {
		$sphere = `polySphere -r ($size/2) -sx 20 -sy 20 -ax 0 1 0 -tx 2 -ch 1`;
	   parent $locator[0] $sphere[0];
	   hide $locator;
	}
}
 

if ( `window -exists spatialSchemeUI` ) {
    deleteUI spatialSchemeUI; // window verwijderen als deze al bestaat
}

window -t "3D relatie schema" -w 600 -h 500 spatialSchemeUI;

	columnLayout -rowSpacing 6 -columnWidth 500;
	
	rowLayout -numberOfColumns 1;
	separator -w 500;
	setParent ..;
	
	rowLayout -numberOfColumns 1;
	text -l "Locators are use to represent the various functions/goals";
	setParent ..;

	rowLayout -numberOfColumns 1;
	text -l "You can give the locator a \"size\" by setting its spherical dimension, 0 means no sphere";
	setParent ..;


	rowLayout -numberOfColumns 2 -columnWidth2 200 300;
		text -label "Diameter: " -align "right";
		floatField locatorDiameter;
		setParent ..;

	
	rowLayout -numberOfColumns 2 -columnWidth2 200 300;
		text -label "Make new locator: " -align "right";
		button -label "Make" -command "newLocator()";
		setParent ..;
		
	rowLayout -numberOfColumns 1;
		separator -w 500;
		setParent ..;

	rowLayout -numberOfColumns 1;
		text -l "You can give connection a certain diameter to represent size, 0 means no diameter ";
		setParent ..;


	rowLayout -numberOfColumns 2 -columnWidth2 200 300;
		text -label "Diameter: " -align "right";
		floatField connectionDiameter;
		setParent ..;

		
	rowLayout -numberOfColumns 2 -columnWidth2 200 300;
		text -l "Select the locators to connect them";
		button -label "Connect" -command "connectLocators()";
		setParent ..;
	
	

showWindow spatialSchemeUI;