"Add Piles Coordinates data concrete Footing in Model \n\n\n Updated : 03-04-2024 \n Shared by : TUMCIVIL"
__author__ = 'TUMCIVIL'
__version__ = '1.0.0'
__title__ = ' Add Piles Coordinates (Units = Millimeters)'
# for timing -------------------------------------------------------------------
import pyrevit.coreutils as cor
from pyrevit.coreutils import Timer
timer = Timer()
# ------------------------------------------------------------------------------
import sys
import csv
import codecs
import os.path as op
from collections import defaultdict

from pyrevit import PyRevitException
from pyrevit import forms
from pyrevit import coreutils
from pyrevit import revit, DB, HOST_APP
from pyrevit import script
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *
import Autodesk.Revit.DB as DB
from Autodesk.Revit.DB.Structure import *
import re

doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument

def checkParameter(parameter):
    storageType = parameter.StorageType
    if (storageType.ToString() == "ElementId"):
        value = parameter.AsValueString()
    return value

def levels():
    levels = DB.FilteredElementCollector(doc).OfCategory(DB.BuiltInCategory.OST_Levels) \
        .WhereElementIsNotElementType() \
        .ToElements()
    # print("levels = {}".format(levels))
    l = len(levels)
    # print("l = {}".format(l))
    for e3 in levels:
        p3 =[]
        name = e3.get_Parameter(BuiltInParameter.DATUM_TEXT).AsString()
        # name = e3.LookupParameter("Name").AsString()
        eleLev = e3.get_Parameter(BuiltInParameter.LEVEL_ELEV).AsDouble()
        # eleLev = e3.LookupParameter("Elevation").AsDouble()
        p3.append(name)
        p3.append(eleLev)
        # print("levels = {} , {}".format(name,eleLev))

        out3.append(p3)
    # for i in out3:
    # print("out3 = {}".format(out3))

def remove_none_digit(s):
    t = ""
    for c in s:
        if c.isdigit():
            t += c
    return t

def NE():
    foundations = DB.FilteredElementCollector(doc).OfCategory(DB.BuiltInCategory.OST_StructuralFoundation) \
        .WhereElementIsNotElementType() \
        .ToElements()

    if len(foundations)!= 0:
        out = []
        familyType = []
        for e in foundations:
                p2 = []
                id = e.Id
                name1 = e.Name
                family2 = e.get_Parameter(BuiltInParameter.ELEM_FAMILY_PARAM)
                value2 = checkParameter(family2)
                if value2 != "Wall Foundation" and value2 != "Foundation Slab" and bool(re.match("Pile", name1)) == True:
                    coor = e.Location.Point
                    xsc = round(UnitUtils.ConvertFromInternalUnits(doc.ActiveProjectLocation.GetProjectPosition(coor)
                            .NorthSouth,UnitTypeId.Millimeters),2)
                    ysc = round(UnitUtils.ConvertFromInternalUnits(doc.ActiveProjectLocation.GetProjectPosition(coor)
                            .EastWest,UnitTypeId.Millimeters),2)
                    for j in e.Parameters:
                        if j.IsShared and j.Definition.Name == "Pile No.":
                            pileNo = e.get_Parameter(j.GUID).AsString()

                    # p2.append(id)  # Id 0
                    # p2.append(pileNo)  # Pile No. 1
                    # p2.append(xsc)  # NorthSouth Pile 2
                    # p2.append(ysc)  # EastWest Pile 3

                    # out.append(p2)

                    t = Transaction(doc, 'change parameter')
                    t.Start()
                    try:
                        e.LookupParameter("NorthSouth").Set(xsc)
                        e.LookupParameter("EastWest").Set(ysc)
                        # e.LookupParameter("Top of Wall").Set(q9)

                    except:
                        # TaskDialog.Show('error', 'error message')
                        pass
                    t.Commit()


if __name__ == "__main__":
    # out2 = []
    # out3 = []
    # levels()
    NE()

# for timing -------------------------------------------------------------------
print("====" * 20)
print("ADD COORDINATED PILES COMPLETELY !")
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)
# ------------------------------------------------------------------------------