fee11abb9f38211a3723a90fee460617791468e9
[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 #include <AIS_InteractiveContext.hxx>
18
19 #include <AIS_DataMapIteratorOfDataMapOfIOStatus.hxx>
20 #include <AIS_ConnectedInteractive.hxx>
21 #include <AIS_GlobalStatus.hxx>
22 #include <AIS_InteractiveObject.hxx>
23 #include <AIS_MultipleConnectedInteractive.hxx>
24 #include <Graphic3d_AspectFillArea3d.hxx>
25 #include <Precision.hxx>
26 #include <Prs3d_BasicAspect.hxx>
27 #include <Prs3d_DatumAspect.hxx>
28 #include <Prs3d_IsoAspect.hxx>
29 #include <Prs3d_LineAspect.hxx>
30 #include <Prs3d_PlaneAspect.hxx>
31 #include <Prs3d_PointAspect.hxx>
32 #include <Prs3d_ShadingAspect.hxx>
33 #include <SelectMgr_EntityOwner.hxx>
34 #include <SelectMgr_SelectionManager.hxx>
35 #include <TColStd_ListIteratorOfListOfInteger.hxx>
36 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
37 #include <TopLoc_Location.hxx>
38 #include <V3d_View.hxx>
39 #include <V3d_Viewer.hxx>
40
41 #include <AIS_Shape.hxx>
42 #include <StdSelect_BRepOwner.hxx>
43 #include <TopoDS_Shape.hxx>
44
45 IMPLEMENT_STANDARD_RTTIEXT(AIS_InteractiveContext, Standard_Transient)
46
47 namespace
48 {
49   typedef NCollection_DataMap<Handle(SelectMgr_SelectableObject), Handle(SelectMgr_IndexedMapOfOwner)> AIS_MapOfObjectOwners;
50   typedef NCollection_DataMap<Handle(SelectMgr_SelectableObject), Handle(SelectMgr_IndexedMapOfOwner)>::Iterator AIS_MapIteratorOfMapOfObjectOwners;
51
52   //! Initialize default highlighting attributes.
53   static void initDefaultHilightAttributes (const Handle(Prs3d_Drawer)& theDrawer,
54                                             const Quantity_Color& theColor)
55   {
56     theDrawer->SetMethod (Aspect_TOHM_COLOR);
57     theDrawer->SetDisplayMode (0);
58     theDrawer->SetColor (theColor);
59
60     theDrawer->SetupOwnShadingAspect();
61     theDrawer->SetupOwnPointAspect();
62     theDrawer->SetLineAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
63     *theDrawer->LineAspect()->Aspect() = *theDrawer->Link()->LineAspect()->Aspect();
64     theDrawer->SetWireAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
65     *theDrawer->WireAspect()->Aspect() = *theDrawer->Link()->WireAspect()->Aspect();
66     theDrawer->SetPlaneAspect (new Prs3d_PlaneAspect());
67     *theDrawer->PlaneAspect()->EdgesAspect() = *theDrawer->Link()->PlaneAspect()->EdgesAspect();
68     theDrawer->SetFreeBoundaryAspect   (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
69     *theDrawer->FreeBoundaryAspect()->Aspect() = *theDrawer->Link()->FreeBoundaryAspect()->Aspect();
70     theDrawer->SetUnFreeBoundaryAspect (new Prs3d_LineAspect (Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0));
71     *theDrawer->UnFreeBoundaryAspect()->Aspect() = *theDrawer->Link()->UnFreeBoundaryAspect()->Aspect();
72     theDrawer->SetDatumAspect (new Prs3d_DatumAspect());
73
74     theDrawer->ShadingAspect()->SetColor (theColor);
75     theDrawer->WireAspect()->SetColor (theColor);
76     theDrawer->LineAspect()->SetColor (theColor);
77     theDrawer->PlaneAspect()->ArrowAspect()->SetColor (theColor);
78     theDrawer->PlaneAspect()->IsoAspect()->SetColor (theColor);
79     theDrawer->PlaneAspect()->EdgesAspect()->SetColor (theColor);
80     theDrawer->FreeBoundaryAspect()->SetColor (theColor);
81     theDrawer->UnFreeBoundaryAspect()->SetColor (theColor);
82     theDrawer->PointAspect()->SetColor (theColor);
83     for (Standard_Integer aPartIter = 0; aPartIter < Prs3d_DatumParts_None; ++aPartIter)
84     {
85       if (Handle(Prs3d_LineAspect) aLineAsp = theDrawer->DatumAspect()->LineAspect ((Prs3d_DatumParts )aPartIter))
86       {
87         aLineAsp->SetColor (theColor);
88       }
89     }
90
91     theDrawer->WireAspect()->SetWidth (2.0);
92     theDrawer->LineAspect()->SetWidth (2.0);
93     theDrawer->PlaneAspect()->EdgesAspect()->SetWidth (2.0);
94     theDrawer->FreeBoundaryAspect()  ->SetWidth (2.0);
95     theDrawer->UnFreeBoundaryAspect()->SetWidth (2.0);
96     theDrawer->PointAspect()->SetTypeOfMarker (Aspect_TOM_O_POINT);
97     theDrawer->PointAspect()->SetScale (2.0);
98
99     // the triangulation should be computed using main presentation attributes,
100     // and should not be overridden by highlighting
101     theDrawer->SetAutoTriangulation (Standard_False);
102   }
103 }
104
105 //=======================================================================
106 //function : AIS_InteractiveContext
107 //purpose  : 
108 //=======================================================================
109
110 AIS_InteractiveContext::AIS_InteractiveContext(const Handle(V3d_Viewer)& MainViewer):
111 myMainPM (new PrsMgr_PresentationManager (MainViewer->StructureManager())),
112 myMainVwr(MainViewer),
113 myMainSel(new StdSelect_ViewerSelector3d()),
114 myToHilightSelected(Standard_True),
115 mySelection(new AIS_Selection()),
116 myFilters (new SelectMgr_AndOrFilter(SelectMgr_FilterType_OR)),
117 myDefaultDrawer(new Prs3d_Drawer()),
118 myCurDetected(0),
119 myCurHighlighted(0),
120 myPickingStrategy (SelectMgr_PickingStrategy_FirstAcceptable),
121 myAutoHilight(Standard_True),
122 myIsAutoActivateSelMode(Standard_True)
123 {
124   mgrSelector = new SelectMgr_SelectionManager (myMainSel);
125
126   myStyles[Prs3d_TypeOfHighlight_None]          = myDefaultDrawer;
127   myStyles[Prs3d_TypeOfHighlight_Selected]      = new Prs3d_Drawer();
128   myStyles[Prs3d_TypeOfHighlight_Dynamic]       = new Prs3d_Drawer();
129   myStyles[Prs3d_TypeOfHighlight_LocalSelected] = new Prs3d_Drawer();
130   myStyles[Prs3d_TypeOfHighlight_LocalDynamic]  = new Prs3d_Drawer();
131   myStyles[Prs3d_TypeOfHighlight_SubIntensity]  = new Prs3d_Drawer();
132
133   myDefaultDrawer->SetZLayer(Graphic3d_ZLayerId_Default);
134   myDefaultDrawer->SetDisplayMode(0);
135   {
136     const Handle(Prs3d_Drawer)& aStyle = myStyles[Prs3d_TypeOfHighlight_Dynamic];
137     aStyle->Link (myDefaultDrawer);
138     initDefaultHilightAttributes (aStyle, Quantity_NOC_CYAN1);
139     aStyle->SetZLayer(Graphic3d_ZLayerId_Top);
140   }
141   {
142     const Handle(Prs3d_Drawer)& aStyle = myStyles[Prs3d_TypeOfHighlight_LocalDynamic];
143     aStyle->Link (myDefaultDrawer);
144     initDefaultHilightAttributes (aStyle, Quantity_NOC_CYAN1);
145     aStyle->SetZLayer(Graphic3d_ZLayerId_Topmost);
146   }
147   {
148     const Handle(Prs3d_Drawer)& aStyle = myStyles[Prs3d_TypeOfHighlight_Selected];
149     aStyle->Link (myDefaultDrawer);
150     initDefaultHilightAttributes (aStyle, Quantity_NOC_GRAY80);
151     aStyle->SetZLayer(Graphic3d_ZLayerId_UNKNOWN);
152   }
153   {
154     const Handle(Prs3d_Drawer)& aStyle = myStyles[Prs3d_TypeOfHighlight_LocalSelected];
155     aStyle->Link (myDefaultDrawer);
156     initDefaultHilightAttributes (aStyle, Quantity_NOC_GRAY80);
157     aStyle->SetZLayer(Graphic3d_ZLayerId_UNKNOWN);
158   }
159   {
160     const Handle(Prs3d_Drawer)& aStyle = myStyles[Prs3d_TypeOfHighlight_SubIntensity];
161     aStyle->SetZLayer(Graphic3d_ZLayerId_UNKNOWN);
162     aStyle->SetMethod(Aspect_TOHM_COLOR);
163     aStyle->SetColor (Quantity_NOC_GRAY40);
164   }
165
166   InitAttributes();
167 }
168
169 //=======================================================================
170 //function : ~AIS_InteractiveContext
171 //purpose  :
172 //=======================================================================
173 AIS_InteractiveContext::~AIS_InteractiveContext()
174 {
175   // clear the current selection
176   mySelection->Clear();
177   mgrSelector.Nullify();
178
179   Handle(AIS_InteractiveContext) aNullContext;
180   for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
181   {
182     const Handle(AIS_InteractiveObject)& anObj = anObjIter.Key();
183     anObj->SetContext (aNullContext);
184     for (SelectMgr_SequenceOfSelection::Iterator aSelIter (anObj->Selections()); aSelIter.More(); aSelIter.Next())
185     {
186       aSelIter.Value()->UpdateBVHStatus (SelectMgr_TBU_Renew);
187     }
188   }
189 }
190
191 //=======================================================================
192 //function : LastActiveView
193 //purpose  :
194 //=======================================================================
195 Handle(V3d_View) AIS_InteractiveContext::LastActiveView() const
196 {
197   if (myLastActiveView == NULL
198    || myMainVwr.IsNull())
199   {
200     return Handle(V3d_View)();
201   }
202
203   // as a precaution - check that myLastActiveView pointer is a valid active View
204   for (V3d_ListOfViewIterator aViewIter = myMainVwr->ActiveViewIterator(); aViewIter.More(); aViewIter.Next())
205   {
206     if (aViewIter.Value() == myLastActiveView)
207     {
208       return aViewIter.Value();
209     }
210   }
211   return Handle(V3d_View)();
212 }
213
214 //=======================================================================
215 //function : UpdateCurrentViewer
216 //purpose  : 
217 //=======================================================================
218
219 void AIS_InteractiveContext::UpdateCurrentViewer()
220 {
221   if (!myMainVwr.IsNull())
222     myMainVwr->Update();
223 }
224
225 //=======================================================================
226 //function : DisplayedObjects
227 //purpose  :
228 //=======================================================================
229 void AIS_InteractiveContext::DisplayedObjects (AIS_ListOfInteractive& theListOfIO) const
230 {
231   for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
232   {
233     if (anObjIter.Key()->DisplayStatus() == PrsMgr_DisplayStatus_Displayed)
234     {
235       theListOfIO.Append (anObjIter.Key());
236     }
237   }
238 }
239
240 //=======================================================================
241 //function : DisplayedObjects
242 //purpose  :
243 //=======================================================================
244 void AIS_InteractiveContext::DisplayedObjects (const AIS_KindOfInteractive theKind,
245                                                const Standard_Integer      theSign,
246                                                AIS_ListOfInteractive&      theListOfIO) const
247 {
248   ObjectsByDisplayStatus (theKind, theSign, PrsMgr_DisplayStatus_Displayed, theListOfIO);
249 }
250
251 //=======================================================================
252 //function : ErasedObjects
253 //purpose  :
254 //=======================================================================
255 void AIS_InteractiveContext::ErasedObjects (AIS_ListOfInteractive& theListOfIO) const
256 {
257   ObjectsByDisplayStatus (PrsMgr_DisplayStatus_Erased, theListOfIO);
258 }
259
260 //=======================================================================
261 //function : ErasedObjects
262 //purpose  :
263 //=======================================================================
264 void AIS_InteractiveContext::ErasedObjects (const AIS_KindOfInteractive theKind,
265                                             const Standard_Integer      theSign,
266                                             AIS_ListOfInteractive&      theListOfIO) const
267 {
268   ObjectsByDisplayStatus (theKind, theSign, PrsMgr_DisplayStatus_Erased, theListOfIO);
269 }
270
271 //=======================================================================
272 //function : ObjectsByDisplayStatus
273 //purpose  :
274 //=======================================================================
275 void AIS_InteractiveContext::ObjectsByDisplayStatus (const PrsMgr_DisplayStatus theStatus,
276                                                      AIS_ListOfInteractive&  theListOfIO) const
277 {
278   for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
279   {
280     if (anObjIter.Key()->DisplayStatus() == theStatus)
281     {
282       theListOfIO.Append (anObjIter.Key());
283     }
284   }
285 }
286
287 //=======================================================================
288 //function : ObjectsByDisplayStatus
289 //purpose  :
290 //=======================================================================
291 void AIS_InteractiveContext::ObjectsByDisplayStatus (const AIS_KindOfInteractive theKind,
292                                                      const Standard_Integer      theSign,
293                                                      const PrsMgr_DisplayStatus  theStatus,
294                                                      AIS_ListOfInteractive&      theListOfIO) const
295 {
296   for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
297   {
298     if (theStatus != PrsMgr_DisplayStatus_None
299      && anObjIter.Key()->DisplayStatus() != theStatus)
300     {
301       continue;
302     }
303     else if (anObjIter.Key()->Type() != theKind)
304     {
305       continue;
306     }
307
308     if (theSign == -1
309      || anObjIter.Key()->Signature() == theSign)
310     {
311       theListOfIO.Append (anObjIter.Key());
312     }
313   }
314 }
315
316 //=======================================================================
317 //function : ObjectsInside
318 //purpose  :
319 //=======================================================================
320 void AIS_InteractiveContext::ObjectsInside (AIS_ListOfInteractive&      theListOfIO,
321                                             const AIS_KindOfInteractive theKind,
322                                             const Standard_Integer      theSign) const
323 {
324   if (theKind == AIS_KindOfInteractive_None
325    && theSign == -1)
326   {
327     for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
328     {
329       theListOfIO.Append (anObjIter.Key());
330     }
331     return;
332   }
333
334   for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
335   {
336     if (anObjIter.Key()->Type() != theKind)
337     {
338       continue;
339     }
340
341     if (theSign == -1
342      || anObjIter.Key()->Signature() == theSign)
343     {
344       theListOfIO.Append (anObjIter.Key());
345     }
346   }
347 }
348
349 //=======================================================================
350 //function : ObjectsForView
351 //purpose  :
352 //=======================================================================
353 void AIS_InteractiveContext::ObjectsForView (AIS_ListOfInteractive&  theListOfIO,
354                                              const Handle(V3d_View)& theView,
355                                              const Standard_Boolean  theIsVisibleInView,
356                                              const PrsMgr_DisplayStatus theStatus) const
357 {
358   Handle(Graphic3d_CView) aViewImpl = theView->View();
359   const Standard_Integer  aViewId   = aViewImpl->Identification();
360   for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
361   {
362     if (theStatus != PrsMgr_DisplayStatus_None
363      && anObjIter.Key()->DisplayStatus() != theStatus)
364     {
365       theListOfIO.Append (anObjIter.Key());
366       continue;
367     }
368
369     Handle(Graphic3d_ViewAffinity) anAffinity = myMainVwr->StructureManager()->ObjectAffinity (anObjIter.Key());
370     const Standard_Boolean isVisible = anAffinity->IsVisible (aViewId);
371     if (isVisible == theIsVisibleInView)
372     {
373       theListOfIO.Append (anObjIter.Key());
374     }
375   }
376 }
377
378 //=======================================================================
379 //function : Display
380 //purpose  :
381 //=======================================================================
382 void AIS_InteractiveContext::Display (const Handle(AIS_InteractiveObject)& theIObj,
383                                       const Standard_Boolean               theToUpdateViewer)
384 {
385   if (theIObj.IsNull())
386   {
387     return;
388   }
389
390   Standard_Integer aDispMode = 0, aHiMod = -1, aSelMode = -1;
391   GetDefModes (theIObj, aDispMode, aHiMod, aSelMode);
392   Display (theIObj, aDispMode, myIsAutoActivateSelMode ? aSelMode : -1, theToUpdateViewer);
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->StructureManager()->ObjectAffinity (theIObj);
410   Handle(Graphic3d_CView) aViewImpl = theView->View();
411   anAffinity->SetVisible (aViewImpl->Identification(), 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 PrsMgr_DisplayStatus           theDispStatus)
431 {
432   if (theIObj.IsNull())
433   {
434     return;
435   }
436
437   if (theDispStatus == PrsMgr_DisplayStatus_Erased)
438   {
439     Erase  (theIObj, theToUpdateViewer);
440     Load (theIObj, theSelectionMode);
441     if (Handle(AIS_GlobalStatus)* aStatusPtr = myObjects.ChangeSeek (theIObj))
442     {
443       (*aStatusPtr)->SetDisplayMode (theDispMode);
444     }
445     return;
446   }
447
448   setContextToObject (theIObj);
449   if (!myObjects.IsBound (theIObj))
450   {
451     setObjectStatus (theIObj, PrsMgr_DisplayStatus_Displayed, theDispMode, theSelectionMode);
452     myMainVwr->StructureManager()->RegisterObject (theIObj);
453     myMainPM->Display(theIObj, theDispMode);
454     if (theSelectionMode != -1)
455     {
456       const Handle(SelectMgr_SelectableObject)& anObj = theIObj; // to avoid ambiguity
457       if (!mgrSelector->Contains (anObj))
458       {
459         mgrSelector->Load (theIObj);
460       }
461       mgrSelector->Activate (theIObj, theSelectionMode);
462     }
463   }
464   else
465   {
466     Handle(AIS_GlobalStatus) aStatus = myObjects (theIObj);
467
468     // Mark the presentation modes hidden of interactive object different from aDispMode.
469     // Then make sure aDispMode is displayed and maybe highlighted.
470     // Finally, activate selection mode <SelMode> if not yet activated.
471     const Standard_Integer anOldMode = aStatus->DisplayMode();
472     if (anOldMode != theDispMode)
473     {
474       if(myMainPM->IsHighlighted (theIObj, anOldMode))
475       {
476         unhighlightGlobal (theIObj);
477       }
478       myMainPM->SetVisibility (theIObj, anOldMode, Standard_False);
479     }
480
481     aStatus->SetDisplayMode (theDispMode);
482
483     theIObj->SetDisplayStatus (PrsMgr_DisplayStatus_Displayed);
484     myMainPM->Display (theIObj, theDispMode);
485     if (aStatus->IsHilighted())
486     {
487       highlightGlobal (theIObj, aStatus->HilightStyle(), theDispMode);
488     }
489     if (theSelectionMode != -1)
490     {
491       const Handle(SelectMgr_SelectableObject)& anObj = theIObj; // to avoid ambiguity
492       if (!mgrSelector->Contains (anObj))
493       {
494         mgrSelector->Load (theIObj);
495       }
496       if (!mgrSelector->IsActivated (theIObj, theSelectionMode))
497       {
498         aStatus->AddSelectionMode (theSelectionMode);
499         mgrSelector->Activate (theIObj, theSelectionMode);
500       }
501     }
502   }
503
504   if (theToUpdateViewer)
505   {
506     myMainVwr->Update();
507   }
508 }
509
510 //=======================================================================
511 //function : Load
512 //purpose  :
513 //=======================================================================
514 void AIS_InteractiveContext::Load (const Handle(AIS_InteractiveObject)& theIObj,
515                                    const Standard_Integer               theSelMode)
516 {
517   if (theIObj.IsNull())
518   {
519     return;
520   }
521
522   setContextToObject (theIObj);
523   if (!myObjects.IsBound (theIObj))
524   {
525     Standard_Integer aDispMode, aHiMod, aSelModeDef;
526     GetDefModes (theIObj, aDispMode, aHiMod, aSelModeDef);
527     setObjectStatus (theIObj, PrsMgr_DisplayStatus_Erased, aDispMode, theSelMode != -1 ? theSelMode : aSelModeDef);
528     myMainVwr->StructureManager()->RegisterObject (theIObj);
529   }
530
531   // Register theIObj in the selection manager to prepare further activation of selection
532   const Handle(SelectMgr_SelectableObject)& anObj = theIObj; // to avoid ambiguity
533   if (!mgrSelector->Contains (anObj))
534   {
535     mgrSelector->Load (theIObj);
536   }
537 }
538
539 //=======================================================================
540 //function : Erase
541 //purpose  :
542 //=======================================================================
543 void AIS_InteractiveContext::Erase (const Handle(AIS_InteractiveObject)& theIObj,
544                                     const Standard_Boolean               theToUpdateViewer)
545 {
546   if (theIObj.IsNull())
547   {
548     return;
549   }
550   
551   if (!theIObj->IsAutoHilight())
552   {
553     theIObj->ClearSelected();
554   }
555
556   EraseGlobal (theIObj, Standard_False);
557   if (theToUpdateViewer)
558   {
559     myMainVwr->Update();
560   }
561 }
562
563 //=======================================================================
564 //function : EraseAll
565 //purpose  :
566 //=======================================================================
567 void AIS_InteractiveContext::EraseAll (const Standard_Boolean theToUpdateViewer)
568 {
569   for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
570   {
571     if (anObjIter.Key()->DisplayStatus() == PrsMgr_DisplayStatus_Displayed)
572     {
573       Erase (anObjIter.Key(), Standard_False);
574     }
575   }
576
577   if (theToUpdateViewer)
578   {
579     myMainVwr->Update();
580   }
581 }
582
583 //=======================================================================
584 //function : DisplayAll
585 //purpose  :
586 //=======================================================================
587 void AIS_InteractiveContext::DisplayAll (const Standard_Boolean theToUpdateViewer)
588 {
589   for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
590   {
591     const PrsMgr_DisplayStatus aStatus = anObjIter.Key()->DisplayStatus();
592     if (aStatus == PrsMgr_DisplayStatus_Erased)
593     {
594       Display (anObjIter.Key(), Standard_False);
595     }
596   }
597
598   if (theToUpdateViewer)
599   {
600     myMainVwr->Update();
601   }
602 }
603
604 //=======================================================================
605 //function : DisplaySelected
606 //purpose  :
607 //=======================================================================
608 void AIS_InteractiveContext::DisplaySelected (const Standard_Boolean theToUpdateViewer)
609 {
610   for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
611   {
612     Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (aSelIter.Value()->Selectable());
613     Display (anObj, Standard_False);
614   }
615
616   if (theToUpdateViewer && !mySelection->Objects().IsEmpty())
617   {
618     myMainVwr->Update();
619   }
620 }
621
622 //=======================================================================
623 //function : EraseSelected
624 //purpose  :
625 //=======================================================================
626 void AIS_InteractiveContext::EraseSelected (const Standard_Boolean theToUpdateViewer)
627 {
628   Standard_Boolean isFound = Standard_False;
629   for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Init (mySelection->Objects()))
630   {
631     Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (aSelIter.Value()->Selectable());
632     Erase (anObj, Standard_False);
633     isFound = Standard_True;
634   }
635
636   if (isFound && theToUpdateViewer)
637   {
638     myMainVwr->Update();
639   }
640 }
641
642 //=======================================================================
643 //function : DisplayStatus
644 //purpose  :
645 //=======================================================================
646 PrsMgr_DisplayStatus AIS_InteractiveContext::DisplayStatus (const Handle(AIS_InteractiveObject)& theIObj) const
647 {
648   if (theIObj.IsNull())
649   {
650     return PrsMgr_DisplayStatus_None;
651   }
652   const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theIObj);
653   return aStatus != NULL ? theIObj->DisplayStatus() : PrsMgr_DisplayStatus_None;
654 }
655
656 //=======================================================================
657 //function : Remove
658 //purpose  :
659 //=======================================================================
660 void AIS_InteractiveContext::Remove (const Handle(AIS_InteractiveObject)& theIObj,
661                                      const Standard_Boolean               theToUpdateViewer)
662 {
663   if (theIObj.IsNull())
664   {
665     return;
666   }
667
668   if (theIObj->HasInteractiveContext())
669   {
670     if (theIObj->myCTXPtr != this)
671     {
672       throw Standard_ProgramError("AIS_InteractiveContext - object has been displayed in another context!");
673     }
674     theIObj->SetContext (Handle(AIS_InteractiveContext)());
675   }
676   ClearGlobal (theIObj, theToUpdateViewer);
677 }
678
679 //=======================================================================
680 //function : RemoveAll
681 //purpose  :
682 //=======================================================================
683 void AIS_InteractiveContext::RemoveAll (const Standard_Boolean theToUpdateViewer)
684 {
685   ClearDetected();
686
687   AIS_ListOfInteractive aList;
688   ObjectsInside (aList);
689   for (AIS_ListOfInteractive::Iterator aListIterator (aList); aListIterator.More(); aListIterator.Next())
690   {
691     Remove (aListIterator.Value(), Standard_False);
692   }
693
694   if (theToUpdateViewer)
695   {
696     myMainVwr->Update();
697   }
698 }
699
700 //=======================================================================
701 //function : HilightWithColor
702 //purpose  : 
703 //=======================================================================
704 void AIS_InteractiveContext::HilightWithColor(const Handle(AIS_InteractiveObject)& theObj,
705                                               const Handle(Prs3d_Drawer)& theStyle,
706                                               const Standard_Boolean theIsToUpdate)
707 {
708   if (theObj.IsNull())
709     return;
710
711   setContextToObject (theObj);
712   if (!myObjects.IsBound (theObj))
713     return;
714
715   const Handle(AIS_GlobalStatus)& aStatus = myObjects (theObj);
716   aStatus->SetHilightStatus (Standard_True);
717
718   if (theObj->DisplayStatus() == PrsMgr_DisplayStatus_Displayed)
719   {
720     highlightGlobal (theObj, theStyle, aStatus->DisplayMode());
721     aStatus->SetHilightStyle (theStyle);
722   }
723
724   if (theIsToUpdate)
725     myMainVwr->Update();
726 }
727
728 //=======================================================================
729 //function : Unhilight
730 //purpose  : 
731 //=======================================================================
732 void AIS_InteractiveContext::Unhilight (const Handle(AIS_InteractiveObject)& theObj,
733                                         const Standard_Boolean theToUpdateViewer)
734 {
735   Handle(AIS_GlobalStatus)* aStatus = !theObj.IsNull() ? myObjects.ChangeSeek (theObj) : NULL;
736   if (aStatus == NULL)
737   {
738     return;
739   }
740
741   (*aStatus)->SetHilightStatus (Standard_False);
742   (*aStatus)->SetHilightStyle (Handle(Prs3d_Drawer)());
743   if (theObj->DisplayStatus() == PrsMgr_DisplayStatus_Displayed)
744   {
745     unhighlightGlobal (theObj);
746   }
747
748   if (theToUpdateViewer)
749   {
750     myMainVwr->Update();
751   }
752 }
753
754 //=======================================================================
755 //function : IsHilighted
756 //purpose  : Returns true if the objects global status is set to highlighted.
757 //=======================================================================
758 Standard_Boolean AIS_InteractiveContext::IsHilighted (const Handle(AIS_InteractiveObject)& theObj) const
759 {
760   const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theObj);
761   return aStatus != NULL
762       && (*aStatus)->IsHilighted();
763 }
764
765 //=======================================================================
766 //function : IsHilighted
767 //purpose  : Returns true if the owner is highlighted with selection style.
768 //=======================================================================
769 Standard_Boolean AIS_InteractiveContext::IsHilighted (const Handle(SelectMgr_EntityOwner)& theOwner) const
770 {
771   if (theOwner.IsNull() || !theOwner->HasSelectable())
772     return Standard_False;
773
774   const Handle(AIS_InteractiveObject) anObj =
775     Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
776
777   if (anObj->GlobalSelOwner() == theOwner)
778   {
779     if (!myObjects.IsBound (anObj))
780       return Standard_False;
781
782     return myObjects (anObj)->IsHilighted();
783   }
784
785   const Handle(Prs3d_Drawer)& aStyle = getSelStyle (anObj, theOwner);
786   const Standard_Integer aHiMode = getHilightMode (anObj, aStyle, -1);
787   return theOwner->IsHilighted (myMainPM, aHiMode);
788 }
789
790 //=======================================================================
791 //function : HighlightStyle
792 //purpose  :
793 //=======================================================================
794 Standard_Boolean AIS_InteractiveContext::HighlightStyle (const Handle(AIS_InteractiveObject)& theObj,
795                                                          Handle(Prs3d_Drawer)& theStyle) const
796 {
797   const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theObj);
798   if (aStatus != NULL
799    && (*aStatus)->IsHilighted())
800   {
801     theStyle = (*aStatus)->HilightStyle();
802     return Standard_True;
803   }
804
805   theStyle.Nullify();
806   return Standard_False;
807 }
808
809 //=======================================================================
810 //function : HighlightStyle
811 //purpose  :
812 //=======================================================================
813 Standard_Boolean AIS_InteractiveContext::HighlightStyle (const Handle(SelectMgr_EntityOwner)& theOwner,
814                                                          Handle(Prs3d_Drawer)& theStyle) const
815 {
816   if (theOwner.IsNull() || !theOwner->HasSelectable())
817     return Standard_False;
818
819   if (IsHilighted (theOwner))
820   {
821     const Handle(AIS_InteractiveObject) anObj =
822       Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
823     if (anObj->GlobalSelOwner() == theOwner)
824     {
825       theStyle = myObjects (anObj)->HilightStyle();
826     }
827     else
828     {
829       // since part selection style is not stored in global status,
830       // check if the object has own selection style. If not, it can
831       // only be highlighted with default selection style (because
832       // sub-intensity does not modify any selection states)
833       theStyle = getSelStyle (anObj, theOwner);
834     }
835     return Standard_True;
836   }
837   else
838   {
839     theStyle.Nullify();
840     return Standard_False;
841   }
842 }
843
844 //=======================================================================
845 //function : IsDisplayed
846 //purpose  : 
847 //=======================================================================
848
849 Standard_Boolean AIS_InteractiveContext::IsDisplayed(const Handle(AIS_InteractiveObject)& theObj) const 
850 {
851   if(theObj.IsNull()) return Standard_False;
852
853   const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theObj);
854   return aStatus != NULL
855       && theObj->DisplayStatus() == PrsMgr_DisplayStatus_Displayed;
856 }
857
858 //=======================================================================
859 //function : IsDisplayed
860 //purpose  :
861 //=======================================================================
862 Standard_Boolean AIS_InteractiveContext::IsDisplayed (const Handle(AIS_InteractiveObject)& theIObj,
863                                                       const Standard_Integer               theMode) const
864 {
865   if (theIObj.IsNull())
866   {
867     return Standard_False;
868   }
869
870   const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theIObj);
871   return aStatus != NULL
872       && theIObj->DisplayStatus() == PrsMgr_DisplayStatus_Displayed
873       && (*aStatus)->DisplayMode() == theMode;
874 }
875
876 //=======================================================================
877 //function : DisplayPriority
878 //purpose  :
879 //=======================================================================
880 Standard_Integer AIS_InteractiveContext::DisplayPriority (const Handle(AIS_InteractiveObject)& theIObj) const
881 {
882   if (theIObj.IsNull())
883   {
884     return -1;
885   }
886
887   const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theIObj);
888   if (aStatus != NULL
889    && (theIObj->DisplayStatus() == PrsMgr_DisplayStatus_Displayed
890     || theIObj->DisplayStatus() == PrsMgr_DisplayStatus_Erased))
891   {
892     Standard_Integer aDispMode = theIObj->HasDisplayMode()
893                                ? theIObj->DisplayMode()
894                                : (theIObj->AcceptDisplayMode (myDefaultDrawer->DisplayMode())
895                                 ? myDefaultDrawer->DisplayMode()
896                                 : 0);
897     return myMainPM->DisplayPriority (theIObj, aDispMode);
898   }
899   return 0;
900 }
901
902 //=======================================================================
903 //function : SetDisplayPriority
904 //purpose  :
905 //=======================================================================
906 void AIS_InteractiveContext::SetDisplayPriority (const Handle(AIS_InteractiveObject)& theIObj,
907                                                  const Standard_Integer               thePriority)
908 {
909   if (theIObj.IsNull())
910   {
911     return;
912   }
913
914   setContextToObject (theIObj);
915   const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theIObj);
916   if (aStatus != NULL
917    && (theIObj->DisplayStatus() == PrsMgr_DisplayStatus_Displayed
918     || theIObj->DisplayStatus() == PrsMgr_DisplayStatus_Erased))
919   {
920     Standard_Integer aDisplayMode = theIObj->HasDisplayMode()
921                                   ? theIObj->DisplayMode()
922                                   : (theIObj->AcceptDisplayMode (myDefaultDrawer->DisplayMode())
923                                     ? myDefaultDrawer->DisplayMode()
924                                     : 0);
925     myMainPM->SetDisplayPriority (theIObj, aDisplayMode, thePriority);
926   }
927 }
928
929 //=======================================================================
930 //function : Redisplay
931 //purpose  :
932 //=======================================================================
933 void AIS_InteractiveContext::Redisplay (const Handle(AIS_InteractiveObject)& theIObj,
934                                         const Standard_Boolean               theToUpdateViewer,
935                                         const Standard_Boolean               theAllModes)
936 {
937   RecomputePrsOnly (theIObj, theToUpdateViewer, theAllModes);
938   RecomputeSelectionOnly (theIObj);
939 }
940
941 //=======================================================================
942 //function : Redisplay
943 //purpose  :
944 //=======================================================================
945 void AIS_InteractiveContext::Redisplay (const AIS_KindOfInteractive theKOI,
946                                         const Standard_Integer    /*theSign*/,
947                                         const Standard_Boolean      theToUpdateViewer)
948 {
949   Standard_Boolean isRedisplayed = Standard_False;
950   for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
951   {
952     Handle(AIS_InteractiveObject) anObj = anObjIter.Key();
953     if (anObj->Type() != theKOI)
954     {
955       continue;
956     }
957
958     Redisplay (anObj, Standard_False);
959     isRedisplayed = anObjIter.Key()->DisplayStatus() == PrsMgr_DisplayStatus_Displayed
960                  || isRedisplayed;
961   }
962
963   if (theToUpdateViewer
964    && isRedisplayed)
965   {
966     myMainVwr->Update();
967   }
968 }
969
970 //=======================================================================
971 //function : RecomputePrsOnly
972 //purpose  :
973 //=======================================================================
974 void AIS_InteractiveContext::RecomputePrsOnly (const Handle(AIS_InteractiveObject)& theIObj,
975                                                const Standard_Boolean               theToUpdateViewer,
976                                                const Standard_Boolean               theAllModes)
977 {
978   if (theIObj.IsNull())
979   {
980     return;
981   }
982
983   theIObj->SetToUpdate();
984   theIObj->UpdatePresentations (theAllModes);
985   if (!theToUpdateViewer)
986   {
987     return;
988   }
989
990   const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theIObj);
991   if (aStatus != NULL
992    && theIObj->DisplayStatus() == PrsMgr_DisplayStatus_Displayed)
993   {
994     myMainVwr->Update();
995   }
996 }
997 //=======================================================================
998 //function : RecomputeSelectionOnly
999 //purpose  : 
1000 //=======================================================================
1001 void AIS_InteractiveContext::RecomputeSelectionOnly (const Handle(AIS_InteractiveObject)& theIO)
1002 {
1003   if (theIO.IsNull())
1004   {
1005     return;
1006   }
1007
1008   mgrSelector->RecomputeSelection (theIO);
1009
1010   const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theIO);
1011   if (aStatus == NULL
1012    || theIO->DisplayStatus() != PrsMgr_DisplayStatus_Displayed)
1013   {
1014     return;
1015   }
1016
1017   TColStd_ListOfInteger aModes;
1018   ActivatedModes (theIO, aModes);
1019   TColStd_ListIteratorOfListOfInteger aModesIter (aModes);
1020   for (; aModesIter.More(); aModesIter.Next())
1021   {
1022     mgrSelector->Activate (theIO, aModesIter.Value());
1023   }
1024 }
1025
1026 //=======================================================================
1027 //function : Update
1028 //purpose  :
1029 //=======================================================================
1030 void AIS_InteractiveContext::Update (const Handle(AIS_InteractiveObject)& theIObj,
1031                                      const Standard_Boolean               theUpdateViewer)
1032 {
1033   if (theIObj.IsNull())
1034   {
1035     return;
1036   }
1037
1038   theIObj->UpdatePresentations();
1039   mgrSelector->Update(theIObj);
1040
1041   if (theUpdateViewer)
1042   {
1043     const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theIObj);
1044     if (aStatus != NULL
1045      && theIObj->DisplayStatus() == PrsMgr_DisplayStatus_Displayed)
1046     {
1047       myMainVwr->Update();
1048     }
1049   }
1050 }
1051
1052 //=======================================================================
1053 //function : SetLocation
1054 //purpose  :
1055 //=======================================================================
1056 void AIS_InteractiveContext::SetLocation (const Handle(AIS_InteractiveObject)& theIObj,
1057                                           const TopLoc_Location&               theLoc)
1058 {
1059   if (theIObj.IsNull())
1060   {
1061     return;
1062   }
1063
1064   if (theIObj->HasTransformation()
1065    && theLoc.IsIdentity())
1066   {
1067     theIObj->ResetTransformation();
1068     mgrSelector->Update (theIObj, Standard_False);
1069     return;
1070   }
1071   else if (theLoc.IsIdentity())
1072   {
1073     return;
1074   }
1075
1076   // first reset the previous location to properly clean everything...
1077   if (theIObj->HasTransformation())
1078   {
1079     theIObj->ResetTransformation();
1080   }
1081
1082   theIObj->SetLocalTransformation (theLoc.Transformation());
1083
1084   mgrSelector->Update (theIObj, Standard_False);
1085
1086   // if the object or its part is highlighted dynamically, it is necessary to apply location transformation
1087   // to its highlight structure immediately
1088   if (!myLastPicked.IsNull() && myLastPicked->IsSameSelectable (theIObj))
1089   {
1090     const Standard_Integer aHiMod = theIObj->HasHilightMode() ? theIObj->HilightMode() : 0;
1091     myLastPicked->UpdateHighlightTrsf (myMainVwr,
1092                                        myMainPM,
1093                                        aHiMod);
1094   }
1095 }
1096
1097 //=======================================================================
1098 //function : ResetLocation
1099 //purpose  :
1100 //=======================================================================
1101 void AIS_InteractiveContext::ResetLocation (const Handle(AIS_InteractiveObject)& theIObj)
1102 {
1103   if (theIObj.IsNull())
1104   {
1105     return;
1106   }
1107
1108   theIObj->ResetTransformation();
1109   mgrSelector->Update (theIObj, Standard_False);
1110 }
1111
1112 //=======================================================================
1113 //function : HasLocation
1114 //purpose  :
1115 //=======================================================================
1116 Standard_Boolean AIS_InteractiveContext::HasLocation (const Handle(AIS_InteractiveObject)& theIObj) const
1117 {
1118   return !theIObj.IsNull()
1119        && theIObj->HasTransformation();
1120 }
1121
1122 //=======================================================================
1123 //function : Location
1124 //purpose  :
1125 //=======================================================================
1126 TopLoc_Location AIS_InteractiveContext::Location (const Handle(AIS_InteractiveObject)& theIObj) const
1127 {
1128   return theIObj->Transformation();
1129 }
1130
1131 //=======================================================================
1132 //function : SetDeviationCoefficient
1133 //purpose  :
1134 //=======================================================================
1135 void AIS_InteractiveContext::SetDeviationCoefficient (const Standard_Real theCoefficient)
1136 {
1137   myDefaultDrawer->SetDeviationCoefficient (theCoefficient);
1138 }
1139
1140 //=======================================================================
1141 //function : SetDeviationAngle
1142 //purpose  :
1143 //=======================================================================
1144 void AIS_InteractiveContext::SetDeviationAngle (const Standard_Real theAngle)
1145 {
1146   myDefaultDrawer->SetDeviationAngle (theAngle);
1147 }
1148
1149 //=======================================================================
1150 //function : DeviationAngle
1151 //purpose  : Gets  deviationAngle
1152 //=======================================================================
1153 Standard_Real AIS_InteractiveContext::DeviationAngle() const
1154 {
1155   return myDefaultDrawer->DeviationAngle();
1156 }
1157
1158 //=======================================================================
1159 //function : DeviationCoefficient
1160 //purpose  :
1161 //=======================================================================
1162 Standard_Real AIS_InteractiveContext::DeviationCoefficient() const
1163 {
1164   return myDefaultDrawer->DeviationCoefficient();
1165 }
1166
1167 //=======================================================================
1168 //function : SetDisplayMode
1169 //purpose  :
1170 //=======================================================================
1171 void AIS_InteractiveContext::SetDisplayMode(const Standard_Integer theMode,
1172                                             const Standard_Boolean theToUpdateViewer)
1173 {
1174   if (theMode == myDefaultDrawer->DisplayMode())
1175   {
1176     return;
1177   }
1178
1179   for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjIter (myObjects); anObjIter.More(); anObjIter.Next())
1180   {
1181     Handle(AIS_InteractiveObject) anObj = anObjIter.Key();
1182     Standard_Boolean toProcess = anObj->IsKind (STANDARD_TYPE(AIS_Shape))
1183                               || anObj->IsKind (STANDARD_TYPE(AIS_ConnectedInteractive))
1184                               || anObj->IsKind (STANDARD_TYPE(AIS_MultipleConnectedInteractive));
1185     
1186     if (!toProcess
1187      ||  anObj->HasDisplayMode()
1188      || !anObj->AcceptDisplayMode (theMode))
1189     {
1190       continue;
1191     }
1192
1193     Handle(AIS_GlobalStatus) aStatus = anObjIter.Value();
1194     aStatus->SetDisplayMode (theMode);
1195
1196     if (anObj->DisplayStatus() == PrsMgr_DisplayStatus_Displayed)
1197     {
1198       myMainPM->Display (anObj, theMode);
1199       if (!myLastPicked.IsNull() && myLastPicked->IsSameSelectable (anObj))
1200       {
1201         myMainPM->BeginImmediateDraw();
1202         unhighlightGlobal (anObj);
1203         myMainPM->EndImmediateDraw (myMainVwr);
1204       }
1205       if (aStatus->IsSubIntensityOn())
1206       {
1207         highlightWithSubintensity (anObj, theMode);
1208       }
1209       myMainPM->SetVisibility (anObj, myDefaultDrawer->DisplayMode(), Standard_False);
1210     }
1211   }
1212
1213   myDefaultDrawer->SetDisplayMode (theMode);
1214   if (theToUpdateViewer)
1215   {
1216     myMainVwr->Update();
1217   }
1218 }
1219
1220 //=======================================================================
1221 //function : SetDisplayMode
1222 //purpose  :
1223 //=======================================================================
1224 void AIS_InteractiveContext::SetDisplayMode (const Handle(AIS_InteractiveObject)& theIObj,
1225                                              const Standard_Integer               theMode,
1226                                              const Standard_Boolean               theToUpdateViewer)
1227 {
1228   setContextToObject (theIObj);
1229   if (!myObjects.IsBound (theIObj))
1230   {
1231     theIObj->SetDisplayMode (theMode);
1232     return;
1233   }
1234   else if (!theIObj->AcceptDisplayMode (theMode))
1235   {
1236     return;
1237   }
1238
1239   Handle(AIS_GlobalStatus) aStatus = myObjects (theIObj);
1240   if (theIObj->DisplayStatus() != PrsMgr_DisplayStatus_Displayed)
1241   {
1242     aStatus->SetDisplayMode (theMode);
1243     theIObj->SetDisplayMode (theMode);
1244     return;
1245   }
1246
1247   // erase presentations for all display modes different from <aMode>
1248   const Standard_Integer anOldMode = aStatus->DisplayMode();
1249   if (anOldMode != theMode)
1250   {
1251     if (myMainPM->IsHighlighted (theIObj, anOldMode))
1252     {
1253       unhighlightGlobal (theIObj);
1254     }
1255     myMainPM->SetVisibility (theIObj, anOldMode, Standard_False);
1256   }
1257
1258   aStatus->SetDisplayMode (theMode);
1259
1260   myMainPM->Display (theIObj, theMode);
1261   if (aStatus->IsHilighted())
1262   {
1263     highlightGlobal (theIObj, getSelStyle (theIObj, theIObj->GlobalSelOwner()), theMode);
1264   }
1265   if (aStatus->IsSubIntensityOn())
1266   {
1267     highlightWithSubintensity (theIObj, theMode);
1268   }
1269
1270   if (theToUpdateViewer)
1271   {
1272     myMainVwr->Update();
1273   }
1274   theIObj->SetDisplayMode (theMode);
1275 }
1276
1277 //=======================================================================
1278 //function : UnsetDisplayMode
1279 //purpose  :
1280 //=======================================================================
1281 void AIS_InteractiveContext::UnsetDisplayMode (const Handle(AIS_InteractiveObject)& theIObj,
1282                                                const Standard_Boolean               theToUpdateViewer)
1283 {
1284   if (theIObj.IsNull()
1285   || !theIObj->HasDisplayMode())
1286   {
1287     return;
1288   }
1289
1290   if (!myObjects.IsBound (theIObj))
1291   {
1292     theIObj->UnsetDisplayMode();
1293     return;
1294   }
1295
1296   const Standard_Integer anOldMode = theIObj->DisplayMode();
1297   if (myDefaultDrawer->DisplayMode() == anOldMode)
1298   {
1299     return;
1300   }
1301
1302   const Handle(AIS_GlobalStatus)& aStatus = myObjects (theIObj);
1303   aStatus->SetDisplayMode (myDefaultDrawer->DisplayMode());
1304
1305   if (theIObj->DisplayStatus() == PrsMgr_DisplayStatus_Displayed)
1306   {
1307     if (myMainPM->IsHighlighted (theIObj, anOldMode))
1308     {
1309       unhighlightGlobal (theIObj);
1310     }
1311     myMainPM->SetVisibility (theIObj, anOldMode, Standard_False);
1312     myMainPM->Display (theIObj, myDefaultDrawer->DisplayMode());
1313     if (aStatus->IsHilighted())
1314     {
1315       highlightSelected (theIObj->GlobalSelOwner());
1316     }
1317     if (aStatus->IsSubIntensityOn())
1318     {
1319       highlightWithSubintensity (theIObj, myDefaultDrawer->DisplayMode());
1320     }
1321
1322     if (theToUpdateViewer)
1323     {
1324       myMainVwr->Update();
1325     }
1326   }
1327
1328   theIObj->UnsetDisplayMode();
1329 }
1330
1331 //=======================================================================
1332 //function : SetCurrentFacingModel
1333 //purpose  :
1334 //=======================================================================
1335 void AIS_InteractiveContext::SetCurrentFacingModel (const Handle(AIS_InteractiveObject)& theIObj,
1336                                                     const Aspect_TypeOfFacingModel       theModel)
1337 {
1338   if (!theIObj.IsNull())
1339   {
1340     theIObj->SetCurrentFacingModel (theModel);
1341   }
1342 }
1343
1344 //=======================================================================
1345 //function : SetColor
1346 //purpose  :
1347 //=======================================================================
1348 void AIS_InteractiveContext::SetColor (const Handle(AIS_InteractiveObject)& theIObj,
1349                                        const Quantity_Color&                theColor,
1350                                        const Standard_Boolean               theToUpdateViewer)
1351 {
1352   if (theIObj.IsNull())
1353   {
1354     return;
1355   }
1356
1357   setContextToObject (theIObj);
1358   theIObj->SetColor (theColor);
1359   theIObj->UpdatePresentations();
1360   if (theToUpdateViewer)
1361   {
1362     UpdateCurrentViewer();
1363   }
1364 }
1365
1366 //=======================================================================
1367 //function : SetIsoOnTriangulation
1368 //purpose  :
1369 //=======================================================================
1370 void AIS_InteractiveContext::IsoOnTriangulation (const Standard_Boolean theIsEnabled,
1371                                                  const Handle(AIS_InteractiveObject)& theObject)
1372 {
1373   if (theObject.IsNull())
1374   {
1375     return;
1376   }
1377
1378   theObject->SetIsoOnTriangulation (theIsEnabled);
1379 }
1380
1381 //=======================================================================
1382 //function : SetDeviationCoefficient
1383 //purpose  :
1384 //=======================================================================
1385 void AIS_InteractiveContext::SetDeviationCoefficient (const Handle(AIS_InteractiveObject)& theIObj,
1386                                                       const Standard_Real                  theCoefficient,
1387                                                       const Standard_Boolean               theToUpdateViewer)
1388 {
1389   if (theIObj.IsNull())
1390   {
1391     return;
1392   }
1393
1394   // to be modified after the related methods of AIS_Shape are passed to InteractiveObject
1395   setContextToObject (theIObj);
1396   if (theIObj->Type() != AIS_KindOfInteractive_Object
1397    && theIObj->Type() != AIS_KindOfInteractive_Shape)
1398   {
1399     return;
1400   }
1401   else if (theIObj->Signature() != 0)
1402   {
1403     return;
1404   }
1405
1406   Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (theIObj);
1407   aShape->SetOwnDeviationCoefficient (theCoefficient);
1408   aShape->UpdatePresentations();
1409   if (theToUpdateViewer)
1410   {
1411     UpdateCurrentViewer();
1412   }
1413 }
1414
1415 //=======================================================================
1416 //function : SetDeviationAngle
1417 //purpose  :
1418 //=======================================================================
1419 void AIS_InteractiveContext::SetDeviationAngle (const Handle(AIS_InteractiveObject)& theIObj,
1420                                                 const Standard_Real                  theAngle,
1421                                                 const Standard_Boolean               theToUpdateViewer)
1422 {
1423   if (theIObj.IsNull())
1424   {
1425     return;
1426   }
1427
1428   // To be modified after the related methods of AIS_Shape are passed to InteractiveObject
1429   setContextToObject (theIObj);
1430   if (theIObj->Type() != AIS_KindOfInteractive_Shape)
1431   {
1432     return;
1433   }
1434   else if (theIObj->Signature() != 0)
1435   {
1436     return;
1437   }
1438
1439   Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (theIObj);
1440   aShape->SetOwnDeviationAngle (theAngle);
1441   aShape->UpdatePresentations();
1442   if (theToUpdateViewer)
1443   {
1444     UpdateCurrentViewer();
1445   }
1446 }
1447
1448 //=======================================================================
1449 //function : SetAngleAndDeviation
1450 //purpose  :
1451 //=======================================================================
1452 void AIS_InteractiveContext::SetAngleAndDeviation (const Handle(AIS_InteractiveObject)& theIObj,
1453                                                    const Standard_Real                  theAngle,
1454                                                    const Standard_Boolean               theToUpdateViewer)
1455 {
1456   if (theIObj.IsNull())
1457   {
1458     return;
1459   }
1460
1461   // To be modified after the related methods of AIS_Shape are passed to InteractiveObject
1462   setContextToObject (theIObj);
1463   if (theIObj->Type() != AIS_KindOfInteractive_Shape)
1464   {
1465     return;
1466   }
1467   if (theIObj->Signature() != 0)
1468   {
1469     return;
1470   }
1471
1472   Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast (theIObj);
1473   aShape->SetAngleAndDeviation (theAngle);
1474   aShape->UpdatePresentations();
1475   if (theToUpdateViewer)
1476   {
1477     UpdateCurrentViewer();
1478   }
1479 }
1480
1481 //=======================================================================
1482 //function : UnsetColor
1483 //purpose  :
1484 //=======================================================================
1485 void AIS_InteractiveContext::UnsetColor (const Handle(AIS_InteractiveObject)& theIObj,
1486                                          const Standard_Boolean               theToUpdateViewer)
1487 {
1488   if (theIObj.IsNull())
1489   {
1490     return;
1491   }
1492
1493   theIObj->UnsetColor();
1494   theIObj->UpdatePresentations();
1495   if (theToUpdateViewer)
1496   {
1497     UpdateCurrentViewer();
1498   }
1499 }
1500
1501 //=======================================================================
1502 //function : HasColor
1503 //purpose  :
1504 //=======================================================================
1505 Standard_Boolean AIS_InteractiveContext::HasColor (const Handle(AIS_InteractiveObject)& theIObj) const
1506 {
1507   return theIObj->HasColor();
1508 }
1509
1510 //=======================================================================
1511 //function : Color
1512 //purpose  :
1513 //=======================================================================
1514 void AIS_InteractiveContext::Color (const Handle(AIS_InteractiveObject)& theIObj,
1515                                     Quantity_Color&                      theColor) const
1516 {
1517   theIObj->Color (theColor);
1518 }
1519
1520 //=======================================================================
1521 //function : Width
1522 //purpose  :
1523 //=======================================================================
1524 Standard_Real AIS_InteractiveContext::Width (const Handle(AIS_InteractiveObject)& theIObj) const
1525 {
1526   return theIObj->Width();
1527 }
1528
1529 //=======================================================================
1530 //function : SetWidth
1531 //purpose  :
1532 //=======================================================================
1533 void AIS_InteractiveContext::SetWidth (const Handle(AIS_InteractiveObject)& theIObj,
1534                                        const Standard_Real                  theWidth,
1535                                        const Standard_Boolean               theToUpdateViewer)
1536 {
1537   if (theIObj.IsNull())
1538   {
1539     return;
1540   }
1541
1542   setContextToObject (theIObj);
1543   theIObj->SetWidth (theWidth);
1544   theIObj->UpdatePresentations();
1545   if (!myLastPicked.IsNull() && myLastPicked->IsSameSelectable (theIObj))
1546   {
1547     if (myLastPicked->IsAutoHilight())
1548     {
1549       const Standard_Integer aHiMode = theIObj->HasHilightMode() ? theIObj->HilightMode() : 0;
1550       myLastPicked->HilightWithColor (myMainPM,
1551                                       myLastPicked->IsSelected() ? getSelStyle (theIObj, myLastPicked) : getHiStyle (theIObj, myLastPicked),
1552                                       aHiMode);
1553     }
1554     else
1555     {
1556       theIObj->HilightOwnerWithColor (myMainPM,
1557                                       myLastPicked->IsSelected() ? getSelStyle (theIObj, myLastPicked) : getHiStyle (theIObj, myLastPicked),
1558                                       myLastPicked);
1559     }
1560   }
1561   if (theToUpdateViewer)
1562   {
1563     UpdateCurrentViewer();
1564   }
1565 }
1566
1567 //=======================================================================
1568 //function : UnsetWidth
1569 //purpose  :
1570 //=======================================================================
1571 void AIS_InteractiveContext::UnsetWidth (const Handle(AIS_InteractiveObject)& theIObj,
1572                                          const Standard_Boolean               theToUpdateViewer)
1573 {
1574   if (theIObj.IsNull())
1575   {
1576     return;
1577   }
1578
1579   theIObj->UnsetWidth();
1580   theIObj->UpdatePresentations();
1581   if (theToUpdateViewer)
1582   {
1583     UpdateCurrentViewer();
1584   }
1585 }
1586
1587 //=======================================================================
1588 //function : SetMaterial
1589 //purpose  :
1590 //=======================================================================
1591 void AIS_InteractiveContext::SetMaterial (const Handle(AIS_InteractiveObject)& theIObj,
1592                                           const Graphic3d_MaterialAspect&      theMaterial,
1593                                           const Standard_Boolean               theToUpdateViewer)
1594 {
1595   if (theIObj.IsNull())
1596   {
1597     return;
1598   }
1599
1600   setContextToObject (theIObj);
1601   theIObj->SetMaterial (theMaterial);
1602   theIObj->UpdatePresentations();
1603   if (theToUpdateViewer)
1604   {
1605     UpdateCurrentViewer();
1606   }
1607 }
1608
1609 //=======================================================================
1610 //function : UnsetMaterial
1611 //purpose  :
1612 //=======================================================================
1613 void AIS_InteractiveContext::UnsetMaterial (const Handle(AIS_InteractiveObject)& theIObj,
1614                                             const Standard_Boolean               theToUpdateViewer)
1615 {
1616   if (theIObj.IsNull())
1617   {
1618     return;
1619   }
1620   theIObj->UnsetMaterial();
1621   theIObj->UpdatePresentations();
1622   if (theToUpdateViewer)
1623   {
1624     UpdateCurrentViewer();
1625   }
1626 }
1627
1628 //=======================================================================
1629 //function : SetTransparency
1630 //purpose  :
1631 //=======================================================================
1632 void AIS_InteractiveContext::SetTransparency (const Handle(AIS_InteractiveObject)& theIObj,
1633                                               const Standard_Real                  theValue,
1634                                               const Standard_Boolean               theToUpdateViewer)
1635 {
1636   if (theIObj.IsNull())
1637   {
1638     return;
1639   }
1640
1641   setContextToObject (theIObj);
1642   if (!theIObj->IsTransparent()
1643     && theValue <= 0.005)
1644   {
1645     return;
1646   }
1647
1648   if (theValue <= 0.005)
1649   {
1650     UnsetTransparency (theIObj, theToUpdateViewer);
1651     return;
1652   }
1653
1654   theIObj->SetTransparency (theValue);
1655   theIObj->UpdatePresentations();
1656   if (theToUpdateViewer)
1657   {
1658     UpdateCurrentViewer();
1659   }
1660 }
1661
1662 //=======================================================================
1663 //function : UnsetTransparency
1664 //purpose  :
1665 //=======================================================================
1666 void AIS_InteractiveContext::UnsetTransparency (const Handle(AIS_InteractiveObject)& theIObj,
1667                                                 const Standard_Boolean               theToUpdateViewer)
1668 {
1669   if (theIObj.IsNull())
1670   {
1671     return;
1672   }
1673
1674   theIObj->UnsetTransparency();
1675   theIObj->UpdatePresentations();
1676   if (theToUpdateViewer)
1677   {
1678     UpdateCurrentViewer();
1679   }
1680 }
1681
1682 //=======================================================================
1683 //function : SetSelectedAspect
1684 //purpose  :
1685 //=======================================================================
1686 void AIS_InteractiveContext::SetSelectedAspect (const Handle(Prs3d_BasicAspect)& theAspect,
1687                                                 const Standard_Boolean           theToUpdateViewer)
1688 {
1689   Standard_DISABLE_DEPRECATION_WARNINGS
1690   Standard_Boolean isFound = Standard_False;
1691   for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
1692   {
1693     isFound = Standard_True;
1694     Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (aSelIter.Value()->Selectable());
1695     anObj->SetAspect (theAspect);
1696   }
1697   Standard_ENABLE_DEPRECATION_WARNINGS
1698
1699   if (isFound && theToUpdateViewer)
1700   {
1701     myMainVwr->Update();
1702   }
1703 }
1704
1705 //=======================================================================
1706 //function : SetLocalAttributes
1707 //purpose  :
1708 //=======================================================================
1709 void AIS_InteractiveContext::SetLocalAttributes (const Handle(AIS_InteractiveObject)& theIObj,
1710                                                  const Handle(Prs3d_Drawer)&          theDrawer,
1711                                                  const Standard_Boolean               theToUpdateViewer)
1712 {
1713   if (theIObj.IsNull())
1714   {
1715     return;
1716   }
1717
1718   setContextToObject (theIObj);
1719   theIObj->SetAttributes (theDrawer);
1720   Update (theIObj, theToUpdateViewer);
1721 }
1722
1723 //=======================================================================
1724 //function : UnsetLocalAttributes
1725 //purpose  :
1726 //=======================================================================
1727 void AIS_InteractiveContext::UnsetLocalAttributes (const Handle(AIS_InteractiveObject)& theIObj,
1728                                                    const Standard_Boolean               theToUpdateViewer)
1729 {
1730   if (theIObj.IsNull())
1731   {
1732     return;
1733   }
1734
1735   setContextToObject (theIObj);
1736   theIObj->UnsetAttributes();
1737   Update (theIObj, theToUpdateViewer);
1738 }
1739
1740 //=======================================================================
1741 //function : Status
1742 //purpose  :
1743 //=======================================================================
1744 void AIS_InteractiveContext::Status (const Handle(AIS_InteractiveObject)& theIObj,
1745                                      TCollection_ExtendedString&          theStatus) const
1746 {
1747   theStatus = "";
1748   if (theIObj.IsNull()
1749   || !myObjects.IsBound (theIObj))
1750   {
1751     return;
1752   }
1753
1754   theStatus += "\t ____________________________________________";
1755   theStatus += "\t| Known at Neutral Point:\n\tDisplayStatus:";
1756   const Handle(AIS_GlobalStatus)& aStatus = myObjects (theIObj);
1757   switch (theIObj->DisplayStatus())
1758   {
1759     case PrsMgr_DisplayStatus_Displayed:
1760     {
1761       theStatus += "\t| -->Displayed\n";
1762       break;
1763     }
1764     case PrsMgr_DisplayStatus_Erased:
1765     {
1766       theStatus += "\t| -->Erased\n";
1767       break;
1768     }
1769     default:
1770       break;
1771   }
1772
1773   theStatus += "\t| Active Display Modes in the MainViewer :\n";
1774   theStatus += "\t|\t Mode ";
1775   theStatus += TCollection_AsciiString (aStatus->DisplayMode());
1776   theStatus += "\n";
1777
1778   if (IsSelected(theIObj)) theStatus +="\t| Selected\n";
1779
1780   theStatus += "\t| Active Selection Modes in the MainViewer :\n";
1781   for (TColStd_ListIteratorOfListOfInteger aSelModeIter (aStatus->SelectionModes()); aSelModeIter.More(); aSelModeIter.Next())
1782   {
1783     theStatus += "\t\t Mode ";
1784     theStatus += TCollection_AsciiString (aSelModeIter.Value());
1785     theStatus += "\n";
1786   }
1787   theStatus += "\t ____________________________________________";
1788 }
1789
1790 //=======================================================================
1791 //function : GetDefModes
1792 //purpose  :
1793 //=======================================================================
1794 void AIS_InteractiveContext::GetDefModes (const Handle(AIS_InteractiveObject)& theIObj,
1795                                           Standard_Integer&                    theDispMode,
1796                                           Standard_Integer&                    theHiMode,
1797                                           Standard_Integer&                    theSelMode) const
1798 {
1799   if (theIObj.IsNull())
1800   {
1801     return;
1802   }
1803
1804   theDispMode = theIObj->HasDisplayMode()
1805               ? theIObj->DisplayMode()
1806               : (theIObj->AcceptDisplayMode (myDefaultDrawer->DisplayMode())
1807                ? myDefaultDrawer->DisplayMode()
1808                : 0);
1809   theHiMode  = theIObj->HasHilightMode()   ? theIObj->HilightMode()   : theDispMode;
1810   theSelMode = theIObj->GlobalSelectionMode();
1811 }
1812
1813 //=======================================================================
1814 //function : EraseGlobal
1815 //purpose  :
1816 //=======================================================================
1817 void AIS_InteractiveContext::EraseGlobal (const Handle(AIS_InteractiveObject)& theIObj,
1818                                           const Standard_Boolean               theToUpdateviewer)
1819 {
1820   Handle(AIS_GlobalStatus) aStatus;
1821   if (theIObj.IsNull()
1822   || !myObjects.Find (theIObj, aStatus)
1823   ||  theIObj->DisplayStatus() == PrsMgr_DisplayStatus_Erased)
1824   {
1825     return;
1826   }
1827
1828   const Standard_Integer aDispMode = theIObj->HasHilightMode() ? theIObj->HilightMode() : 0;
1829   unselectOwners (theIObj);
1830   myMainPM->SetVisibility (theIObj, aStatus->DisplayMode(), Standard_False);
1831
1832   if (!myLastPicked.IsNull()
1833     && myLastPicked->IsSameSelectable (theIObj))
1834   {
1835     clearDynamicHighlight();
1836   }
1837
1838   // make sure highlighting presentations are properly erased
1839   theIObj->ErasePresentations (false);
1840
1841   if (IsSelected (theIObj)
1842    && aStatus->DisplayMode() != aDispMode)
1843   {
1844     myMainPM->SetVisibility (theIObj, aDispMode, Standard_False);
1845   }
1846
1847   for (TColStd_ListIteratorOfListOfInteger aSelModeIter (aStatus->SelectionModes()); aSelModeIter.More(); aSelModeIter.Next())
1848   {
1849     mgrSelector->Deactivate (theIObj, aSelModeIter.Value());
1850   }
1851   aStatus->ClearSelectionModes();
1852   theIObj->SetDisplayStatus (PrsMgr_DisplayStatus_Erased);
1853
1854   if (theToUpdateviewer)
1855   {
1856     myMainVwr->Update();
1857   }
1858 }
1859
1860 //=======================================================================
1861 //function : unselectOwners
1862 //purpose  :
1863 //=======================================================================
1864 void AIS_InteractiveContext::unselectOwners (const Handle(AIS_InteractiveObject)& theObject)
1865 {
1866   SelectMgr_SequenceOfOwner aSeq;
1867   for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
1868   {
1869     if (aSelIter.Value()->IsSameSelectable (theObject))
1870     {
1871       aSeq.Append (aSelIter.Value());
1872     }
1873   }
1874   for (SelectMgr_SequenceOfOwner::Iterator aDelIter (aSeq); aDelIter.More(); aDelIter.Next())
1875   {
1876     AddOrRemoveSelected (aDelIter.Value(), Standard_False);
1877   }
1878 }
1879
1880 //=======================================================================
1881 //function : ClearGlobal
1882 //purpose  :
1883 //=======================================================================
1884 void AIS_InteractiveContext::ClearGlobal (const Handle(AIS_InteractiveObject)& theIObj,
1885                                           const Standard_Boolean               theToUpdateviewer)
1886 {
1887   Handle(AIS_GlobalStatus) aStatus;
1888   if (theIObj.IsNull()
1889   || !myObjects.Find (theIObj, aStatus))
1890   {
1891     // for cases when reference shape of connected interactives was not displayed
1892     // but its selection primitives were calculated
1893     const Handle(SelectMgr_SelectableObject)& anObj = theIObj; // to avoid ambiguity
1894     mgrSelector->Remove (anObj);
1895     return;
1896   }
1897
1898   unselectOwners (theIObj);
1899
1900   myMainPM->Erase (theIObj, -1);
1901   theIObj->ErasePresentations (true); // make sure highlighting presentations are properly erased
1902
1903   // Object removes from Detected sequence
1904   for (Standard_Integer aDetIter = myDetectedSeq.Lower(); aDetIter <= myDetectedSeq.Upper();)
1905   {
1906     Handle(SelectMgr_EntityOwner) aPicked = myMainSel->Picked (myDetectedSeq (aDetIter));
1907     Handle(AIS_InteractiveObject) anObj;
1908     if (!aPicked.IsNull())
1909     {
1910       anObj = Handle(AIS_InteractiveObject)::DownCast (aPicked->Selectable());
1911     }
1912
1913     if (!anObj.IsNull()
1914       && anObj == theIObj)
1915     {
1916       myDetectedSeq.Remove (aDetIter);
1917       if (myCurDetected == aDetIter)
1918       {
1919         myCurDetected = Min (myDetectedSeq.Upper(), aDetIter);
1920       }
1921       if (myCurHighlighted == aDetIter)
1922       {
1923         myCurHighlighted = 0;
1924       }
1925     }
1926     else
1927     {
1928       aDetIter++;
1929     }
1930   }
1931
1932   // remove IO from the selection manager to avoid memory leaks
1933   const Handle(SelectMgr_SelectableObject)& anObj = theIObj; // to avoid ambiguity
1934   mgrSelector->Remove (anObj);
1935
1936   setObjectStatus (theIObj, PrsMgr_DisplayStatus_None, -1, -1);
1937   myMainVwr->StructureManager()->UnregisterObject (theIObj);
1938
1939   for (V3d_ListOfViewIterator aDefViewIter (myMainVwr->DefinedViewIterator()); aDefViewIter.More(); aDefViewIter.Next())
1940   {
1941     aDefViewIter.Value()->View()->ChangeHiddenObjects()->Remove (theIObj.get());
1942   }
1943
1944   if (!myLastPicked.IsNull())
1945   {
1946     if (myLastPicked->IsSameSelectable (theIObj))
1947     {
1948       clearDynamicHighlight();
1949       myLastPicked.Nullify();
1950     }
1951   }
1952
1953   if (theToUpdateviewer
1954    && theIObj->DisplayStatus() == PrsMgr_DisplayStatus_Displayed)
1955   {
1956     myMainVwr->Update();
1957   }
1958 }
1959
1960 //=======================================================================
1961 //function : ClearGlobalPrs
1962 //purpose  :
1963 //=======================================================================
1964 void AIS_InteractiveContext::ClearGlobalPrs (const Handle(AIS_InteractiveObject)& theIObj,
1965                                              const Standard_Integer               theMode,
1966                                              const Standard_Boolean               theToUpdateViewer)
1967 {
1968   const Handle(AIS_GlobalStatus)* aStatus = !theIObj.IsNull() ? myObjects.Seek (theIObj) : NULL;
1969   if (aStatus == NULL)
1970   {
1971     return;
1972   }
1973
1974   if ((*aStatus)->DisplayMode() == theMode)
1975   {
1976     const Standard_Integer aDispMode = theIObj->HasHilightMode() ? theIObj->HilightMode() : 0;
1977     if (aDispMode == theMode
1978      && myMainPM->IsHighlighted (theIObj, theMode))
1979     {
1980       unhighlightGlobal (theIObj);
1981     }
1982
1983     myMainPM->Erase (theIObj, theMode);
1984   }
1985
1986   if (theIObj->DisplayStatus() == PrsMgr_DisplayStatus_Displayed
1987    && theToUpdateViewer)
1988   {
1989     myMainVwr->Update();
1990   }
1991 }
1992
1993 //=======================================================================
1994 //function : ClearDetected
1995 //purpose  :
1996 //=======================================================================
1997 Standard_Boolean AIS_InteractiveContext::ClearDetected (Standard_Boolean theToRedrawImmediate)
1998 {
1999   myCurDetected = 0;
2000   myCurHighlighted = 0;
2001   myDetectedSeq.Clear();
2002   Standard_Boolean toUpdate = Standard_False;
2003   if (!myLastPicked.IsNull() && myLastPicked->HasSelectable())
2004   {
2005     toUpdate = Standard_True;
2006     clearDynamicHighlight();
2007   }
2008   myLastPicked.Nullify();
2009   myMainSel->ClearPicked();
2010   if (toUpdate && theToRedrawImmediate)
2011   {
2012     myMainVwr->RedrawImmediate();
2013   }
2014   return toUpdate;
2015 }
2016
2017 //=======================================================================
2018 //function : DrawHiddenLine
2019 //purpose  :
2020 //=======================================================================
2021 Standard_Boolean AIS_InteractiveContext::DrawHiddenLine() const
2022 {
2023   return myDefaultDrawer->DrawHiddenLine();
2024 }
2025
2026 //=======================================================================
2027 //function : EnableDrawHiddenLine
2028 //purpose  :
2029 //=======================================================================
2030 void AIS_InteractiveContext::EnableDrawHiddenLine() const
2031 {
2032   myDefaultDrawer->EnableDrawHiddenLine();
2033 }
2034
2035 //=======================================================================
2036 //function : DisableDrawHiddenLine 
2037 //purpose  :
2038 //=======================================================================
2039 void AIS_InteractiveContext::DisableDrawHiddenLine() const
2040 {
2041   myDefaultDrawer->DisableDrawHiddenLine();
2042 }
2043
2044 //=======================================================================
2045 //function : HiddenLineAspect
2046 //purpose  :
2047 //=======================================================================
2048 Handle (Prs3d_LineAspect) AIS_InteractiveContext::HiddenLineAspect() const
2049 {
2050   return myDefaultDrawer->HiddenLineAspect();
2051 }
2052
2053 //=======================================================================
2054 //function : SetHiddenLineAspect
2055 //purpose  :
2056 //=======================================================================
2057 void AIS_InteractiveContext::SetHiddenLineAspect (const Handle(Prs3d_LineAspect)& theAspect) const
2058 {
2059   myDefaultDrawer->SetHiddenLineAspect (theAspect);
2060 }
2061
2062 //=======================================================================
2063 //function : SetIsoNumber
2064 //purpose  :
2065 //=======================================================================
2066 void AIS_InteractiveContext::SetIsoNumber (const Standard_Integer theNb,
2067                                            const AIS_TypeOfIso    theType)
2068 {
2069   switch (theType)
2070   {
2071     case AIS_TOI_IsoU:
2072       myDefaultDrawer->UIsoAspect()->SetNumber (theNb);
2073       break;
2074     case AIS_TOI_IsoV:
2075       myDefaultDrawer->VIsoAspect()->SetNumber (theNb);
2076       break;
2077     case AIS_TOI_Both:
2078       myDefaultDrawer->UIsoAspect()->SetNumber (theNb);
2079       myDefaultDrawer->VIsoAspect()->SetNumber (theNb);
2080       break;
2081   }
2082 }
2083
2084 //=======================================================================
2085 //function : IsoNumber
2086 //purpose  :
2087 //=======================================================================
2088 Standard_Integer AIS_InteractiveContext::IsoNumber (const AIS_TypeOfIso theType)
2089 {
2090   switch (theType)
2091   {
2092     case AIS_TOI_IsoU: return myDefaultDrawer->UIsoAspect()->Number();
2093     case AIS_TOI_IsoV: return myDefaultDrawer->VIsoAspect()->Number();
2094     case AIS_TOI_Both: return myDefaultDrawer->UIsoAspect()->Number() == myDefaultDrawer->VIsoAspect()->Number()
2095                             ? myDefaultDrawer->UIsoAspect()->Number()
2096                             : -1;
2097   }
2098   return 0;
2099 }
2100
2101 //=======================================================================
2102 //function : IsoOnPlane
2103 //purpose  :
2104 //=======================================================================
2105 void AIS_InteractiveContext::IsoOnPlane (const Standard_Boolean theToSwitchOn)
2106 {
2107   myDefaultDrawer->SetIsoOnPlane (theToSwitchOn);
2108 }
2109
2110 //=======================================================================
2111 //function : IsoOnPlane
2112 //purpose  :
2113 //=======================================================================
2114 Standard_Boolean AIS_InteractiveContext::IsoOnPlane() const
2115 {
2116   return myDefaultDrawer->IsoOnPlane();
2117 }
2118
2119 //=======================================================================
2120 //function : IsoOnTriangulation
2121 //purpose  :
2122 //=======================================================================
2123 void AIS_InteractiveContext::IsoOnTriangulation (const Standard_Boolean theToSwitchOn)
2124 {
2125   myDefaultDrawer->SetIsoOnTriangulation (theToSwitchOn);
2126 }
2127
2128 //=======================================================================
2129 //function : IsoOnTriangulation
2130 //purpose  :
2131 //=======================================================================
2132 Standard_Boolean AIS_InteractiveContext::IsoOnTriangulation() const
2133 {
2134   return myDefaultDrawer->IsoOnTriangulation();
2135 }
2136
2137 //=======================================================================
2138 //function : SetPixelTolerance
2139 //purpose  :
2140 //=======================================================================
2141 void AIS_InteractiveContext::SetPixelTolerance (const Standard_Integer thePrecision)
2142 {
2143   myMainSel->SetPixelTolerance (thePrecision);
2144 }
2145
2146 //=======================================================================
2147 //function : PixelTolerance
2148 //purpose  :
2149 //=======================================================================
2150 Standard_Integer AIS_InteractiveContext::PixelTolerance() const
2151 {
2152   return myMainSel->PixelTolerance();
2153 }
2154
2155 //=======================================================================
2156 //function : SetSelectionSensitivity
2157 //purpose  : Allows to manage sensitivity of a particular selection of interactive object theObject
2158 //=======================================================================
2159 void AIS_InteractiveContext::SetSelectionSensitivity (const Handle(AIS_InteractiveObject)& theObject,
2160                                                       const Standard_Integer theMode,
2161                                                       const Standard_Integer theNewSensitivity)
2162 {
2163   mgrSelector->SetSelectionSensitivity (theObject, theMode, theNewSensitivity);
2164 }
2165
2166 //=======================================================================
2167 //function : InitAttributes
2168 //purpose  :
2169 //=======================================================================
2170 void AIS_InteractiveContext::InitAttributes()
2171 {
2172   Graphic3d_MaterialAspect aMat (Graphic3d_NameOfMaterial_Brass);
2173   myDefaultDrawer->ShadingAspect()->SetMaterial (aMat);
2174
2175 //  myDefaultDrawer->ShadingAspect()->SetColor(Quantity_NOC_GRAY70);
2176   Handle(Prs3d_LineAspect) aLineAspect = myDefaultDrawer->HiddenLineAspect();
2177   aLineAspect->SetColor      (Quantity_NOC_GRAY20);
2178   aLineAspect->SetWidth      (1.0);
2179   aLineAspect->SetTypeOfLine (Aspect_TOL_DASH);
2180
2181   // tolerance to 2 pixels...
2182   SetPixelTolerance (2);
2183
2184   // Customizing the drawer for trihedrons and planes...
2185   Handle(Prs3d_DatumAspect) aTrihAspect = myDefaultDrawer->DatumAspect();
2186   const Standard_Real aLength = 100.0;
2187   aTrihAspect->SetAxisLength (aLength, aLength, aLength);
2188   const Quantity_Color aColor = Quantity_NOC_LIGHTSTEELBLUE4;
2189   aTrihAspect->LineAspect(Prs3d_DatumParts_XAxis)->SetColor (aColor);
2190   aTrihAspect->LineAspect(Prs3d_DatumParts_YAxis)->SetColor (aColor);
2191   aTrihAspect->LineAspect(Prs3d_DatumParts_ZAxis)->SetColor (aColor);
2192
2193   Handle(Prs3d_PlaneAspect) aPlaneAspect = myDefaultDrawer->PlaneAspect();
2194   const Standard_Real aPlaneLength = 200.0;
2195   aPlaneAspect->SetPlaneLength (aPlaneLength, aPlaneLength);
2196   aPlaneAspect->EdgesAspect()->SetColor (Quantity_NOC_SKYBLUE);
2197 }
2198
2199 //=======================================================================
2200 //function : TrihedronSize
2201 //purpose  :
2202 //=======================================================================
2203 Standard_Real AIS_InteractiveContext::TrihedronSize() const
2204 {
2205   return myDefaultDrawer->DatumAspect()->AxisLength(Prs3d_DatumParts_XAxis);
2206 }
2207
2208 //=======================================================================
2209 //function : SetTrihedronSize
2210 //purpose  :
2211 //=======================================================================
2212 void AIS_InteractiveContext::SetTrihedronSize (const Standard_Real    theVal,
2213                                                const Standard_Boolean /*updateviewer*/)
2214 {
2215   myDefaultDrawer->DatumAspect()->SetAxisLength (theVal, theVal, theVal);
2216   Redisplay (AIS_KindOfInteractive_Datum, 3, Standard_False);
2217   Redisplay (AIS_KindOfInteractive_Datum, 4, Standard_True);
2218 }
2219
2220 //=======================================================================
2221 //function : SetPlaneSize
2222 //purpose  :
2223 //=======================================================================
2224 void AIS_InteractiveContext::SetPlaneSize(const Standard_Real    theValX,
2225                                           const Standard_Real    theValY,
2226                                           const Standard_Boolean theToUpdateViewer)
2227 {
2228   myDefaultDrawer->PlaneAspect()->SetPlaneLength (theValX, theValY);
2229   Redisplay (AIS_KindOfInteractive_Datum, 7, theToUpdateViewer);
2230 }
2231
2232 //=======================================================================
2233 //function : SetPlaneSize
2234 //purpose  :
2235 //=======================================================================
2236 void AIS_InteractiveContext::SetPlaneSize (const Standard_Real    theVal,
2237                                            const Standard_Boolean theToUpdateViewer)
2238 {
2239   SetPlaneSize (theVal, theVal, theToUpdateViewer);
2240 }
2241
2242 //=======================================================================
2243 //function : PlaneSize
2244 //purpose  :
2245 //=======================================================================
2246 Standard_Boolean AIS_InteractiveContext::PlaneSize (Standard_Real& theX,
2247                                                     Standard_Real& theY) const
2248 {
2249   theX = myDefaultDrawer->PlaneAspect()->PlaneXLength();
2250   theY = myDefaultDrawer->PlaneAspect()->PlaneYLength();
2251   return (Abs (theX - theY) <= Precision::Confusion());
2252 }
2253
2254 //=======================================================================
2255 //function : SetZLayer
2256 //purpose  :
2257 //=======================================================================
2258 void AIS_InteractiveContext::SetZLayer (const Handle(AIS_InteractiveObject)& theIObj,
2259                                         const Graphic3d_ZLayerId theLayerId)
2260 {
2261   if (theIObj.IsNull())
2262     return;
2263
2264   theIObj->SetZLayer (theLayerId);
2265 }
2266
2267 //=======================================================================
2268 //function : GetZLayer
2269 //purpose  :
2270 //=======================================================================
2271 Graphic3d_ZLayerId AIS_InteractiveContext::GetZLayer (const Handle(AIS_InteractiveObject)& theIObj) const
2272 {
2273   return !theIObj.IsNull()
2274        ?  theIObj->ZLayer()
2275        :  Graphic3d_ZLayerId_UNKNOWN;
2276 }
2277
2278 //=======================================================================
2279 //function : RebuildSelectionStructs
2280 //purpose  : Rebuilds 1st level of BVH selection forcibly
2281 //=======================================================================
2282 void AIS_InteractiveContext::RebuildSelectionStructs()
2283 {
2284   myMainSel->RebuildObjectsTree (Standard_True);
2285 }
2286
2287 //=======================================================================
2288 //function : Disconnect
2289 //purpose  : Disconnects selectable object from an assembly and updates selection structures
2290 //=======================================================================
2291 void AIS_InteractiveContext::Disconnect (const Handle(AIS_InteractiveObject)& theAssembly,
2292                                          const Handle(AIS_InteractiveObject)& theObjToDisconnect)
2293 {
2294   if (theAssembly->IsInstance ("AIS_MultipleConnectedInteractive"))
2295   {
2296     Handle(AIS_MultipleConnectedInteractive) theObj (Handle(AIS_MultipleConnectedInteractive)::DownCast (theAssembly));
2297     theObj->Disconnect (theObjToDisconnect);
2298     if (!myObjects.IsBound (theObjToDisconnect))
2299     {
2300       // connected presentation might contain displayed presentations
2301       myMainPM->Erase (theObjToDisconnect, -1);
2302       theObjToDisconnect->ErasePresentations (true);
2303     }
2304
2305     const Handle(SelectMgr_SelectableObject)& anObj = theObjToDisconnect; // to avoid ambiguity
2306     mgrSelector->Remove (anObj);
2307   }
2308   else if (theAssembly->IsInstance ("AIS_ConnectedInteractive") && theObjToDisconnect.IsNull())
2309   {
2310     Handle(AIS_ConnectedInteractive) theObj (Handle(AIS_ConnectedInteractive)::DownCast (theAssembly));
2311     theObj->Disconnect();
2312     const Handle(SelectMgr_SelectableObject)& anObj = theObj; // to avoid ambiguity
2313     mgrSelector->Remove (anObj);
2314   }
2315   else
2316     return;
2317 }
2318
2319 //=======================================================================
2320 //function : FitSelected
2321 //purpose  : Fits the view corresponding to the bounds of selected objects
2322 //=======================================================================
2323 void AIS_InteractiveContext::FitSelected (const Handle(V3d_View)& theView)
2324 {
2325   FitSelected (theView, 0.01, Standard_True);
2326 }
2327
2328 //=======================================================================
2329 //function : BoundingBoxOfSelection
2330 //purpose  :
2331 //=======================================================================
2332 Bnd_Box AIS_InteractiveContext::BoundingBoxOfSelection() const
2333 {
2334   Bnd_Box aBndSelected;
2335   AIS_MapOfObjectOwners anObjectOwnerMap;
2336   for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
2337   {
2338     const Handle(SelectMgr_EntityOwner)& anOwner = aSelIter.Value();
2339     Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
2340     if (anObj->IsInfinite())
2341     {
2342       continue;
2343     }
2344
2345     if (anOwner == anObj->GlobalSelOwner())
2346     {
2347       Bnd_Box aTmpBnd;
2348       anObj->BoundingBox (aTmpBnd);
2349       aBndSelected.Add (aTmpBnd);
2350     }
2351     else
2352     {
2353       Handle(SelectMgr_IndexedMapOfOwner) anOwnerMap;
2354       if (!anObjectOwnerMap.Find (anOwner->Selectable(), anOwnerMap))
2355       {
2356         anOwnerMap = new SelectMgr_IndexedMapOfOwner();
2357         anObjectOwnerMap.Bind (anOwner->Selectable(), anOwnerMap);
2358       }
2359
2360       anOwnerMap->Add (anOwner);
2361     }
2362   }
2363
2364   for (AIS_MapIteratorOfMapOfObjectOwners anIter (anObjectOwnerMap); anIter.More(); anIter.Next())
2365   {
2366     const Handle(SelectMgr_SelectableObject) anObject = anIter.Key();
2367     Bnd_Box aTmpBox = anObject->BndBoxOfSelected (anIter.ChangeValue());
2368     aBndSelected.Add (aTmpBox);
2369   }
2370
2371   return aBndSelected;
2372 }
2373
2374 //=======================================================================
2375 //function : FitSelected
2376 //purpose  : Fits the view corresponding to the bounds of selected objects
2377 //=======================================================================
2378 void AIS_InteractiveContext::FitSelected (const Handle(V3d_View)& theView,
2379                                           const Standard_Real theMargin,
2380                                           const Standard_Boolean theToUpdate)
2381 {
2382   Bnd_Box aBndSelected = BoundingBoxOfSelection();
2383   if (!aBndSelected.IsVoid())
2384   {
2385     theView->FitAll (aBndSelected, theMargin, theToUpdate);
2386   }
2387 }
2388
2389 //=======================================================================
2390 //function : SetTransformPersistence
2391 //purpose  :
2392 //=======================================================================
2393 void AIS_InteractiveContext::SetTransformPersistence (const Handle(AIS_InteractiveObject)& theObject,
2394                                                       const Handle(Graphic3d_TransformPers)& theTrsfPers)
2395 {
2396   theObject->SetTransformPersistence (theTrsfPers);
2397   if (!myObjects.IsBound (theObject))
2398   {
2399     return;
2400   }
2401
2402   mgrSelector->UpdateSelection (theObject);
2403
2404   const Graphic3d_ZLayerId  aLayerId   = theObject->ZLayer();
2405   const Handle(V3d_Viewer)& aCurViewer = CurrentViewer();
2406   for (V3d_ListOfViewIterator anActiveViewIter (aCurViewer->ActiveViewIterator()); anActiveViewIter.More(); anActiveViewIter.Next())
2407   {
2408     anActiveViewIter.Value()->View()->InvalidateBVHData (aLayerId);
2409     anActiveViewIter.Value()->View()->InvalidateZLayerBoundingBox (aLayerId);
2410   }
2411 }
2412
2413 //=======================================================================
2414 //function : GravityPoint
2415 //purpose  :
2416 //=======================================================================
2417 gp_Pnt AIS_InteractiveContext::GravityPoint (const Handle(V3d_View)& theView) const
2418 {
2419   return theView->GravityPoint();
2420 }
2421
2422 //=======================================================================
2423 //function : setObjectStatus
2424 //purpose  :
2425 //=======================================================================
2426 void AIS_InteractiveContext::setObjectStatus (const Handle(AIS_InteractiveObject)& theIObj,
2427                                               const PrsMgr_DisplayStatus theStatus,
2428                                               const Standard_Integer theDispMode,
2429                                               const Standard_Integer theSelectionMode)
2430 {
2431   theIObj->SetDisplayStatus (theStatus);
2432   if (theStatus != PrsMgr_DisplayStatus_None)
2433   {
2434     Handle(AIS_GlobalStatus) aStatus = new AIS_GlobalStatus();
2435     aStatus->SetDisplayMode (theDispMode);
2436     if (theSelectionMode != -1)
2437     {
2438       aStatus->AddSelectionMode (theSelectionMode);
2439     }
2440     myObjects.Bind (theIObj, aStatus);
2441   }
2442   else
2443   {
2444     myObjects.UnBind (theIObj);
2445   }
2446
2447   for (PrsMgr_ListOfPresentableObjectsIter aPrsIter (theIObj->Children()); aPrsIter.More(); aPrsIter.Next())
2448   {
2449     Handle(AIS_InteractiveObject) aChild (Handle(AIS_InteractiveObject)::DownCast (aPrsIter.Value()));
2450     if (aChild.IsNull())
2451     {
2452       continue;
2453     }
2454
2455     setObjectStatus (aChild, theStatus, theDispMode, theSelectionMode);
2456   }
2457 }
2458
2459 //=======================================================================
2460 //function : highlightWithColor
2461 //purpose  :
2462 //=======================================================================
2463 void AIS_InteractiveContext::highlightWithColor (const Handle(SelectMgr_EntityOwner)& theOwner,
2464                                                  const Handle(V3d_Viewer)& theViewer)
2465 {
2466   const Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
2467   if (anObj.IsNull())
2468   {
2469     return;
2470   }
2471
2472   const Handle(Prs3d_Drawer)& aStyle = getHiStyle (anObj, theOwner);
2473   const Standard_Integer aHiMode = getHilightMode (anObj, aStyle, -1);
2474
2475   myMainPM->BeginImmediateDraw();
2476   theOwner->HilightWithColor (myMainPM, aStyle, aHiMode);
2477   myMainPM->EndImmediateDraw (theViewer.IsNull() ? myMainVwr : theViewer);
2478 }
2479
2480 //=======================================================================
2481 //function : highlightSelected
2482 //purpose  :
2483 //=======================================================================
2484 void AIS_InteractiveContext::highlightSelected (const Handle(SelectMgr_EntityOwner)& theOwner)
2485 {
2486   AIS_NListOfEntityOwner anOwners;
2487   const Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
2488   if (anObj.IsNull())
2489   {
2490     return;
2491   }
2492
2493   if (!theOwner->IsAutoHilight())
2494   {
2495     SelectMgr_SequenceOfOwner aSeq;
2496     for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
2497     {
2498       if (aSelIter.Value()->IsSameSelectable (anObj))
2499       {
2500         anOwners.Append (aSelIter.Value());
2501       }
2502     }
2503   }
2504   else
2505   {
2506     anOwners.Append (theOwner);
2507   }
2508   highlightOwners (anOwners, Handle(Prs3d_Drawer)());
2509 }
2510
2511 //=======================================================================
2512 //function : highlightGlobal
2513 //purpose  :
2514 //=======================================================================
2515 void AIS_InteractiveContext::highlightGlobal (const Handle(AIS_InteractiveObject)& theObj,
2516                                               const Handle(Prs3d_Drawer)& theStyle,
2517                                               const Standard_Integer theDispMode)
2518 {
2519   if (theObj.IsNull())
2520   {
2521     return;
2522   }
2523
2524   const Standard_Integer aHiMode = getHilightMode (theObj, theStyle, theDispMode);
2525   const Handle(SelectMgr_EntityOwner)& aGlobOwner = theObj->GlobalSelOwner();
2526
2527   if (aGlobOwner.IsNull())
2528   {
2529     myMainPM->Color (theObj, theStyle, aHiMode);
2530     return;
2531   }
2532
2533   AIS_NListOfEntityOwner anOwners;
2534   if (!aGlobOwner->IsAutoHilight())
2535   {
2536     SelectMgr_SequenceOfOwner aSeq;
2537     for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
2538     {
2539       if (aSelIter.Value()->IsSameSelectable (theObj))
2540       {
2541         anOwners.Append (aSelIter.Value());
2542       }
2543     }
2544   }
2545   else
2546   {
2547     anOwners.Append (aGlobOwner);
2548   }
2549   highlightOwners (anOwners, theStyle);
2550 }
2551
2552 //=======================================================================
2553 //function : unhighlightSelected
2554 //purpose  :
2555 //=======================================================================
2556 void AIS_InteractiveContext::unhighlightSelected (const Standard_Boolean theIsToHilightSubIntensity)
2557 {
2558   unhighlightOwners (mySelection->Objects(), theIsToHilightSubIntensity);
2559 }
2560
2561 //=======================================================================
2562 //function : unhighlightOwners
2563 //purpose  :
2564 //=======================================================================
2565 void AIS_InteractiveContext::unhighlightOwners (const AIS_NListOfEntityOwner& theOwners,
2566                                                 const Standard_Boolean theIsToHilightSubIntensity)
2567 {
2568   NCollection_IndexedMap<Handle(AIS_InteractiveObject)> anObjToClear;
2569   for (AIS_NListOfEntityOwner::Iterator aSelIter (theOwners); aSelIter.More(); aSelIter.Next())
2570   {
2571     const Handle(SelectMgr_EntityOwner) anOwner = aSelIter.Value();
2572     const Handle(AIS_InteractiveObject) anInteractive = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
2573     Handle(AIS_GlobalStatus)* aStatusPtr = myObjects.ChangeSeek (anInteractive);
2574     if (!aStatusPtr)
2575     {
2576       continue;
2577     }
2578
2579     if (anOwner->IsAutoHilight())
2580     {
2581       anOwner->Unhilight (myMainPM);
2582       if (theIsToHilightSubIntensity)
2583       {
2584         if ((*aStatusPtr)->IsSubIntensityOn())
2585         {
2586           const Standard_Integer aHiMode = getHilightMode (anInteractive, (*aStatusPtr)->HilightStyle(), (*aStatusPtr)->DisplayMode());
2587           highlightWithSubintensity (anOwner, aHiMode);
2588         }
2589       }
2590     }
2591     else
2592     {
2593       anObjToClear.Add (anInteractive);
2594     }
2595     if (anOwner == anInteractive->GlobalSelOwner())
2596     {
2597       (*aStatusPtr)->SetHilightStatus (Standard_False);
2598     }
2599     (*aStatusPtr)->SetHilightStyle (Handle(Prs3d_Drawer)());
2600   }
2601   for (NCollection_IndexedMap<Handle(AIS_InteractiveObject)>::Iterator anIter (anObjToClear); anIter.More(); anIter.Next())
2602   {
2603     const Handle(AIS_InteractiveObject)& anObj = anIter.Value();
2604     myMainPM->Unhighlight (anObj);
2605     anObj->ClearSelected();
2606   }
2607 }
2608
2609 //=======================================================================
2610 //function : unhighlightGlobal
2611 //purpose  :
2612 //=======================================================================
2613 void AIS_InteractiveContext::unhighlightGlobal (const Handle(AIS_InteractiveObject)& theObj)
2614 {
2615   if (theObj.IsNull())
2616   {
2617     return;
2618   }
2619
2620   const Handle(SelectMgr_EntityOwner)& aGlobOwner = theObj->GlobalSelOwner();
2621   if (aGlobOwner.IsNull())
2622   {
2623     myMainPM->Unhighlight (theObj);
2624     return;
2625   }
2626
2627   AIS_NListOfEntityOwner anOwners;
2628   anOwners.Append (aGlobOwner);
2629   unhighlightOwners (anOwners);
2630 }
2631
2632 //=======================================================================
2633 //function : turnOnSubintensity
2634 //purpose  :
2635 //=======================================================================
2636 void AIS_InteractiveContext::turnOnSubintensity (const Handle(AIS_InteractiveObject)& theObject,
2637                                                  const Standard_Integer theDispMode,
2638                                                  const Standard_Boolean theIsDisplayedOnly) const
2639 {
2640   // the only differ with selection highlight is color, so sync transparency values
2641   const Handle(Prs3d_Drawer)& aSubStyle = myStyles[Prs3d_TypeOfHighlight_SubIntensity];
2642   aSubStyle->SetTransparency (myStyles[Prs3d_TypeOfHighlight_Selected]->Transparency());
2643
2644   if (theObject.IsNull())
2645   {
2646     for (AIS_DataMapIteratorOfDataMapOfIOStatus anObjsIter (myObjects); anObjsIter.More(); anObjsIter.Next())
2647     {
2648       const Handle(AIS_GlobalStatus)& aStatus = anObjsIter.Value();
2649       if (theObject->DisplayStatus() != PrsMgr_DisplayStatus_Displayed && theIsDisplayedOnly)
2650       {
2651         continue;
2652       }
2653
2654       aStatus->SetSubIntensity (true);
2655       myMainPM->Color (anObjsIter.Key(), aSubStyle, theDispMode != -1 ? theDispMode : aStatus->DisplayMode());
2656     }
2657   }
2658   else
2659   {
2660     Handle(AIS_GlobalStatus) aStatus;
2661     if (!myObjects.Find (theObject, aStatus))
2662     {
2663       return;
2664     }
2665
2666     if (theObject->DisplayStatus() != PrsMgr_DisplayStatus_Displayed && theIsDisplayedOnly)
2667     {
2668       return;
2669     }
2670
2671     aStatus->SetSubIntensity (true);
2672     myMainPM->Color (theObject, aSubStyle, theDispMode != -1 ? theDispMode : aStatus->DisplayMode());
2673   }
2674 }
2675
2676 //=======================================================================
2677 //function : highlightWithSubintensity
2678 //purpose  :
2679 //=======================================================================
2680 void AIS_InteractiveContext::highlightWithSubintensity (const Handle(AIS_InteractiveObject)& theObject,
2681                                                         const Standard_Integer theMode) const
2682 {
2683   // the only differ with selection highlight is color, so
2684   // sync transparency values
2685   myStyles[Prs3d_TypeOfHighlight_SubIntensity]->SetTransparency (myStyles[Prs3d_TypeOfHighlight_Selected]->Transparency());
2686
2687   myMainPM->Color (theObject, myStyles[Prs3d_TypeOfHighlight_SubIntensity], theMode);
2688 }
2689
2690 //=======================================================================
2691 //function : highlightWithSubintensity
2692 //purpose  :
2693 //=======================================================================
2694 void AIS_InteractiveContext::highlightWithSubintensity (const Handle(SelectMgr_EntityOwner)& theOwner,
2695                                                         const Standard_Integer theMode) const
2696 {
2697   // the only differ with selection highlight is color, so
2698   // sync transparency values
2699   myStyles[Prs3d_TypeOfHighlight_SubIntensity]->SetTransparency (myStyles[Prs3d_TypeOfHighlight_Selected]->Transparency());
2700
2701   theOwner->HilightWithColor (myMainPM, myStyles[Prs3d_TypeOfHighlight_SubIntensity], theMode);
2702 }
2703
2704 //=======================================================================
2705 //function : isSlowHiStyle
2706 //purpose  :
2707 //=======================================================================
2708 Standard_Boolean AIS_InteractiveContext::isSlowHiStyle (const Handle(SelectMgr_EntityOwner)& theOwner,
2709                                                         const Handle(V3d_Viewer)& theViewer) const
2710 {
2711   if (const Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable()))
2712   {
2713     const Handle(Prs3d_Drawer)& aHiStyle = getHiStyle (anObj, myLastPicked);
2714     return aHiStyle->ZLayer() == Graphic3d_ZLayerId_UNKNOWN
2715        || !theViewer->ZLayerSettings (aHiStyle->ZLayer()).IsImmediate();
2716   }
2717   return Standard_False;
2718 }
2719
2720 //=======================================================================
2721 //function : MoveTo
2722 //purpose  :
2723 //=======================================================================
2724 AIS_StatusOfDetection AIS_InteractiveContext::MoveTo (const Standard_Integer  theXPix,
2725                                                       const Standard_Integer  theYPix,
2726                                                       const Handle(V3d_View)& theView,
2727                                                       const Standard_Boolean  theToRedrawOnUpdate)
2728 {
2729   if (theView->Viewer() != myMainVwr)
2730   {
2731     throw Standard_ProgramError ("AIS_InteractiveContext::MoveTo() - invalid argument");
2732   }
2733   myMainSel->Pick (theXPix, theYPix, theView);
2734   return moveTo (theView, theToRedrawOnUpdate);
2735 }
2736
2737 //=======================================================================
2738 //function : MoveTo
2739 //purpose  :
2740 //=======================================================================
2741 AIS_StatusOfDetection AIS_InteractiveContext::MoveTo (const gp_Ax1& theAxis,
2742                                                       const Handle(V3d_View)& theView,
2743                                                       const Standard_Boolean  theToRedrawOnUpdate)
2744 {
2745   if (theView->Viewer() != myMainVwr)
2746   {
2747     throw Standard_ProgramError ("AIS_InteractiveContext::MoveTo() - invalid argument");
2748   }
2749   myMainSel->Pick (theAxis, theView);
2750   return moveTo (theView, theToRedrawOnUpdate);
2751 }
2752
2753 //=======================================================================
2754 //function : moveTo
2755 //purpose  :
2756 //=======================================================================
2757 AIS_StatusOfDetection AIS_InteractiveContext::moveTo (const Handle(V3d_View)& theView,
2758                                                       const Standard_Boolean  theToRedrawOnUpdate)
2759 {
2760   myCurDetected = 0;
2761   myCurHighlighted = 0;
2762   myDetectedSeq.Clear();
2763   myLastActiveView = theView.get();
2764
2765   // preliminaires
2766   AIS_StatusOfDetection aStatus        = AIS_SOD_Nothing;
2767   Standard_Boolean      toUpdateViewer = Standard_False;
2768
2769   myFilters->SetDisabledObjects (theView->View()->HiddenObjects());
2770
2771   // filling of myAISDetectedSeq sequence storing information about detected AIS objects
2772   // (the objects must be AIS_Shapes)
2773   const Standard_Integer aDetectedNb = myMainSel->NbPicked();
2774   Standard_Integer aNewDetected = 0;
2775   Standard_Boolean toIgnoreDetTop = Standard_False;
2776   for (Standard_Integer aDetIter = 1; aDetIter <= aDetectedNb; ++aDetIter)
2777   {
2778     Handle(SelectMgr_EntityOwner) anOwner = myMainSel->Picked (aDetIter);
2779     if (anOwner.IsNull()
2780      || !myFilters->IsOk (anOwner))
2781     {
2782       if (myPickingStrategy == SelectMgr_PickingStrategy_OnlyTopmost)
2783       {
2784         toIgnoreDetTop = Standard_True;
2785       }
2786       continue;
2787     }
2788
2789     if (aNewDetected < 1
2790     && !toIgnoreDetTop)
2791     {
2792       aNewDetected = aDetIter;
2793     }
2794
2795     myDetectedSeq.Append (aDetIter);
2796   }
2797
2798   if (aNewDetected >= 1)
2799   {
2800     myCurHighlighted = myDetectedSeq.Lower();
2801
2802     // Does nothing if previously detected object is equal to the current one.
2803     // However in advanced selection modes the owners comparison
2804     // is not effective because in that case only one owner manage the
2805     // selection in current selection mode. It is necessary to check the current detected
2806     // entity and hilight it only if the detected entity is not the same as
2807     // previous detected (IsForcedHilight call)
2808     Handle(SelectMgr_EntityOwner) aNewPickedOwner = myMainSel->Picked (aNewDetected);
2809     if (aNewPickedOwner == myLastPicked && !aNewPickedOwner->IsForcedHilight())
2810     {
2811       return myLastPicked->IsSelected()
2812            ? AIS_SOD_Selected
2813            : AIS_SOD_OnlyOneDetected;
2814     }
2815
2816     // Previously detected object is unhilighted if it is not selected or hilighted
2817     // with selection color if it is selected. Such highlighting with selection color
2818     // is needed only if myToHilightSelected flag is true. In this case previously detected
2819     // object has been already highlighted with myHilightColor during previous MoveTo()
2820     // method call. As result it is necessary to rehighligt it with mySelectionColor.
2821     if (!myLastPicked.IsNull() && myLastPicked->HasSelectable())
2822     {
2823       if (isSlowHiStyle (myLastPicked, theView->Viewer()))
2824       {
2825         theView->Viewer()->Invalidate();
2826       }
2827
2828       clearDynamicHighlight();
2829       toUpdateViewer = Standard_True;
2830     }
2831
2832     // initialize myLastPicked field with currently detected object
2833     myLastPicked = aNewPickedOwner;
2834
2835     // highlight detected object if it is not selected or myToHilightSelected flag is true
2836     if (myLastPicked->HasSelectable())
2837     {
2838       if (myAutoHilight
2839        && (!myLastPicked->IsSelected()
2840          || myToHilightSelected))
2841       {
2842         if (isSlowHiStyle (myLastPicked, theView->Viewer()))
2843         {
2844           theView->Viewer()->Invalidate();
2845         }
2846
2847         highlightWithColor (myLastPicked, theView->Viewer());
2848         toUpdateViewer = Standard_True;
2849       }
2850
2851       aStatus = myLastPicked->IsSelected()
2852               ? AIS_SOD_Selected
2853               : AIS_SOD_OnlyOneDetected;
2854     }
2855   }
2856   else
2857   {
2858     // previously detected object is unhilighted if it is not selected or hilighted
2859     // with selection color if it is selected
2860     aStatus = AIS_SOD_Nothing;
2861     if (myAutoHilight
2862     && !myLastPicked.IsNull()
2863      && myLastPicked->HasSelectable())
2864     {
2865       if (isSlowHiStyle (myLastPicked, theView->Viewer()))
2866       {
2867         theView->Viewer()->Invalidate();
2868       }
2869
2870       clearDynamicHighlight();
2871       toUpdateViewer = Standard_True;
2872     }
2873
2874     myLastPicked.Nullify();
2875   }
2876
2877   if (toUpdateViewer
2878    && theToRedrawOnUpdate)
2879   {
2880     if (theView->ComputedMode())
2881     {
2882       theView->Viewer()->Update();
2883     }
2884     else
2885     {
2886       if (theView->IsInvalidated())
2887       {
2888         theView->Viewer()->Redraw();
2889       }
2890       else
2891       {
2892         theView->Viewer()->RedrawImmediate();
2893       }
2894     }
2895   }
2896
2897   return aStatus;
2898 }
2899
2900 //=======================================================================
2901 //function : AddSelect
2902 //purpose  :
2903 //=======================================================================
2904 AIS_StatusOfPick AIS_InteractiveContext::AddSelect (const Handle(SelectMgr_EntityOwner)& theObject)
2905 {
2906   mySelection->AddSelect (theObject);
2907
2908   Standard_Integer aSelNum = NbSelected();
2909   return (aSelNum == 0) ? AIS_SOP_NothingSelected
2910                         : (aSelNum == 1) ? AIS_SOP_OneSelected
2911                                          : AIS_SOP_SeveralSelected;
2912 }
2913
2914 //=======================================================================
2915 //function : SelectRectangle
2916 //purpose  :
2917 //=======================================================================
2918 AIS_StatusOfPick AIS_InteractiveContext::SelectRectangle (const Graphic3d_Vec2i&    thePntMin,
2919                                                           const Graphic3d_Vec2i&    thePntMax,
2920                                                           const Handle(V3d_View)&   theView,
2921                                                           const AIS_SelectionScheme theSelScheme)
2922 {
2923   if (theView->Viewer() != myMainVwr)
2924   {
2925     throw Standard_ProgramError ("AIS_InteractiveContext::SelectRectangle() - invalid argument");
2926   }
2927
2928   myLastActiveView = theView.get();
2929   myMainSel->Pick (thePntMin.x(), thePntMin.y(), thePntMax.x(), thePntMax.y(), theView);
2930
2931   AIS_NArray1OfEntityOwner aPickedOwners;
2932   if (myMainSel->NbPicked() > 0)
2933   {
2934     aPickedOwners.Resize (1, myMainSel->NbPicked(), false);
2935     for (Standard_Integer aPickIter = 1; aPickIter <= myMainSel->NbPicked(); ++aPickIter)
2936     {
2937       aPickedOwners.SetValue (aPickIter, myMainSel->Picked (aPickIter));
2938     }
2939   }
2940
2941   return Select (aPickedOwners, theSelScheme);
2942 }
2943
2944 //=======================================================================
2945 //function : SelectPolygon
2946 //purpose  :
2947 //=======================================================================
2948 AIS_StatusOfPick AIS_InteractiveContext::SelectPolygon (const TColgp_Array1OfPnt2d& thePolyline,
2949                                                         const Handle(V3d_View)&     theView,
2950                                                         const AIS_SelectionScheme   theSelScheme)
2951 {
2952   if (theView->Viewer() != myMainVwr)
2953   {
2954     throw Standard_ProgramError ("AIS_InteractiveContext::SelectPolygon() - invalid argument");
2955   }
2956
2957   myLastActiveView = theView.get();
2958   myMainSel->Pick (thePolyline, theView);
2959
2960   AIS_NArray1OfEntityOwner aPickedOwners;
2961   if (myMainSel->NbPicked() > 0)
2962   {
2963     aPickedOwners.Resize (1, myMainSel->NbPicked(), false);
2964     for (Standard_Integer aPickIter = 1; aPickIter <= myMainSel->NbPicked(); ++aPickIter)
2965     {
2966       aPickedOwners.SetValue (aPickIter, myMainSel->Picked (aPickIter));
2967     }
2968   }
2969
2970   return Select (aPickedOwners, theSelScheme);
2971 }
2972
2973 //=======================================================================
2974 //function : SelectPoint
2975 //purpose  :
2976 //=======================================================================
2977 AIS_StatusOfPick AIS_InteractiveContext::SelectPoint (const Graphic3d_Vec2i&    thePnt,
2978                                                       const Handle(V3d_View)&   theView,
2979                                                       const AIS_SelectionScheme theSelScheme)
2980 {
2981   if (theView->Viewer() != myMainVwr)
2982   {
2983     throw Standard_ProgramError ("AIS_InteractiveContext::SelectPoint() - invalid argument");
2984   }
2985
2986   myLastActiveView = theView.get();
2987   myMainSel->Pick (thePnt.x(), thePnt.y(), theView);
2988
2989   AIS_NArray1OfEntityOwner aPickedOwners;
2990   if (myMainSel->NbPicked() > 0)
2991   {
2992     aPickedOwners.Resize (1, myMainSel->NbPicked(), false);
2993     for (Standard_Integer aPickIter = 1; aPickIter <= myMainSel->NbPicked(); ++aPickIter)
2994     {
2995       aPickedOwners.SetValue (aPickIter, myMainSel->Picked (aPickIter));
2996     }
2997   }
2998
2999   return Select (aPickedOwners, theSelScheme);
3000 }
3001
3002 //=======================================================================
3003 //function : SelectDetected
3004 //purpose  :
3005 //=======================================================================
3006 AIS_StatusOfPick AIS_InteractiveContext::SelectDetected (const AIS_SelectionScheme theSelScheme)
3007 {
3008   if (theSelScheme == AIS_SelectionScheme_Replace && !myLastPicked.IsNull())
3009   {
3010     Graphic3d_Vec2i aMousePos (-1, -1);
3011     gp_Pnt2d aMouseRealPos = myMainSel->GetManager().GetMousePosition();
3012     if (!Precision::IsInfinite (aMouseRealPos.X()) &&
3013         !Precision::IsInfinite (aMouseRealPos.Y()))
3014     {
3015       aMousePos.SetValues ((Standard_Integer )aMouseRealPos.X(), (Standard_Integer )aMouseRealPos.Y());
3016     }
3017     if (myLastPicked->HandleMouseClick (aMousePos, Aspect_VKeyMouse_LeftButton, Aspect_VKeyFlags_NONE, false))
3018     {
3019       return AIS_SOP_NothingSelected;
3020     }
3021   }
3022
3023   AIS_NArray1OfEntityOwner aPickedOwners (1, 1);
3024   aPickedOwners.SetValue (1, myLastPicked);
3025   return Select (aPickedOwners, theSelScheme);
3026 }
3027
3028 //=======================================================================
3029 //function : Select
3030 //purpose  :
3031 //=======================================================================
3032 AIS_StatusOfPick AIS_InteractiveContext::Select (const Standard_Integer  theXPMin,
3033                                                  const Standard_Integer  theYPMin,
3034                                                  const Standard_Integer  theXPMax,
3035                                                  const Standard_Integer  theYPMax,
3036                                                  const Handle(V3d_View)& theView,
3037                                                  const Standard_Boolean  theToUpdateViewer)
3038 {
3039   AIS_StatusOfPick aStatus = SelectRectangle (Graphic3d_Vec2i (theXPMin, theYPMin),
3040                                               Graphic3d_Vec2i (theXPMax, theYPMax),
3041                                               theView);
3042   if (theToUpdateViewer)
3043   {
3044     UpdateCurrentViewer();
3045   }
3046   return aStatus;
3047 }
3048
3049 //=======================================================================
3050 //function : Select
3051 //purpose  : Selection by polyline
3052 //=======================================================================
3053 AIS_StatusOfPick AIS_InteractiveContext::Select (const TColgp_Array1OfPnt2d& thePolyline,
3054                                                  const Handle(V3d_View)&     theView,
3055                                                  const Standard_Boolean      theToUpdateViewer)
3056 {
3057   AIS_StatusOfPick aStatus = SelectPolygon (thePolyline, theView);
3058   if (theToUpdateViewer)
3059   {
3060     UpdateCurrentViewer();
3061   }
3062   return aStatus;
3063 }
3064
3065 //=======================================================================
3066 //function : Select
3067 //purpose  :
3068 //=======================================================================
3069 AIS_StatusOfPick AIS_InteractiveContext::Select (const Standard_Boolean theToUpdateViewer)
3070 {
3071   AIS_StatusOfPick aStatus = SelectDetected();
3072   if (theToUpdateViewer)
3073   {
3074     UpdateCurrentViewer();
3075   }
3076   return aStatus;
3077 }
3078
3079 //=======================================================================
3080 //function : ShiftSelect
3081 //purpose  :
3082 //=======================================================================
3083 AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const Standard_Boolean theToUpdateViewer)
3084 {
3085   AIS_StatusOfPick aStatus = SelectDetected (AIS_SelectionScheme_XOR);
3086   if (theToUpdateViewer)
3087   {
3088     UpdateCurrentViewer();
3089   }
3090   return aStatus;
3091 }
3092
3093 //=======================================================================
3094 //function : ShiftSelect
3095 //purpose  :
3096 //=======================================================================
3097 AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const Standard_Integer theXPMin,
3098                                                       const Standard_Integer theYPMin,
3099                                                       const Standard_Integer theXPMax,
3100                                                       const Standard_Integer theYPMax,
3101                                                       const Handle(V3d_View)& theView,
3102                                                       const Standard_Boolean theToUpdateViewer)
3103 {
3104   AIS_StatusOfPick aStatus = SelectRectangle (Graphic3d_Vec2i (theXPMin, theYPMin),
3105                                               Graphic3d_Vec2i (theXPMax, theYPMax),
3106                                               theView,
3107                                               AIS_SelectionScheme_XOR);
3108   if (theToUpdateViewer)
3109   {
3110     UpdateCurrentViewer();
3111   }
3112   return aStatus;
3113 }
3114
3115 //=======================================================================
3116 //function : ShiftSelect
3117 //purpose  :
3118 //=======================================================================
3119 AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const TColgp_Array1OfPnt2d& thePolyline,
3120                                                       const Handle(V3d_View)& theView,
3121                                                       const Standard_Boolean theToUpdateViewer)
3122 {
3123   AIS_StatusOfPick aStatus = SelectPolygon (thePolyline, theView, AIS_SelectionScheme_XOR);
3124   if (theToUpdateViewer)
3125   {
3126     UpdateCurrentViewer();
3127   }
3128   return aStatus;
3129 }
3130
3131 //=======================================================================
3132 //function : Select
3133 //purpose  :
3134 //=======================================================================
3135 AIS_StatusOfPick AIS_InteractiveContext::Select (const AIS_NArray1OfEntityOwner& theOwners,
3136                                                  const AIS_SelectionScheme theSelScheme)
3137 {
3138   NCollection_IndexedMap<Handle(SelectMgr_EntityOwner)> aSelOwnerMap (myAutoHilight ? mySelection->Objects().Size() : 0);
3139   if (myAutoHilight)
3140   {
3141     clearDynamicHighlight();
3142
3143     // collect currently selected owners
3144     for (AIS_NListOfEntityOwner::Iterator anOwnerIter (mySelection->Objects()); anOwnerIter.More(); anOwnerIter.Next())
3145     {
3146       aSelOwnerMap.Add (anOwnerIter.Value());
3147     }
3148   }
3149
3150   mySelection->SelectOwners (theOwners, theSelScheme, myMainSel->GetManager().IsOverlapAllowed(), myFilters);
3151
3152   if (myAutoHilight)
3153   {
3154     // collect lists of owners to unhighlight (unselected) and to highlight (selected)
3155     AIS_NListOfEntityOwner anOwnersToUnhighlight, anOwnersToHighlight;
3156     for (AIS_NListOfEntityOwner::Iterator anOwnerIter (mySelection->Objects()); anOwnerIter.More(); anOwnerIter.Next())
3157     {
3158       // add newly selected owners
3159       const Handle(SelectMgr_EntityOwner)& anOwner = anOwnerIter.Value();
3160       if (!aSelOwnerMap.RemoveKey (anOwner))
3161       {
3162         // newly selected owner
3163         anOwnersToHighlight.Append (anOwner);
3164       }
3165       else
3166       {
3167         // already selected owner
3168         if (!anOwner->IsAutoHilight()
3169           && theSelScheme != AIS_SelectionScheme_XOR
3170           && theSelScheme != AIS_SelectionScheme_Add)
3171         {
3172           // hack to perform AIS_InteractiveObject::ClearSelected() before highlighting
3173           anOwnersToUnhighlight.Append (anOwner);
3174           anOwnersToHighlight.Append (anOwner);
3175         }
3176         else if (anOwner->IsForcedHilight()
3177              || !anOwner->IsAutoHilight())
3178         {
3179           anOwnersToHighlight.Append (anOwner);
3180         }
3181       }
3182     }
3183
3184     for (NCollection_IndexedMap<Handle(SelectMgr_EntityOwner)>::Iterator anOwnerIter (aSelOwnerMap); anOwnerIter.More(); anOwnerIter.Next())
3185     {
3186       // owners removed from selection
3187       const Handle(SelectMgr_EntityOwner)& anOwner = anOwnerIter.Value();
3188       anOwnersToUnhighlight.Append (anOwner);
3189     }
3190
3191     unhighlightOwners (anOwnersToUnhighlight);
3192     highlightOwners (anOwnersToHighlight, Handle(Prs3d_Drawer)());
3193   }
3194
3195   Standard_Integer aSelNum = NbSelected();
3196   return (aSelNum == 0) ? AIS_SOP_NothingSelected
3197                         : (aSelNum == 1) ? AIS_SOP_OneSelected
3198                                          : AIS_SOP_SeveralSelected;
3199 }
3200
3201 //=======================================================================
3202 //function : HilightSelected
3203 //purpose  :
3204 //=======================================================================
3205 void AIS_InteractiveContext::HilightSelected (const Standard_Boolean theToUpdateViewer)
3206 {
3207   // In case of selection without using local context
3208   clearDynamicHighlight();
3209
3210   highlightOwners (mySelection->Objects(), Handle(Prs3d_Drawer)());
3211
3212   if (theToUpdateViewer)
3213   {
3214     UpdateCurrentViewer();
3215   }
3216 }
3217
3218 //=======================================================================
3219 //function : highlightOwners
3220 //purpose  :
3221 //=======================================================================
3222 void AIS_InteractiveContext::highlightOwners (const AIS_NListOfEntityOwner& theOwners,
3223                                               const Handle(Prs3d_Drawer)& theStyle)
3224 {
3225   NCollection_DataMap<Handle(AIS_InteractiveObject), NCollection_Handle<SelectMgr_SequenceOfOwner> > anObjOwnerMap;
3226   for (AIS_NListOfEntityOwner::Iterator aSelIter (theOwners); aSelIter.More(); aSelIter.Next())
3227   {
3228     const Handle(SelectMgr_EntityOwner) anOwner = aSelIter.Value();
3229     const Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
3230     if (anObj.IsNull())
3231       continue;
3232
3233     const Handle(Prs3d_Drawer)& anObjSelStyle = !theStyle.IsNull() ? theStyle : getSelStyle (anObj, anOwner);
3234     Handle(AIS_GlobalStatus)* aStatusPtr = myObjects.ChangeSeek (anObj);
3235     if (!aStatusPtr)
3236     {
3237       continue;
3238     }
3239     if (anOwner == anObj->GlobalSelOwner())
3240     {
3241       (*aStatusPtr)->SetHilightStatus (Standard_True);
3242       (*aStatusPtr)->SetHilightStyle (anObjSelStyle);
3243     }
3244     if (!anOwner->IsAutoHilight())
3245     {
3246       NCollection_Handle<SelectMgr_SequenceOfOwner> aSeq;
3247       if (anObjOwnerMap.Find (anObj, aSeq))
3248       {
3249         aSeq->Append (anOwner);
3250       }
3251       else
3252       {
3253         aSeq = new SelectMgr_SequenceOfOwner();
3254         aSeq->Append (anOwner);
3255         anObjOwnerMap.Bind (anObj, aSeq);
3256       }
3257     }
3258     else
3259     {
3260       const Standard_Integer aHiMode = getHilightMode (anObj, anObjSelStyle, (*aStatusPtr)->DisplayMode());
3261       anOwner->HilightWithColor (myMainPM, anObjSelStyle, aHiMode);
3262     }
3263   }
3264
3265   if (!anObjOwnerMap.IsEmpty())
3266   {
3267     for (NCollection_DataMap<Handle(AIS_InteractiveObject), NCollection_Handle<SelectMgr_SequenceOfOwner> >::Iterator anIter (anObjOwnerMap);
3268          anIter.More(); anIter.Next())
3269     {
3270       anIter.Key()->HilightSelected (myMainPM, *anIter.Value());
3271     }
3272     anObjOwnerMap.Clear();
3273   }
3274 }
3275
3276 //=======================================================================
3277 //function : UnhilightSelected
3278 //purpose  :
3279 //=======================================================================
3280 void AIS_InteractiveContext::UnhilightSelected (const Standard_Boolean theToUpdateViewer)
3281 {
3282   unhighlightSelected();
3283
3284   if (theToUpdateViewer)
3285   {
3286     UpdateCurrentViewer();
3287   }
3288 }
3289
3290 //=======================================================================
3291 //function : ClearSelected
3292 //purpose  :
3293 //=======================================================================
3294 void AIS_InteractiveContext::ClearSelected (const Standard_Boolean theToUpdateViewer)
3295 {
3296   if (NbSelected() == 0)
3297   {
3298     return;
3299   }
3300
3301   if (myAutoHilight)
3302   {
3303     unhighlightSelected();
3304   }
3305
3306   mySelection->Clear();
3307   if (myAutoHilight)
3308   {
3309     clearDynamicHighlight();
3310   }
3311
3312   if (theToUpdateViewer)
3313   {
3314     UpdateCurrentViewer();
3315   }
3316 }
3317
3318 //=======================================================================
3319 //function : SetSelected
3320 //purpose  : Sets the whole object as selected and highlights it with selection color
3321 //=======================================================================
3322 void AIS_InteractiveContext::SetSelected (const Handle(AIS_InteractiveObject)& theObject,
3323                                           const Standard_Boolean theToUpdateViewer)
3324 {
3325   if (theObject.IsNull())
3326   {
3327     return;
3328   }
3329
3330   if (!myObjects.IsBound (theObject))
3331   {
3332     return;
3333   }
3334
3335   Handle(SelectMgr_EntityOwner) anOwner = theObject->GlobalSelOwner();
3336   if (anOwner.IsNull())
3337   {
3338     return;
3339   }
3340
3341   const Handle(Prs3d_Drawer)& anObjSelStyle = getSelStyle (theObject, anOwner);
3342   if (NbSelected() == 1 && myObjects (theObject)->IsHilighted() && myAutoHilight)
3343   {
3344     Handle(Prs3d_Drawer) aCustomStyle;
3345     if (HighlightStyle (theObject, aCustomStyle))
3346     {
3347       if (!aCustomStyle.IsNull() && anObjSelStyle != aCustomStyle)
3348       {
3349         HilightWithColor (theObject, anObjSelStyle, theToUpdateViewer);
3350       }
3351     }
3352     return;
3353   }
3354
3355   for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
3356   {
3357     const Handle(SelectMgr_EntityOwner)& aSelOwner = aSelIter.Value();
3358     if (!myFilters->IsOk (aSelOwner))
3359     {
3360       continue;
3361     }
3362
3363     Handle(AIS_InteractiveObject) aSelectable = Handle(AIS_InteractiveObject)::DownCast (aSelOwner->Selectable());
3364     if (myAutoHilight)
3365     {
3366       Unhilight (aSelectable, Standard_False);
3367     }
3368     if (aSelOwner == aSelectable->GlobalSelOwner())
3369     {
3370       if (Handle(AIS_GlobalStatus)* aStatusPtr = myObjects.ChangeSeek (aSelectable))
3371       {
3372         (*aStatusPtr)->SetHilightStatus (Standard_False);
3373       }
3374     }
3375   }
3376
3377   // added to avoid untimely viewer update...
3378   mySelection->ClearAndSelect (anOwner);
3379
3380   if (myAutoHilight)
3381   {
3382     Handle(Prs3d_Drawer) aCustomStyle;
3383     if (HighlightStyle (theObject, aCustomStyle))
3384     {
3385       if (!aCustomStyle.IsNull() && anObjSelStyle != aCustomStyle)
3386       {
3387         HilightWithColor (theObject, anObjSelStyle, Standard_False);
3388       }
3389     }
3390     else
3391     {
3392       HilightWithColor (theObject, anObjSelStyle, Standard_False);
3393     }
3394   }
3395
3396   if (theToUpdateViewer)
3397   {
3398     UpdateCurrentViewer();
3399   }
3400 }
3401
3402 //=======================================================================
3403 //function : SetSelected
3404 //purpose  : Sets the whole object as selected and highlights it with selection color
3405 //=======================================================================
3406 void AIS_InteractiveContext::SetSelected (const Handle(SelectMgr_EntityOwner)& theOwner,
3407                                           const Standard_Boolean theToUpdateViewer)
3408 {
3409   if (theOwner.IsNull() || !theOwner->HasSelectable() || !myFilters->IsOk (theOwner))
3410   {
3411     return;
3412   }
3413
3414   const Handle(AIS_InteractiveObject) anObject = Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
3415   const Handle(Prs3d_Drawer)& anObjSelStyle = getSelStyle (anObject, theOwner);
3416   if (NbSelected() == 1 && theOwner->IsSelected() && !theOwner->IsForcedHilight())
3417   {
3418     Handle(Prs3d_Drawer) aCustomStyle;
3419     if (myAutoHilight && HighlightStyle (theOwner, aCustomStyle))
3420     {
3421       if (!aCustomStyle.IsNull() && anObjSelStyle != aCustomStyle)
3422       {
3423         const Standard_Integer aHiMode = anObject->HasHilightMode() ? anObject->HilightMode() : 0;
3424         theOwner->HilightWithColor (myMainPM, anObjSelStyle, aHiMode);
3425       }
3426     }
3427     return;
3428   }
3429
3430   if (!myObjects.IsBound (anObject))
3431   {
3432     return;
3433   }
3434
3435   if (myAutoHilight)
3436   {
3437     unhighlightSelected();
3438   }
3439
3440   mySelection->ClearAndSelect (theOwner);
3441   if (myAutoHilight)
3442   {
3443     Handle(Prs3d_Drawer) aCustomStyle;
3444     if (!HighlightStyle (theOwner, aCustomStyle) ||
3445       (!aCustomStyle.IsNull() && aCustomStyle != anObjSelStyle))
3446     {
3447       highlightSelected (theOwner);
3448     }
3449   }
3450
3451   if (theToUpdateViewer)
3452   {
3453     UpdateCurrentViewer();
3454   }
3455 }
3456
3457 //=======================================================================
3458 //function : AddOrRemoveSelected
3459 //purpose  :
3460 //=======================================================================
3461 void AIS_InteractiveContext::AddOrRemoveSelected (const Handle(AIS_InteractiveObject)& theObject,
3462                                                   const Standard_Boolean theToUpdateViewer)
3463 {
3464   if (theObject.IsNull()
3465   || !myObjects.IsBound (theObject))
3466   {
3467     return;
3468   }
3469
3470   const Handle(SelectMgr_EntityOwner) anOwner = theObject->GlobalSelOwner();
3471   if (!anOwner.IsNull()
3472     && anOwner->HasSelectable())
3473   {
3474     AddOrRemoveSelected (anOwner, theToUpdateViewer);
3475   }
3476 }
3477
3478 //=======================================================================
3479 //function : AddOrRemoveSelected
3480 //purpose  : Allows to highlight or unhighlight the owner given depending on
3481 //           its selection status
3482 //=======================================================================
3483 void AIS_InteractiveContext::AddOrRemoveSelected (const Handle(SelectMgr_EntityOwner)& theOwner,
3484                                                   const Standard_Boolean theToUpdateViewer)
3485 {
3486   if (theOwner.IsNull() || !theOwner->HasSelectable())
3487   {
3488     return;
3489   }
3490
3491   if (!myFilters->IsOk(theOwner) && !theOwner->IsSelected())
3492   {
3493     return;
3494   }
3495
3496   mySelection->Select (theOwner);
3497
3498   if (myAutoHilight)
3499   {
3500     const Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
3501     Handle(AIS_GlobalStatus)* aStatusPtr = myObjects.ChangeSeek (anObj);
3502     if (!aStatusPtr)
3503     {
3504       return;
3505     }
3506
3507     if (theOwner->IsSelected())
3508     {
3509       highlightSelected (theOwner);
3510     }
3511     else
3512     {
3513       AIS_NListOfEntityOwner anOwners;
3514       anOwners.Append (theOwner);
3515       unhighlightOwners (anOwners);
3516
3517       (*aStatusPtr)->SetHilightStyle (Handle(Prs3d_Drawer)());
3518     }
3519   }
3520
3521   if (theToUpdateViewer)
3522   {
3523     UpdateCurrentViewer();
3524   }
3525 }
3526
3527 // =======================================================================
3528 // function : SetSelectedState
3529 // purpose  :
3530 // =======================================================================
3531 Standard_Boolean AIS_InteractiveContext::SetSelectedState (const Handle(SelectMgr_EntityOwner)& theEntity,
3532                                                            const Standard_Boolean theIsSelected)
3533 {
3534   if (theEntity.IsNull())
3535   {
3536     throw Standard_ProgramError ("Internal error: AIS_InteractiveContext::SetSelectedState() called with NO object");
3537   }
3538
3539   if (!theEntity->HasSelectable()
3540     || mySelection->IsSelected (theEntity) == theIsSelected)
3541   {
3542     return false;
3543   }
3544
3545   if (theEntity->IsAutoHilight())
3546   {
3547     AddOrRemoveSelected (theEntity, false);
3548     return true;
3549   }
3550
3551   if (theIsSelected)
3552   {
3553     const AIS_SelectStatus aSelStatus = mySelection->AddSelect (theEntity);
3554     theEntity->SetSelected (true);
3555     return aSelStatus == AIS_SS_Added;
3556   }
3557   else
3558   {
3559     const AIS_SelectStatus aSelStatus = mySelection->Select (theEntity);
3560     theEntity->SetSelected (false);
3561     return aSelStatus == AIS_SS_Removed;
3562   }
3563 }
3564
3565 //=======================================================================
3566 //function : IsSelected
3567 //purpose  :
3568 //=======================================================================
3569 Standard_Boolean AIS_InteractiveContext::IsSelected (const Handle(AIS_InteractiveObject)& theObj) const
3570 {
3571   if (theObj.IsNull())
3572   {
3573     return Standard_False;
3574   }
3575
3576   const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theObj);
3577   if (aStatus == NULL)
3578   {
3579     return Standard_False;
3580   }
3581
3582   const Standard_Integer aGlobalSelMode = theObj->GlobalSelectionMode();
3583   const TColStd_ListOfInteger& anActivatedModes = (*aStatus)->SelectionModes();
3584   for (TColStd_ListIteratorOfListOfInteger aModeIter (anActivatedModes); aModeIter.More(); aModeIter.Next())
3585   {
3586     if (aModeIter.Value() == aGlobalSelMode)
3587     {
3588       if (Handle(SelectMgr_EntityOwner) aGlobOwner = theObj->GlobalSelOwner())
3589       {
3590         return aGlobOwner->IsSelected();
3591       }
3592       return Standard_False;
3593     }
3594   }
3595   return Standard_False;
3596 }
3597
3598 //=======================================================================
3599 //function : FirstSelectedObject
3600 //purpose  :
3601 //=======================================================================
3602 Handle(AIS_InteractiveObject) AIS_InteractiveContext::FirstSelectedObject() const
3603 {
3604   return !mySelection->Objects().IsEmpty()
3605         ? Handle(AIS_InteractiveObject)::DownCast (mySelection->Objects().First()->Selectable())
3606         : Handle(AIS_InteractiveObject)();
3607 }
3608
3609 //=======================================================================
3610 //function : HasSelectedShape
3611 //purpose  :
3612 //=======================================================================
3613 Standard_Boolean AIS_InteractiveContext::HasSelectedShape() const
3614 {
3615   if (!mySelection->More())
3616   {
3617     return Standard_False;
3618   }
3619
3620   const Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast (mySelection->Value());
3621   return !anOwner.IsNull() && anOwner->HasShape();
3622 }
3623
3624 //=======================================================================
3625 //function : SelectedShape
3626 //purpose  :
3627 //=======================================================================
3628 TopoDS_Shape AIS_InteractiveContext::SelectedShape() const
3629 {
3630   if (!mySelection->More())
3631   {
3632     return TopoDS_Shape();
3633   }
3634
3635   const Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast (mySelection->Value());
3636   if (anOwner.IsNull() || !anOwner->HasSelectable())
3637   {
3638     return TopoDS_Shape();
3639   }
3640
3641   return anOwner->Shape().Located (anOwner->Location() * anOwner->Shape().Location());
3642 }
3643
3644 //=======================================================================
3645 //function : EntityOwners
3646 //purpose  :
3647 //=======================================================================
3648 void AIS_InteractiveContext::EntityOwners (Handle(SelectMgr_IndexedMapOfOwner)& theOwners,
3649                                            const Handle(AIS_InteractiveObject)& theIObj,
3650                                            const Standard_Integer theMode) const
3651 {
3652   if (theIObj.IsNull())
3653   {
3654     return;
3655   }
3656
3657   TColStd_ListOfInteger aModes;
3658   if (theMode == -1)
3659   {
3660     ActivatedModes (theIObj, aModes);
3661   }
3662   else
3663   {
3664     aModes.Append (theMode);
3665   }
3666
3667   if (theOwners.IsNull())
3668   {
3669     theOwners = new SelectMgr_IndexedMapOfOwner();
3670   }
3671
3672   for (TColStd_ListIteratorOfListOfInteger anItr (aModes); anItr.More(); anItr.Next())
3673   {
3674     const int aMode = anItr.Value();
3675     const Handle(SelectMgr_Selection)& aSel = theIObj->Selection (aMode);
3676     if (aSel.IsNull())
3677     {
3678       continue;
3679     }
3680
3681     for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
3682     {
3683       if (Handle(Select3D_SensitiveEntity) aEntity = aSelEntIter.Value()->BaseSensitive())
3684       {
3685         if (const Handle(SelectMgr_EntityOwner)& aOwner = aEntity->OwnerId())
3686         {
3687           theOwners->Add (aOwner);
3688         }
3689       }
3690     }
3691   }
3692 }
3693
3694 //=======================================================================
3695 //function : HasDetectedShape
3696 //purpose  :
3697 //=======================================================================
3698 Standard_Boolean AIS_InteractiveContext::HasDetectedShape() const
3699 {
3700   Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast (myLastPicked);
3701   return !anOwner.IsNull()
3702        && anOwner->HasShape();
3703 }
3704
3705 //=======================================================================
3706 //function : DetectedShape
3707 //purpose  :
3708 //=======================================================================
3709 const TopoDS_Shape& AIS_InteractiveContext::DetectedShape() const
3710 {
3711   Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast (myLastPicked);
3712   return anOwner->Shape();
3713 }
3714
3715 //=======================================================================
3716 //function : HilightNextDetected
3717 //purpose  :
3718 //=======================================================================
3719 Standard_Integer AIS_InteractiveContext::HilightNextDetected (const Handle(V3d_View)& theView,
3720                                                               const Standard_Boolean  theToRedrawImmediate)
3721 {
3722   myMainPM->ClearImmediateDraw();
3723   if (myDetectedSeq.IsEmpty())
3724   {
3725     return 0;
3726   }
3727
3728   if (++myCurHighlighted > myDetectedSeq.Upper())
3729   {
3730     myCurHighlighted = myDetectedSeq.Lower();
3731   }
3732   const Handle(SelectMgr_EntityOwner)& anOwner = myMainSel->Picked (myDetectedSeq (myCurHighlighted));
3733   if (anOwner.IsNull())
3734   {
3735     return 0;
3736   }
3737
3738   highlightWithColor (anOwner, theView->Viewer());
3739   myLastPicked = anOwner;
3740
3741   if (theToRedrawImmediate)
3742   {
3743     myMainPM->RedrawImmediate (theView->Viewer());
3744     myMainVwr->RedrawImmediate();
3745   }
3746
3747   return myCurHighlighted;
3748 }
3749
3750 //=======================================================================
3751 //function : HilightPreviousDetected
3752 //purpose  :
3753 //=======================================================================
3754 Standard_Integer AIS_InteractiveContext::HilightPreviousDetected (const Handle(V3d_View)& theView,
3755                                                                   const Standard_Boolean  theToRedrawImmediate)
3756 {
3757   myMainPM->ClearImmediateDraw();
3758   if (myDetectedSeq.IsEmpty())
3759   {
3760     return 0;
3761   }
3762
3763   if (--myCurHighlighted < myDetectedSeq.Lower())
3764   {
3765     myCurHighlighted = myDetectedSeq.Upper();
3766   }
3767   const Handle(SelectMgr_EntityOwner)& anOwner = myMainSel->Picked (myDetectedSeq (myCurHighlighted));
3768   if (anOwner.IsNull())
3769   {
3770     return 0;
3771   }
3772
3773   highlightWithColor (anOwner, theView->Viewer());
3774   myLastPicked = anOwner;
3775
3776   if (theToRedrawImmediate)
3777   {
3778     myMainPM->RedrawImmediate (theView->Viewer());
3779     myMainVwr->RedrawImmediate();
3780   }
3781
3782   return myCurHighlighted;
3783 }
3784
3785 //=======================================================================
3786 //function : DetectedCurrentOwner
3787 //purpose  :
3788 //=======================================================================
3789 Handle(SelectMgr_EntityOwner) AIS_InteractiveContext::DetectedCurrentOwner() const
3790 {
3791   return MoreDetected()
3792        ? myMainSel->Picked (myDetectedSeq (myCurDetected))
3793        : Handle(SelectMgr_EntityOwner)();
3794 }
3795
3796 //=======================================================================
3797 //function : DetectedCurrentShape
3798 //purpose  :
3799 //=======================================================================
3800 const TopoDS_Shape& AIS_InteractiveContext::DetectedCurrentShape() const
3801 {
3802   static const TopoDS_Shape AIS_InteractiveContext_myDummyShape;
3803
3804   Standard_DISABLE_DEPRECATION_WARNINGS
3805   Handle(AIS_Shape) aCurrentShape = Handle(AIS_Shape)::DownCast (DetectedCurrentObject());
3806   Standard_ENABLE_DEPRECATION_WARNINGS
3807   return !aCurrentShape.IsNull()
3808         ? aCurrentShape->Shape()
3809         : AIS_InteractiveContext_myDummyShape;
3810 }
3811
3812 //=======================================================================
3813 //function : DetectedCurrentObject
3814 //purpose  :
3815 //=======================================================================
3816 Handle(AIS_InteractiveObject) AIS_InteractiveContext::DetectedCurrentObject() const
3817 {
3818   return MoreDetected()
3819        ? Handle(AIS_InteractiveObject)::DownCast (myMainSel->Picked (myDetectedSeq (myCurDetected))->Selectable())
3820        : Handle(AIS_InteractiveObject)();
3821 }
3822
3823 //=======================================================================
3824 //function : SetSelectionModeActive
3825 //purpose  :
3826 //=======================================================================
3827 void AIS_InteractiveContext::SetSelectionModeActive (const Handle(AIS_InteractiveObject)& theObj,
3828                                                      const Standard_Integer theMode,
3829                                                      const Standard_Boolean theIsActive,
3830                                                      const AIS_SelectionModesConcurrency theActiveFilter,
3831                                                      const Standard_Boolean theIsForce)
3832 {
3833   if (theObj.IsNull())
3834   {
3835     return;
3836   }
3837
3838   const Handle(AIS_GlobalStatus)* aStat = myObjects.Seek (theObj);
3839   if (aStat == NULL)
3840   {
3841     return;
3842   }
3843
3844   if (!theIsActive
3845    || (theMode == -1
3846     && theActiveFilter == AIS_SelectionModesConcurrency_Single))
3847   {
3848     if (theObj->DisplayStatus() == PrsMgr_DisplayStatus_Displayed
3849      || theIsForce)
3850     {
3851       if (theMode == -1)
3852       {
3853         for (TColStd_ListIteratorOfListOfInteger aModeIter ((*aStat)->SelectionModes()); aModeIter.More(); aModeIter.Next())
3854         {
3855           mgrSelector->Deactivate (theObj, aModeIter.Value());
3856         }
3857       }
3858       else
3859       {
3860         mgrSelector->Deactivate (theObj, theMode);
3861       }
3862     }
3863
3864     if (theMode == -1)
3865     {
3866       (*aStat)->ClearSelectionModes();
3867     }
3868     else
3869     {
3870       (*aStat)->RemoveSelectionMode (theMode);
3871     }
3872     return;
3873   }
3874   else if (theMode == -1)
3875   {
3876     return;
3877   }
3878
3879   if ((*aStat)->SelectionModes().Size() == 1
3880    && (*aStat)->SelectionModes().First() == theMode)
3881   {
3882     return;
3883   }
3884
3885   if (theObj->DisplayStatus() == PrsMgr_DisplayStatus_Displayed
3886    || theIsForce)
3887   {
3888     switch (theActiveFilter)
3889     {
3890       case AIS_SelectionModesConcurrency_Single:
3891       {
3892         for (TColStd_ListIteratorOfListOfInteger aModeIter ((*aStat)->SelectionModes()); aModeIter.More(); aModeIter.Next())
3893         {
3894           mgrSelector->Deactivate (theObj, aModeIter.Value());
3895         }
3896         (*aStat)->ClearSelectionModes();
3897         break;
3898       }
3899       case AIS_SelectionModesConcurrency_GlobalOrLocal:
3900       {
3901         const Standard_Integer aGlobSelMode = theObj->GlobalSelectionMode();
3902         TColStd_ListOfInteger aRemovedModes;
3903         for (TColStd_ListIteratorOfListOfInteger aModeIter ((*aStat)->SelectionModes()); aModeIter.More(); aModeIter.Next())
3904         {
3905           if ((theMode == aGlobSelMode && aModeIter.Value() != aGlobSelMode)
3906            || (theMode != aGlobSelMode && aModeIter.Value() == aGlobSelMode))
3907           {
3908             mgrSelector->Deactivate (theObj, aModeIter.Value());
3909             aRemovedModes.Append (aModeIter.Value());
3910           }
3911         }
3912         if (aRemovedModes.Size() == (*aStat)->SelectionModes().Size())
3913         {
3914           (*aStat)->ClearSelectionModes();
3915         }
3916         else
3917         {
3918           for (TColStd_ListIteratorOfListOfInteger aModeIter (aRemovedModes); aModeIter.More(); aModeIter.Next())
3919           {
3920             (*aStat)->RemoveSelectionMode (aModeIter.Value());
3921           }
3922         }
3923         break;
3924       }
3925       case AIS_SelectionModesConcurrency_Multiple:
3926       {
3927         break;
3928       }
3929     }
3930     mgrSelector->Activate (theObj, theMode);
3931   }
3932   (*aStat)->AddSelectionMode (theMode);
3933 }
3934
3935 // ============================================================================
3936 // function : Activate
3937 // purpose  :
3938 // ============================================================================
3939 void AIS_InteractiveContext::Activate (const Standard_Integer theMode,
3940                                        const Standard_Boolean theIsForce)
3941 {
3942   AIS_ListOfInteractive aDisplayedObjects;
3943   DisplayedObjects (aDisplayedObjects);
3944   for (AIS_ListOfInteractive::Iterator anIter (aDisplayedObjects); anIter.More(); anIter.Next())
3945   {
3946     Load (anIter.Value(), -1);
3947     Activate (anIter.Value(), theMode, theIsForce);
3948   }
3949 }
3950
3951 // ============================================================================
3952 // function : Deactivate
3953 // purpose  :
3954 // ============================================================================
3955 void AIS_InteractiveContext::Deactivate (const Standard_Integer theMode)
3956 {
3957   AIS_ListOfInteractive aDisplayedObjects;
3958   DisplayedObjects (aDisplayedObjects);
3959   for (AIS_ListOfInteractive::Iterator anIter (aDisplayedObjects); anIter.More(); anIter.Next())
3960   {
3961     Deactivate (anIter.Value(), theMode);
3962   }
3963 }
3964
3965 // ============================================================================
3966 // function : Deactivate
3967 // purpose  :
3968 // ============================================================================
3969 void AIS_InteractiveContext::Deactivate()
3970 {
3971   AIS_ListOfInteractive aDisplayedObjects;
3972   DisplayedObjects (aDisplayedObjects);
3973
3974   for (AIS_ListOfInteractive::Iterator anIter (aDisplayedObjects); anIter.More(); anIter.Next())
3975   {
3976     Deactivate (anIter.Value());
3977   }
3978 }
3979
3980 //=======================================================================
3981 //function : ActivatedModes
3982 //purpose  :
3983 //=======================================================================
3984 void AIS_InteractiveContext::ActivatedModes (const Handle(AIS_InteractiveObject)& theObj,
3985                                              TColStd_ListOfInteger& theList) const
3986 {
3987   const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theObj);
3988   if (aStatus != NULL)
3989   {
3990     for (TColStd_ListIteratorOfListOfInteger aModeIter ((*aStatus)->SelectionModes()); aModeIter.More(); aModeIter.Next())
3991     {
3992       theList.Append (aModeIter.Value());
3993     }
3994   }
3995 }
3996
3997 //=======================================================================
3998 //function : SubIntensityOn
3999 //purpose  :
4000 //=======================================================================
4001 void AIS_InteractiveContext::SubIntensityOn (const Handle(AIS_InteractiveObject)& theObj,
4002                                              const Standard_Boolean theToUpdateViewer)
4003 {
4004   turnOnSubintensity (theObj);
4005   if (theToUpdateViewer)
4006   {
4007     myMainVwr->Update();
4008   }
4009 }
4010
4011 //=======================================================================
4012 //function : SubIntensityOff
4013 //purpose  :
4014 //=======================================================================
4015 void AIS_InteractiveContext::SubIntensityOff (const Handle(AIS_InteractiveObject)& theObj,
4016                                               const Standard_Boolean theToUpdateViewer)
4017 {
4018   const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theObj);
4019   if (aStatus == NULL
4020    || !(*aStatus)->IsSubIntensityOn())
4021   {
4022     return;
4023   }
4024
4025   (*aStatus)->SetSubIntensity (false);
4026   Standard_Boolean toUpdateMain = Standard_False;
4027   if (theObj->DisplayStatus() == PrsMgr_DisplayStatus_Displayed)
4028   {
4029     myMainPM->Unhighlight (theObj);
4030     toUpdateMain = Standard_True;
4031   }
4032
4033   if (IsSelected (theObj))
4034   {
4035     highlightSelected (theObj->GlobalSelOwner());
4036   }
4037
4038   if (theToUpdateViewer && toUpdateMain)
4039   {
4040     myMainVwr->Update();
4041   }
4042 }
4043
4044 //=======================================================================
4045 //function : DisplayActiveSensitive
4046 //purpose  :
4047 //=======================================================================
4048 void AIS_InteractiveContext::DisplayActiveSensitive(const Handle(V3d_View)& theView)
4049 {
4050   myMainSel->DisplaySensitive (theView);
4051 }
4052
4053 //=======================================================================
4054 //function : DisplayActiveSensitive
4055 //purpose  :
4056 //=======================================================================
4057 void AIS_InteractiveContext::DisplayActiveSensitive (const Handle(AIS_InteractiveObject)& theObj,
4058                                                      const Handle(V3d_View)& theView)
4059 {
4060   const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theObj);
4061   if (aStatus == NULL)
4062   {
4063     return;
4064   }
4065
4066   for (TColStd_ListIteratorOfListOfInteger aModeIter ((*aStatus)->SelectionModes()); aModeIter.More(); aModeIter.Next())
4067   {
4068     const Handle(SelectMgr_Selection)& aSel = theObj->Selection (aModeIter.Value());
4069     myMainSel->DisplaySensitive (aSel, theObj->Transformation(), theView, Standard_False);
4070   }
4071 }
4072
4073 //=======================================================================
4074 //function : ClearActiveSensitive
4075 //purpose  :
4076 //=======================================================================
4077 void AIS_InteractiveContext::ClearActiveSensitive (const Handle(V3d_View)& theView)
4078 {
4079   myMainSel->ClearSensitive (theView);
4080 }
4081
4082 //=======================================================================
4083 //function : IsImmediateModeOn
4084 //purpose  :
4085 //=======================================================================
4086 Standard_Boolean AIS_InteractiveContext::IsImmediateModeOn() const
4087 {
4088   return myMainPM->IsImmediateModeOn();
4089 }
4090
4091 //=======================================================================
4092 //function : BeginImmediateDraw
4093 //purpose  :
4094 //=======================================================================
4095 Standard_Boolean AIS_InteractiveContext::BeginImmediateDraw()
4096 {
4097   if (myMainPM->IsImmediateModeOn())
4098   {
4099     myMainPM->BeginImmediateDraw();
4100     return Standard_True;
4101   }
4102   return Standard_False;
4103 }
4104
4105 //=======================================================================
4106 //function : ImmediateAdd
4107 //purpose  :
4108 //=======================================================================
4109 Standard_Boolean AIS_InteractiveContext::ImmediateAdd (const Handle(AIS_InteractiveObject)& theObj,
4110                                                        const Standard_Integer theMode)
4111 {
4112   if (myMainPM->IsImmediateModeOn())
4113   {
4114     myMainPM->AddToImmediateList (myMainPM->Presentation (theObj, theMode));
4115     return Standard_True;
4116   }
4117   return Standard_False;
4118 }
4119
4120 //=======================================================================
4121 //function : EndImmediateDraw
4122 //purpose  :
4123 //=======================================================================
4124 Standard_Boolean AIS_InteractiveContext::EndImmediateDraw (const Handle(V3d_View)& theView)
4125 {
4126   if (myMainPM->IsImmediateModeOn())
4127   {
4128     myMainPM->EndImmediateDraw (theView->Viewer());
4129     return Standard_True;
4130   }
4131   return Standard_False;
4132 }
4133
4134 //=======================================================================
4135 //function : EndImmediateDraw
4136 //purpose  :
4137 //=======================================================================
4138 Standard_Boolean AIS_InteractiveContext::EndImmediateDraw()
4139 {
4140   if (myMainPM->IsImmediateModeOn())
4141   {
4142     myMainPM->EndImmediateDraw (myMainVwr);
4143     return Standard_True;
4144   }
4145   return Standard_False;
4146 }
4147
4148 //=======================================================================
4149 //function : SetPolygonOffsets
4150 //purpose  :
4151 //=======================================================================
4152 void AIS_InteractiveContext::SetPolygonOffsets (const Handle(AIS_InteractiveObject)& theObj,
4153                                                 const Standard_Integer   theMode,
4154                                                 const Standard_ShortReal theFactor,
4155                                                 const Standard_ShortReal theUnits,
4156                                                 const Standard_Boolean   theToUpdateViewer)
4157 {
4158   if (theObj.IsNull())
4159   {
4160     return;
4161   }
4162
4163   setContextToObject (theObj);
4164   theObj->SetPolygonOffsets (theMode, theFactor, theUnits);
4165
4166   const Handle(AIS_GlobalStatus)* aStatus = theToUpdateViewer ? myObjects.Seek (theObj) : NULL;
4167   if (aStatus != NULL
4168    && theObj->DisplayStatus() == PrsMgr_DisplayStatus_Displayed)
4169   {
4170     myMainVwr->Update();
4171   }
4172 }
4173
4174 //=======================================================================
4175 //function : HasPolygonOffsets
4176 //purpose  :
4177 //=======================================================================
4178 Standard_Boolean AIS_InteractiveContext::HasPolygonOffsets (const Handle(AIS_InteractiveObject)& theObj) const
4179 {
4180   return !theObj.IsNull() && theObj->HasPolygonOffsets();
4181 }
4182
4183 //=======================================================================
4184 //function : PolygonOffsets
4185 //purpose  :
4186 //=======================================================================
4187 void AIS_InteractiveContext::PolygonOffsets (const Handle(AIS_InteractiveObject)& theObj,
4188                                              Standard_Integer&   theMode,
4189                                              Standard_ShortReal& theFactor,
4190                                              Standard_ShortReal& theUnits) const
4191 {
4192   if (HasPolygonOffsets (theObj))
4193   {
4194     theObj->PolygonOffsets (theMode, theFactor, theUnits);
4195   }
4196 }
4197
4198 //=======================================================================
4199 //function : DumpJson
4200 //purpose  :
4201 //=======================================================================
4202 void AIS_InteractiveContext::DumpJson (Standard_OStream& theOStream, Standard_Integer) const
4203 {
4204   OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
4205
4206   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myObjects.Size())
4207
4208   OCCT_DUMP_FIELD_VALUE_POINTER (theOStream, mgrSelector.get())
4209   OCCT_DUMP_FIELD_VALUE_POINTER (theOStream, myMainPM.get())
4210   OCCT_DUMP_FIELD_VALUE_POINTER (theOStream, myMainVwr.get())
4211   OCCT_DUMP_FIELD_VALUE_POINTER (theOStream, myMainSel.get())
4212   OCCT_DUMP_FIELD_VALUE_POINTER (theOStream, myLastActiveView)
4213
4214   OCCT_DUMP_FIELD_VALUE_POINTER (theOStream, myLastPicked.get())
4215
4216   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myToHilightSelected)
4217
4218   OCCT_DUMP_FIELD_VALUE_POINTER (theOStream, mySelection.get())
4219   OCCT_DUMP_FIELD_VALUE_POINTER (theOStream, myFilters.get())
4220   OCCT_DUMP_FIELD_VALUE_POINTER (theOStream, myDefaultDrawer.get())
4221
4222   OCCT_DUMP_FIELD_VALUE_POINTER (theOStream, myStyles[Prs3d_TypeOfHighlight_Selected])
4223   OCCT_DUMP_FIELD_VALUE_POINTER (theOStream, myStyles[Prs3d_TypeOfHighlight_Dynamic])
4224   OCCT_DUMP_FIELD_VALUE_POINTER (theOStream, myStyles[Prs3d_TypeOfHighlight_LocalSelected])
4225   OCCT_DUMP_FIELD_VALUE_POINTER (theOStream, myStyles[Prs3d_TypeOfHighlight_LocalDynamic])
4226   OCCT_DUMP_FIELD_VALUE_POINTER (theOStream, myStyles[Prs3d_TypeOfHighlight_SubIntensity])
4227
4228   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myDetectedSeq.Size())
4229
4230   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myCurDetected)
4231   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myCurHighlighted)
4232   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myPickingStrategy)
4233   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myAutoHilight)
4234   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myIsAutoActivateSelMode)
4235 }