Hottomson 发表于 2008-6-20 16:55

Maxwell 3D v11 VBScript Example

前言:参考官方的文档An M3DFS Macro Example编写了Maxwell 3D v11的脚本,与原文档格式基本一致,方便对照,主要的变动是原mac采用重新建立模型,这里采用修改模型的方法。没有采用中文编写,我英语水平超水(四级都没有过 ) ,语法、词汇错误就别指正了。

Defining the Problem
The device in this example is an actuator whose main parts are a magnetic circuit made of nonlinear steel and an energizing coil. In this analysis, you will plot the force on the armature as a function of the gap for
the coil current, as well as the coil’s self inductance as a function of the gap for the same currents. Both plots must be formatted in such a way that they can be imported directly into PlotData.
The macro reads all the necessary information from an ASCII file. This data set should contain the description of the geometry, range of gap values, range of current values, and the number of turns of the coil. The number of gaps and current steps will always be greater than two.
Finally, the macro should create the model to take advantage of the two-fold symmetry. Thus, only one- quarter of the model should be created. The results obtained from the macro (the plots of the force and inductance) should be scaled to represent the characteristics of the full model. Since Maxwell reports the
per turn inductance, an appropriate scaling must be performed to represent the measurable inductance.

The magnetic circuit is composed of a U-shaped object (in the quarter model it appears as an L-shaped object), a cylindrical core (one-quarter of it in the quarter model) and an armature. The cylindrical coil is wound around the core. The quarter model of the device is shown above.

Create the Script
The easiest way to create a script is to record the operations performed during the normal creation of the model. Once the model is created some editing is required to replace values used in the model with variables . Additional lines will usually be needed inside the script.
Start recording the script:
1 Click Tools>Record Script.
The Save As dialog box appears.
2 Use the file browser to locate the folder in which you want to save the script, such as C:\Ansoft\Maxwell11\Scripts, and then double-click the folder’s name.
3 Type “actuator” in the File name text box, and then click Save.
The script is saved in the folder you selected by the file name actuator.vbs.

Create the Model for the Macro
Now that the script is recording steps, create the 3D model in the Maxwell 3D Modeler.
        Create the model
1.        Click Project>Maxwell Design and select mm as units for the geometry.
2.        Create a box with the first corner at (0, 0, 0) and a size (40, 40, 5). Name this box u_plate.
3.        Create an additional box (part of the U-plate subassembly) with the first corner at (0, 35, 5) and the opposite corner at (40, 40, 65). Name this box u_plate1.
4.        Unite the two boxes. The U-plate subassembly is now complete.
5.        Create a new box with the first corner at (0, 0, 70) and the diagonally opposed corner at (50, 50,75). Name this box armature.
6.        Choose Lines/Rectangle and create a rectangle with the first corner at (0, 0, 5) in the xz plane with size x=10, z=60. Name this rectangle core.
7.        Choose Draw/Sweep/Around Axis… and sweep the rectangle clockwise around the z-axis by 90 degrees, using 8 segments. The magnetic circuit is now complete and only the coil needs to be added to the model.
8.        Choose Lines/Rectangle and create a rectangle with the first corner at (15, 0, 20) in the xz plane and the opposite corner at (25, 0, 50). Name this rectangle coil.
9.        Choose Draw/Sweep/Around Axis and create the coil by sweeping the rectangle around the z- axis 90 degrees using 8 segments. You should now add terminals to both end faces of the quarter coil.
10.        Select coil and choose 3D Modeler/Surface/Section and create two rectangles on the end faces and name them term1 and term2.
11.        Choose Draw/Region and create a region with the padding percentages:–X:0, +X: 300, -Y:0, +Y:300, -Z:50, +Z:100.
12.        Choose File/Save to save the project.

Assign Materials and Sources
Once the model is complete, you can assign material properties and the current sources to the objects in the model.
        Define the materials, sources, and executive parameters:
1.        Assign steel_1010 to the armature, u_plate, and core.
2.        Assign copper to the coil.
3.        Assign 1000 amperes to the two terminals of the coil.
4.        Choose File/Save to save the project.

Define Executive Parameters and Solution Options
Now define which solutions are to be generated, and the conditions with which they are to be solved.
        Define the executive parameters and solution options:
1.        Select the virtual force calculation for the armature.
2.        Choose coil as the object to be included in the inductance matrix calculation.
3.        Choose Maxwell/Anlysis setup/Add solution setup. The Solve Setup window appears.
4.        Set percent refinement per pass to 50% and leave the rest of the parameters at their default values.
5.        Choose OK.

Stop Recording the Macro
All steps needed for the script have now been completed.
To stop recording a script:
• On the Tools menu, click Stop Script Recording.
Maxwell stops recording to the script.

Hottomson 发表于 2008-6-20 16:56

Completed Macro
The following script can be executed as it appears. As a result, the model will be reproduced exactly as it was created. However, because the intent is to create a more complex script, some editing is required.Dim oAnsoftApp
Dim oDesktop
Dim oProject
Dim oDesign
Dim oEditor
Dim oModule

