summaryrefslogtreecommitdiff
path: root/src/AIS/AIS_LocalContext_1.cxx
blob: 4e47fe794f3d4aec70ea4a52ad608e67208cbc89 (plain)
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
// Copyright: 	Matra-Datavision 1996
// File:	AIS_LocalContext_1.cxx
// Created:	Wed Oct 30 11:09:05 1996
// Author:	Robert COUBLANC
//		<rob>
// Modified by rob Thu Apr 02 1998 
//              - use of optimisation in SelectMgr_ViewerSelector
//              -> Best management in detected entities...

#define BUC60569 	//GG_051199 Enable to select the local context 
//			in any case and especially in multi selection mode.
//			Note that right now when an hilighted owner is selected
//			this owner is unhilighted,this permits to see the selection!
//		 	Principle : an owner can have 3 state :
//			1 : The owner is selected and no more highlightable
//			0 : The owner is NOT selected
//			-1: The owner is selected but stay highlightable (NEW)

// IMP230600	//GG Add protection on selection methodes
//			when nothing is selected

#define BUC60726        //GG_040900 When nothing is detected, 
//			Clear the last temporary stuff in any case

#define BUC60765	//GG_121000 Avoid to raise when the same selection 
//			is attached to several local context.

#define BUC60771	//GG_261000	Avoid to crash after closing a view
//			containing a selected entity and creating a new one.

#define BUC60774	//GG_261000	Returns right select status on
//			bounding-box selection type.

#define BUC60818	//GG_300101	Enable detection even if
//			SetAutomaticHilight(FALSE) has been used.

#define IMP300101       //GG Enable to use polygon highlighting

#define BUC60863	//GG_270301 Clear hilight soon after selecting or
//                      unselecting something in Local Context mode.

#define BUC60876	//GG_050401 Clear selection always even
//			if the current highlight mode is not 0.

#define BUC60953        //SAV_060701 For Select optimization. Selection by rectangle case.
                        // for single selection no optimization done.

#define IMP120701	//SZV made a shape valid for selection
//			when required.

#define IMP160701       //SZV Add InitDetected(),MoreDetected(),NextDetected(),
//                       DetectedCurrentShape(),DetectedCurrentObject()
//                       methods

#define OCC138          //VTN Avoding infinit loop in AddOrRemoveSelected method.

#define OCC189          //SAV: 18/03/02 AIS_Selection::Objects() returns ListOfTransient
                        // instead of array.
  
#define USE_MAP         //san : 18/04/03 USE_MAP - additional datamap is used to speed up access 
                        //to certain owners in AIS_Selection::myresult list  

#define IMP120402	// GG : Add protection in manual detection methods 
//			after deselecting any item using ShiftSelect action. 
//			Thanks to Ivan FONTAINE of SAMTECH company

#define IMP051001	//GG manage Z detection 

#define OCC9026		//AEL Performance optimization of the FindSelectedOwnerFromShape() method.

#include <AIS_LocalContext.jxx>
#include <StdSelect_BRepOwner.hxx>
#include <TColStd_MapOfTransient.hxx>
#include <TColStd_MapIteratorOfMapOfTransient.hxx>
#include <Prs3d_Presentation.hxx>
#include <Prs3d_Drawer.hxx>
#include <Prs3d_ShadingAspect.hxx>
#include <AIS_LocalStatus.hxx>
#include <StdPrs_WFShape.hxx>
#include <Visual3d_TransientManager.hxx>
#include <Graphic3d_Array1OfVertex.hxx>
#include <Graphic3d_Group.hxx>
#include <Select3D_SensitiveTriangulation.hxx>
#include <SelectBasics_SensitiveEntity.hxx>
#include <TCollection_AsciiString.hxx>
#ifdef OCC9026
#include <SelectMgr_DataMapIteratorOfDataMapOfIntegerSensitive.hxx>
#endif
#include <SelectMgr_Selection.hxx>
#include <OSD_Environment.hxx>
#include <SelectMgr_DataMapOfObjectOwners.hxx>

#include <Geom_Transformation.hxx>
#include <AIS_Selection.hxx>
#ifdef IMP120701
#include <AIS_Shape.hxx>
#endif


static Standard_Integer GetHiMod(const Handle(AIS_InteractiveObject)& IO)
{
  return IO->HasHilightMode() ? IO->HilightMode():0;
}

//==================================================
// Function: 
// Purpose :
//==================================================
AIS_StatusOfDetection AIS_LocalContext::MoveTo(const Standard_Integer Xpix,
					       const Standard_Integer Ypix,
					       const Handle(V3d_View)& aview)
{
  // check that ViewerSelector gives 
  if(aview->Viewer()== myCTX->CurrentViewer()) {
#ifdef IMP160701
    //Nullify class members storing information about detected AIS objects.
    myAISCurDetected = 0;
    myAISDetectedSeq.Clear();
#endif
    myCurDetected = 0;
    myDetectedSeq.Clear();
    myMainVS->Pick(Xpix,Ypix,aview);
    Standard_Boolean had_nothing = myMainVS->NbPicked()==0;
    Standard_Integer NbDetected =  myMainVS->NbPicked();
    Handle(SelectMgr_EntityOwner) EO;

    for(Standard_Integer i_detect = 1;i_detect<=NbDetected;i_detect++){
      EO = myMainVS->Picked(i_detect);
      if(!EO.IsNull()){
	if(myFilters->IsOk(EO)) {
	  myDetectedSeq.Append(i_detect); // normallly they are already arranged in correct order...
#ifdef IMP160701
        Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast(EO->Selectable());
        if(!Handle(AIS_Shape)::DownCast(anObj).IsNull())
          myAISDetectedSeq.Append(anObj);
#endif
      }
    }
    }
    
    //result of  courses..
    if(had_nothing || myDetectedSeq.IsEmpty()){
      if(mylastindex !=0 && mylastindex <= myMapOfOwner.Extent()){

#ifdef BUC60863
	Unhilight(myMapOfOwner(mylastindex),aview);
#else
	if(!IsSelected(myMapOfOwner(mylastindex)))
	  	Unhilight(myMapOfOwner(mylastindex),aview);
#endif
      }
      mylastindex=0;
      return (had_nothing ? AIS_SOD_Nothing : AIS_SOD_AllBad);
    }
    
    // all owners detected by the selector are passed to the 
    // filters and correct ones are preserved...
    myCurDetected = 1;
    EO = myMainVS->Picked(myDetectedSeq(myCurDetected));
    
    static Standard_Boolean Normal_State(Standard_True);
    static Standard_Boolean firsttime(Standard_True);
    if(firsttime){
      OSD_Environment toto("HITRI");
      if(!toto.Value().IsEmpty())
	Normal_State = Standard_False;
      firsttime = Standard_False;
    }
    
    if(Normal_State)
      ManageDetected(EO,aview);
    else
      HilightTriangle(1,aview);
    
    if(myDetectedSeq.Length() == 1){
      if(NbDetected==1)
	return AIS_SOD_OnlyOneDetected;
      else
	return AIS_SOD_OnlyOneGood;
    }
    else 
      return AIS_SOD_SeveralGood;
    
  
  }
  return AIS_SOD_Error;
}

