icon

Support Forum

Create Group with ElementGroupElement


Hi there,

I am currently trying to group 2 elements under one single group. In doing so, I was hoping to enforce a Parent-child relationship where the two selected elements would be the child of the group.

For some reason that I can not comprehend, the group is being created with the given name, but there are no elements associated with it. Furthermore, when I select this group, no elements are highlighted. Please find enclosed the script I am using in the .txt format.

Additionally, is there a good example of how to create a group using SingleElementSelectInteractor or MultiElementSelectInteractor?
I couldn't find one.

Looking forward to hearing from you.

Cheers,

Anhänge (1)

CreateGroup.txt
Erweiterung der Datei passt nicht zum Inhalt!
Typ: text/x-script.python
0-mal heruntergeladen
Größe: 3,24 KiB

HI,

apparently giving a python script a .txt extension doesn't change anything Can you share the script in a different way?

There's no such example because creating an element group with Python API is possible, only when you also create the elements in that group. In other words: you can create the elements and the group around it. But not select existing elements and group them. At least that is something I have never seen and never tried. This is because to create a group, you neet to create the ElementGroupElement and this requires a list of AllplanElements. As a result of selection you don't get the AllplanElement directly, but only a list of BaseElementAdapters. It is possible for some objects to get the AllplanElement from the Adapter. So your approach should theoretically be achievable, but only for certain types of objects.

Cheers,
Bart

Dear Bart

Here is the script I am currently using. Please let me know what your thoughts are on this.
This script is able to create a group, but it comes empty without any child elements for some .ifc models, but for others, it actually comes with child elements while duplicating the elements and placing them randomly in space.

from typing import TYPE_CHECKING

from NemAll_Python_BaseElements import CommonProperties
from NemAll_Python_BasisElements import (ModelElement2D,
                                         ModelElement3D,
                                         ElementGroupProperties,
                                         ElementGroupElement)




from CreateElementResult import CreateElementResult
from ScriptObjectInteractors.SingleElementSelectInteractor import (
    SingleElementSelectInteractor, SingleElementSelectResult
)
from TypeCollections.ModelEleList import ModelEleList
from BaseScriptObject import BaseScriptObject, BaseScriptObjectData

if TYPE_CHECKING:
    from __BuildingElementStubFiles.InputInteractorBuildingElement import (
        InputInteractorBuildingElement as BuildingElement
    )
else:
    from BuildingElement import BuildingElement


def check_allplan_version(_build_ele: BuildingElement, _version: str) -> bool:
    return True  # support all versions


def create_script_object(
    build_ele: BuildingElement,
    script_object_data: BaseScriptObjectData
) -> BaseScriptObject:
    return GroupTwoElements(build_ele, script_object_data)


class GroupTwoElements(BaseScriptObject):
    def __init__(self, build_ele, data: BaseScriptObjectData):
        super().__init__(data)
        self.build_ele = build_ele
        self.sel1 = SingleElementSelectResult()
        self.sel2 = SingleElementSelectResult()
        self.step = 0

    def start_input(self):
        self.step = 1
        self.script_object_interactor = SingleElementSelectInteractor(
            self.sel1, prompt_msg="Select first element"
        )

    def start_next_input(self):
        if self.step == 1:
            self.step = 2
            self.script_object_interactor = SingleElementSelectInteractor(
                self.sel2, prompt_msg="Select second element"
            )
        else:
            self.script_object_interactor = None

    def execute(self) -> CreateElementResult:


        basis_elements = []
        for sel in (self.sel1, self.sel2):
            adapter = sel.sel_element

            # 1) Get the element's own properties & *local* geometry
            cp = adapter.GetCommonProperties()
            geom = adapter.GetGeometry()   
            # <<-- LOCAL geometry!

            # 2) Wrap in a true BasisElement
            if adapter.Is3DElement():
                elem = ModelElement3D(cp, geom)
            else:
                elem = ModelElement2D(cp, geom)

            basis_elements.append(elem)

        print("SELECTED ELEMENTS", basis_elements)

        # 3) Prepare group properties
        group_cp = CommonProperties()
        group_cp.GetGlobalProperties()        # <<-- place group in model
        group_props = ElementGroupProperties()
        group_props.Name           = "MyGroup"
        group_props.ModifiableFlag = True     # <<-- allow opening to show children
        # group_props.SubType     = <whatever subtype you need>

        # 4) Create the group in *local* coords
        group = ElementGroupElement(group_cp, group_props, basis_elements)

        # 5) Return it
        ml = ModelEleList()
        ml.append(group)
        return CreateElementResult(elements=ml)

Cheers.