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