//==================================================
// Function: 
// Purpose :
//==================================================
AIS_StatusOfPick AIS_LocalContext::Select(const Standard_Boolean updateviewer)
{
  if(myAutoHilight)
    UnhilightPicked(Standard_False);
  
  Standard_Integer DI = DetectedIndex();
  AIS_Selection::SetCurrentSelection(mySelName.ToCString());
  Standard_Integer NbSel = AIS_Selection::Extent();

  if(DI<=0){
    ClearSelected(updateviewer);
    return NbSel== 0 ? AIS_SOP_NothingSelected : AIS_SOP_Removed;
  }

  const Handle(SelectMgr_EntityOwner)& EO = myMapOfOwner(DI);

#ifdef BUC60569
  ClearSelected(Standard_False);
  Standard_Integer state = EO->State();
  if( state < 1 ){
    EO->State(1);
    if( state == 0 ) AIS_Selection::Select(EO);
  }
#else
  if(!IsSelected(EO))
    AIS_Selection::ClearAndSelect(EO);
#endif
    
  if(myAutoHilight) {
#ifdef BUC60863
    const Handle(V3d_Viewer)& aViewer = myCTX->CurrentViewer();
    for(aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews())
      Unhilight(EO, aViewer->ActiveView());

    // san - advanced selection highlighting mechanism
    if (!EO->IsAutoHilight() && EO->HasSelectable()){
      Handle(AIS_InteractiveObject) anIO = 
        Handle(AIS_InteractiveObject)::DownCast(EO->Selectable());
      UpdateSelected(anIO, Standard_False);
    }

    if(updateviewer)
        myCTX->CurrentViewer()->Update();
#else
    HilightPicked(updateviewer);
#endif
  }
  return ( AIS_Selection::Extent() == 1)? AIS_SOP_OneSelected : AIS_SOP_SeveralSelected ;
}
//==================================================
// Function: 
// Purpose :
//==================================================
AIS_StatusOfPick AIS_LocalContext::Select(const Standard_Integer XPMin,
					  const Standard_Integer YPMin,
					  const Standard_Integer XPMax,
					  const Standard_Integer YPMax, 
					  const Handle(V3d_View)& aView,
					  const Standard_Boolean updateviewer)
{
  if(aView->Viewer()== myCTX->CurrentViewer()){
    myMainVS->Pick( XPMin,YPMin,XPMax,YPMax,aView);
    if (myAutoHilight) UnhilightPicked(Standard_False);
    
    AIS_Selection::SetCurrentSelection(mySelName.ToCString());
    Standard_Integer LastExt = AIS_Selection::Extent();
    
    myMainVS->Init();
    if(!myMainVS->More()) {
      ClearSelected(updateviewer);
      mylastindex=0;
      return LastExt == 0 ? AIS_SOP_NothingSelected:AIS_SOP_Removed;
    }

    ClearSelected(Standard_False);
   
    for(myMainVS->Init();myMainVS->More();myMainVS->Next()){
      const Handle(SelectMgr_EntityOwner)& OWNR = myMainVS->Picked();
      if(myFilters->IsOk(OWNR)){
	// it can be helpfil to classify this owner immediately...
#ifdef BUC60569
	  Standard_Integer state = OWNR->State();
	  if( state < 1 ){
#ifdef BUC60953
            if( state == 0 ) AIS_Selection::Select(OWNR);
            OWNR->State(1);
#else
            OWNR->State(1);
            if( state == 0 ) AIS_Selection::Select(OWNR);
#endif //BUC60953
	  }
#else //BUC60569
	  if(!IsSelected(OWNR)){
	    OWNR->State(1);
	    AIS_Selection::Select(OWNR);
	  }
#endif //BUC60569
      }
    }
    if (myAutoHilight) 
      HilightPicked(updateviewer);
  }
#ifdef BUC60774
  Standard_Integer NS = AIS_Selection::Extent();
  if( NS == 1 ) return AIS_SOP_OneSelected;
  else if( NS > 1 ) return AIS_SOP_SeveralSelected;
#endif
  return AIS_SOP_Error;
}



//==================================================
// Function: 
// Purpose :
//==================================================
AIS_StatusOfPick AIS_LocalContext::ShiftSelect(const Standard_Boolean updateviewer)
{
  Standard_Integer I = DetectedIndex();
  if(I>0){
#ifndef BUC60863
    if(myAutoHilight)
      UnhilightPicked(Standard_False);
#endif
    
    AIS_Selection::SetCurrentSelection(mySelName.ToCString());
#ifdef BUC60774
    Standard_Integer NbSel = AIS_Selection::Extent();
#endif
    const Handle(SelectMgr_EntityOwner)& EO  = myMapOfOwner(I);
#ifdef BUC60569
    Standard_Integer mod = EO->State()==0 ? -1 : 0;
#else
    Standard_Integer mod = EO->State()==0 ? 1 : 0;
#endif

#ifdef BUC60953
    AIS_Selection::Select(EO);
    EO->State(mod);
#else
    EO->State(mod);

    AIS_Selection::Select(EO);
#endif
    
    if(myAutoHilight) {
#ifdef BUC60863
      const Handle(V3d_Viewer)& aViewer = myCTX->CurrentViewer();
      for(aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews())
        Unhilight(EO, aViewer->ActiveView());

      // san - advanced selection highlighting mechanism
      if (!EO->IsAutoHilight() && EO->HasSelectable()){
        Handle(AIS_InteractiveObject) anIO = 
          Handle(AIS_InteractiveObject)::DownCast(EO->Selectable());
        UpdateSelected(anIO, Standard_False);
      }

      if(updateviewer)
        myCTX->CurrentViewer()->Update();
#else
      HilightPicked(updateviewer);
#endif
    } 
#ifdef BUC60774
    Standard_Integer NS = AIS_Selection::Extent();
    if( NS == 1 ) return AIS_SOP_OneSelected;
    else if( NS > 1 ) return AIS_SOP_SeveralSelected;
    return NbSel== 0 ? AIS_SOP_NothingSelected : AIS_SOP_Removed;
#endif
  }
  return AIS_SOP_Error;
}
//==================================================
// Function: the already selected objects are unselected
// Purpose : others are selected.
//==================================================
AIS_StatusOfPick AIS_LocalContext::ShiftSelect(const Standard_Integer XPMin,
				   const Standard_Integer YPMin,
				   const Standard_Integer XPMax,
				   const Standard_Integer YPMax, 
				   const Handle(V3d_View)& aView,
				   const Standard_Boolean updateviewer)
{
  if(aView->Viewer()== myCTX->CurrentViewer()) {
    myMainVS->Pick( XPMin,YPMin,XPMax,YPMax,aView);
#ifdef BUC60774
    AIS_Selection::SetCurrentSelection(mySelName.ToCString());
    Standard_Integer LastExt = AIS_Selection::Extent();
#endif
    myMainVS->Init();
    if(!myMainVS->More()) 
#ifdef BUC60774
      return LastExt == 0 ? AIS_SOP_NothingSelected:AIS_SOP_Removed;
#else
      return AIS_SOP_NothingSelected; // no effet if click on empty space
#endif

    AIS_Selection::SetCurrentSelection(mySelName.ToCString());
    if (myAutoHilight) UnhilightPicked(Standard_False);
    
    for(myMainVS->Init();myMainVS->More();myMainVS->Next()){
      const Handle(SelectMgr_EntityOwner)& EO = myMainVS->Picked();
      if(myFilters->IsOk(EO)){
#ifdef BUC60569
        Standard_Integer mod = EO->State()==0 ? -1 : 0;
#else
	Standard_Integer mod = EO->State()==0 ? 1 : 0;
#endif

#ifdef BUC60953
	AIS_Selection::Select(EO);
	EO->State(mod);
#else
	EO->State(mod);
	AIS_Selection::Select(EO);
#endif
      }
    }
    if (myAutoHilight) HilightPicked(updateviewer);
    
  }
#ifdef BUC60774
  Standard_Integer NS = AIS_Selection::Extent();
  if( NS == 1 ) return AIS_SOP_OneSelected;
  else if( NS > 1 ) return AIS_SOP_SeveralSelected;
#endif
  return AIS_SOP_Error;
}

