首先介绍一下需要用到的API(下面的API都基于ansa.base库):
CollectEntities这个方法是用来收集实体,它返回一个收集到实体的列表。
GetEntityCardValues这个方法是用来从下图所示的“Card”上获取pid、mid等参数的值。
示例代码如下:
import ansa
from ansa importbase
def main():
collector = base.CollectNewModelEntities()
n = base.CreateEntity(ansa.constants.ABAQUS, 'NODE')
new_entities = collector.report()
del collector
print(len(new_entities))
注意: 此处一定要有del collector.
SetEntityCardValues设置或者改变”Card”中参数的值。
CheckAndFixGeometry检查并且自动清理几何,他返回一个包含了错误信息的字典或者None。
此外还有Or(),All(),ZoomAll(),Compress(‘’)等辅助方法。
下面介绍一下程序逻辑:
我们这个程序是对于一个装配件而言的,所以会有很多个Part,因此肯定要用到循环来依次取出每一个Part进行操作。首先收集所有的实体,然后使用For循环依次对每个Part进行操作。先使用Or()与ZoomAll()显示单个实体,然后使用CheckAndFixGeometry进行自动几何清理,如果没有错误,则进行下一步(如果有错误说明无法自动清理,需要手动清理)。接下来需要收集实体‘FACE’,如果收集的实体不为空,那么我们将收集到的’FACE’进行Skin抽中面。接下来根据“PSHELL”来收集新的实体对象。“PSHELL”也就是如下图所示的‘Card’
然后执行report()方法获取新的实体列表。如果这个列表长度不为零,那么通过GetEntityCardValues方法获取新的PID信息,然后再通过SetEntityCardValues方法设置属性信息。最后使用All()和ZoomAll()显示所有实体,再使用Compress()压缩即可。
完整代码如下:
import ansa
from ansa importbase
from ansa importconstants
def Test():
all_parts =base.CollectEntities(constants.NASTRAN, None, "ANSAPART")
print(len(all_parts))
i=0
for part in all_parts:
base.Or(part)
n=base.ZoomAll()
vals_1 = ('Name', 'PID')
part_info =base.GetEntityCardValues(constants.NASTRAN, part, vals_1)
options=['CRACKS','OVERLAPS','NEEDLEFACES','COLLAPSED CONS', 'UNCHECKED FACES']
fix=[1,1,1,1,1]
ret=base.CheckAndFixGeometry(part,options,fix,True,True)
if ret==None:
faces=base.CollectEntities(constants.NASTRAN,part,'FACE')
if len(faces) != 0:
collector =base.CollectNewModelEntities(constants.NASTRAN, 'PSHELL')
num_shell_deions=base.Skin(apply_thickness=True,new_pid=True,offset_type=2,ok_to_offset=True,max_thickness=5.0,delete=True,entities= faces)
new_entities =collector.report()
print(len(new_entities))
if len(new_entities) != 0:
vals_2 =('Name', 'PID' ,'T')
new_pid_info=base.GetEntityCardValues(constants.NASTRAN,new_entities[0], vals_2)
vals_3={
'Name':"S"+part_info[vals_1[0]]+"_"+str(new_pid_info[vals_2[2]]),}
base.SetEntityCardValues(constants.NASTRAN,new_entities[0], vals_3)
del collector
print("num . "+str(i)+" :"+new_entities[0]._name)
i += 1
base.All()
m=base.ZoomAll()
base.Compress('')
这里就不多讲了,方法逻辑基本没多大区别,有不懂得API可以查看帮助文档。直接上代码。
import ansa
from ansa import base
from ansa import constants
def mid_Casting():
all_pshell =base.CollectEntities(constants.NASTRAN, None, "PSHELL")
print(len(all_pshell))
i = 0
for pshell inall_pshell:
print("pshell_name: "+pshell._name)
if pshell._name[0] != "S":
collectorPshell = base.CollectNewModelEntities(constants.NASTRAN,'PSHELL')
collectorShell = base.CollectNewModelEntities(constants.NASTRAN,"SHELL")
all_faces = base.CollectEntities(constants.NASTRAN,pshell,"FACE")
result = base.MidSurfAuto(faces = all_faces,
thick=5.,
length=1,
elem_type=3,
join_distance=0.5,
paste_triple_len=0.5,
exact_middle=True,
handle_as_single_solid=False,
part="use_current")
base.DeleteEntity(pshell, True)
new_Pshell = collectorPshell.report()
print("The new pshell's length is : ", len(new_Pshell))
print("The new Pshell's name is :",new_Pshell[0]._name)
new_Shell = collectorShell.report()
print("The new Shell's number is :", len(new_Shell))
AnsaPartReference = base.GetEntity(constants.NASTRAN,"SHELL",new_Shell[0]._id )
print("The AnsaPartReference's id is :", AnsaPartReference)
CurrentPart = base.GetEntityPart(AnsaPartReference)
CurrentPartName = base.GetEntityCardValues(constants.NASTRAN, CurrentPart,("Name",))
print("The CurrentPartName is :",CurrentPartName["Name"])
base.SetEntityCardValues(constants.NASTRAN, new_Pshell[0],{
"Name": "C"+CurrentPartName["Name"]})
del collectorPshell
del collectorShell
i+=1
转载自 :https://www.sohu.com/a/157864134_744423
原文作者: 有限元在线
发表日期: 2017-07-17
扫描下方二维码关注我的微信公众号 - CAE软件二次开发Lab,阅读更多精彩内容!