#
# 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.
#
# Author: Larry Pearce
# Revised by: Larry Pearce
# Revision History
# Date         Change                                 By
# 2020-10-13   Original code                          Larry Pearce


import os
import apex_sdk

import System
import System.Windows.Controls as WPFControls


def getUIContent():

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

    my_toolProperty.TitleText = "Node/Element Counts"

    file_path = os.path.dirname(os.path.realpath(__file__))
    my_toolProperty.TitleImageUriString = os.path.join(os.path.dirname(file_path), r"res\auCountElms.png")

    my_toolProperty.WorkFlowInstructions = """<HTML><BODY><P STYLE=\"font-size:10;\"><SPAN>Counts elements/nodes in selected Parts</SPAN></P>
    <P><SPAN> </SPAN></P>
    <P><SPAN>Counts all nodes/elements in Apex Mesh Bodies in the selected Parts.  If no Parts are selected, all Parts are used.  
             Nodes and elements in Connectors, Point Masses, etc. are not considered.  Only nodes/elements contained in Apex Mesh Bodies are considered.</SPAN></P>
    <P><SPAN> </SPAN></P>
    <P STYLE=\"font-size:10;\"><SPAN>Send questions, comments and Elvis sightings to larry.pearce@mscsoftware.com</SPAN></P></BODY></HTML>"""

    my_toolProperty.AppliedCommand = apex_sdk.ActionCommand(System.Action(HandleApplyButton))

    my_toolProperty.ShowPickChoice = True
    my_toolProperty.PickFilterList = setPickFilterList()

    return my_toolProperty


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)

    #Part
    #pickChoices.Add(apex_sdk.PickFilterTypes.Assembly)
    pickChoices.Add(apex_sdk.PickFilterTypes.Part)

    #Return the pick filter list
    return pickChoices


def getCustomToolPropertyContent():

    my_Grid = WPFControls.Grid()

    createLayout(my_Grid, 8, 2)

    global numNodesTextBox
    global numElmsTextBox
    global numBeamElmsTextBox
    global numShellElmsTextBox
    global numTetElmsTextBox
    global numHexElmsTextBox

    iRow = iCol = 0
    numNodesTextBox = createDataBox(my_Grid, "Nodes:", "", iRow, iCol)

    iRow += 1
    numElmsTextBox = createDataBox(my_Grid, "Elements:", "", iRow, iCol)

    iRow += 1
    numBeamElmsTextBox = createDataBox(my_Grid, "Beam Elms:", "", iRow, iCol)

    iRow += 1
    numShellElmsTextBox = createDataBox(my_Grid, "Shell Elms:", "", iRow, iCol)

    iRow += 1
    numTetElmsTextBox = createDataBox(my_Grid, "Tet Elms:", "", iRow, iCol)

    iRow += 1
    numHexElmsTextBox = createDataBox(my_Grid, "Hex/Wedge Elms:", "", iRow, iCol)



    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 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


@apex_sdk.errorhandler
def HandleApplyButton():

    file_path = os.path.dirname(os.path.realpath(__file__))
    script_path= os.path.join(file_path, 'auCountElms.py')

    apex_sdk.runScriptFunction(script_path, "main", callback=UpdateEntityCounts)

#	apex_sdk.runScriptFunction(r"C:\users\pearce\documents\MSC_Apex Workspace\UserDefinedTools\count_nodes_elements.py", 
#                              "count_nodes_elements", 
#                              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()
    numNodesTextBox.Text = str(ret_dict["num_nodes"])
    numElmsTextBox.Text  = str(ret_dict["num_elements"])

    numBeamElmsTextBox.Text  = str(ret_dict["beam_elements"])
    numShellElmsTextBox.Text  = str(ret_dict["shell_elements"])
    numTetElmsTextBox.Text  = str(ret_dict["tet_elements"])
    numHexElmsTextBox.Text  = str(ret_dict["hexwedge_elements"])

