![]() |
|
tips & techniques Working with the Moldflow Plastics Insight API Tools By David Rieffel, Product Engineer, Siegel-Robert, Inc., St. Louis, MO Motivation for using the API I am going to recount my motivation for using the MPI® API tools, and hopefully, it will become yours as well. Ask yourself these questions:
If you answered "Yes" to any of these questions, then the Moldflow Plastics Insight® (MPI) API (Application Programming Interface) most likely is the solution to your problems.
Simple beginnings I used the Record Macro command to generate this code (Figure 1). I band-selected around some nodes and elements and copied them by 0,0,1. Then I band-selected around some other nodes and elements and moved them by 0,0,1. Easy enough, right?
Upon opening the saved VBS macro file, look at Line 1 and say, "What does this line do, and what is Set?" From a FORTRAN background I was, and still am, used to working with numerical, logical, and string data types. Generally, FORTRAN is not used to interface with other programs. I use FORTRAN just for basic processing of data files. So now we need to talk about object oriented programming (OOP) and what is an object. (NOTE: This is my understanding of objects, not the textbook definitions.) Objects are entities that MPI/Synergy exposes through its OLE/Automation interface. The Set statement links the VBS variable to the MPI/Synergy program objects, and we use those VBS variables to control MPI/Synergy. In this example:
Make it more flexible While using the Record Macro command is a good first step, the resulting script is not very flexible. Let's modify it to allow any vector to be specified for copying selected entities (Figure 2).
First, delete Lines 8-13 and comment out Lines 5-7 of the original script (comment lines begin with a single quote mark and are not executed when the script is run). Next, add an input box so a string of numbers can be entered to represent the vector for entities to be copied along (Line 7 in the modified script). As a test, I added a message box (Line 8 in this example) to output the vector entered in the input box. When writing code, I find it best to write a small segment of code and then output some of the results to a message box to verify that the script is behaving correctly. This message box isn't necessary to run the final script, so I commented it out. To continue, split the string into an array (Line 11) and then set the Vector variable to the array components (Line 15). Line 17 is a message box that prompts the user to select the entities to be copied. Line 19 links the variable SD to the StudyDoc object of the MPI/Synergy program (StudyDoc is the object used to query and manipulate studies). Finally, Line 21 translates and copies the entities that have been selected inside MPI/Synergy. I made two programming mistakes in my first attempt at writing this code:
In both cases, the error message identifies the line and the character where the error originates. From my perspective, this example is not too difficult. Remembering the first time I tried to write a script, that experience was rather excruciating and painful. I didn't, and to some extent still don't, understand object-oriented programming. It took me a full two days to get my first try at a script to run. The second, third, and fourth scripts didn't fare much better. So you say, "I'm not going to spend days writing scripts, what a waste of time!" or "Learning to write VBS is too difficult." It is not a waste of time. Rather, it's an investment. Portions of the simple coding get copied and modified slightly to create a new, more powerful code. Then, you can begin to tackle more difficult things, such as querying entity properties, putting scripts into Excel, and the like. The more experience you gain, the quicker you will be able to create more powerful scripts. Looking at a bit of code For a more in-depth example, let's look at the main code of the mav.vbs script found on the Macro Exchange area of the MCC (Figure 3). The mav.vbs script moves or copies entities between a set of nodes. This script requires the user to pre-select entities before running the script.
Line 37 uses the Set command to link the Syn variable to the MPI/Synergy program. In the previous examples, extra variables and Set statements were created to access objects in the MPI/Synergy program. In this instance, however, I didn't want to create all these extra variables. Instead, the object and its method invocation are piggybacked onto the Syn object. Study.Doc.Selection (Line 39) returns a list of entities selected inside MPI/Synergy as an EntList object. An EntList object corresponds to an ordered list of model entities. It also has a public attribute, Size, which tells how many things are selected. So in short, this line tells the script how many things are selected inside MPI/Synergy. Line 40 displays a message box if no entities have been selected. In Line 41, setting the value of Syn to "Nothing" terminates the life of the Syn variable. It is important to terminate the life of a variable when it is no longer useful. (Consult the archived Lunchtime Luminaries session for more on this topic.) In Line 42, Wscript.quit terminates the script. Line 47 allows the user to specify whether the objects are to be moved or copied. Line 52 prompts the user to input the number of copies, if necessary. Line 56 moves the entities that were pre-selected to the MoveList variable. Line 57 empties the selection. This is because the Study.Doc.Selection variable is needed to select the nodes to move along. Lines 60-67 prompt the user to select nodes. The prompt displayed to the user depends on whether the script was previously instructed to move or to copy the pre-selected entities (the MoveList). In Line 69, FUNGetSelection is a function that takes a selection and separates out the nodes. If the user band-selects around a node, the selection can include associated triangles, curves, etc. This function weeds out the unwanted selected entities (e.g., triangles).
Line 71 checks to see that at least two nodes have been selected. Line 78 deactivates local coordinate systems. This script won't move things properly if any are active. Let's continue our examination of the mav.vbs code (Figure 4).
Line 81 prompts the user to enter the distance or a percentage of the distance between the selected nodes, along the vector to move/copy the entities. Lines 83-87 determine the last node in the list. Because arrays are indexed from 0, the script takes the size and subtracts 1. If the script is moving entities and the user accidentally selected more than two nodes to define the vector, the point to move to is set to the second node found in the selection list. In Line 90, SUBGetCoord is a subroutine that takes the node coordinates and places them in the vector VecA. Nodelist.Entity(0) is the base coordinate for moving/copying the selected entities. Lines 91-104 loop on the second to the nth node. Line 92 gets the coordinates of the destination node and puts them in vector VecB. (See Line 90.) In Line 93, SUBSubtract is a subroutine that subtracts two vectors. In Lines 94-99, the If loop determines if you wanted to use a percentage or an actual distance to specify the actual movement dimension. In Line 95, SUBScale is a subroutine that scales input (VecB) to the decimal percentage that was input on Line 81 and outputs it into the vector Offset. In Line 97, SUBNormalize is a subroutine that normalizes the displacement vector. The SUBScale subroutine in Line 98 works the same as in Line 95. Lines 101-102 put the Offset values into another vector. Finally, In Line 103, the script moves/copies the selection. Lines 106-109 clear the selection and end the lives of the variables. Of course, it is possible that my code could be simplified. However, with most of my scripts, once it works, I don't like to mess with it too much. As the old saying goes, "If it ain't broke, don't fix it." The API in use Now, let's look at an example of using the API to create a manifold system on a part. This example uses three scripts that are available on the Macro Exchange area of the MCC. Using the scripts helps to minimize repetitive tasks involved in accomplishing this modeling task manually. First, we start with a midplane model of a simple part (Figure 5) on which we would like to place five drops. A model of a generic drop is available in another MPI study (Figure 6). Note that it's good modeling practice to create different types of entities on different layers (for example, one layer to hold part nodes, another layer to hold part triangle elements, another layer to hold beam elements, and so on), to make it easier to view and operate on desired entities.
Node 637 in the lower left corner of Figure 5 is at 0,0,0 and the rest of the nodes shown are locations where the additional drops are required. Add the generic drop to the injection part study. Now make sure the Manifold layer is turned off, select all the beams, and turn off the Drop layers (Figure 7). Use the mav.vbs script to copy the generic drop. Type mav at the command prompt to run the script. Use the copy option, and specify 1 copy. Using the Ctrl key, first select N637 and then band-select around the five nodes on the part. Select OK on the message box, and accept the 100% default for the distance. Upon turning the Drop layer on, the display will show that the generic drop has been copied to the five part nodes that were previously selected (Figure 8). Entities that are selected, whether their layer is shown or not, will be copied. The copied entities will be created on the layers on which the parent entity resides. Use the scn_v2.vbs script to create nodes where the drops intersect the manifold plane. To complete the manifold system, turn off all the layers except the Manifold Nodes and the Drop Nodes. Select the two manifold nodes, N639 and N641; these two nodes are used to represent a vector in space. Using the Ctrl key, band-select around the nodes for the outer drops in sets to create additional 3D vectors: N676 and N677, N684 and N685, N660 and N661, N652 and N653, in this order (Figure 9). Type scn_v2 at the command prompt to create the nodes where the drops will intersect the manifold and where the manifold forms a T-intersection. Use the Global Merge command to merge coincident nodes.
Use the beam.vbs script to add beam elements between nodes. Select the manifold beam to be duplicated. Type beam at the command prompt, then select the nodes between which to create the manifold beam. Run the script again to create the annular beams between the drops and the manifold. See Figure 10. Scripts for just about anything Scripts can be created for most of the commonly used tasks in MPI/Synergy. I have scripts for just about all of the meshing tools. I can project nodes to a plane and match nodes on a Fusion mesh. I have scripts to adjust the aspect ratios of elements, merge individually selected node sets, and control the look of the post-processing. For the most part, the only limits you have are your programming capability and your determination to create the scripts. A word about selections With the API, a script can query entities selected inside MPI/Synergy. The response is in the form of EntList, as described above. It is important to realize how the order and type of selection used affects the final pattern and content of EntList. The pattern and order of data passed from the MPI/Synergy program to the recorded VBS macro script can affect the results based on how the script is coded. Figure 11 shows the different results that occur by band-selecting around Node 7, Node 13, and then Nodes 7 and 13. The first thing to note is that any time a banded selection is made, the entity (or query) list is created, or appended, in a Node, Beam, Triangle, Curve, or Nodal Boundary Condition order. The second thing to note is that using the Ctrl key to append to the section adds entities in the same order, and items can also occur multiple times in a selection list, as shown with T9 and T12 in Figure 11. (This is why using Ctrl+M to rotate or translate objects can move an entity 2-3 times farther than intended because it exists in the selection list more than once!) Now, taking the result from above and using the Shift key to deselect T5 will result in the EntList being reordered and duplicate entities removed, as shown in Figure 12. Depending on how the script is coded, this can have an undesired effect on the result.
Using the Ctrl key to deselect preserves the pattern of the EntList and removes only the deselected entity throughout the selection (Figure 13).
The end of the beginning Now you have a general idea about the types of problems that the API can help you solve. You have a list of a few good references, and you have reviewed some examples of a recorded macro, how to modify a recorded macro to make it more flexible, and how to put scripts to work in a modeling task. You were given a brief overview of pattern and order in which entities are selected inside MPI/Synergy — recognizing the pattern of selection could help you to sort data for looping scripts. Don't forget that the MCC is a great resource. Get out there and start using the API! Download someone else's script and modify it or post your own script on the MCC. If you have problems, ask for help on the Talk to MPI Users section of the MCC and post your code there. What's in the future? I am thinking about writing a script to create the clamp tonnage plot for a part oriented in car position. I may also create a script for growing valve gate drops. I'm currently working on a script for creating a more in-depth recommended ram speed output.
|
|||||||||||||||||
| Copyright © Moldflow Corporation 2004. All rights reserved. | ||||||||||||||||||