How to copy all elements of a business process diagram and paste them as model elements into another BP diagram using openAPI

Hello guys
Please consider this case :
I have two business process diagram ( IDiagramUIModel diagram1 and IDiagramUIModel diagram2)
I want to copy all elements from diagram1 and paste them as model elements into diagram2 with a plugin
Is there any simple functionality in openAPI for this action?

Any idea ?

Hi Mamad,

Thank you for your post. I have forwarded your question to our engineers. When there is any update, I will let you know.

Best regards,
Jick Yeung

Hi Mamad,

The steps involve duplicating the elements and adding them to the target diagram. The pseudocode below shows how to program copy and paste (as model) via the Open API.

IDiagram diagramA = …; // assuming that diagramA is the source
IDiagram diagramB = …; // diagramB is the target

IDiagramElement[] deAs = diagramA.toDiagramElementArray();
for (IDiagramElement deA : deAs) {
IDiagramElement deB = deA.duplicate(); // copied A’s shape/connector
if (deA.getModelElement() != null) {
IModelElement meB = deA.getModelElement().duplicate(); // copied A’s shape’s model element
deB.setModelElement(meB);
}

daigramB.addDiagramElement(deB); // add the new shape/connector into B
}

Please try. Feel free to let us know if you have any questions in further.

Best regards,
Jick Yeung

Hi @Jick and thank you for your reply
I tried your code but it seems it does not work !
Actually no shape or model element will add to diagramB after running this code and its still empty !

Let us check…

Hi Mamad,

The code below works for us. Could you take a look?

    IDiagramUIModel diagramA = ApplicationManager.instance().getDiagramManager().getActiveDiagram();
    IDiagramUIModel diagramB = ApplicationManager.instance().getDiagramManager().createDiagram(DiagramManager.DIAGRAM_TYPE_COMMUNICATION_DIAGRAM);

    IDiagramElement[] deAs = diagramA.toDiagramElementArray();
    for (IDiagramElement deA : deAs) {
    IDiagramElement deB = deA.duplicate(); // copied A’s shape/connector
    if (deA.getModelElement() != null) {
    IModelElement meB = deA.getModelElement().duplicate(); // copied A’s shape’s model element
    deB.setModelElement(meB);
    }

    diagramB.addDiagramElement(deB); // add the new shape/connector into B
    }

Best regards,
Jick Yeung

:thinking:

Hi Mamad, does it work?