/*
This example demonstrates how to work with the safearray returned
from ClusterElementCollection.Array using the GridData object
*/
// ***************************************
//
// SETUP
//
// Create scene with sphere
NewScene( null, false );
var obj = ActiveSceneRoot.AddGeometry( "Grid", "MeshSurface" ) ;
obj.subdivu = 2;
obj.subdivv = 1;
AddVertexColorsToMesh( obj )
// ***************************************
//
// FIND VERTEX COLORS
//
// Get current polygon mesh geometry
var polymesh = obj.ActivePrimitive.Geometry;
// Returns a ClusterPropertyCollection
var vertexcolors = polymesh.VertexColors;
Application.LogMessage( vertexcolors.Count );
for ( var j=0; j<vertexcolors.Count; j++ ) {
// Gets a ClusterProperty
var vertexcolor = vertexcolors(j);
Application.LogMessage( vertexcolor.Fullname );
// Returns a ClusterElementCollection
var cls_prop_elems = vertexcolor.Elements;
// Returns a VBArray
var vbValues = cls_prop_elems.Array;
Application.LogMessage( "# of dimensions: " + vbValues.dimensions() );
for ( var d=1; d<=vbValues.dimensions(); d++ ) {
Application.LogMessage( "range of dimension " + d + ": " + vbValues.lbound(d) + ".." + vbValues.ubound(d) );
}
// DISPLAY INFO
var gridtable = MakeMeAPPG( vbValues );
var canceled = InspectObj( gridtable, "", "", siModal, false );
// CHANGE INFO
if ( !canceled ) {
grid_mod = gridtable.Parameters("VertexColorChart").Value;
grid_mod.SetCell( 0, 0, 1.23 );
DeleteObj( gridtable );
gridtable = MakeMeAPPG( grid_mod.Data );
// RE-DISPLAY MODIFIED INFO
InspectObj( gridtable, "", "", siModal, false );
}
}
// ***************************************
//
// ADD VERTEX COLORS
//
function AddVertexColorsToMesh( in_Mesh )
{
// Get mesh's material
var mat = in_Mesh.Material;
// If material is not local add one
if ( mat.IsA(siSharedPSet) ) {
mat = in_Mesh.AddMaterial();
}
// Get polygon mesh geometry
var polymesh = in_Mesh.ActivePrimitive.Geometry;
polymesh.AddVertexColor();
}
// ***************************************
//
// Functions to support PPG
//
function MakeMeAPPG( in_VBArray )
{
var gprop = ActiveSceneRoot.AddProperty( "CustomProperty", false, "Visual2DArray" );
var gparam = gprop.AddGridParameter( "VertexColorChart" );
var ugrid = gparam.Value;
// Set bounds of this grid
ugrid.ColumnCount = in_VBArray.ubound(1)+1;
ugrid.RowCount = in_VBArray.ubound(2)+1;
ugrid.Data = in_VBArray;
// Set up the labels
var tmp = new Array( "R", "G", "B", "A" );
for ( var c=0; c<ugrid.ColumnCount; c++ ) {
ugrid.SetColumnLabel( c, tmp[c]) ;
}
for ( var r=0; r<ugrid.RowCount; r++ ) {
ugrid.SetRowLabel( r, "Cluster " + r.toString() );
}
// Return the Property Set (not the Grid itself)
return gprop;
} |