Create Diagram Image Shape with API

Your diagrams are not necessary to be modeled just with the standard notations. With the Diagram Image Shapes, you can enrich your diagram with customized image files. The Diagram Image Shapes can be created via the prattle of the diagram, as well as using Open API. This article will show you how to create Diagram Image Shapes from an image file deployed in your plugin using Open API.

Having your diagram ready

Before creating the Diagram Image Shape, we have to obtain the diagram where the shape will be hosted. This can be a brand new diagram, or an existing diagram in your project. Suppose you would like to create the Diagram Image Shape on a brand new diagram. you can use the diagramManager.createDiagram() method to create a new diagram.

DiagramManager diagramManager = ApplicationManager.instance().getDiagramManager();
IDiagramUIModel diagram = diagramManager.createDiagram(DiagramManager.DIAGRAM_TYPE_OVERVIEW_DIAGRAM);
diagram.setName("Image Shape Test");

Or if you want to create the Diagram Image Shape on an existing diagram, you can use the toDiagramArray() method from IProject, then loop through the array to find out your target diagram.

Create Diagram Image Shape to your diagram

Once the diagram is ready, you can then go ahead to create the Diagram Image Shape. The Diagram Image Shape can be created using the createDiagramElement() method from DiagramManager.

IImageShapeUIModel imageShape = (IImageShapeUIModel) diagramManager.createDiagramElement(diagram, IModelElementFactory.instance().createImageShape());

Now we have to specify the image. Since the image file is deployed along with the plugin, you can define a static variable in your plugin class to obtain the plugin folder from the VPPluginInfo which passed as parameter in the loaded() method.

public class CreateDiagramImageShapePlugin implements VPPlugin {
  public static java.io.File PluginDir;
  public void loaded(VPPluginInfo aArg0) {
    PluginDir = aArg0.getPluginDir();
  }
  public void unloaded() {}
}

Now in your controller class you can use this static variable to obtain the plugin folder and determine the path of your image file, and put it into the image shape. Since the image is from an external file, therefore the image shape need to specify the image mode as linked. This is to make sure the image shape can be loaded after you save and reopen the project file.

File imageFile = new File(CreateDiagramImageShapePlugin.PluginDir, "images/vp_logo.png");
imageShape.setImagePath(imageFile.getAbsolutePath());
imageShape.setMode(IImageShapeUIModel.LINKED);

Finally we can specify the image object to the image shape, where should it created as well as show up the diagram.

try{
  imageShape.setImage(ImageIO.read(imageFile));
  imageShape.setBounds(100, 100, 284, 137);
  diagramManager.openDiagram(diagram);
} catch (IOException e) {
  ApplicationManager.instance().getViewManager().showMessage(e.getMessage());
  e.printStackTrace();
}

Sample Plugin

This sample plugin demonstrate how to create Diagram Image Shape into an Overview diagram. After deploy the plugin to VP applications, you can click on the plugin button in application toolbar to trigger it.

Create diagram image shape from plugin menu

Create diagram image shape from plugin menu

 

This will create an Overview Diagram with a Diagram Image Shape.

Diagram image shape created

Diagram image shape created

Download Sample Plugin

You can click this link to download the sample plugin.

Related Articles

1322 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply