Skip to main content

Overview of model editing

The extension allows you to edit models using the API as follows.

To add a model, use the AddNewRootModel method of the IProject object or the AddNewModel method of the IModel object. To set values ​​to fields, use the SetField method of the IModel object.

public void AddNewRootModel(ICommandContext c, ICommandParams p) 
{
//Create a model in the project
IModel ucModel = c.App.Workspace.CurrentProject.AddNewRootModel("UseCaseModel");

//Create a use case model and an actor model
IModel uc1 = ucModel.AddNewModel("UseCases", "UseCase");
uc1.SetField("Name", "Drive at a constant speed");
uc1.SetField("Description", "・・・");

IModel uc2 = ucModel.AddNewModel("UseCases", "UseCase");
uc2.SetField("Name", "Drive following");

IModel ac1 = ucModel.AddNewModel("Actors", "Actor");
ac1.SetField("Name", "Driver");
IModel ac2 = ucModel.AddNewModel("Actors", "Actor");
ac2.SetField("Name", "Control Panel");
}

To get the field value, use the GetField method or GetFieldString method of the IModel object.

public void GetField(ICommandContext c, ICommandParams p) 
{
//Get the current model
IModel model = c.App.Workspace.CurrentModel;

//Get the field value of "SomeField"
object someFieldValue = model.GetField("SomeField");

//Get the field value as a string
string someStringField = model.GetFieldString("SomeField");
}

To associate, use the Relate method of the IModel object. If you specify a reference field, the association will be performed, but if you specify an owned field, the association will not be performed.

public void Relate(ICommandContext c, ICommandParams p) 
{
//Get the model to be related
IModel uc1 = ..
IModel ac1 = ..
IModel ac2 = ..

//Specify the reference field of the use case to relate to the actor
uc1.Relate("MainActor", ac1);

//If the owned field of the use case is specified, the relationship will not be created
uc1.Relate("SubActors", ac2);
}

You can also use the SetField method of the IModel object to relate. If a reference field is specified, a reference relationship will be added, and if an owned field is specified, the model will be moved. For detailed usage, please refer to here.

public void Relate(ICommandContext c, ICommandParams p) 
{
//Get the model to be related
IModel uc1 = ..
IModel ac1 = ..
IModel ac2 = ..

//Specify the reference field of the use case to associate with the actor
uc1.SetField("MainActor", ac1);

//Specify the owned field of the use case to move the actor under the use case
//The model that was already set will be deleted
uc1.SetField("SubActors", ac2);
}

To move a model, use the MoveTo method of the IModel object, and to delete it, use the Delete method of the IModel object.

public void MoveToAndDelete(ICommandContext c, ICommandParams p) 
{
//Get the model to operate on
IModel uc1 = ..
IModel uc2 = ..

//Add a use case scenario
var scenario1 = uc1.AddNewModel("Scenarios", "Scenario");

//Add by specifying the index
var scenario2 = uc1.AddNewModelAt("Scenarios", "Scenario", "after", 0);

//Move the scenario
scenario1.MoveTo(uc2, "Scenarios", "after", 0);

//Delete the scenario
scenario2.Delete();
}