# coding: utf-8
#
# 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-12   Original code                          Larry Pearce

import apex
import time


def main(dict={}):
    apex.disableShowOutput()

    myModel = apex.currentModel()

    _selected = apex.selection.getCurrentSelection()

    if _selected.len() == 0:
        apex.session.displayStatusMessage("Nothing selected?")
        time.sleep(5)
        #print("Nothing selected?")
        return

    dataToDisplay = dict["dataToDisplay"]
    topo = []


    if dataToDisplay == "Section" or dataToDisplay == "Thickness":
        topo.append(apex.mesh.ElementTopology.Tria3)
        topo.append(apex.mesh.ElementTopology.Quad4)
    elif dataToDisplay == "Span" or dataToDisplay[0:5] == "Shape":
        topo.append(apex.mesh.ElementTopology.Bar2)
    else:
        topo.append(apex.mesh.ElementTopology.Bar2)
        topo.append(apex.mesh.ElementTopology.Tria3)
        topo.append(apex.mesh.ElementTopology.Quad4)
        topo.append(apex.mesh.ElementTopology.Tetra4)
        topo.append(apex.mesh.ElementTopology.Tetra10)
        topo.append(apex.mesh.ElementTopology.Penta6)
        topo.append(apex.mesh.ElementTopology.Penta15)
        topo.append(apex.mesh.ElementTopology.Hexa8)
        topo.append(apex.mesh.ElementTopology.Hexa20)

    elms = apex.entityCollection()
    for sel in _selected:
        if sel.getEntityType() == apex.EntityType.Element:
            if sel.getTopology() in topo:
                elms.append(sel)

    if elms.len() == 0:
        apex.session.displayStatusMessage("No elements of the proper topology selected?")
        time.sleep(5)
        #print("No elements selected?")
        return

    elmData = []
    for elm in elms:
        if dataToDisplay == "Material":
            if elm.getMaterial() is None:
                name = "None"
            else:
                name = elm.getMaterial().getName()
        elif dataToDisplay == "Section":
            if elm.getSection() is None:
                name = "None"
            else:
                name = elm.getSection().getName()
        elif dataToDisplay == "Thickness":
            if elm.getSection() == "None":
                name = "-NA-"
            else:
                thick = elm.getSection().getThickness()
                name = "%7.4f" % thick
        elif dataToDisplay == "Span":
            if elm.getSpan() is None:
                name = "None"
            else:
                name = elm.getSpan().getName()
        elif dataToDisplay == "Shape Name":
            if elm.getSpan() is None:
                name = "None"
            else:
                name = elm.getSpan().getStationAShape().getName()
        elif dataToDisplay == "Shape Area":
            if elm.getSpan() is None:
                name = "-NA-"
            else:
                name = "%7.4f" % elm.getSpan().getStationAShape().getBeamShapeArea()
        elif dataToDisplay == "Elm Topology":
            topo = elm.getTopology()
            if topo == 0:
                toponame = "Bar2"
            if topo == 1:
                toponame = "Tria3"
            if topo == 2:
                toponame = "Tria6"
            if topo == 3:
                toponame = "Quad4"
            if topo == 4:
                toponame = "Quad8"
            if topo == 5:
                toponame = "Tetra4"
            if topo == 6:
                toponame = "Tetra10"
            if topo == 7:
                toponame = "Penta6"
            if topo == 8:
                toponame = "Penta15"
            if topo == 9:
                toponame = "Hexa8"
            if topo == 10:
                toponame = "Hexa20"
            name = toponame
        elif dataToDisplay == "Elm Nodes":
            nodeids = elm.getNodeIds()
            name = str(nodeids)

        elmData.append([elm, elm.getNodes(), name])

    color = apex.ColorRGB(255, 255, 255)
    for data in elmData:
        elm   = data[0]
        nodes = data[1]
        label = data[2]
        x = y = z = 0.0
        for node in nodes:
            x += node.getX()
            y += node.getY()
            z += node.getZ()
        x = x/nodes.len()
        y = y/nodes.len()
        z = z/nodes.len()

        apex.display.displayText(text=label, textLocation=apex.Coordinate(x, y, z), graphicsFontColor=color, graphicsFontSize=12)


def clearLabels(dict={}):
    apex.disableShowOutput()
    myModel = apex.currentModel()

    apex.display.clearAllGraphicsText()


