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());
351 myHilightPrs->SetClipPlanes (myClipPlanes);
358 //=======================================================================
359 //function : GetSelectPresentation
361 //=======================================================================
362 Handle(Prs3d_Presentation) SelectMgr_SelectableObject::GetSelectPresentation (const Handle(PrsMgr_PresentationManager3d)& theMgr)
364 if (mySelectionPrs.IsNull() && !theMgr.IsNull())
366 mySelectionPrs = new Prs3d_Presentation (theMgr->StructureManager());
367 mySelectionPrs->SetTransformPersistence (TransformPersistence());
368 mySelectionPrs->SetClipPlanes (myClipPlanes);
371 return mySelectionPrs;
374 //=======================================================================
375 //function : ErasePresentations
377 //=======================================================================
378 void SelectMgr_SelectableObject::ErasePresentations (Standard_Boolean theToRemove)
380 if (!mySelectionPrs.IsNull())
382 mySelectionPrs->Erase();
385 mySelectionPrs->Clear();
386 mySelectionPrs.Nullify();
389 if (!myHilightPrs.IsNull())
391 myHilightPrs->Erase();
394 myHilightPrs->Clear();
395 myHilightPrs.Nullify();
400 //=======================================================================
401 //function : SetZLayer
403 //=======================================================================
404 void SelectMgr_SelectableObject::SetZLayer (const Graphic3d_ZLayerId theLayerId)
406 // update own presentations
407 PrsMgr_PresentableObject::SetZLayer (theLayerId);
409 // update selection presentations
410 if (!mySelectionPrs.IsNull())
411 mySelectionPrs->SetZLayer (theLayerId);
413 if (!myHilightPrs.IsNull())
414 myHilightPrs->SetZLayer (theLayerId);
416 // update all entity owner presentations
417 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
419 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
420 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
422 if (Handle(Select3D_SensitiveEntity) aEntity = Handle(Select3D_SensitiveEntity)::DownCast (aSelEntIter.Value()->BaseSensitive()))
424 if (Handle(SelectMgr_EntityOwner) aOwner = Handle(SelectMgr_EntityOwner)::DownCast (aEntity->OwnerId()))
426 aOwner->SetZLayer (theLayerId);
433 //=======================================================================
434 //function : UpdateClipping
436 //=======================================================================
437 void SelectMgr_SelectableObject::UpdateClipping()
439 PrsMgr_PresentableObject::UpdateClipping();
440 if (!mySelectionPrs.IsNull())
442 mySelectionPrs->SetClipPlanes (myClipPlanes);
444 if (!myHilightPrs.IsNull())
446 myHilightPrs->SetClipPlanes (myClipPlanes);
450 //=======================================================================
451 //function : updateSelection
452 //purpose : Sets update status FULL to selections of the object. Must be
453 // used as the only method of UpdateSelection from outer classes
454 // to prevent BVH structures from being outdated.
455 //=======================================================================
456 void SelectMgr_SelectableObject::updateSelection (const Standard_Integer theMode)
460 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
462 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
463 aSel->UpdateStatus (SelectMgr_TOU_Full);
468 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
470 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
471 if (aSel->Mode() == theMode)
473 aSel->UpdateStatus (SelectMgr_TOU_Full);
479 //=======================================================================
480 //function : SetAssemblyOwner
481 //purpose : Sets common entity owner for assembly sensitive object entities
482 //=======================================================================
483 void SelectMgr_SelectableObject::SetAssemblyOwner (const Handle(SelectMgr_EntityOwner)& theOwner,
484 const Standard_Integer theMode)
488 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
490 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
491 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
493 aSelEntIter.Value()->BaseSensitive()->Set (theOwner);
499 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
501 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
502 if (aSel->Mode() == theMode)
504 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
506 aSelEntIter.Value()->BaseSensitive()->Set (theOwner);
513 //=======================================================================
514 //function : BndBoxOfSelected
516 //=======================================================================
517 Bnd_Box SelectMgr_SelectableObject::BndBoxOfSelected (const Handle(SelectMgr_IndexedMapOfOwner)& theOwners)
519 if (theOwners->IsEmpty())
523 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
525 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
526 if (aSel->GetSelectionState() != SelectMgr_SOS_Activated)
529 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
531 const Handle(SelectMgr_EntityOwner) anOwner = Handle(SelectMgr_EntityOwner)::DownCast (aSelEntIter.Value()->BaseSensitive()->OwnerId());
532 if (theOwners->Contains (anOwner))
534 Select3D_BndBox3d aBox = aSelEntIter.Value()->BaseSensitive()->BoundingBox();
535 aBnd.Update (aBox.CornerMin().x(), aBox.CornerMin().y(), aBox.CornerMin().z(),
536 aBox.CornerMax().x(), aBox.CornerMax().y(), aBox.CornerMax().z());
544 //=======================================================================
545 //function : GlobalSelOwner
546 //purpose : Returns entity owner corresponding to selection of the object as a whole
547 //=======================================================================
548 Handle(SelectMgr_EntityOwner) SelectMgr_SelectableObject::GlobalSelOwner() const
550 const Handle(SelectMgr_Selection)& aGlobalSel = Selection (myGlobalSelMode);
551 if (!aGlobalSel.IsNull()
552 && !aGlobalSel->IsEmpty())
554 return Handle(SelectMgr_EntityOwner)::DownCast (aGlobalSel->Entities().First()->BaseSensitive()->OwnerId());
556 return THE_NULL_ENTITYOWNER;
559 //=======================================================================
560 //function : GetAssemblyOwner
562 //=======================================================================
563 const Handle(SelectMgr_EntityOwner)& SelectMgr_SelectableObject::GetAssemblyOwner() const
565 return THE_NULL_ENTITYOWNER;