How to handle input data from a WPF window?

Have a look at this version of your script.
Chatgpt is not good at generating stuff from scratch but decent when it is about fixing code. Too little time for me to take the time to understand the whole thing.

you should try erik fritz course about wpf to get you started properly


# -*- coding: UTF-8 -*-
from pyrevit import forms
import wpf, os, clr
clr.AddReference("System")
from System.Windows import Window
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *
from pyrevit.forms import WPFWindow

doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument
# get the path for the script
PATH_SCRIPT = os.path.dirname(__file__)

#get the default wall type within the document
wall_Type_Id = doc.GetDefaultElementTypeId(ElementTypeGroup.WallType)
def_wall_Type = doc.GetElement(wall_Type_Id)

#get the default floor type within the document
floor_Type_Id = doc.GetDefaultElementTypeId(ElementTypeGroup.FloorType)
def_floor_Type = doc.GetElement(floor_Type_Id)

#collecting levels
levels = FilteredElementCollector(doc).OfCategory(
    BuiltInCategory.OST_Levels).WhereElementIsNotElementType().ToElements()
#collecting materials
materials = FilteredElementCollector(doc).WherePasses(ElementCategoryFilter(BuiltInCategory.OST_Materials))

# get the base level 0.00
level = [l for l in levels if
         l.LookupParameter('Elévation').AsValueString() == '0.00' or l.LookupParameter('Elévation').AsDouble() == 0]

if len(level) > 0:
    lvl = level[0]
    lvl_Id = lvl.Id

# the form class
class ModalForm(Window):
    def __init__(self):
        path_xaml_file = os.path.join(PATH_SCRIPT, 'cofrage_modifié.xaml')
        wpf.LoadComponent(self, path_xaml_file)
        self.ShowDialog()

    def collect_input_data(self):
        """Collect and convert input data from the UI"""
        try:
            A = UnitUtils.ConvertToInternalUnits(float(self.A_value.Text), UnitTypeId.Meters)
            B = UnitUtils.ConvertToInternalUnits(float(self.B_value.Text), UnitTypeId.Meters)
            H = UnitUtils.ConvertToInternalUnits(float(self.H_value.Text), UnitTypeId.Meters)
            ep = UnitUtils.ConvertToInternalUnits(float(self.ep_value.Text), UnitTypeId.Meters)
            contrainte = float(self.contrainte_value.Text)
            return A, B, H, ep, contrainte
        except ValueError:
            forms.alert("Invalid input. Please enter numeric values.")
            return None

    def get_or_create_concrete_material(self, contrainte):
        """Retrieve or create the required concrete material"""
        material = [i for i in materials if i.Name == "Béton - Coulé sur place - Béton{}".format(contrainte)]
        if len(material) > 0:
            return material[0].Id
        else:
            with Transaction(doc, 'Get/create concrete material') as t:
                t.Start()
                m_Id = Material.Create(doc, "Béton - Coulé sur place - Béton{}".format(contrainte))
                mat = doc.GetElement(m_Id)
                mat.MaterialClass = "Béton"
                mat.MaterialCategory = "Béton"
                mat.Color = Color(192, 192, 192)
                mat.Shininess = 128
                mat.Smoothness = 50
                asset = StructuralAsset("ConcreteStructuralAsset", StructuralAssetClass.Concrete)
                asset.ConcreteCompression = UnitUtils.ConvertToInternalUnits(float(contrainte), UnitTypeId.Megapascals)
                asset.Density = UnitUtils.ConvertToInternalUnits(2500, UnitTypeId.KilogramsPerCubicMeter)
                asset.SetPoissonRatio(0.2)
                asset.SetYoungModulus(UnitUtils.ConvertToInternalUnits(11000 * float(contrainte) ** (1 / 3), UnitTypeId.Megapascals))
                struc = PropertySetElement.Create(doc, asset)
                mat.SetMaterialAspectByPropertySet(MaterialAspect.Structural, struc.Id)
                appar_Id = ElementId(177984)
                mat.AppearanceAssetId = appar_Id
                t.Commit()
            return m_Id

    def ok_button(self, sender, e):
        """OK button handler"""
        input_data = self.collect_input_data()
        if not input_data:
            return
        A, B, H, ep, contrainte = input_data

        # Get or create concrete material
        m_Id = self.get_or_create_concrete_material(contrainte)

        # Now proceed with the geometry and transaction logic
        # Create tank walls, floor, etc.
        self.create_tank_geometry(A, B, H, ep, m_Id)

        self.Close()

    def create_tank_geometry(self, A, B, H, ep, m_Id):
        """Creates the tank geometry inside a transaction"""
        Pt0 = XYZ(-(A + ep) / 2, -(B + ep) / 2, 0)
        c0 = XYZ(-(A / 2 + ep), -(B / 2 + ep), 0)
        Pt1 = XYZ((A + ep) / 2, -(B + ep) / 2, 0)
        c1 = XYZ(A / 2 + ep, -(B / 2 + ep), 0)
        Pt2 = XYZ((A + ep) / 2, (B + ep) / 2, 0)
        c2 = XYZ(A / 2 + ep, B / 2 + ep, 0)
        Pt3 = XYZ(-(A + ep) / 2, (B + ep) / 2, 0)
        c3 = XYZ(-(A / 2 + ep), B / 2 + ep, 0)

        Lines = [Line.CreateBound(Pt0, Pt1), Line.CreateBound(Pt1, Pt2), Line.CreateBound(Pt2, Pt3), Line.CreateBound(Pt3, Pt0)]
        crvs = [Line.CreateBound(c0, c1), Line.CreateBound(c1, c2), Line.CreateBound(c2, c3), Line.CreateBound(c3, c0)]

        sloop = Line.CreateBound(XYZ(0, 0, 0), XYZ(1 / 0.3048, 0, 0))

        loop = CurveLoop()
        for crv in crvs:
            loop.Append(crv)
        curves = [loop]

        # Create Plane and Sketch_plan:
        with Transaction(doc, 'Create Tank Geometry') as t:
            t.Start()
            # new wall type
            voile = def_wall_Type.Duplicate('voile-BA_20')
            # new floor type
            Dalle = def_floor_Type.Duplicate('Dalle_BA_20')
            cs1 = voile.GetCompoundStructure()
            cs2 = Dalle.GetCompoundStructure()

            for structLayer1, structLayer2 in zip(cs1.GetLayers(), cs2.GetLayers()):
                cs1.SetLayerFunction(structLayer1.LayerId, MaterialFunctionAssignment.Structure)
                cs1.SetMaterialId(structLayer1.LayerId, m_Id)
                cs1.SetLayerWidth(0, ep)
                cs2.SetLayerFunction(structLayer2.LayerId, MaterialFunctionAssignment.Structure)
                cs2.SetMaterialId(structLayer2.LayerId, m_Id)
                cs2.SetLayerWidth(0, ep)

            voile.SetCompoundStructure(cs1)
            Dalle.SetCompoundStructure(cs2)
            for l in Lines:
                # create walls
                Wall.Create(doc, l, voile.Id, lvl_Id, H, 0.00, False, True)
                # create floor
                Floor.Create(doc, curves, Dalle.Id, lvl_Id, True, sloop, 0)
            t.Commit()

    def cancel_button(self, sender, e):
        self.Close()

form = ModalForm()
1 Like