Get Models and Presentations

You can learn how to get Models and Presentations by using Astah API. You can find a list of available Models and Presentations to get from here.

Get Models

Find models in a project

Call findElements() in ProjectAccessor to get models in a project. ProjectAccessor can get from AstahAPI#getProjectAccessor().

Sample with ModelFinder

Block Definitions Diagram Picker
public class BlockDefinitionsDiagramPicker implements ModelFinder {
    public boolean isTarget(INamedElement namedElement) {
        return namedElement instanceof IBlockDefinitionDiagram;
    }
}
Entry Point Picker
public class EntryPointPicker implements ModelFinder {
    public boolean isTarget(INamedElement namedElement) {
        if (namedElement instanceof IPseudostate) {
            return ((IPseudostate) namedElement).isEntryPointPseudostate();
        }
        return false;
    }
}

Get a model selected in the structure tree

public IEntity[] printSelectedEntities(IViewManager viewManager) {
    IProjectViewManager projectViewManager = viewManager.getProjectViewManager();
    return projectViewManager.getSelectedEntities();
}

Get a model selected on the diagram

public List<IElement> getModelsOfSelectedPresentations(IViewManager viewManager) {
    List<IElement> models = new ArrayList<>();
    for (IPresentation selectedP : getSelectedPresentations(viewManager)) {
        models.add(selectedP.getModel());
    }
    return models;
}

private IPresentation[] getSelectedPresentations(IViewManager viewManager) {
    IDiagramViewManager diagramViewManager = viewManager.getDiagramViewManager();
    return diagramViewManager.getSelectedPresentations();
}

Stereotypes

You can get Stereotypes as an array of Strings with IElement#getStereotypes(). The example below shows how to get the block stereotype of a block model.

public String getBlockStereotype(IBlock block) {
  String[] stereotypes = block.getStereotypes();
  return stereotypes[0];
}

Get Diagrams and Presentations

Get Diagrams

public List<IActivityDiagram> getActivityDiagram(IPackage pkg) {
    List<IActivityDiagram> activityDiagrams = new ArrayList<>();

    IDiagram[] diagrams = pkg.getDiagrams();
    for (IDiagram diagram : diagrams) {
       if (diagram instanceof IActivityDiagram
           && !((IActivityDiagram )diagram).isFlowChart()) {
           activityDiagrams.add((IActivityDiagram)diagram);
       }
    }
    return activityDiagrams;
}

Get all presentations in the diagram

With IDiagram#getPresentations() you can get all the presentations in the diagram.
Please note that IElement#getPresentations() behaves differently.

public IPresentation[] getOwnedPresentation(IDiagram diagram) throws InvalidUsingException {
    return diagram.getPresentations();
}

Get presentation from model

With IElement#getPresentations() you can get all the presentations that will be “IPresentation#getModel() == element”.
Since IDiagram#getPresentations() will get the presentation in the diagram, exception handling is done in the example below.

public IPresentation[] getPresentation(IElement element) throws InvalidUsingException {
    if (element instanceof IDiagram) {
        throw new IllegalArgumentException("element is IDiagram.");
    }
    return element.getPresentations();
}

Get presentation of selected model on the diagram

With IDiagramViewManager#getSelectedPresentations() you can get presentation of a model that is currently selected on the diagram.

public IPresentation[] getSelectedPresentations(IViewManager viewManager) {
    IDiagramViewManager diagramViewManager = viewManager.getDiagramViewManager();
    return diagramViewManager.getSelectedPresentations();
}