#
# 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 sys
import os
import apex_sdk
import clr

#.NET references
import System
import System.Windows.Controls as WPFControls
from System.Windows.Automation import AutomationProperties
from Microsoft.Win32 import OpenFileDialog
from Microsoft.Win32 import SaveFileDialog

file_path = os.path.dirname(os.path.realpath(__file__))
utils_path= os.path.dirname(os.path.realpath(file_path))
xml_file= os.path.join(utils_path, 'res\\UtilityUserSettings.xml')
#xml_file= r"D:\Software\APEX-Utilities-Test\Apex Utilities\Utilities\Material\ImportfromHypersizer\res\UtilityUserSettings.xml"
	
if not utils_path in sys.path:
	sys.path.insert(0, utils_path)
	print('inserted ' + utils_path + ' into sys.path')	
	

def read_settings(settings_file, tmp_h=""):
	silent_flag = True
	#
	inf = open(settings_file, 'r')
	lines = inf.readlines()
	inf.close()
	keys = []
	SettingsD = {}
	n = 0
	while n < len(lines):
		if lines[n].find('<') > -1 and  lines[n].find('\\') < 0 and lines[n].find('/') < 0:
			# found start tag
			# print lines[n]
			tmp = lines[n].strip().replace('<', '').replace('>', '')
			# print tmp
			endtag = "<\\" + tmp + ">"
			endtag2 = "</" + tmp + ">"
			# print endtag
			n += 1
			SettingsD[tmp] = {}
			while lines[n].find(endtag) < 0 and lines[n].find(endtag2) <0 :
				#while lines[n].find(endtag) < 0 :
				if lines[n].find('=') > 1:
					newkey = lines[n].strip().split('=')[0]
					SettingsD[tmp][newkey] = lines[n].strip().split('=')[1]
					n += 1
				elif lines[n].find('#') == 0:
					continue
				else:
					# not a valid settnigs command
					#print "Not a valid settngs command: " + lines[n]
					n += 1
				#print(lines[n])
		n += 1

	# supress write out of all settings

	if silent_flag != True:
		if tmp_h != -1:
			print("====")
			print("Following setting groups detected:")

		for item in reversed(sorted(SettingsD.keys())):
			if tmp_h != -1:
				print("==== " + item + "   " + " ====")

			for key in list(SettingsD[item].keys()):
				if tmp_h != "" and tmp_h != -1:
					print("%-16s : %-12s" % (key, SettingsD[item][key].strip()))
				elif tmp_h == -1:
					continue
				else:
					print("%-16s : %-12s" % (key, SettingsD[item][key].strip()))
	#
	return SettingsD


def getUIContent():

	my_toolProperty = apex_sdk.ToolPropertyContainer()
	my_toolProperty.ToolPropertyContent = getCustomToolPropertyContent()

	my_toolProperty.TitleText = "Import data from HyperSIzer"
	
	file_path = os.path.dirname(os.path.realpath(__file__))
	my_toolProperty.TitleImageUriString = os.path.dirname(file_path) + "\\res\\importHyperSizer.png"
	
	my_toolProperty.WorkFlowInstructions = """<HTML><BODY><P STYLE=\"font-size:10;\"><SPAN>Import model and properties coming from a Hypersizer optimization</SPAN></P>
	<P><SPAN> </SPAN></P>
	<P STYLE=\"font-size:10;\"><SPAN>Send questions to stefan.tynelius@hexagon.com - Elvis sightings to larry.pearce@hexagon.com</SPAN></P></BODY></HTML>"""

	#handle apply button (green) click event
	my_toolProperty.AppliedCommand = apex_sdk.ActionCommand(System.Action(HandleApplyButton))
	# handle exit button (red) click event
	my_toolProperty.ExitCommand = apex_sdk.ActionCommand(System.Action(HandleExitButton))
	
	#define pickFiterTools
	#my_toolProperty.ShowPickChoice = True
	#my_toolProperty.IsCustomTool = True
	#my_toolProperty.PickFilterList = setPickFilterTools()
	
	return my_toolProperty

def createTextBlock(parent, text, row, col):
	textBlock = WPFControls.TextBlock()
	textBlock.Text = text
	
	textBlock.Margin = System.Windows.Thickness(5.)
	
	WPFControls.Grid.SetRow(textBlock, row)
	WPFControls.Grid.SetColumn(textBlock, col)
	parent.Children.Add(textBlock)
	
	return textBlock

