import FreeCAD as App

class Asm4StructureParseUseCase:
    _parts = []
    _label = []

    def getSubPartsLabel(self, group):
        groupLabel = []
        for el in group:
            if str(el) == '<Part::PartFeature>':
                groupLabel.append(el.Label)
        return groupLabel

    def parseLabel(self, nextGroup, label, level=2, nextGroupParse=0):
        if nextGroup.__len__() == nextGroupParse:
            return
        else:
            groupParts = []

            for el in nextGroup:
                if str(el) == '<App::Link object>':
                    groupParts.append(el)

            for el in groupParts:
                if str(el) == '<App::Link object>':
                    label.append({
                        "level": level,
                        "attachedTo": el.AttachedTo.split('#'),
                        "label": el.Label,
                        "axis": self.getSubPartsLabel(el.Group)
                    })

    def initParse(self):

        model = App.ActiveDocument.RootObjects[1]
        self._label.append({
            "level": 1,
            "attachedTo": "Parent Assembly",
            "label": model.Label,
            "axis": self.getSubPartsLabel(model.Group)
        })
        for parent in model.Group:
            if str(parent) == '<App::Link object>':
                self._label.append({
                    "level": 1,
                    "attachedTo": parent.AttachedTo.split('#'),
                    "label": parent.Label,
                    "axis": self.getSubPartsLabel(parent.Group)
                })
        print(self._label)


 