ANSA自带脚本SegregatePID.py(路径:..\BETA_CAE_Systems\ansa_v22.0.0\scripts\Properties)主要功能是:若一个property中存在多个部件,利用该脚本可以对这个property进行拆分,拆分后一个部件对应一个property,且每个新的prop属性和初始prop完全一致。
'''==================================================* HEADER==================================================Developed by:Nikolas DrivakosDate:15/02/2013BETA CAE SystemsKato Scholari, Thessaloniki,GR-57500, Epanomi,GreeceTel: +302392021420 , +302311993300Fax: +302392021417E-mail: ansa@beta-cae.comURL: http://www.beta-cae.comCopyright (c) 2011 BETA CAE SystemsAll rights reserved.=================================================* DISCLAIMER=================================================BETA CAE Systems assumes no responsibility orliability for any damages, errors, inaccuraciesor data loss caused by installation or use of thissoftware.=================================================* HISTORY=================================================Date: Updater:Modifications:-------------------------------------------------Date: Updater:Modifications:-------------------------------------------------''''''Name:SegregatePIDDescription: It assigns new PID to all "islands" of faces or FE that belong to the selected properties / FE'''import ansafrom ansa import basefrom ansa import constantsdef SegregatePIDMain():"""It assigns new PID to all "islands" of faces or FE that belong to the selected properties / FE"""props = base.PickEntities(constants.NASTRAN, ("PSHELL",))if not props:print('No PSHELLs were found')return 1m_faces = dict()m_elems = dict()for prop in props:faces = base.CollectEntities(constants.NASTRAN, prop, "FACE", True)if not faces:continueelem_in_faces = base.CollectEntities(base.NASTRAN, faces, "SHELL", True)for face in faces:if face in m_faces:continuebase.Or(face)base.Neighb('ALL')new_faces = base.CollectEntities(constants.NASTRAN, new_faces, "FACE", filter_visible=True)prop_new = base.CopyEntity(None, prop)for new_face in new_faces:base.SetEntityCardValues(constants.NASTRAN, new_face, {"PID":prop_new._id})m_faces[new_face] = 1for elem_in_face in elem_in_faces:m_elems [elem_in_face] = 1del m_faceselems = base.CollectEntities(constants.NASTRAN, props, "SHELL")for elem in elems:if elem in m_elems:continueret = base.GetEntityCardValues(NASTRAN, elem, ("PID",))old_prop = base.GetEntity(constants.NASTRAN,"PSHELL", ret['PID'])base.Or(elem)base.Neighb("ALL")prop_new = base.CopyEntity(None, old_prop)new_elems = base.CollectEntities(constants.NASTRAN, None, "SHELL", filter_visible=True)for new_elem in new_elems:base.SetEntityCardValues(constants.NASTRAN, new_elem, {"PID":prop_new._id})m_elems[new_elem] = 1
elem_in_faces = base.CollectEntities(constants.NASTRAN, faces, "SHELL", True)new_faces = base.CollectEntities(constants.NASTRAN, None, "FACE", filter_visible=True)ret = base.GetEntityCardValues(constants.NASTRAN, elem, ("PID",))from ansa import basefrom ansa import constantsfrom typing import List, Union, TupleDECK = constants.NASTRANdef SegregatePIDMain():# 拾取待拆分的propertyprops:Union[List[base.Entity], None] = base.PickEntities(DECK, ("PSHELL",))if not props:print('当前模型不存在propety,请先导入模型...')return None# 按face的独立性进行拆分elemInFaces:List[base.Entity] = list()for prop in props:faces:List[base.Entity] = base.CollectEntities(DECK, prop, 'FACE', recursive=True)if not faces:continuewhile faces:newFaces:List[base.Entity] = base.GatherFaces(faces[0], 40.0, visibility='visible', container='PID_region')newProp:base.Entity = base.CopyEntity(None, prop)_ = [newFace.set_entity_values(DECK, {'PID': newProp._id}) for newFace in newFaces]faces = list(set(faces)-set(newFaces))elemInFaces.extend(base.CollectEntities(DECK, faces, ('SHELL', 'SOLID'), recursive=True))# 按网格的独立性进行拆分for prop in props:elems:List[base.Entity] = base.CollectEntities(DECK, prop, ('SHELL', 'SOLID'), recursive=True)elems = list(set(elems)-set(elemInFaces))while elems:elem = elems[0]newElems:List[base.Entity] = base.GatherElements(DECK, type='Same', search_mode='Connectivity', gather_from='Visible', gather_from_containers=elem, source=elem)if not newElems: newElems = [elem]else: newElems = newElems[elem]newProp:base.Entity = base.CopyEntity(None, prop)_ = [newElem.set_entity_values(DECK, {'PID': newProp._id}) for newElem in newElems]elems = list(set(elems)-set(newElems))return None
from ansa import basefrom ansa import constantsfrom ansa import guitkfrom typing import List, Union, TupleDECK = constants.NASTRANclass Segregate(object):selectTypes:Tuple[str] = ('Visible', 'All', 'Selected')entTypes:Tuple[str] = ('AnsaPart', 'Property')def __init__(self):self.mainWindow = guitk.BCWindowCreate('模型拆分器', guitk.constants.BCOnExitDestroy)self.mainBox = guitk.BCVBoxCreate(self.mainWindow)self.init()guitk.BCShow(self.mainWindow)def init(self):selectBox = guitk.BCVBoxCreate(self.mainBox)selectTypeBox = guitk.BCHBoxCreate(selectBox)guitk.BCLabelCreate(selectTypeBox, '选择方式: ')self.selectTypeCombo = guitk.BCComboBoxCreate(selectTypeBox, self.selectTypes)guitk.BCComboBoxSetCurrentItem(self.selectTypeCombo, 0)entTypeBox = guitk.BCHBoxCreate(selectBox)guitk.BCLabelCreate(entTypeBox, '选择对象类型: ')self.entTypeCombo = guitk.BCComboBoxCreate(entTypeBox, self.entTypes)guitk.BCComboBoxSetCurrentItem(self.entTypeCombo, 0)self.propSegCheckBox = guitk.BCCheckBoxCreate(entTypeBox, '是否按PID拆分')guitk.BCCheckBoxSetChecked(self.propSegCheckBox, False)guitk.BCShow(self.propSegCheckBox)guitk.BCComboBoxSetCurrentIndexChangedFunction(self.entTypeCombo, self.entTypeComboChangedFunc, None)controlBox = guitk.BCHBoxCreate(self.mainBox)self.okButton = guitk.BCPushButtonCreate(controlBox, 'OK', self.okFunc, None)self.cancelButton = guitk.BCPushButtonCreate(controlBox, 'Cancal', self.rejectFunc, None)return None@propertydef curSelectType(self)->str:val:str = guitk.BCComboBoxCurrentText(self.selectTypeCombo)return val if val else ''@propertydef curEntType(self)->str:val:str = guitk.BCComboBoxCurrentText(self.entTypeCombo)return val if val else ''@propertydef isSegAsPID(self)->bool:return guitk.BCCheckBoxIsChecked(self.propSegCheckBox)def entTypeComboChangedFunc(self, combo, index, data)->None:if index==0:guitk.BCShow(self.propSegCheckBox)else:guitk.BCCheckBoxSetChecked(self.propSegCheckBox, False)guitk.BCHide(self.propSegCheckBox)return Nonedef collectEntities(self)->Union[List[base.Entity], None]:stype:str = self.curSelectTypeif stype not in self.selectTypes:print('当前选择类型设置有误,请重新设置!')return NoneentTypes:Tuple[str] = ('ANSAPART', ) if self.curEntType==self.entTypes[0] else ('PSHELL', 'PSOLID')if stype==self.selectTypes[0]:entities:List[base.Entity] = base.CollectEntities(DECK, None, entTypes, filter_visible=True)elif stype==self.selectTypes[1]:entities:List[base.Entity] = base.CollectEntities(DECK, None, entTypes, filter_visible=False)else:entities:Union[List[base.Entity], None] = base.PickEntities(DECK, entTypes)return entities@staticmethoddef segregateAsFace(props:List[base.Entity])->List[base.Entity]:elemInFaces:List[base.Entity] = list()for prop in props:faces:List[base.Entity] = base.CollectEntities(DECK, prop, 'FACE', recursive=True)if not faces:continuewhile faces:newFaces:List[base.Entity] = base.GatherFaces(faces[0], 40.0, visibility='visible', container='PID_region')newProp:base.Entity = base.CopyEntity(None, prop)_ = [newFace.set_entity_values(DECK, {'PID': newProp._id}) for newFace in newFaces]faces = list(set(faces)-set(newFaces))elemInFaces.extend(base.CollectEntities(DECK, faces, ('SHELL', 'SOLID'), recursive=True))return elemInFaces@staticmethoddef segregateAsElem(props:List[base.Entity], outsideElems:List[base.Entity]=None)->None:if outsideElems is None: outsideElems = []for prop in props:elems:List[base.Entity] = base.CollectEntities(DECK, prop, ('SHELL', 'SOLID'), recursive=True)elems = list(set(elems)-set(outsideElems))while elems:elem = elems[0]newElems:List[base.Entity] = base.GatherElements(DECK, type='Same', search_mode='Connectivity', gather_from='Visible', gather_from_containers=elem, source=elem)if not newElems: newElems = [elem]else: newElems = newElems[elem]newProp:base.Entity = base.CopyEntity(None, prop)_ = [newElem.set_entity_values(DECK, {'PID': newProp._id}) for newElem in newElems]elems = list(set(elems)-set(newElems))return None@class methoddef segregateAsPID(cls, props:List[base.Entity])->None:elemInFaces:List[base.Entity] = cls.segregateAsFace(props)return cls.segregateAsElem(props, elemInFaces)@class methoddef segregateAsPart(cls, parts:List[base.Entity])->None:for part in parts:PIDNumber:int = part.get_entity_values(DECK, ('PIDs Number', ))['PIDs Number']if PIDNumber==0:base.DeleteEntity(part)continueelif PIDNumber==1:continueprops:List[base.Entity] = base.CollectEntities(DECK, part, '__PROPERTIES__', recursive=True)if not props:continuenewParts:List[base.Entity] = list()for prop in props[1:]:count:int = 1newPart:base.Entity = base.NewPart(f'{part._name}_I0001')while not newPart:count += 1newPart = base.NewPart(f'{part._name}_I{count:0>4d}')newParts.append(newPart)entities:List[base.Entity] = base.CollectEntities(DECK, prop, ('FACE', 'SHELL', 'SOLID'), recursive=True)base.SetEntityPart(entities, newPart)base.ConnectPartInstances(part, newParts)ret = base.GetPartDepth(part)parent:base.Entity = None if not ret else ret['parent_part']if parent: base.SetEntityPart(newParts, parent)return Nonedef okFunc(self, window, data):entities:Union[List[base.Entity], None] = self.collectEntities()if not entities: return Noneif self.curEntType==self.entTypes[0]:if self.isSegAsPID:props:List[base.Entity] = base.CollectEntities(DECK, entities, ('PSHELL', 'PSOLID'), recursive=True)self.segregateAsPID(props)self.segregateAsPart(entities)base.Compress({'Groups/Parts':1, 'PSHELL': 1, 'PSOLID': 1}, DECK)else:self.segregateAsPID(entities)base.Compress({'PSHELL': 1, 'PSOLID': 1}, DECK)self.rejectFunc(window, data)return Nonedef rejectFunc(self, window, data):guitk.BCDestroy(self.mainWindow)return 0