def updateComboBox(comboBox, comboItems):
	
	for comboItem in comboItems:
		item = WPFControls.ComboBoxItem()
		item.Content=comboItem
		comboBox.Items.Add(item)
	
	comboBox.SelectedIndex="0"

#get tool property content
def getCustomToolPropertyContent():
	#Create a Grid
	my_Grid = WPFControls.Grid()
	
	createLayout(my_Grid, 15, 2)

	row = 0
	col = 0
	
	# get defaults from settings file
	file_path = os.path.dirname(os.path.realpath(__file__))
	utils_path= os.path.dirname(os.path.realpath(file_path))
	#settings_file= os.path.join(utils_path, 'UtilityUserSettings.xml')
	
	SettingsD=read_settings(xml_file)
	
	if 1==0:
		# global ConsolidateOptionComboBox, ConsolidateOptionTextBlock
		# ConsolidateOptionTextBlock = createTextBlock(my_Grid, "Analysis option:", row, 0)
		# ConsolidateOptionComboBox  = createComboBox2(my_Grid, row, 1)
		# updateComboBox2(ConsolidateOptionComboBox, ["Nastran SOL101", "Nastran SOL400","Marc Mentat","Nastran SOL106","Abaqus .inp"])
		# ConsolidateOptionComboBox.SelectedIndex = 0
		# row += 1
		
		# ConsolidateOptionComboBox.SelectionChanged += ConsolidateOptionComboBoxCB
		
		# ConsolidateOptionComboBox.Visibility = System.Windows.Visibility.Collapsed
		# ConsolidateOptionTextBlock.Visibility = System.Windows.Visibility.Collapsed
		
		# Bdf export options
		
			#Consolidate Variable Thickness
		
		# try:
			# convert_node_thick_flag = SettingsD['System']['NodeThicknessToPshells'].upper()

			
			# thick_inc=SettingsD['System']['NodeThicknessToPshellsInc']
			# thick_lo_limit=SettingsD['System']['NodeThicknessToPshellsLower_T']
			# thick_up_limit=SettingsD['System']['NodeThicknessToPshellsUpper_T']
			
		# except:
			# convert_node_thick_flag = "FALSE"
			# thick_lo_limit="0."
			# thick_inc="0.1"
			# thick_up_limit="100."
		
		# if convert_node_thick_flag == "TRUE":
			# SelectConsolidateOption = 0
		# else:
			# SelectConsolidateOption = 1
		
		
		
		# global ConsolidateOptionComboBox, ConsolidateOptionTextBlock
		# ConsolidateOptionTextBlock = createTextBlock(my_Grid, "Consolidate Variable Thickness:", row, 0)
		# ConsolidateOptionComboBox  = createComboBox2(my_Grid, row, 1)
		# updateComboBox2(ConsolidateOptionComboBox, ["Yes", "No"])
		# ConsolidateOptionComboBox.SelectedIndex = 0
		# row += 1
		
		# ConsolidateOptionComboBox.SelectionChanged += ConsolidateOptionComboBoxCB
		
		# ConsolidateOptionComboBox.Visibility = System.Windows.Visibility.Visible
		# ConsolidateOptionTextBlock.Visibility = System.Windows.Visibility.Visible
		
		
		#NodeThicknessToPshells=True
		#NodeThicknessToPshellsLower_T=0.1
		#NodeThicknessToPshellsUpper_T=15.0
		#NodeThicknessToPshellsInc=0.1
		
		# global nodeThicknessLoTextBox, nodeThicknessLoTextBlock
		# nodeThicknessLoTextBox, nodeThicknessLoTextBlock= createDataBox(my_Grid, "Lower Thickness ", thick_lo_limit, row, col)
		# row += 1
		# nodeThicknessLoTextBox.Visibility = System.Windows.Visibility.Visible
		# nodeThicknessLoTextBlock.Visibility       = System.Windows.Visibility.Visible
		
		# global nodeThicknessUpTextBox, nodeThicknessUpTextBlock
		# nodeThicknessUpTextBox, nodeThicknessUpTextBlock= createDataBox(my_Grid, "Upper Thickness", thick_up_limit, row, col)
		# row += 1
		# nodeThicknessUpTextBox.Visibility = System.Windows.Visibility.Visible
		# nodeThicknessUpTextBlock.Visibility       = System.Windows.Visibility.Visible
		
		# global ThicknessRangeIncTextBox, ThicknessRangeIncTextBlock
		# ThicknessRangeIncTextBox, ThicknessRangeIncTextBlock  = createDataBox(my_Grid, "Thickness Inc. ", thick_inc, row, col)
		# row += 1
		# ThicknessRangeIncTextBox.Visibility = System.Windows.Visibility.Visible
		# ThicknessRangeIncTextBlock.Visibility       = System.Windows.Visibility.Visible
		
		
		# ---- Scenarios----
		
		# global scenariosComboBox, scenariosComboTextBlock
		# scenariosComboTextBlock = createTextBlock(my_Grid, "Scenario:", row, 0)
		# scenariosComboBox       = createComboBox2(my_Grid, row, 1)
		# row += 1

		# scenariosComboTextBlock.Visibility = System.Windows.Visibility.Visible
		# scenariosComboBox.Visibility       = System.Windows.Visibility.Visible
		
		# #file_path = os.path.dirname(os.path.realpath(__file__))
		# #scenarios_script= os.path.join(file_path, 'getScenarios.py')
		# #apex_sdk.runScriptFunction(scenarios_script, "main", callback=getScenariosCB)
		
		# # ---- Parts----
		
		# global partsComboBox, partsComboTextBlock
		# partsComboTextBlock = createTextBlock(my_Grid, "Parts:", row, 0)
		# partsComboBox       = createComboBox2(my_Grid, row, 1)
		# row += 1

		# partsComboTextBlock.Visibility = System.Windows.Visibility.Collapsed
		# partsComboBox.Visibility       = System.Windows.Visibility.Collapsed
		
		
		# # Get both parts and scenarios... a trick is used to get both...
		
		# file_path = os.path.dirname(os.path.realpath(__file__))
		# script_path_parts= os.path.join(file_path, 'getScenarios.py')
		# apex_sdk.runScriptFunction(script_path_parts, "main", callback=getScenariosCB)
		
		# test_list=["TRUE","YES"]
		# solveOption=0
		# try:
			# if SettingsD['NASTRAN_JOB_SETUP']['RunNastran'].upper() in test_list:
				# solveOption=2
		# except:
			# pass
		
		
		
		
		#global solveOptionComboBox, solveOptionTextBox
		#solveOptionComboBox, solveOptionTextBox = createComboBox(my_Grid, "Export Options:", ["Run-Ready input deck", "Bulk data only", "Run Analysis"], 1, row, col)
		#solveOptionComboBox.SelectedIndex = solveOption
		#row += 1
		
		#global postComboBox, postTextBlock
		#postComboBox, postTextBlock = createComboBox(my_Grid, "Post Options: ", ["No Post", "Post using Patran"], 0, row, col)
		#row += 1
		
		#postTextBlock.Visibility = System.Windows.Visibility.Collapsed
		#postComboBox.Visibility       = System.Windows.Visibility.Collapsed
		
		
		
		# ---- Material----
		#global matlComboBox, matlComboTextBlock
		#matlComboTextBlock = createTextBlock(my_Grid, "Material:", row, 0)
		#matlComboBox       = createComboBox2(my_Grid, row, 1)
		#row += 1

		#matlComboTextBlock.Visibility = System.Windows.Visibility.Visible
		#matlComboBox.Visibility       = System.Windows.Visibility.Visible
		
		#file_path = os.path.dirname(os.path.realpath(__file__))
		#script_path= os.path.join(file_path, 'auGetMaterials.py')
		#apex_sdk.runScriptFunction(script_path, "main", callback=getMatls)
		pass
	

	# File Save Text box
	
	global fileTextBox
	fileTextBox = createDataBox2(my_Grid, "", row, 1)
	#row += 1
	
	#WPFControls.Grid.SetColumnSpan(fileTextBox, 2)
	
	readCsvBtn = WPFControls.Button()
	readCsvBtn.Content="Read stack from HS"
	
	#WPFControls.Grid.SetColumnSpan(readCsvBtn, 2)
	
	WPFControls.Grid.SetRow(readCsvBtn, row)
	WPFControls.Grid.SetColumn(readCsvBtn, col)
	
	#Link a function to the Button "Click" event 
	#This function will be called every time the Button is clicked
	readCsvBtn.Click+=HandleOpenCsvBtn
	
	# Add the controls to the Grid
	my_Grid.Children.Add(readCsvBtn)
	
	row += 1
	# Options File  Text box
	
	#if not os.path.isfile(xml_path):
	#	xml_file="aaa"
	#	print("xml_file: " + xml_file + " can not be found")
		
	
	
	
	
	global readBdfTextBox
	readBdfTextBox = createDataBox2(my_Grid,"", row, 1)
	#row += 1
	
	#WPFControls.Grid.SetColumnSpan(readBdfTextBox, 2)
	
	readBdfBtn = WPFControls.Button()
	readBdfBtn.Content="Open Bdf"
	
	#WPFControls.Grid.SetColumnSpan(readBdfBtn, 2)
	
	WPFControls.Grid.SetRow(readBdfBtn, row)
	WPFControls.Grid.SetColumn(readBdfBtn, col)
	
	#Link a function to the Button "Click" event 
	#This function will be called every time the Button is clicked
	readBdfBtn.Click+=HandleOpenBdfBtn
	
	# Add the controls to the Grid
	my_Grid.Children.Add(readBdfBtn)
	
	
	#Return the Grid
		
	return my_Grid
	
