# 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                          Larry Pearce

import apex


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

    myModel = apex.currentModel()

    _selected = apex.selection.getCurrentSelection()
    if _selected.len() == 0:
        parts = myModel.getParts(True)
    else:
        parts = apex.entityCollection()
        for sel in _selected:
            if sel.asPart() is not None :
                parts.append(sel)

    numElms      = 0
    numSolidElms = 0
    numShellElms = 0
    numHexElms   = 0
    numBeamElms  = 0
    numNodes     = 0
    for part in parts:
        try:
            meshBodies = part.getMeshes()
            for mesh in meshBodies:
                elms = mesh.getElements()
                nodes = mesh.getNodes()
                numElms  += elms.len()
                numNodes += nodes.len()
                try:
                    elms = mesh.asSolidMesh().getElements()
                except:
                    try:
                        elms = mesh.asHexMesh().getElements()
                    except:
                        try:
                            elms = mesh.asSurfaceMesh().getElements()
                        except:
                            try:
                                elms = mesh.asCurveMesh().getElements()
                            except:
                                k = 1
                            else:
                                numBeamElms += elms.len()
                        else:
                            numShellElms += elms.len()
                    else:
                        numHexElms += elms.len()
                else:
                    numSolidElms += elms.len()
        except:
            print("Entity count omitted for Part: "+part.getFullName()[27:])

    print(" ")
    print("Elements: "+str(numElms))
    print("Nodes: "+str(numNodes))
    print(" ")
    print("Beam Elements: "+str(numBeamElms))
    print("Shell Elements: "+str(numShellElms))
    print("Solid (Tet) Elements: "+str(numSolidElms))
    print("Hex, Wedge Elements: "+str(numHexElms))

    ret_dict = {}
    ret_dict["num_nodes"]         = numNodes
    ret_dict["num_elements"]      = numElms
    ret_dict["beam_elements"]     = numBeamElms
    ret_dict["shell_elements"]    = numShellElms
    ret_dict["tet_elements"]      = numSolidElms
    ret_dict["hexwedge_elements"] = numHexElms

    return ret_dict



