cancel
Showing results for 
Search instead for 
Did you mean: 

Delete Companion Results with NX Open Python

Valued Contributor
Valued Contributor

Is it possible to delete companion results within a Python journal?
I tried recording a delete which generated the following code:

 

# NX 10.0.0.24
# Journal created by u566264 on Mon Nov 02 16:14:36 2015 GMT Standard Time
#
import math
import NXOpen
import NXOpen.CAE
def main() :

    theSession  = NXOpen.Session.GetSession()
    workSimPart = theSession.Parts.BaseWork
    displaySimPart = theSession.Parts.BaseDisplay
    markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Delete Companion Result")
   
    simSimulation1 = workSimPart.FindObject("Simulation")
    simSolution1 = simSimulation1.FindObject("Solution[Thermal Steady State]")
    simResultReference1 = simSolution1.Find("Thermal")
    companionResult1 = simResultReference1.Find("CompanionResult[tmg Act-A]")
    simResultReference1.CompanionResults.Delete(companionResult1)
   
    # ----------------------------------------------
    #   Menu: Tools->Journal->Stop Recording
    # ----------------------------------------------
   
if __name__ == '__main__':
    main()

 

However, trying to run this yields the following error:

 

AttributeError: 'NXOpen.CAE.SimResultReference' object has no attribute 'Find'

 

I can't find any delete methods in the API for companion results. Does anyone know how to do this?

 

Thanks,

Peter

4 REPLIES

Re: Delete Companion Results with NX Open Python

Siemens Phenom Siemens Phenom
Siemens Phenom

SimResultReference seems like it may have a couple of bugs in it. Please contact GTAC and file a PR.

 

In the meantime, give the following a try:

 

import sys
import traceback

import NXOpen

theSession = NXOpen.Session.GetSession()
theLw = theSession.ListingWindow

def main(*args): 

    simPart = theSession.Parts.BaseWork

    try:

        # Add your application code here
        theLw.Open()

        activeSolution = simPart.Simulation.ActiveSolution
        theLw.WriteLine("Active Solution: " + activeSolution.Name)

        resultRef = activeSolution.GetResultReferenceByIndex(0)

        companionResultCollection = resultRef.CompanionResults

        for companionResult in companionResultCollection:
            theLw.WriteLine("  Deleting companion result: " + companionResult.Name)
            companionResultCollection.Delete(companionResult)

    except Exception as ex:

        # Show the exception in a Message Box
        theUI.NXMessageBox.Show("Hello World Exception", NXOpen.NXMessageBox.DialogType.Error, str(ex))
        
        # Write the exception to the listing window
        theLw.Open()
        theLw.WriteLine("ex:")
        theLw.WriteLine(str(ex))
        
        # Print the exception to the syslog
        print(ex)

        # Write the traceback to the listing window
        theLw.WriteLine("")
        theLw.WriteLine("traceback:")
        theLw.WriteLine(traceback.format_exc())

        # Print the traceback to the syslog
        traceback.print_exc()

    except:

        print("Unexpected error:", sys.exc_info()[0])

# Main program.

if __name__ == "__main__":
    main()

Re: Delete Companion Results with NX Open Python

Valued Contributor
Valued Contributor

Thanks Jim,

To make it delete all companion results rather than every other, I have replaced the for loop in your code with the following:

 

        results = []

        for companionResult in companionResultCollection:
            results.append(companionResult)
            theLw.WriteLine("  Deleting companion result: " + companionResult.Name)
            
        for myResult in results:
            companionResultCollection.Delete(myResult)

Where do I go to submit a PR?

 

 

Peter

Re: Delete Companion Results with NX Open Python

Siemens Phenom Siemens Phenom
Siemens Phenom

CAE.SimSolution.ResultReferenceCount() is suppossed to return the number of results so that you can loop over them all using CAE.SimSolution.GetResultReferenceByIndex()

 

However, I get a fatal error when calling ResultReferenceCount(), so I hardcoded the index to 0 in GetResultReferenceByIndex().

 

PR 7555094 was submitted on the ResultReferenceCount error.

Re: Delete Companion Results with NX Open Python

Genius
Genius
 results = []

        for companionResult in companionResultCollection:
            results.append(companionResult)
            theLw.WriteLine("  Deleting companion result: " + companionResult.Name)
            
        for i in range(0, len(results)):
            results.pop(i)
            

just another way to write it. or use a slice index

del results[0:lastpos] 

or remove the specific element

results.remove('nameofelement')