0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / SelectMgr / SelectMgr_SelectionManager.cxx
1 // Created on: 1995-02-13
2 // Created by: Mister rmi
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <SelectMgr_SelectionManager.hxx>
18
19 #include <Select3D_SensitiveGroup.hxx>
20 #include <SelectMgr_SelectableObject.hxx>
21 #include <SelectMgr_Selection.hxx>
22 #include <StdSelect_BRepSelectionTool.hxx>
23 #include <TCollection_AsciiString.hxx>
24
25 IMPLEMENT_STANDARD_RTTIEXT(SelectMgr_SelectionManager,Standard_Transient)
26
27 //==================================================
28 // Function: Create
29 // Purpose :
30 //==================================================
31 SelectMgr_SelectionManager::SelectMgr_SelectionManager (const Handle(SelectMgr_ViewerSelector)& theSelector)
32 : mySelector (theSelector)
33 {
34   //
35 }
36
37 //==================================================
38 // Function: Contains
39 // Purpose :
40 //==================================================
41 Standard_Boolean SelectMgr_SelectionManager::Contains (const Handle(SelectMgr_SelectableObject)& theObject) const
42 {
43   return myGlobal.Contains (theObject);
44 }
45
46 //==================================================
47 // Function: Load
48 // Purpose :
49 //==================================================
50 void SelectMgr_SelectionManager::Load (const Handle(SelectMgr_SelectableObject)& theObject,
51                                        const Standard_Integer theMode)
52 {
53   if (myGlobal.Contains(theObject))
54     return;
55
56   for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
57   {
58     Load (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode);
59   }
60
61   if (!theObject->HasOwnPresentations())
62     return;
63
64   myGlobal.Add(theObject);
65   if (!mySelector->Contains (theObject) && theObject->HasOwnPresentations())
66   {
67     mySelector->AddSelectableObject (theObject);
68   }
69   if (theMode != -1)
70     loadMode (theObject, theMode);
71 }
72
73 //==================================================
74 // Function: Remove
75 // Purpose :
76 //==================================================
77 void SelectMgr_SelectionManager::Remove (const Handle(SelectMgr_SelectableObject)& theObject)
78 {
79   for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
80   {
81     Remove (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()));
82   }
83
84   if (!theObject->HasOwnPresentations())
85     return;
86
87   if (myGlobal.Contains (theObject))
88   {
89     if (mySelector->Contains (theObject))
90     {
91       for (SelectMgr_SequenceOfSelection::Iterator aSelIter (theObject->Selections()); aSelIter.More(); aSelIter.Next())
92       {
93         mySelector->RemoveSelectionOfObject (theObject, aSelIter.Value());
94         aSelIter.Value()->UpdateBVHStatus (SelectMgr_TBU_Remove);
95         mySelector->Deactivate (aSelIter.Value());
96       }
97       mySelector->RemoveSelectableObject (theObject);
98     }
99     myGlobal.Remove (theObject);
100   }
101
102   theObject->ClearSelections();
103 }
104
105 //==================================================
106 // Function: Activate
107 // Purpose :
108 //==================================================
109 void SelectMgr_SelectionManager::Activate (const Handle(SelectMgr_SelectableObject)& theObject,
110                                            const Standard_Integer theMode)
111 {
112   if (theMode == -1)
113     return;
114
115   for (PrsMgr_ListOfPresentableObjectsIter anChildIter (theObject->Children()); anChildIter.More(); anChildIter.Next())
116   {
117     Handle(SelectMgr_SelectableObject) aChild = Handle(SelectMgr_SelectableObject)::DownCast (anChildIter.Value());
118     if (aChild->DisplayStatus() != PrsMgr_DisplayStatus_Erased)
119     {
120       Activate (aChild, theMode);
121     }
122   }
123   if (!theObject->HasOwnPresentations())
124     return;
125
126   Standard_Boolean isComputed = Standard_False;
127   if (const Handle(SelectMgr_Selection)& aSelOld = theObject->Selection (theMode))
128   {
129     isComputed = !aSelOld->IsEmpty();
130   }
131   if (!isComputed)
132   {
133     loadMode (theObject, theMode);
134   }
135
136   const Handle(SelectMgr_Selection)& aSelection = theObject->Selection (theMode);
137   switch (aSelection->UpdateStatus())
138   {
139     case SelectMgr_TOU_Full:
140     {
141       if (theObject->HasSelection (theMode))
142       {
143         mySelector->RemoveSelectionOfObject (theObject, aSelection);
144       }
145       theObject->RecomputePrimitives (theMode);
146       // pass through SelectMgr_TOU_Partial
147     }
148     Standard_FALLTHROUGH
149     case SelectMgr_TOU_Partial:
150     {
151       theObject->UpdateTransformations (aSelection);
152       mySelector->RebuildObjectsTree();
153       break;
154     }
155     default:
156       break;
157   }
158   aSelection->UpdateStatus(SelectMgr_TOU_None);
159
160   switch (aSelection->BVHUpdateStatus())
161   {
162     case SelectMgr_TBU_Add:
163     case SelectMgr_TBU_Renew:
164     {
165       mySelector->AddSelectionToObject (theObject, aSelection);
166       break;
167     }
168     case SelectMgr_TBU_Remove:
169     {
170       if (aSelection->GetSelectionState() == SelectMgr_SOS_Deactivated)
171       {
172         mySelector->AddSelectionToObject (theObject, aSelection);
173       }
174       break;
175     }
176     default:
177       break;
178   }
179   aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
180
181   if (myGlobal.Contains (theObject))
182   {
183     mySelector->Activate (theObject->Selection (theMode));
184   }
185 }
186
187 //==================================================
188 // Function: Deactivate
189 // Purpose :
190 //==================================================
191 void SelectMgr_SelectionManager::Deactivate (const Handle(SelectMgr_SelectableObject)& theObject,
192                                              const Standard_Integer theMode)
193 {
194   for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
195   {
196     Deactivate (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode);
197   }
198   if (!theObject->HasOwnPresentations())
199   {
200     return;
201   }
202   if (!myGlobal.Contains(theObject))
203   {
204     return;
205   }
206
207   const Handle(SelectMgr_Selection)& aSel = theObject->Selection (theMode);
208   if (theMode == -1)
209   {
210     for (SelectMgr_SequenceOfSelection::Iterator aSelIter (theObject->Selections()); aSelIter.More(); aSelIter.Next())
211     {
212       mySelector->Deactivate (aSelIter.Value());
213     }
214   }
215   else if (!aSel.IsNull())
216   {
217     mySelector->Deactivate (aSel);
218   }
219 }
220
221 //=======================================================================
222 //function : IsActivated
223 //purpose  :
224 //=======================================================================
225 Standard_Boolean SelectMgr_SelectionManager::IsActivated (const Handle(SelectMgr_SelectableObject)& theObject,
226                                                           const Standard_Integer theMode) const
227 {
228   for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
229   {
230     if (IsActivated (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode))
231       return Standard_True;
232   }
233   if (!theObject->HasOwnPresentations())
234   {
235     return Standard_False;
236   }
237   if (!myGlobal.Contains(theObject))
238   {
239     return Standard_False;
240   }
241
242   if (theMode == -1)
243   {
244     for (SelectMgr_SequenceOfSelection::Iterator aSelIter (theObject->Selections()); aSelIter.More(); aSelIter.Next())
245     {
246       if (mySelector->Status (aSelIter.Value()) == SelectMgr_SOS_Activated)
247       {
248         return Standard_True;
249       }
250     }
251     return Standard_False;
252   }
253
254   const Handle(SelectMgr_Selection)& aSelection = theObject->Selection (theMode);
255   if (aSelection.IsNull())
256   {
257     return Standard_False;
258   }
259   return !aSelection.IsNull()
260        && mySelector->Status (aSelection) == SelectMgr_SOS_Activated;
261 }
262
263 //=======================================================================
264 //function : ClearSelectionStructures
265 //purpose  : Removes sensitive entities from all viewer selectors
266 //           after method Clear() was called to the selection they belonged to
267 //           or it was recomputed somehow
268 //=======================================================================
269 void SelectMgr_SelectionManager::ClearSelectionStructures (const Handle(SelectMgr_SelectableObject)& theObj,
270                                                            const Standard_Integer theMode)
271 {
272   for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObj->Children()); anChildrenIter.More(); anChildrenIter.Next())
273   {
274     ClearSelectionStructures (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode);
275   }
276
277   if (!theObj->HasOwnPresentations())
278   {
279     return;
280   }
281   if (!myGlobal.Contains(theObj))
282   {
283     return;
284   }
285
286   if (theMode != -1)
287   {
288     if (const Handle(SelectMgr_Selection)& aSelection = theObj->Selection (theMode))
289     {
290       mySelector->RemoveSelectionOfObject (theObj, aSelection);
291       aSelection->UpdateBVHStatus (SelectMgr_TBU_Add);
292     }
293   }
294   else
295   {
296     for (SelectMgr_SequenceOfSelection::Iterator aSelIter (theObj->Selections()); aSelIter.More(); aSelIter.Next())
297     {
298       const Handle(SelectMgr_Selection)& aSelection = aSelIter.Value();
299       mySelector->RemoveSelectionOfObject (theObj, aSelection);
300       aSelection->UpdateBVHStatus (SelectMgr_TBU_Add);
301     }
302   }
303   mySelector->RebuildObjectsTree();
304 }
305
306 //=======================================================================
307 //function : RestoreSelectionStructuress
308 //purpose  : Re-adds newely calculated sensitive  entities of recomputed selection
309 //           defined by mode theMode to all viewer selectors contained that selection.
310 //=======================================================================
311 void SelectMgr_SelectionManager::RestoreSelectionStructures (const Handle(SelectMgr_SelectableObject)& theObj,
312                                                              const Standard_Integer theMode)
313 {
314   for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObj->Children()); anChildrenIter.More(); anChildrenIter.Next())
315   {
316     RestoreSelectionStructures (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode);
317   }
318   if (!theObj->HasOwnPresentations())
319   {
320     return;
321   }
322   if (!myGlobal.Contains(theObj))
323   {
324     return;
325   }
326
327   if (theMode != -1)
328   {
329     if (const Handle(SelectMgr_Selection)& aSelection = theObj->Selection (theMode))
330     {
331       mySelector->AddSelectionToObject (theObj, aSelection);
332       aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
333     }
334   }
335   else
336   {
337     for (SelectMgr_SequenceOfSelection::Iterator aSelIter (theObj->Selections()); aSelIter.More(); aSelIter.Next())
338     {
339       const Handle(SelectMgr_Selection)& aSelection = aSelIter.Value();
340       mySelector->AddSelectionToObject (theObj, aSelection);
341       aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
342     }
343   }
344   mySelector->RebuildObjectsTree();
345 }
346
347 //==================================================
348 // Function: recomputeSelectionMode
349 // Purpose :
350 //==================================================
351 void SelectMgr_SelectionManager::recomputeSelectionMode (const Handle(SelectMgr_SelectableObject)& theObject,
352                                                          const Handle(SelectMgr_Selection)& theSelection,
353                                                          const Standard_Integer theMode)
354 {
355   theSelection->UpdateStatus (SelectMgr_TOU_Full);
356
357   ClearSelectionStructures (theObject, theMode);
358   theObject->RecomputePrimitives (theMode);
359   RestoreSelectionStructures (theObject, theMode);
360   theSelection->UpdateStatus (SelectMgr_TOU_None);
361   theSelection->UpdateBVHStatus (SelectMgr_TBU_None);
362 }
363
364 //==================================================
365 // Function: Update
366 // Purpose :
367 //==================================================
368 void SelectMgr_SelectionManager::RecomputeSelection (const Handle(SelectMgr_SelectableObject)& theObject,
369                                                      const Standard_Boolean theIsForce,
370                                                      const Standard_Integer theMode)
371 {
372   if (theIsForce)
373   {
374     if (theMode == -1)
375     {
376       ClearSelectionStructures (theObject);
377       theObject->RecomputePrimitives();
378       theObject->UpdateTransformation();
379       RestoreSelectionStructures (theObject);
380     }
381     else if (theObject->HasSelection (theMode))
382     {
383       ClearSelectionStructures (theObject, theMode);
384       theObject->RecomputePrimitives (theMode);
385       theObject->UpdateTransformation();
386       RestoreSelectionStructures (theObject, theMode);
387     }
388     return;
389   }
390
391   for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
392   {
393     RecomputeSelection (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theIsForce, theMode);
394   }
395   if (!theObject->HasOwnPresentations())
396   {
397     return;
398   }
399   if (!myGlobal.Contains (theObject))
400   {
401     return;
402   }
403
404   if (theMode == -1)
405   {
406     for (SelectMgr_SequenceOfSelection::Iterator aSelIter (theObject->Selections()); aSelIter.More(); aSelIter.Next())
407     {
408       const Handle(SelectMgr_Selection)& aSelection = aSelIter.Value();
409       const Standard_Integer aSelMode = aSelection->Mode();
410       recomputeSelectionMode (theObject, aSelection, aSelMode);
411     }
412   }
413   else
414   {
415     if (const Handle(SelectMgr_Selection)& aSelection = theObject->Selection (theMode))
416     {
417       recomputeSelectionMode (theObject, aSelection, theMode);
418     }
419   }
420 }
421
422 //=======================================================================
423 //function : Update
424 //purpose  : Selections are recalculated if they are flagged
425 //           "TO RECALCULATE" and activated in one of selectors.
426 //           If ForceUpdate = True, and they are "TO RECALCULATE"
427 //           This is done without caring for the state of activation.
428 //=======================================================================
429 void SelectMgr_SelectionManager::Update (const Handle(SelectMgr_SelectableObject)& theObject,
430                                          const Standard_Boolean theIsForce)
431 {
432   for (PrsMgr_ListOfPresentableObjectsIter aChildIter (theObject->Children()); aChildIter.More(); aChildIter.Next())
433   {
434     Update (Handle(SelectMgr_SelectableObject)::DownCast (aChildIter.Value()), theIsForce);
435   }
436   if (!theObject->HasOwnPresentations())
437   {
438     return;
439   }
440
441   for (SelectMgr_SequenceOfSelection::Iterator aSelIter (theObject->Selections()); aSelIter.More(); aSelIter.Next())
442   {
443     const Handle(SelectMgr_Selection)& aSelection = aSelIter.Value();
444     if (theIsForce || mySelector->Status (aSelection) == SelectMgr_SOS_Activated)
445     {
446       switch (aSelection->UpdateStatus())
447       {
448         case SelectMgr_TOU_Full:
449         {
450           ClearSelectionStructures (theObject, aSelection->Mode());
451           theObject->RecomputePrimitives (aSelection->Mode()); // no break on purpose...
452           RestoreSelectionStructures (theObject, aSelection->Mode());
453           // pass through SelectMgr_TOU_Partial
454         }
455         Standard_FALLTHROUGH
456         case SelectMgr_TOU_Partial:
457         {
458           theObject->UpdateTransformations (aSelection);
459           mySelector->RebuildObjectsTree();
460           break;
461         }
462         default:
463           break;
464       }
465       aSelection->UpdateStatus (SelectMgr_TOU_None);
466       aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
467     }
468   }
469 }
470
471 //==================================================
472 // Function: loadMode
473 // Purpose : Private Method
474 //==================================================
475 void SelectMgr_SelectionManager::loadMode (const Handle(SelectMgr_SelectableObject)& theObject,
476                                            const Standard_Integer theMode)
477 {
478   if (theMode == -1)
479   {
480     return;
481   }
482
483   if (const Handle(SelectMgr_Selection)& aSelOld = theObject->Selection (theMode))
484   {
485     if (aSelOld->IsEmpty())
486     {
487       if (aSelOld->BVHUpdateStatus() == SelectMgr_TBU_Remove)
488       {
489         Handle(SelectMgr_Selection) aNewSel = new SelectMgr_Selection (theMode);
490         theObject->AddSelection (aNewSel, theMode);
491         aNewSel->UpdateBVHStatus (SelectMgr_TBU_Remove);
492         aNewSel->SetSelectionState (SelectMgr_SOS_Deactivated);
493
494         buildBVH (aNewSel);
495       }
496     }
497     return;
498   }
499
500   Handle(SelectMgr_Selection) aNewSel = new SelectMgr_Selection (theMode);
501   theObject->AddSelection (aNewSel, theMode);
502   if (myGlobal.Contains (theObject))
503   {
504     mySelector->AddSelectionToObject (theObject, aNewSel);
505     aNewSel->UpdateBVHStatus (SelectMgr_TBU_None);
506   }
507
508   buildBVH (aNewSel);
509 }
510
511 //==================================================
512 // Function: buildBVH
513 // Purpose : Private Method
514 //==================================================
515 void SelectMgr_SelectionManager::buildBVH (const Handle(SelectMgr_Selection)& theSelection)
516 {
517   if (mySelector->ToPrebuildBVH())
518   {
519     for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIter (theSelection->Entities()); anIter.More(); anIter.Next())
520     {
521       const Handle(Select3D_SensitiveEntity)& anEntity = anIter.Value()->BaseSensitive();
522       mySelector->QueueBVHBuild (anEntity);
523
524       if (Handle(Select3D_SensitiveGroup) aGroup = Handle(Select3D_SensitiveGroup)::DownCast (anEntity))
525       {
526         for (Select3D_IndexedMapOfEntity::Iterator aSubEntitiesIter (aGroup->Entities()); aSubEntitiesIter.More(); aSubEntitiesIter.Next())
527         {
528           const Handle(Select3D_SensitiveEntity)& aSubEntity = aSubEntitiesIter.Value();
529           mySelector->QueueBVHBuild (aSubEntity);
530         }
531       }
532     }
533   }
534   else
535   {
536     StdSelect_BRepSelectionTool::PreBuildBVH (theSelection);
537   }
538 }
539
540 //=======================================================================
541 //function : SetUpdateMode
542 //purpose  :
543 //=======================================================================
544 void SelectMgr_SelectionManager::SetUpdateMode (const Handle(SelectMgr_SelectableObject)& theObject,
545                                                 const SelectMgr_TypeOfUpdate theType)
546 {
547   for (SelectMgr_SequenceOfSelection::Iterator aSelIter (theObject->Selections()); aSelIter.More(); aSelIter.Next())
548   {
549     aSelIter.Value()->UpdateStatus (theType);
550   }
551 }
552
553 //=======================================================================
554 //function : SetUpdateMode
555 //purpose  :
556 //=======================================================================
557 void SelectMgr_SelectionManager::SetUpdateMode (const Handle(SelectMgr_SelectableObject)& theObject,
558                                                 const Standard_Integer theMode,
559                                                 const SelectMgr_TypeOfUpdate theType)
560 {
561   if (const Handle(SelectMgr_Selection)& aSel = theObject->Selection (theMode))
562   {
563     aSel->UpdateStatus (theType);
564   }
565 }
566
567 //=======================================================================
568 //function : SetSelectionSensitivity
569 //purpose  : Allows to manage sensitivity of a particular selection of interactive object theObject and
570 //           changes previous sensitivity value of all sensitive entities in selection with theMode
571 //           to the given theNewSensitivity.
572 //=======================================================================
573 void SelectMgr_SelectionManager::SetSelectionSensitivity (const Handle(SelectMgr_SelectableObject)& theObject,
574                                                           const Standard_Integer theMode,
575                                                           const Standard_Integer theNewSens)
576 {
577   Standard_ASSERT_RAISE (theNewSens >= 0, "Error! Selection sensitivity should not be negative value.");
578   if (theObject.IsNull())
579   {
580     return;
581   }
582
583   const Handle(SelectMgr_Selection)& aSel = theObject->Selection (theMode);
584   if (aSel.IsNull())
585   {
586     return;
587   }
588
589   const Standard_Integer aPrevSens = aSel->Sensitivity();
590   aSel->SetSensitivity (theNewSens);
591   if (myGlobal.Contains (theObject)
592    && mySelector->Contains (theObject))
593   {
594     mySelector->myTolerances.Decrement (aPrevSens);
595     mySelector->myTolerances.Add (theNewSens);
596   }
597 }
598
599 //=======================================================================
600 //function : UpdateSelection
601 //purpose  :
602 //=======================================================================
603 void SelectMgr_SelectionManager::UpdateSelection (const Handle(SelectMgr_SelectableObject)& theObject)
604 {
605   if (myGlobal.Contains (theObject)
606    && mySelector->Contains (theObject))
607   {
608     mySelector->MoveSelectableObject (theObject);
609   }
610 }