Skip to main content

Add a shape to the editor

If the shape definition in the ER diagram view definition maps to a class, adding a model will not add the shape to the ER diagram (hereinafter referred to as a class-specific shape. Related page).

To add a class-specific shape to an ER diagram, use the IDiagram.AddNodeShape method.

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.