# coding: utf-8
#
# Copyright © 2020 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.
#

import apex
import datetime

def main(dict={}):
	apex.disableShowOutput()
	print("I am in getScenarios And Events:", datetime.datetime.now())
	
	study = apex.getPrimaryStudy()
	try:
		scenarios = study.getScenarios(True)
	except:
		print("There are no scenarios in this model")
		scenarios=[]


	#Static Scenario 1
	#Step: Step 1
	
	#Dynamic Scenario Part 1
	#Step: 2.Modal Extraction
	#Step: 3.Dynamic
	
	#Mode Scenario Part 1
	#Step: Mode Scenario Part 1
	
	#Buckling Scenario Part 1
	#Step: 1.Buckling


	scenarioNames = []
	EventL=[]
	for scenario in scenarios:
		# get events Output List with each element representing scenario with event names separated by $
		steps=scenario.getSteps()
		
		AllowedEnvironments=["Static","Dynamic","Mode","Buckling"]
		if "EnvironmentType" in dict.keys():
			if dict["EnvironmentType"] in AllowedEnvironments:
				EnvType=dict["EnvironmentType"]
			else:
				EnvType="Static"
		else:
			EnvType="Static"
			
		if EnvType=="Static":
			#Check firs step
			if steps[0].getName().find("Step") == 0 or steps[0].getName().find("Etap") == 0 or steps[0].getName().find("Schritt") == 0:
				scenarioNames.append( scenario.getName() )
				print(scenario.getName())
				
				step=steps[0]
				events=step.getEvents()
				print("There are",len(events) )
				EventL.append('$'.join([ event.getName() for event in events ]))
			else:
				print("Scenario: ", scenario.getName() , " is not of the right type" )
		
		elif EnvType=="Dynamic":
			if steps[len(steps) -1 ].getName().find("3.Dynamic") == 0:
				scenarioNames.append( scenario.getName() )
				print(scenario.getName())
				EventL.append('N/A')
			else:
				print("Scenario: ", scenario.getName() , " is not of the right type" )	
				
				
		elif EnvType=="Mode":
			if steps[0].getName().find("Mode") == 0:
				scenarioNames.append( scenario.getName() )
				print(scenario.getName())
				EventL.append('N/A')
			else:
				print("Scenario: ", scenario.getName() , " is not of the right type" )	
				
		elif EnvType=="Buckling":
			if steps[0].getName().find("1.Buckling") == 0:
				scenarioNames.append( scenario.getName() )
				print(scenario.getName())
				EventL.append('N/A')
			else:
				print("Scenario: ", scenario.getName() , " is not of the right type" )	



	print("ScenarioNames:",scenarioNames)
	print("Event List:",EventL)
			
		
	ret_dict = {}
	ret_dict["ScenarioList"] = scenarioNames
	ret_dict["EventL"] = EventL
	
	return ret_dict

if __name__ == "__main__":
	dict={}
	dict["EnvironmentType"]="Static"
	main(dict)
