To get or modify models using Astah API, you need to access the project first.

section divider

How to access a project

Every time you access a project, you need to useĀ ProjectAccessor.

You can access only one project at a time. So when you modify more than one project, make sure you open only one project at a time and close before you move to another. Here’re the sample code.

Open/Close a project

ProjectAccessor prjAccessor = AstahAPI.getAstahAPI().getProjectAccessor();
    prjAccessor.open("C:\test.asml");
    prjAccessor.close();

Create/Save a project

ProjectAccessor prjAccessor = AstahAPI.getAstahAPI().getProjectAccessor();
    prjAccessor.create("C:\test.asml");
    prjAccessor.save();
    prjAccessor.close();

Create a project and save as

ProjectAccessor prjAccessor = AstahAPI.getAstahAPI().getProjectAccessor();
    prjAccessor.create("C:\test.asml");
    prjAccessor.saveAs("C:\test2.asml");
    prjAccessor.close();
section divider

Get project models

How to get the root of Astah SysML model tree

All model information in Astah SysML project is stored in a tree structure. The root of the tree is the project model. To get the project model, get the ProjectAccessor object, open the Astah SysML project file, then get Project Model (IModel object).

    ProjectAccessor prjAccessor = AstahAPI.getAstahAPI().getProjectAccessor();
    // Open project file 
    prjAccessor.open(inputFile);
    // Get project model
    IModel iModel = prjAccessor.getProject();

How to access Astah SysML model tree – Get all blocks recursively

Packages include Subsystems and Models.
Get all model elements directly under the package (IPackage) in an array by calling getOwnedElements() and get only packages abstracted from it.

/**
   * Get blocks under packages recursively.
   * 
   * @param iPackage
   *     Selected Package
   * @param iBlocks
   *     The list of all stored blocks
   * @return The list of all stored packages
   */
 private List getBlocks(IPackage iPackage) {
     List iBlocks = new ArrayList();
     INamedElement[] iNamedElements = iPackage.getOwnedElements();
     for (int i = 0; i < iNamedElements.length; i++) {
         INamedElement iNamedElement = iNamedElements[i];
         if (iNamedElement instanceof IPackage) {
            iBlocks.addAll(getBlocks((IPackage)iNamedElement));
         } 
         if (iNamedElement instanceof IBlock) {
            iBlocks.add(iNamedElement);
         }
     }
     return iBlocks;
 }