0026719: Visualization - cannot pick zoom persistent object
[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
f751596e 31static Standard_Integer FindIndex (const SelectMgr_SequenceOfSelector& theSelectorsSeq,
32 const Handle(SelectMgr_ViewerSelector)& theSelector)
7fd59977 33{
f751596e 34 Standard_Integer aFoundIdx = 0;
7fd59977 35
f751596e 36 for (Standard_Integer anIdx = 1; anIdx <= theSelectorsSeq.Length() && aFoundIdx==0; anIdx++)
37 {
38 if (theSelector == theSelectorsSeq.Value (anIdx))
39 aFoundIdx = anIdx;
40 }
7fd59977 41
f751596e 42 return aFoundIdx;
43}
7fd59977 44
7fd59977 45//==================================================
46// Function: Create
47// Purpose :
48//==================================================
f751596e 49SelectMgr_SelectionManager::SelectMgr_SelectionManager() {}
7fd59977 50
51//==================================================
52// Function: Add
53// Purpose :
54//==================================================
f751596e 55void SelectMgr_SelectionManager::Add (const Handle(SelectMgr_ViewerSelector)& theSelector)
7fd59977 56{
f751596e 57 mySelectors.Add (theSelector);
7fd59977 58}
59
7fd59977 60//==================================================
61// Function: Remove
62// Purpose :
63//==================================================
f751596e 64void SelectMgr_SelectionManager::Remove (const Handle(SelectMgr_ViewerSelector)& theSelector)
7fd59977 65{
f751596e 66 for (SelectMgr_DataMapIteratorOfDataMapOfObjectSelectors aSelIter (myLocal); aSelIter.More(); aSelIter.Next())
7fd59977 67 {
f751596e 68 SelectMgr_SequenceOfSelector& theSelectors = myLocal.ChangeFind (aSelIter.Key());
69 Standard_Integer aRank = FindIndex (theSelectors, theSelector);
70 if (aRank != 0 && aRank <= theSelectors.Length())
71 theSelectors.Remove (aRank);
7fd59977 72 }
f751596e 73
74 if (mySelectors.Contains (theSelector))
75 mySelectors.Remove (theSelector);
7fd59977 76}
77
78//==================================================
79// Function: Contains
80// Purpose :
81//==================================================
f751596e 82Standard_Boolean SelectMgr_SelectionManager::Contains (const Handle(SelectMgr_ViewerSelector)& theSelector) const
83{
84 return mySelectors.Contains (theSelector);
85}
7fd59977 86
87//==================================================
88// Function: Contains
89// Purpose :
90//==================================================
f751596e 91Standard_Boolean SelectMgr_SelectionManager::Contains (const Handle(SelectMgr_SelectableObject)& theObject) const
92{
93 if (myGlobal.Contains (theObject))
94 return Standard_True;
7fd59977 95
f751596e 96 if (myLocal.IsBound (theObject))
97 return Standard_True;
7fd59977 98
f751596e 99 return Standard_False;
7fd59977 100}
101
7fd59977 102//==================================================
103// Function: Load
104// Purpose :
105//==================================================
f751596e 106void SelectMgr_SelectionManager::Load (const Handle(SelectMgr_SelectableObject)& theObject,
107 const Standard_Integer theMode)
7fd59977 108{
f751596e 109 if (myGlobal.Contains(theObject))
110 return;
7fd59977 111
f751596e 112 for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
113 {
114 Load (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode);
7fd59977 115 }
f751596e 116
117 if (!theObject->HasOwnPresentations())
118 return;
119
120 myGlobal.Add(theObject);
121 for (TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
122 {
123 Handle(SelectMgr_ViewerSelector) aSelector =
124 Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
125 if (!aSelector->Contains (theObject) && theObject->HasOwnPresentations())
126 {
127 aSelector->AddSelectableObject (theObject);
7fd59977 128 }
129 }
f751596e 130 if (theMode != -1)
131 loadMode (theObject, theMode);
7fd59977 132}
133
134
135//==================================================
f751596e 136// Function: Load
7fd59977 137// Purpose :
138//==================================================
f751596e 139void SelectMgr_SelectionManager::Load (const Handle(SelectMgr_SelectableObject)& theObject,
140 const Handle(SelectMgr_ViewerSelector)& theSelector,
141 const Standard_Integer theMode)
7fd59977 142{
f751596e 143 if (!mySelectors.Contains (theSelector))
144 {
145 mySelectors.Add (theSelector);
146 }
7fd59977 147
f751596e 148 if (theMode != -1)
149 loadMode (theObject, theMode, theSelector);
7fd59977 150
f751596e 151 if (theObject->HasOwnPresentations())
152 theSelector->AddSelectableObject (theObject);
153
154 if (myLocal.IsBound (theObject))
155 {
156 SelectMgr_SequenceOfSelector& aSelectors = myLocal.ChangeFind (theObject);
157 if (FindIndex (aSelectors, theSelector) == 0)
158 {
159 aSelectors.Append (theSelector);
7fd59977 160 }
7fd59977 161 }
f751596e 162 else
163 {
164 if (!myGlobal.Contains (theObject))
165 {
166 if (theObject->HasOwnPresentations())
167 {
168 SelectMgr_SequenceOfSelector aSelectors;
169 aSelectors.Append (theSelector);
170 myLocal.Bind (theObject, aSelectors);
171 }
172 else
173 {
174 for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
7fd59977 175 {
f751596e 176 Load (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theSelector, theMode);
7fd59977 177 }
178 }
7fd59977 179 }
7fd59977 180 }
181}
182
f751596e 183
7fd59977 184//==================================================
185// Function: Remove
186// Purpose :
187//==================================================
f751596e 188void SelectMgr_SelectionManager::Remove (const Handle(SelectMgr_SelectableObject)& theObject)
7fd59977 189{
f751596e 190 if (myGlobal.Contains (theObject))
191 {
192 for (TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
193 {
194 Handle(SelectMgr_ViewerSelector) aCurSelector =
195 Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
7fd59977 196
f751596e 197 if (!aCurSelector->Contains (theObject))
198 continue;
7fd59977 199
f751596e 200 for (theObject->Init(); theObject->More(); theObject->Next())
201 {
202 aCurSelector->RemoveSelectionOfObject (theObject, theObject->CurrentSelection());
203 theObject->CurrentSelection()->UpdateBVHStatus (SelectMgr_TBU_Remove);
7fd59977 204 }
f751596e 205 aCurSelector->RemoveSelectableObject (theObject);
7fd59977 206 }
f751596e 207
208 myGlobal.Remove (theObject);
7fd59977 209 }
f751596e 210 else if (myLocal.IsBound (theObject))
211 {
212 SelectMgr_SequenceOfSelector& aSelectors = myLocal.ChangeFind (theObject);
213 for (Standard_Integer aSelectorsIdx = 1; aSelectorsIdx <= aSelectors.Length(); aSelectorsIdx++)
214 {
215 Handle(SelectMgr_ViewerSelector) aCurSelector =
216 Handle(SelectMgr_ViewerSelector)::DownCast (aSelectors (aSelectorsIdx));
217 if (!aCurSelector->Contains (theObject))
218 continue;
219
220 for (theObject->Init(); theObject->More(); theObject->Next())
221 {
222 aCurSelector->RemoveSelectionOfObject (theObject, theObject->CurrentSelection());
223 theObject->CurrentSelection()->UpdateBVHStatus (SelectMgr_TBU_Remove);
224 }
225 aCurSelector->RemoveSelectableObject (theObject);
226 }
227
228 myLocal.UnBind (theObject);
229 }
230 else if (!theObject->HasOwnPresentations())
231 {
232 for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
233 {
234 Remove (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()));
235 }
236 }
237
238 theObject->ClearSelections();
7fd59977 239}
240
241//==================================================
f751596e 242// Function: Remove
7fd59977 243// Purpose :
244//==================================================
f751596e 245void SelectMgr_SelectionManager::Remove (const Handle(SelectMgr_SelectableObject)& theObject,
246 const Handle(SelectMgr_ViewerSelector)& theSelector)
7fd59977 247{
f751596e 248 if (!theSelector->Contains (theObject))
249 return;
7fd59977 250
f751596e 251 for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
252 {
253 Remove (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theSelector);
254 }
7fd59977 255
f751596e 256 if (!theObject->HasOwnPresentations())
257 return;
7fd59977 258
f751596e 259 for (theObject->Init(); theObject->More(); theObject->Next())
260 {
261 theSelector->RemoveSelectionOfObject (theObject, theObject->CurrentSelection());
262 theObject->CurrentSelection()->UpdateBVHStatus (SelectMgr_TBU_Remove);
7fd59977 263 }
264
f751596e 265 theSelector->RemoveSelectableObject (theObject);
7fd59977 266
f751596e 267 if (myLocal.IsBound (theObject))
268 {
269 SelectMgr_SequenceOfSelector& aSelectors = myLocal.ChangeFind (theObject);
270 for (Standard_Integer aSelectorIdx = 1; aSelectorIdx <= aSelectors.Length(); aSelectorIdx++)
271 {
272 if (aSelectors (aSelectorIdx) == theSelector)
273 {
274 aSelectors.Remove (aSelectorIdx);
7fd59977 275 break;
7fd59977 276 }
f751596e 277 }
7fd59977 278
f751596e 279 if (aSelectors.IsEmpty())
280 {
281 myLocal.UnBind (theObject);
7fd59977 282 }
283 }
284}
285
7fd59977 286//==================================================
287// Function: Activate
288// Purpose :
289//==================================================
f751596e 290void SelectMgr_SelectionManager::Activate (const Handle(SelectMgr_SelectableObject)& theObject,
291 const Standard_Integer theMode,
292 const Handle(SelectMgr_ViewerSelector)& theSelector)
7fd59977 293{
f751596e 294 if (theMode == -1)
295 return;
7fd59977 296
f751596e 297 if (!theSelector.IsNull() && !mySelectors.Contains (theSelector))
298 return;
7fd59977 299
f751596e 300 if (!theObject->HasOwnPresentations())
301 {
302 for (PrsMgr_ListOfPresentableObjectsIter anChildIter (theObject->Children()); anChildIter.More(); anChildIter.Next())
303 {
304 Activate (Handle(SelectMgr_SelectableObject)::DownCast (anChildIter.Value()), theMode, theSelector);
305 }
7fd59977 306
f751596e 307 return;
308 }
7fd59977 309
f751596e 310 Standard_Boolean isComputed = Standard_False;
311 if (theObject->HasSelection (theMode))
312 {
313 isComputed = theObject->Selection (theMode)->IsEmpty() ? 0 : 1;
314 }
315
316 if (!isComputed)
317 loadMode (theObject, theMode);
318
319 if (theSelector.IsNull())
320 {
321 if (myGlobal.Contains (theObject))
7fd59977 322 {
f751596e 323 for (TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
324 {
325 Handle(SelectMgr_ViewerSelector) aCurSelector =
326 Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
327 Activate (theObject, theMode, aCurSelector);
328 }
329 }
330 else if (myLocal.IsBound (theObject))
331 {
332 SelectMgr_SequenceOfSelector& theSelectors = myLocal.ChangeFind (theObject);
333 for (Standard_Integer aSelectorIdx = 1; aSelectorIdx <= theSelectors.Length(); aSelectorIdx++)
334 {
335 Handle(SelectMgr_ViewerSelector) aCurSelector =
336 Handle(SelectMgr_ViewerSelector)::DownCast (theSelectors (aSelectorIdx));
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();
f751596e 692 Standard_Integer aSelMode = aSelection->Mode();
693
694 for (TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors); aSelectorIter.More(); aSelectorIter.Next())
695 {
c5f3a425 696 Handle(SelectMgr_ViewerSelector) aCurSelector (Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key()));
f751596e 697 if (aCurSelector->Status (aSelection) == SelectMgr_SOS_Activated)
698 {
f751596e 699 ClearSelectionStructures (theObject, aSelMode, aCurSelector);
700 theObject->RecomputePrimitives(aSelMode);
701 RestoreSelectionStructures (theObject, aSelMode, aCurSelector);
7fd59977 702 }
703 }
e11850fd 704 aSelection->UpdateStatus (SelectMgr_TOU_None);
705 aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
7fd59977 706 }
707}
708
709//=======================================================================
710//function : Update
81bba717 711//purpose : Selections are recalculated if they are flagged
712// "TO RECALCULATE" and activated in one of selectors.
713// If ForceUpdate = True, and they are "TO RECALCULATE"
714// This is done without caring for the state of activation.
7fd59977 715//=======================================================================
f751596e 716void SelectMgr_SelectionManager::Update (const Handle(SelectMgr_SelectableObject)& theObject,
717 const Standard_Boolean theIsForce)
7fd59977 718{
f751596e 719 for (PrsMgr_ListOfPresentableObjectsIter aChildIter (theObject->Children()); aChildIter.More(); aChildIter.Next())
0717ddc1 720 {
f751596e 721 Update (Handle(SelectMgr_SelectableObject)::DownCast (aChildIter.Value()), theIsForce);
0717ddc1 722 }
723
f751596e 724 if (!theObject->HasOwnPresentations())
725 return;
7fd59977 726
f751596e 727 for (theObject->Init(); theObject->More(); theObject->Next())
728 {
729 const Handle(SelectMgr_Selection)& aSelection = theObject->CurrentSelection();
730 if (theIsForce)
731 {
732 switch (aSelection->UpdateStatus())
733 {
7fd59977 734 case SelectMgr_TOU_Full:
f751596e 735 ClearSelectionStructures (theObject, aSelection->Mode());
736 theObject->RecomputePrimitives (aSelection->Mode()); // no break on purpose...
737 RestoreSelectionStructures (theObject, aSelection->Mode());
7fd59977 738 case SelectMgr_TOU_Partial:
f751596e 739 theObject->UpdateTransformations (aSelection);
740 rebuildSelectionStructures();
7fd59977 741 break;
7fd59977 742 default:
743 break;
7fd59977 744 }
f751596e 745 aSelection->UpdateStatus (SelectMgr_TOU_None);
746 aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
7fd59977 747 }
748
f751596e 749 for (TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors); aSelectorIter.More(); aSelectorIter.Next())
750 {
c5f3a425 751 Handle(SelectMgr_ViewerSelector) aSelector (Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key()));
f751596e 752 Update (theObject, aSelector, Standard_False);
7fd59977 753 }
754 }
755}
756
7fd59977 757//==================================================
758// Function: Update
81bba717 759// Purpose : Attention, it is required to know what is done...
7fd59977 760//==================================================
f751596e 761void SelectMgr_SelectionManager::Update (const Handle(SelectMgr_SelectableObject)& theObject,
762 const Handle(SelectMgr_ViewerSelector)& theSelector,
763 const Standard_Boolean theIsForce)
764{
765 if (!mySelectors.Contains (theSelector))
766 return;
767
768 Standard_Boolean isKnown = myGlobal.Contains (theObject);
769 if (!isKnown)
770 isKnown = (myLocal.IsBound (theObject) && (FindIndex (myLocal.Find (theObject), theSelector) != 0));
771 if (!isKnown)
772 return;
773
774 for (PrsMgr_ListOfPresentableObjectsIter aChildIter (theObject->Children()); aChildIter.More(); aChildIter.Next())
775 {
776 Update (Handle(SelectMgr_SelectableObject)::DownCast (aChildIter.Value()), theSelector, theIsForce);
777 }
778
779 if (!theObject->HasOwnPresentations())
780 return;
781
782 for (theObject->Init(); theObject->More(); theObject->Next())
783 {
784 const Handle(SelectMgr_Selection)& aSelection = theObject->CurrentSelection();
785 if (theIsForce)
786 {
787 switch (aSelection->UpdateStatus())
788 {
7fd59977 789 case SelectMgr_TOU_Full:
f751596e 790 ClearSelectionStructures (theObject, aSelection->Mode());
791 theObject->RecomputePrimitives (aSelection->Mode());
792 RestoreSelectionStructures (theObject, aSelection->Mode());
7fd59977 793 case SelectMgr_TOU_Partial:
f751596e 794 theObject->UpdateTransformations (aSelection);
795 rebuildSelectionStructures();
7fd59977 796 break;
7fd59977 797 default:
798 break;
7fd59977 799 }
f751596e 800 aSelection->UpdateStatus (SelectMgr_TOU_None);
801 aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
7fd59977 802 }
803
f751596e 804 if (theSelector->Status (aSelection) == SelectMgr_SOS_Activated)
805 {
806 switch (aSelection->UpdateStatus())
807 {
7fd59977 808 case SelectMgr_TOU_Full:
f751596e 809 ClearSelectionStructures (theObject, aSelection->Mode(), theSelector);
810 theObject->RecomputePrimitives (aSelection->Mode());
811 RestoreSelectionStructures (theObject, aSelection->Mode(), theSelector);
7fd59977 812 case SelectMgr_TOU_Partial:
f751596e 813 if (theObject->HasTransformation())
814 {
815 theObject->UpdateTransformations (aSelection);
816 theSelector->RebuildObjectsTree();
817 }
7fd59977 818 break;
7fd59977 819 default:
820 break;
7fd59977 821 }
f751596e 822
823 aSelection->UpdateStatus(SelectMgr_TOU_None);
824 aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
7fd59977 825 }
826 }
827}
828
829//==================================================
f751596e 830// Function: loadMode
831// Purpose : Private Method
7fd59977 832//==================================================
f751596e 833void SelectMgr_SelectionManager::loadMode (const Handle(SelectMgr_SelectableObject)& theObject,
834 const Standard_Integer theMode,
835 const Handle(SelectMgr_ViewerSelector)& theSelector)
7fd59977 836{
f751596e 837 if (theMode == -1)
838 return;
7fd59977 839
f751596e 840 if (!theObject->HasSelection (theMode))
841 {
842 Handle(SelectMgr_Selection) aNewSel = new SelectMgr_Selection (theMode);
843 theObject->AddSelection (aNewSel, theMode);
c04c30b3 844 if (theSelector.IsNull())
f751596e 845 {
846 if (myGlobal.Contains (theObject))
847 {
848 TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors);
849 for ( ; aSelectorIter.More(); aSelectorIter.Next())
850 {
851 Handle(SelectMgr_ViewerSelector) aSelector =
852 Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key());
853 aSelector->AddSelectionToObject (theObject, aNewSel);
854 aNewSel->UpdateBVHStatus (SelectMgr_TBU_None);
855 }
856 }
857 else if (myLocal.IsBound (theObject))
858 {
859 const SelectMgr_SequenceOfSelector& aSelectors = myLocal (theObject);
860 for (Standard_Integer aSelectorIdx = 1; aSelectorIdx <= aSelectors.Length(); ++aSelectorIdx)
861 {
862 aSelectors (aSelectorIdx)->AddSelectionToObject (theObject, aNewSel);
863 aNewSel->UpdateBVHStatus (SelectMgr_TBU_None);
864 }
865 }
866 }
867 else
868 {
869 theSelector->AddSelectionToObject (theObject, aNewSel);
870 aNewSel->UpdateBVHStatus (SelectMgr_TBU_None);
871 }
7fd59977 872 }
f751596e 873 else if (theObject->Selection (theMode)->IsEmpty())
7fd59977 874 {
f751596e 875 if (theObject->Selection (theMode)->BVHUpdateStatus() == SelectMgr_TBU_Remove)
876 {
877 Handle(SelectMgr_Selection) aNewSel = new SelectMgr_Selection (theMode);
878 theObject->AddSelection (aNewSel, theMode);
879 theObject->Selection (theMode)->UpdateBVHStatus (SelectMgr_TBU_Remove);
880 theObject->Selection (theMode)->SetSelectionState (SelectMgr_SOS_Deactivated);
881 }
7fd59977 882 }
883}
884
7fd59977 885//=======================================================================
886//function : SetUpdateMode
f751596e 887//purpose :
7fd59977 888//=======================================================================
f751596e 889void SelectMgr_SelectionManager::SetUpdateMode (const Handle(SelectMgr_SelectableObject)& theObject,
890 const SelectMgr_TypeOfUpdate theType)
7fd59977 891{
f751596e 892 for (theObject->Init(); theObject->More(); theObject->Next())
893 theObject->CurrentSelection()->UpdateStatus (theType);
7fd59977 894}
895
f751596e 896//=======================================================================
897//function : SetUpdateMode
898//purpose :
899//=======================================================================
900void SelectMgr_SelectionManager::SetUpdateMode (const Handle(SelectMgr_SelectableObject)& theObject,
901 const Standard_Integer theMode,
902 const SelectMgr_TypeOfUpdate theType)
7fd59977 903{
f751596e 904 if (theObject->HasSelection (theMode))
905 theObject->Selection (theMode)->UpdateStatus (theType);
7fd59977 906}
907
8a1170ad 908//=======================================================================
909//function : SetSelectionSensitivity
910//purpose : Allows to manage sensitivity of a particular selection of interactive object theObject and
911// changes previous sensitivity value of all sensitive entities in selection with theMode
912// to the given theNewSensitivity.
913//=======================================================================
914void SelectMgr_SelectionManager::SetSelectionSensitivity (const Handle(SelectMgr_SelectableObject)& theObject,
915 const Standard_Integer theMode,
916 const Standard_Integer theNewSens)
917{
918 Standard_ASSERT_RAISE (theNewSens > 0,
919 "Error! Selection sensitivity have positive value.");
920
921 if (theObject.IsNull() || !theObject->HasSelection (theMode))
922 return;
923
924 Handle(SelectMgr_Selection) aSel = theObject->Selection (theMode);
925 const Standard_Integer aPrevSens = aSel->Sensitivity();
926 aSel->SetSensitivity (theNewSens);
927
928 if (!(myGlobal.Contains (theObject) || myLocal.IsBound (theObject)))
929 return;
930
931 if (myGlobal.Contains (theObject))
932 {
933 for (TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
934 {
935 Handle(SelectMgr_ViewerSelector) aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
936 if (aSelector->Contains (theObject))
937 {
938 aSelector->myTolerances.Decrement (aPrevSens);
939 aSelector->myTolerances.Add (theNewSens);
940 aSelector->myToUpdateTolerance = Standard_True;
941 }
942 }
943 }
944 if (myLocal.IsBound (theObject))
945 {
946 const SelectMgr_SequenceOfSelector& aSelectors = myLocal (theObject);
947 for (SelectMgr_SequenceOfSelector::Iterator aLocalIter (aSelectors); aLocalIter.More(); aLocalIter.Next())
948 {
949 Handle(SelectMgr_ViewerSelector)& aCurSel = aLocalIter.ChangeValue();
950 aCurSel->myTolerances.Decrement (aPrevSens);
951 aCurSel->myTolerances.Add (theNewSens);
952 aCurSel->myToUpdateTolerance = Standard_True;
953 }
954 }
955}