# -*- coding: utf-8 -*-
__author__ = 'TUMCIVIL'
__version__ = '2.0.0'
__title__ = 'เซ็ตค่าระดับเท\nคอนกรีตเสาใต้พื้น'
__doc__ = """Created by : TUMCIVIL TEAM
Version : 2.0.0
Update : วันที่ 03-03-2025
_________________________________________________________________
รายการ :
เซ็ตระดับเทคอนกรีตเสาจากระดับท้องพื้นที่ต่ำสุด

_________________________________________________________________
ข้อจำกัดการใช้งาน :
1.ไม่สามารถใช้กับพื้นที่ Slope ได้
2.ไม่สามารถใช้กับคพื้นี่สร้างจาก Model In-Place ได้
3.ต้องตัดเสาออก 2 ส่วน โดยใช้คำสั่ง Split Element (SL) ก่อนทุกครั้ง
_________________________________________________________________
วิธีการใช้ :
1.เลือกพื้นที่มาเชื่อมกับหัวเสาเสร็จแล้วกด Finish
2.เลือกเสาล่างที่ต้องการเซ็ตระดับเทคอนกรีตเสา(เสาต้องถูกตัดแยกเป็นเสาล่างและเสาบนก่อน)
3.เลือกเสาบนที่ต้องการเซ็ตระดับเทคอนกรีตเสา
4.ในกรณีที่ต้องการเช็ตเฉพาะเสาล่างอย่างเดียว ในหัวข้อที่ 3 
   ให้กด ESC จะมีข้อความบอกเสาบนไม่ได้ถูกเลือกรายการ
   ทำเฉพาะเสาล่าง ให้กด Ok 

 """
# 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

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

#>>>>>>>>> LIMIT SELECTION
class CustomISelectionFilter(ISelectionFilter):
    """Filter user selection to certain element."""
    def __init__(self, cats):
        self.cats = cats
        # print("self.cats = {}".format(self.cats))
    def AllowElement(self, e):
        if str(e.Category.Id) == str(self.cats):
            return True
        #     print("return = True")
        # print("return = False")
        return False

class ISelectionFilter_Classes(ISelectionFilter):
    def __init__(self, allowed_types):
        """ ISelectionFilter made to filter with types
        :param allowed_types: list of allowed Types"""
        self.allowed_types = allowed_types

    def AllowElement(self, element):
        if type(element) in self.allowed_types:
            return True
        # else:
        #     return False

#>>>>>>>>>> PICK COLUMN
def pick_beam(given_uidoc = uidoc):
    """Function to promt user to select a column element in Revit UI."""
    beam_ref = given_uidoc.Selection.PickObjects(ObjectType.Element, CustomISelectionFilter("-2001320"), "เลือกคาน")    # -2001320 <- Id of OST_StructuralFraming
    beam     = given_uidoc.Document.GetElement(beam_ref)
    return beam
def pick_column(given_uidoc = uidoc,text = "เลือกเสา" ):
    """Function to promt user to select a column element in Revit UI."""
    # forms.WarningBar(title='Select Walls.')
    column_ref = given_uidoc.Selection.PickObject(ObjectType.Element, CustomISelectionFilter("-2001330"), text)    # -2001330 <- Id of OST_StructuralColumns
    column     = given_uidoc.Document.GetElement(column_ref)
    return column

def pick_floor(given_uidoc = uidoc,text = ""):
    """Function to promt user to select a column element in Revit UI."""
    # beam_ref = given_uidoc.Selection.PickObject(ObjectType.LinkedElement, CustomISelectionFilter("-2000032"), text)    # -2001320 <- Id of OST_StructuralFraming
    floor_ref = given_uidoc.Selection.PickObject(ObjectType.Element, CustomISelectionFilter("-2000032"), text)    # -2001320 <- Id of OST_StructuralFraming
    floor     = given_uidoc.Document.GetElement(floor_ref)
    # print("TYPE BEAM = {}".format(type(beam)))
    return floor

def get_selected_walls(uidoc, exitscript = True):
    """Function to Pick Walls. Currently selected walls will be pre-selected."""
    doc       = uidoc.Document
    selection = uidoc.Selection  # type: Selection

    selected_elements = [doc.GetElement(e_id) for e_id in selection.GetElementIds()]
    selected_walls    = [e for e in selected_elements if type(e) == Wall]


    ref_walls         = [Reference(r) for r in selected_walls]
    ref_preselection  = List[Reference](ref_walls)

    # Pick Walls (exterior walls are preselected)
    ISF_Rooms      = ISelectionFilter_Classes([Wall,])
    selected_walls = []

    try:
        with forms.WarningBar(title='Select Walls.'):
            ref_selected_walls = selection.PickObjects(ObjectType.Element,
                                                       ISF_Rooms,
                                                       'Select Rooms',
                                                       ref_preselection)

        selected_walls  = [doc.GetElement(ref) for ref in ref_selected_walls]
    except:
        pass

    if not selected_walls:
        error_msg = 'No Walls were selected.\nPlease Try Again'
        forms.alert(error_msg, title='Wall Selection has Failed.', exitscript=exitscript)

    return selected_walls

