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