Delete objets using Py4C

Hi,

is it now possible to delete objects using Py4C (like OwnedDeploymentLinks …)?

thanks for your answer

d

You can use:

EObject.delete_e_object(myOwnedDeploymentLinks)

or

EObject.delete_all_e_objects([myOwnedDeploymentLinks1, myOwnedDeploymentLinks2])

It should update other element of the Capella model. But you might want to double check this.

Hello Yvan,

I tried you first answer, I have an error:

Do I do something wrong?

regards

d

You passed a Java object. You can wrap the java object in a Python object CapellaElement(myOwnedDeploymentLinks).

too strong for me!!!

it works, thanks

it’s difficult to navigate between java object et python ones…

regards

d

1 Like

You can have a look at the tips and tricks section of the documentation that explains this kinds of errors and how to navigate between the Python/Java APIs. It can help, but yes it takes some practice.

Hi Yvan,

I’m so sorry, I have another error that I don’t understand when I try to remove physical Components

I also try with “EObject…(CapellaElement(modules))”

Do you have an idea?

d

You are looping over a list of element and you are deleting an element of this list inside the loop which is forbidden by Java. You can add the element for deletion to a list and delete them after outside of the loop.

to_delete = []
for element in elements:
    ...
    to_delete.append(element)
    ...
EObject.delete_all_e_objects(to_delete)

Hi,

Thanks for your suggestion.

Nevertheless, I had an error:

but regarding your previous advice, it works in scripting:

Have a nice day

d

The error message says you are passing a Python list to the method delete but the code shows you are passing one element… You might want to check the type of to_delete[i]:

print(type(to_delete[i]))