def get_selected_beams(uidoc, exitscript = True):
    """Function to Pick Beams. Currently selected beams will be pre-selected."""
    doc       = uidoc.Document
    selection = uidoc.Selection  # type: Selection

    selected_beams = []
    # ISF_Rooms      = CustomISelectionFilter("-2001352")
    ISF_Rooms      = CustomISelectionFilter("-2001320")
    selected_elements = [doc.GetElement(e_id) for e_id in selection.GetElementIds()]
    selected_beams    = [e for e in selected_elements]

    ref_beams         = [Reference(r) for r in selected_beams]
    ref_preselection  = List[Reference](ref_beams)

    # Pick Beams (exterior walls are preselected)

    try:
        with forms.WarningBar(title='โปรดเลือกคานที่เชื่อมหัวเสาเสร็จแล้วกด Finish.'):
            ref_selected_beams = selection.PickObjects(ObjectType.Element,
                                                       ISF_Rooms,
                                                       'โปรดเลือกคานเสร็จแล้วกด Finish',
                                                       ref_preselection)

        selected_beams  = [doc.GetElement(ref) for ref in ref_selected_beams]
    except:
        pass

    if not selected_beams:
        error_msg = 'No Beams were selected.\nPlease Try Again'
        forms.alert(error_msg, title='Beams Selection has Failed.', exitscript=exitscript)

    return selected_beams

def get_selected_elements(uidoc = uidoc, exitscript=True):
    """Property that retrieves selected views or promt user to select some from the dialog box."""
    doc       = uidoc.Document
    selection = uidoc.Selection  # type: Selection

    try:
        selected_elements = [doc.GetElement(e_id) for e_id in selection.GetElementIds()]
        if not selected_elements:
            forms.alert("No elements  were selected.\nPlease, try again.", exitscript=exitscript)
    except:
        return

    return selected_elements
def levels():
    levels = DB.FilteredElementCollector(doc).OfCategory(DB.BuiltInCategory.OST_Levels) \
        .WhereElementIsNotElementType() \
        .ToElements()
    l = len(levels)
    for e3 in levels:
        p3 =[]
        name = e3.get_Parameter(BuiltInParameter.DATUM_TEXT).AsString()
        eleLev = e3.get_Parameter(BuiltInParameter.LEVEL_ELEV).AsDouble()
        id = e3.Id
        p3.append(name)
        p3.append(eleLev)
        p3.append(id)

        out3.append(p3)


if __name__ == '__main__':

    out = []
    out3 = []
    levels()
    try:   
            # Selected_beam = get_selected_beams(uidoc)
            Selected_floor = pick_floor(uidoc, "เลือกพื้นตัวหลัก")


    except:
        forms.alert("Script is canceled.", exitscript=True, title="Script Canceled.")

    if Selected_floor.Category.Id.IntegerValue == int(BuiltInCategory.OST_Floors):
        # print("IsBeam = True")
        Main_Top_Constraint = Selected_floor.get_Parameter(BuiltInParameter.STRUCTURAL_ELEVATION_AT_TOP).AsDouble()
        Main_Bottom_Constraint = Selected_floor.get_Parameter(
            BuiltInParameter.STRUCTURAL_ELEVATION_AT_BOTTOM).AsDouble()
        # print("Main_Top_Constraint = {}".format(convert_internal_units(float(Main_Top_Constraint), False, units='mm')))
        # print("Main_Bottom_Constraint = {}".format(convert_internal_units(float(Main_Bottom_Constraint), False, units='mm')))

    floorEleBottomMin = Main_Bottom_Constraint
    # >>>>>>>>>> SELECT MAIN COLUMN
    try:    Selected_column = pick_column(uidoc,"เลือกเสาล่าง")
    except: forms.alert("เสาล่างไม่ได้ถูกเลือกรายการถูกยกเลิก.",exitscript=True,title="Script Canceled.")

    try:    Selected_column2 = pick_column(uidoc,"เลือกเสาบน")
    except: forms.alert("เสาบนไม่ได้ถูกเลือกรายการทำเฉพาะเสาล่าง.",exitscript=False,title="Script Canceled.")



    set_Column_Top = ""
    set_Column_Ele_Top = 0.0
    set_Column_Top_offset = 0.0
    set_Column_Buttom_offset = 0.0
    top_Level_Column = Selected_column.get_Parameter(BuiltInParameter.SCHEDULE_TOP_LEVEL_PARAM).AsElementId()
    top_Offset_Column = Selected_column.get_Parameter(BuiltInParameter.SCHEDULE_TOP_LEVEL_OFFSET_PARAM).AsDouble()
    # convert_internal_units( self.main_rebar_dia, False, "mm")
    for i in out3:
        if i[2] == top_Level_Column:
            set_Column_Top = i[2]
            set_Column_Ele_Top = i[1]
            break
    set_Column_Top_offset = floorEleBottomMin-set_Column_Ele_Top
    set_Column_Buttom_offset = floorEleBottomMin-set_Column_Ele_Top

    t = Transaction(doc, 'change parameter')
    t.Start()
    try:
        Selected_column.get_Parameter(BuiltInParameter.SCHEDULE_TOP_LEVEL_PARAM).Set(set_Column_Top)
        Selected_column.get_Parameter(BuiltInParameter.SCHEDULE_TOP_LEVEL_OFFSET_PARAM).Set(set_Column_Top_offset)
        Selected_column2.get_Parameter(BuiltInParameter.SCHEDULE_BASE_LEVEL_PARAM).Set(set_Column_Top)
        Selected_column2.get_Parameter(BuiltInParameter.SCHEDULE_BASE_LEVEL_OFFSET_PARAM).Set(set_Column_Buttom_offset)
    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)
# ------------------------------------------------------------------------------