1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
16 #include <Geom_Transformation.hxx>
17 #include <Graphic3d_GraphicDriver.hxx>
18 #include <Prs3d_Presentation.hxx>
19 #include <Prs3d_PresentationShadow.hxx>
20 #include <PrsMgr_ModedPresentation.hxx>
21 #include <PrsMgr_PresentableObject.hxx>
22 #include <PrsMgr_Presentation.hxx>
23 #include <PrsMgr_PresentationManager.hxx>
24 #include <PrsMgr_Presentations.hxx>
25 #include <Standard_NoSuchObject.hxx>
26 #include <Standard_Type.hxx>
27 #include <TColStd_ListIteratorOfListOfTransient.hxx>
28 #include <V3d_View.hxx>
30 IMPLEMENT_STANDARD_RTTIEXT(PrsMgr_PresentationManager,MMgt_TShared)
32 // =======================================================================
33 // function : PrsMgr_PresentationManager
35 // =======================================================================
36 PrsMgr_PresentationManager::PrsMgr_PresentationManager (const Handle(Graphic3d_StructureManager)& theStructureManager)
37 : myStructureManager (theStructureManager),
38 myImmediateModeOn (0),
39 mySelectionColor (Quantity_NOC_GRAY99)
44 // =======================================================================
47 // =======================================================================
48 void PrsMgr_PresentationManager::Display (const Handle(PrsMgr_PresentableObject)& thePrsObj,
49 const Standard_Integer theMode)
51 if (thePrsObj->HasOwnPresentations())
53 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode, Standard_True);
54 if (aPrs->MustBeUpdated())
56 Update (thePrsObj, theMode);
59 if (myImmediateModeOn > 0)
61 AddToImmediateList (aPrs->Presentation());
70 thePrsObj->Compute (this, Handle(Prs3d_Presentation)(), theMode);
73 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
75 Display (anIter.Value(), theMode);
79 // =======================================================================
82 // =======================================================================
83 void PrsMgr_PresentationManager::Erase (const Handle(PrsMgr_PresentableObject)& thePrsObj,
84 const Standard_Integer theMode)
86 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
88 Erase (anIter.Value(), theMode);
91 PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
92 for (Standard_Integer aPrsIter = 1; aPrsIter <= aPrsList.Length(); ++aPrsIter)
94 const PrsMgr_ModedPresentation& aModedPrs = aPrsList.Value (aPrsIter);
95 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aModedPrs.Presentation()->PresentationManager();
96 if (theMode == aPrsList (aPrsIter).Mode()
99 if (!aModedPrs.Presentation().IsNull())
101 aModedPrs.Presentation()->Erase();
103 aPrsList.Remove (aPrsIter);
109 // =======================================================================
112 // =======================================================================
113 void PrsMgr_PresentationManager::Clear (const Handle(PrsMgr_PresentableObject)& thePrsObj,
114 const Standard_Integer theMode)
116 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
118 Clear (anIter.Value(), theMode);
121 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
128 // =======================================================================
129 // function : SetVisibility
131 // =======================================================================
132 void PrsMgr_PresentationManager::SetVisibility (const Handle(PrsMgr_PresentableObject)& thePrsObj,
133 const Standard_Integer theMode,
134 const Standard_Boolean theValue)
136 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
138 SetVisibility (anIter.Value(), theMode, theValue);
140 if (!thePrsObj->HasOwnPresentations())
145 Presentation (thePrsObj, theMode)->SetVisible (theValue);
148 // =======================================================================
149 // function : Highlight
151 // =======================================================================
152 void PrsMgr_PresentationManager::Highlight (const Handle(PrsMgr_PresentableObject)& thePrsObj,
153 const Standard_Integer theMode)
155 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
157 Highlight (anIter.Value(), theMode);
159 if (!thePrsObj->HasOwnPresentations())
164 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode, Standard_True);
165 if (aPrs->MustBeUpdated())
167 Update (thePrsObj, theMode);
170 if (myImmediateModeOn > 0)
172 Handle(Prs3d_PresentationShadow) aShadow = new Prs3d_PresentationShadow (myStructureManager, aPrs->Presentation());
173 aShadow->Highlight (Aspect_TOHM_COLOR, mySelectionColor);
174 AddToImmediateList (aShadow);
178 aPrs->Highlight (Aspect_TOHM_COLOR, mySelectionColor);
182 // =======================================================================
183 // function : Unhighlight
185 // =======================================================================
186 void PrsMgr_PresentationManager::Unhighlight (const Handle(PrsMgr_PresentableObject)& thePrsObj,
187 const Standard_Integer theMode)
189 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
191 Unhighlight (anIter.Value(), theMode);
194 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
201 // =======================================================================
202 // function : SetDisplayPriority
204 // =======================================================================
205 void PrsMgr_PresentationManager::SetDisplayPriority (const Handle(PrsMgr_PresentableObject)& thePrsObj,
206 const Standard_Integer theMode,
207 const Standard_Integer theNewPrior) const
209 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
211 SetDisplayPriority (anIter.Value(), theMode, theNewPrior);
214 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
217 aPrs->SetDisplayPriority (theNewPrior);
221 // =======================================================================
222 // function : DisplayPriority
224 // =======================================================================
225 Standard_Integer PrsMgr_PresentationManager::DisplayPriority (const Handle(PrsMgr_PresentableObject)& thePrsObj,
226 const Standard_Integer theMode) const
228 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
230 Standard_Integer aPriority = DisplayPriority (anIter.Value(), theMode);
237 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
238 return !aPrs.IsNull()
239 ? aPrs->DisplayPriority()
243 // =======================================================================
244 // function : IsDisplayed
246 // =======================================================================
247 Standard_Boolean PrsMgr_PresentationManager::IsDisplayed (const Handle(PrsMgr_PresentableObject)& thePrsObj,
248 const Standard_Integer theMode) const
250 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
252 if (IsDisplayed (anIter.Value(), theMode))
254 return Standard_True;
258 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
259 return !aPrs.IsNull()
260 && aPrs->IsDisplayed();
263 // =======================================================================
264 // function : IsHighlighted
266 // =======================================================================
267 Standard_Boolean PrsMgr_PresentationManager::IsHighlighted (const Handle(PrsMgr_PresentableObject)& thePrsObj,
268 const Standard_Integer theMode) const
270 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
272 if (IsHighlighted (anIter.Value(), theMode))
274 return Standard_True;
278 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
279 return !aPrs.IsNull()
280 && aPrs->IsHighlighted();
283 // =======================================================================
286 // =======================================================================
287 void PrsMgr_PresentationManager::Update (const Handle(PrsMgr_PresentableObject)& thePrsObj,
288 const Standard_Integer theMode) const
290 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
292 Update (anIter.Value(), theMode);
295 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
299 thePrsObj->Fill (this, aPrs, theMode);
300 aPrs->SetUpdateStatus (Standard_False);
304 // =======================================================================
305 // function : BeginImmediateDraw
307 // =======================================================================
308 void PrsMgr_PresentationManager::BeginImmediateDraw()
310 if (++myImmediateModeOn > 1)
315 ClearImmediateDraw();
318 // =======================================================================
319 // function : ClearImmediateDraw
321 // =======================================================================
322 void PrsMgr_PresentationManager::ClearImmediateDraw()
324 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
326 anIter.Value()->Erase();
329 for (PrsMgr_ListOfPresentations::Iterator anIter (myViewDependentImmediateList); anIter.More(); anIter.Next())
331 anIter.Value()->Erase();
334 myImmediateList.Clear();
335 myViewDependentImmediateList.Clear();
338 // =======================================================================
339 // function : displayImmediate
340 // purpose : Handles the structures from myImmediateList and its visibility
341 // in all views of the viewer given by setting proper affinity
342 // =======================================================================
343 void PrsMgr_PresentationManager::displayImmediate (const Handle(V3d_Viewer)& theViewer)
345 for (theViewer->InitActiveViews(); theViewer->MoreActiveViews(); theViewer->NextActiveViews())
347 const Handle(Graphic3d_CView)& aView = theViewer->ActiveView()->View();
348 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
350 const Handle(Prs3d_Presentation)& aPrs = anIter.Value();
354 Handle(Graphic3d_Structure) aViewDepPrs;
355 Handle(Prs3d_PresentationShadow) aShadowPrs = Handle(Prs3d_PresentationShadow)::DownCast (aPrs);
356 if (!aShadowPrs.IsNull() && aView->IsComputed (aShadowPrs->ParentId(), aViewDepPrs))
358 aShadowPrs.Nullify();
359 aShadowPrs = new Prs3d_PresentationShadow (myStructureManager,
360 Handle(Prs3d_Presentation)::DownCast (aViewDepPrs));
361 aShadowPrs->SetZLayer (aViewDepPrs->CStructure()->ZLayer());
362 aShadowPrs->SetClipPlanes (aViewDepPrs->GetClipPlanes());
363 aShadowPrs->CStructure()->IsForHighlight = 1;
364 aShadowPrs->Highlight (Aspect_TOHM_COLOR, aPrs->HighlightColor());
365 myViewDependentImmediateList.Append (aShadowPrs);
367 // handles custom highlight presentations which were defined in overridden
368 // HilightOwnerWithColor method of a custom AIS objects and maintain its
369 // visibility in different views on their own
370 else if (aShadowPrs.IsNull())
376 if (!aShadowPrs->IsDisplayed())
378 aShadowPrs->CStructure()->ViewAffinity = new Graphic3d_ViewAffinity();
379 aShadowPrs->CStructure()->ViewAffinity->SetVisible (Standard_False);
380 aShadowPrs->Display();
383 Standard_Integer aViewId = aView->Identification();
384 bool isParentVisible = aShadowPrs->ParentAffinity().IsNull() ?
385 Standard_True : aShadowPrs->ParentAffinity()->IsVisible (aViewId);
386 aShadowPrs->CStructure()->ViewAffinity->SetVisible (aViewId, isParentVisible);
391 // =======================================================================
392 // function : EndImmediateDraw
394 // =======================================================================
395 void PrsMgr_PresentationManager::EndImmediateDraw (const Handle(V3d_Viewer)& theViewer)
397 if (--myImmediateModeOn > 0)
402 displayImmediate (theViewer);
405 // =======================================================================
406 // function : RedrawImmediate
407 // purpose : Clears all immediate structures and redisplays with proper
409 //=======================================================================
410 void PrsMgr_PresentationManager::RedrawImmediate (const Handle(V3d_Viewer)& theViewer)
412 if (myImmediateList.IsEmpty())
415 // Clear previously displayed structures
416 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
418 anIter.Value()->Erase();
420 for (PrsMgr_ListOfPresentations::Iterator anIter (myViewDependentImmediateList); anIter.More(); anIter.Next())
422 anIter.Value()->Erase();
424 myViewDependentImmediateList.Clear();
426 displayImmediate (theViewer);
429 // =======================================================================
430 // function : AddToImmediateList
432 //=======================================================================
433 void PrsMgr_PresentationManager::AddToImmediateList (const Handle(Prs3d_Presentation)& thePrs)
435 if (myImmediateModeOn < 1)
440 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
442 if (anIter.Value() == thePrs)
448 myImmediateList.Append (thePrs);
451 // =======================================================================
452 // function : HasPresentation
454 // =======================================================================
455 Standard_Boolean PrsMgr_PresentationManager::HasPresentation (const Handle(PrsMgr_PresentableObject)& thePrsObj,
456 const Standard_Integer theMode) const
458 if (!thePrsObj->HasOwnPresentations())
459 return Standard_False;
461 const PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
462 for (Standard_Integer aPrsIter = 1; aPrsIter <= aPrsList.Length(); ++aPrsIter)
464 const PrsMgr_ModedPresentation& aModedPrs = aPrsList.Value (aPrsIter);
465 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aModedPrs.Presentation()->PresentationManager();
466 if (theMode == aModedPrs.Mode()
469 return Standard_True;
472 return Standard_False;
475 // =======================================================================
476 // function : Presentation
478 // =======================================================================
479 Handle(PrsMgr_Presentation) PrsMgr_PresentationManager::Presentation (const Handle(PrsMgr_PresentableObject)& thePrsObj,
480 const Standard_Integer theMode,
481 const Standard_Boolean theToCreate,
482 const Handle(PrsMgr_PresentableObject)& theSelObj) const
484 const PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
485 for (Standard_Integer aPrsIter = 1; aPrsIter <= aPrsList.Length(); ++aPrsIter)
487 const PrsMgr_ModedPresentation& aModedPrs = aPrsList.Value (aPrsIter);
488 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aModedPrs.Presentation()->PresentationManager();
489 if (theMode == aModedPrs.Mode()
492 return aModedPrs.Presentation();
498 return Handle(PrsMgr_Presentation)();
501 Handle(PrsMgr_Presentation) aPrs = new PrsMgr_Presentation (this, thePrsObj);
502 aPrs->SetZLayer (thePrsObj->ZLayer());
503 aPrs->Presentation()->CStructure()->ViewAffinity = myStructureManager->ObjectAffinity (!theSelObj.IsNull() ? theSelObj : thePrsObj);
504 thePrsObj->Presentations().Append (PrsMgr_ModedPresentation (aPrs, theMode));
505 thePrsObj->Fill (this, aPrs, theMode);
507 // set layer index accordingly to object's presentations
508 aPrs->SetUpdateStatus (Standard_False);
512 // =======================================================================
513 // function : RemovePresentation
515 // =======================================================================
516 Standard_Boolean PrsMgr_PresentationManager::RemovePresentation (const Handle(PrsMgr_PresentableObject)& thePrsObj,
517 const Standard_Integer theMode)
519 PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
520 for (Standard_Integer aPrsIter = 1; aPrsIter <= aPrsList.Length(); ++aPrsIter)
522 const PrsMgr_ModedPresentation& aModedPrs = aPrsList.Value (aPrsIter);
523 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aModedPrs.Presentation()->PresentationManager();
524 if (theMode == aPrsList (aPrsIter).Mode()
527 aPrsList.Remove (aPrsIter);
528 return Standard_True;
531 return Standard_False;
534 // =======================================================================
535 // function : SetZLayer
537 // =======================================================================
538 void PrsMgr_PresentationManager::SetZLayer (const Handle(PrsMgr_PresentableObject)& thePrsObj,
539 const Standard_Integer theLayerId)
541 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
543 SetZLayer (anIter.Value(), theLayerId);
545 if (!thePrsObj->HasOwnPresentations())
550 thePrsObj->SetZLayer (theLayerId);
553 // =======================================================================
554 // function : GetZLayer
556 // =======================================================================
557 Standard_Integer PrsMgr_PresentationManager::GetZLayer (const Handle(PrsMgr_PresentableObject)& thePrsObj) const
559 return thePrsObj->ZLayer();
562 // =======================================================================
563 // function : Connect
565 // =======================================================================
566 void PrsMgr_PresentationManager::Connect (const Handle(PrsMgr_PresentableObject)& thePrsObject,
567 const Handle(PrsMgr_PresentableObject)& theOtherObject,
568 const Standard_Integer theMode,
569 const Standard_Integer theOtherMode)
571 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObject, theMode, Standard_True);
572 Handle(PrsMgr_Presentation) aPrsOther = Presentation (theOtherObject, theOtherMode, Standard_True);
573 aPrs->Connect (aPrsOther);
576 // =======================================================================
577 // function : Transform
579 // =======================================================================
580 void PrsMgr_PresentationManager::Transform (const Handle(PrsMgr_PresentableObject)& thePrsObj,
581 const Handle(Geom_Transformation)& theTransformation,
582 const Standard_Integer theMode)
584 Presentation (thePrsObj, theMode)->Transform (theTransformation);
588 // =======================================================================
591 // =======================================================================
592 void PrsMgr_PresentationManager::Color (const Handle(PrsMgr_PresentableObject)& thePrsObj,
593 const Quantity_NameOfColor theColor,
594 const Standard_Integer theMode,
595 const Handle(PrsMgr_PresentableObject)& theSelObj,
596 const Standard_Integer theImmediateStructLayerId)
598 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
600 Color (anIter.Value(), theColor, theMode, NULL, theImmediateStructLayerId);
602 if (!thePrsObj->HasOwnPresentations())
607 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode, Standard_True, theSelObj);
608 if (aPrs->MustBeUpdated())
610 Update (thePrsObj, theMode);
613 if (myImmediateModeOn > 0)
615 Handle(Prs3d_PresentationShadow) aShadow = new Prs3d_PresentationShadow (myStructureManager, aPrs->Presentation());
616 aShadow->SetZLayer (theImmediateStructLayerId);
617 aShadow->SetClipPlanes (aPrs->Presentation()->GetClipPlanes());
618 aShadow->CStructure()->IsForHighlight = 1;
619 aShadow->Highlight (Aspect_TOHM_COLOR, theColor);
620 AddToImmediateList (aShadow);
624 aPrs->Highlight (Aspect_TOHM_COLOR, theColor);
628 // =======================================================================
629 // function : BoundBox
631 // =======================================================================
632 void PrsMgr_PresentationManager::BoundBox (const Handle(PrsMgr_PresentableObject)& thePrsObj,
633 const Standard_Integer theMode)
635 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode, Standard_True);
636 if (aPrs->MustBeUpdated())
638 Update (thePrsObj, theMode);
640 aPrs->Highlight (Aspect_TOHM_BOUNDBOX, mySelectionColor);
645 // =======================================================================
646 // function : updatePrsTransformation
647 // purpose : Internal funtion that scans thePrsList for shadow presentations
648 // and applies transformation theTrsf to them in case if parent ID
649 // of shadow presentation is equal to theRefId
650 // =======================================================================
651 void updatePrsTransformation (const PrsMgr_ListOfPresentations& thePrsList,
652 const Standard_Integer theRefId,
653 const Graphic3d_Mat4& theTrsf)
655 for (PrsMgr_ListOfPresentations::Iterator anIter (thePrsList); anIter.More(); anIter.Next())
657 const Handle(Prs3d_Presentation)& aPrs = anIter.Value();
661 Handle(Prs3d_PresentationShadow) aShadowPrs = Handle(Prs3d_PresentationShadow)::DownCast (aPrs);
662 if (aShadowPrs.IsNull() || aShadowPrs->ParentId() != theRefId)
665 aShadowPrs->CStructure()->Transformation = theTrsf;
670 // =======================================================================
671 // function : UpdateHighlightTrsf
673 // =======================================================================
674 void PrsMgr_PresentationManager::UpdateHighlightTrsf (const Handle(V3d_Viewer)& theViewer,
675 const Handle(PrsMgr_PresentableObject)& theObj,
676 const Standard_Integer theMode,
677 const Handle(PrsMgr_PresentableObject)& theSelObj)
682 const Handle(Prs3d_Presentation)& aBasePrs = Presentation (theObj, theMode, Standard_False)->Presentation();
683 const Handle(Prs3d_Presentation)& aParentPrs = theSelObj.IsNull() ?
684 aBasePrs : Presentation (theSelObj, theMode, Standard_False)->Presentation();
685 const Standard_Integer aParentId = aParentPrs->CStructure()->Id;
687 updatePrsTransformation (myImmediateList, aParentId, aBasePrs->CStructure()->Transformation);
689 if (!myViewDependentImmediateList.IsEmpty())
691 for (theViewer->InitActiveViews(); theViewer->MoreActiveViews(); theViewer->NextActiveViews())
693 const Handle(Graphic3d_CView)& aView = theViewer->ActiveView()->View();
694 Handle(Graphic3d_Structure) aViewDepParentPrs;
695 if (aView->IsComputed (aParentId, aViewDepParentPrs))
697 updatePrsTransformation (myViewDependentImmediateList,
698 aViewDepParentPrs->CStructure()->Id,
699 aBasePrs->CStructure()->Transformation);