0028047: Visualization - support objects with customized highlighting in AIS_Interact...
[occt.git] / src / SelectMgr / SelectMgr_SelectableObject.cxx
CommitLineData
b311480e 1// Created on: 1995-02-20
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
f838dac4 17#include <SelectMgr_SelectableObject.hxx>
7fd59977 18
7f917335 19#include <Aspect_TypeOfMarker.hxx>
42cf5bc1 20#include <Bnd_Box.hxx>
21#include <gp_Pnt.hxx>
7f917335 22#include <Graphic3d_AspectLine3d.hxx>
23#include <Graphic3d_AspectMarker3d.hxx>
42cf5bc1 24#include <Prs3d_Drawer.hxx>
25#include <Prs3d_LineAspect.hxx>
26#include <Prs3d_PlaneAspect.hxx>
27#include <Prs3d_PointAspect.hxx>
28#include <Prs3d_Presentation.hxx>
69ff08ff 29#include <PrsMgr_PresentableObjectPointer.hxx>
42cf5bc1 30#include <PrsMgr_PresentationManager3d.hxx>
31#include <Select3D_SensitiveEntity.hxx>
32#include <SelectBasics_EntityOwner.hxx>
33#include <SelectMgr_EntityOwner.hxx>
c04c30b3 34#include <SelectMgr_IndexedMapOfOwner.hxx>
42cf5bc1 35#include <SelectMgr_Selection.hxx>
36#include <SelectMgr_SelectionManager.hxx>
37#include <Standard_NoSuchObject.hxx>
38#include <Standard_NotImplemented.hxx>
39#include <Standard_Type.hxx>
0717ddc1 40#include <TopLoc_Location.hxx>
7fd59977 41
92efcf78 42IMPLEMENT_STANDARD_RTTIEXT(SelectMgr_SelectableObject,PrsMgr_PresentableObject)
43
7fd59977 44static Standard_Integer Search (const SelectMgr_SequenceOfSelection& seq,
45 const Handle (SelectMgr_Selection)& theSel)
46{
47 Standard_Integer ifound=0;
48 for (Standard_Integer i=1;i<=seq.Length()&& ifound==0;i++)
49 {if(theSel == seq.Value(i)) ifound=i;}
50 return ifound;
51}
52
7fd59977 53//==================================================
f838dac4 54// Function: SelectMgr_SelectableObject
7fd59977 55// Purpose :
56//==================================================
57
c3282ec1 58SelectMgr_SelectableObject::SelectMgr_SelectableObject (const PrsMgr_TypeOfPresentation3d aTypeOfPresentation3d)
59: PrsMgr_PresentableObject (aTypeOfPresentation3d),
f751596e 60 myAssemblyOwner (NULL),
c3282ec1 61 myAutoHilight (Standard_True),
62 myGlobalSelMode (0)
7f917335 63{
f838dac4 64 //
7f917335 65}
7fd59977 66
4c0d97ac 67//==================================================
68// Function: Destructor
69// Purpose : Clears all selections of the object
70//==================================================
71SelectMgr_SelectableObject::~SelectMgr_SelectableObject()
72{
73 for (Standard_Integer aSelIdx = 1; aSelIdx <= myselections.Length(); ++aSelIdx)
74 {
75 myselections.Value (aSelIdx)->Clear();
76 }
77}
7fd59977 78
79//==================================================
7411850a 80// Function: HasSelection
7fd59977 81// Purpose :
82//==================================================
7411850a 83Standard_Boolean SelectMgr_SelectableObject::HasSelection (const Standard_Integer theMode) const
7fd59977 84{
7411850a 85 for (Standard_Integer aSelIdx = 1; aSelIdx <= myselections.Length(); ++aSelIdx)
86 {
87 if (((myselections.Value (aSelIdx))->Mode()) == theMode)
88 return Standard_True;
89 }
7fd59977 90 return Standard_False;
91}
92
93//==================================================
f751596e 94// Function: RecomputePrimitives
95// Purpose : IMPORTANT: Do not use this method to update
96// selection primitives except implementing custom
97// selection manager! This method does not take
98// into account necessary BVH updates, but may
99// invalidate the pointers it refers to.
100// TO UPDATE SELECTION properly from outside classes,
101// use method UpdateSelection.
7fd59977 102//==================================================
f751596e 103void SelectMgr_SelectableObject::RecomputePrimitives()
7fd59977 104{
f751596e 105 for (Standard_Integer aSelIdx = 1; aSelIdx <= myselections.Length(); aSelIdx++)
7fd59977 106 {
f751596e 107 RecomputePrimitives (myselections.ChangeValue (aSelIdx)->Mode());
7fd59977 108 }
109}
110
7fd59977 111//==================================================
f751596e 112// Function: RecomputePrimitives
113// Purpose : IMPORTANT: Do not use this method to update
114// selection primitives except implementing custom
115// selection manager! This method does not take
116// into account necessary BVH updates, but may
117// invalidate the pointers it refers to.
118// TO UPDATE SELECTION properly from outside classes,
119// use method UpdateSelection.
7fd59977 120//==================================================
f751596e 121void SelectMgr_SelectableObject::RecomputePrimitives (const Standard_Integer theMode)
7fd59977 122{
69ff08ff 123 Handle(PrsMgr_PresentableObject) aPrsParent (Parent());
124 Handle(SelectMgr_SelectableObject) aSelParent = Handle(SelectMgr_SelectableObject)::DownCast (aPrsParent);
125
f751596e 126 for (Standard_Integer aSelIdx =1; aSelIdx <= myselections.Length(); aSelIdx++ )
127 {
128 if (myselections.Value (aSelIdx)->Mode() == theMode)
129 {
130 myselections (aSelIdx)->Clear();
131 ComputeSelection (myselections (aSelIdx), theMode);
132 myselections (aSelIdx)->UpdateStatus (SelectMgr_TOU_Partial);
133 myselections (aSelIdx)->UpdateBVHStatus (SelectMgr_TBU_Renew);
69ff08ff 134 if (theMode == 0 && ! aSelParent.IsNull() && ! aSelParent->GetAssemblyOwner().IsNull())
f751596e 135 {
69ff08ff 136 SetAssemblyOwner (aSelParent->GetAssemblyOwner(), theMode);
f751596e 137 }
7fd59977 138 return;
139 }
140 }
f751596e 141
142 Handle(SelectMgr_Selection) aNewSel = new SelectMgr_Selection (theMode);
143 ComputeSelection (aNewSel, theMode);
144
69ff08ff 145 if (theMode == 0 && ! aSelParent.IsNull() && ! aSelParent->GetAssemblyOwner().IsNull())
f751596e 146 {
69ff08ff 147 SetAssemblyOwner (aSelParent->GetAssemblyOwner(), theMode);
f751596e 148 }
149
150 aNewSel->UpdateStatus (SelectMgr_TOU_Partial);
151 aNewSel->UpdateBVHStatus (SelectMgr_TBU_Add);
152
153 myselections.Append (aNewSel);
7fd59977 154}
155
156//==================================================
157// Function: ClearSelections
158// Purpose :
159//==================================================
160void SelectMgr_SelectableObject::ClearSelections(const Standard_Boolean update)
161{
162 for (Standard_Integer i =1; i<= myselections.Length(); i++ ) {
163 myselections.Value(i)->Clear();
f751596e 164 myselections.Value (i)->UpdateBVHStatus (SelectMgr_TBU_Remove);
7fd59977 165 if(update)
f751596e 166 {
7fd59977 167 myselections.Value(i)->UpdateStatus(SelectMgr_TOU_Full);
f751596e 168 }
7fd59977 169 }
170}
171
172
173//==================================================
174// Function: Selection
175// Purpose :
176//==================================================
177
178const Handle(SelectMgr_Selection)& SelectMgr_SelectableObject
179::Selection(const Standard_Integer aMode) const
180{
7fd59977 181 Standard_Boolean Found = Standard_False;
182 Standard_Integer Rank=0;
183 for (Standard_Integer i=1;i<=myselections.Length() && !Found;i++)
184 {
185 if((myselections.Value(i))->Mode()==aMode){ Found = Standard_True;
186 Rank=i;}}
187 return myselections.Value(Rank);
188}
189
190
191//==================================================
192// Function: AddSelection
193// Purpose :
194//==================================================
195
196void SelectMgr_SelectableObject
197::AddSelection(const Handle(SelectMgr_Selection)& aSel,
198 const Standard_Integer aMode)
199{
f751596e 200 Standard_Boolean isReplaced = Standard_False;
201 if(aSel->IsEmpty())
202 {
203 ComputeSelection(aSel, aMode);
7fd59977 204 aSel->UpdateStatus(SelectMgr_TOU_Partial);
f751596e 205 aSel->UpdateBVHStatus (SelectMgr_TBU_Add);
7fd59977 206 }
f751596e 207 if (HasSelection(aMode))
208 {
209 const Handle(SelectMgr_Selection)& temp= Selection(aMode);
210 Standard_Integer I = Search(myselections,temp);
211 if(I!=0)
7fd59977 212 {
f751596e 213 myselections.Remove(I);
214 isReplaced = Standard_True;
7fd59977 215 }
f751596e 216 }
217
7fd59977 218 myselections.Append(aSel);
f751596e 219 if (isReplaced)
220 {
221 myselections.Last()->UpdateBVHStatus (SelectMgr_TBU_Renew);
222 }
223
69ff08ff 224 if (aMode == 0)
f751596e 225 {
69ff08ff 226 Handle(PrsMgr_PresentableObject) aPrsParent (Parent());
227 Handle(SelectMgr_SelectableObject) aSelParent = Handle(SelectMgr_SelectableObject)::DownCast (aPrsParent);
228 if (! aSelParent.IsNull() && ! aSelParent->GetAssemblyOwner().IsNull())
229 {
230 SetAssemblyOwner (aSelParent->GetAssemblyOwner(), aMode);
231 }
f751596e 232 }
7fd59977 233}
234
235
236
237//=======================================================================
0717ddc1 238//function : ReSetTransformation
7fd59977 239//purpose :
240//=======================================================================
0717ddc1 241void SelectMgr_SelectableObject::ResetTransformation()
7fd59977 242{
f751596e 243 for (Init(); More(); Next())
244 {
245 const Handle(SelectMgr_Selection) & aSel = CurrentSelection();
246 for (aSel->Init(); aSel->More(); aSel->Next())
247 {
248 aSel->UpdateStatus(SelectMgr_TOU_Partial);
249 aSel->UpdateBVHStatus (SelectMgr_TBU_None);
7fd59977 250 }
7fd59977 251 }
252
0717ddc1 253 PrsMgr_PresentableObject::ResetTransformation();
7fd59977 254}
255
7fd59977 256//=======================================================================
0717ddc1 257//function : UpdateTransformation
7fd59977 258//purpose :
259//=======================================================================
825aa485 260void SelectMgr_SelectableObject::UpdateTransformation()
7fd59977 261{
825aa485 262 for (Init(); More(); Next())
263 {
264 CurrentSelection()->UpdateStatus (SelectMgr_TOU_Partial);
7fd59977 265 }
7fd59977 266
825aa485 267 PrsMgr_PresentableObject::UpdateTransformation();
7fd59977 268}
269
7fd59977 270//=======================================================================
0717ddc1 271//function : UpdateTransformation
7fd59977 272//purpose :
273//=======================================================================
913a4c4a 274void SelectMgr_SelectableObject::UpdateTransformations(const Handle(SelectMgr_Selection)& Sel)
7fd59977 275{
0717ddc1 276 TopLoc_Location aSelfLocation (Transformation());
7fd59977 277 Handle(Select3D_SensitiveEntity) SE;
0717ddc1 278 if(aSelfLocation.IsIdentity()) return;
7fd59977 279 for(Sel->Init();Sel->More();Sel->Next()){
f751596e 280 SE = Handle(Select3D_SensitiveEntity)::DownCast (Sel->Sensitive()->BaseSensitive());
7fd59977 281 if(!SE.IsNull()){
29d43f9c
A
282 const Handle(SelectBasics_EntityOwner)& aEOwner = SE->OwnerId();
283 Handle(SelectMgr_EntityOwner) aMgrEO =
284 Handle(SelectMgr_EntityOwner)::DownCast (aEOwner);
285 if (!aMgrEO.IsNull())
0717ddc1 286 aMgrEO->SetLocation (aSelfLocation);
7fd59977 287 }
288 }
289}
290
291//=======================================================================
292//function : HilightSelected
293//purpose :
294//=======================================================================
295void SelectMgr_SelectableObject::HilightSelected
296 ( const Handle(PrsMgr_PresentationManager3d)&,
297 const SelectMgr_SequenceOfOwner&)
298{
299 Standard_NotImplemented::Raise ("SelectMgr_SelectableObject::HilightSelected");
300}
301
302//=======================================================================
303//function : ClearSelected
304//purpose :
305//=======================================================================
306void SelectMgr_SelectableObject::ClearSelected ()
307{
7fd59977 308 if( !mySelectionPrs.IsNull() )
2b442de5 309 mySelectionPrs->Clear();
7fd59977 310}
311
312//=======================================================================
731c9b5b 313//function : ClearDynamicHighlight
314//purpose :
315//=======================================================================
316void SelectMgr_SelectableObject::ClearDynamicHighlight (const Handle(PrsMgr_PresentationManager3d)& theMgr)
317{
318 theMgr->ClearImmediateDraw();
319}
320
321//=======================================================================
7fd59977 322//function : HilightOwnerWithColor
323//purpose :
324//=======================================================================
8e5fb5ea 325void SelectMgr_SelectableObject::HilightOwnerWithColor (const Handle(PrsMgr_PresentationManager3d)&,
f838dac4 326 const Handle(Prs3d_Drawer)&,
8e5fb5ea 327 const Handle(SelectMgr_EntityOwner)&)
7fd59977 328{
329 Standard_NotImplemented::Raise ("SelectMgr_SelectableObject::HilightOwnerWithColor");
330}
331
332//=======================================================================
333//function : MaxFaceNodes
334//purpose :
335//=======================================================================
336Standard_Boolean SelectMgr_SelectableObject::IsAutoHilight () const
337{
338 return myAutoHilight;
339}
340
341//=======================================================================
342//function : MaxFaceNodes
343//purpose :
344//=======================================================================
345void SelectMgr_SelectableObject::SetAutoHilight ( const Standard_Boolean newAutoHilight )
346{
347 myAutoHilight = newAutoHilight;
348}
349
350//=======================================================================
351//function : GetHilightPresentation
352//purpose :
353//=======================================================================
778cd667 354Handle(Prs3d_Presentation) SelectMgr_SelectableObject::GetHilightPresentation (const Handle(PrsMgr_PresentationManager3d)& theMgr)
7fd59977 355{
778cd667 356 if (myHilightPrs.IsNull() && !theMgr.IsNull())
357 {
358 myHilightPrs = new Prs3d_Presentation (theMgr->StructureManager());
359 myHilightPrs->SetTransformPersistence (TransformPersistence());
360 }
7fd59977 361
362 return myHilightPrs;
363}
364
365
366//=======================================================================
367//function : GetSelectPresentation
368//purpose :
369//=======================================================================
778cd667 370Handle(Prs3d_Presentation) SelectMgr_SelectableObject::GetSelectPresentation (const Handle(PrsMgr_PresentationManager3d)& theMgr)
7fd59977 371{
778cd667 372 if (mySelectionPrs.IsNull() && !theMgr.IsNull())
373 {
374 mySelectionPrs = new Prs3d_Presentation (theMgr->StructureManager());
375 mySelectionPrs->SetTransformPersistence (TransformPersistence());
7fd59977 376 }
778cd667 377
7fd59977 378 return mySelectionPrs;
379}
380
59f45b7c 381//=======================================================================
382//function : SetZLayer
383//purpose :
384//=======================================================================
a1954302 385void SelectMgr_SelectableObject::SetZLayer (const Graphic3d_ZLayerId theLayerId)
59f45b7c 386{
59f45b7c 387 // update own presentations
a1954302 388 PrsMgr_PresentableObject::SetZLayer (theLayerId);
59f45b7c 389
390 // update selection presentations
391 if (!mySelectionPrs.IsNull())
392 mySelectionPrs->SetZLayer (theLayerId);
393
394 if (!myHilightPrs.IsNull())
395 myHilightPrs->SetZLayer (theLayerId);
396
397 // update all entity owner presentations
398 for (Init (); More () ;Next ())
399 {
400 const Handle(SelectMgr_Selection)& aSel = CurrentSelection();
401 for (aSel->Init (); aSel->More (); aSel->Next ())
402 {
403 Handle(Select3D_SensitiveEntity) aEntity =
f751596e 404 Handle(Select3D_SensitiveEntity)::DownCast (aSel->Sensitive()->BaseSensitive());
59f45b7c 405 if (!aEntity.IsNull())
406 {
407 Handle(SelectMgr_EntityOwner) aOwner =
408 Handle(SelectMgr_EntityOwner)::DownCast (aEntity->OwnerId());
409 if (!aOwner.IsNull())
a1954302 410 aOwner->SetZLayer (theLayerId);
59f45b7c 411 }
412 }
413 }
414}
6262338c 415
416//=======================================================================
21a2b7cc 417//function : updateSelection
f751596e 418//purpose : Sets update status FULL to selections of the object. Must be
419// used as the only method of UpdateSelection from outer classes
420// to prevent BVH structures from being outdated.
421//=======================================================================
21a2b7cc 422void SelectMgr_SelectableObject::updateSelection (const Standard_Integer theMode)
f751596e 423{
424 if (theMode == -1)
425 {
426 for (Init(); More(); Next())
427 {
428 const Handle(SelectMgr_Selection)& aSel = CurrentSelection();
429 aSel->UpdateStatus (SelectMgr_TOU_Full);
430 }
431
432 return;
433 }
434
435 for (Init(); More(); Next())
436 {
437 if (CurrentSelection()->Mode() == theMode)
438 {
439 CurrentSelection()->UpdateStatus (SelectMgr_TOU_Full);
440 return;
441 }
442 }
443}
444
445//=======================================================================
f751596e 446//function : SetAssemblyOwner
447//purpose : Sets common entity owner for assembly sensitive object entities
448//=======================================================================
449void SelectMgr_SelectableObject::SetAssemblyOwner (const Handle(SelectMgr_EntityOwner)& theOwner,
450 const Standard_Integer theMode)
451{
452 if (theMode == -1)
453 {
454 for (Standard_Integer aModeIter = 1; aModeIter <= myselections.Length(); ++aModeIter)
455 {
456 Handle(SelectMgr_Selection)& aSel = myselections.ChangeValue (aModeIter);
457 for (aSel->Init(); aSel->More(); aSel->Next())
458 {
459 aSel->Sensitive()->BaseSensitive()->Set (theOwner);
460 }
461 }
462
463 return;
464 }
465
466 if (!HasSelection (theMode))
467 return;
468
469 for (Standard_Integer aModeIter = 1; aModeIter <= myselections.Length(); ++aModeIter)
470 {
471 if (myselections.Value (aModeIter)->Mode() == theMode)
472 {
473 Handle(SelectMgr_Selection)& aSel = myselections.ChangeValue (aModeIter);
474 for (aSel->Init(); aSel->More(); aSel->Next())
475 {
476 aSel->Sensitive()->BaseSensitive()->Set (theOwner);
477 }
478 return;
479 }
480 }
481}
482
483//=======================================================================
484//function : GetAssemblyOwner
485//purpose : Returns common entity owner if it is an assembly
486//=======================================================================
487const Handle(SelectMgr_EntityOwner)& SelectMgr_SelectableObject::GetAssemblyOwner() const
488{
489 return myAssemblyOwner;
490}
b586500b 491
492//=======================================================================
493//function : BndBoxOfSelected
02974a19 494//purpose :
b586500b 495//=======================================================================
02974a19 496Bnd_Box SelectMgr_SelectableObject::BndBoxOfSelected (const Handle(SelectMgr_IndexedMapOfOwner)& theOwners)
b586500b 497{
b586500b 498 if (theOwners->IsEmpty())
02974a19 499 return Bnd_Box();
b586500b 500
02974a19 501 Bnd_Box aBnd;
b586500b 502 for (Init(); More(); Next())
503 {
504 const Handle(SelectMgr_Selection)& aSel = CurrentSelection();
505 if (aSel->GetSelectionState() != SelectMgr_SOS_Activated)
506 continue;
507
508 for (aSel->Init(); aSel->More(); aSel->Next())
509 {
510 const Handle(SelectMgr_EntityOwner) anOwner =
511 Handle(SelectMgr_EntityOwner)::DownCast (aSel->Sensitive()->BaseSensitive()->OwnerId());
512 if (theOwners->Contains (anOwner))
513 {
514 Select3D_BndBox3d aBox = aSel->Sensitive()->BaseSensitive()->BoundingBox();
02974a19 515 aBnd.Update (aBox.CornerMin().x(), aBox.CornerMin().y(), aBox.CornerMin().z(),
516 aBox.CornerMax().x(), aBox.CornerMax().y(), aBox.CornerMax().z());
b586500b 517 }
518 }
519 }
520
521 return aBnd;
522}
c3282ec1 523
524//=======================================================================
525//function : GlobalSelOwner
526//purpose : Returns entity owner corresponding to selection of the object as a whole
527//=======================================================================
528Handle(SelectMgr_EntityOwner) SelectMgr_SelectableObject::GlobalSelOwner() const
529{
530 Handle(SelectMgr_EntityOwner) anOwner;
531
532 if (!HasSelection (myGlobalSelMode))
533 return anOwner;
534
535 const Handle(SelectMgr_Selection)& aGlobalSel = Selection (myGlobalSelMode);
536 if (aGlobalSel->IsEmpty())
537 return anOwner;
538
539 aGlobalSel->Init();
540 anOwner =
541 Handle(SelectMgr_EntityOwner)::DownCast (aGlobalSel->Sensitive()->BaseSensitive()->OwnerId());
542
543 return anOwner;
544}