HFSS学习笔记(四)利用MATLAB脚本进行HFSS快速建模

HFSS学习笔记(四)利用MATLAB脚本进行HFSS快速建模

在HFSS中,我们经常会遇到复杂且重复的模型,比如你要画30个不同的矩形块,每个矩形块都有自己的参数比如,a1,a2,…,a30,那么此时你就要重复建立30个模型,这样的话,耗费时间且效率低,因此,使用HFSS的脚本功能,利用代码的循环功能,来实现快速建模。

一、HFSS脚本录制

1.打开HFSS,点击上方Tools->Record Script to File,HFSS开始记录你的操作,并将你的操作转换为代码(有点类似录像的意思)

保存成vbs格式(将你的操作记录保存成相应的文件)
HFSS学习笔记(四)利用MATLAB脚本进行HFSS快速建模_第1张图片
2.建立了一个长为ax11,宽为w的长条
HFSS学习笔记(四)利用MATLAB脚本进行HFSS快速建模_第2张图片
3.停止记录

4.用打开记事本的方式打开你保存的vbs文件,查看代码

Dim oAnsoftApp
Dim oDesktop
Dim oProject
Dim oDesign
Dim oEditor
Dim oModule
Set oAnsoftApp = CreateObject("Ansoft.ElectronicsDesktop")
Set oDesktop = oAnsoftApp.GetAppDesktop()
oDesktop.RestoreWindow
Set oProject = oDesktop.SetActiveProject("cucao_unit_impedace_ay=7_number=5")
Set oDesign = oProject.SetActiveDesign("13_jiaoben")
Set oEditor = oDesign.SetActiveEditor("3D Modeler")
oEditor.CreateRectangle Array("NAME:RectangleParameters", "IsCovered:=", true, "XStart:=",  _
  "-ax11/2", "YStart:=", "-w/2", "ZStart:=", "h", "Width:=", "ax11", "Height:=",  _
  "w", "WhichAxis:=", "Z"), Array("NAME:Attributes", "Name:=", "Rectangle9", "Flags:=",  _
  "", "Color:=", "(143 175 143)", "Transparency:=", 0, "PartCoordinateSystem:=",  _
  "Global", "UDMId:=", "", "MaterialValue:=", "" & Chr(34) & "vacuum" & Chr(34) & "", "SurfaceMaterialValue:=",  _
  "" & Chr(34) & "" & Chr(34) & "", "SolveInside:=", true, "IsMaterialEditable:=",  _
  true, "UseMaterialAppearance:=", false, "IsLightweight:=", false)

5.以上操作相当于有了一个模板,依据以上模板可以修改各种值。

二、MATLAB复现

1.首先用matlab复现以上功能-即建立一个长为ax11,宽为w的长条。
该段matlab代码主要功能是:
(1)创建一个txt文件
(2)用fprintf()功能将vbs文件中的代码打印到目标txt文件中

clc;clear;close all;
fid = fopen('ax-model3.txt', 'wt');%会在当前文件夹下生成ax-model3.txt文件

fprintf(fid, 'Dim oAnsoftApp\n');
fprintf(fid, 'Dim oDesktop\n');
fprintf(fid, 'Dim oProject\n');
fprintf(fid, 'Dim oDesign\n');
fprintf(fid, 'Dim oEditor\n');
fprintf(fid, 'Dim oModule\n');
fprintf(fid,'Set oAnsoftApp = CreateObject("Ansoft.ElectronicsDesktop")\n')
fprintf(fid,'Set oDesktop = oAnsoftApp.GetAppDesktop()\n')
fprintf(fid,'oDesktop.RestoreWindow\n')
fprintf(fid,'Set oProject = oDesktop.SetActiveProject("cucao_unit_impedace_ay=7_number=5")\n')
fprintf(fid,'Set oDesign = oProject.SetActiveDesign("13_jiaoben")\n')
fprintf(fid,'Set oEditor = oDesign.SetActiveEditor("3D Modeler")\n')


fprintf(fid,'oEditor.CreateRectangle Array("NAME:RectangleParameters", "IsCovered:=", true, "XStart:=",  _\n')
fprintf(fid,'  "-ax11/2", "YStart:=", "-w/2", "ZStart:=", "h", "Width:=", "ax11", "Height:=",  _\n')
fprintf(fid,'  "w", "WhichAxis:=", "Z"), Array("NAME:Attributes", "Name:=", "Rectangle9", "Flags:=",  _\n')
fprintf(fid,'  "", "Color:=", "(143 175 143)", "Transparency:=", 0, "PartCoordinateSystem:=",  _\n')
fprintf(fid,'  "Global", "UDMId:=", "", "MaterialValue:=", "" & Chr(34) & "vacuum" & Chr(34) & "", "SurfaceMaterialValue:=",  _\n')
fprintf(fid,'  "" & Chr(34) & "" & Chr(34) & "", "SolveInside:=", true, "IsMaterialEditable:=",  _\n')
fprintf(fid,'  true, "UseMaterialAppearance:=", false, "IsLightweight:=", false)\n')