//==================================================
// Function: Select
// Purpose : Selection by polyline
//==================================================
AIS_StatusOfPick AIS_LocalContext::Select(const TColgp_Array1OfPnt2d& aPolyline,
					  const Handle(V3d_View)& aView,
					  const Standard_Boolean updateviewer)
{
  if(aView->Viewer()== myCTX->CurrentViewer()){
    myMainVS->Pick(aPolyline,aView);
    if (myAutoHilight) UnhilightPicked(Standard_False);
    
    AIS_Selection::SetCurrentSelection(mySelName.ToCString());
    Standard_Integer LastExt = AIS_Selection::Extent();
    
    myMainVS->Init();
    if(!myMainVS->More()) {
      ClearSelected(updateviewer);
      mylastindex=0;
      return LastExt == 0 ? AIS_SOP_NothingSelected:AIS_SOP_Removed;
    }

    ClearSelected(Standard_False);

    for(myMainVS->Init();myMainVS->More();myMainVS->Next()){
      const Handle(SelectMgr_EntityOwner)& OWNR = myMainVS->Picked();
      if(myFilters->IsOk(OWNR)){
	// it can be helpfil to classify this owner immediately...
#ifdef BUC60953
        Standard_Integer state = OWNR->State();
        if( state < 1 ){
          if( state == 0 ) AIS_Selection::AddSelect(OWNR);
          OWNR->State(1);
        }
#else 
	if(!IsSelected(OWNR)){
	  OWNR->State(1);
	  AIS_Selection::AddSelect(OWNR);
	}
#endif //BUC60953
      }
    }

    if (myAutoHilight) 
      HilightPicked(updateviewer);
  }
#ifdef BUC60774
  Standard_Integer NS = AIS_Selection::Extent();
  if( NS == 1 ) return AIS_SOP_OneSelected;
  else if( NS > 1 ) return AIS_SOP_SeveralSelected;
#endif
  return AIS_SOP_Error;
}

//==================================================
// Function: Select
// Purpose : Selection by polyline
//==================================================
AIS_StatusOfPick AIS_LocalContext::ShiftSelect( const TColgp_Array1OfPnt2d& aPolyline,
                                                const Handle(V3d_View)& aView,
                                                const Standard_Boolean updateviewer )
{
    if( aView->Viewer() == myCTX->CurrentViewer() ) {
        myMainVS->Pick( aPolyline, aView );
        
        AIS_Selection::SetCurrentSelection( mySelName.ToCString() );
        Standard_Integer LastExt = AIS_Selection::Extent();
        myMainVS->Init();
        if( !myMainVS->More() ) 
            return LastExt == 0 ? AIS_SOP_NothingSelected : AIS_SOP_Removed;
        
        AIS_Selection::SetCurrentSelection( mySelName.ToCString() );
        
        if ( myAutoHilight )
            UnhilightPicked( Standard_False );
        for( myMainVS->Init(); myMainVS->More(); myMainVS->Next() ) {
            const Handle(SelectMgr_EntityOwner)& EO = myMainVS->Picked();
            if( myFilters->IsOk( EO ) ) {
                Standard_Integer mod = EO->State() == 0 ? -1 : 0;
                AIS_Selection::Select(EO);
                EO->State( mod );
            }
        }
        if ( myAutoHilight )
            HilightPicked( updateviewer );
    }
    Standard_Integer NS = AIS_Selection::Extent();
    if( NS == 1 ) 
        return AIS_SOP_OneSelected;
    else if( NS > 1 ) 
        return AIS_SOP_SeveralSelected;
    return AIS_SOP_Error;
}

//==================================================
// Function: 
// Purpose :
//==================================================
void AIS_LocalContext::Hilight(const Handle(SelectMgr_EntityOwner)& Ownr,
			       const Handle(V3d_View)& aview)
{
#ifdef BUC60863
  if( aview.IsNull() ) return;
  aview->TransientManagerClearDraw();
#else
  if(aview->TransientManagerBeginDraw())
    Visual3d_TransientManager::EndDraw();
#endif  
  myMainPM->BeginDraw();
  Handle(SelectMgr_SelectableObject) SO = Ownr->Selectable();
  Standard_Integer HM = GetHiMod(*((Handle(AIS_InteractiveObject)*)&SO));
  Ownr->HilightWithColor(myMainPM,myCTX->HilightColor(),HM);
#ifdef IMP051001
  myMainPM->EndDraw(aview,myCTX->ZDetection());
#else
  myMainPM->EndDraw(aview);
#endif

}

//==================================================
// Function: 
// Purpose :
//==================================================
void AIS_LocalContext::Unhilight(const Handle(SelectMgr_EntityOwner)& Ownr,
	    const Handle(V3d_View)& aview)
{

  Handle(SelectMgr_SelectableObject) SO = Ownr->Selectable();
  Standard_Integer HM = GetHiMod(*((Handle(AIS_InteractiveObject)*)&SO));
#ifdef BUC60863
  if( aview.IsNull() ) return;
  if( IsSelected(Ownr) ) {
    if ( Ownr->IsAutoHilight() )
      Ownr->HilightWithColor(myMainPM,myCTX->SelectionColor(),HM);
  }    
  else 
  {
    myMainPM->BeginDraw();
    Ownr->Unhilight(myMainPM,HM);
    myMainPM->EndDraw(aview);
  }
  aview->TransientManagerClearDraw();
#else
  if(aview->TransientManagerBeginDraw())
    Visual3d_TransientManager::EndDraw();
  myMainPM->BeginDraw();
  Ownr->Unhilight(myMainPM,HM);
  myMainPM->EndDraw(aview);
#endif
  
}

