Uploaded by ferenczdavid90

CST - VBA Macros and Linking External Programs

advertisement
VBA Macros and Linking External
Programs
Automation/Macros/Templates/Batch/MATLAB
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 1
Contents








Introduction
Project Templates
Automatic Construction of Models
Template Based Postprocessing
ResultReader.dll
Linking External Programs
Creating Macros & Templates
Examples
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 2
Please keep in mind...
• Programming in VBA / Matlab / etc offers a wide area of
possibilites, which can„t be completely explored in short
time. It typically needs more time and energy as expected, until
everything works.
• Before starting
• Please check carefully, which alternative solutions exist. Please
discuss this with a VBA expert in CST„s support team, to find the
easiest out of many possible approaches.
• learn with existing small examples to gain experience. It is
typically quicker to change existing running examples, than to
start from scratch.
• CST provides VBA-Online help and several other examples
within installation and www, but still our main goal is to offer
comfortable solutions without the need of additional coding.
(CST Maintenance Fee does not include customized programming)
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 3
Motivation
Clicking on CST Logo brings
you to Content Slide
Why to use VBA-macros automation / external tools?
1. Automatic construction of models
2. Specialized-Postprocessing  Check Result templates,
existing ResultReader.dll + examples
3. Use external optimizers (=1.+2.)  check CST internal
optimizers
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 4
Project Templates
Customize Start Environment
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 5
Project Templates
Customize the default settings for particular types of applications.
At the beginning:
or later:
File -> New...
File -> Select Template...
The template library can be easily extended
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 6
Project Templates
Input some commands you often use for your MWS
structures, e.g.:
Frequency range, units, Background-mat + boundaries
Definition of materials (parameters, favorite colours ...)
Working plane settings (especially snapping !)
Monitors at favorite frequencies, ...
Open the history list
Mark the commands, press „Macro“
Give a name to your macro, e.g. „File / My defaults“
Click „Make globally available“, then „OK“
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 7
Project Templates
Source-Files are stored in
<CST-STUDIO-SUITE>\Library\New Project Templates
Each template <NAME>
consists of 3 files:
<NAME>.bmp
1) <NAME>.tpl (required)
(contains VBA-commands)
2) <NAME>.bmp (optional)
(contains displayed picture)
<NAME>.txt
3) <NAME>.txt (optional)
(contains displayed description)
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 8
Automatic Construction of Models
Difference: Structure & Control Macro
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 9
All Construction-Steps need to be stored in History List
Various Options exist
1. .mcs (structure macro) with BeginHide/EndHide/Assign for dialoguehandling, etc
Examples e.g. in Macros > Construct > Coils or Construct > Online
+ quickest way to bring structure parts to history list
- dialogue handling a bit tricky
2. .mcr (control macro) with VBA-command AddToHistory
Examples e.g. in Macros > Construct > Wires or Construct > Vias
+ dialogue handling easier than .mcs
- all commands need to be entered into a string first ““““, especially
cumbersome for VBA commands with arguments
3. External bas-file (might even contain subroutines) can be included
into history via
Macros > File > Import bas-file into history list
+ most flexible way for complex loops/subroutines
- bas-file is external file, not automatically included in cst-file
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 10
Structure Macros
 Modify the structure
 Stored in the history list for parametric model
definition
 Name: ... . mcs
 Example: Creation of advanced geometry, e.g.
spirals,…
Macros -> Construct -> Coils -> Sectioned Spiral
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 11
Control Macros
 Normally do not modify the structure
 Are not automatically stored in the history list
 If actions should be stored in the history list, all
commands need to be entered into a string first ““““
and then submitted to History via AddToHistory
 Name: ... . mcr
 Examples: Postprocessing, Calculation of analytical
line impedance, etc.
Macros -> Calculate -> Calculate Analytical Line
Impedance
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 12
Template Based Postprocessing
Customized Postprocessing within STUDIO
Environment
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 13
Existing Result Templates
Purpose: Customized and automized postprocessing
Many postprocessing options are
already available.
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 14
Result Templates
 Store in <installationpath>/Library/Result
Templates/1D/my_template.rtp
 Will be evaluated after each solver run.
 Can perform just an action or return 1D or 0D values.
Performs an action
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 15
Result Templates
 Store in <installationpath>/Library/Result
Templates/0D/my_template.rtp
 Will be evaluated after each solver run.
 Can perform just an action or return 1D or 0D values.
 0D Results can serve as Optimizer Goal
