I’m trying to create treeViwer to show the elements from Project browser with the following preliminary code
loadModule('/System/UI')
loadModule('/System/UI Builder')
loadModule('/System/Resources')
# Include needed for the Capella modeller API
include('workspace://Python4Capella/simplified_api/capella.py')
if False:
from simplified_api.capella import *
# Include needed for utilities
include('workspace://Python4Capella/utilities/CapellaPlatform.py')
if False:
from utilities.CapellaPlatform import *
aird_path = '/In-Flight Entertainment System/In-Flight Entertainment System.aird'
model = CapellaModel()
model.open(aird_path)
se = model.get_system_engineering()
print("System Engineering:", se.get_name())
def get_model_hierarchy():
"""Retrieve only the Operational Analysis as the root element for the TreeViewer."""
operational_analysis = se.get_operational_analysis()
if operational_analysis:
print("Root Element Retrieved:", operational_analysis.get_name())
return [operational_analysis] # Return the actual Operational Analysis object
else:
print("No Operational Analysis found.")
return []
def get_children():
"""Retrieve children by navigating the containment structure using eContainer."""
element = getProviderElement()
if hasattr(element, "get_all_contents"):
children = element.get_all_contents()
print(f"{element.get_name()} has {len(children)} children.")
return children if children else None
else:
print(f"{element.get_name()} has no children.")
return None
def get_resource_size():
"""Callback function to provide a size metric for each element."""
element = getProviderElement()
if hasattr(element, "get_all_contents"):
child_count = len(element.get_all_contents())
return f"{child_count} children"
return "Size not available"
# UI setup with tree viewer, columns, and comparator
createView("Operational Analysis Hierarchy")
viewer = createTreeViewer(get_model_hierarchy(), get_children)
# Create columns for the viewer
createViewerColumn(viewer, "Element", createLabelProvider(lambda: getProviderElement().get_name()), 4)
createViewerColumn(viewer, "Size", createLabelProvider(get_resource_size), 1)
# Add comparator to sort elements
createComparator(viewer, "return (getProviderElement().get_all_contents() != null) ? 1 : 2;")
But I’m not able to show the treViewer, by any chance, does anyone know how to view the project browser?. Any help would be appreciated
I was not able to make this works. The first level is displayed as expected but when I try to expend the first level I have empty list passed to the label and content providers… I’m not sure why. Maybe you will figure it out, if you do let me know what was wrong.
def get_model_hierarchy():
"""Retrieve only the Operational Analysis as the root element for the TreeViewer."""
operational_analysis = se.get_operational_analysis()
if operational_analysis:
print("Root Element Retrieved:", operational_analysis.get_name())
return [operational_analysis.get_java_object()] # Return the actual Operational Analysis object
else:
print("No Operational Analysis found.")
return None
def get_children(element):
"""Retrieve children by navigating the containment structure using eContainer."""
print("get_children()", element)
try:
children = element.eContents()
print(f"{element.getName()} has {len(children)} children.")
res = children
print(res)
return res
except Exception:
return None
def get_name_label(element):
"""Retrieve children by navigating the containment structure using eContainer."""
print("get_name_label()", element)
try:
return element.getName()
except Exception:
return "<No name>"
def get_size_label(element):
"""Callback function to provide a size metric for each element."""
try:
child_count = len(element.eContents())
return f"{child_count} children"
except Exception:
return "No children"
# UI setup with tree viewer, columns, and comparator
createView("Operational Analysis Hierarchy")
viewer = createTreeViewer(get_model_hierarchy(), "get_children(getProviderElement())")
# Create columns for the viewer
createViewerColumn(viewer, "Element", createLabelProvider("get_name_label(getProviderElement())"), 4)
createViewerColumn(viewer, "Size", createLabelProvider("get_size_label(getProviderElement())"), 1)
# Add comparator to sort elements
# createComparator(viewer, "return (getProviderElement().get_all_contents() != null) ? 1 : 2;")
I have been following the discussion on this topic thread with great interest. Could you kindly guide me to the relevant documentation that I should review to effectively execute the tasks discussed?
The EASE documentation on the UI can be found here. There are also some examples but they are in Javascript.
And finally you can have a look at the GUI section of the Python4Capella tips and tricks. I Added some working examples for Python. One important point is that callbacks are strings with the code that need to be interpreted in Python where in Javascript the function itself is passed.
If you have any progress on the tree viewer or a table viewer please contribute to this thread.
Thank you for reply Yvan, I appreciate it. Maybe I should search alternative to treeViewer that able to show the project browser from UI.
Here an example of ListViewer that could be help for someone
def get_architecture_elements(element, level=0):
elements = []
indent = " " * level
element_name = getattr(element, "get_name", lambda: f"Unnamed Element ({type(element).__name__})")()
elements.append(f"{indent}{element_name}")
children = element.get_contents() if hasattr(element, "get_all_contents") else []
for child in children:
elements.extend(get_architecture_elements(child, level + 1))
return elements
def get_model_hierarchy():
main_architectures = [se.get_operational_analysis()]
unique_elements = set()
hierarchy = []
for architecture in main_architectures:
if architecture is not None:
elements = get_architecture_elements(architecture)
for element in elements:
if element not in unique_elements:
unique_elements.add(element)
hierarchy.append(element)
return hierarchy
class ModelTreeDialog:
def __init__(self):
self.listViewer = None
self.elements = get_model_hierarchy()
def build(self):
print("Building UI components...")
scrolledComposite = createScrolledComposite("1/1 o! o!")
pushComposite(scrolledComposite)
self.listViewer = createListViewer(self.elements, layout="o! o!")
print("List viewer created:", self.listViewer)
popComposite() # Pop the scrolled composite to return to the main composite
dialog_instance = ModelTreeDialog()
javaDialog = createDialog("dialog_instance.build()", "Capella Model Hierarchy", "Browse architectures and their elements")
result = executeUI("javaDialog.open()")
You probably can ask on the EASE development mailing list or on the EASE bugzilla what we are missing here. It’s probably not far from a working script. They moved to gitlab. Please link your question here so I can keep track to provide new sample script and/or improve the documentation.