1 // Created on: 1995-02-20
2 // Created by: Mister rmi
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <SelectMgr_SelectableObject.hxx>
19 #include <Aspect_TypeOfMarker.hxx>
20 #include <Bnd_Box.hxx>
22 #include <Graphic3d_AspectLine3d.hxx>
23 #include <Graphic3d_AspectMarker3d.hxx>
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>
29 #include <PrsMgr_PresentableObjectPointer.hxx>
30 #include <PrsMgr_PresentationManager3d.hxx>
31 #include <Select3D_SensitiveEntity.hxx>
32 #include <SelectBasics_EntityOwner.hxx>
33 #include <SelectMgr_EntityOwner.hxx>
34 #include <SelectMgr_IndexedMapOfOwner.hxx>
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>
40 #include <TopLoc_Location.hxx>
42 IMPLEMENT_STANDARD_RTTIEXT(SelectMgr_SelectableObject,PrsMgr_PresentableObject)
46 Handle(SelectMgr_Selection) THE_NULL_SELECTION;
47 Handle(SelectMgr_EntityOwner) THE_NULL_ENTITYOWNER;
50 //==================================================
51 // Function: SelectMgr_SelectableObject
53 //==================================================
55 SelectMgr_SelectableObject::SelectMgr_SelectableObject (const PrsMgr_TypeOfPresentation3d aTypeOfPresentation3d)
56 : PrsMgr_PresentableObject (aTypeOfPresentation3d),
57 myAutoHilight (Standard_True),
64 //==================================================
65 // Function: Destructor
66 // Purpose : Clears all selections of the object
67 //==================================================
68 SelectMgr_SelectableObject::~SelectMgr_SelectableObject()
70 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
72 aSelIter.Value()->Clear();
76 //==================================================
77 // Function: RecomputePrimitives
78 // Purpose : IMPORTANT: Do not use this method to update
79 // selection primitives except implementing custom
80 // selection manager! This method does not take
81 // into account necessary BVH updates, but may
82 // invalidate the pointers it refers to.
83 // TO UPDATE SELECTION properly from outside classes,
84 // use method UpdateSelection.
85 //==================================================
86 void SelectMgr_SelectableObject::RecomputePrimitives()
88 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
90 RecomputePrimitives (aSelIter.Value()->Mode());
94 //==================================================
95 // Function: RecomputePrimitives
96 // Purpose : IMPORTANT: Do not use this method to update
97 // selection primitives except implementing custom
98 // selection manager! This method does not take
99 // into account necessary BVH updates, but may
100 // invalidate the pointers it refers to.
101 // TO UPDATE SELECTION properly from outside classes,
102 // use method UpdateSelection.
103 //==================================================
104 void SelectMgr_SelectableObject::RecomputePrimitives (const Standard_Integer theMode)
106 SelectMgr_SelectableObject* aSelParent = dynamic_cast<SelectMgr_SelectableObject* > (Parent());
107 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
109 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
110 if (aSel->Mode() == theMode)
113 ComputeSelection (aSel, theMode);
114 aSel->UpdateStatus (SelectMgr_TOU_Partial);
115 aSel->UpdateBVHStatus (SelectMgr_TBU_Renew);
116 if (theMode == 0 && aSelParent != NULL)
118 if (const Handle(SelectMgr_EntityOwner)& anAsmOwner = aSelParent->GetAssemblyOwner())
120 SetAssemblyOwner (anAsmOwner, theMode);
127 Handle(SelectMgr_Selection) aNewSel = new SelectMgr_Selection (theMode);
128 ComputeSelection (aNewSel, theMode);
130 if (theMode == 0 && aSelParent != NULL)
132 if (const Handle(SelectMgr_EntityOwner)& anAsmOwner = aSelParent->GetAssemblyOwner())
134 SetAssemblyOwner (anAsmOwner, theMode);
138 aNewSel->UpdateStatus (SelectMgr_TOU_Partial);
139 aNewSel->UpdateBVHStatus (SelectMgr_TBU_Add);
141 myselections.Append (aNewSel);
144 //==================================================
145 // Function: ClearSelections
147 //==================================================
148 void SelectMgr_SelectableObject::ClearSelections (const Standard_Boolean theToUpdate)
150 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
152 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
154 aSel->UpdateBVHStatus (SelectMgr_TBU_Remove);
157 aSel->UpdateStatus (SelectMgr_TOU_Full);
163 //==================================================
164 // Function: Selection
166 //==================================================
168 const Handle(SelectMgr_Selection)& SelectMgr_SelectableObject::Selection (const Standard_Integer theMode) const
172 return THE_NULL_SELECTION;
175 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
177 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
178 if (aSel->Mode() == theMode)
183 return THE_NULL_SELECTION;
186 //==================================================
187 // Function: AddSelection
189 //==================================================
191 void SelectMgr_SelectableObject::AddSelection (const Handle(SelectMgr_Selection)& theSel,
192 const Standard_Integer theMode)
194 if(theSel->IsEmpty())
196 ComputeSelection(theSel, theMode);
197 theSel->UpdateStatus(SelectMgr_TOU_Partial);
198 theSel->UpdateBVHStatus (SelectMgr_TBU_Add);
201 Standard_Boolean isReplaced = Standard_False;
202 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
204 if (aSelIter.Value()->Mode() == theMode)
206 isReplaced = Standard_True;
207 myselections.Remove (aSelIter);
212 myselections.Append (theSel);
215 myselections.Last()->UpdateBVHStatus (SelectMgr_TBU_Renew);
220 SelectMgr_SelectableObject* aSelParent = dynamic_cast<SelectMgr_SelectableObject* > (Parent());
221 if (aSelParent != NULL)
223 if (const Handle(SelectMgr_EntityOwner)& anAsmOwner = aSelParent->GetAssemblyOwner())
225 SetAssemblyOwner (anAsmOwner, theMode);
231 //=======================================================================
232 //function : ReSetTransformation
234 //=======================================================================
235 void SelectMgr_SelectableObject::ResetTransformation()
237 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
239 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
240 aSel->UpdateStatus (SelectMgr_TOU_Partial);
241 aSel->UpdateBVHStatus (SelectMgr_TBU_None);
244 PrsMgr_PresentableObject::ResetTransformation();
247 //=======================================================================
248 //function : UpdateTransformation
250 //=======================================================================
251 void SelectMgr_SelectableObject::UpdateTransformation()
253 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
255 aSelIter.Value()->UpdateStatus (SelectMgr_TOU_Partial);
258 PrsMgr_PresentableObject::UpdateTransformation();
261 //=======================================================================
262 //function : UpdateTransformation
264 //=======================================================================
265 void SelectMgr_SelectableObject::UpdateTransformations (const Handle(SelectMgr_Selection)& theSel)
267 const TopLoc_Location aSelfLocation (Transformation());
268 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (theSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
270 if (Handle(Select3D_SensitiveEntity) aSensEntity = Handle(Select3D_SensitiveEntity)::DownCast (aSelEntIter.Value()->BaseSensitive()))
272 const Handle(SelectBasics_EntityOwner)& aEOwner = aSensEntity->OwnerId();
273 if (Handle(SelectMgr_EntityOwner) aMgrEO = Handle(SelectMgr_EntityOwner)::DownCast (aEOwner))
275 aMgrEO->SetLocation (aSelfLocation);
281 //=======================================================================
282 //function : HilightSelected
284 //=======================================================================
285 void SelectMgr_SelectableObject::HilightSelected (const Handle(PrsMgr_PresentationManager3d)&,
286 const SelectMgr_SequenceOfOwner&)
288 throw Standard_NotImplemented("SelectMgr_SelectableObject::HilightSelected");
291 //=======================================================================
292 //function : ClearSelected
294 //=======================================================================
295 void SelectMgr_SelectableObject::ClearSelected()
297 if(!mySelectionPrs.IsNull())
299 mySelectionPrs->Clear();
303 //=======================================================================
304 //function : ClearDynamicHighlight
306 //=======================================================================
307 void SelectMgr_SelectableObject::ClearDynamicHighlight (const Handle(PrsMgr_PresentationManager3d)& theMgr)
309 theMgr->ClearImmediateDraw();
312 //=======================================================================
313 //function : HilightOwnerWithColor
315 //=======================================================================
316 void SelectMgr_SelectableObject::HilightOwnerWithColor (const Handle(PrsMgr_PresentationManager3d)&,
317 const Handle(Prs3d_Drawer)&,
318 const Handle(SelectMgr_EntityOwner)&)
320 throw Standard_NotImplemented("SelectMgr_SelectableObject::HilightOwnerWithColor");
323 //=======================================================================
324 //function : IsAutoHilight
326 //=======================================================================
327 Standard_Boolean SelectMgr_SelectableObject::IsAutoHilight() const
329 return myAutoHilight;
332 //=======================================================================
333 //function : SetAutoHilight
335 //=======================================================================
336 void SelectMgr_SelectableObject::SetAutoHilight ( const Standard_Boolean newAutoHilight )
338 myAutoHilight = newAutoHilight;
341 //=======================================================================
342 //function : GetHilightPresentation
344 //=======================================================================
345 Handle(Prs3d_Presentation) SelectMgr_SelectableObject::GetHilightPresentation (const Handle(PrsMgr_PresentationManager3d)& theMgr)
347 if (myHilightPrs.IsNull() && !theMgr.IsNull())
349 myHilightPrs = new Prs3d_Presentation (theMgr->StructureManager());
350 myHilightPrs->SetTransformPersistence (TransformPersistence());
357 //=======================================================================
358 //function : GetSelectPresentation
360 //=======================================================================
361 Handle(Prs3d_Presentation) SelectMgr_SelectableObject::GetSelectPresentation (const Handle(PrsMgr_PresentationManager3d)& theMgr)
363 if (mySelectionPrs.IsNull() && !theMgr.IsNull())
365 mySelectionPrs = new Prs3d_Presentation (theMgr->StructureManager());
366 mySelectionPrs->SetTransformPersistence (TransformPersistence());
369 return mySelectionPrs;
372 //=======================================================================
373 //function : ErasePresentations
375 //=======================================================================
376 void SelectMgr_SelectableObject::ErasePresentations (Standard_Boolean theToRemove)
378 if (!mySelectionPrs.IsNull())
380 mySelectionPrs->Erase();
383 mySelectionPrs->Clear();
384 mySelectionPrs.Nullify();
387 if (!myHilightPrs.IsNull())
389 myHilightPrs->Erase();
392 myHilightPrs->Clear();
393 myHilightPrs.Nullify();
398 //=======================================================================
399 //function : SetZLayer
401 //=======================================================================
402 void SelectMgr_SelectableObject::SetZLayer (const Graphic3d_ZLayerId theLayerId)
404 // update own presentations
405 PrsMgr_PresentableObject::SetZLayer (theLayerId);
407 // update selection presentations
408 if (!mySelectionPrs.IsNull())
409 mySelectionPrs->SetZLayer (theLayerId);
411 if (!myHilightPrs.IsNull())
412 myHilightPrs->SetZLayer (theLayerId);
414 // update all entity owner presentations
415 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
417 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
418 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
420 if (Handle(Select3D_SensitiveEntity) aEntity = Handle(Select3D_SensitiveEntity)::DownCast (aSelEntIter.Value()->BaseSensitive()))
422 if (Handle(SelectMgr_EntityOwner) aOwner = Handle(SelectMgr_EntityOwner)::DownCast (aEntity->OwnerId()))
424 aOwner->SetZLayer (theLayerId);
431 //=======================================================================
432 //function : updateSelection
433 //purpose : Sets update status FULL to selections of the object. Must be
434 // used as the only method of UpdateSelection from outer classes
435 // to prevent BVH structures from being outdated.
436 //=======================================================================
437 void SelectMgr_SelectableObject::updateSelection (const Standard_Integer theMode)
441 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
443 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
444 aSel->UpdateStatus (SelectMgr_TOU_Full);
449 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
451 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
452 if (aSel->Mode() == theMode)
454 aSel->UpdateStatus (SelectMgr_TOU_Full);
460 //=======================================================================
461 //function : SetAssemblyOwner
462 //purpose : Sets common entity owner for assembly sensitive object entities
463 //=======================================================================
464 void SelectMgr_SelectableObject::SetAssemblyOwner (const Handle(SelectMgr_EntityOwner)& theOwner,
465 const Standard_Integer theMode)
469 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
471 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
472 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
474 aSelEntIter.Value()->BaseSensitive()->Set (theOwner);
480 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
482 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
483 if (aSel->Mode() == theMode)
485 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
487 aSelEntIter.Value()->BaseSensitive()->Set (theOwner);
494 //=======================================================================
495 //function : BndBoxOfSelected
497 //=======================================================================
498 Bnd_Box SelectMgr_SelectableObject::BndBoxOfSelected (const Handle(SelectMgr_IndexedMapOfOwner)& theOwners)
500 if (theOwners->IsEmpty())
504 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
506 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
507 if (aSel->GetSelectionState() != SelectMgr_SOS_Activated)
510 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
512 const Handle(SelectMgr_EntityOwner) anOwner = Handle(SelectMgr_EntityOwner)::DownCast (aSelEntIter.Value()->BaseSensitive()->OwnerId());
513 if (theOwners->Contains (anOwner))
515 Select3D_BndBox3d aBox = aSelEntIter.Value()->BaseSensitive()->BoundingBox();
516 aBnd.Update (aBox.CornerMin().x(), aBox.CornerMin().y(), aBox.CornerMin().z(),
517 aBox.CornerMax().x(), aBox.CornerMax().y(), aBox.CornerMax().z());
525 //=======================================================================
526 //function : GlobalSelOwner
527 //purpose : Returns entity owner corresponding to selection of the object as a whole
528 //=======================================================================
529 Handle(SelectMgr_EntityOwner) SelectMgr_SelectableObject::GlobalSelOwner() const
531 const Handle(SelectMgr_Selection)& aGlobalSel = Selection (myGlobalSelMode);
532 if (!aGlobalSel.IsNull()
533 && !aGlobalSel->IsEmpty())
535 return Handle(SelectMgr_EntityOwner)::DownCast (aGlobalSel->Entities().First()->BaseSensitive()->OwnerId());
537 return THE_NULL_ENTITYOWNER;
540 //=======================================================================
541 //function : GetAssemblyOwner
543 //=======================================================================
544 const Handle(SelectMgr_EntityOwner)& SelectMgr_SelectableObject::GetAssemblyOwner() const
546 return THE_NULL_ENTITYOWNER;