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