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

import apex
from apex.construct import Point3D, Point2D

import math


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

    _selected = apex.selection.getCurrentSelection()

    #allParts = myModel.getParts(True)

    allParts = _selected

    # for the moment, assume all elements in a single Part have the same material
    partData = []
    totalVolume = 0.0
    totalMass   = 0.0
    for part in allParts:
        print("Part: "+part.getName())
        data = [part.getName(), 0.0, 0.0, 0, 0, 0] # [part name, part volume, part mass, density, # mesh bodies, # solids]
        solids = part.getSolids()
        data[5] = solids.len()
        if solids.len() == 0:
            partData.append(data)
            continue

        partVolume = 0.0
        for solid in solids:
            partVolume += solid.getVolume()
        data[1] = partVolume
        totalVolume += partVolume

        meshBodies = part.getMeshes()
        data[4] = meshBodies.len()
        if meshBodies.len() == 0:
            partData.append(data)
            continue

        density = 0.0
        for mesh in meshBodies:
            elms = mesh.getElements()
            if elms.len() == 0: continue
            matl = elms[0].getMaterial() # get the material for one of the elements in a mesh body
            if matl == None: continue
            #refs = matl.getReferences()
            #print(refs.len())
            #for ref in refs:
            #	print(ref.getEntityType())
            #	print(apex.EntityType.Part)

            print(matl.getName())
            try:
                density = matl.getDensity()
            except:
                density = 0.0
            break
        print(" Using density: %7.4g" % (density))
        if density == 0.0:
            data[3] = 0
            partData.append(data)
            continue

        data[3] = 1
        partMass = 0.0
        for solid in solids:
            partMass += solid.getMassNominal(density = density)

        data[2] = partMass
        partData.append(data)

        totalMass   += partMass

    print(" ")
    print(" ")
    print("** SUMMARY BY PART **")

    retDict = {}

    for data in partData:
        print("Part: "+data[0])
        print(" Volume: %7.4g" % (data[1]))
        print(" Mass:   %7.4g" % (data[2]))

    retDict["partData"] =  partData
    print(" ")
    print("Total volume for all selected parts: %7.4g" % (totalVolume))
    print("Total mass for all selected parts:   %7.4g" % (totalMass))

    retDict["totalData"] = ["Total Selection", totalVolume, totalMass, "", "", ""]

    return retDict





