CST宏讲解---想了解的可以看看!
Examples Overview
The usage of the VBA macro language is described in detail in the printed documentation "Advanced topics".
Here are some typical examples to get in touch with the CST MICROWAVE STUDIO® VBA language:
General
• How to use the VBA Objects in general.
Modelling:
• Create analytical 2D curve.
• Create analytical 3D curve.
Solver and Post processing
• Accessing S-Parameter Phase.
• Calculating the real part of S11 at a certain frequency.
• Define an excitation function.
• Adding user defined entries to the result tree.
• Creating 3D scalar and vector plots:
• Create a farfield array.
How to use the VBA Objects
Example 1: Call CST MICROWAVE STUDIO™ from within an external application
Creates a CST MICROWAVE STUDIO™ Application Object, opens a project and creates a brick:
dim app as Object
‘Starts the CST MICROWAVE STUDIO™
set app = CreateObject(”MWStudio.Application”)
app.OpenFile(”c:Exampleswg.mod”)
dim br as Object
set br = CreateObject(”MWStudio.Brick”)
With br
.Reset
.Name "BrickOne"
.Layer "default"
.Xrange "0", "1"
.Yrange "0", "2"
.Zrange "0", "4"
.Create
End With
Example2: Open a file within the build-in VBA interpreter
Opens a project from the VBA-Interpreter of the CST MICROWAVE STUDIO™:
OpenFile(”c:Exampleswg.mod”)
With br
.Reset
.Name "BrickOne"
.Layer "default"
.Xrange "0", "1"
.Yrange "0", "2"
.Zrange "0", "4"
.Create
End With
Create an anayltical 2D curve
'--------------------------------------------------
' arbitrary xy-function is converted into a curve
'--------------------------------------------------
Sub Main ()
On Error GoTo Curve_Exists
Curve.NewCurve "2D-Analytical"
Curve_Exists:
On Error GoTo 0
Dim sCurveName As String
BeginHide
Dim iii As Integer
iii = 1
While SelectTreeItem("Curves2D-Analyticalspline_"+CStr(iii))
iii = iii+1
Wend
sCurveName = "spline_"+CStr(iii)
assign "sCurveName"
EndHide
With Spline
.Reset
.Name sCurveName
.Curve "2D-Analytical"
' ======================================
' !!! Do not change ABOVE this line !!!
' ======================================
' -----------------------------------------------------------
' adjust x-range as for-loop parameters (xmin,max,stepsize)
' enter y-Function-statement within for-loop
' fixed parameters a,b,c have to be declared via Dim-Statement
' -----------------------------------------------------------
' NOTE: available MWS-Parameters can be used without
' declaration at any place (loop-dimensions, ...)
' (for parametric curves during parameter-sweeps and optimisation !)
' -------------------------------------------
Dim xxx As Double, yyy As Double
Dim aaa As Double ' not necessary if aaa is model parameter
aaa = 1.23456/Atn(0.5)
For xxx = 1.5 To 10 STEP 0.5
yyy = 3*aaa/xxx + Sin(xxx^2)
.LineTo xxx , yyy
Next xxx
' ======================================
' !!! Do not change BELOW this line !!!
' ======================================
.Create
End With
SelectTreeItem("Curves2D-Analytical"+sCurveName)
End Sub
Create an analytical 3D curve
'--------------------------------------------------
' arbitrary analytical xyz-function is converted into a curve
'--------------------------------------------------
Sub Main ()
On Error GoTo Curve_Exists
Curve.NewCurve "3D-Analytical"
Curve_Exists:
On Error GoTo 0
Dim sCurveName As String
BeginHide
Dim iii As Integer
iii = 1
While SelectTreeItem("Curves3D-Analytical3dpolygon_"+CStr(iii))
iii = iii+1
Wend
sCurveName = "3dpolygon_"+CStr(iii)
assign "sCurveName"
EndHide
With Polygon3D
.Reset
.Name sCurveName
.Curve "3D-Analytical"
' ======================================
' !!! Do not change ABOVE this line !!!
' ======================================
' -----------------------------------------------------------
' adjust x-range as for-loop parameters (xmin,max,step)
' enter y/z-Function-statement within for-loop
' fixed parameters a,b,c have to be declared via Dim-Statement
' -----------------------------------------------------------
' NOTE: available MWS-Parameters can be used without
' declaration at any place (loop-dimensions, ...)
' (for parametric curves during parameter-sweeps and optimisation !)
' -------------------------------------------
Dim xxx As Double, yyy As Double, zzz As Double
Dim aaa As Double ' not necessary if aaa is model parameter
aaa = 0.3
For xxx = Pi/2 To 10*2*Pi STEP 0.2
yyy = Sqr(2*Abs(Cos(xxx))) + 3*aaa/xxx + 2*Sin(xxx)
zzz = Abs(yyy) - Sqr(2*Abs(Cos(xxx))) + 3*aaa/xxx + 2*Sin(xxx)
.Point Sin(xxx) , Cos(xxx) , aaa*xxx
Next xxx
' ======================================
' !!! Do not change BELOW this line !!!
' ======================================
.Create
End With
SelectTreeItem("Curves3D-Analytical"+sCurveName)
End Sub
Accessing S-Parameter Phase
Example 1: Accessing all values of S-Parameter phase, stored in file ^p1(1)1(1).sig
With Result1D ("p1(1)1(1)") ‘ Connect to phase of S11
nn = .GetN ‘ Get number of frq-points
For ii = 0 To nn-1
' Read all points; index of first point is zero.
vx = .GetX(ii) ‘ here: frequency
vy = .GetY(ii) ‘ here: according phase
Next ii
End With
Calculating the real part of S11 at a certain frequency
Calculating the real part of S11 at a certain frequency (here 0.65 GHz)
Dim a11 As Object
Dim p11 As Object
Set a11 = Result1D ("a1(1)1(1)")
Set p11 = Result1D ("p1(1)1(1)")
Dim n As Integer
Dim frq As Double
frq=0.65
n=a11.GetClosestIndexFromX(frq)
phase = Pi/180.0 * p11.GetY(n)
ampli = a11.GetY(n)
real = ampli * cos(phase)
ResultTree Examples
The ResultTree VBA Object offers very interesting possibilities to configure the Navigation Tree. It is possible to insert different simulation results as well as VBA macros. The following examples will show its functionality.
Examples to add Signal Views into the tree
All following examples add items into the folder ”My S-Parameters”. If this folder does not exist it will be created.
For inserting signals into the tree it is possible to import results from other projects. Therefore the complete result file name must be specified with the File method. If the results of the current project shall be inserted, the project name in the result file name may be omitted.
Signal into Tree 1: Adds a linear view of the amplitude of an extern S11 S-Parameter into the folder.
Signal into Tree 2: Adds a linear view of the phase of an extern S11 S-Parameter into the folder.
Signal into Tree 3: Adds a Smith Chart view of a S11 S-Parameter into the folder.
Signal into Tree 4: Adds a Polar Plot of the S11 S-Parameter into the folder.
Examples to add Field Plot Views into the tree
For results others than Signals only those of the current project can be inserted into the tree!
Field Plot into Tree: Adds a 3D-Vector plot into the tree.
Farfield into Tree: Adds a Farfield into the tree.
Examples to add a macro into the tree
Every time a macro is executed from the tree, a VBA command interpreter is started and evaluates the string that has been set by the Macro method. Therefore no variables that are defined within the script that creates the folder entry can be accessed by the macro.
Macro into Tree 1: Adds a command to start the time domain solver into the tree.
Macro into Tree 2: Adds a macro into the tree.
Macro into Tree 3: Adds an external VBA script into the tree.
Signal into Tree 1
Adds a linear view of the amplitude of the S11 S-Parameter from the project 봂xtProj?into the folder. ExtProj is supposed to be located in the directory of the current project.
With ResultTree
.Reset
.Name "My S-ParametersS11 (lin)" ' The entry name and its destination folder
.Title "S11 (linear)" ' Title of the plot
.File "ExtProj^a1(1)1(1).sig" ' The name of the signal file
.Type "XYSignal" ' Plot Type
.XLabel "f/Hz" ' Label for the x-axis
.Subtype "Linear" ' Sub Plot Type
.Add
End With
See also: Result Tree
Signal into Tree 2
Adds a linear view of the phase of the S11 S-Parameter from the project 봂xtProj?into the folder. ExtProj is supposed to be located in the directory of the current project.
With ResultTree
.Reset
' The entry name and its destination folder
.Name "My S-ParametersS11 (phase)"
.Title "S11 (phase)" ' Title of the plot
.File "ExtProj^p1(1)1(1).sig" ' The name of the signal file
.Type "XYSignal" ' Plot Type
.XLabel "f/Hz" ' Label for the x-axis
.Subtype "Phase" ' Sub Plot Type
.Add
End With
See also: Result Tree
Signal into Tree 3
Adds a Smith Chart view of the S11 S-Parameter from the current project into the folder.
A value has to be given to which the values in the plot are normalized.
With ResultTree
.Reset
' The entry name and its destination folder
.Name "My S-ParametersS11 (Smith Chart)"
.Title "S11" ' Title of the plot
.ReferenceImpedance "50" ' Impedance to which the Values
' will be normalized
' Result files with the amplitude and phase values
.File "^a1(1)1(1).sig"
.File2 "^p1(1)1(1).sig"
.Type "SmithSignal" ' Type of the plot
.Add
End With
See also: Result Tree
Signal into Tree 4
Adds a Polar Plot view of the S11 S-Parameter from the current project into the folder.
With ResultTree
.Reset
' The entry name and its destination folder
.Name "My S-ParametersS11 (polar)"
' Result files with the amplitude and phase values
.File "^a1(1)1(1).sig"
.File2 "^p1(1)1(1).sig"
.Type "PolarSignal" ' Type of the plot
.Add
End With
Field Plot into Tree
Adds a Field Plot view of the electric field from the current project into the folder ”My FieldView”.
With ResultTree
' The entry name and its destination folder
.Name "My FieldViewE_Field"
.File "^e1_1.m3d" ' Result file name
.Type "Efield3D" ' Entry type
.Add
End With
Farfield into Tree
Adds a Farfield Plot view from the current project into the folder 봎y FieldView?
With ResultTree
' The entry name and its destination folder
.Name "My FieldViewFarfield"
.File "^ff1_1.ffm" ' Result file name
.Type "Farfield" ' Entry type
.Add
End With
Macro into Tree 1
Adds a command into the 봘serdefined?folder that starts the time domain solver.
With ResultTree
' The entry name and its destination folder
.Name "UserdefinedMacro1"
' String to be evaluated by the VBA Interpreter
.Macro "Solver.Start"
.Type "Macro" ' Entry type
.Add
End With
Macro into Tree 2
Adds the previously defined control macro ”AutoTest” into the tree.
At first a string will be defined that contains a VBA command that executes the control macro. The command is ”RunMacro” that takes a string (the name of the control macro) as its argument. However, strings have to be specified within quotes. Unfortunately quotes are special characters which are not recognized as normal characters. They mark the start and the end of a string. Therefore the variable a is defined with a single quote as its only content. With this quote the entire command string can be constructed.
Dim a As String
a = """
a = "RunMacro " & a & "AutoTest" & a
' a contains the string: RunMacro "AutoTest"
With ResultTree
' The entry name and its destination folder
.Name "UserdefinedMacro2"
' String to be evaluated by the VBA Interpreter
.Macro a
.Type "Macro" ' Entry type
.Add
End With
Macro into Tree 3
Adds an external VBA script into the tree. Let the name of the external macro be ”Macro1.bas” that will be located in the directory of the current project.
At first a string will be defined that contains a VBA command that executes an external VBA script file. The command is ”MacroRun” that takes a string (the name of the script) as its argument. However, strings have to be specified within quotes. Unfortunately quotes are special characters which are not recognized as normal characters. They mark the start and the end of a string. Therefore the variable a is defined with a single quote as its only content. With this quote the entire command string can be constructed.
Dim a As String
a = """
a = "MacroRun " & a & "Macro1.bas" & a
' a contains the string: MacroRun "Macro1.bas"
With ResultTree
' The entry name and its destination folder
.Name "UserdefinedMacro3"
' String to be evaluated by the VBA Interpreter
.Macro a
.Type "Macro" ' Entry type
.Add
End With
ScalarPlot3D Example
The following script plots surfaces of equal amplitude of the vector field component Y of the electric field ”e1”.
‘ Plot only a wire frame of the structure to be able to look inside
Plot.wireframe True
‘ Select the Y-Component of the electric field e1 in the tree
SelectTreeItem ("2D/3D ResultsE-Fielde1Y")
‘ Plot the scalar field of the selected monitor
With ScalarPlot3D
.Type "isosurfaces"
.PlotAmplitude False
.Scaling 50
.LogScale False
.ContourLines False
.ScaleToVectorMaximum True
.Quality 60
.Plot
End With
See Also: SelectTreeItem, Plot
VectorPlot3D Example
The following script plots the electric field ”e1” (if available) in a linear scale by using 400 colored arrows.
‘ Plot only a wire frame of the structure to be able to look inside
Plot.wireframe True
‘ Select the desired monitor in the tree.
SelectTreeItem ("2D/3D ResultsE-Fielde1")
‘ Plot the field of the selected monitor
With VectorPlot3D
.Type "arrowscolor"
.Objects 400
.Scaling 50
.LogScale False
.Plot
End With
See Also: SelectTreeItem, Plot
FarfieldArray Object
Group: Postprocessing
Description: Defines the antenna array pattern for a farfieldplot based on a single antenna element.
Methods: Reset , UseArray , Arraytype , XSet , YSet , Zset , SetList , DeleteList , Antenna
Functions: none
Example:
With FarfieldArray
.Reset
.UseArray "True"
.Arraytype "Rectangular"
.ZSet "2", "2,5", "90"
.SetList
.Arraytype "Edit"
.Antenna "0", "0", "0", "1.0", "0"
End With
是vb,我现在的做法是建模了以后把history里面的东西直接生成vba,然后放在vb里面,改改格式,再写几句需要的程序,比较笨:P
讲的不清楚,我以前用过ansoft的宏命令,用的熟练了,很好用的,效率很高!
强人啊,用来干啥的
好!