Calling C++ Custom Commands from Scripting
 
 
 

When a C++ custom command returns an array, the value received by the caller depends on the scripting language, and whether a CValueArray or a CRefArray is returned. For example, if a command returns a CValueArray, then a JScript caller gets a safe array, but if a command returns a CRefArray, then a JScript caller gets an XSICollection.

 

Return Value

Language

CRefArray

CValueArray

VBScript

XSICollection

XSICollection

JScript

XSICollection

safe array

PerlScript

XSICollection

array

Python

XSICollection instance

tuple

JScript Example: Calling a C++ command that returns an array

//
// GetValueArray() is a C++ custom command that returns a CValueArray
//

var aSafeArray = GetValueArray();

var aVBArray = new VBArray( aSafeArray );
logmessage( typeof( aVBArray ) );
logmessage( aVBArray.dimensions() );

// Access the VBArray directly
for (var i=0; i <= aVBArray.ubound(1); i++)
{
	Application.LogMessage( aVBArray.getItem(i).fullname );
}

// Convert the VBArray to a JScript array
var aJSarray = aVBArray.toArray();
for (e=new Enumerator(aJSarray);!e.atEnd();e.moveNext())
{
	var o = e.item();
	logmessage( o.fullname );
}

//
// GetRefArray() is a C++ custom command that returns a CRefArray
//

var objects = GetRefArray();
logmessage( objects.type );	//INFO : XSICollection
for (e=new Enumerator(objects);!e.atEnd();e.moveNext())
{
	var o = e.item();
	Application.LogMessage( o.fullname );
}

Python Example: Calling a C++ command that returns an array

#
# GetValueArray is a C++ custom command that returns a CValueArray
#

objects = Application.GetValueArray()

Application.Logmessage( unicode(type(objects)) )#INFO : <type 'tuple'>

for o in objects:
	Application.LogMessage( o.name )
	Application.Logmessage( unicode(type(o)) )#INFO : <type 'instance'>
	Application.LogMessage( Application.classname(o) )

#
# GetRefArray is a C++ custom command that returns a CRefArray
#

objects = Application.GetRefArray()
Application.Logmessage( unicode(type(objects)) )#INFO : <type 'instance'>
Application.Logmessage( objects.type )# XSICollection

for o in objects:
	Application.LogMessage(o.name)

for i in range(objects.Count):
	Application.LogMessage(objects(i).FullName)

VBScript Example: Calling a C++ command that returns an array

'
' GetValueArray() returns a CValueArray
'
set objects = GetValueArray()

logmessage( typename( objects ) )'INFO : Object
logmessage( objects.type )'INFO : XSICollection

for each o in objects
	logmessage o.name
next
'
' GetRefArray() returns a CRefArray
'
set objects = GetRefArray()
logmessage typename( objects )'INFO : Object
logmessage( objects.type )'INFO : XSICollection
for each o in objects
	logmessage o.name
next

PerlScript Example: Calling a C++ command that returns an array

#
# GetRefArray() returns a CRefArray
#

# scalar context
my $objects = GetRefArray();
$Application->LogMessage( $objects->Type );#INFO : XSICollection
for $i (0..$objects->Count-1) {
	$o = $objects->Item($i);
	$Application->LogMessage( $o->IsKindOf("polymsh") );
	$Application->LogMessage( $o->{'FullName'} );
}


# list context
my @objects = GetRefArray();
$Application->logmessage( $objects[0]->Type );		#INFO : XSICollection

for $i (0..$objects[0]->Count-1) {
	$o = $objects[0]->Item($i);
	$Application->LogMessage( $o->IsKindOf("polymsh") );
	$Application->LogMessage( $o->{'FullName'} );
}

#
# Test() returns a CValueArray
#

# list context
my @test = Test();

$Application->LogMessage( ref( $test[0] ) );	#INFO : ARRAY

for $o (@{$test[0]}) {
	$Application->LogMessage( $o->FullName );
}


# scalar context
my $test = Test();

$Application->LogMessage( ref( $test ) );	#INFO : ARRAY

for $o (@{$test}) {
	$Application->LogMessage( ref( $o ) );	#INFO : Win32::OLE
	$Application->LogMessage( $o->FullName );
}