Set oAnsoftApp = CreateObject("AnsoftMaxwell.MaxwellScriptInterface")
Set oDesktop = oAnsoftApp.GetAppDesktop()
oDesktop.RestoreWindow
Set oProject = oDesktop.SetActiveProject("Project1")
oProject.InsertDesign "Maxwell", "MaxwellDesign1", "Magnetostatic", ""
Set oDesign = oProject.SetActiveDesign("MaxwellDesign1")
Set oEditor = oDesign.SetActiveEditor("3D Modeler")

oEditor.CreateBox Array("NAME:BoxParameters", "CoordinateSystemID:=", -1, "XPosition:=",_
"0mm", "YPosition:=", "0mm", "ZPosition:=", "0mm", "XSize:=", "40mm", "YSize:=",_
"40mm", "ZSize:=", "5mm"), Array("NAME:Attributes", "Name:=", "u_plate", "Flags:=", "", "Color:=",_
"(132 132 193)", "Transparency:=", 0, "PartCoordinateSystem:=", "Global", "MaterialName:=",_
"vacuum", "SolveInside:=", true)

oEditor.CreateBox Array("NAME:BoxParameters", "CoordinateSystemID:=", -1, "XPosition:=",_
"0mm", "YPosition:=", "35mm", "ZPosition:=", "5mm", "XSize:=", "40mm", "YSize:=",_
"5mm", "ZSize:=", "60mm"), Array("NAME:Attributes", "Name:=", "u_plate1", "Flags:=", "", "Color:=",_
"(132 132 193)", "Transparency:=", 0, "PartCoordinateSystem:=", "Global", "MaterialName:=",_
"vacuum", "SolveInside:=", true)

oEditor.Unite Array("NAME:Selections", "Selections:=", "u_plate,u_plate1"), Array("NAME:UniteParameters", "CoordinateSystemID:=",_
-1, "KeepOriginals:=", false)
oEditor.CreateBox Array("NAME:BoxParameters", "CoordinateSystemID:=", -1, "XPosition:=",_
"0mm", "YPosition:=", "0mm", "ZPosition:=", "70mm", "XSize:=", "50mm", "YSize:=",_
"50mm", "ZSize:=", "5mm"), Array("NAME:Attributes", "Name:=", "armature", "Flags:=",_
"", "Color:=", "(132 132 193)", "Transparency:=", 0, "PartCoordinateSystem:=",_
"Global", "MaterialName:=", "vacuum", "SolveInside:=", true)
oEditor.CreateRectangle Array("NAME:RectangleParameters", "CoordinateSystemID:=",_
-1, "IsCovered:=", true, "XStart:=", "0mm", "YStart:=", "0mm", "ZStart:=", "5mm", "Width:=",_
"60mm", "Height:=", "10mm", "WhichAxis:=", "Y"), Array("NAME:Attributes", "Name:=",_
"core", "Flags:=", "", "Color:=", "(132 132 193)", "Transparency:=", 0, "PartCoordinateSystem:=",_
"Global", "MaterialName:=", "vacuum", "SolveInside:=", true)
oEditor.SweepAroundAxis Array("NAME:Selections", "Selections:=", "core"), Array("NAME:AxisSweepParameters", "CoordinateSystemID:=",_
-1, "DraftAngle:=", "0deg", "DraftType:=", "Round", "CheckFaceFaceIntersection:=",_
false, "SweepAxis:=", "Z", "SweepAngle:=", "90deg", "NumOfSegments:=", "8")
oEditor.CreateRectangle Array("NAME:RectangleParameters", "CoordinateSystemID:=",_
-1, "IsCovered:=", true, "XStart:=", "15mm", "YStart:=", "0mm", "ZStart:=",_
"20mm", "Width:=", "30mm", "Height:=", "10mm", "WhichAxis:=", "Y"), Array("NAME:Attributes", "Name:=",_
"coil", "Flags:=", "", "Color:=", "(132 132 193)", "Transparency:=", 0, "PartCoordinateSystem:=",_
"Global", "MaterialName:=", "vacuum", "SolveInside:=", true)
oEditor.SweepAroundAxis Array("NAME:Selections", "Selections:=", "coil"), Array("NAME:AxisSweepParameters", "CoordinateSystemID:=",_
-1, "DraftAngle:=", "0deg", "DraftType:=", "Round", "CheckFaceFaceIntersection:=",_
false, "SweepAxis:=", "Z", "SweepAngle:=", "90deg", "NumOfSegments:=", "8")
oEditor.Section Array("NAME:Selections", "Selections:=", "coil"), Array("NAME:SectionToParameters", "CoordinateSystemID:=",_
-1, "SectionPlane:=", "ZX")
oEditor.ChangeProperty Array("NAME:AllTabs", Array("NAME:Geometry3DAttributeTab", Array("NAME:PropServers",_
"Section1"), Array("NAME:ChangedProps", Array("NAME:Name", "Value:=", "term1"))))
oEditor.Section Array("NAME:Selections", "Selections:=", "coil"), Array("NAME:SectionToParameters", "CoordinateSystemID:=",_
-1, "SectionPlane:=", "YZ")
oEditor.ChangeProperty Array("NAME:AllTabs", Array("NAME:Geometry3DAttributeTab", Array("NAME:PropServers",_
"Section1"), Array("NAME:ChangedProps", Array("NAME:Name", "Value:=", "term2"))))
oEditor.CreateRegion Array("NAME:RegionParameters", "CoordinateSystemID:=", -1, "+XPadding:=",_
"300", "-XPadding:=", "0", "+YPadding:=", "300", "-YPadding:=", "0", "+ZPadding:=",_
"100", "-ZPadding:=", "50"), Array("NAME:Attributes", "Name:=", "Region", "Flags:=",_
"Wireframe#", "Color:=", "(255 0 0)", "Transparency:=", 0, "PartCoordinateSystem:=",_
"Global", "MaterialName:=", "vacuum", "SolveInside:=", true)
oEditor.ChangeProperty Array("NAME:AllTabs", Array("NAME:Geometry3DAttributeTab", Array("NAME:PropServers",_
"armature", "u_plate", "core"), Array("NAME:ChangedProps", Array("NAME:Material", "Material:=",_
"steel_1010"))))
oEditor.ChangeProperty Array("NAME:AllTabs", Array("NAME:Geometry3DAttributeTab", Array("NAME:PropServers",_
"coil"), Array("NAME:ChangedProps", Array("NAME:Material", "Material:=", "copper"))))
Set oModule = oDesign.GetModule("BoundarySetup")
oModule.AssignCurrent Array("NAME:Current1", "Current:=", "1000A", "IsSolid:=",_
true, "Point out of terminal:=", false, "Objects:=", Array("term1"))
oModule.AssignCurrent Array("NAME:Current2", "Current:=", "1000A", "IsSolid:=",_
true, "Point out of terminal:=", true, "Objects:=", Array("term2"))
Set oModule = oDesign.GetModule("MaxwellParameterSetup")
oModule.AssignForce Array("NAME:Force1", "Is Virtual:=", true, "Reference CS:=",_
"Global", "Objects:=", Array("armature"))
oModule.AssignMatrix Array("NAME:Matrix1", Array("NAME:MatrixEntry", Array("NAME:MatrixEntry", "Source:=", _
"Current1", "NumberOfTurns:=", "1")), Array("NAME:MatrixGroup"), "Objects:=", Array( _
"coil"))
Set oModule = oDesign.GetModule("AnalysisSetup")
oModule.InsertSetup "Magnetostatic", Array("NAME:Setup1", "MaximumPasses:=", 10, "MinimumPasses:=",_
2, "MinimumConvergedPasses:=", 1, "PercentRefinement:=", 30, "LinearResidual:=",_
1E-005, "ConvergenceDispParamId:=", -1, "SolveFieldOnly:=", false, "PercentError:=",_
1, "SolveMatrixAtLast:=", true, "UseOutputVariable:=", false, "PreAdaptMesh:=",_
false, "NonLinearResidual:=", 0.01, "SolverType:=", 0, "MuNonLinearBH:=", true, "ComputeHc:=",_
false, "HcNonLinearBH:=", true)