def ConsolidateOptionComboBoxCB(sender, event):
	# I need to learn and understand sender and event
	# global ConsolidateOptionComboBox, ConsolidateOptionTextBlock
	# global scenariosComboBox, scenariosComboTextBlock
	# global solveOptionComboBox, solveOptionTextBox
	# global nodeThicknessLoTextBox, nodeThicknessLoTextBlock
	# global nodeThicknessUpTextBox, nodeThicknessUpTextBlock
	# global ThicknessRangeIncTextBox, ThicknessRangeIncTextBlock
	global fileTextBox
	# global partsComboBox, partsComboTextBlock
	# global postComboBox, postTextBlock
	global readCsvBtn
	
	
	#choices = ConsolidateOptionComboBox.SelectedValue.ToString().split(":")
	#choice = choices[len(choices)-1].lstrip().rstrip()
	
	#["Nastran SOL101", "Nastran SOL400","Marc Mentat","Nastran SOL106","Abaqus .inp"]
	
	
	# if choice == "Yes":

		# #scenariosComboBox.Visibility       = System.Windows.Visibility.Visible
		# #scenariosComboTextBlock.Visibility       = System.Windows.Visibility.Visible 
		# #partsComboBox.Visibility       = System.Windows.Visibility.Collapsed
		# #partsComboTextBlock.Visibility       = System.Windows.Visibility.Collapsed 
		# #solveOptionTextBox.Visibility       = System.Windows.Visibility.Visible
		# #solveOptionComboBox.Visibility       = System.Windows.Visibility.Visible
		# #postTextBlock.Visibility       = System.Windows.Visibility.Collapsed
		# #postComboBox.Visibility       = System.Windows.Visibility.Collapsed
		# # nodeThicknessLoTextBox.Visibility       = System.Windows.Visibility.Visible
		# # nodeThicknessLoTextBlock.Visibility       = System.Windows.Visibility.Visible
		# # nodeThicknessUpTextBox.Visibility       = System.Windows.Visibility.Visible
		# # nodeThicknessUpTextBlock.Visibility       = System.Windows.Visibility.Visible
		# # ThicknessRangeIncTextBox.Visibility       = System.Windows.Visibility.Visible
		# # ThicknessRangeIncTextBlock.Visibility       = System.Windows.Visibility.Visible
		# #fileTextBox.Visibility       = System.Windows.Visibility.Visible
		# #readCsvBtn.Visibility           = System.Windows.Visibility.Visible
		
	# elif choice == "No":

		# #scenariosComboBox.Visibility       = System.Windows.Visibility.Visible
		# #scenariosComboTextBlock.Visibility       = System.Windows.Visibility.Visible
		# #partsComboBox.Visibility       = System.Windows.Visibility.Collapsed
		# #partsComboTextBlock.Visibility       = System.Windows.Visibility.Collapsed
		# #solveOptionTextBox.Visibility       = System.Windows.Visibility.Collapsed
		# #solveOptionComboBox.Visibility       = System.Windows.Visibility.Collapsed
		# #postTextBlock.Visibility       = System.Windows.Visibility.Collapsed
		# #postComboBox.Visibility       = System.Windows.Visibility.Collapsed
		# # nodeThicknessLoTextBox.Visibility       = System.Windows.Visibility.Collapsed
		# # nodeThicknessLoTextBlock.Visibility       = System.Windows.Visibility.Collapsed
		# # nodeThicknessUpTextBox.Visibility       = System.Windows.Visibility.Collapsed
		# # nodeThicknessUpTextBlock.Visibility       = System.Windows.Visibility.Collapsed
		# # ThicknessRangeIncTextBox.Visibility       = System.Windows.Visibility.Collapsed
		# # ThicknessRangeIncTextBlock.Visibility       = System.Windows.Visibility.Collapsed
		# #fileTextBox.Visibility       = System.Windows.Visibility.Visible	
		# #readCsvBtn.Visibility           = System.Windows.Visibility.Visible
	

	
