0027104: DownCast() cannot return null for mismatched handle
[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//==================================================
652// Function: Update
653// Purpose :
654//==================================================
f751596e 655void SelectMgr_SelectionManager::RecomputeSelection (const Handle(SelectMgr_SelectableObject)& theObject,
656 const Standard_Boolean theIsForce,
657 const Standard_Integer theMode)
7fd59977 658{
f751596e 659 if (theIsForce)
660 {
661 if (theMode == -1)
662 {
663 ClearSelectionStructures (theObject);
664 theObject->RecomputePrimitives();
665 theObject->UpdateTransformation();
666 RestoreSelectionStructures (theObject);
7fd59977 667 }
f751596e 668 else if (theObject->HasSelection (theMode))
669 {
670 ClearSelectionStructures (theObject, theMode);
671 theObject->RecomputePrimitives (theMode);
672 theObject->UpdateTransformation();
673 RestoreSelectionStructures (theObject, theMode);
7fd59977 674 }
675 return;
676 }
f751596e 677
678 for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
679 {
680 RecomputeSelection (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theIsForce, theMode);
681 }
682
683 if (!theObject->HasOwnPresentations())
684 return;
685
686 if (!(myGlobal.Contains (theObject) || myLocal.IsBound (theObject)))
687 return;
688
689 for(theObject->Init(); theObject->More(); theObject->Next())
690 {
691 const Handle(SelectMgr_Selection)& aSelection = theObject->CurrentSelection();
eece3af2 692 aSelection->UpdateStatus (SelectMgr_TOU_Full);
f751596e 693 Standard_Integer aSelMode = aSelection->Mode();
694
695 for (TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors); aSelectorIter.More(); aSelectorIter.Next())
696 {
c5f3a425 697 Handle(SelectMgr_ViewerSelector) aCurSelector (Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key()));
f751596e 698 if (aCurSelector->Status (aSelection) == SelectMgr_SOS_Activated)
699 {
f751596e 700 ClearSelectionStructures (theObject, aSelMode, aCurSelector);
701 theObject->RecomputePrimitives(aSelMode);
702 RestoreSelectionStructures (theObject, aSelMode, aCurSelector);
eece3af2 703 aSelection->UpdateStatus (SelectMgr_TOU_None);
704 aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
7fd59977 705 }
706 }
707 }
708}
709
710//=======================================================================
711//function : Update
81bba717 712//purpose : Selections are recalculated if they are flagged
713// "TO RECALCULATE" and activated in one of selectors.
714// If ForceUpdate = True, and they are "TO RECALCULATE"
715// This is done without caring for the state of activation.
7fd59977 716//=======================================================================
f751596e 717void SelectMgr_SelectionManager::Update (const Handle(SelectMgr_SelectableObject)& theObject,
718 const Standard_Boolean theIsForce)
7fd59977 719{
f751596e 720 for (PrsMgr_ListOfPresentableObjectsIter aChildIter (theObject->Children()); aChildIter.More(); aChildIter.Next())
0717ddc1 721 {
f751596e 722 Update (Handle(SelectMgr_SelectableObject)::DownCast (aChildIter.Value()), theIsForce);
0717ddc1 723 }
724
f751596e 725 if (!theObject->HasOwnPresentations())
726 return;
7fd59977 727
f751596e 728 for (theObject->Init(); theObject->More(); theObject->Next())
729 {
730 const Handle(SelectMgr_Selection)& aSelection = theObject->CurrentSelection();
731 if (theIsForce)
732 {
733 switch (aSelection->UpdateStatus())
734 {
7fd59977 735 case SelectMgr_TOU_Full:
f751596e 736 ClearSelectionStructures (theObject, aSelection->Mode());
737 theObject->RecomputePrimitives (aSelection->Mode()); // no break on purpose...
738 RestoreSelectionStructures (theObject, aSelection->Mode());
7fd59977 739 case SelectMgr_TOU_Partial:
f751596e 740 theObject->UpdateTransformations (aSelection);
741 rebuildSelectionStructures();
7fd59977 742 break;
7fd59977 743 default:
744 break;
7fd59977 745 }
f751596e 746 aSelection->UpdateStatus (SelectMgr_TOU_None);
747 aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
7fd59977 748 }
749
f751596e 750 for (TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors); aSelectorIter.More(); aSelectorIter.Next())
751 {
c5f3a425 752 Handle(SelectMgr_ViewerSelector) aSelector (Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key()));
f751596e 753 Update (theObject, aSelector, Standard_False);
7fd59977 754 }
755 }
756}
757
7fd59977 758//==================================================
759// Function: Update
81bba717 760// Purpose : Attention, it is required to know what is done...
7fd59977 761//==================================================
f751596e 762void SelectMgr_SelectionManager::Update (const Handle(SelectMgr_SelectableObject)& theObject,
763 const Handle(SelectMgr_ViewerSelector)& theSelector,
764 const Standard_Boolean theIsForce)
765{
766 if (!mySelectors.Contains (theSelector))
767 return;
768
769 Standard_Boolean isKnown = myGlobal.Contains (theObject);
770 if (!isKnown)
771 isKnown = (myLocal.IsBound (theObject) && (FindIndex (myLocal.Find (theObject), theSelector) != 0));
772 if (!isKnown)
773 return;
774
775 for (PrsMgr_ListOfPresentableObjectsIter aChildIter (theObject->Children()); aChildIter.More(); aChildIter.Next())
776 {
777 Update (Handle(SelectMgr_SelectableObject)::DownCast (aChildIter.Value()), theSelector, theIsForce);
778 }
779
780 if (!theObject->HasOwnPresentations())
781 return;
782
783 for (theObject->Init(); theObject->More(); theObject->Next())
784 {
785 const Handle(SelectMgr_Selection)& aSelection = theObject->CurrentSelection();
786 if (theIsForce)
787 {
788 switch (aSelection->UpdateStatus())
789 {
7fd59977 790 case SelectMgr_TOU_Full:
f751596e 791 ClearSelectionStructures (theObject, aSelection->Mode());
792 theObject->RecomputePrimitives (aSelection->Mode());
793 RestoreSelectionStructures (theObject, aSelection->Mode());
7fd59977 794 case SelectMgr_TOU_Partial:
f751596e 795 theObject->UpdateTransformations (aSelection);
796 rebuildSelectionStructures();
7fd59977 797 break;
7fd59977 798 default:
799 break;
7fd59977 800 }
f751596e 801 aSelection->UpdateStatus (SelectMgr_TOU_None);
802 aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
7fd59977 803 }
804
f751596e 805 if (theSelector->Status (aSelection) == SelectMgr_SOS_Activated)
806 {
807 switch (aSelection->UpdateStatus())
808 {
7fd59977 809 case SelectMgr_TOU_Full:
f751596e 810 ClearSelectionStructures (theObject, aSelection->Mode(), theSelector);
811 theObject->RecomputePrimitives (aSelection->Mode());
812 RestoreSelectionStructures (theObject, aSelection->Mode(), theSelector);
7fd59977 813 case SelectMgr_TOU_Partial:
f751596e 814 if (theObject->HasTransformation())
815 {
816 theObject->UpdateTransformations (aSelection);
817 theSelector->RebuildObjectsTree();
818 }
7fd59977 819 break;
7fd59977 820 default:
821 break;
7fd59977 822 }
f751596e 823
824 aSelection->UpdateStatus(SelectMgr_TOU_None);
825 aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
7fd59977 826 }
827 }
828}
829
830//==================================================
f751596e 831// Function: loadMode
832// Purpose : Private Method
7fd59977 833//==================================================
f751596e 834void SelectMgr_SelectionManager::loadMode (const Handle(SelectMgr_SelectableObject)& theObject,
835 const Standard_Integer theMode,
836 const Handle(SelectMgr_ViewerSelector)& theSelector)
7fd59977 837{
f751596e 838 if (theMode == -1)
839 return;
7fd59977 840
f751596e 841 if (!theObject->HasSelection (theMode))
842 {
843 Handle(SelectMgr_Selection) aNewSel = new SelectMgr_Selection (theMode);
844 theObject->AddSelection (aNewSel, theMode);
c04c30b3 845 if (theSelector.IsNull())
f751596e 846 {
847 if (myGlobal.Contains (theObject))
848 {
849 TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors);
850 for ( ; aSelectorIter.More(); aSelectorIter.Next())
851 {
852 Handle(SelectMgr_ViewerSelector) aSelector =
853 Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key());
854 aSelector->AddSelectionToObject (theObject, aNewSel);
855 aNewSel->UpdateBVHStatus (SelectMgr_TBU_None);
856 }
857 }
858 else if (myLocal.IsBound (theObject))
859 {
860 const SelectMgr_SequenceOfSelector& aSelectors = myLocal (theObject);
861 for (Standard_Integer aSelectorIdx = 1; aSelectorIdx <= aSelectors.Length(); ++aSelectorIdx)
862 {
863 aSelectors (aSelectorIdx)->AddSelectionToObject (theObject, aNewSel);
864 aNewSel->UpdateBVHStatus (SelectMgr_TBU_None);
865 }
866 }
867 }
868 else
869 {
870 theSelector->AddSelectionToObject (theObject, aNewSel);
871 aNewSel->UpdateBVHStatus (SelectMgr_TBU_None);
872 }
7fd59977 873 }
f751596e 874 else if (theObject->Selection (theMode)->IsEmpty())
7fd59977 875 {
f751596e 876 if (theObject->Selection (theMode)->BVHUpdateStatus() == SelectMgr_TBU_Remove)
877 {
878 Handle(SelectMgr_Selection) aNewSel = new SelectMgr_Selection (theMode);
879 theObject->AddSelection (aNewSel, theMode);
880 theObject->Selection (theMode)->UpdateBVHStatus (SelectMgr_TBU_Remove);
881 theObject->Selection (theMode)->SetSelectionState (SelectMgr_SOS_Deactivated);
882 }
7fd59977 883 }
884}
885
7fd59977 886//=======================================================================
887//function : SetUpdateMode
f751596e 888//purpose :
7fd59977 889//=======================================================================
f751596e 890void SelectMgr_SelectionManager::SetUpdateMode (const Handle(SelectMgr_SelectableObject)& theObject,
891 const SelectMgr_TypeOfUpdate theType)
7fd59977 892{
f751596e 893 for (theObject->Init(); theObject->More(); theObject->Next())
894 theObject->CurrentSelection()->UpdateStatus (theType);
7fd59977 895}
896
f751596e 897//=======================================================================
898//function : SetUpdateMode
899//purpose :
900//=======================================================================
901void SelectMgr_SelectionManager::SetUpdateMode (const Handle(SelectMgr_SelectableObject)& theObject,
902 const Standard_Integer theMode,
903 const SelectMgr_TypeOfUpdate theType)
7fd59977 904{
f751596e 905 if (theObject->HasSelection (theMode))
906 theObject->Selection (theMode)->UpdateStatus (theType);
7fd59977 907}
908
8a1170ad 909//=======================================================================
910//function : SetSelectionSensitivity
911//purpose : Allows to manage sensitivity of a particular selection of interactive object theObject and
912// changes previous sensitivity value of all sensitive entities in selection with theMode
913// to the given theNewSensitivity.
914//=======================================================================
915void SelectMgr_SelectionManager::SetSelectionSensitivity (const Handle(SelectMgr_SelectableObject)& theObject,
916 const Standard_Integer theMode,
917 const Standard_Integer theNewSens)
918{
919 Standard_ASSERT_RAISE (theNewSens > 0,
920 "Error! Selection sensitivity have positive value.");
921
922 if (theObject.IsNull() || !theObject->HasSelection (theMode))
923 return;
924
925 Handle(SelectMgr_Selection) aSel = theObject->Selection (theMode);
926 const Standard_Integer aPrevSens = aSel->Sensitivity();
927 aSel->SetSensitivity (theNewSens);
928
929 if (!(myGlobal.Contains (theObject) || myLocal.IsBound (theObject)))
930 return;
931
932 if (myGlobal.Contains (theObject))
933 {
934 for (TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
935 {
936 Handle(SelectMgr_ViewerSelector) aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
937 if (aSelector->Contains (theObject))
938 {
939 aSelector->myTolerances.Decrement (aPrevSens);
940 aSelector->myTolerances.Add (theNewSens);
941 aSelector->myToUpdateTolerance = Standard_True;
942 }
943 }
944 }
945 if (myLocal.IsBound (theObject))
946 {
947 const SelectMgr_SequenceOfSelector& aSelectors = myLocal (theObject);
948 for (SelectMgr_SequenceOfSelector::Iterator aLocalIter (aSelectors); aLocalIter.More(); aLocalIter.Next())
949 {
950 Handle(SelectMgr_ViewerSelector)& aCurSel = aLocalIter.ChangeValue();
951 aCurSel->myTolerances.Decrement (aPrevSens);
952 aCurSel->myTolerances.Add (theNewSens);
953 aCurSel->myToUpdateTolerance = Standard_True;
954 }
955 }
956}