JScriptHelper Example

The following is an example that shows the use of the JScriptHelper method and property.
// This example shows how you can use JScriptHelper to convert an array to
// a a VBArray which you can then pass to other Mentor Grapics apis. 
 
// add any type librarys to be used. 
Scripting.AddTypeLibrary("MGCPCB.ExpeditionPCBApplication"); 
Scripting.AddTypeLibrary("Scripting.FileSystemObject"); 
 
// Get the application object 
var pcbApp = Application; 
 
// Get the active document 
var pcbDoc = pcbApp.ActiveDocument; 
 
// Server validation function 
// License the document 
ValidateServer(pcbDoc); 
var oHelper = Scripting.CreateObject("JScriptHelper.ScriptHelper"); 
 
var obj = oHelper.Nothing; 
if(obj == oHelper.Nothing) 
 obj = pcbDoc.ConductorLayerGfxs2(0, epcbSelectSelected).Item(1); 
if(obj==oHelper.Nothing) 
 obj = pcbDoc.FabricationLayerGfxs(epcbFabAll, epcbSelectSelected).Item(1); 
if(obj==oHelper.Nothing) 
 obj = pcbDoc.UserLayerGfxs(epcbSelectSelected).Item(1); 
 
// Get an array from the interface 
var arr = obj.Geometry.PointsArray; 
 
// Convert it to a JScript array. 
var jArr = ToJPntsArray(arr); 
 
// Once you have a jArr you can manipulate it in JScript. 
 
// Display the JScript array 
pcbApp.Gui.Display(GetPointsArrayString(jArr)); 
 
 
// Once you have a JScript array, you can use JScript to modify the array.
// This function uses JScript functionality to modify all elements of a
// JScript array. 
// Assume jArr is an initialized JScript array. 
for (i = 0; i < jArr[0].length; i++) 
{ 
 jArr[0][i] = parseFloat(jArr[0][i]) + 100; 
} 
// Before passing the array back to the interface convert  
// it back to a SAFEARRAY. This can be done inline. 
 
// Use the shifted points to create user layer gfxs. 
var userLayerGfx = pcbDoc.PutUserLayerGfx(pcbDoc.UserLayers.Item(1), 0, jArr[0].length, ToSafePntsArray(jArr), false, oHelper.Nothing, epcbUnitCurrent) 
userLayerGfx.Selected = true 
 
// Notice that JScriptHelper is used with the PutUserLayerGfx function
// call.  
// Also notice that the oHelper (JScriptHelper) specifies Nothing (the
// property) for the interface. “Nothing” is not a keyword in JScript, but
// is needed in many Mentor Graphics automation functions. 
 
//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
//Local functions 
 
function ToSafePntsArray(jArr) 
{ 
 return oHelper.ToSafeArray(jArr); 
} 
 
// This function converts a VBArray to a two-dimensional JScript array.
// Note that this function does not require JScriptHelper, but uses
// inherent JScript functionality. 
function ToJPntsArray(VBArr) 
{ 
 // Create a VBArray object 
 var vbArr = new VBArray(VBArr) 
  
// Convert it a a single dimension JArray 
 var jsArrSingelDim = vbArr.toArray(); 
 
 // Make the single dim array into a multidimensional array. 
 // assumes there are 2 dimensions with 3 rows in the first dimension 
 var numPnts = jsArrSingelDim.length / 3; 
 var i; 
 var arr = new Array(3); 
 arr[0] = new Array(numPnts); 
 arr[1] = new Array(numPnts); 
 arr[2] = new Array(numPnts); 
 for (i = 0; i < numPnts; i++) 
 { 
 arr[0][i] = parseFloat(jsArrSingelDim[i*3]); 
 arr[1][i] = parseFloat(jsArrSingelDim[i*3+1]); 
 arr[2][i] = parseFloat(jsArrSingelDim[i*3+2]); 
 } 
  
return arr; 
} 
 
// This function reads a JScript array into a stream format so that it can
// be displayed or output. 
function GetPointsArrayString(pnts) 
{ 
 var i,j; 
 var str=""; 
 for(i = 0; i<pnts[0].length; i++) { 
 str = str + "(" + pnts[0][i]; 
 str = str + "," + pnts[1][i]; 
 str = str + "," + pnts[2][i] + ")"; 
 str = str + '
 
 } 
 return str; 
} 
 
// Server validation function 
function ValidateServer(docObj) { 
 
 var keyInt; 
 var licenseTokenInt; 
 var licenseServerObj; 
 var retValInt = 1; 
  
// Ask Xpedition Layout’s document for the key; 
 keyInt = docObj.Validate(0); 
  
// Get license server; 
 licenseServerObj = new ActiveXObject("MGCPCBAutomationLicensing.Application"); 
  
// Ask the license server for the license token; 
 licenseTokenInt = licenseServerObj.GetToken(keyInt); 
 
 // Release license server; 
 licenseServerObj = null; 
 
 // Check for error in Validate 
 try { 
 // Ask the document to validate the license token; 
 docObj.Validate(licenseTokenInt); 
 } catch(err) { 
 retValInt = 0 
 } 
  
return retValInt; 
}