0024171: Eliminate CLang compiler warning -Wreorder
[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-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21 //Modified by ROB : Traque des UpdateConversion intempestifs.
22
23 #define BUC60688       //GG 25/05/00 Add SetSensitivity() methods.
24
25 #define BUC60722        //GG_040900 Disable detection on an unviewable object
26
27 #define IMP160701       //SZV Add InitDetected(),MoreDetected(),NextDetected(),
28 //                       DetectedCurrentShape(),DetectedCurrentObject()
29 //                       methods
30
31 #define ALE70590        //GG  Avoid raise especially under W2000-SP2
32 //              when opening many local context due to a
33 //              system error in the selection name computation routine.
34 //              Many thanks to Philippe CARRET for the helpfull he has 
35 //              give to accelerate the resolution of this problem.
36
37 #include <AIS_LocalContext.ixx>
38 #include <SelectMgr_OrFilter.hxx>
39 #include <SelectMgr_CompositionFilter.hxx>
40 #include <AIS_LocalStatus.hxx>
41 #include <AIS_Shape.hxx>
42 #include <TColStd_ListIteratorOfListOfInteger.hxx>
43 #include <AIS_ListIteratorOfListOfInteractive.hxx>
44 #include <AIS_ListOfInteractive.hxx>
45 #include <AIS_DataMapIteratorOfDataMapOfSelStat.hxx>
46 #include <TopAbs_ShapeEnum.hxx>
47 #include <Graphic3d_Structure.hxx>
48 #include <Prs3d_LineAspect.hxx>
49 #include <Prs3d_PlaneAspect.hxx>
50 #include <Prs3d_PointAspect.hxx>
51 #include <Prs3d_Presentation.hxx>
52 #include <Aspect_TypeOfMarker.hxx>
53 #include <StdSelect_ShapeTypeFilter.hxx>
54 #include <AIS_Selection.hxx>
55 #include <V3d_Viewer.hxx>
56 #include <V3d_View.hxx>
57 #include <Visual3d_TransientManager.hxx>
58 #include <Visual3d_View.hxx>
59
60 #ifdef ALE70590
61 #include <stdio.h>
62 #else
63 #include <Standard_SStream.hxx>
64 #endif
65
66 static TCollection_AsciiString AIS_Local_SelName(const Standard_Address address,
67                                                  const Standard_Integer anIndex)
68 {
69 //  TCollection_AsciiString SelName;
70 #ifdef ALE70590
71   char string[100];
72   sprintf(string,"%p_%d", address, anIndex);    // works under any system 
73   TCollection_AsciiString SelName(string);
74 #else
75   Standard_SStream stream;
76   stream<<address;      // something is wrong here using the SStream because
77 //              the following access to rdbuf crash for an unknown reason 
78 //              especially under W2000 with SP2 and sometime under WNT and W98.
79 //              NOTE that stream is not ended by a NULL char and it's probably
80 //              one of the reasons why this crash.
81 //              In any case the resulting ascii string give a wrong and random
82 //              name under WINDOWS !
83   TCollection_AsciiString SelName(stream.rdbuf()->str());
84 //  SelName = TCollection_AsciiString("AIS_Local_");
85   TCollection_AsciiString theind(anIndex);
86   SelName += "_";
87   SelName += theind;
88 #endif
89   return SelName;
90 }
91
92
93 //=======================================================================
94 //function : AIS_LocalContext
95 //purpose  : 
96 //=======================================================================
97
98
99 AIS_LocalContext::AIS_LocalContext(){}
100
101 AIS_LocalContext::AIS_LocalContext(const Handle(AIS_InteractiveContext)& aCtx,
102                                    const Standard_Integer Index,
103                                    const Standard_Boolean LoadDisplayed,
104                                    const Standard_Boolean AcceptStandardModes,
105                                    const Standard_Boolean AcceptEraseOfTemp,
106                                    const Standard_Boolean /*BothViewers*/):
107 myCTX(aCtx),
108 myLoadDisplayed(LoadDisplayed),
109 myAcceptStdMode(AcceptStandardModes),
110 myAcceptErase(AcceptEraseOfTemp),
111 mySM(aCtx->SelectionManager()),
112 myMainVS(new StdSelect_ViewerSelector3d(aCtx->MainSelector()->Projector())),
113 myFilters(new SelectMgr_OrFilter()),
114 myAutoHilight(Standard_True),
115 mylastindex(0),
116 mylastgood(0),
117 myCurDetected(0)
118 #ifdef IMP160701
119 ,myAISCurDetected(0)
120 #endif
121 {
122   // bind self to AIS_InteractiveContext::myLocalContexts. Further, the
123   // constructor executes logic that implies that the context is already
124   // created and mapped.
125   aCtx->myLocalContexts.Bind (Index, this);
126
127   myMainPM = aCtx->MainPrsMgr();
128   mySelName = AIS_Local_SelName(this, Index);
129   AIS_Selection::CreateSelection(mySelName.ToCString());
130
131   mySM->Add(myMainVS);
132   if(myLoadDisplayed) LoadContextObjects();
133   Process(Standard_False);
134
135 }
136
137
138 //=======================================================================
139 //function : SetContext
140 //purpose  : 
141 //=======================================================================
142
143 void AIS_LocalContext::SetContext(const Handle(AIS_InteractiveContext)& aCtx)
144 {  myCTX = aCtx;}
145
146 //=======================================================================
147 //function : Display
148 //purpose  : 
149 //=======================================================================
150
151 Standard_Boolean AIS_LocalContext::Display(const Handle(AIS_InteractiveObject)& anInteractive,
152                                            const Standard_Integer WhichMode,
153                                            const Standard_Boolean AllowShapeDecomposition,
154                                            const Standard_Integer ActivationMode)
155 {
156   if(myActiveObjects.IsBound(anInteractive)){
157     const Handle(AIS_LocalStatus)& STAT = myActiveObjects(anInteractive);
158     
159     if(STAT->DisplayMode() == -1){
160       if(!myMainPM->IsDisplayed(anInteractive,WhichMode))
161         myMainPM->Display(anInteractive,WhichMode);
162       if(STAT->IsTemporary())
163         STAT->SetDisplayMode(WhichMode);
164     }
165     else if(STAT->DisplayMode()!=WhichMode && STAT->IsTemporary()){
166       myMainPM->Erase(anInteractive,STAT->DisplayMode());
167       STAT->SetDisplayMode(WhichMode);
168       if(!myMainPM->IsDisplayed(anInteractive,WhichMode))
169         myMainPM->Display(anInteractive,WhichMode);
170     }
171     
172     if(ActivationMode!=-1){
173       if(!STAT->IsActivated(ActivationMode)){
174         STAT->ClearSelectionModes();
175         mySM->Load(anInteractive,myMainVS);
176         STAT->AddSelectionMode(ActivationMode);
177         mySM->Activate(anInteractive,ActivationMode,myMainVS);
178       }
179     }
180   }
181   else {
182     Handle(AIS_LocalStatus) Att = new AIS_LocalStatus();
183     
184     if(anInteractive->AcceptShapeDecomposition() && AllowShapeDecomposition)
185       Att->SetDecomposition(Standard_True);
186     else 
187       Att->SetDecomposition(Standard_False);
188     // status temporary or not
189     if(myCTX->DisplayStatus(anInteractive) == AIS_DS_None ||
190        myCTX->DisplayStatus(anInteractive) == AIS_DS_Temporary)
191       Att->SetTemporary(Standard_True);
192     else
193       Att->SetTemporary(Standard_False); 
194
195
196     
197     if(!myCTX->IsDisplayed(anInteractive,WhichMode)){
198       
199       //storing information....
200       Att->SetDisplayMode(WhichMode);
201       if (ActivationMode!=-1)
202         Att->AddSelectionMode(ActivationMode);
203       Standard_Integer HiMod = anInteractive->HasHilightMode()? anInteractive->HilightMode(): WhichMode;
204       Att->SetHilightMode(HiMod);
205
206       if(!myMainPM->IsDisplayed(anInteractive,WhichMode))
207         myMainPM->Display(anInteractive,WhichMode);
208       
209       if(ActivationMode!=-1){
210         mySM->Load(anInteractive,myMainVS);
211         mySM->Activate(anInteractive,ActivationMode,myMainVS);
212       }
213     }
214     else{
215       Standard_Integer HiMod = anInteractive->HasHilightMode()? anInteractive->HilightMode(): WhichMode;
216       Att->SetHilightMode(HiMod);
217     }
218     myActiveObjects.Bind(anInteractive,Att);
219   }  
220   Process(anInteractive);
221
222   
223   
224
225   return Standard_True;
226 }
227
228 //=======================================================================
229 //function : Load
230 //purpose  : 
231 //=======================================================================
232
233 Standard_Boolean AIS_LocalContext::
234 Load(const Handle(AIS_InteractiveObject)& anInteractive,
235      const Standard_Boolean AllowShapeDecomposition,
236      const Standard_Integer ActivationMode)
237 {
238   if(myActiveObjects.IsBound(anInteractive)) return Standard_False;
239   Handle(AIS_LocalStatus) Att = new AIS_LocalStatus();
240   
241   if(anInteractive->AcceptShapeDecomposition() && AllowShapeDecomposition)
242     Att->SetDecomposition(Standard_True);
243   else 
244     Att->SetDecomposition(Standard_False);
245   
246   if(!myCTX->IsDisplayed(anInteractive))
247     Att->SetTemporary(Standard_True);
248   else
249     Att->SetTemporary(Standard_False);
250   Att->SetDisplayMode(-1);
251   
252   //storing information....
253   if(ActivationMode!=-1)
254     Att->AddSelectionMode(ActivationMode);
255   Standard_Integer HiMod = anInteractive->HasHilightMode()? anInteractive->HilightMode():0;
256   Att->SetHilightMode(HiMod);
257   //Action
258   
259   mySM->Load(anInteractive,myMainVS);
260   if(ActivationMode != -1){
261     mySM->Activate(anInteractive,ActivationMode,myMainVS);
262   }
263   myActiveObjects.Bind(anInteractive,Att);
264   Process(anInteractive);
265   return Standard_True;
266 }
267
268 //=======================================================================
269 //function : ClearPrs
270 //purpose  : 
271 //=======================================================================
272
273 Standard_Boolean AIS_LocalContext::
274 ClearPrs(const Handle(AIS_InteractiveObject)& anInteractive,
275          const Standard_Integer aMode)
276 {
277   if(!myActiveObjects.IsBound(anInteractive))
278     return Standard_False;
279
280   Standard_Boolean jobdone(Standard_False);
281   const Handle(AIS_LocalStatus)& STAT = myActiveObjects(anInteractive);
282   
283   //Display step
284   if(STAT->IsSubIntensityOn()) {
285     STAT->SubIntensityOff();
286     if(STAT->HilightMode()==aMode)
287       myMainPM->Unhighlight(anInteractive,STAT->HilightMode());
288   }
289   myMainPM->Clear(anInteractive,aMode); // correction connexions 23/09/97
290   jobdone = Standard_True;
291   if(STAT->DisplayMode()==aMode)
292     STAT->SetDisplayMode(-1);
293   return jobdone;
294 }
295 //=======================================================================
296 //function : Erase
297 //purpose  : 
298 //=======================================================================
299
300 Standard_Boolean AIS_LocalContext::
301 Erase(const Handle(AIS_InteractiveObject)& anInteractive)
302 {
303   if(!myActiveObjects.IsBound(anInteractive))
304     return Standard_False;
305   const Handle(AIS_LocalStatus)& STAT = myActiveObjects(anInteractive);
306   
307   //Display step
308   if(STAT->IsSubIntensityOn()) {
309     STAT->SubIntensityOff();
310     myMainPM->Unhighlight(anInteractive,STAT->HilightMode());
311   }
312
313   Standard_Boolean status(Standard_False);
314
315   if(STAT->DisplayMode()!=-1) {
316     if(IsSelected(anInteractive))
317       AddOrRemoveSelected(anInteractive);
318     if(myMainPM->IsHighlighted(anInteractive,STAT->HilightMode()))
319       myMainPM->Unhighlight(anInteractive,STAT->HilightMode());
320     myMainPM->Erase(anInteractive,STAT->DisplayMode());
321     STAT->SetDisplayMode(-1);
322     status = Standard_True;
323   }
324   if(STAT->IsTemporary()){
325     if(myMainPM->IsDisplayed(anInteractive,STAT->HilightMode()))
326       myMainPM->Erase(anInteractive,STAT->HilightMode());
327   }
328   //selection step
329   
330   TColStd_ListIteratorOfListOfInteger It(STAT->SelectionModes());
331   for(;It.More();It.Next())
332     mySM->Deactivate(anInteractive,It.Value(),myMainVS);
333   //  STAT->ClearSelectionModes();
334   return status;
335 }
336
337
338 //=======================================================================
339 //function : SetShapeDecomposition
340 //purpose  : 
341 //=======================================================================
342
343 void AIS_LocalContext::SetShapeDecomposition(const Handle(AIS_InteractiveObject)& aStoredObject, 
344                                                 const Standard_Boolean aStatus)
345 {
346   if(!myActiveObjects.IsBound(aStoredObject)) return;
347   
348   if(aStatus == myActiveObjects(aStoredObject)->Decomposed()) 
349     return;
350   
351   myActiveObjects(aStoredObject)->SetDecomposition(aStatus);
352
353   Process(aStoredObject);
354 }
355
356 //=======================================================================
357 //function : Clear
358 //purpose  : 
359 //=======================================================================
360
361 void AIS_LocalContext::Clear(const AIS_ClearMode aType)
362 {
363   switch (aType){
364   case AIS_CM_All:
365     {
366       ClearObjects();
367       myFilters->Clear();
368       while(!myListOfStandardMode.IsEmpty())
369         DeactivateStandardMode(AIS_Shape::SelectionType(myListOfStandardMode.Last()));
370       break;
371     }
372   case AIS_CM_Interactive:
373     ClearObjects();
374     break;
375   case AIS_CM_Filters:
376     myFilters->Clear();
377     break;
378   case AIS_CM_StandardModes:
379     {
380       while(!myListOfStandardMode.IsEmpty())
381         DeactivateStandardMode(AIS_Shape::SelectionType(myListOfStandardMode.Last()));
382       break;
383     }
384   case AIS_CM_TemporaryShapePrs:
385     ClearDetected();
386   }
387   UpdateSort();
388 }
389 //=======================================================================
390 //function : ActivateMode
391 //purpose  : 
392 //=======================================================================
393
394 void AIS_LocalContext::ActivateMode(const Handle(AIS_InteractiveObject)& aSelectable,
395                                        const Standard_Integer aMode)
396 {
397   if(!myActiveObjects.IsBound(aSelectable)) return;
398 //  if(myActiveObjects(aSelectable)->SelectionMode()!=aMode)
399 //    mySM->Deactivate(aSelectable,aMode,myMainVS);
400   if(aMode != -1){
401     myActiveObjects(aSelectable)->AddSelectionMode(aMode);
402     mySM->Activate(aSelectable,aMode,myMainVS);
403   }
404   UpdateSort();
405 }
406 //=======================================================================
407 //function : ActivateMode
408 //purpose  : 
409 //=======================================================================
410
411 void AIS_LocalContext::DeactivateMode(const Handle(AIS_InteractiveObject)& aSelectable,
412                                          const Standard_Integer aMode)
413 {
414   if(!myActiveObjects.IsBound(aSelectable)) return;
415   
416   if(aMode==-1) return;
417   
418   myActiveObjects(aSelectable)->RemoveSelectionMode(aMode);
419   mySM->Deactivate(aSelectable,aMode,myMainVS);
420   UpdateSort();
421   
422 }
423 //=======================================================================
424 //function : ActivateMode
425 //purpose  : 
426 //=======================================================================
427
428 void AIS_LocalContext::Deactivate(const Handle(AIS_InteractiveObject)& aSelectable)
429 {
430   if(!myActiveObjects.IsBound(aSelectable)) return;
431   
432   mySM->Deactivate(aSelectable,myMainVS);
433   myActiveObjects(aSelectable)->ClearSelectionModes();
434   UpdateSort();
435 }
436
437 //=======================================================================
438 //function : Remove
439 //purpose  : 
440 //=======================================================================
441
442 Standard_Boolean AIS_LocalContext::Remove(const Handle(AIS_InteractiveObject)& aSelectable)
443 {
444   if(!myActiveObjects.IsBound(aSelectable)) return Standard_False;
445
446   if(IsSelected(aSelectable))
447     AddOrRemoveSelected(aSelectable,Standard_False);
448     
449   const Handle(AIS_LocalStatus)& Att = myActiveObjects(aSelectable);
450   
451   TColStd_ListIteratorOfListOfInteger It;
452   Standard_Boolean jobdone(Standard_False);
453   // it is checked which were the temporary attributes 
454   // and they are set to 0
455
456   // desactivate standard modes
457   if(Att->Decomposed()){
458     for(It.Initialize(myListOfStandardMode);It.More();It.Next()){
459       mySM->Deactivate(aSelectable,It.Value(),myMainVS);
460     }
461   }
462   
463   // if object or temporary presentations...
464   if(Att->IsTemporary())
465     {
466       if(Att->IsSubIntensityOn())
467         myMainPM->Unhighlight(aSelectable,Att->HilightMode());
468       
469       // remove if bug on clear correct...
470       myMainPM->Erase(aSelectable,Att->DisplayMode());
471       myMainPM->Clear(aSelectable,Att->DisplayMode());
472       if(myMainPM->IsDisplayed(aSelectable,Att->HilightMode()))
473         myMainPM->Erase(aSelectable,Att->HilightMode());
474       //        myMainPM->Clear(aSelectable,Att->HilightMode());
475       jobdone = Standard_True;
476     }
477   // if below intensity
478   else
479     {
480       if(Att->IsSubIntensityOn())
481         myCTX->SubIntensityOff(aSelectable);
482     }
483   // desactivate stored proper modes
484   for(It.Initialize(Att->SelectionModes());It.More();It.Next()){
485     mySM->Deactivate(aSelectable,It.Value(),myMainVS);
486   }
487 // pop : si je laisses cela plantes dans les elements de construction  
488 //       alors a toi de jouer ROB
489 //  RemoveSelected(aSelectable);
490
491   if(IsSelected(aSelectable))
492     AddOrRemoveSelected(aSelectable);
493   myActiveObjects.UnBind(aSelectable);
494
495   //Last detected object keeps for lastindex initialization.
496   Handle(SelectMgr_EntityOwner) aLastPicked = myMainVS->OnePicked();
497
498   UpdateSort();
499
500   //Object removes from SelectMgr
501   if( mySM->Contains(aSelectable) )
502     mySM->Remove(aSelectable);
503
504   //Object removes from Detected sequence
505   AIS_SequenceOfInteractive detectAIS;
506
507   Standard_Integer i = 1;
508   for(i = 1 ; i < myAISDetectedSeq.Length(); i++)
509   {
510     Handle(AIS_InteractiveObject) anObj = DetectedCurrentObject();
511     if( !anObj.IsNull() && anObj != aSelectable )
512       myAISDetectedSeq.Remove( i );
513   }
514
515   Standard_Integer aHM = aSelectable->HasHilightMode() ? aSelectable->HilightMode() : 0;
516
517   //EntityOwners remove from AIS_Selection
518   Handle(AIS_Selection) aSel = AIS_Selection::Selection(mySelName.ToCString());
519   AIS_NListTransient::Iterator anIter(aSel->Objects()); 
520   AIS_NListTransient removeEntites;
521   for(; anIter.More(); anIter.Next()){
522     const Handle(Standard_Transient)& Tr = anIter.Value();
523     if (!Tr.IsNull()){
524       const Handle(SelectMgr_EntityOwner)& anOwnr = *((const Handle(SelectMgr_EntityOwner)*) &Tr);
525       if(anOwnr->Selectable() == aSelectable){
526         removeEntites.Append(Tr);
527         if(IsSelected(anOwnr))
528           anOwnr->Unhilight(myMainPM, aHM);//Unhilight selected
529       }
530     }
531   }
532   AIS_NListTransient::Iterator anIterRemove(removeEntites); 
533   for(; anIterRemove.More(); anIterRemove.Next())
534     aSel->Select(anIterRemove.Value());//EntityOwner removes from the selection data
535
536   //EntityOwners remove from myMapOfOwner
537   SelectMgr_IndexedMapOfOwner ownersToKeep; 
538   const Handle(V3d_Viewer)& aViewer = myCTX->CurrentViewer();
539   for(i = 1; i <= myMapOfOwner.Extent(); i++){
540     const Handle(SelectMgr_EntityOwner)& anOwner = myMapOfOwner(i) ;
541     if(!anOwner.IsNull()) {
542       if(anOwner->Selectable() != aSelectable)
543         ownersToKeep.Add(anOwner);
544       else
545       {
546         if(anOwner->IsHilighted(myMainPM, aHM))
547         {
548           for(aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews())
549             Unhilight(anOwner, aViewer->ActiveView());
550         }
551       }
552     }
553   }
554   myMapOfOwner.Clear();
555   myMapOfOwner.Assign(ownersToKeep);
556   mylastindex = myMapOfOwner.FindIndex(aLastPicked);
557
558   return Standard_True;
559 }
560
561 //=======================================================================
562 //function : ActivateStandardMode
563 //purpose  : 
564 //=======================================================================
565
566 void AIS_LocalContext::ActivateStandardMode(const TopAbs_ShapeEnum aType)
567 {
568   
569   //check if it is not in the list
570   TColStd_ListIteratorOfListOfInteger It(myListOfStandardMode);
571   for(;It.More();It.Next())
572     if(It.Value()==aType)  return;
573   Standard_Integer IMode = AIS_Shape::SelectionMode(aType);
574   
575
576   // create a hidden filter answering ok to the type except for :
577   // if the type is shape...
578   // if the filters already impact at the type <aType>
579   if(aType != TopAbs_SHAPE){
580     if(myStdFilters[IMode].IsNull())
581       myStdFilters[IMode] = new StdSelect_ShapeTypeFilter(aType);
582     if(!HasFilters(aType))
583       myFilters->Add(myStdFilters[IMode]);
584   }
585   
586   // the mode is activated for all objects of type Shape 
587   // accepting the decomposition in standard mode.
588   myListOfStandardMode.Append(IMode);
589   
590   AIS_DataMapIteratorOfDataMapOfSelStat ItM(myActiveObjects);
591
592   for(;ItM.More();ItM.Next()){
593 #ifdef BUC60722
594     AIS_DisplayStatus DS = 
595         myCTX->DisplayStatus(Handle(AIS_InteractiveObject)::DownCast(ItM.Key()));
596     if( ItM.Value()->Decomposed() && (DS != AIS_DS_FullErased)  )
597 #else
598     if(ItM.Value()->Decomposed())
599 #endif
600       myCTX->SelectionManager()->Activate(ItM.Key(),
601                                           IMode,
602                                           myMainVS);
603   }
604   
605 }
606
607 //=======================================================================
608 //function : DeActivateStandardMode
609 //purpose  : 
610 //=======================================================================
611
612 void AIS_LocalContext::DeactivateStandardMode(const TopAbs_ShapeEnum aType)
613 {
614   TColStd_ListIteratorOfListOfInteger It(myListOfStandardMode);
615   Standard_Integer IMode = AIS_Shape::SelectionMode(aType);
616   for(;It.More();It.Next())
617     if(It.Value()==IMode) {
618       AIS_DataMapIteratorOfDataMapOfSelStat ItM(myActiveObjects);
619       
620       for(;ItM.More();ItM.Next()){
621         if(ItM.Value()->Decomposed()){
622           myCTX->SelectionManager()->Deactivate(ItM.Key(),
623                                                 IMode,
624                                                 myMainVS);
625           ItM.Value()->RemoveSelectionMode(IMode);
626         }
627       }
628       myListOfStandardMode.Remove(It);
629       if(myFilters->IsIn(myStdFilters[IMode]))
630         myFilters->Remove(myStdFilters[IMode]);
631       UpdateSort();
632       return;
633     }   
634   UpdateSort();
635 }
636
637 //=======================================================================
638 //function : AddFilter
639 //purpose  : 
640 //=======================================================================
641
642 void AIS_LocalContext::AddFilter(const Handle(SelectMgr_Filter)& aFilter)
643 {
644   // it is checked if the filter impacts at the type of active sub-shape 
645   // for which a filter of type has been already implemented...
646
647   TColStd_ListIteratorOfListOfInteger It(myListOfStandardMode);
648   
649   for(;It.More();It.Next()){
650     if(aFilter->ActsOn(AIS_Shape::SelectionType(It.Value())))
651       if(myFilters->IsIn(myStdFilters[It.Value()]))
652         myFilters->Remove(myStdFilters[It.Value()]);
653   } 
654   myFilters->Add(aFilter);
655 }
656
657 //=======================================================================
658 //function : RemoveFilter
659 //purpose  : 
660 //=======================================================================
661
662 void AIS_LocalContext::RemoveFilter(const Handle(SelectMgr_Filter)& aFilter)
663 {
664   if(myFilters->IsIn(aFilter)) myFilters->Remove(aFilter);
665   
666   // it is checked if the filter for type standard is active.
667   // if yes, it is checked there are still similarities among the
668   // remaining filters...
669   //     otherwise, the standard filter is restored to
670   //     continu selecting active modes...
671   TColStd_ListIteratorOfListOfInteger It(myListOfStandardMode);
672   TopAbs_ShapeEnum SE;
673   for(;It.More();It.Next()){
674     SE = AIS_Shape::SelectionType(It.Value());
675     if(aFilter->ActsOn(SE))
676       if(!HasFilters(SE))
677         myFilters->Add(myStdFilters[It.Value()]);
678   }
679 }
680
681
682
683 Standard_Boolean AIS_LocalContext::HasSameProjector(const Handle(Select3D_Projector)& thePrj) const
684 {
685   const Handle(Select3D_Projector)& aCurPrj = myMainVS->Projector();
686   if (aCurPrj->Perspective() != thePrj->Perspective())
687     return Standard_False;  
688   if (aCurPrj->Perspective() && aCurPrj->Focus() != thePrj->Focus())
689     return Standard_False;
690   const gp_GTrsf& aCurTrsf = aCurPrj->Transformation();
691   const gp_GTrsf& aPrjTrsf = thePrj->Transformation();
692
693   for (Standard_Integer i = 1; i <= 3; ++i)
694   {
695     for (Standard_Integer j = 1; j <= 3 ; ++j)
696     {
697       if (aCurTrsf.Value (i, j) != aPrjTrsf.Value (i, j))
698         return Standard_False;
699     }
700   }
701
702   return Standard_True;
703 }
704
705
706 //=======================================================================
707 //function : Terminate
708 //purpose  : 
709 //=======================================================================
710
711 void AIS_LocalContext::Terminate( const Standard_Boolean updateviewer )
712 {
713   ClearDetected();
714   Clear();
715   myMapOfOwner.Clear();
716   
717   mylastindex=0;
718   // clear the selector...
719   myMainVS->Clear();
720   myCTX->SelectionManager()->Remove(myMainVS);
721   
722
723   AIS_Selection::SetCurrentSelection(mySelName.ToCString());
724   Handle(AIS_Selection) S = AIS_Selection::CurrentSelection();
725   Handle(Standard_Transient) Tr;
726   for(S->Init();S->More();S->Next()){
727     Tr = S->Value();
728     (*((Handle(SelectMgr_EntityOwner)*)&Tr))->State(0);
729   }
730
731       
732   AIS_Selection::Select();
733   AIS_Selection::Remove(mySelName.ToCString());
734
735   // CLE
736   // const Handle(V3d_Viewer)& Vwr = myCTX->CurrentViewer();
737   Handle(V3d_Viewer) Vwr = myCTX->CurrentViewer();
738   // ENDCLE
739   Handle(V3d_View) curV;
740   for(Vwr->InitActiveViews();Vwr->MoreActiveViews();Vwr->NextActiveViews()){
741     curV = Vwr->ActiveView(); 
742     Visual3d_TransientManager::ClearDraw( curV->View(), updateviewer );
743   }
744
745
746 #ifdef DEB
747   Handle(V3d_View) BidV;
748   myMainVS->ClearAreas(BidV);
749   myMainVS->ClearSensitive(BidV);
750   
751 #endif
752 }
753
754
755 //=======================================================================
756 //function : SubIntensity
757 //purpose  : 
758 //=======================================================================
759
760 void AIS_LocalContext::SubIntensityOn(const Handle(AIS_InteractiveObject)& anObject)
761 {
762   if(!myActiveObjects.IsBound(anObject)) return;
763   
764   const Handle(AIS_LocalStatus)& Att = myActiveObjects(anObject);
765
766   if(Att->IsTemporary()) 
767     myMainPM->Color(anObject,myCTX->SubIntensityColor(),Att->DisplayMode());
768   
769   Att->SubIntensityOn();
770 }
771 //=======================================================================
772 //function : SubIntensity
773 //purpose  : 
774 //=======================================================================
775
776 void AIS_LocalContext::SubIntensityOff(const Handle(AIS_InteractiveObject)& anObject)
777 {
778   if(!myActiveObjects.IsBound(anObject)) return;
779   
780   const Handle(AIS_LocalStatus)& Att = myActiveObjects(anObject);
781
782   if(Att->IsTemporary()) 
783     myMainPM->Unhighlight(anObject);
784   Att->SubIntensityOff();
785 }
786
787
788 //=======================================================================
789 //function : Hilight
790 //purpose  : 
791 //=======================================================================
792
793 void AIS_LocalContext::Hilight(const  Handle(AIS_InteractiveObject)& anObject)
794 {
795   if(!myActiveObjects.IsBound(anObject)){
796     Standard_Integer HiMod = anObject->HasHilightMode()? anObject->HilightMode() : 0; 
797     Handle(AIS_LocalStatus) Att = new AIS_LocalStatus(Standard_True,
798                                                       Standard_False,
799                                                       -1,-1,HiMod);
800     myActiveObjects.Bind(anObject,Att);
801     
802   }
803   const Handle(AIS_LocalStatus)& Att = myActiveObjects(anObject);
804   myMainPM->Color(anObject,myCTX->HilightColor(),Att->HilightMode());
805   Att->SubIntensityOn();
806 }
807 //=======================================================================
808 //function : Hilight
809 //purpose  : 
810 //=======================================================================
811
812 void AIS_LocalContext::Hilight(const  Handle(AIS_InteractiveObject)& anObject,
813                                const Quantity_NameOfColor Col)
814 {
815   if(!myActiveObjects.IsBound(anObject)){
816     Standard_Integer HiMod = anObject->HasHilightMode()? anObject->HilightMode() : 0; 
817     Handle(AIS_LocalStatus) Att = new AIS_LocalStatus(Standard_True,
818                                                       Standard_False,
819                                                       -1,-1,HiMod);
820     myActiveObjects.Bind(anObject,Att);
821     
822   }
823   const Handle(AIS_LocalStatus)& Att = myActiveObjects(anObject);
824   myMainPM->Color(anObject,Col,Att->HilightMode());
825   Att->SubIntensityOn();
826   Att->SetHilightColor(Col);
827 }
828
829 //=======================================================================
830 //function : Unhilight
831 //purpose  : 
832 //=======================================================================
833
834 void AIS_LocalContext::Unhilight(const Handle(AIS_InteractiveObject)& anObject)
835 {
836   if(!myActiveObjects.IsBound(anObject)) return;
837   
838   // chieck if by hazard the object is somewhere else...
839   Standard_Integer Indx;
840   Standard_Boolean IsSomeWhereElse  = 
841     myCTX->IsInLocal(anObject,Indx) && Indx != myCTX->IndexOfCurrentLocal();
842   
843   const Handle(AIS_LocalStatus)& Att = myActiveObjects(anObject);
844   myMainPM->Unhighlight(anObject,Att->HilightMode());
845   if(Att->IsTemporary() && Att->DisplayMode()==-1)
846     if(!IsSomeWhereElse)
847       myMainPM->Erase(anObject,Att->HilightMode());
848   
849   Att->SubIntensityOff();
850   Att->SetHilightColor(Quantity_NOC_WHITE);
851 }
852
853
854 //=======================================================================
855 //function : IsIn
856 //purpose  : 
857 //=======================================================================
858
859 Standard_Boolean AIS_LocalContext::
860 IsIn(const Handle(AIS_InteractiveObject)& anObject) const 
861 {
862   return myActiveObjects.IsBound(anObject);
863 }
864
865 //=======================================================================
866 //function : IsHilighted
867 //purpose  : 
868 //=======================================================================
869
870 Standard_Boolean AIS_LocalContext::IsHilighted(const Handle(AIS_InteractiveObject)& anObject) const 
871 {
872   if(!myActiveObjects.IsBound(anObject)) return Standard_False;
873   return myActiveObjects(anObject)->IsSubIntensityOn();
874 }
875
876 Standard_Boolean AIS_LocalContext::IsHilighted(const Handle(AIS_InteractiveObject)& anObject,
877                                                Standard_Boolean& WithColor,
878                                                Quantity_NameOfColor& HiCol) const 
879 {
880   if(!myActiveObjects.IsBound(anObject)) return Standard_False;
881   if( myActiveObjects(anObject)->IsSubIntensityOn()){
882     HiCol = myActiveObjects(anObject)->HilightColor();
883     if(HiCol==Quantity_NOC_WHITE)
884       WithColor = Standard_True;
885     else
886       WithColor = Standard_False;
887     return Standard_True;
888   }
889   return Standard_False;
890 }
891
892
893 void AIS_LocalContext::SetDisplayPriority(const Handle(AIS_InteractiveObject)& anObject,
894                                           const Standard_Integer Prior)
895 {
896   if(!myActiveObjects.IsBound(anObject)) return;
897   const Handle(AIS_LocalStatus)& STAT = myActiveObjects(anObject);
898   if(STAT->DisplayMode()==-1) return;
899   myMainPM->SetDisplayPriority(anObject,STAT->DisplayMode(),Prior);
900   if(STAT->IsSubIntensityOn())
901     myMainPM->SetDisplayPriority(anObject,STAT->HilightMode(),Prior);
902   
903   
904 }
905
906 //=======================================================================
907 //function : DisplayedObjects
908 //purpose  : 
909 //=======================================================================
910 Standard_Integer AIS_LocalContext::DisplayedObjects(TColStd_MapOfTransient& theMap) const
911 {
912   Standard_Integer NbDisp(0);
913   for(AIS_DataMapIteratorOfDataMapOfSelStat it(myActiveObjects);it.More();it.Next()){
914     const Handle(SelectMgr_SelectableObject)& SO = it.Key();
915     if(!theMap.Contains(SO))
916       if(it.Value()->DisplayMode()!=-1){
917         theMap.Add(SO);
918         NbDisp++;
919       }
920   }
921   return NbDisp;
922 }
923
924
925 //=======================================================================
926 //function : IsDisplayed
927 //purpose  : 
928 //=======================================================================
929
930 Standard_Boolean AIS_LocalContext::IsDisplayed(const Handle(AIS_InteractiveObject)& anObject) const 
931 {
932   if(!myActiveObjects.IsBound(anObject)) return Standard_False;
933   return (myActiveObjects(anObject)->DisplayMode()!=-1);
934 }
935
936 //=======================================================================
937 //function : IsDisplayed
938 //purpose  : 
939 //=======================================================================
940
941 Standard_Boolean AIS_LocalContext::IsDisplayed(const Handle(AIS_InteractiveObject)& anObject,
942                                                   const Standard_Integer aMode) const 
943 {
944   if(!myActiveObjects.IsBound(anObject)) return Standard_False;
945   return (myActiveObjects(anObject)->DisplayMode()==aMode);
946 }
947
948 //=======================================================================
949 //function : SelectionModes
950 //purpose  : 
951 //=======================================================================
952
953 const TColStd_ListOfInteger& AIS_LocalContext::
954 SelectionModes(const Handle(AIS_InteractiveObject)& anObject) const 
955 {
956   return myActiveObjects(anObject)->SelectionModes(); 
957 }
958
959 //=======================================================================
960 //function : Status
961 //purpose  : 
962 //=======================================================================
963
964 TCollection_AsciiString AIS_LocalContext::Status() const 
965 {
966   TCollection_AsciiString t;
967   return t;
968 }
969
970 const Handle(AIS_LocalStatus)& AIS_LocalContext::Status(const Handle(AIS_InteractiveObject)& anObject) const 
971 {
972   return myActiveObjects(anObject);
973 }
974
975 //=======================================================================
976 //function : LoadContextObjects
977 //purpose  : 
978 //=======================================================================
979
980 void AIS_LocalContext::LoadContextObjects()
981 {
982   AIS_ListIteratorOfListOfInteractive It;
983   if(myLoadDisplayed) {
984     AIS_ListOfInteractive LL;
985     myCTX->DisplayedObjects(LL,Standard_True);
986     Handle(AIS_LocalStatus) Att;
987     for (It.Initialize(LL);It.More();It.Next()){
988       Att= new AIS_LocalStatus();
989       Att->SetDecomposition((It.Value()->AcceptShapeDecomposition() && myAcceptStdMode));
990       Att->SetTemporary(Standard_False);
991       Att->SetHilightMode(It.Value()->HasHilightMode()? It.Value()->HilightMode(): 0);
992       
993       myActiveObjects.Bind(It.Value(),Att);
994     }
995   }
996 }
997
998 void AIS_LocalContext::UnloadContextObjects()
999 {
1000   AIS_ListIteratorOfListOfInteractive It;
1001   if(myLoadDisplayed) 
1002   {
1003     AIS_ListOfInteractive LL;
1004     myCTX->DisplayedObjects(LL,Standard_True);
1005     
1006     for (It.Initialize(LL);It.More();It.Next())
1007     {
1008       myActiveObjects.UnBind(It.Value());
1009     }
1010   }
1011 }
1012 //=======================================================================
1013 //function : Process
1014 //purpose  : 
1015 //=======================================================================
1016
1017 void AIS_LocalContext::Process(const Handle(SelectMgr_SelectableObject)& anObject,
1018                                const Standard_Boolean WithProj)
1019
1020   if(!myActiveObjects.IsBound(anObject)) return;
1021   if(myActiveObjects(anObject)->Decomposed())
1022     ActivateStandardModes(anObject,WithProj);
1023   else
1024     {
1025       TColStd_ListIteratorOfListOfInteger It(myActiveObjects(anObject)->SelectionModes());
1026       for(;It.More();It.Next())
1027         myCTX->SelectionManager()->Activate(anObject,It.Value(),myMainVS,WithProj);
1028     }
1029 }
1030
1031 //=======================================================================
1032 //function : Process
1033 //purpose  : 
1034 //=======================================================================
1035
1036 void AIS_LocalContext::Process(const Standard_Boolean WithProj)
1037
1038
1039   myMainVS->Clear();
1040   
1041   AIS_DataMapIteratorOfDataMapOfSelStat It(myActiveObjects);
1042   
1043   for(;It.More();It.Next()){
1044     myCTX->SelectionManager()->Load(It.Key(),myMainVS);
1045     if(It.Value()->Decomposed()) 
1046       ActivateStandardModes(It.Key(),WithProj);
1047     else if( myCTX->GetAutoActivateSelection() )
1048     {
1049       It.Value()->AddSelectionMode(0);
1050       myCTX->SelectionManager()->Activate(It.Key(),0,myMainVS,WithProj);
1051     }
1052   }
1053
1054 }
1055
1056 //=======================================================================
1057 //function : ActivateModes
1058 //purpose  : 
1059 //=======================================================================
1060
1061 void AIS_LocalContext::ActivateStandardModes(const Handle(SelectMgr_SelectableObject)& anObject,
1062                                              const Standard_Boolean WithProj)
1063
1064   if(!myActiveObjects.IsBound(anObject)) return;
1065   
1066   TColStd_ListIteratorOfListOfInteger itl (myListOfStandardMode);
1067
1068   const Handle(AIS_LocalStatus)&  LS = myActiveObjects(anObject);
1069   if(LS->Decomposed()){
1070     for(;itl.More();itl.Next()){
1071       myCTX->SelectionManager()->Activate(anObject,itl.Value(),myMainVS,WithProj);
1072       LS->AddSelectionMode(itl.Value());
1073     }
1074   }
1075 }
1076
1077
1078 //=======================================================================
1079 //function : ClearObjects
1080 //purpose  : 
1081 //=======================================================================
1082
1083 void AIS_LocalContext::ClearObjects()
1084 {
1085   AIS_DataMapIteratorOfDataMapOfSelStat It(myActiveObjects);
1086   for(;It.More();It.Next())
1087     {
1088       Handle(AIS_InteractiveObject) SO =
1089         Handle(AIS_InteractiveObject)::DownCast(It.Key());
1090       
1091       const Handle(AIS_LocalStatus)& CurAtt = It.Value();
1092       //TColStd_ListIteratorOfListOfInteger ItL;
1093       // if object is temporary the presentations managed by myMainPM are removed
1094       AIS_DisplayStatus TheDS = myCTX->DisplayStatus(SO);
1095       
1096       if(TheDS != AIS_DS_Displayed){
1097         if(myMainPM->IsDisplayed(SO,CurAtt->DisplayMode())){
1098           if(CurAtt->IsSubIntensityOn()&&
1099              myMainPM->IsHighlighted(SO,CurAtt->HilightMode()))
1100             myMainPM->Unhighlight(SO,CurAtt->HilightMode());
1101           myMainPM->Erase(SO,CurAtt->DisplayMode());
1102         }
1103         
1104         if(CurAtt->IsTemporary()){
1105           myMainPM->Erase(SO,CurAtt->DisplayMode());}
1106 //        myMainPM->Clear(SO,CurAtt->DisplayMode());}
1107       }
1108       else {
1109         if (CurAtt->IsSubIntensityOn()){
1110           myCTX->SubIntensityOff(Handle(AIS_InteractiveObject)::DownCast(SO));}
1111         Standard_Integer DiMo = SO->HasDisplayMode()?
1112           SO->DisplayMode():myCTX->DisplayMode();
1113         if(CurAtt->DisplayMode()!=-1 &&
1114            CurAtt->DisplayMode()!= DiMo)
1115           myMainPM->Erase(SO,CurAtt->DisplayMode());
1116       }
1117       
1118       TColStd_ListIteratorOfListOfInteger ITL(CurAtt->SelectionModes());
1119       for(;ITL.More();ITL.Next())
1120         mySM->Deactivate(SO,ITL.Value(),myMainVS);
1121       
1122       if(CurAtt->IsTemporary())
1123         mySM->Remove(SO,myMainVS);
1124       
1125     }
1126   ClearSelected( Standard_False );
1127   myActiveObjects.Clear();
1128 //  myMainVS->ClearAreas();myMainVS->ClearSensitive();
1129 }
1130
1131
1132 Standard_Boolean AIS_LocalContext::IsDecompositionOn() const 
1133 {return !myListOfStandardMode.IsEmpty();}
1134
1135
1136
1137
1138 //=======================================================================
1139 //function : HasAlreadyFilters
1140 //purpose  : 
1141 //=======================================================================
1142
1143 Standard_Boolean AIS_LocalContext::
1144 HasFilters(const TopAbs_ShapeEnum aType) const 
1145 {
1146   return myFilters->ActsOn(aType);
1147 }
1148
1149 void AIS_LocalContext::ClearDetected()
1150 {
1151   for(Standard_Integer I=1;I<=myMapOfOwner.Extent();I++){
1152     
1153     if(!myMapOfOwner(I).IsNull()){
1154       if(myMapOfOwner(I)->IsHilighted(myMainPM))
1155         myMapOfOwner(I)->Unhilight(myMainPM);
1156       else if (myMapOfOwner(I)->IsHilighted(myCTX->CollectorPrsMgr()))
1157         myMapOfOwner(I)->Unhilight(myCTX->CollectorPrsMgr());
1158       
1159       else{
1160         const Handle(SelectMgr_SelectableObject)& SO = 
1161           myMapOfOwner.FindKey(I)->Selectable();
1162         if(myActiveObjects.IsBound(SO)){
1163           const Handle(AIS_LocalStatus)& Att = myActiveObjects(SO);
1164           
1165           if(Att->IsTemporary() &&
1166              Att->DisplayMode()==-1 && 
1167              Att->SelectionModes().IsEmpty()){
1168             myMapOfOwner(I)->Clear(myMainPM);
1169             //myMapOfOwner(I)->Clear();//rob-jmi...
1170           }
1171         }
1172       }
1173     }
1174   }
1175
1176 }
1177
1178 void AIS_LocalContext::UpdateConversion()
1179 {
1180   myMainVS->UpdateConversion();
1181 }
1182
1183 void AIS_LocalContext::UpdateSort()
1184 {
1185   myMainVS->UpdateSort();
1186 }
1187
1188
1189
1190 //=======================================================================
1191 //function : IMMEDIATE MODE
1192 //purpose  : 
1193 //=======================================================================
1194
1195 Standard_Boolean  AIS_LocalContext::BeginImmediateDraw()
1196 {
1197   if(myMainPM->IsImmediateModeOn()){
1198     myMainPM->BeginDraw();
1199     return Standard_True;
1200   }
1201   return Standard_False;
1202 }
1203
1204
1205 Standard_Boolean AIS_LocalContext::ImmediateAdd(const Handle(AIS_InteractiveObject)& anIObj,
1206                                                       const Standard_Integer aMode)
1207 {
1208   if(!myMainPM->IsImmediateModeOn())
1209     return Standard_False;
1210   myMainPM->Add(anIObj,aMode);
1211   return Standard_True;
1212 }
1213
1214 Standard_Boolean AIS_LocalContext::ImmediateRemove(const Handle(AIS_InteractiveObject)& anIObj,
1215                                                    const Standard_Integer aMode)
1216 {
1217   if(!myMainPM->IsImmediateModeOn())   return Standard_False;
1218   myMainPM->Remove(anIObj,aMode);
1219   return Standard_True;
1220 }
1221
1222 Standard_Boolean AIS_LocalContext::EndImmediateDraw(const Handle(V3d_View)& aView,
1223                                                           const Standard_Boolean DoubleBuf)
1224 {
1225   if(!myMainPM->IsImmediateModeOn()) return Standard_False;
1226   myMainPM->EndDraw(aView,DoubleBuf);
1227   return Standard_True;
1228 }
1229
1230 Standard_Boolean AIS_LocalContext::IsImmediateModeOn() const
1231 {return myMainPM->IsImmediateModeOn();}
1232
1233 void AIS_LocalContext::SetSensitivityMode(const StdSelect_SensitivityMode aMode) {
1234
1235   myMainVS->SetSensitivityMode(aMode);
1236 }
1237
1238 StdSelect_SensitivityMode AIS_LocalContext::SensitivityMode() const {
1239
1240   return myMainVS->SensitivityMode();
1241 }
1242
1243 void AIS_LocalContext::SetSensitivity(const Standard_Real aPrecision) {
1244
1245   myMainVS->SetSensitivity(aPrecision);
1246 }
1247
1248 Standard_Real AIS_LocalContext::Sensitivity() const {
1249
1250   return myMainVS->Sensitivity();
1251 }
1252
1253 void AIS_LocalContext::SetPixelTolerance(const Standard_Integer aPrecision) {
1254
1255   myMainVS->SetPixelTolerance(aPrecision);
1256 }
1257
1258 Standard_Integer AIS_LocalContext::PixelTolerance() const {
1259
1260   return myMainVS->PixelTolerance();
1261 }
1262
1263 //=======================================================================
1264 //function : SetZLayer
1265 //purpose  :
1266 //=======================================================================
1267
1268 void AIS_LocalContext::SetZLayer (const Handle(AIS_InteractiveObject)& theIObj,
1269                                   const Standard_Integer theLayerId)
1270 {
1271   if (!myActiveObjects.IsBound (theIObj)) 
1272     return;
1273
1274   const Handle(AIS_LocalStatus)& aStatus = myActiveObjects (theIObj);
1275   if (aStatus->DisplayMode () == -1)
1276     return;
1277
1278   theIObj->SetZLayer (myMainPM, theLayerId);
1279 }
1280
1281 //=======================================================================
1282 //function : GetZLayer
1283 //purpose  : 
1284 //=======================================================================
1285
1286 Standard_Integer AIS_LocalContext::GetZLayer (const Handle(AIS_InteractiveObject)& theIObj) const
1287 {
1288   if (!myActiveObjects.IsBound (theIObj)) 
1289     return -1;
1290
1291   return theIObj->GetZLayer (myMainPM);
1292 }