[ 本帖最后由 Hottomson 于 2008-6-20 17:05 编辑 ]

Hottomson 发表于 2008-6-20 16:57

Parameterizing the Macro Geometry
In cases where the geometry needs to be parameterized, substantial editing is required. First, several variables defining the geometry have to be defined. Variables are initialized as follows:'Define
Dim        u_plate_length
Dim        u_plate_width
Dim        u_plate_thick
Dim        u_plate_high
Dim        armature_length       
Dim        armature_width
Dim        armature_thick
Dim        core_radius       
Dim        core_height       
Dim        coil_1_height
Dim        coil_1_inner_rad
Dim        coil_1_outer_rad
Dim        coil_1_position       
Dim        no_current_steps
Dim        no_turns_coil1
Dim        current_min
Dim        current_max
Dim        no_gaps       
Dim        gap_min
Dim        gap_max

u_plate_length = 100
u_plate_width =        60
u_plate_thick =        5
u_plate_high = 75
armature_length        = 120
armature_width = 80
armature_thick = 5
core_radius        = 10
core_height        = 70
coil_1_height =        5
coil_1_inner_rad = 15
coil_1_outer_rad = 25
coil_1_position        = 25
no_current_steps = 2
no_turns_coil1 = 1000
current_min        = 0.5
current_max        = 1
no_gaps        = 2
gap_min        = 0.01
gap_max        = 1
The current range, number of current steps, and number of turns of the coil are also included because it is a good programming style to keep the initialization of all the parameters together. Names chosen for the variables are meaningful and describe their role in the model.

These names correspond with the following figure:



