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