#
# 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 Microsoft.Win32 import OpenFileDialog

# Define current file path of example
current_file_path = os.path.dirname(os.path.realpath(__file__))

# Set pre-defined properties of ToolPropertyContainer
def getUIContent():

    my_toolProperty = apex_sdk.ToolPropertyContainer()
    # Provide an icon and a name for the tool property panel
    my_toolProperty.TitleImageUriString = os.path.join(os.path.dirname(current_file_path), r"res\Icon_importAdamsLoads.png")
    my_toolProperty.TitleText = "import Adams Loads"


    my_toolProperty.WorkFlowInstructions = """<HTML><BODY><P STYLE=\"font-size:10;\"><SPAN>Import loads from Adams</SPAN></P>
    <P><SPAN> </SPAN></P>
    <P STYLE=\"font-size:10;\"><SPAN>Creates a dummy surface and adds loads from a chosen Adams export to it</SPAN></P></BODY></HTML>
    <P><SPAN> </SPAN></P>
    <P STYLE=\"font-size:10;\"><SPAN>+ Select the file you want to import (it has to be in abaqus format) </SPAN></P></BODY></HTML>
    <P STYLE=\"font-size:10;\"><SPAN>+ click apply</SPAN></P></BODY></HTML>"""
    
    # handle apply button (green) click event
    my_toolProperty.AppliedCommand = apex_sdk.ActionCommand(System.Action(HandleApplyButton))
    
    # Define UI
    my_toolProperty.ToolPropertyContent = getCustomToolPropertyContent()
    return my_toolProperty

def getCustomToolPropertyContent():

    # Create a Grid
    my_Grid = WPFControls.Grid()

    # Add 2 Rows and 1 Column
    my_Grid.RowDefinitions.Add(WPFControls.RowDefinition())
    my_Grid.RowDefinitions.Add(WPFControls.RowDefinition())
    my_Grid.ColumnDefinitions.Add(WPFControls.ColumnDefinition())

    # Create an empty input TextBox to display the file name
    # Assign it to Row 0, Column 0
    global fileNameTextBox
    fileNameTextBox = WPFControls.TextBox()
    WPFControls.Grid.SetRow(fileNameTextBox, 0)
    WPFControls.Grid.SetColumn(fileNameTextBox, 0)

    # Create a button and set it's text to "Choose file"
    # Assign it to Row1, Column 0
    importBtn = WPFControls.Button()
    importBtn.Content = "Choose file"
    WPFControls.Grid.SetRow(importBtn, 1)
    WPFControls.Grid.SetColumn(importBtn, 0)

    # Link a function to the Button "Click" event 
    # This function will be called every time the Button is clicked
    importBtn.Click+=HandleImportButton

    # Add the controls to the Grid
    my_Grid.Children.Add(fileNameTextBox)
    my_Grid.Children.Add(importBtn)

    # Return the Grid
    return my_Grid


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

    # Create a File open dialog
    dialog = OpenFileDialog()

    # Configure for single file selection
    dialog.Multiselect = False

    # Set up the file types you want to support
    dialog.Filter = "All Files|*.*"

    # Display the dialog
    # If it returns anything
    #   1) Get the file name
    #   2) Display it on the text box 
    if dialog.ShowDialog():
        selectedFile = dialog.FileName
        fileNameTextBox.Text = selectedFile
        global filePath_Loads
        filePath_Loads = fileNameTextBox.Text
        
@apex_sdk.errorhandler
def HandleApplyButton():
    dictionary = {}
    dictionary['filePath'] = filePath_Loads
    file_path = os.path.dirname(os.path.realpath(__file__))
    script_path= os.path.join(file_path, 'importAdamsLoads.py')
    apex_sdk.runScriptFunction(script_path, "main", dictionary)