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


def convert_real2(val):
    ax = val.strip()
    if not ax:
        return 0.0
    return float(ax[0:1] + ax[1:].upper().replace('-', 'E-').replace('+', 'E+').replace('EE', 'E'))



def process_ctetra(BoltElList, TopList, cnt1, cpid, eflagtet, eltype, eqDict, inl, lin, matInteg,
                   pSolDict, partName, pidname, tetFace, tetId):
    out_strs=[]
    if len(lin) > 60:
        torder = 2
    else:
        torder = 1
    if eflagtet == 1 or (cpid != int(lin[16:24])):
        cpid = int(lin[16:24])
        intAba = matInteg[pSolDict[cpid][0]]
        if intAba == 'cont':
            elSuffix = 'M'
            out_strs.append('** Modified formulation for contact selected from Material Description\n')
        elif intAba == 'inc':
            elSuffix = 'I'
            out_strs.append('** Improved stress formulation selected from Material Description\n')
        elif intAba == 'std':
            elSuffix = ''
            out_strs.append('** Full integration formulation selected from Material Description\n')
        else:
            elSuffix = ''
            out_strs.append('** Full integration formulation selected since Material Description is missing\n')
        partpid = '%s_PID_%i' % (partName, cpid)
        pidname[partpid] = [cpid, 'sol']
        if len(lin) > 60:
            out_strs.append('*ELEMENT, TYPE=C3D10%s, ELSET=%s\n' % (elSuffix, partpid))
        else:
            out_strs.append('*ELEMENT, TYPE=C3D4, ELSET=%s\n' % (partpid))
        eflagtet = 0
    tId = int(lin[8:16])
    eltype[tId] = 4
    nl = []
    nl.append(int(lin[24:32]))
    nl.append(int(lin[32:40]))
    nl.append(int(lin[40:48]))
    nl.append(int(lin[48:56]))
    g = 0
    while g < 4:
        if nl[g] in TopList:
            nl[g] = eqDict[nl[g]]
            BoltElList.add(tId)
        g = g + 1
    if torder == 2:
        nl.append(int(lin[56:64]))
        nl.append(int(lin[64:72]))
        tId = int(lin[8:16])
        lin = inl[cnt1]
        cnt1 = cnt1 + 1
        nl.append(int(lin[8:16]))
        nl.append(int(lin[16:24]))
        nl.append(int(lin[24:32]))
        nl.append(int(lin[32:40]))
        g = 4
        while g < 10:
            if nl[g] in TopList:
                nl[g] = eqDict[nl[g]]
                BoltElList.add(tId)
            g = g + 1
        out_strs.append('%8i,%8i,%8i,%8i,%8i,%8i,%8i,%8i,%8i,%8i,%8i\n' % (
            tId, nl[0], nl[1], nl[2], nl[3], nl[4], nl[5], nl[6], nl[7], nl[8], nl[9]))
    else:
        out_strs.append( '%8i,%8i,%8i,%8i,%8i\n' % (tId, nl[0], nl[1], nl[2], nl[3]))
    


    tetFace[tId, 1] = [nl[0], nl[2], nl[1]]
    tetFace[tId, 2] = [nl[0], nl[1], nl[3]]
    tetFace[tId, 4] = [nl[0], nl[3], nl[2]]
    tetFace[tId, 3] = [nl[1], nl[2], nl[3]]

    tetId.append(tId)
    return cnt1, cpid, eflagtet, out_strs,tId, nl    
    
def process_chexa(BoltElList, TopList, cnt1, cpid, eflaghex, eltype, eqDict, hexFace, hexId, inl, lin,
                  matInteg, outf, pSolDict, partName, pidname):
