#
# 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.
#
# MSC Apex - User Defined Tools using Python Scripts
# Radio Button UI Example
# 
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\script.png")

   my_toolProperty.TitleText = "Radio Buttons"

   # Define UI
   my_toolProperty.ToolPropertyContent = getCustomToolPropertyContent()

   # Handle apply button (green) click event
   my_toolProperty.AppliedCommand = apex_sdk.ActionCommand(System.Action(HandleApplyButton))
   
   return my_toolProperty


# Define Layout and Components
def getCustomToolPropertyContent():

   # Create a Grid
   my_Grid = WPFControls.Grid()

   # Add 6 Rows and One Column
   my_Grid.RowDefinitions.Add(WPFControls.RowDefinition())
   my_Grid.RowDefinitions.Add(WPFControls.RowDefinition())
   my_Grid.RowDefinitions.Add(WPFControls.RowDefinition())
   my_Grid.RowDefinitions.Add(WPFControls.RowDefinition()) 
   my_Grid.ColumnDefinitions.Add(WPFControls.ColumnDefinition())
   
   # Create a label for the first radio button array
   meshTextBlock = WPFControls.TextBlock()
   meshTextBlock.Text = "Vertical Array"
   WPFControls.Grid.SetRow(meshTextBlock, 0)
   WPFControls.Grid.SetColumn(meshTextBlock, 0)

   # Create a vertical array of three radio buttons
   # Create a WrapPanel   
   vWrapPanel = WPFControls.WrapPanel()
   vWrapPanel.Orientation=WPFControls.Orientation.Vertical
   
   # Create a List to hold the radio buttons in the first array
   # We'll use this later to make it easier to find which button is checked 
   global vertical_radio_buttons
   vertical_radio_buttons = [] 

   # Create a first radio button
   # Add it to the list and to the WrapPanel
   vertical_radio_buttons.append(WPFControls.RadioButton())
   vertical_radio_buttons[0].Content = "Option 1"
   # Set the first radio button to be checked
   # Note the horrible notation required to define a Nullable Boolean True
   vertical_radio_buttons[0].IsChecked = System.Nullable[System.Boolean](True)
   vWrapPanel.Children.Add(vertical_radio_buttons[0])
 
   # Create a second radio button and add to the WrapPanel
   vertical_radio_buttons.append(WPFControls.RadioButton())
   vertical_radio_buttons[1].Content = "Option 2"
   vWrapPanel.Children.Add(vertical_radio_buttons[1])
   
   # Create a third radio button and add to the WrapPanel
   vertical_radio_buttons.append(WPFControls.RadioButton())
   vertical_radio_buttons[2].Content = "Option 3"
   vWrapPanel.Children.Add(vertical_radio_buttons[2])

   # Position the WrapPanel in the first row, column of the Grid
   WPFControls.Grid.SetRow(vWrapPanel, 1)
   WPFControls.Grid.SetColumn(vWrapPanel, 0)

   # Add the WrapPanel to the Grid
   my_Grid.Children.Add(meshTextBlock)   
   my_Grid.Children.Add(vWrapPanel)

   # Second Button Array
   # Create a label for the second radio button array
   meshTextBlock2 = WPFControls.TextBlock()
   meshTextBlock2.Text = "Horizontal Array"
   WPFControls.Grid.SetRow(meshTextBlock2, 2)
   WPFControls.Grid.SetColumn(meshTextBlock2, 0)

   # Create a horizontal array of three radio buttons
   # Create a WrapPanel   
   hWrapPanel = WPFControls.WrapPanel()
   hWrapPanel.Orientation=WPFControls.Orientation.Horizontal

   # Create a List to hold the radio buttons in the first array
   # We'll use this later to make it easier to find which button is checked 
   global horizontal_radio_buttons
   horizontal_radio_buttons = [] 
   
   # Create a first radio button and add to the WrapPanel 
   horizontal_radio_buttons.append(WPFControls.RadioButton())
   horizontal_radio_buttons[0].Content = "Option 1"

   # Set the first radio button to be checked
   # Note the horrible notation required to define a Nullable Boolean True
   horizontal_radio_buttons[0].IsChecked = System.Nullable[System.Boolean](True)
   hWrapPanel.Children.Add(horizontal_radio_buttons[0])
 
   # Create a second radio button and add to the WrapPanel
   horizontal_radio_buttons.append(WPFControls.RadioButton())
   horizontal_radio_buttons[1].Content = "Option 2"
   hWrapPanel.Children.Add(horizontal_radio_buttons[1])
   
   # Create a third radio button and add to the WrapPanel
   horizontal_radio_buttons.append(WPFControls.RadioButton())
   horizontal_radio_buttons[2].Content = "Option 3"
   hWrapPanel.Children.Add(horizontal_radio_buttons[2])

   # Position the WrapPanel in the first row, column of the Grid
   WPFControls.Grid.SetRow(hWrapPanel, 3)
   WPFControls.Grid.SetColumn(hWrapPanel, 0)

   #Add the WrapPanel to the Grid
   my_Grid.Children.Add(meshTextBlock2)
   my_Grid.Children.Add(hWrapPanel)

   # Return the Grid   
   return my_Grid


# Apply button handler (Green check mark)
# This function is called each time the Apply button is clicked
@apex_sdk.errorhandler
def HandleApplyButton():
   
   # Create a Dictionary to store the user defined tool data  
   dictionary = {}
 
   # Find the checked button in radio_button_vertical
   # And store it in the dictionary 
   # Note that this is somewhat safe since only one radio button can be checked
   vertical_checked_content = [x for x in vertical_radio_buttons if x.IsChecked == True][0].Content          
   dictionary["vertical_checked"] = vertical_checked_content

   # Find the checked button in radio_button_horizontal
   # And store it in the dictionary 
   horizontal_checked_content = [x for x in horizontal_radio_buttons if x.IsChecked == True][0].Content          
   dictionary["horizontal_checked"] = horizontal_checked_content
   
   # Call the Apex API script using apex_sdk.runScriptFunction()
   # argument 1 = the fully qualified path name of your API script
   # argument 2 = the name of the function in your API script that will receive the Dictionary
   # argument 3 = the Dictionary containing the UI data
   apex_sdk.runScriptFunction(os.path.join(current_file_path, r"radio_button.py"),
                              "radio_button",
                              dictionary)
