0024023: Revamp the OCCT Handle -- general
[occt.git] / src / AIS / AIS_InteractiveContext_2.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 #include <AIS_InteractiveContext.jxx>
18
19 #include <AIS_LocalContext.hxx>
20
21 #include <AIS_GlobalStatus.hxx>
22 #include <TColStd_ListIteratorOfListOfInteger.hxx>
23
24 #include <AIS_DataMapIteratorOfDataMapOfIOStatus.hxx>
25 #include <AIS_DataMapIteratorOfDataMapOfILC.hxx>
26 #include <Graphic3d_StructureManager.hxx>
27 #include <Graphic3d_Structure.hxx>
28 #include <Graphic3d_MapOfStructure.hxx>
29 #include <Graphic3d_MapIteratorOfMapOfStructure.hxx>
30 #include <AIS_Selection.hxx>
31
32 #include <StdSelect_ViewerSelector3d.hxx>
33
34 //=======================================================================
35 //function : OpenLocalContext
36 //purpose  : 
37 //=======================================================================
38
39 Standard_Integer AIS_InteractiveContext::
40 OpenLocalContext(const Standard_Boolean UseDisplayedObjects, 
41                  const Standard_Boolean AllowShapeDecomposition, 
42                  const Standard_Boolean AcceptEraseOfTemporary,
43                  const Standard_Boolean /*BothViewers*/)
44 {
45
46   // the entities eventually detected just before the context was opened are unhighlighted...
47   if(!IsCurrent(myLastPicked)){
48     if(!myLastPicked.IsNull()){
49       Standard_Integer HiMod = myLastPicked->HasHilightMode()?myLastPicked->HilightMode():0;
50       myMainPM->Unhighlight(myLastPicked,HiMod);
51     }}
52   
53   if(!mylastmoveview.IsNull()){
54     if(myCurLocalIndex>0)
55       myLocalContexts(myCurLocalIndex)->UnhilightLastDetected(mylastmoveview);
56   }
57   
58   // entities connected to dynamic selection at neutral point are set to 0.
59   
60   myLastinMain.Nullify();
61   myLastPicked.Nullify();
62   myWasLastMain = Standard_True;
63
64   myCurLocalIndex = HighestIndex() + 1;
65   
66   Handle(AIS_LocalContext) NewLocal= new AIS_LocalContext(this,myCurLocalIndex,
67                                                           UseDisplayedObjects,
68                                                           AllowShapeDecomposition,
69                                                           AcceptEraseOfTemporary);
70   // the AIS_LocalContext bind itself to myLocalContexts
71   // because procedures performed in AIS_LocalContext constructor
72   // already may access myLocalContexts(myCurLocalIndex) (like methods AIS_LocalContext::IsSelected()).
73
74 #ifdef OCCT_DEBUG
75   cout<<"\tOpen Local Context No "<<myCurLocalIndex<<endl;
76   if(UseDisplayedObjects){
77     cout<<"\t\tObjects from Neutral Point loaded"<<endl;
78     if(AllowShapeDecomposition)
79       cout<<"\t\tDecomposition Authorized for Loaded Shapes"<<endl;
80     else
81       cout<<"\t\tNo Decomposition Authorized for Loaded Shapes"<<endl;
82   }
83   else
84     cout<<"\t\tNo Objects Were Loaded "<<endl;
85 #endif
86   return myCurLocalIndex;
87 }
88
89 //=======================================================================
90 //function : CloseLocalContext
91 //purpose  : 
92 //=======================================================================
93
94 void AIS_InteractiveContext::CloseLocalContext(const Standard_Integer Index,
95                                                const Standard_Boolean updateviewer)
96 {
97
98  Standard_Boolean debugmode(Standard_False);
99 #ifdef OCCT_DEBUG
100  debugmode = Standard_True;
101 #endif
102  
103  Standard_Integer GoodIndex = (Index ==-1) ? myCurLocalIndex : Index;
104
105  if(debugmode) cout<<"Call to CloseLocalContext - Index  "<<GoodIndex<<endl;
106  if(!HasOpenedContext()){
107    if(debugmode) cout<<"\t But No Local Context is opened"<<endl;
108    return;
109  }
110  if(!myLocalContexts.IsBound(GoodIndex)) {
111    if(debugmode) cout<<" Attempt to Close a non-existent Local Context"<<endl;
112    return;
113  }
114  
115  // the only open local context is closed...
116  if(myLocalContexts.Extent()==1 && GoodIndex == myCurLocalIndex){
117    
118    myLocalContexts(myCurLocalIndex)->Terminate( updateviewer );
119    myLocalContexts.UnBind(myCurLocalIndex);
120    myCurLocalIndex = 0;
121
122    ResetOriginalState(Standard_False);
123    if(debugmode)
124      cout<<"No More Opened Local Context "<<endl;
125  }
126  
127  // Otherwise the local context will be still open after the current is closed
128  else{
129    Handle(StdSelect_ViewerSelector3d) VS = myLocalContexts(GoodIndex)->MainSelector();
130    myLocalContexts(GoodIndex)->Terminate();
131    myLocalContexts.UnBind(GoodIndex);
132    // the current is closed...
133    if(GoodIndex==myCurLocalIndex){
134      myCurLocalIndex = HighestIndex();
135    }
136    else if(debugmode)
137      cout<<"a No Current Local Context WasClosed"<<endl;
138    
139    if(debugmode) cout<<"Index Of CurrentLocalContext:"<<myCurLocalIndex<<endl;
140    
141  }
142    
143  
144  if(updateviewer) myMainVwr->Update();
145 }
146
147 //=======================================================================
148 //function : CloseAllContexts
149 //purpose  : 
150 //=======================================================================
151
152 void AIS_InteractiveContext::CloseAllContexts(const Standard_Boolean updateviewer)
153 {
154   
155   while(!myLocalContexts.IsEmpty()){
156     CloseLocalContext(myCurLocalIndex,Standard_False);
157   }
158   
159   ResetOriginalState(Standard_False);
160
161   if(updateviewer) myMainVwr->Update();
162 }
163
164 //=======================================================================
165 //function : IndexOfCurrentLocal
166 //purpose  : 
167 //=======================================================================
168
169 Standard_Integer AIS_InteractiveContext::IndexOfCurrentLocal() const
170 {
171   return myCurLocalIndex;
172 }
173
174 //=======================================================================
175 //function : ClearLocalContext
176 //purpose  : 
177 //=======================================================================
178
179 void AIS_InteractiveContext::ClearLocalContext(const AIS_ClearMode aMode)
180 {
181   if (!HasOpenedContext()) return;
182   myLocalContexts(myCurLocalIndex)->Clear(aMode);
183
184 }
185
186 //=======================================================================
187 //function : HighestIndex
188 //purpose  : 
189 //=======================================================================
190
191 Standard_Integer AIS_InteractiveContext::HighestIndex() const
192 {
193   AIS_DataMapIteratorOfDataMapOfILC It(myLocalContexts);
194   Standard_Integer HiInd = 0;
195   for(;It.More();It.Next())
196     HiInd = (It.Key()>HiInd) ? It.Key() : HiInd;
197   return HiInd;
198
199 }
200
201
202 //=======================================================================
203 //function : Activate
204 //purpose  : 
205 //=======================================================================
206
207 void AIS_InteractiveContext::
208 Activate(const Handle(AIS_InteractiveObject)& anIObj, 
209          const Standard_Integer aMode,
210          const Standard_Boolean theIsForce)
211 {
212   if(!HasOpenedContext()){
213     if(!myObjects.IsBound(anIObj)) return;
214     const Handle(AIS_GlobalStatus)& STAT = myObjects(anIObj);
215     if(STAT->GraphicStatus()==AIS_DS_Displayed || theIsForce)
216       mgrSelector->Activate(anIObj,aMode,myMainSel);
217     STAT ->AddSelectionMode(aMode);
218   }
219   else{
220    myLocalContexts(myCurLocalIndex)->ActivateMode(anIObj,aMode);
221   }
222 }
223
224 //=======================================================================
225 //function : LocalSelector
226 //purpose  : 
227 //=======================================================================
228 Handle( StdSelect_ViewerSelector3d ) AIS_InteractiveContext::LocalSelector() const
229 {
230   if( !HasOpenedContext() )
231       return Handle( StdSelect_ViewerSelector3d )();
232   else
233       return myLocalContexts( myCurLocalIndex )->MainSelector();
234 }
235
236
237 //=======================================================================
238 //function : DeActivate
239 //purpose  : 
240 //=======================================================================
241 void AIS_InteractiveContext::
242 Deactivate(const Handle(AIS_InteractiveObject)& anIObj)
243 {
244   if(!HasOpenedContext()){
245     if(!myObjects.IsBound(anIObj)) return;
246     TColStd_ListIteratorOfListOfInteger ItL;
247     for(ItL.Initialize(myObjects(anIObj)->SelectionModes());
248         ItL.More();
249         ItL.Next()){
250       if(myObjects(anIObj)->GraphicStatus() == AIS_DS_Displayed)
251         mgrSelector->Deactivate(anIObj,ItL.Value(),myMainSel);
252     }
253     myObjects(anIObj)->ClearSelectionModes();
254   }
255   else{
256     const Handle(AIS_LocalContext)& LC = myLocalContexts(myCurLocalIndex);
257     LC->Deactivate(anIObj);
258   }
259 }
260
261 //=======================================================================
262 //function : Deactivate
263 //purpose  : 
264 //=======================================================================
265
266 void AIS_InteractiveContext::Deactivate(const Handle(AIS_InteractiveObject)& anIObj, 
267            const Standard_Integer aMode)
268 {
269   if(!HasOpenedContext()){
270     if(!myObjects.IsBound(anIObj)) return;
271     const Handle(AIS_GlobalStatus)& STAT = myObjects(anIObj);
272
273     if(STAT->GraphicStatus() == AIS_DS_Displayed)
274       mgrSelector->Deactivate(anIObj,aMode,myMainSel);
275     STAT->RemoveSelectionMode(aMode);
276   }
277   else{
278    myLocalContexts(myCurLocalIndex)->DeactivateMode(anIObj,aMode);
279   }
280 }
281
282 //=======================================================================
283 //function : ActivatedModes
284 //purpose  : 
285 //=======================================================================
286
287 void AIS_InteractiveContext::
288 ActivatedModes(const Handle(AIS_InteractiveObject)& anIObj, 
289                TColStd_ListOfInteger& theList) const 
290 {
291   TColStd_ListIteratorOfListOfInteger ItL;
292   if(!HasOpenedContext()){
293     if(myObjects.IsBound(anIObj)){
294       for(ItL.Initialize(myObjects(anIObj)->SelectionModes());
295           ItL.More();
296           ItL.Next())
297         theList.Append(ItL.Value());
298       
299     }
300   }
301   else{
302     if(myLocalContexts(myCurLocalIndex)->IsIn(anIObj)){
303       for(ItL.Initialize(myLocalContexts(myCurLocalIndex)->SelectionModes(anIObj));
304           ItL.More();
305           ItL.Next())
306         theList.Append(ItL.Value());
307     }
308   }
309 }
310
311 //=======================================================================
312 //function : SetShapeDecomposition
313 //purpose  : 
314 //=======================================================================
315
316 void AIS_InteractiveContext::SetShapeDecomposition(const Handle(AIS_InteractiveObject)& anIObj,
317                                                    const Standard_Boolean StdModeSensitive)
318 {
319   if(!HasOpenedContext()) return;
320   myLocalContexts(myCurLocalIndex)->SetShapeDecomposition(anIObj,StdModeSensitive);
321 }
322
323 //=======================================================================
324 //function : SetTemporaryAttributes
325 //purpose  : 
326 //=======================================================================
327
328 void AIS_InteractiveContext::
329 SetTemporaryAttributes(const Handle(AIS_InteractiveObject)& /*anObj*/,
330                        const Handle(Prs3d_Drawer)& /*aDrawer*/,
331                        const Standard_Boolean /*updateviewer*/)
332 {
333 }
334
335 //=======================================================================
336 //function : SubIntensityOn
337 //purpose  : 
338 //=======================================================================
339 void AIS_InteractiveContext::
340 SubIntensityOn(const Handle(AIS_InteractiveObject)& anIObj,
341                const Standard_Boolean updateviewer)
342 {
343   if(!HasOpenedContext()){
344     if(!myObjects.IsBound(anIObj))
345       return;
346     const Handle(AIS_GlobalStatus)& GB=myObjects(anIObj);
347     if(GB->IsSubIntensityOn())
348       return;
349     GB->SubIntensityOn();
350     Standard_Boolean UpdMain(Standard_False);
351     
352     for(TColStd_ListIteratorOfListOfInteger It(GB->DisplayedModes());It.More();It.Next()){
353       if (GB->GraphicStatus()==AIS_DS_Displayed)
354       {
355         myMainPM->Color(anIObj,mySubIntensity,It.Value());
356         UpdMain = Standard_True;
357       }
358     }
359     if(updateviewer){
360       if(UpdMain)
361         myMainVwr->Update();
362     }
363   }
364   else {
365     if(myObjects.IsBound(anIObj)){
366       const Handle(AIS_GlobalStatus)& STAT = myObjects(anIObj);
367       STAT->SubIntensityOn();
368       TColStd_ListIteratorOfListOfInteger ItL;
369       for (ItL.Initialize(STAT->DisplayedModes());ItL.More();ItL.Next())
370         myMainPM->Color(anIObj,mySubIntensity,ItL.Value());
371     }
372     else
373       myLocalContexts(myCurLocalIndex)->SubIntensityOn(anIObj);
374     
375     if(updateviewer) myMainVwr->Update();
376   }
377 }
378 //=======================================================================
379 //function : SubIntensityOff
380 //purpose  : 
381 //=======================================================================
382
383 void AIS_InteractiveContext::
384 SubIntensityOff(const Handle(AIS_InteractiveObject)& anIObj,
385                 const Standard_Boolean updateviewer)
386 {
387   if(!HasOpenedContext()){
388     if(!myObjects.IsBound(anIObj))
389       return;
390     const Handle(AIS_GlobalStatus)& GB=myObjects(anIObj);
391     if(!GB->IsSubIntensityOn())
392       return;
393     GB->SubIntensityOff();
394     Standard_Boolean UpdMain(Standard_False);
395     
396     for(TColStd_ListIteratorOfListOfInteger It(GB->DisplayedModes());It.More();It.Next()){
397       if(GB->GraphicStatus()==AIS_DS_Displayed)
398       {
399         myMainPM->Unhighlight(anIObj,It.Value());
400         UpdMain = Standard_True;
401       }
402     }
403     
404     Standard_Integer DM,HM,SM;
405     GetDefModes(anIObj,DM,HM,SM);
406     if(AIS_Selection::IsSelected(anIObj))
407       myMainPM->Highlight(anIObj,HM);
408     
409     if(updateviewer){
410       if(UpdMain)
411         myMainVwr->Update();
412     }
413   }
414   else {
415     if(myObjects.IsBound(anIObj)){
416       const Handle(AIS_GlobalStatus)& STAT = myObjects(anIObj);
417       STAT->SubIntensityOff();
418       TColStd_ListIteratorOfListOfInteger ItL;
419       for (ItL.Initialize(STAT->DisplayedModes());ItL.More();ItL.Next())
420         myMainPM->Unhighlight(anIObj,ItL.Value());
421       if(STAT->IsHilighted())
422         Hilight(anIObj);
423     }
424     else
425       myLocalContexts(myCurLocalIndex)->SubIntensityOff(anIObj);
426     if(IsSelected(anIObj))
427       Hilight(anIObj);
428     
429     if(updateviewer) myMainVwr->Update();
430   }
431 }
432
433 //=======================================================================
434 //function : SubIntensityOn
435 //purpose  : ALL THE DISPLAYED OBJECTS HAVE SUBINTENSITY...
436 //=======================================================================
437
438 void AIS_InteractiveContext::SubIntensityOn(const Standard_Boolean updateviewer)
439 {
440   if(!HasOpenedContext()) return;
441   
442   AIS_DataMapIteratorOfDataMapOfIOStatus It (myObjects);
443   TColStd_ListIteratorOfListOfInteger ItM;
444   for(;It.More();It.Next()){
445     const Handle(AIS_GlobalStatus)& STAT = It.Value();
446     if(STAT->GraphicStatus()==AIS_DS_Displayed)
447       {
448         STAT->SubIntensityOn();
449         for(ItM.Initialize(STAT->DisplayedModes());ItM.More();ItM.Next())
450           {myMainPM->Color(It.Key(),mySubIntensity,ItM.Value());}
451       }
452   }
453   if(updateviewer) myMainVwr->Update();
454 }
455
456 //=======================================================================
457 //function : SubIntensityOff
458 //purpose  : 
459 //=======================================================================
460 void AIS_InteractiveContext::SubIntensityOff(const Standard_Boolean updateviewer)
461 {
462   if(!HasOpenedContext()) return;
463
464   AIS_DataMapIteratorOfDataMapOfIOStatus It (myObjects);
465   TColStd_ListIteratorOfListOfInteger ItL;
466   for(;It.More();It.Next()){
467     const Handle(AIS_GlobalStatus)& STAT = It.Value();
468     if(STAT->IsSubIntensityOn())
469       STAT->SubIntensityOff();
470     for(ItL.Initialize(STAT->DisplayedModes());ItL.More();ItL.Next())
471       myMainPM->Unhighlight(It.Key());
472   }
473
474   if(updateviewer) myMainVwr->Update();
475 }
476
477 //=======================================================================
478 //function : AddFilter
479 //purpose  : 
480 //=======================================================================
481 void AIS_InteractiveContext::AddFilter(const Handle(SelectMgr_Filter)& aFilter)
482 {
483   if(HasOpenedContext())
484     myLocalContexts(myCurLocalIndex)->AddFilter(aFilter);
485   else
486     myFilters->Add(aFilter);
487 }
488
489 //=======================================================================
490 //function : ActivateStandardMode
491 //purpose  : 
492 //=======================================================================
493 void AIS_InteractiveContext::ActivateStandardMode(const TopAbs_ShapeEnum aStandardActivation)
494 {
495   if(!HasOpenedContext()) return;
496   myLocalContexts(myCurLocalIndex)->ActivateStandardMode (aStandardActivation);
497 }
498
499 //=======================================================================
500 //function : DeActivateStandardMode
501 //purpose  : 
502 //=======================================================================
503 void AIS_InteractiveContext::DeactivateStandardMode(const TopAbs_ShapeEnum aStandardActivation)
504 {
505   if(!HasOpenedContext()) return;
506   myLocalContexts(myCurLocalIndex)->DeactivateStandardMode (aStandardActivation);
507 }
508
509 //=======================================================================
510 //function : RemoveFilter
511 //purpose  : 
512 //=======================================================================
513 void AIS_InteractiveContext::RemoveFilter(const Handle(SelectMgr_Filter)& aFilter)
514 {
515   if(HasOpenedContext())
516     myLocalContexts(myCurLocalIndex)->RemoveFilter (aFilter);
517   else
518     myFilters->Remove(aFilter);
519 }
520
521 //=======================================================================
522 //function : RemoveFilters
523 //purpose  : 
524 //=======================================================================
525
526 void AIS_InteractiveContext::RemoveFilters()
527 {
528   if(!HasOpenedContext())
529     myFilters->Clear();
530   else
531     myLocalContexts(myCurLocalIndex)->Clear(AIS_CM_Filters);
532 }
533
534 //=======================================================================
535 //function : ActivatedStandardModes
536 //purpose  : 
537 //=======================================================================
538 const TColStd_ListOfInteger& AIS_InteractiveContext::ActivatedStandardModes() const 
539 {
540   return myLocalContexts(myCurLocalIndex)->StandardModes();
541 }
542
543 //=======================================================================
544 //function : Filters
545 //purpose  : 
546 //=======================================================================
547 const SelectMgr_ListOfFilter& AIS_InteractiveContext::Filters() const 
548 {
549   if(HasOpenedContext())
550     return myLocalContexts(myCurLocalIndex)->ListOfFilter();
551   return myFilters->StoredFilters();
552 }
553
554 //=======================================================================
555 //function : DisplayActiveSensitive
556 //purpose  : 
557 //=======================================================================
558 void AIS_InteractiveContext::DisplayActiveSensitive(const Handle(V3d_View)& aviou)
559 {
560   if(HasOpenedContext())
561     myLocalContexts(myCurLocalIndex)->DisplaySensitive(aviou);
562   else
563     myMainSel->DisplaySensitive(aviou);
564 }
565 //=======================================================================
566 //function : DisplayActiveSensitive
567 //purpose  : 
568 //=======================================================================
569
570 void AIS_InteractiveContext::DisplayActiveSensitive(const Handle(AIS_InteractiveObject)& anIObj,
571                                                     const Handle(V3d_View)& aviou)
572 {
573   TColStd_ListIteratorOfListOfInteger It;
574   Handle(StdSelect_ViewerSelector3d) VS;
575   if(HasOpenedContext()){
576     const Handle(AIS_LocalContext)& LC = myLocalContexts(myCurLocalIndex);
577     if(!LC->IsIn(anIObj)) return;
578     It.Initialize(LC->SelectionModes(anIObj));
579     VS = LC->MainSelector();
580   }
581   else{
582     if(!myObjects.IsBound(anIObj)) return;
583     It.Initialize(myObjects(anIObj)->SelectionModes());
584     VS = myMainSel;
585   }
586   
587   
588   for(;It.More();It.Next()){
589     const Handle(SelectMgr_Selection)& Sel = anIObj->Selection(It.Value());
590     VS->DisplaySensitive(Sel,anIObj->Transformation(), aviou,Standard_False);
591   }  
592   
593 }
594
595 //=======================================================================
596 //function : ClearActiveSensitive
597 //purpose  : 
598 //=======================================================================
599 void AIS_InteractiveContext::ClearActiveSensitive(const Handle(V3d_View)& aviou)
600 {
601   
602   if(HasOpenedContext())
603     myLocalContexts(myCurLocalIndex)->ClearSensitive(aviou);
604   else
605     myMainSel->ClearSensitive(aviou);
606 }
607
608 //=======================================================================
609 //function : SetAutomaticHilight
610 //purpose  : 
611 //=======================================================================
612 void  AIS_InteractiveContext::SetAutomaticHilight(const Standard_Boolean aStatus)
613 {
614
615   if(HasOpenedContext())
616     myLocalContexts(myCurLocalIndex)->SetAutomaticHilight(aStatus);
617 }
618
619 //=======================================================================
620 //function : AutomaticHilight
621 //purpose  : 
622 //=======================================================================
623 Standard_Boolean AIS_InteractiveContext::AutomaticHilight() const 
624 {
625   if(HasOpenedContext())
626     return myLocalContexts(myCurLocalIndex)->AutomaticHilight();
627   return Standard_True;
628 }
629
630 //=======================================================================
631 //function : UseDisplayedObjects
632 //purpose  : 
633 //=======================================================================
634
635 void AIS_InteractiveContext::UseDisplayedObjects()
636 {
637   if(HasOpenedContext())
638     myLocalContexts(myCurLocalIndex)->LoadContextObjects();
639 }
640
641 //=======================================================================
642 //function : NotUseDisplayedObjects
643 //purpose  : 
644 //=======================================================================
645
646 void AIS_InteractiveContext::NotUseDisplayedObjects()
647 {
648   if(HasOpenedContext())
649     myLocalContexts(myCurLocalIndex)->UnloadContextObjects();
650 }
651
652
653
654
655 //=======================================================================
656 //function : PurgeDisplay
657 //purpose  : 
658 //=======================================================================
659
660 Standard_Integer AIS_InteractiveContext::PurgeDisplay()
661 {
662   if(HasOpenedContext()) return 0;
663   
664   Standard_Integer NbStr = PurgeViewer(myMainVwr);
665   myMainVwr->Update();
666   return NbStr;
667
668 }
669
670
671 //=======================================================================
672 //function : PurgeViewer
673 //purpose  : 
674 //=======================================================================
675 Standard_Integer AIS_InteractiveContext::PurgeViewer(const Handle(V3d_Viewer)& Vwr)
676 {
677   Handle(Graphic3d_StructureManager) GSM = Vwr->Viewer();
678   Standard_Integer NbCleared(0);
679   Graphic3d_MapOfStructure SOS;
680   GSM->DisplayedStructures(SOS);
681
682   Handle(Graphic3d_Structure) G;
683   for(Graphic3d_MapIteratorOfMapOfStructure It(SOS); It.More();It.Next()){
684     G = It.Key();
685     Standard_Address Add = G->Owner();
686     if(Add==NULL){
687       G->Erase();
688       G->Clear();// it means that it is not referenced as a presentation of InterfactiveObject...
689       NbCleared++;
690     }
691     Handle(AIS_InteractiveObject) IO = (AIS_InteractiveObject*)Add;
692     if(!myObjects.IsBound(IO)){
693       G->Erase();
694       NbCleared++;
695     }
696   }
697   return NbCleared;
698 }
699
700 //=======================================================================
701 //function : IsImmediateModeOn
702 //purpose  :
703 //=======================================================================
704
705 Standard_Boolean AIS_InteractiveContext::IsImmediateModeOn()  const 
706 {
707   if(!HasOpenedContext()) return Standard_False;
708   return myLocalContexts(myCurLocalIndex)->IsImmediateModeOn();
709 }
710
711 //=======================================================================
712 //function : BeginImmediateDraw
713 //purpose  :
714 //=======================================================================
715
716 Standard_Boolean AIS_InteractiveContext::BeginImmediateDraw()
717 {
718   return HasOpenedContext()
719       && myLocalContexts (myCurLocalIndex)->BeginImmediateDraw();
720 }
721
722 //=======================================================================
723 //function : ImmediateAdd
724 //purpose  :
725 //=======================================================================
726
727 Standard_Boolean AIS_InteractiveContext::ImmediateAdd (const Handle(AIS_InteractiveObject)& theObj,
728                                                        const Standard_Integer               theMode)
729 {
730   return HasOpenedContext()
731       && myLocalContexts (myCurLocalIndex)->ImmediateAdd (theObj, theMode);
732 }
733
734 //=======================================================================
735 //function : EndImmediateDraw
736 //purpose  :
737 //=======================================================================
738
739 Standard_Boolean AIS_InteractiveContext::EndImmediateDraw (const Handle(V3d_View)& theView)
740 {
741   return HasOpenedContext()
742       && myLocalContexts (myCurLocalIndex)->EndImmediateDraw (theView);
743 }
744
745 //=======================================================================
746 //function : EndImmediateDraw
747 //purpose  :
748 //=======================================================================
749
750 Standard_Boolean AIS_InteractiveContext::EndImmediateDraw()
751 {
752   if (!HasOpenedContext())
753   {
754     return Standard_False;
755   }
756
757   myMainVwr->InitActiveViews();
758   if (!myMainVwr->MoreActiveViews())
759   {
760     return Standard_False;
761   }
762
763   Handle(V3d_View) aView = myMainVwr->ActiveView();
764   return myLocalContexts (myCurLocalIndex)->EndImmediateDraw (aView);
765 }
766
767
768 //=======================================================================
769 //function : ResetOriginalState
770 //purpose  : 
771 //=======================================================================
772
773 void AIS_InteractiveContext::ResetOriginalState(const Standard_Boolean updateviewer)
774 {
775   Standard_Boolean upd_main(Standard_False);
776   TColStd_ListIteratorOfListOfInteger itl;
777   myMainSel->ResetSelectionActivationStatus();
778
779   for (AIS_DataMapIteratorOfDataMapOfIOStatus it(myObjects);it.More();it.Next()){
780     const Handle(AIS_InteractiveObject)& iobj = it.Key();
781     const Handle(AIS_GlobalStatus)& STAT = it.Value();
782     switch(STAT->GraphicStatus()){
783     case AIS_DS_Displayed:{
784       upd_main = Standard_True;
785       
786       // part display...
787       for(itl.Initialize(STAT->DisplayedModes());itl.More();itl.Next())
788         myMainPM->Display(iobj,itl.Value());
789       if(STAT->IsHilighted()){
790         if(STAT->HilightColor()!=Quantity_NOC_WHITE)
791           HilightWithColor(iobj,STAT->HilightColor(),Standard_False);
792         else
793           Hilight(iobj,Standard_False);
794       }
795       //part selection
796       for(itl.Initialize(STAT->SelectionModes());itl.More();itl.Next()){
797         if(itl.Value()!=-1)
798           mgrSelector->Activate(iobj,itl.Value(),myMainSel);
799       }
800       break; 
801     }
802     case AIS_DS_Erased:{
803       EraseGlobal(iobj,Standard_False);
804       break;
805     }
806     default:
807       break;
808     }
809   }
810   if(updateviewer){
811     if(upd_main) 
812       myMainVwr->Update();
813   }
814 }
815
816 //=======================================================================
817 //function : SetZDetection
818 //purpose  : 
819 //=======================================================================
820 void  AIS_InteractiveContext::SetZDetection(const Standard_Boolean aStatus)
821 {
822   myZDetectionFlag = aStatus;
823 }
824
825 //=======================================================================
826 //function : ZDetection 
827 //purpose  : 
828 //=======================================================================
829 Standard_Boolean AIS_InteractiveContext::ZDetection() const 
830 {
831   return myZDetectionFlag;
832 }