ANSA取得Entity的function為base.GetEntity,一般需要三個參數,deck、element_type及element_id。
其中element_type也可以用來取得property或是material等較大集合內的Entity。
如果base.GetEntity呼叫成功,會回傳該Entity,如果找不到的話,會回傳None。如果將element_id指定為非int的值,則會產生TypeError。
下面我們產生一個LS-DYNA的SECTION_SHELL property,分別使用兩種element_type來取得。
from ansa import base, constants
def create_and_get_shell():
deck = constants.LSDYNA
type_ = 'SECTION_SHELL'
shell = base.CreateEntity(deck, type_, {'PID': 1})
print(f'{shell=}')
got_shell1 = base.GetEntity(deck, type_, 1)
print(f'{got_shell1=}')
type_ = '__PROPERTIES__'
got_shell2 = base.GetEntity(deck, type_, 1)
print(f'{got_shell2=}')
從回傳結果來看,我們的確都可以取得所建立的SECTION_SHELL Entity。
shell=<Entity: 0x7f92e820fb80: type: 1301(1301,N_PSHELL) id:1>
got_shell1=<Entity: 0x7f92e820fb80: type: 1301(1301,N_PSHELL) id:1>
got_shell2=<Entity: 0x7f92e820fb80: type: 1301(1301,N_PSHELL) id:1>
ANSA取得Entities的function,分別有base.CollectEntities及base.NameToEnts兩種。
base.CollectEntities一般需要三個參數,deck、containers及search_types。
containers是一種集合的Entity,例如set、part及group等,如果設為None,代表要搜尋當前整個ANSA database。
search_types可以指定各種ANSA Entity,比較特別的是可以指定為__ALL_ENTITIES__,取得整個ANSA database的所有Entities。
如果base.CollectEntities有搜尋到Entity的話,會回傳一個list。如果未搜尋到的話,則會回傳一個空的list。
舉個例子,這裡我們建立四個node Entity,一個element_shell Entity及一個名為set1的set Entity,並透過base.AddToSet將element_shell Entity加入set1中,最後我們就可以透過base.CollectEntities在set1這個container內,搜尋到剛剛加入的element_shell Entity。
from ansa import base, constants
def collect_entities():
deck = constants.LSDYNA
node1 = base.CreateEntity(deck, 'NODE', {'X': 0, 'Y': 0, 'Z': 0})
node2 = base.CreateEntity(deck, 'NODE', {'X': 10, 'Y': 0, 'Z': 0})
node3 = base.CreateEntity(deck, 'NODE', {'X': 10, 'Y': 10, 'Z': 0})
node4 = base.CreateEntity(deck, 'NODE', {'X': 0, 'Y': 10, 'Z': 0})
shell = base.CreateEntity(
deck, 'ELEMENT_SHELL', {'PID': 1, 'N1': node1, 'N2': node2, 'N3': node3, 'N4': node4})
set1 = base.CreateEntity(deck, 'SET', {'Name': 'set1'})
base.AddToSet(set1, shell)
got_shell = base.CollectEntities(deck, set1, 'ELEMENT_SHELL')
print(f'{got_shell=}')
其輸出為:
got_shell=[<Entity: 0x7f92a7c60000: type: 513(513,N_CQUAD4) id:-1>]
So far so good!
但是當我們想取得的不是element_shell Entity,而是其內所含的四個node Entity時,該怎麼做呢?base.CollectEntities的recursive=True這個參數可以幫我們解決這難題。
got_nodes1 = base.CollectEntities(deck, set1, 'NODE')
print(f'{got_nodes1=}')
got_nodes2 = base.CollectEntities(deck, set1, 'NODE', recursive=True)
print(f'{got_nodes2=}')
從回傳結果可以看出,當未使用recursive時,ANSA認為set1這個container內,只有一個element_shell Entity而沒有node Entity,所以會回傳一個空的list。但是當使用recursive時,ANSA就會再進一步搜尋element_shell Entity內是否有node Entity,結果找到四個,並回傳一個list。
got_nodes1=[]
got_nodes2=[<Entity: 0x7f92a7d02000: type: 1101(1101,N_GRID) id:-1>, <Entity: 0x7f92a7d02038: type: 1101(1101,N_GRID) id:-2>, <Entity: 0x7f92a7d02070: type: 1101(1101,N_GRID) id:-3>, <Entity: 0x7f92a7d020a8: type: 1101(1101,N_GRID) id:-4>]
base.NameToEnts一般需要三個參數,pattern、deck及match。pattern為一regular expression,背後使用的是PCRE-Perl Compatible Regular Expressions。
match則有四種模式:
constants.ENM_REGEX(預設)
constants.ENM_EXACT
constants.ENM_SUBSTRING
constants.ENM_SUBSTRING_IGNORECASE
我們比較常用的是constants.ENM_SUBSTRING(考慮大小寫)。
舉個例子,這裡我們建立set1(名字為SET1)及set2(名字為SET2)兩個set。
from ansa import base, constants
def name_to_ents():
deck = constants.LSDYNA
set1 = base.CreateEntity(deck, 'SET', {'Name': 'SET1'})
set2 = base.CreateEntity(deck, 'SET', {'Name': 'SET2'})
sets1 = base.NameToEnts('SET', deck=deck, match=constants.ENM_SUBSTRING)
print(f'{sets1=}')
sets2 = base.NameToEnts('set', deck=deck, match=constants.ENM_SUBSTRING)
print(f'{sets2=}')
sets3 = base.NameToEnts(
'set', deck=deck, match=constants.ENM_SUBSTRING_IGNORECASE)
print(f'{sets3=}')
pattern設為SET並搭配match為constants.ENM_SUBSTRING,可成功得到這兩個set。pattern設為set並搭配match為constants.ENM_SUBSTRING,則會得到None。pattern設為set並搭配match為constants.ENM_SUBSTRING_IGNORECASE,則因為忽略大小寫而可成功得到這兩個set。sets1=[<Entity: 0x7f320373e9e8: type: 3105(3105,PAMGROUP) id:1>, <Entity: 0x7f320373f268: type: 3105(3105,PAMGROUP) id:2>]
sets2=None
sets3=[<Entity: 0x7f320373e9e8: type: 3105(3105,PAMGROUP) id:1>, <Entity: 0x7f320373f268: type: 3105(3105,PAMGROUP) id:2>]