Flowfront - Main page  
 


Contact Flowfront

How to advertise
Learn about our advertisers
Subscribe to Flowfront


www.plasticszone.com


www.moldflow.com

 

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

Click to zoom in

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:

  1. Set Synergy = CreateObject("synergy.Synergy")
  2. Set BoundaryConditions = Synergy.BoundaryConditions()
  3. Set Vector = Synergy.CreateVector()
  4. Set EntList = BoundaryConditions.CreateEntityList()
  5. EntList.SelectFromString "N5037 "
  6. Vector.SetXYZ 0, -1, 0
  7. 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:

  1. Set Synergy = CreateObject("synergy.Synergy")
  2. Set SD = Synergy.StudyDoc()
  3. If SD Is Nothing Then
  4. MsgBox "This script can be run only when a study is open", vbCritical, "No Study"
  5.   WScript.Quit
  6.   End If
  7. ‘ ----- Get list of entities
  8. Set EntList = SD.Selection
  9. ListSize = EntList.Size
  10. If ListSize = 0 Then
  11.   MsgBox "Select some nodes first before running this script", vbCritical, "No Nodes"
  12.   WScript.Quit
  13. End If
  14. ‘ ----- Obtain injection entrance orientation
  15. Dim Input, Vals, X, Y, Z
  16. X = 1.0
  17. Y = 0.0
  18. Z = 0.0
  19. Input = Inputbox("Enter injection entrance orientation (X,Y,Z):", "Orientation")
  20. Replace Input, ",", " "
  21. Vals = Split(Input, " ")
  22. If UBound(Vals,1) >= 0 Then X = Vals(0)
  23. If UBound(Vals,1) >= 1 Then Y = Vals(1)
  24. If UBound(Vals,1) >= 2 Then Z = Vals(2)
  25. ‘ ----- Set direction vector for the injection points
  26. Set Vector = Synergy.CreateVector()
  27. Vector.SetXYZ X, Y, Z
  28. ‘ ----- Create injection points at nodes & filter out all other entities
  29. Set BoundaryConditions = Synergy.BoundaryConditions()
  30. For I = 0 To ListSize-1
  31.   Set Ent = EntList.Entity(I)
  32.   Name = Ent.ConvertToString
  33.   If ( Left(Name, 1) = "N") Then BoundaryConditions.CreateNDBC Ent, Vector, 40000, Nothing
  34. 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

Click to zoom in

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:

  1. Set Synergy = CreateObject("synergy.Synergy")
  2. Set Viewer = Synergy.Viewer()
  3. Set Plot = Viewer.GetPlot(14)
  4. Plot.SetScaleOption 2
  5. Plot.SetMinValue 1
  6. Plot.SetMaxValue 5
  7. Plot.SetPlotMethod 1
  8. 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

  1. Set Synergy = CreateObject("synergy.Synergy")
  2. Set Viewer = Synergy.Viewer()
  3. Set Plot = Viewer.GetActivePlot()
  4. ‘ ----- Check whether plot is active
  5. If Plot Is Nothing Then
  6.   MsgBox "No plot displayed", vbCritical, "No Plot"
  7.   WScript.Quit
  8. End If
  9. Plot.SetScaleOption 2
  10. Set Args = WScript.Arguments
  11. ‘ ----- Set minimum and maximum value if specified
  12. If Args.Count > 0 Then
  13.   If IsNumeric(Args(0)) Then Plot.SetMinValue Args(0)
  14. End If
  15. If Args.Count > 1 Then
  16.   If IsNumeric(Args(1)) Then Plot.SetMaxValue Args(1)
  17. End If
  18. Plot.SetPlotMethod 1
  19. 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.