#
# Copyright © 2021 MSC.Software Corporation
#  
# Permission to use Scripts
#  
# Permission is granted to MSC.Software customers with valid licenses of the MSC Apex™ software 
# to use script files posted on this page (collectively “Scripts”) solely for customers’ own authorized 
# internal use in conjunction with licensed use of the MSC Apex software.  
# No other use is permitted.  No license to MSC Apex or other software is granted under this Permission.
#  
# SCRIPTS ARE PROVIDED ON AN "AS-IS" BASIS, WITH NO WARRANTIES OF ANY KIND.  USE OF THE SCRIPTS IS AT CUSTOMER'S SOLE RISK.
#  
# IN NO EVENT SHALL MSC.SOFTWARE CORPORATION, ITS AFFILIATES, SUBSIDIARIES, SUPPLIERS, AGENTS, REPRESENTATIVES OR EMPLOYEES BE LIABLE FOR ANY 
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, TORT, 
# STRICT LIABILITY, OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION USE OF THE SCRIPTS, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
#  
# Customer shall indemnify, defend and hold MSC.Software Corporation, its affiliates, subsidiaries, suppliers, agents, 
# employees or representatives (collectively, “Indemnified Parties”) harmless from and against any third-party claims 
# brought against Indemnified Parties rising out of or in connection with Customer’s use of the Scripts.
#
# MSC Apex - Custom Tool Selection Stage example using Python Scripts
# Selection Stage script to be called by Selection Stage UI Example
# 
import apex

def ResetStages( dict = {} ):
    # reset (clear) all selection stages
    apex.selection.resetStages()
    return ShowStageContent( dict )

def ShowStageContent(dict={}):
    # print each selection stage list contents to console
    print( '***** Staged Entity lists *****' )
    for i in range(3):
        stageStr = str( i+1 )
        stagedEnts = apex.selection.getStageSelectionList( i+1 )
        print( 'Stage ' + stageStr + ' stagedEnts:' )
        for ent in stagedEnts:
            print( '\t' + ent.name )
    txt = '***** Staged Entity lists *****\n'
    for i in range(3):
        stageStr = str( i+1 )
        stagedEnts = apex.selection.getStageSelectionList( i+1 )
        txt += 'Stage ' + stageStr + ' Ents: '
        for ent in stagedEnts:
            txt += ent.name + ', '
        txt += '\n'
    outdict={}
    outdict['SELECTIONS'] = txt
    return outdict

def ExitTool(dict={}):
    # reset (clear) all selection stages
    apex.selection.resetStages()
    apex.selection.enableStageSelection( False )

def ClearStageSelectionList( dict={} ):
    # clear selection stage list contents
    clearStage = int( dict["ClearStage"] ) + 1
    apex.selection.clearStageSelectionList( clearStage )
    
def SetCurrentStage( dict={} ):
    newStageStr = dict["NewStage"]
    newStage = int(newStageStr) + 1
    ok = apex.selection.setCurrentStage( newStage )
    return ShowStageContent( dict )

def EnableStageSelection( dict={} ):
    apex.selection.enableStageSelection( True )
