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

_________________________________________________________________
ข้อจำกัดการใช้งาน :
1.ตำแหน่งเริ่มต้นจะอยู่ที่ Internal origin (0,0,0) เท่านั้น
2.ไม่สามารถสร้างกริดไลน์เอียงได้
_________________________________________________________________
วิธีการใช้ :
1.ใส่ชื่อกริดไลน์แนวนอน (A..Z,or 1...2) หลังจากใส่ข้อมูลแรกแล้วสามารถใส่ข้อมูลแบบหลาย
  กริดไลน์ เช่น B-H หรือ 2-10 ยกเว้น B1-H หรือ ก-ฮ เป็นต้น 
2.ใส่ระยะห่างระหว่างกริดไลน์แนวนอนหน่วยเป็นมิลลิเมตร ให้ใส่ค่าเริ่มต้นเป็นค่า = 0 ก่อน
3.กดเพิ่มข้อมูลที่ Add Data จะปรากฏค่าในตารางด้านขวามือ
4.ถ้าต้องการลบข้อมูลในตารางทั้งหมดกด Delete All Data 
ถ้าต้องการเพิ่มข้อมูลให้กดเลือกตำแหน่งในตารางแล้วใส่ข้อมูลเพิ่มในข้อ 1 และ 2 ตามลำดับแล้วกด Insert Data
ถ้าต้องการแก้ไขข้อมูลในตารางให้กดเลือกข้อมูลในตารางแล้ว แก้ไขข้อมูลในข้อ 1 และ 2 ตามลำดับ แล้วกด Modify Data 
ถ้าต้องการลบข้อมูลที่ละบรรทัดให้กดเลือกข้อมูลในตาราง แล้วกด Delete Row Data
5.ต้องการเริ่มกริดไลน์แนวนอนจากบนลงล่างให้เลือกปุ่ม Gridline Start at UP ต้องการล่างขึ้นบน 
  ให้เลือกปุ่ม Gridline Start at DOWN
6.ใส่กริดไลน์แนวตั้งเหมือนขั้นตอน 1-4
7.ต้องการเริ่มกริดไลน์แนวตั้งจากซ้ายไปขวาให้เลิอก Gridline Start at LIFT ต้องการจากขวาไปซ่าย
  ให้เลิอก Gridline Start at RIGHT
