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