0031668: Visualization - WebGL sample doesn't work on Emscripten 1.39
[occt.git] / src / AIS / AIS_InteractiveContext_2.cxx
1 // Created on: 1997-01-29
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
18 #include <AIS_DataMapIteratorOfDataMapOfIOStatus.hxx>
19 #include <AIS_GlobalStatus.hxx>
20 #include <AIS_InteractiveContext.hxx>
21 #include <AIS_InteractiveObject.hxx>
22 #include <AIS_Selection.hxx>
23 #include <Graphic3d_MapIteratorOfMapOfStructure.hxx>
24 #include <Graphic3d_MapOfStructure.hxx>
25 #include <Graphic3d_Structure.hxx>
26 #include <Graphic3d_StructureManager.hxx>
27 #include <Prs3d_BasicAspect.hxx>
28 #include <Prs3d_LineAspect.hxx>
29 #include <Quantity_Color.hxx>
30 #include <SelectMgr_EntityOwner.hxx>
31 #include <SelectMgr_Filter.hxx>
32 #include <SelectMgr_OrFilter.hxx>
33 #include <SelectMgr_SelectionManager.hxx>
34 #include <Standard_Transient.hxx>
35 #include <StdSelect_ViewerSelector3d.hxx>
36 #include <TCollection_AsciiString.hxx>
37 #include <TCollection_ExtendedString.hxx>
38 #include <TColStd_ListIteratorOfListOfInteger.hxx>
39 #include <TopLoc_Location.hxx>
40 #include <TopoDS_Shape.hxx>
41 #include <V3d_View.hxx>
42 #include <V3d_Viewer.hxx>
43
44 //=======================================================================
45 //function : SetSelectionModeActive
46 //purpose  :
47 //=======================================================================
48 void AIS_InteractiveContext::SetSelectionModeActive (const Handle(AIS_InteractiveObject)& theObj,
49                                                      const Standard_Integer theMode,
50                                                      const Standard_Boolean theIsActive,
51                                                      const AIS_SelectionModesConcurrency theActiveFilter,
52                                                      const Standard_Boolean theIsForce)
53 {
54   if (theObj.IsNull())
55   {
56     return;
57   }
58
59   const Handle(AIS_GlobalStatus)* aStat = myObjects.Seek (theObj);
60   if (aStat == NULL)
61   {
62     return;
63   }
64
65   if (!theIsActive
66    || (theMode == -1
67     && theActiveFilter == AIS_SelectionModesConcurrency_Single))
68   {
69     if ((*aStat)->GraphicStatus() == AIS_DS_Displayed
70      || theIsForce)
71     {
72       if (theMode == -1)
73       {
74         for (TColStd_ListIteratorOfListOfInteger aModeIter ((*aStat)->SelectionModes()); aModeIter.More(); aModeIter.Next())
75         {
76           mgrSelector->Deactivate (theObj, aModeIter.Value());
77         }
78       }
79       else
80       {
81         mgrSelector->Deactivate (theObj, theMode);
82       }
83     }
84
85     if (theMode == -1)
86     {
87       (*aStat)->ClearSelectionModes();
88     }
89     else
90     {
91       (*aStat)->RemoveSelectionMode (theMode);
92     }
93     return;
94   }
95   else if (theMode == -1)
96   {
97     return;
98   }
99
100   if ((*aStat)->SelectionModes().Size() == 1
101    && (*aStat)->SelectionModes().First() == theMode)
102   {
103     return;
104   }
105
106   if ((*aStat)->GraphicStatus() == AIS_DS_Displayed
107     || theIsForce)
108   {
109     switch (theActiveFilter)
110     {
111       case AIS_SelectionModesConcurrency_Single:
112       {
113         for (TColStd_ListIteratorOfListOfInteger aModeIter ((*aStat)->SelectionModes()); aModeIter.More(); aModeIter.Next())
114         {
115           mgrSelector->Deactivate (theObj, aModeIter.Value());
116         }
117         (*aStat)->ClearSelectionModes();
118         break;
119       }
120       case AIS_SelectionModesConcurrency_GlobalOrLocal:
121       {
122         const Standard_Integer aGlobSelMode = theObj->GlobalSelectionMode();
123         TColStd_ListOfInteger aRemovedModes;
124         for (TColStd_ListIteratorOfListOfInteger aModeIter ((*aStat)->SelectionModes()); aModeIter.More(); aModeIter.Next())
125         {
126           if ((theMode == aGlobSelMode && aModeIter.Value() != aGlobSelMode)
127            || (theMode != aGlobSelMode && aModeIter.Value() == aGlobSelMode))
128           {
129             mgrSelector->Deactivate (theObj, aModeIter.Value());
130             aRemovedModes.Append (aModeIter.Value());
131           }
132         }
133         if (aRemovedModes.Size() == (*aStat)->SelectionModes().Size())
134         {
135           (*aStat)->ClearSelectionModes();
136         }
137         else
138         {
139           for (TColStd_ListIteratorOfListOfInteger aModeIter (aRemovedModes); aModeIter.More(); aModeIter.Next())
140           {
141             (*aStat)->RemoveSelectionMode (aModeIter.Value());
142           }
143         }
144         break;
145       }
146       case AIS_SelectionModesConcurrency_Multiple:
147       {
148         break;
149       }
150     }
151     mgrSelector->Activate (theObj, theMode);
152   }
153   (*aStat)->AddSelectionMode (theMode);
154 }
155
156 // ============================================================================
157 // function : Activate
158 // purpose  :
159 // ============================================================================
160 void AIS_InteractiveContext::Activate (const Standard_Integer theMode,
161                                        const Standard_Boolean theIsForce)
162 {
163   AIS_ListOfInteractive aDisplayedObjects;
164   DisplayedObjects (aDisplayedObjects);
165
166   for (AIS_ListIteratorOfListOfInteractive anIter (aDisplayedObjects); anIter.More(); anIter.Next())
167   {
168     Load (anIter.Value(), -1);
169     Activate (anIter.Value(), theMode, theIsForce);
170   }
171
172 }
173
174 // ============================================================================
175 // function : Deactivate
176 // purpose  :
177 // ============================================================================
178 void AIS_InteractiveContext::Deactivate (const Standard_Integer theMode)
179 {
180   AIS_ListOfInteractive aDisplayedObjects;
181   DisplayedObjects (aDisplayedObjects);
182
183   for (AIS_ListIteratorOfListOfInteractive anIter (aDisplayedObjects); anIter.More(); anIter.Next())
184   {
185     Deactivate (anIter.Value(), theMode);
186   }
187 }
188
189 // ============================================================================
190 // function : Deactivate
191 // purpose  :
192 // ============================================================================
193 void AIS_InteractiveContext::Deactivate()
194 {
195   AIS_ListOfInteractive aDisplayedObjects;
196   DisplayedObjects (aDisplayedObjects);
197
198   for (AIS_ListIteratorOfListOfInteractive anIter (aDisplayedObjects); anIter.More(); anIter.Next())
199   {
200     Deactivate (anIter.Value());
201   }
202 }
203
204 //=======================================================================
205 //function : ActivatedModes
206 //purpose  :
207 //=======================================================================
208 void AIS_InteractiveContext::ActivatedModes (const Handle(AIS_InteractiveObject)& theObj,
209                                              TColStd_ListOfInteger& theList) const
210 {
211   const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theObj);
212   if (aStatus != NULL)
213   {
214     for (TColStd_ListIteratorOfListOfInteger aModeIter ((*aStatus)->SelectionModes()); aModeIter.More(); aModeIter.Next())
215     {
216       theList.Append (aModeIter.Value());
217     }
218   }
219 }
220
221 //=======================================================================
222 //function : SubIntensityOn
223 //purpose  : 
224 //=======================================================================
225 void AIS_InteractiveContext::
226 SubIntensityOn(const Handle(AIS_InteractiveObject)& anIObj,
227                const Standard_Boolean updateviewer)
228 {
229   turnOnSubintensity (anIObj);
230   if (updateviewer)
231     myMainVwr->Update();
232 }
233 //=======================================================================
234 //function : SubIntensityOff
235 //purpose  : 
236 //=======================================================================
237
238 void AIS_InteractiveContext::SubIntensityOff (const Handle(AIS_InteractiveObject)& theObj,
239                                               const Standard_Boolean theToUpdateViewer)
240 {
241   const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theObj);
242   if (aStatus == NULL
243    || !(*aStatus)->IsSubIntensityOn())
244   {
245     return;
246   }
247
248   (*aStatus)->SubIntensityOff();
249   Standard_Boolean toUpdateMain = Standard_False;
250   if ((*aStatus)->GraphicStatus() == AIS_DS_Displayed)
251   {
252     myMainPM->Unhighlight (theObj);
253     toUpdateMain = Standard_True;
254   }
255     
256   if (IsSelected (theObj))
257   {
258     highlightSelected (theObj->GlobalSelOwner());
259   }
260
261   if (theToUpdateViewer && toUpdateMain)
262   {
263     myMainVwr->Update();
264   }
265 }
266
267 //=======================================================================
268 //function : AddFilter
269 //purpose  : 
270 //=======================================================================
271 void AIS_InteractiveContext::AddFilter(const Handle(SelectMgr_Filter)& aFilter)
272 {
273   myFilters->Add(aFilter);
274 }
275
276 //=======================================================================
277 //function : RemoveFilter
278 //purpose  : 
279 //=======================================================================
280 void AIS_InteractiveContext::RemoveFilter(const Handle(SelectMgr_Filter)& aFilter)
281 {
282   myFilters->Remove(aFilter);
283 }
284
285 //=======================================================================
286 //function : RemoveFilters
287 //purpose  : 
288 //=======================================================================
289
290 void AIS_InteractiveContext::RemoveFilters()
291 {
292   myFilters->Clear();
293 }
294
295 //=======================================================================
296 //function : Filters
297 //purpose  : 
298 //=======================================================================
299 const SelectMgr_ListOfFilter& AIS_InteractiveContext::Filters() const 
300 {
301   return myFilters->StoredFilters();
302 }
303
304 //=======================================================================
305 //function : DisplayActiveSensitive
306 //purpose  : 
307 //=======================================================================
308 void AIS_InteractiveContext::DisplayActiveSensitive(const Handle(V3d_View)& aviou)
309 {
310   myMainSel->DisplaySensitive(aviou);
311 }
312 //=======================================================================
313 //function : DisplayActiveSensitive
314 //purpose  : 
315 //=======================================================================
316
317 void AIS_InteractiveContext::DisplayActiveSensitive(const Handle(AIS_InteractiveObject)& theObj,
318                                                     const Handle(V3d_View)& theView)
319 {
320   const Handle(AIS_GlobalStatus)* aStatus = myObjects.Seek (theObj);
321   if (aStatus == NULL)
322   {
323     return;
324   }
325
326   for (TColStd_ListIteratorOfListOfInteger aModeIter ((*aStatus)->SelectionModes()); aModeIter.More(); aModeIter.Next())
327   {
328     const Handle(SelectMgr_Selection)& aSel = theObj->Selection (aModeIter.Value());
329     myMainSel->DisplaySensitive (aSel, theObj->Transformation(), theView, Standard_False);
330   }
331 }
332
333 //=======================================================================
334 //function : ClearActiveSensitive
335 //purpose  : 
336 //=======================================================================
337 void AIS_InteractiveContext::ClearActiveSensitive (const Handle(V3d_View)& theView)
338 {
339   myMainSel->ClearSensitive (theView);
340 }
341
342 //=======================================================================
343 //function : PurgeDisplay
344 //purpose  : 
345 //=======================================================================
346
347 Standard_Integer AIS_InteractiveContext::PurgeDisplay()
348 {
349   Standard_Integer NbStr = PurgeViewer(myMainVwr);
350   myMainVwr->Update();
351   return NbStr;
352 }
353
354
355 //=======================================================================
356 //function : PurgeViewer
357 //purpose  : 
358 //=======================================================================
359 Standard_Integer AIS_InteractiveContext::PurgeViewer(const Handle(V3d_Viewer)& Vwr)
360 {
361   Handle(Graphic3d_StructureManager) GSM = Vwr->StructureManager();
362   Standard_Integer NbCleared(0);
363   Graphic3d_MapOfStructure SOS;
364   GSM->DisplayedStructures(SOS);
365
366   Handle(Graphic3d_Structure) G;
367   for(Graphic3d_MapIteratorOfMapOfStructure It(SOS); It.More();It.Next()){
368     G = It.Key();
369     Standard_Address Add = G->Owner();
370     if(Add==NULL){
371       G->Erase();
372       G->Clear();// it means that it is not referenced as a presentation of InterfactiveObject...
373       NbCleared++;
374     }
375     Handle(AIS_InteractiveObject) IO = (AIS_InteractiveObject*)Add;
376     if(!myObjects.IsBound(IO)){
377       G->Erase();
378       NbCleared++;
379     }
380   }
381   return NbCleared;
382 }
383
384 //=======================================================================
385 //function : IsImmediateModeOn
386 //purpose  :
387 //=======================================================================
388
389 Standard_Boolean AIS_InteractiveContext::IsImmediateModeOn()  const 
390 {
391   return myMainPM->IsImmediateModeOn();
392 }
393
394 //=======================================================================
395 //function : BeginImmediateDraw
396 //purpose  :
397 //=======================================================================
398
399 Standard_Boolean AIS_InteractiveContext::BeginImmediateDraw()
400 {
401   if (myMainPM->IsImmediateModeOn())
402   {
403     myMainPM->BeginImmediateDraw();
404     return Standard_True;
405   }
406   return Standard_False;
407 }
408
409 //=======================================================================
410 //function : ImmediateAdd
411 //purpose  :
412 //=======================================================================
413
414 Standard_Boolean AIS_InteractiveContext::ImmediateAdd (const Handle(AIS_InteractiveObject)& theObj,
415                                                        const Standard_Integer               theMode)
416 {
417   if (!myMainPM->IsImmediateModeOn())
418   {
419     return Standard_False;
420   }
421
422   myMainPM->AddToImmediateList (myMainPM->Presentation (theObj, theMode));
423   return Standard_True;
424 }
425
426 //=======================================================================
427 //function : EndImmediateDraw
428 //purpose  :
429 //=======================================================================
430
431 Standard_Boolean AIS_InteractiveContext::EndImmediateDraw (const Handle(V3d_View)& theView)
432 {
433   if (!myMainPM->IsImmediateModeOn())
434   {
435     return Standard_False;
436   }
437
438   myMainPM->EndImmediateDraw (theView->Viewer());
439   return Standard_True;
440 }
441
442 //=======================================================================
443 //function : EndImmediateDraw
444 //purpose  :
445 //=======================================================================
446
447 Standard_Boolean AIS_InteractiveContext::EndImmediateDraw()
448 {
449   if (!myMainPM->IsImmediateModeOn())
450   {
451     return Standard_False;
452   }
453
454   myMainPM->EndImmediateDraw (myMainVwr);
455   return Standard_True;
456 }