clsGeoReference.cls 89 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303
VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsGeoReference"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
Option Explicit
Public Enum LayerTypeRefence
   Poligonos = 0
   Trecho_Rede_Agua = 1
   Componente_Rede_Agua = 2
   Trecho_Rede_esgoto = 3
   Componente_Rede_Esgoto = 4
   Trecho_Rede_Drenagem = 5
   Componente_Rede_Drenagem = 6
   RAMAIS_AGUA = 7
   RAMAIS_ESGOTO = 8
   DOCUMENTOS = 9
   AMARRACAO = 10
   LOTES = 11
   OUTROS = 12
   CONSUMIDOR_GPS = 13
End Enum

Dim a As String
Dim b As String
Dim c As String
Dim d As String
Dim e As String
Dim f As String
Dim g As String
Dim h As String
Dim ii As String
Dim j As String
Dim k As String
Dim l As String
Dim vm As String



'Public stopProcess
Public object_ids As String
Private p As Boolean, i As Integer, MyLines() As String
Private mvartdb As TeDatabase 'local copy
Private mvartdbcon As TeDatabase 'local copy
Private mvartcs As TeCanvas
Public Property Set tdb(ByVal vData As TeDatabase)
    Set mvartdb = vData
End Property

Public Property Get tdb() As TeDatabase
    Set tdb = mvartdb
End Property

Public Property Set tdbcon(ByVal vData As TeDatabase)
    Set mvartdbcon = vData
End Property

Public Property Get tdbcon() As TeDatabase
    Set tdbcon = mvartdbcon
End Property
'Propriedade do Canvas
'
'
Public Property Set tcs(ByVal vData As TeCanvas)
    Set mvartcs = vData
End Property

Public Property Get tcs() As TeCanvas
    Set tcs = mvartcs
End Property

'###############################################################
' Autor     : Luis Claudio Rodrigues Domingues    Data: 20/12/05
' Nome      : GetReferenceLayer
' Descrição : Retorna o layer de referencia
'###############################################################

Public Function GetReferenceLayer(LayerName As String, Optional LayerLines As Boolean) As String
On Error GoTo Trata_Erro
   Dim rs As ADODB.Recordset, strsql As String
  
  If frmCanvas.TipoConexao <> 4 Then
    strsql = "SELECT * From X_LayersComponents where (layerline='" & LayerName & "' or layerline='" & UCase(LayerName) & "' or layercomponent='" & LayerName & "' or layercomponent='" & UCase(LayerName) & "') "
   If LayerLines Then
        strsql = strsql & " and layerline='" & LayerName & "'"
   End If
   
    Set rs = Conn.execute(strsql)
   
   If Not rs.EOF Then
      If LCase(rs.Fields("LAYERLINE").value) = LCase(LayerName) Then
         GetReferenceLayer = UCase(rs.Fields("LAYERCOMPONENT").value)
      Else
         GetReferenceLayer = UCase(rs.Fields("LAYERLINE").value)
      End If
   Else
      MsgBox "Não foi encontrado " & LayerName & " no banco de dados.", vbInformation
   End If
   
   Else
Dim va As String
Dim vm As String
Dim vi As String
Dim vo As String
Dim vu As String
Dim vc As String
va = "X_LAYERSCOMPONENTS"
vm = "LAYERLINE"
vi = "LAYERCOMPONENT"


'MsgBox Conn.ConnectionString

  strsql = "SELECT * From " + """" + va + """" + " where (" + """" + vm + """" + "='" & UCase(LayerName) & "' or " + """" + vm + """" + "='" & UCase(LayerName) & "' or " + """" + vi + """" + "='" & UCase(LayerName) & "' or " + """" + vi + """" + "='" & UCase(LayerName) & "')"
    
      If LayerLines Then

        strsql = strsql & " and " + """" + vm + """" + "='" & LayerName & "'"
   End If
       
       
  
       
       
       
       'rs.Open strsql, Conn, adOpenDynamic, adLockOptimistic
   Set rs = Conn.execute(strsql)
   
   If Not rs.EOF Then
      If (rs.Fields("LAYERLINE").value) = LayerName Then
         GetReferenceLayer = (rs.Fields("LAYERCOMPONENT").value)
      Else
         GetReferenceLayer = (rs.Fields("LAYERLINE").value)
      End If
   Else
      MsgBox "Não foi encontrado " & LayerName & " no banco de dados.", vbInformation
   End If
   
   End If
  
  ' If LayerLines Then
    '    strsql = strsql & " and layerline='" & LayerName & "'"
  ' End If
  
   rs.Close
   Set rs = Nothing
Trata_Erro:
    If Err.Number = 0 Or Err.Number = 20 Then
       Resume Next
    Else
       
      PrintErro "clsGeoReference", "Public Function GetReferenceLayer", CStr(Err.Number), CStr(Err.Description), True
       
    End If
End Function

'###############################################################
' Autor     : Luis Claudio Rodrigues Domingues    Data: 02/10/06
' Nome      : GetReferenceLayer
' Descrição : Retorna o layer de referencia
'###############################################################

