from RFEM.enums import *
from RFEM.initModel import Model, SetAddonStatus, Calculate_all
from RFEM.dataTypes import inf
from RFEM.globalParameter import *
from RFEM.BasicObjects.material import Material
from RFEM.BasicObjects.section import Section
from RFEM.BasicObjects.node import Node
from RFEM.BasicObjects.member import Member
from RFEM.TypesForNodes.nodalSupport import NodalSupport
from RFEM.LoadCasesAndCombinations.staticAnalysisSettings import StaticAnalysisSettings
from RFEM.LoadCasesAndCombinations.loadCase import LoadCase
from RFEM.LoadCasesAndCombinations.designSituation import DesignSituation
from RFEM.LoadCasesAndCombinations.loadCasesAndCombinations import LoadCasesAndCombinations
from RFEM.LoadCasesAndCombinations.loadCombination import LoadCombination
from RFEM.Loads.memberLoad import MemberLoad
from RFEM.Results.resultTables import ResultTables, GetMaxValue
from RFEM.ImportExport.exports import ExportResultTablesToCSV
import os
import sys

Model(True, "Stahlbau_Optimierung")

#GlobalParameter.AddParameter(1, 'Spannweite', 'S', GlobalParameterUnitGroup.LENGTH, GlobalParameterDefinitionType.DEFINITION_TYPE_VALUE, [4])
#span = Model.clientModel.service.get_global_parameter(1).value
span = 4    #

Material(1, "S235")
Section(1, "IPE 160")
Section(2, "IPE 120")

frameDistance = 0       #

while True: 
    first = 0.5 * span
    n = 1
    m = 0
    frameDistance = 0

    for i in range(3):
        Node(n , 0, frameDistance, 0)
        Node(n + 1, span, frameDistance, 0)
        Node(n + 2, 0, frameDistance, -3)
        Node(n + 3, span, frameDistance, -3)
        Node(n + 4, first, frameDistance, -3.5)

        # Stäbe für Stützen
        Member(m + 1, n, n + 2, 0, 1)       # Knoten 1, Knoten 3
        Member(m + 2, n + 1, n + 3, 0, 1)

        # Stäbe für Balken
        Member(m + 3, n + 2, n + 4, 0, 1)        
        Member(m + 4, n + 3, n + 4, 0, 1)

        if i > 0:
            # Stäbe für Längsaussteifung
            Member(m + 5, n - 3, n + 2, 0, 2)
            Member(m + 6, n - 2, n + 3, 0, 2)
            m += 6
        else:
            m += 4
        
        n += 5 
        
        frameDistance -= 3.5

    NodalSupport(1, '1 2 6 7 11 12', NodalSupportType.HINGED)

    LoadCase(2, 'Schnee', [False],ActionCategoryType.ACTION_CATEGORY_SNOW_ICE_LOADS_H_LESS_OR_EQUAL_TO_1000_M_QS)

    MemberLoad(1, 1, '3 4 7 8 13 14', MemberLoadDirection.LOAD_DIRECTION_GLOBAL_Z_OR_USER_DEFINED_W_PROJECTED, 1000)
    MemberLoad(2, 2, '3 4 7 8 13 14', MemberLoadDirection.LOAD_DIRECTION_GLOBAL_Z_OR_USER_DEFINED_W_PROJECTED, 4000)

    DesignSituation(1, DesignSituationType.DESIGN_SITUATION_TYPE_STR_PERMANENT_AND_TRANSIENT_6_10)

    LoadCombination(1, AnalysisType.ANALYSIS_TYPE_STATIC, 1, combination_items=[[1.35, 1, 0, False]])
    LoadCombination(2, AnalysisType.ANALYSIS_TYPE_STATIC, 1, combination_items=[[1.35, 1, 0, False], [1.5, 2, 0, False]])

    Calculate_all()

    deformations = ResultTables.MembersGlobalDeformations(CaseObjectType.E_OBJECT_TYPE_DESIGN_SITUATION, 1,0)
    
    maxDeformation = round(GetMaxValue(deformations, 'displacement_absolute') * 100, 2)

    deformationLimit = (span * 100)/300
    if maxDeformation > deformationLimit:
        print(f"maximale Spannweite: {span - 0.5}")
        print()
        break
    
    print(f"Spannweite: {span} \n max. Verformung: {maxDeformation} cm")
    span += 0.5
        