def createLayout(parent, numRows, numCols):
	for k in range(numRows):
		parent.RowDefinitions.Add(WPFControls.RowDefinition())
	for k in range(numCols):
		parent.ColumnDefinitions.Add(WPFControls.ColumnDefinition())

def createComboBox(parent, title, labels, selected, row, col):
	textBlock = WPFControls.TextBlock()
	textBlock.Text = title
	WPFControls.Grid.SetRow(textBlock, row)
	WPFControls.Grid.SetColumn(textBlock, col)

	parent.Children.Add(textBlock)
	
	comboBox = WPFControls.ComboBox()
	for label in labels:
		item = WPFControls.ComboBoxItem()
		item.Content=label
		comboBox.Items.Add(item)
	
	WPFControls.Grid.SetRow(comboBox, row)
	WPFControls.Grid.SetColumn(comboBox, col+1)
	
	if selected > len(labels): selected = 0
	comboBox.SelectedIndex=str(selected)
	
	parent.Children.Add(comboBox)
	
	return comboBox, textBlock

def createComboBox2(parent, row, col):
	comboBox = WPFControls.ComboBox()
	
	comboBox.Margin = System.Windows.Thickness(5.)
	
	WPFControls.Grid.SetRow(comboBox, row)
	WPFControls.Grid.SetColumn(comboBox, col)
	
	parent.Children.Add(comboBox)
	
	return comboBox