Public Function GetTypeText(LayerName As String) As Long
On Error GoTo Trata_Erro
   Dim rs As ADODB.Recordset
   a = "X_LAYERSCOMPONENTS"
   b = "LAYERLINE"
     If frmCanvas.TipoConexao <> 4 Then
   Set rs = Conn.execute("SELECT * From X_LayersComponents where layerline='" & LayerName & "'")
   Else
    Set rs = Conn.execute("SELECT * From " + """" + a + """" + " where " + """" + b + """" + "='" & LayerName & "'")
   End If
   If Not rs.EOF Then
      GetTypeText = rs.Fields("TypeText").value
   End If
   rs.Close
   Set rs = Nothing
Trata_Erro:
    If Err.Number = 0 Or Err.Number = 20 Then
       Resume Next
    Else
       
      PrintErro "clsGeoReference", "Public Function GetTypeText", CStr(Err.Number), CStr(Err.Description), True
       
    End If
End Function

'###############################################################
' Autor     : Luis Claudio Rodrigues Domingues    Data: 02/10/06
' Nome      : GetReferenceLayer
' Descrição : Retorna o layer de referencia
'###############################################################

'Public Function GetTypeReference(layerName As String) As Long
'   Dim rs As ADODB.Recordset
'   Set rs = Conn.execute("SELECT * From X_LayersComponents where layerline='" & layerName & "'")
'   If Not rs.EOF Then
'      GetTypeReference = rs.Fields("TypeReference").value
'   End If
'   rs.Close
'   Set rs = Nothing
'End Function


Public Function InsertTextInPoint(geom_id As Long, object_id As String, Layer As String, Optional CotaI As Double, Optional CotaF As Double) As Boolean
   
   Dim X As Double, Y As Double, Distancia As Integer
   
   Dim TbRefTexto As String
   Dim SQL As String
   Dim rs As ADODB.Recordset
   
   'LOCALIZA A TABELA DE TEXTOS PARA PEGAR AS COTAS E DEPOIS EXCLUIR OS REGISTROS
   
   Set rs = New ADODB.Recordset
    If frmCanvas.TipoConexao <> 4 Then
   SQL = "SELECT TR.GEOM_TABLE FROM TE_REPRESENTATION TR WHERE GEOM_TYPE = 128 AND TR.LAYER_ID IN (SELECT LAYER_ID FROM TE_LAYER WHERE NAME = '" & Layer & "')"
   Else
   Dim va As String
   Dim ve As String
   Dim vi As String
   Dim vo As String
   Dim vu As String
   Dim vc As String
va = "te_representation"
vm = "geom_table"
vi = "geom_type"
vo = "layer_id"
vu = "te_layer"
vc = "name"
   
   SQL = "SELECT " + """" + va + """" + "." + """" + vm + """" + " FROM " + """" + va + """" + "  WHERE " + """" + vi + """" + " = '128' AND " + """" + va + """" + "." + """" + vo + """" + " IN (SELECT " + """" + vo + """" + " FROM " + """" + vu + """" + " WHERE " + """" + vc + """" + " = '" & Layer & "')"
   End If
   
      rs.Open SQL, Conn, adOpenDynamic, adLockOptimistic
   
   If rs.EOF = False Then
      
      TbRefTexto = rs!GEOM_TABLE
      rs.Close
      
      'EXCLUI O REGISTRO
a = LCase(TbRefTexto)

c = "object_id"

      If frmCanvas.TipoConexao <> 4 Then
      Conn.execute "DELETE FROM " & TbRefTexto & " WHERE OBJECT_ID = '" & object_id & "'"

      Else
      Conn.execute "DELETE FROM " + """" + a + """" + " WHERE " + """" + c + """" + " = '" & object_id & "'"
      End If
      
      Set rs = New ADODB.Recordset
       If frmCanvas.TipoConexao <> 4 Then
      SQL = "SELECT OBJECT_ID_, GROUNDHEIGHT AS " + """" + "INI" + """" + ", GROUNDHEIGHTFINAL AS " + """" + "FIM" + """" + " FROM " & Layer & " WHERE OBJECT_ID_ = '" & object_id & "' ORDER BY COMPONENT_ID"
      Else
      a = "OBJECT_ID_"
b = "INITIALGROUNDHEIGHT"
c = "FINALGROUNDHEIGHT"
d = "COMPONENT_ID"
     SQL = "SELECT " + """" + a + """" + "," + """" + b + """" + " AS INI, " + """" + c + """" + " AS FIM FROM " + """" + Layer + """" + " WHERE " + """" + a + """" + " = '" & object_id & "' ORDER BY " + """" + d + """" + ""
    
      
      End If
      
        rs.Open SQL, Conn, adOpenDynamic, adLockOptimistic

      If rs.EOF = False Then
         tdbcon.setCurrentLayer Layer
         
         'LOCALIZA O CENTRO DA GEOMETRIA DO PONTO EM X E Y
         tdbcon.getCenterGeometry CLng(rs!Object_id_), rs!Object_id_, tpPOINTS, X, Y
                  
         tdbcon.InsertTextInDatabase rs!Object_id_, Round(rs!ini, 3), 1, X, Y + 2, 0, , , True
         tdbcon.InsertTextInDatabase rs!Object_id_, Round(rs!fim, 3), 1, X, Y - 3, 0, , , True
         
         'INSERE OS TEXTOS COM A DIFERENÇA ENTRE COTA TERRENO E COTA PROFUNDIDADE NUMA NOVA LOCALIZAÇÃO
         
         Dim dblDIF As Double
         Dim strDIF As String
         
         dblDIF = rs!ini - rs!fim
         strDIF = Round(dblDIF, 3)
         
         tdbcon.InsertTextInDatabase rs!Object_id_, strDIF, 1, X + 3, Y - 0.5, 0, , , True
         
      End If

   End If
  
End Function

'Public Function UpdateTextInPoint(LayerName As String, object_id As String) As Boolean
'
'   InsertTextInPoint
'
'   On Error GoTo Trata_Erro
'   '####### DECLARAÇÃO DE VARIAVEIS LOCAIS ##########
'   If LCase(LayerName) = "watercomponents" Then Exit Function
'   Dim RsOut As ADODB.Recordset, RsInt As ADODB.Recordset, a As Integer
'   Set RsInt = Conn.execute("SELECT * from texts" & GetLayerID(LayerName) & " Where object_id='" & object_id & "'")
'   Set RsOut = Conn.execute("SELECT * from " & LayerName & " Where object_id_='" & object_id & "'")
'
'   '####### ROTINA DE ATUALIZAÇÃO ##############
'   For a = 1 To 2
'      Conn.execute "UPDATE TEXTS" & GetLayerID(LayerName) & _
'         " SET TEXT_VALUE = " & IIf(a = 1, Replace(RsOut!GroundHeight, ",", "."), Replace(RsOut!GroundHeightFinal, ",", ".")) & _
'         " WHERE GEOM_ID=" & RsInt!geom_id
'      RsInt.MoveNext
'
'   Next
'   RsInt.Close
'   RsOut.Close
'   Set RsInt = Nothing
'   Set RsOut = Nothing
'
'Trata_Erro:
'    If Err.Number = 0 Or Err.Number = 20 Then
'       Resume Next
'    Else
'       'xxxxx mantendo o código antigo 01/08/2008 xxxxx
'       If Not (RsOut Is Nothing) Then
'           If RsOut.State = adStateOpen Then RsOut.Close
'       End If
'       If Not (RsInt Is Nothing) Then
'          If RsInt.State = adStateOpen Then RsInt.Close
'       End If
'       Set RsInt = Nothing
'       Set RsOut = Nothing
'       'xxxxx mantendo o código antigo 01/08/2008 FIM xxxxx
'       Open App.path & "\Controles\GeoSanLog.txt" For Append As #1
'       Print #1, Now & " " & strUser & " " & Versao_Geo & " - clsGeoReference - Public Function UpdateTextInPoint - " & Err.Number & " - " & Err.Description
'       Close #1
'       MsgBox "Um posssível erro foi identificado:" & Chr(13) & Chr(13) & Err.Description & Chr(13) & Chr(13) & "Foi gerado na pasta do aplicativo o arquivo GeoSanLog.txt com informações desta ocorrência.", vbInformation
'    End If
'
'End Function

Public Function UpdateTextsInLines(LayerName_ As String, object_id As String)
   On Error GoTo Trata_Erro
   
   '####### DECLARAÇÃO DE VARIAVEIS LOCAIS ##########
   Dim rsTexts As ADODB.Recordset, rsWLines As ADODB.Recordset, LayId As Integer
   Dim comprimento As Double, Material As String, Diametro As String, CotaI As Double, CotaF As Double
   '####### ROTINA  ##############
   Dim L5 As String

   LayId = GetLayerID(LayerName_)
   
   L5 = str(LayId)
   L5 = Trim(L5)
  If frmCanvas.TipoConexao <> 4 Then
   Set rsTexts = Conn.execute(convertQuery("SELECT * From Texts" & LayId & " where object_id='" & object_id & "' Order by geom_id", CInt(typeconnection)))
   Else
   Dim au As String
    Dim ae As String
     Dim ai As String
      Dim ao As String
      Dim uu As String
      uu = str(LayId)
      uu = Trim(uu)
      au = "texts"
      ae = "object_id"
      ai = "geom_id"
      
    'Set rsTexts = Conn.execute(convertQuery("SELECT * From " + """" + au + uu + """" + " where " + """" + ae + """" + "='" & object_id & "' Order by " + """" + ai + """" + "", CInt(typeconnection)))
 
   Set rsTexts = New ADODB.Recordset
     rsTexts.Open "SELECT * From " + """" + au + L5 + """" + " where " + """" + ae + """" + "='" & object_id & "' Order by " + """" + ai + """" + "", Conn, adOpenDynamic, adLockOptimistic

  
   
   
   End If
   
   
   If Not rsTexts.EOF Then
      
      If frmCanvas.TipoConexao <> 4 Then
      
      Set rsWLines = Conn.execute("SELECT * From  " & LayerName_ & " left JOIN x_Material on material=materialid where object_id_='" & object_id & "'")
      Else
     Dim ay As String
    Dim ax As String
     Dim az As String
     Dim aa As String
     ax = "X_MATERIAL"
      ay = "OBJECT_ID_"
      az = "MATERIALID"
     aa = "MATERIAL"
        Set rsWLines = Conn.execute("SELECT * From  " + """" + LayerName_ + """" + " left JOIN " + """" + ax + """" + " on " + """" + aa + """" + "=" + """" + az + """" + " where " + """" + ay + """" + "='" & object_id & "'")
        
      End If
      
      
      Diametro = Chr$(216) & " " & IIf(IsNull(rsWLines!INTERNALDIAMETER), 0, rsWLines!INTERNALDIAMETER)
      
      Material = IIf(IsNull(rsWLines!MATERIALNAME), "", rsWLines!MATERIALNAME)
      If frmCanvas.TipoConexao = 4 Then
      
      If Material = "" Then
      Material = "0"
      End If
      End If
      comprimento = IIf(rsWLines!Length = 0, rsWLines!LENGTHCALCULATED, rsWLines!Length)
      
         If LayerName_ = "SEWERLINES" Then
            CotaI = rsWLines!INITIALTUBEDEEPNESS
            CotaF = rsWLines!FINALTUBEDEEPNESS

         Else
            CotaI = rsWLines!INITIALGROUNDHEIGHT
            CotaF = rsWLines!FINALGROUNDHEIGHT
         
         End If
      
a = "texts"
b = LayId
c = "b"
d = "text_value"
e = CotaI
f = CotaF
g = comprimento

ii = "geom_id"
j = "ECONOMIAS"
k = "CONSUMO_LPS"
l = "TB_LIGACOES"


  
     If GetTypeText(LayerName_) = 2 Then  'Rede Esgoto / Drenagem
         'Inclinação
         If frmCanvas.TipoConexao <> 4 Then
         Conn.execute "Update Texts" & LayId & " Set text_value = '" & Round(((CotaI - CotaF) / comprimento) * 100, 2) & "%" & "' Where geom_id=" & rsTexts.Fields("geom_id")
         Else
         a = "texts"
b = LayId
c = "b"
d = "text_value"
e = CotaI
f = CotaF
g = comprimento

ii = "geom_id"
j = "ECONOMIAS"
k = "CONSUMO_LPS"
l = "TB_LIGACOES"
         'MsgBox "Update " + """" + a + LayId + """" + " Set " + """" + d + """" + "='" & Round(((CotaI - CotaF) / comprimento) * 100, 2) & "%" & "'  Where " + """" + ii + """" + "='" & rsTexts.Fields("geom_id") & "'"
        Conn.execute "Update " + """" + "texts" + L5 + """" + " Set " + """" + "text_value" + """" + "='" & Round(((CotaI - CotaF) / comprimento) * 100, 2) & "%" & "'  Where " + """" + "geom_id" + """" + "='" & rsTexts.Fields("geom_id") & "'"
         End If
         rsTexts.MoveNext
         'Diamentro
          If frmCanvas.TipoConexao <> 4 Then
         Conn.execute "Update Texts" & LayId & " Set text_value = '" & Diametro & "' Where geom_id=" & rsTexts.Fields("geom_id")
         Else
         a = "texts"
b = LayId
c = "b"
d = "text_value"
e = CotaI
f = CotaF
g = comprimento

ii = "geom_id"
j = "ECONOMIAS"
k = "CONSUMO_LPS"
l = "TB_LIGACOES"
         'MsgBox "Update " + """" + a + LayId + """" + " Set " + """" + d + """" + " = '" & Diametro & "' Where " + """" + ii + """" + "='" & rsTexts.Fields("geom_id") & "'"
         Conn.execute "Update " + """" + "texts" + L5 + """" + " Set " + """" + "text_value" + """" + " = '" & Diametro & "' Where " + """" + "geom_id" + """" + "='" & rsTexts.Fields("geom_id") & "'"
         End If
         rsTexts.MoveNext
         'Material
          If frmCanvas.TipoConexao <> 4 Then
         Conn.execute "Update Texts" & LayId & " Set text_value = '" & Material & "' Where geom_id=" & rsTexts.Fields("geom_id")
         Else
         a = "texts"
b = LayId
c = "b"
d = "text_value"
e = CotaI
f = CotaF
g = comprimento

ii = "geom_id"
j = "ECONOMIAS"
k = "CONSUMO_LPS"
l = "TB_LIGACOES"
         'MsgBox "Update " + """" + a + """" + LayId + """" + " Set " + """" + d + """" + " = '" & Material & "' Where " + """" + ii + """" + "='" & rsTexts.Fields("geom_id") & "'"
         Conn.execute "Update " + """" + "texts" + L5 + """" + " Set " + """" + "text_value" + """" + " = '" & Material & "' Where " + """" + "geom_id" + """" + "='" & rsTexts.Fields("geom_id") & "'"
         End If
         rsTexts.MoveNext
         'Comprimento
         If frmCanvas.TipoConexao <> 4 Then
         Conn.execute "Update Texts" & LayId & " Set text_value = '" & comprimento & "' Where geom_id='" & rsTexts.Fields("geom_id") & "'"
         
         Else
         a = "texts"
b = LayId
c = "b"
d = "text_value"
e = CotaI
f = CotaF
g = comprimento

ii = "geom_id"
j = "ECONOMIAS"
k = "CONSUMO_LPS"
l = "TB_LIGACOES"

         'MsgBox "Update" + """" + "texts" + LayId + """" + " Set " + """" + "text_value" + """" + " = '" & comprimento & "' Where  " + """" + "geom_id" + """" + "='" & rsTexts.Fields("geom_id") & "'"
         Conn.execute "Update" + """" + "texts" + L5 + """" + " Set " + """" + "text_value" + """" + " = '" & comprimento & "' Where  " + """" + "geom_id" + """" + "='" & rsTexts.Fields("geom_id") & "'"
         End If
         rsTexts.MoveNext
         

a = "texts"
b = LayId
c = "b"
d = "TEXT_VALUE"
e = Round(CotaI, 2)
f = "'e'"
g = rsTexts.Fields("geom_id")
h = "'g'"
ii = "GEOM_ID"
j = "ECONOMIAS"
k = "CONSUMO_LPS"
l = "TB_LIGACOES"

            'Cota Inicial
             If frmCanvas.TipoConexao <> 4 Then
            Conn.execute "Update Texts" & LayId & " Set text_value = '" & Round(CotaI, 2) & "' Where geom_id=" & rsTexts.Fields("geom_id")
            Else
            Conn.execute "Update " + """" + "texts" + L5 + """" + "  Set " + """" + "text_value" + """" + " = '" & Round(CotaI, 2) & "' Where " + """" + "geom_id" + """" + "='" & rsTexts.Fields("geom_id") & "'"
            End If
            rsTexts.MoveNext
            'Cota Final
a = "texts"
b = LayId
c = "b"
d = "TEXT_VALUE"
e = Round(CotaF, 2)
f = "'e'"
g = rsTexts.Fields("geom_id")
h = "'g'"
ii = "GEOM_ID"
j = "ECONOMIAS"
k = "CONSUMO_LPS"
l = "TB_LIGACOES"
            If frmCanvas.TipoConexao <> 4 Then
            Conn.execute "Update Texts" & LayId & " Set text_value = '" & Round(CotaF, 2) & "' Where geom_id=" & rsTexts.Fields("geom_id")
            Else
            Conn.execute "Update " + """" + "texts" + L5 + """" + " Set " + """" + "text_value" + """" + " =  '" & Round(CotaF, 2) & "' Where  " + """" + "geom_id" + """" + "='" & rsTexts.Fields("geom_id") + "'"
            End If
         
a = "texts"
b = LayId
c = "b"
d = "TEXT_VALUE"
e = Round(CotaI, 2)
f = "'e'"
g = rsTexts.Fields("geom_id")
h = "'g'"

         If Not rsTexts.EOF Then
            rsTexts.MoveNext
            If Not rsTexts.EOF Then
             If frmCanvas.TipoConexao <> 4 Then
               Conn.execute "Update Texts" & LayId & " Set text_value = '" & Round(CotaI, 2) & "' Where geom_id=" & rsTexts.Fields("geom_id")
               Else
               Conn.execute "Update " + """" + "texts" + L5 + """" + "  Set " + """" + "TEXT_VALUE" + """" + " =  '" & Round(CotaI, 2) & "' Where  " + """" + "geom_id" + """" + "='" & rsTexts.Fields("geom_id") & "'"
               End If
            End If
         End If
         If Not rsTexts.EOF Then
            rsTexts.MoveNext
a = "Texts"
b = LayId
c = "b"
d = "TEXT_VALUE"
e = Round(CotaF, 2)
f = "'e'"
g = rsTexts.Fields("geom_id")
h = "'g'"
            If frmCanvas.TipoConexao <> 4 Then
            Conn.execute "Update Texts" & LayId & " Set text_value = '" & Round(CotaF, 2) & "' Where geom_id=" & rsTexts.Fields("geom_id")
            Else
            Conn.execute "Update " + """" + "texts" + L5 + """" + " Set " + """" + "text_value" + """" + " =  '" & Round(CotaF, 2) & "' Where  " + """" + "geom_id" + """" + "='" & rsTexts.Fields("geom_id") & "'"
            End If
         End If
      Else 'Rede de Agua
         'Diametro
         'Dim nSQL As String
a = "texts"
b = LayId
c = "b"
d = "TEXT_VALUE"
e = Round(CotaF, 2)
f = "'e'"
g = rsTexts.Fields("geom_id")
h = "'g'"
         'nSQL = "Update Texts" & LayId & " Set text_value = '" & Diametro & "' Where geom_id= " & rsTexts.Fields("geom_id")
         If frmCanvas.TipoConexao <> 4 Then
         Conn.execute "Update Texts" & LayId & " Set text_value = '" & Diametro & "' Where geom_id= " & rsTexts.Fields("geom_id")
         Else
           Conn.execute "Update " + """" + "texts" + L5 + """" + " Set " + """" + "text_value" + """" + " = '" & Diametro & "'" + " Where  " + """" + "geom_id" + """" + "='" & rsTexts.Fields("geom_id") & "'"
         End If
         rsTexts.MoveNext
         'Material
         If frmCanvas.TipoConexao <> 4 Then
         Conn.execute "Update Texts" & LayId & " Set text_value = '" & Material & "' Where geom_id= " & rsTexts.Fields("geom_id")
         Else
          Conn.execute "Update  " + """" + "texts" + L5 + """" + " Set " + """" + "text_value" + """" + " = '" & Material & "'" + " Where  " + """" + "geom_id" + """" + "='" & rsTexts.Fields("geom_id") & "'"
         End If
         rsTexts.MoveNext
         'Comprimento
     
      End If
      
      rsWLines.Close
   End If
   Set rsWLines = Nothing
   rsTexts.Close
   Set rsTexts = Nothing
Trata_Erro:
    If Err.Number = 0 Or Err.Number = 20 Or Err.Number = 3021 Then
       Resume Next
    Else
       
      PrintErro "clsGeoReference", "Public Function UpdateTextsInLines", CStr(Err.Number), CStr(Err.Description), True
       
    End If
   
End Function
' Calcula os posicionamentos dos textos na linha e insere os mesmos
'
' geom_id - da linha
' object_id - da linha
' Diametro - do trecho de rede
' Material - do trecho de rede
' comprimento - do trecho de rede
' InsCotas - se serão colocadas as cotas
' CotaI - cota inicial do trecho
' CotaF - cota final do trecho
'
Public Function InsertTextInDatabase(geom_id As Long, object_id As String, _
                  Diametro As String, Material As String, comprimento As Double, Optional InsCotas As Boolean = False, Optional CotaI As Double, Optional CotaF As Double) As Boolean
    On Error GoTo Trata_Erro
    '####### DECLARAÇÃO DE VARIAVEIS LOCAIS ##########
    'On Error GoTo InsertTextInDatabase_Err
    Dim SEG As Integer, QtdePontos As Integer, ANGULO As Double, Side As TeGeometrySide
    Dim X As Double, Y As Double, l As Double, Distancia As Integer
    '####### ROTINA DE INSERÇÃO ##############
    
    With tdb
        'retorna em x, y as coordenadas da bissetriz da cadeia de linhas (meio dela),
        'para sabermos onde colocaremos os textos da linha (comprimento, etc.)
        .getCenterGeometry geom_id, object_id, tpLINES, X, Y
        'obtem a quantidade de pontos (vértices) em uma cadeia de linhas
        QtdePontos = .getQuantityPointsLine(geom_id, object_id)
        'divide por dois para saber em que segmento de linha iremos inserir o texto. se a divisao nao for exata pegaremos o segmento mais a esquerda.
        SEG = CInt(QtdePontos / 2)
        'obtem o angulo do segmento escolhido
        ANGULO = .getAngle(geom_id, object_id, SEG, 2)
        .getLengthOfLine geom_id, object_id, l
        'obtem a informação se a linha foi desenhada da direita para esquerda, ou da esquerda para direita, indefinido ou leftRight (???)
        Side = .getSide(X, Y, object_id, geom_id, tpLINES)
        If Side = TeRight Then
            Distancia = 6
        Else
            Distancia = 6
            'Distancia = -1
        End If
        If Not InsCotas Then
            '--- Diametro
            ' obtem as coords. x e y perpendiculares a linha (object_id), no meio da linha + 1 metro (L/2+1),
            ' a uma distância (-Distancia) perpendicular a linha
            'L = comprimento
            If l > 10 Then 'textos do diametro e material um ao lado do outro
                If ANGULO <= 90 Or ANGULO > 270 Then 'quadrante 1 e 4
                    .getPerpendicularPoint geom_id, object_id, (l / 3) * 2, 2, X, Y
                ElseIf ANGULO = 270 Then
                    .getPerpendicularPoint geom_id, object_id, (l / 3), -2, X, Y
                Else
                    .getPerpendicularPoint geom_id, object_id, (l / 3), -2, X, Y
                End If
                .InsertTextInDatabase object_id, Diametro, 1, X, Y, IIf(ANGULO = 270, 90, ANGULO), , , True
                ' --- Material em baixo, esquerda
                If ANGULO <= 90 Or ANGULO > 270 Then 'Q1 Q4
                    .getPerpendicularPoint geom_id, object_id, (l / 3), 2, X, Y
                ElseIf ANGULO = 270 Then
                    .getPerpendicularPoint geom_id, object_id, (l / 3) * 2, -2, X, Y
                Else 'Q2 Q3
                    .getPerpendicularPoint geom_id, object_id, (l / 3) * 2, -2, X, Y
                End If
                .InsertTextInDatabase object_id, IIf(Material = "0", "", Material), 1, X, Y, IIf(ANGULO = 270, 90, ANGULO), , , True
            Else
                If ANGULO <= 90 Or ANGULO > 270 Then 'quadrante 1 e 4
                    .getPerpendicularPoint geom_id, object_id, (l / 2), 2, X, Y
                ElseIf ANGULO = 270 Then
                    .getPerpendicularPoint geom_id, object_id, (l / 2), -2, X, Y
                Else
                    .getPerpendicularPoint geom_id, object_id, (l / 2), -2, X, Y
                End If
                .InsertTextInDatabase object_id, Diametro, 1, X, Y, IIf(ANGULO = 270, 90, ANGULO), , , True
                ' --- Material em baixo, esquerda
                If ANGULO <= 90 Or ANGULO > 270 Then 'Q1 Q4
                    .getPerpendicularPoint geom_id, object_id, (l / 2), 3.5, X, Y
                ElseIf ANGULO = 270 Then
                    .getPerpendicularPoint geom_id, object_id, (l / 2), -3.5, X, Y
                Else 'Q2 Q3
                    .getPerpendicularPoint geom_id, object_id, (l / 2), -3.5, X, Y
                End If
                .InsertTextInDatabase object_id, IIf(Material = "0", "", Material), 1, X, Y, IIf(ANGULO = 270, 90, ANGULO), , , True
            End If
            '--- Comprimento em cima da linha , no meio
            If ANGULO <= 90 Or ANGULO > 270 Then 'Q1 Q4
            .getPerpendicularPoint geom_id, object_id, (l / 2), -0.5, X, Y
            ElseIf ANGULO = 270 Then
            .getPerpendicularPoint geom_id, object_id, (l / 2), 0.5, X, Y
            Else 'Q2 Q3
            .getPerpendicularPoint geom_id, object_id, (l / 2), 0.5, X, Y
            End If
            .InsertTextInDatabase object_id, Round(comprimento, 2), 1, X, Y, IIf(ANGULO = 270, 90, ANGULO), , , True
        Else
            '--- Inclinação a esquerda
            If ANGULO <= 90 Or ANGULO > 270 Then
                .getPerpendicularPoint geom_id, object_id, (l / 3), 2, X, Y
            ElseIf ANGULO = 270 Then
                .getPerpendicularPoint geom_id, object_id, (l / 3) * 2, -2, X, Y
            Else
                .getPerpendicularPoint geom_id, object_id, (l / 3) * 2, -2, X, Y
            End If
            If Not (CotaI = 0 Or CotaF = 0) Then
                .InsertTextInDatabase object_id, Round(((CotaI - CotaF) / comprimento) * 100, 2) & "%", 1, X, Y, ANGULO, , , True
            Else
                .InsertTextInDatabase object_id, 0 & "%", 1, X, Y, ANGULO, , , True
            End If
            '--- Diametro em baixo, direita
            If ANGULO <= 90 Or ANGULO > 270 Then 'quadrante 1 e 4
                .getPerpendicularPoint geom_id, object_id, (l / 3) * 2, 2, X, Y
            ElseIf ANGULO = 270 Then
                .getPerpendicularPoint geom_id, object_id, (l / 3), -2, X, Y
            Else
                .getPerpendicularPoint geom_id, object_id, (l / 3), -2, X, Y
            End If
            .InsertTextInDatabase object_id, Diametro, 1, X, Y, IIf(ANGULO = 270, 90, ANGULO), , , True
            '--- Material em baixo, meio
            If ANGULO <= 90 Or ANGULO > 270 Then
                .getPerpendicularPoint geom_id, object_id, (l / 2), 2, X, Y
            ElseIf ANGULO = 270 Then
                .getPerpendicularPoint geom_id, object_id, (l / 2), -2, X, Y
            Else
                .getPerpendicularPoint geom_id, object_id, (l / 2), -2, X, Y
            End If
            .InsertTextInDatabase object_id, IIf(Material = "0", "", Material), 1, X, Y, IIf(ANGULO = 270, 90, ANGULO), , , True
            '--- Comprimento em cima da linha , no meio
            If ANGULO <= 90 Or ANGULO > 270 Then
                .getPerpendicularPoint geom_id, object_id, (l / 2), -0.5, X, Y
            ElseIf ANGULO = 270 Then
                .getPerpendicularPoint geom_id, object_id, (l / 2), 0.5, X, Y
            Else
                .getPerpendicularPoint geom_id, object_id, (l / 2), 0.5, X, Y
            End If
            .InsertTextInDatabase object_id, Round(comprimento, 2), 1, X, Y, IIf(ANGULO = 270, 90, ANGULO), , , True
            With tdb
                '--- CotaInicial
                If ANGULO <= 90 Or ANGULO > 270 Then
                    .getPerpendicularPoint geom_id, object_id, (l * 0.15), -0.5, X, Y
                ElseIf ANGULO = 270 Then
                    .getPerpendicularPoint geom_id, object_id, (l * 0.15), 0.5, X, Y
                Else
                    .getPerpendicularPoint geom_id, object_id, (l * 0.15), 0.5, X, Y
                End If
                .InsertTextInDatabase object_id, CotaI, 1, X, Y, ANGULO, , , True
                '--- CotaFinal
                If ANGULO <= 90 Or ANGULO > 270 Then
                    .getPerpendicularPoint geom_id, object_id, (l * 0.85), -0.5, X, Y
                ElseIf ANGULO = 270 Then
                    .getPerpendicularPoint geom_id, object_id, (l * 0.85), 0.5, X, Y
                Else
                    .getPerpendicularPoint geom_id, object_id, (l * 0.85), 0.5, X, Y
                End If
                .InsertTextInDatabase object_id, CotaF, 1, X, Y, ANGULO, , , True
            End With
        End If
    End With
    Exit Function

Trata_Erro:
    If Err.Number = 0 Or Err.Number = 20 Then
        Resume Next
    Else
        PrintErro "clsGeoReference", "Public Function InsertTextInDatabase", CStr(Err.Number), CStr(Err.Description), True
    End If
    'InsertTextInDatabase_Err:
    'MsgBox "Não foi possível inserir o texto automaticamente,contate o suporte:(011) 381602034 - Nexus ", vbExclamation, "Atenção"
End Function
' Obtem a identificação do layer
' Retorna o número de identificação do layer
'
' LayerName_ - nome do layer
'
Public Function GetLayerID(LayerName_ As String) As Integer
    On Error GoTo Trata_Erro
    Dim rs As ADODB.Recordset
    Dim a As String
    Dim b As String
    Dim c As String
    
    a = "layer_id"
    b = "te_layer"
    c = "name"
    If frmCanvas.TipoConexao <> 4 Then
        Set rs = Conn.execute(convertQuery("SELECT Layer_id from Te_Layer where name='" & LayerName_ & "' or name='" & UCase(LayerName_) & "'", CInt(typeconnection)))
        If rs.EOF = False Then
            GetLayerID = rs(0).value            'obtem o número do layer (id)
        End If
        rs.Close
        Set rs = Nothing
    Else
        Set rs = Conn.execute(convertQuery("SELECT " + """" + a + """" + " from " + """" + b + """" + " where " + """" + c + """" + "='" & LayerName_ & "'", CInt(typeconnection)))
        If rs.EOF = False Then
            GetLayerID = rs(0).value            'obtem o número do layer (id)
        End If
        rs.Close
        Set rs = Nothing
    End If
    Exit Function
    
Trata_Erro:
    If Err.Number = 0 Or Err.Number = 20 Then
        Resume Next
    Else
        PrintErro "clsGeoReference", "Public Function GetLayerID", CStr(Err.Number), CStr(Err.Description), True
    End If
End Function

Public Function GetLayerAttrib(LayerName_ As String, layer_id As Integer, Attr_Link As String) As Boolean
On Error GoTo Trata_Erro
   Dim rs As ADODB.Recordset
   Set rs = Conn.execute(convertQuery("SELECT l.Layer_id, t.attr_link from Te_Layer_Table t inner join te_layer l on l.layer_id=t.layer_id where l.name='" & LayerName_ & "'", CInt(typeconnection)))
   If rs.EOF Then
      GetLayerAttrib = False
   Else
      layer_id = rs(0).value
      Attr_Link = rs(1).value
      GetLayerAttrib = True
   End If
   rs.Close
   Set rs = Nothing

Trata_Erro:
    If Err.Number = 0 Or Err.Number = 20 Then
       Resume Next
    Else
       
      PrintErro "clsGeoReference", "Public Function GetLayerAttrib", CStr(Err.Number), CStr(Err.Description), True
       
    End If

End Function

Public Function DeleteTextObjectWithInsertText(LayerName As String, geom_id As Long, Object_id_ As String) As Boolean
On Error GoTo Trata_Erro
   Dim rs As ADODB.Recordset, IDLayer As Integer
   Dim gus As String
   Dim Diameter As String, Lenght As Double, Material As String
a = "layer_id"
b = "te_layer"
c = "name"
d = "OBJECT_ID"
e = Object_id_
f = "'e'"
g = "X_MATERIAL"
h = "MATERIALID"
ii = "MATERIAL"
   If frmCanvas.TipoConexao <> 4 Then
   Set rs = Conn.execute(convertQuery("SELECT layer_id from te_layer where name='" & LayerName & "'", CInt(typeconnection)))
   Else
   Set rs = Conn.execute(convertQuery("SELECT " + """" + "layer_id" + """" + " from " + """" + "te_layer" + """" + " where " + """" + "name" + """" + "='" & LayerName & "'", CInt(typeconnection)))
   
   End If
   If Not rs.EOF Then
      IDLayer = rs(0)
      If frmCanvas.TipoConexao = 4 Then
      gus = str(IDLayer)
      gus = Trim(gus)
      End If
      
      rs.Close
      Set rs = Nothing
      'problema
      'A linha abaixo foi comentada para correção no dia 31/05/2007
      'Conn.execute "DeleteTXTVisual " & IDLayer & "," & object_id_
      DeleteTXTVisual IDLayer, Object_id_
      'Conn.execute "Delete From texts" & IDLayer & "_txvisual where geom_id in(SELECT geom_id from texts" & IDLayer & " where object_id = '" & object_id_ & "')"
      
      
      
      
a = "texts"
b = IDLayer
c = "b"
d = "object_id"
e = "OBJECT_ID_"
f = "'e'"
g = "X_MATERIAL"
h = "MATERIALID"
ii = "MATERIAL"
Dim zz As String
Dim za As String
za = a + b
zz = Trim(za)
      If frmCanvas.TipoConexao <> 4 Then
      Conn.execute "Delete From Texts" & IDLayer & " Where object_id = " & Object_id_
      Else
     ' MsgBox "Delete From " + """" + zz + """" + " Where " + """" + d + """" + " =  '" & Val(Object_id_) & "'"
      Conn.execute "Delete From " + """" + "texts" + gus + """" + " Where " + """" + d + """" + " =  '" & Val(Object_id_) & "'"
      End If
      
      If frmCanvas.TipoConexao <> 4 Then
      Set rs = Conn.execute("SELECT * from " & LayerName & " Left Join X_Material on MaterialId=Material where object_id_='" & Object_id_ & "'")
      Else
          Set rs = Conn.execute("SELECT * from " + """" + LayerName + """" + " Left Join" + """" + g + """" + " on " + """" + h + """" + "=" + """" + ii + """" + " where " + """" + e + """" + "='" & Val(Object_id_) & "'")
      End If
      
      
  
      If Not rs.EOF Then
         Diameter = Chr$(216) & " " & rs.Fields("InternalDiameter")
         Lenght = IIf(rs.Fields("length") = 0, rs.Fields("lengthcalculated"), rs.Fields("length"))
         Material = IIf(IsNull(rs.Fields("MaterialName")), "0", rs.Fields("MaterialName"))
         If UCase(LayerName) = "WATERLINES" Then
            InsertTextInDatabase geom_id, Object_id_, Diameter, Material, Lenght
         Else
            InsertTextInDatabase geom_id, Object_id_, Diameter, Material, Lenght, True, rs!INITIALTUBEDEEPNESS, rs!FINALTUBEDEEPNESS
         End If
      End If
   End If
   rs.Close
   Set rs = Nothing
   
Trata_Erro:
   If Err.Number = 0 Or Err.Number = 20 Then
      Resume Next
   Else
       
      PrintErro "clsGeoReference", "Public Function DeleteTextObjectWithInsertText", CStr(Err.Number), CStr(Err.Description), True
   
   End If
   
End Function
' Apaga uma rede de água, esgoto ou drenagem, eliminando os trechos e nós das redes
'
'
Public Function DeleteRede(LayerName As String, object_id As String)
    On Error GoTo Trata_Erro
    Dim rs As ADODB.Recordset, RsCount As ADODB.Recordset, strsql As String
    Dim LayId As Integer, LayIdRef As Integer
    Dim localErro As String

    localErro = "0"
    If frmCanvas.TipoConexao <> 4 Then
        Set rs = Conn.execute(convertQuery("SELECT Layer_id from Te_Layer where name='" & GetReferenceLayer(LayerName) & "'", CInt(typeconnection)))
    Else
        Dim aa As String
        Dim ab As String
        Dim ac As String
        aa = "layer_id"
        ab = "te_layer"
        ac = "name"
        Set rs = Conn.execute(convertQuery("SELECT " + """" + aa + """" + " from " + """" + ab + """" + " where " + """" + ac + """" + "='" & GetReferenceLayer(LayerName) & "'", CInt(typeconnection)))
    End If
    localErro = "1"
    LayId = rs(0).value
    If frmCanvas.TipoConexao <> 4 Then
        Set rs = Conn.execute(convertQuery("SELECT Layer_id from Te_Layer where name='" & LayerName & "'", CInt(typeconnection)))
    Else
        Set rs = Conn.execute(convertQuery("SELECT " + """" + aa + """" + " from " + """" + ab + """" + " where " + """" + ac + """" + "='" & LayerName & "'", CInt(typeconnection)))
    End If
    LayIdRef = rs(0).value
    a = "INITIALCOMPONENT"
    b = "FINALCOMPONENT"
    c = "OBJECT_ID_"
    If frmCanvas.TipoConexao <> 4 Then
        Set rs = Conn.execute("SELECT InitialComponent, FinalComponent From " & LayerName & _
        " Where Object_id_ = " & object_id)
    Else
        Set rs = Conn.execute("SELECT " + """" + "INITIALCOMPONENT" + """" + ", " + """" + "FINALCOMPONENT" + """" + " From " + """" + LayerName + """" + "Where " + """" + "OBJECT_ID_" + """" + " = '" & object_id & "'")
    End If
    localErro = "2"
    If Not rs.EOF Then
        If frmCanvas.TipoConexao <> 4 Then
            localErro = "2A"
            Set RsCount = Conn.execute("SELECT count(*) as " + """" + "Qde" + """" + " From " & LayerName & _
            " Where InitialComponent=" & rs.Fields("InitialComponent") & _
            " or FinalComponent = " & rs.Fields("InitialComponent"))
            If RsCount!Qde = 1 Then
                strsql = "'" & rs!INITIALCOMPONENT & "'"
            End If
            RsCount.Close
            Set RsCount = Conn.execute("SELECT count(*) as " + """" + "Qde" + """" + " From " & LayerName & _
            " Where InitialComponent=" & rs.Fields("FinalComponent") & _
            " or FinalComponent = " & rs.Fields("FinalComponent"))
            If RsCount!Qde = 1 Then
                strsql = strsql & IIf(strsql <> "", ",", "") & "'" & rs!FinalComponent & "'"
            End If
            RsCount.Close
        Else
            localErro = "2B"
            Dim aq As String
            aq = LayerName
            Set RsCount = Conn.execute("SELECT count(*) as " + """" + "Qde" + """" + " From " + """" + aq + """" + _
            " Where " + """" + a + """" + "='" & rs.Fields("INITIALCOMPONENT") & "'" + _
            " or " + """" + b + """" + " = '" & rs.Fields("INITIALCOMPONENT") & "'")
            If RsCount!Qde = 1 Then
                strsql = "'" & rs!INITIALCOMPONENT & "'"
            End If
            RsCount.Close
            Set RsCount = Conn.execute("SELECT count(*) as " + """" + "Qde" + """" + " From " + """" + aq + """" + _
            " Where " + """" + a + """" + "='" & rs.Fields("FINALCOMPONENT") & "'" + _
            " or " + """" + b + """" + " = '" & rs.Fields("FINALCOMPONENT") & "'")
            If RsCount!Qde = 1 Then
                strsql = strsql & IIf(strsql <> "", ",", "") & "'" & rs!FinalComponent & "'"
            End If
            RsCount.Close
        End If
    End If
    rs.Close
    localErro = "3"
    Set rs = Nothing
    Dim comandDelete As String
    Dim CMD_Delete As String
    Dim RefLayer As String
    RefLayer = GetReferenceLayer(LayerName)
    If strsql <> "" Then
        a = RefLayer
        b = "+a+"
        c = "OBJECT_ID_"
        d = "points"
        e = LayId
        f = "e"
        g = "DATA"
        h = "texts"
        ii = "object_id"
        Dim d2 As String
        d2 = "object_id_"
        If frmCanvas.TipoConexao <> 4 Then
            localErro = "3A"
            CMD_Delete = "Delete From " & RefLayer & "data where object_id_ In (" & strsql & ")"
            Conn.execute (CMD_Delete)
            CMD_Delete = "Delete From " & RefLayer & " where object_id_ In (" & strsql & ")"
            Conn.execute (CMD_Delete)
            CMD_Delete = "Delete From points" & LayId & " where object_id In (" & strsql & ")"
            Conn.execute (CMD_Delete)
            tdb.setCurrentLayer RefLayer
            'VERIFICA SE OS NÓS POSSUEM TEXTOS PARA SEREM DELETADOS
            If Not tdb.existsRepresentation(128) = 0 Then
                CMD_Delete = "Delete From texts" & LayId & " where object_id In (" & strsql & ")"
                Conn.execute (CMD_Delete)
            End If
        Else
            localErro = "3B"
            CMD_Delete = "Delete From " + """" + a + g + """" + " where " + """" + c + """" + " In (" & strsql & ")"
            Conn.execute (CMD_Delete)
            CMD_Delete = "Delete From " + """" + a + """" + " where " + """" + "OBJECT_ID_" + """" + " In (" & strsql & ")"
            Conn.execute (CMD_Delete)
            CMD_Delete = "Delete From " + """" + d + e + """" + " where " + """" + ii + """" + " In (" & strsql & ")"
            Conn.execute (CMD_Delete)
            tdb.setCurrentLayer RefLayer
            'VERIFICA SE OS NÓS POSSUEM TEXTOS PARA SEREM DELETADOS
            If Not tdb.existsRepresentation(128) = 0 Then
                CMD_Delete = "Delete From " + """" + h + e + """" + " where " + """" + ii + """" + " In (" & strsql & ")"
            End If
            Conn.execute (CMD_Delete)
        End If
    End If
    localErro = "4"
    If frmCanvas.TipoConexao <> 4 Then
        Conn.execute "Delete From " & LayerName & "Data where object_id_ = '" & object_id & "'"
        Conn.execute "Delete From " & LayerName & " where object_id_ = '" & object_id & "'"
        DeleteTXTVisual LayIdRef, object_id
        Conn.execute "Delete From texts" & LayIdRef & " where object_id = '" & object_id & "'"
        Conn.execute "Delete From lines" & LayIdRef & " where object_id = '" & object_id & "'"
    Else
        localErro = "5"
        Dim d1 As String
        d1 = "object_id"
        a = LayerName
        b = "DATA"
        c = LayIdRef
        e = "c"
        d = "OBJECT_ID_"
        f = "texts"
        g = "lines"
        CMD_Delete = "Delete From " + """" + a + b + """" + " where " + """" + "OBJECT_ID_" + """" + " = '" & object_id & "' "
        Conn.execute (CMD_Delete)
        Conn.execute "Delete From " + """" + a + """" + " where " + """" + d + """" + " = '" & object_id & "'"
        DeleteTXTVisual LayIdRef, object_id
        f = "texts"
        c = LayIdRef
        CMD_Delete = "Delete From " + """" + f + c + """" + " where " + """" + d1 + """" + " = '" & object_id & "' "
        Conn.execute (CMD_Delete)
        c = LayIdRef
        g = "lines"
        CMD_Delete = "Delete From " + """" + g + c + """" + " where " + """" + d1 + """" + " = '" & object_id & "' "
        Conn.execute (CMD_Delete)
    End If

Trata_Erro:
    If Err.Number = 0 Or Err.Number = 20 Then
        Resume Next
    Else
        PrintErro "clsGeoReference - Local Erro:" + localErro, "Public Function DeleteRede", CStr(Err.Number), CStr(Err.Description), True
    End If
End Function
'###############################################################
' Autor     : Luis Claudio Rodrigues Domingues    Data: 20/12/05
' Nome      : SELECTRede
' Descrição : Colore a rede até encontrar válvula
'###############################################################
'a partir do object_id selecionado pelo usuário ele vai localizar todos os trechos de redes até encontrar registros. Válvulas (VRPs) e registros fixos (divisa de setor de abastecimento), não são considerados. Ele procura somente até encontrar um nó do tipo REGISTRO
'
'
Public Function SELECTRede(Object_id_ As String)
    On Error GoTo Trata_Erro
    Dim rs As ADODB.Recordset
    Dim iContA As Integer, iContB As Integer
    Dim MyLocalLines() As String        'object_id s do vetor de trechos de redes que estão conectados aos nós inicial e final do último trecho de rede pesquisado
    Dim iLn As Integer, iPnt
    Dim MyComponents(1) As String
    Dim ACHOU As Boolean
    Dim rsType As ADODB.Recordset
    Dim fg As String
    Dim fh As String
    Dim fi As String
    Dim fj As String
    Dim fl As String
    Dim fm As String
    Dim fn As String
    Dim fo As String
    Dim ga As String
    Dim ga2 As String
    
    Set rsType = New ADODB.Recordset
    'Obtem todos os tipos de componentes de rede (nós da rede) que podem ser cadastrados
    If frmCanvas.TipoConexao <> 4 Then
        'Caso seja SQLServer ou Oracle
        rsType.Open "SELECT ID_TYPE,DESCRIPTION_,SPECIFICATION_ FROM WATERCOMPONENTSTYPES", Conn, adOpenDynamic, adLockReadOnly
    Else
        fg = "ID_TYPE"
        fh = "DESCRIPTION_"
        fi = "SPECIFICATION_"
        fj = "WATERCOMPONENTSTYPES"
        fl = "INITIALCOMPONENT"
        fm = "FINALCOMPONENT"
        fn = "WATERLINES"
        fo = "OBJECT_ID_"
        ga = "WATERLINES"
        ga2 = "WATERCOMPONENTS"
        'MsgBox "SELECT " + """" + fg + """" + "," + """" + fh + """" + "," + """" + fi + """" + " FROM " + """" + fj + ""
        rsType.Open "SELECT " + """" + fg + """" + "," + """" + fh + """" + "," + """" + fi + """" + " FROM " + """" + fj + """" + "", Conn, adOpenDynamic, adLockOptimistic
    End If
    'verifica se a tabela contém registros, caso contrário não é possível realizar esta funcionalidade, existe erro na base de dados
    If rsType.EOF = True Then
        MsgBox "Não encontrou os tipos de componentes de rede existentes na tabela WATERCOMPONENTSTYPES. Saindo da funcionalidade."
        Exit Function
    End If
    'obtem o número do nó inicial e o número do nó final do trecho de rede de água selecionado
    If frmCanvas.TipoConexao <> 4 Then
        'se SQLServer ou Oracle
        Set rs = Conn.execute("SELECT InitialComponent,FinalComponent from waterlines where object_id_='" & Object_id_ & "'")
    Else
        'se Postgres
        Set rs = Conn.execute("SELECT " + """" + fl + """" + "," + """" + fm + """" + " from " + """" + ga + """" + " where " + """" + fo + """" + "='" & Object_id_ & "'")
    End If
    If Not stopProcess Then
        DoEvents
        If Not rs.EOF Then
            MyComponents(0) = rs(0)         'número do nó inicial do trecho de rede
            MyComponents(1) = rs(1)         'número do nó final do trecho de rede
            rs.Close                        'fecha a conexão que obteve os nós do trecho de rede
            Set rs = Nothing
            'processa para os dois nós
            For iContA = 0 To 1
continuaFor:
                'obtem o tipo de nó a partir do nó inicial (primeira passada pelo For), e a partir do nó final (segunda passada pelo For), para saber se é um registro e deve parar a procura
                If frmCanvas.TipoConexao <> 4 Then
                    'se SQLServer ou Oracle
                    Set rs = Conn.execute("SELECT iD_type from watercomponents where object_id_='" & MyComponents(iContA) & "'")
                Else
                    'se Postgres
                    Set rs = Conn.execute("SELECT " + """" + fg + """" + " from " + """" + ga2 + """" + " where " + """" + fo + """" + "='" & MyComponents(iContA) & "'")
                End If
                'verifica em WATERCOMPONENTS se encontrou o nó inicial ou final (dependendo da passagem do For) que estava especificado na tabela WATERLINES
                If Not rs.EOF Then
                    'encontrou o nó em WATERCOMPONENTS que WATERLINES dizia existir (inicial ou final dependendo da passagem do For)
                    ACHOU = False
                    rsType.Filter = "ID_TYPE='" & rs!id_Type & "'"
                    If rsType.EOF = False Then
                        If Trim(rsType!Specification_) = "REGISTER" Then       'verifica se é um ponto de parada
                            ACHOU = True
                        End If
                    End If
                    'O PROBLEMA ESTÁ AQUI POIS QUANDO ELE ACHA UMA VALVULA(VALVE) ELE ABANDONA SE PROCURAR O OUTRO LADO
                    If ACHOU = True Then 'valor original = 1 mas não há nada cadastrado como tipo 1
                        If iContA = 0 Then
                            iContA = 1
                            GoTo continuaFor:
                        End If
                        'Exit For
                    Else
                        rs.Close
                        Set rs = Nothing
                        'procura por outros trechos de redes os quais estão conectados aos nós inicial e final
                        If frmCanvas.TipoConexao <> 4 Then
                            Set rs = Conn.execute("SELECT object_id_ from waterlines where InitialComponent=" & MyComponents(iContA) & " or FinalComponent= " & MyComponents(iContA))
                        Else
                            ' Set rs = Conn.execute("SELECT " + """" + fo + """" + " from " + """" + fn + """" + " where " + """" + fl + """" + "='" & MyComponents(iContA) & "' or " + """" + fm + """" + "= '" & MyComponents(iContA)) & "'"
                            Set rs = Conn.execute("SELECT " + """" + "OBJECT_ID_" + """" + " from " + """" + "WATERLINES" + """" + " where " + """" + "INITIALCOMPONENT" + """" + "=" & MyComponents(iContA) & " or " + """" + "FINALCOMPONENT" + """" + "= " & MyComponents(iContA))
                        End If
                        While Not rs.EOF 'caso tenha encontrado outros trechos de rede (novos trechos) conectados as extremidades inicial e final
                            ReDim Preserve MyLocalLines(iLn)
                            MyLocalLines(iLn) = rs!Object_id_
                            iLn = iLn + 1
                            rs.MoveNext
                        Wend
                        iLn = iLn - 1
                        rs.Close
                        Set rs = Nothing
                        For iPnt = 0 To iLn
                            SubSELECTRede MyLocalLines(iPnt)
                        Next
                    End If
                Else
                    'não encontrou o nó em WATERCOMPONENTS, existe uma inconsistência no banco de dados
                    MsgBox "Não foi encontrado o nó em WATERCOMPONETS de número " & MyComponents(iContA) & "O trecho de rede número " & Object_id_ & " com nó inicial " & MyComponents(0) & " e nó final " & MyComponents(1) & " foi detectada uma inconsistência na base de dados. Rodar o ValidaBase e analisar o arquivo de log gerado pelo mesmo."
                    rs.Close
                    Set rs = Nothing
                End If
            Next
        Else
            rs.Close
            Set rs = Nothing
        End If
    End If
    If Not (rs Is Nothing) Then
        If rs.State = adStateOpen Then
            rs.Close
        End If
    End If
    Set rs = Nothing
    Exit Function

Trata_Erro:
   If Err.Number = 0 Or Err.Number = 20 Then
       Resume Next
   Else
      PrintErro "clsGeoReference", "Public Function DeleteTextObjectWithInsertText", CStr(Err.Number), CStr(Err.Description), True
   End If
End Function

Private Sub SubSELECTRede(Object_id_ As String)
    Dim a As Integer, naoChamar As Boolean
    If Not p Then
        ReDim Preserve MyLines(i)
        p = True
    End If
    For a = 0 To i
        If MyLines(a) = Object_id_ Then
            naoChamar = True
        End If
    Next
    If Not naoChamar Then
        FrmProcess.txtRecord = i
        FrmProcess.Refresh
        MyLines(i) = Object_id_
        If Object_id_ <> "" Then
            If object_ids = "" Then
                object_ids = "'" & Object_id_ & "'"
            Else
                object_ids = object_ids & ",'" & Object_id_ & "'"
            End If
      End If
      i = i + 1
      ReDim Preserve MyLines(i)
      tcs.setDetachedLineStyle 3, 1, RGB(255, 255, 0), True
      tcs.addDetachedIds tpLINES, , Object_id_
      SELECTRede Object_id_
   End If
End Sub


'###############################################################
' Autor     : Luis Claudio Rodrigues Domingues    Data: 20/12/05
' Nome      : GetDeclivity
' Descrição : Colori a rede seguindo a declividade
'###############################################################
Public Function GetDeclivity(LayerName As String, RefLayer As String, Obj As String)
   Dim rs As ADODB.Recordset
   Dim iContA As Integer, iContB As Integer
   Dim MyLocalLines() As String, iLn As Integer, iPnt
   Dim MyComponents(1) As String
   Dim cota(1) As Double
   Dim NextNo As String


a = "INITIALCOMPONENT"
b = "FINALCOMPONENT"
c = "INITIALGROUNDHEIGHT"
d = "FINALGROUNDHEIGHT"
e = "OBJECT_ID_"
   If frmCanvas.TipoConexao <> 4 Then
   Set rs = Conn.execute("SELECT InitialComponent,FinalComponent,InitialGroundHeight,FinalGroundHeight from " & LayerName & " where object_id_='" & Obj & "'")
   Else
    Set rs = Conn.execute("SELECT " + """" + a + """" + "," + """" + b + """" + "," + """" + c + """" + "," + """" + d + """" + " from " + """" + LayerName + """" + " where " + """" + e + """" + "='" & Obj & "'")
   End If
   If Not rs.EOF Then
      MyComponents(0) = rs(0)
      MyComponents(1) = rs(1)
      cota(0) = rs!INITIALGROUNDHEIGHT
      cota(1) = rs!FINALGROUNDHEIGHT
      rs.Close
      Set rs = Nothing
      If frmCanvas.TipoConexao <> 4 Then
     ' Set rs = Conn.execute("SELECT object_id_, InitialGroundHeight,FinalGroundHeight from " & LayerName & " " & _
              ' "where (InitialComponent in (" & MyComponents(0) & "," & MyComponents(1) & ") " & _
              ' "Or FinalComponent in (" & MyComponents(0) & "," & MyComponents(1) & ")) " & _
              ' "and FinalGroundHeight >= " & Replace(cota(1), ",", ".") & " and InitialGroundHeight >= " & Replace(cota(0), ",", "."))
               
               Set rs = Conn.execute("SELECT object_id_, InitialGroundHeight,FinalGroundHeight from " & LayerName & " " & _
               "where (InitialComponent in (" & MyComponents(0) & "," & MyComponents(1) & ") " & _
               "Or FinalComponent in (" & MyComponents(0) & "," & MyComponents(1) & ")) " & _
               "and FinalGroundHeight > " & Replace(cota(1), ",", ".") & " and InitialGroundHeight > " & Replace(cota(0), ",", "."))
               
            
            '    WritePrivateProfileString "A", "A", SQL, App.path & "\DEBUG.INI"
             
               
               Else
                 Set rs = Conn.execute("SELECT " + """" + e + """" + "," + """" + c + """" + "," + """" + d + """" + " from " + """" + LayerName + """" + " " & _
               "where (" + """" + a + """" + " in ('" & MyComponents(0) & "','" & MyComponents(1) & "') " & _
               "Or " + """" + b + """" + " in ('" & MyComponents(0) & "','" & MyComponents(1) & "')) " & _
               "and " + """" + d + """" + " > '" & Replace(cota(1), ",", ".") & "' and " + """" + c + """" + " > '" & Replace(cota(0), ",", ".") & "'")
               
               End If
               
         

               
      tcs.setDetachedLineStyle 3, 1, RGB(255, 255, 0), True
      tcs.addDetachedIds tpLINES, , Obj
      While Not rs.EOF
         ReDim Preserve MyLocalLines(iLn)
         MyLocalLines(iLn) = rs!Object_id_
         iLn = iLn + 1
         rs.MoveNext
      Wend
      iLn = iLn - 1
      rs.Close
      Set rs = Nothing
      For iPnt = 0 To iLn
         SubSELECTDeclivity LayerName, RefLayer, MyLocalLines(iPnt)
      Next
   End If
   If Not (rs Is Nothing) Then
      If rs.State = adStateOpen Then rs.Close
   End If
   Set rs = Nothing
End Function

Private Sub SubSELECTDeclivity(LayerName As String, RefLayer As String, Object_id_ As String)
   Dim a As Integer, naoChamar As Boolean
   If Not p Then
      ReDim Preserve MyLines(i)
      p = True
   End If
   For a = 0 To i
     If MyLines(a) = Object_id_ Then naoChamar = True
   Next
   If Not naoChamar Then
     FrmProcess.txtRecord = i
     FrmProcess.Refresh
     MyLines(i) = Object_id_
     i = i + 1
     ReDim Preserve MyLines(i)
     GetDeclivity LayerName, RefLayer, Object_id_
   End If
End Sub

Public Function GetLinesValues(Obj As String, RSNos As ADODB.Recordset, mtype As Long) As String
   Dim rs As ADODB.Recordset, RsN As ADODB.Recordset
   Dim iContA As Integer, iContB As Integer
   Dim MyLocalLines() As String, iLn As Integer, iPnt
   Dim MyComponents(1) As String
   Dim cota(1) As Double
   Dim NextNo As String
   
   'If Obj = "7705" Then Stop
   a = "INITIALCOMPONENT"
b = "FINALCOMPONENT"
c = "INITIALGROUNDHEIGHT"
d = "FINALGROUNDHEIGHT"
e = "OBJECT_ID_"
f = "WATERLINES"
g = "WATERCOMPONENTS"
h = "CALCULENODE"
   If frmCanvas.TipoConexao <> 4 Then
   Set rs = Conn.execute("SELECT InitialComponent,FinalComponent,InitialGroundHeight,FinalGroundHeight from waterlines where object_id_='" & Obj & "'")
   Else
      Set rs = Conn.execute("SELECT " + """" + a + """" + "," + """" + b + """" + "," + """" + c + """" + "," + """" + d + """" + " from " + """" + f + """" + " where " + """" + e + """" + "='" & Obj & "'")
   
   End If
   If Not rs.EOF Then
      MyComponents(0) = rs(0)
      MyComponents(1) = rs(1)
      rs.Close
      Set rs = Nothing
      
      
      'Verifica de os nós encontrados são de calculo,
       If frmCanvas.TipoConexao <> 4 Then
        Set RsN = Conn.execute("SELECT Object_id_ from watercomponents where object_id_ in(" & _
        MyComponents(0) & "," & MyComponents(1) & ") and calculenode<>0")
        Else
        Set RsN = Conn.execute("SELECT " + """" + e + """" + " from " + """" + g + """" + " where " + """" + e + """" + " in('" & MyComponents(0) & "','" & MyComponents(1) & "') and ""+h+""<>'0'")
        
        End If
      'Esta parte do código tem a pretençao de verificar qual o components final do trecho
      'está com erro
      
      'Rotina que define o trecho
      If RSNos.EOF Then
         RSNos.AddNew
      End If
      If Not RsN.EOF Then
         
         If IsNull(RSNos!cpi) Then
            
            RSNos!cpi = RsN(0)
            RSNos!lin = Obj
            RsN.MoveNext
         End If
      End If
      
      If Not RsN.EOF Then
         If Not IsNull(RSNos!cpi) And (Not RsN(0) = RSNos.Fields("cpI").value) Then
            RSNos!cpf = RsN(0)
            RsN.MoveNext
            
         End If
      Else
         'Resolve aqui
         'quais os trechos ligados ao no que náo é o de calculo
         RSNos!cpf = GetSecundComponent(IIf(RSNos!cpi = MyComponents(0), MyComponents(1), MyComponents(0)), RSNos, mtype)
         
         'Se não for encontrado um nó final de calculo, este trecho deve é ignorado
         If RSNos!cpf = "" Then
            Exit Function
         End If
         
         If RSNos!cpi = MyComponents(0) Then
            MyComponents(1) = RSNos!cpf
         Else
            MyComponents(0) = RSNos!cpf
         End If
      End If
      
      tcs.setDetachedLineStyle 3, 1, RGB(255, 255, 0), True
      tcs.addDetachedIds tpLINES, , Obj
      tcs.redraw
      
      'retorna todos os trechos ligados aos nós do trechos recibido "obj"
      
      '        n1-----l1------n2
      '        |            / |
      '        |         /    |
      '        l4     l5      l2
      '        |    /         |
      '        | /            |
      '        n4-----l3------n3
      
      'Trecho de Entrada = l1
      'Query abaixo retornor = l1,l2,l4,l5
        a = "INITIALCOMPONENT"
b = "FINALCOMPONENT"
c = "INITIALGROUNDHEIGHT"
d = "FINALGROUNDHEIGHT"
e = "OBJECT_ID_"
f = "WATERLINES"
g = "WATERCOMPONENTS"
h = "CALCULENODE"
i = "ID_TYPE"
   If frmCanvas.TipoConexao <> 4 Then
      Set rs = Conn.execute("SELECT object_id_, InitialGroundHeight,FinalGroundHeight from waterlines " & _
               "where (InitialComponent in (SELECT object_id_ from watercomponents where object_id_ in(" & MyComponents(0) & "," & MyComponents(1) & ")) " & _
               "Or FinalComponent in (SELECT object_id_ from watercomponents where object_id_ in(" & MyComponents(0) & "," & MyComponents(1) & "))) " & _
               " and id_type = " & mtype)
      
      Else
           Set rs = Conn.execute("SELECT " + """" + e + """" + "," + """" + c + """" + "," + """" + d + """" + " from " + """" + f + """" + _
               "where (" + """" + a + """" + " in (SELECT " + """" + e + """" + " from " + """" + g + """" + " where " + """" + e + """" + " in('" & MyComponents(0) & "','" & MyComponents(1) & "')) " & _
               "Or " + """" + b + """" + " in (SELECT " + """" + e + """" + " from " + """" + g + """" + " where " + """" + e + """" + " in('" & MyComponents(0) & "','" & MyComponents(1) & "'))) " & _
               " and " + """" + i + """" + " = '" & mtype & "'")
      
      End If
      
      
      While Not rs.EOF
         ReDim Preserve MyLocalLines(iLn)
         MyLocalLines(iLn) = rs!Object_id_
         iLn = iLn + 1
         rs.MoveNext
      Wend
      iLn = iLn - 1
      rs.Close
      Set rs = Nothing
      For iPnt = 0 To iLn
         GetSubPrimaria MyLocalLines(iPnt), RSNos, mtype
      Next
      DoEvents
   
   End If
   If Not (rs Is Nothing) Then
      If rs.State = adStateOpen Then rs.Close
   End If
   Set rs = Nothing
End Function


Public Sub GetSubPrimaria(Object_id_ As String, rs As ADODB.Recordset, mtype As Long)
   'Registra e chama quem sera o proximo
   Dim a As Integer, naoChamar As Boolean
   If Not p Then
      ReDim Preserve MyLines(i)
      p = True
   End If
   For a = 0 To i
     If MyLines(a) = Object_id_ Then naoChamar = True
   Next
   If Not naoChamar Then
     FrmProcess.txtRecord = i
     FrmProcess.Refresh
     MyLines(i) = Object_id_
     i = i + 1
     ReDim Preserve MyLines(i)
     rs.Filter = "lin=" & Object_id_
     GetLinesValues Object_id_, rs, mtype
     
   End If

End Sub


Private Function GetSecundComponent(Node_Object_id_ As String, RsTmp As ADODB.Recordset, mtype As Long) As String
   'Registra e chama quem sera o proximo
   Dim RsTrechos As New ADODB.Recordset, a As Integer, naoChamar As Boolean
   Dim rsNodes As New ADODB.Recordset
   
 a = "INITIALCOMPONENT"
b = "FINALCOMPONENT"
c = "INITIALGROUNDHEIGHT"
d = "FINALGROUNDHEIGHT"
e = "OBJECT_ID_"
f = "WATERLINES"
g = "WATERCOMPONENTS"
h = "CALCULENODE"
i = "iD_TYPE"
   If frmCanvas.TipoConexao <> 4 Then
   'retorna os trechos ligados ao no que não de calculo
   RsTrechos.Open "SELECT object_id_, InitialComponent,FinalComponent from waterlines where (FinalComponent=" & Node_Object_id_ & " or  InitialComponent= " & Node_Object_id_ & ") and id_type =" & mtype, Conn
   Else
    RsTrechos.Open "SELECT " + """" + e + """" + ", " + """" + a + """" + "," + """" + b + """" + " from " + """" + f + """" + " where (" + """" + b + """" + "='" & Node_Object_id_ & "' or  " + """" + a + """" + "= '" & Node_Object_id_ & "') and " + """" + i + """" + " ='" & mtype & "', Conn"
   End If
   While Not RsTrechos.EOF
   
      For a = 0 To i
        If MyLines(a) = RsTrechos!Object_id_ Then naoChamar = True
      Next
      If Not naoChamar Then 'vai chamar=false
        MyLines(i) = RsTrechos!Object_id_
        i = i + 1
        ReDim Preserve MyLines(i)
        
        
        If RsTrechos!INITIALCOMPONENT = Node_Object_id_ Then
        If frmCanvas.TipoConexao <> 4 Then
           rsNodes.Open "SELECT * from watercomponents where object_id_='" & RsTrechos!FinalComponent & "'", Conn
           Else
           rsNodes.Open "SELECT * from " + """" + g + """" + " where " + """" + e + """" + "='" & RsTrechos!FinalComponent & "'", Conn
           End If
           If rsNodes!CALCULENODE = 0 Then
              GetSecundComponent RsTrechos!FinalComponent, RsTmp, mtype
           Else
              GetSecundComponent = RsTrechos!FinalComponent
           End If
        Else
        If frmCanvas.TipoConexao <> 4 Then
           rsNodes.Open "SELECT * from watercomponents where object_id_='" & RsTrechos!INITIALCOMPONENT & "'", Conn
           Else
           rsNodes.Open "SELECT * from " + """" + g + """" + " where " + """" + e + """" + "='" & RsTrechos!INITIALCOMPONENT & "'", Conn
           End If
           If rsNodes!CALCULENODE = 0 Then
              GetSecundComponent RsTrechos!INITIALCOMPONENT, RsTmp, mtype
           Else
              GetSecundComponent = RsTrechos!INITIALCOMPONENT
           End If
        End If
      End If
      naoChamar = False
      RsTrechos.MoveNext
   Wend
   RsTrechos.Close
   If rsNodes.State = 1 Then
      rsNodes.Close
   End If
   Set RsTrechos = Nothing
   Set rsNodes = Nothing
End Function


Public Sub DeleteTXTVisual(layer_id As Integer, Object_id_ As String)
   On Error GoTo DeleteTXTVisual_Error
   Dim SQLComand As String
a = "texts"
b = layer_id
c = "b"
d = "_txvisual"
f = "geom_id"
g = layer_id
h = "g"
ii = "object_id"
   If frmCanvas.TipoConexao <> 4 Then
   SQLComand = "Delete From texts" & layer_id & "_txvisual where geom_id in (SELECT geom_id from texts" & layer_id & " where object_id = " & Object_id_ & ")"
   Else
   SQLComand = "Delete From " + """" + a + b + d + """" + " where " + """" + f + """" + " in (SELECT " + """" + f + """" + " from " + """" + a + g + """" + " where " + """" + ii + """" + " = '" & Object_id_ & "')"
   End If
   'MsgBox SQLComand
   'Conn.execute ("Delete From texts" & layer_id & "_txvisual where geom_id in (SELECT geom_id from texts" & layer_id & " where object_id = " & Object_id_)
   Conn.execute (SQLComand)
   Exit Sub
DeleteTXTVisual_Error:
   MsgBox Err.Number & Err.Description
   
   Resume Next
End Sub

Public Function IsValidLayerOperation(LayerName As String, T_Reference As LayerTypeRefence) As Boolean
   Dim rs As ADODB.Recordset, layer_id As Integer
   layer_id = GetLayerID(LayerName)
   g = "GS_LAYER_CONFIG"
h = "LAYER_ID"
ii = "TYPE_REFERENCE"


      If frmCanvas.TipoConexao <> 4 Then
   Set rs = Conn.execute("SELECT * from gs_layer_config where layer_id=" & layer_id & " and type_reference=" & T_Reference)
   Else
     Set rs = Conn.execute("SELECT * from " + """" + g + """" + " where " + """" + h + """" + "='" & layer_id & "' and " + """" + ii + """" + "='" & T_Reference & "'")
   
   End If
   If rs.EOF Then
      IsValidLayerOperation = False
   Else
      IsValidLayerOperation = True
   End If
   rs.Close
   Set rs = Nothing
End Function
' Retorna o tipo de layer de referência
'
' LayerName - nome do layer
'
Public Function GetLayerTypeReference(LayerName As String) As LayerTypeRefence
    On Error GoTo Trata_Erro
    
    Dim rs As ADODB.Recordset, layer_id As Integer
    
    layer_id = GetLayerID(LayerName)                            'obtem o id do layer
    Set rs = Conn.execute("SELECT * from gs_layer_config where layer_id=" & layer_id)
    If rs.EOF Then
        If LayerName = "NXGS_V_LIG_COMERCIAL_GPS" Then          'este if foi adicionado para considerar a leitura dos pontos GPS, no endSelect de um ponto, mas esta função endSelect deve ser revisada
            GetLayerTypeReference = CONSUMIDOR_GPS              'informa que o layer de referência é um ponto GPS
        Else
            GetLayerTypeReference = 0
        End If
    Else
       GetLayerTypeReference = rs.Fields("type_reference").value
    End If
    rs.Close
    Set rs = Nothing
    Exit Function

Trata_Erro:
    If Err.Number = 0 Or Err.Number = 20 Then
        Resume Next
    Else
        PrintErro "clsGeoReference", "Public Function GetLayerTypeReference", CStr(Err.Number), CStr(Err.Description), True
    End If
End Function

Public Function GetLayerNameByTypeReference(Refence As LayerTypeRefence) As String
   Dim rs As ADODB.Recordset
  g = "GS_LAYER_CONFIG"
h = "LAYER_ID"
ii = "TYPE_REFERENCE"
   
If frmCanvas.TipoConexao <> 4 Then

   Set rs = Conn.execute("SELECT layer_id from gs_layer_config where type_reference=" & Refence)
   Else
   Set rs = Conn.execute("SELECT " + """" + h + """" + " from " + """" + g + """" + " where " + """" + ii + """" + "='" & Refence & "'")
   End If
   If Not rs.EOF Then
      GetLayerNameByTypeReference = GetLayerNameLayerID(rs.Fields(0).value)
   End If
   rs.Close
   Set rs = Nothing
End Function
' Esta função tem como objetivo obter o layer de referência de uma determinad operação. Ele está informado na tabela S_LAYER_CONFIG_LAYERS
' Esta função simplesmente vai nesta tabela e obtem o nome do layer de referência a partir do layer slecionado pelo usuário e o tipo de operação que o usuário deseja realizar
'
' LayerName - recebe o nome do layer que foi selecionado pelo usuário
' T_Operation - tipo de operação (ver tabela com os tipos de operações GS_LAYER_CONFIG_LAYERS)
'
Public Function GetLayerOperation(LayerName As String, T_Operation As Integer) As String
    Dim rs As ADODB.Recordset, layer_id As Integer
    Dim s As String
    Dim mensagemInformativa As String                   'para informar o que aconteceu simplesmente. Não foi um erro e sim o usuário não selecionou o plano correto.
    
    k = "GS_LAYER_CONFIG_LAYERS"
    h = "LAYER_ID"
    l = "TYPE_OPERATION"
    layer_id = GetLayerID(LayerName)                    'RETORNA O NUMERO ID DE UM DETERMINADO LAYER
    'layer_id = 3
    If frmCanvas.TipoConexao <> 4 Then
        s = "SELECT * from gs_layer_config_layers where layer_id='" & layer_id & "' and type_operation='" & T_Operation & "'"
    Else
        s = "SELECT * from " + """" + k + """" + " where " + """" + h + """" + "='" & layer_id & "' and " + """" + l + """" + "='" & T_Operation & "'"
    End If
    Set rs = New ADODB.Recordset
    rs.Open s, Conn, adOpenDynamic, adLockOptimistic
    If rs.EOF = False Then
        GetLayerOperation = GetLayerNameLayerID(rs("LAYER_REFERENCE").value)
    Else
        'MsgBox "Não foi localizada referencia para para o layer " & LayerName & ", ID = " & layer_id & " na tabela gs_layer_config_layers com o tipo de operação = " & T_Operation, vbExclamation, "" ---- retirado e colocado como mensagem para a NEXUS
        mensagemInformativa = "Mensagem informativa. Usuário não selecionou o plano correto antes de desenhar a rede. Não foi localizada referencia para para o layer " & LayerName & ", ID = " & layer_id & " na tabela gs_layer_config_layers com o tipo de operação = " & T_Operation
        ErroUsuario.Registra "clsGeoReference", "GetLayerOperation", CStr(Err.Number), CStr(Err.Description), False, glo.enviaEmails, mensagemInformativa   'não apresenta nada para o usuário, pois o usuário apenas não selecionou o plano correto antes de desenhar
    End If
    rs.Close
    Set rs = Nothing
End Function
' Função para localizar o nome de um layer na tabela te_layer, tendo o número do mesmo
' Retorna nome do layer a partir do número (id) do layer
'
' layer_id - número (id) do layer a er procurado
'
Public Function GetLayerNameLayerID(layer_id As Integer) As String
    Dim rs As ADODB.Recordset
    Dim s As String
    Dim str As String
    
    Set rs = New ADODB.Recordset
    k = "te_layer"
    h = "layer_id"
    a = "name"
    If frmCanvas.TipoConexao <> 4 Then
        str = "SELECT name from Te_Layer where layer_id=" & layer_id
    Else
        str = "SELECT " + """" + a + """" + " from " + """" + k + """" + " where " + """" + h + """" + "='" & layer_id & "'"
    End If
    Set rs = Conn.execute(str)
    If rs.EOF = False Then
        GetLayerNameLayerID = rs(0).value
    Else
        MsgBox "Falha em: SELECT name from te_Layer where layer_id= " & layer_id & " - Function GetLayerNameLayerID. Tabela GS_LAYER_CONFIG_LAYERS não possui o LAYER_REFERENCE para encontrar os ramais em um lote da Prefeitura (LOTEGIS-não mais utilizada)"
    End If
    rs.Close
    Set rs = Nothing
End Function

Public Sub AddAtributesLinesOut(LayerName As String)
   Dim rs As ADODB.Recordset, str As String, Point1 As String, Point2 As String, compr As Double, a As Integer, X As Double, Y As Double
   If frmCanvas.TipoConexao <> 4 Then
   str = "SELECT object_id from lines38 left join waterlines on object_id=object_id_ where object_id_ is null"
   Else
   a = "INITIALCOMPONENT"
b = "FINALCOMPONENT"
c = "INITIALGROUNDHEIGHT"
d = "FINALGROUNDHEIGHT"
e = "OBJECT_ID_"
f = "WATERLINES"
g = "WATERCOMPONENTS"
h = "lines38"
i = "object_id"
     str = "SELECT " + """" + i + """" + " from " + """" + h + """" + " left join " + """" + f + """" + " on " + """" + i + """" + "=" + """" + e + """" + " where " + """" + e + """" + " is null"
   End If
   Set rs = Conn.execute(str)
   While Not rs.EOF
      compr = 0
      tdb.getLengthOfLine 0, rs(0).value, compr
      For a = 0 To 1
         tdb.getPointOfLine 0, rs(0).value, a, X, Y
         If tdbcon.locateGeometry(X, Y, tpPOINTS, 1) Then
             If a = 0 Then
               Point1 = tdbcon.objectIds(0)
             Else
               Point2 = tdbcon.objectIds(0)
               
                   a = "OBJECT_ID_"
      b = "INITIALCOMPONENT"
      c = "FINALCOMPONENT"
      d = "LENGHTCALCULATED"
      


     If frmCanvas.TipoConexao <> 4 Then
         
     str = "insert into " & LayerName & _
                     " (object_id_,InitialComponent,FinalComponent, lenghtcalculated) values ('" & _
                     rs.Fields("object_id").value & "','" & Point1 & "','" & Point2 & "'," & Replace(compr, ",", ".") & ")"
     
     Else
     
      str = "insert into " + """" + LayerName + """" + " (" + """" + a + """" + "," + """" + b + """" + "," + """" + c + "," + """" + d + """" + ") values ('" & _
                     rs.Fields("object_id").value & "','" & Point1 & "','" & Point2 & "','" & Replace(compr, ",", ".") & "')"
     End If
               
               
               
            
               Conn.execute str
             End If
         End If
      Next
      rs.MoveNext
   Wend
   rs.Close
   Set rs = Nothing
   
a = "lines38"
b = "OBJECT_ID_"
c = "line_id"
d = "WATERLINES"
e = "object_id"
f = "LINE_ID"
    If frmCanvas.TipoConexao <> 4 Then
   Conn.execute "update lines38 set object_id = a.line_id " & _
                "from waterlines a inner join lines38 b on b.object_id = a.object_id_ " & _
                "Where a.line_id <> a.Object_id_"
   Conn.execute "update waterlines set object_id_ = line_id where line_id <> object_id_"
   Else
   Conn.execute "update " + """" + a + """" + " set " + """" + b + """" + " = " + """" + d + """" + "." + """" + f + """" + _
                "from " + """" + d + """" + " inner join " + """" + a + """" + "on " + """" + a + """" + "." + """" + e + """" + " = " + """" + d + """" + "." + """" + b + """" + _
                "Where " + """" + d + """" + "." + """" + f + """" + " <> " + """" + d + """" + "." + """" + b + """" + ""
   Conn.execute "update " + """" + d + """" + " set " + """" + b + """" + " = " + """" + c + """" + " where " + """" + c + """" + " <> " + """" + b + """" + ""
   
   End If
End Sub

'Public Sub Update_Demand_Node()
'   'On Error GoTo Update_Demand_Node_err '
'   Dim IniciouTransacao As Boolean
'   Dim rsLines As ADODB.Recordset, str As String
'   Dim rsNodes As ADODB.Recordset
'   Dim RsSpecfication As New ADODB.Recordset
'   Dim rsTrecho As New ADODB.Recordset, Demanda As Double
'   Dim NOI As String, UltimoNoDemand As String
'   Dim frm As New frmProgressBar, UltimoTrecho As String
'   Dim RSpARAMETER As New ADODB.Recordset
'   RSpARAMETER.Open "SELECT * from gs_parameters", Conn, adOpenKeyset, adLockOptimistic
'
'
'
'   frm.Show , FrmMain
'   Screen.MousePointer = vbHourglass
'
'   str = GetQueryProcess(22)
'   If RSpARAMETER!demand_update_line_id > 0 Then
'      str = Replace(str, "-- and w.line_id > @lines_id", " and w.line_id >" & RSpARAMETER!demand_update_line_id)
'   Else
'      str = Replace(str, "-- and w.line_id > @lines_id", "")
'      Conn.execute "UPDATE WATERCOMPONENTS SET DEMAND = 0"
'   End If
'
'   Set rsNodes = Conn.execute(str)
'   frm.ProgressBar1.Max = rsNodes(0).value
'   frm.ProgressBar1.Min = 0
'   rsNodes.Close
'   Set rsNodes = Nothing
'
'   str = GetQueryProcess(21)
'   If RSpARAMETER!demand_update_line_id > 0 Then
'      str = Replace(str, "-- and w.line_id > @lines_id", " and w.line_id >" & RSpARAMETER!demand_update_line_id)
'   Else
'      str = Replace(str, "-- and w.line_id > @lines_id", "")
'   End If
'   Set rsLines = ConnSec.execute(str)
'   Set rsNodes = New ADODB.Recordset
'   rsNodes.CursorType = adOpenDynamic
'   rsNodes.LockType = adLockOptimistic
'
'   str = "SELECT OBJECT_ID_, demand from watercomponents "
'   Set rsNodes = Conn.execute(str)
'
'   With rsLines
'
'      While Not .EOF
'         str = "object_id_='" & .Fields("initialcomponent").value & "' OR "
'         str = str & "object_id_='" & .Fields("finalcomponent").value & "'"
'         rsNodes.Requery
'         rsNodes.Filter = str
'         Conn.BeginTrans
'         IniciouTransacao = True
'         UltimoTrecho = .Fields("OBJECT_ID_").value
'         While Not rsNodes.EOF
'            str = "SELECT OBJECT_ID_ from watercomponents c inner join watercomponentsTypes w on w.id_type=c.id_type "
'            str = str & "where w.specification_ ='JUNCTION' AND C.OBJECT_ID_='" & rsNodes.Fields("OBJECT_ID_").value & "'"
'            RsSpecfication.Open str, Conn
'            If Not RsSpecfication.EOF Then
'               Demanda = Round(IIf(IsNull(.Fields("CONSUMO").value), 0, .Fields("CONSUMO").value) / 2, 2)
'               If Demanda > 0 Then
'                  Conn.execute "update watercomponents set demand= demand + " & Replace(Demanda, ",", ".") & _
'                               " where object_id_='" & rsNodes.Fields("object_id_").value & "'"
'               End If
'               'rsNodes.Fields("DEMAND").value = rsNodes.Fields("DEMAND").value + Round(.Fields("CONSUMO").value / 2, 2)
'               UltimoNoDemand = rsNodes.Fields("object_id_").value
'               rsNodes.MoveNext
'            Else 'O NO É UMA VALVULA OU BOMBA OU RESERVATORIO
'               rsTrecho.Open "SELECT * from waterlines " & _
'                  "where (initialcomponent = '" & rsNodes.Fields("OBJECT_ID_").value & "' or finalcomponent = '" & rsNodes.Fields("OBJECT_ID_").value & "')" & _
'                  " AND OBJECT_ID_ <> '" & UltimoTrecho & "'", Conn
'               If rsTrecho.EOF Then
'                  str = "object_id_='" & UltimoNoDemand & "'"
'                  rsNodes.Requery
'                  rsNodes.Filter = str
'               Else
'                  If CStr(rsTrecho.Fields("initialcomponent")) <> rsNodes.Fields("OBJECT_ID_").value Then
'                     NOI = rsTrecho.Fields("initialcomponent")
'                  ElseIf CStr(rsTrecho.Fields("finalcomponent")) <> rsNodes.Fields("OBJECT_ID_").value Then
'                     NOI = rsTrecho.Fields("finalcomponent")
'                  End If
'                  UltimoTrecho = rsTrecho.Fields("OBJECT_ID_").value
'                  str = "object_id_='" & NOI & "'"
'                  rsNodes.Requery
'                  rsNodes.Filter = str
'               End If
'               rsTrecho.Close
'            End If
'            RsSpecfication.Close
'         Wend
'         frm.ProgressBar1.Min = frm.ProgressBar1.Min + 1
'         frm.Caption = frm.ProgressBar1.Min & "/" & frm.ProgressBar1.Max
'         DoEvents
'         RSpARAMETER!demand_update_line_id = .Fields("object_id_").value
'         RSpARAMETER!DEMAND_LAST_UPDATE_LINE_ID = .Fields("object_id_").value
'         RSpARAMETER.Update
'         Conn.CommitTrans
'         IniciouTransacao = False
'         .MoveNext
'      Wend
'   End With
'   Unload frm
'   RSpARAMETER!demand_update_line_id = 0
'   RSpARAMETER.Update
'   Set frm = Nothing
'   Screen.MousePointer = vbNormal
'   Set RsSpecfication = Nothing
'   Set rsTrecho = Nothing
'   'rsNodes.Update
'   rsNodes.Close
'   Set rsNodes = Nothing
'   rsLines.Close
'   Set rsLines = Nothing
'   RSpARAMETER.Close
'   Set RSpARAMETER = Nothing
'   Exit Sub
'Update_Demand_Node_err:
'   If IniciouTransacao Then Conn.RollbackTrans
'   Screen.MousePointer = vbNormal
'   If Not RsSpecfication Is Nothing Then
'      If RsSpecfication.State = 1 Then RsSpecfication.Close
'      Set RsSpecfication = Nothing
'   End If
'   If Not rsTrecho Is Nothing Then
'      If rsTrecho.State = 1 Then rsTrecho.Close
'      Set rsTrecho = Nothing
'   End If
'   If Not rsNodes Is Nothing Then
'      If rsNodes.State = 1 Then rsNodes.Close
'      Set rsNodes = Nothing
'   End If
'   If Not rsLines Is Nothing Then
'      If rsLines.State = 1 Then rsLines.Close
'      Set rsLines = Nothing
'   End If
'   If Not RSpARAMETER Is Nothing Then
'      If RSpARAMETER.State = 1 Then RSpARAMETER.Close
'      Set RSpARAMETER = Nothing
'   End If
'
'   MsgBox Err.Description, vbExclamation
'
'End Sub

Public Sub Update_Demand_Node()
   On Error GoTo Update_Demand_Node_err '
   Dim IniciouTransacao As Boolean
   Dim rsLines As ADODB.Recordset, str As String
   Dim rsNodes As ADODB.Recordset
   Dim RsSpecfication As New ADODB.Recordset
   Dim rsTrecho As New ADODB.Recordset, Demanda As Double
   Dim NOI As String, UltimoNoDemand As String
   Dim frm As New frmProgressBar, UltimoTrecho As String
   Dim RSpARAMETER As New ADODB.Recordset
   
'   If frmCanvas.TipoConexao = 1 Then 'SQL
'      'MsgBox "Conexão SQL"
'      UPDATE_DEMANDA_BY_JONATHAS
'      Exit Sub
'   End If
   
   
   
      a = "gs_parameters"
      b = "INITIALCOMPONENT"
      c = "FINALCOMPONENT"
      d = "LENGHTCALCULATED"

   If frmCanvas.TipoConexao <> 4 Then
   RSpARAMETER.Open "SELECT * from gs_parameters", Conn, adOpenKeyset, adLockOptimistic
   Else
   RSpARAMETER.Open "SELECT * from " + """" + a + """", Conn, adOpenKeyset, adLockOptimistic
   End If
   
   str = ""
   str = GetQueryProcess(22)
   
   If Trim(str) = "" Then Exit Sub
   
   frm.Show , FrmMain
   Screen.MousePointer = vbHourglass
   
   If RSpARAMETER!demand_update_line_id > 0 Then
      str = Replace(str, "-- and w.line_id > @lines_id", " and w.line_id >" & RSpARAMETER!demand_update_line_id)
   Else
      str = Replace(str, "-- and w.line_id > @lines_id", "")
      
a = "WATERCOMPONENTS"
b = "DEMAND"
  If frmCanvas.TipoConexao <> 4 Then
      Conn.execute "UPDATE WATERCOMPONENTS SET DEMAND = 0"
      Else
      Conn.execute "UPDATE " + """" + a + """" + " SET " + """" + b + """" + " = '0'"
      End If
   End If
   
   Set rsNodes = Conn.execute(str)
   frm.ProgressBar1.Max = rsNodes(0).value
   frm.ProgressBar1.Min = 0
   rsNodes.Close
   Set rsNodes = Nothing
   
   str = GetQueryProcess(21)
   If RSpARAMETER!demand_update_line_id > 0 Then
      str = Replace(str, "-- and w.line_id > @lines_id", " and w.line_id >" & RSpARAMETER!demand_update_line_id)
   Else
      str = Replace(str, "-- and w.line_id > @lines_id", "")
   End If
   Set rsLines = ConnSec.execute(str)
   Set rsNodes = New ADODB.Recordset
   rsNodes.CursorType = adOpenDynamic
   rsNodes.CursorLocation = adUseClient
   
   
   With rsLines
      
      While Not .EOF
         Conn.BeginTrans
         IniciouTransacao = True
         UltimoTrecho = .Fields("OBJECT_ID_").value
          If frmCanvas.TipoConexao <> 4 Then
         str = "SELECT OBJECT_ID_, w.specification_ from watercomponents c left join watercomponentsTypes w on w.id_type=c.id_type "
         str = str & "where C.OBJECT_ID_='" & .Fields("initialcomponent").value & "' or  C.OBJECT_ID_='" & .Fields("finalcomponent").value & "'"
         Else
         b = "WATERCOMPONENTS"
c = "DEMAND"
d = "TIPO"
e = Replace(Demanda, ",", ".")
f = "ESPECIFICATION_"
g = "WATERCOMPONENTSTYPES"
h = "'g'"
'i = DEMAND
j = "iD_TYPE"
k = "OBJECT_ID_"

str = "SELECT " + """" + k + """" + ", " + """" + g + """" + "." + """" + e + """" + " from " + """" + b + """" + "  left join " + """" + g + """" + "  on " + """" + g + """" + "." + """" + j + """" + "=" + """" + b + """" + "." + """" + j + """" + ""
         str = str & "where " + """" + b + """" + "." + """" + k + """" + "='" & .Fields("INITIALCOMPONENT").value & "' or  " + """" + b + """" + " ." + """" + k + """" + " ='" & .Fields("FINALCOMPONENT").value & "'"
         End If
         rsNodes.Open str, Conn
         While Not rsNodes.EOF
            
            Demanda = Round(IIf(IsNull(.Fields("CONSUMO").value), 0, .Fields("CONSUMO").value) / 2, 2)
            
            If rsNodes.Fields("specification_").value = "JUNCTION" Then
               If Demanda > 0 Then
               a = "OBJECT_ID_"
b = "WATERCOMPONENTS"
c = "DEMAND"
d = "TIPO"
e = Replace(Demanda, ",", ".")
f = "e'"
g = rsNodes.Fields("object_id_").value
h = "'g'"
'i = DEMAND
j = "'i"
k = "OBEJECT_ID"


                  If frmCanvas.TipoConexao <> 4 Then
                  Conn.execute "update watercomponents set demand= demand + " & Replace(Demanda, ",", ".") & _
                               " where object_id_='" & rsNodes.Fields("object_id_").value & "'"
                Else
                 Conn.execute "update " + """" + b + """" + " set " + """" + c + """" + " = '" + c + e + "' where " + """" + k + """" + "='" + g + "'"
                End If
               End If
               UltimoNoDemand = rsNodes.Fields("object_id_").value
               
            Else 'O NO É UMA VALVULA OU BOMBA OU RESERVATORIO
               Sub_Update_Demand_Next_Node rsNodes.Fields("OBJECT_ID_").value, UltimoNoDemand, UltimoTrecho, Demanda
            End If
            rsNodes.MoveNext
         Wend
         rsNodes.Close
         frm.ProgressBar1.value = frm.ProgressBar1.value + 1
         frm.Caption = frm.ProgressBar1.Min & "/" & frm.ProgressBar1.Max
         DoEvents
         RSpARAMETER!demand_update_line_id = .Fields("object_id_").value
         RSpARAMETER!DEMAND_LAST_UPDATE_LINE_ID = .Fields("object_id_").value
         RSpARAMETER.Update
         Conn.CommitTrans
         IniciouTransacao = False
         .MoveNext
      Wend
   End With
   Unload frm
   RSpARAMETER!demand_update_line_id = 0
   RSpARAMETER.Update
   Set frm = Nothing
   Screen.MousePointer = vbNormal
   Set RsSpecfication = Nothing
   Set rsTrecho = Nothing
   Set rsNodes = Nothing
   rsLines.Close
   Set rsLines = Nothing
   RSpARAMETER.Close
   Set RSpARAMETER = Nothing
   Exit Sub
Update_Demand_Node_err:
   If IniciouTransacao Then Conn.RollbackTrans
   Screen.MousePointer = vbNormal
   If Not RsSpecfication Is Nothing Then
      If RsSpecfication.State = 1 Then RsSpecfication.Close
      Set RsSpecfication = Nothing
   End If
   If Not rsTrecho Is Nothing Then
      If rsTrecho.State = 1 Then rsTrecho.Close
      Set rsTrecho = Nothing
   End If
   If Not rsNodes Is Nothing Then
      If rsNodes.State = 1 Then rsNodes.Close
      Set rsNodes = Nothing
   End If
   If Not rsLines Is Nothing Then
      If rsLines.State = 1 Then rsLines.Close
      Set rsLines = Nothing
   End If
   If Not RSpARAMETER Is Nothing Then
      If RSpARAMETER.State = 1 Then RSpARAMETER.Close
      Set RSpARAMETER = Nothing
   End If
    MsgBox Err.Description, vbExclamation
    If mid(Err.Description, 1, 9) = "ORA-03114" Then
        Conn.RollbackTrans
        MsgBox "Não há conexão ativa com o banco de dados", vbExclamation, "Conexão desativada"
        End
    End If
   'Resume

End Sub

Sub Sub_Update_Demand_Next_Node(No As String, UltimoNo As String, Trecho As String, Demanda As Double)
   Dim rsTrecho As New ADODB.Recordset
   Dim RsSpecfication As ADODB.Recordset
   Dim strsql  As String
   If frmCanvas.TipoConexao <> 4 Then
   rsTrecho.Open "SELECT * from waterlines " & _
      "where (initialcomponent = '" & No & "' or finalcomponent = '" & No & "')" & _
      " AND OBJECT_ID_ <> '" & Trecho & "'", Conn
      Else
      a = "WATERCOMPONENTS"
b = "DEMAND"
c = Replace(Demanda, ",", ".")
d = "WATERLINES"
e = "FINALCOMPONENT"
f = "INITIALCOMPONENT"
g = "OBJECT_ID_"

      rsTrecho.Open "SELECT * from " + """" + d + """" + " " & _
      "where (" + """" + f + """" + " = '" & No & "' or " + """" + e + """" + " = '" & No & "')" & _
      " AND " + """" + g + """" + " <> '" & Trecho & "'", Conn
      End If
      
      
   If rsTrecho.EOF Then

 If frmCanvas.TipoConexao <> 4 Then
 
      Conn.execute "update watercomponents set demand= demand + " & Replace(Demanda, ",", ".") & _
             " where object_id_='" & UltimoNo & "'"
             Else
             a = "WATERCOMPONENTS"
b = "DEMAND"
c = Replace(Demanda, ",", ".")
d = "WATERLINES"
'e = DEMAND
f = "'e'"
g = "OBJECT_ID_"
             Conn.execute "update " + """" + a + """" + "set " + """" + b + """" + "='b + " & Replace(Demanda, ",", ".") ' & "where " +""""+ g +""""+ "='" & UltimoNo & "'"
             End If
             
             
      rsTrecho.Close
      Set rsTrecho = Nothing
   Else
      If CStr(rsTrecho.Fields("INITIALCOMPONENT")) <> No Then
         No = rsTrecho.Fields("INITIALCOMPONENT")
      ElseIf CStr(rsTrecho.Fields("FINALCOMPONENT")) <> No Then
         No = rsTrecho.Fields("FINALCOMPONENT")
      End If
a = "WATERCOMPONENTS"
b = "DEMAND"
c = Replace(Demanda, ",", ".")
d = "'c'"
'e = DEMAND
f = "'e'"
g = "OBJECT_ID_"
h = "WATERCOMPONENTSTYPES"
i = "ID_TYPE"

k = Replace(Demanda, ",", ".")
l = "'k'"
Dim gu As String
gu = "SPECIFICATION_"
      If frmCanvas.TipoConexao <> 4 Then
      strsql = "SELECT OBJECT_ID_ from watercomponents c inner join watercomponentsTypes w on w.id_type=c.id_type "
      strsql = strsql & "where w.specification_ ='JUNCTION' AND C.OBJECT_ID_='" & No & "'"
      Else
       strsql = "SELECT " + """" + g + """" + " from " + """" + a + """" + " inner join " + """" + h + """" + " on " + """" + h + """" + "." + """" + i + """" + "=" + """" + a + """" + "." + """" + i + """" + ""
      strsql = strsql + "where " + """" + h + """" + "." + """" + gu + """" + " ='JUNCTION' AND " + """" + a + """" + "." + """" + g + """" + "='" & No & "'"
      End If
      Set RsSpecfication = New ADODB.Recordset
      RsSpecfication.Open strsql, Conn
      If Not RsSpecfication.EOF Then
         If Demanda > 0 Then
         If frmCanvas.TipoConexao <> 4 Then
            Conn.execute "update watercomponents set demand= demand + " & Replace(Demanda, ",", ".") & _
                         " where object_id_='" & No & "'"
                         Else
                           Conn.execute "update " + """" + a + """" + " set " + """" + e + """" + "= " + """" + "demand" + Replace(Demanda, ",", ".") + """" + _
                         " where " + """" + g + """" + "='" & No & "'"
                         End If
         End If
         rsTrecho.Close
         Set rsTrecho = Nothing
         RsSpecfication.Close
         Set RsSpecfication = Nothing
      
      Else 'O NO É UMA VALVULA OU BOMBA OU RESERVATORIO
         Trecho = rsTrecho.Fields("object_id_").value
         rsTrecho.Close
         Set rsTrecho = Nothing
         RsSpecfication.Close
         Set RsSpecfication = Nothing
         
         Sub_Update_Demand_Next_Node No, UltimoNo, Trecho, Demanda
      End If
   End If
End Sub