0026272: Visualization - provide a possibility to activate selection modes without...
[occt.git] / src / AIS / AIS_InteractiveContext.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  XAB & Serguei Dec 97 (angle &deviation coeffts)
18
19 #include <AIS_ConnectedInteractive.hxx>
20 #include <AIS_DataMapIteratorOfDataMapOfILC.hxx>
21 #include <AIS_DataMapIteratorOfDataMapOfIOStatus.hxx>
22 #include <AIS_GlobalStatus.hxx>
23 #include <AIS_InteractiveContext.hxx>
24 #include <AIS_InteractiveObject.hxx>
25 #include <AIS_ListIteratorOfListOfInteractive.hxx>
26 #include <AIS_LocalContext.hxx>
27 #include <AIS_LocalStatus.hxx>
28 #include <AIS_MapIteratorOfMapOfInteractive.hxx>
29 #include <AIS_MultipleConnectedInteractive.hxx>
30 #include <AIS_Selection.hxx>
31 #include <AIS_Shape.hxx>
32 #include <AIS_Trihedron.hxx>
33 #include <Geom_Axis2Placement.hxx>
34 #include <Graphic3d_AspectFillArea3d.hxx>
35 #include <HLRBRep.hxx>
36 #include <OSD_Environment.hxx>
37 #include <Precision.hxx>
38 #include <Prs3d_BasicAspect.hxx>
39 #include <Prs3d_DatumAspect.hxx>
40 #include <Prs3d_IsoAspect.hxx>
41 #include <Prs3d_LineAspect.hxx>
42 #include <Prs3d_PlaneAspect.hxx>
43 #include <Prs3d_ShadingAspect.hxx>
44 #include <PrsMgr_ModedPresentation.hxx>
45 #include <PrsMgr_PresentableObject.hxx>
46 #include <Quantity_Color.hxx>
47 #include <SelectMgr_EntityOwner.hxx>
48 #include <SelectMgr_Filter.hxx>
49 #include <SelectMgr_OrFilter.hxx>
50 #include <SelectMgr_SelectionManager.hxx>
51 #include <Standard_Atomic.hxx>
52 #include <Standard_Transient.hxx>
53 #include <Standard_Type.hxx>
54 #include <StdSelect_ViewerSelector3d.hxx>
55 #include <TCollection_AsciiString.hxx>
56 #include <TCollection_ExtendedString.hxx>
57 #include <TColStd_ListIteratorOfListOfInteger.hxx>
58 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
59 #include <TopLoc_Location.hxx>
60 #include <TopoDS_Shape.hxx>
61 #include <UnitsAPI.hxx>
62 #include <V3d_View.hxx>
63 #include <V3d_Viewer.hxx>
64 #include <Visual3d_View.hxx>
65 #include <Visual3d_ViewManager.hxx>
66
67 //#include <AIS_DataMapIteratorOfDataMapOfInteractiveInteger.hxx>
68 namespace
69 {
70   static volatile Standard_Integer THE_AIS_INDEX_SEL = 0;
71   static volatile Standard_Integer THE_AIS_INDEX_CUR = 0;
72
73   static TCollection_AsciiString AIS_Context_NewSelName()
74   {
75     return TCollection_AsciiString ("AIS_SelContext_")
76          + TCollection_AsciiString (Standard_Atomic_Increment (&THE_AIS_INDEX_SEL));
77   }
78
79   static TCollection_AsciiString AIS_Context_NewCurName()
80   {
81     return TCollection_AsciiString ("AIS_CurContext_")
82          + TCollection_AsciiString (Standard_Atomic_Increment (&THE_AIS_INDEX_CUR));
83   }
84
85   typedef NCollection_DataMap<Handle(SelectMgr_SelectableObject), Handle(SelectMgr_IndexedMapOfOwner)> AIS_MapOfObjectOwners;
86   typedef NCollection_DataMap<Handle(SelectMgr_SelectableObject), Handle(SelectMgr_IndexedMapOfOwner)>::Iterator AIS_MapIteratorOfMapOfObjectOwners;
87 }
88
89 //=======================================================================
90 //function : AIS_InteractiveContext
91 //purpose  : 
92 //=======================================================================
93
94 AIS_InteractiveContext::AIS_InteractiveContext(const Handle(V3d_Viewer)& MainViewer):
95 mgrSelector(new SelectMgr_SelectionManager()),
96 myMainPM(new PrsMgr_PresentationManager3d(MainViewer->Viewer())),
97 myMainVwr(MainViewer),
98 myMainSel(new StdSelect_ViewerSelector3d()),
99 myWasLastMain(Standard_False),
100 myCurrentTouched(Standard_False),
101 mySelectedTouched(Standard_False),
102 myToHilightSelected(Standard_True),
103 myFilters(new SelectMgr_OrFilter()),
104 myDefaultDrawer(new Prs3d_Drawer()),
105 myDefaultColor(Quantity_NOC_GOLDENROD),
106 myHilightColor(Quantity_NOC_CYAN1),
107 mySelectionColor(Quantity_NOC_GRAY80),
108 myPreselectionColor(Quantity_NOC_GREEN),
109 mySubIntensity(Quantity_NOC_GRAY40),
110 myDisplayMode(0),
111 myCurLocalIndex(0),
112 myAISCurDetected(0),
113 myZDetectionFlag(0),
114 myIsAutoActivateSelMode( Standard_True )
115
116   InitAttributes();
117 }
118
119 void AIS_InteractiveContext::Delete() const
120 {
121   // clear the static current selection
122   AIS_Selection::ClearCurrentSelection();
123
124   // to avoid an exception
125   if (AIS_Selection::Find (mySelectionName.ToCString()))
126   {
127     AIS_Selection::Remove (mySelectionName.ToCString());
128   }
129
130   // to avoid an exception
131   if (AIS_Selection::Find (myCurrentName.ToCString()))
132   {
133     AIS_Selection::Remove (myCurrentName.ToCString());
134   }
135
136   // let's remove one reference explicitly. this operation's supposed to
137   // be performed when mgrSelector will be destroyed but anyway...
138   const Handle(SelectMgr_ViewerSelector)& aSelector = myMainSel; // to avoid ambiguity
139   mgrSelector->Remove (aSelector);
140
141   Handle(AIS_InteractiveContext) aNullContext;
142   for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
143   {
144     Handle(AIS_InteractiveObject) anObj = anObjIter.Key();
145     anObj->SetContext (aNullContext);
146     for (anObj->Init(); anObj->More(); anObj->Next())
147     {
148       anObj->CurrentSelection()->UpdateBVHStatus (SelectMgr_TBU_Renew);
149     }
150   }
151   MMgt_TShared::Delete();
152 }
153
154 //=======================================================================
155 //function : AIS_SelectionName
156 //purpose  : 
157 //=======================================================================
158 const TCollection_AsciiString& AIS_InteractiveContext::SelectionName() const 
159 {
160   if(!HasOpenedContext())
161     return mySelectionName;
162   return myLocalContexts(myCurLocalIndex)->SelectionName();
163
164
165
166
167
168 //=======================================================================
169 //function : UpdateCurrentViewer
170 //purpose  : 
171 //=======================================================================
172
173 void AIS_InteractiveContext::UpdateCurrentViewer()
174 {
175   if (!myMainVwr.IsNull())
176     myMainVwr->Update();
177 }
178
179
180 //=======================================================================
181 //function : DomainOfMainViewer
182 //purpose  : 
183 //=======================================================================
184
185 Standard_CString AIS_InteractiveContext::DomainOfMainViewer() const 
186 {
187   return myMainVwr->Domain();
188   
189 }
190
191 //=======================================================================
192 //function : DisplayedObjects
193 //purpose  :
194 //=======================================================================
195 void AIS_InteractiveContext::DisplayedObjects (AIS_ListOfInteractive& theListOfIO,
196                                                const Standard_Boolean theOnlyFromNeutral) const
197 {
198   if (!HasOpenedContext()
199    || theOnlyFromNeutral)
200   {
201     for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
202     {
203       if (anObjIter.Value()->GraphicStatus() == AIS_DS_Displayed)
204       {
205         theListOfIO.Append (anObjIter.Key());
206       }
207     }
208     return;
209   }
210
211   // neutral point
212   TColStd_MapOfTransient aDispMap;
213   for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
214   {
215     if (anObjIter.Value()->GraphicStatus() == AIS_DS_Displayed)
216     {
217       aDispMap.Add (anObjIter.Key());
218     }
219   }
220
221   // parse all local contexts...
222   for (AIS_DataMapIteratorOfDataMapOfILC aCtxIter (myLocalContexts); aCtxIter.More(); aCtxIter.Next())
223   {
224     const Handle(AIS_LocalContext)& aLocCtx = aCtxIter.Value();
225     aLocCtx->DisplayedObjects (aDispMap);
226   }
227
228   Handle(AIS_InteractiveObject) anObj;
229   for (TColStd_MapIteratorOfMapOfTransient aDispMapIter (aDispMap); aDispMapIter.More(); aDispMapIter.Next())
230   {
231     const Handle(Standard_Transient)& aTransient = aDispMapIter.Key();
232     anObj = Handle(AIS_InteractiveObject)::DownCast (aTransient);
233     theListOfIO.Append (anObj);
234   }
235 }
236
237 //=======================================================================
238 //function : DisplayedObjects
239 //purpose  :
240 //=======================================================================
241 void AIS_InteractiveContext::DisplayedObjects (const AIS_KindOfInteractive theKind,
242                                                const Standard_Integer      theSign,
243                                                AIS_ListOfInteractive&      theListOfIO,
244                                                const Standard_Boolean /*OnlyFromNeutral*/) const
245 {
246   ObjectsByDisplayStatus (theKind, theSign, AIS_DS_Displayed, theListOfIO);
247 }
248
249 //=======================================================================
250 //function : ErasedObjects
251 //purpose  :
252 //=======================================================================
253 void AIS_InteractiveContext::ErasedObjects (AIS_ListOfInteractive& theListOfIO) const
254 {
255   ObjectsByDisplayStatus (AIS_DS_Erased, theListOfIO);
256 }
257
258 //=======================================================================
259 //function : ErasedObjects
260 //purpose  :
261 //=======================================================================
262 void AIS_InteractiveContext::ErasedObjects (const AIS_KindOfInteractive theKind,
263                                             const Standard_Integer      theSign,
264                                             AIS_ListOfInteractive&      theListOfIO) const
265 {
266   ObjectsByDisplayStatus (theKind, theSign, AIS_DS_Erased, theListOfIO);
267 }
268
269 //=======================================================================
270 //function : ObjectsByDisplayStatus
271 //purpose  :
272 //=======================================================================
273 void AIS_InteractiveContext::ObjectsByDisplayStatus (const AIS_DisplayStatus theStatus,
274                                                      AIS_ListOfInteractive&  theListOfIO) const
275 {
276   for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
277   {
278     if (anObjIter.Value()->GraphicStatus() == theStatus)
279     {
280       theListOfIO.Append (anObjIter.Key());
281     }
282   }
283 }
284
285 //=======================================================================
286 //function : ObjectsByDisplayStatus
287 //purpose  :
288 //=======================================================================
289 void AIS_InteractiveContext::ObjectsByDisplayStatus (const AIS_KindOfInteractive theKind,
290                                                      const Standard_Integer      theSign,
291                                                      const AIS_DisplayStatus     theStatus,
292                                                      AIS_ListOfInteractive&      theListOfIO) const
293 {
294   for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
295   {
296     if (theStatus != AIS_DS_None
297      && anObjIter.Value()->GraphicStatus() != theStatus)
298     {
299       continue;
300     }
301     else if (anObjIter.Key()->Type() != theKind)
302     {
303       continue;
304     }
305
306     if (theSign == -1
307      || anObjIter.Key()->Signature() == theSign)
308     {
309       theListOfIO.Append (anObjIter.Key());
310     }
311   }
312 }
313
314 //=======================================================================
315 //function : ObjectsInside
316 //purpose  :
317 //=======================================================================
318 void AIS_InteractiveContext::ObjectsInside (AIS_ListOfInteractive&      theListOfIO,
319                                             const AIS_KindOfInteractive theKind,
320                                             const Standard_Integer      theSign) const
321 {
322   if (theKind == AIS_KOI_None
323    && theSign == -1)
324   {
325     for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
326     {
327       theListOfIO.Append (anObjIter.Key());
328     }
329     return;
330   }
331
332   for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
333   {
334     if (anObjIter.Key()->Type() != theKind)
335     {
336       continue;
337     }
338
339     if (theSign == -1
340      || anObjIter.Key()->Signature() == theSign)
341     {
342       theListOfIO.Append (anObjIter.Key());
343     }
344   }
345 }
346
347 //=======================================================================
348 //function : ObjectsForView
349 //purpose  :
350 //=======================================================================
351 void AIS_InteractiveContext::ObjectsForView (AIS_ListOfInteractive&  theListOfIO,
352                                              const Handle(V3d_View)& theView,
353                                              const Standard_Boolean  theIsVisibleInView,
354                                              const AIS_DisplayStatus theStatus) const
355 {
356   const Graphic3d_CView* aCView  = reinterpret_cast<const Graphic3d_CView* >(theView->View()->CView());
357   const Standard_Integer aViewId = aCView->ViewId;
358   for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
359   {
360     if (theStatus != AIS_DS_None
361      && anObjIter.Value()->GraphicStatus() != theStatus)
362     {
363       theListOfIO.Append (anObjIter.Key());
364       continue;
365     }
366
367     Handle(Graphic3d_ViewAffinity) anAffinity = myMainVwr->Viewer()->ObjectAffinity (anObjIter.Key());
368     const Standard_Boolean isVisible = anAffinity->IsVisible (aViewId);
369     if (isVisible == theIsVisibleInView)
370     {
371       theListOfIO.Append (anObjIter.Key());
372     }
373   }
374 }
375
376 //=======================================================================
377 //function : Display
378 //purpose  :
379 //=======================================================================
380 void AIS_InteractiveContext::Display (const Handle(AIS_InteractiveObject)& theIObj,
381                                       const Standard_Boolean               theToUpdateViewer)
382 {
383   if (theIObj.IsNull())
384   {
385     return;
386   }
387
388   Standard_Integer aDispMode = 0, aHiMod = -1, aSelMode = -1;
389   GetDefModes (theIObj, aDispMode, aHiMod, aSelMode);
390
391   Display (theIObj, aDispMode, myIsAutoActivateSelMode ? aSelMode : -1,
392            theToUpdateViewer, theIObj->AcceptShapeDecomposition());
393 }
394
395 //=======================================================================
396 //function : SetViewAffinity
397 //purpose  :
398 //=======================================================================
399 void AIS_InteractiveContext::SetViewAffinity (const Handle(AIS_InteractiveObject)& theIObj,
400                                               const Handle(V3d_View)&              theView,
401                                               const Standard_Boolean               theIsVisible)
402 {
403   if (theIObj.IsNull()
404   || !myObjects.IsBound (theIObj))
405   {
406     return;
407   }
408
409   Handle(Graphic3d_ViewAffinity) anAffinity = myMainVwr->Viewer()->ObjectAffinity (theIObj);
410   const Graphic3d_CView* aCView = reinterpret_cast<const Graphic3d_CView* >(theView->View()->CView());
411   anAffinity->SetVisible (aCView->ViewId, theIsVisible == Standard_True);
412   if (theIsVisible)
413   {
414     theView->View()->ChangeHiddenObjects()->Remove (theIObj.get());
415   }
416   else
417   {
418     theView->View()->ChangeHiddenObjects()->Add (theIObj.get());
419   }
420 }
421
422 //=======================================================================
423 //function : Display
424 //purpose  :
425 //=======================================================================
426 void AIS_InteractiveContext::Display (const Handle(AIS_InteractiveObject)& theIObj,
427                                       const Standard_Integer               theDispMode,
428                                       const Standard_Integer               theSelectionMode,
429                                       const Standard_Boolean               theToUpdateViewer,
430                                       const Standard_Boolean               theToAllowDecomposition,
431                                       const AIS_DisplayStatus              theDispStatus)
432 {
433   if (theIObj.IsNull())
434   {
435     return;
436   }
437
438   if (theDispStatus == AIS_DS_Erased)
439   {
440     Erase  (theIObj, theToUpdateViewer);
441     Load (theIObj, theSelectionMode, theToAllowDecomposition);
442     return;
443   }
444
445   if (!theIObj->HasInteractiveContext())
446   {
447     theIObj->SetContext (this);
448   }
449
450   if (theDispStatus == AIS_DS_Temporary
451   && !HasOpenedContext())
452   {
453     return;
454   }
455   else if (HasOpenedContext())
456   {
457     if (theDispStatus == AIS_DS_None
458      || theDispStatus == AIS_DS_Temporary)
459     {
460       myLocalContexts (myCurLocalIndex)->Display (theIObj, theDispMode, theToAllowDecomposition, theSelectionMode);
461       if (theToUpdateViewer)
462       {
463         myMainVwr->Update();
464       }
465       return;
466     }
467   }
468
469   if (!myObjects.IsBound (theIObj))
470   {
471     Handle(AIS_GlobalStatus) aStatus = new AIS_GlobalStatus (AIS_DS_Displayed, theDispMode, theSelectionMode);
472     myObjects.Bind   (theIObj, aStatus);
473     Handle(Graphic3d_ViewAffinity) anAffinity = myMainVwr->Viewer()->RegisterObject (theIObj);
474     myMainPM->Display(theIObj, theDispMode);
475     if (theSelectionMode != -1)
476     {
477       const Handle(SelectMgr_SelectableObject)& anObj = theIObj; // to avoid ambiguity
478       if (!mgrSelector->Contains (anObj))
479       {
480         mgrSelector->Load (theIObj);
481       }
482       mgrSelector->Activate (theIObj, theSelectionMode, myMainSel);
483     }
484   }
485   else
486   {
487     Handle(AIS_GlobalStatus) aStatus = myObjects (theIObj);
488     if (aStatus->GraphicStatus() == AIS_DS_Temporary)
489     {
490       return;
491     }
492
493     // Erase presentations for all display modes different from aDispMode.
494     // Then make sure aDispMode is displayed and maybe highlighted.
495     // Finally, activate selection mode <SelMode> if not yet activated.
496     TColStd_ListOfInteger aModesToRemove;
497     for (TColStd_ListIteratorOfListOfInteger aDispModeIter (aStatus->DisplayedModes()); aDispModeIter.More(); aDispModeIter.Next())
498     {
499       const Standard_Integer anOldMode = aDispModeIter.Value();
500       if (anOldMode != theDispMode)
501       {
502         aModesToRemove.Append (anOldMode);
503         if(myMainPM->IsHighlighted (theIObj, anOldMode))
504         {
505           myMainPM->Unhighlight (theIObj, anOldMode);
506         }
507         myMainPM->Erase (theIObj, anOldMode);
508       }
509     }
510
511     for (TColStd_ListIteratorOfListOfInteger aRemModeIter (aModesToRemove); aRemModeIter.More(); aRemModeIter.Next())
512     {
513       aStatus->RemoveDisplayMode (aRemModeIter.Value());
514     }
515
516     if (!aStatus->IsDModeIn (theDispMode))
517     {
518       aStatus->AddDisplayMode (theDispMode);
519     }
520
521     myMainPM->Display (theIObj, theDispMode);
522     aStatus->SetGraphicStatus (AIS_DS_Displayed);
523     if (aStatus->IsHilighted())
524     {
525       const Standard_Integer aHiMod = theIObj->HasHilightMode() ? theIObj->HilightMode() : theDispMode;
526       myMainPM->Highlight (theIObj, aHiMod);
527     }
528     if (theSelectionMode != -1)
529     {
530       const Handle(SelectMgr_SelectableObject)& anObj = theIObj; // to avoid ambiguity
531       if (!mgrSelector->Contains (anObj))
532       {
533         mgrSelector->Load (theIObj);
534       }
535       if (!mgrSelector->IsActivated (theIObj, theSelectionMode))
536       {
537         mgrSelector->Activate (theIObj, theSelectionMode, myMainSel);
538       }
539     }
540   }
541
542   if (theToUpdateViewer)
543   {
544     myMainVwr->Update();
545   }
546 }
547
548 //=======================================================================
549 //function : Load
550 //purpose  :
551 //=======================================================================
552 void AIS_InteractiveContext::Load (const Handle(AIS_InteractiveObject)& theIObj,
553                                    const Standard_Integer               theSelMode,
554                                    const Standard_Boolean               theToAllowDecomposition)
555 {
556   if (theIObj.IsNull())
557   {
558     return;
559   }
560
561   if (!theIObj->HasInteractiveContext())
562   {
563     theIObj->SetContext (this);
564   }
565
566   if (HasOpenedContext())
567   {
568     myLocalContexts (myCurLocalIndex)->Load (theIObj, theToAllowDecomposition, theSelMode);
569     return;
570   }
571
572   if (theSelMode == -1
573   && !theToAllowDecomposition)
574   {
575     if (!myObjects.IsBound (theIObj))
576     {
577       Standard_Integer aDispMode, aHiMod, aSelModeDef;
578       GetDefModes (theIObj, aDispMode, aHiMod, aSelModeDef);
579       Handle(AIS_GlobalStatus) aStatus = new AIS_GlobalStatus (AIS_DS_Erased, aDispMode, aSelModeDef);
580       myObjects.Bind (theIObj, aStatus);
581     }
582
583     // Register theIObj in the selection manager to prepare further activation of selection
584     const Handle(SelectMgr_SelectableObject)& anObj = theIObj; // to avoid ambiguity
585     if (!mgrSelector->Contains (anObj))
586     {
587       mgrSelector->Load (theIObj);
588     }
589   }
590 }
591
592 //=======================================================================
593 //function : Erase
594 //purpose  :
595 //=======================================================================
596 void AIS_InteractiveContext::Erase (const Handle(AIS_InteractiveObject)& theIObj,
597                                     const Standard_Boolean               theToUpdateViewer)
598 {
599   if (theIObj.IsNull())
600   {
601     return;
602   }
603   
604   if (!theIObj->IsAutoHilight())
605   {
606     theIObj->ClearSelected();
607   }
608
609   Standard_Boolean wasInCtx = Standard_False;
610   if (HasOpenedContext())
611   {
612     // First it is checked if it is possible to remove in the current local context
613     // then one tries to remove in other local contexts, if they allow it...
614     wasInCtx = myLocalContexts (myCurLocalIndex)->Erase (theIObj);
615     for (AIS_DataMapIteratorOfDataMapOfILC aCtxIter (myLocalContexts); aCtxIter.More(); aCtxIter.Next())
616     {
617       if (aCtxIter.Value()->AcceptErase())
618       {
619         wasInCtx = aCtxIter.Value()->Erase (theIObj) || wasInCtx;
620       }
621     }
622   }
623
624   if (!wasInCtx)
625   {
626     EraseGlobal (theIObj, Standard_False);
627   }
628
629   if (theToUpdateViewer)
630   {
631     myMainVwr->Update();
632   }
633 }
634
635 //=======================================================================
636 //function : EraseAll
637 //purpose  :
638 //=======================================================================
639 void AIS_InteractiveContext::EraseAll (const Standard_Boolean theToUpdateViewer)
640 {
641   if (HasOpenedContext())
642   {
643     return;
644   }
645
646   for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
647   {
648     if (anObjIter.Value()->GraphicStatus() == AIS_DS_Displayed)
649     {
650       Erase (anObjIter.Key(), Standard_False);
651     }
652   }
653
654   if (theToUpdateViewer)
655   {
656     myMainVwr->Update();
657   }
658 }
659
660 //=======================================================================
661 //function : DisplayAll
662 //purpose  :
663 //=======================================================================
664 void AIS_InteractiveContext::DisplayAll (const Standard_Boolean theToUpdateViewer)
665 {
666   if (HasOpenedContext())
667   {
668     return;
669   }
670
671   for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
672   {
673     const AIS_DisplayStatus aStatus = anObjIter.Value()->GraphicStatus();
674     if (aStatus == AIS_DS_Erased)
675     {
676       Display (anObjIter.Key(), Standard_False);
677     }
678   }
679
680   if (theToUpdateViewer)
681   {
682     myMainVwr->Update();
683   }
684 }
685
686 //=======================================================================
687 //function : DisplaySelected
688 //purpose  :
689 //=======================================================================
690 void AIS_InteractiveContext::DisplaySelected (const Standard_Boolean theToUpdateViewer)
691 {
692   if (HasOpenedContext())
693   {
694     return;
695   }
696
697   Standard_Boolean      isFound  = Standard_False;
698   Handle(AIS_Selection) aSelIter = AIS_Selection::Selection (myCurrentName.ToCString());
699   for (aSelIter->Init(); aSelIter->More(); aSelIter->Next())
700   {
701     Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (aSelIter->Value());
702     Display (anObj, Standard_False);
703     isFound = Standard_True;
704   }
705
706   if (isFound && theToUpdateViewer)
707   {
708     myMainVwr->Update();
709   }
710 }
711
712 //=======================================================================
713 //function : EraseSelected
714 //purpose  :
715 //=======================================================================
716 void AIS_InteractiveContext::EraseSelected (const Standard_Boolean theToUpdateViewer)
717 {
718   if (HasOpenedContext())
719   {
720     return;
721   }
722
723   Standard_Boolean      isFound  = Standard_False;
724   Handle(AIS_Selection) aSelIter = AIS_Selection::Selection(myCurrentName.ToCString());
725   for (aSelIter->Init(); aSelIter->More(); aSelIter->Next())
726   {
727     Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (aSelIter->Value());
728     Erase (anObj, Standard_False);
729     isFound = Standard_True;
730   }
731
732   if (isFound && theToUpdateViewer)
733   {
734     myMainVwr->Update();
735   }
736 }
737
738 //=======================================================================
739 //function : 
740 //purpose  : 
741 //=======================================================================
742
743 Standard_Boolean AIS_InteractiveContext::KeepTemporary(const Handle(AIS_InteractiveObject)& anIObj,
744                                                        const Standard_Integer WhichContext)
745 {
746   if(anIObj.IsNull()) return Standard_False;
747
748   if(!HasOpenedContext()) return Standard_False;
749   if(myObjects.IsBound(anIObj)) return Standard_False;
750   if(WhichContext!=-1 && !myLocalContexts.IsBound(WhichContext)) return Standard_False;
751   
752   // Protection : if one tries to preserve a temporary object
753   // which is not in the local active context... rob 11-06-97
754
755   Standard_Integer IsItInLocal = myCurLocalIndex;
756   Standard_Boolean Found(Standard_False);
757
758   while(IsItInLocal>0 && !Found){
759     if(!myLocalContexts.IsBound(IsItInLocal))
760       IsItInLocal--;
761     else if(myLocalContexts(IsItInLocal)->IsIn(anIObj))
762       Found = Standard_True;
763     else
764       IsItInLocal--;
765   }
766
767   if(!Found) return Standard_False;
768   
769
770 //  const Handle(AIS_LocalStatus)& LS = (WhichContext== -1) ? 
771 //    myLocalContexts(IsItInLocal)->Status(anIObj):myLocalContexts(WhichContext)->Status(anIObj);
772   // CLE
773   // const Handle(AIS_LocalStatus)& LS = myLocalContexts(IsItInLocal)->Status(anIObj);
774   Handle(AIS_LocalStatus) LS = myLocalContexts(IsItInLocal)->Status(anIObj);
775   // ENDCLE
776   
777   
778   if(LS->IsTemporary()){
779     Standard_Integer DM,HM,SM;
780     GetDefModes(anIObj,DM,HM,SM);
781     
782     SM = LS->SelectionModes().IsEmpty() ? SM : LS->SelectionModes().First();
783     if(LS->DisplayMode()!= DM ){
784       Standard_Integer LSM =  LS->SelectionModes().IsEmpty() ? -1 : LS->SelectionModes().First();
785       myLocalContexts(IsItInLocal)->Display(anIObj,DM,LS->Decomposed(),LSM);
786     }
787
788     Handle (AIS_GlobalStatus) GS = new AIS_GlobalStatus(AIS_DS_Displayed,
789                                                         DM,
790                                                         SM,
791                                                         Standard_False);
792 //    GS->SubIntensityOn();
793     myObjects.Bind(anIObj,GS);
794     mgrSelector->Load(anIObj);
795     mgrSelector->Activate(anIObj,SM,myMainSel);
796     
797     LS->SetTemporary(Standard_False);
798   }                                 
799   return Standard_True;
800 }
801
802 //=======================================================================
803 //function : DisplayStatus
804 //purpose  :
805 //=======================================================================
806 AIS_DisplayStatus AIS_InteractiveContext::DisplayStatus (const Handle(AIS_InteractiveObject)& theIObj) const
807 {
808   if (theIObj.IsNull())
809   {
810     return AIS_DS_None;
811   }
812   else if (myObjects.IsBound (theIObj))
813   {
814     return myObjects (theIObj)->GraphicStatus();
815   }
816
817   for (AIS_DataMapIteratorOfDataMapOfILC aCtxIter (myLocalContexts); aCtxIter.More(); aCtxIter.Next())
818   {
819     if (aCtxIter.Value()->IsIn (theIObj))
820     {
821       return AIS_DS_Temporary;
822     }
823   }
824   return AIS_DS_None;
825 }
826
827 //=======================================================================
828 //function : DisplayedModes
829 //purpose  :
830 //=======================================================================
831 const TColStd_ListOfInteger& AIS_InteractiveContext::DisplayedModes (const Handle(AIS_InteractiveObject)& theIObj) const
832 {
833   return myObjects (theIObj)->DisplayedModes();
834 }
835
836 //=======================================================================
837 //function : Remove
838 //purpose  :
839 //=======================================================================
840 void AIS_InteractiveContext::Remove (const Handle(AIS_InteractiveObject)& theIObj,
841                                      const Standard_Boolean               theToUpdateViewer)
842 {
843   if (theIObj.IsNull())
844   {
845     return;
846   }
847
848   if (HasOpenedContext())
849   {
850     myLocalContexts (myCurLocalIndex)->Remove (theIObj);
851     for (AIS_DataMapIteratorOfDataMapOfILC aCtxIter (myLocalContexts); aCtxIter.More(); aCtxIter.Next())
852     {
853       if (aCtxIter.Value()->AcceptErase())
854       {
855         aCtxIter.Value()->Remove (theIObj);
856       }
857     }
858   }
859
860   ClearGlobal (theIObj, theToUpdateViewer);
861 }
862
863 //=======================================================================
864 //function : RemoveAll
865 //purpose  :
866 //=======================================================================
867 void AIS_InteractiveContext::RemoveAll (const Standard_Boolean theToUpdateViewer)
868 {
869   AIS_ListOfInteractive aList;
870   ObjectsInside (aList);
871   for (AIS_ListIteratorOfListOfInteractive aListIterator (aList); aListIterator.More(); aListIterator.Next())
872   {
873     Remove (aListIterator.Value(), Standard_False);
874   }
875
876   if (theToUpdateViewer)
877   {
878     myMainVwr->Update();
879   }
880 }
881
882 //=======================================================================
883 //function : ClearPrs
884 //purpose  :
885 //=======================================================================
886 void AIS_InteractiveContext::ClearPrs (const Handle(AIS_InteractiveObject)& theIObj,
887                                        const Standard_Integer               theMode,
888                                        const Standard_Boolean               theToUpdateViewer)
889 {
890   if (theIObj.IsNull())
891   {
892     return;
893   }
894
895   if (!HasOpenedContext())
896   {
897     ClearGlobalPrs (theIObj, theMode, theToUpdateViewer);
898     return;
899   }
900
901   Standard_Boolean wasInCtx = myLocalContexts (myCurLocalIndex)->ClearPrs (theIObj, theMode);
902   for (AIS_DataMapIteratorOfDataMapOfILC aCtxIter (myLocalContexts); aCtxIter.More(); aCtxIter.Next())
903   {
904     if (aCtxIter.Value()->AcceptErase())
905     {
906       wasInCtx = aCtxIter.Value()->ClearPrs (theIObj, theMode) || wasInCtx;
907     }
908   }
909   if (!wasInCtx)
910   {
911     ClearGlobalPrs (theIObj, theMode, theToUpdateViewer);
912   }
913   else if (theToUpdateViewer)
914   {
915     myMainVwr->Update();
916   }
917 }
918
919 //=======================================================================
920 //function : Hilight
921 //purpose  :
922 //=======================================================================
923 void AIS_InteractiveContext::Hilight (const Handle(AIS_InteractiveObject)& theIObj,
924                                       const Standard_Boolean               theToUpdateViewer)
925 {
926   if (theIObj.IsNull())
927   {
928     return;
929   }
930
931   if (!theIObj->HasInteractiveContext())
932   {
933     theIObj->SetContext (this);
934   }
935   if (!HasOpenedContext())
936   {
937     if (!myObjects.IsBound (theIObj))
938     {
939       return;
940     }
941
942     Handle(AIS_GlobalStatus) aStatus = myObjects (theIObj);
943     aStatus->SetHilightStatus (Standard_True);
944     if (aStatus->GraphicStatus() == AIS_DS_Displayed)
945     {
946       Standard_Integer aHilightMode = theIObj->HasHilightMode() ? theIObj->HilightMode() : 0;
947       myMainPM->Highlight (theIObj, aHilightMode);
948     }
949   }
950   else
951   {
952     myLocalContexts (myCurLocalIndex)->Hilight (theIObj);
953   }
954
955   if (theToUpdateViewer)
956   {
957     myMainVwr->Update();
958   }
959 }
960 //=======================================================================
961 //function : Hilight
962 //purpose  : 
963 //=======================================================================
964
965 void AIS_InteractiveContext::HilightWithColor(const Handle(AIS_InteractiveObject)& anIObj,
966                                               const Quantity_NameOfColor aCol,
967                                               const Standard_Boolean updateviewer)
968 {
969   if(anIObj.IsNull()) return;
970
971   if(!anIObj->HasInteractiveContext()) anIObj->SetContext(this);
972
973   if (!HasOpenedContext())
974   {
975     if(!myObjects.IsBound(anIObj)) return;
976
977     const Handle(AIS_GlobalStatus)& aStatus = myObjects(anIObj);
978     aStatus->SetHilightStatus (Standard_True);
979
980     if (aStatus->GraphicStatus() == AIS_DS_Displayed)
981     {
982       const Standard_Integer aHilightMode = anIObj->HasHilightMode() ? anIObj->HilightMode() : 0;
983       myMainPM->Color (anIObj, aCol, aHilightMode);
984       aStatus->SetHilightColor (aCol);
985     }
986   }
987   else
988   {
989     myLocalContexts(myCurLocalIndex)->Hilight(anIObj,aCol);
990   }
991   if(updateviewer) myMainVwr->Update();
992 }
993
994 //=======================================================================
995 //function : Unhilight
996 //purpose  : 
997 //=======================================================================
998
999 void AIS_InteractiveContext::Unhilight(const Handle(AIS_InteractiveObject)& anIObj, const Standard_Boolean updateviewer)
1000 {
1001   if(anIObj.IsNull()) return;
1002
1003   if (!HasOpenedContext())
1004   {
1005     if(!myObjects.IsBound(anIObj)) return;
1006
1007     const Handle(AIS_GlobalStatus)& aStatus = myObjects(anIObj);
1008     aStatus->SetHilightStatus (Standard_False);
1009     aStatus->SetHilightColor(Quantity_NOC_WHITE);
1010
1011     if (aStatus->GraphicStatus() == AIS_DS_Displayed)
1012     {
1013       Standard_Integer aHilightMode = anIObj->HasHilightMode() ? anIObj->HilightMode() : 0;
1014       myMainPM->Unhighlight (anIObj, aHilightMode);
1015     }
1016   }
1017   else
1018   {
1019     myLocalContexts(myCurLocalIndex)->Unhilight(anIObj);
1020   }
1021   if(updateviewer) myMainVwr->Update();
1022 }
1023
1024 //=======================================================================
1025 //function : IsHilighted
1026 //purpose  : 
1027 //=======================================================================
1028
1029 Standard_Boolean AIS_InteractiveContext::IsHilighted(const Handle(AIS_InteractiveObject)& anIObj) const 
1030 {
1031   if(anIObj.IsNull()) return Standard_False;
1032
1033   if (!HasOpenedContext()){
1034     if(!myObjects.IsBound(anIObj)) 
1035       return Standard_False;
1036     return myObjects(anIObj)->IsHilighted();
1037   }
1038   AIS_DataMapIteratorOfDataMapOfILC ItM(myLocalContexts);
1039   for(;ItM.More();ItM.Next()){
1040     if(ItM.Value()->IsHilighted(anIObj))
1041       return Standard_True;
1042   }
1043   return Standard_False;
1044 }
1045
1046 Standard_Boolean AIS_InteractiveContext::IsHilighted(const Handle(AIS_InteractiveObject)& anIObj,
1047                                                      Standard_Boolean& WithColor,
1048                                                      Quantity_NameOfColor& TheHiCol) const
1049 {
1050   if(!HasOpenedContext()){
1051     if(myObjects.IsBound(anIObj)){
1052       const Handle(AIS_GlobalStatus)& STAT = myObjects(anIObj);
1053       if(STAT->IsHilighted()){
1054         if(STAT->HilightColor()!=Quantity_NOC_WHITE){
1055           WithColor=Standard_True;
1056           TheHiCol = STAT->HilightColor();
1057         }
1058         else
1059           WithColor = Standard_False;
1060         return Standard_True;
1061       }
1062     }
1063     return Standard_False;
1064   }
1065   Standard_Integer MaxIndex = HighestIndex();
1066   for(Standard_Integer i=MaxIndex;i>=1 ; i--){
1067     if(myLocalContexts.IsBound(i)){
1068       if(myLocalContexts(i)->IsHilighted(anIObj,WithColor,TheHiCol))
1069         return Standard_True;
1070     }
1071     
1072   }
1073   return Standard_False;
1074 }
1075
1076 //=======================================================================
1077 //function : IsHilighted
1078 //purpose  : Returns true if the objects global status is set to highlighted.
1079 //           theIsCustomColor flag defines if highlight color is not equal to OCCT's
1080 //           default Quantity_NOC_WHITE color. If theIsCustomColor is true,
1081 //           custom highlight color name will be stored to theCustomColorName
1082 //=======================================================================
1083 Standard_Boolean AIS_InteractiveContext::IsHilighted (const Handle(SelectMgr_EntityOwner)& theOwner,
1084                                                       Standard_Boolean& theIsCustomColor,
1085                                                       Quantity_NameOfColor& theCustomColorName) const
1086 {
1087   if (theOwner.IsNull() || !theOwner->HasSelectable())
1088     return Standard_False;
1089
1090   const Handle(AIS_InteractiveObject) anObj =
1091     Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
1092
1093   if (!myObjects.IsBound (anObj))
1094     return Standard_False;
1095
1096   const Handle(AIS_GlobalStatus)& anObjStatus = myObjects (anObj);
1097   if (anObjStatus->IsHilighted())
1098   {
1099     if (anObjStatus->HilightColor() != Quantity_NOC_WHITE)
1100     {
1101       theIsCustomColor = Standard_True;
1102       theCustomColorName = anObjStatus->HilightColor();
1103     }
1104     else
1105     {
1106       theIsCustomColor = Standard_False;
1107     }
1108
1109     return Standard_True;
1110   }
1111
1112   return Standard_False;
1113 }
1114
1115 //=======================================================================
1116 //function : IsDisplayed
1117 //purpose  : 
1118 //=======================================================================
1119
1120 Standard_Boolean AIS_InteractiveContext::IsDisplayed(const Handle(AIS_InteractiveObject)& anIObj) const 
1121 {
1122   if(anIObj.IsNull()) return Standard_False;
1123
1124
1125   if(myObjects.IsBound(anIObj)) 
1126     if(myObjects(anIObj)->GraphicStatus()==AIS_DS_Displayed)
1127       return Standard_True;
1128   
1129   AIS_DataMapIteratorOfDataMapOfILC ItM(myLocalContexts);
1130   for(;ItM.More();ItM.Next()){
1131     if(ItM.Value()->IsDisplayed(anIObj))
1132       return Standard_True;
1133   }
1134   return Standard_False;
1135   
1136 }
1137
1138 //=======================================================================
1139 //function : IsDisplayed
1140 //purpose  :
1141 //=======================================================================
1142 Standard_Boolean AIS_InteractiveContext::IsDisplayed (const Handle(AIS_InteractiveObject)& theIObj,
1143                                                       const Standard_Integer               theMode) const
1144 {
1145   if (theIObj.IsNull())
1146   {
1147     return Standard_False;
1148   }
1149
1150   if (myObjects.IsBound (theIObj))
1151   {
1152     Handle(AIS_GlobalStatus) aStatus = myObjects (theIObj);
1153     if (aStatus->GraphicStatus() == AIS_DS_Displayed
1154      && aStatus->IsDModeIn (theMode))
1155     {
1156       return Standard_True;
1157     }
1158   }
1159
1160   for (AIS_DataMapIteratorOfDataMapOfILC aCtxIter (myLocalContexts); aCtxIter.More(); aCtxIter.Next())
1161   {
1162     if (aCtxIter.Value()->IsDisplayed (theIObj, theMode))
1163     {
1164       return Standard_True;
1165     }
1166   }
1167   return Standard_False;
1168 }
1169
1170 //=======================================================================
1171 //function : DisplayPriority
1172 //purpose  :
1173 //=======================================================================
1174 Standard_Integer AIS_InteractiveContext::DisplayPriority (const Handle(AIS_InteractiveObject)& theIObj) const
1175 {
1176   if (theIObj.IsNull())
1177   {
1178     return -1;
1179   }
1180   else if (!myObjects.IsBound (theIObj))
1181   {
1182     return 0;
1183   }
1184
1185   Handle(AIS_GlobalStatus) aStatus = myObjects (theIObj);
1186   if (aStatus->GraphicStatus() == AIS_DS_Displayed
1187    || aStatus->GraphicStatus() == AIS_DS_Erased)
1188   {
1189     Standard_Integer aDispMode = theIObj->HasDisplayMode()
1190                                ? theIObj->DisplayMode()
1191                                : (theIObj->AcceptDisplayMode (myDisplayMode)
1192                                 ? myDisplayMode
1193                                 : 0);
1194     return myMainPM->DisplayPriority (theIObj, aDispMode);
1195   }
1196   return 0;
1197 }
1198
1199 //=======================================================================
1200 //function : SetDisplayPriority
1201 //purpose  :
1202 //=======================================================================
1203 void AIS_InteractiveContext::SetDisplayPriority (const Handle(AIS_InteractiveObject)& theIObj,
1204                                                  const Standard_Integer               thePriority)
1205 {
1206   if (theIObj.IsNull())
1207   {
1208     return;
1209   }
1210
1211   if (!theIObj->HasInteractiveContext())
1212   {
1213     theIObj->SetContext (this);
1214   }
1215
1216   if (myObjects.IsBound (theIObj))
1217   {
1218     Handle(AIS_GlobalStatus) aStatus = myObjects (theIObj);
1219     if (aStatus->GraphicStatus() == AIS_DS_Displayed
1220      || aStatus->GraphicStatus() == AIS_DS_Erased)
1221     {
1222       Standard_Integer aDisplayMode = theIObj->HasDisplayMode()
1223                                     ? theIObj->DisplayMode()
1224                                     : (theIObj->AcceptDisplayMode (myDisplayMode)
1225                                      ? myDisplayMode
1226                                      : 0);
1227       myMainPM->SetDisplayPriority (theIObj, aDisplayMode, thePriority);
1228     }
1229   }
1230   else if (HasOpenedContext())
1231   {
1232     myLocalContexts (myCurLocalIndex)->SetDisplayPriority (theIObj, thePriority);
1233   }
1234 }
1235
1236 //=======================================================================
1237 //function : Redisplay
1238 //purpose  :
1239 //=======================================================================
1240 void AIS_InteractiveContext::Redisplay (const Handle(AIS_InteractiveObject)& theIObj,
1241                                         const Standard_Boolean               theToUpdateViewer,
1242                                         const Standard_Boolean               theAllModes)
1243 {
1244   RecomputePrsOnly (theIObj, theToUpdateViewer, theAllModes);
1245   RecomputeSelectionOnly (theIObj);
1246 }
1247
1248 //=======================================================================
1249 //function : Redisplay
1250 //purpose  :
1251 //=======================================================================
1252 void AIS_InteractiveContext::Redisplay (const AIS_KindOfInteractive theKOI,
1253                                         const Standard_Integer    /*theSign*/,
1254                                         const Standard_Boolean      theToUpdateViewer)
1255 {
1256   Standard_Boolean isRedisplayed = Standard_False;
1257   for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
1258   {
1259     Handle(AIS_InteractiveObject) anObj = anObjIter.Key();
1260     if (anObj->Type() != theKOI)
1261     {
1262       continue;
1263     }
1264
1265     Redisplay (anObj, Standard_False);
1266     isRedisplayed = anObjIter.Value()->GraphicStatus() == AIS_DS_Displayed
1267                  || isRedisplayed;
1268   }
1269
1270   if (theToUpdateViewer
1271    && isRedisplayed)
1272   {
1273     myMainVwr->Update();
1274   }
1275 }
1276
1277 //=======================================================================
1278 //function : RecomputePrsOnly
1279 //purpose  :
1280 //=======================================================================
1281 void AIS_InteractiveContext::RecomputePrsOnly (const Handle(AIS_InteractiveObject)& theIObj,
1282                                                const Standard_Boolean               theToUpdateViewer,
1283                                                const Standard_Boolean               theAllModes)
1284 {
1285   if (theIObj.IsNull())
1286   {
1287     return;
1288   }
1289
1290   theIObj->Update (theAllModes);
1291   if (!theToUpdateViewer)
1292   {
1293     return;
1294   }
1295
1296   if (HasOpenedContext()
1297    || (myObjects.IsBound (theIObj)
1298     && myObjects (theIObj)->GraphicStatus() == AIS_DS_Displayed))
1299   {
1300     myMainVwr->Update();
1301   }
1302 }
1303 //=======================================================================
1304 //function : RecomputeSelectionOnly
1305 //purpose  : 
1306 //=======================================================================
1307 void AIS_InteractiveContext::RecomputeSelectionOnly (const Handle(AIS_InteractiveObject)& theIO)
1308 {
1309   if (theIO.IsNull())
1310   {
1311     return;
1312   }
1313
1314   mgrSelector->RecomputeSelection (theIO);
1315
1316   if (HasOpenedContext())
1317   {
1318     for (Standard_Integer aContextIdx = 1; aContextIdx <= myLocalContexts.Extent(); aContextIdx++)
1319     {
1320       myLocalContexts (aContextIdx)->ClearOutdatedSelection (theIO, Standard_False);
1321     }
1322     return;
1323   }
1324
1325   if (!myObjects.IsBound (theIO) ||
1326       myObjects (theIO)->GraphicStatus() != AIS_DS_Displayed)
1327   {
1328     return;
1329   }
1330
1331   TColStd_ListOfInteger aModes;
1332   ActivatedModes (theIO, aModes);
1333   TColStd_ListIteratorOfListOfInteger aModesIter (aModes);
1334   for (; aModesIter.More(); aModesIter.Next())
1335   {
1336     mgrSelector->Activate (theIO, aModesIter.Value(), myMainSel);
1337   }
1338 }
1339
1340 //=======================================================================
1341 //function : Update
1342 //purpose  :
1343 //=======================================================================
1344 void AIS_InteractiveContext::Update (const Handle(AIS_InteractiveObject)& theIObj,
1345                                      const Standard_Boolean               theUpdateViewer)
1346 {
1347   if (theIObj.IsNull())
1348   {
1349     return;
1350   }
1351
1352   TColStd_ListOfInteger aPrsModes;
1353   theIObj->ToBeUpdated (aPrsModes);
1354   for (TColStd_ListIteratorOfListOfInteger aPrsModesIt (aPrsModes); aPrsModesIt.More(); aPrsModesIt.Next())
1355   {
1356     theIObj->Update (aPrsModesIt.Value(), Standard_False);
1357   }
1358
1359   mgrSelector->Update(theIObj);
1360
1361   for (Standard_Integer aContextIdx = 1; aContextIdx <= myLocalContexts.Extent(); aContextIdx++)
1362   {
1363     myLocalContexts (aContextIdx)->ClearOutdatedSelection (theIObj, Standard_False);
1364   }
1365
1366   if (theUpdateViewer)
1367   {
1368     if (!myObjects.IsBound (theIObj))
1369     {
1370       return;
1371     }
1372
1373     switch (myObjects (theIObj)->GraphicStatus())
1374     {
1375       case AIS_DS_Displayed:
1376       case AIS_DS_Temporary:
1377         myMainVwr->Update();
1378         break;
1379       default:
1380         break;
1381     }
1382   }
1383 }
1384
1385 //=======================================================================
1386 //function : SetLocation
1387 //purpose  :
1388 //=======================================================================
1389 void AIS_InteractiveContext::SetLocation (const Handle(AIS_InteractiveObject)& theIObj,
1390                                           const TopLoc_Location&               theLoc)
1391 {
1392   if (theIObj.IsNull())
1393   {
1394     return;
1395   }
1396
1397   if (theIObj->HasTransformation()
1398    && theLoc.IsIdentity())
1399   {
1400     theIObj->ResetTransformation();
1401     mgrSelector->Update (theIObj, Standard_False);
1402     return;
1403   }
1404   else if (theLoc.IsIdentity())
1405   {
1406     return;
1407   }
1408
1409   // first reset the previous location to properly clean everything...
1410   if (theIObj->HasTransformation())
1411   {
1412     theIObj->ResetTransformation();
1413   }
1414
1415   theIObj->SetLocalTransformation (theLoc.Transformation());
1416
1417   if (!HasOpenedContext())
1418   {
1419     mgrSelector->Update (theIObj, Standard_False);
1420   }
1421   else
1422   {
1423     Handle(StdSelect_ViewerSelector3d) aTempSel = myLocalContexts (myCurLocalIndex)->MainSelector();
1424     mgrSelector->Update (theIObj, aTempSel, Standard_False);
1425   }
1426 }
1427
1428 //=======================================================================
1429 //function : ResetLocation
1430 //purpose  :
1431 //=======================================================================
1432 void AIS_InteractiveContext::ResetLocation (const Handle(AIS_InteractiveObject)& theIObj)
1433 {
1434   if (theIObj.IsNull())
1435   {
1436     return;
1437   }
1438
1439   theIObj->ResetTransformation();
1440   mgrSelector->Update (theIObj, Standard_False);
1441 }
1442
1443 //=======================================================================
1444 //function : HasLocation
1445 //purpose  :
1446 //=======================================================================
1447 Standard_Boolean AIS_InteractiveContext::HasLocation (const Handle(AIS_InteractiveObject)& theIObj) const
1448 {
1449   return !theIObj.IsNull()
1450        && theIObj->HasTransformation();
1451 }
1452
1453 //=======================================================================
1454 //function : Location
1455 //purpose  :
1456 //=======================================================================
1457 TopLoc_Location AIS_InteractiveContext::Location (const Handle(AIS_InteractiveObject)& theIObj) const
1458 {
1459   return theIObj->Transformation();
1460 }
1461
1462 //=======================================================================
1463 //function : SetDeviationCoefficient
1464 //purpose  :
1465 //=======================================================================
1466 void AIS_InteractiveContext::SetDeviationCoefficient (const Standard_Real theCoefficient)
1467 {
1468   myDefaultDrawer->SetDeviationCoefficient (theCoefficient);
1469 }
1470
1471 //=======================================================================
1472 //function : SetDeviationAngle
1473 //purpose  :
1474 //=======================================================================
1475 void AIS_InteractiveContext::SetDeviationAngle (const Standard_Real theAngle)
1476 {
1477   myDefaultDrawer->SetDeviationCoefficient (theAngle);
1478 }
1479
1480 //=======================================================================
1481 //function : DeviationAngle
1482 //purpose  : Gets  deviationAngle
1483 //=======================================================================
1484 Standard_Real AIS_InteractiveContext::DeviationAngle() const
1485 {
1486   return myDefaultDrawer->DeviationAngle();
1487 }
1488
1489 //=======================================================================
1490 //function : DeviationCoefficient
1491 //purpose  :
1492 //=======================================================================
1493 Standard_Real AIS_InteractiveContext::DeviationCoefficient() const
1494 {
1495   return myDefaultDrawer->DeviationCoefficient();
1496 }
1497
1498 //=======================================================================
1499 //function : SetHLRDeviationCoefficient
1500 //purpose  :
1501 //=======================================================================
1502 void AIS_InteractiveContext::SetHLRDeviationCoefficient (const Standard_Real theCoefficient)
1503 {
1504   myDefaultDrawer->SetHLRDeviationCoefficient (theCoefficient);
1505 }
1506
1507 //=======================================================================
1508 //function : HLRDeviationCoefficient
1509 //purpose  :
1510 //=======================================================================
1511 Standard_Real AIS_InteractiveContext::HLRDeviationCoefficient() const
1512 {
1513   return myDefaultDrawer->HLRDeviationCoefficient();
1514 }
1515
1516 //=======================================================================
1517 //function : SetHLRAngle
1518 //purpose  :
1519 //=======================================================================
1520 void AIS_InteractiveContext::SetHLRAngle (const Standard_Real theAngle)
1521 {
1522   myDefaultDrawer->SetHLRAngle (theAngle);
1523 }
1524
1525 //=======================================================================
1526 //function : SetHLRAngleAndDeviation
1527 //purpose  : compute with anangle a HLRAngle and a HLRDeviationCoefficient 
1528 //           and set them in myHLRAngle and in myHLRDeviationCoefficient
1529 //           of myDefaultDrawer 
1530 //=======================================================================
1531 void AIS_InteractiveContext::SetHLRAngleAndDeviation (const Standard_Real theAngle)
1532 {
1533   Standard_Real anOutAngl, anOutDefl;
1534   HLRBRep::PolyHLRAngleAndDeflection (theAngle, anOutAngl, anOutDefl);
1535
1536   myDefaultDrawer->SetHLRAngle                (anOutAngl);
1537   myDefaultDrawer->SetHLRDeviationCoefficient (anOutDefl);
1538 }
1539
1540 //=======================================================================
1541 //function : HLRAngle
1542 //purpose  :
1543 //=======================================================================
1544 Standard_Real AIS_InteractiveContext::HLRAngle() const 
1545 {
1546   return myDefaultDrawer->HLRAngle();
1547 }
1548
1549 //=======================================================================
1550 //function : SetDisplayMode
1551 //purpose  :
1552 //=======================================================================
1553 void AIS_InteractiveContext::SetDisplayMode (const AIS_DisplayMode  theMode,
1554                                              const Standard_Boolean theToUpdateViewer)
1555 {
1556   if (theMode == myDisplayMode)
1557   {
1558     return;
1559   }
1560
1561   for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
1562   {
1563     Handle(AIS_InteractiveObject) anObj = anObjIter.Key();
1564     Standard_Boolean toProcess = anObj->IsKind (STANDARD_TYPE(AIS_Shape))
1565                               || anObj->IsKind (STANDARD_TYPE(AIS_ConnectedInteractive))
1566                               || anObj->IsKind (STANDARD_TYPE(AIS_MultipleConnectedInteractive));
1567     
1568     if (!toProcess
1569      ||  anObj->HasDisplayMode()
1570      || !anObj->AcceptDisplayMode (theMode))
1571     {
1572       continue;
1573     }
1574
1575     Handle(AIS_GlobalStatus) aStatus = anObjIter.Value();
1576     if (aStatus->IsDModeIn (myDisplayMode))
1577     {
1578       aStatus->RemoveDisplayMode (myDisplayMode);
1579     }
1580
1581     aStatus->AddDisplayMode (theMode);
1582     if (aStatus->GraphicStatus() == AIS_DS_Displayed)
1583     {
1584       myMainPM->Display (anObj, theMode);
1585       if (!myLastPicked.IsNull() && myLastPicked->Selectable() == anObj)
1586       {
1587         myMainPM->BeginImmediateDraw();
1588         myMainPM->Unhighlight (anObj, myDisplayMode);
1589         myMainPM->EndImmediateDraw (myMainVwr);
1590       }
1591       if (aStatus->IsSubIntensityOn())
1592       {
1593         myMainPM->Color (anObj, mySubIntensity, theMode);
1594       }
1595       myMainPM->SetVisibility (anObj, myDisplayMode, Standard_False);
1596     }
1597   }
1598
1599   myDisplayMode = theMode;
1600   if (theToUpdateViewer)
1601   {
1602     myMainVwr->Update();
1603   }
1604 }
1605
1606 //=======================================================================
1607 //function : SetDisplayMode
1608 //purpose  :
1609 //=======================================================================
1610 void AIS_InteractiveContext::SetDisplayMode (const Handle(AIS_InteractiveObject)& theIObj,
1611                                              const Standard_Integer               theMode,
1612                                              const Standard_Boolean               theToUpdateViewer)
1613 {
1614   if (!theIObj->HasInteractiveContext())
1615   {
1616     theIObj->SetContext(this);
1617   }
1618
1619   if (!myObjects.IsBound (theIObj))
1620   {
1621     theIObj->SetDisplayMode (theMode);
1622     return;
1623   }
1624   else if (!theIObj->AcceptDisplayMode (theMode))
1625   {
1626     return;
1627   }
1628
1629   Handle(AIS_GlobalStatus) aStatus = myObjects (theIObj);
1630   if (aStatus->GraphicStatus() != AIS_DS_Displayed)
1631   {
1632     theIObj->SetDisplayMode (theMode);
1633     return;
1634   }
1635
1636   // erase presentations for all display modes different from <aMode>
1637   TColStd_ListOfInteger aModesToRemove;
1638   for (TColStd_ListIteratorOfListOfInteger aDispModeIter (aStatus->DisplayedModes()); aDispModeIter.More(); aDispModeIter.Next())
1639   {
1640     const Standard_Integer anOldMode = aDispModeIter.Value();
1641     if (anOldMode != theMode)
1642     {
1643       aModesToRemove.Append (anOldMode);
1644       if (myMainPM->IsHighlighted (theIObj, anOldMode))
1645       {
1646         myMainPM->Unhighlight (theIObj, anOldMode);
1647       }
1648       myMainPM->SetVisibility (theIObj, anOldMode, Standard_False);
1649     }
1650   }
1651
1652   for (TColStd_ListIteratorOfListOfInteger aRemModeIter (aModesToRemove); aRemModeIter.More(); aRemModeIter.Next())
1653   {
1654     aStatus->RemoveDisplayMode (aRemModeIter.Value());
1655   }
1656
1657   if (!aStatus->IsDModeIn (theMode))
1658   {
1659     aStatus->AddDisplayMode (theMode);
1660   }
1661
1662   myMainPM->Display (theIObj, theMode);
1663   Standard_Integer aDispMode, aHiMode, aSelMode;
1664   GetDefModes (theIObj, aDispMode, aHiMode, aSelMode);
1665   if (aStatus->IsHilighted())
1666   {
1667     myMainPM->Highlight (theIObj, aHiMode);
1668   }
1669   if (aStatus->IsSubIntensityOn())
1670   {
1671     myMainPM->Color (theIObj, mySubIntensity, theMode);
1672   }
1673
1674   if (theToUpdateViewer)
1675   {
1676     myMainVwr->Update();
1677   }
1678   theIObj->SetDisplayMode (theMode);
1679 }
1680
1681 //=======================================================================
1682 //function : UnsetDisplayMode
1683 //purpose  :
1684 //=======================================================================
1685 void AIS_InteractiveContext::UnsetDisplayMode (const Handle(AIS_InteractiveObject)& theIObj,
1686                                                const Standard_Boolean               theToUpdateViewer)
1687 {
1688   if (theIObj.IsNull()
1689   || !theIObj->HasDisplayMode())
1690   {
1691     return;
1692   }
1693
1694   if (!myObjects.IsBound (theIObj))
1695   {
1696     theIObj->UnsetDisplayMode();
1697     return;
1698   }
1699
1700   const Standard_Integer anOldMode = theIObj->DisplayMode();
1701   if (myDisplayMode == anOldMode)
1702   {
1703     return;
1704   }
1705
1706   const Handle(AIS_GlobalStatus)& aStatus = myObjects (theIObj);
1707   aStatus->RemoveDisplayMode (anOldMode);
1708   if (!aStatus->IsDModeIn(myDisplayMode))
1709   {
1710     aStatus->AddDisplayMode (myDisplayMode);
1711   }
1712
1713   if (aStatus->GraphicStatus() == AIS_DS_Displayed)
1714   {
1715     if (myMainPM->IsHighlighted (theIObj, anOldMode))
1716     {
1717       myMainPM->Unhighlight (theIObj, anOldMode);
1718     }
1719     myMainPM->SetVisibility (theIObj, anOldMode, Standard_False);
1720     myMainPM->Display (theIObj, myDisplayMode);
1721
1722     Standard_Integer aDispMode, aHiMode, aSelMode;
1723     GetDefModes (theIObj, aDispMode, aHiMode, aSelMode);
1724     if (aStatus->IsHilighted())
1725     {
1726       myMainPM->Highlight (theIObj, aHiMode);
1727     }
1728     if (aStatus->IsSubIntensityOn())
1729     {
1730       myMainPM->Color (theIObj, mySubIntensity, myDisplayMode);
1731     }
1732
1733     if (theToUpdateViewer)
1734     {
1735       myMainVwr->Update();
1736     }
1737   }
1738
1739   theIObj->UnsetDisplayMode();
1740 }
1741
1742 //=======================================================================
1743 //function : SetCurrentFacingModel
1744 //purpose  :
1745 //=======================================================================
1746 void AIS_InteractiveContext::SetCurrentFacingModel (const Handle(AIS_InteractiveObject)& theIObj,
1747                                                     const Aspect_TypeOfFacingModel       theModel)
1748 {
1749   if (!theIObj.IsNull())
1750   {
1751     theIObj->SetCurrentFacingModel (theModel);
1752   }
1753 }
1754
1755 //=======================================================================
1756 //function : redisplayPrsRecModes
1757 //purpose  :
1758 //=======================================================================
1759 void AIS_InteractiveContext::redisplayPrsRecModes (const Handle(AIS_InteractiveObject)& theIObj,
1760                                                    const Standard_Boolean               theToUpdateViewer)
1761 {
1762   if (theIObj->RecomputeEveryPrs())
1763   {
1764     theIObj->Update (Standard_True);
1765     theIObj->UpdateSelection();
1766   }
1767   else
1768   {
1769     for (TColStd_ListIteratorOfListOfInteger aModes (theIObj->ListOfRecomputeModes()); aModes.More(); aModes.Next())
1770     {
1771       theIObj->Update (aModes.Value(), Standard_False);
1772     }
1773     theIObj->UpdateSelection();
1774     theIObj->SetRecomputeOk();
1775   }
1776
1777   if (theToUpdateViewer)
1778   {
1779     UpdateCurrentViewer();
1780   }
1781 }
1782
1783 //=======================================================================
1784 //function : redisplayPrsModes
1785 //purpose  :
1786 //=======================================================================
1787 void AIS_InteractiveContext::redisplayPrsModes (const Handle(AIS_InteractiveObject)& theIObj,
1788                                                 const Standard_Boolean               theToUpdateViewer)
1789 {
1790   if (theIObj->RecomputeEveryPrs())
1791   {
1792     theIObj->Update (Standard_True);
1793     theIObj->UpdateSelection();
1794   }
1795   else
1796   {
1797     TColStd_ListOfInteger aModes;
1798     theIObj->ToBeUpdated (aModes);
1799     for (TColStd_ListIteratorOfListOfInteger aModeIter (aModes); aModeIter.More(); aModeIter.Next())
1800     {
1801       theIObj->Update (aModeIter.Value(), Standard_False);
1802     }
1803     theIObj->SetRecomputeOk();
1804   }
1805
1806   if (theToUpdateViewer)
1807   {
1808     UpdateCurrentViewer();
1809   }
1810 }
1811
1812 //=======================================================================
1813 //function : SetColor
1814 //purpose  :
1815 //=======================================================================
1816 void AIS_InteractiveContext::SetColor (const Handle(AIS_InteractiveObject)& theIObj,
1817                                        const Quantity_NameOfColor           theColor,
1818                                        const Standard_Boolean               theToUpdateViewer)
1819 {
1820   SetColor (theIObj, Quantity_Color(theColor), theToUpdateViewer);
1821 }
1822
1823 //=======================================================================
1824 //function : SetColor
1825 //purpose  :
1826 //=======================================================================
1827 void AIS_InteractiveContext::SetColor (const Handle(AIS_InteractiveObject)& theIObj,
1828                                        const Quantity_Color&                theColor,
1829                                        const Standard_Boolean               theToUpdateViewer)
1830 {
1831   if (theIObj.IsNull())
1832   {
1833     return;
1834   }
1835
1836   if (!theIObj->HasInteractiveContext())
1837   {
1838     theIObj->SetContext (this);
1839   }
1840   theIObj->SetColor (theColor);
1841   redisplayPrsRecModes (theIObj, theToUpdateViewer);
1842 }
1843
1844 //=======================================================================
1845 //function : SetDeviationCoefficient
1846 //purpose  :
1847 //=======================================================================
1848 void AIS_InteractiveContext::SetDeviationCoefficient (const Handle(AIS_InteractiveObject)& theIObj,
1849                                                       const Standard_Real                  theCoefficient,
1850                                                       const Standard_Boolean               theToUpdateViewer)
1851 {
1852   if (theIObj.IsNull())
1853   {
1854     return;
1855   }
1856
1857   if (!theIObj->HasInteractiveContext())
1858   {
1859     theIObj->SetContext (this);
1860   }
1861
1862   // to be modified after the related methods of AIS_Shape are passed to InteractiveObject
1863   if (theIObj->Type() != AIS_KOI_Object
1864    && theIObj->Type() != AIS_KOI_Shape)
1865   {
1866     return;
1867   }
1868   else if (theIObj->Signature() != 0)
1869   {
1870     return;
1871   }
1872
1873   Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (theIObj);
1874   aShape->SetOwnDeviationCoefficient (theCoefficient);
1875   redisplayPrsModes (theIObj, theToUpdateViewer);
1876 }
1877
1878 //=======================================================================
1879 //function : SetHLRDeviationCoefficient
1880 //purpose  :
1881 //=======================================================================
1882 void AIS_InteractiveContext::SetHLRDeviationCoefficient (const Handle(AIS_InteractiveObject)& theIObj,
1883                                                          const Standard_Real                  theCoefficient,
1884                                                          const Standard_Boolean               theToUpdateViewer)
1885 {
1886   if (theIObj.IsNull())
1887   {
1888     return;
1889   }
1890
1891   if (!theIObj->HasInteractiveContext())
1892   {
1893     theIObj->SetContext (this);
1894   }
1895  
1896   // To be modified after the related methods of AIS_Shape are passed to InteractiveObject
1897   if (theIObj->Type() != AIS_KOI_Object
1898    && theIObj->Type() != AIS_KOI_Shape)
1899   {
1900     return;
1901   }
1902   else if (theIObj->Signature() != 0)
1903   {
1904     return;
1905   }
1906
1907   Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (theIObj);
1908   aShape->SetOwnHLRDeviationCoefficient (theCoefficient);
1909   redisplayPrsModes (theIObj, theToUpdateViewer);
1910 }
1911
1912 //=======================================================================
1913 //function : SetDeviationAngle
1914 //purpose  :
1915 //=======================================================================
1916 void AIS_InteractiveContext::SetDeviationAngle (const Handle(AIS_InteractiveObject)& theIObj,
1917                                                 const Standard_Real                  theAngle,
1918                                                 const Standard_Boolean               theToUpdateViewer)
1919 {
1920   if (theIObj.IsNull())
1921   {
1922     return;
1923   }
1924
1925   if (!theIObj->HasInteractiveContext())
1926   {
1927     theIObj->SetContext (this);
1928   }
1929  
1930   // To be modified after the related methods of AIS_Shape are passed to InteractiveObject
1931   if (theIObj->Type() != AIS_KOI_Shape)
1932   {
1933     return;
1934   }
1935   else if (theIObj->Signature() != 0)
1936   {
1937     return;
1938   }
1939
1940   Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (theIObj);
1941   aShape->SetOwnDeviationAngle (theAngle);
1942   redisplayPrsModes (theIObj, theToUpdateViewer);
1943 }
1944
1945 //=======================================================================
1946 //function : SetAngleAndDeviation
1947 //purpose  :
1948 //=======================================================================
1949 void AIS_InteractiveContext::SetAngleAndDeviation (const Handle(AIS_InteractiveObject)& theIObj,
1950                                                    const Standard_Real                  theAngle,
1951                                                    const Standard_Boolean               theToUpdateViewer)
1952 {
1953   if (theIObj.IsNull())
1954   {
1955     return;
1956   }
1957
1958   if (!theIObj->HasInteractiveContext())
1959   {
1960     theIObj->SetContext (this);
1961   }
1962
1963   // To be modified after the related methods of AIS_Shape are passed to InteractiveObject
1964   if (theIObj->Type() != AIS_KOI_Shape)
1965   {
1966     return;
1967   }
1968   if (theIObj->Signature() != 0)
1969   {
1970     return;
1971   }
1972
1973   Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (theIObj);
1974   aShape->SetAngleAndDeviation (theAngle);
1975
1976   if (theIObj->RecomputeEveryPrs())
1977   {
1978     theIObj->Update (Standard_True);
1979     theIObj->UpdateSelection();
1980   }
1981   else
1982   {
1983     Update (theIObj, theToUpdateViewer);
1984   }
1985 }
1986
1987 //=======================================================================
1988 //function : SetHLRAngleAndDeviation
1989 //purpose  :
1990 //=======================================================================
1991 void AIS_InteractiveContext::SetHLRAngleAndDeviation (const Handle(AIS_InteractiveObject)& theIObj,
1992                                                       const Standard_Real                  theAngle,
1993                                                       const Standard_Boolean               theToUpdateViewer)
1994 {
1995   if (theIObj.IsNull())
1996   {
1997     return;
1998   }
1999
2000   if (!theIObj->HasInteractiveContext())
2001   {
2002     theIObj->SetContext (this);
2003   }
2004
2005   // To be modified after the related methods of AIS_Shape are passed to InteractiveObject
2006   if (theIObj->Type() != AIS_KOI_Shape)
2007   {
2008     return;
2009   }
2010   if (theIObj->Signature() != 0)
2011   {
2012     return;
2013   }
2014   Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (theIObj);
2015   aShape->SetHLRAngleAndDeviation (theAngle);
2016   redisplayPrsModes (theIObj, theToUpdateViewer);
2017 }
2018
2019 //=======================================================================
2020 //function : SetHLRDeviationAngle
2021 //purpose  :
2022 //=======================================================================
2023 void AIS_InteractiveContext::SetHLRDeviationAngle (const Handle(AIS_InteractiveObject)& theIObj,
2024                                                    const Standard_Real                  theAngle,
2025                                                    const Standard_Boolean               theToUpdateViewer)
2026 {
2027   if (theIObj.IsNull())
2028   {
2029     return;
2030   }
2031
2032   if (!theIObj->HasInteractiveContext())
2033   {
2034     theIObj->SetContext (this);
2035   }
2036
2037   // To be modified after the related methods of AIS_Shape are passed to InteractiveObject
2038   if (theIObj->Type() != AIS_KOI_Shape)
2039   {
2040     return;
2041   }
2042   if (theIObj->Signature() != 0)
2043   {
2044     return;
2045   }
2046   Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (theIObj);
2047   aShape->SetOwnHLRDeviationAngle (theAngle);
2048   redisplayPrsModes (theIObj, theToUpdateViewer);
2049 }
2050
2051 //=======================================================================
2052 //function : UnsetColor
2053 //purpose  :
2054 //=======================================================================
2055 void AIS_InteractiveContext::UnsetColor (const Handle(AIS_InteractiveObject)& theIObj,
2056                                          const Standard_Boolean               theToUpdateViewer)
2057 {
2058   if (theIObj.IsNull())
2059   {
2060     return;
2061   }
2062
2063   theIObj->UnsetColor();
2064   redisplayPrsRecModes (theIObj, theToUpdateViewer);
2065 }
2066
2067 //=======================================================================
2068 //function : HasColor
2069 //purpose  :
2070 //=======================================================================
2071 Standard_Boolean AIS_InteractiveContext::HasColor (const Handle(AIS_InteractiveObject)& theIObj) const
2072 {
2073   return theIObj->HasColor();
2074 }
2075
2076 //=======================================================================
2077 //function : Color
2078 //purpose  :
2079 //=======================================================================
2080 Quantity_NameOfColor AIS_InteractiveContext::Color (const Handle(AIS_InteractiveObject)& theIObj) const
2081 {
2082   return theIObj->Color();
2083 }
2084
2085 //=======================================================================
2086 //function : Color
2087 //purpose  :
2088 //=======================================================================
2089 void AIS_InteractiveContext::Color (const Handle(AIS_InteractiveObject)& theIObj,
2090                                     Quantity_Color&                      theColor) const
2091 {
2092   theIObj->Color (theColor);
2093 }
2094
2095 //=======================================================================
2096 //function : Width
2097 //purpose  :
2098 //=======================================================================
2099 Standard_Real AIS_InteractiveContext::Width (const Handle(AIS_InteractiveObject)& theIObj) const
2100 {
2101   return theIObj->Width();
2102 }
2103
2104 //=======================================================================
2105 //function : SetWidth
2106 //purpose  :
2107 //=======================================================================
2108 void AIS_InteractiveContext::SetWidth (const Handle(AIS_InteractiveObject)& theIObj,
2109                                        const Standard_Real                  theWidth,
2110                                        const Standard_Boolean               theToUpdateViewer)
2111 {
2112   if (theIObj.IsNull())
2113   {
2114     return;
2115   }
2116
2117   if (!theIObj->HasInteractiveContext())
2118   {
2119     theIObj->SetContext (this);
2120   }
2121
2122   theIObj->SetWidth (theWidth);
2123   redisplayPrsRecModes (theIObj, theToUpdateViewer);
2124   if (!myLastinMain.IsNull() && myLastinMain->Selectable() == theIObj)
2125   {
2126     if (myLastinMain->IsAutoHilight())
2127     {
2128       const Standard_Integer aHiMode =
2129         theIObj->HasHilightMode() ? theIObj->HilightMode() : 0;
2130       myLastinMain->HilightWithColor (myMainPM, myLastinMain->IsSelected() ? mySelectionColor : myHilightColor, aHiMode);
2131     }
2132     else
2133     {
2134       theIObj->HilightOwnerWithColor (myMainPM, myLastinMain->IsSelected() ? mySelectionColor : myHilightColor, myLastinMain);
2135     }
2136   }
2137 }
2138
2139 //=======================================================================
2140 //function : UnsetWidth
2141 //purpose  :
2142 //=======================================================================
2143 void AIS_InteractiveContext::UnsetWidth (const Handle(AIS_InteractiveObject)& theIObj,
2144                                          const Standard_Boolean               theToUpdateViewer)
2145 {
2146   if (theIObj.IsNull())
2147   {
2148     return;
2149   }
2150
2151   theIObj->UnsetWidth();
2152   redisplayPrsRecModes (theIObj, theToUpdateViewer);
2153 }
2154
2155 //=======================================================================
2156 //function : SetMaterial
2157 //purpose  :
2158 //=======================================================================
2159 void AIS_InteractiveContext::SetMaterial (const Handle(AIS_InteractiveObject)& theIObj,
2160                                           const Graphic3d_NameOfMaterial       theName,
2161                                           const Standard_Boolean               theToUpdateViewer)
2162 {
2163   if (theIObj.IsNull())
2164   {
2165     return;
2166   }
2167
2168   if (!theIObj->HasInteractiveContext())
2169   {
2170     theIObj->SetContext (this);
2171   }
2172
2173   theIObj->SetMaterial (theName);
2174   redisplayPrsRecModes (theIObj, theToUpdateViewer);
2175 }
2176
2177 //=======================================================================
2178 //function : UnsetMaterial
2179 //purpose  :
2180 //=======================================================================
2181 void AIS_InteractiveContext::UnsetMaterial (const Handle(AIS_InteractiveObject)& theIObj,
2182                                             const Standard_Boolean               theToUpdateViewer)
2183 {
2184   if (theIObj.IsNull())
2185   {
2186     return;
2187   }
2188   theIObj->UnsetMaterial();
2189   redisplayPrsRecModes (theIObj, theToUpdateViewer);
2190 }
2191
2192 //=======================================================================
2193 //function : SetTransparency
2194 //purpose  :
2195 //=======================================================================
2196 void AIS_InteractiveContext::SetTransparency (const Handle(AIS_InteractiveObject)& theIObj,
2197                                               const Standard_Real                  theValue,
2198                                               const Standard_Boolean               theToUpdateViewer)
2199 {
2200   if (theIObj.IsNull())
2201   {
2202     return;
2203   }
2204
2205   if (!theIObj->HasInteractiveContext())
2206   {
2207     theIObj->SetContext (this);
2208   }
2209
2210   if (!theIObj->IsTransparent()
2211     && theValue <= 0.05)
2212   {
2213     return;
2214   }
2215
2216   if (theValue <= 0.05)
2217   {
2218     UnsetTransparency (theIObj, theToUpdateViewer);
2219     return;
2220   }
2221
2222   theIObj->SetTransparency (theValue);
2223   redisplayPrsRecModes (theIObj, theToUpdateViewer);
2224 }
2225
2226 //=======================================================================
2227 //function : UnsetTransparency
2228 //purpose  :
2229 //=======================================================================
2230 void AIS_InteractiveContext::UnsetTransparency (const Handle(AIS_InteractiveObject)& theIObj,
2231                                                 const Standard_Boolean               theToUpdateViewer)
2232 {
2233   if (theIObj.IsNull())
2234   {
2235     return;
2236   }
2237
2238   theIObj->UnsetTransparency();
2239   redisplayPrsRecModes (theIObj, theToUpdateViewer);
2240 }
2241
2242 //=======================================================================
2243 //function : SetSelectedAspect
2244 //purpose  :
2245 //=======================================================================
2246 void AIS_InteractiveContext::SetSelectedAspect (const Handle(Prs3d_BasicAspect)& theAspect,
2247                                                 const Standard_Boolean           theIsGlobalChange,
2248                                                 const Standard_Boolean           theToUpdateViewer)
2249 {
2250   if (HasOpenedContext())
2251   {
2252     return;
2253   }
2254
2255   Standard_Boolean isFound = Standard_False;
2256   Handle(AIS_Selection) aSelIter = AIS_Selection::Selection (myCurrentName.ToCString());
2257   for (aSelIter->Init(); aSelIter->More(); aSelIter->Next())
2258   {
2259     isFound = Standard_True;
2260     Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (aSelIter->Value());
2261     anObj->SetAspect (theAspect, theIsGlobalChange);
2262   }
2263
2264   if (isFound
2265    && theToUpdateViewer)
2266   {
2267     myMainVwr->Update();
2268   }
2269 }
2270
2271 //=======================================================================
2272 //function : SetLocalAttributes
2273 //purpose  :
2274 //=======================================================================
2275 void AIS_InteractiveContext::SetLocalAttributes (const Handle(AIS_InteractiveObject)& theIObj,
2276                                                  const Handle(Prs3d_Drawer)&          theDrawer,
2277                                                  const Standard_Boolean               theToUpdateViewer)
2278 {
2279   if (theIObj.IsNull())
2280   {
2281     return;
2282   }
2283
2284   if (!theIObj->HasInteractiveContext())
2285   {
2286     theIObj->SetContext (this);
2287   }
2288
2289   theIObj->SetAttributes (theDrawer);
2290   Update (theIObj, theToUpdateViewer);
2291 }
2292
2293 //=======================================================================
2294 //function : UnsetLocalAttributes
2295 //purpose  :
2296 //=======================================================================
2297 void AIS_InteractiveContext::UnsetLocalAttributes (const Handle(AIS_InteractiveObject)& theIObj,
2298                                                    const Standard_Boolean               theToUpdateViewer)
2299 {
2300   if (theIObj.IsNull())
2301   {
2302     return;
2303   }
2304
2305   if (!theIObj->HasInteractiveContext())
2306   {
2307     theIObj->SetContext (this);
2308   }
2309   theIObj->UnsetAttributes();
2310   Update (theIObj, theToUpdateViewer);
2311 }
2312
2313 //=======================================================================
2314 //function : Status
2315 //purpose  :
2316 //=======================================================================
2317 void AIS_InteractiveContext::Status (const Handle(AIS_InteractiveObject)& theIObj,
2318                                      TCollection_ExtendedString&          theStatus) const
2319 {
2320   theStatus = "";
2321   if (theIObj.IsNull()
2322   || !myObjects.IsBound (theIObj))
2323   {
2324     return;
2325   }
2326
2327   theStatus += "\t ____________________________________________";
2328   theStatus += "\t| Known at Neutral Point:\n\tDisplayStatus:";
2329   const Handle(AIS_GlobalStatus)& aStatus = myObjects (theIObj);
2330   switch (aStatus->GraphicStatus())
2331   {
2332     case AIS_DS_Displayed:
2333     {
2334       theStatus += "\t| -->Displayed\n";
2335       break;
2336     }
2337     case AIS_DS_Erased:
2338     {
2339       theStatus += "\t| -->Erased\n";
2340       break;
2341     }
2342     default:
2343       break;
2344   }
2345
2346   theStatus += "\t| Active Display Modes in the MainViewer :\n";
2347   for (TColStd_ListIteratorOfListOfInteger aDispModeIter (aStatus->DisplayedModes()); aDispModeIter.More(); aDispModeIter.Next())
2348   {
2349     theStatus += "\t|\t Mode ";
2350     theStatus += TCollection_AsciiString (aDispModeIter.Value());
2351     theStatus += "\n";
2352   }
2353   if (IsSelected(theIObj)) theStatus +="\t| Selected\n";
2354
2355   theStatus += "\t| Active Selection Modes in the MainViewer :\n";
2356   for (TColStd_ListIteratorOfListOfInteger aSelModeIter (aStatus->SelectionModes()); aSelModeIter.More(); aSelModeIter.Next())
2357   {
2358     theStatus += "\t\t Mode ";
2359     theStatus += TCollection_AsciiString (aSelModeIter.Value());
2360     theStatus += "\n";
2361   }
2362   theStatus += "\t ____________________________________________";
2363 }
2364
2365 //=======================================================================
2366 //function : GetDefModes
2367 //purpose  :
2368 //=======================================================================
2369 void AIS_InteractiveContext::GetDefModes (const Handle(AIS_InteractiveObject)& theIObj,
2370                                           Standard_Integer&                    theDispMode,
2371                                           Standard_Integer&                    theHiMode,
2372                                           Standard_Integer&                    theSelMode) const
2373 {
2374   if (theIObj.IsNull())
2375   {
2376     return;
2377   }
2378
2379   theDispMode = theIObj->HasDisplayMode()
2380               ? theIObj->DisplayMode()
2381               : (theIObj->AcceptDisplayMode (myDisplayMode)
2382                ? myDisplayMode
2383                : 0);
2384   theHiMode  = theIObj->HasHilightMode()   ? theIObj->HilightMode()   : theDispMode;
2385   theSelMode = theIObj->GlobalSelectionMode();
2386 }
2387
2388 //=======================================================================
2389 //function : EraseGlobal
2390 //purpose  :
2391 //=======================================================================
2392 void AIS_InteractiveContext::EraseGlobal (const Handle(AIS_InteractiveObject)& theIObj,
2393                                           const Standard_Boolean               theToUpdateviewer)
2394 {
2395   if (theIObj.IsNull()
2396   || !myObjects.IsBound (theIObj))
2397   {
2398     return;
2399   }
2400
2401   Handle(AIS_GlobalStatus) aStatus = myObjects (theIObj);
2402
2403   const Standard_Integer aDispMode = theIObj->HasHilightMode() ? theIObj->HilightMode() : 0;
2404   if (aStatus->GraphicStatus() == AIS_DS_Temporary
2405    || aStatus->GraphicStatus() == AIS_DS_Erased)
2406   {
2407     return;
2408   }
2409
2410   for (TColStd_ListIteratorOfListOfInteger aDispModeIter (aStatus->DisplayedModes()); aDispModeIter.More(); aDispModeIter.Next())
2411   {
2412     if (aStatus->IsHilighted())
2413     {
2414       if (IsCurrent (theIObj))
2415       {
2416         AddOrRemoveCurrentObject (theIObj, Standard_False);
2417       }
2418       else if (myMainPM->IsHighlighted (theIObj, aDispModeIter.Value()))
2419       {
2420         myMainPM->Unhighlight (theIObj, aDispModeIter.Value());
2421       }
2422     }
2423
2424     myMainPM->SetVisibility (theIObj, aDispModeIter.Value(), Standard_False);
2425   }
2426
2427   if (IsSelected (theIObj)
2428   && !aStatus->IsDModeIn (aDispMode))
2429   {
2430     myMainPM->SetVisibility (theIObj, aDispMode, Standard_False);
2431   }
2432
2433   for (TColStd_ListIteratorOfListOfInteger aSelModeIter (aStatus->SelectionModes()); aSelModeIter.More(); aSelModeIter.Next())
2434   {
2435     mgrSelector->Deactivate (theIObj, aSelModeIter.Value(), myMainSel);
2436   }
2437   aStatus->ClearSelectionModes();
2438   aStatus->SetGraphicStatus (AIS_DS_Erased);
2439
2440   if (theToUpdateviewer)
2441   {
2442     myMainVwr->Update();
2443   }
2444 }
2445
2446 //=======================================================================
2447 //function : unhighlightOwners
2448 //purpose  :
2449 //=======================================================================
2450 void AIS_InteractiveContext::unhighlightOwners (const Handle(AIS_InteractiveObject)& theObject)
2451 {
2452   Handle(AIS_Selection) aSel = AIS_Selection::Selection (myCurrentName.ToCString());
2453   aSel->Init();
2454   while (aSel->More())
2455   {
2456     const Handle(SelectMgr_EntityOwner) anOwner =
2457       Handle(SelectMgr_EntityOwner)::DownCast (aSel->Value());
2458     if (anOwner->Selectable() == theObject)
2459     {
2460       if (anOwner->IsSelected())
2461       {
2462         AddOrRemoveSelected (anOwner, Standard_False);
2463         aSel->Init();
2464         continue;
2465       }
2466     }
2467     aSel->Next();
2468   }
2469 }
2470
2471 //=======================================================================
2472 //function : ClearGlobal
2473 //purpose  :
2474 //=======================================================================
2475 void AIS_InteractiveContext::ClearGlobal (const Handle(AIS_InteractiveObject)& theIObj,
2476                                           const Standard_Boolean               theToUpdateviewer)
2477 {
2478   if (theIObj.IsNull()
2479   || !myObjects.IsBound (theIObj))
2480   {
2481     // for cases when reference shape of connected interactives was not displayed
2482     // but its selection primitives were calculated
2483     const Handle(SelectMgr_SelectableObject)& anObj = theIObj; // to avoid ambiguity
2484     mgrSelector->Remove (anObj);
2485     return;
2486   }
2487
2488   Handle(AIS_GlobalStatus) aStatus = myObjects (theIObj);
2489   unhighlightOwners (theIObj);
2490   for (TColStd_ListIteratorOfListOfInteger aDispModeIter (aStatus->DisplayedModes()); aDispModeIter.More(); aDispModeIter.Next())
2491   {
2492     myMainPM->Erase (theIObj, aDispModeIter.Value());
2493     myMainPM->Clear (theIObj, aDispModeIter.Value());
2494   }
2495
2496   // Object removes from Detected sequence
2497   for(Standard_Integer aDetIter = 1; aDetIter < myAISDetectedSeq.Length(); ++aDetIter)
2498   {
2499     Handle(AIS_InteractiveObject) anObj = DetectedCurrentObject();
2500     if (!anObj.IsNull()
2501       && anObj != theIObj)
2502     {
2503       myAISDetectedSeq.Remove (aDetIter);
2504     }
2505   }
2506
2507   // remove IO from the selection manager to avoid memory leaks
2508   const Handle(SelectMgr_SelectableObject)& anObj = theIObj; // to avoid ambiguity
2509   mgrSelector->Remove (anObj);
2510
2511   myObjects.UnBind (theIObj);
2512   myMainVwr->Viewer()->UnregisterObject (theIObj);
2513   for (myMainVwr->InitDefinedViews(); myMainVwr->MoreDefinedViews(); myMainVwr->NextDefinedViews())
2514   {
2515     myMainVwr->DefinedView()->View()->ChangeHiddenObjects()->Remove (theIObj.get());
2516   }
2517
2518   if (!myLastinMain.IsNull() && myLastinMain->Selectable() == theIObj)
2519     myLastinMain.Nullify();
2520   if (!myLastPicked.IsNull() && myLastPicked->Selectable() == theIObj)
2521     myLastPicked.Nullify();
2522   myMainPM->ClearImmediateDraw();
2523
2524   if (theToUpdateviewer && aStatus->GraphicStatus() == AIS_DS_Displayed)
2525   {
2526     myMainVwr->Update();
2527   }
2528 }
2529
2530 //=======================================================================
2531 //function : ClearGlobalPrs
2532 //purpose  :
2533 //=======================================================================
2534 void AIS_InteractiveContext::ClearGlobalPrs (const Handle(AIS_InteractiveObject)& theIObj,
2535                                              const Standard_Integer               theMode,
2536                                              const Standard_Boolean               theToUpdateViewer)
2537 {
2538   if (theIObj.IsNull()
2539   || !myObjects.IsBound (theIObj))
2540   {
2541     return;
2542   }
2543
2544   const Handle(AIS_GlobalStatus)& aStatus = myObjects (theIObj);
2545   if (aStatus->IsDModeIn (theMode))
2546   {
2547     const Standard_Integer aDispMode = theIObj->HasHilightMode() ? theIObj->HilightMode() : 0;
2548     if (aDispMode == theMode
2549      && myMainPM->IsHighlighted (theIObj, theMode))
2550     {
2551       myMainPM->Unhighlight (theIObj, theMode);
2552     }
2553
2554     myMainPM->Erase (theIObj, theMode);
2555     myMainPM->Clear (theIObj, theMode);
2556   }
2557
2558   if (aStatus->GraphicStatus() == AIS_DS_Displayed
2559    && theToUpdateViewer)
2560   {
2561     myMainVwr->Update();
2562   }
2563 }
2564
2565 //=======================================================================
2566 //function : DrawHiddenLine
2567 //purpose  :
2568 //=======================================================================
2569 Standard_Boolean AIS_InteractiveContext::DrawHiddenLine() const
2570 {
2571   return myDefaultDrawer->DrawHiddenLine();
2572 }
2573
2574 //=======================================================================
2575 //function : EnableDrawHiddenLine
2576 //purpose  :
2577 //=======================================================================
2578 void AIS_InteractiveContext::EnableDrawHiddenLine() const
2579 {
2580   myDefaultDrawer->EnableDrawHiddenLine();
2581 }
2582
2583 //=======================================================================
2584 //function : DisableDrawHiddenLine 
2585 //purpose  :
2586 //=======================================================================
2587 void AIS_InteractiveContext::DisableDrawHiddenLine() const
2588 {
2589   myDefaultDrawer->DisableDrawHiddenLine();
2590 }
2591
2592 //=======================================================================
2593 //function : HiddenLineAspect
2594 //purpose  :
2595 //=======================================================================
2596 Handle (Prs3d_LineAspect) AIS_InteractiveContext::HiddenLineAspect() const
2597 {
2598   return myDefaultDrawer->HiddenLineAspect();
2599 }
2600
2601 //=======================================================================
2602 //function : SetHiddenLineAspect
2603 //purpose  :
2604 //=======================================================================
2605 void AIS_InteractiveContext::SetHiddenLineAspect (const Handle(Prs3d_LineAspect)& theAspect) const
2606 {
2607   myDefaultDrawer->SetHiddenLineAspect (theAspect);
2608 }
2609
2610 //=======================================================================
2611 //function : SetIsoNumber
2612 //purpose  :
2613 //=======================================================================
2614 void AIS_InteractiveContext::SetIsoNumber (const Standard_Integer theNb,
2615                                            const AIS_TypeOfIso    theType)
2616 {
2617   switch (theType)
2618   {
2619     case AIS_TOI_IsoU:
2620       myDefaultDrawer->UIsoAspect()->SetNumber (theNb);
2621       break;
2622     case AIS_TOI_IsoV:
2623       myDefaultDrawer->VIsoAspect()->SetNumber (theNb);
2624       break;
2625     case AIS_TOI_Both:
2626       myDefaultDrawer->UIsoAspect()->SetNumber (theNb);
2627       myDefaultDrawer->VIsoAspect()->SetNumber (theNb);
2628       break;
2629   }
2630 }
2631
2632 //=======================================================================
2633 //function : IsoNumber
2634 //purpose  :
2635 //=======================================================================
2636 Standard_Integer AIS_InteractiveContext::IsoNumber (const AIS_TypeOfIso theType)
2637 {
2638   switch (theType)
2639   {
2640     case AIS_TOI_IsoU: return myDefaultDrawer->UIsoAspect()->Number();
2641     case AIS_TOI_IsoV: return myDefaultDrawer->VIsoAspect()->Number();
2642     case AIS_TOI_Both: return myDefaultDrawer->UIsoAspect()->Number() == myDefaultDrawer->VIsoAspect()->Number()
2643                             ? myDefaultDrawer->UIsoAspect()->Number()
2644                             : -1;
2645   }
2646   return 0;
2647 }
2648
2649 //=======================================================================
2650 //function : IsoOnPlane
2651 //purpose  :
2652 //=======================================================================
2653 void AIS_InteractiveContext::IsoOnPlane (const Standard_Boolean theToSwitchOn)
2654 {
2655   myDefaultDrawer->SetIsoOnPlane (theToSwitchOn);
2656 }
2657
2658 //=======================================================================
2659 //function : IsoOnPlane
2660 //purpose  :
2661 //=======================================================================
2662 Standard_Boolean AIS_InteractiveContext::IsoOnPlane() const
2663 {
2664   return myDefaultDrawer->IsoOnPlane();
2665 }
2666
2667 //=======================================================================
2668 //function : SetPixelTolerance
2669 //purpose  : Disables the mechanism of adaptive tolerance calculation in
2670 //           SelectMgr_ViewerSelector and sets the given tolerance for ALL
2671 //           sensitive entities activated. For more information, see
2672 //           SelectMgr_ViewerSelector.hxx
2673 //=======================================================================
2674 void AIS_InteractiveContext::SetPixelTolerance (const Standard_Real thePrecision)
2675 {
2676   if (HasOpenedContext())
2677   {
2678     myLocalContexts (myCurLocalIndex)->SetPixelTolerance (thePrecision);
2679   }
2680   else
2681   {
2682     myMainSel->SetPixelTolerance (thePrecision);
2683   }
2684 }
2685
2686 //=======================================================================
2687 //function : PixelTolerance
2688 //purpose  :
2689 //=======================================================================
2690 Standard_Real AIS_InteractiveContext::PixelTolerance() const
2691 {
2692   return HasOpenedContext()
2693        ? myLocalContexts (myCurLocalIndex)->PixelTolerance()
2694        : myMainSel->PixelTolerance();
2695 }
2696
2697 //=======================================================================
2698 //function : IsInLocal
2699 //purpose  :
2700 //=======================================================================
2701 Standard_Boolean AIS_InteractiveContext::IsInLocal (const Handle(AIS_InteractiveObject)& theIObj,
2702                                                     Standard_Integer&                    theIndex) const
2703 {
2704   if (theIObj.IsNull())
2705   {
2706     return Standard_False;
2707   }
2708
2709   // if it exists at neutral point 0 index is returned
2710   if (myObjects.IsBound (theIObj))
2711   {
2712     theIndex = 0;
2713     return Standard_False;
2714   }
2715
2716   for (Standard_Integer aCtxIter = 1; aCtxIter <= myLocalContexts.Extent(); ++aCtxIter)
2717   {
2718     if (myLocalContexts.IsBound (aCtxIter))
2719     {
2720       if(myLocalContexts (aCtxIter)->IsIn (theIObj))
2721       {
2722         theIndex = aCtxIter;
2723         return Standard_True;
2724       }
2725     }
2726   }
2727   theIndex = -1;
2728   return Standard_False;
2729 }
2730
2731 //=======================================================================
2732 //function : InitAttributes
2733 //purpose  :
2734 //=======================================================================
2735 void AIS_InteractiveContext::InitAttributes()
2736 {
2737   mgrSelector->Add (myMainSel);
2738   myCurrentName   = AIS_Context_NewCurName();
2739   mySelectionName = AIS_Context_NewSelName();
2740
2741   AIS_Selection::CreateSelection (mySelectionName.ToCString());
2742   AIS_Selection::CreateSelection (myCurrentName.ToCString());
2743
2744   myDefaultDrawer->SetShadingAspectGlobal (Standard_False);
2745   Graphic3d_MaterialAspect aMat (Graphic3d_NOM_BRASS);
2746   myDefaultDrawer->ShadingAspect()->SetMaterial (aMat);
2747
2748 //  myDefaultDrawer->ShadingAspect()->SetColor(Quantity_NOC_GRAY70);
2749   Handle(Prs3d_LineAspect) aLineAspect = myDefaultDrawer->HiddenLineAspect();
2750   aLineAspect->SetColor      (Quantity_NOC_GRAY20);
2751   aLineAspect->SetWidth      (1.0);
2752   aLineAspect->SetTypeOfLine (Aspect_TOL_DASH);
2753
2754   // tolerance to 2 pixels...
2755   SetPixelTolerance (2.0);
2756
2757   // Customizing the drawer for trihedrons and planes...
2758   Handle(Prs3d_DatumAspect) aTrihAspect = myDefaultDrawer->DatumAspect();
2759   const Standard_Real aLength = 100.0;
2760   aTrihAspect->SetAxisLength (aLength, aLength, aLength);
2761   const Quantity_NameOfColor aColor = Quantity_NOC_LIGHTSTEELBLUE4;
2762   aTrihAspect->FirstAxisAspect() ->SetColor (aColor);
2763   aTrihAspect->SecondAxisAspect()->SetColor (aColor);
2764   aTrihAspect->ThirdAxisAspect() ->SetColor (aColor);
2765
2766   Handle(Prs3d_PlaneAspect) aPlaneAspect = myDefaultDrawer->PlaneAspect();
2767   const Standard_Real aPlaneLength = 200.0;
2768   aPlaneAspect->SetPlaneLength (aPlaneLength, aPlaneLength);
2769   aPlaneAspect->EdgesAspect()->SetColor (Quantity_NOC_SKYBLUE);
2770 }
2771
2772 //=======================================================================
2773 //function : TrihedronSize
2774 //purpose  :
2775 //=======================================================================
2776 Standard_Real AIS_InteractiveContext::TrihedronSize() const
2777 {
2778   return myDefaultDrawer->DatumAspect()->FirstAxisLength();
2779 }
2780
2781 //=======================================================================
2782 //function : SetTrihedronSize
2783 //purpose  :
2784 //=======================================================================
2785 void AIS_InteractiveContext::SetTrihedronSize (const Standard_Real    theVal,
2786                                                const Standard_Boolean /*updateviewer*/)
2787 {
2788   myDefaultDrawer->DatumAspect()->SetAxisLength (theVal, theVal, theVal);
2789   Redisplay (AIS_KOI_Datum, 3, Standard_False);
2790   Redisplay (AIS_KOI_Datum, 4, Standard_True);
2791 }
2792
2793 //=======================================================================
2794 //function : SetPlaneSize
2795 //purpose  :
2796 //=======================================================================
2797 void AIS_InteractiveContext::SetPlaneSize(const Standard_Real    theValX,
2798                                           const Standard_Real    theValY,
2799                                           const Standard_Boolean /*updateviewer*/)
2800 {
2801   myDefaultDrawer->PlaneAspect()->SetPlaneLength (theValX, theValY);
2802   Redisplay (AIS_KOI_Datum, 7);
2803 }
2804
2805 //=======================================================================
2806 //function : SetPlaneSize
2807 //purpose  :
2808 //=======================================================================
2809 void AIS_InteractiveContext::SetPlaneSize (const Standard_Real    theVal,
2810                                            const Standard_Boolean theToUpdateViewer)
2811 {
2812   SetPlaneSize (theVal, theVal, theToUpdateViewer);
2813 }
2814
2815 //=======================================================================
2816 //function : PlaneSize
2817 //purpose  :
2818 //=======================================================================
2819 Standard_Boolean AIS_InteractiveContext::PlaneSize (Standard_Real& theX,
2820                                                     Standard_Real& theY) const
2821 {
2822   theX = myDefaultDrawer->PlaneAspect()->PlaneXLength();
2823   theY = myDefaultDrawer->PlaneAspect()->PlaneYLength();
2824   return (Abs (theX - theY) <= Precision::Confusion());
2825 }
2826
2827 //=======================================================================
2828 //function : SetAutoActivateSelection
2829 //purpose  :
2830 //=======================================================================
2831 void AIS_InteractiveContext::SetAutoActivateSelection (const Standard_Boolean theIsAuto)
2832 {
2833   myIsAutoActivateSelMode = theIsAuto;
2834 }
2835
2836 //=======================================================================
2837 //function : GetAutoActivateSelection
2838 //purpose  :
2839 //=======================================================================
2840 Standard_Boolean AIS_InteractiveContext::GetAutoActivateSelection() const
2841 {
2842   return myIsAutoActivateSelMode;
2843 }
2844
2845 //=======================================================================
2846 //function : SetZLayer
2847 //purpose  :
2848 //=======================================================================
2849 void AIS_InteractiveContext::SetZLayer (const Handle(AIS_InteractiveObject)& theIObj,
2850                                         const Standard_Integer theLayerId)
2851 {
2852   if (theIObj.IsNull())
2853     return;
2854
2855   theIObj->SetZLayer (theLayerId);
2856 }
2857
2858 //=======================================================================
2859 //function : GetZLayer
2860 //purpose  :
2861 //=======================================================================
2862 Standard_Integer AIS_InteractiveContext::GetZLayer (const Handle(AIS_InteractiveObject)& theIObj) const
2863 {
2864   return !theIObj.IsNull()
2865        ?  theIObj->ZLayer()
2866        :  Graphic3d_ZLayerId_UNKNOWN;
2867 }
2868
2869 //=======================================================================
2870 //function : RebuildSelectionStructs
2871 //purpose  : Rebuilds 1st level of BVH selection forcibly
2872 //=======================================================================
2873 void AIS_InteractiveContext::RebuildSelectionStructs()
2874 {
2875   myMainSel->RebuildObjectsTree (Standard_True);
2876 }
2877
2878 //=======================================================================
2879 //function : Disconnect
2880 //purpose  : Disconnects selectable object from an assembly and updates selection structures
2881 //=======================================================================
2882 void AIS_InteractiveContext::Disconnect (const Handle(AIS_InteractiveObject)& theAssembly,
2883                                          const Handle(AIS_InteractiveObject)& theObjToDisconnect)
2884 {
2885   if (theAssembly->IsInstance ("AIS_MultipleConnectedInteractive"))
2886   {
2887     Handle(AIS_MultipleConnectedInteractive) theObj (Handle(AIS_MultipleConnectedInteractive)::DownCast (theAssembly));
2888     theObj->Disconnect (theObjToDisconnect);
2889     const Handle(SelectMgr_SelectableObject)& anObj = theObjToDisconnect; // to avoid ambiguity
2890     mgrSelector->Remove (anObj);
2891   }
2892   else if (theAssembly->IsInstance ("AIS_ConnectedInteractive") && theObjToDisconnect.IsNull())
2893   {
2894     Handle(AIS_ConnectedInteractive) theObj (Handle(AIS_ConnectedInteractive)::DownCast (theAssembly));
2895     theObj->Disconnect();
2896     const Handle(SelectMgr_SelectableObject)& anObj = theObj; // to avoid ambiguity
2897     mgrSelector->Remove (anObj);
2898   }
2899   else
2900     return;
2901 }
2902
2903 //=======================================================================
2904 //function : FitSelected
2905 //purpose  : Fits the view corresponding to the bounds of selected objects
2906 //=======================================================================
2907 void AIS_InteractiveContext::FitSelected (const Handle(V3d_View)& theView,
2908                                           const Standard_Real theMargin,
2909                                           const Standard_Boolean theToUpdate)
2910 {
2911   Standard_CString aSelName = HasOpenedContext() ?
2912       myLocalContexts (myCurLocalIndex)->SelectionName().ToCString()
2913     : myCurrentName.ToCString();
2914
2915   Bnd_Box aBndSelected;
2916
2917   const Handle(AIS_Selection)& aSelection = AIS_Selection::Selection (aSelName);
2918   AIS_MapOfObjectOwners anObjectOwnerMap;
2919   for (aSelection->Init(); aSelection->More(); aSelection->Next())
2920   {
2921     Handle(AIS_InteractiveObject) anObj (Handle(AIS_InteractiveObject)::DownCast (aSelection->Value()));
2922     if (!anObj.IsNull())
2923     {
2924       if (anObj->IsInfinite())
2925         continue;
2926
2927       Bnd_Box aTmpBnd;
2928       anObj->BoundingBox (aTmpBnd);
2929       aBndSelected.Add (aTmpBnd);
2930     }
2931     else
2932     {
2933       Handle(SelectMgr_EntityOwner) anOwner (Handle(SelectMgr_EntityOwner)::DownCast (aSelection->Value()));
2934       if (anOwner.IsNull())
2935         continue;
2936
2937       Handle(SelectMgr_IndexedMapOfOwner) anOwnerMap;
2938       if (!anObjectOwnerMap.Find (anOwner->Selectable(), anOwnerMap))
2939       {
2940         anOwnerMap = new SelectMgr_IndexedMapOfOwner();
2941         anObjectOwnerMap.Bind (anOwner->Selectable(), anOwnerMap);
2942       }
2943
2944       anOwnerMap->Add (anOwner);
2945     }
2946   }
2947
2948   for (AIS_MapIteratorOfMapOfObjectOwners anIter (anObjectOwnerMap); anIter.More(); anIter.Next())
2949   {
2950     const Handle(SelectMgr_SelectableObject) anObject = anIter.Key();
2951     Bnd_Box aTmpBox = anObject->BndBoxOfSelected (anIter.ChangeValue());
2952     aBndSelected.Add (aTmpBox);
2953   }
2954
2955   anObjectOwnerMap.Clear();
2956
2957   if (aBndSelected.IsVoid())
2958     return;
2959
2960   theView->FitAll (aBndSelected, theMargin, theToUpdate);
2961 }