from ansa import guitkfrom ansa import basefrom typing import List, Tuple, Dict, Union
deck = base.CurrentDeck()PARTTYPETUPLE:Tuple[str] = ('ANSAPART', 'ANSAGROUP')VIEWCOLNAMETUPLE:Tuple[str] = ('Module Id', 'Name', 'Status')
def setItemText(item:object, name:str, id:str='')->None: ''' 对输入的item设置名称和ID ''' guitk.BCListViewItemSetText(item, VIEWCOLNAMETUPLE.index('Name'), name) if id: guitk.BCListViewItemSetText(item, VIEWCOLNAMETUPLE.index('Module Id'), id) return None def createItem(parentItem:object, part:base.Entity, isTop:bool=False)->object: ''' 功能: 为输入的part在parentItem下创建一个新的item 参数: parentItem object 准备新创建item的父节点,为item或listView类型 part: Entity 一个part或group对象 isTop bool 判断需要创建节点是否为根节点的判断器 返回值: 返回生成的item对象 ''' if not isTop: item:object = guitk.BCListViewItemAddChild(parentItem) else: item:object = guitk.BCListViewAddTopLevelItem(parentItem) if part.ansa_type(deck)==PARTTYPETUPLE[0]: partId: str = part.get_entity_values(deck, ['Module Id', ])['Module Id'] else: partId: str = '' setItemText(item, part._name.strip(), id=partId) guitk.BCListViewItemSetUserData(item, part) return item
def loadModelTree03()->None: groupIter:List[base.Entity] = base.CollectEntitiesI(deck, None, PARTTYPETUPLE) createItemsAsDepth(leftModelView, groupIter, 0) return None
def createItemsAsDepth(parentItem:object, groups:List[base.Entity], depth:int)->None: isTop:bool = True if depth==0 else False for group in groups: if base.GetPartDepth(group)['depth']!=depth:continue name:str = group._name.strip() try: item = leftPartToItemDict[name] except KeyError: item = createItem(parentItem, group, isTop=isTop) leftPartToItemDict[name] = item if group.ansa_type(deck)==PARTTYPETUPLE[0]:continue newGroups = base.CollectEntitiesI(deck, group, PARTTYPETUPLE) createItemsAsDepth(item, newGroups, depth+1) return None
if __name__ == '__main__': leftPartToItemDict:Dict[str, List[object]] = dict() mainWindow:object = guitk.BCWindowCreate('结构树构造', guitk.constants.BCOnExitDestroy) mainBox:object = guitk.BCVBoxCreate(mainWindow) leftModelView:object = guitk.BCListViewCreate(mainBox, 3, VIEWCOLNAMETUPLE, True) guitk.BCListViewSetIsRootDecorated(leftModelView, True) guitk.BCListViewSetFilterEnabled(leftModelView, True) guitk.BCListViewSetSelectionMode(leftModelView, guitk.constants.BCMulti) loadModelTree03() guitk.BCShow(mainWindow)