Parameterization
Next, the dimensions and locations of all the objects in the model are parameterized by replacing some of the numerical values with corresponding variables. Some calculations are also performed where needed.
As an example of how to do this, examine the following lines. Here, both the original and revised format of a geometry command are shown:oEditor.CreateBox Array("NAME:BoxParameters", "CoordinateSystemID:=", -1, "XPosition:=",_
"0mm", "YPosition:=", "0mm", "ZPosition:=", "0mm", "XSize:=", "40mm", "YSize:=",_
"40mm", "ZSize:=", "5mm"), Array("NAME:Attributes", "Name:=", "u_plate", "Flags:=", "", "Color:=",_
"(132 132 193)", "Transparency:=", 0, "PartCoordinateSystem:=", "Global", "MaterialName:=",_
"vacuum", "SolveInside:=", true)is replaced with the sequenceDim        u_plate_x,u_plate_y
u_plate_x=u_plate_width/2
u_plate_y=u_plate_length/2
oEditor.CreateBox Array("NAME:BoxParameters", "CoordinateSystemID:=", -1, "XPosition:=",_
"0mm", "YPosition:=",        "0mm", "ZPosition:=", "0mm", "XSize:=",        CStrmm(u_plate_x), "YSize:=",_
CStrmm(u_plate_y), "ZSize:=",        CStrmm(u_plate_thick)),        Array("NAME:Attributes", "Name:=", "u_plate", _
"Flags:=", "", "Color:=",       "(132 132 193)", "Transparency:=",        0, "PartCoordinateSystem:=", "Global", "MaterialName:=",_
"vacuum",        "SolveInside:=", true)Here used a self define function named CStrmm, which convert digital number to string with mm as the unit.Function CStrmm(nDig)
        CStrmm = CStr(nDig)+"mm"
End        FunctionThe rest of the geometry section is modified along these guidelines, using mathematical operations and defining temporary variables whenever needed. The rest of the geometry section can be observed in the final form of the script at the end of the application note. Since the intent is to sweep the gap between the U-plate and armature, a gap variable is also defined and used in constructing the parametrized geometry.
The structure of the gap loop is shown below. The format chosen here is a WHILE (condition)- loop body- WEND. The body of the loop is executed as long as the WHILE condition holds true:Dim        gap_loop_index,no_gaps
no_gaps = 2
gap_loop_index = 0
While gap_loop_index < no_gaps
        temp = gap_max - gap_min
        temp3 =        no_gaps        - 1
        temp1 =        temp/temp3
        temp2 =        temp1*gap_loop_index
        gap        = gap_min +        temp2
        temp = u_plate_thick
        temp = temp        + u_plate_high
        temp = temp        + gap

        oEditor.ChangeProperty Array("NAME:AllTabs", Array("NAME:Geometry3DCmdTab", Array("NAME:PropServers",_
                "armature:CreateBox:1"), Array("NAME:ChangedProps", Array("NAME:Position", "X:=", "0mm", "Y:=",_
                "0mm", "Z:=", CStrmm(temp)))))
        '………………………… (body of the loop)
        gap_loop_index = gap_loop_index+1
'loop back for gap
WendThe loop counter (gap_loop_index) is incremented at the end of the loop, and if the condition of the WHILE statement still holds true, the geometry is modified.


Assigning Variable Values from Files
Another way to assign values to variables is to read the data from a specified text file or access databse. About this you can reference the books of VBScript.


Material Assignment Section
The material assignment section remains unchanged since the objects in our model are always the same. The structure of the material section inside the macro is shown below. Note that the materials must exist in the database prior to launching the execution of the script.
This section takes the following format:oEditor.ChangeProperty Array("NAME:AllTabs", Array("NAME:Geometry3DAttributeTab", Array("NAME:PropServers", _
"armature", "u_plate", "core"), Array("NAME:ChangedProps", Array("NAME:Material",        "Material:=",_
"steel_1010"))))
oEditor.ChangeProperty Array("NAME:AllTabs", Array("NAME:Geometry3DAttributeTab", Array("NAME:PropServers", _
"coil1"),        Array("NAME:ChangedProps", Array("NAME:Material", "Material:=",        "copper"))))Boundary/Source Section
The structure of the boundary and source assignment section of the macro is presented below. Here you must add a new loop to sweep the current in the specified range. The structure of the current loop is the same as that of the gap loop. For the current source, the ampere-turns are calculated and assigned to terminals:loop_index = 0
loop_limit = no_current_steps
temp = current_max - current_min
temp3 =no_current_steps        -1
temp1 =        temp/temp3
temp2 =        temp1*loop_index
curr = current_min + temp2
curr = curr        * no_turns_coil1
oModule.AssignCurrent Array("NAME:curr1_in", "Current:=", CStrA(curr), "IsSolid:=",       _
true,        "Point out of terminal:=", false, "Objects:=", Array("term1"))
oModule.AssignCurrent Array("NAME:curr1_out", "Current:=", CStrA(curr),        "IsSolid:=",_
true,        "Point out of terminal:=", true, "Objects:=", Array("term2"))
'....................
loop_limit = no_current_steps
While        loop_index < loop_limit
        temp = current_max - current_min
        temp3 =no_current_steps        -1
        temp1 =        temp/temp3
        temp2 =        temp1*loop_index
        curr = current_min + temp2
        curr = curr        * no_turns_coil1
        '....................       
        oDesign.ChangeProperty Array("NAME:AllTabs", Array("NAME:MaxwellTab", Array("NAME:PropServers",_
                "BoundarySetup:curr1_in"), Array("NAME:ChangedProps", Array("NAME:Current", "Value:=",_
                CStrA(curr)))))
        oDesign.ChangeProperty Array("NAME:AllTabs", Array("NAME:MaxwellTab", Array("NAME:PropServers",_
                "BoundarySetup:curr1_out"), Array("NAME:ChangedProps", Array("NAME:Current", "Value:=",_
                CStrA(curr)))))
        '.........................
        loop_index = loop_index +1
