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 <Graphic3d_AspectLine3d.hxx>
20 #include <Graphic3d_AspectMarker3d.hxx>
21 #include <Prs3d_Drawer.hxx>
22 #include <Prs3d_LineAspect.hxx>
23 #include <Prs3d_PlaneAspect.hxx>
24 #include <Prs3d_PointAspect.hxx>
25 #include <Prs3d_Presentation.hxx>
26 #include <PrsMgr_PresentationManager.hxx>
27 #include <Select3D_SensitiveEntity.hxx>
28 #include <SelectMgr_EntityOwner.hxx>
29 #include <SelectMgr_IndexedMapOfOwner.hxx>
30 #include <SelectMgr_Selection.hxx>
31 #include <SelectMgr_SelectionManager.hxx>
32 #include <Standard_NoSuchObject.hxx>
33 #include <Standard_NotImplemented.hxx>
34 #include <TopLoc_Location.hxx>
36 IMPLEMENT_STANDARD_RTTIEXT(SelectMgr_SelectableObject,PrsMgr_PresentableObject)
40 static const Handle(SelectMgr_Selection) THE_NULL_SELECTION;
41 static const Handle(SelectMgr_EntityOwner) THE_NULL_ENTITYOWNER;
44 //==================================================
45 // Function: SelectMgr_SelectableObject
47 //==================================================
49 SelectMgr_SelectableObject::SelectMgr_SelectableObject (const PrsMgr_TypeOfPresentation3d aTypeOfPresentation3d)
50 : PrsMgr_PresentableObject (aTypeOfPresentation3d),
53 myAutoHilight (Standard_True)
58 //==================================================
59 // Function: Destructor
60 // Purpose : Clears all selections of the object
61 //==================================================
62 SelectMgr_SelectableObject::~SelectMgr_SelectableObject()
64 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
66 aSelIter.Value()->Clear();
70 //==================================================
71 // Function: RecomputePrimitives
72 // Purpose : IMPORTANT: Do not use this method to update
73 // selection primitives except implementing custom
74 // selection manager! This method does not take
75 // into account necessary BVH updates, but may
76 // invalidate the pointers it refers to.
77 // TO UPDATE SELECTION properly from outside classes,
78 // use method UpdateSelection.
79 //==================================================
80 void SelectMgr_SelectableObject::RecomputePrimitives()
82 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
84 RecomputePrimitives (aSelIter.Value()->Mode());
88 //==================================================
89 // Function: RecomputePrimitives
90 // Purpose : IMPORTANT: Do not use this method to update
91 // selection primitives except implementing custom
92 // selection manager! This method does not take
93 // into account necessary BVH updates, but may
94 // invalidate the pointers it refers to.
95 // TO UPDATE SELECTION properly from outside classes,
96 // use method UpdateSelection.
97 //==================================================
98 void SelectMgr_SelectableObject::RecomputePrimitives (const Standard_Integer theMode)
100 SelectMgr_SelectableObject* aSelParent = dynamic_cast<SelectMgr_SelectableObject* > (Parent());
101 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
103 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
104 if (aSel->Mode() == theMode)
107 ComputeSelection (aSel, theMode);
108 aSel->UpdateStatus (SelectMgr_TOU_Partial);
109 aSel->UpdateBVHStatus (SelectMgr_TBU_Renew);
110 if (theMode == 0 && aSelParent != NULL)
112 if (const Handle(SelectMgr_EntityOwner)& anAsmOwner = aSelParent->GetAssemblyOwner())
114 SetAssemblyOwner (anAsmOwner, theMode);
121 Handle(SelectMgr_Selection) aNewSel = new SelectMgr_Selection (theMode);
122 ComputeSelection (aNewSel, theMode);
124 if (theMode == 0 && aSelParent != NULL)
126 if (const Handle(SelectMgr_EntityOwner)& anAsmOwner = aSelParent->GetAssemblyOwner())
128 SetAssemblyOwner (anAsmOwner, theMode);
132 aNewSel->UpdateStatus (SelectMgr_TOU_Partial);
133 aNewSel->UpdateBVHStatus (SelectMgr_TBU_Add);
135 myselections.Append (aNewSel);
138 //==================================================
139 // Function: ClearSelections
141 //==================================================
142 void SelectMgr_SelectableObject::ClearSelections (const Standard_Boolean theToUpdate)
144 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
146 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
148 aSel->UpdateBVHStatus (SelectMgr_TBU_Remove);
151 aSel->UpdateStatus (SelectMgr_TOU_Full);
157 //==================================================
158 // Function: Selection
160 //==================================================
162 const Handle(SelectMgr_Selection)& SelectMgr_SelectableObject::Selection (const Standard_Integer theMode) const
166 return THE_NULL_SELECTION;
169 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
171 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
172 if (aSel->Mode() == theMode)
177 return THE_NULL_SELECTION;
180 //==================================================
181 // Function: AddSelection
183 //==================================================
185 void SelectMgr_SelectableObject::AddSelection (const Handle(SelectMgr_Selection)& theSel,
186 const Standard_Integer theMode)
188 if(theSel->IsEmpty())
190 ComputeSelection(theSel, theMode);
191 theSel->UpdateStatus(SelectMgr_TOU_Partial);
192 theSel->UpdateBVHStatus (SelectMgr_TBU_Add);
195 Standard_Boolean isReplaced = Standard_False;
196 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
198 if (aSelIter.Value()->Mode() == theMode)
200 isReplaced = Standard_True;
201 myselections.Remove (aSelIter);
206 myselections.Append (theSel);
209 myselections.Last()->UpdateBVHStatus (SelectMgr_TBU_Renew);
214 SelectMgr_SelectableObject* aSelParent = dynamic_cast<SelectMgr_SelectableObject* > (Parent());
215 if (aSelParent != NULL)
217 if (const Handle(SelectMgr_EntityOwner)& anAsmOwner = aSelParent->GetAssemblyOwner())
219 SetAssemblyOwner (anAsmOwner, theMode);
225 //=======================================================================
226 //function : ReSetTransformation
228 //=======================================================================
229 void SelectMgr_SelectableObject::ResetTransformation()
231 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
233 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
234 aSel->UpdateStatus (SelectMgr_TOU_Partial);
235 aSel->UpdateBVHStatus (SelectMgr_TBU_None);
238 PrsMgr_PresentableObject::ResetTransformation();
241 //=======================================================================
242 //function : UpdateTransformation
244 //=======================================================================
245 void SelectMgr_SelectableObject::UpdateTransformation()
247 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
249 aSelIter.Value()->UpdateStatus (SelectMgr_TOU_Partial);
252 PrsMgr_PresentableObject::UpdateTransformation();
253 if (!mySelectionPrs.IsNull())
255 mySelectionPrs->SetTransformation (TransformationGeom());
257 if (!myHilightPrs.IsNull())
259 myHilightPrs->SetTransformation (TransformationGeom());
263 //=======================================================================
264 //function : UpdateTransformation
266 //=======================================================================
267 void SelectMgr_SelectableObject::UpdateTransformations (const Handle(SelectMgr_Selection)& theSel)
269 const TopLoc_Location aSelfLocation (Transformation());
270 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (theSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
272 if (const Handle(Select3D_SensitiveEntity)& aSensEntity = aSelEntIter.Value()->BaseSensitive())
274 if (const Handle(SelectMgr_EntityOwner)& aEOwner = aSensEntity->OwnerId())
276 aEOwner->SetLocation (aSelfLocation);
282 //=======================================================================
283 //function : HilightSelected
285 //=======================================================================
286 void SelectMgr_SelectableObject::HilightSelected (const Handle(PrsMgr_PresentationManager)&,
287 const SelectMgr_SequenceOfOwner&)
289 throw Standard_NotImplemented("SelectMgr_SelectableObject::HilightSelected");
292 //=======================================================================
293 //function : ClearSelected
295 //=======================================================================
296 void SelectMgr_SelectableObject::ClearSelected()
298 if(!mySelectionPrs.IsNull())
300 mySelectionPrs->Clear();
304 //=======================================================================
305 //function : ClearDynamicHighlight
307 //=======================================================================
308 void SelectMgr_SelectableObject::ClearDynamicHighlight (const Handle(PrsMgr_PresentationManager)& theMgr)
310 theMgr->ClearImmediateDraw();
313 //=======================================================================
314 //function : HilightOwnerWithColor
316 //=======================================================================
317 void SelectMgr_SelectableObject::HilightOwnerWithColor (const Handle(PrsMgr_PresentationManager)&,
318 const Handle(Prs3d_Drawer)&,
319 const Handle(SelectMgr_EntityOwner)&)
321 throw Standard_NotImplemented("SelectMgr_SelectableObject::HilightOwnerWithColor");
324 //=======================================================================
325 //function : GetHilightPresentation
327 //=======================================================================
328 Handle(Prs3d_Presentation) SelectMgr_SelectableObject::GetHilightPresentation (const Handle(PrsMgr_PresentationManager)& theMgr)
330 if (myHilightPrs.IsNull() && !theMgr.IsNull())
332 myHilightPrs = new Prs3d_Presentation (theMgr->StructureManager());
333 myHilightPrs->SetTransformPersistence (TransformPersistence());
334 myHilightPrs->SetClipPlanes (myClipPlanes);
335 myHilightPrs->SetTransformation (TransformationGeom());
341 //=======================================================================
342 //function : GetSelectPresentation
344 //=======================================================================
345 Handle(Prs3d_Presentation) SelectMgr_SelectableObject::GetSelectPresentation (const Handle(PrsMgr_PresentationManager)& theMgr)
347 if (mySelectionPrs.IsNull() && !theMgr.IsNull())
349 mySelectionPrs = new Prs3d_Presentation (theMgr->StructureManager());
350 mySelectionPrs->SetTransformPersistence (TransformPersistence());
351 mySelectionPrs->SetClipPlanes (myClipPlanes);
352 mySelectionPrs->SetTransformation (TransformationGeom());
355 return mySelectionPrs;
358 //=======================================================================
359 //function : ErasePresentations
361 //=======================================================================
362 void SelectMgr_SelectableObject::ErasePresentations (Standard_Boolean theToRemove)
364 if (!mySelectionPrs.IsNull())
366 mySelectionPrs->Erase();
369 mySelectionPrs->Clear();
370 mySelectionPrs.Nullify();
373 if (!myHilightPrs.IsNull())
375 myHilightPrs->Erase();
378 myHilightPrs->Clear();
379 myHilightPrs.Nullify();
384 //=======================================================================
385 //function : SetZLayer
387 //=======================================================================
388 void SelectMgr_SelectableObject::SetZLayer (const Graphic3d_ZLayerId theLayerId)
390 // update own presentations
391 PrsMgr_PresentableObject::SetZLayer (theLayerId);
393 // update selection presentations
394 if (!mySelectionPrs.IsNull())
395 mySelectionPrs->SetZLayer (theLayerId);
397 if (!myHilightPrs.IsNull())
398 myHilightPrs->SetZLayer (theLayerId);
400 // update all entity owner presentations
401 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
403 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
404 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
406 if (const Handle(Select3D_SensitiveEntity)& aEntity = aSelEntIter.Value()->BaseSensitive())
408 if (const Handle(SelectMgr_EntityOwner)& aOwner = aEntity->OwnerId())
410 aOwner->SetZLayer (theLayerId);
417 //=======================================================================
418 //function : UpdateClipping
420 //=======================================================================
421 void SelectMgr_SelectableObject::UpdateClipping()
423 PrsMgr_PresentableObject::UpdateClipping();
424 if (!mySelectionPrs.IsNull())
426 mySelectionPrs->SetClipPlanes (myClipPlanes);
428 if (!myHilightPrs.IsNull())
430 myHilightPrs->SetClipPlanes (myClipPlanes);
434 //=======================================================================
435 //function : updateSelection
436 //purpose : Sets update status FULL to selections of the object. Must be
437 // used as the only method of UpdateSelection from outer classes
438 // to prevent BVH structures from being outdated.
439 //=======================================================================
440 void SelectMgr_SelectableObject::updateSelection (const Standard_Integer theMode)
444 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
446 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
447 aSel->UpdateStatus (SelectMgr_TOU_Full);
452 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
454 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
455 if (aSel->Mode() == theMode)
457 aSel->UpdateStatus (SelectMgr_TOU_Full);
463 //=======================================================================
464 //function : SetAssemblyOwner
465 //purpose : Sets common entity owner for assembly sensitive object entities
466 //=======================================================================
467 void SelectMgr_SelectableObject::SetAssemblyOwner (const Handle(SelectMgr_EntityOwner)& theOwner,
468 const Standard_Integer theMode)
472 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
474 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
475 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
477 aSelEntIter.Value()->BaseSensitive()->Set (theOwner);
483 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
485 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
486 if (aSel->Mode() == theMode)
488 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
490 aSelEntIter.Value()->BaseSensitive()->Set (theOwner);
497 //=======================================================================
498 //function : BndBoxOfSelected
500 //=======================================================================
501 Bnd_Box SelectMgr_SelectableObject::BndBoxOfSelected (const Handle(SelectMgr_IndexedMapOfOwner)& theOwners)
503 if (theOwners->IsEmpty())
507 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
509 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
510 if (aSel->GetSelectionState() != SelectMgr_SOS_Activated)
513 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
515 const Handle(SelectMgr_EntityOwner)& anOwner = aSelEntIter.Value()->BaseSensitive()->OwnerId();
516 if (theOwners->Contains (anOwner))
518 Select3D_BndBox3d aBox = aSelEntIter.Value()->BaseSensitive()->BoundingBox();
519 aBnd.Update (aBox.CornerMin().x(), aBox.CornerMin().y(), aBox.CornerMin().z(),
520 aBox.CornerMax().x(), aBox.CornerMax().y(), aBox.CornerMax().z());
528 //=======================================================================
529 //function : GlobalSelOwner
530 //purpose : Returns entity owner corresponding to selection of the object as a whole
531 //=======================================================================
532 Handle(SelectMgr_EntityOwner) SelectMgr_SelectableObject::GlobalSelOwner() const
534 const Handle(SelectMgr_Selection)& aGlobalSel = Selection (myGlobalSelMode);
535 if (!aGlobalSel.IsNull()
536 && !aGlobalSel->IsEmpty())
538 return aGlobalSel->Entities().First()->BaseSensitive()->OwnerId();
540 return THE_NULL_ENTITYOWNER;
543 //=======================================================================
544 //function : GetAssemblyOwner
546 //=======================================================================
547 const Handle(SelectMgr_EntityOwner)& SelectMgr_SelectableObject::GetAssemblyOwner() const
549 return THE_NULL_ENTITYOWNER;
552 // =======================================================================
553 // function : DumpJson
555 // =======================================================================
556 void SelectMgr_SelectableObject::DumpJson (Standard_OStream& theOStream, const Standard_Integer theDepth) const
558 DUMP_CLASS_BEGIN (theOStream, SelectMgr_SelectableObject);
560 DUMP_FIELD_VALUES_BY_KIND (theOStream, theDepth, PrsMgr_PresentableObject);
561 DUMP_FIELD_VALUE_NUMERICAL (theOStream, myGlobalSelMode);
562 DUMP_FIELD_VALUE_NUMERICAL (theOStream, myAutoHilight);