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