WendForce and Matrix Setup Section
The following section contains the force and inductance matrix setup:Set        oModule        = oDesign.GetModule("MaxwellParameterSetup")
oModule.AssignForce        Array("NAME:group1", "Is Virtual:=", true, "Reference CS:=",_
"Global",        "Objects:=", Array("armature"))

oModule.AssignMatrix Array("NAME:Matrix1", Array("NAME:MatrixEntry", Array("NAME:MatrixEntry", "Source:=",        _
"curr1_in", "NumberOfTurns:=", "1")),        Array("NAME:MatrixGroup"), "Objects:=",        Array( _
"coil1"))Solver Setup Section
The following section contains the solver setup:Set        oModule        = oDesign.GetModule("AnalysisSetup")
oModule.InsertSetup        "Magnetostatic", Array("NAME:SolnOptnSetup", "MaximumPasses:=",        10, "MinimumPasses:=",_
2, "MinimumConvergedPasses:=", 1,        "PercentRefinement:=", 50, "LinearResidual:=",        _
1E-006, "ConvergenceDispParamId:=", -1, "SolveFieldOnly:=", false, "PercentError:=",        _
1, "SolveMatrixAtLast:=",        true, "UseOutputVariable:=", false,        "PreAdaptMesh:=",_
false, "NonLinearResidual:=",        0.01, "SolverType:=", 0, "MuNonLinearBH:=",        true, "ComputeHc:=",_
false, "HcNonLinearBH:=",        true)Executing the MacroOnce the macro is complete, you can execute it from the Maxwell 3D Field Simulator.
To run a script:
1 Click Tools>Run Script.
The Open dialog box appears.
2 Use the file browser to locate the folder in which you saved the script, and then double-click the folder’s name.
3 Type the name of the script in the File name text box, or click its name, and then click Open.
Maxwell executes the script.
To supply script arguments when running from Tools>Run Script, use the edit field at the bottom of the file selection dialog. You can access the script arguments using the AnsoftScriptHost.arguments collection from vbscript. This is a standard COM collection.
To run a script from a command line (as described in the Maxwell Online Help):
1 Type one of the following command line options at the command line:
-runscriptandexit or -runscript
2 Press Enter.

Solution Results
The following section contains getting the results and extracting the results:oDesign.AddOutputVariable "ForceVal", "Force_magnitude"‘Note: Here can’t work well
oDesign.AddOutputVariable "MatrixVal", "L(curr1_in,curr1_in)"

Dim        ForceVal,MatrixVal
'
'Create the file, open it for data entry
'
Set oFS = CreateObject("Scripting.FileSystemObject")
Set ofile1 = oFS.CreateTextFile ("force.txt")
line = chr(9) & "Force vs gap"
ofile1.WriteLine line
line = "GAP [ mm ]" & chr(9)& "FORCE [ Newtons ]"
ofile1.WriteLine line

Set ofile2 = oFS.CreateTextFile ("L11.txt")
line = chr(9) & "L_11 vs gap"
ofile2.WriteLine line
line = "GAP [ mm ]" & chr(9)& "INDUCTANCE [ Henries ]"
ofile2.WriteLine line

oDesign.Analyze        "SolnOptnSetup"

ForceVal = oDesign.GetOutputVariableValue("ForceVal", "SolnOptnSetup : LastAdaptive","", "")
MatrixVal =        oDesign.GetOutputVariableValue("MatrixVal",        "SolnOptnSetup : LastAdaptive","", "")

ForceVal = ForceVal*4
line = gap & chr(9) & ForceVal
ofile1.WriteLine line

MatrixVal = MatrixVal*4*no_turns_coil1*no_turns_coil1
line = gap & chr(9) & MatrixVal
ofile2.WriteLine line

[ 本帖最后由 Hottomson 于 2008-6-20 17:11 编辑 ]

Hottomson 发表于 2008-6-20 16:59

Complete Macro
The final script is presented below:Dim        oAnsoftApp
Dim        oDesktop
Dim        oProject
Dim        oDesign
Dim        oEditor
Dim        oModule

'Define
Dim        u_plate_length
Dim        u_plate_width
Dim        u_plate_thick
Dim        u_plate_high
Dim        armature_length       
Dim        armature_width
Dim        armature_thick
Dim        core_radius       
Dim        core_height       
Dim        coil_1_height
Dim        coil_1_inner_rad
Dim        coil_1_outer_rad
Dim        coil_1_position       
Dim        no_current_steps
Dim        no_turns_coil1
Dim        current_min
Dim        current_max
Dim        no_gaps       
Dim        gap_min
Dim        gap_max

u_plate_length = 100
u_plate_width =        60
u_plate_thick =        5
u_plate_high = 75
armature_length        = 120
armature_width = 80
armature_thick = 5
core_radius        = 10
core_height        = 70
coil_1_height =        5
coil_1_inner_rad = 15
coil_1_outer_rad = 25
coil_1_position        = 25
no_current_steps = 2
no_turns_coil1 = 1000
current_min        = 0.5
current_max        = 1
no_gaps        = 2
gap_min        = 0.01
gap_max        = 1

