Creating Diagram Element with User-Defined image

Within VP, we are trying to model cloud based technologies on a diagram, similar to what you can do within the web version of VP.
To achieve this we use an ArchiMate diagram. We then add an Application Interface to the diagram which creates model element within the model explorer as well as a diagram element. From there you can simply right-click on the diagram element go to presentation options and change the image by selecting an image (example below).

Screenshot 2022-02-08 at 13.40.42

However is there a way to change the image using the APIs?

I have tried to achieve this by doing something like …

IDiagramUIModel activeDiagram = diagramManager.getActiveDiagram();
		
String dir = Paths.get("../plugins/plugin/lib/assets").toAbsolutePath().normalize().toString();
String imagePath = dir + "/App Engine.png";
System.out.println(">>>> " + imagePath); //eg; /Applications/Visual Paradigm.app/Contents/Resources/app/plugins/plugin/lib/assets/App Engine.png

//create model element
IArchiMateApplicationInterface technologyInterface = (IArchiMateApplicationInterface) IModelElementFactory.instance().create(IModelElementFactory.MODEL_TYPE_ARCHI_MATE_APPLICATION_INTERFACE);
technologyInterface.setName("App Engine");

//create diagram element
IArchiMateApplicationInterfaceUIModel interfaceUIModel = (IArchiMateApplicationInterfaceUIModel) diagramManager.createDiagramElement(activeDiagram, technologyInterface);

//shape props for diagram element
IShapeUIModel shape = (IShapeUIModel) interfaceUIModel;
shape.setDisplayImagePath(imagePath);

I tried using IImageShapeUIModel however I get the following error when casting from IArchiMateApplicationInterfaceUIModel.

IImageShapeUIModel imgShape = (IImageShapeUIModel) interfaceUIModel;

File imageFile = new File(imagePath);

imgShape.setMode(IImageShapeUIModel.EMBEDDED);
imgShape.setImagePath(imageFile.getAbsolutePath());					
imgShape.setImage(ImageIO.read(imageFile));

Error: java.lang.ClassCastException: v.bem.ad cannot be cast to com.vp.plugin.diagram.shape.IImageShapeUIModel

I’m currently using VP 16.1, build: 20220110ab

Any help to apply a user-defined image using the APIs would be much appreciated.

To solve this problem, use the sample below:

    IDiagramUIModel diagram = ApplicationManager.instance().getDiagramManager().getActiveDiagram();
    IShapeUIModel shape = diagram.toShapeUIModelArray()[0];
    
    ProjectManager projectManager = ApplicationManager.instance().getProjectManager();
    
    File imageFile = new File("e:\\MobiusOne.jpg");
    String imagePath = projectManager.getShapeDisplayImagePath(imageFile, shape.getShapeType());
    try {
        projectManager.importShapeDisplayImage(imagePath, imageFile, projectManager.getProject());
    } catch (Exception e) {
        e.printStackTrace();                    
    }
    shape.setDisplayImagePath(imagePath);
    shape.resetCaption();

Thanks VP support!

Thanks GregS for posting the solution! :slight_smile: :clap: