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