0026076: Visualization - empty bounding box of a shape after closing local context
[occt.git] / src / SelectMgr / SelectMgr_SelectionManager.cxx
CommitLineData
b311480e 1// Created on: 1995-02-13
2// Created by: Mister rmi
3// Copyright (c) 1995-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 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
973c2be1 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.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
17#include <SelectMgr_SelectionManager.ixx>
18#include <SelectMgr_ViewerSelector.hxx>
19#include <SelectMgr_Selection.hxx>
20#include <SelectMgr_SequenceOfSelector.hxx>
21#include <TColStd_MapIteratorOfMapOfTransient.hxx>
22#include <TColStd_MapIteratorOfMapOfTransient.hxx>
23#include <TCollection_AsciiString.hxx>
24#include <TColStd_ListOfInteger.hxx>
25#include <TColStd_ListIteratorOfListOfInteger.hxx>
26#include <SelectMgr_DataMapIteratorOfDataMapOfObjectSelectors.hxx>
27#include <OSD_Environment.hxx>
28
f751596e 29static Standard_Integer FindIndex (const SelectMgr_SequenceOfSelector& theSelectorsSeq,
30 const Handle(SelectMgr_ViewerSelector)& theSelector)
7fd59977 31{
f751596e 32 Standard_Integer aFoundIdx = 0;
7fd59977 33
f751596e 34 for (Standard_Integer anIdx = 1; anIdx <= theSelectorsSeq.Length() && aFoundIdx==0; anIdx++)
35 {
36 if (theSelector == theSelectorsSeq.Value (anIdx))
37 aFoundIdx = anIdx;
38 }
7fd59977 39
f751596e 40 return aFoundIdx;
41}
7fd59977 42
7fd59977 43//==================================================
44// Function: Create
45// Purpose :
46//==================================================
f751596e 47SelectMgr_SelectionManager::SelectMgr_SelectionManager() {}
7fd59977 48
49//==================================================
50// Function: Add
51// Purpose :
52//==================================================
f751596e 53void SelectMgr_SelectionManager::Add (const Handle(SelectMgr_ViewerSelector)& theSelector)
7fd59977 54{
f751596e 55 mySelectors.Add (theSelector);
7fd59977 56}
57
7fd59977 58//==================================================
59// Function: Remove
60// Purpose :
61//==================================================
f751596e 62void SelectMgr_SelectionManager::Remove (const Handle(SelectMgr_ViewerSelector)& theSelector)
7fd59977 63{
f751596e 64 for (SelectMgr_DataMapIteratorOfDataMapOfObjectSelectors aSelIter (myLocal); aSelIter.More(); aSelIter.Next())
7fd59977 65 {
f751596e 66 SelectMgr_SequenceOfSelector& theSelectors = myLocal.ChangeFind (aSelIter.Key());
67 Standard_Integer aRank = FindIndex (theSelectors, theSelector);
68 if (aRank != 0 && aRank <= theSelectors.Length())
69 theSelectors.Remove (aRank);
7fd59977 70 }
f751596e 71
72 if (mySelectors.Contains (theSelector))
73 mySelectors.Remove (theSelector);
7fd59977 74}
75
76//==================================================
77// Function: Contains
78// Purpose :
79//==================================================
f751596e 80Standard_Boolean SelectMgr_SelectionManager::Contains (const Handle(SelectMgr_ViewerSelector)& theSelector) const
81{
82 return mySelectors.Contains (theSelector);
83}
7fd59977 84
85//==================================================
86// Function: Contains
87// Purpose :
88//==================================================
f751596e 89Standard_Boolean SelectMgr_SelectionManager::Contains (const Handle(SelectMgr_SelectableObject)& theObject) const
90{
91 if (myGlobal.Contains (theObject))
92 return Standard_True;
7fd59977 93
f751596e 94 if (myLocal.IsBound (theObject))
95 return Standard_True;
7fd59977 96
f751596e 97 return Standard_False;
7fd59977 98}
99
7fd59977 100//==================================================
101// Function: Load
102// Purpose :
103//==================================================
f751596e 104void SelectMgr_SelectionManager::Load (const Handle(SelectMgr_SelectableObject)& theObject,
105 const Standard_Integer theMode)
7fd59977 106{
f751596e 107 if (myGlobal.Contains(theObject))
108 return;
7fd59977 109
f751596e 110 for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
111 {
112 Load (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode);
7fd59977 113 }
f751596e 114
115 if (!theObject->HasOwnPresentations())
116 return;
117
118 myGlobal.Add(theObject);
119 for (TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
120 {
121 Handle(SelectMgr_ViewerSelector) aSelector =
122 Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
123 if (!aSelector->Contains (theObject) && theObject->HasOwnPresentations())
124 {
125 aSelector->AddSelectableObject (theObject);
7fd59977 126 }
127 }
f751596e 128 if (theMode != -1)
129 loadMode (theObject, theMode);
7fd59977 130}
131
132
133//==================================================
f751596e 134// Function: Load
7fd59977 135// Purpose :
136//==================================================
f751596e 137void SelectMgr_SelectionManager::Load (const Handle(SelectMgr_SelectableObject)& theObject,
138 const Handle(SelectMgr_ViewerSelector)& theSelector,
139 const Standard_Integer theMode)
7fd59977 140{
f751596e 141 if (!mySelectors.Contains (theSelector))
142 {
143 mySelectors.Add (theSelector);
144 }
7fd59977 145
f751596e 146 if (theMode != -1)
147 loadMode (theObject, theMode, theSelector);
7fd59977 148
f751596e 149 if (theObject->HasOwnPresentations())
150 theSelector->AddSelectableObject (theObject);
151
152 if (myLocal.IsBound (theObject))
153 {
154 SelectMgr_SequenceOfSelector& aSelectors = myLocal.ChangeFind (theObject);
155 if (FindIndex (aSelectors, theSelector) == 0)
156 {
157 aSelectors.Append (theSelector);
7fd59977 158 }
7fd59977 159 }
f751596e 160 else
161 {
162 if (!myGlobal.Contains (theObject))
163 {
164 if (theObject->HasOwnPresentations())
165 {
166 SelectMgr_SequenceOfSelector aSelectors;
167 aSelectors.Append (theSelector);
168 myLocal.Bind (theObject, aSelectors);
169 }
170 else
171 {
172 for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
7fd59977 173 {
f751596e 174 Load (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theSelector, theMode);
7fd59977 175 }
176 }
7fd59977 177 }
7fd59977 178 }
179}
180
f751596e 181
7fd59977 182//==================================================
183// Function: Remove
184// Purpose :
185//==================================================
f751596e 186void SelectMgr_SelectionManager::Remove (const Handle(SelectMgr_SelectableObject)& theObject)
7fd59977 187{
f751596e 188 if (myGlobal.Contains (theObject))
189 {
190 for (TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
191 {
192 Handle(SelectMgr_ViewerSelector) aCurSelector =
193 Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
7fd59977 194
f751596e 195 if (!aCurSelector->Contains (theObject))
196 continue;
7fd59977 197
f751596e 198 for (theObject->Init(); theObject->More(); theObject->Next())
199 {
200 aCurSelector->RemoveSelectionOfObject (theObject, theObject->CurrentSelection());
201 theObject->CurrentSelection()->UpdateBVHStatus (SelectMgr_TBU_Remove);
7fd59977 202 }
f751596e 203 aCurSelector->RemoveSelectableObject (theObject);
7fd59977 204 }
f751596e 205
206 myGlobal.Remove (theObject);
7fd59977 207 }
f751596e 208 else if (myLocal.IsBound (theObject))
209 {
210 SelectMgr_SequenceOfSelector& aSelectors = myLocal.ChangeFind (theObject);
211 for (Standard_Integer aSelectorsIdx = 1; aSelectorsIdx <= aSelectors.Length(); aSelectorsIdx++)
212 {
213 Handle(SelectMgr_ViewerSelector) aCurSelector =
214 Handle(SelectMgr_ViewerSelector)::DownCast (aSelectors (aSelectorsIdx));
215 if (!aCurSelector->Contains (theObject))
216 continue;
217
218 for (theObject->Init(); theObject->More(); theObject->Next())
219 {
220 aCurSelector->RemoveSelectionOfObject (theObject, theObject->CurrentSelection());
221 theObject->CurrentSelection()->UpdateBVHStatus (SelectMgr_TBU_Remove);
222 }
223 aCurSelector->RemoveSelectableObject (theObject);
224 }
225
226 myLocal.UnBind (theObject);
227 }
228 else if (!theObject->HasOwnPresentations())
229 {
230 for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
231 {
232 Remove (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()));
233 }
234 }
235
236 theObject->ClearSelections();
7fd59977 237}
238
239//==================================================
f751596e 240// Function: Remove
7fd59977 241// Purpose :
242//==================================================
f751596e 243void SelectMgr_SelectionManager::Remove (const Handle(SelectMgr_SelectableObject)& theObject,
244 const Handle(SelectMgr_ViewerSelector)& theSelector)
7fd59977 245{
f751596e 246 if (!theSelector->Contains (theObject))
247 return;
7fd59977 248
f751596e 249 for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
250 {
251 Remove (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theSelector);
252 }
7fd59977 253
f751596e 254 if (!theObject->HasOwnPresentations())
255 return;
7fd59977 256
f751596e 257 for (theObject->Init(); theObject->More(); theObject->Next())
258 {
259 theSelector->RemoveSelectionOfObject (theObject, theObject->CurrentSelection());
260 theObject->CurrentSelection()->UpdateBVHStatus (SelectMgr_TBU_Remove);
7fd59977 261 }
262
f751596e 263 theSelector->RemoveSelectableObject (theObject);
7fd59977 264
f751596e 265 if (myLocal.IsBound (theObject))
266 {
267 SelectMgr_SequenceOfSelector& aSelectors = myLocal.ChangeFind (theObject);
268 for (Standard_Integer aSelectorIdx = 1; aSelectorIdx <= aSelectors.Length(); aSelectorIdx++)
269 {
270 if (aSelectors (aSelectorIdx) == theSelector)
271 {
272 aSelectors.Remove (aSelectorIdx);
7fd59977 273 break;
7fd59977 274 }
f751596e 275 }
7fd59977 276
f751596e 277 if (aSelectors.IsEmpty())
278 {
279 myLocal.UnBind (theObject);
7fd59977 280 }
281 }
282}
283
7fd59977 284//==================================================
285// Function: Activate
286// Purpose :
287//==================================================
f751596e 288void SelectMgr_SelectionManager::Activate (const Handle(SelectMgr_SelectableObject)& theObject,
289 const Standard_Integer theMode,
290 const Handle(SelectMgr_ViewerSelector)& theSelector)
7fd59977 291{
f751596e 292 if (theMode == -1)
293 return;
7fd59977 294
f751596e 295 if (!theSelector.IsNull() && !mySelectors.Contains (theSelector))
296 return;
7fd59977 297
f751596e 298 if (!theObject->HasOwnPresentations())
299 {
300 for (PrsMgr_ListOfPresentableObjectsIter anChildIter (theObject->Children()); anChildIter.More(); anChildIter.Next())
301 {
302 Activate (Handle(SelectMgr_SelectableObject)::DownCast (anChildIter.Value()), theMode, theSelector);
303 }
7fd59977 304
f751596e 305 return;
306 }
7fd59977 307
f751596e 308 Standard_Boolean isComputed = Standard_False;
309 if (theObject->HasSelection (theMode))
310 {
311 isComputed = theObject->Selection (theMode)->IsEmpty() ? 0 : 1;
312 }
313
314 if (!isComputed)
315 loadMode (theObject, theMode);
316
317 if (theSelector.IsNull())
318 {
319 if (myGlobal.Contains (theObject))
7fd59977 320 {
f751596e 321 for (TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
322 {
323 Handle(SelectMgr_ViewerSelector) aCurSelector =
324 Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
325 Activate (theObject, theMode, aCurSelector);
326 }
327 }
328 else if (myLocal.IsBound (theObject))
329 {
330 SelectMgr_SequenceOfSelector& theSelectors = myLocal.ChangeFind (theObject);
331 for (Standard_Integer aSelectorIdx = 1; aSelectorIdx <= theSelectors.Length(); aSelectorIdx++)
332 {
333 Handle(SelectMgr_ViewerSelector) aCurSelector =
334 Handle(SelectMgr_ViewerSelector)::DownCast (theSelectors (aSelectorIdx));
335 Activate (theObject, theMode, aCurSelector);
336 }
7fd59977 337 }
f751596e 338 }
339
340 const Handle(SelectMgr_Selection)& aSelection = theObject->Selection (theMode);
341
342 switch (aSelection->UpdateStatus())
343 {
344 case SelectMgr_TOU_Full:
345 if (theObject->HasSelection (theMode))
346 theSelector->RemoveSelectionOfObject (theObject, aSelection);
347 theObject->RecomputePrimitives (theMode);
348 case SelectMgr_TOU_Partial:
349 if(theObject->HasTransformation())
350 theObject->UpdateTransformations (aSelection);
351 theSelector->RebuildObjectsTree();
352 break;
7fd59977 353 default:
354 break;
7fd59977 355 }
f751596e 356 aSelection->UpdateStatus(SelectMgr_TOU_None);
7fd59977 357
f751596e 358 switch (aSelection->BVHUpdateStatus())
359 {
360 case SelectMgr_TBU_Add:
361 case SelectMgr_TBU_Renew:
362 theSelector->AddSelectionToObject (theObject, aSelection);
363 break;
364 case SelectMgr_TBU_Remove:
365 if (aSelection->GetSelectionState() == SelectMgr_SOS_Deactivated)
366 theSelector->AddSelectionToObject (theObject, aSelection);
367 break;
368 default:
369 break;
370 }
371 aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
7fd59977 372
f751596e 373 if (myGlobal.Contains (theObject))
374 {
375 if (theMode != 0 && theSelector->IsActive (theObject, 0))
376 {
377 theSelector->Deactivate (theObject->Selection (0));
378 }
379 theSelector->Activate (theObject->Selection (theMode));
380 }
381 else
382 {
383 if (myLocal.IsBound (theObject))
384 {
385 if (FindIndex (myLocal.Find (theObject), theSelector) == 0)
386 (myLocal.ChangeFind (theObject)).Append (theSelector);
387 theSelector->Activate (theObject->Selection (theMode));
7fd59977 388 }
389 }
390}
391
392//==================================================
393// Function: Deactivate
394// Purpose :
395//==================================================
f751596e 396void SelectMgr_SelectionManager::Deactivate (const Handle(SelectMgr_SelectableObject)& theObject,
397 const Standard_Integer theMode,
398 const Handle(SelectMgr_ViewerSelector)& theSelector)
7fd59977 399{
7fd59977 400
f751596e 401 if (!theObject->HasOwnPresentations())
402 {
403 for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
404 {
405 Deactivate (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode, theSelector);
7fd59977 406 }
407
f751596e 408 return;
7fd59977 409 }
7fd59977 410
f751596e 411 Standard_Boolean isInGlobal = myGlobal.Contains (theObject);
412 Standard_Boolean hasSelection = theMode == -1 ? Standard_True : theObject->HasSelection (theMode);
7fd59977 413
f751596e 414 if (theSelector.IsNull())
415 {
416 Handle(SelectMgr_ViewerSelector) aSelector;
417 for (TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors); aSelectorIter.More(); aSelectorIter.Next())
418 {
419 aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key());
420 if (isInGlobal || myLocal.IsBound (theObject))
421 {
422 if (theMode == -1)
423 {
424 for (theObject->Init(); theObject->More(); theObject->Next())
425 {
426 aSelector->Deactivate (theObject->CurrentSelection());
427 }
428 }
429 else
430 {
431 if (hasSelection)
432 aSelector->Deactivate (theObject->Selection (theMode));
433 }
434 }
7fd59977 435 }
436 }
f751596e 437 else
7fd59977 438 {
f751596e 439 if (theMode == -1)
440 {
441 for (theObject->Init(); theObject->More(); theObject->Next())
442 {
443 theSelector->Deactivate (theObject->CurrentSelection());
444 }
445 }
446 else
447 if (hasSelection)
448 theSelector->Deactivate (theObject->Selection (theMode));
449 }
7fd59977 450}
7fd59977 451
f751596e 452//=======================================================================
453//function : IsActivated
454//purpose :
455//=======================================================================
456Standard_Boolean SelectMgr_SelectionManager::IsActivated (const Handle(SelectMgr_SelectableObject)& theObject,
457 const Standard_Integer theMode,
458 const Handle(SelectMgr_ViewerSelector)& theSelector) const
7fd59977 459{
f751596e 460 if (!theObject->HasOwnPresentations())
7fd59977 461 {
f751596e 462 for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
463 {
464 if (IsActivated (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode, theSelector))
465 return Standard_True;
466 }
7fd59977 467
f751596e 468 return Standard_False;
469 }
7fd59977 470
f751596e 471 if (!(myGlobal.Contains (theObject) || myLocal.IsBound (theObject)))
472 return Standard_False;
7fd59977 473
f751596e 474 if (theMode == -1 && theSelector.IsNull())
475 {
476 for (theObject->Init(); theObject->More(); theObject->Next())
477 {
478 if (IsActivated (theObject, theObject->CurrentSelection()->Mode()))
479 return Standard_True;
480 }
7fd59977 481
f751596e 482 return Standard_False;
483 }
7fd59977 484
f751596e 485 if (!theObject->HasSelection (theMode))
486 return Standard_False;
7fd59977 487
f751596e 488 const Handle(SelectMgr_Selection)& aSelection = theObject->Selection (theMode);
489 if (theSelector.IsNull())
490 {
491 for (TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors); aSelectorIter.More(); aSelectorIter.Next())
492 {
493 const Handle(SelectMgr_ViewerSelector)& aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key());
494 if (aSelector->Status (aSelection) == SelectMgr_SOS_Activated)
495 return Standard_True;
496 }
7fd59977 497 }
f751596e 498 else
499 {
500 return theSelector->Status (aSelection) == SelectMgr_SOS_Activated;
7fd59977 501 }
502
f751596e 503 return Standard_False;
7fd59977 504}
505
506//=======================================================================
f751596e 507//function : ClearSelectionStructures
508//purpose : Removes sensitive entities from all viewer selectors
509// after method Clear() was called to the selection they belonged to
510// or it was recomputed somehow
7fd59977 511//=======================================================================
f751596e 512void SelectMgr_SelectionManager::ClearSelectionStructures (const Handle(SelectMgr_SelectableObject)& theObj,
513 const Standard_Integer theMode,
514 const Handle(SelectMgr_ViewerSelector)& theSelector)
7fd59977 515{
f751596e 516 for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObj->Children()); anChildrenIter.More(); anChildrenIter.Next())
517 {
518 ClearSelectionStructures (theObj, theMode, theSelector);
7fd59977 519 }
7fd59977 520
f751596e 521 if (!theObj->HasOwnPresentations())
522 return;
7fd59977 523
f751596e 524 if (theSelector == NULL)
525 {
526 if (!(myGlobal.Contains (theObj) || myLocal.IsBound(theObj)))
527 return;
7fd59977 528
f751596e 529 TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors);
530 Handle(SelectMgr_ViewerSelector) aSelector;
531 for( ; aSelectorsIter.More(); aSelectorsIter.Next())
532 {
533 aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
534 ClearSelectionStructures (theObj, theMode, aSelector);
535 }
7fd59977 536 }
f751596e 537 else
538 {
539 if (!(myGlobal.Contains (theObj) || myLocal.IsBound (theObj)))
540 return;
541
542 if (theMode != -1)
543 {
544 if (theObj->HasSelection (theMode))
545 {
546 const Handle(SelectMgr_Selection)& aSelection = theObj->Selection (theMode);
547 if (theObj->HasSelection (theMode))
548 {
549 theSelector->RemoveSelectionOfObject (theObj, aSelection);
550 aSelection->UpdateBVHStatus (SelectMgr_TBU_Add);
551 }
552 }
553 }
554 else
555 {
556 for (theObj->Init(); theObj->More(); theObj->Next())
557 {
558 const Handle(SelectMgr_Selection)& aSelection = theObj->CurrentSelection();
559 theSelector->RemoveSelectionOfObject (theObj, aSelection);
560 aSelection->UpdateBVHStatus (SelectMgr_TBU_Add);
561 }
562 }
563 theSelector->RebuildObjectsTree();
7fd59977 564 }
565}
566
567//=======================================================================
f751596e 568//function : RestoreSelectionStructuress
569//purpose : Re-adds newely calculated sensitive entities of recomputed selection
570// defined by mode theMode to all viewer selectors contained that selection.
7fd59977 571//=======================================================================
f751596e 572void SelectMgr_SelectionManager::RestoreSelectionStructures (const Handle(SelectMgr_SelectableObject)& theObj,
573 const Standard_Integer theMode,
574 const Handle(SelectMgr_ViewerSelector)& theSelector)
7fd59977 575{
f751596e 576 for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObj->Children()); anChildrenIter.More(); anChildrenIter.Next())
577 {
578 RestoreSelectionStructures (theObj, theMode, theSelector);
7fd59977 579 }
7fd59977 580
f751596e 581 if (!theObj->HasOwnPresentations())
582 return;
7fd59977 583
f751596e 584 if (theSelector == NULL)
585 {
586 if (!(myGlobal.Contains (theObj) || myLocal.IsBound(theObj)))
587 return;
7fd59977 588
f751596e 589 TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors);
590 Handle(SelectMgr_ViewerSelector) aSelector;
591 for( ; aSelectorsIter.More(); aSelectorsIter.Next())
592 {
593 aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
594 RestoreSelectionStructures (theObj, theMode, aSelector);
595 }
7fd59977 596 }
f751596e 597 else
598 {
599 if (!(myGlobal.Contains (theObj) || myLocal.IsBound (theObj)))
600 return;
7fd59977 601
f751596e 602 if (theMode != -1)
603 {
604 if (theObj->HasSelection (theMode))
605 {
606 const Handle(SelectMgr_Selection)& aSelection = theObj->Selection (theMode);
607 if (theObj->HasSelection (theMode))
608 {
609 theSelector->AddSelectionToObject (theObj, aSelection);
610 aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
611 }
612 }
613 }
614 else
615 {
616 for (theObj->Init(); theObj->More(); theObj->Next())
617 {
618 const Handle(SelectMgr_Selection)& aSelection = theObj->CurrentSelection();
619 theSelector->AddSelectionToObject (theObj, aSelection);
620 aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
621 }
622 }
623 theSelector->RebuildObjectsTree();
7fd59977 624 }
7fd59977 625}
626
627//=======================================================================
f751596e 628//function : rebuildSelectionStructures
629//purpose : Internal function that marks 1st level BVH of object theObj
630// as outdated
7fd59977 631//=======================================================================
f751596e 632void SelectMgr_SelectionManager::rebuildSelectionStructures (const Handle(SelectMgr_ViewerSelector)& theSelector)
7fd59977 633{
f751596e 634 if (theSelector == NULL)
635 {
636 Handle(SelectMgr_ViewerSelector) aSelector;
637 for(TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
638 {
639 aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
640 rebuildSelectionStructures (aSelector);
641 }
642 }
643 else
644 {
645 theSelector->RebuildObjectsTree();
646 }
7fd59977 647}
648
649//==================================================
650// Function: Update
651// Purpose :
652//==================================================
f751596e 653void SelectMgr_SelectionManager::RecomputeSelection (const Handle(SelectMgr_SelectableObject)& theObject,
654 const Standard_Boolean theIsForce,
655 const Standard_Integer theMode)
7fd59977 656{
f751596e 657 if (theIsForce)
658 {
659 if (theMode == -1)
660 {
661 ClearSelectionStructures (theObject);
662 theObject->RecomputePrimitives();
663 theObject->UpdateTransformation();
664 RestoreSelectionStructures (theObject);
7fd59977 665 }
f751596e 666 else if (theObject->HasSelection (theMode))
667 {
668 ClearSelectionStructures (theObject, theMode);
669 theObject->RecomputePrimitives (theMode);
670 theObject->UpdateTransformation();
671 RestoreSelectionStructures (theObject, theMode);
7fd59977 672 }
673 return;
674 }
f751596e 675
676 for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
677 {
678 RecomputeSelection (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theIsForce, theMode);
679 }
680
681 if (!theObject->HasOwnPresentations())
682 return;
683
684 if (!(myGlobal.Contains (theObject) || myLocal.IsBound (theObject)))
685 return;
686
687 for(theObject->Init(); theObject->More(); theObject->Next())
688 {
689 const Handle(SelectMgr_Selection)& aSelection = theObject->CurrentSelection();
690 aSelection->UpdateStatus (SelectMgr_TOU_Full);
691 Standard_Integer aSelMode = aSelection->Mode();
692
693 for (TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors); aSelectorIter.More(); aSelectorIter.Next())
694 {
695 const Handle(SelectMgr_ViewerSelector)& aCurSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key());
696 if (aCurSelector->Status (aSelection) == SelectMgr_SOS_Activated)
697 {
698 switch (aSelection->UpdateStatus())
699 {
700 case SelectMgr_TOU_Full:
701 ClearSelectionStructures (theObject, aSelMode, aCurSelector);
702 theObject->RecomputePrimitives(aSelMode);
703 RestoreSelectionStructures (theObject, aSelMode, aCurSelector);
704 case SelectMgr_TOU_Partial:
705 theObject->UpdateTransformations (aSelection);
706 aCurSelector->RebuildObjectsTree();
707 aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
708 break;
709 default:
710 break;
7fd59977 711 }
f751596e 712 aSelection->UpdateStatus (SelectMgr_TOU_None);
7fd59977 713 }
714 }
715 }
716}
717
718//=======================================================================
719//function : Update
81bba717 720//purpose : Selections are recalculated if they are flagged
721// "TO RECALCULATE" and activated in one of selectors.
722// If ForceUpdate = True, and they are "TO RECALCULATE"
723// This is done without caring for the state of activation.
7fd59977 724//=======================================================================
f751596e 725void SelectMgr_SelectionManager::Update (const Handle(SelectMgr_SelectableObject)& theObject,
726 const Standard_Boolean theIsForce)
7fd59977 727{
f751596e 728 for (PrsMgr_ListOfPresentableObjectsIter aChildIter (theObject->Children()); aChildIter.More(); aChildIter.Next())
0717ddc1 729 {
f751596e 730 Update (Handle(SelectMgr_SelectableObject)::DownCast (aChildIter.Value()), theIsForce);
0717ddc1 731 }
732
f751596e 733 if (!theObject->HasOwnPresentations())
734 return;
7fd59977 735
f751596e 736 for (theObject->Init(); theObject->More(); theObject->Next())
737 {
738 const Handle(SelectMgr_Selection)& aSelection = theObject->CurrentSelection();
739 if (theIsForce)
740 {
741 switch (aSelection->UpdateStatus())
742 {
7fd59977 743 case SelectMgr_TOU_Full:
f751596e 744 ClearSelectionStructures (theObject, aSelection->Mode());
745 theObject->RecomputePrimitives (aSelection->Mode()); // no break on purpose...
746 RestoreSelectionStructures (theObject, aSelection->Mode());
7fd59977 747 case SelectMgr_TOU_Partial:
f751596e 748 theObject->UpdateTransformations (aSelection);
749 rebuildSelectionStructures();
7fd59977 750 break;
7fd59977 751 default:
752 break;
7fd59977 753 }
f751596e 754 aSelection->UpdateStatus (SelectMgr_TOU_None);
755 aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
7fd59977 756 }
757
f751596e 758 for (TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors); aSelectorIter.More(); aSelectorIter.Next())
759 {
760 const Handle(SelectMgr_ViewerSelector)& aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key());
761 Update (theObject, aSelector, Standard_False);
7fd59977 762 }
763 }
764}
765
7fd59977 766//==================================================
767// Function: Update
81bba717 768// Purpose : Attention, it is required to know what is done...
7fd59977 769//==================================================
f751596e 770void SelectMgr_SelectionManager::Update (const Handle(SelectMgr_SelectableObject)& theObject,
771 const Handle(SelectMgr_ViewerSelector)& theSelector,
772 const Standard_Boolean theIsForce)
773{
774 if (!mySelectors.Contains (theSelector))
775 return;
776
777 Standard_Boolean isKnown = myGlobal.Contains (theObject);
778 if (!isKnown)
779 isKnown = (myLocal.IsBound (theObject) && (FindIndex (myLocal.Find (theObject), theSelector) != 0));
780 if (!isKnown)
781 return;
782
783 for (PrsMgr_ListOfPresentableObjectsIter aChildIter (theObject->Children()); aChildIter.More(); aChildIter.Next())
784 {
785 Update (Handle(SelectMgr_SelectableObject)::DownCast (aChildIter.Value()), theSelector, theIsForce);
786 }
787
788 if (!theObject->HasOwnPresentations())
789 return;
790
791 for (theObject->Init(); theObject->More(); theObject->Next())
792 {
793 const Handle(SelectMgr_Selection)& aSelection = theObject->CurrentSelection();
794 if (theIsForce)
795 {
796 switch (aSelection->UpdateStatus())
797 {
7fd59977 798 case SelectMgr_TOU_Full:
f751596e 799 ClearSelectionStructures (theObject, aSelection->Mode());
800 theObject->RecomputePrimitives (aSelection->Mode());
801 RestoreSelectionStructures (theObject, aSelection->Mode());
7fd59977 802 case SelectMgr_TOU_Partial:
f751596e 803 theObject->UpdateTransformations (aSelection);
804 rebuildSelectionStructures();
7fd59977 805 break;
7fd59977 806 default:
807 break;
7fd59977 808 }
f751596e 809 aSelection->UpdateStatus (SelectMgr_TOU_None);
810 aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
7fd59977 811 }
812
f751596e 813 if (theSelector->Status (aSelection) == SelectMgr_SOS_Activated)
814 {
815 switch (aSelection->UpdateStatus())
816 {
7fd59977 817 case SelectMgr_TOU_Full:
f751596e 818 ClearSelectionStructures (theObject, aSelection->Mode(), theSelector);
819 theObject->RecomputePrimitives (aSelection->Mode());
820 RestoreSelectionStructures (theObject, aSelection->Mode(), theSelector);
7fd59977 821 case SelectMgr_TOU_Partial:
f751596e 822 if (theObject->HasTransformation())
823 {
824 theObject->UpdateTransformations (aSelection);
825 theSelector->RebuildObjectsTree();
826 }
7fd59977 827 break;
7fd59977 828 default:
829 break;
7fd59977 830 }
f751596e 831
832 aSelection->UpdateStatus(SelectMgr_TOU_None);
833 aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
7fd59977 834 }
835 }
836}
837
838//==================================================
f751596e 839// Function: loadMode
840// Purpose : Private Method
7fd59977 841//==================================================
f751596e 842void SelectMgr_SelectionManager::loadMode (const Handle(SelectMgr_SelectableObject)& theObject,
843 const Standard_Integer theMode,
844 const Handle(SelectMgr_ViewerSelector)& theSelector)
7fd59977 845{
f751596e 846 if (theMode == -1)
847 return;
7fd59977 848
f751596e 849 if (!theObject->HasSelection (theMode))
850 {
851 Handle(SelectMgr_Selection) aNewSel = new SelectMgr_Selection (theMode);
852 theObject->AddSelection (aNewSel, theMode);
853 if (theSelector == NULL)
854 {
855 if (myGlobal.Contains (theObject))
856 {
857 TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors);
858 for ( ; aSelectorIter.More(); aSelectorIter.Next())
859 {
860 Handle(SelectMgr_ViewerSelector) aSelector =
861 Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key());
862 aSelector->AddSelectionToObject (theObject, aNewSel);
863 aNewSel->UpdateBVHStatus (SelectMgr_TBU_None);
864 }
865 }
866 else if (myLocal.IsBound (theObject))
867 {
868 const SelectMgr_SequenceOfSelector& aSelectors = myLocal (theObject);
869 for (Standard_Integer aSelectorIdx = 1; aSelectorIdx <= aSelectors.Length(); ++aSelectorIdx)
870 {
871 aSelectors (aSelectorIdx)->AddSelectionToObject (theObject, aNewSel);
872 aNewSel->UpdateBVHStatus (SelectMgr_TBU_None);
873 }
874 }
875 }
876 else
877 {
878 theSelector->AddSelectionToObject (theObject, aNewSel);
879 aNewSel->UpdateBVHStatus (SelectMgr_TBU_None);
880 }
7fd59977 881 }
f751596e 882 else if (theObject->Selection (theMode)->IsEmpty())
7fd59977 883 {
f751596e 884 if (theObject->Selection (theMode)->BVHUpdateStatus() == SelectMgr_TBU_Remove)
885 {
886 Handle(SelectMgr_Selection) aNewSel = new SelectMgr_Selection (theMode);
887 theObject->AddSelection (aNewSel, theMode);
888 theObject->Selection (theMode)->UpdateBVHStatus (SelectMgr_TBU_Remove);
889 theObject->Selection (theMode)->SetSelectionState (SelectMgr_SOS_Deactivated);
890 }
7fd59977 891 }
892}
893
7fd59977 894//=======================================================================
895//function : SetUpdateMode
f751596e 896//purpose :
7fd59977 897//=======================================================================
f751596e 898void SelectMgr_SelectionManager::SetUpdateMode (const Handle(SelectMgr_SelectableObject)& theObject,
899 const SelectMgr_TypeOfUpdate theType)
7fd59977 900{
f751596e 901 for (theObject->Init(); theObject->More(); theObject->Next())
902 theObject->CurrentSelection()->UpdateStatus (theType);
7fd59977 903}
904
f751596e 905//=======================================================================
906//function : SetUpdateMode
907//purpose :
908//=======================================================================
909void SelectMgr_SelectionManager::SetUpdateMode (const Handle(SelectMgr_SelectableObject)& theObject,
910 const Standard_Integer theMode,
911 const SelectMgr_TypeOfUpdate theType)
7fd59977 912{
f751596e 913 if (theObject->HasSelection (theMode))
914 theObject->Selection (theMode)->UpdateStatus (theType);
7fd59977 915}
916