#
# 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
# Display Node Element Counts 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\Robot_48.png")
   my_toolProperty.TitleText = "Node Element Counts"

   # Define UI
   my_toolProperty.ToolPropertyContent = getCustomToolPropertyContent()

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

   # Define PickFilterList
   my_toolProperty.ShowPickChoice = True
   my_toolProperty.PickFilterList = setPickFilterList()
  
   return my_toolProperty


# Set PickFilters
def setPickFilterList():
   
    # Create an empty List of strings
    pickChoices = System.Collections.Generic.List[System.String]()	
	
    # Exclusive picking and visibility picking	
    pickChoices.Add(apex_sdk.PickFilterTypes.ExclusivePicking)
    pickChoices.Add(apex_sdk.PickFilterTypes.VisibilityPicking)
	
    # Add Types
    pickChoices.Add(apex_sdk.PickFilterTypes.Assembly)
    pickChoices.Add(apex_sdk.PickFilterTypes.Part)
    pickChoices.Add(apex_sdk.PickFilterTypes.SolidMesh)
    pickChoices.Add(apex_sdk.PickFilterTypes.SurfaceMesh)
    pickChoices.Add(apex_sdk.PickFilterTypes.CurveMesh)
	
    # Return the pick filter list
    return pickChoices      


# Define Layout and Components
def getCustomToolPropertyContent():

   # Create a Grid
   my_Grid = WPFControls.Grid()
   
   # Add 2 Rows and Two Columns
   my_Grid.RowDefinitions.Add(WPFControls.RowDefinition())
   my_Grid.RowDefinitions.Add(WPFControls.RowDefinition())

   my_Grid.ColumnDefinitions.Add(WPFControls.ColumnDefinition())
   my_Grid.ColumnDefinitions.Add(WPFControls.ColumnDefinition())

   # Row 0
   # Create a label (TextBlock)
   # Set its Text value
   # Assign it to Row 0, Column 0
   nodeCountTextBlock = WPFControls.TextBlock()
   nodeCountTextBlock.Text = "Node Count:"
   WPFControls.Grid.SetRow(nodeCountTextBlock, 0)
   WPFControls.Grid.SetColumn(nodeCountTextBlock, 0)

   # Create an empty input TextBox assign it to Row 0, Column 1
   global nodeCountTextBox
   nodeCountTextBox = WPFControls.TextBox()
   WPFControls.Grid.SetRow(nodeCountTextBox, 0)
   WPFControls.Grid.SetColumn(nodeCountTextBox, 1)

   # Add the Row 0 controls to the Grid
   my_Grid.Children.Add(nodeCountTextBlock)
   my_Grid.Children.Add(nodeCountTextBox)

   # Row 1
   # Create a label
   elementCountTextBlock = WPFControls.TextBlock()
   elementCountTextBlock.Text = "Element Count:"
   WPFControls.Grid.SetRow(elementCountTextBlock, 1)
   WPFControls.Grid.SetColumn(elementCountTextBlock, 0)
   
   # Create an empty input TextBox assign it to Row 0, Column 1
   global elementCountTextBox
   elementCountTextBox = WPFControls.TextBox()
   WPFControls.Grid.SetRow(elementCountTextBox, 1)
   WPFControls.Grid.SetColumn(elementCountTextBox, 1) 
   
   # Add the Row 1 controls to the Grid
   my_Grid.Children.Add(elementCountTextBlock)
   my_Grid.Children.Add(elementCountTextBox)

   # 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():
      
   # Call you Apex API script using the runScriptFunction() method
   # 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 name of the UI Function that handle data returned by the API script function
   apex_sdk.runScriptFunction(os.path.join(current_file_path, r"display_node_element_counts.py"),
                              "get_node_element_counts",
                              callback=UpdateEntityCounts)

#This function receives data from the Script API function and updates the Tool UI 
@apex_sdk.errorhandler
def UpdateEntityCounts():
   ret_dict = apex_sdk.getScriptFunctionReturnData()
   nodeCountTextBox.Text = str(ret_dict["num_nodes"])
   elementCountTextBox.Text = str(ret_dict["num_elements"])
