# -*- coding: utf-8 -*-
__author__ = 'TUMCIVIL'
__version__ = '2.0.0'
__title__ = 'สร้างไดเมนชั่นกริต'
__doc__ = """Created by : TUMCIVIL TEAM
Version : 2.0.0
Update : วันที่ 03-03-2025
_________________________________________________________________
รายการ :
สร้างไดเมนชั่นกริต

_________________________________________________________________
ข้อจำกัดการใช้งาน :
1.ไม่มี

_________________________________________________________________
วิธีการใช้ :
1.-



 """

# Import commom language runtime
import clr
# Import Revit API
from Autodesk.Revit.DB import FilteredElementCollector, ElementCategoryFilter, \
							BuiltInCategory, ReferenceArray, Reference, Transaction, \
							TransactionGroup, Line
# Import Revit API
from Autodesk.Revit.Creation.ItemFactoryBase import NewDimension
from Autodesk.Revit.DB import XYZ
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *
import Autodesk.Revit.DB as DB
# Store current document into variable
doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument

# Get current view
view = doc.ActiveView

# Select all grids by filter
# gridsFilter = ElementCategoryFilter(BuiltInCategory.OST_Grids)
# gridsCollector = FilteredElementCollector(doc).WherePasses(gridsFilter) \
# 				.WhereElementIsNotElementType()

gridsCollector = DB.FilteredElementCollector(doc).OfCategory(DB.BuiltInCategory.OST_Grids) \
        .WhereElementIsNotElementType() \
        .ToElements()

# Variables to split grids into columns and rows
gridsColumn = ReferenceArray()
gridsColumnTotal = ReferenceArray()
gridsRow = ReferenceArray()
gridsRowTotal = ReferenceArray()
gridsC = []
gridsR = []

for i in gridsCollector:
	if round(i.Curve.GetEndPoint(0).Y,2) == round(i.Curve.GetEndPoint(1).Y,2):
		gridsColumn.Append(Reference(i))
		gridsC.append(i)
	elif round(i.Curve.GetEndPoint(0).X,2) == round(i.Curve.GetEndPoint(1).X,2):
		gridsRow.Append(Reference(i))
		gridsR.append(i)

offset = UnitUtils.ConvertToInternalUnits(1000, UnitTypeId.Millimeters)
offset2 = UnitUtils.ConvertToInternalUnits(500, UnitTypeId.Millimeters)

# Retrieve endpoints
endPointC0 = (gridsC[0].Curve.GetEndPoint(0))
endPointC1 = (gridsC[-1].Curve.GetEndPoint(0))
gridsColumnTotal.Append(Reference(gridsC[0]))
gridsColumnTotal.Append(Reference(gridsC[-1]))

endPointR0 = gridsR[0].Curve.GetEndPoint(0)
endPointR1 = gridsR[-1].Curve.GetEndPoint(0)
gridsRowTotal.Append(Reference(gridsR[0]))
gridsRowTotal.Append(Reference(gridsR[-1]))

if endPointC0.X < endPointR0.X:
	endPointC0_1 = endPointC0+XYZ(offset,0,0)
else:
	endPointC0_1 = endPointC0+XYZ(-offset,0,0)
if endPointC1.X < endPointR0.X:
	endPointC1_1 = endPointC1+XYZ(offset,0,0)
else:
	endPointC1_1 = endPointC1+XYZ(-offset,0,0)

if endPointC0.X < endPointR0.X:
	endPointC0T_1 = endPointC0+XYZ(offset2,0,0)
else:
	endPointC0T_1 = endPointC0+XYZ(-offset2,0,0)
if endPointC1.X < endPointR0.X:
	endPointC1T_1 = endPointC1+XYZ(offset2,0,0)
else:
	endPointC1T_1 = endPointC1+XYZ(-offset2,0,0)

if endPointR0.Y < endPointC0.Y:
	endPointR0_1 = endPointR0+XYZ(0,offset,0)
else:
	endPointR0_1 = endPointR0+XYZ(0,-offset,0)
if endPointR1.Y < endPointC0.Y:
	endPointR1_1 = endPointR1+XYZ(0,offset,0)
else:
	endPointR1_1 = endPointR1+XYZ(0,-offset,0)

if endPointR0.Y < endPointC0.Y:
	endPointR0T_1 = endPointR0+XYZ(0,offset2,0)
else:
	endPointR0T_1 = endPointR0+XYZ(0,-offset2,0)
if endPointR1.Y < endPointC0.Y:
	endPointR1T_1 = endPointR1+XYZ(0,offset2,0)
else:
	endPointR1T_1 = endPointR1+XYZ(0,-offset2,0)

# Create line to place dimension
lineC = Line.CreateBound(endPointC0_1, endPointC1_1)
lineCT = Line.CreateBound(endPointC0T_1, endPointC1T_1)
lineR = Line.CreateBound(endPointR0_1, endPointR1_1)
lineRT = Line.CreateBound(endPointR0T_1, endPointR1T_1)
# Create transaction to create dimensions
t = Transaction(doc, "Dimension grids")
t.Start()

# Create dimensions
doc.Create.NewDimension(view, lineC, gridsColumn)
doc.Create.NewDimension(view, lineR, gridsRow)
doc.Create.NewDimension(view, lineCT, gridsColumnTotal)
doc.Create.NewDimension(view, lineRT, gridsRowTotal)

# Commit transaction
t.Commit()