Set        oAnsoftApp = CreateObject("AnsoftMaxwell.MaxwellScriptInterface.11")
Set        oDesktop = oAnsoftApp.GetAppDesktop()

oDesktop.RestoreWindow
Set        oProject = oDesktop.NewProject
oProject.InsertDesign "Maxwell", "MaxwellDesign1", "Magnetostatic",        ""
Set        oDesign        = oProject.SetActiveDesign("MaxwellDesign1")
Set        oEditor        = oDesign.SetActiveEditor("3D Modeler")

Dim        temp,temp1,temp2,temp3,gap,gap_loop_index

gap_loop_index=0
temp = gap_max - gap_min
temp3 =        no_gaps        - 1
temp1 =        temp/temp3
temp2 =        temp1*gap_loop_index
gap        = gap_min +        temp2

Dim        u_plate_x,u_plate_y
u_plate_x=u_plate_width/2
u_plate_y=u_plate_length/2
oEditor.CreateBox Array("NAME:BoxParameters", "CoordinateSystemID:=", -1, "XPosition:=",_
"0mm", "YPosition:=",        "0mm", "ZPosition:=", "0mm", "XSize:=",        CStrmm(u_plate_x), "YSize:=",_
CStrmm(u_plate_y), "ZSize:=",        CStrmm(u_plate_thick)),        Array("NAME:Attributes", "Name:=", "u_plate", _
"Flags:=", "", "Color:=",       "(132 132 193)", "Transparency:=",        0, "PartCoordinateSystem:=", "Global", "MaterialName:=",_
"vacuum",        "SolveInside:=", true)
'
'Calculate dimensions for quarter model        U Plate
'
temp1 =        u_plate_x*(-1)
temp2 =        u_plate_thick*(-1)
oEditor.CreateBox Array("NAME:BoxParameters", "CoordinateSystemID:=", -1, "XPosition:=",_
CStrmm(u_plate_x), "YPosition:=",        CStrmm(u_plate_y), "ZPosition:=", CStrmm(u_plate_thick), "XSize:=",        CStrmm(temp1), "YSize:=",_
CStrmm(temp2), "ZSize:=",        CStrmm(u_plate_high)), Array("NAME:Attributes",        "Name:=", "u1",        _
"Flags:=", "", "Color:=",       "(132 132 193)", "Transparency:=",        0, "PartCoordinateSystem:=", "Global", "MaterialName:=",_
"vacuum",        "SolveInside:=", true)

oEditor.Unite Array("NAME:Selections", "Selections:=", "u_plate,u1"), Array("NAME:UniteParameters",        "CoordinateSystemID:=",       _
-1, "KeepOriginals:=", false)
'
'Calculate dimensions for armature
'
temp = u_plate_thick
temp = temp        + u_plate_high
temp = temp        + gap
arm_y =        armature_length/2
arm_x =        armature_width/2
oEditor.CreateBox Array("NAME:BoxParameters", "CoordinateSystemID:=", -1, "XPosition:=",_
"0mm", "YPosition:=",        "0mm", "ZPosition:=", CStrmm(temp),        "XSize:=", CStrmm(arm_x), "YSize:=",_
CStrmm(arm_y), "ZSize:=",        CStrmm(armature_thick)), Array("NAME:Attributes", "Name:=",        "armature",        "Flags:=",        _
"", "Color:=", "(132 132 193)", "Transparency:=",        0, "PartCoordinateSystem:=",_
"Global",        "MaterialName:=", "vacuum",        "SolveInside:=", true)
'
'Calculate dimensions for the core
'
temp = u_plate_thick
oEditor.CreateRectangle        Array("NAME:RectangleParameters", "CoordinateSystemID:=",_
-1, "IsCovered:=", true, "XStart:=", "0mm", "YStart:=", "0mm", "ZStart:=", CStrmm(temp), "Width:=",_
CStrmm(core_height), "Height:=", CStrmm(core_radius),        "WhichAxis:=", "Y"), Array("NAME:Attributes", "Name:=",       _
"core", "Flags:=", "", "Color:=",        "(132 132 193)", "Transparency:=", 0, "PartCoordinateSystem:=",       _
"Global",        "MaterialName:=", "vacuum",        "SolveInside:=", true)
oEditor.SweepAroundAxis        Array("NAME:Selections", "Selections:=", "core"), Array("NAME:AxisSweepParameters",        "CoordinateSystemID:=",       _
-1, "DraftAngle:=", "0deg", "DraftType:=", "Round", "CheckFaceFaceIntersection:=",_
false, "SweepAxis:=",        "Z", "SweepAngle:=", "90deg", "NumOfSegments:=", "8")
'
'Calculate dimensions for coil_1
'
temp = u_plate_thick
temp = temp        + coil_1_position
temp1 =        coil_1_outer_rad - coil_1_inner_rad

