clsTerraLib.cls 79.5 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
VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsTerraLib"
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

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 i As String
Dim j As String
Dim k As String
Dim l As String
Dim m As String
Public Enum TypeGeometryEvent
   tg_NoEvent = 0
   tg_DrawNetWorkline = 1
   tg_DrawNetWorkNode = 2
   tg_MoveNetWorkNode = 3
   tg_SelectObject = 4
   tg_DrawGeometrys = 5
   tg_DrawPoint = 6
   tg_ZoomArea = 7
   tg_Pan = 8
   tg_DrawRamal = 9
   tg_InsertDoc = 10
   tg_MoveNetWorkVertice = 11
   tg_MoveGpsPoint = 12
   tg_DrawRamalAuto = 13                                        'para selecionar o trecho de rede ao qual os ramais serão desenhados
   tg_DrawRamalAutoSelecionaConsumidor = 14                     'para selecionar o consumidor que será associado ao ramal que será desenhado
   tg_DrawRamalAddConsumer = 15                                 'para selecionar o ramal ao qual serão ligados os consumidores
   tg_DrawRamalAddConsumerSelecionaConsumidor = 16              'para selecionar os consumidores quer irão ligar-se aos ramais
End Enum

Private LayerReference As String, rs As ADODB.Recordset
Private mvartcs As TeCanvas 'local copy
Private mvarCtrlMgr As Manager 'local copy
Private mvarCGeo As clsGeoReference 'local copy
Private mvarTerraEvent As TypeGeometryEvent 'local copy

Private mvartdb As TeDatabase 'local copy
Private mvartdbcon As TeDatabase 'local copy
Private mvartdbconref As TeDatabase 'local copy
' Recebe o objeto da conexão TeDatabase
'
Public Property Set tdb(ByVal vData As TeDatabase)
    Set mvartdb = vData
    Set cgeo.tdb = vData
End Property
' Retorna o objeto da conexão TeDatabase
'
Public Property Get tdb() As TeDatabase
    Set tdb = mvartdb
End Property

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

Public Property Get tdbcon() As TeDatabase
    Set tdbcon = mvartdbcon
End Property

Public Property Set tdbconref(ByVal vData As TeDatabase)
    Set mvartdbconref = vData
End Property

Public Property Get tdbconref() As TeDatabase
    Set tdbconref = mvartdbconref
End Property


Public Property Let TerraEvent(ByVal vData As TypeGeometryEvent)
    mvarTerraEvent = vData
End Property

Public Property Get TerraEvent() As TypeGeometryEvent
    TerraEvent = mvarTerraEvent
End Property

Public Property Set cgeo(ByVal vData As clsGeoReference)
    Set mvarCGeo = vData
End Property

Public Property Get cgeo() As clsGeoReference
    Set cgeo = mvarCGeo
End Property

Public Property Set CtrlMgr(ByVal vData As Manager)
    Set mvarCtrlMgr = vData
End Property

Public Property Get CtrlMgr() As Manager
    Set CtrlMgr = mvarCtrlMgr
End Property

Public Property Set tcs(ByVal vData As TeCanvas)
    Set mvartcs = vData
    
End Property

Public Property Get tcs() As TeCanvas
    Set tcs = mvartcs
End Property
' Classe responsável pelo desenho da rede. Aquí é onde é ativado o início do desenho de uma rede
' Retorna verdadeiro se foi ativada corretamente
'
'
Public Function DrawNetWorkLine(Optional mback As Boolean) As Boolean
    On Error GoTo Trata_Erro
    With tcs                                                             'com o objeto teCanvas
        LayerReference = cgeo.GetLayerOperation(.getCurrentLayer, 0)      'obtem o nome do layer de referencia, por exemplo se o principal é waterlines o de referência é watercomponents, sewerlines é sewercomponents, etc.
        If Trim(LayerReference) <> "" Then
            tdbcon.setCurrentLayer LayerReference                          'define como ativo o layer de nós
        Else
            MsgBox "Selecione antes o plano em que deseja desenhar!", vbInformation, "Desenho de Rede"
            DrawNetWorkLine = False
            Exit Function
        End If
        'tdbconref.setCurrentLayer "CEL_PONTO_ATRIB"
        Select Case cgeo.GetLayerTypeReference(.getCurrentLayer)                      'obtem o tipo de layer de referência
            Case LayerTypeRefence.Trecho_Rede_Agua, LayerTypeRefence.Trecho_Rede_Drenagem, LayerTypeRefence.Trecho_Rede_esgoto
                If Not mback Then
                    .object_id = ""
                    .addLayerToSnap cgeo.GetReferenceLayer(tcs.getCurrentLayer)             'obtem o layer de referência e coloca-o como de snap
                    .setReferenceLayer cgeo.GetReferenceLayer(tcs.getCurrentLayer)          'seta o nome do layer de referência
                    CtrlMgr.LoadDefaultProperties 0, tcs.getCurrentLayer, True              'carrega os valores padrão do gerenciador de propriedades
                    CtrlMgr.GridEnabled True: CtrlMgr.GridVisibled True
                End If
                .DrawNetWorkLine
                TerraEvent = tg_DrawNetWorkline
                DrawNetWorkLine = True
            Case Else
                DrawNetWorkLine = False
                MsgBox "Não é possivel desenhar rede neste plano: " & .getCurrentLayer, vbExclamation
                Exit Function
        End Select
    End With
    Exit Function
    
Trata_Erro:
    If Err.Number = 0 Or Err.Number = 20 Then
        Resume Next
    Else
        PrintErro "clsTerraLib", "Public Function DrawNetWorkLine", CStr(Err.Number), CStr(Err.Description), True
    End If
End Function
' Entra neste evento logo após selecionado um nó da rede para ser movido para uma nova posição.
' Ele checa se foi selecionado mais de um nó pois é permitido mover somente um nó por vez
' Se estiver tudo OK então adiciona o layer de snap, referência e ativa o comendo de mover nó de uma rede
' Entra neste evento antes de salvar no banco, com os dados originais
'
'
Public Function MoveNetWorkNode()
    On Error GoTo Trata_Erro
    Dim layerReferencia As String                                       'nome do layer de referência
    
    layerReferencia = cgeo.GetReferenceLayer(tcs.getCurrentLayer)
    If layerReferencia <> "" Then
        If tcs.getSelectCount(points) - 1 = 0 Then                      'VERIFICA SE SOMENTE 1 PONTO FOI SELECIONADO PARA MOVER
            Dim objIdNoMovendo As Long                                  'object_id do nó que foi selecionado para mover
            Dim trechos As New CLocalizaTrechos                         'instancia objeto que irá localizar os trechos conectados ao nó selecionado
            Dim objIDsTrechosMovendo() As String                        'lista com todos os object_ids dos trechos que serão movidos com o nó (presos no nó)
            Dim contTrecho As Integer                                   'contador de trechos de rede sendo movidos
            Dim listObjIDsRamais() As String                            'lista de todos objIDs dos ramais de um determinado trecho
            Dim listGeomIDsRamais() As String                           'lista de todos os geomIDs dos ramais de um determinado trecho
            Dim contRamal As Integer                                    'contador de ramais conectados em um determinado trecho de rede
            Dim distIniRamalAntes As Double                             'distância do início do ramal antes de tanto o trecho quanto o ramal serem movidos
            Dim distRamalDoInicioTrecho As New CDistRamalInicioTrecho   'clases para calcular a distância do início do ramal antes de tanto o trecho quanto o ramal serem movidos
            Dim objIDsRamais As New CObtemObjIDsRamais
            Dim coordRamal As New CCoordIniFinLinha                     'classe para obter as coordenadas inicial e final do ramal que foi movido
            Dim objIdTrecho As String
            Dim objIdRamal As String
            Dim geomIdRamal As String
            Dim contador As Integer                                     'número de ramais processados
            Dim comprTrecho As Double                                   'comprimento total do trecho de rede antes de mover
            Dim retorno As Boolean
            
            If layerReferencia = "WATERLINES" Then                      'é água, tem que tratar todos os ramais
            'nesta etapa ele vai localizar todos os trechos que serão movidos e os respectivos ramais se houverem e armazenar tudo na memória
                contador = 0
                objIdNoMovendo = tcs.getSelectObjectId(0, 4)                        'obtem o object_id da primeira (0) (e única) geometria do tipo 4=ponto (ver tipos em setActiveGeometry)
                varGlobais.objIdNoSelecionado = objIdNoMovendo                      'guarda o valor do objId do nó da rede que está movendo e foi selecionado, para depois poder atribir a cota ao mesmo
                trechos.obtemTrechos objIdNoMovendo, objIDsTrechosMovendo           'obtem todos os object_ids dos trechos de rede que estão conectados ao nó que está movendo
                varGlobais.objIdTre = Null                                          'zera a variável global, caso contenha algum valor lá
                varGlobais.objIdTre = objIDsTrechosMovendo                          'armazena na variável global todos os object_ids dos trechos de rede que estão conectados ao nó que está movendo
                varGlobais.totalTrechos = UBound(objIDsTrechosMovendo)              'armazena o número total de trechos de rede que estão sendo movidos
                For contTrecho = 0 To UBound(objIDsTrechosMovendo)                  'para cada trecho de rede que estiver sendo movido, começando em zero
                    objIdTrecho = objIDsTrechosMovendo(contTrecho)
                    objIDsRamais.getObjIDs objIdTrecho, listObjIDsRamais, listGeomIDsRamais     'obtem todos os objIDs dos ramais que estão ligados ao trecho de rede que está sendo movido
                    If listObjIDsRamais(0) <> "" Then                               'existem ramais conectados ao trecho
                        For contRamal = 0 To UBound(listObjIDsRamais)               'enquanto existirem ramais
                            contador = contador + 1
                            ReDim Preserve ramalMovendo(contador - 1)
                            objIdRamal = listObjIDsRamais(contRamal)
                            geomIdRamal = listGeomIDsRamais(contRamal)
                            distIniRamalAntes = distRamalDoInicioTrecho.Distancia(objIdTrecho, objIdRamal)  'obtem a distância do início do ramal antes de tanto o trecho quanto o ramal serem movidos
                            ramalMovendo(contador - 1).objIdTrecho = objIdTrecho
                            ramalMovendo(contador - 1).objIdRamal = objIdRamal
                            ramalMovendo(contador - 1).geomIdRamal = geomIdRamal
                            ramalMovendo(contador - 1).Distancia = distIniRamalAntes
                            coordRamal.GetStartEndPointsRamal objIdRamal                                'obtem as coordenadas inicial e final do ramal
                            ramalMovendo(contador - 1).xHidrom = coordRamal.linha.xf                    'pega as coordenadas de onde está o hidrômetro
                            ramalMovendo(contador - 1).yHidrom = coordRamal.linha.yf
                            retorno = cGeoDatabase.geoDatabase.setCurrentLayer("WaterLines")
                            retorno = cGeoDatabase.geoDatabase.getLengthOfLine(objIdTrecho, "", comprTrecho) 'está como geom_id deveria ser na segunda
                            ramalMovendo(contador - 1).comprTrecho = comprTrecho
                        Next                                                        'próximo ramal
                    Else                                                            'não existem ramais
                        contador = contador + 1
                        ReDim Preserve ramalMovendo(contador - 1)
                        ramalMovendo(contador - 1).objIdTrecho = objIdTrecho
                        ramalMovendo(contador - 1).objIdRamal = -1
                    End If
                Next                                                                'próximo trecho
            'final da etapa
            Else                                                                    'é esgoto, não trata ramais de esgoto
                objIdNoMovendo = tcs.getSelectObjectId(0, 4)                        'obtem o object_id da primeira (0) (e única) geometria do tipo 4=ponto (ver tipos em setActiveGeometry)
                varGlobais.objIdNoSelecionado = objIdNoMovendo                      'guarda o valor do objId do nó da rede que está movendo e foi selecionado, para depois poder atribir a cota ao mesmo
            End If
            tcs.addLayerToSnap cgeo.GetReferenceLayer(tcs.getCurrentLayer)
            tcs.setReferenceLayer cgeo.GetReferenceLayer(tcs.getCurrentLayer)
            tcs.MoveNetWorkNode: TerraEvent = tg_MoveNetWorkNode
        Else
            MsgBox "Selecione um ponto para esta operação", vbExclamation
       End If
    Else
        MsgBox "Selecione um plano válido e um ponto, para esta operação", vbExclamation
    End If
    Exit Function

