To get or modify models using Astah API, you need to access the project first.
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.
ProjectAccessor prjAccessor = AstahAPI.getAstahAPI().getProjectAccessor();
prjAccessor.open("C:\test.asml");
prjAccessor.close();
ProjectAccessor prjAccessor = AstahAPI.getAstahAPI().getProjectAccessor();
prjAccessor.create("C:\test.asml");
prjAccessor.save();
prjAccessor.close();
ProjectAccessor prjAccessor = AstahAPI.getAstahAPI().getProjectAccessor();
prjAccessor.create("C:\test.asml");
prjAccessor.saveAs("C:\test2.asml");
prjAccessor.close();
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();
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;
}