def updateComboBox2(comboBox, comboItems):
	
	for comboItem in comboItems:
		item = WPFControls.ComboBoxItem()
		item.Content=comboItem
		comboBox.Items.Add(item)
	
	comboBox.SelectedIndex="0"

def createDataBox(parent, title, value, row, col):
	textBlock = WPFControls.TextBlock()
	textBlock.Text = title
	WPFControls.Grid.SetRow(textBlock, row)
	WPFControls.Grid.SetColumn(textBlock, col)

	textBox =WPFControls.TextBox()
	WPFControls.Grid.SetRow(textBox, row)
	WPFControls.Grid.SetColumn(textBox, col+1)
	textBox.Text=value
	
	parent.Children.Add(textBlock)
	parent.Children.Add(textBox)
	
	return textBox,textBlock

def createDataBox2(parent, value, row, col):
	textBox =WPFControls.TextBox()
	WPFControls.Grid.SetRow(textBox, row)
	WPFControls.Grid.SetColumn(textBox, col)
	textBox.Text=value
	
	parent.Children.Add(textBox)
	
	return textBox

def setPickFilterTools():
	pickChoices = System.Collections.Generic.List[System.String]()
	
	#exclusive picking and visibility picking	
	pickChoices.Add(apex_sdk.PickFilterTypes.ExclusivePicking)
	pickChoices.Add(apex_sdk.PickFilterTypes.VisibilityPicking)
	
	#Assembly and part
	#pickChoices.Add(apex_sdk.PickFilterTypes.Assembly)
	pickChoices.Add(apex_sdk.PickFilterTypes.Part)
	
	#Bodies	
	#pickChoices.Add(apex_sdk.PickFilterTypes.SolidMesh)
	#pickChoices.Add(apex_sdk.PickFilterTypes.Solid)
	#pickChoices.Add(apex_sdk.PickFilterTypes.SurfaceMesh)
	#pickChoices.Add(apex_sdk.PickFilterTypes.Surface)
	#pickChoices.Add(apex_sdk.PickFilterTypes.CurveMesh)
	#pickChoices.Add(apex_sdk.PickFilterTypes.Curve)
	#pickChoices.Add(apex_sdk.PickFilterTypes.Point)
	
	#features
	#pickChoices.Add(apex_sdk.PickFilterTypes.CellMesh)
	#pickChoices.Add(apex_sdk.PickFilterTypes.FaceMesh)
	#pickChoices.Add(apex_sdk.PickFilterTypes.EdgeMesh)
	#pickChoices.Add(apex_sdk.PickFilterTypes.Cell)
	#pickChoices.Add(apex_sdk.PickFilterTypes.VertexMesh)
	
	#Lower Dimensional Entities
	#pickChoices.Add(apex_sdk.PickFilterTypes.Element3D)
	#pickChoices.Add(apex_sdk.PickFilterTypes.Element2D)
	#pickChoices.Add(apex_sdk.PickFilterTypes.Face)
	#pickChoices.Add(apex_sdk.PickFilterTypes.MeshSeed)
	#pickChoices.Add(apex_sdk.PickFilterTypes.Element1D)
	#pickChoices.Add(apex_sdk.PickFilterTypes.Edge)
	#pickChoices.Add(apex_sdk.PickFilterTypes.SeedPoint)
	#pickChoices.Add(apex_sdk.PickFilterTypes.Node)
	#pickChoices.Add(apex_sdk.PickFilterTypes.Vertex)
	
	#Sub Lower Dimensional Entities
	#pickChoices.Add(apex_sdk.PickFilterTypes.ElementEdge)
	#pickChoices.Add(apex_sdk.PickFilterTypes.ElementFace)
	
	#Interactions and connections
	#pickChoices.Add(apex_sdk.PickFilterTypes.DiscreteTie)
	#pickChoices.Add(apex_sdk.PickFilterTypes.Connector)
	#pickChoices.Add(apex_sdk.PickFilterTypes.EdgeTie)
	#pickChoices.Add(apex_sdk.PickFilterTypes.Joint)
	#pickChoices.Add(apex_sdk.PickFilterTypes.Glue)
	
	#LBCs
	#pickChoices.Add(apex_sdk.PickFilterTypes.Pressure)
	#pickChoices.Add(apex_sdk.PickFilterTypes.Gravity)
	#pickChoices.Add(apex_sdk.PickFilterTypes.Constraint)
	#pickChoices.Add(apex_sdk.PickFilterTypes.ForceMoment)
	#pickChoices.Add(apex_sdk.PickFilterTypes.EnforcedMotion)
	
	#Attributes and other objects
	#pickChoices.Add(apex_sdk.PickFilterTypes.CompositeZone)
	#pickChoices.Add(apex_sdk.PickFilterTypes.LayeredPanel)
	#pickChoices.Add(apex_sdk.PickFilterTypes.InterfacePoint)
	#pickChoices.Add(apex_sdk.PickFilterTypes.PointMass)
	#pickChoices.Add(apex_sdk.PickFilterTypes.BeamSpan)
	#pickChoices.Add(apex_sdk.PickFilterTypes.Offset)
	
	#Sensors
	#pickChoices.Add(apex_sdk.PickFilterTypes.MotionEnvelopeSensor)
	#pickChoices.Add(apex_sdk.PickFilterTypes.ClearanceSensor)
	#pickChoices.Add(apex_sdk.PickFilterTypes.CrossSectionSensor)
	#pickChoices.Add(apex_sdk.PickFilterTypes.PointSensor)
	
	#Coordinate system
	#pickChoices.Add(apex_sdk.PickFilterTypes.CoordinateSystem)
	
	#Tool Registration
	#Tool Name has to be the same
	#apex_sdk.PickFilterRegistration.RegisterTool("Auto Tie Nodes", pickChoices)
	return pickChoices


