# -*- coding: utf-8 -*-
__author__ = 'TUMCIVIL'
__version__ = '2.0.0'
__title__ = 'เซ็ตค่าระดับยอดเสาไปหลังผนัง'
__doc__ = """Created by : TUMCIVIL TEAM
Version : 2.0.0
Update : วันที่ 03-03-2025
_________________________________________________________________
รายการ :
เซ็ตระดับยอดเสาใไปหลังผนัง

_________________________________________________________________
ข้อจำกัดการใช้งาน :
1.ไม่สามารถใช้กับเสาที่ Slope ได้
2.ไม่สามารถใช้กับเสาที่สร้างจาก Model In-Place ได้
_________________________________________________________________
วิธีการใช้ :
1.เลือกผนัง
2.เลือกเสาที่ต้องการเช็ตระดับยอดเสา ออกกด ESC

   

 """
# for timing -------------------------------------------------------------------
# import pyrevit.coreutils as cor
# from pyrevit.coreutils import Timer
# timer = Timer()
# ------------------------------------------------------------------------------
import sys
import re
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *
import Autodesk.Revit.DB as DB
from Autodesk.Revit.DB.Structure import *
from Autodesk.Revit.DB.Structure import Rebar as rb
import pyrevit

import  os
from pyrevit import forms, revit
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB import (BuiltInParameter)

#>>>>>>>>>> .NET IMPORTS
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System")
from System.Collections.Generic import List
from System.Windows.Controls import ComboBoxItem
import wpf
from Autodesk.Revit.UI.Selection    import ISelectionFilter, ObjectType, Selection

#>>>>>>>>>> CUSTOM IMPORTS
from Snippets._convert_units import convert_internal_units
from Snippets._level import levels,get_levels_walls,get_levels_columns
from Snippets._level import *
from Selection._select_element import CustomISelectionFilter, pick_column,pick_wall
from System.Collections.ObjectModel import ObservableCollection
from System.ComponentModel import INotifyPropertyChanged

doc   = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument
app   = __revit__.Application





if __name__ == '__main__':

    out = []
    out3 = []
    levels_model = levels()
    # print("levels_model = {}".format(levels_model))

    # >>>>>>>>>> SELECT MAIN WALL
    try:
        Selected_Wall = pick_wall(uidoc, "เลือกผนังตัวหลัก")
        # Selected_column = pick_column(uidoc, "เลือกเสาตัวหลัก")
    except: forms.alert("ผนังไม่ได้ถูกเลือกรายการถูกยกเลิก.",exitscript=True,title="Script Canceled.")


    if Selected_Wall.Category.Id.IntegerValue == int(BuiltInCategory.OST_Walls):
        levels_wall = get_levels_walls(levels_model, Selected_Wall)
        Main_Top_elevation = levels_wall[1]
        Main_Bottom_elevation = levels_wall[0]

        for i in levels_model:
            if Main_Top_elevation < i[1] and i == levels_model[0]:
                Main_Top_Level_value = i[1]
                Main_Top_Level = i[2]
            elif Main_Top_elevation >= i[1]:
                Main_Top_Level_value = i[1]
                Main_Top_Level = i[2]

        match_top_offset = Main_Top_elevation - Main_Top_Level_value



        # print("Main_Top_Level = {}".format(Main_Top_Level))
        # print("match_top_offset = {}".format(convert_internal_units(match_top_offset, False,units='mm')))
        # print("Main_Top_elevation = {}".format(convert_internal_units(Main_Top_elevation, False,units='mm')))
        # print("Main_Bottom_elevation = {}".format(convert_internal_units(Main_Bottom_elevation, False,units='mm')))



        with forms.WarningBar(title="เลือกเสาชนหลังผนัง: (ออกกด 'Esc')", handle_esc=True):
            while True:

                #>>>>>>>>>> PICK Columns
                try:    Match_column = pick_column(uidoc)
                except: break
                if not Match_column: break
                levels_column = get_levels_columns(levels_model,Match_column)
                Top_elevation = levels_column[1]
                Bottom_elevation = levels_column[0]


                with Transaction(doc, "Column") as t:
                    t.Start()
                    if Top_elevation > Bottom_elevation:
                        Match_column.get_Parameter(BuiltInParameter.SCHEDULE_TOP_LEVEL_PARAM).Set(Main_Top_Level)
                        Match_column.get_Parameter(BuiltInParameter.SCHEDULE_TOP_LEVEL_OFFSET_PARAM).Set(match_top_offset)
                    elif Top_elevation < Bottom_elevation:
                        Match_column.get_Parameter(BuiltInParameter.SCHEDULE_BASE_LEVEL_PARAM).Set(Main_Top_Level)
                        Match_column.get_Parameter(BuiltInParameter.SCHEDULE_BASE_LEVEL_OFFSET_PARAM).Set(match_top_offset)


                    t.Commit()
    else:
        forms.alert("Column isn't selected then exited.", exitscript=True, title="Script Canceled.")






    # >>>>>>>>>> SELECT MAIN COLUMN

    # try:
    #     Selected_columns = get_selected_columns(uidoc)

    # except:
    #     pass
    # # print ("Selected_columns = {}".format(Selected_columns))
    # for e in Selected_columns:
    #     t = Transaction(doc, 'change parameter')
    #     t.Start()
    #     try:
    #         e.get_Parameter(BuiltInParameter.SCHEDULE_BASE_LEVEL_PARAM).Set(Level_Footing)
    #         e.get_Parameter(BuiltInParameter.SCHEDULE_BASE_LEVEL_OFFSET_PARAM).Set(set_footing_offset_level)
    #     except:
    #         pass
    #     t.Commit()

# for timing -------------------------------------------------------------------
# print("====" * 20)
# endtime = timer.get_time()
# print("Time been used = {} sec.".format(endtime))
# currentDate = cor.current_date()
# currentTime = cor.current_time()
# print ("Date : {} ; {}".format(currentDate,currentTime))
# print ("Credited by : {}".format(__author__))
# print("====" * 20)
# ------------------------------------------------------------------------------