# 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-13   Original code (1.0)                    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

    labelMaterials = dict["labelMaterials"]

    elms = apex.entityCollection()
    for sel in _selected:
        if isinstance(sel, apex.Assembly):
            elms += sel.getElements()
        elif isinstance(sel, apex.Part):
            meshes = sel.getMeshes()
            for mesh in meshes:
                elms += mesh.getElements()
        elif isinstance(sel, apex.mesh.SolidMesh):
            elms += sel.getElements()
        elif isinstance(sel, apex.mesh.SurfaceMesh):
            elms += sel.getElements()
        elif isinstance(sel, apex.mesh.CurveMesh):
            elms += sel.getElements()
        elif isintance(sel, apex.mesh.Element):
            elms.append(sel)

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

    elmData = []
    matls   = {}
    labels  = []
    for elm in elms:
        elmMatl = elm.getMaterial()
        if elmMatl is None:
            matlName = "None"
            rgb = (89, 89, 89)
        else:
            matlName = elmMatl.getName()
            rgb = elmMatl.getColor()

        elmData.append([elm, matlName])
        if matlName in matls.keys(): continue
        colorRGB = apex.ColorRGB(rgb[0], rgb[1], rgb[2])
        matls[matlName] = colorRGB
        labels.append([elm, matlName])

    for e in elmData:
        elm = e[0]
        elm.highlight(matls[e[1]], 2, 3)

    if labelMaterials == "True":
        color = apex.ColorRGB(255, 255, 255)
        for label in labels:
            nodes = label[0].getNodes()
            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[1], textLocation=apex.Coordinate(x, y, z), graphicsFontColor=color, graphicsFontSize=14)


def clearHighlights(dict={}):
    apex.disableShowOutput()
    myModel = apex.currentModel()
    allElms = myModel.getElements()

    for elm in allElms:
        elm.clearHighlight()

    apex.display.clearAllGraphicsText()




