Skip to main content

Add a shape to the editor

If you have associated a shape with a field in the view definition, the shape will be automatically added when you update the corresponding field, such as adding a model or adding an association.
If you have associated a shape with a class in the view definition, the shape will not be automatically added even if you update the model. To add such a shape, you can add it using the AddNodeShape method of the IDiagram object.

public void AddNodeShape(ICommandContext c, ICommandParams p) 
{
//Get the currently displayed diagram
IDiagram diagram = c.App.Workspace.CurrentEditor as IDiagram;
if (diagram == null)
{
//If no diagram is displayed, terminate the process
c.App.Output.WriteLine("sample", "Diagram not displayed.");
return;
}

//Add a model of the "Class" metaclass to the "OwnedElements" field of the diagram's parent model so that it becomes a sibling element of the diagram
IModel package = diagram.Model.Owner;
IModel newModel = package.AddNewModel("OwnedElements", "Class");

//Add a shape corresponding to the added model.
if (!diagram.CanAddNodeShape(newModel))
{
//If unable to add, terminate processing
c.App.Output.WriteLine("sample", $"Cannot add shape corresponding to model:{newModel.Name} to diagram:{diagram.Model.Name}.");
return;
}
diagram.AddNodeShape(newModel);
}

See How to delete shapes here for more information.