铝镁合金 | 空气 | 超细玻璃 | 软铝 | |
密度[kg/m³] | 2560 | 1.295 | 33 | 4450 |
导热系数[W/(m⋅K) ] | 158 | 0.024 | 0.025 | 202 |
def mat_definition(mat2geo_dict):""" 批量材料分配函数 参数:mat2geo_dict - 几何体-材料映射字典(dict类型) 返回:isExist_mat - 材料存在性校验结果(bool类型);missing_mats - 缺失的材料 """
参数mat2geo_dict:接收外部定义的映射关系,提高函数复用性(更换场景时仅需修改字典,无需改函数逻辑)。
mat_lib = [mat.Name for mat in Model.Materials.Children] # 提取材料名称列表mat_needs = list(mat2geo_dict.values()) # 转listisExist_mat = Truemissing_mats = []for need_mat in mat_needs: if need_mat not in mat_lib: isExist_mat = False missing_mats.append(need_mat)
Model.Materials.Children:ANSYS 核心 API,返回当前 Model 中所有已加载的材料对象列表,是实现自动化的关键;
missing_mats列表:记录缺失材料,避免用户反复排查,直接定位问题。
for geo_name, target_mat in mat2geo_dict.items(): # 遍历字典项 for part in Model.Geometry.Children: # 遍历部件 for body in part.Children: # 遍历几何体 if geo_name in body.Name: # 名称匹配 body.Material = target_mat # 核心分配操作
三层遍历逻辑:Model.Geometry.Children(部件)→ part.Children(体),覆盖 ANSYS 中 “几何 - 部件 - 体” 的层级结构,确保不遗漏任何几何体;
geo_name in body.Name:采用 “包含匹配” 而非 “完全匹配”,兼容几何体命名包含后缀的场景(如 “inner wall_1”“inner wall_2” 均可匹配);
body.Material = target_mat:核心赋值语句,通过 API 直接修改几何体的材料属性,替代手动操作。
# 定义几何体-材料映射字典mat2geo_dict = { "inner wall": "Mag2Al", "insulation1": "air", "insulation2": "glass", "outer wall": "al"}# 执行分配函数mat_check_result = mat_definition(mat2geo_dict)# 校验失败时终止if not mat_check_result: raise Exception("材料缺失,脚本终止执行")
根据 “几何体 - 材料” 对应关系,定义 Python 字典mat2geo_dict,作为脚本的核心映射数据,后续通过字典遍历实现批量操作。
# 几何体-材料映射字典(键:几何体名称,值:ANSYS材料库名称)mat2geo_dict = { "inner wall": "Mag2Al", # 内壁 -> 铝镁合金 "insulation1": "air", # 空气夹层 -> 空气 "insulation2": "glass", # 超细玻璃保温层 -> 超细玻璃 "outer wall": "al" # 外壁 -> 软铝}
在材料缺失时,会主动抛出异常,避免脚本 “无报错但未执行” 的隐性问题,能够提高调试效率。
# -*- coding: utf-8 -*-"""ANSYS Mechanical 批量材料定义脚本 (Python 2.7)功能:根据几何体-材料映射字典,自动校验材料并批量分配作者:小郭老师日期:2025-11-02版本:V1.0"""def mat_definition(mat2geo_dict): """ 批量材料分配函数 参数:mat2geo_dict - 几何体-材料映射字典(dict类型) 返回:isExist_mat - 材料存在性校验结果(bool类型:True=所有材料存在,False=存在缺失材料);missing_mats - 缺失的材料 """ # 步骤1:获取当前Model中已加载的所有材料名称(从Materials树中读取) mat_lib = [mat.Name for mat in Model.Materials.Children] # 提取材料名称列表 print "当前ANSYS材料库中已加载的材料:%s" % mat_lib # 步骤2:提取映射字典中所需的所有材料(去重) mat_needs = list(set(mat2geo_dict.values())) # set去重后转list print "本次批量分配所需的材料:%s" % mat_needs # 步骤3:校验材料存在性 isExist_mat = True missing_mats = [] for need_mat in mat_needs: if need_mat not in mat_lib: isExist_mat = False missing_mats.append(need_mat) # 步骤4:根据校验结果执行操作 if isExist_mat: print "\n=== 所有材料已存在,开始批量分配 ===" for geo_name, target_mat in mat2geo_dict.items(): # 遍历字典项 for part in Model.Geometry.Children: # 遍历部件 for body in part.Children: # 遍历几何体 if geo_name in body.Name: # 名称匹配 body.Material = target_mat # 核心分配操作 print "分配成功:几何体「%s」→ 材料「%s」" % (body.Name, target_mat) print "=== 批量材料分配完成 ===" else: print "材料缺失,分配终止!缺失的材料:%s" % missing_mats print "解决方案:1. 进入Engineering Data模块;2. 补充材料" return isExist_mat, missing_mats# ---------------------- 脚本执行入口 ----------------------# 定义几何体-材料映射字典mat2geo_dict = { "inner wall": "Mag2Al", "insulation1": "air", "insulation2": "glass", "outer wall": "al"}# 执行分配函数mat_check_result, missing_mats = mat_definition(mat2geo_dict)# 校验失败时终止if not mat_check_result: raise Exception("材料缺失,脚本终止执行")
提供vip群答疑和模型下载
《ANSYS Workbench & Mechanical企业级二次开发程序与Python应用入门进阶》
以下是课程大纲及主要内容: