値を返す

 
 
 

カスタム コマンドは、数値、文字列、オブジェクト、オブジェクトのコレクションなどの値を戻すことができます。 値を返すには、コマンドが、Init コールバックで Command.ReturnValue または Command::EnableReturnValue を実行する必要があります。そうすると、Execute コールバックが戻り値を処理します。

戻り値が無効の場合、コマンドによって戻される値がすべて無視されます。 戻り値は有効になっていても、コマンドが値を戻さない場合、空の Variant が戻されます。

スクリプト コマンドの場合、戻り値は Execute コールバックが戻す任意の値となります。

JScript の例: カスタム コマンドからオブジェクトのコレクションを戻す

function MyCommand_Init( ctxt )
{
	var oCmd;
	oCmd = ctxt.Source;

	// This command returns a value
	oCmd.ReturnValue = true;

	var oArgs;
	oArgs = oCmd.Arguments;

	// By default, this arg will be a collection of all groups
	oArgs.AddWithHandler("cloObjects","Collection", "#group*" );
	return true;
}

function MyCommand_Execute( cloObjects )
{
	// Return all objects in the groups
	return cloObjects.Expand();
}

C++ コマンドは、コールバック コンテキストの ReturnValue 属性に戻り値を格納します。

C++ の例: カスタム コマンドからオブジェクトのコレクションを戻す

XSIPLUGINCALLBACK CStatus MyCommand_Execute( CRef& in_ctxt )
{
	Context ctxt( in_ctxt );
	CValueArray args = ctxt.GetAttribute(L"Arguments");
	
	// This command uses the Collection argument handler
	CValueArray collection = args[0];

	// An array 
	CRefArray objects;

	for ( long j = 0; j < collection.GetCount(); ++j )
	{
		CValue val = collection[j];
		CRef ref( val );
		if ( ref.GetClassID() == siX3DObjectID )
		{
			objects.Add( ref );
		}
	}

	// Put the CRefArray in a CValue object
	CValue retval( objects );

	// Return the X3DObjects
	ctxt.PutAttribute( L"ReturnValue", retval );

	// This does the same thing as above:
//	ctxt.PutAttribute( L"ReturnValue", objects );
	return CStatus::OK;
}