Let’s use the script editor – Part2 –

Hello, this is Lily.

Are you using a Script Editor? This article is the continuation of Let’s use the script editor – Part 1 –.

In the project file, models and presentations exist. The presentation has information on the appearance of the element. In many cases, the Astah API obtains information from these and rewrites it. If you use the API, you need to know what the model and presentation type is. You can know it by looking at the List of supported models but it is a bit troublesome. So I wrote a script. Let’s easily examine the type using this.


A script that prints information on models of selected elements on the diagram

This script is useful for examining the type of the model. You can select multiple items by holding down the Ctrl key and clicking. Let’s run the script in that state.

In the example below, you can see that the operation is IOperation, the attribute is IAttribute, and the association is IAssociation. It is also available outside class diagrams.

selectedelements

// A script that prints information on models of selected elements on the diagram
getSelectedElements().forEach(
    function (element) {
        var infoString = ""
        var stereotypes = Java.from(element.getStereotypes())
        if (stereotypes.length > 0) {
            infoString += "<<" + stereotypes.join(">><<") + ">> "
        }
        infoString += element.getName() + " - " + getClassName(element)
        print(infoString)
    }
)

/** Get the JavaScript array of the model of the selected element on the diagram. */
function getSelectedElements() {
    var viewManager = astah.getViewManager()
    var diagramViewManager = viewManager.getDiagramViewManager()
    var selectedPresentations = diagramViewManager.getSelectedElements()
    return Java.from(selectedPresentations)
}

/** Get the name of the type of the object. */
function getClassName(object) {
    var clazz = object.getClass()
    var interfaces = clazz.getInterfaces()
    if (interfaces.length > 0) {
        return interfaces[0].getName()
    }
    return clazz.getName()
}

Mind map topics do not have models. If an element without a model is selected, nothing is printed.


A script to print information on the presentation selected on the diagram

This script is useful for examining the type of presentation. In the example below, you can see that the relation is ILinkPresentation. Attributes and operations are selected, but these have no presentation. So the class presentation will be returned. It is also available outside class diagrams.

selectedpresentations

// A script to print information on the presentation selected on the diagram
getSelectedPresentations().forEach(
    function (presentation) {
        var infoString = presentation.getLabel() + " - " + getClassSimpleName(presentation)
        var model = presentation.getModel()
        if (model != null) {
            infoString += " - " + getClassSimpleName(model)
        }
        print(infoString)
    }
)

/** Get the JavaScript array of the selected presentation on the diagram. */
function getSelectedPresentations() {
    var viewManager = astah.getViewManager()
    var diagramViewManager = viewManager.getDiagramViewManager()
    var selectedPresentations = diagramViewManager.getSelectedPresentations()
    return Java.from(selectedPresentations)
}

/** Get a simple name of the type of the object. */
function getClassSimpleName(object) {
    var clazz = object.getClass()
    var interfaces = clazz.getInterfaces()
    if (interfaces.length > 0) {
        return interfaces[0].getSimpleName()
    }
    return clazz.getSimpleName()
}

I introduced the processing of selected diagram elements. Please you try customizing  with reference to Astah API User Guide obtain information on all models in the project, obtain information on the selected model in the structure tree, etc …

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s