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