Trata_Erro:
    If Err.Number = 0 Or Err.Number = 20 Then
        Resume Next
    Else
       ErroUsuario.Registra "clsTerralib", "MoveNetWorkNode", CStr(Err.Number), CStr(Err.Description), True, glo.enviaEmails
    End If
   
End Function
' Chamada quando o usuário seleciona para inserir um nó na rede
Public Function DrawNetWorkNode()
On Error GoTo Trata_Erro
   
   With tcs
      If cgeo.GetReferenceLayer(tcs.getCurrentLayer, True) <> "" Then
         If tcs.getSelectCount(lines) = 1 Then
            'CtrlMgr.LoadDefaultProperties 0, CGeo.GetReferenceLayer(.getCurrentLayer), True
            
            tcs.setReferenceLayer cgeo.GetReferenceLayer(.getCurrentLayer)
            
            tcs.addLayerToSnap .getCurrentLayer
            
            tcs.addLayerToSnap cgeo.GetReferenceLayer(.getCurrentLayer)
            
            tcs.insertNetWorkNode False: TerraEvent = tg_DrawNetWorkNode
         ElseIf tcs.getSelectCount(lines) < 1 Then
            MsgBox "Selecione uma rede, para esta operação, depois de um clique na linha", vbExclamation
         ElseIf tcs.getSelectCount(lines) > 1 Then
            MsgBox "Selecione apenas uma rede, para esta operação, depois de um clique na linha", vbExclamation
         End If
      Else
         MsgBox "Selecione um plano válido, para esta operação", vbExclamation
      End If
   End With
   Exit Function

Trata_Erro:
    If Err.Number = 0 Or Err.Number = 20 Then
        Resume Next
    Else
       ErroUsuario.Registra "clsTerralib", "DrawNetWorkNode", CStr(Err.Number), CStr(Err.Description), True, glo.enviaEmails
    End If
End Function
' Para mover um vértice de um trecho de rede de água. Entra nesta rotina após selecionar a rede
' O objetivo aqui é coletar os dados para que o sistema possa depois saber quais ramais necessitam
' ser recalculados em sua nova posição. São todos salvos em memória
'
'
'
Public Function moveVertice()
    Dim objIdSelecionado As String                                          'objectId do trecho que foi selecionado no qual será movido um ou mais vértices
    Dim contador As Integer                                                 'número de ramais processados
    Dim objIdTrecho As String                                               'objectId do trecho que foi selecionado no qual será movido, mesmo que o anterior, só para ficar igual o código
    Dim objIDsRamais As New CObtemObjIDsRamais
    Dim listObjIDsRamais() As String                                        'lista de todos objIDs dos ramais de um determinado trecho
    Dim listGeomIDsRamais() As String                                       'lista de todos os geomIDs dos ramais de um determinado trecho
    Dim contRamal As Integer                                                'contador de ramais conectados em um determinado trecho de rede
    Dim objIdRamal As String
    Dim geomIdRamal As String
    Dim distIniRamalAntes As Double                                         'distância do início do ramal antes de tanto o trecho quanto o ramal serem movidos
    Dim distRamalDoInicioTrecho As New CDistRamalInicioTrecho               'clases para calcular a distância do início do ramal antes de tanto o trecho quanto o ramal serem movidos
    Dim retorno As Boolean
    Dim coordRamal As New CCoordIniFinLinha                                 'classe para obter as coordenadas inicial e final do ramal que foi movido
    Dim comprTrecho As Double                                               'comprimento total do trecho de rede antes de mover

    If tcs.getCurrentLayer = "WATERLINES" Then
        objIdSelecionado = varGlobais.objIdTreSelecionado                   'para obter o object_id da linha que foi selecionada para mover o vértice da mesma. Esta variável foi configurada em  frmCanvas.TCanvas_onEndSELECT
        tcs.moveGeometryPoint
        'nesta etapa ele vai localizar todos os trechos que serão movidos e os respectivos ramais se houverem e armazenar tudo na memória
        contador = 0
        varGlobais.objIdTre = Null                                          'zera a variável global, caso contenha algum valor lá
        varGlobais.objIdTre = objIdSelecionado                              'só um revisar para ver se nao deveria ser vetor 'armazena na variável global todos os object_ids dos trechos de rede que estão conectados ao nó que está movendo
        varGlobais.totalTrechos = 1                                         'armazena o número total de trechos de rede que estão sendo movidos. Um só pois está movendo o vértice de um trecho
        objIdTrecho = objIdSelecionado
        objIDsRamais.getObjIDs objIdTrecho, listObjIDsRamais, listGeomIDsRamais     'obtem todos os objIDs dos ramais que estão ligados ao trecho de rede que está sendo movido
        If listObjIDsRamais(0) <> "" Then                                   'existem ramais conectados ao trecho
            For contRamal = 0 To UBound(listObjIDsRamais)                   'enquanto existirem ramais
                contador = contador + 1
                ReDim Preserve ramalMovendo(contador - 1)
                objIdRamal = listObjIDsRamais(contRamal)
                geomIdRamal = listGeomIDsRamais(contRamal)
                distIniRamalAntes = distRamalDoInicioTrecho.Distancia(objIdTrecho, objIdRamal)  'obtem a distância do início do ramal antes de tanto o trecho quanto o ramal serem movidos
                ramalMovendo(contador - 1).objIdTrecho = objIdTrecho
                ramalMovendo(contador - 1).objIdRamal = objIdRamal
                ramalMovendo(contador - 1).geomIdRamal = geomIdRamal
                ramalMovendo(contador - 1).Distancia = distIniRamalAntes
                coordRamal.GetStartEndPointsRamal objIdRamal                'obtem as coordenadas inicial e final do ramal
                ramalMovendo(contador - 1).xHidrom = coordRamal.linha.xf    'pega as coordenadas de onde está o hidrômetro
                ramalMovendo(contador - 1).yHidrom = coordRamal.linha.yf
                retorno = cGeoDatabase.geoDatabase.setCurrentLayer("WaterLines")
                retorno = cGeoDatabase.geoDatabase.getLengthOfLine(objIdTrecho, "", comprTrecho) 'está como geom_id deveria ser na segunda
                ramalMovendo(contador - 1).comprTrecho = comprTrecho
            Next                                                            'próximo ramal
        Else                                                                'não existem ramais
            contador = contador + 1
            ReDim Preserve ramalMovendo(contador - 1)
            ramalMovendo(contador - 1).objIdTrecho = objIdTrecho
            ramalMovendo(contador - 1).objIdRamal = -1
        End If
        'final da etapa
    Else
        MsgBox "Selecione um plano válido, para esta operação", vbExclamation
    End If
End Function
Public Function DrawPoint()
On Error GoTo Trata_Erro
   
   If cgeo.GetLayerTypeReference(tcs.getCurrentLayer) = DOCUMENTOS Then
      tcs.DrawPoint
      tcs.addLayerToSnap tcs.getCurrentLayer
   Else
      MsgBox "Selecione um plano válido, para esta operação", vbExclamation
   End If

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

End Function
' Desenha um ramal junto a rede de água ou esgoto
' Estamos utilizando atualmente apenas ramais de água
'
'
Public Function DrawRamal() As Boolean
On Error GoTo Trata_Erro
   
   If cgeo.IsValidLayerOperation(tcs.getCurrentLayer, RAMAIS_AGUA) Or cgeo.IsValidLayerOperation(tcs.getCurrentLayer, RAMAIS_ESGOTO) Then
      Screen.MousePointer = vbHourglass
      tcs.drawLine
      tcs.addLayerToSnap cgeo.GetLayerOperation(tcs.getCurrentLayer, 1)         ' Seta o layer de snap de referência. Para isso tem que buscar esta informação na tabela GS_LAYER_CONFIG_LAYERS. Onde o layer ativo selecionado e o tipo de operação 1 (desenhar ramal) retorne o número do layer de referência para snap. Por exemplo, se for um ramal o layer de referência de snap é o de redes de água (1)
      tdb.setCurrentLayer tcs.getCurrentLayer                                   ' Seta o layer ativo de desenho o que estiver selecionado pelo usuário
      tdbcon.setCurrentLayer cgeo.GetLayerOperation(tcs.getCurrentLayer, 1)
      tdbconref.setCurrentLayer cgeo.GetLayerOperation(tcs.getCurrentLayer, 1)
      FrmMain.sbStatusBar.Panels(1).Text = "Desenho de ramal: Clique em um treho da rede e desenhe o ramal"
      Screen.MousePointer = vbNormal
   Else
      MsgBox "Selecione um tema de plano de ramais", vbExclamation
   End If
   Exit Function
   
Trata_Erro:
    If Err.Number = 0 Or Err.Number = 20 Then
       Resume Next
    Else
       
       PrintErro "clsTerraLib", "Public Function DrawRamal", CStr(Err.Number), CStr(Err.Description), True
       
    End If
    Screen.MousePointer = vbNormal
