plm world simulation connection 2012 connection may, 2012 nx open - simulation results access...
TRANSCRIPT
Simulation Connection May, 2012
NX Open - Simulation Results Access
Introduction NX Open is a collection of Application Programming Interface (API) toolkits that allow for the flexible integration
of ancillary software applications with NX through an open architecture. The NX Open APIs, by design, provide
an open architecture which can be utilized by third parties, customers, and in-house users for integrating and/or
creating custom software applications.
Commercial post processing programs provide a broad variety of tools for extracting, manipulating and
documenting the results of a finite element analysis. In spite of all of this, there are still occasions where the
individual analyst would like to extract data that is unique to his discipline, methodology or preferences, or to
automate a repetitive process across many design variations. The NX Open API can be used to provide access to
the data and automation to the process.
In this workshop, the user will create a Visual Basic program that will generate a list of all of the result quantities
in the Simulation. Following that, the program will be extended to report the displacement at a particular node
and the stress at a particular element across all subcases.
The workshop will illustrate the following concepts and processes:
Creating a Visual Studio project using the NX Open Wizard
Listing the contents of the Results and ResultAccess objects
Accessing Nodal results
Accessing Element Nodal results
Files provided:
1. Sample NX model
a. plate.prt
b. plate_f.fem
c. plate_s.sim
d. plate_s-sol101.f06
e. plate_s-sol101.op2
2. NxResultAccess_final.vb - Your code at the end of the workshop should look similar to this. Compare
your code to it if you run into trouble
3. NxResultAccess_ListOutput.txt – Your code at the end of the workshop should produce list window
output similar to this
Contents Introduction ................................................................................................................................................................1
Start ............................................................................................................................................................................3
Start NX 8 ...............................................................................................................................................................3
Start Microsoft Visual Studio 2010 ........................................................................................................................3
Notes ......................................................................................................................................................................3
Create a Visual Studio project ....................................................................................................................................4
Create a new project using the NX Open Wizard ..................................................................................................4
Application Settings ...........................................................................................................................................5
Entry Points ........................................................................................................................................................6
Examine the Project ...............................................................................................................................................6
Create code to list all available results .......................................................................................................................8
Check that the user is running in a SIM part ..........................................................................................................8
Loop through Solutions ..........................................................................................................................................8
Loop through Loadcases and Iterations .................................................................................................................9
Test the code written to this point ........................................................................................................................9
Loop through the ResultTypes in the Iteration ................................................................................................... 11
List the Components for this ResultType ............................................................................................................ 11
List any Sections if they exist .............................................................................................................................. 11
Run the final listing code ..................................................................................................................................... 12
Create code to query node/element result data .................................................................................................... 13
Get the ResultAccess object ................................................................................................................................ 13
Print Displacements for Node 65 ........................................................................................................................ 13
Print Stresses for Element 25 .............................................................................................................................. 14
Optional activities .................................................................................................................................................... 15
Results from other solvers....................................................................................................................................... 15
End of Workshop ..................................................................................................................................................... 16
Start
Start NX 8
Set Role to ‘Advanced with Full Menus
Open the plate_s.sim part. This is a simple part containing a solution with two subcases
Start Microsoft Visual Studio 2010 To ensure that the environment is configured correctly, Visual Studio should be started from a NX command
prompt. The Windows Start Menu or a desktop shortcut should not be used.
Open a NX Command Prompt
o Start | All Programs | Siemens NX 8.0 | NX Tools | Command Prompt
Start Microsoft Visual Studio 2010
o Type ‘devenv’ in the Command Prompt window
Notes
The NX Simulation prt/fem/sim files referenced in this workshop are located in the
P:\parts\NX_Open_Sim folder
A copy of final NX Open programs saved at the end of this workshop is also provided for reference
This folder is write protected. Please copy files to C:\parts\NX_Open_Sim prior to starting NX and
accessing them.
Create a Visual Studio project When you create a project in Visual Studio using the NX Wizard, it is correctly configured with references to the
NX Open dlls.
Create a new project using the NX Open Wizard Select File, New Project
On the New Project form:
o Select the NX8 Open VB Wizard
o Set the name to ‘caeResultAccess’
o Set the Location to an appropriate folder on your local machine
o ‘OK’
o ‘Next’
Application Settings
o Set type to ‘An internal application that can be activated from a NX session (DLL)’
You will actually be running the VB script directly in this workshop. It could be compiled
into a dll if desired.
o Toggle on the ‘Use NXOpen.UF API’ and ‘UseNXOpen.UI API’ options
This workshop does not require these however the few lines of code that these options
add are good to have in case they are needed.
o ‘Next’
Entry Points
o Set activation to ‘Explicitly (Main)
o Set unload option to ‘Automatically, when the application completes’
This is useful for applications that are going to be run only one time or for while you are
creating applications. When the application is unloaded after it runs, you can recompile
and overwrite the .dll without exiting and restarting the NX session
o ‘Finish’
Examine the Project The project will be created with a template Visual Basic file (Module1.vb). Double click on Module1.vb in the
Solution Explorer to open the file in the editing window.
Module1.vb contains:
1. Statements to import the basic Namespaces required
2. A Main() subroutine which is the entry point for explicit activation
a. Instantiation of objects for the Session, UI and UFSession
3. A GetUnloadOption function
The objects in the Main() subroutine and the GetUnloadOption are set based on the selections made on the
Application Settings and Entry Points dialogs in the Wizard.
Create code to list all available results Initially, we will create code to lop through all results in all solutions in the current SIM file. Hopefully, this
exercise will provide a better understanding of the results schema.
Check that the user is running in a SIM part Solutions and results are only available in a SIM part.
Add code to check that the application is being executed in a SIM part. If not, then open an error dialog
and terminate
o Insert the following above ‘End Sub’
o Don’t add the light grey text, which represents existing code. It is there to show context of
where the colored text is to be inserted
Loop through Solutions An array of solution objects is obtained from the simPart.Solutions object. A Result object must be created to
proceed further. This is analogous to loading the result on the Post Navigator when running interactively in NX
Add code to loop through all solutions and create a result object for each
o Insert the following above ‘End Sub’
' TODO: Add your application code here
' Define the listing window so we can write text information
Dim theLw As ListingWindow = theSession.ListingWindow
theLw.Open()
' Define the message box to report any errors
Dim theNxMessageBox As NXMessageBox = theUI.NXMessageBox
' Get the current work part
Dim basePart As BasePart = theSession.Parts.BaseWork
' Verify that it is a SIM part
Dim simPart As CAE.SimPart = TryCast(basePart, CAE.SimPart)
If simPart Is Nothing Then
theNxMessageBox.Show("NX Result Access", NXMessageBox.DialogType.Error, _
"The current work part is not a SIM part")
Return
End If
End Sub
End If
' Loop through all solutions in the SIM
For Each solution As CAE.SimSolution In simPart.Simulation.Solutions.ToArray
theLw.WriteLine("Solution: [" + solution.Name + _
"] Solver Type: [" + solution.SolverType + _
"] Solution Type: [" + solution.SolutionType + _
"] Analysis Type: [" + solution.AnalysisType + "]")
' Create a Result object for the current solution
Dim resultManager As CAE.ResultManager = theSession.ResultManager
Dim solResult As CAE.Result = resultManager.CreateSolutionResult(solution)
' Clean up the Result object
resultManager.DeleteResult(solResult)
Next solution
End Sub
Loop through Loadcases and Iterations Loadcases are obtained from the Result. Iterations are obtained from the loadcase.
Add code to loop through each loadcase and iteration in the result
o Insert the following above ‘' Clean up the Result object’
Test the code written to this point Since this is a single Visual Basic script, it does not have to be compiled into a .dll or .exe to run. On the Windows
platform, simple scripts written using the VB and C# .NET languages can be executed directly. NX will
automatically compile them at runtime.
Save the changes to Module1.vb in Visual Studio
Switch to the NX session with plate_s.sim set to the work part
Tools | Journal | Play
o Browse…
Navigate to Module1.vb on your system and select it
o Run
List window output should look similar to the following:
Feel free to repeat this process to test your journal at any point in this workshop
Dim solResult As CAE.Result = resultManager.CreateSolutionResult(solution)
' Loop through all loadcases in the result
For Each loadcase As CAE.BaseLoadcase In solResult.GetLoadcases
theLw.WriteLine(vbNewLine + " Loadcase: [" + loadcase.Name + "]")
' Loop through all iterations in the loadcase
For Each iteration As CAE.BaseIteration In loadcase.GetIterations
theLw.WriteLine(" Iteration: [" + iteration.Name + _
"] Type: [" + iteration.Type.ToString + "]")
Next iteration
Next loadcase
' Clean up the Result object
Visual Studio Tip
Right click on a class and select Go to Definition to open that class in the Object Browser.
The Object Browser provides detailed information on the class including the methods and properties available
Loop through the ResultTypes in the Iteration Result types are displacement, stress, etc.
Add code to loop through all ResultTypes in the iteration
o Insert the following above ‘Next iteration’
List the Components for this ResultType Components are the specific quantities available for each ResultType, for example, X, Y, Z displacement or Max
Prin, min Prin, Von Mises stress, etc.
Add code to loop through all Components in the ResultType
o Insert the following above ‘theLw.WriteLine(vbNewLine)’
List any Sections if they exist Sections are used if multiple data values exist for a particular result at a particular node or element. Examples
are shell top/bottom, beam stress recovery points, etc. Sections will not exist for many result types.
Add code to loop through all ResultTypes in the iteration
o Insert the following above ‘theLw.WriteLine(vbNewLine)’
"] Type: [" + iteration.Type.ToString + "]")
' Loop through each result type in the iteration
For Each baseResultType As CAE.BaseResultType In iteration.GetResultTypes
Dim resultType As CAE.ResultType = TryCast(baseResultType, CAE.ResultType)
theLw.WriteLine(" ResultType = [" + resultType.Name + "]")
theLw.WriteLine(" Datatype = [" + resultType.Datatype.ToString + "]")
theLw.WriteLine(" Location = [" + resultType.Location.ToString + "] ")
theLw.WriteLine(" Quatitiy = [" + resultType.Quantity.ToString + "] ")
theLw.WriteLine(" Complex = [" + resultType.Complex.ToString + "]")
theLw.WriteLine(" Title = [" + resultType.Title + "]")
theLw.WriteLine(" SubTitle = [" + resultType.Subtitle + "]")
theLw.WriteLine(" Username = [" + resultType.UserName + "]")
theLw.WriteLine(vbNewLine)
Next baseResultType
Next iteration
theLw.WriteLine(" Username = [" + resultType.UserName + "]")
' Loop through the components available for this result type
Dim components() As CAE.Result.Component = Nothing
resultType.AskComponents(components)
Dim compList As String = Nothing
For Each component As CAE.Result.Component In components
compList += " [" + component.ToString + "]"
Next component
theLw.WriteLine(" Components =" + compList)
theLw.WriteLine(vbNewLine)
Run the final listing code The code written to this point simply traverses through the Simulation Solution Results hierarchy and lists the
type of results data that is stored in the solver results file.
Save the changes to Module1.vb in Visual Studio
Switch to the NX session with plate_s.sim set to the work part
o Play the journal
List window output should look similar to the following:
theLw.WriteLine(" Components =" + compList)
' Loop through each section in the ResultType
' Note that sections do not exist for all result types
Dim sections() As CAE.Result.Section = resultType.GetSectionDefined
If sections.Length > 0 Then
Dim sectionList As String = Nothing
For Each section As CAE.Result.Section In resultType.GetSectionDefined
sectionList += " [" + section.ToString + "]"
Next section
theLw.WriteLine(" Sections =" + sectionList)
End If
theLw.WriteLine(vbNewLine)
Create code to query node/element result data The next step will be to actually query the nodal/elemental data values.
Get the ResultAccess object The ResultAccess object represents a specific result state (Loadcase, Iteration, ResultType, etc.) for a given
Result. It provides access to the actual node and element results data. A ResultType and Component must be
specified via a ResultParameter object to obtain a ResultAccess object
Create a ResultParameter object and set the current ResultType, a possible result component and a
coordinate system.
o Any valid component for the current ResultType will do for now.
Simply use the first one from the array of all components returned by
ResultType.AskComponents
This can be changed later when specific results are requested.
o Insert the following above ‘theLw.WriteLine(vbNewLine)’
Print Displacements for Node 65 This is an arbitrary example of getting access to displacement results for a particular node.
Add code to print displacements for node 65
o Test to make sure the current ResultType is a Displacement quantity and that the data location
is at the nodes.
o Nodes in the results (and the result values) are indexed from 1 to the number of nodes in the
model. Use Result.AskNodeIndex() to get the index for a particular node label
o Insert the following above ‘' Clean up the ResultAccess and ResultParameters objects’
End If
' Get the ResultAccess object
Dim resultParam As CAE.ResultParameters = resultManager.CreateResultParameters
resultParam.SetGenericResultType(resultType)
resultParam.SetResultComponent(components(0))
resultParam.SetCoordinateSystem(CAE.Result.CoordinateSystem.AbsoluteRectangular)
Dim resultAccess As CAE.ResultAccess = resultManager.CreateResultAccess(solResult, resultParam)
' Clean up the ResultAccess and ResultParameters objects
resultManager.DeleteResultAccess(resultAccess)
resultManager.DeleteResultParameters(resultParam)
theLw.WriteLine(vbNewLine)
Print Stresses for Element 25 This is an arbitrary example of getting access to stress results for a particular element. It is very similar to the
nodal displacement code added above. The only difference is a test for element nodal stress Result Type and
using the AskElementNodalResultAllComponents() function
Add code to print stresses for element 25
o Test to make sure the current ResultType is a Stress quantity and that the data location is at the
nodes on elements.
o Elements in the results (and the result values) are also indexed from 1 to the number of
elements in the model. Use Result.AskElementIndex() to get the index for a particular element
label
o The values are stored in a packed array.
The array can be addressed as:
index = node_index*numComponents + component_index
o Insert the following above ‘' Clean up the ResultAccess and ResultParameters objects’
Dim resultAccess As CAE.ResultAccess = resultManager.CreateResultAccess(solResult, resultParam)
' Print displacements at node 65
If resultType.Quantity = CAE.Result.Quantity.Displacement And _
resultType.Location = CAE.Result.Location.Nodal Then
' Get the index for the node label
Dim nodeIndex As Integer = solResult.AskNodeIndex(65)
' Get the result values for this node
Dim nodeValues() As Double = Nothing
resultAccess.AskNodalResultAllComponents(nodeIndex, nodeValues)
theLw.WriteLine(vbNewLine + " Node 65 Displacements:")
For i As Integer = 0 To nodeValues.Length - 1
theLw.WriteLine(" Component: [" + components(i).ToString + _
"] Value: [" + nodeValues(i).ToString + "]")
Next
End If
' Clean up the ResultAccess and ResultParameters objects
Optional activities 1. Open the .f06 file provided and compare the printed results values from NX Nastran to the values your
program wrote to the listing window.
2. Find the node with the maximum displacement magnitude
3. Find the element with the maximum Von Mises stress
4. Print results values in different units
Results from other solvers This example stepped through results in a NX Nastran Simulation that contained a NX Nastran Structural solution
with results in the provided .op2 file. The ResultManager.CreateSolutionResult() function creates a result object
from any solution type in the simulation. This could be a NX Thermal or Flow analysis, or results from third party
solvers such as Abaqus or Ansys. The API takes care of translating results from the solver specific results file
format into the standard Result object with Loadcases, Iterations, ResultTypes, etc.
Similarly, you can create a Result object for an external result file (one that is not related to a solution in the
current NX SIM file) using the ResultManager.CreateImportedResults() function. This will create a Result object
directly on the specified solver results file.
End If
' Print stresses at element 25
If resultType.Quantity = CAE.Result.Quantity.Stress And _
resultType.Location = CAE.Result.Location.ElementNodal Then
Dim elementIndex As Integer = solResult.AskElementIndex(25)
Dim elemNodeIdxs() As Integer = Nothing
Dim iNumComponents As Integer
Dim elementValues() As Double = Nothing
resultAccess.AskElementNodalResultAllComponents(elementIndex, elemNodeIdxs, _
iNumComponents, elementValues)
theLw.WriteLine(vbNewLine + " Element 25 Stresses:")
For i As Integer = 0 To elemNodeIdxs.Length - 1
Dim nodeLabel As Integer = solResult.AskNodeLabel(elemNodeIdxs(i))
theLw.WriteLine(" Node: [" + nodeLabel.ToString + "]")
For j As Integer = 0 To iNumComponents - 1
Dim valueIndex As Integer = i * iNumComponents + j
theLw.WriteLine(" Component: [" + components(j).ToString + _
"] Value: [" + elementValues(valueIndex).ToString + "]")
Next j
Next i
End If
' Clean up the ResultAccess and ResultParameters objects