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