I am gathering pieced of code for a macro in NX.CAE . I am looking for a way of checking if a Solution, Event and Element ID exist in a given simulation file. The solution is a Response Simulation. So far I have isolated the following
Dim strSolutionToUse as String Dim strEventName as String strSolutionToUse = "aa" strEventName= "SRSZ.803" Dim solution1 As CAE.ResponseSimulation.Solution = CType(workSimPart.Simulation.ResponseSimulationMan
ager.Solutions.FindObject(strSolutionToUse), CAE.ResponseSimulation.Solution) Dim rSEvent1 As CAE.ResponseSimulation.RSEvent = CType(solution1.FindObject(strEventName), CAE.ResponseSimulation.RSEvent) solution1.ActiveEvent = rSEvent1
Any help/ideas/suggestins, etc would be woelcome
In NXOpen, if you want to iterate on objects, look for ObjectCollection. In your example, you are looking for a specific Solution, so you should look at SolutionCollection, which is returned by the ResponseSimulationManager.Solutions method. I would not rely on FindObject to find the solution I need because you cannot be sure the solution name is the key to find the right solution. The string passed to FindObject must be the same as the one you would find when recording a journal and it is not guaranteed to be the solution's name. Here is a code snippet:
Dim theSession As Session = Session.GetSession() Dim simPart As NXOpen.CAE.SimPart = theSession.Parts.BaseWork Dim manager As NXOpen.CAE.ResponseSimulation.Manager = simPart.Simulation.ResponseSimulationManager Dim solutions As CAE.ResponseSimulation.Solution() = manager.Solutions.ToArray() Dim matchedSolution As CAE.ResponseSimulation.Solution For Each solution In solutions If solution.GetSolutionName = "The Solution you are looking for" Then matchedSolution = solution Exit For End If Next Dim matchedEvent As CAE.ResponseSimulation.RSEvent For Each rsEvent In matchedSolution.GetEvents() If rsEvent.GetEventName = "The event you are looking for" Then matchedEvent = rsEvent End If Next Dim elementIdToMatch As Integer = 1234 Dim femPart As NXOpen.CAE.BaseFemPart = simPart.FemPart Dim elementMap As NXOpen.CAE.FEElementLabelMap = femPart.BaseFEModel.FeelementLabelMap Dim element As NXOpen.CAE.FEElement = elementMap.GetElement(elementIdToMatch) If (Not element Is Nothing) Then Dim nodes As NXOpen.CAE.FENode() = element.GetNodes() End If
You should iterate on all solutions and look for a specific name. It would be the same idea for events. For elements, you should use the FEElementLabelMap to check for the existence of an element.
Thanks a lot.
Once the solution has been found I can activate it after the loop
If solution.GetSolutionName= strSolName Then matchedSolution = mySolution
theSimPart.Simulation.ActiveSolution = matchedSolution
Though i am not sur eif activating a solution is required to process the events in it. A bit like in an excel macro where one does not need to activate a sheet to write to it.
I am trying to tweak the (kindly) provided code to "fit" in my main macro but I am having trouble wi the: RespSimSolFound.GetEvents() statement. i get the erro msg about not deing defined. Below is the code
Option Strict Off Imports System Imports NXOpen Imports NXUI Imports NXOpen.UF Module TestRespSimResults Dim theSession As Session = Session.GetSession() Dim theLW = theSession.ListingWindow() Dim theUI As NXOpen.UI = NXOpen.UI.GetUI() Dim theNXMessageBox As NXMessageBox = theUI.NXMessageBox Sub Main() theLW.Open() Dim basePart As BasePart = theSession.Parts.BaseWork Dim theSimPart as CAE.SimPart Dim theRespSimManager As NXOpen.CAE.ResponseSimulation.Manager = theSimPart.Simulation.ResponseSimulationManager Try theSimPart = CType(basePart, CAE.SimPart) Catch ex as NXException 'part must not be a .sim part, warn user & exit journal msgbox("switch to a .sim part") return End Try Dim strRSSolName as String Dim strRSEventName as String strRSSolName = "RespSim1" strRSEventName = "NameOfEventToFind" Dim RespSimSolFound As CAE.ResponseSimulation.Solution For Each myRespSimCol As CAE.ResponseSimulation.Solution In theRespSimManager.Solutions If myRespSimCol.GetSolutionName = strRSSolName Then 'Set the Resp. Sim. to be used later to search for the event RespSimSolFound = myRespSimCol end if Next Dim matchedEvent As CAE.ResponseSimulation.RSEvent For each myRSEvent as RespSimSolFound.GetEvents() In theRespSimManager.Solutions theLW.WriteLine("Event Name is:" & myRSEvent.GetEventName) If myRSEvent.GetEventName = strRSEventName Then matchedEvent = rsEvent End If Next End Sub End Module
Your variable RespSimSolFound is probably not set because the name you are looking for probably do not match. Display all the solution names in your Listing Window. Or debug your dll to see what is happening.
Case sensitivity is an issue when comparing string values.
"Solution_1" <> "solution_1" <> "SoLuTiOn_1"
To perform a case insensitive string test, you could convert both strings to upper case (or lower case) before the test, or use the String.Compare() method with the proper value for the "case sensitive" argument.
"Solution_1".ToUpper = "solution_1".ToUpper = "SoLuTiOn_1".ToUpper
String.Compare("Solution_1", "solution_1", True) = 0
solution appears to be
Dim RespSimSolFound As CAE.ResponseSimulation.Solution Dim MatchedEvent As CAE.ResponseSimulation.RSEvent For each myRSEvent As CAE.ResponseSimulation.RSEvent In RespSimSolFound.GetEvents theLW.WriteLine(" Event Name is:" & myRSEvent.GetEventName) If myRSEvent.GetEventName = strRSEventName Then MatchedEvent = myRSEvent End If Next