oEditor.CreateRectangle        Array("NAME:RectangleParameters", "CoordinateSystemID:=",_
-1, "IsCovered:=", true, "XStart:=", CStrmm(coil_1_inner_rad), "YStart:=", "0mm",        "ZStart:=",       _
CStrmm(temp),        "Width:=", CStrmm(temp1), "Height:=", CStrmm(coil_1_height), "WhichAxis:=",        "Y"), Array("NAME:Attributes", "Name:=",_
"coil1", "Flags:=", "", "Color:=", "(132 132 193)", "Transparency:=",        0, "PartCoordinateSystem:=",_
"Global",        "MaterialName:=", "vacuum",        "SolveInside:=", true)
oEditor.SweepAroundAxis        Array("NAME:Selections", "Selections:=", "coil1"), Array("NAME:AxisSweepParameters", "CoordinateSystemID:=",_
-1, "DraftAngle:=", "0deg", "DraftType:=", "Round", "CheckFaceFaceIntersection:=",_
false, "SweepAxis:=",        "Z", "SweepAngle:=", "90deg", "NumOfSegments:=", "8")

oEditor.Section        Array("NAME:Selections", "Selections:=", "coil1"), Array("NAME:SectionToParameters", "CoordinateSystemID:=",_
-1, "SectionPlane:=",        "ZX")

oEditor.ChangeProperty Array("NAME:AllTabs", Array("NAME:Geometry3DAttributeTab", Array("NAME:PropServers",       _
"Section1"), Array("NAME:ChangedProps", Array("NAME:Name", "Value:=",        "term1"))))
oEditor.Section        Array("NAME:Selections", "Selections:=", "coil1"), Array("NAME:SectionToParameters", "CoordinateSystemID:=",_
-1,        "SectionPlane:=", "YZ")
oEditor.ChangeProperty Array("NAME:AllTabs", Array("NAME:Geometry3DAttributeTab", Array("NAME:PropServers",       _
"Section1"), Array("NAME:ChangedProps",        Array("NAME:Name", "Value:=", "term2"))))
'
'the new region        defination
'
oEditor.CreateRegion Array("NAME:RegionParameters",        "CoordinateSystemID:=",        -1,        "+XPadding:=",        _
"300", "-XPadding:=",        "0", "+YPadding:=",        "300", "-YPadding:=", "0", "+ZPadding:=",_
"100", "-ZPadding:=",        "50"), Array("NAME:Attributes",        "Name:=", "Region",        "Flags:=",        _
"Wireframe#",        "Color:=", "(255 0 0)",        "Transparency:=", 0, "PartCoordinateSystem:=",        _
"Global",        "MaterialName:=", "vacuum",        "SolveInside:=", true)

oEditor.ChangeProperty Array("NAME:AllTabs", Array("NAME:Geometry3DAttributeTab", Array("NAME:PropServers",       _
"armature", "u_plate", "core"), Array("NAME:ChangedProps", Array("NAME:Material",        "Material:=",_
"steel_1010"))))
oEditor.ChangeProperty Array("NAME:AllTabs", Array("NAME:Geometry3DAttributeTab", Array("NAME:PropServers",       _
"coil1"),        Array("NAME:ChangedProps", Array("NAME:Material", "Material:=",        "copper"))))
Set        oModule        = oDesign.GetModule("BoundarySetup")
'
'Loop for current steps
'
loop_index = 0
loop_limit = no_current_steps
temp = current_max - current_min
temp3 =no_current_steps        -1
temp1 =        temp/temp3
temp2 =        temp1*loop_index
curr = current_min + temp2
curr = curr        * no_turns_coil1
oModule.AssignCurrent Array("NAME:curr1_in", "Current:=", CStrA(curr), "IsSolid:=",       _
true,        "Point out of terminal:=", false, "Objects:=", Array("term1"))
oModule.AssignCurrent Array("NAME:curr1_out", "Current:=", CStrA(curr),        "IsSolid:=",_
true,        "Point out of terminal:=", true, "Objects:=", Array("term2"))

Set        oModule        = oDesign.GetModule("MaxwellParameterSetup")
oModule.AssignForce        Array("NAME:group1", "Is Virtual:=", true, "Reference CS:=",_
"Global",        "Objects:=", Array("armature"))

oModule.AssignMatrix Array("NAME:Matrix1", Array("NAME:MatrixEntry", Array("NAME:MatrixEntry", "Source:=",        _
"curr1_in", "NumberOfTurns:=", "1")),        Array("NAME:MatrixGroup"), "Objects:=",        Array( _
"coil1"))

Set        oModule        = oDesign.GetModule("AnalysisSetup")
oModule.InsertSetup        "Magnetostatic", Array("NAME:SolnOptnSetup", "MaximumPasses:=",        10, "MinimumPasses:=",_
2, "MinimumConvergedPasses:=", 1,        "PercentRefinement:=", 50, "LinearResidual:=",        _
1E-006, "ConvergenceDispParamId:=", -1, "SolveFieldOnly:=", false, "PercentError:=",        _
1, "SolveMatrixAtLast:=",        true, "UseOutputVariable:=", false,        "PreAdaptMesh:=",_
false, "NonLinearResidual:=",        0.01, "SolverType:=", 0, "MuNonLinearBH:=",        true, "ComputeHc:=",_
false, "HcNonLinearBH:=",        true)

[ 本帖最后由 Hottomson 于 2008-6-20 17:12 编辑 ]

Hottomson 发表于 2008-6-20 17:00

continue...oDesign.AddOutputVariable "ForceVal", "Force_magnitude"
oDesign.AddOutputVariable "MatrixVal", "L(curr1_in,curr1_in)"

