# 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


apex.setScriptUnitSystem( unitSystemName = r'''in-slinch-s-lbf''' )

# convert lb/in^3 density to slinches
densityConversion = 1.0/386.09

myModel = apex.currentModel()

myMaterials = []

myMaterials.append({"name":"Aluminum", "type":"ISOTROPIC", "RHO":0.101, "E":10.0E6, "NU":0.3,  "description":"default aluminum", "color":[255,255,0]})
myMaterials.append({"name":"Steel",    "type":"ISOTROPIC", "RHO":0.284, "E":30.0E6, "NU":0.3,  "description":"default steel",    "color":[255,192,0]})
myMaterials.append({"name":"Titanium", "type":"ISOTROPIC", "RHO":0.16,  "E":16.0E6, "NU":0.27, "description":"default Titanium", "color":[255, 0, 0]})

myMaterials.append({"name":"Aluminum 2014-T6", "type":"ISOTROPIC", "RHO":0.101, "E":10.6E6, "NU":0.33, "XT":55.E3, "XC":57.E3, "S":37.E3, "description":"approximate", "color":[247, 150,  70]})
myMaterials.append({"name":"Aluminum 6061-T6", "type":"ISOTROPIC", "RHO":0.101, "E":10.0E6, "NU":0.33, "XT":40.E3, "XC":40.E3, "S":30.E3, "description":"approximate", "color":[75,  172, 198]})
myMaterials.append({"name":"Aluminum 7075-T6", "type":"ISOTROPIC", "RHO":0.101, "E":10.4E6, "NU":0.33, "XT":70.E3, "XC":70.E3, "S":42.E3, "description":"approximate", "color":[128, 100, 162]})

myMaterials.append({"name":"Jones-Glass-Epoxy",    "type":"2D ORTHOTROPIC", "RHO":0.1, "EX":7.8e6, "EY":2.6e6,  "GXY":1.3e6,  "NUXY":0.25, "XT":1.5e5, "XC":1.5e5,  "YT":4.e3,  "YC":2.e4,  "S":6.e3,  "description":"from Mechanics of Composite Materials by Jones", "color":[244, 66, 66]})
myMaterials.append({"name":"Jones-Boron-Epoxy",    "type":"2D ORTHOTROPIC", "RHO":0.1, "EX":3.e7,  "EY":3.e6,   "GXY":1.e6,   "NUXY":0.3,  "XT":2.e5,  "XC":4.e5,   "YT":1.2e4, "YC":4.e4,  "S":1.8e4, "description":"from Mechanics of Composite Materials by Jones", "color":[244, 164, 66]})
myMaterials.append({"name":"Jones-Graphite-Epoxy", "type":"2D ORTHOTROPIC", "RHO":0.1, "EX":3.e7,  "EY":7.5e5,  "GXY":3.75e5, "NUXY":0.25, "XT":1.5e5, "XC":1.5e5,  "YT":6.e3,  "YC":1.7e4, "S":1.e4,  "description":"from Mechanics of Composite Materials by Jones", "color":[209, 244, 66]})

def main():
    numMatlsCreated       = 0
    numIsotropicMatls     = 0
    num2DOrthotropicMatls = 0

    existingMatlNames = []
    existingMatls = apex.catalog.getMaterials([{"path":myModel.getPath()}])
    if existingMatls.len() > 0:
        for matl in existingMatls:
            existingMatlNames.append(matl.getName())
    for matl in myMaterials:
        if matl["name"] in existingMatlNames:
            print("A material with name: "+matl["name"]+" already exists ... skipping")
            continue
        existingMatlNames.append(matl["name"])
        try:
            myMatl = apex.catalog.createMaterial( name = matl.get("name", "Material"), description = matl.get("description", ""), color = matl.get("color", [255, 255, 255]))
            _density = matl.get("RHO", 0.0)
            _density = _density*densityConversion
            myMatl.update(density = _density)
            if matl["type"] == "ISOTROPIC":
                myMatl.update(elasticModulus = matl["E"], poissonRatio = matl["NU"])
                if matl.get("XT") is not None:
                    myMatl.update(failureLimitsIsotropic = {'stressLimitTension' : matl["XT"], },)
                if matl.get("XC") is not None:
                    myMatl.update(failureLimitsIsotropic = {'stressLimitCompression' : matl["XC"], },)
                if matl.get("S") is not None:
                    myMatl.update(failureLimitsIsotropic = {'stressLimitShear' : matl["S"], },)
                numIsotropicMatls += 1
            elif matl["type"] == "2D ORTHOTROPIC":
                myMatl.update(materialType = apex.attribute.MaterialType.Orthotropic)
                myMatl.update(elasticityOrthotropic2D = {'elasticModulsX' : matl["EX"], },)
                myMatl.update(elasticityOrthotropic2D = {'elasticModulsY' : matl["EY"], },)
                myMatl.update(elasticityOrthotropic2D = {'shearModulusXY' : matl["GXY"], },)
                myMatl.update(elasticityOrthotropic2D = {'shearModulusYZ' : matl.get("GYZ", matl["GXY"]), },)
                myMatl.update(elasticityOrthotropic2D = {'shearModulusXZ' : matl.get("GXZ", matl["GXY"]), },)
                myMatl.update(poissonRatio = matl["NUXY"])
                myMatl.update(failureLimitsOrthotropic2D = {'stressLimitTensionX' : matl["XT"], },)
                myMatl.update(failureLimitsOrthotropic2D = {'stressLimitTensionY' : matl["YT"], },)
                myMatl.update(failureLimitsOrthotropic2D = {'stressLimitCompressionX' : matl["XC"], },)
                myMatl.update(failureLimitsOrthotropic2D = {'stressLimitCompressionY' : matl["YC"], },)
                myMatl.update(failureLimitsOrthotropic2D = {'stressLimitInplaneShear' : matl["S"], },)
                _density = matl.get("RHO", 0.0)
                _density = _density*densityConversion
                myMatl.update(density = _density)
                num2DOrthotropicMatls += 1
            else:
                continue
            print(matl["type"]+" material created: "+matl["name"])
            numMatlsCreated += 1
        except:
            print("Unable to create material: "+matl["name"])

    print("====================================")
    print("Number of materials defined in script: "+str(len(myMaterials)))
    print("Number of Materials Created: "+str(numMatlsCreated))
    print("Number of Isotropic Materials Created: "+str(numIsotropicMatls))
    print("Number of 2D Orthotropic Materials Created: "+str(num2DOrthotropicMatls))

main()

