icon

[Frage] Access ElementGroup [Gelöst]

Schlagworte:
  • ElementGroup
  • Access
  • Data
  • PythonParts
  • Script

Hi

I would like to access an element group that already exists in the model space.
When selecting the element group, I get the element (for example a line) that is part of the element group.
With GetParent, I can access the element group.
At least, this is what I can see when querying the element type.
Below, I have included the code lines.

elemente = self.post_element_selection.GetSelectedElements(self.coord_input.GetInputViewDocument())
ele_ad = AllplanElementAdapter.BaseElementAdapter(elemente)
line = AllplanBaseElements.GetElement(ele_ad) # get's the Line-Element that was selected

parent = AllplanElementAdapter.BaseElementAdapterParentElementService.GetParentElement(ele_ad)
type = parent.GetElementAdapterType().GetTypeName() # 'ElementGroup_TypeUUID'

I would like to be able to read all the elements of the element group so that I can use their geometry.
However, I don’t understand how this can be done.
Does anyone have a good idea on how to achieve this?

Kind regards,
Diego

Lösung anzeigen Lösung verbergen

Hi Diego,

If you are only interested in the geometry of all the elements in the group, this shouldn't be a problem.

As you already noticed, the result of the selection is not the element group directly (it is not possible in ALLPLAN to select an element group) but an element belonging to this group. You can get the group with the parent element service, as you did. Then you can use the ChildElementsService to get all the elements belonging to this group.

Here's a small code-snippet with a ScriptObject, that does exactly this and prints the selected groups and all the elements belonging to them (regardless, which elements were selected):

class ExampleScriptObjectInteractor(BaseScriptObject):
    def __init__(self,
                 build_ele: BuildingElement,
                 script_object_data: BaseScriptObjectData):
        """Default constructor"""

        super().__init__(script_object_data)
        self.build_ele        = build_ele
        self.selection_result = MultiElementSelectInteractorResult()

    def execute(self) -> CreateElementResult:
        """Execute the element creation"""
        return CreateElementResult()

    def start_input(self):
        self.script_object_interactor = MultiElementSelectInteractor(
            prompt_msg="Select elements",
            interactor_result = self.selection_result,
            )

    def start_next_input(self):
        self.script_object_interactor   = None

        ele_group_filter_function = AllplanIFW.QueryTypeID(AllplanEleAdapter.ElementGroup_TypeUUID)
        parent_elements = [AllplanEleAdapter.BaseElementAdapterParentElementService.GetParentElement(ele) for ele in self.selection_result.sel_elements]
        ele_group_filter = filter(ele_group_filter_function, parent_elements)
        selected_groups = AllplanEleAdapter.BaseElementAdapterList(list(ele_group_filter))

        print("Selected groups:")

        for i, group in enumerate(selected_groups):
            print(f"{i}. {group.GetModelElementUUID() }")
            children = AllplanEleAdapter.BaseElementAdapterChildElementsService.GetChildElements(group, True)
            for child in children:
                print(f"    - {child.GetElementAdapterType().GetTypeName()} ({child.GetModelElementUUID() })")

As you can see above, I used the QueryTypeID object as filtering function to get only the ElementGroup_TypeUUID objects. You can do it, because it's callable.

I hope that helps.

Cheers,
Bart

Hi Diego,

If you are only interested in the geometry of all the elements in the group, this shouldn't be a problem.

As you already noticed, the result of the selection is not the element group directly (it is not possible in ALLPLAN to select an element group) but an element belonging to this group. You can get the group with the parent element service, as you did. Then you can use the ChildElementsService to get all the elements belonging to this group.

Here's a small code-snippet with a ScriptObject, that does exactly this and prints the selected groups and all the elements belonging to them (regardless, which elements were selected):

class ExampleScriptObjectInteractor(BaseScriptObject):
    def __init__(self,
                 build_ele: BuildingElement,
                 script_object_data: BaseScriptObjectData):
        """Default constructor"""

        super().__init__(script_object_data)
        self.build_ele        = build_ele
        self.selection_result = MultiElementSelectInteractorResult()

    def execute(self) -> CreateElementResult:
        """Execute the element creation"""
        return CreateElementResult()

    def start_input(self):
        self.script_object_interactor = MultiElementSelectInteractor(
            prompt_msg="Select elements",
            interactor_result = self.selection_result,
            )

    def start_next_input(self):
        self.script_object_interactor   = None

        ele_group_filter_function = AllplanIFW.QueryTypeID(AllplanEleAdapter.ElementGroup_TypeUUID)
        parent_elements = [AllplanEleAdapter.BaseElementAdapterParentElementService.GetParentElement(ele) for ele in self.selection_result.sel_elements]
        ele_group_filter = filter(ele_group_filter_function, parent_elements)
        selected_groups = AllplanEleAdapter.BaseElementAdapterList(list(ele_group_filter))

        print("Selected groups:")

        for i, group in enumerate(selected_groups):
            print(f"{i}. {group.GetModelElementUUID() }")
            children = AllplanEleAdapter.BaseElementAdapterChildElementsService.GetChildElements(group, True)
            for child in children:
                print(f"    - {child.GetElementAdapterType().GetTypeName()} ({child.GetModelElementUUID() })")

As you can see above, I used the QueryTypeID object as filtering function to get only the ElementGroup_TypeUUID objects. You can do it, because it's callable.

I hope that helps.

Cheers,
Bart

Hi Bart

Many thanks for your input. The example helped me and solved my problem.
I can now access the geometry of all elements in the element group.

Thank you very much
cheers
Diego