Returns a value
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 16
ResultReader.dll
Customized Postprocessing from External
Programs
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 17
ResultReader.dll
Provides external access to CST results
C:\ProgramFiles\CST-STUDIO-SUITE\ResultReaderDLL
Ready to use Matlab-Scripts, also for accessing 3D-Fielddata and farfields
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 18
ResultReader.dll - Examples
1D Results
Nearfields
(only hex-mesh
no subgrid)
Farfields
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 19
ResultReader.dll  Online Help
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 20
Linking External Programs
Usage of external Optimizers
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 21
Integration Into Workflow
e.g. ppt-Reports
MS Windows
Scripting
Host
Excel, Word,
Matlab, AutoCad,
etc...
COM
DCOM
e.g. bidirectional Excel link
CST Software
(MWS, DS)
CST Studios can be both: OLE client and server
OLE: CommunicationStandard for Data Exchange
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 22
Changing Parameter Values from outside CST
Whenever CST geometries shall be modified from
external software (e.g. Matlab), we recommend to
open an existing cst-file, containing parameters.
Those Parameters can then be modified, as shown
below. Afterwards „Rebuild“ updates the history and
applies the changes to geometry and other settings.
control program
Bas-file
StoreParameter „gap“ „7“
Matlab
invoke(mws, 'StoreParameter',‘gap', value);
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 23
Command Line Options  Online Help
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 24
Example Batch Mode (Command Line)
"C:\ProgramFiles\CST-STUDIO-SUITE\CST DESIGN ENVIRONMENT.exe" –m C:\Work\Start_CST.bas
The contents of the Start_CST.bas file could look as follows:
Sub Main
OpenFile("C:\work\test1.cst”)
Solver.Start
Save
End Sub
If only a solver should be started, above construction
can be shortened via:
"C:\ProgramFiles\CST-STUDIO-SUITE\CST DESIGN ENVIRONMENT.exe" –m –r "C:\work\test1.cst”
-r = T-Solver
-f = F-Solver
-e = E-Solver
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 25
Matlab calls and controls CST
% Start CST Studio (always last installed CST version is used)
cst = actxserver('CSTStudio.application')
mws = invoke(cst, 'NewMWS')
% Open the MWS model
invoke(mws, 'OpenFile', 'C:\CST_Test\tsplitter.cst');
for iii = 1:1:2
% Parameter sweep
% Set value of parameter
invoke(mws, 'StoreParameter','offset', iii);
invoke(mws, ‚Rebuild„);
% Start solver
solver = invoke(mws, 'Solver');
invoke(solver, 'Start');
% Take over the result; here, as an example, the amplitude (linear) of S11
result = invoke(mws, 'Result1D', 'a1(1)1(1)');
numOfValue = invoke(result, 'GetN'); % Might be used to initialize the variables
invoke(result, 'Save', 'C:\tmp\filename');
A = importdata('C:\tmp\filename', ' ', 4)
x(:, iii) = A.data(:, 1);
% x-data column
y(:, iii) = A.data(:, 2);
% y-data column
end
invoke(mws, 'Save');
invoke(mws, 'Quit');
release(result);
release(solver);
release(mws);
release(cst);
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 26
Reminder: ResultReader.dll
ResultReader.dll can be
used in combination
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 27
Creating Macros & Templates
Integrated Development Environment
GUI Builder
Macro Structure
Templates Structure
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 28
The Integrated Development Environment
Open IDE
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 29
The Integrated Development Environment
Run the macro
Debug the macro
Edit macro code here (syntax highlighting)
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 30
The Integrated Development Environment
Macro Debugger:
 Set breakpoints
 Step through the macro
 Watch variables
 etc…
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 31
The Integrated Development Environment
Graphical GUI builder
Source code for the dialog box is automatically created and
Inserted at the caret‘s position
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 32
Where macro files are stored
Global macros:
stored in the Global macro path
(INST_DIR\Library\Macros)
 Local macros:
stored in the same path as the project
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 33
Macro Name Convention
Macro name:
Besides the name the macro name defines if it is a structure
or control macro and in which of the CST STUDIO SUITE™
programs it will be available.
Folder where the macro is
stored defines the position in
the macro hierarchy
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 34
Structure of a CST STUDIO SUITE Macro
Comments – start with '
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 35
Structure of a CST STUDIO SUITE Macro
Options:
Option Explicit – Force declaration
of all variables
Option Private Module – Public variables
invisible from outside the project
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 36
Structure of a CST STUDIO SUITE Macro
Included libraries
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 37
Structure of a CST STUDIO SUITE Macro
Public variables
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 38
Structure of a CST STUDIO SUITE Macro
Every macro contains at least
the subroutine „Main“
exept Result Templates!
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 39
Structure of a CST STUDIO SUITE Macro
Other needed functions
and subroutines
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 40
Structure of a Result Template
Defines all necessary
input data
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 42
Structure of a Result Template
Dialog built by GUI
builder
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 43
Structure of a Result Template
Gets default settings/
already stored settings
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 44
Structure of a Result Template
Stores Settings in
common script
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 45
Structure of a Result Template
Mandatory function for
a 1D Template
(0D Templates need Evaluate0D As Double)
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 46
How to Create Macros?
There exist different ways to create a macro
 Copy and modify an existing macro
 Go to the history list, select lines and press „Macro“
 Use Macro -> Make VBA Control Macro and start from Scratch
 Let MWS create the macro‘s framework by pressing „Edit“ for
 User defined excitation function
 User defined parameter sweep watch
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 47
List of all VBA Objects
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 48
History List
VBA commands can be obtained by executing the action via
the GUI and checking the history list for the command.
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 49
Load exisiting Macros
All existing Macros and Templates are stored in:
With drag and drop they can be opened in the VBA editor.
A macro similar to the current needs can be chosen and modified.
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 50
Getting More Information
 Online Help
 All commands are also listed in the Online Help
 Have a look at the pre-loaded macro examples
 Visit a special training class on macroprogramming
 Learning by doing….
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 51
Example
Farfield Plot Sweep
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 52
Example: Farfield Plot Sweep Macro
Selects Farfield for superposition,
frequency evt. Needs to be adapted.
Settings for number of antennas,
spaceshift, phaseshift. Fixed for the
time beeing. (Variable in Template)
Settings for farfield plot
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 53
Example: Farfield Plot Sweep Macro
Sets array settings.
Adds obtained value to 1D object
Adds 1D Object to the tree
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 54
Obtain Results
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 55
Example
Farfield Plot Sweep: Version as Template with
Dialog
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 56
Example: Farfield Plot Sweep - Result Template
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 57
Example: Farfield Plot Sweep - Result Template
Function Define(sName As String, bCreate As Boolean, bNameChanged As Boolean) As Boolean
Define = True
Begin Dialog UserDialog 400,203 ' %GRID:10,7,1,1
GroupBox 20,14,360,182,"Antenna Array Sweep",.GroupBox1
Text 40,35,90,14,"Direction:",.Text1
Text 160,35,90,14,"X",.Text2,2
Text 40,63,90,14,"Number:",.Text3
TextBox 160,63,90,21,.nant
Text 40,98,90,14,"Spaceshift:",.Text4
Text 40,133,90,14,"Phaseshift:",.Text5
TextBox 160,98,90,21,.spaceshift
TextBox 160,133,90,21,.phaseshift
OKButton 40,168,140,21
CancelButton 210,168,140,21
End Dialog
Dim dlg As UserDialog
dlg.nant
= GetScriptSetting("NumberOfAntennas","1")
dlg.spaceshift = GetScriptSetting("SpaceShift","0")
dlg.phaseshift = GetScriptSetting("PhaseShift","0")
User dialog editor
Default values
If (Not Dialog(dlg)) Then
' The user left the dialog box without pressing Ok.
Define = False
Else
' The user properly left the dialog box by pressing Ok.
Define = True
' Store the script settings into the database for later reuse by either the define function (for modifications)
' or the evaluate function.
StoreScriptSetting("NumberOfAntennas",dlg.nant)
StoreScriptSetting("SpaceShift",dlg.spaceshift)
StoreScriptSetting("PhaseShift",dlg.phaseshift)
End If
End Function
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 58
Store Settings
Needs to be adapted, here fixed
Read previously defined settings
Select plot to be evaluated
Change array settings and update
Evaluate value of interest and add to 1D results
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 59
Example: Farfield Plot Sweep - Result Template
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 60
Example: Farfield Plot Sweep Template
CST – COMPUTER SIMULATION TECHNOLOGY | www.cst.com | May-10 | 61
Download