Current Issue Past Issues Moldflow.com Advertise Contact Us

» Print This Page   

tips & techniques

Automating the Creation of Results Files Using the Moldflow Plastics Insight API

By Matthew Jaworski, Training Services Manager

There is a saying that “knowledge itself is not fully realized unless it is shared.” The knowledge gained from performing CAE analysis is no exception to this statement. One of the best ways to communicate analysis results to others is to share the actual results themselves.

Moldflow Plastics Insight® (MPI®) and now Moldflow Plastics Advisers® (MPA®) users can export Moldflow Results files (*.mfr) for use with Moldflow Communicator. Moldflow Communicator is a free utility developed specifically for visualizing, quantifying and comparing Moldflow Results files; download it from www.moldflow.com/communicator.

Moldflow results files can contain a specified number of results from one or two studies in a project, thus allowing users and non-users alike to share analysis results and design knowledge within distributed product teams and across organizations. However, the manual process of marking results for export can be tedious, especially if more than one study is involved. This article shows you how to automate the creation of *.mfr files in MPI using the Application Programming Interface (API), which is an Object Linking and Embedding (OLE) programming interface.

Objective: Automate MPI *.mfr file creation for any study with Dual Domain Fill+Pack analysis results.

Step 1: Preparation

For our example lid part (Figure 1), we have already successfully completed a Dual Domain Fill+Pack analysis and customized any plot properties by right-clicking on a desired plot and selecting Properties. Figure 1 shows the results of changing the Fill time plot properties to display as contour lines.



Figure 1: Fill time plot with properties modified to display contour lines.

Step 2: Recording a Macro

You do not have to be a guru in computer programming to use the API functionality effectively. The easiest way to get started is to record the steps you want to automate using the macro recording tool in MPI’s user interface, Synergy. The procedure to record a macro for marking and exporting results to an MPI *.mfr file is as follows:

  1. Click start_macro.tif (Tools > Start Macro Recording).
  2. Right-click on a result name in the list of results and select Mark for export.
    An asterisk appears after the result name indicating it has now been marked for export.

    NOTE: Analysis logs are exported by default. To exclude all analysis logs from the Moldflow Results File, right-click the Logs checkbox, then select Unmark for export.
  3. Repeat to mark additional results for export in the same way.
    In this example, we have marked six results for export: Fill time, Pressure at V/P switchover, Volumetric shrinkage at ejection, Average velocity, Bulk temperature, and Temperature at flow front (see Figure 2).

    NOTE: MPI 6.2 and MPA 8.1 allow you to export a maximum of eight results per solver type from each study.
  4. Click export_icon.tif (File > Export).
  5. Navigate to the location in which you want to save the export file.
  6. In the File name text box, enter a name for the export file.
    In this example, our file name is fill.mfr.
  7. Select Moldflow Results File (*.mfr) in the Save as type drop-down list, and then click Save.
  8. A confirmation dialog appears to inform you the results were exported successfully. Click OK.
  9. Click stop_macro.tif (Tools > Stop Macro Recording).
    The Save Macro dialog appears.
  10. Navigate to a directory of your choice, enter a name for the macro in the File name box, and then click Save.
    The new script is saved with a *.vbs file extension, and it now can be used as a Visual Basic script. In this example, our script name is mfr_export.vbs, and it is saved in the directory C:\My MPI Projects\scripts.

    NOTE: The default script location depends on the operating system and installation parameters, but you can modify the location by changing your preferences within Synergy (File > Preferences > General (tab) > Project directory).


Figure 2: Results marked for export are indicated by an asterisk after the result name in the Study Tasks pane.

Table 1 shows the contents of the recorded macro. NOTE: The numbers listed before the script lines in Table 1 are there for reference only.

