Hide diagram elements with P4C

Hello,
I am trying to modify a diagram from a Python4Capella script, what I want to do is:
-Clone a diagram with P4C
-Hide some diagram elements with P4C
Is it possible to perform these operations, or P4C doesn’t allow them?

For the second topic another possible way around that I imagined could be to hide the diagram elements basing on some property elements values or attributes values, obviously if it’s possible to set those values with P4C.

At the end what I am interested in are requirement elements in the diagram, I would like to clone a diagram where requirements are present, hide some requirements blocks (filtering on information that is in the code) either modifing directly the diagram if possible or modifiing a value that then will hide the requirement.

Is this achievable with P4C? Which of these things can be done and what cannot be done?

To hide and reveal elements on a diagram you can use:

org.eclipse.sirius.diagram.business.api.helper.graphicalfilters.HideFilterHelper.INSTANCE.hide(diagramElement)
org.eclipse.sirius.diagram.business.api.helper.graphicalfilters.HideFilterHelper.INSTANCE.reveal(diagramElement)

You can use an instance of the Python class Diagram to get the list of diagram elements:

myDiagram.get_java_object().getRepresentation().getRepresentationElements()

On a representation element you can use getTarget() to get the referenced semantic element (an element from the Capella model).

To copy a representation you can use:

e_object_query = org.eclipse.sirius.business.api.query.EObjectQuery(myDiagram.get_java_object())
org.eclipse.sirius.business.api.dialect.DialectManager.INSTANCE.copyRepresentation(myDiagram.get_java_object(), 'My representation Copy', e_object_query.getSession(), None)

And if I want to do that from inside a cycle that is iterating on Requirement elements: “for req in se.get_all_contents_by_type(Requirement)”
how do I get the the diagram (in your script “myDiagram”) from the “req” element of the cycle?

myDiagram should be the diagram you want to copy/edit.

From an element, you can list all diagrams it appears on:

for myDiagram in req.get_representing_diagrams():
    ...

You can open the capella.py and select the Diagram class, then use CTRL+SHIT+G to show all references to the Diagram class in the simplified Python API. That will give you all the methods that returns Diagram instances.

Hi !

I"m trying to do something similar: hide all the component exchanges in a given diagram, with the following code:

component_exchanges_java_objects = [x.get_java_object() for x in se.get_all_contents_by_type(ComponentExchange)] 
    
for diagram in model.get_all_diagrams():
    if diagram.get_name() == "[LAB] XXX":
        for elt in diagram.get_java_object().getRepresentation().getRepresentationElements():
            if elt.getTarget() in component_exchanges_java_objects :
                print("found Component Exchange, hiding", elt)
                   org.eclipse.sirius.diagram.business.api.helper.graphicalfilters.HideFilterHelper.INSTANCE.hide(elt)

but when I go back to the diagram, the component exchanges are still visible, even after a refresh. I’m I missing something ?

Your code seems correct. Did you run it in a transaction and committed it ?
You might also check if you can do that from the Capella UI… But I think it should not be a problem.

Yes it is part of a transaction.

I used the following code and it works:

# start a transaction to modify the Capella model
model.start_transaction()
try:
    component_exchanges_java_objects = [x.get_java_object() for x in se.get_all_contents_by_type(ComponentExchange)] 
    
    for diagram in model.get_all_diagrams():
        if diagram.get_name() == "[LAB] IFE System - All Components, CEs":
            for elt in diagram.get_java_object().getRepresentation().getRepresentationElements():
                if elt.getTarget() in component_exchanges_java_objects :
                    print("found Component Exchange, hiding", elt)
                    org.eclipse.sirius.diagram.business.api.helper.graphicalfilters.HideFilterHelper.INSTANCE.hide(elt)
except:
    # if something went wrong we rollback the transaction
    model.rollback_transaction()
    raise
else:
    # if everything is ok we commit the transaction
    model.commit_transaction()

# save the Capella model
model.save()

The only change in your code was the indentation on the hide line. So maybe that was the issue.

the indentation was just a error in my post, not in my code :slight_smile: . I tried putting the code in its own transaction, but it doesnt change anything :frowning: .

Did you try to show/hide element via Capella UI ? Something might prevent it in your case…

Yes, it works fine :frowning:

Can you try the above code on the IFE sample project ?
Do you have any errors in the console or in the Error log view after running the script ?

It works on the sample project. If I run the script twice on my project, I get the expected result ^^

ok… I don’t really understand why this could happen but it kind of works.