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),
52 myAutoHilight (Standard_True)
57 //==================================================
58 // Function: Destructor
59 // Purpose : Clears all selections of the object
60 //==================================================
61 SelectMgr_SelectableObject::~SelectMgr_SelectableObject()
63 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
65 aSelIter.Value()->Clear();
69 //==================================================
70 // Function: RecomputePrimitives
71 // Purpose : IMPORTANT: Do not use this method to update
72 // selection primitives except implementing custom
73 // selection manager! This method does not take
74 // into account necessary BVH updates, but may
75 // invalidate the pointers it refers to.
76 // TO UPDATE SELECTION properly from outside classes,
77 // use method UpdateSelection.
78 //==================================================
79 void SelectMgr_SelectableObject::RecomputePrimitives()
81 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
83 RecomputePrimitives (aSelIter.Value()->Mode());
87 //==================================================
88 // Function: RecomputePrimitives
89 // Purpose : IMPORTANT: Do not use this method to update
90 // selection primitives except implementing custom
91 // selection manager! This method does not take
92 // into account necessary BVH updates, but may
93 // invalidate the pointers it refers to.
94 // TO UPDATE SELECTION properly from outside classes,
95 // use method UpdateSelection.
96 //==================================================
97 void SelectMgr_SelectableObject::RecomputePrimitives (const Standard_Integer theMode)
99 SelectMgr_SelectableObject* aSelParent = dynamic_cast<SelectMgr_SelectableObject* > (Parent());
100 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
102 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
103 if (aSel->Mode() == theMode)
106 ComputeSelection (aSel, theMode);
107 aSel->UpdateStatus (SelectMgr_TOU_Partial);
108 aSel->UpdateBVHStatus (SelectMgr_TBU_Renew);
109 if (theMode == 0 && aSelParent != NULL)
111 if (const Handle(SelectMgr_EntityOwner)& anAsmOwner = aSelParent->GetAssemblyOwner())
113 SetAssemblyOwner (anAsmOwner, theMode);
120 Handle(SelectMgr_Selection) aNewSel = new SelectMgr_Selection (theMode);
121 ComputeSelection (aNewSel, theMode);
123 if (theMode == 0 && aSelParent != NULL)
125 if (const Handle(SelectMgr_EntityOwner)& anAsmOwner = aSelParent->GetAssemblyOwner())
127 SetAssemblyOwner (anAsmOwner, theMode);
131 aNewSel->UpdateStatus (SelectMgr_TOU_Partial);
132 aNewSel->UpdateBVHStatus (SelectMgr_TBU_Add);
134 myselections.Append (aNewSel);
137 //==================================================
138 // Function: ClearSelections
140 //==================================================
141 void SelectMgr_SelectableObject::ClearSelections (const Standard_Boolean theToUpdate)
143 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
145 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
147 aSel->UpdateBVHStatus (SelectMgr_TBU_Remove);
150 aSel->UpdateStatus (SelectMgr_TOU_Full);
156 //==================================================
157 // Function: Selection
159 //==================================================
161 const Handle(SelectMgr_Selection)& SelectMgr_SelectableObject::Selection (const Standard_Integer theMode) const
165 return THE_NULL_SELECTION;
168 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
170 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
171 if (aSel->Mode() == theMode)
176 return THE_NULL_SELECTION;
179 //==================================================
180 // Function: AddSelection
182 //==================================================
184 void SelectMgr_SelectableObject::AddSelection (const Handle(SelectMgr_Selection)& theSel,
185 const Standard_Integer theMode)
187 if(theSel->IsEmpty())
189 ComputeSelection(theSel, theMode);
190 theSel->UpdateStatus(SelectMgr_TOU_Partial);
191 theSel->UpdateBVHStatus (SelectMgr_TBU_Add);
194 Standard_Boolean isReplaced = Standard_False;
195 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
197 if (aSelIter.Value()->Mode() == theMode)
199 isReplaced = Standard_True;
200 myselections.Remove (aSelIter);
205 myselections.Append (theSel);
208 myselections.Last()->UpdateBVHStatus (SelectMgr_TBU_Renew);
213 SelectMgr_SelectableObject* aSelParent = dynamic_cast<SelectMgr_SelectableObject* > (Parent());
214 if (aSelParent != NULL)
216 if (const Handle(SelectMgr_EntityOwner)& anAsmOwner = aSelParent->GetAssemblyOwner())
218 SetAssemblyOwner (anAsmOwner, theMode);
224 //=======================================================================
225 //function : ReSetTransformation
227 //=======================================================================
228 void SelectMgr_SelectableObject::ResetTransformation()
230 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
232 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
233 aSel->UpdateStatus (SelectMgr_TOU_Partial);
234 aSel->UpdateBVHStatus (SelectMgr_TBU_None);
237 PrsMgr_PresentableObject::ResetTransformation();
240 //=======================================================================
241 //function : UpdateTransformation
243 //=======================================================================
244 void SelectMgr_SelectableObject::UpdateTransformation()
246 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
248 aSelIter.Value()->UpdateStatus (SelectMgr_TOU_Partial);
251 PrsMgr_PresentableObject::UpdateTransformation();
252 if (!mySelectionPrs.IsNull())
254 mySelectionPrs->SetTransformation (TransformationGeom());
256 if (!myHilightPrs.IsNull())
258 myHilightPrs->SetTransformation (TransformationGeom());
262 //=======================================================================
263 //function : UpdateTransformation
265 //=======================================================================
266 void SelectMgr_SelectableObject::UpdateTransformations (const Handle(SelectMgr_Selection)& theSel)
268 const TopLoc_Location aSelfLocation (Transformation());
269 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (theSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
271 if (const Handle(Select3D_SensitiveEntity)& aSensEntity = aSelEntIter.Value()->BaseSensitive())
273 if (const Handle(SelectMgr_EntityOwner)& aEOwner = aSensEntity->OwnerId())
275 aEOwner->SetLocation (aSelfLocation);
281 //=======================================================================
282 //function : HilightSelected
284 //=======================================================================
285 void SelectMgr_SelectableObject::HilightSelected (const Handle(PrsMgr_PresentationManager)&,
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_PresentationManager)& theMgr)
309 theMgr->ClearImmediateDraw();
312 //=======================================================================
313 //function : HilightOwnerWithColor
315 //=======================================================================
316 void SelectMgr_SelectableObject::HilightOwnerWithColor (const Handle(PrsMgr_PresentationManager)&,
317 const Handle(Prs3d_Drawer)&,
318 const Handle(SelectMgr_EntityOwner)&)
320 throw Standard_NotImplemented("SelectMgr_SelectableObject::HilightOwnerWithColor");
323 //=======================================================================
324 //function : GetHilightPresentation
326 //=======================================================================
327 Handle(Prs3d_Presentation) SelectMgr_SelectableObject::GetHilightPresentation (const Handle(PrsMgr_PresentationManager)& theMgr)
329 if (myHilightPrs.IsNull() && !theMgr.IsNull())
331 myHilightPrs = new Prs3d_Presentation (theMgr->StructureManager());
332 myHilightPrs->SetTransformPersistence (TransformPersistence());
333 myHilightPrs->SetClipPlanes (myClipPlanes);
334 myHilightPrs->SetTransformation (TransformationGeom());
340 //=======================================================================
341 //function : GetSelectPresentation
343 //=======================================================================
344 Handle(Prs3d_Presentation) SelectMgr_SelectableObject::GetSelectPresentation (const Handle(PrsMgr_PresentationManager)& theMgr)
346 if (mySelectionPrs.IsNull() && !theMgr.IsNull())
348 mySelectionPrs = new Prs3d_Presentation (theMgr->StructureManager());
349 mySelectionPrs->SetTransformPersistence (TransformPersistence());
350 mySelectionPrs->SetClipPlanes (myClipPlanes);
351 mySelectionPrs->SetTransformation (TransformationGeom());
354 return mySelectionPrs;
357 //=======================================================================
358 //function : ErasePresentations
360 //=======================================================================
361 void SelectMgr_SelectableObject::ErasePresentations (Standard_Boolean theToRemove)
363 if (!mySelectionPrs.IsNull())
365 mySelectionPrs->Erase();
368 mySelectionPrs->Clear();
369 mySelectionPrs.Nullify();
372 if (!myHilightPrs.IsNull())
374 myHilightPrs->Erase();
377 myHilightPrs->Clear();
378 myHilightPrs.Nullify();
383 //=======================================================================
384 //function : SetZLayer
386 //=======================================================================
387 void SelectMgr_SelectableObject::SetZLayer (const Graphic3d_ZLayerId theLayerId)
389 // update own presentations
390 PrsMgr_PresentableObject::SetZLayer (theLayerId);
392 // update selection presentations
393 if (!mySelectionPrs.IsNull())
394 mySelectionPrs->SetZLayer (theLayerId);
396 if (!myHilightPrs.IsNull())
397 myHilightPrs->SetZLayer (theLayerId);
399 // update all entity owner presentations
400 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
402 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
403 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
405 if (const Handle(Select3D_SensitiveEntity)& aEntity = aSelEntIter.Value()->BaseSensitive())
407 if (const Handle(SelectMgr_EntityOwner)& aOwner = aEntity->OwnerId())
409 aOwner->SetZLayer (theLayerId);
416 //=======================================================================
417 //function : UpdateClipping
419 //=======================================================================
420 void SelectMgr_SelectableObject::UpdateClipping()
422 PrsMgr_PresentableObject::UpdateClipping();
423 if (!mySelectionPrs.IsNull())
425 mySelectionPrs->SetClipPlanes (myClipPlanes);
427 if (!myHilightPrs.IsNull())
429 myHilightPrs->SetClipPlanes (myClipPlanes);
433 //=======================================================================
434 //function : updateSelection
435 //purpose : Sets update status FULL to selections of the object. Must be
436 // used as the only method of UpdateSelection from outer classes
437 // to prevent BVH structures from being outdated.
438 //=======================================================================
439 void SelectMgr_SelectableObject::updateSelection (const Standard_Integer theMode)
443 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
445 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
446 aSel->UpdateStatus (SelectMgr_TOU_Full);
451 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
453 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
454 if (aSel->Mode() == theMode)
456 aSel->UpdateStatus (SelectMgr_TOU_Full);
462 //=======================================================================
463 //function : SetAssemblyOwner
464 //purpose : Sets common entity owner for assembly sensitive object entities
465 //=======================================================================
466 void SelectMgr_SelectableObject::SetAssemblyOwner (const Handle(SelectMgr_EntityOwner)& theOwner,
467 const Standard_Integer theMode)
471 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
473 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
474 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
476 aSelEntIter.Value()->BaseSensitive()->Set (theOwner);
482 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
484 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
485 if (aSel->Mode() == theMode)
487 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
489 aSelEntIter.Value()->BaseSensitive()->Set (theOwner);
496 //=======================================================================
497 //function : BndBoxOfSelected
499 //=======================================================================
500 Bnd_Box SelectMgr_SelectableObject::BndBoxOfSelected (const Handle(SelectMgr_IndexedMapOfOwner)& theOwners)
502 if (theOwners->IsEmpty())
506 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (myselections); aSelIter.More(); aSelIter.Next())
508 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
509 if (aSel->GetSelectionState() != SelectMgr_SOS_Activated)
512 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
514 const Handle(SelectMgr_EntityOwner)& anOwner = aSelEntIter.Value()->BaseSensitive()->OwnerId();
515 if (theOwners->Contains (anOwner))
517 Select3D_BndBox3d aBox = aSelEntIter.Value()->BaseSensitive()->BoundingBox();
518 aBnd.Update (aBox.CornerMin().x(), aBox.CornerMin().y(), aBox.CornerMin().z(),
519 aBox.CornerMax().x(), aBox.CornerMax().y(), aBox.CornerMax().z());
527 //=======================================================================
528 //function : GlobalSelOwner
529 //purpose : Returns entity owner corresponding to selection of the object as a whole
530 //=======================================================================
531 Handle(SelectMgr_EntityOwner) SelectMgr_SelectableObject::GlobalSelOwner() const
533 const Handle(SelectMgr_Selection)& aGlobalSel = Selection (myGlobalSelMode);
534 if (!aGlobalSel.IsNull()
535 && !aGlobalSel->IsEmpty())
537 return aGlobalSel->Entities().First()->BaseSensitive()->OwnerId();
539 return THE_NULL_ENTITYOWNER;
542 //=======================================================================
543 //function : GetAssemblyOwner
545 //=======================================================================
546 const Handle(SelectMgr_EntityOwner)& SelectMgr_SelectableObject::GetAssemblyOwner() const
548 return THE_NULL_ENTITYOWNER;
551 // =======================================================================
552 // function : DumpJson
554 // =======================================================================
555 void SelectMgr_SelectableObject::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
557 OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
559 OCCT_DUMP_BASE_CLASS (theOStream, theDepth, PrsMgr_PresentableObject)
561 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, mySelectionPrs.get())
562 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, myHilightPrs.get())
564 for (SelectMgr_SequenceOfSelection::Iterator anIterator (myselections); anIterator.More(); anIterator.Next())
566 const Handle(SelectMgr_Selection)& aSelection = anIterator.Value();
567 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, aSelection.get())
570 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myGlobalSelMode)
571 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myAutoHilight)