C# でコマンドを呼び出すには、XSIApplication.ExecuteCommand メソッドを使用します。詳細については、以下のセクションを参照してください。
入力引数は、System.Object オブジェクトの配列(Variant 型の C# バージョン)にパック化されています。
// 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);
すべての引数は、コマンド引数に明示的に渡される必要があります。 ただし、場合によっては、null 値で渡すことによって既定値を呼び出すことができます。 これは JScript に似ていますが、JScript では最後のオプション引数を省略することができるのに対し、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" );
app.ExecuteCommand("LastFrame", null);
コマンド名のみを渡そうとすると、XSIApplication.ExecuteCommand メソッドに指定した引数の個数が正しくないという旨のコンパイル エラーが発生します。
XSIApplication.ExecuteCommand は、常に C# の System.Object オブジェクトにマップする Variant オブジェクトを返します。返される配列の内容については、コマンド リファレンスのドキュメントを確認してください。
この例では、返された C# のオブジェクトを ActionSource インタフェースに明示的にキャストします。
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
XSIApplication.ExecuteCommand は、ユーザがテストしたり、正しいデータ型またはインタフェースにキャストしたりできる出力引数の配列を含む Variant オブジェクトを返します。
一部のコマンドには値を返し、かつ出力引数を使用するものがあります。 この種類のコマンドでは戻り値にしかアクセスできません。 このような場合は、VBScript カスタム コマンドを実装して、すべてのエレメントを配列または XSICollection で返す必要があります。
呼び出す必要がある各コマンドの戻り値および出力パラメータについては、「コマンドおよびスクリプト リファレンス」を参照してください。
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);
}
}