Calling Commands from C#

 
 
 

Commands can be invoked in C# using the XSIApplication.ExecuteCommand method. For more information, see the following sections:

How to Pass in Arguments

Input arguments are packed into an array of System.Object objects (the C# version of the Variant type):

// Use XSIUtils.BuildPath to create the string path to the preset
CXSIUtilsClass utils = new CXSIUtilsClass();
string presetfile = utils.BuildPath(
    app.get_InstallationPath(siInstallationPath.siUserPath),
    "Data", "DSPresets", "Properties", "Example.Preset", "", "", ""
    );

// Notice how you don't have to explicitly cast these strings into objects
// in order to pack them into the array of objects
object[] args = new object[2] { presetfile, "MyPSet" };
app.ExecuteCommand("SILoadPreset", args);

C# Example: Calling a Command with Optional Arguments

All arguments need to be explicitly passed to command arguments. However, in some cases you can invoke the default value by passing in a null value. This is similar to JScript, but whereas JScript lets you omit any trailing optional arguments, you must still pass them in C#:

using XSIOM;
CXSIApplicationClass app = new CXSIApplicationClass();

// Notice that passing null triggers the default value 
app.ExecuteCommand("NewScene", null);
Model root = app.ActiveSceneRoot;
X3DObject sph = root.AddGeometry("Cube", "MeshSurface", "MyCube");

// The JScript version can simply ignore the final argument and it 
// will default to false, but C# needs to explicitly pass null
object[] args = new object[3] { sph.Name + ".kine.local.posx", "-10 + T * 3", null };
app.ExecuteCommand("AddExpr", args);

// JScript version would look like this:
NewScene( null, false );
var root = Application.ActiveSceneRoot;
var sph = root.AddGeometry("Cube", "MeshSurface", "MyCube");

// Here the final argument is missing
AddExpr( sph.Name + ".kine.local.posx", "-10 + T * 3" );

C# Example: Calling a Command with No Arguments

If the command takes no arguments you can specify a null:

app.ExecuteCommand("LastFrame", null);
Important

If you try to pass only the name of the command, you will get a compiler error telling you are trying to specify the wrong number of arguments for the XSIApplication.ExecuteCommand method.

Dealing with Return Values

XSIApplication.ExecuteCommand always returns a Variant object which maps to a System.Object object in C#. You can check the command reference documentation to figure out what is contained in the returned array.

C# Example: Calling a Command with a Return Value

This example explicitly casts the returned C# Object to the ActionSource interface:

using XSIOM;
CXSIApplicationClass app = new CXSIApplicationClass();

// Setup the scene
Model root = app.ActiveSceneRoot;
X3DObject sph = root.AddGeometry("Cube", "MeshSurface", "MyCube");

// Build the arguments list for the StoreAction command 
object[] args = new object[11];
args[0] = sph.Name; 
args[1] = "MyCube.kine.local.posx,MyCube.kine.local.posy,MyCube.kine.local.posz"; 
args[2] = 1; 
args[3] = "StoredStaticPose"; 
args[4] = true; 
args[5] = 1.0; 
args[6] = 5.0; 
args[7] = false; 
args[8] = false; 
args[9] = false; 
args[10] = -1.0;
ActionSource src = (ActionSource)app.ExecuteCommand("StoreAction", args);
app.LogMessage("Created new source: " + src.FullName, siSeverity.siInfo);
// Sources.Scene_Root.StoredStaticPose

Getting Output Arguments

C# Example: Calling a Command with Output Arguments

The XSIApplication.ExecuteCommand returns a Variant object containing an array of the output arguments which you can test and cast to the correct data type or interface.

Important

Some commands both return a value and use output arguments. For those commands only the return value is accessible. The only possible workaround in this case is to implement a VBScript custom command to return all elements in an array or XSICollection.

Check the Commands and Scripting Reference for return value and output parameter information on each command you need to call.

object[] args = new object[7] { StringModule.siGenericObjectFilter, null, null, null, null, null, null };
Array rtn = (Array)app.ExecuteCommand("PickElement", args);
foreach (object obj in rtn)
{
	if (obj.GetType() == System.Int32)
	{
		int val = (int)obj;
		Log(val.ToString());
	}
	else
	{
		CollectionItem itm = (CollectionItem)obj;
		Log(itm.Name);
	}
}