#Function to handle the Import Button "Click" event
#This function gets called every time the Button is clicked
@apex_sdk.errorhandler
def HandleOpenCsvBtn(sender,args):

	#Create a File open dialog
	#dialog = OpenFileDialog()
	dialog = OpenFileDialog()
	
	#Configure for single file selection
	dialog.Multiselect = False
	
	#Set up the file types you want to support
	dialog.Filter = "All Files  |*.csv"
	#dialog.Filter = "MWF Files  |*.mwf* | All Files  |*.*"

	#Display the dialog
	#If it returns anything
	#   get the file name
	#   display it on the text nox 
	if dialog.ShowDialog():
		selectedFile = dialog.FileName
		fileTextBox.Text = selectedFile


#Function to handle the Import Button "Click" event
#This function gets called every time the Button is clicked
@apex_sdk.errorhandler
def HandleOpenBdfBtn(sender,args):

	#Create a File open dialog
	#dialog = OpenFileDialog()
	dialog = OpenFileDialog()
	
	#Configure for single file selection
	dialog.Multiselect = False
	
	#Set up the file types you want to support
	#dialog.Filter = "All Files  |*.*"
	dialog.Filter = "All Files  |*.bdf"

	#Display the dialog
	#If it returns anything
	#   get the file name
	#   display it on the text nox 
	if dialog.ShowDialog():
		selectedFile = dialog.FileName
		readBdfTextBox.Text = selectedFile

		