Table 1: Macro Recording:

  1. SetLocale("en-us")
  2. Set Synergy = CreateObject("synergy.Synergy")
  3. Synergy.SetUnits "Metric"
  4. Set PlotManager = Synergy.PlotManager()
  5. PlotManager.MarkResultForExport "Fill time", True
  6. Set PlotManager = Synergy.PlotManager()
  7. PlotManager.MarkResultForExport "Pressure at V/P switchover", True
  8. Set PlotManager = Synergy.PlotManager()
  9. PlotManager.MarkResultForExport " Volumetric shrinkage at ejection", True
  10. Set PlotManager = Synergy.PlotManager()
  11. PlotManager.Mark Result ForExport "Average Velocity", True
  12. Set PlotManager = Synergy.PlotManager()
  13. PlotManager.MarkResultForExport "Bulk Temperature", True
  14. Set PlotManager = Synergy.PlotManager()
  15. PlotManager.MarkResultForExport "Temperature at flow front", True
  16. Set Project = Synergy.Project()
  17. Project.Export "C:\Temp\mfr_Export\fill.mfr", True, True
  • Line 1 ensures the locale in which the script was recorded, in this case English – US, is also the locale in which the script is played back to avoid any locale-related character issues.
  • Line 2 creates an OLE automation object that starts the most recent version of Synergy.
    NOTE: Only one version of Synergy is able to run at any time when using the API.
  • Line 3 defines the default units. English can replace Metric in this line to use English measurement units by default.
  • Lines 4, 6, 8, 10, 12, and 14 initialize the PlotManager object using the Synergy PlotManager class. Only one statement is needed, but the recorded script treats each user interface action as a discrete action, thus resulting in the redundancy.
  • Lines 5, 7, 9, 11, 13, and 15 mark our selected results for export using the PlotManager class.
    NOTE: The MarkResultForExport function is sensitive to result name changes, so this script may not be compatible with future or previous MPI releases if the result names are different.
  • Line 16 initializes the Project object using the Synergy Project class.
  • Line 17 exports the *.mfr file to the path specified using the Project class. The True text is for the dialog box decisions associated with this export command.

Step 3: Using a Saved Macro

Now we can use the saved macro script to export the selected results automatically from any Dual Domain analysis that has Fill+Pack results, without having to mark each result for export manually. The procedure for playing a saved macro is as follows:

  1. Select play_macro.tif (Tools > Play Macro).
    The Open Macro dialog appears. Navigate to the location where the desired macro file is stored. In this example, our macro file was saved in the C:\My MPI Projects\scripts folder.

    NOTE: The default macro location is system and install dependent.
  2. Select a script, then select Open.
    In this example, we open the mfr_export.vbs file.
  3. The script plays.

NOTE: You must use the MPI command line if the script requires command line arguments, unless the script prompts for user input.

Step 4: Modifying the Macro Script

We could easily stop here, but what if we want to make modifications to further enhance the automation or usability of the script? Below are some examples of enhancements that could be added to improve the script, such as adding explanatory comments, checking for errors, and adding a pop-up window to allow user-specified file name changes during export.