8.เมื่อใส่ข้อมูลเป็นที่เรียบร้อยให้กด Create Gridline
9.ต้องการออกกด Exit
 """
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *
from pyrevit.forms import WPFWindow
from pyrevit import forms

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


class dataHGL:
    def __init__(self, hgname, hgspacing):
        self.hgname = hgname
        self.hgspacing = hgspacing

    @property
    def hgname(self):
        return self.__hgname

    @hgname.setter
    def hgname(self, hgname):
        self.__hgname = hgname

    @property
    def hgspacing(self):
        return self.__hgspacing

    @hgspacing.setter
    def hgspacing(self, hgspacing):
        self.__hgspacing = hgspacing

    def __str__(self):
        return "{} {}".format(self.hgname, self.hgspacing)

class dataVGL:
    def __init__(self, vgname, vgspacing):
        self.vgname = vgname
        self.vgspacing = vgspacing

    @property
    def vgname(self):
        return self.__vgname

    @vgname.setter
    def vgname(self, vgname):
        self.__vgname = vgname

    @property
    def vgspacing(self):
        return self.__vgspacing

    @vgspacing.setter
    def vgspacing(self, vgspacing):
        self.__vgspacing = vgspacing

    def __str__(self):
        return "{} {}".format(self.vgname, self.vgspacing)

class dataHgRa:
    def __init__(self, u1):
        self.u1 = u1
        # self. =
    @property
    def u1(self):
        return self.__u1

    @u1.setter
    def u1(self, u1):
        self.__u1 = u1

    def __str__(self):
        return "{}".format(self.u1)

class dataVgRa:
    def __init__(self, v1):
        self.v1 = v1
    @property
    def v1(self):
        return self.__v1
    @v1.setter
    def v1(self, v1):
        self.__v1 = v1

    def __str__(self):
        return "{}".format(self.v1)

app      = __revit__.Application
rvt_year = int(app.VersionNumber)
def convert_internal_units(value, get_internal = True, units='m'):
    #type: (float, bool, str) -> float
    """Function to convert Internal units to meters or vice versa.
    :param value:        Value to convert
    :param get_internal: True to get internal units, False to get Meters
    :param units:        Select desired Units: ['m', 'm2']
    :return:             Length in Internal units or Meters."""

    if rvt_year >= 2021:
        from Autodesk.Revit.DB import UnitTypeId
        if   units == 'm' : units = UnitTypeId.Meters
        elif units == "m2": units = UnitTypeId.SquareMeters
        elif units == 'cm': units = UnitTypeId.Centimeters
    else:
        from Autodesk.Revit.DB import DisplayUnitType
        if   units == 'm' : units = DisplayUnitType.DUT_METERS
        elif units == "m2": units = DisplayUnitType.DUT_SQUARE_METERS
        elif units == "cm": units = DisplayUnitType.DUT_CENTIMETERS

    if get_internal:
        return UnitUtils.ConvertToInternalUnits(value, units)
    return UnitUtils.ConvertFromInternalUnits(value, units)
class ModalForm(WPFWindow):
    def __init__(self, xaml_file_name):
        WPFWindow.__init__(self, xaml_file_name)
        # self.IsEnabled = "True"
        self.ShowDialog()
    def addHgBtn(self, sender, e):
        # y1 = []
        d1 = self.HGNameTB.Text
        d2 = self.HGSpacingTB.Text
        d1_1 = d1.replace(" ","").split("-")
        if d1 != "" or d2 != "":
            if "-" in d1:
                if d1_1[0].isdigit() and d1_1[1].isdigit():
                    r1 = d1_1[0]
                    r2 = d1_1[1]
                    for i in range(int(r1), int(r2)+1):
                        d3 = dataHGL(i, d2)
                        self.DataGridHorizontal.Items.Add(d3)
                elif d1_1[0].isalpha() and len(d1_1[0]) == 1 and d1_1[1].isalpha() and len(d1_1[1]) == 1:
                    o1 = ord(d1_1[0])
                    o2 = ord(d1_1[1])+1
                    for i in range(int(o1),int(o2)):
                        d3 = dataHGL(chr(i), d2)
                        self.DataGridHorizontal.Items.Add(d3)
                else:
                    forms.alert("({}) can not type".format(self.HGNameTB.Text),ok="True")
                    self.HGNameTB.Clear()
                    pass
            else:
                d3 = dataHGL(d1, d2)
                self.DataGridHorizontal.Items.Add(d3)
        else:
            pass

    def InHgBtn(self, sender, e):
        row1 = self.DataGridHorizontal.SelectedIndex
        if row1 > 0 or row1 == 0:
            self.DataGridHorizontal.Items.Insert(row1, dataHGL(self.HGNameTB.Text, self.HGSpacingTB.Text))

    def DataGridHorizontal_SelectionChanged(self, sender, e):
        # self.IsEnabled = "True"
        # self.MoHgBtn(self,sender,IsEnabled = "True")
        row1 = self.DataGridHorizontal.SelectedIndex
        if row1 >0 or row1 == 0:
            row = self.DataGridHorizontal.ItemContainerGenerator.ContainerFromIndex(row1)
            self.HGNameTB.Text = self.DataGridHorizontal.Columns[0].GetCellContent(row).Text
            self.HGSpacingTB.Text = self.DataGridHorizontal.Columns[1].GetCellContent(row).Text

    def MoHgBtn(self, sender, e):
        row1 = self.DataGridHorizontal.SelectedIndex
        if row1 > 0 or row1 == 0:
            self.DataGridHorizontal.Items.RemoveAt(row1)
            self.DataGridHorizontal.Items.Insert(row1,dataHGL(self.HGNameTB.Text, self.HGSpacingTB.Text))

    def DelHgBtn(self, sender, e):
        self.DataGridHorizontal.Items.Clear()

    def DelRowHgBtn(self, sender, e):
        row1 = self.DataGridHorizontal.SelectedIndex
        if row1 > 0 or row1 == 0:
            self.DataGridHorizontal.Items.RemoveAt(row1)

    def HgRadioBtn(self, sender, e):
        if (self.upHgRadioBtn.IsChecked == True):
            self.u1 = 1
        elif (self.downHgRadioBtn.IsChecked == True):
            self.u1 = 0

    def addVgBtn(self, sender, e):
        # p1 = []
        d1 = self.VGNameTB.Text
        d2 = self.VGSpacingTB.Text
        d1_1 = d1.replace(" ", "").split("-")
        if d1 != "" or d2 != "":
            if "-" in d1:
                if d1_1[0].isdigit() and d1_1[1].isdigit():
                    r1 = d1_1[0]
                    r2 = d1_1[1]
                    for i in range(int(r1), int(r2) + 1):
                        d3 = dataVGL(i, d2)
                        self.DataGridVertical.Items.Add(d3)
                elif d1_1[0].isalpha() and len(d1_1[0]) == 1 and d1_1[1].isalpha() and len(d1_1[1]) == 1:
                    o1 = ord(d1_1[0])
                    o2 = ord(d1_1[1]) + 1
                    for i in range(int(o1), int(o2)):
                        d3 = dataVGL(chr(i), d2)
                        self.DataGridVertical.Items.Add(d3)
                else:
                    forms.alert("({}) can not type".format(self.VGNameTB.Text), ok="True")
                    self.VGNameTB.Clear()
                    pass
            else:
                d3 = dataVGL(d1, d2)
                self.DataGridVertical.Items.Add(d3)
        else:
            pass

    def InVgBtn(self, sender, e):
        row1 = self.DataGridVertical.SelectedIndex
        if row1 > 0 or row1 == 0:
            # self.DataGridVertical.Items.RemoveAt(row1)
            self.DataGridVertical.Items.Insert(row1, dataVGL(self.VGNameTB.Text, self.VGSpacingTB.Text))

    def DataGridVertical_SelectionChanged(self, sender, e):
        row1 = self.DataGridVertical.SelectedIndex
        if row1 >0 or row1 == 0:
            row = self.DataGridVertical.ItemContainerGenerator.ContainerFromIndex(row1)
            self.VGNameTB.Text = self.DataGridVertical.Columns[0].GetCellContent(row).Text
            self.VGSpacingTB.Text = self.DataGridVertical.Columns[1].GetCellContent(row).Text


    def MoVgBtn(self, sender, e):
        row1 = self.DataGridVertical.SelectedIndex
        if row1 > 0 or row1 == 0:
            self.DataGridVertical.Items.RemoveAt(row1)
            self.DataGridVertical.Items.Insert(row1, dataVGL(self.VGNameTB.Text, self.VGSpacingTB.Text))

    def DelVgBtn(self, sender, e):
        self.DataGridVertical.Items.Clear()

    def DelRowVgBtn(self, sender, e):
        row1 = self.DataGridVertical.SelectedIndex
        if row1 > 0 or row1 == 0:
            self.DataGridVertical.Items.RemoveAt(row1)

    def VgRadioBtn(self, sender, e):
        if (self.liftVgRadioBtn.IsChecked == True):
            self.v1 = 0
        elif (self.rightVgRadioBtn.IsChecked == True):
            self.v1 = 1

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

    def CrBtn(self, sender, e):
        HG = []
        VG = []
        HGr = self.DataGridHorizontal.Items.Count
        for i in range(HGr):
            q1 = []
            row = self.DataGridHorizontal.ItemContainerGenerator.ContainerFromIndex(i)
            nameHG = self.DataGridHorizontal.Columns[0].GetCellContent(row).Text
            spcingHG = self.DataGridHorizontal.Columns[1].GetCellContent(row).Text
            q1.append(nameHG)
            q1.append(spcingHG)
            HG.append(q1)
        sumHgSp = round(sum(float(c[1]) for c in HG), 2)

        VGr = self.DataGridVertical.Items.Count
        for i in range(VGr):
            q2 = []
            row = self.DataGridVertical.ItemContainerGenerator.ContainerFromIndex(i)
            nameVG = self.DataGridVertical.Columns[0].GetCellContent(row).Text
            spcingVG = self.DataGridVertical.Columns[1].GetCellContent(row).Text
            q2.append(nameVG)
            q2.append(spcingVG)
            VG.append(q2)
        sumVgSp = round(sum(float(c[1]) for c in VG), 2)
        if sumHgSp == 0:
            sumHgSp = 10000
        if sumVgSp == 0:
            sumVgSp = 10000

        if (self.upHgRadioBtn.IsChecked or self.downHgRadioBtn.IsChecked) and (self.liftVgRadioBtn.IsChecked or self.rightVgRadioBtn.IsChecked):
            if self.u1 == 0 and self.v1 == 0:
                if len(HG) > 0:
                    n = 0
                    HG3 = 0
                    for i in HG:
                        n = n + 1
                        HG1 = HG[n - 1][0]
                        HG2 = UnitUtils.ConvertToInternalUnits(float(HG[n - 1][1]), UnitTypeId.Millimeters)
                        if n == 1:
                            R_1 = XYZ(UnitUtils.ConvertToInternalUnits(-2000, UnitTypeId.Millimeters), 0, 0)
                            R_2 = XYZ(UnitUtils.ConvertToInternalUnits((sumVgSp + 2000), UnitTypeId.Millimeters), 0, 0)
                            line = Line.CreateBound(R_1, R_2)
                        else:
                            HG3 = HG2 + HG3
                            R_1 = XYZ(UnitUtils.ConvertToInternalUnits(-2000, UnitTypeId.Millimeters), HG3, 0)
                            R_2 = XYZ(UnitUtils.ConvertToInternalUnits((sumVgSp + 2000), UnitTypeId.Millimeters), HG3, 0)
                            line = Line.CreateBound(R_1, R_2)
                        t = Transaction(doc, 'grid')
                        t.Start()
                        grid_R1 = Grid.Create(doc, line)
                        name = grid_R1.get_Parameter(BuiltInParameter.DATUM_TEXT)
                        name.Set(HG1)
                        t.Commit()
                if len(VG) > 0:
                    n = 0
                    VG3 = 0
                    for i in VG:
                        n = n + 1
                        VG1 = VG[n - 1][0]
                        VG2 = UnitUtils.ConvertToInternalUnits(float(VG[n - 1][1]), UnitTypeId.Millimeters)
                        if n == 1:
                            C_1 = XYZ(0, UnitUtils.ConvertToInternalUnits(-2000, UnitTypeId.Millimeters), 0)
                            C_2 = XYZ(0, UnitUtils.ConvertToInternalUnits((sumHgSp + 2000), UnitTypeId.Millimeters), 0)
                            line = Line.CreateBound(C_1, C_2)
                        else:
                            VG3 = VG2 + VG3
                            C_1 = XYZ(VG3, UnitUtils.ConvertToInternalUnits(-2000, UnitTypeId.Millimeters), 0)
                            C_2 = XYZ(VG3,
                                    UnitUtils.ConvertToInternalUnits((sumHgSp + 2000), UnitTypeId.Millimeters), 0)
                            line = Line.CreateBound(C_1, C_2)
                        t = Transaction(doc, 'grid')
                        t.Start()
                        grid_C1 = Grid.Create(doc, line)
                        name = grid_C1.get_Parameter(BuiltInParameter.DATUM_TEXT)
                        name.Set(VG1)
                        t.Commit()

            elif self.u1 == 0 and self.v1 == 1:
                if len(HG) > 0:
                    n = 0
                    HG3 = 0
                    for i in HG:
                        n = n + 1
                        HG1 = HG[n - 1][0]
                        HG2 = UnitUtils.ConvertToInternalUnits(float(HG[n - 1][1]), UnitTypeId.Millimeters)
                        if n == 1:
                            R_1 = XYZ(UnitUtils.ConvertToInternalUnits(-2000, UnitTypeId.Millimeters), 0, 0)
                            R_2 = XYZ(UnitUtils.ConvertToInternalUnits((sumVgSp + 2000), UnitTypeId.Millimeters), 0, 0)
                            line = Line.CreateBound(R_1, R_2)
                        else:
                            HG3 = HG2 + HG3
                            R_1 = XYZ(UnitUtils.ConvertToInternalUnits(-2000, UnitTypeId.Millimeters), HG3, 0)
                            R_2 = XYZ(UnitUtils.ConvertToInternalUnits((sumVgSp + 2000), UnitTypeId.Millimeters), HG3, 0)
                            line = Line.CreateBound(R_1, R_2)
                        t = Transaction(doc, 'grid')
                        t.Start()
                        grid_R1 = Grid.Create(doc, line)
                        name = grid_R1.get_Parameter(BuiltInParameter.DATUM_TEXT)
                        name.Set(HG1)
                        t.Commit()
                if len(VG) > 0:
                    n = 0
                    VG3 = UnitUtils.ConvertToInternalUnits(sumVgSp, UnitTypeId.Millimeters)
                    for i in VG:
                        n = n + 1
                        VG1 = VG[n - 1][0]
                        VG2 = UnitUtils.ConvertToInternalUnits(float(VG[n - 1][1]), UnitTypeId.Millimeters)
                        if n == 1:
                            C_1 = XYZ(VG3, UnitUtils.ConvertToInternalUnits(-2000, UnitTypeId.Millimeters), 0)
                            C_2 = XYZ(VG3, UnitUtils.ConvertToInternalUnits((sumHgSp + 2000), UnitTypeId.Millimeters), 0)
                            line = Line.CreateBound(C_1, C_2)
                        else:
                            VG3 = VG3 - VG2
                            C_1 = XYZ(VG3, UnitUtils.ConvertToInternalUnits(-2000, UnitTypeId.Millimeters), 0)
                            C_2 = XYZ(VG3,UnitUtils.ConvertToInternalUnits((sumHgSp + 2000), UnitTypeId.Millimeters), 0)
                            line = Line.CreateBound(C_1, C_2)
                        t = Transaction(doc, 'grid')
                        t.Start()
                        grid_C1 = Grid.Create(doc, line)
                        name = grid_C1.get_Parameter(BuiltInParameter.DATUM_TEXT)
                        name.Set(VG1)
                        t.Commit()
            elif self.u1 == 1 and self.v1 == 0:
                if len(HG) > 0:
                    n = 0
                    HG3 = UnitUtils.ConvertToInternalUnits(sumHgSp, UnitTypeId.Millimeters)
                    for i in HG:
                        n = n + 1
                        HG1 = HG[n - 1][0]
                        HG2 = UnitUtils.ConvertToInternalUnits(float(HG[n - 1][1]), UnitTypeId.Millimeters)
                        if n == 1:
                            R_1 = XYZ(UnitUtils.ConvertToInternalUnits(-2000, UnitTypeId.Millimeters), HG3, 0)
                            R_2 = XYZ(UnitUtils.ConvertToInternalUnits((sumVgSp + 2000), UnitTypeId.Millimeters), HG3, 0)
                            line = Line.CreateBound(R_1, R_2)
                        else:
                            HG3 = HG3 - HG2
                            R_1 = XYZ(UnitUtils.ConvertToInternalUnits(-2000, UnitTypeId.Millimeters), HG3, 0)
                            R_2 = XYZ(UnitUtils.ConvertToInternalUnits((sumVgSp + 2000), UnitTypeId.Millimeters), HG3, 0)
                            line = Line.CreateBound(R_1, R_2)
                        t = Transaction(doc, 'grid')
                        t.Start()
                        grid_R1 = Grid.Create(doc, line)
                        name = grid_R1.get_Parameter(BuiltInParameter.DATUM_TEXT)
                        name.Set(HG1)
                        t.Commit()
                if len(VG) > 0:
                    n = 0
                    VG3 = 0
                    for i in VG:
                        n = n + 1
                        VG1 = VG[n - 1][0]
                        VG2 = UnitUtils.ConvertToInternalUnits(float(VG[n - 1][1]), UnitTypeId.Millimeters)
                        if n == 1:
                            C_1 = XYZ(0, UnitUtils.ConvertToInternalUnits(-2000, UnitTypeId.Millimeters), 0)
                            C_2 = XYZ(0, UnitUtils.ConvertToInternalUnits((sumHgSp + 2000), UnitTypeId.Millimeters), 0)
                            line = Line.CreateBound(C_2, C_1)
                        else:
                            VG3 = VG2 + VG3
                            C_1 = XYZ(VG3, UnitUtils.ConvertToInternalUnits(-2000, UnitTypeId.Millimeters), 0)
                            C_2 = XYZ(VG3,
                                    UnitUtils.ConvertToInternalUnits((sumHgSp + 2000), UnitTypeId.Millimeters), 0)
                            line = Line.CreateBound(C_2, C_1)
                        t = Transaction(doc, 'grid')
                        t.Start()
                        grid_C1 = Grid.Create(doc, line)
                        name = grid_C1.get_Parameter(BuiltInParameter.DATUM_TEXT)
                        name.Set(VG1)
                        t.Commit()
            elif self.u1 == 1 and self.v1 == 1:
                if len(HG) > 0:
                    n = 0
                    HG3 = UnitUtils.ConvertToInternalUnits(sumHgSp, UnitTypeId.Millimeters)
                    for i in HG:
                        n = n + 1
                        HG1 = HG[n - 1][0]
                        HG2 = UnitUtils.ConvertToInternalUnits(float(HG[n - 1][1]), UnitTypeId.Millimeters)
                        if n == 1:
                            R_1 = XYZ(UnitUtils.ConvertToInternalUnits(-2000, UnitTypeId.Millimeters), HG3, 0)
                            R_2 = XYZ(UnitUtils.ConvertToInternalUnits((sumVgSp + 2000), UnitTypeId.Millimeters), HG3, 0)
                            line = Line.CreateBound(R_1, R_2)
                        else:
                            HG3 = HG3 - HG2
                            R_1 = XYZ(UnitUtils.ConvertToInternalUnits(-2000, UnitTypeId.Millimeters), HG3, 0)
                            R_2 = XYZ(UnitUtils.ConvertToInternalUnits((sumVgSp + 2000), UnitTypeId.Millimeters), HG3, 0)
                            line = Line.CreateBound(R_1, R_2)
                        t = Transaction(doc, 'grid')
                        t.Start()
                        grid_R1 = Grid.Create(doc, line)
                        name = grid_R1.get_Parameter(BuiltInParameter.DATUM_TEXT)
                        name.Set(HG1)
                        t.Commit()
                if len(VG) > 0:
                    n = 0
                    VG3 = UnitUtils.ConvertToInternalUnits(sumVgSp, UnitTypeId.Millimeters)
                    for i in VG:
                        n = n + 1
                        VG1 = VG[n - 1][0]
                        VG2 = UnitUtils.ConvertToInternalUnits(float(VG[n - 1][1]), UnitTypeId.Millimeters)
                        if n == 1:
                            C_1 = XYZ(VG3, UnitUtils.ConvertToInternalUnits(-2000, UnitTypeId.Millimeters), 0)
                            C_2 = XYZ(VG3, UnitUtils.ConvertToInternalUnits((sumHgSp + 2000), UnitTypeId.Millimeters), 0)
                            line = Line.CreateBound(C_2, C_1)
                        else:
                            VG3 = VG3 - VG2
                            C_1 = XYZ(VG3, UnitUtils.ConvertToInternalUnits(-2000, UnitTypeId.Millimeters), 0)
                            C_2 = XYZ(VG3, UnitUtils.ConvertToInternalUnits((sumHgSp + 2000), UnitTypeId.Millimeters), 0)
                            line = Line.CreateBound(C_2, C_1)
                        t = Transaction(doc, 'grid')
                        t.Start()
                        grid_C1 = Grid.Create(doc, line)
                        name = grid_C1.get_Parameter(BuiltInParameter.DATUM_TEXT)
                        name.Set(VG1)
                        t.Commit()
            self.Close()
        elif (self.upHgRadioBtn.IsChecked or self.downHgRadioBtn.IsChecked) and (self.liftVgRadioBtn.IsChecked == False or self.rightVgRadioBtn.IsChecked == False):
            if self.u1 == 0:
                if len(HG) > 0:
                    n = 0
                    HG3 = 0
                    for i in HG:
                        n = n + 1
                        HG1 = HG[n - 1][0]
                        HG2 = UnitUtils.ConvertToInternalUnits(float(HG[n - 1][1]), UnitTypeId.Millimeters)
                        if n == 1:
                            R_1 = XYZ(UnitUtils.ConvertToInternalUnits(-2000, UnitTypeId.Millimeters), 0, 0)
                            R_2 = XYZ(UnitUtils.ConvertToInternalUnits((sumVgSp + 2000), UnitTypeId.Millimeters), 0, 0)
                            line = Line.CreateBound(R_1, R_2)
                        else:
                            HG3 = HG2 + HG3
                            R_1 = XYZ(UnitUtils.ConvertToInternalUnits(-2000, UnitTypeId.Millimeters), HG3, 0)
                            R_2 = XYZ(UnitUtils.ConvertToInternalUnits((sumVgSp + 2000), UnitTypeId.Millimeters), HG3, 0)
                            line = Line.CreateBound(R_1, R_2)
                        t = Transaction(doc, 'grid')
                        t.Start()
                        grid_R1 = Grid.Create(doc, line)
                        name = grid_R1.get_Parameter(BuiltInParameter.DATUM_TEXT)
                        name.Set(HG1)
                        t.Commit()
            elif self.u1 == 1:
                if len(HG) > 0:
                    n = 0
                    HG3 = UnitUtils.ConvertToInternalUnits(sumHgSp, UnitTypeId.Millimeters)
                    for i in HG:
                        n = n + 1
                        HG1 = HG[n - 1][0]
                        HG2 = UnitUtils.ConvertToInternalUnits(float(HG[n - 1][1]), UnitTypeId.Millimeters)
                        if n == 1:
                            R_1 = XYZ(UnitUtils.ConvertToInternalUnits(-2000, UnitTypeId.Millimeters), HG3, 0)
                            R_2 = XYZ(UnitUtils.ConvertToInternalUnits((sumVgSp + 2000), UnitTypeId.Millimeters), HG3, 0)
                            line = Line.CreateBound(R_1, R_2)
                        else:
                            HG3 = HG3 - HG2
                            R_1 = XYZ(UnitUtils.ConvertToInternalUnits(-2000, UnitTypeId.Millimeters), HG3, 0)
                            R_2 = XYZ(UnitUtils.ConvertToInternalUnits((sumVgSp + 2000), UnitTypeId.Millimeters), HG3, 0)
                            line = Line.CreateBound(R_1, R_2)
                        t = Transaction(doc, 'grid')
                        t.Start()
                        grid_R1 = Grid.Create(doc, line)
                        name = grid_R1.get_Parameter(BuiltInParameter.DATUM_TEXT)
                        name.Set(HG1)
                        t.Commit()
            self.Close()
        elif (self.upHgRadioBtn.IsChecked== False or self.downHgRadioBtn.IsChecked== False) and (self.liftVgRadioBtn.IsChecked or self.rightVgRadioBtn.IsChecked):
            if self.v1 == 0:
                if len(VG) > 0:
                    n = 0
                    VG3 = 0
                    for i in VG:
                        n = n + 1
                        VG1 = VG[n - 1][0]
                        VG2 = UnitUtils.ConvertToInternalUnits(float(VG[n - 1][1]), UnitTypeId.Millimeters)
                        if n == 1:
                            C_1 = XYZ(0, UnitUtils.ConvertToInternalUnits(-2000, UnitTypeId.Millimeters), 0)
                            C_2 = XYZ(0, UnitUtils.ConvertToInternalUnits((sumHgSp + 2000), UnitTypeId.Millimeters), 0)
                            line = Line.CreateBound(C_2, C_1)
                        else:
                            VG3 = VG2 + VG3
                            C_1 = XYZ(VG3, UnitUtils.ConvertToInternalUnits(-2000, UnitTypeId.Millimeters), 0)
                            C_2 = XYZ(VG3,
                                    UnitUtils.ConvertToInternalUnits((sumHgSp + 2000), UnitTypeId.Millimeters), 0)
                            line = Line.CreateBound(C_2, C_1)
                        t = Transaction(doc, 'grid')
                        t.Start()
                        grid_C1 = Grid.Create(doc, line)
                        name = grid_C1.get_Parameter(BuiltInParameter.DATUM_TEXT)
                        name.Set(VG1)
                        t.Commit() 
            elif self.v1 == 1:
                if len(VG) > 0:
                    n = 0
                    VG3 = UnitUtils.ConvertToInternalUnits(sumVgSp, UnitTypeId.Millimeters)
                    for i in VG:
                        n = n + 1
                        VG1 = VG[n - 1][0]
                        VG2 = UnitUtils.ConvertToInternalUnits(float(VG[n - 1][1]), UnitTypeId.Millimeters)
                        if n == 1:
                            C_1 = XYZ(VG3, UnitUtils.ConvertToInternalUnits(-2000, UnitTypeId.Millimeters), 0)
                            C_2 = XYZ(VG3, UnitUtils.ConvertToInternalUnits((sumHgSp + 2000), UnitTypeId.Millimeters), 0)
                            line = Line.CreateBound(C_2, C_1)
                        else:
                            VG3 = VG3 - VG2
                            C_1 = XYZ(VG3, UnitUtils.ConvertToInternalUnits(-2000, UnitTypeId.Millimeters), 0)
                            C_2 = XYZ(VG3, UnitUtils.ConvertToInternalUnits((sumHgSp + 2000), UnitTypeId.Millimeters), 0)
                            line = Line.CreateBound(C_2, C_1)
                        t = Transaction(doc, 'grid')
                        t.Start()
                        grid_C1 = Grid.Create(doc, line)
                        name = grid_C1.get_Parameter(BuiltInParameter.DATUM_TEXT)
                        name.Set(VG1)
                        t.Commit()
            self.Close()
        else:
            forms.alert("PLEASE SELECT 2 (UP OR DOWN,LIFT OR RIGHT)", ok="True")
            pass



if __name__ == '__main__':
    form = ModalForm('interface.xaml')