@apex_sdk.errorhandler
def HandleApplyButton():
	file_path = os.path.dirname(os.path.realpath(__file__))
	script_path= os.path.join(file_path, 'importHyperSizer.py')
	# Create a Dictionary to store the user defined tool data  
	dictionary = {}
	
	# choices = ConsolidateOptionComboBox.SelectedValue.ToString().split(":")
	# choice = choices[len(choices)-1].lstrip().rstrip()
	# dictionary["ConsolidateThickness"] = choice
	
	# choices = solveOptionComboBox.SelectedValue.ToString().split(":")
	# choice = choices[len(choices)-1].lstrip().rstrip()
	# dictionary["RunNastran"] = choice
	
	#choices = postComboBox.SelectedValue.ToString().split(":")
	#choice = choices[len(choices)-1].lstrip().rstrip()
	#dictionary["PostOption"] = choice
	
	# choices = scenariosComboBox.SelectedValue.ToString().split(":")
	# choice = choices[len(choices)-1].lstrip().rstrip()
	
	# dictionary["Scenario"] = choice
	dictionary["csv_file"]        = fileTextBox.Text
	dictionary["bdf_file"]        = readBdfTextBox.Text
	
	#choices = matlComboBox.SelectedValue.ToString().split(":")
	#choice = choices[len(choices)-1].lstrip().rstrip()
	#dictionary["material"] = choice
	
	#choices = partsComboBox.SelectedValue.ToString().split(":")
	#choice = choices[len(choices)-1].lstrip().rstrip()
	#dictionary["Part"] = choice
	
	#dictionary["tolerance"]    = toleranceTextBox.Text
	
	#dictionary["nodeThicknessLo"]     = nodeThicknessLoTextBox.Text
	#dictionary["nodeThicknessUp"]     = nodeThicknessUpTextBox.Text
	#dictionary["ThicknessRangeInc"]     = ThicknessRangeIncTextBox.Text
	
	apex_sdk.runScriptFunction(script_path, "main", dictionary)
		
# exit button (red) from tool header
@apex_sdk.errorhandler
def HandleExitButton():
	print("HandleExitButton")


	
	
#This function receives data from the Script API function and updates the Tool UI 
@apex_sdk.errorhandler
def getScenariosCB():
	global scenariosComboBox
	global partsComboBox

	
	ret_dict = apex_sdk.getScriptFunctionReturnData()
	
	tmp = ret_dict["ScenarioList"]
	
	scenarios=["Entire Model- Default Loadcase"]
	parts=[]
	
	partsFlag=False
	for item in tmp:
		if item == '#Parts': 
			partsFlag=True
			continue
		if not partsFlag:
			scenarios.append(item)
		else:
			parts.append(item)

	
	updateComboBox2(scenariosComboBox, scenarios)	
	updateComboBox2(partsComboBox, parts)	
	