Skip to main content

Delete a shape from the editor

If you have associated a shape with a field in the view definition, the shape will be automatically deleted when you update the corresponding field, such as deleting a model or deleting an association.
Similarly, if you have associated a shape with a class in the view definition, the shape will be automatically deleted when you delete the model. If you want to keep the model and delete only the shape, you can use the Delete method of the IShape object.

public void Delete(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, end the process
c.App.Output.WriteLine("sample", "No diagram is displayed.");
return;
}

//Get the currently selected shape
INode node = diagram.GetSelectedShapes().OfType<INode>().FirstOrDefault();
if (node ​​== null)
{
//If no shape is selected, end the process
c.App.Output.WriteLine("sample", "No shape is selected.");
return;
}
//If true is specified as the argument, the model corresponding to the shape will also be deleted
//If true is specified as the argument If false or nothing is specified, the model corresponding to the shape will not be deleted.
node.Delete(false);
}

Please refer to Adding a shape here for more information.