#
# 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')
	
if not utils_path in sys.path:
	sys.path.insert(0, utils_path)
	print('inserted ' + utils_path + ' into sys.path')	

def getUIContent():

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

	my_toolProperty.TitleText = "Load Material from MaterialDir"
	
	file_path = os.path.dirname(os.path.realpath(__file__))
	my_toolProperty.TitleImageUriString = os.path.dirname(file_path) + "\\res\\loadMaterials.png"
	
	my_toolProperty.WorkFlowInstructions = """<HTML><BODY><P STYLE=\"font-size:10;\"><SPAN>Load Materials from Predefined folder</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

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

	row = 0
	col = 0
	
	
	# create Checkbox
	global readMaterials_Toggle		
	readMaterials_Toggle  = createCheckBox(my_Grid, "Load Materials", 1, row, col)
	row += 1

	
	global readTempMaterials_Toggle		
	readTempMaterials_Toggle  = createCheckBox(my_Grid, "Load Temporary Materials", 0, row, col)
	row += 1
	
	createEmptyRow(my_Grid, row)
	row += 1
	
	# 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')
	
	
	row += 1
	global userOptionsTextBox
	userOptionsTextBox = createDataBox2(my_Grid, xml_file, row, 1)
	#row += 1
	
	#WPFControls.Grid.SetColumnSpan(userOptionsTextBox, 2)
	
	userOptionBtn = WPFControls.Button()
	userOptionBtn.Content="User Options..."
	
	#WPFControls.Grid.SetColumnSpan(userOptionBtn, 2)
	
	WPFControls.Grid.SetRow(userOptionBtn, row)
	WPFControls.Grid.SetColumn(userOptionBtn, col)
	
	#Link a function to the Button "Click" event 
	#This function will be called every time the Button is clicked
	userOptionBtn.Click+=HandleUserOptionBtn
	
	# Add the controls to the Grid
	my_Grid.Children.Add(userOptionBtn)
		
	return my_Grid
	
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 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 createCheckBox(parent, title, active, row, col):
	myWrapPanel = WPFControls.WrapPanel()
	myWrapPanel.Orientation = WPFControls.Orientation.Vertical

	CheckBox = WPFControls.CheckBox()
	CheckBox.Content = title
	myWrapPanel.Children.Add(CheckBox)

	WPFControls.Grid.SetRow(myWrapPanel, row)
	WPFControls.Grid.SetColumn(myWrapPanel, col)
	CheckBox.IsChecked = System.Nullable[System.Boolean](active)

	parent.Children.Add(myWrapPanel)

	return CheckBox

def createEmptyRow(parent, row):
	textBlock = WPFControls.TextBlock()
	textBlock.Text = ""
	WPFControls.Grid.SetRow(textBlock, row)
	WPFControls.Grid.SetColumn(textBlock, 0)

	parent.Children.Add(textBlock)

	return


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 HandleUserOptionBtn(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 = "xml Files  |*.xml* | All Files  |*.*"
	dialog.Filter = "All Files  |*.xml"

	#Display the dialog
	#If it returns anything
	#   get the file name
	#   display it on the text nox 
	if dialog.ShowDialog():
		selectedFile = dialog.FileName
		userOptionsTextBox.Text = selectedFile
		
@apex_sdk.errorhandler
def HandleApplyButton():
	file_path = os.path.dirname(os.path.realpath(__file__))
	script_path= os.path.join(file_path, 'loadMaterial.py')
	# Create a Dictionary to store the user defined tool data  
	dictionary = {}
	
	# dictionary["Scenario"] = choice
	dictionary["xml_file"]        = userOptionsTextBox.Text
	dictionary["ReadMaterials"]	=readMaterials_Toggle.IsChecked
	dictionary["ReadTempMaterials"]	=readTempMaterials_Toggle.IsChecked
	apex_sdk.runScriptFunction(script_path, "main", dictionary)
		
# exit button (red) from tool header
@apex_sdk.errorhandler
def HandleExitButton():
	print("HandleExitButton")