//=======================================================================
//function : HilightPicked
//purpose  : 
//=======================================================================
void AIS_LocalContext::HilightPicked(const Standard_Boolean updateviewer)
{
  Standard_Boolean updMain(Standard_False),updColl(Standard_False);

  Handle(AIS_Selection) Sel = AIS_Selection::Selection(mySelName.ToCString());
#ifdef BUC60765
  if( Sel.IsNull() ) return;
#endif

  Handle (PrsMgr_PresentationManager3d) PM = myMainPM;
  SelectMgr_DataMapOfObjectOwners aMap;
  
  // to avoid problems when there is a loop searching for selected objects...
#if !defined OCC189 && !defined USE_MAP
  const TColStd_Array1OfTransient& Obj = Sel->Objects()->Array1();
  for(Standard_Integer i =Obj.Lower();i<=Sel->NbStored();i++)
  {
    const Handle(Standard_Transient)& Tr = Obj(i);
#else
  const AIS_NListTransient& Obj = Sel->Objects();
  AIS_NListTransient::Iterator anIter( Obj );
  for(; anIter.More(); anIter.Next())
  {
    const Handle(Standard_Transient)& Tr = anIter.Value();
#endif
    if(!Tr.IsNull()){
      const Handle(SelectMgr_EntityOwner)& Ownr =
        *((const Handle(SelectMgr_EntityOwner)*) &Tr);
      Handle(AIS_InteractiveObject) IO;
      if(Ownr->HasSelectable()){
	Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(Ownr);
	if(BROwnr.IsNull() || !BROwnr->ComesFromDecomposition()){
	  Handle(SelectMgr_SelectableObject) SO  = Ownr->Selectable();
	  IO = *((Handle(AIS_InteractiveObject)*)&SO);
	  if(myCTX->IsInCollector(IO)){
	    PM = myCTX->CollectorPrsMgr();
	    updColl = Standard_True;}
	  else
	    updMain = Standard_True;
	}
	else
	  updMain = Standard_True;
      }
      else
	updMain = Standard_True;
      Handle(SelectMgr_SelectableObject) SO = Ownr->Selectable();
      Standard_Integer HM = GetHiMod(*((Handle(AIS_InteractiveObject)*)&SO));
      if ( Ownr->IsAutoHilight() )
        Ownr->HilightWithColor(PM,myCTX->SelectionColor(),HM);
      else if ( aMap.IsBound (SO) )
        aMap.ChangeFind(SO).Append ( Ownr );        
      else {
        SelectMgr_SequenceOfOwner aSeq;
        aSeq.Append ( Ownr );
        aMap.Bind ( SO, aSeq );
      }      
    }
  }

  for ( SelectMgr_DataMapIteratorOfMapOfObjectOwners aMapIter(aMap); 
        aMapIter.More(); aMapIter.Next() )
    aMapIter.Key()->HilightSelected ( myMainPM, aMapIter.Value() );

  if(updateviewer){
#ifdef BUC60863
     myCTX->CurrentViewer()->Update();
#else
    if(updMain) myCTX->CurrentViewer()->Update();
#endif
    if(updColl) myCTX->Collector()->Update();
  }
}

//==================================================
// Function: 
// Purpose :
//==================================================
void AIS_LocalContext::
UnhilightPicked(const Standard_Boolean updateviewer)
{
  Standard_Boolean updMain(Standard_False),updColl(Standard_False);

  Handle(AIS_Selection) Sel = AIS_Selection::Selection(mySelName.ToCString());
#ifdef BUC60765
  if( Sel.IsNull() ) return;
#endif
  Handle (PrsMgr_PresentationManager3d) PM = myMainPM;
  SelectMgr_DataMapOfObjectOwners anObjMap;
  SelectMgr_SequenceOfOwner anOwnSeq;
  
#if !defined OCC189 && !defined USE_MAP  
  const TColStd_Array1OfTransient& Obj = Sel->Objects()->Array1();
  for(Standard_Integer i =Obj.Lower();i<=Sel->NbStored();i++){
    const Handle(Standard_Transient)& Tr = Obj(i);
#else
  const AIS_NListTransient& Obj = Sel->Objects();
  AIS_NListTransient::Iterator anIter( Obj );
  for(; anIter.More(); anIter.Next()){
    const Handle(Standard_Transient)& Tr = anIter.Value();
#endif
    if(!Tr.IsNull()){
      const Handle(SelectMgr_EntityOwner)& Ownr =
        *((const Handle(SelectMgr_EntityOwner)*) &Tr);
      Standard_Integer HM(0);
      if(Ownr->HasSelectable()){
#ifdef BUC60876
	Handle(SelectMgr_SelectableObject) SO  = Ownr->Selectable();
	Handle(AIS_InteractiveObject) IO = *((Handle(AIS_InteractiveObject)*)&SO);
  anObjMap.Bind ( IO, anOwnSeq );

  HM = GetHiMod(IO);
#endif
	Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(Ownr);
	if(BROwnr.IsNull() || !BROwnr->ComesFromDecomposition()){
#ifndef BUC60876
	  Handle(SelectMgr_SelectableObject) SO  = Ownr->Selectable();
	  Handle(AIS_InteractiveObject) IO = *((Handle(AIS_InteractiveObject)*)&SO);
	  HM = GetHiMod(IO);
#endif
	  if(myCTX->IsInCollector(IO)){
	    PM = myCTX->CollectorPrsMgr();
	    updColl = Standard_True;}
	  else
	    updMain = Standard_True;
	}
	else
	  updMain = Standard_True;
      }
      Ownr->Unhilight(PM,HM);
    }
  }
  
  for ( SelectMgr_DataMapIteratorOfMapOfObjectOwners anIter1 ( anObjMap ); 
        anIter1.More(); anIter1.Next() )
    if ( !anIter1.Key()->IsAutoHilight() )
      anIter1.Key()->ClearSelected();

  if(updateviewer){
#ifdef BUC60774
    myCTX->CurrentViewer()->Update();
#else
    if(updMain) myCTX->CurrentViewer()->Update();
#endif
    if(updColl) myCTX->Collector()->Update();
  }
  
}

//=======================================================================
//function : IsSelected
//purpose  : 
//=======================================================================
Standard_Boolean AIS_LocalContext::IsSelected(const Handle(AIS_InteractiveObject)& anIObj) const 
{
  return (!FindSelectedOwnerFromIO(anIObj).IsNull());
}

//=======================================================================
//function : IsSelected
//purpose  : 
//=======================================================================

Standard_Boolean AIS_LocalContext::IsSelected(const Handle(SelectMgr_EntityOwner)& Ownr) const 
{
  if (Ownr.IsNull()) return Standard_False;
#ifdef BUC60569
  Standard_Boolean state = (Ownr->State()!=0);
#else
  Standard_Boolean state = (Ownr->State()==1);
#endif
  return state;
}

//==================================================
// Function: 
// Purpose :
//==================================================
void AIS_LocalContext::
InitSelected()
{
  AIS_Selection::SetCurrentSelection(mySelName.ToCString());
  AIS_Selection::CurrentSelection()->Init();
}

//==================================================
// Function: 
// Purpose :
//==================================================
Standard_Boolean AIS_LocalContext::
MoreSelected() const 
{
  return AIS_Selection::CurrentSelection()->More();
}

//==================================================
// Function: 
// Purpose :
//==================================================
void AIS_LocalContext::
NextSelected()
{
  AIS_Selection::CurrentSelection()->Next();
}

//==================================================
// Function: 
// Purpose :
//==================================================
Standard_Boolean AIS_LocalContext::
HasShape() const 
{
  Handle(Standard_Transient) Tr = AIS_Selection::CurrentSelection()->Value();
  if( Tr.IsNull() ) return Standard_False;
  Handle(SelectMgr_EntityOwner) EO = *((Handle(SelectMgr_EntityOwner)*)&Tr);
  Handle(StdSelect_BRepOwner) BRO = Handle(StdSelect_BRepOwner)::DownCast(EO);
  if(BRO.IsNull()) return Standard_False;
  Standard_Boolean hasshape = BRO->HasShape();
  Standard_Boolean comes = BRO->ComesFromDecomposition();
  return (hasshape&&comes);
}

//==================================================
// Function: 
// Purpose :
//==================================================
const TopoDS_Shape& AIS_LocalContext::
SelectedShape() const 
{
  static TopoDS_Shape aSh;
  Handle(Standard_Transient) Tr = AIS_Selection::CurrentSelection()->Value();
  Handle(SelectMgr_EntityOwner) EO = *((Handle(SelectMgr_EntityOwner)*)&Tr);
  Handle(StdSelect_BRepOwner) BRO = Handle(StdSelect_BRepOwner)::DownCast(EO);
  if( BRO.IsNull() ) 
  {
    return aSh;
  }
  return BRO->Shape();
}

//==================================================
// Function: 
// Purpose :
//==================================================
Handle(AIS_InteractiveObject) AIS_LocalContext::
SelectedInteractive() const 
{
  Handle(AIS_InteractiveObject) IO;
  Handle(Standard_Transient) Tr = AIS_Selection::CurrentSelection()->Value();
  if( !Tr.IsNull() ) {
    Handle(SelectMgr_EntityOwner) EO = *((Handle(SelectMgr_EntityOwner)*)&Tr);
    Handle(SelectMgr_SelectableObject) SO;
    if(EO->HasSelectable()){
      SO = EO->Selectable();
      IO = *((Handle(AIS_InteractiveObject)*)&SO);
    }
  }
  return IO;
}
//==================================================
// Function: 
// Purpose :
//==================================================
Handle(SelectMgr_EntityOwner) AIS_LocalContext::
SelectedOwner() const 
{
  Handle(SelectMgr_EntityOwner) EO;
  Handle(Standard_Transient) Tr = AIS_Selection::CurrentSelection()->Value();
  if( !Tr.IsNull() )
  	EO = *((Handle(SelectMgr_EntityOwner)*)&Tr);
  return EO;
}

//==================================================
// Function: 
// Purpose :
//==================================================
Standard_Boolean AIS_LocalContext::
HasApplicative() const 
{
  Handle(AIS_InteractiveObject) IO = SelectedInteractive();
  if( IO.IsNull() ) return Standard_False;
  return IO->HasOwner();
}

//==================================================
// Function: 
// Purpose :
//==================================================
const Handle(Standard_Transient)& AIS_LocalContext::
SelectedApplicative() const 
{
  return SelectedInteractive()->GetOwner();
}



//=======================================================================
//function : UpdateSelection
//purpose  : should disappear...
//=======================================================================
void AIS_LocalContext::UpdateSelected(const Standard_Boolean updateviewer)
{
  UnhilightPicked(Standard_False);
  HilightPicked(updateviewer);
}

//================================================================
// Function : UpdateSelected
// Purpose  : Part of advanced selection mechanism.
//            Highlightes or clears selection presentation for the given IO
//================================================================
void AIS_LocalContext::UpdateSelected(const Handle(AIS_InteractiveObject)& anobj,
                                      const Standard_Boolean updateviewer)
{
  if (anobj.IsNull() || anobj->IsAutoHilight())
    return;

  AIS_Selection::SetCurrentSelection(mySelName.ToCString());
  Handle(AIS_Selection) Sel = AIS_Selection::CurrentSelection();

  SelectMgr_SequenceOfOwner aSeq;
  for ( Sel->Init(); Sel->More(); Sel->Next() ){
    Handle(SelectMgr_EntityOwner) aOwner = Handle(SelectMgr_EntityOwner)::DownCast(Sel->Value());

    if ( !aOwner.IsNull() && aOwner->HasSelectable() && aOwner->Selectable() == anobj )
      aSeq.Append( aOwner );
  }

  if ( aSeq.Length() )
    anobj->HilightSelected( myMainPM, aSeq );
  else
    anobj->ClearSelected();

  if(updateviewer){
     myCTX->CurrentViewer()->Update();
  }
}

//==================================================
// Function: ClearSelected
// Purpose :
//==================================================
void AIS_LocalContext::ClearSelected(const Standard_Boolean updateviewer)
{
  UnhilightPicked(updateviewer);
  AIS_Selection::SetCurrentSelection(mySelName.ToCString());

  Handle(AIS_Selection) Sel = AIS_Selection::CurrentSelection();
#if !defined OCC189 && !defined USE_MAP   
  const TColStd_Array1OfTransient& Obj = Sel->Objects()->Array1();
  for(Standard_Integer i =Obj.Lower();i<=Sel->NbStored();i++){
    const Handle(Standard_Transient)& Tr = Obj(i);
#else
  const AIS_NListTransient& Obj = Sel->Objects();
  AIS_NListTransient::Iterator anIter( Obj );
  for(; anIter.More(); anIter.Next()){
    const Handle(Standard_Transient)& Tr = anIter.Value();
#endif
    if(!Tr.IsNull()){
      (*((const Handle(SelectMgr_EntityOwner)*)&Tr))->State(0);
    }
  }
  AIS_Selection::Select();
  mylastindex = 0;
}


//=======================================================================
//function : SetSelected
//purpose  : 
//=======================================================================
void AIS_LocalContext::SetSelected(const Handle(AIS_InteractiveObject)& anIObj,
				   const Standard_Boolean updateviewer)
{
  if(!IsValidForSelection(anIObj)) return;
  UnhilightPicked(Standard_False);
  
  //1st case, owner already <anIObj> as owner  
  // and not separated is found...

  Handle(AIS_Selection) sel = AIS_Selection::Selection(mySelName.ToCString());
  //Standard_Boolean found(Standard_False);
  Handle(Standard_Transient) Tr;
  Handle(SelectMgr_EntityOwner) EO = FindSelectedOwnerFromIO(anIObj);
  if(EO.IsNull()){
    //check if in selection number 0 there is an owner that can be triturated...
    if(anIObj->HasSelection(0)){
      const Handle(SelectMgr_Selection)& SIOBJ = anIObj->Selection(0);
      SIOBJ->Init();
      if(SIOBJ->More()){
	Handle(SelectBasics_EntityOwner) BO = SIOBJ->Sensitive()->OwnerId();
	EO = *((Handle(SelectMgr_EntityOwner)*)&BO);
      }
    }
    if(EO.IsNull()) 
      EO = new SelectMgr_EntityOwner(anIObj);
  }
  
  ClearSelected(Standard_False);
#ifdef OCC138 
  AIS_Selection::Select(EO);
  EO->State(1);
#else
  EO->State(1);
  AIS_Selection::Select(EO);
#endif
  HilightPicked(updateviewer);
}

//=======================================================================
//function : AddOrRemoveSelected
//purpose  : 
//=======================================================================

void AIS_LocalContext::AddOrRemoveSelected(const Handle(AIS_InteractiveObject)& anIObj,
					   const Standard_Boolean updateviewer)
{
  if(!IsValidForSelection(anIObj)) return;
  UnhilightPicked(Standard_False);
  // first check if it is selected...
  Handle(SelectMgr_EntityOwner) EO;

  EO = FindSelectedOwnerFromIO(anIObj);
#ifndef OCC138
  if(!EO.IsNull())
    EO->State(0);
  else{
#else
  if(EO.IsNull()) {
#endif
    if(anIObj->HasSelection(0)){
      const Handle(SelectMgr_Selection)& SIOBJ = anIObj->Selection(0);
      SIOBJ->Init();
      if(SIOBJ->More()){
	Handle(SelectBasics_EntityOwner) BO = SIOBJ->Sensitive()->OwnerId();
	EO = *((Handle(SelectMgr_EntityOwner)*)&BO);
      }
    }
    if(EO.IsNull()) 
      EO = new SelectMgr_EntityOwner(anIObj);
#ifndef OCC138
    EO->State(1);
#endif
  }
  
//  cout<<"AIS_LocalContext::AddOrRemoveSelected : Selection = "<<mySelName<<endl;
  const Handle(AIS_Selection)& S = AIS_Selection::Selection(mySelName.ToCString());
#ifdef OCC138
  if(!S.IsNull()) {
    AIS_SelectStatus aStatus = S->Select(EO);
    if(aStatus == AIS_SS_Added)
      EO->State(1);
    else
      EO->State(0);
  }
#else
  if(!S.IsNull())
    S->Select(EO);
#endif
  HilightPicked(updateviewer);
}

//=======================================================================
//function : AddOrRemoveSelected
//purpose  : To check...
//=======================================================================
void AIS_LocalContext::AddOrRemoveSelected(const TopoDS_Shape& Sh,
					   const Standard_Boolean updateviewer)
{     
  UnhilightPicked(Standard_False);
  Handle(SelectMgr_EntityOwner) EO = FindSelectedOwnerFromShape(Sh);
  if(!EO.IsNull()){
//    cout<<"AIS_LocalContext::AddOrRemoveSelected(sh) : Selection = "<<mySelName<<endl;
    
#ifdef OCC138
    AIS_Selection::Selection(mySelName.ToCString())->Select(EO);
    EO->State(1);
#else
    EO->State(1);
    AIS_Selection::Selection(mySelName.ToCString())->Select(EO);
#endif
  }
  HilightPicked(updateviewer);
}

void AIS_LocalContext::AddOrRemoveSelected(const Handle(SelectMgr_EntityOwner)& Ownr,
					   const Standard_Boolean updateviewer)
{     
  //Not Yet Implemented
  if(myAutoHilight)
  UnhilightPicked(Standard_False);
//  cout<<"AIS_LocalContext::AddOrRemoveSelected(ownr) : Selection = "<<mySelName<<endl;

  Standard_Integer mod = Ownr->State()==0 ? 1 : 0;
#ifdef OCC138
  AIS_Selection::Selection(mySelName.ToCString())->Select(Ownr);

  Ownr->State(mod);  
#else
  Ownr->State(mod);
  
  AIS_Selection::Selection(mySelName.ToCString())->Select(Ownr);
#endif
  if(myAutoHilight)
  HilightPicked(updateviewer);
}

//==================================================
// Function: 
// Purpose :
//==================================================
void AIS_LocalContext::ManageDetected(const Handle(SelectMgr_EntityOwner)& aPickOwner, 
				      const Handle(V3d_View)& aview)
{
#ifdef BUC60818
  // Warning : aPickOwner may be null !
  if (aPickOwner.IsNull()) return;
#else
  if(!myAutoHilight) return;
#endif
//  const Handle(SelectMgr_SelectableObject)& SO = aPickOwner->Selectable();
  Standard_Boolean okStatus = myFilters->IsOk(aPickOwner);
  // OK...
  if(okStatus){
    //=======================================================================================================
    // 2 cases : a- object is in the map of picks:
    //             1. this is the same index as the last detected: -> Do nothing
    //             2. otherwise :
    //                  - if lastindex = 0 (no object was detected at the last step)
    //                    the object presentation is highlighted and lastindex = index(objet)
    //                  - othrwise : 
    //                           the presentation of the object corresponding to lastindex is "unhighlighted" 
    //                           it is removed if the object is not visualized but only active
    //                           then the presentation of the detected object is highlighted and lastindex = index(objet)
    //         b- the object is not in the map of picked objects
    //                  - if lastindex != 0 (object detected at the last step) it is unhighlighted ...
    //            if the object was decomposed, presentation is created for the detected shape and the couple
    //             (Proprietaire,Prs)is added in the map.
    //           otherwise the couple(proprietaire, NullPrs) is placed in the map and the interactive object 
    //           itself is highlighted.
    //                              
    //=======================================================================================================

    
    //szv:
    Standard_Boolean wasContained = myMapOfOwner.Contains(aPickOwner);
    Standard_Integer theNewIndex = 0;
    if (wasContained)
      theNewIndex = myMapOfOwner.FindIndex(aPickOwner);
    else
      theNewIndex = myMapOfOwner.Add(aPickOwner);

    // For the advanced mesh selection mode the owner indices comparison
    // is not effective because in that case only one owner manage the
    // selection in current selection mode. It is necessary to check the current detected
    // entity and hilight it only if the detected entity is not the same as 
    // previous detected (IsForcedHilight call)
    if (theNewIndex != mylastindex || aPickOwner->IsForcedHilight()) {

      if (mylastindex && mylastindex <= myMapOfOwner.Extent()) {

        const Handle(SelectMgr_EntityOwner)& LastOwnr = myMapOfOwner(mylastindex);
#ifdef BUC60863
        Unhilight(LastOwnr,aview);
#else
        if(!IsSelected(LastOwnr))
          Unhilight(LastOwnr,aview);
#endif
      }

      if (myAutoHilight) {
        // wasContained should not be checked because with this verification different
        // behaviour of application may occer depending whether mouse is moved above 
        // owner first or second time
        //if (wasContained) {
#ifdef BUC60569
          if (aPickOwner->State() <= 0 || myCTX->ToHilightSelected())
#else
          if(!IsSelected (aPickOwner) || myCTX->ToHilightSelected())
#endif
            Hilight(aPickOwner,aview);
        /*}
        else Hilight(aPickOwner,aview);*/
      }

      mylastindex = theNewIndex;
    }
  }

  if (mylastindex) mylastgood = mylastindex;
  
}


//=======================================================================
//function : HasDetectedShape
//purpose  : 
//=======================================================================

Standard_Boolean AIS_LocalContext::HasDetectedShape() const 
{
  if(mylastindex==0) return Standard_False;
  return IsShape(mylastindex);
}

//=======================================================================
//function : DetectedShape
//purpose  : 
//=======================================================================

const TopoDS_Shape&
AIS_LocalContext::DetectedShape() const
{
  static TopoDS_Shape bidsh;
  if(mylastindex != 0)
  {
    Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(myMapOfOwner(mylastindex));
    if(BROwnr.IsNull()) return bidsh;
    return BROwnr->Shape();
  }
  return bidsh;
}					    

//=======================================================================
//function : DetectedInteractive
//purpose  : 
//=======================================================================

Handle(AIS_InteractiveObject) 
AIS_LocalContext::DetectedInteractive() const 
{
  Handle(AIS_InteractiveObject) Iobj;
  if(IsValidIndex(mylastindex)){
    Handle(SelectMgr_SelectableObject) SO = myMapOfOwner.FindKey(mylastindex)->Selectable();
    Iobj = *((Handle(AIS_InteractiveObject)*) &SO);
  }
  return Iobj;
}
//=======================================================================
//function : DetectedInteractive
//purpose  : 
//=======================================================================
Handle(SelectMgr_EntityOwner) AIS_LocalContext::DetectedOwner() const 
{
  Handle(SelectMgr_EntityOwner) bid;
  if(!IsValidIndex(mylastindex)) return bid;
  return myMapOfOwner.FindKey(mylastindex);
}


//=======================================================================
//function : ComesFromDecomposition
//purpose  : 
//=======================================================================

Standard_Boolean AIS_LocalContext::ComesFromDecomposition(const Standard_Integer PickedIndex) const 
{
  const Handle(SelectMgr_EntityOwner)& OWN = myMapOfOwner.FindKey(PickedIndex);
  Handle(SelectMgr_SelectableObject) aSel  = OWN->Selectable();
  if (myActiveObjects.IsBound (aSel)) { // debug of jmi
    const Handle(AIS_LocalStatus)& Stat      = myActiveObjects(aSel);    
    return Stat->Decomposed();
  }
  return Standard_False;
}


//=======================================================================
//function : DisplayAreas
//purpose  : 
//=======================================================================

void AIS_LocalContext::DisplayAreas(const Handle(V3d_View)& aviou)
{
    myMainVS->DisplayAreas(aviou);
}

//=======================================================================
//function : ClearAreas
//purpose  : 
//=======================================================================

void AIS_LocalContext::ClearAreas(const Handle(V3d_View)& aviou)
{
    myMainVS->ClearAreas(aviou);
}

//=======================================================================
//function : DisplaySensitive
//purpose  : 
//=======================================================================

void AIS_LocalContext::DisplaySensitive(const Handle(V3d_View)& aviou)
{
    myMainVS->DisplaySensitive(aviou);
}

//=======================================================================
//function : ClearSensitive
//purpose  : 
//=======================================================================

void AIS_LocalContext::ClearSensitive(const Handle(V3d_View)& aviou)
{
    myMainVS->ClearSensitive(aviou);
}


//=======================================================================
//function : IsShape
//purpose  : 
//=======================================================================
Standard_Boolean AIS_LocalContext::IsShape(const Standard_Integer Index) const
{
  
  if(Handle(StdSelect_BRepOwner)::DownCast(myMapOfOwner.FindKey(Index)).IsNull())
    return Standard_False;
  return 
    ComesFromDecomposition(Index);
}

Standard_Boolean AIS_LocalContext::IsValidForSelection(const Handle(AIS_InteractiveObject)& anIObj) const 
{

#ifdef IMP120701
  // Shape was not transfered from AIS_Shape to EntityOwner
  Handle(AIS_Shape) shape = Handle(AIS_Shape)::DownCast(anIObj);
  if( !shape.IsNull() ) 
    return myFilters->IsOk(new StdSelect_BRepOwner(shape->Shape(),shape));
#endif
  return myFilters->IsOk(new SelectMgr_EntityOwner(anIObj));
}


//=======================================================================
//function : HilightNextDetected
//purpose  : 
//=======================================================================

Standard_Integer AIS_LocalContext::HilightNextDetected(const Handle(V3d_View)& V)
{
  // go to the next owner

  if(myDetectedSeq.IsEmpty()) return Standard_False;
  Standard_Integer L = myDetectedSeq.Length();
  myCurDetected++;

  if(myCurDetected>L)
    myCurDetected = 1;
  Handle(SelectMgr_EntityOwner) EO = myMainVS->Picked(myCurDetected);
#ifdef IMP120402
  if( EO.IsNull() ) return 0;
#endif

  static Standard_Boolean Normal_State(Standard_True);
  static Standard_Boolean firsttime(Standard_True);
  if(firsttime){
    OSD_Environment toto("HITRI");
    if(!toto.Value().IsEmpty())
      Normal_State = Standard_False;
    firsttime = Standard_False;
  }


  if(Normal_State)
    ManageDetected(EO,V);
  else
    HilightTriangle(myCurDetected,V);
  return myCurDetected;
}

//=======================================================================
//function : HilightPreviousDetected
//purpose  : 
//=======================================================================
Standard_Integer AIS_LocalContext::HilightPreviousDetected(const Handle(V3d_View)& V)
{
  if(myDetectedSeq.IsEmpty()) return Standard_False;

  myCurDetected--;

  if(myCurDetected<1)
    myCurDetected = 1;
  Handle(SelectMgr_EntityOwner) EO = myMainVS->Picked(myCurDetected);
#ifdef IMP120402
  if( EO.IsNull() ) return 0;
#endif

  static Standard_Boolean Normal_State(Standard_True);
  static Standard_Boolean firsttime(Standard_True);
  if(firsttime){
    OSD_Environment toto("HITRI");
    if(!toto.Value().IsEmpty())
      Normal_State = Standard_False;
    firsttime = Standard_False;
  }



  if(Normal_State)
    ManageDetected(EO,V);
  else
    HilightTriangle(myCurDetected,V);
  return myCurDetected;
}

//=======================================================================
//function : UnhilightLastDetected
//purpose  : 
//=======================================================================
Standard_Boolean AIS_LocalContext::UnhilightLastDetected(const Handle(V3d_View)& aview)
{
  if(!IsValidIndex(mylastindex)) return Standard_False;
  myMainPM->BeginDraw();
  const Handle(SelectMgr_EntityOwner)& Ownr = myMapOfOwner(mylastindex);
  Standard_Integer HM(0);
  if(Ownr->HasSelectable()){
    Handle(SelectMgr_SelectableObject) SO = Ownr->Selectable();
    HM = GetHiMod(*((Handle(AIS_InteractiveObject)*)&SO));
  }
  myMapOfOwner(mylastindex)->Unhilight(myMainPM,HM);
  myMainPM->EndDraw(aview);
  mylastindex =0;
  return Standard_True;
}



//=======================================================================
//function : HilightTriangle
//purpose  : 
//=======================================================================

void AIS_LocalContext::HilightTriangle(const Standard_Integer Rank,
				       const Handle(V3d_View)& view)
{
  static Standard_Integer PrevRank(0);
  if(Rank==PrevRank) return;
//  PrevRank = Rank; 
  Handle(SelectBasics_SensitiveEntity) SE = myMainVS->Primitive(Rank);
  if(SE->IsKind(STANDARD_TYPE(Select3D_SensitiveTriangulation))){
    Handle(Select3D_SensitiveTriangulation) Tr = *((Handle(Select3D_SensitiveTriangulation)*)&SE);
    gp_Pnt p1,p2,p3 ; Tr->DetectedTriangle(p1,p2,p3);
    static Graphic3d_Array1OfVertex Vtt(1,3);

    Vtt.SetValue(1,Graphic3d_Vertex(p1.X(),p1.Y(),p1.Z()));
    Vtt.SetValue(2,Graphic3d_Vertex(p2.X(),p2.Y(),p2.Z()));
    Vtt.SetValue(3,Graphic3d_Vertex(p3.X(),p3.Y(),p3.Z()));
    static Handle(Prs3d_Presentation)  TriPrs = 
      new Prs3d_Presentation(myMainPM->StructureManager());
    TriPrs->Clear();
#ifdef IMP300101
    Handle(Prs3d_ShadingAspect) asp = myCTX->DefaultDrawer()->ShadingAspect();
    asp->SetColor(myCTX->HilightColor());
    TriPrs->SetShadingAspect(asp);
#endif
    Prs3d_Root::CurrentGroup(TriPrs)->Polygon(Vtt);

#ifndef IMP300101
    if(view->TransientManagerBeginDraw())
      Visual3d_TransientManager::EndDraw();
#endif
    if(view->TransientManagerBeginDraw()) {
      //P->Exploration();
      Visual3d_TransientManager::DrawStructure(TriPrs);
      Visual3d_TransientManager::EndDraw();
    }
    
  }
  
}

//=======================================================================
//function : FindSelectedOwnerFromIO
//purpose  : it is checked if one of the selected owners really presents IObj
//=======================================================================
Handle(SelectMgr_EntityOwner) AIS_LocalContext::FindSelectedOwnerFromIO
                          (const Handle(AIS_InteractiveObject)& anIObj) const 
{
  Handle(SelectMgr_EntityOwner) EO,bid;
  if (anIObj.IsNull()) return EO;
  
  Handle(AIS_Selection) Sel = AIS_Selection::Selection(mySelName.ToCString());
  if(Sel.IsNull()) {
#ifdef DEB
    cout<<"\t\tAIS_LocalCOntext::FindSelectedOwnerFromShape : Selection "
        <<mySelName<<" Nulle "<<endl;
#endif
    return EO;
  }
  Standard_Boolean found(Standard_False);
#if !defined OCC189 && !defined USE_MAP     
  const TColStd_Array1OfTransient& Obj = Sel->Objects()->Array1();
  for(Standard_Integer i =Obj.Lower();i<=Sel->NbStored();i++){
    const Handle(Standard_Transient)& Tr = Obj(i);
#else
  const AIS_NListTransient& Obj = Sel->Objects();
  AIS_NListTransient::Iterator anIter( Obj );
  for(; anIter.More(); anIter.Next()){
    const Handle(Standard_Transient)& Tr = anIter.Value();
#endif
    if(!Tr.IsNull()){
      EO = *((Handle(SelectMgr_EntityOwner)*)&Tr);
      if(EO->HasSelectable()){
	Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(EO);
	if(BROwnr.IsNull() || !BROwnr->ComesFromDecomposition()){
	  if (anIObj == EO->Selectable()){
	    found =Standard_True;
	    break;
	  }
	}
      }
    }
  }
  if(found)  return EO;
  return bid;
}

//=======================================================================
//function : FindSelectedOwnerFromShape
//purpose  : it is checked if one of the selected owners really presents IObj
//=======================================================================
Handle(SelectMgr_EntityOwner) AIS_LocalContext::FindSelectedOwnerFromShape(const TopoDS_Shape& sh) const 
{
#ifdef OCC9026
  Handle(SelectMgr_EntityOwner) EO, bid;
#else
  Handle(SelectMgr_EntityOwner) EO;
#endif
  if (sh.IsNull()) return EO;
  
  Handle(AIS_Selection) Sel = AIS_Selection::Selection(mySelName.ToCString());
  if(Sel.IsNull()) {
#ifdef DEB
    cout<<"\t\tAIS_LocalCOntext::FindSelectedOwnerFromShape : Selection "<<mySelName<<" Nulle "<<endl;
#endif
    return EO;
  }
  
  Standard_Boolean found(Standard_False);

#ifdef OCC9026
  if (!found) {
    //now iterate over all shapes loaded into the context (but inside the collector)
    SelectMgr_DataMapIteratorOfDataMapOfIntegerSensitive aSensitiveIt (myMainVS->Primitives());
    for (; aSensitiveIt.More(); aSensitiveIt.Next()) {
      EO = Handle(SelectMgr_EntityOwner)::DownCast (aSensitiveIt.Value()->OwnerId());
      Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(EO);
      if (!BROwnr.IsNull() && BROwnr->HasShape() && BROwnr->Shape() == sh) {
	 found = Standard_True;
	 break;
      }
    }
  }
#else
#if !defined OCC189 && !defined USE_MAP   
  const TColStd_Array1OfTransient& Obj = Sel->Objects()->Array1();
  for(Standard_Integer i =Obj.Lower();i<=Sel->NbStored();i++){
    const Handle(Standard_Transient)& Tr = Obj(i);
#else
  const AIS_NListTransient& Obj = Sel->Objects();
  AIS_NListTransient::Iterator anIter( Obj );
  for(; anIter.More(); anIter.Next()){
    const Handle(Standard_Transient)& Tr = anIter.Value();
#endif
    if(!Tr.IsNull()){
      
      EO = *((Handle(SelectMgr_EntityOwner)*)&Tr);
      if(EO->HasShape())
	if ( EO->Shape() == sh)
	  found =Standard_True;
          break;
    }
  }
#endif

  if(found)  return EO;
  return bid;
}

#ifdef IMP160701
//=======================================================================
//function : AIS_LocalContext::InitDetected
//purpose  :
//=======================================================================

void AIS_LocalContext::InitDetected()
{
  myAISCurDetected = myAISDetectedSeq.Length()? 1 : 0;
}

//=======================================================================
//function : AIS_LocalContext::MoreDetected
//purpose  :
//=======================================================================

Standard_Boolean AIS_LocalContext::MoreDetected() const
{
  return (myAISCurDetected > 0 && myAISCurDetected <= myAISDetectedSeq.Length());
}


//=======================================================================
//function : AIS_LocalContext::NextDetected
//purpose  :
//=======================================================================

void AIS_LocalContext::NextDetected()
{
  if (MoreDetected()) myAISCurDetected++;
}

//=======================================================================
//function : DetectedCurrentShape
//purpose  :
//=======================================================================

const TopoDS_Shape& AIS_LocalContext::DetectedCurrentShape() const
{
  static TopoDS_Shape bidsh;
  if (MoreDetected())
    return Handle(AIS_Shape)::DownCast(myAISDetectedSeq(myAISCurDetected))->Shape();
  return bidsh;
}

//=======================================================================
//function : DetectedCurrentObject
//purpose  :
//=======================================================================

Handle(AIS_InteractiveObject) AIS_LocalContext::DetectedCurrentObject() const
{
  Handle(AIS_InteractiveObject) theIObj;
  if (MoreDetected())
    theIObj = myAISDetectedSeq(myAISCurDetected);

  return theIObj;
}
#endif