ANSYS-SCDM二次开发中,如下分别为是structure和group下的部件名称,接下来通过python分别遍历这两部分名称:
代码:
1、遍历group:
for group in GetActiveWindow().Groups:
#创建字典用于储存group对象
group_dict={}
#【group名称】
#循环遍历group群组下的所有对象
for group in GetActiveWindow().Groups:
#获取group对象名称
prefix1=group.GetName()
# 判断遍历名称是否在字典中,不在就创建列表并储存对象
if not prefix1 in group_dict.keys():
group_dict[prefix1]=[]
#将实体对象添加到列表中
group_dict[prefix1].append(group)
#遍历字典中的每一个
print(group.Name)
2、遍历structure:
for body in GetRootPart().GetAllBodies()
3、当包含component时:
import os
component_dirct={}
part =GetRootPart()
component1=part.GetComponents()[0]
component2=part.GetComponents()[1]
for body1 in component1.Content.Bodies:
print component1.GetName()+"-"+body1.GetName()
4、遍历对象完成后,对所需要的对象进行分组:将前缀相同或者名称相同的存于列表:
for group_key in group_dict:
#判断字典中每一个对象前缀是否为inlet或者outlet
if group_key.startswith('inlet') or group_key.startswith('outlet'):
#将前缀为inlet和outlet的名称对象添加到inle_key字典中
inlet_key.append(group_key)
if group_key.startswith('bc_convection'):
#将前缀为bc_convection的名称对象添加到bconvection_key字典中
bconvection_key.append(group_key)
5、将component下的对象以父项—子项的方式进行储存
#遍历part结果solid下的对象成员
for solid_body in component1.Content.Bodies :
#获取part对象名称
prefix2=component1.GetName()+'-'+solid_body.GetName() #输出类型为solid+名称
#判断遍历solid名称是否存在于字典中,不在就创建列表并储存对象
if not prefix2 in structure_solid.keys():
structure_solid[prefix2]=[]
structure_solid[prefix2].append(solid_body)
6、创建文件夹:当不存在时,进行创建
import os
def mkdir(path):
path="D:\Group"
folder = os.path.exists(path)
if not folder: #判断是否存在文件夹如果不存在则创建为文件夹
os.makedirs(path) #makedirs 创建文件时如果路径不存在会创建这个路径
else:
print "--- There is this folder! ---"
os.chdir(r"D:\Group")
import codecs #解决中问乱码问题
7、遍历名称写入文本:
import os
os.chdir(r"D:\workSpace\ANSYS\TurbineCAE\Test620\Fluid\Group")
import codecs #解决中问乱码问题
#【文本写入】
f = codecs.open('test.py','w',encoding="utf-8")
#写入字符
try:
#写入第一行进出口边界名称
f.write('n'+"BoundaryZoneList = "r'{0}', r'{1}', r'{2}'"".format(inlet_key[2],inlet_key[1],inlet_key[0]))
f.write("t# 进出口边界名称")
except:
print("请创建进出口边界")
f.close()
最后的结果
总结:以上内容包括:遍历scdm中part名称,创建NS下的group名称、名称存储打印规则,文件夹的创建、文本写入等内容。有不对的地方欢迎指正。



