Today, I want to show you how easy it is to export the image of an ARIS model using a small ARIS Report. The simple report exports the images of the selected models into a MS Excel sheet.

First, we need to know which model should be exported. This is done as follows:

var models = ArisData.getSelectedModels();

We can create a new MS Excel workbook by calling the method “createExcelWorkbook”. The parameter specifies the file to be created.

var workbook = Context.createExcelWorkbook(Context.getSelectedFile());

To create a sheet for every selected model you have to call the method “createSheet”. The parameter specifies the name of the new sheet. Below, I use the name of the model as the name of the new sheet.

var sheet = workbook.createSheet(oModel.Name(g_nLoc));    

Getting the graphic of a model can be done by calling the Graphic method of the model object. This method takes three parameters. The first parameter means the object is always visible on a part of the graphic. The second parameter defines if the returned graphic should be black & white or contain colors (false = color, true = black and white). The last parameter specifies the language to be returned.

var pic = oModel.Graphic(false,false,g_nLoc);

To get the width and height of the graphic, we use the methods “getWidth” and “getHeight”.

var width = pic.getWidth(Constants.SIZE_PIXEL ) * 20 * (100.0/125.0)
var height = pic.getHeight(Constants.SIZE_PIXEL ) * 20 * 20*(100.0/133.0)

To add the model graphic to the MS Excel table, we have to call the method “setPicture”. The parameters describe the picture to be added and the cells and rows where the picture starts and ends.

sheet.setPicture (data, 0, 4, 0+xCells, 4+yCells, 0, 0, xTwips, yTwips);

Those are the most important commands used in the script. Everything else is just iterating over the list of models, etc. Below is the complete script code.

var g_nLoc = Context.getSelectedLanguage();
var models = ArisData.getSelectedModels();
var workbook = Context.createExcelWorkbook(Context.getSelectedFile());
var dummyOutput = Context.createOutputObject();
main();
dummyOutput.WriteReport() //otherwise it would overwrite the excel output!
workbook.write();

function main(){
    for(var i=0;i<models.length;i++){
        var oModel = models[i];
        var sheet = workbook.createSheet(oModel.Name(g_nLoc));    
        pictureOfModel(oModel,sheet);
    }
}

function pictureOfModel(oModel, sheet) {
    var pic = oModel.Graphic(false,false,g_nLoc);
    var sfileName = oModel.GUID() + ".png";
    var width = pic.getWidth(Constants.SIZE_PIXEL ) * 20 * (100.0/125.0) //twips, correction factor because of excel anomaly (X): * (100.0/125.0)
    var xCells = width / 1024;
    var xTwips = width % 1024;
    var height = pic.getHeight(Constants.SIZE_PIXEL ) * 20*(100.0/133.0) //twips, correction factor because of excel anomaly (Y): \*(100.0/133.0)*/
    var yCells = height / 256;
    var yTwips = height % 256;
    pic.setZoom(150) // image improvement only(optional)
    pic.Save(dummyOutput, sfileName);
    var data = Context.getFile(sfileName, Constants.LOCATION_OUTPUT);

    sheet.setPicture ( data, 0, 4, 0+xCells, 4+yCells, 0, 0, xTwips, yTwips);
    Context.deleteFile(sfileName);
}

Here you can download the entire source code from the "Output picture of model" report.

Note: Make sure to join the Reports & Macros group. You will find additional tutorials and a list of free ARIS reports and macros.

 or register to reply.

Notify Moderator