894dfe250e71dc8347c03e45ef550ae3e2655343
[occt.git] / src / AIS / AIS_InteractiveContext_1.cxx
1 // Created on: 1997-01-29
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <AIS_DataMapIteratorOfDataMapOfIOStatus.hxx>
19 #include <AIS_GlobalStatus.hxx>
20 #include <AIS_InteractiveContext.hxx>
21 #include <AIS_InteractiveObject.hxx>
22 #include <AIS_LocalContext.hxx>
23 #include <AIS_MapIteratorOfMapOfInteractive.hxx>
24 #include <AIS_MapOfInteractive.hxx>
25 #include <AIS_Selection.hxx>
26 #include <AIS_Shape.hxx>
27 #include <AIS_StatusOfDetection.hxx>
28 #include <AIS_StatusOfPick.hxx>
29 #include <Aspect_Grid.hxx>
30 #include <Prs3d_BasicAspect.hxx>
31 #include <Prs3d_LineAspect.hxx>
32 #include <Prs3d_Presentation.hxx>
33 #include <Quantity_Color.hxx>
34 #include <SelectBasics_SensitiveEntity.hxx>
35 #include <SelectMgr_EntityOwner.hxx>
36 #include <SelectMgr_Filter.hxx>
37 #include <SelectMgr_OrFilter.hxx>
38 #include <SelectMgr_Selection.hxx>
39 #include <SelectMgr_SelectionManager.hxx>
40 #include <Standard_Transient.hxx>
41 #include <StdSelect_BRepOwner.hxx>
42 #include <StdSelect_ViewerSelector3d.hxx>
43 #include <TCollection_AsciiString.hxx>
44 #include <TCollection_ExtendedString.hxx>
45 #include <TColStd_ListIteratorOfListOfInteger.hxx>
46 #include <TopLoc_Location.hxx>
47 #include <TopoDS_Shape.hxx>
48 #include <V3d_AmbientLight.hxx>
49 #include <V3d_DirectionalLight.hxx>
50 #include <V3d_Light.hxx>
51 #include <V3d_PositionalLight.hxx>
52 #include <V3d_SpotLight.hxx>
53 #include <V3d_View.hxx>
54 #include <V3d_Viewer.hxx>
55
56 typedef NCollection_DataMap<Handle(AIS_InteractiveObject), NCollection_Handle<SelectMgr_SequenceOfOwner> > AIS_MapOfObjSelectedOwners;
57
58 //=======================================================================
59 //function : highlightWithColor
60 //purpose  :
61 //=======================================================================
62 void AIS_InteractiveContext::highlightWithColor (const Handle(SelectMgr_EntityOwner)& theOwner,
63                                                  const Quantity_NameOfColor theColor,
64                                                  const Handle(V3d_Viewer)& theViewer)
65 {
66   const Handle(AIS_InteractiveObject) anObj =
67     Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
68   if (anObj.IsNull())
69     return;
70   const Standard_Integer aHiMode = anObj->HasHilightMode() ? anObj->HilightMode() : 0;
71
72   myMainPM->BeginImmediateDraw();
73   theOwner->HilightWithColor (myMainPM, theColor, aHiMode);
74   myMainPM->EndImmediateDraw (theViewer.IsNull() ? myMainVwr : theViewer);
75 }
76
77 //=======================================================================
78 //function : highlightSelected
79 //purpose  :
80 //=======================================================================
81 void AIS_InteractiveContext::highlightSelected (const Handle(SelectMgr_EntityOwner)& theOwner,
82                                                 const Quantity_NameOfColor theSelColor)
83 {
84   const Handle(AIS_InteractiveObject) anObj =
85     Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
86   if (anObj.IsNull())
87     return;
88   const Standard_Integer aHiMode = anObj->HasHilightMode() ? anObj->HilightMode() : 0;
89
90   if (!theOwner->IsAutoHilight())
91   {
92     AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
93     const Handle(AIS_Selection)& aCurSel = AIS_Selection::CurrentSelection();
94     SelectMgr_SequenceOfOwner aSeq;
95     for (aCurSel->Init(); aCurSel->More(); aCurSel->Next())
96     {
97       const Handle(SelectMgr_EntityOwner) aSelOwnr =
98         Handle(SelectMgr_EntityOwner)::DownCast (aCurSel->Value());
99       if (aSelOwnr->Selectable() != anObj)
100         continue;
101       aSeq.Append (aSelOwnr);
102     }
103     anObj->HilightSelected (myMainPM, aSeq);
104   }
105   else
106   {
107     theOwner->HilightWithColor (myMainPM, theSelColor, aHiMode);
108   }
109 }
110
111 //=======================================================================
112 //function : unhighlightSelected
113 //purpose  :
114 //=======================================================================
115 void AIS_InteractiveContext::unhighlightSelected (const Standard_Boolean theIsToHilightSubIntensity)
116 {
117   AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
118   Handle(AIS_Selection) aCurSel = AIS_Selection::Selection (myCurrentName.ToCString());
119   NCollection_IndexedMap<Handle(AIS_InteractiveObject)> anObjToClear;
120   for (aCurSel->Init(); aCurSel->More(); aCurSel->Next())
121   {
122     const Handle(SelectMgr_EntityOwner) anOwner =
123       Handle(SelectMgr_EntityOwner)::DownCast (aCurSel->Value());
124     if (anOwner.IsNull() || !anOwner->HasSelectable())
125       continue;
126
127     const Handle(AIS_InteractiveObject) anInteractive =
128       Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
129     if (anOwner->IsAutoHilight())
130     {
131       const Standard_Integer aHiMode = anInteractive->HasHilightMode() ? anInteractive->HilightMode() : 0;
132       anOwner->Unhilight (myMainPM, aHiMode);
133       if (theIsToHilightSubIntensity)
134       {
135         if (myObjects.IsBound (anInteractive) && myObjects (anInteractive)->IsSubIntensityOn())
136         {
137           anOwner->HilightWithColor (myMainPM, mySubIntensity, aHiMode);
138         }
139       }
140     }
141     else
142     {
143       if (!anObjToClear.Contains (anInteractive))
144         anObjToClear.Add (anInteractive);
145     }
146     anOwner->State (0);
147     if (anOwner == anInteractive->GlobalSelOwner())
148     {
149       myObjects.ChangeFind (anInteractive)->SetHilightStatus (Standard_False);
150     }
151   }
152   for (NCollection_IndexedMap<Handle(AIS_InteractiveObject)>::Iterator anIter (anObjToClear); anIter.More(); anIter.Next())
153   {
154     const Handle(AIS_InteractiveObject)& anObj = anIter.Value();
155     const Standard_Integer aHiMode = anObj->HasHilightMode() ? anObj->HilightMode() : 0;
156     myMainPM->Unhighlight (anObj, aHiMode);
157     anObj->ClearSelected();
158   }
159 }
160
161 //=======================================================================
162 //function : MoveTo
163 //purpose  :
164 //=======================================================================
165 AIS_StatusOfDetection AIS_InteractiveContext::MoveTo (const Standard_Integer  theXPix,
166                                                       const Standard_Integer  theYPix,
167                                                       const Handle(V3d_View)& theView,
168                                                       const Standard_Boolean  theToRedrawOnUpdate)
169 {
170   if (HasOpenedContext())
171   {
172     myWasLastMain = Standard_True;
173     return myLocalContexts (myCurLocalIndex)->MoveTo (theXPix, theYPix, theView, theToRedrawOnUpdate);
174   }
175
176   myAISCurDetected = 0;
177   myAISDetectedSeq.Clear();
178
179   if (theView->Viewer() != myMainVwr)
180   {
181     return AIS_SOD_Error;
182   }
183
184   // preliminaires
185   myLastPicked  = myLastinMain;
186   myWasLastMain = Standard_True;
187   AIS_StatusOfDetection aStatus        = AIS_SOD_Nothing;
188   Standard_Boolean      toUpdateViewer = Standard_False;
189
190   myFilters->SetDisabledObjects (theView->View()->HiddenObjects());
191   myMainSel->Pick (theXPix, theYPix, theView);
192
193   // filling of myAISDetectedSeq sequence storing information about detected AIS objects
194   // (the objects must be AIS_Shapes)
195   const Standard_Integer aDetectedNb = myMainSel->NbPicked();
196   Standard_Integer aNewDetected = 0;
197   for (Standard_Integer aDetIter = 1; aDetIter <= aDetectedNb; ++aDetIter)
198   {
199     Handle(SelectMgr_EntityOwner) anOwner = myMainSel->Picked (aDetIter);
200     if (anOwner.IsNull()
201      || !myFilters->IsOk (anOwner))
202     {
203       continue;
204     }
205
206     if (aNewDetected < 1)
207     {
208       aNewDetected = aDetIter;
209     }
210     Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
211     if (!anObj.IsNull())
212     {
213       myAISDetectedSeq.Append (anObj);
214     }
215   }
216
217   if (aNewDetected >= 1)
218   {
219     // does nothing if previously detected object is equal to the current one
220     Handle(SelectMgr_EntityOwner) aNewPickedOwner = myMainSel->Picked (aNewDetected);
221     if (aNewPickedOwner == myLastPicked)
222     {
223       return myLastPicked->IsSelected()
224            ? AIS_SOD_Selected
225            : AIS_SOD_OnlyOneDetected;
226     }
227  
228     // Previously detected object is unhilighted if it is not selected or hilighted 
229     // with selection color if it is selected. Such highlighting with selection color 
230     // is needed only if myToHilightSelected flag is true. In this case previously detected
231     // object has been already highlighted with myHilightColor during previous MoveTo() 
232     // method call. As result it is necessary to rehighligt it with mySelectionColor.
233     if (!myLastPicked.IsNull() && myLastPicked->HasSelectable())
234     {
235       myMainPM->ClearImmediateDraw();
236       const Handle(AIS_InteractiveObject) aLastPickedAIS = Handle(AIS_InteractiveObject)::DownCast (myLastPicked->Selectable());
237       const Standard_Integer aHiMod = aLastPickedAIS->HasHilightMode() ? aLastPickedAIS->HilightMode() : 0;
238       if (!myLastPicked->IsSelected())
239       {
240         myLastPicked->Unhilight (myMainPM, aHiMod);
241         toUpdateViewer = Standard_True;
242       }
243       else if (myToHilightSelected)
244       {
245         highlightWithColor (aNewPickedOwner, mySelectionColor, theView->Viewer());
246         toUpdateViewer = Standard_True;
247       }
248     }
249
250     // initialize myLastPicked field with currently detected object
251     myLastPicked = aNewPickedOwner;
252     myLastinMain = myLastPicked;
253
254     // highlight detected object if it is not selected or myToHilightSelected flag is true
255     if (myLastPicked->HasSelectable())
256     {
257       if (!myLastPicked->IsSelected() || myToHilightSelected)
258       {
259         highlightWithColor (myLastPicked, myHilightColor, theView->Viewer());
260         toUpdateViewer = Standard_True;
261       }
262
263       aStatus = myLastPicked->IsSelected()
264               ? AIS_SOD_Selected
265               : AIS_SOD_OnlyOneDetected;
266     }
267   }
268   else
269   {
270     // previously detected object is unhilighted if it is not selected or hilighted
271     // with selection color if it is selected
272     aStatus = AIS_SOD_Nothing;
273     if (!myLastPicked.IsNull() && myLastPicked->HasSelectable())
274     {
275       myMainPM->ClearImmediateDraw();
276       const Handle(AIS_InteractiveObject) aLastPickedAIS = Handle(AIS_InteractiveObject)::DownCast (myLastPicked->Selectable());
277       Standard_Integer aHiMod = aLastPickedAIS->HasHilightMode() ? aLastPickedAIS->HilightMode() : 0;
278       if (!myLastPicked->IsSelected())
279       {
280         if (myLastPicked->IsAutoHilight())
281         {
282           myLastPicked->Unhilight (myMainPM, aHiMod);
283         }
284         toUpdateViewer = Standard_True;
285       }
286       else if (myToHilightSelected)
287       {
288         highlightSelected (myLastPicked, mySelectionColor);
289         toUpdateViewer = Standard_True;
290       }
291     }
292
293     myLastinMain.Nullify();
294     myLastPicked.Nullify();
295   }
296
297   if (toUpdateViewer
298    && theToRedrawOnUpdate)
299   {
300     theView->Viewer()->Update();
301   }
302
303   mylastmoveview = theView;
304   return aStatus;
305 }
306
307 //=======================================================================
308 //function : Select
309 //purpose  : 
310 //=======================================================================
311 AIS_StatusOfPick AIS_InteractiveContext::Select (const Standard_Integer  theXPMin,
312                                                  const Standard_Integer  theYPMin,
313                                                  const Standard_Integer  theXPMax,
314                                                  const Standard_Integer  theYPMax,
315                                                  const Handle(V3d_View)& theView,
316                                                  const Standard_Boolean  toUpdateViewer)
317 {
318   // all objects detected by the selector are taken, previous current objects are emptied,
319   // new objects are put...
320
321   if (HasOpenedContext())
322   {
323     return myLocalContexts(myCurLocalIndex)->Select (theXPMin, theYPMin,
324                                                      theXPMax, theYPMax,
325                                                      theView, toUpdateViewer);
326   }
327
328   ClearSelected (Standard_False);
329
330   Handle(StdSelect_ViewerSelector3d) aSelector;
331
332   if (theView->Viewer() == myMainVwr)
333   {
334     aSelector = myMainSel;
335     myWasLastMain = Standard_True;
336   }
337
338   aSelector->Pick (theXPMin, theYPMin, theXPMax, theYPMax, theView);
339   AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
340
341   for (aSelector->Init(); aSelector->More(); aSelector->Next())
342   {
343     const Handle(SelectMgr_EntityOwner)& aCurOwner = aSelector->Picked();
344     if (aCurOwner.IsNull() || !aCurOwner->HasSelectable() || !myFilters->IsOk (aCurOwner))
345       continue;
346
347     AIS_Selection::Select (aCurOwner);
348     aCurOwner->State (1);
349   }
350
351   HilightSelected (toUpdateViewer);
352
353   Standard_Integer aSelNum = NbSelected();
354
355   return (aSelNum == 0) ? AIS_SOP_NothingSelected
356                         : (aSelNum == 1) ? AIS_SOP_OneSelected
357                                          : AIS_SOP_SeveralSelected;
358   
359 }
360
361 //=======================================================================
362 //function : Select
363 //purpose  : Selection by polyline
364 //=======================================================================
365 AIS_StatusOfPick AIS_InteractiveContext::Select (const TColgp_Array1OfPnt2d& thePolyline,
366                                                  const Handle(V3d_View)&     theView,
367                                                  const Standard_Boolean      toUpdateViewer)
368 {
369   // all objects detected by the selector are taken, previous current objects are emptied,
370   // new objects are put...
371
372   if (HasOpenedContext())
373   {
374     return myLocalContexts(myCurLocalIndex)->Select (thePolyline, theView, toUpdateViewer);
375   }
376
377   ClearSelected (Standard_False);
378
379   Handle(StdSelect_ViewerSelector3d) aSelector;
380
381   if (theView->Viewer() == myMainVwr)
382   {
383     aSelector = myMainSel;
384     myWasLastMain = Standard_True;
385   }
386
387   aSelector->Pick (thePolyline, theView);
388   AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
389
390   for (aSelector->Init(); aSelector->More(); aSelector->Next())
391   {
392     const Handle(SelectMgr_EntityOwner) anOwner =
393       Handle(SelectMgr_EntityOwner)::DownCast (aSelector->Picked());
394     if (anOwner.IsNull() || !anOwner->HasSelectable() || !myFilters->IsOk (anOwner))
395       continue;
396
397     AIS_Selection::Select (anOwner);
398     anOwner->State (1);
399   }
400
401   HilightSelected (toUpdateViewer);
402
403   Standard_Integer aSelNum = NbSelected();
404
405   return (aSelNum == 0) ? AIS_SOP_NothingSelected
406                         : (aSelNum == 1) ? AIS_SOP_OneSelected
407                                          : AIS_SOP_SeveralSelected;
408   
409 }
410
411 //=======================================================================
412 //function : Select
413 //purpose  : 
414 //=======================================================================
415 AIS_StatusOfPick AIS_InteractiveContext::Select (const Standard_Boolean toUpdateViewer)
416 {
417   if (HasOpenedContext())
418   {
419     if(myWasLastMain)
420     {
421       return myLocalContexts(myCurLocalIndex)->Select (toUpdateViewer);
422     }
423     else
424     {
425       myLocalContexts(myCurLocalIndex)->SetSelected (Handle(AIS_InteractiveObject)::DownCast (myLastPicked->Selectable()), toUpdateViewer);
426       return AIS_SOP_OneSelected;
427     }
428   }
429
430   myMainPM->ClearImmediateDraw();
431   if (myWasLastMain && !myLastinMain.IsNull())
432   {
433     if(!myLastinMain->IsSelected())
434     {
435       SetSelected (myLastinMain, Standard_False);
436       if(toUpdateViewer)
437       {
438         UpdateCurrentViewer();
439       }
440     }
441   }
442   else
443   {
444     unhighlightSelected (Standard_True);
445
446     AIS_Selection::Select();
447     if (toUpdateViewer && myWasLastMain)
448     {
449         UpdateCurrentViewer();
450     }
451   }
452
453   Standard_Integer aSelNum = NbSelected();
454
455   return (aSelNum == 0) ? AIS_SOP_NothingSelected
456                         : (aSelNum == 1) ? AIS_SOP_OneSelected
457                                          : AIS_SOP_SeveralSelected;
458 }
459
460 //=======================================================================
461 //function : ShiftSelect
462 //purpose  : 
463 //=======================================================================
464 AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const Standard_Boolean toUpdateViewer)
465 {
466   if (HasOpenedContext())
467   {
468     if(myWasLastMain)
469     {
470       return myLocalContexts (myCurLocalIndex)->ShiftSelect (toUpdateViewer);
471     }
472     else
473     {
474       myLocalContexts (myCurLocalIndex)->AddOrRemoveSelected (myLastPicked, toUpdateViewer);
475
476       Standard_Integer aSelNum = NbSelected();
477       return (aSelNum == 0) ? AIS_SOP_NothingSelected
478                             : (aSelNum == 1) ? AIS_SOP_OneSelected
479                                              : AIS_SOP_SeveralSelected;
480     }
481   }
482
483   myMainPM->ClearImmediateDraw();
484   if (myWasLastMain && !myLastinMain.IsNull())
485   {
486     AddOrRemoveSelected (myLastinMain, toUpdateViewer);
487   }
488
489   Standard_Integer aSelNum = NbSelected();
490
491   return (aSelNum == 0) ? AIS_SOP_NothingSelected
492                         : (aSelNum == 1) ? AIS_SOP_OneSelected
493                         : AIS_SOP_SeveralSelected;
494 }
495
496 //=======================================================================
497 //function : ShiftSelect
498 //purpose  : 
499 //=======================================================================
500 AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const Standard_Integer theXPMin,
501                                                       const Standard_Integer theYPMin,
502                                                       const Standard_Integer theXPMax,
503                                                       const Standard_Integer theYPMax,
504                                                       const Handle(V3d_View)& theView,
505                                                       const Standard_Boolean toUpdateViewer)
506 {
507   if (HasOpenedContext())
508   {
509     return myLocalContexts(myCurLocalIndex)->ShiftSelect (theXPMin, theYPMin, theXPMax, theYPMax,
510                                                           theView, toUpdateViewer);
511   }
512
513   UnhilightSelected (Standard_False);
514
515   Handle(StdSelect_ViewerSelector3d) aSelector;
516   if (theView->Viewer() == myMainVwr)
517   {
518     aSelector = myMainSel;
519     myWasLastMain = Standard_True;
520   }
521   else
522   {
523     return AIS_SOP_NothingSelected;
524   }
525
526   aSelector->Pick (theXPMin, theYPMin, theXPMax, theYPMax, theView);
527   AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
528   for (aSelector->Init(); aSelector->More(); aSelector->Next())
529   {
530     const Handle(SelectMgr_EntityOwner) anOwner =  Handle(SelectMgr_EntityOwner)::DownCast (aSelector->Picked());
531     if (anOwner.IsNull() || !anOwner->HasSelectable() || !myFilters->IsOk (anOwner))
532       continue;
533
534     AIS_SelectStatus aSelStatus = AIS_Selection::Select (anOwner);
535     Standard_Integer aState = (aSelStatus == AIS_SS_Added) ? 1 : 0;
536     anOwner->State (aState);
537   }
538
539   HilightSelected (toUpdateViewer);
540
541   Standard_Integer aSelNum = NbSelected();
542
543   return (aSelNum == 0) ? AIS_SOP_NothingSelected
544                         : (aSelNum == 1) ? AIS_SOP_OneSelected
545                                          : AIS_SOP_SeveralSelected;
546
547 }
548
549 //=======================================================================
550 //function : ShiftSelect
551 //purpose  : 
552 //=======================================================================
553 AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const TColgp_Array1OfPnt2d& thePolyline,
554                                                       const Handle(V3d_View)& theView,
555                                                       const Standard_Boolean toUpdateViewer)
556 {
557   if (HasOpenedContext())
558   {
559     return myLocalContexts(myCurLocalIndex)->ShiftSelect (thePolyline, theView, toUpdateViewer);
560   }
561
562   UnhilightSelected (Standard_False);
563
564   Handle(StdSelect_ViewerSelector3d) aSelector;
565
566   if (theView->Viewer() == myMainVwr)
567   {
568     aSelector= myMainSel;
569     myWasLastMain = Standard_True;
570   }
571   else
572   {
573     return AIS_SOP_NothingSelected;
574   }
575
576   aSelector->Pick (thePolyline, theView);
577
578   AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
579   for (aSelector->Init(); aSelector->More(); aSelector->Next())
580   {
581     const Handle(SelectMgr_EntityOwner) anOwner =  Handle(SelectMgr_EntityOwner)::DownCast (aSelector->Picked());
582     if (anOwner.IsNull() || !anOwner->HasSelectable() || !myFilters->IsOk (anOwner))
583       continue;
584
585     AIS_SelectStatus aSelStatus = AIS_Selection::Select (anOwner);
586     Standard_Integer aState = (aSelStatus == AIS_SS_Added) ? 1 : 0;
587     anOwner->State (aState);
588   }
589
590   HilightSelected (toUpdateViewer);
591
592   Standard_Integer aSelNum = NbSelected();
593
594   return (aSelNum == 0) ? AIS_SOP_NothingSelected
595                         : (aSelNum == 1) ? AIS_SOP_OneSelected
596                                          : AIS_SOP_SeveralSelected;
597 }
598
599 //=======================================================================
600 //function : SetCurrentObject
601 //purpose  : OBSOLETE, please use SetSelected() instead
602 //TODO     : Remove in process of local context deletion
603 //=======================================================================
604 void AIS_InteractiveContext::SetCurrentObject (const Handle(AIS_InteractiveObject)& theObject,
605                                                const Standard_Boolean theToUpdateViewer)
606 {
607   if (HasOpenedContext())
608     return;
609
610   SetSelected (theObject, theToUpdateViewer);
611 }
612
613 //=======================================================================
614 //function : AddOrRemoveCurrentObject
615 //purpose  : OBSOLETE, please use AddOrRemoveSelected() instead
616 //TODO     : Remove in process of local context deletion
617 //=======================================================================
618 void AIS_InteractiveContext::AddOrRemoveCurrentObject (const Handle(AIS_InteractiveObject)& theObj,
619                                                        const Standard_Boolean theIsToUpdateViewer)
620 {
621   if (HasOpenedContext())
622     return;
623
624   AddOrRemoveSelected (theObj, theIsToUpdateViewer);
625 }
626 //=======================================================================
627 //function : UpdateCurrent
628 //purpose  : OBSOLETE, please use UpdateSelected() instead
629 //TODO     : Remove in process of local context deletion
630 //=======================================================================
631 void AIS_InteractiveContext::UpdateCurrent()
632 {
633   UpdateSelected();
634 }
635
636 //=======================================================================
637 //function : IsCurrent
638 //purpose  : OBSOLETE, please use IsSelected() instead
639 //TODO     : Remove in process of local context deletion
640 //=======================================================================
641 Standard_Boolean AIS_InteractiveContext::IsCurrent (const Handle(AIS_InteractiveObject)& theObject) const
642 {
643   return IsSelected (theObject);
644 }
645
646 //=======================================================================
647 //function : InitCurrent
648 //purpose  : OBSOLETE, please use InitSelected() instead
649 //TODO     : Remove in process of local context deletion
650 //=======================================================================
651 void AIS_InteractiveContext::InitCurrent()
652 {
653   if (HasOpenedContext())
654     return;
655
656   InitSelected();
657 }
658
659 //=======================================================================
660 //function : MoreCurrent
661 //purpose  : OBSOLETE, please use MoreSelected() instead
662 //TODO     : Remove in process of local context deletion
663 //=======================================================================
664 Standard_Boolean AIS_InteractiveContext::MoreCurrent() const 
665 {
666   return !HasOpenedContext() && MoreSelected();
667 }
668
669 //=======================================================================
670 //function : NextCurrent
671 //purpose  : OBSOLETE, please use NextSelected() instead
672 //TODO     : Remove in process of local context deletion
673 //=======================================================================
674 void AIS_InteractiveContext::NextCurrent()
675 {
676   if (HasOpenedContext())
677     return;
678
679   NextSelected();
680 }
681
682 //=======================================================================
683 //function : Current
684 //purpose  : OBSOLETE, please use SelectedInteractive() instead
685 //TODO     : Remove in process of local context deletion
686 //=======================================================================
687 Handle(AIS_InteractiveObject) AIS_InteractiveContext::Current() const 
688 {
689   return HasOpenedContext() ? NULL : SelectedInteractive();
690 }
691
692 //=======================================================================
693 //function : NbCurrents
694 //purpose  : OBSOLETE, please use NbSelected() instead
695 //TODO     : Remove in process of local context deletion
696 //=======================================================================
697 Standard_Integer AIS_InteractiveContext::NbCurrents()
698 {
699   return HasOpenedContext() ? -1 : NbSelected();
700 }
701
702 //=======================================================================
703 //function : HilightCurrents
704 //purpose  : OBSOLETE, please use HilightSelected() instead
705 //TODO     : Remove in process of local context deletion
706 //=======================================================================
707 void AIS_InteractiveContext::HilightCurrents (const Standard_Boolean theToUpdateViewer)
708 {
709   if (HasOpenedContext())
710     return;
711
712   HilightSelected (theToUpdateViewer);
713 }
714
715 //=======================================================================
716 //function : UnhilightCurrents
717 //purpose  : OBSOLETE, please use UnhilightSelected() instead
718 //TODO     : Remove in process of local context deletion
719 //=======================================================================
720 void AIS_InteractiveContext::UnhilightCurrents (const Standard_Boolean theToUpdateViewer)
721 {
722   if (HasOpenedContext())
723     return;
724
725   UnhilightSelected (theToUpdateViewer);
726 }
727
728 //=======================================================================
729 //function : ClearCurrents
730 //purpose  : OBSOLETE, please use ClearCurrents() instead
731 //TODO     : Remove in process of local context deletion
732 //=======================================================================
733 void AIS_InteractiveContext::ClearCurrents(const Standard_Boolean theToUpdateViewer)
734 {
735   if (HasOpenedContext())
736     return;
737
738   ClearSelected (theToUpdateViewer);
739 }
740
741
742 //=======================================================================
743 //function : HilightSelected
744 //purpose  :
745 //=======================================================================
746 void AIS_InteractiveContext::HilightSelected (const Standard_Boolean theToUpdateViewer)
747 {
748   if (HasOpenedContext())
749   {
750     return myLocalContexts (myCurLocalIndex)->HilightPicked (theToUpdateViewer);
751   }
752
753   // In case of selection without using local context
754   myMainPM->ClearImmediateDraw();
755   AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
756   Handle(AIS_Selection) aSel = AIS_Selection::Selection (myCurrentName.ToCString());
757   AIS_MapOfObjSelectedOwners anObjOwnerMap;
758   for (aSel->Init(); aSel->More(); aSel->Next())
759   {
760     const Handle(SelectMgr_EntityOwner) anOwner =
761       Handle(SelectMgr_EntityOwner)::DownCast (aSel->Value());
762     if (!anOwner.IsNull() && anOwner->HasSelectable())
763     {
764       const Handle(AIS_InteractiveObject) anObj =
765         Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
766       if (anOwner == anObj->GlobalSelOwner())
767       {
768         Handle(AIS_GlobalStatus)& aState = myObjects.ChangeFind (anObj);
769         aState->SetHilightStatus (Standard_True);
770         aState->SetHilightColor (mySelectionColor);
771       }
772       anOwner->State (1);
773       if (!anOwner->IsAutoHilight())
774       {
775         NCollection_Handle<SelectMgr_SequenceOfOwner> aSeq;
776         if (anObjOwnerMap.Find (anObj, aSeq))
777         {
778           aSeq->Append (anOwner);
779         }
780         else
781         {
782           aSeq = new SelectMgr_SequenceOfOwner();
783           aSeq->Append (anOwner);
784           anObjOwnerMap.Bind (anObj, aSeq);
785         }
786       }
787       else
788       {
789         const Standard_Integer aHiMode = anObj->HasHilightMode() ? anObj->HilightMode() : 0;
790         anOwner->HilightWithColor (myMainPM, mySelectionColor, aHiMode);
791       }
792     }
793   }
794
795   if (!anObjOwnerMap.IsEmpty())
796   {
797     for (AIS_MapOfObjSelectedOwners::Iterator anIter (anObjOwnerMap); anIter.More(); anIter.Next())
798     {
799       anIter.Key()->HilightSelected (myMainPM, *anIter.Value());
800     }
801     anObjOwnerMap.Clear();
802   }
803
804   if (theToUpdateViewer)
805     UpdateCurrentViewer();
806 }
807
808 //=======================================================================
809 //function : UnhilightSelected
810 //purpose  :
811 //=======================================================================
812 void AIS_InteractiveContext::UnhilightSelected (const Standard_Boolean theToUpdateViewer)
813 {
814   if (HasOpenedContext())
815   {
816     return myLocalContexts (myCurLocalIndex)->UnhilightPicked (theToUpdateViewer);
817   }
818
819   AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
820   Handle(AIS_Selection) aSel = AIS_Selection::Selection (myCurrentName.ToCString());
821   for (aSel->Init(); aSel->More(); aSel->Next())
822   {
823     const Handle(SelectMgr_EntityOwner) anOwner =
824       Handle(SelectMgr_EntityOwner)::DownCast (aSel->Value());
825     if (!anOwner.IsNull() && anOwner->HasSelectable())
826     {
827       const Handle(AIS_InteractiveObject) anObj =
828         Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
829       if (anOwner == anObj->GlobalSelOwner())
830       {
831         myObjects.ChangeFind (anObj)->SetHilightStatus (Standard_False);
832       }
833       anOwner->State (0);
834       const Standard_Integer aHiMode = anObj->HasHilightMode() ? anObj->HasHilightMode() : 0;
835       anOwner->Unhilight (myMainPM, aHiMode);
836     }
837   }
838
839   if (theToUpdateViewer)
840     UpdateCurrentViewer();
841 }
842
843
844 //=======================================================================
845 //function : ClearSelected
846 //purpose  :
847 //=======================================================================
848 void AIS_InteractiveContext::ClearSelected (const Standard_Boolean theToUpdateViewer)
849 {
850   if (HasOpenedContext())
851     return myLocalContexts (myCurLocalIndex)->ClearSelected (theToUpdateViewer);
852
853   if (NbSelected() == 0)
854     return;
855
856   unhighlightSelected();
857
858   AIS_Selection::Select();
859   myMainPM->ClearImmediateDraw();
860
861   if (theToUpdateViewer)
862     UpdateCurrentViewer();
863 }
864
865 //=======================================================================
866 //function : UpdateSelected
867 //purpose  :
868 //=======================================================================
869 void AIS_InteractiveContext::UpdateSelected (const Standard_Boolean theToUpdateViewer)
870 {
871   if (HasOpenedContext())
872   {
873     return myLocalContexts(myCurLocalIndex)->UpdateSelected (theToUpdateViewer);
874   }
875
876   HilightSelected (theToUpdateViewer);
877 }
878
879 //=======================================================================
880 //function : SetSelected
881 //purpose  : Sets the whole object as selected and highlights it with selection color
882 //=======================================================================
883 void AIS_InteractiveContext::SetSelected (const Handle(AIS_InteractiveObject)& theObject,
884                                           const Standard_Boolean theToUpdateViewer)
885 {
886   if(HasOpenedContext())
887   {
888     return myLocalContexts (myCurLocalIndex)->SetSelected (theObject, theToUpdateViewer);
889   }
890
891   if (theObject.IsNull())
892     return;
893   if(!myObjects.IsBound (theObject))
894     Display (theObject, Standard_False);
895   if (theObject->HasSelection (0))
896     return;
897
898   if (NbSelected() == 1 && myObjects (theObject)->IsHilighted())
899   {
900     Quantity_NameOfColor aHiCol;
901     Standard_Boolean hasHiCol = Standard_False;
902     if (IsHilighted (theObject, hasHiCol, aHiCol))
903     {
904       if (hasHiCol && aHiCol!= mySelectionColor)
905       {
906         HilightWithColor (theObject, mySelectionColor, theToUpdateViewer);
907       }
908     }
909     return;
910   }
911
912   AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
913   Handle(AIS_Selection) aCurSel = AIS_Selection::Selection (myCurrentName.ToCString());
914   for (aCurSel->Init(); aCurSel->More(); aCurSel->Next())
915   {
916     const Handle(SelectMgr_EntityOwner) anOwner =
917       Handle(SelectMgr_EntityOwner)::DownCast (aCurSel->Value());
918     if (anOwner.IsNull() || !anOwner->HasSelectable() || !myFilters->IsOk (anOwner))
919       continue;
920
921     Handle(AIS_InteractiveObject) aSelectable =
922       Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
923     Unhilight (aSelectable, Standard_False);
924     anOwner->State (0);
925     if (anOwner == aSelectable->GlobalSelOwner())
926     {
927       myObjects.ChangeFind (aSelectable)->SetHilightStatus (Standard_False);
928     }
929   }
930
931   // added to avoid untimely viewer update...
932   const Handle(SelectMgr_Selection)& aSel = theObject->Selection (0);
933   if (aSel->IsEmpty())
934     return;
935   aSel->Init();
936   Handle(SelectMgr_EntityOwner) anOwner =
937     Handle(SelectMgr_EntityOwner)::DownCast (aSel->Sensitive()->BaseSensitive()->OwnerId());
938   AIS_Selection::ClearAndSelect (anOwner);
939   anOwner->State (1);
940   if (anOwner == theObject->GlobalSelOwner())
941   {
942     Handle(AIS_GlobalStatus)& aState = myObjects.ChangeFind (theObject);
943     aState->SetHilightStatus (Standard_True);
944     aState->SetHilightColor (mySelectionColor);
945   }
946   Quantity_NameOfColor aHiCol;
947   Standard_Boolean hasHiCol = Standard_False;
948   if (IsHilighted (theObject, hasHiCol, aHiCol))
949   {
950     if (hasHiCol && aHiCol!= mySelectionColor)
951     {
952       HilightWithColor (theObject, mySelectionColor, Standard_False);
953     }
954   }
955   else
956   {
957     HilightWithColor (theObject, mySelectionColor, Standard_False);
958   }
959
960   if (theToUpdateViewer)
961     UpdateCurrentViewer();
962 }
963
964 //=======================================================================
965 //function : SetSelected
966 //purpose  : Sets the whole object as selected and highlights it with selection color
967 //=======================================================================
968 void AIS_InteractiveContext::SetSelected (const Handle(SelectMgr_EntityOwner)& theOwner,
969                                           const Standard_Boolean theToUpdateViewer)
970 {
971   if (theOwner.IsNull() || !theOwner->HasSelectable() || !myFilters->IsOk (theOwner))
972     return;
973
974   const Handle(AIS_InteractiveObject) anObject =
975     Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
976
977   if (NbSelected() == 1 && theOwner->IsSelected())
978   {
979     Quantity_NameOfColor aCustomColor;
980     Standard_Boolean isCustomColorSet;
981     if (IsHilighted (theOwner, isCustomColorSet, aCustomColor))
982     {
983       if (isCustomColorSet && aCustomColor != mySelectionColor)
984       {
985         const Standard_Integer aHiMode = anObject->HasHilightMode() ? anObject->HilightMode() : 0;
986         theOwner->HilightWithColor (myMainPM, mySelectionColor, aHiMode);
987       }
988     }
989     return;
990   }
991
992   if (!myObjects.IsBound (anObject))
993     Display (anObject, Standard_False);
994
995   unhighlightSelected();
996
997   AIS_Selection::ClearAndSelect (theOwner);
998   theOwner->State (1);
999   Quantity_NameOfColor aCustomColor;
1000   Standard_Boolean isCustomColorSet;
1001   if (!IsHilighted (theOwner, isCustomColorSet, aCustomColor) || (isCustomColorSet && aCustomColor!= mySelectionColor))
1002   {
1003     highlightSelected (theOwner, mySelectionColor);
1004   }
1005
1006   if (theOwner == anObject->GlobalSelOwner())
1007   {
1008     Handle(AIS_GlobalStatus)& aState = myObjects.ChangeFind (anObject);
1009     aState->SetHilightStatus (Standard_True);
1010     aState->SetHilightColor (mySelectionColor);
1011   }
1012
1013   if (theToUpdateViewer)
1014     UpdateCurrentViewer();
1015 }
1016
1017 //=======================================================================
1018 //function : AddOrRemoveSelected
1019 //purpose  : Adds or removes current object from AIS selection and highlights/unhighlights it.
1020 //           Since this method makes sence only for neutral point selection of a whole object,
1021 //           if 0 selection of the object is empty this method simply does nothing.
1022 //=======================================================================
1023 void AIS_InteractiveContext::AddOrRemoveSelected (const Handle(AIS_InteractiveObject)& theObject,
1024                                                   const Standard_Boolean theToUpdateViewer)
1025 {
1026   if (theObject.IsNull())
1027     return;
1028
1029   if (HasOpenedContext())
1030     return myLocalContexts (myCurLocalIndex)->AddOrRemoveSelected (theObject, theToUpdateViewer);
1031
1032   const Standard_Integer aGlobalSelMode = theObject->GlobalSelectionMode();
1033   if (!myObjects.IsBound (theObject) || !theObject->HasSelection (aGlobalSelMode))
1034     return;
1035
1036   if (!theObject->HasInteractiveContext())
1037     theObject->SetContext (this);
1038
1039   const Handle(SelectMgr_Selection)& aSel = theObject->Selection (aGlobalSelMode);
1040
1041   if (aSel->IsEmpty())
1042     return;
1043
1044   aSel->Init();
1045   const Handle(SelectMgr_EntityOwner) anOwner =
1046     Handle(SelectMgr_EntityOwner)::DownCast (aSel->Sensitive()->BaseSensitive()->OwnerId());
1047
1048   if (anOwner.IsNull() || !anOwner->HasSelectable())
1049     return;
1050
1051   AddOrRemoveSelected (anOwner, theToUpdateViewer);
1052 }
1053 //=======================================================================
1054 //function : AddOrRemoveSelected
1055 //purpose  : 
1056 //=======================================================================
1057
1058 void AIS_InteractiveContext::AddOrRemoveSelected (const TopoDS_Shape& aShap,
1059                                             const Standard_Boolean updateviewer)
1060
1061   if(!HasOpenedContext()) {
1062 #ifdef OCCT_DEBUG
1063     cout<<" Attempt to remove a selected shape with no opened local context"<<endl;
1064 #endif
1065     return;
1066   }
1067   
1068   myLocalContexts(myCurLocalIndex)->AddOrRemoveSelected(aShap,updateviewer);
1069   if(updateviewer) UpdateCurrentViewer();
1070   
1071 }
1072
1073 //=======================================================================
1074 //function : AddOrRemoveSelected
1075 //purpose  : Allows to highlight or unhighlight the owner given depending on
1076 //           its selection status
1077 //=======================================================================
1078 void AIS_InteractiveContext::AddOrRemoveSelected (const Handle(SelectMgr_EntityOwner)& theOwner,
1079                                                   const Standard_Boolean theToUpdateViewer)
1080 {
1081   if (HasOpenedContext())
1082     return myLocalContexts(myCurLocalIndex)->AddOrRemoveSelected (theOwner, theToUpdateViewer);
1083
1084   if (theOwner.IsNull() || !theOwner->HasSelectable())
1085     return;
1086
1087   AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
1088   Handle(AIS_Selection) aCurSel = AIS_Selection::Selection (myCurrentName.ToCString());
1089
1090   AIS_SelectStatus aSelStat = AIS_Selection::Select (theOwner);
1091
1092   Standard_Integer aState = aSelStat == AIS_SS_Added ?  1 : 0;
1093   theOwner->State (aState);
1094   const Handle(AIS_InteractiveObject) anObj =
1095     Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
1096   const Standard_Boolean isGlobal = anObj->GlobalSelOwner() == theOwner;
1097   Handle(AIS_GlobalStatus)& aStatus = myObjects.ChangeFind (anObj);
1098   const Standard_Integer aHiMode = anObj->HasHilightMode() ? anObj->HilightMode() : 0;
1099   if (aState == 1)
1100   {
1101     highlightSelected (theOwner, mySelectionColor);
1102     if (isGlobal)
1103     {
1104       aStatus->SetHilightStatus (Standard_True);
1105       aStatus->SetHilightColor (mySelectionColor);
1106     }
1107   }
1108   else
1109   {
1110     if (theOwner->IsAutoHilight())
1111       theOwner->Unhilight (myMainPM, aHiMode);
1112     else
1113       anObj->ClearSelected();
1114     aStatus->SetHilightStatus (Standard_False);
1115     aStatus->SetHilightColor (Quantity_NOC_WHITE);
1116   }
1117
1118   if (theToUpdateViewer)
1119     UpdateCurrentViewer();
1120 }
1121
1122
1123 //=======================================================================
1124 //function : IsSelected
1125 //purpose  :
1126 //=======================================================================
1127 Standard_Boolean AIS_InteractiveContext::IsSelected (const Handle(AIS_InteractiveObject)& theObj) const
1128 {
1129   if(HasOpenedContext())
1130     return myLocalContexts(myCurLocalIndex)->IsSelected (theObj);
1131
1132   if (theObj.IsNull() || !myObjects.IsBound (theObj))
1133     return Standard_False;
1134
1135   const Standard_Integer aGlobalSelMode = theObj->GlobalSelectionMode();
1136   const TColStd_ListOfInteger& anActivatedModes = myObjects (theObj)->SelectionModes();
1137   Standard_Boolean isGlobalModeActivated = Standard_False;
1138   for (TColStd_ListIteratorOfListOfInteger aModeIter (anActivatedModes); aModeIter.More(); aModeIter.Next())
1139   {
1140     if (aModeIter.Value() == aGlobalSelMode)
1141     {
1142       isGlobalModeActivated = Standard_True;
1143       break;
1144     }
1145   }
1146   if (!theObj->HasSelection (aGlobalSelMode) || !isGlobalModeActivated || theObj->GlobalSelOwner().IsNull())
1147     return Standard_False;
1148
1149   return theObj->GlobalSelOwner()->State() == 1;
1150 }
1151
1152 //=======================================================================
1153 //function : IsSelected
1154 //purpose  : Returns true is the owner given is selected
1155 //=======================================================================
1156 Standard_Boolean AIS_InteractiveContext::IsSelected (const Handle(SelectMgr_EntityOwner)& theOwner) const
1157 {
1158   if (HasOpenedContext())
1159     return myLocalContexts(myCurLocalIndex)->IsSelected (theOwner);
1160
1161   if (theOwner.IsNull())
1162     return Standard_False;
1163
1164   return theOwner->IsSelected();
1165 }
1166
1167 //=======================================================================
1168 //function : InitSelected
1169 //purpose  :
1170 //=======================================================================
1171 void AIS_InteractiveContext::InitSelected()
1172 {
1173   if (HasOpenedContext())
1174   {
1175     myLocalContexts (myCurLocalIndex)->InitSelected();
1176     return;
1177   }
1178
1179   AIS_Selection::Selection (myCurrentName.ToCString())->Init();
1180 }
1181
1182 //=======================================================================
1183 //function : MoreSelected
1184 //purpose  :
1185 //=======================================================================
1186 Standard_Boolean AIS_InteractiveContext::MoreSelected() const
1187 {
1188   if (HasOpenedContext())
1189     return myLocalContexts (myCurLocalIndex)->MoreSelected();
1190
1191   return AIS_Selection::Selection (myCurrentName.ToCString())->More();
1192 }
1193
1194 //=======================================================================
1195 //function : NextSelected
1196 //purpose  :
1197 //=======================================================================
1198 void AIS_InteractiveContext::NextSelected()
1199 {
1200   if(HasOpenedContext())
1201   {
1202     return myLocalContexts (myCurLocalIndex)->NextSelected();
1203     return;
1204   }
1205
1206   AIS_Selection::Selection (myCurrentName.ToCString())->Next();
1207 }
1208
1209 //=======================================================================
1210 //function : HasSelectedShape
1211 //purpose  :
1212 //=======================================================================
1213 Standard_Boolean AIS_InteractiveContext::HasSelectedShape() const
1214 {
1215   if(HasOpenedContext())
1216   {
1217     return myLocalContexts(myCurLocalIndex)->HasSelectedShape();
1218   }
1219
1220   Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (SelectedInteractive());
1221   return !aShape.IsNull();
1222 }
1223
1224 //=======================================================================
1225 //function : SelectedShape
1226 //purpose  :
1227 //=======================================================================
1228 TopoDS_Shape AIS_InteractiveContext::SelectedShape() const
1229 {
1230   if (HasOpenedContext())
1231   {
1232     return myLocalContexts (myCurLocalIndex)->SelectedShape();
1233   }
1234
1235   if (AIS_Selection::Selection (myCurrentName.ToCString())->Extent() == 0)
1236     return TopoDS_Shape();
1237
1238   const Handle(StdSelect_BRepOwner) anOwner =
1239     Handle(StdSelect_BRepOwner)::DownCast (AIS_Selection::Selection (myCurrentName.ToCString())->Value());
1240   if (!anOwner->HasSelectable())
1241     return TopoDS_Shape();
1242
1243   return anOwner->Shape().Located (anOwner->Location() * anOwner->Shape().Location());
1244 }
1245
1246 //=======================================================================
1247 //function : SelectedInteractive
1248 //purpose  :
1249 //=======================================================================
1250 Handle(AIS_InteractiveObject) AIS_InteractiveContext::SelectedInteractive() const 
1251 {
1252   if (HasOpenedContext())
1253   {
1254     return myLocalContexts(myCurLocalIndex)->SelectedInteractive();
1255   }
1256
1257   const Handle(SelectMgr_EntityOwner) anOwner =
1258     Handle(SelectMgr_EntityOwner)::DownCast (AIS_Selection::Selection (myCurrentName.ToCString())->Value());
1259   if (anOwner.IsNull() || !anOwner->HasSelectable())
1260     return NULL;
1261
1262   return Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
1263 }
1264 //=======================================================================
1265 //function : SelectedOwner
1266 //purpose  :
1267 //=======================================================================
1268 Handle(SelectMgr_EntityOwner) AIS_InteractiveContext::SelectedOwner() const
1269 {
1270   if(HasOpenedContext())
1271   {
1272     return myLocalContexts(myCurLocalIndex)->SelectedOwner();
1273   }
1274
1275   Handle(AIS_Selection) aCurSel = AIS_Selection::Selection (myCurrentName.ToCString());
1276
1277   return aCurSel->Extent() > 0 ?
1278     Handle(SelectMgr_EntityOwner)::DownCast (aCurSel->Value()) : NULL;
1279 }
1280
1281 //=======================================================================
1282 //function : EntityOwners
1283 //purpose  : 
1284 //=======================================================================
1285 void AIS_InteractiveContext::EntityOwners(Handle(SelectMgr_IndexedMapOfOwner)& theOwners,
1286                                           const Handle(AIS_InteractiveObject)& theIObj,
1287                                           const Standard_Integer theMode) const 
1288 {
1289   if ( theIObj.IsNull() )
1290       return;
1291
1292   TColStd_ListOfInteger aModes;
1293   if ( theMode == -1 )
1294     ActivatedModes( theIObj, aModes );
1295   else
1296     aModes.Append( theMode );
1297
1298   if (theOwners.IsNull())
1299     theOwners = new SelectMgr_IndexedMapOfOwner();
1300
1301   TColStd_ListIteratorOfListOfInteger anItr( aModes );
1302   for (; anItr.More(); anItr.Next() )
1303   {
1304     int aMode = anItr.Value();
1305     if ( !theIObj->HasSelection( aMode ) )
1306       continue;
1307
1308     Handle(SelectMgr_Selection) aSel = theIObj->Selection(aMode);
1309
1310     for ( aSel->Init(); aSel->More(); aSel->Next() )
1311     {
1312       Handle(SelectBasics_SensitiveEntity) aEntity = aSel->Sensitive()->BaseSensitive();
1313       if ( aEntity.IsNull() )
1314         continue;
1315
1316       Handle(SelectMgr_EntityOwner) aOwner =
1317         Handle(SelectMgr_EntityOwner)::DownCast(aEntity->OwnerId());
1318       if ( !aOwner.IsNull() )
1319         theOwners->Add( aOwner );
1320     }
1321   }
1322 }
1323
1324 //=======================================================================
1325 //function : NbSelected
1326 //purpose  :
1327 //=======================================================================
1328 Standard_Integer AIS_InteractiveContext::NbSelected()
1329 {
1330   Standard_Integer aNbSelected = 0;
1331   for (InitSelected(); MoreSelected(); NextSelected())
1332   {
1333     aNbSelected++;
1334   }
1335
1336   return aNbSelected;
1337 }
1338
1339 //=======================================================================
1340 //function : HasApplicative
1341 //purpose  :
1342 //=======================================================================
1343   Standard_Boolean AIS_InteractiveContext::HasApplicative() const 
1344 {
1345   return SelectedInteractive()->HasOwner();
1346 }
1347
1348 //=======================================================================
1349 //function : Applicative
1350 //purpose  :
1351 //=======================================================================
1352 Handle(Standard_Transient) AIS_InteractiveContext::Applicative() const 
1353 {
1354   return SelectedInteractive()->GetOwner();
1355 }
1356
1357 //==================================================
1358 // Function: HasDetected
1359 // Purpose :
1360 //==================================================
1361 Standard_Boolean AIS_InteractiveContext::HasDetected() const
1362 {
1363   if(HasOpenedContext())
1364     return myLocalContexts(myCurLocalIndex)->HasDetected();
1365
1366   return !myLastPicked.IsNull();
1367 }
1368
1369 //=======================================================================
1370 //function : HasDetectedShape
1371 //purpose  : 
1372 //=======================================================================
1373
1374 Standard_Boolean AIS_InteractiveContext::HasDetectedShape() const 
1375 {
1376   if(HasOpenedContext())
1377     return myLocalContexts(myCurLocalIndex)->HasDetectedShape();
1378   return Standard_False;
1379 }
1380
1381 //=======================================================================
1382 //function : DetectedShape
1383 //purpose  : 
1384 //=======================================================================
1385
1386 const TopoDS_Shape&
1387 AIS_InteractiveContext::DetectedShape() const
1388 {
1389   return myLocalContexts(myCurLocalIndex)->DetectedShape();
1390 }                                           
1391
1392 //=======================================================================
1393 //function : DetectedInteractive
1394 //purpose  :
1395 //=======================================================================
1396 Handle(AIS_InteractiveObject) AIS_InteractiveContext::DetectedInteractive() const
1397 {
1398   if (HasOpenedContext())
1399     return myLocalContexts(myCurLocalIndex)->DetectedInteractive();
1400
1401   return Handle(AIS_InteractiveObject)::DownCast (myLastPicked->Selectable());
1402 }
1403
1404
1405 Standard_Boolean AIS_InteractiveContext::HasNextDetected() const 
1406 {
1407   if(!HasOpenedContext())
1408     return Standard_False; // temporaire
1409   else
1410     return myLocalContexts(myCurLocalIndex)->HasNextDetected();
1411   
1412 }
1413
1414
1415 //=======================================================================
1416 //function : DetectedOwner
1417 //purpose  : 
1418 //=======================================================================
1419 Handle(SelectMgr_EntityOwner) AIS_InteractiveContext::DetectedOwner() const
1420 {
1421   if (HasOpenedContext())
1422     return myLocalContexts(myCurLocalIndex)->DetectedOwner();
1423
1424   return myLastPicked;
1425 }
1426
1427 //=======================================================================
1428 //function : HilightNextDetected
1429 //purpose  :
1430 //=======================================================================
1431 Standard_Integer AIS_InteractiveContext::HilightNextDetected (const Handle(V3d_View)& theView,
1432                                                               const Standard_Boolean  theToRedrawImmediate)
1433 {
1434   return HasOpenedContext()
1435        ? myLocalContexts (myCurLocalIndex)->HilightNextDetected (theView, theToRedrawImmediate)
1436        : 0;
1437     
1438 }
1439
1440 //=======================================================================
1441 //function : HilightNextDetected
1442 //purpose  :
1443 //=======================================================================
1444 Standard_Integer AIS_InteractiveContext::HilightPreviousDetected (const Handle(V3d_View)& theView,
1445                                                                   const Standard_Boolean  theToRedrawImmediate)
1446 {
1447   return HasOpenedContext()
1448        ? myLocalContexts (myCurLocalIndex)->HilightPreviousDetected (theView, theToRedrawImmediate)
1449        : 0;
1450     
1451 }
1452
1453 //=======================================================================
1454 //function : InitDetected
1455 //purpose  :
1456 //=======================================================================
1457 void AIS_InteractiveContext::InitDetected()
1458 {
1459   if (HasOpenedContext())
1460   {
1461     myLocalContexts(myCurLocalIndex)->InitDetected();
1462     return;
1463   }
1464
1465   if(myAISDetectedSeq.Length() != 0)
1466   {
1467     myAISCurDetected = 1;
1468   }
1469 }
1470
1471 //=======================================================================
1472 //function : MoreDetected
1473 //purpose  :
1474 //=======================================================================
1475 Standard_Boolean AIS_InteractiveContext::MoreDetected() const
1476 {
1477   if (HasOpenedContext())
1478   {
1479     return myLocalContexts(myCurLocalIndex)->MoreDetected();
1480   }
1481
1482   return (myAISCurDetected > 0 && myAISCurDetected <= myAISDetectedSeq.Length()) ?
1483           Standard_True : Standard_False;
1484 }
1485
1486 //=======================================================================
1487 //function : NextDetected
1488 //purpose  :
1489 //=======================================================================
1490 void AIS_InteractiveContext::NextDetected()
1491 {
1492   if(HasOpenedContext())
1493   {
1494     myLocalContexts(myCurLocalIndex)->NextDetected();
1495     return;
1496   }
1497
1498   myAISCurDetected++;
1499 }
1500
1501 //=======================================================================
1502 //function : DetectedCurrentShape
1503 //purpose  :
1504 //=======================================================================
1505 const TopoDS_Shape& AIS_InteractiveContext::DetectedCurrentShape() const
1506 {
1507   if (HasOpenedContext())
1508   {
1509     return myLocalContexts(myCurLocalIndex)->DetectedCurrentShape();
1510   }
1511
1512   static TopoDS_Shape aDummyShape;
1513
1514   Handle(AIS_Shape) aCurrentShape = Handle(AIS_Shape)::DownCast (DetectedCurrentObject());
1515
1516   if (aCurrentShape.IsNull())
1517   {
1518     return aDummyShape;
1519   }
1520
1521   return aCurrentShape->Shape();
1522 }
1523
1524 //=======================================================================
1525 //function : DetectedCurrentObject
1526 //purpose  :
1527 //=======================================================================
1528 Handle(AIS_InteractiveObject) AIS_InteractiveContext::DetectedCurrentObject() const
1529 {
1530   if (HasOpenedContext())
1531   {
1532     return myLocalContexts(myCurLocalIndex)->DetectedCurrentObject();
1533   }
1534
1535   return MoreDetected() ? myAISDetectedSeq(myAISCurDetected) : NULL;
1536 }
1537
1538 //=======================================================================
1539 //function : FirstSelectedObject
1540 //purpose  :
1541 //=======================================================================
1542 Handle(AIS_InteractiveObject) AIS_InteractiveContext::FirstSelectedObject()
1543 {
1544   Handle(AIS_InteractiveObject) anObject;
1545
1546   if (HasOpenedContext())
1547     return anObject;
1548
1549   InitSelected();
1550   if (MoreSelected())
1551   {
1552     return SelectedInteractive();
1553   }
1554   return anObject;
1555 }
1556
1557 //=======================================================================
1558 //function : RedrawImmediate
1559 //purpose  : Redisplays immediate strucures of the viewer given according to their visibility
1560 //=======================================================================
1561 void AIS_InteractiveContext::RedrawImmediate (const Handle(V3d_Viewer)& theViewer)
1562 {
1563   myMainPM->RedrawImmediate (theViewer);
1564 }