|
|
tips
& techniques
Working with the Moldflow Plastics Insight API Tools
By Murali Anna-Reddy, Moldflow Corporation
The Application Programming Interface (API) was introduced in Moldflow
Plastics Insight® (MPI®) 4.0 to allow users and strategic partners
to extend, automate and customize a wide range of MPI functionality. API
tools expand the scope of MPI software by enabling users to automate common
tasks, implement standardized corporate protocols and best practices,
interface with third-party software applications and customize the user
interface. The API mechanism is based on Microsoft’s OLE Automation and
allows integration with any OLE-enabled client, also known as an automation
client. Automation clients include VBScript (VBS) programs, Visual Basic
for Applications (VBA) and Visual Basic applications, ActiveX scripts
in Internet Explorer, and Perl/Python scripts. The focus of this article
is to provide an introduction to the API and illustrate how to use it
effectively through a few examples.
The API tools include:
- MPI OLE Automation server: Access MPI capability using any
automation client.
- Macro recording and playback: Record and play back user-interface
(UI) actions in VBS scripts. Use recorded macros directly, or modify
them to enhance and extend their capability.
- Command line: Execute common tasks from a command line. A few
built-in commands are provided for use and to serve as examples to aid
in building custom commands. Build custom commands using macro recording
with subsequent editing or build them from scratch.
- ASCII input and output files: Import and export model data
in ASCII format. Export results in Patran and XML formats.
Example 1
Objective: Automate creation of multiple injection points on a
3D model. The image on the right shows a typical 3D model where plastic
flows in through the entire top surface. To simulate this behavior, you
will need to create an injection point at each node on the top surface.
Using the injection entrance tool, you would have to click once at each
node in order to assign an entrance to it. This script will allow you
to select all relevant nodes using the rubberband selection tool or other
means and assign entrances to all selected nodes at once.
Procedure: First, start macro recording (Tools > Start
Macro Recording), then create an injection point. Next, stop macro
recording (Tools > Stop Macro Recording), and save the
macro to a file. The macro is stored as a VBS script with a .vbs
extension. This recorded macro will serve as a starting point for the
script that we want to create to achieve our objective and also provide
insight into the various automation objects in MPI and their capability.
The recorded macro is shown below along with a detailed explanation.
| Macro Recording:
- Set Synergy = CreateObject("synergy.Synergy")
- Set BoundaryConditions = Synergy.BoundaryConditions()
- Set Vector = Synergy.CreateVector()
- Set EntList = BoundaryConditions.CreateEntityList()
- EntList.SelectFromString "N5037 "
- Vector.SetXYZ 0, -1, 0
- Set EntList_1 = BoundaryConditions.CreateNDBC(EntList,
Vector, 40000, Nothing)
|
- Line 1 creates an application object. This will basically be
the preamble to every script you create since the Synergy
application object is the top-level object using which you can
access and/or create all other automation objects in MPI.
- Line 2 creates a BoundaryConditions object. This object
is used to create boundary conditions such as injection, coolant
and gas entrances, vent locations, etc.
- Line 3 creates a Vector object. This will be used subsequently
to set the orientation direction for injection entrances.
- Line 4 creates an empty entity list (EntList) object,
which will be used to define the nodes on which the injection
entrances will be created. Entities include essentially all useful
geometry and mesh components of your model including nodes, elements,
curves, regions, surfaces, coordinate systems and boundary conditions.
Most API functions that operate on the model take entity lists
as input or provide them as output.
- In line 5, the node is assigned to the entity list object created
in line 4. Entity names or labels in MPI are of the form Entity
Prefix Entity ID, where Entity Prefix is N
for nodes, T for triangles, TE
for tetrahedra, etc., and Entity ID is the numerical
identifier (or label) of the entity in question. The function
SelectFromString populates an entity list from a list
of entity names. When this script is later modified, instead of
selecting individual injection nodes, an entity list is simply
obtained from the current selection in the model.
- In line 6, MPI assigns the orientation direction of the injection
entrance to the vector created in line 3. This orientation is
automatically calculated by MPI from the faces of the tetrahedra
that share the node. In the modified version of this script shown
below, the orientation direction is obtained from the user.
- In line 7, the injection entrance is created. MPI records the
newly created injection entrance into an entity list EntList_1.
The parameters to the function CreateNDBC comprise the
entity list that specifies the nodes on which entrances are to
be created, the orientation, the property ID of the boundary condition
(40000 for injection entrances) and the property
to be assigned to the newly created boundary condition. In this
case, Nothing is specified as the property since
MPI uses the default property for injection entrances.
Modified Script to Automate Multiple Injection Point Creation:
- Set Synergy = CreateObject("synergy.Synergy")
- Set SD = Synergy.StudyDoc()
- If SD Is Nothing Then
- MsgBox "This script can be run only when a study is
open", vbCritical, "No Study"
- WScript.Quit
- End If
- ‘ ----- Get list of entities
- Set EntList = SD.Selection
- ListSize = EntList.Size
- If ListSize = 0 Then
- MsgBox "Select some nodes first before
running this script", vbCritical, "No Nodes"
- WScript.Quit
- End If
- ‘ ----- Obtain injection entrance orientation
- Dim Input, Vals, X, Y, Z
- X = 1.0
- Y = 0.0
- Z = 0.0
- Input = Inputbox("Enter injection entrance orientation
(X,Y,Z):", "Orientation")
- Replace Input, ",", " "
- Vals = Split(Input, " ")
- If UBound(Vals,1) >= 0 Then X = Vals(0)
- If UBound(Vals,1) >= 1 Then Y = Vals(1)
- If UBound(Vals,1) >= 2 Then Z = Vals(2)
- ‘ ----- Set direction vector for the injection points
- Set Vector = Synergy.CreateVector()
- Vector.SetXYZ X, Y, Z
- ‘ ----- Create injection points at nodes & filter
out all other entities
- Set BoundaryConditions = Synergy.BoundaryConditions()
- For I = 0 To ListSize-1
- Set Ent = EntList.Entity(I)
- Name = Ent.ConvertToString
- If ( Left(Name, 1) = "N") Then BoundaryConditions.CreateNDBC
Ent, Vector, 40000, Nothing
- Next
|
- Lines beginning with the single quote character (such as lines
7, 14, 25, et al.) are comment lines. It is generally considered
good practice to annotate code with meaningful comments for future
reference.
- Line 4 obtains the currently active study document object. If
a study is not open in the user interface (UI), Nothing
will be returned by the StudyDoc function. The script
checks for this and exits if necessary in lines 3 through 6.
- Lines 8 and 9 obtain the current selection from the document
and assign it to an entity list.
- Line 9 gets the number of entities in the list. Lines 10 through
13 and ensure that at least one entity is selected before continuing
with the rest of the script.
- Lines 19 through 24 prompt the user for the orientation direction
for the boundary conditions and then parse the input provided
by the user. Standard VBS functions such as Replace,
Split and UBound have been utilized in order
to accomplish this task. You can use the information provided
at this URL in order to assist you in your VBS programming tasks:
http://msdn.microsoft.com/library/defaut.asp?url=/library/en-us/script56/html/
vbscripttoc.asp
- Lines 29 through 34 loop through the entity list and assign
an injection entrance if the selection is a node. Line 31 obtains
the I-th entity in the list and line 32 obtains the entity label.
Line 33 checks whether the label starts with N
in order to determine if the entity is a node; if it is, an injection
entrance is created on it.
|
You can find extensive documentation of the API within MPI at Contents >
Application Programming Interface > Using the API > API
Command Reference.
Example 2
Objective: Develop a command that will change a plot that is being
displayed to line contours and set the minimum and maximum values for
the plotted range.
Procedure: Assume that we have run a fill analysis on a study.
First, turn the Fill time plot to display the results in the default
manner, which is typically a smoothly shaded plot with minimum and maximum
values ranging from 0 to the fill time. Now, start macro recording. Then,
display the properties of the plot using the command Results >
Plot Properties. Select the Methods tab and choose the Contour
option in the Selection group box. Then, switch to the Scaling
tab; now, choose the Specified option and set the maximum and minimum
values for the range to 1 and 5 seconds, respectively. Finally, stop recording
and save the script to the user commands folder under the name dlc.vbs.
A typical location for this folder is C:\My MPI 4.0 Projects\Commands.
This script can be played back from the MPI command line (View >
Command Line) by typing in dcl after we are through
with the modifications necessary to convert it into a command.
| Macro Recording:
- Set Synergy = CreateObject("synergy.Synergy")
- Set Viewer = Synergy.Viewer()
- Set Plot = Viewer.GetPlot(14)
- Plot.SetScaleOption 2
- Plot.SetMinValue 1
- Plot.SetMaxValue 5
- Plot.SetPlotMethod 1
- Plot.Regenerate
|
- Line 3 obtains the Plot object from MPI/Synergy. A
numeric identifier identifies plots since plot names are not unique.
However, we will modify this line in order to get the active plot
in the viewer rather than addressing a plot by its numeric identifier
in order to make this script universally applicable.
- Line 4 sets the scaling option to Specified.
- Lines 5 and 6 set the minimum and maximum values for the plotted
range.
- Line 7 sets the plot method to Contour in order to
display line contours.
- Line 8 redraws the plot.
Modified Command Script
- Set Synergy = CreateObject("synergy.Synergy")
- Set Viewer = Synergy.Viewer()
- Set Plot = Viewer.GetActivePlot()
- ‘ ----- Check whether plot is active
- If Plot Is Nothing Then
- MsgBox "No plot displayed", vbCritical,
"No Plot"
- WScript.Quit
- End If
- Plot.SetScaleOption 2
- Set Args = WScript.Arguments
- ‘ ----- Set minimum and maximum value if specified
- If Args.Count > 0 Then
- If IsNumeric(Args(0)) Then Plot.SetMinValue
Args(0)
- End If
- If Args.Count > 1 Then
- If IsNumeric(Args(1)) Then Plot.SetMaxValue
Args(1)
- End If
- Plot.SetPlotMethod 1
- Plot.Regenerate
|
- Line 3 obtains the active plot in the viewer. Lines 5 through
8 check the returned value to ensure that a plot is currently
displayed.
- Line 9 sets the plot scaling to Specified.
- Lines 12 through 17 set the minimum and maximum values for the
plotted range. These are specified as command line arguments when
the script is invoked. You can invoke this script by typing dlc
to just change the plotting method to line contours or by typing,
for example, dlc 0 2 to display line contours
for values between 0 and 2 seconds.
- Line 18 sets the plot method to line contours and line 19 redisplays
the plot.
|
You can use this command script to change a displayed plot to line contours
quickly and optionally specify the minimum and maximum values for the
plotted range. You can also create command scripts to quickly set other
plot properties in a similar manner.
Want to learn more Tips and
Techniques?
Visit the Moldflow
Community Center. MPI users will find a Macro Exchange as well as
FAQs, Hints and Tips, and a host of valuable information.
|