User Tools

Site Tools


Simulate Selected Group

Summary

Below is an example demonstrating how to perform a custom simulation where the only contributors are the selected nodes in the scene.

API Areas of Interest

Example

Simulate_Selected_Group.dsa
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function(){
 
	/*********************************************************************/
	// String : A function for retrieving a translation if one exists
	function text( sText )
	{
		// If the version of the application supports qsTr()
		if( typeof( qsTr ) != "undefined" ){
			// Return the translated (if any) text
			return qsTr( sText );
		}
 
		// Return the original text
		return sText;
	};
 
	/*********************************************************************/
	// Boolean : A function for testing whether or not a QObject instance
	// inherits one of a list of types
	function inheritsType( oObject, aTypeNames )
	{
		// If the object does not define the 'inherits' function
		if( !oObject || typeof( oObject.inherits ) != "function" ){
			// We are done... it is not a QObject
			return false;
		}
 
		// Iterate over the list of type names
		for( var i = 0, nTypes = aTypeNames.length; i < nTypes; i += 1 ){
			// If the object does not inherit the 'current' type
			if( !oObject.inherits( aTypeNames[i] ) ){
				// Next!!
				continue;
			}
 
			// Return the result
			return true;
		}
 
		// Return the result
		return false;
	};
 
	/*********************************************************************/
	// Boolean : A function for filtering duplicate elements from a list
	function filterElementInArray( oElement, nIndex, aValues )
	{
		return aValues.indexOf( oElement ) == nIndex;
	};
 
	/*********************************************************************/
	// Boolean : A function for filtering nodes in a list that are visibile
	// in simulation
	function filterNodeVisibileInSimulation( oNode, nIndex, aValues )
	{
		return oNode.isVisibileInSimulation();
	};
 
	/*********************************************************************/
	// Get the simulation manager
	var oSimMgr = App.getSimulationMgr();
	// If we do not have a simulation manager
	if( !oSimMgr ){
		// Inform the user
		MessageBox.critical( text( "The simulation manager could not be found." ),
			text( "Resource Error" ), text( "&OK" ) );
 
		// We are done...
		return;
	}
 
	// Get the active simulation engine
	var oSimEngine = oSimMgr.getActiveSimulationEngine();
	// If we do not have a simulation engine
	if( !oSimEngine ){
		// Inform the user
		MessageBox.critical( text( "An active simulation engine could not be found." ),
			text( "Resource Error" ), text( "&OK" ) );
 
		// We are done...
		return;
	}
 
	// Set the busy cursor to let the user know we are working
	setBusyCursor();
 
	// Initialize
	var aSimNodes = [];
 
	// Declare working variable
	var oNode;
 
	// Get the list of selected nodes
	var aNodes = Scene.getSelectedNodeList();
	// Iterate over the list 
	for( var i = 0, nNodes = aNodes.length; i < nNodes; i += 1 ){
		// Get the 'current' node
		oNode = aNodes[ i ];
 
		// If it is a group node
		if ( inheritsType( oNode, [ "DzGroupNode" ] ) ){
			// If the group is visible in simulation
			if ( oNode.isVisibileInSimulation() ){
				// Append the list of recursive child nodes
				aSimNodes = aSimNodes.concat( oNode.getNodeChildren( true ) );
			}
		// If it is not a group node, but is visible in simulation
		} else if( oNode.isVisibileInSimulation() ) {
			// Append the node
			aSimNodes = aSimNodes.concat( oNode );
		}
	}
 
	// We only want unique nodes - remove duplicates
	aSimNodes = aSimNodes.filter( filterElementInArray );
	// We only want nodes that are visible in simulation
	aSimNodes = aSimNodes.filter( filterNodeVisibileInSimulation );
 
	// We are done working, let the user know
	clearBusyCursor();
 
	// Perform a custom simulation with our list of nodes
	oSimEngine.customSimulate( aSimNodes );
 
// Finalize the function and invoke
})();