# Created by Andrzej Lach @ 2021
# https://github.com/AndrzejLach89

from aqa.math import *
from varmain.primitiv import * 
from varmain.var_basic import * 
from varmain.custom import *




@activate(Group="Support",
 TooltipShort="Profil",
 TooltipLong="Profil", 
 LengthUnit="mm",
 Ports=1)
@group("MainDimensions")
@param(L11=LENGTH, TooltipShort="Profile 1 length", Ask4Dist=True)
@param(L12=LENGTH, TooltipShort="Profile 1 length", Ask4Dist=True)
@param(Dz=LENGTH, TooltipLong="Pipe OD")



def PROFIL(s, Dz=114.300, L11=100.000, L12=100.000, ID="BIP_PROFIL", **kw):
    # standard pipe Dz list
    pipeDiameterList = {
        21.300: 15,
        26.900: 20,
        33.700: 25,
        42.400: 32,
        48.300: 40,
        60.300: 50,
        76.100: 65,
        88.900: 80,
        114.300: 100,
        139.700: 125,
        168.300: 150,
        219.100: 200,
        273.000: 250,
        323.900: 300,
        355.600: 350,
        406.400: 400,
        457.200: 450,
        508.000: 500,
        609.600: 600,
        711.200: 700
        }
    # standard bolt diameter list
    boltDiameterList = {
        15: 8.000,
        20: 8.000,
        25: 8.000,
        32: 8.000,
        40: 12.000,
        50: 12.000,
        65: 12.000,
        80: 12.000,
        100: 16.000,
        125: 16.000,
        150: 16.000,
        200: 20.000,
        250: 20.000,
        300: 24.000,
        350: 24.000,
        400: 24.000,
        450: 24.000,
        500: 24.000,
        600: 24.000,
        700: 24.000
        }
    supportSizeList = {
        15: {'C': 39.000, 'E': 30.000, 'B': 8.000},
        20: {'C': 42.000, 'E': 35.000, 'B': 8.000},
        25: {'C': 45.000, 'E': 42.000, 'B': 8.000},
        32: {'C': 60.000, 'E': 53.000, 'B': 8.000},
        40: {'C': 75.000, 'E': 61.000, 'B': 12.000},
        50: {'C': 81.000, 'E': 73.000, 'B': 12.000},
        65: {'C': 89.000, 'E': 89.000, 'B': 12.000},
        80: {'C': 95.000, 'E': 101.000, 'B': 12.000},
        100: {'C': 115.000, 'E': 131.000, 'B': 16.000},
        125: {'C': 127.000, 'E': 156.000, 'B': 16.000},
        150: {'C': 141.000, 'E': 184.000, 'B': 16.000},
        200: {'C': 174.000, 'E': 240.000, 'B': 20.000},
        250: {'C': 201.000, 'E': 294.000, 'B': 20.000},
        300: {'C': 233.000, 'E': 348.000, 'B': 24.000},
        350: {'C': 249.000, 'E': 380.000, 'B': 24.000},
        400: {'C': 275.000, 'E': 432.000, 'B': 24.000},
        450: {'C': 301.000, 'E': 484.000, 'B': 24.000},
        500: {'C': 326.000, 'E': 534.000, 'B': 24.000},
        600: {'C': 377.000, 'E': 636.000, 'B': 24.000},
        700: {'C': 430.000, 'E': 737.000, 'B': 24.000}
        }
    nutSizeList = {
        8: {'h': 6.500, 'd': 13.000, 'x': 7.5056},
        12: {'h': 10.000, 'd': 18.000, 'x': 10.3923},
        16: {'h': 13.000, 'd': 24.000, 'x': 13.8564},
        20: {'h': 16.000, 'd': 30.000, 'x': 17.3205},
        24: {'h': 19.000, 'd': 36.000, 'x': 20.7846}
        }
    profileList = {
        15: {'L': 50.000, 'S': 5.000, 'O': 50.000, 'Lm': 1000.000},
        20: {'L': 50.000, 'S': 5.000, 'O': 50.000, 'Lm': 1000.000},
        25: {'L': 50.000, 'S': 5.000, 'O': 50.000, 'Lm': 1000.000},
        32: {'L': 50.000, 'S': 5.000, 'O': 60.000, 'Lm': 1000.000},
        40: {'L': 50.000, 'S': 5.000, 'O': 60.000, 'Lm': 1000.000},
        50: {'L': 50.000, 'S': 5.000, 'O': 60.000, 'Lm': 1000.000},
        65: {'L': 50.000, 'S': 5.000, 'O': 70.000, 'Lm': 1000.000},
        80: {'L': 50.000, 'S': 5.000, 'O': 70.000, 'Lm': 1000.000},
        100: {'L': 70.000, 'S': 7.000, 'O': 80.000, 'Lm': 1250.000},
        125: {'L': 70.000, 'S': 7.000, 'O': 80.000, 'Lm': 1250.000},
        150: {'L': 70.000, 'S': 7.000, 'O': 80.000, 'Lm': 1250.000},
        200: {'L': 100.000, 'S': 10.000, 'O': 100.000, 'Lm': 1800.000},
        250: {'L': 100.000, 'S': 10.000, 'O': 100.000, 'Lm': 1800.000},
        300: {'L': 100.000, 'S': 10.000, 'O': 100.000, 'Lm': 1800.000},
        350: {'L': 100.000, 'S': 10.000, 'O': 100.000, 'Lm': 1800.000},
        400: {'L': 100.000, 'S': 10.000, 'O': 100.000, 'Lm': 1800.000},
        450: {'L': 100.000, 'S': 10.000, 'O': 100.000, 'Lm': 1800.000},
        500: {'L': 120.000, 'S': 12.000, 'O': 100.000, 'Lm': 1800.000},
        600: {'L': 120.000, 'S': 12.000, 'O': 100.000, 'Lm': 1800.000},
        700: {'L': 120.000, 'S': 12.000, 'O': 100.000, 'Lm': 1800.000}
        }
    # get DN
    if Dz not in pipeDiameterList:
        findClosest= {}
        for element in pipeDiameterList:
            findClosest[element] = abs(Dz - element)
        DN = pipeDiameterList[min(findClosest, key=findClosest.get)]
        for element in pipeDiameterList:
            if pipeDiameterList[element] == DN:
                Dz = element
    else:
        DN = pipeDiameterList[Dz]
    # get bolt
    boltDiameter = boltDiameterList[DN]
    # get clamp sizes
    sizes = supportSizeList[DN]
    C = sizes['C']
    E = sizes['E']
    boltYOffset = E/2.000
    
    # claculations for profiles
    profile1Type = profile1List[DN]
    profile1L = profile1Type['L']
    profile1S = profile1Type['S']
    profileOffest = profile1Type['O']
    profile1Lm = profile1Type['Lm']
    
    profile1Cut = profile1L - profile1S
    profile1Length = E + 2.000 * boltDiameter + 10.000
    if L11 < profile1Length/2.000:
        L11 = profile1Length/2.000
    if L12 < profile1Length/2.000:
        L12 = profile1Length/2.000

    profile1ZOffset = -(Dz+profile1L)/2.000
    cut1XOffset = profile1S/2.000
    cut1ZOffset = -(Dz + profile1Cut)/2.000 - profile1S
    
    uBoltOffset = supportSizeList[DN]['E']/2.000 + supportSizeList[DN]['B']/2.000 + profileOffest/2.000
    profile1YOffset = E/2.000 + boltDiameter/2.000 + profile1L + 15.000
    
    cutX1Offset = profile1S/2.000
    cutZ1Offset = -(Dz + profile1Cut)/2.000 - profile1S
    
    # add profile 11
    o01 = BOX(s, L=L11, W=profile1L, H=profile1L).translate((0.000, 0.000, profile1ZOffset))
    o02 = BOX(s, L=L11, W=profile1L, H=profile1L).translate((profile1S, 0.000, profile1ZOffset-profile1S))
    o01.subtractFrom(o02)
    o02.erase()
    o01.translate((0.000, -L11/2.000, 0.000))
    s.setLinearDimension('L11',(0.000, 0.000, profile1ZOffset+profile1L/2.000), (0.000, -L11, profile1ZOffset+profile1L/2.000))
    
    # add profile 12
    if L12 < profile1YOffset:
        L12 = profile1YOffset
    o1 = BOX(s, L=L12, W=profile1L, H=profile1L).translate((0.000, 0.000, profile1ZOffset))
    o2 = BOX(s, L=L12, W=profile1L, H=profile1L).translate((profile1S, 0.000, profile1ZOffset-profile1S))
    o1.subtractFrom(o2)
    o2.erase()
    o1.translate((0.000, L12/2.000, 0.000))
    s.setLinearDimension('L12',(0.000, 0.000, profile1ZOffset+profile1L/2.000), (0.000, L12, profile1ZOffset+profile1L/2.000))
    
    # join profiles
    o1.uniteWith(o01)
    o01.erase()
    
    # set port point
    s.setPoint((0.000, 0.000, 0.000), (-1.000, 0.000, 0.000))

    


