Scripting HFSS Inside MatLab

Scripting HFSS isn’t hard if you understand scripting HFSS the normal way. If you don’t understand how scripting in HFSS works, you will want to read that first.

Changing a Setup Frequency: A Stupid Example

Change a project’s setup frequency, just like from the HFSS examples.

Note

This script requires that you have AntennaTemplate.aedt on your desktop.

%% Change the setup frequency in Matlab in a kind of dumb way
script = fopen('hfss_script.py', 'wt');

fprintf(script, 'import os\n');
fprintf(script, 'project_file = os.path.join(os.path.expanduser("~"), "Desktop")\n');
fprintf(script, 'project_file = os.path.join(project_file, "AntennaTemplate.aedt")\n');
fprintf(script, 'import ScriptEnv\n');
fprintf(script, 'ScriptEnv.Initialize("Ansoft.ElectronicsDesktop")\n');
fprintf(script, 'oDesktop.RestoreWindow()\n');
fprintf(script, 'oDesktop.OpenProject(project_file)\n');
fprintf(script, 'oProject = oDesktop.SetActiveProject("AntennaTemplate")\n');
fprintf(script, 'oDesign = oProject.SetActiveDesign("HFSSDesign1")\n');
fprintf(script, 'oDesign.ChangeProperty(["NAME:AllTabs", ["NAME:HfssTab", ["NAME:PropServers", "AnalysisSetup:Setup8"],\n');
fprintf(script, '            ["NAME:ChangedProps", [ "NAME:Solution Freq", "MustBeInt:=", False, "Value:=", "500MHz"]]]])\n');
fprintf(script, 'oDesign.ChangeProperty([ "NAME:AllTabs", ["NAME:HfssTab", ["NAME:PropServers", "AnalysisSetup:Setup8:Sweep1"],\n');
fprintf(script, '            ["NAME:ChangedProps", [ "NAME:Start", "MustBeInt:=", False, "Value:=", "460MHz"]]]])\n');
fprintf(script, 'oDesign.ChangeProperty([ "NAME:AllTabs", [ "NAME:HfssTab", [ "NAME:PropServers", "AnalysisSetup:Setup8:Sweep1" ],\n');
fprintf(script, '            [ "NAME:ChangedProps", [ "NAME:Stop", "MustBeInt:=", False, "Value:=", "540MHz"]]]])\n');

fclose(script);
system('"C:\Program Files\AnsysEM\AnsysEM16.0\Win64\ansysedt.exe" -RunScript "hfss_script.py"')

Note

This will only run if the path to hfss is "C:\Program Files\AnsysEM\AnsysEM16.0\Win64\ansysedt.exe", which it is not guaranteed to be. You will need to replace that part on the last line with the correct path for you.

This is not a terrible script, but it’s very specific, to the case it is doing, and you have to remember to change things in lots of places. For example, what if you want to change the solution frequency? You have to remember to change the setup frequency, the sweep start frequency, and the sweep stop frequency. It would be better if you only had to change one frequency value; the script below is a little more general in that sense.

Changing a Setup Frequency: A Slightly Better Example

This example is almost completely the same as the previous example, but is a tiny bit more general. There is a little bit of string formatting, but nothing too fancy. This script will change the frequency of a sweep based on the center setup frequency.

Note

This script requires that you have AntennaTemplate.aedt on your desktop.

%% Change the setup frequency in Matlab in a little smarter way
% center frequency in MHz; everything will change based on this.
frequency = 700; %MHz
sweepStart = frequency - (frequency * 0.1);
sweepStop = frequency + (frequency * 0.1);

%% Change the setup frequency in Matlab in a little smarter way
script = fopen('hfss_script.py', 'wt');

fprintf(script, 'import os\n');
fprintf(script, 'project_file = os.path.join(os.path.expanduser("~"), "Desktop")\n');
fprintf(script, 'project_file = os.path.join(project_file, "AntennaTemplate.aedt")\n');
fprintf(script, 'import ScriptEnv\n');
fprintf(script, 'ScriptEnv.Initialize("Ansoft.ElectronicsDesktop")\n');
fprintf(script, 'oDesktop.RestoreWindow()\n');
fprintf(script, 'oDesktop.OpenProject(project_file)\n');
fprintf(script, 'oProject = oDesktop.SetActiveProject("AntennaTemplate")\n');
fprintf(script, 'oDesign = oProject.SetActiveDesign("HFSSDesign1")\n');
fprintf(script, 'oDesign.ChangeProperty(["NAME:AllTabs", ["NAME:HfssTab", ["NAME:PropServers", "AnalysisSetup:Setup8"],\n');
fprintf(script, '            ["NAME:ChangedProps", [ "NAME:Solution Freq", "MustBeInt:=", False, "Value:=", "%fMHz"]]]])\n', frequency);
fprintf(script, 'oDesign.ChangeProperty([ "NAME:AllTabs", ["NAME:HfssTab", ["NAME:PropServers", "AnalysisSetup:Setup8:Sweep1"],\n');
fprintf(script, '            ["NAME:ChangedProps", [ "NAME:Start", "MustBeInt:=", False, "Value:=", "%fMHz"]]]])\n', sweepStart);
fprintf(script, 'oDesign.ChangeProperty([ "NAME:AllTabs", [ "NAME:HfssTab", [ "NAME:PropServers", "AnalysisSetup:Setup8:Sweep1" ],\n');
fprintf(script, '            [ "NAME:ChangedProps", [ "NAME:Stop", "MustBeInt:=", False, "Value:=", "%fMHz"]]]])\n', sweepStop);

fclose(script);
system('"C:\Program Files\AnsysEM\AnsysEM16.0\Win64\ansysedt.exe" -RunScript "hfss_script.py"')

This is a little better than the last example; now we only have to change one number, and the script will still act appropriately.