from PyQt5.QtWidgets import QDialog, QWidget, QLabel, QLineEdit, QPushButton, QComboBox, QGridLayout

class Dialog(QDialog):
    def __init__(self):
        super().__init__()
        self.initUI()


    def initUI(self):
        self.widget = QDialog()
        self.layout = QGridLayout()

        # Fenstereinstellungen
        self.setGeometry(800,500,350,220) # (Startknoten(Pixel), Abmessungen (Pixel))
        self.setWindowTitle("PyQt5 Example")       

        # Label/Text erstellen
        self.labelSpanNumber = QLabel("Anzahl der Felder: ")        
        self.labelSpan = QLabel("Spannweite: ")        
        self.LabelSection = QLabel("Querschnitt: ")
        
        # Eingabefelder erstellen
        self.spanNumber = QLineEdit(self)        
        self.span = QLineEdit(self)
        
        #Kombobox zur Querschnittsauswahl
        self.section = QComboBox(self)
        self.section.addItem("HEA 200")
        self.section.addItem("HEA 220")
        self.section.addItem("HEA 240")
        self.section.addItem("HEA 260")

        # Button zum Starten der Modellerstellung
        self.buttonStart = QPushButton("Start")
        self.buttonStart.clicked.connect(self.buttonStart_clicked)
        
        #Hinzufügen der Komponenten zum Fenster
        self.layout.addWidget(self.labelSpanNumber, 0, 0)
        self.layout.addWidget(self.labelSpan, 1, 0)
        self.layout.addWidget(self.LabelSection, 2, 0)
        self.layout.addWidget(self.spanNumber, 0, 1)
        self.layout.addWidget(self.span, 1, 1)
        self.layout.addWidget(self.section, 2, 1)
        self.layout.addWidget(self.buttonStart, 4, 0, 1, -1) # (item, Zeile, Spalte, rowSpan, columnSpan) -> -1 = bis nach rechts
     
        # Layout aktiv setzen
        self.setLayout(self.layout)       

    def buttonStart_clicked(self):
        self.spanNumber = int(self.spanNumber.text())
        self.span = float(self.span.text())
        self.section = str(self.section.currentText())
        
        self.accept()
    
    def get_spanNumber(self):
        return self.spanNumber
    
    def get_span(self):        
        return self.span  
    
    def get_section(self):
        return self.section
    
    
def getValues():       
    # GUI initialisieren
    dialog = Dialog()
    dialog.exec()

    spanNumber = dialog.get_spanNumber()
    span = dialog.get_span()
    section = dialog.get_section()

    return spanNumber, span, section

spanNumber, span, section = getValues()

try:
    materials.create(1, user_defined_name_enabled=False, name="S235 | CYS EN 1993-1-1:2009-03")
    sections.create_standardized_steel(1, section)

    nodes.create_standard(1)
    nodal_supports.create(1, user_defined_name_enabled=True, name="Hinged", nodes="1", spring=[inf, inf, inf], rotational_restraint=[inf, 0, inf])
   
    nodeNo = 2
    memberNo = 1

    nodesList = []
    supportedNodes = []
    memberList = []
    nodesList.append(str(1))

    for i in range(spanNumber):
        nodes.create_standard(nodeNo, (i+1) * span, 0, 0)
        nodesList.append(str(nodeNo))
        supportedNodes.append(str(nodeNo))   

        nodeStr = ",".join(nodesList)
        
        lines.create_polyline(i + 1, nodeStr)
        lineStr = str(i + 1)
        
        members.create_beam(memberNo, line= lineStr)
        memberList.append(str(memberNo))

        nodesList = []
        nodesList.append(str(nodeNo))

        nodeNo += 1
        memberNo += 1
    
    supportNodeStr = ",".join(supportedNodes)       
    nodal_supports.create(2, nodes=supportNodeStr, spring=[0, inf, inf], rotational_restraint=[0, 0, inf])    
except: 
    print("Error setting geometry!")

try:
    static_analysis_settings.create(1)
    load_cases.create(1, name="Self-weight", static_analysis_settings="SA1")
    load_cases.create(2, name="Schnee", static_analysis_settings="SA1", action_category="ACTION_CATEGORY_SNOW_ICE_LOADS_H_LESS_OR_EQUAL_TO_1000_M_QS", comment="Comment")
    
    memberStr = ",".join(memberList)
    
    load_cases[2].member_loads.create_force(1, memberStr, load_case=2, load_direction="Z_P (W_P )", magnitude=2000)

except:
    print("Error setting load cases and loads")