Dim        ForceVal,MatrixVal
'
'Create the file, open it for data entry
'
Set oFS = CreateObject("Scripting.FileSystemObject")
Set ofile1 = oFS.CreateTextFile ("force.txt")
line = chr(9) & "Force vs gap"
ofile1.WriteLine line
line = "GAP [ mm ]" & chr(9)& "FORCE [ Newtons ]"
ofile1.WriteLine line

Set ofile2 = oFS.CreateTextFile ("L11.txt")
line = chr(9) & "L_11 vs gap"
ofile2.WriteLine line
line = "GAP [ mm ]" & chr(9)& "INDUCTANCE [ Henries ]"
ofile2.WriteLine line

While gap_loop_index        < no_gaps

        temp = gap_max - gap_min
        temp3 =        no_gaps        - 1
        temp1 =        temp/temp3
        temp2 =        temp1*gap_loop_index
        gap        = gap_min +        temp2
        temp = u_plate_thick
        temp = temp        + u_plate_high
        temp = temp        + gap

        oEditor.ChangeProperty Array("NAME:AllTabs", Array("NAME:Geometry3DCmdTab", Array("NAME:PropServers",_
                "armature:CreateBox:1"), Array("NAME:ChangedProps", Array("NAME:Position", "X:=", "0mm", "Y:=",_
                "0mm", "Z:=", CStrmm(temp)))))

        loop_limit = no_current_steps
        While        loop_index < loop_limit
                temp = current_max - current_min
                temp3 =no_current_steps        -1
                temp1 =        temp/temp3
                temp2 =        temp1*loop_index
                curr = current_min + temp2
                curr = curr        * no_turns_coil1
               
                line = "Current: "& curr
                ofile1.WriteLine line
                ofile2.WriteLine line
               
                oDesign.ChangeProperty Array("NAME:AllTabs", Array("NAME:MaxwellTab", Array("NAME:PropServers",_
                        "BoundarySetup:curr1_in"), Array("NAME:ChangedProps", Array("NAME:Current", "Value:=",_
                        CStrA(curr)))))
                oDesign.ChangeProperty Array("NAME:AllTabs", Array("NAME:MaxwellTab", Array("NAME:PropServers",_
                        "BoundarySetup:curr1_out"), Array("NAME:ChangedProps", Array("NAME:Current", "Value:=",_
                        CStrA(curr)))))
               
                oDesign.Analyze        "SolnOptnSetup"
               
                ForceVal = oDesign.GetOutputVariableValue("ForceVal", "SolnOptnSetup : LastAdaptive","", "")
                MatrixVal =        oDesign.GetOutputVariableValue("MatrixVal",        "SolnOptnSetup : LastAdaptive","", "")
               
                ForceVal = ForceVal*4
                line = gap & chr(9) & ForceVal
                ofile1.WriteLine line
               
                MatrixVal = MatrixVal*4*no_turns_coil1*no_turns_coil1
                line = gap & chr(9) & MatrixVal
                ofile2.WriteLine line
                loop_index = loop_index +1
        Wend
        gap_loop_index = gap_loop_index+1
Wend

ofile1.close
ofile2.close


Function CStrmm(nDig)
        CStrmm = CStr(nDig)+"mm"
End        Function
Function CStrA(nDig)
        CStrA =        CStr(nDig)+"A"
End        Function这个问题不用宏,直接采用参数化分析就可以完成。该脚本仅做参考,不保证正常运行 。

[ 本帖最后由 Hottomson 于 2008-6-20 17:13 编辑 ]

Hottomson 发表于 2008-6-20 17:01

这个问题不用宏,直接采用参数化分析就可以完成。该脚本仅做参考,不保证正常运行 。

遗留问题:关于Force的输出存在问题,以下语句不对:
oDesign.AddOutputVariable "ForceVal", "Force_magnitude"
该语句采用宏录制的,但实际不能运行,还请高手帮忙解决Force的输出问题。

jean__l 发表于 2008-7-29 10:07

能用脚本做已经是高人了,而且还能慷慨的拿出来与大家交流,难能可贵啊!

aw5708 发表于 2009-1-3 16:40

高手就是高手啊,让人仰望啊!

qwerty123 发表于 2009-4-14 16:53

太难了,凡夫俗子根本看不懂

waillee83 发表于 2009-4-28 09:59

ed2k://|file|ansoft_maxwell_3d_v11+crack.rar|319962249|C7EA27C3CEA9E0DDABE4EFAFC2EAC61B|/

mandylion2008 发表于 2009-7-1 00:18

谢谢楼主提供!!

pandajuan 发表于 2009-7-1 15:18

{:1_490:}太强了

付云清 发表于 2009-12-17 19:28

我哭死了,就没有一篇用中文的?!俺都不是学英语的~~~~~~~~~~~~

H60880316 发表于 2009-12-17 20:42

这些都不是自己写的,只不过是录制的脚本而已,这也拿上来贴,笑死人了。
有什么意义?!
有时间看看我写VB脚本和MATLAB脚本吧!

jeromehu 发表于 2010-1-7 10:07

楼主就是人才
页: [1]
查看完整版本: Maxwell 3D v11 VBScript Example