Example Modified Script Segments to Automate MFR File Creation: Help, Comments and Explicit Option

  1. '@ DESCRIPTION
  2. '@ This command will automatically export specified MPI Dual Domain
  3. '@ Fill plots into an MFR file for one study.
  4. '@
  5. '@ Plots are Fill time, Pressure at V/P switchover, Volumetric shrinkage
  6. '@ at ejection, Average velocity, Bulk temperature, Temperature at flow
  7. '@ front
  8. ‘@
  9. '@ SYNTAX
  10. '@ mfr_export
  11. '@
  12. '@ DEPENDENCIES/LIMITATIONS
  13. '@ 1. User must open a study in MPI Synergy before using this command
  14. '@ 2. The study must have fill analysis results for a Dual Domain mesh
  15. ‘@ 3. Script valid for MPI 6.x result plot names
  16. '@
  17. '@ HISTORY
  18. '@ Created by MJJ; 14/Mar/08
  19. ‘@@
  20. Option Explicit
  21. Dim CustomText, FileName, Fpath1, Fullpath, FullPathMsg, FullPathTitle
  22. Dim fso, Plot, PlotManager, Project, StudyDoc, Synergy

    Results Availability Check
  23. Set Plot = PlotManager.FindPlotByName("Fill time")
  24. If Plot Is Nothing Then
  25. MsgBox "No Fill time plot in result set",, "ERROR"
  26. WScript.Quit
  27. End If

    Change Export File Name

  28. Set Project = Synergy.Project()
  29. CustomText = InputBox("Enter the file name for the exported MFR file without an extension:” & vbcrlf & “EX: fill.mfr", "Export MFR File Name")
  30. Set fso = CreateObject("Scripting.FileSystemObject")
  31. Fpath1 = fso.GetAbsolutePathName(".")
  32. Fullpath = Fpath1 & "\" & Customtext
  33. Project.Export Fullpath, True, True
  34. FullPathMsg = "The MFR file was successfully saved in:" & vbcrlf & Fullpath
  35. FullPathTitle = "MFR Export Successful!"
  36. MsgBox FullPathMsg,,FullPathTitle
  • Lines beginning with the single quote character (') are comment lines and are ignored when the script is executed. It is generally considered good practice to add explanatory comments at the beginning of scripts for future reference.
  • Lines 1-18 use the '@ prefix to signify help text for a command. Line 19 uses the '@@ prefix to signify the end of the help lines. If the script file (mfr_export.vbs) is placed in the MPI installation commands directory, typing "help mfr_export" in the Synergy command line will extract the help header information and display it as command help, as shown in Figure 3.
  • Line 20 forces explicit declaration of all variables in that script (e.g. Dim in lines 20-21), which helps reduce programming errors. For instance, if you attempt to use an undeclared object name, an error occurs.
  • Lines 21-22 declare object variables and allocate storage space
  • Line 23 initializes the Plot object with the Fill time result. Any available result plot name can replace the Fill time text to change the result retrieved. This is useful to know if you want a different set of results exported than those used in this example, or if the result name changes in a future MPI release.
  • Line 24-27 is an If statement that checks if a Fill time result exists in the study. If it does not, Nothing is returned and a message box titled ERROR pops up and displays the message text shown in line 25.
  • Line 26 quits the script.
  • Line 28 initializes the Project object with the most recent Synergy project.
  • Line 29 populates the CustomText object with user input generated from a pop-up input box with guided instructions.
    Alternatively we could have used this line to obtain the user specified full path location and *.mfr file name for saving (e.g. C:\Temp\MFR_Export\fill.mfr).
  • Line 30 initializes the fso file system object.
  • Line 31 uses the fso object to obtain the current MPI project directory the active study is in.
  • Line 32 populates the Fullpath object with an assembly of the project directory and user entered export file name (e.g. CustomText object contents).
  • Line 33 exports the selected results to the user defined mfr file in the current project directory.
  • Line 34 populates the FullPathMsg object with a message indicating where the file was stored.
    Vbcrlf is a Visual Basic Carriage Return Line Feed which simply moves to the next line.
  • Line 35 populates the FullPathTitle object with text.
  • Line 36 uses the MsgBox function to display a message box with the text in line 34 and the title text from line 35.


Figure 3: Command line help example

Step 5: Test-drive a Script

A fully functional version of the modified script described in this article is available to registered MPI customers on the MPI Community Center web site in the Training Material > Macro Exchange - Scripts area; select Export MFR fill results to a file. Download this script onto your local computer and use Step 3 to try it out for yourself.

NOTE: Moldflow Results files exported from MPI 6.2 must be viewed with Moldflow Communicator 2.0.

Extensive documentation and more examples of using the API can be found in the MPI Help (Help > Search Help > Contents (tab) > Application Programming Interface (API) or Help > Tutorials > Automating MPI).


Learn more Tips & Techniques | Visit the MPI Community Center or MPA Community Center today. Registered customers will find Hints and Tips articles, user discussion forums, known bugs and limitations, and a host of other valuable information.



Moldflow Corporation Copyright © 2008 - All Rights Reserved - Sponsored by Moldflow Corporation

Terms of Use | Privacy Policy