This blog post is about how to create model documents by using the Sparx Enterprise Architect (EA) API. Model documents are part of EA Virtual Documents and are modeled as child elements of report packages. Therefore the creation of model documents described hereafter requires an existing report package. Of course, report packages can also be created using the Sparx EA API:

var reportPackageName = "Arbitrary";
var parentPackage = repository.GetPackageByGuid("{00000000-0000-0000-0000-000000000000}");

var childPackages = parentPackage.Packages;
var reportPackage = (Package)childPackages.AddNew(reportPackageName, "Package");
reportPackage.Update();

reportPackage.Element.Stereotype = "report package";
reportPackage.Alias = "report package";
reportPackage.Version = "1.0";
reportPackage.Element.Status = "Proposed";
reportPackage.Update();

reportPackage = repository.GetPackageByGuid(reportPackage.PackageGUID);

var taggedValue = (TaggedValue) reportPackage.Element.TaggedValues.GetByName("ReportTitle");
taggedValue.Value = "Arbitrary report title";
taggedValue.Update();

reportPackage = repository.GetPackageByGuid(reportPackage.PackageGUID);

First a new package is added to the child package collection of an existing package. After that the stereotype is set to report package and some other properties of the package like version and status are set as desired. The call to the Update method triggers the update request to the underlying database. Last but not least the tagged value with name ReportTitle is set.

In the above code the report package is reloaded multiple times to ensure that the object always reflects the applied changes (i.e. Guid of package, added tagged values, …).

Now the report package is ready to be used as parent for model document creation.

var modelDocumentElement = (Element)reportPackage.Elements.AddNew("Model document name", "Class");
modelDocumentElement.TreePos = 1;
modelDocumentElement.StereotypeEx = "model document";
modelDocumentElement.Update();

modelDocumentElement = repository.GetElementByGuid(modelDocumentElement.ElementGUID);

var taggedValue = (TaggedValue)modelDocumentElement.TaggedValues.GetByName("RTFTemplate");
taggedValue.Value = "Arbitrary Template";
taggedValue.Notes = "Default: Model Report";
taggedValue.Update();

var position = 1;
foreach (Package package in packages)
{
                var attribute = modelDocumentElement.Attributes.AddNew(package.Name, "Package");
                attribute.ClassifierID = package.Element.ElementID;
                attribute.Pos = position;
                attribute.Update();

                position++;
}

modelDocumentElement = repository.GetElementByGuid(modelDocumentElement.ElementGUID);

The creation of a model document is similar to the creation of a report package. A new element of type Class with stereotypeEx model document has to be added to the elements collection of a report package. The TreePos defines the position of the model document in the tree and is therefore used for ordering purposes. To define the packages a model document refers to, an attribute of type Package has to be added to the attributes collection of the model document for each package. Besides the classifier, which has to be set to the element Id of the package, the position of the package reference can be defined by setting the Pos property.

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.