# Stefan added try/except need to be taken care of
#    try:
#        eflaghex == 1
#    except:
#        print("Warning eflaghex not defined...setting it to zero")
#        eflaghex = 0
    lin3=inl[cnt1+1]
    if lin3[0:1] == ' ' or lin3[0:1] == '+':
        horder=2
    else :
        horder=1

    out_strs=[]
    if eflaghex == 1 or (cpid != int(lin[16:24])):
        cpid = int(lin[16:24])
        intAba = matInteg[pSolDict[cpid][0]]
        if intAba == 'inc':
            elSuffix = 'I'
            out_strs.append('** Incomapatible modes formulation selected from Material Description\n')
        elif intAba == 'red':
            elSuffix = 'R'
            out_strs.append('** Reduced integration formulation selected from Material Description\n')
        elif intAba == 'std':
            elSuffix = ''
            out_strs.append('** Full integration formulation selected from Material Description\n')
        else:
            elSuffix = ''
            out_strs.append('** Full integration formulation selected since Material Description is missing\n')
        partpid = '%s_PID_%i' % (partName, cpid)
        pidname[partpid] = [cpid, 'sol']
        if horder == 1 :
            tmp = '*ELEMENT, TYPE=C3D8%s, ELSET=%s\n' % (elSuffix, partpid)
        else :
            tmp = '*ELEMENT, TYPE=C3D20%s, ELSET=%s\n' % (elSuffix, partpid)
        out_strs.append(tmp)
        eflaghex = 0
    nl = []
    nl.append(int(lin[24:32]))
    nl.append(int(lin[32:40]))
    nl.append(int(lin[40:48]))
    nl.append(int(lin[48:56]))
    nl.append(int(lin[56:64]))
    nl.append(int(lin[64:72]))
    hId = int(lin[8:16])
    hexId.append(hId)
    eltype[hId] = 6
    lin = inl[cnt1]
    cnt1 = cnt1 + 1
    nl.append(int(lin[8:16]))
    nl.append(int(lin[16:24]))
    g = 0
    while g < 8:
        if nl[g] in TopList:
            nl[g] = eqDict[nl[g]]
            BoltElList.add(hId)
        g = g + 1
    if horder == 2 :
        nl.append(int(lin[24:32]))
        nl.append(int(lin[32:40]))
        nl.append(int(lin[40:48]))
        nl.append(int(lin[48:56]))
        nl.append(int(lin[56:64]))
        nl.append(int(lin[64:72]))
        lin = inl[cnt1]
        cnt1 = cnt1 + 1
        nl.append(int(lin[8:16]))
        nl.append(int(lin[16:24]))
        nl.append(int(lin[24:32]))
        nl.append(int(lin[32:40]))
        nl.append(int(lin[40:48]))
        nl.append(int(lin[48:56]))
        g = 8
        while g < 20:
            if nl[g] in TopList:
                nl[g] = eqDict[nl[g]]
                BoltElList.add(hId)
            g = g + 1
        tmp = '%8i,%8i,%8i,%8i,%8i,%8i,%8i,%8i,%8i,%8i,%8i,\n%8i,%8i,%8i,%8i,%8i,%8i,%8i,%8i,%8i,%8i\n' % (
            hId, nl[0], nl[1], nl[2], nl[3], nl[4], nl[5], nl[6], nl[7], nl[8], nl[9], nl[10], nl[11], 
            nl[16], nl[17], nl[18], nl[19], nl[12], nl[13], nl[14], nl[15]))
    else :
        tmp = '%8i,%8i,%8i,%8i,%8i,%8i,%8i,%8i,%8i\n' % (
        hId, nl[0], nl[1], nl[2], nl[3], nl[4], nl[5], nl[6], nl[7])
    out_strs.append(tmp)
    hexFace[hId, 1] = [nl[0], nl[3], nl[2], nl[1]]
    hexFace[hId, 3] = [nl[0], nl[1], nl[5], nl[4]]
    hexFace[hId, 6] = [nl[0], nl[4], nl[7], nl[3]]
    hexFace[hId, 4] = [nl[1], nl[2], nl[6], nl[5]]
    hexFace[hId, 5] = [nl[2], nl[3], nl[6], nl[7]]
    hexFace[hId, 2] = [nl[4], nl[5], nl[6], nl[7]]
    return cnt1, cpid, hId, out_strs, eflaghex, nl
    
def process_cquad4(cpid, eflagq, eltype, lin, matInteg, pShlDict, partName, pidname):
    out_strs=[]
    if eflagq == 1 or (cpid != int(lin[16:24])):
        cpid = int(lin[16:24])
        intAba = matInteg[pShlDict[cpid][0]]
        if intAba == 'red':
            elSuffix = 'R'
            out_strs.append('** Reduced integration formulation selected from Material Description\n')
        elif intAba == 'std':
            elSuffix = ''
            out_strs.append('** Full integration formulation selected from Material Description\n')
        else:
            elSuffix = ''
            out_strs.append('** Full integration formulation selected since Material Description is missing\n')
        partpid = '%s_PID_%i' % (partName, cpid)
        pidname[partpid] = [cpid, 'shell']
        tmp = '*ELEMENT, TYPE=S4%s, ELSET=%s\n' % (elSuffix, partpid)
        out_strs.append(tmp)
        eflagq = 0
    tmp = '%8i,%8i,%8i,%8i,%8i\n' % (
        int(lin[8:16]), int(lin[24:32]), int(lin[32:40]), int(lin[40:48]), int(lin[48:56]))
    out_strs.append(tmp)
    eltype[int(lin[8:16])] = 1
    return cpid, eflagq, out_strs


