# 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 getIdRange(ents):
    ids = []
    minId = maxId = 0
    for ent in ents:
        id = int(ent.getId())
        ids.append(id)
    if len(ids) > 0:
        minId = min(ids)
        maxId = max(ids)
    return [minId, maxId]


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

    ret_dict = {}
    ret_dict["None"] = ["None", [0, 0], [0, 0]]

    myModel = apex.currentModel()

    parts = apex.entityCollection()
    _selected = apex.selection.getCurrentSelection()
    for sel in _selected:
        if sel.getEntityType() == apex.EntityType.Part:
            parts.append(sel)

    #parts = myModel.getParts(True)

    if parts.len() == 0:
        parts = myModel.getParts(True)

    for part in parts:
        data = []
        data.append(part.getName())

        print("** Part: "+part.getName())
        meshBodies = part.getMeshes()

        print("Mesh Bodies: "+str(meshBodies.len()))
        if meshBodies.len() < 1: continue

        totalNodes = 0
        minId = 999999999
        maxId = 0
        for meshBody in meshBodies:
            nodes = meshBody.getNodes()
            numNodes = nodes.len()
            totalNodes += numNodes
            if numNodes > 0:
                idRange = getIdRange(nodes)
                if idRange[0] < minId: minId = idRange[0]
                if idRange[1] > maxId: maxId = idRange[1]
        if totalNodes == 0:
            minId = maxId = 0

        print("Node min ID: "+str(minId))
        print("Node max ID: "+str(maxId))

        data.append([minId, maxId])

        totalElms = 0
        minId = 99999999
        maxId = 0
        for meshBody in meshBodies:
            elms = meshBody.getElements()
            numElms = elms.len()
            totalElms += numElms
            if numElms > 0:
                idRange = getIdRange(elms)
                if idRange[0] < minId: minId = idRange[0]
                if idRange[1] > maxId: maxId = idRange[1]
        if totalElms == 0:
            minId = maxId = 0

        print("Elm min ID: "+str(minId))
        print("Elm max ID: "+str(maxId))

        data.append([minId, maxId])

        ret_dict[part.getFullName()] = data

    return ret_dict