fclose(fid);

2.将生成的ax-model3.txt文件修改为ax-model3.vbs文件,修改很简单,直接重命名,将后缀.txt改成.vbs即可
3.打开HFSS,运行我们生成的ax-model3.vbs脚本

4.生成相应的长条
HFSS学习笔记(四)利用MATLAB脚本进行HFSS快速建模_第3张图片

三、MATLAB实现循环建模

1.首先,因为每个长条尺寸不同,我们要先建立好每个长条对应的变量ax,这一步也可以用脚本来去建立
2.然后就是循环建模,代码如下

clc;clear;close all;
fid = fopen('ax-model4.txt', 'wt');%会在当前文件夹下生成ax-model4.txt文件

fprintf(fid, 'Dim oAnsoftApp\n');
fprintf(fid, 'Dim oDesktop\n');
fprintf(fid, 'Dim oProject\n');
fprintf(fid, 'Dim oDesign\n');
fprintf(fid, 'Dim oEditor\n');
fprintf(fid, 'Dim oModule\n');
fprintf(fid,'Set oAnsoftApp = CreateObject("Ansoft.ElectronicsDesktop")\n')
fprintf(fid,'Set oDesktop = oAnsoftApp.GetAppDesktop()\n')
fprintf(fid,'oDesktop.RestoreWindow\n')
fprintf(fid,'Set oProject = oDesktop.SetActiveProject("cucao_unit_impedace_ay=7_number=5")\n')
fprintf(fid,'Set oDesign = oProject.SetActiveDesign("13_jiaoben")\n')

str = ["ax11" "ax12" "ax13" "ax14" "ax15" "ax21" "ax22" "ax23" "ax24" "ax25" "ax31" "ax32" "ax33" "ax34" "ax35" "ax41" "ax42" "ax43" "ax44" "ax45" "ax51" "ax52" "ax53" "ax54" "ax55"]

%先要建立所有变量
for ax = ["ax11" "ax12" "ax13" "ax14" "ax15" "ax21" "ax22" "ax23" "ax24" "ax25" "ax31" "ax32" "ax33" "ax34" "ax35" "ax41" "ax42" "ax43" "ax44" "ax45" "ax51" "ax52" "ax53" "ax54" "ax55"]

fprintf(fid,'oDesign.ChangeProperty Array("NAME:AllTabs", Array("NAME:LocalVariableTab", Array("NAME:PropServers",  _\n')
fprintf(fid,'  "LocalVariables"), Array("NAME:NewProps", Array("NAME:%s", "PropType:=", "VariableProp", "UserDef:=",  _\n',ax)
fprintf(fid,'  true, "Value:=", "8mm"))))\n')

end

%开始3D建模
fprintf(fid,'Set oEditor = oDesign.SetActiveEditor("3D Modeler")\n')

for ax = ["ax11" "ax12" "ax13" "ax14" "ax15" "ax21" "ax22" "ax23" "ax24" "ax25" "ax31" "ax32" "ax33" "ax34" "ax35" "ax41" "ax42" "ax43" "ax44" "ax45" "ax51" "ax52" "ax53" "ax54" "ax55"]
    
fprintf(fid,'oEditor.CreateRectangle Array("NAME:RectangleParameters", "IsCovered:=", true, "XStart:=",  _\n')
fprintf(fid,'  "-%s/2", "YStart:=", "-w/2", "ZStart:=", "h", "Width:=", "%s", "Height:=",  _\n',ax,ax)
fprintf(fid,'  "w", "WhichAxis:=", "Z"), Array("NAME:Attributes", "Name:=", "%s", "Flags:=",  _\n',ax)
fprintf(fid,'  "", "Color:=", "(143 175 143)", "Transparency:=", 0, "PartCoordinateSystem:=",  _\n')
fprintf(fid,'  "Global", "UDMId:=", "", "MaterialValue:=", "" & Chr(34) & "vacuum" & Chr(34) & "", "SurfaceMaterialValue:=",  _\n')
fprintf(fid,'  "" & Chr(34) & "" & Chr(34) & "", "SolveInside:=", true, "IsMaterialEditable:=",  _\n')
fprintf(fid,'  true, "UseMaterialAppearance:=", false, "IsLightweight:=", false)\n')

end
fclose(fid);

3.将生成的txt文件修改为vbs文件,然后在hfss中运行
4.建模的效果如下
HFSS学习笔记(四)利用MATLAB脚本进行HFSS快速建模_第4张图片
5.因为在创建变量的时候,每个变量的值都是一样的,所以,可以根据自己的参数在左栏的properties中进行修改

你可能感兴趣的:(HFSS软件设计,matlab,电磁学)