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