End Function
' Apaga um elemento do banco de dados geográfico
'
'
'
Public Function Delete() As Boolean
    On Error GoTo Trata_Erro
    Dim i As Integer, rs As ADODB.Recordset
    Dim strsql As String
    Dim linhasSelecionadasMemoria As Integer                            'número total de linhas selecionadas pelo usuário que estão na memória
    Dim pontosSelecionadosMemoria As Integer                            'número total de pontos selecionados pelo usuário que estão na memória
    
    With tcs                            'com o objeto do TeCanvas
        Select Case TerraEvent
            Case tg_SelectObject                                                                                                                                'caso tenha selecionado um objeto para apagar faça, se não selecionou, vai para o final e sai
                Select Case cgeo.GetLayerTypeReference(tcs.getCurrentLayer)  ' cgeo.GetTypeReference(tcs.getCurrentLayer)                                       'conforme o número do layer que foi selecionado
                    Case LayerTypeRefence.Trecho_Rede_Agua, LayerTypeRefence.Trecho_Rede_Drenagem, LayerTypeRefence.Trecho_Rede_esgoto                          'selecionou o layer de redes, vai apagar um trecho de rede
                        If MsgBox("Deseja realmente excluir as rede(s) selecionada(s)", vbQuestion + vbYesNo + vbDefaultButton2) = vbYes Then
                            linhasSelecionadasMemoria = .getSelectCount(lines)
                            For i = 0 To linhasSelecionadasMemoria - 1
                                'ZERA O OBJECT_ID_TRECHO DO RAMAL QUE ESTÁ CONECTADO A REDE QUE ESTA SENDO EXCLUÍDA
                                Dim RetornaLayer As String
                                RetornaLayer = UCase(cgeo.GetLayerOperation(.getCurrentLayer, 1))
                                If RetornaLayer = "RAMAIS_AGUA" Or RetornaLayer = "RAMAIS_ESGOTO" Then
                                    a = RetornaLayer
                                    b = "+a+"
                                    c = "OBJECT_ID_TRECHO"
                                    If frmCanvas.TipoConexao <> 4 Then
                                        strsql = "UPDATE " & RetornaLayer & " SET OBJECT_ID_TRECHO = '0' WHERE OBJECT_ID_TRECHO = '" & .getSelectObjectId(i, lines) & "'"
                                    Else
                                        strsql = "UPDATE " + """" + a + """" + " SET " + """" + c + """" + " = '0' WHERE " + """" + c + """" + "  = '" & .getSelectObjectId(i, lines) & "'"
                                    End If
                                    Conn.execute (strsql)
                                End If
                                '*****************************************************************************************************
                                'CHAMA O PROCEDIMENTO DE DELETE DE REDES
                                cgeo.DeleteRede .getCurrentLayer, .getSelectObjectId(i, lines)
                            Next
                        End If
                    Case LayerTypeRefence.AMARRACAO                                                                                                             'layer número 10 - caso deseje apagar uma amarração de rede
                        .deleteSelectGeometry
                    Case LayerTypeRefence.DOCUMENTOS                                                                                                            'layer número 9 - caso deseje apagar um documento associado
                        If MsgBox("Deseja realmente excluir o(s) ponto(s) de documentação", 36) = vbYes Then
                            For i = 0 To tcs.getSelectCount(points) - 1
                                If frmCanvas.TipoConexao <> 4 Then
                                    Conn.execute "Delete From X_Files Where Object_id_='" & tcs.getSelectObjectId(i, points) & "'"
                                Else
                                    c = "X_FILES"
                                    d = "OBJECT_ID_"
                                    Conn.execute "Delete From " + """" + c + """" + " Where " + """" + d + """" + "='" & tcs.getSelectObjectId(i, points) & "'"
                                End If
                            Next
                            .deleteSelectGeometry
                        End If
                    Case LayerTypeRefence.RAMAIS_AGUA, LayerTypeRefence.RAMAIS_ESGOTO                                                                           'layer número 7 - caso deseje apagar um ramal de água ou esgoto. Apaga a Geometria com attributo
                        pontosSelecionadosMemoria = .getSelectCount(points)                                                                                     'número total de pontos selecionados pelo usuário que estão na memória
                        linhasSelecionadasMemoria = .getSelectCount(lines)                                                                                      'número total de linhas selecionadas pelo usuário que estão na memória
                        If pontosSelecionadosMemoria > 0 Or linhasSelecionadasMemoria > 0 Then                                                                  'verifica se o usuário selecionou mais de um ramal, pois só pode apagar um ramal por vez
                            If pontosSelecionadosMemoria > 1 Or linhasSelecionadasMemoria > 1 Then
                                MsgBox "Somente é possivel excluir um ramal por vez", vbExclamation
                                Exit Function
                            End If
                            If frmCanvas.TipoConexao <> 4 Then                                                                                                  'caso seja SQLServer ou Oracle
                                Set rs = Conn.execute("SELECT * from " & tcs.getCurrentLayer & "_LIGACAO WHERE OBJECT_ID_='" & IIf(pontosSelecionadosMemoria = 1, .getSelectObjectId(0, points), .getSelectObjectId(0, lines)) & "'")     'para verificar se existem ligações de água ainda associadas ao ramal, pois não posso apagar um ramal se existem hidrômetros associados ao mesmo
                            Else                                                                                                                                'caso seja Postgres
                                Set rs = Conn.execute("SELECT * from " & """" + tcs.getCurrentLayer & "_LIGACAO" + """" + " WHERE " + """" + "OBJECT_ID_" + """" + "='" & IIf(pontosSelecionadosMemoria = 1, .getSelectObjectId(0, points), .getSelectObjectId(0, lines)) & "'")
                            End If
                            If Not rs.EOF Then                                                                                                                  'existem hidrômetros associados/ativos junto ao ramal, não pode apagar
                                MsgBox "Para excluir o ramal é necessário que ele não tenha nenhum hidrômentro associado.", vbInformation, ""
                                Exit Function
                            End If
                            If MsgBox("Deseja realmente excluir o ramal selecionado", 36) = vbYes Then
                                If pontosSelecionadosMemoria = 1 Then                                                                                             'verifica quantas geometrias foram selecionadas, deveria ser apenas uma geometria, a do ramal
                                    For i = 0 To pontosSelecionadosMemoria - 1
                                        a = "OBJECT_ID_"
                                        If frmCanvas.TipoConexao <> 4 Then                                                                                      'se for SQLServer ou Oracle
                                            Conn.execute "Delete From " & tcs.getCurrentLayer & "  Where Object_id_='" & .getSelectObjectId(i, points) & "'"
                                            Conn.execute "Delete From lines" & cgeo.GetLayerID(.getCurrentLayer) & " Where Object_id='" & .getSelectObjectId(i, points) & "'"
                                            Conn.execute "Delete From points" & cgeo.GetLayerID(.getCurrentLayer) & " Where Object_id='" & .getSelectObjectId(i, points) & "'"
                                        Else                                                                                                                    'se for Postgres
                                            Conn.execute "Delete From " & """" + tcs.getCurrentLayer + """" & "  Where " + """" + "OBJECT_ID_" + """" + "='" & .getSelectObjectId(i, points) & "'"
                                            Conn.execute "Delete From " + """" + "lines" & cgeo.GetLayerID(.getCurrentLayer) & """" + " Where " + """" + "object_id" + """" + "='" & .getSelectObjectId(i, points) & "'"
                                            Conn.execute "Delete From " + """" + "points" & cgeo.GetLayerID(.getCurrentLayer) & """" + " Where " + """" + "object_id" + """" + "='" & .getSelectObjectId(i, points) & "'"
                                        End If
                                    Next
                                Else                                                                                                                            'só pode existir um ponto na extremidade do ramal, o qual é a ligação do hidrômetro, portanto se entrar neste else é por que este ponto não foi selecionado, pois já foi testado antes se existia mais de um ponto
                                    MsgBox "Você não selecionou o hidrômetro, somente o ramal. Selecione ambos ou somente o hidrômetro para apagar."
                                End If
                            End If
                        Else
                            MsgBox "Nenhum ramal selecionado para exclusão", vbExclamation
                        End If
                End Select
                tcs.Normal
                tcs.Select
                tcs.plotView
        End Select
    End With
    Exit Function
    
Trata_Erro:
    If Err.Number = 0 Or Err.Number = 20 Then
        Resume Next
    Else
        PrintErro "clsTerraLib", "Public Function Delete", CStr(Err.Number), CStr(Err.Description), True
    End If
End Function

Public Function OnPoint(X As Double, Y As Double)
On Error GoTo Trata_Erro
   Dim object_id As String, frm As New FrmAssociation
   tdb.setCurrentLayer tcs.getCurrentLayer
   If tdb.locateGeometry(X, Y, tpPOINTS, 1) = 1 Then
      TerraEvent = tg_SelectObject
      tcs.Normal
      tcs.Select
      tcs.addSelectObjectIds tdb.objectIds(0)
      frm.Init tdb.objectIds(0), tcs, tdb
      
   Else
      TerraEvent = tg_DrawGeometrys
      frm.Init "", tcs, tdb, X, Y
      tcs.Normal
      tcs.Select
      TerraEvent = tg_SelectObject
   End If
   
   Set frm = Nothing

Trata_Erro:
   If Err.Number = 0 Or Err.Number = 20 Then
       Resume Next
   Else
    
      PrintErro "clsTerraLib", "Public Function OnPoint", CStr(Err.Number), CStr(Err.Description), True
      
   End If
   
End Function
' Entra nesta função quando está desenhando ou selecionando o ramal
' Ela é chamada quando entro o segundo click do ramal
'
'
Public Function OnRamal(X As Double, Y As Double, object_id_ramal As String) As Boolean
    On Error GoTo Trata_Erro
    Dim frm As New FrmCadastroRamal, Qtde As Long, a As Long
    Dim object_id_lote As String
    
    tdbconref.setCurrentLayer cgeo.GetLayerOperation(tcs.getCurrentLayer, 1)
    'Verifica se existe um filtro anterior de ramais e chama se já houver filtro anterior
    If ReadINI("RAMAISFILTROLOTES", "ATIVADO", App.path & "\CONTROLES\GEOSAN.INI") = "SIM" Then
        Dim TBP As String
        TBP = ReadINI("RAMAISFILTROLOTES", "TABELA_PLANO", App.path & "\CONTROLES\GEOSAN.INI")
        If TBP <> "" Then
            tdbconref.setCurrentLayer TBP
        End If
    End If
    'Chama a caixa de diálogo de cadastro de ramais
    object_id_lote = ""    ' zera apenas para aparecer na caixa de diálogo
    If UCase(tcs.getCurrentLayer) = "RAMAIS_AGUA" Then
        'ORIGINAL
        frm.Init "AGUA", object_id_ramal, tcs, tdb, tdbcon, object_id_lote, ""
    Else
        frm.Init "ESGOTO", object_id_ramal, tcs, tdb, tdbcon, object_id_lote, ""
    End If
    Exit Function
Trata_Erro:
    If Err.Number = 0 Or Err.Number = 20 Then
        Resume Next
    ElseIf Err.Number = -2147467259 Then
        PrintErro "clsTerraLib", "Public Function OnRamal", CStr(Err.Number), CStr(Err.Description), True
    Else
        PrintErro "clsTerraLib", "Public Function OnRamal", CStr(Err.Number), CStr(Err.Description), True
    End If
End Function
' Função chamada para salvar no banco de dados
'
'
'
'
Public Function SaveInDatabase() As Boolean
    On Error GoTo Trata_Erro
    Dim USR As String
    
    With tcs
        Select Case TerraEvent                                                                  'A variavel TerraEvent determina o evento/funcão que o usuário está utilizando
            Case tg_DrawNetWorkline, tg_MoveNetWorkNode, tg_DrawNetWorkNode, tg_DrawGeometrys   'caso o usuario esteja desenhando uma ou mais geometrias, salva as geometrias
                tcs.saveOnMemory
                tcs.SaveInDatabase
                Screen.MousePointer = vbHourglass
                Screen.MousePointer = vbNormal
            Case tg_SelectObject                                                                'Caso o ususario estiver apenas consultando os atribuitos de uma ou mais geometria. Salva apenas os atributos
                Select Case cgeo.GetLayerTypeReference(tcs.getCurrentLayer)
                    Case LayerTypeRefence.OUTROS
                        MsgBox "Somente leitura", vbExclamation
                    Case Else
                        If tcs.getSelectCount(GetGeometrySELECTed()) = 1 Then
                            varGlobais.realizaCommit = True                                     'indica que está tudo ok para realizar commit. Se algo ocorrer de errado a partir do saveInDatabase ou mesmo nas propriedades, volta como falso e ai não faz nada
                            SaveNetWorkAttributesSingle GetGeometrySELECTed()
                        ElseIf tcs.getSelectCount(GetGeometrySELECTed()) > 1 Then
                            varGlobais.realizaCommit = True                                     'indica que está tudo ok para realizar commit. Se algo ocorrer de errado a partir do saveInDatabase ou mesmo nas propriedades, volta como falso e ai não faz nada
                            SaveNetWorkAttributesMultiple GetGeometrySELECTed()
                        End If
                End Select
                .plotView
            Case tg_MoveNetWorkVertice                                                          'está movendo apenas um vértice da rede de água
'                tcs.saveOnMemory
'                tcs.SaveInDatabase
'                tcs.plotView
            Case tg_MoveGpsPoint
                tcs.saveOnMemory
                tcs.SaveInDatabase
                Screen.MousePointer = vbHourglass
                Screen.MousePointer = vbNormal
        End Select
    End With
    Exit Function
    
Trata_Erro:
    If Err.Number = 0 Or Err.Number = 20 Then
        Resume Next
    ElseIf mid(Err.Description, 1, 9) = "ORA-03114" Then
        PrintErro "clsTerraLib", "Public Function SaveInDatabase()", CStr(Err.Number), CStr(Err.Description), True
        End
    Else
        PrintErro "clsTerraLib", "Public Function SaveInDatabase()", CStr(Err.Number), CStr(Err.Description), True
    End If
    Screen.MousePointer = vbNormal
End Function

Public Function GetGeometrySELECTed() As TypeGeometry
On Error GoTo Trata_Erro
   If tcs.getSelectCount(Polyguns) > 0 Then
      GetGeometrySELECTed = Polyguns
   ElseIf tcs.getSelectCount(lines) > 0 Then
      GetGeometrySELECTed = lines
   ElseIf tcs.getSelectCount(points) > 0 Then
      GetGeometrySELECTed = points
   ElseIf tcs.getSelectCount(texts) > 0 Then
      GetGeometrySELECTed = texts
   End If
   Exit Function
   
Trata_Erro:
   If Err.Number = 0 Or Err.Number = 20 Then
      Resume Next
   Else
      PrintErro "clsTerraLib", "Public Function GetGeometrySELECTed()", CStr(Err.Number), CStr(Err.Description), True
      
   End If
End Function
' Salva somente os atributos de um trecho de rede
'
'
'
Public Function SaveNetWorkAttributesSingle(Geometry As TypeGeometry)
    On Error GoTo Trata_Erro
    
    If CtrlMgr.Itens.Item(1).ValueStore >= 0 Then
       CtrlMgr.SaveProperties tcs.getSelectObjectId(0, Geometry), strUser 'inserido dia 25/11/08 Jonathas
       If Geometry = lines Then
          cgeo.UpdateTextsInLines tcs.getCurrentLayer, tcs.getSelectObjectId(0, Geometry)
       Else
          'SALVA TEXTO DE PONTO SOMENTE SE O PONTO FOR UMA PEÇA DE ESGOTO
          If tcs.getCurrentLayer = "SEWERCOMPONENTS" Then
             cgeo.InsertTextInPoint tcs.getSelectObjectId(0, Geometry), tcs.getSelectObjectId(0, Geometry), tcs.getCurrentLayer
          End If
       End If
    End If
    Exit Function
    
Trata_Erro:
    If Err.Number = 0 Or Err.Number = 20 Then
        Resume Next
    Else
       varGlobais.realizaCommit = False                      'pede para voltar tudo o que está fazendo no banco de dados, para traz e não comitar nada
       ErroUsuario.Registra "clsTerraLib", "SaveNetWorkAttributesSingle", CStr(Err.Number), CStr(Err.Description), True, glo.enviaEmails
    End If
End Function
' Salva os atributos de vários trechos de rede
'
'
'
Public Function SaveNetWorkAttributesMultiple(Geometry As TypeGeometry) As Boolean
    On Error GoTo Trata_Erro
    Dim object_id As String, i As Integer
    If CtrlMgr.Itens.Item(1).ValueStore >= 0 Then
       For i = 0 To tcs.getSelectCount(Geometry) - 1
          object_id = tcs.getSelectObjectId(i, Geometry)
          If object_id <> "" Then
              CtrlMgr.SaveMultProperties object_id, tcs.getCurrentLayer
              If Geometry = lines Then
                 cgeo.UpdateTextsInLines tcs.getCurrentLayer, object_id
              Else
                 cgeo.InsertTextInPoint CLng(object_id), object_id, tcs.getCurrentLayer
                 'cgeo.InsertTextInPoint tcs.getCurrentLayer, object_id
              End If
          End If
       Next
    End If
    Exit Function
   
Trata_Erro:
    If Err.Number = 0 Or Err.Number = 20 Then
        Resume Next
    Else
       varGlobais.realizaCommit = False                      'pede para voltar tudo o que está fazendo no banco de dados, para traz e não comitar nada
       ErroUsuario.Registra "clsTerraLib", "SaveNetWorkAttributesMultiple", CStr(Err.Number), CStr(Err.Description), True, glo.enviaEmails
    End If
End Function
' Cria agora a tabela de atributos de rede
'
' LINE_ID - Id da linha a ser criada
' Node_id1 - Nó inicial da linha a ser criado ou atualizado
' Node_id2 - Nó final da linha a ser criado ou atualizado
' Movendo - indica se está criando ou movendo um trecho de rede
'
Public Function CreatNetWorkAttribute(LINE_ID As Long, Node_id1 As Long, Node_id2 As Long, Movendo As Boolean) As Boolean
    On Error GoTo Trata_Erro
    Dim az As String
    Dim object_id As String, CompCalc As Double, a As Integer, ld As String, nd1 As String, nd2 As String
    Dim LayerName As String
    Dim layerReferencia As String
    Dim i As Byte
    Dim No As String
    Dim getZNo As New CObtemZdoNo                           'método para o cálculo da cota z do nó inserido
    Dim zNo As Double                                       'cota z do nó inserido que será calculada
    Dim stringSQL As String
    Dim rsq As New ADODB.Recordset
    Dim TBGeometria As String
    Dim compMax As Double
    Dim getObjIdsNos As New CObtemObjIdsNos                 'para receber os obj_ids dos nós inicial e final
    Dim noInicial As String
    Dim noFinal As String
    Dim retorno As Boolean
    
    If TerraEvent = tg_DrawNetWorkline Then
        LayerName = tcs.getCurrentLayer
    Else
        LayerName = cgeo.GetReferenceLayer(tcs.getCurrentLayer)
    End If
    layerReferencia = cgeo.GetReferenceLayer(LayerName)                         'obtem o nome do layer de atributos de referência WaterComponents ou SwerComponents
    TBGeometria = tdb.getRepresentationTableName(layerReferencia, tpPOINTS)     'obtem o nome do layer de geometrias de referência Points2 ou Points4
    If Movendo = True Then                                                      'SE ESTIVER APENAS MOVENDO, EXCLUI E REFAZ O TEXTO DA LINHA, E ATUALIZA A COTA DO NÓ
        LayerName = tcs.GetReferenceLayer                                       'SETA O LAYER DE REFERENCIA COMO ATIVO
        cgeo.DeleteTextObjectWithInsertText LayerName, LINE_ID, CStr(LINE_ID)
        'atualização da cota do nó
        'Antes de atribuir o Z do nó verifica se o usuário quer que isso seja realizado enquanto ele desenha a rede. Se o MDT não estiver pronto o usuário pode optar por desligar esta opção
        If varGlobais.deveCalcularZNo = True Then
            No = varGlobais.objIdNoSelecionado
            
            If No <> "NULO" Then
                If frmCanvas.TipoConexao <> 4 Then      'caso seja Oracle ou SQLServer
                    stringSQL = "SELECT object_id FROM " & TBGeometria & " WHERE OBJECT_ID = " & No     'Tabela Points2 para água ou Points4 para esgoto
                    rsq.Open stringSQL, Conn, adOpenDynamic, adLockReadOnly
                Else    'caso seja Postgres
                        'implementar
                End If
                If rsq.EOF = False Then ' SE ENCONTROU O REGISTRO, ou seja o nó existe
                    zNo = getZNo.CObtemZFromObjIdNo(No, TBGeometria)                     'obtem a cota z do nó a partir do object_id do mesmo. Passa o nome do layer de geometrias dos nós de água o esgoto
                    If frmCanvas.TipoConexao <> 4 Then
                        stringSQL = "UPDATE " & layerReferencia & " SET GROUNDHEIGHT = " & Replace(Round(zNo, 2), ",", ".") & " where object_id_ = " & No   'atualiza a cota do nó
                        Conn.execute (stringSQL)
                    Else 'Postgres
                        'Implementar
                    End If
                Else                    'nó não existe
                    'deveria ter encontrado o nó - exibir mensagem de erro
                    MsgBox ("Nó " & No & " não foi encontrado.")
                End If
                rsq.Close
                varGlobais.objIdNoSelecionado = "NULO"                      'para rodar só uma vez (a primeira), pois entra nesta rotina várias vezes para cada seguimento de rede
            End If
        End If
        'fim da atualização da cota do nó movido
    Else                    ' ESTÁ DESENHANDO A REDE
        Set rs = New ADODB.Recordset
        'prepara querie para procurar em todas as linhas de rede se já existe alguma com o object_id_ da que será criada
        If frmCanvas.TipoConexao <> 4 Then
            rsq.Open ("SELECT * FROM " & LayerName & " WHERE OBJECT_ID_ = " & LINE_ID), Conn, adOpenDynamic, adLockReadOnly
        Else
            b = "OBJECT_ID_"
            rsq.Open ("SELECT * FROM " + """" + LayerName + """" + " WHERE " + """" + b + """" + " = '" & LINE_ID & "'"), Conn, adOpenDynamic, adLockOptimistic
        End If
        If rsq.EOF = True Then ' SE NÃO ENCONTROU O REGISTRO
            'CRIA UM NOVO REGISTRO NA TABELA DE ATRIBUTOS DE LINHAS COM LINE_ID,OBJECT_ID_ IGUAL AO CÓDIGO DA GEOMETRIA
            ' O PRIMNEIRO LINE ID É NUMERICO E O SEGUNDO É TEXTO
            If frmCanvas.TipoConexao <> 4 Then
                stringSQL = "INSERT INTO " & LayerName & " (LINE_ID,OBJECT_ID_) VALUES (" & LINE_ID & ",'" & LINE_ID & "')"
            Else
                az = "LINE_ID"
                b = "OBJECT_ID_"
                c = "INSCRICAO_LOTE"
                stringSQL = "INSERT INTO  " + """" + LayerName + """" + " ( " + """" + az + """" + "," + """" + b + """" + ") VALUES ('" & LINE_ID & "','" & LINE_ID & "')"
            End If
            Conn.execute (stringSQL)
        Else
            'NÃO DEVERIA TER ENCONTRADO
        End If
        rsq.Close
        'DLL Pm4Manager SALAVA AS PRORPIEDADES DA LINHA NA TABELA DE ATRIBUTOS
        CtrlMgr.SaveProperties CStr(LINE_ID), strUser
        'OBTEM NA VARIÁVEL CompCalc O COMPRIMENTO DA NOVA LINHA
        tdb.getLengthOfLine LINE_ID, CStr(LINE_ID), CompCalc
        'ATUALIZADO O COMPRIMENTO, NÓ INICIAL E NÓ FINAL DA LINHA NA TABELA DE ATRIBUTOS
        compMax = CompCalc  ' Replace(Round(CompCalc, 2), ",", ".")
        If frmCanvas.TipoConexao <> 4 Then
            If compMax > 9999.99 Then
                ' SE O COMPRIMENTO MAXIMO DE UMA REDE FOR ULTRAPASSADO, ELA SERÁ SALVA NO ENTANTO COM O LIMITE MAXIMO DE COMPRIMENTO
                MsgBox "Rede com comprimento máximo '9999.99' ultrapassado.", vbExclamation, "Aviso"
                stringSQL = "UPDATE " & LayerName & " SET LENGTHCALCULATED = 9999.99, INITIALCOMPONENT = '" & Node_id1 & "', FINALCOMPONENT = '" & Node_id2 & "' WHERE OBJECT_ID_ = '" & LINE_ID & "'"
            Else
                stringSQL = "UPDATE " & LayerName & " SET LENGTHCALCULATED = " & Replace(Round(CompCalc, 2), ",", ".") & ", INITIALCOMPONENT = '" & Node_id1 & "', FINALCOMPONENT = '" & Node_id2 & "' WHERE OBJECT_ID_ = '" & LINE_ID & "'"
            End If
        Else
            az = LayerName
            c = "LENGTHCALCULATED"
            d = "INITIALCOMPONENT"
            e = "FINALCOMPONENT"
            f = "OBJECT_ID_"
            g = Round(Replace(Round(CompCalc, 2), ",", "."))
            h = Round(g)
            If compMax > 9999.99 Then
                ' SE O COMPRIMENTO MAXIMO DE UMA REDE FOR ULTRAPASSADO, ELA SERÁ SALVA NO ENTANTO COM O LIMITE MAXIMO DE COMPRIMENTO
                MsgBox "Rede com comprimento máximo '9999.99' ultrapassado.", vbExclamation, "Aviso"
                stringSQL = "UPDATE " + """" + az + """" + " SET " + """" + c + """" + " = '9999.99', " + """" + d + """" + " = '" & Node_id1 & "', " + """" + e + """" + " = '" & Node_id2 & "' WHERE " + """" + f + """" + " = '" & LINE_ID & "'"
            Else
                ' MsgBox "UPDATE " + """" + az + """" + " SET " + """" + c + """" + " = '" + g + "', " + """" + d + """" + " = '" & Node_id1 & "', " + """" + e + """" + " = '" & Node_id2 & "' WHERE " + """" + f + """" + " = '" & line_id & "'"
                stringSQL = "UPDATE " + """" + LayerName + """" + " SET " + """" + c + """" + " = '" + h + "', " + """" + d + """" + " = '" & Node_id1 & "', " + """" + e + """" + " = '" & Node_id2 & "' WHERE " + """" + f + """" + " = '" & LINE_ID & "'"
                'WritePrivateProfileString "A", "A", stringSQL, App.path & "\DEBUG.INI"
            End If
        End If
        Conn.execute (stringSQL)
        'DEPOIS DE CARREGADAS AS INFORMAÇÕES NA TABELA DE ATRIBUTOS, CHAMA O MÉTODO DE CRIAR TEXTOS
        InsertTextAttributesLine LayerName, LINE_ID, CStr(LINE_ID)                                  'aqui entre outras coisas insere as cotas iniciais e finais no trecho de rede de esgoto, quanto for esgoto
        'xxxxxxxxxxx
        'INSERE O ATRIBUTO DO NÓ 1 E NÓ 2 NA TABELA DE ATRIBUTOS COM O SEGUINTE LOOP
        For i = 1 To 2 'EXECUTA 2 VEZES A ROTINA ABAIXO
            If i = 1 Then
                No = Node_id1
            Else
                No = Node_id2
            End If
            Set rs = New ADODB.Recordset
            If frmCanvas.TipoConexao <> 4 Then
                rsq.Open ("SELECT * FROM " & layerReferencia & " WHERE OBJECT_ID_ = '" & No & "'"), Conn, adOpenDynamic, adLockReadOnly
            Else
                az = "OBJECT_ID_"
                rsq.Open ("SELECT * FROM " + """" + layerReferencia + """" + " WHERE " + """" + az + """" + " = '" & No & "'"), Conn, adOpenDynamic, adLockOptimistic
            End If
            If rsq.EOF = True Then ' SE NÃO ENCONTROU O REGISTRO, ou seja o nó não existe ainda
                'Aqui ele atribui o Z ao nó
                'Antes de atribuir o Z do nó verifica se o usuário quer que isso seja realizado enquanto ele desenha a rede. Se o MDT não estiver pronto o usuário pode optar por desligar esta opção
                If varGlobais.deveCalcularZNo = True Then
                    zNo = getZNo.CObtemZFromObjIdNo(No, TBGeometria)                     'obtem a cota z do nó a partir do object_id do mesmo. Passa o nome do layer de geometrias dos nós de água o esgoto
                Else
                    zNo = 0
                End If
                If frmCanvas.TipoConexao <> 4 Then
                    stringSQL = "INSERT INTO " & layerReferencia & " (COMPONENT_ID,OBJECT_ID_, GROUNDHEIGHT) VALUES (" & No & ",'" & No & "', " & Replace(Round(zNo, 2), ",", ".") & ")"
                    Conn.execute (stringSQL)
                Else
                    az = "OBJECT_ID_"
                    b = "COMPONENT_ID"
                    c = "INSCRICAO_LOTE"
                    d = "TIPO"
                    e = "HIDROMETRADO"
                    f = "ECONOMIAS"
                    g = "CONSUMO_LPS"
                    h = "TB_LIGACOES"
                    Conn.execute ("INSERT INTO " + """" + layerReferencia + """" + " (" + """" + b + """" + "," + """" + az + """" + ") VALUES ('" & No & "','" & No & "')")
                End If
                'CRIA UM NOVO REGISTRO NA TABELA DE ATRIBUTOS DE NÓS COM COMPONENT_ID,OBJECT_ID_
                If cgeo.GetTypeText(LayerName) = 2 Then
                    cgeo.InsertTextInPoint CLng(No), CStr(No), layerReferencia, 0, 0
                End If
            End If
            rsq.Close
        Next
        'Rotina que procura Atributos por referencia
        'If FrmMain.mnuLoadAttributeByReference.Checked Then
        '   LoadAttributeByReference Node_id1
        '   LoadAttributeByReference Node_id2
        'End If
    End If
    CreatNetWorkAttribute = True
    Exit Function

Trata_Erro:
    If Err.Number = 0 Or Err.Number = 20 Then
        Resume Next
    Else
        varGlobais.realizaCommit = False                      'pede para voltar tudo o que está fazendo no banco de dados, para traz e não comitar nada
        ErroUsuario.Registra "clsTerraLib", "CreatNetWorkAttribute", CStr(Err.Number), CStr(Err.Description), True, glo.enviaEmails
    End If
End Function

Private Function NewObject(LayerName As String, ID As Long, object_id As String, Geometry As TypeGeometry, Optional compr As Double) As Boolean
On Error GoTo Trata_Erro
   Dim LenghtCalc As Double
   Dim rsNew As ADODB.Recordset
   Dim strsql As String
   
   If Geometry = lines Then


  
     

     If frmCanvas.TipoConexao <> 4 Then
         
    strsql = "INSERT INTO " & LayerName & " (LINE_ID,OBJECT_ID_) VALUES (" & ID & ",'" & ID & "')"
     Else
     a = "OBJECT_ID_"
      b = "LINE_ID"
      c = "INSCRICAO_LOTE"
      
      e = "HIDROMETRADO"
      f = "LENGTHCALCULATED"
     
      strsql = "INSERT INTO " + """" + LayerName + """" + " (" + """" + b + """" + "," + """" + a + """" + ") VALUES ('" & ID & "','" & ID & "')"
     End If


     
      Conn.execute (strsql)
   
      CtrlMgr.SaveProperties object_id, strUser 'variável strUser incluida em 25/11/08 Jonathas
         
         If frmCanvas.TipoConexao <> 4 Then
         
         Conn.execute "Update " & LayerName & " set lengthcalculated= " & Replace(Round(compr, 2), ",", ".") & " where Object_id_ = " & object_id
         Else
         a = "OBJECT_ID_"
      b = "LINE_ID"
      c = "INSCRICAO_LOTE"
      
      e = "HIDROMETRADO"
      f = "LENGTHCALCULATED"
         Conn.execute "Update " + """" + LayerName + """" + " set " + """" + f + """" + " = '" & Replace(Round(compr, 2), ",", ".") & "' where " + """" + a + """" + " = '" & object_id & "'"
         End If
      'CHAMA O MÉTODO DE CRIAR TEXTOS
      InsertTextAttributesLine LayerName, ID, object_id

   
   ElseIf Geometry = points Then
      
     


     If frmCanvas.TipoConexao <> 4 Then
         
    strsql = "INSERT INTO " & LayerName & " (COMPONENT_ID,OBJECT_ID_) VALUES (" & ID & ",'" & ID & "')"
     Else
      a = "OBJECT_ID_"
      b = "COMPONENT_ID"
      c = "INSCRICAO_LOTE"
     
     
     strsql = "INSERT INTO " + """" + LayerName + """" + " (" + """" + b + """" + "," + """" + a + """" + ") VALUES ('" & ID & "','" & ID & "')"
     End If
      
      
      
     
      Conn.execute (strsql)
      If cgeo.GetTypeText(LayerName) = 2 Then
         cgeo.InsertTextInPoint ID, object_id, LayerName, 0, 0
      End If
    
   End If
      
   NewObject = True

Trata_Erro:
    If Err.Number = 0 Or Err.Number = 20 Then
       Resume Next
    Else
      
      PrintErro "clsTerraLib", "Private Function NewObject", CStr(Err.Number), CStr(Err.Description), True
       
    End If
End Function
' Insere os atributos de texto da linha no banco de dados
'
' LayerName - nome do layer
' geom_id - da linha
' object_id - da linha
'
Private Function InsertTextAttributesLine(LayerName As String, geom_id As Long, object_id As String) As Boolean
    On Error GoTo Trata_Erro
    Dim Length As Double, Diameter As String, Material As String, CotaIni As Double, CotaFim As Double

    If cgeo.GetTypeText(tcs.getCurrentLayer) = 1 Then       ' redes de água
        a = "X_MATERIAL"
    Else                                                    ' redes de esgoto ou drenagem
        a = "X_MATERIAL_ESGOTO"
    End If
    
    b = "MATERIAL"
    c = "MATERIALID"
    d = "OBJECT_ID_"
    If frmCanvas.TipoConexao <> 4 Then
        Set rs = Conn.execute("SELECT * From  " & tcs.getCurrentLayer & " left JOIN " & a & " on material=materialid where object_id_='" & object_id & "'")
    Else
        'MsgBox "ARQUIVO DEBUG SALVO"
        'WritePrivateProfileString "A", "A", "SELECT * From  " + """" + tcs.getCurrentLayer + """" + " left JOIN " + """" + a + """" + " on" + """" + b + """" + "=" + """" + c + """" + " where " + """" + d + """" + "='" & object_id & "'", App.path & "\DEBUG.INI"
        Set rs = Conn.execute("SELECT * From  " + """" + tcs.getCurrentLayer + """" + " left JOIN " + """" + a + """" + " on" + """" + b + """" + "=" + """" + c + """" + " where " + """" + d + """" + "='" & object_id & "'")
    End If
    If Not rs.EOF Then
        Diameter = Chr$(216) & " " & IIf(IsNull(rs!INTERNALDIAMETER), 0, rs!INTERNALDIAMETER)
        Material = IIf(IsNull(rs!MATERIALNAME), 0, rs!MATERIALNAME)
        Length = IIf(rs!Length = 0, rs!LENGTHCALCULATED, rs!Length)
        If tcs.getCurrentLayer = "SEWERLINES" Then                                                              'aqui deve ser inserida a cota a partir do MDT. Tenho o geom_id e obj_id da rede de esgoto
            CotaIni = rs!INITIALTUBEDEEPNESS
            CotaFim = rs!FINALTUBEDEEPNESS
        Else
            CotaIni = rs!INITIALGROUNDHEIGHT
            CotaFim = rs!FINALGROUNDHEIGHT
        End If
        'insere no banco de dado os textos do trecho de rede
        If cgeo.GetTypeText(tcs.getCurrentLayer) = 1 Then
            cgeo.InsertTextInDatabase geom_id, object_id, Diameter, Material, Length                           'insere sem as cotas iniciais e finais (redes de água)
        Else
            cgeo.InsertTextInDatabase geom_id, object_id, Diameter, Material, Length, True, CotaIni, CotaFim   'insere com as cotas iniciais e finais (redes de esgoto e drenagem)
        End If
    End If
    rs.Close
    Set rs = Nothing

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

Public Function CreatNetWorkNode(ByVal node_id As Long, ByVal line1_id As Long, ByVal line2_id As Long, Movendo As Boolean)

On Error GoTo Trata_Erro
   
   Dim Nd As String, ld1 As String, ld2 As String, object_id As String
   Dim IniComp As String, FimComp As String, CompCalc As Double, LayerName As String, LayerREf As String
   
   LayerREf = tcs.GetReferenceLayer ' cgeo.GetReferenceLayer(LayerName)
   tdbcon.setCurrentLayer LayerREf 'cgeo.GetReferenceLayer(tcs.getCurrentLayer)
   LayerName = tcs.getCurrentLayer
   
   If Movendo = True Then
      
      'VERIFICA SE O TIPO DO PONTO É ESGOTO, CASO SIM ELE RECEBE TEXTO
      If cgeo.GetTypeText(LayerName) = 2 Then
         
         cgeo.InsertTextInPoint node_id, CStr(node_id), LayerName, 0, 0

      End If
   
   Else
      
      'RETORNA O NÓ INICIAL E FINAL DA LINHA EXISTENTE QUE RECEBEU O NOVO NÓ
      Set rs = New ADODB.Recordset
      If frmCanvas.TipoConexao <> 4 Then
      rs.Open ("SELECT INITIALCOMPONENT, FINALCOMPONENT FROM " & LayerName & " WHERE OBJECT_ID_ = '" & line1_id & "'"), Conn, adOpenForwardOnly, adLockReadOnly
      Else
      f = "INITIALCOMPONENT"
      g = "FINALCOMPONENT"
      h = "OBJECT_ID_"
      'Dim aaa As String
      'aaa = "SELECT " + """" + f + """" + ", " + """" + g + """" + " FROM " + """" + LayerName + """" + " WHERE " + """" + h + """" + " = '" & line1_id & "'"

      
   '   MsgBox "ARQUIVO DEBUG SALVO"
' WritePrivateProfileString "A", "A", aaa, App.path & "\DEBUG.INI"

      
      
      
      
      rs.Open ("SELECT " + """" + f + """" + ", " + """" + g + """" + " FROM " + """" + LayerName + """" + " WHERE " + """" + h + """" + " = '" & line1_id & "'"), Conn, adOpenDynamic, adLockOptimistic
      End If
      
      If rs.EOF = False Then
         IniComp = rs.Fields("InitialComponent").value
         FimComp = rs.Fields("FinalComponent").value
      End If
      
      rs.Close
      Set rs = Nothing
   
      Nd = node_id
      'MOMENTO QUE CRIA O ATRIBUTO DO NOVO COMPONENTE DE REDE
      
      'strsql = "INSERT INTO " & LayerName & " (COMPONENT_ID,OBJECT_ID_) VALUES (" & node_id & ",'" & node_id & "')"
      
      
     If frmCanvas.TipoConexao <> 4 Then
         
   Conn.execute ("INSERT INTO " & LayerREf & " (COMPONENT_ID,OBJECT_ID_) VALUES (" & node_id & ",'" & node_id & "')")
     
     Else
      
        a = "OBJECT_ID_"
      b = "COMPONENT_ID"
      c = "LINE_ID"
    
     
   Conn.execute ("INSERT INTO " + """" + LayerREf + """" + " (" + """" + b + """" + "," + """" + a + """" + ") VALUES ('" & node_id & "','" & node_id & "')")
     End If
      
      
      
      
      'VERIFICA SE O TIPO DO PONTO É ESGOTO, CASO SIM ELE RECEBE TEXTO
      If cgeo.GetTypeText(LayerName) = 2 Then
         cgeo.InsertTextInPoint node_id, CStr(node_id), LayerREf, 0, 0
      End If
      
         
      'CRIA ATRIBUTOS PARA A NOVA LINHA
      
      If frmCanvas.TipoConexao <> 4 Then
         
   Conn.execute ("INSERT INTO " & LayerName & " (LINE_ID,OBJECT_ID_) VALUES (" & line2_id & ",'" & line2_id & "')")
   
     Else
      a = "OBJECT_ID_"
      b = "COMPONENT_ID"
      c = "LINE_ID"
      d = "TIPO"
     
   Conn.execute ("INSERT INTO " + """" + LayerName + """" + " (" + """" + c + """" + "," + """" + a + """" + ") VALUES ('" & line2_id & "','" & line2_id & "')")
   
   End If
      
            
      'DLL Pm4Manager SALVA OS ATRIBUTOS
      CtrlMgr.SaveProperties CStr(line2_id), strUser 'variável strUser incluida em 25/11/08 Jonathas
         
      'ATUALIZA O COMPRIMENTO DA NOVA LINHA
      tdb.getLengthOfLine line2_id, CStr(line2_id), CompCalc
      If frmCanvas.TipoConexao <> 4 Then
      Conn.execute "UPDATE " & LayerName & " SET LENGTHCALCULATED = " & Replace(Round(CompCalc, 2), ",", ".") & " WHERE OBJECT_ID_ = '" & line2_id & "'"
      Else
      a = LayerName
b = "+a+"
c = "LENGTHCALCULATED"
d = Round(Replace(Round(CompCalc, 2), ",", "."))
e = "'d'"
f = "OBJECT_ID_"

       Conn.execute "UPDATE " + """" + a + """" + " SET " + """" + c + """" + " = '" & d & "' WHERE " + """" + f + """" + " = '" & line2_id & "'"
      End If
      'ATUALIZA COMPONENTE INICIAL E FINAL DA NOVA LINHA
      If frmCanvas.TipoConexao <> 4 Then
      
      
     
      
      Conn.execute "Update " & LayerName & " set InitialComponent= " & node_id & ", FinalComponent=" & FimComp & " WHERE OBJECT_ID_ = '" & line2_id & "'"
      Else
      
      a = LayerName

c = "LENGTHCALCULATED"


f = "OBJECT_ID_"
g = "NODE_ID"
h = "INITIALCOMPONENT"
i = "FINALCOMPONENT"
j = FimComp

Dim aaa, bbb, ccc As String
aaa = str(j)
bbb = str(node_id)
ccc = str(line2_id)


      
      '  Dim bbbb As String
   ' bbbb = "Update " + """" & LayerName & """" + " set " + """" + "INITIALCOMPONENT" + """" + "=" + bbb
   '   MsgBox "ARQUIVO DEBUG SALVO"
 'WritePrivateProfileString "A", "A", bbbb, App.path & "\DEBUG.INI"

      
      Conn.execute "Update " + """" & LayerName & """" + " set " + """" + "INITIALCOMPONENT" + """" + "='" + bbb + "', " + """" + "FINALCOMPONENT" + """" + "= ' " & aaa & " ' WHERE " + """" + "OBJECT_ID_" + """" + " = '" & ccc & "'"
      

      End If
      'CHAMA O MÉTODO DE CRIAR TEXTOS PARA A NOVA LINHA
      InsertTextAttributesLine LayerName, line2_id, CStr(line2_id)
         

            
      
      'ATUALIZA O COMPRIMENTO DA NOVA LINHA
      tdb.getLengthOfLine line1_id, CStr(line1_id), CompCalc
       If frmCanvas.TipoConexao <> 4 Then
      Conn.execute "Update " & LayerName & " set lengthcalculated= " & Replace(Round(CompCalc, 2), ",", ".") & " where Object_id_ = '" & line1_id & "'"
      Else
      a = LayerName
b = "+a+"
c = "LENGTHCALCULATED"
d = Round(Replace(Round(CompCalc, 2), ",", "."))
e = "'d'"
f = "OBJECT_ID_"
g = "NODE_ID"
h = "INITIALCOMPONENT"
i = "FINALCOMPONENT"
j = IniComp
k = "'j'"
l = node_id
m = "'l'"
      Conn.execute "Update " + """" + a + """" + " set " + """" + c + """" + "='" + d + "' where " + """" + f + """" + " = '" & line1_id & "'"
      End If
      'ATUALIZA COMPONENTE INICIAL E FINAL DA ANTIGA LINHA
        If frmCanvas.TipoConexao <> 4 Then
      Conn.execute "Update " & LayerName & " set InitialComponent= " & IniComp & ", FinalComponent=" & node_id & " WHERE OBJECT_ID_ = '" & line1_id & "'"
      Else
      
                    
      
      a = LayerName
b = "+a+"
c = "LENGTHCALCULATED"
d = Replace(Round(CompCalc, 2), ",", ".")
e = "'d'"
f = "OBJECT_ID_"
g = "NODE_ID"
h = "INITIALCOMPONENT"
i = "FINALCOMPONENT"
j = IniComp
k = "'j'"
l = node_id
m = "'l'"
      
      
      
        Conn.execute "Update " + """" + a + """" + " set " + """" + h + """" + " = '" & IniComp & "', " + """" + i + """" + "='" & node_id & "' WHERE " + """" + f + """" + " = '" & line1_id & "'"
      End If
      'CHAMA O MÉTODO DE EXCLUIR E CRIAR TEXTOS PARA A ANTIGA LINHA
      cgeo.DeleteTextObjectWithInsertText LayerName, line1_id, CStr(line1_id)
         
      

      
      If UCase(LayerName) = "WATERLINES" Then
         'REASSOCIA OS RAMAIS DE AGUA A NOVA REDE PARTINDO DO PRINCÍPIO QUE ELA ESTEJA NO MESMO LOCAL DA ANTIGA
         Set rs = New ADODB.Recordset
         If frmCanvas.TipoConexao <> 4 Then
         rs.Open ("SELECT OBJECT_ID_ FROM RAMAIS_AGUA WHERE OBJECT_ID_TRECHO = '" & line1_id & "'"), Conn, adOpenDynamic, adLockReadOnly
         Else
            a = "RAMAIS_AGUA"
      b = "OBJECT_ID_"
      c = "OBJECT_ID_TRECHO"
        rs.Open ("SELECT " + """" + b + """" + " FROM " + """" + a + """" + " WHERE " + """" + c + """" + " = '" & line1_id & "'"), Conn, adOpenDynamic, adLockOptimistic
         End If

     If frmCanvas.TipoConexao <> 4 Then
         Conn.execute ("DELETE FROM POLIGONO_SELECAO WHERE USUARIO = '" & strUser & "' AND TIPO = '2'")
         Else
a = "POLIGONO_SELECAO"
b = "USUARIO"
c = "TIPO"

         Conn.execute ("DELETE FROM " + """" + a + """" + " WHERE " + """" + b + """" + " = '" & strUser & "' AND " + """" + c + """" + " = '2'")
         End If
         If rs.EOF = False Then
            Do While Not rs.EOF
            
              

     If frmCanvas.TipoConexao <> 4 Then
    
         
     Conn.execute ("INSERT INTO POLIGONO_SELECAO (OBJECT_ID_,USUARIO,TIPO) VALUES ( '" & rs!Object_id_ & "','" & strUser & "',2)")
     
     Else
     a = "POLIGONO_SELECAO"
      b = "OBJECT_ID_"
      c = "USUARIO"
      d = "TIPO"
      e = "HIDROMETRADO"
      f = "ECONOMIAS"
      g = "CONSUMO_LPS"
      h = "TB_LIGACOES"


 Conn.execute ("INSERT INTO " + """" + a + """" + " (" + """" + b + """" + "," + """" + c + """" + "," + """" + d + """" + ") VALUES ( '" & rs!Object_id_ & "','" & strUser & "','2')")
     End If
            
            
              
               rs.MoveNext
            Loop
            
            'CHAMA A FUNÇÃO PARA RELOCALIZAR TRECHOS DOS RAMAIS DE AGUA
            frmAlteraNoPoligono.ATUALIZA_TRECHOS_RAMAIS_AGUA
         
         End If
         rs.Close
        
      End If
   End If
   
'   'Rotina que procura Atributos por referencia
'   If FrmMain.mnuLoadAttributeByReference.Checked Then
'      LoadAttributeByReference node_id
'   End If
   
Trata_Erro:
    If Err.Number = 0 Or Err.Number = 20 Then
       Resume Next
    Else
      
      PrintErro "clsTerraLib", "Public Function CreatNetWorkNode", CStr(Err.Number), CStr(Err.Description), True
       
    End If
End Function

Public Function DistanceBetween(ByVal X1 As Double, ByVal Y1 As Double, ByVal X2 As Double, ByVal Y2 As Double) As Double
  ' Calculate the distance between two points, given their X/Y coordinates.
  
  ' The short version...
  DistanceBetween = Sqr((Abs(X2 - X1) ^ 2) + (Abs(Y2 - Y1) ^ 2))
  
End Function


Public Function SearchGeomtryForAttribute()
'   On Error GoTo SearchGeomtryForAttribute_err
'   Dim xmin As Double, ymin As Double, xmax As Double, ymax As Double, Object_id As String
'   Object_id = FrmMsg.init("Entre com o número da inscrição", gsInteger)
'
'   CleanAllGeometries
'   If Object_id <> "" Then
'      With tcs
'         .Normal
'         Set rs = Conn.execute("SELECT Object_id_ From lotes where inscricao='" & Object_id & "'")
'         If Not rs.EOF Then
'            Object_id = IIf(IsNull(rs!Object_id_), "", rs!Object_id_)
'            If Object_id <> "" Then
'               If .addSELECTObjectIds(Object_id) = 1 Then
'                  .getSELECTBox xmin, ymin, xmax, ymax
'                  .setWorld xmin - 1000, ymin - 1000, xmax + 1000, ymax + 1000
'                  .SELECT
'                  .setScale 1000
'               Else
'                  MsgBox "Objecto não encontrado", vbExclamation
'               End If
'            Else
'               MsgBox "Objecto não encontrado", vbExclamation
'            End If
'         Else
'            MsgBox "Número da inscrição não encontrado", vbExclamation
'
'         End If
'         rs.Close
'         Set rs = Nothing
'      End With
'   End If
'   Exit Function
'SearchGeomtryForAttribute_err:
'   MsgBox Err.Description
   Dim frm As New FrmPesquisaCliente
   frm.Init tcs
   Set frm = Nothing
End Function

Public Sub DrawInterSection()
   tcs.drawWithCircleIntersection True
End Sub

Private Sub CleanAllGeometries()
   With tcs
     .clearSelectItens 1
     .clearSelectItens 2
     .clearSelectItens 128
     .clearSelectItens 4
   End With
End Sub


Private Sub Class_Initialize()
   Set cgeo = New clsGeoReference
End Sub

Private Sub Class_Terminate()
   Set cgeo = Nothing
End Sub

Public Function PmNewLine(LayerName As String, Geometry As TypeGeometry, object_id As String) As Boolean
   
   'CRIA UMA NOVA LINHA NA TABELA DE ATRIBUTOS
   
   Dim Object_id_  As String
   Dim rsNew As ADODB.Recordset
   Set rsNew = New ADODB.Recordset
   
   rsNew.Open LayerName, Conn, adOpenKeyset, adLockOptimistic, adCmdTable
   
   rsNew.AddNew
                                          'INSERE MESMO CÓDIGO DO AUTO NUMBER DA TABELA DE GEOMETRIAS
   rsNew("OBJECT_ID_").value = object_id  '                                                       XXXXX strUser & Now ' INSERE VALOR EM OBJECT_ID_ PARA QUE SEJA CRIADO O CÓDIGO AUTO NUMERAÇÃO EM LINE_ID

   rsNew("LINE_ID").value = object_id

   rsNew.Update
   
  
   rsNew.Close
   Set rsNew = Nothing
   
  
   PmNewLine = True

End Function
Public Function PmNewPoint(LayerName As String, Geometry As TypeGeometry, object_id As String) As Boolean
   
   'CRIA UMA NOVA LINHA NA TABELA DE ATRIBUTOS
   
   Dim Object_id_  As String
   Dim rsNew As ADODB.Recordset
   Set rsNew = New ADODB.Recordset
   
   rsNew.Open LayerName, Conn, adOpenKeyset, adLockOptimistic, adCmdTable
   
   rsNew.AddNew
                                          'INSERE MESMO CÓDIGO DO AUTO NUMBER DA TABELA DE GEOMETRIAS
   rsNew("OBJECT_ID_").value = object_id  '                                                       XXXXX strUser & Now ' INSERE VALOR EM OBJECT_ID_ PARA QUE SEJA CRIADO O CÓDIGO AUTO NUMERAÇÃO EM LINE_ID
      
   rsNew.Update
   
   rsNew.Close
   Set rsNew = Nothing
   
   
   PmNewPoint = True

End Function
Private Sub LoadAttributeByReference(geom_id As Long)


' FUNÇÃO ESTÁ DESABILITADA


   On Error GoTo Trata_Erro
   
   If CDbl(geom_id) = 0 Then
      Exit Sub
   End If
   
   Dim rsPoints As New ADODB.Recordset, rsRef As New ADODB.Recordset
   Dim id_Type As Integer, Id_SubType As Integer, value As Integer
   Dim layer_id As Integer, Attr_Link As String
   
   If cgeo.GetLayerAttrib("WATERCOMPONENTS", layer_id, Attr_Link) Then
   If frmCanvas.TipoConexao <> 4 Then
      rsPoints.Open "SELECT * from points" & layer_id & " inner join watercomponents on object_id = object_id_ where geom_id=" & geom_id, Conn, adOpenKeyset, adLockOptimistic, adCmdText
      Else
      a = "points"
      b = "WATERCOMPONENTS"
      c = "OBJECT_ID_"
      d = "obect_id"
      e = "geom_id"
      rsPoints.Open "SELECT * from " + """" + a + layer_id + """" + " inner join " + """" + b + """" + " on " + """" + d + """" + " = " + """" + c + """" + " where " + """" + e + """" + "='" & geom_id & "'", Conn, adOpenKeyset, adLockOptimistic, adCmdText
      End If
      If rsPoints.EOF = False Then
         
         If rsPoints!id_Type > 0 Then
            rsPoints.Close
            Set rsPoints = Nothing
            Exit Sub
         End If
         
         With tdbconref
            
            .setCurrentLayer "WATERCOMPONENTS"
            
            If .locateGeometry(rsPoints!X, rsPoints!Y, 4, 0.7) Then
               If cgeo.GetLayerAttrib("CEL_PONTO_ATRIB", layer_id, Attr_Link) Then
                  rsRef.Open "SELECT * from CEL_PONTO_ATRIB where " & Attr_Link & "=" & .objectIds(0), Conn, adOpenKeyset, adLockOptimistic
                  If GetTypeObjectByReference(rsRef!NOME_CELUL, id_Type, Id_SubType, value) Then
                       If frmCanvas.TipoConexao <> 4 Then
                        Conn.execute "update watercomponents set id_type=" & id_Type & _
                                                                 ", angle = " & Replace(rsRef!ANGULO, ",", ".") & _
                                                                 ", NOME_CELUL = '" & Replace(rsRef!NOME_CELUL, ",", ".") & _
                                                                 "', ORIGEM_CAL = '" & Replace(rsRef!ORIGEM_CAL, ",", ".") & _
                                                                 "', X_ = " & Replace(rsRef!X_, ",", ".") & _
                                                                 ", Y_ = " & Replace(rsRef!Y_, ",", ".") & _
                                                                 ", COR = " & Replace(rsRef!COR, ",", ".") & _
                                                                 ", TAMANHO_X = " & Replace(rsRef!TAMANHO_X, ",", ".") & _
                                                                 ", TAMANHO_Y = " & Replace(rsRef!TAMANHO_Y, ",", ".") & _
                                                                 ", CENT_CEL_X = " & Replace(rsRef!CENT_CEL_X, ",", ".") & _
                                                                 ", CENT_CEL_Y = " & Replace(rsRef!CENT_CEL_Y, ",", ".") & _
                                                                 ", COR_CELULA = " & Replace(rsRef!COR_CELULA, ",", ".") & _
                                                                 ", ESC_CEL_X = " & Replace(rsRef!ESC_CEL_X, ",", ".") & _
                                                                 ", ESC_CEL_Y = " & Replace(rsRef!ESC_CEL_Y, ",", ".") & _
                                                                 " where object_id_ =" & rsPoints!object_id
                                                                 
                                                                 
Else
                                                                 
Dim a1 As String
Dim b1 As String
Dim c1 As String
Dim d1 As String
Dim e1 As String
Dim f1 As String
Dim g1 As String
Dim h1 As String
Dim i1 As String
Dim j1 As String
Dim k1 As String
Dim l1 As String
                                                                 
Dim a2 As String
Dim b2 As String
Dim c2 As String
Dim d2 As String
Dim e2 As String
Dim f2 As String
Dim g2 As String
Dim h2 As String
Dim i2 As String
Dim j2 As String
Dim k2 As String
Dim l2 As String
Dim m2 As String
 Dim m3 As String
 Dim m4 As String
  Dim m5 As String
a = "WATERCOMPONENTS"
b = "ID_TYPE"
c = id_Type
d = "'c'"
e = "ANGLE"
f = Replace(rsRef!ANGULO, ",", ".")
g = "'f'"
h = "NOME_CELUL"
i = Replace(rsRef!NOME_CELUL, ",", ".")
j = "'i'"
k = "X_"
l = "Y_"
m = "COR"
a1 = "TAMANHO_X"
b1 = "TAMANHO_Y"
c1 = "CENT_CEL_X"
d1 = "CENT_CEL_X"
e1 = "ANGLE"
f1 = "CENT_CEL_X"
g1 = "CENT_CEL_Y"
h1 = "COR_CELULA"
i1 = "ESC_CEL_X"
j1 = "ESC_CEL_Y"
k1 = "OBJECT_ID_"
l1 = Replace(rsRef!ORIGEM_CAL, ",", ".")
a2 = Replace(rsRef!X_, ",", ".")
b2 = Replace(rsRef!Y_, ",", ".")
c2 = Replace(rsRef!COR, ",", ".")
d2 = Replace(rsRef!TAMANHO_X, ",", ".")
e2 = Replace(rsRef!TAMANHO_Y, ",", ".")
f2 = Replace(rsRef!CENT_CEL_X, ",", ".")
g2 = Replace(rsRef!CENT_CEL_Y, ",", ".")
h2 = Replace(rsRef!COR_CELULA, ",", ".")
i2 = Replace(rsRef!ESC_CEL_X, ",", ".")
j2 = Replace(rsRef!ESC_CEL_Y, ",", ".")
h2 = rsPoints!object_id
l2 = h2
m2 = "id_type"
m3 = "ORIGEM_CAL"
m4 = l1
m5 = i


Conn.execute "update " + """" + a + """" + " set " + """" + m2 + """" + " ='" & id_Type & _
"', " + """" + e + """" + " = '" & f & _
"'," + """" + h + """" + " =  '" & m5 & _
"', " + """" + m3 + """" + " = '" & l1 & _
"'," + """" + k + """" + " = '" & a2 & _
"', " + """" + l + """" + " = '" & b2 & _
"', " + """" + m + """" + " = '" & c2 & _
"', " + """" + a1 + """" + " = '" & d2 & _
"', " + """" + b1 + """" + " = '" & e2 & _
"', " + """" + i2 + """" + " = '" & f2 & _
"', " + """" + g1 + """" + " = '" & g2 & _
"', " + """" + h1 + """" + " = '" & h2 & _
"', " + """" + i1 + """" + " = '" & i2 & _
"', " + """" + j1 + """" + " = '" & j2 & _
"' where " + """" + k1 + """" + " ='" & l2 & "'"
                                                                 
End If
                                                                 
                                                                 
                                         
                                                                 
                                                                 
                                                                 
                     If Id_SubType > 0 Then
                     
                       
      b = "WATERCOMPONENTSDATA"
      c = "OBECT_ID"
      d = "ID_TYPE"
      e = "ID_SUBTYPE"
      f = "VALUE_"
      


     If frmCanvas.TipoConexao <> 4 Then
         
     Conn.execute "Insert Into WaterComponentsData (object_id_,id_type,id_subtype,value_)" & _
                           "values(" & rsPoints!object_id & "," & id_Type & "," & Id_SubType & "," & value & ")"
     
     Else
     
      Conn.execute "Insert Into " + """" + b + """" + "(" + """" + c + """" + "," + """" + d + """" + "," + """" + e + """" + "," + """" + f + """" + ")" & _
                           "values('" & rsPoints!object_id & "','" & id_Type & "','" & Id_SubType & "','" & value & "')"
     End If
                     
                     
                     
                        
                     End If
                  End If
                  rsRef.Close
               End If
            End If
         End With
      End If
   End If
   

Trata_Erro:
    If Err.Number = 0 Or Err.Number = 20 Then
       Resume Next
    Else
        'xxxx mantido do tratamento antigo
        If Not rsRef Is Nothing Then
           If rsRef.State = 1 Then rsRef.Close
           Set rsRef = Nothing
        End If
        If Not rsPoints Is Nothing Then
           If rsPoints.State = 1 Then rsPoints.Close
           Set rsPoints = Nothing
        End If
        'xxxx
      
         PrintErro "clsTerraLib", "Private Sub LoadAttributeByReference", CStr(Err.Number), CStr(Err.Description), True
       
    End If

End Sub

Function GetTypeObjectByReference(Description As String, id_Type As Integer, Id_SubType As Integer, value As Integer) As Boolean
   GetTypeObjectByReference = True
   Select Case Description
      Case "ADAPTA"
         id_Type = 31 'Adaptador
      Case "BOOSTE"
         id_Type = 36 'Booster
      Case "CRUSEM", "CRUZET"
         id_Type = 18 'cruzeta
         Id_SubType = 4
         value = 2
      Case "CUR011"
         id_Type = 18 'cruzeta
         Id_SubType = 4
         value = 7
      Case "CUR022"
         id_Type = 18 'cruzeta
         Id_SubType = 4
         value = 6
      Case "CUR045", "CUR315"
         id_Type = 18 'cruzeta
         Id_SubType = 4
         value = 4
      Case "CUR090", "CUR270"
         id_Type = 18 'cruzeta
         Id_SubType = 4
         value = 3
      Case "CUR337"
         id_Type = 18 'cruzeta
         Id_SubType = 4
         value = 6
      Case "CUR349"
         id_Type = 18 'cruzeta
         Id_SubType = 4
         value = 7
      Case "DESCA1", "DESCAR", "DESCAR"
         id_Type = 23
      Case "GIBAUL"
         id_Type = 34
      Case "HIDRAN"
         id_Type = 2
      Case "HIDROM"
         id_Type = 28
      Case "HIDSUB"
         id_Type = 26
      Case "HIDVAL"
         id_Type = 25
      Case "LUVA", "LUVA__"
         id_Type = 33
      Case "MACROM"
         id_Type = 38
      Case "RED", "REDUCA"
         id_Type = 37
      Case "REGGEN", "REGIST", "RREGIS"
         id_Type = 1
      Case "TAP"
         id_Type = 32
      Case "TE", "TEE001", "TEE002"
         id_Type = 18
         Id_SubType = 4
         value = 1
      Case "VALESF"
         id_Type = 1
         Id_SubType = 1
         value = 2
      Case "VALRED"
         id_Type = 18
         Id_SubType = 5
         value = 5
      Case "VALRET"
         id_Type = 1
         Id_SubType = 1
         value = 4
      Case "VALVA", "VALVUL"
         id_Type = 1
         Id_SubType = 1
         value = 0
      Case "VENTOS"
         id_Type = 22
      Case "VENTUR"
         id_Type = 29
      Case "PLUGUE" ' CAP
         id_Type = 35
      Case "AMARRA", "ARVORE", "BL2", "BOCALE", "BOCALO", "CV", "CXDRE", "CXDREE", _
         "CAV", "CAVAL", "CAVAL_", "$D", "$U", "A3", "ACO", "EPS", _
         "FILTRO", "FROM_B", "INCE", "MADE", "MARC", "NORTE", "PIQUET", "PLUGUE", "POSTE", "PV", "PVDRE", _
         "SETA", "SETDRE", "SETESG"
         GetTypeObjectByReference = False
   End Select
      
End Function