Is there a way to set bounds of components in diagram created by script?

Hi all,

I’m currently trying to modify the create_LAB_diagram script in order to define the positions of components created in the diagram

To do so I added a call to set_bounds function from diagram.py

# we define a function that will recursively walk the given LogicalComponent and it’s children to apply the given mapping.

def create_logical_component_mappings(parentContainer, logical_component, mapping):
    for child in logical_component.get_owned_logical_components():
        # the semantic element of the “LAB Logical Component” is the component Part
        newContainer = apply_mapping(parentContainer, mappingComponent, child.get_java_object().getAbstractTypedElements()[0])
        newContainer.getSemanticElements().add(logical_component.get_java_object())    
        
        set_bounds(newContainer, [10, 20, 50, 80])
    
        # we call the same function on the child LogicalComponent
        create_logical_component_mappings(newContainer, child, mapping)

And then encounter the following error: “the given diagram element has no GMF node.” Because eInverseByType does not find any object referencing the created container

It seems that the created DNodeContainerSpec (newContainer) is not “fully” created

I’ve tried adding a call to a refresh (org.eclipse.sirius.diagram.business.internal.dialect.DiagramDialectServices().refesh(…)) without any success

How could I set up the bounds of a component in diagram created through script?

Thanks in advance

You will need to open the diagram in Capella for Sirius to do its magic. Some DNode or DEdge might be recreated and GMF nodes will be attached. This process need information from the Display, that’s why it can’t be done without opening the diagram (as far as I understood). Depending on the depth of elements and which elements are visible, you might need to do this many times:

editor = Sirius.open_representation(my_diagram.get_java_object())
print("Openining diagram", my_diagram.get_name(), end="")
while editor.isDirty():
    print(".", end="")
    model = CapellaModel()
    model.session = Sirius.get_session(my_diagram.get_java_object())
    model.save()
    executeUI("org.eclipse.ui.PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().closeEditor(org.eclipse.ui.PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor(), True)")
    editor = Sirius.open_representation(my_diagram.get_java_object())
print(" done.")

This will open and close the diagram editor several times and leave it opened at the end. That’s not perfect from a user perspective but it works.