0026945: Visualization - selection does not work after closing one of local contexts...
[occt.git] / src / AIS / AIS_LocalContext.cxx
1 // Created on: 1997-01-17
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 //Modified by ROB : Traque des UpdateConversion intempestifs.
18
19 #include <AIS_DataMapIteratorOfDataMapOfSelStat.hxx>
20 #include <AIS_InteractiveContext.hxx>
21 #include <AIS_InteractiveObject.hxx>
22 #include <AIS_ListIteratorOfListOfInteractive.hxx>
23 #include <AIS_ListOfInteractive.hxx>
24 #include <AIS_LocalContext.hxx>
25 #include <AIS_LocalStatus.hxx>
26 #include <AIS_Selection.hxx>
27 #include <AIS_Shape.hxx>
28 #include <Aspect_TypeOfMarker.hxx>
29 #include <Graphic3d_Structure.hxx>
30 #include <Prs3d_LineAspect.hxx>
31 #include <Prs3d_PlaneAspect.hxx>
32 #include <Prs3d_PointAspect.hxx>
33 #include <Prs3d_Presentation.hxx>
34 #include <SelectMgr_CompositionFilter.hxx>
35 #include <SelectMgr_EntityOwner.hxx>
36 #include <SelectMgr_Filter.hxx>
37 #include <SelectMgr_OrFilter.hxx>
38 #include <SelectMgr_SelectableObject.hxx>
39 #include <SelectMgr_SelectionManager.hxx>
40 #include <Standard_Transient.hxx>
41 #include <Standard_Type.hxx>
42 #include <StdSelect_ShapeTypeFilter.hxx>
43 #include <StdSelect_ViewerSelector3d.hxx>
44 #include <TCollection_AsciiString.hxx>
45 #include <TColStd_ListIteratorOfListOfInteger.hxx>
46 #include <TopAbs_ShapeEnum.hxx>
47 #include <TopoDS_Shape.hxx>
48 #include <V3d_View.hxx>
49 #include <V3d_Viewer.hxx>
50
51 #include <stdio.h>
52 IMPLEMENT_STANDARD_RTTIEXT(AIS_LocalContext,MMgt_TShared)
53
54 static TCollection_AsciiString AIS_Local_SelName(const Standard_Address address,
55                                                  const Standard_Integer anIndex)
56 {
57 //  TCollection_AsciiString SelName;
58   char string[100];
59   sprintf(string,"%p_%d", address, anIndex);    // works under any system 
60   TCollection_AsciiString SelName(string);
61   return SelName;
62 }
63
64
65 //=======================================================================
66 //function : AIS_LocalContext
67 //purpose  : 
68 //=======================================================================
69
70
71 AIS_LocalContext::AIS_LocalContext(){}
72
73 AIS_LocalContext::AIS_LocalContext(const Handle(AIS_InteractiveContext)& aCtx,
74                                    const Standard_Integer Index,
75                                    const Standard_Boolean LoadDisplayed,
76                                    const Standard_Boolean AcceptStandardModes,
77                                    const Standard_Boolean AcceptEraseOfTemp,
78                                    const Standard_Boolean /*BothViewers*/):
79 myCTX(aCtx),
80 myLoadDisplayed(LoadDisplayed),
81 myAcceptStdMode(AcceptStandardModes),
82 myAcceptErase(AcceptEraseOfTemp),
83 mySM(aCtx->SelectionManager()),
84 myMainVS(aCtx->MainSelector()),
85 myFilters(new SelectMgr_OrFilter()),
86 myAutoHilight(Standard_True),
87 myMapOfOwner (new SelectMgr_IndexedMapOfOwner()),
88 mylastindex(0),
89 mylastgood(0),
90 myCurDetected(0),
91 myAISCurDetected(0)
92
93 {
94   // bind self to AIS_InteractiveContext::myLocalContexts. Further, the
95   // constructor executes logic that implies that the context is already
96   // created and mapped.
97   aCtx->myLocalContexts.Bind (Index, this);
98
99   myMainVS->ResetSelectionActivationStatus();
100   myMainPM = aCtx->MainPrsMgr();
101   mySelName = AIS_Local_SelName(this, Index);
102   AIS_Selection::CreateSelection(mySelName.ToCString());
103
104   mySM->Add(myMainVS);
105   if(myLoadDisplayed) LoadContextObjects();
106   Process();
107
108 }
109
110
111 //=======================================================================
112 //function : SetContext
113 //purpose  : 
114 //=======================================================================
115
116 void AIS_LocalContext::SetContext(const Handle(AIS_InteractiveContext)& aCtx)
117 {  myCTX = aCtx;}
118
119 //=======================================================================
120 //function : Display
121 //purpose  : 
122 //=======================================================================
123
124 Standard_Boolean AIS_LocalContext::Display(const Handle(AIS_InteractiveObject)& anInteractive,
125                                            const Standard_Integer WhichMode,
126                                            const Standard_Boolean AllowShapeDecomposition,
127                                            const Standard_Integer ActivationMode)
128 {
129   if(myActiveObjects.IsBound(anInteractive)){
130     const Handle(AIS_LocalStatus)& STAT = myActiveObjects(anInteractive);
131     
132     if(STAT->DisplayMode() == -1){
133       if(!myMainPM->IsDisplayed(anInteractive,WhichMode))
134         myMainPM->Display(anInteractive,WhichMode);
135       if(STAT->IsTemporary())
136         STAT->SetDisplayMode(WhichMode);
137     }
138     else if(STAT->DisplayMode()!=WhichMode && STAT->IsTemporary()){
139       myMainPM->Erase(anInteractive,STAT->DisplayMode());
140       STAT->SetDisplayMode(WhichMode);
141       if(!myMainPM->IsDisplayed(anInteractive,WhichMode))
142         myMainPM->Display(anInteractive,WhichMode);
143     }
144     
145     if(ActivationMode!=-1){
146       if(!STAT->IsActivated(ActivationMode)){
147         STAT->ClearSelectionModes();
148         mySM->Load(anInteractive,myMainVS);
149         STAT->AddSelectionMode(ActivationMode);
150         mySM->Activate(anInteractive,ActivationMode,myMainVS);
151       }
152     }
153   }
154   else {
155     Handle(AIS_LocalStatus) Att = new AIS_LocalStatus();
156     
157     if(anInteractive->AcceptShapeDecomposition() && AllowShapeDecomposition)
158       Att->SetDecomposition(Standard_True);
159     else 
160       Att->SetDecomposition(Standard_False);
161     // status temporary or not
162     if(myCTX->DisplayStatus(anInteractive) == AIS_DS_None ||
163        myCTX->DisplayStatus(anInteractive) == AIS_DS_Temporary)
164       Att->SetTemporary(Standard_True);
165     else
166       Att->SetTemporary(Standard_False); 
167
168
169     
170     if(!myCTX->IsDisplayed(anInteractive,WhichMode)){
171       
172       //storing information....
173       Att->SetDisplayMode(WhichMode);
174       if (ActivationMode!=-1)
175         Att->AddSelectionMode(ActivationMode);
176       Standard_Integer HiMod = anInteractive->HasHilightMode()? anInteractive->HilightMode(): WhichMode;
177       Att->SetHilightMode(HiMod);
178
179       if(!myMainPM->IsDisplayed(anInteractive,WhichMode))
180         myMainPM->Display(anInteractive,WhichMode);
181       
182       if(ActivationMode!=-1){
183         mySM->Load(anInteractive,myMainVS);
184         mySM->Activate(anInteractive,ActivationMode,myMainVS);
185       }
186     }
187     else{
188       Standard_Integer HiMod = anInteractive->HasHilightMode()? anInteractive->HilightMode(): WhichMode;
189       Att->SetHilightMode(HiMod);
190     }
191     myActiveObjects.Bind(anInteractive,Att);
192   }  
193   Process(anInteractive);
194
195   
196   
197
198   return Standard_True;
199 }
200
201 //=======================================================================
202 //function : Load
203 //purpose  : 
204 //=======================================================================
205
206 Standard_Boolean AIS_LocalContext::
207 Load(const Handle(AIS_InteractiveObject)& anInteractive,
208      const Standard_Boolean AllowShapeDecomposition,
209      const Standard_Integer ActivationMode)
210 {
211   if (myActiveObjects.IsBound (anInteractive))
212   {
213     if (anInteractive->HasSelection (ActivationMode))
214     {
215       const Handle(SelectMgr_Selection)& aSel = anInteractive->Selection (ActivationMode);
216       if (aSel->GetSelectionState() != SelectMgr_SOS_Activated)
217       {
218         if (!myMainVS->Contains (anInteractive))
219         {
220           mySM->Load (anInteractive, myMainVS);
221         }
222         mySM->Activate (anInteractive, ActivationMode, myMainVS);
223         return Standard_True;
224       }
225     }
226     return Standard_False;
227   }
228
229   Handle(AIS_LocalStatus) Att = new AIS_LocalStatus();
230   
231   if(anInteractive->AcceptShapeDecomposition() && AllowShapeDecomposition)
232     Att->SetDecomposition(Standard_True);
233   else 
234     Att->SetDecomposition(Standard_False);
235   
236   if(!myCTX->IsDisplayed(anInteractive))
237     Att->SetTemporary(Standard_True);
238   else
239     Att->SetTemporary(Standard_False);
240   Att->SetDisplayMode(-1);
241   
242   //storing information....
243   if(ActivationMode!=-1)
244     Att->AddSelectionMode(ActivationMode);
245   Standard_Integer HiMod = anInteractive->HasHilightMode()? anInteractive->HilightMode():0;
246   Att->SetHilightMode(HiMod);
247   //Action
248   
249   mySM->Load(anInteractive,myMainVS);
250   if(ActivationMode != -1){
251     mySM->Activate(anInteractive,ActivationMode,myMainVS);
252   }
253   myActiveObjects.Bind(anInteractive,Att);
254   Process(anInteractive);
255   return Standard_True;
256 }
257
258 //=======================================================================
259 //function : ClearPrs
260 //purpose  : 
261 //=======================================================================
262
263 Standard_Boolean AIS_LocalContext::
264 ClearPrs(const Handle(AIS_InteractiveObject)& anInteractive,
265          const Standard_Integer aMode)
266 {
267   if(!myActiveObjects.IsBound(anInteractive))
268     return Standard_False;
269
270   Standard_Boolean jobdone(Standard_False);
271   const Handle(AIS_LocalStatus)& STAT = myActiveObjects(anInteractive);
272   
273   //Display step
274   if(STAT->IsSubIntensityOn()) {
275     STAT->SubIntensityOff();
276     if(STAT->HilightMode()==aMode)
277       myMainPM->Unhighlight(anInteractive,STAT->HilightMode());
278   }
279   myMainPM->Clear(anInteractive,aMode); // correction connexions 23/09/97
280   jobdone = Standard_True;
281   if(STAT->DisplayMode()==aMode)
282     STAT->SetDisplayMode(-1);
283   return jobdone;
284 }
285 //=======================================================================
286 //function : Erase
287 //purpose  : 
288 //=======================================================================
289
290 Standard_Boolean AIS_LocalContext::
291 Erase(const Handle(AIS_InteractiveObject)& anInteractive)
292 {
293   if(!myActiveObjects.IsBound(anInteractive))
294     return Standard_False;
295   const Handle(AIS_LocalStatus)& STAT = myActiveObjects(anInteractive);
296   
297   //Display step
298   if(STAT->IsSubIntensityOn()) {
299     STAT->SubIntensityOff();
300     myMainPM->Unhighlight(anInteractive,STAT->HilightMode());
301   }
302
303   Standard_Boolean status(Standard_False);
304
305   if(STAT->DisplayMode()!=-1) {
306     if(IsSelected(anInteractive))
307       AddOrRemoveSelected(anInteractive);
308     if(myMainPM->IsHighlighted(anInteractive,STAT->HilightMode()))
309       myMainPM->Unhighlight(anInteractive,STAT->HilightMode());
310     myMainPM->SetVisibility (anInteractive, STAT->DisplayMode(), Standard_False);
311     STAT->SetDisplayMode(-1);
312     status = Standard_True;
313   }
314   if(STAT->IsTemporary()){
315     if(myMainPM->IsDisplayed(anInteractive,STAT->HilightMode()))
316       myMainPM->SetVisibility (anInteractive, STAT->HilightMode(), Standard_False);
317   }
318
319   // Deactivate selectable entities of interactive object
320   const Handle(SelectMgr_SelectableObject)& anObj = anInteractive; // to avoid ambiguity
321   if (mySM->Contains (anObj))
322   {
323     while (!STAT->SelectionModes().IsEmpty())
324     {
325       mySM->Deactivate (anInteractive, STAT->SelectionModes().Last(), myMainVS);
326       STAT->RemoveSelectionMode (STAT->SelectionModes().Last());
327     }
328   }
329
330   ClearOutdatedSelection (anInteractive, Standard_True);
331
332   return status;
333 }
334
335
336 //=======================================================================
337 //function : SetShapeDecomposition
338 //purpose  : 
339 //=======================================================================
340
341 void AIS_LocalContext::SetShapeDecomposition(const Handle(AIS_InteractiveObject)& aStoredObject, 
342                                                 const Standard_Boolean aStatus)
343 {
344   if(!myActiveObjects.IsBound(aStoredObject)) return;
345   
346   if(aStatus == myActiveObjects(aStoredObject)->Decomposed()) 
347     return;
348   
349   myActiveObjects(aStoredObject)->SetDecomposition(aStatus);
350
351   Process(aStoredObject);
352 }
353
354 //=======================================================================
355 //function : Clear
356 //purpose  : 
357 //=======================================================================
358
359 void AIS_LocalContext::Clear(const AIS_ClearMode aType)
360 {
361   switch (aType){
362   case AIS_CM_All:
363     {
364       ClearObjects();
365       myFilters->Clear();
366       while(!myListOfStandardMode.IsEmpty())
367         DeactivateStandardMode(AIS_Shape::SelectionType(myListOfStandardMode.Last()));
368       break;
369     }
370   case AIS_CM_Interactive:
371     ClearObjects();
372     break;
373   case AIS_CM_Filters:
374     myFilters->Clear();
375     break;
376   case AIS_CM_StandardModes:
377     {
378       while(!myListOfStandardMode.IsEmpty())
379         DeactivateStandardMode(AIS_Shape::SelectionType(myListOfStandardMode.Last()));
380       break;
381     }
382   case AIS_CM_TemporaryShapePrs:
383     ClearDetected();
384   }
385 }
386 //=======================================================================
387 //function : ActivateMode
388 //purpose  : 
389 //=======================================================================
390
391 void AIS_LocalContext::ActivateMode(const Handle(AIS_InteractiveObject)& aSelectable,
392                                        const Standard_Integer aMode)
393 {
394   if(!myActiveObjects.IsBound(aSelectable)) return;
395 //  if(myActiveObjects(aSelectable)->SelectionMode()!=aMode)
396 //    mySM->Deactivate(aSelectable,aMode,myMainVS);
397   if(aMode != -1){
398     myActiveObjects(aSelectable)->AddSelectionMode(aMode);
399     mySM->Activate(aSelectable,aMode,myMainVS);
400   }
401 }
402 //=======================================================================
403 //function : ActivateMode
404 //purpose  : 
405 //=======================================================================
406
407 void AIS_LocalContext::DeactivateMode(const Handle(AIS_InteractiveObject)& aSelectable,
408                                          const Standard_Integer aMode)
409 {
410   if(!myActiveObjects.IsBound(aSelectable)) return;
411   
412   if(aMode==-1) return;
413   
414   myActiveObjects(aSelectable)->RemoveSelectionMode(aMode);
415   mySM->Deactivate(aSelectable,aMode,myMainVS);
416 }
417 //=======================================================================
418 //function : ActivateMode
419 //purpose  : 
420 //=======================================================================
421
422 void AIS_LocalContext::Deactivate(const Handle(AIS_InteractiveObject)& aSelectable)
423 {
424   if(!myActiveObjects.IsBound(aSelectable)) return;
425   
426   mySM->Deactivate(aSelectable, -1, myMainVS);
427   myActiveObjects(aSelectable)->ClearSelectionModes();
428 }
429
430 //=======================================================================
431 //function : Remove
432 //purpose  : 
433 //=======================================================================
434
435 Standard_Boolean AIS_LocalContext::Remove(const Handle(AIS_InteractiveObject)& aSelectable)
436 {
437   if(!myActiveObjects.IsBound (aSelectable))
438   {
439     return Standard_False;
440   }
441
442   if (IsSelected (aSelectable))
443   {
444     AddOrRemoveSelected (aSelectable, Standard_False);
445   }
446
447   const Handle(AIS_LocalStatus)& Att = myActiveObjects (aSelectable);
448
449   TColStd_ListIteratorOfListOfInteger It;
450
451   // Deactivate standard modes
452   if (Att->Decomposed())
453   {
454     for (It.Initialize (myListOfStandardMode); It.More(); It.Next())
455     {
456       mySM->Deactivate (aSelectable, It.Value(), myMainVS);
457     }
458   }
459
460   // If object or temporary presentations
461   if (Att->IsTemporary())
462   {
463     if (Att->IsSubIntensityOn())
464     {
465       myMainPM->Unhighlight (aSelectable, Att->HilightMode());
466     }
467
468     myMainPM->Erase (aSelectable, Att->DisplayMode());
469     myMainPM->Clear (aSelectable, Att->DisplayMode());
470     if (myMainPM->IsDisplayed (aSelectable, Att->HilightMode()))
471     {
472       myMainPM->Erase (aSelectable, Att->HilightMode());
473     }
474   }
475   // If subintensity used
476   else if (Att->IsSubIntensityOn())
477   {
478     myCTX->SubIntensityOff (aSelectable);
479   }
480
481   // Deactivate stored selection modes
482   for (It.Initialize (Att->SelectionModes()); It.More(); It.Next())
483   {
484     mySM->Deactivate (aSelectable, It.Value(), myMainVS);
485   }
486
487   // Remove the interactive object from selection manager
488   const Handle(SelectMgr_SelectableObject)& anObj = aSelectable; // to avoid ambiguity
489   if (mySM->Contains (anObj))
490   {
491     mySM->Remove (anObj);
492   }
493   ClearOutdatedSelection (aSelectable, Standard_True);
494
495   // This should be done at the very end because most methods use
496   // myActiveObjects even during clean-up
497   myActiveObjects.UnBind (aSelectable);
498   return Standard_True;
499 }
500
501 //=======================================================================
502 //function : ActivateStandardMode
503 //purpose  : 
504 //=======================================================================
505
506 void AIS_LocalContext::ActivateStandardMode(const TopAbs_ShapeEnum aType)
507 {
508   
509   //check if it is not in the list
510   TColStd_ListIteratorOfListOfInteger It(myListOfStandardMode);
511   for(;It.More();It.Next())
512     if(It.Value()==aType)  return;
513   Standard_Integer IMode = AIS_Shape::SelectionMode(aType);
514   
515
516   // create a hidden filter answering ok to the type except for :
517   // if the type is shape...
518   // if the filters already impact at the type <aType>
519   if(aType != TopAbs_SHAPE){
520     if(myStdFilters[IMode].IsNull())
521       myStdFilters[IMode] = new StdSelect_ShapeTypeFilter(aType);
522     if(!HasFilters(aType))
523       myFilters->Add(myStdFilters[IMode]);
524   }
525   
526   // the mode is activated for all objects of type Shape 
527   // accepting the decomposition in standard mode.
528   myListOfStandardMode.Append(IMode);
529   
530   AIS_DataMapIteratorOfDataMapOfSelStat ItM(myActiveObjects);
531
532   for(;ItM.More();ItM.Next()){
533     if(ItM.Value()->Decomposed())
534       myCTX->SelectionManager()->Activate(ItM.Key(),
535                                           IMode,
536                                           myMainVS);
537     ItM.Value()->AddSelectionMode (IMode);
538   }
539   
540 }
541
542 //=======================================================================
543 //function : DeActivateStandardMode
544 //purpose  : 
545 //=======================================================================
546
547 void AIS_LocalContext::DeactivateStandardMode(const TopAbs_ShapeEnum aType)
548 {
549   TColStd_ListIteratorOfListOfInteger It(myListOfStandardMode);
550   Standard_Integer IMode = AIS_Shape::SelectionMode(aType);
551   for(;It.More();It.Next())
552     if(It.Value()==IMode) {
553       AIS_DataMapIteratorOfDataMapOfSelStat ItM(myActiveObjects);
554       
555       for(;ItM.More();ItM.Next()){
556         if(ItM.Value()->Decomposed()){
557           myCTX->SelectionManager()->Deactivate(ItM.Key(),
558                                                 IMode,
559                                                 myMainVS);
560           ItM.Value()->RemoveSelectionMode(IMode);
561         }
562       }
563       myListOfStandardMode.Remove(It);
564       if(myFilters->IsIn(myStdFilters[IMode]))
565         myFilters->Remove(myStdFilters[IMode]);
566       return;
567     }   
568 }
569
570 //=======================================================================
571 //function : AddFilter
572 //purpose  : 
573 //=======================================================================
574
575 void AIS_LocalContext::AddFilter(const Handle(SelectMgr_Filter)& aFilter)
576 {
577   // it is checked if the filter impacts at the type of active sub-shape 
578   // for which a filter of type has been already implemented...
579
580   TColStd_ListIteratorOfListOfInteger It(myListOfStandardMode);
581   
582   for(;It.More();It.Next()){
583     if(aFilter->ActsOn(AIS_Shape::SelectionType(It.Value())))
584       if(myFilters->IsIn(myStdFilters[It.Value()]))
585         myFilters->Remove(myStdFilters[It.Value()]);
586   } 
587   myFilters->Add(aFilter);
588 }
589
590 //=======================================================================
591 //function : RemoveFilter
592 //purpose  : 
593 //=======================================================================
594
595 void AIS_LocalContext::RemoveFilter(const Handle(SelectMgr_Filter)& aFilter)
596 {
597   if(myFilters->IsIn(aFilter)) myFilters->Remove(aFilter);
598   
599   // it is checked if the filter for type standard is active.
600   // if yes, it is checked there are still similarities among the
601   // remaining filters...
602   //     otherwise, the standard filter is restored to
603   //     continu selecting active modes...
604   TColStd_ListIteratorOfListOfInteger It(myListOfStandardMode);
605   TopAbs_ShapeEnum SE;
606   for(;It.More();It.Next()){
607     SE = AIS_Shape::SelectionType(It.Value());
608     if(aFilter->ActsOn(SE))
609       if(!HasFilters(SE))
610         myFilters->Add(myStdFilters[It.Value()]);
611   }
612 }
613
614 //=======================================================================
615 //function : Terminate
616 //purpose  :
617 //=======================================================================
618
619 void AIS_LocalContext::Terminate (const Standard_Boolean theToUpdate)
620 {
621   ClearDetected();
622   Clear();
623   myMapOfOwner->Clear();
624   
625   mylastindex=0;
626   // clear the selector...
627   myMainVS->Clear();
628   
629
630   AIS_Selection::SetCurrentSelection(mySelName.ToCString());
631   Handle(AIS_Selection) S = AIS_Selection::CurrentSelection();
632   Handle(Standard_Transient) Tr;
633   for(S->Init();S->More();S->Next()){
634     Tr = S->Value();
635     Handle(SelectMgr_EntityOwner)::DownCast (Tr)->SetSelected (Standard_False);
636   }
637
638       
639   AIS_Selection::Select();
640   AIS_Selection::Remove(mySelName.ToCString());
641
642   Handle(V3d_View) aDummyView;
643   myMainVS->ClearSensitive (aDummyView);
644
645   if (theToUpdate)
646   {
647     myCTX->UpdateCurrentViewer();
648   }
649 }
650
651
652 //=======================================================================
653 //function : SubIntensity
654 //purpose  : 
655 //=======================================================================
656
657 void AIS_LocalContext::SubIntensityOn(const Handle(AIS_InteractiveObject)& anObject)
658 {
659   if(!myActiveObjects.IsBound(anObject)) return;
660   
661   const Handle(AIS_LocalStatus)& Att = myActiveObjects(anObject);
662
663   if(Att->IsTemporary()) 
664     myMainPM->Color(anObject,myCTX->SubIntensityColor(),Att->DisplayMode());
665   
666   Att->SubIntensityOn();
667 }
668 //=======================================================================
669 //function : SubIntensity
670 //purpose  : 
671 //=======================================================================
672
673 void AIS_LocalContext::SubIntensityOff(const Handle(AIS_InteractiveObject)& anObject)
674 {
675   if(!myActiveObjects.IsBound(anObject)) return;
676   
677   const Handle(AIS_LocalStatus)& Att = myActiveObjects(anObject);
678
679   if(Att->IsTemporary()) 
680     myMainPM->Unhighlight(anObject);
681   Att->SubIntensityOff();
682 }
683
684
685 //=======================================================================
686 //function : Hilight
687 //purpose  : 
688 //=======================================================================
689
690 void AIS_LocalContext::Hilight(const  Handle(AIS_InteractiveObject)& anObject)
691 {
692   if(!myActiveObjects.IsBound(anObject)){
693     Standard_Integer HiMod = anObject->HasHilightMode()? anObject->HilightMode() : 0; 
694     Handle(AIS_LocalStatus) Att = new AIS_LocalStatus(Standard_True,
695                                                       Standard_False,
696                                                       -1,-1,HiMod);
697     myActiveObjects.Bind(anObject,Att);
698     
699   }
700   const Handle(AIS_LocalStatus)& Att = myActiveObjects(anObject);
701   myMainPM->Color(anObject,myCTX->HilightColor(),Att->HilightMode());
702   Att->SubIntensityOn();
703 }
704 //=======================================================================
705 //function : Hilight
706 //purpose  : 
707 //=======================================================================
708
709 void AIS_LocalContext::Hilight(const  Handle(AIS_InteractiveObject)& anObject,
710                                const Quantity_NameOfColor Col)
711 {
712   if(!myActiveObjects.IsBound(anObject)){
713     Standard_Integer HiMod = anObject->HasHilightMode()? anObject->HilightMode() : 0; 
714     Handle(AIS_LocalStatus) Att = new AIS_LocalStatus(Standard_True,
715                                                       Standard_False,
716                                                       -1,-1,HiMod);
717     myActiveObjects.Bind(anObject,Att);
718     
719   }
720   const Handle(AIS_LocalStatus)& Att = myActiveObjects(anObject);
721   myMainPM->Color(anObject,Col,Att->HilightMode());
722   Att->SubIntensityOn();
723   Att->SetHilightColor(Col);
724 }
725
726 //=======================================================================
727 //function : Unhilight
728 //purpose  : 
729 //=======================================================================
730
731 void AIS_LocalContext::Unhilight(const Handle(AIS_InteractiveObject)& anObject)
732 {
733   if(!myActiveObjects.IsBound(anObject)) return;
734   
735   // chieck if by hazard the object is somewhere else...
736   Standard_Integer Indx;
737   Standard_Boolean IsSomeWhereElse  = 
738     myCTX->IsInLocal(anObject,Indx) && Indx != myCTX->IndexOfCurrentLocal();
739   
740   const Handle(AIS_LocalStatus)& Att = myActiveObjects(anObject);
741   myMainPM->Unhighlight(anObject,Att->HilightMode());
742   if(Att->IsTemporary() && Att->DisplayMode()==-1)
743     if(!IsSomeWhereElse)
744       myMainPM->SetVisibility (anObject, Att->HilightMode(), Standard_False);
745
746   Att->SubIntensityOff();
747   Att->SetHilightColor(Quantity_NOC_WHITE);
748 }
749
750
751 //=======================================================================
752 //function : IsIn
753 //purpose  : 
754 //=======================================================================
755
756 Standard_Boolean AIS_LocalContext::
757 IsIn(const Handle(AIS_InteractiveObject)& anObject) const 
758 {
759   return myActiveObjects.IsBound(anObject);
760 }
761
762 //=======================================================================
763 //function : IsHilighted
764 //purpose  : 
765 //=======================================================================
766
767 Standard_Boolean AIS_LocalContext::IsHilighted(const Handle(AIS_InteractiveObject)& anObject) const 
768 {
769   if(!myActiveObjects.IsBound(anObject)) return Standard_False;
770   return myActiveObjects(anObject)->IsSubIntensityOn();
771 }
772
773 Standard_Boolean AIS_LocalContext::IsHilighted(const Handle(AIS_InteractiveObject)& anObject,
774                                                Standard_Boolean& WithColor,
775                                                Quantity_NameOfColor& HiCol) const 
776 {
777   if(!myActiveObjects.IsBound(anObject)) return Standard_False;
778   if( myActiveObjects(anObject)->IsSubIntensityOn()){
779     HiCol = myActiveObjects(anObject)->HilightColor();
780     if(HiCol==Quantity_NOC_WHITE)
781       WithColor = Standard_True;
782     else
783       WithColor = Standard_False;
784     return Standard_True;
785   }
786   return Standard_False;
787 }
788
789
790 void AIS_LocalContext::SetDisplayPriority(const Handle(AIS_InteractiveObject)& anObject,
791                                           const Standard_Integer Prior)
792 {
793   if(!myActiveObjects.IsBound(anObject)) return;
794   const Handle(AIS_LocalStatus)& STAT = myActiveObjects(anObject);
795   if(STAT->DisplayMode()==-1) return;
796   myMainPM->SetDisplayPriority(anObject,STAT->DisplayMode(),Prior);
797   if(STAT->IsSubIntensityOn())
798     myMainPM->SetDisplayPriority(anObject,STAT->HilightMode(),Prior);
799   
800   
801 }
802
803 //=======================================================================
804 //function : DisplayedObjects
805 //purpose  : 
806 //=======================================================================
807 Standard_Integer AIS_LocalContext::DisplayedObjects(TColStd_MapOfTransient& theMap) const
808 {
809   Standard_Integer NbDisp(0);
810   for(AIS_DataMapIteratorOfDataMapOfSelStat it(myActiveObjects);it.More();it.Next()){
811     const Handle(SelectMgr_SelectableObject)& SO = it.Key();
812     if(!theMap.Contains(SO))
813       if(it.Value()->DisplayMode()!=-1){
814         theMap.Add(SO);
815         NbDisp++;
816       }
817   }
818   return NbDisp;
819 }
820
821
822 //=======================================================================
823 //function : IsDisplayed
824 //purpose  : 
825 //=======================================================================
826
827 Standard_Boolean AIS_LocalContext::IsDisplayed(const Handle(AIS_InteractiveObject)& anObject) const 
828 {
829   if(!myActiveObjects.IsBound(anObject)) return Standard_False;
830   return (myActiveObjects(anObject)->DisplayMode()!=-1);
831 }
832
833 //=======================================================================
834 //function : IsDisplayed
835 //purpose  : 
836 //=======================================================================
837
838 Standard_Boolean AIS_LocalContext::IsDisplayed(const Handle(AIS_InteractiveObject)& anObject,
839                                                   const Standard_Integer aMode) const 
840 {
841   if(!myActiveObjects.IsBound(anObject)) return Standard_False;
842   return (myActiveObjects(anObject)->DisplayMode()==aMode);
843 }
844
845 //=======================================================================
846 //function : SelectionModes
847 //purpose  : 
848 //=======================================================================
849
850 const TColStd_ListOfInteger& AIS_LocalContext::
851 SelectionModes(const Handle(AIS_InteractiveObject)& anObject) const 
852 {
853   return myActiveObjects(anObject)->SelectionModes(); 
854 }
855
856 //=======================================================================
857 //function : Status
858 //purpose  : 
859 //=======================================================================
860
861 TCollection_AsciiString AIS_LocalContext::Status() const 
862 {
863   TCollection_AsciiString t;
864   return t;
865 }
866
867 const Handle(AIS_LocalStatus)& AIS_LocalContext::Status(const Handle(AIS_InteractiveObject)& anObject) const 
868 {
869   return myActiveObjects(anObject);
870 }
871
872 //=======================================================================
873 //function : LoadContextObjects
874 //purpose  : 
875 //=======================================================================
876
877 void AIS_LocalContext::LoadContextObjects()
878 {
879   AIS_ListIteratorOfListOfInteractive It;
880   if(myLoadDisplayed) {
881     AIS_ListOfInteractive LL;
882     myCTX->DisplayedObjects(LL,Standard_True);
883     Handle(AIS_LocalStatus) Att;
884     for (It.Initialize(LL);It.More();It.Next()){
885       const Handle(AIS_InteractiveObject)& anObj = It.Value();
886       Att= new AIS_LocalStatus();
887       Att->SetDecomposition((anObj->AcceptShapeDecomposition() && myAcceptStdMode));
888       Att->SetTemporary(Standard_False);
889       Att->SetHilightMode(anObj->HasHilightMode()? anObj->HilightMode(): 0);
890       for (anObj->Init(); anObj->More(); anObj->Next())
891       {
892         const Handle(SelectMgr_Selection)& aSel = anObj->CurrentSelection();
893         aSel->SetSelectionState (SelectMgr_SOS_Deactivated);
894       }
895       myActiveObjects.Bind(anObj,Att);
896     }
897   }
898 }
899
900 void AIS_LocalContext::UnloadContextObjects()
901 {
902   AIS_ListIteratorOfListOfInteractive It;
903   if(myLoadDisplayed) 
904   {
905     AIS_ListOfInteractive LL;
906     myCTX->DisplayedObjects(LL,Standard_True);
907     
908     for (It.Initialize(LL);It.More();It.Next())
909     {
910       myActiveObjects.UnBind(It.Value());
911     }
912   }
913 }
914 //=======================================================================
915 //function : Process
916 //purpose  : 
917 //=======================================================================
918
919 void AIS_LocalContext::Process(const Handle(SelectMgr_SelectableObject)& anObject)
920
921   if(!myActiveObjects.IsBound(anObject)) return;
922   if(myActiveObjects(anObject)->Decomposed())
923     ActivateStandardModes(anObject);
924   else
925     {
926       TColStd_ListIteratorOfListOfInteger It(myActiveObjects(anObject)->SelectionModes());
927       for(;It.More();It.Next())
928         myCTX->SelectionManager()->Activate(anObject,It.Value(),myMainVS);
929     }
930 }
931
932 //=======================================================================
933 //function : Process
934 //purpose  : 
935 //=======================================================================
936
937 void AIS_LocalContext::Process()
938
939
940   myMainVS->Clear();
941   
942   AIS_DataMapIteratorOfDataMapOfSelStat It(myActiveObjects);
943   
944   for(;It.More();It.Next()){
945     myCTX->SelectionManager()->Load(It.Key(),myMainVS);
946     if(It.Value()->Decomposed()) 
947       ActivateStandardModes(It.Key());
948     else if( myCTX->GetAutoActivateSelection() )
949     {
950       It.Value()->AddSelectionMode(0);
951       myCTX->SelectionManager()->Activate(It.Key(),0,myMainVS);
952     }
953   }
954
955 }
956
957 //=======================================================================
958 //function : ActivateModes
959 //purpose  : 
960 //=======================================================================
961
962 void AIS_LocalContext::ActivateStandardModes(const Handle(SelectMgr_SelectableObject)& anObject)
963
964   if(!myActiveObjects.IsBound(anObject)) return;
965   
966   TColStd_ListIteratorOfListOfInteger itl (myListOfStandardMode);
967
968   const Handle(AIS_LocalStatus)&  LS = myActiveObjects(anObject);
969   if(LS->Decomposed()){
970     for(;itl.More();itl.Next()){
971       myCTX->SelectionManager()->Activate(anObject,itl.Value(),myMainVS);
972       LS->AddSelectionMode(itl.Value());
973     }
974   }
975 }
976
977
978 //=======================================================================
979 //function : ClearObjects
980 //purpose  : 
981 //=======================================================================
982
983 void AIS_LocalContext::ClearObjects()
984 {
985   AIS_DataMapIteratorOfDataMapOfSelStat It(myActiveObjects);
986   for(;It.More();It.Next())
987     {
988       Handle(AIS_InteractiveObject) SO =
989         Handle(AIS_InteractiveObject)::DownCast(It.Key());
990       
991       const Handle(AIS_LocalStatus)& CurAtt = It.Value();
992       //TColStd_ListIteratorOfListOfInteger ItL;
993       // if object is temporary the presentations managed by myMainPM are removed
994       AIS_DisplayStatus TheDS = myCTX->DisplayStatus(SO);
995       
996       if(TheDS != AIS_DS_Displayed){
997         if(myMainPM->IsDisplayed(SO,CurAtt->DisplayMode())){
998           if(CurAtt->IsSubIntensityOn()&&
999              myMainPM->IsHighlighted(SO,CurAtt->HilightMode()))
1000             myMainPM->Unhighlight(SO,CurAtt->HilightMode());
1001           myMainPM->Erase(SO,CurAtt->DisplayMode());
1002         }
1003         
1004         if(CurAtt->IsTemporary()){
1005           myMainPM->Erase(SO,CurAtt->DisplayMode());}
1006 //        myMainPM->Clear(SO,CurAtt->DisplayMode());}
1007       }
1008       else {
1009         if (CurAtt->IsSubIntensityOn()){
1010           myCTX->SubIntensityOff(Handle(AIS_InteractiveObject)::DownCast(SO));}
1011         Standard_Integer DiMo = SO->HasDisplayMode()?
1012           SO->DisplayMode():myCTX->DisplayMode();
1013         if(CurAtt->DisplayMode()!=-1 &&
1014            CurAtt->DisplayMode()!= DiMo)
1015           myMainPM->Erase(SO,CurAtt->DisplayMode());
1016       }
1017       
1018       TColStd_ListIteratorOfListOfInteger aSelModeIter (CurAtt->SelectionModes());
1019       for ( ; aSelModeIter.More(); aSelModeIter.Next())
1020       {
1021         Standard_Integer aSelMode = aSelModeIter.Value();
1022         mySM->Deactivate (SO, aSelMode, myMainVS);
1023       }
1024
1025     }
1026   ClearSelected( Standard_False );
1027
1028   // Clear selection structures for temporary objects, created in local context
1029   for (AIS_DataMapIteratorOfDataMapOfSelStat anIter (myActiveObjects); anIter.More(); anIter.Next())
1030   {
1031     if (anIter.Value()->IsTemporary())
1032     {
1033       mySM->Remove (anIter.Key(), myMainVS);
1034     }
1035   }
1036
1037   myActiveObjects.Clear();
1038 }
1039
1040
1041 Standard_Boolean AIS_LocalContext::IsDecompositionOn() const 
1042 {return !myListOfStandardMode.IsEmpty();}
1043
1044
1045
1046
1047 //=======================================================================
1048 //function : HasAlreadyFilters
1049 //purpose  : 
1050 //=======================================================================
1051
1052 Standard_Boolean AIS_LocalContext::
1053 HasFilters(const TopAbs_ShapeEnum aType) const 
1054 {
1055   return myFilters->ActsOn(aType);
1056 }
1057
1058 void AIS_LocalContext::ClearDetected()
1059 {
1060   for(Standard_Integer I=1;I<=myMapOfOwner->Extent();I++)
1061   {
1062     if(!myMapOfOwner->FindKey (I).IsNull())
1063     {
1064       if(myMapOfOwner->FindKey (I)->IsHilighted(myMainPM))
1065         myMapOfOwner->FindKey (I)->Unhilight(myMainPM);
1066       else
1067       {
1068         const Handle(SelectMgr_SelectableObject)& SO = 
1069           myMapOfOwner->FindKey (I)->Selectable();
1070         if(myActiveObjects.IsBound(SO))
1071         {
1072           const Handle(AIS_LocalStatus)& Att = myActiveObjects(SO);
1073
1074           if(Att->IsTemporary() &&
1075              Att->DisplayMode()==-1 && 
1076              Att->SelectionModes().IsEmpty())
1077           {
1078             myMapOfOwner->FindKey (I)->Clear(myMainPM);
1079           }
1080         }
1081       }
1082     }
1083   }
1084 }
1085
1086 //=======================================================================
1087 //function : BeginImmediateDraw
1088 //purpose  :
1089 //=======================================================================
1090 Standard_Boolean AIS_LocalContext::BeginImmediateDraw()
1091 {
1092   if (myMainPM->IsImmediateModeOn())
1093   {
1094     myMainPM->BeginImmediateDraw();
1095     return Standard_True;
1096   }
1097   return Standard_False;
1098 }
1099
1100 //=======================================================================
1101 //function : ImmediateAdd
1102 //purpose  :
1103 //=======================================================================
1104 Standard_Boolean AIS_LocalContext::ImmediateAdd (const Handle(AIS_InteractiveObject)& theObj,
1105                                                  const Standard_Integer               theMode)
1106 {
1107   if (!myMainPM->IsImmediateModeOn())
1108   {
1109     return Standard_False;
1110   }
1111
1112   myMainPM->AddToImmediateList (myMainPM->Presentation (theObj, theMode)->Presentation());
1113   return Standard_True;
1114 }
1115
1116 //=======================================================================
1117 //function : EndImmediateDraw
1118 //purpose  :
1119 //=======================================================================
1120 Standard_Boolean AIS_LocalContext::EndImmediateDraw (const Handle(V3d_Viewer)& theViewer)
1121 {
1122   if (!myMainPM->IsImmediateModeOn())
1123   {
1124     return Standard_False;
1125   }
1126
1127   myMainPM->EndImmediateDraw (theViewer);
1128   return Standard_True;
1129 }
1130
1131 // =======================================================================
1132 // function : ClearImmediateDraw
1133 // purpose  :
1134 // =======================================================================
1135 void AIS_LocalContext::ClearImmediateDraw()
1136 {
1137   myMainPM->ClearImmediateDraw();
1138 }
1139
1140 //=======================================================================
1141 //function : IsImmediateModeOn
1142 //purpose  :
1143 //=======================================================================
1144 Standard_Boolean AIS_LocalContext::IsImmediateModeOn() const
1145 {
1146   return myMainPM->IsImmediateModeOn();
1147 }
1148
1149 void AIS_LocalContext::SetPixelTolerance(const Standard_Integer aPrecision) {
1150
1151   myMainVS->SetPixelTolerance(aPrecision);
1152 }
1153
1154 Standard_Integer AIS_LocalContext::PixelTolerance() const {
1155
1156   return myMainVS->PixelTolerance();
1157 }
1158
1159 //=======================================================================
1160 //function : SetSelectionSensitivity
1161 //purpose  : Allows to manage sensitivity of a particular selection of interactive object theObject
1162 //=======================================================================
1163 void AIS_LocalContext::SetSelectionSensitivity (const Handle(AIS_InteractiveObject)& theObject,
1164                                                 const Standard_Integer theMode,
1165                                                 const Standard_Integer theNewSensitivity)
1166 {
1167   mySM->SetSelectionSensitivity (theObject, theMode, theNewSensitivity);
1168 }