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.
15 #include <PrsMgr_PresentationManager.hxx>
17 #include <Geom_Transformation.hxx>
18 #include <Graphic3d_GraphicDriver.hxx>
19 #include <Prs3d_Drawer.hxx>
20 #include <Prs3d_Presentation.hxx>
21 #include <Prs3d_PresentationShadow.hxx>
22 #include <PrsMgr_PresentableObject.hxx>
23 #include <PrsMgr_Presentation.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, Standard_Transient)
32 // =======================================================================
33 // function : PrsMgr_PresentationManager
35 // =======================================================================
36 PrsMgr_PresentationManager::PrsMgr_PresentationManager (const Handle(Graphic3d_StructureManager)& theStructureManager)
37 : myStructureManager (theStructureManager),
43 // =======================================================================
46 // =======================================================================
47 void PrsMgr_PresentationManager::Display (const Handle(PrsMgr_PresentableObject)& thePrsObj,
48 const Standard_Integer theMode)
50 if (thePrsObj->HasOwnPresentations())
52 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode, Standard_True);
53 if (aPrs->MustBeUpdated())
55 Update (thePrsObj, theMode);
58 if (myImmediateModeOn > 0)
60 AddToImmediateList (aPrs);
69 thePrsObj->Compute (this, Handle(Prs3d_Presentation)(), theMode);
72 if (thePrsObj->ToPropagateVisualState())
74 for (PrsMgr_ListOfPresentableObjectsIter anIter(thePrsObj->Children()); anIter.More(); anIter.Next())
76 Display(anIter.Value(), theMode);
81 // =======================================================================
84 // =======================================================================
85 void PrsMgr_PresentationManager::Erase (const Handle(PrsMgr_PresentableObject)& thePrsObj,
86 const Standard_Integer theMode)
88 if (thePrsObj->ToPropagateVisualState())
90 for (PrsMgr_ListOfPresentableObjectsIter anIter(thePrsObj->Children()); anIter.More(); anIter.Next())
92 Erase(anIter.Value(), theMode);
96 PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
97 for (PrsMgr_Presentations::Iterator aPrsIter (aPrsList); aPrsIter.More();)
99 const Handle(PrsMgr_Presentation)& aPrs = aPrsIter.Value();
106 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aPrs->PresentationManager();
107 if ((theMode == aPrs->Mode() || theMode == -1)
108 && (this == aPrsMgr))
112 aPrsList.Remove (aPrsIter);
126 // =======================================================================
129 // =======================================================================
130 void PrsMgr_PresentationManager::Clear (const Handle(PrsMgr_PresentableObject)& thePrsObj,
131 const Standard_Integer theMode)
133 if (thePrsObj->ToPropagateVisualState())
135 for (PrsMgr_ListOfPresentableObjectsIter anIter(thePrsObj->Children()); anIter.More(); anIter.Next())
137 Clear(anIter.Value(), theMode);
141 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
148 // =======================================================================
149 // function : SetVisibility
151 // =======================================================================
152 void PrsMgr_PresentationManager::SetVisibility (const Handle(PrsMgr_PresentableObject)& thePrsObj,
153 const Standard_Integer theMode,
154 const Standard_Boolean theValue)
156 if (thePrsObj->ToPropagateVisualState())
158 for (PrsMgr_ListOfPresentableObjectsIter anIter(thePrsObj->Children()); anIter.More(); anIter.Next())
160 SetVisibility(anIter.Value(), theMode, theValue);
163 if (!thePrsObj->HasOwnPresentations())
168 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
171 aPrs->SetVisible (theValue);
175 // =======================================================================
176 // function : Unhighlight
178 // =======================================================================
179 void PrsMgr_PresentationManager::Unhighlight (const Handle(PrsMgr_PresentableObject)& thePrsObj)
181 if (thePrsObj->ToPropagateVisualState())
183 for (PrsMgr_ListOfPresentableObjectsIter anIter(thePrsObj->Children()); anIter.More(); anIter.Next())
185 Unhighlight(anIter.Value());
189 const PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
190 for (PrsMgr_Presentations::Iterator aPrsIter (aPrsList); aPrsIter.More(); aPrsIter.Next())
192 const Handle(PrsMgr_Presentation)& aPrs = aPrsIter.Value();
193 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aPrs->PresentationManager();
195 && aPrs->IsHighlighted())
202 // =======================================================================
203 // function : SetDisplayPriority
205 // =======================================================================
206 void PrsMgr_PresentationManager::SetDisplayPriority (const Handle(PrsMgr_PresentableObject)& thePrsObj,
207 const Standard_Integer theMode,
208 const Standard_Integer theNewPrior) const
210 if (thePrsObj->ToPropagateVisualState())
212 for (PrsMgr_ListOfPresentableObjectsIter anIter(thePrsObj->Children()); anIter.More(); anIter.Next())
214 SetDisplayPriority(anIter.Value(), theMode, theNewPrior);
218 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
221 aPrs->SetDisplayPriority (theNewPrior);
225 // =======================================================================
226 // function : DisplayPriority
228 // =======================================================================
229 Standard_Integer PrsMgr_PresentationManager::DisplayPriority (const Handle(PrsMgr_PresentableObject)& thePrsObj,
230 const Standard_Integer theMode) const
232 if (thePrsObj->ToPropagateVisualState())
234 for (PrsMgr_ListOfPresentableObjectsIter anIter(thePrsObj->Children()); anIter.More(); anIter.Next())
236 Standard_Integer aPriority = DisplayPriority(anIter.Value(), theMode);
244 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
245 return !aPrs.IsNull()
246 ? aPrs->DisplayPriority()
250 // =======================================================================
251 // function : IsDisplayed
253 // =======================================================================
254 Standard_Boolean PrsMgr_PresentationManager::IsDisplayed (const Handle(PrsMgr_PresentableObject)& thePrsObj,
255 const Standard_Integer theMode) const
257 if (thePrsObj->ToPropagateVisualState())
259 for (PrsMgr_ListOfPresentableObjectsIter anIter(thePrsObj->Children()); anIter.More(); anIter.Next())
261 if (IsDisplayed(anIter.Value(), theMode))
263 return Standard_True;
268 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
269 return !aPrs.IsNull()
270 && aPrs->IsDisplayed();
273 // =======================================================================
274 // function : IsHighlighted
276 // =======================================================================
277 Standard_Boolean PrsMgr_PresentationManager::IsHighlighted (const Handle(PrsMgr_PresentableObject)& thePrsObj,
278 const Standard_Integer theMode) const
280 if (thePrsObj->ToPropagateVisualState())
282 for (PrsMgr_ListOfPresentableObjectsIter anIter(thePrsObj->Children()); anIter.More(); anIter.Next())
284 if (IsHighlighted(anIter.Value(), theMode))
286 return Standard_True;
291 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
292 return !aPrs.IsNull()
293 && aPrs->IsHighlighted();
296 // =======================================================================
299 // =======================================================================
300 void PrsMgr_PresentationManager::Update (const Handle(PrsMgr_PresentableObject)& thePrsObj,
301 const Standard_Integer theMode) const
303 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
305 Update (anIter.Value(), theMode);
308 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
312 thePrsObj->Fill (this, aPrs, theMode);
313 aPrs->SetUpdateStatus (Standard_False);
317 // =======================================================================
318 // function : BeginImmediateDraw
320 // =======================================================================
321 void PrsMgr_PresentationManager::BeginImmediateDraw()
323 if (++myImmediateModeOn > 1)
328 ClearImmediateDraw();
331 // =======================================================================
332 // function : ClearImmediateDraw
334 // =======================================================================
335 void PrsMgr_PresentationManager::ClearImmediateDraw()
337 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
339 anIter.Value()->Erase();
342 for (PrsMgr_ListOfPresentations::Iterator anIter (myViewDependentImmediateList); anIter.More(); anIter.Next())
344 anIter.Value()->Erase();
347 myImmediateList.Clear();
348 myViewDependentImmediateList.Clear();
351 // =======================================================================
352 // function : displayImmediate
353 // purpose : Handles the structures from myImmediateList and its visibility
354 // in all views of the viewer given by setting proper affinity
355 // =======================================================================
356 void PrsMgr_PresentationManager::displayImmediate (const Handle(V3d_Viewer)& theViewer)
358 for (V3d_ListOfViewIterator anActiveViewIter (theViewer->ActiveViewIterator()); anActiveViewIter.More(); anActiveViewIter.Next())
360 const Handle(Graphic3d_CView)& aView = anActiveViewIter.Value()->View();
361 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
363 const Handle(Prs3d_Presentation)& aPrs = anIter.Value();
367 Handle(Graphic3d_Structure) aViewDepPrs;
368 Handle(Prs3d_PresentationShadow) aShadowPrs = Handle(Prs3d_PresentationShadow)::DownCast (aPrs);
369 if (!aShadowPrs.IsNull() && aView->IsComputed (aShadowPrs->ParentId(), aViewDepPrs))
371 aShadowPrs.Nullify();
372 aShadowPrs = new Prs3d_PresentationShadow (myStructureManager, aViewDepPrs);
373 aShadowPrs->SetZLayer (aViewDepPrs->CStructure()->ZLayer());
374 aShadowPrs->SetClipPlanes (aViewDepPrs->ClipPlanes());
375 aShadowPrs->CStructure()->IsForHighlight = 1;
376 aShadowPrs->Highlight (aPrs->HighlightStyle());
377 myViewDependentImmediateList.Append (aShadowPrs);
379 // handles custom highlight presentations which were defined in overridden
380 // HilightOwnerWithColor method of a custom AIS objects and maintain its
381 // visibility in different views on their own
382 else if (aShadowPrs.IsNull())
388 if (!aShadowPrs->IsDisplayed())
390 aShadowPrs->CStructure()->ViewAffinity = new Graphic3d_ViewAffinity();
391 aShadowPrs->CStructure()->ViewAffinity->SetVisible (Standard_False);
392 aShadowPrs->Display();
395 Standard_Integer aViewId = aView->Identification();
396 bool isParentVisible = aShadowPrs->ParentAffinity().IsNull() ?
397 Standard_True : aShadowPrs->ParentAffinity()->IsVisible (aViewId);
398 aShadowPrs->CStructure()->ViewAffinity->SetVisible (aViewId, isParentVisible);
403 // =======================================================================
404 // function : EndImmediateDraw
406 // =======================================================================
407 void PrsMgr_PresentationManager::EndImmediateDraw (const Handle(V3d_Viewer)& theViewer)
409 if (--myImmediateModeOn > 0)
414 displayImmediate (theViewer);
417 // =======================================================================
418 // function : RedrawImmediate
419 // purpose : Clears all immediate structures and redisplays with proper
421 //=======================================================================
422 void PrsMgr_PresentationManager::RedrawImmediate (const Handle(V3d_Viewer)& theViewer)
424 if (myImmediateList.IsEmpty())
427 // Clear previously displayed structures
428 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
430 anIter.Value()->Erase();
432 for (PrsMgr_ListOfPresentations::Iterator anIter (myViewDependentImmediateList); anIter.More(); anIter.Next())
434 anIter.Value()->Erase();
436 myViewDependentImmediateList.Clear();
438 displayImmediate (theViewer);
441 // =======================================================================
442 // function : AddToImmediateList
444 //=======================================================================
445 void PrsMgr_PresentationManager::AddToImmediateList (const Handle(Prs3d_Presentation)& thePrs)
447 if (myImmediateModeOn < 1)
452 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
454 if (anIter.Value() == thePrs)
460 myImmediateList.Append (thePrs);
463 // =======================================================================
464 // function : HasPresentation
466 // =======================================================================
467 Standard_Boolean PrsMgr_PresentationManager::HasPresentation (const Handle(PrsMgr_PresentableObject)& thePrsObj,
468 const Standard_Integer theMode) const
470 if (!thePrsObj->HasOwnPresentations())
471 return Standard_False;
473 const PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
474 for (PrsMgr_Presentations::Iterator aPrsIter (aPrsList); aPrsIter.More(); aPrsIter.Next())
476 const Handle(PrsMgr_Presentation)& aPrs = aPrsIter.Value();
477 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aPrs->PresentationManager();
478 if (theMode == aPrs->Mode()
481 return Standard_True;
484 return Standard_False;
487 // =======================================================================
488 // function : Presentation
490 // =======================================================================
491 Handle(PrsMgr_Presentation) PrsMgr_PresentationManager::Presentation (const Handle(PrsMgr_PresentableObject)& thePrsObj,
492 const Standard_Integer theMode,
493 const Standard_Boolean theToCreate,
494 const Handle(PrsMgr_PresentableObject)& theSelObj) const
496 const PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
497 for (PrsMgr_Presentations::Iterator aPrsIter (aPrsList); aPrsIter.More(); aPrsIter.Next())
499 const Handle(PrsMgr_Presentation)& aPrs = aPrsIter.Value();
500 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aPrs->PresentationManager();
501 if (theMode == aPrs->Mode()
510 return Handle(PrsMgr_Presentation)();
513 Handle(PrsMgr_Presentation) aPrs = new PrsMgr_Presentation (this, thePrsObj, theMode);
514 aPrs->SetZLayer (thePrsObj->ZLayer());
515 aPrs->CStructure()->ViewAffinity = myStructureManager->ObjectAffinity (!theSelObj.IsNull() ? theSelObj : thePrsObj);
516 thePrsObj->Presentations().Append (aPrs);
517 thePrsObj->Fill (this, aPrs, theMode);
519 // set layer index accordingly to object's presentations
520 aPrs->SetUpdateStatus (Standard_False);
524 // =======================================================================
525 // function : RemovePresentation
527 // =======================================================================
528 Standard_Boolean PrsMgr_PresentationManager::RemovePresentation (const Handle(PrsMgr_PresentableObject)& thePrsObj,
529 const Standard_Integer theMode)
531 PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
532 for (PrsMgr_Presentations::Iterator aPrsIter (aPrsList); aPrsIter.More(); aPrsIter.Next())
534 const Handle(PrsMgr_Presentation)& aPrs = aPrsIter.Value();
535 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aPrs->PresentationManager();
536 if (theMode == aPrs->Mode()
539 aPrsList.Remove (aPrsIter);
540 return Standard_True;
543 return Standard_False;
546 // =======================================================================
547 // function : SetZLayer
549 // =======================================================================
550 void PrsMgr_PresentationManager::SetZLayer (const Handle(PrsMgr_PresentableObject)& thePrsObj,
551 const Graphic3d_ZLayerId theLayerId)
553 if (thePrsObj->ToPropagateVisualState())
555 for (PrsMgr_ListOfPresentableObjectsIter anIter(thePrsObj->Children()); anIter.More(); anIter.Next())
557 SetZLayer(anIter.Value(), theLayerId);
561 if (!thePrsObj->HasOwnPresentations())
566 thePrsObj->SetZLayer (theLayerId);
569 // =======================================================================
570 // function : GetZLayer
572 // =======================================================================
573 Graphic3d_ZLayerId PrsMgr_PresentationManager::GetZLayer (const Handle(PrsMgr_PresentableObject)& thePrsObj) const
575 return thePrsObj->ZLayer();
578 // =======================================================================
579 // function : Connect
581 // =======================================================================
582 void PrsMgr_PresentationManager::Connect (const Handle(PrsMgr_PresentableObject)& thePrsObject,
583 const Handle(PrsMgr_PresentableObject)& theOtherObject,
584 const Standard_Integer theMode,
585 const Standard_Integer theOtherMode)
587 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObject, theMode, Standard_True);
588 Handle(PrsMgr_Presentation) aPrsOther = Presentation (theOtherObject, theOtherMode, Standard_True);
589 aPrs->Connect (aPrsOther.get(), Graphic3d_TOC_DESCENDANT);
592 // =======================================================================
593 // function : Transform
595 // =======================================================================
596 void PrsMgr_PresentationManager::Transform (const Handle(PrsMgr_PresentableObject)& thePrsObj,
597 const Handle(Geom_Transformation)& theTransformation,
598 const Standard_Integer theMode)
600 Presentation (thePrsObj, theMode)->SetTransformation (theTransformation);
603 // =======================================================================
606 // =======================================================================
607 void PrsMgr_PresentationManager::Color (const Handle(PrsMgr_PresentableObject)& thePrsObj,
608 const Handle(Prs3d_Drawer)& theStyle,
609 const Standard_Integer theMode,
610 const Handle(PrsMgr_PresentableObject)& theSelObj,
611 const Standard_Integer theImmediateStructLayerId)
613 if (thePrsObj->ToPropagateVisualState())
615 for (PrsMgr_ListOfPresentableObjectsIter anIter(thePrsObj->Children()); anIter.More(); anIter.Next())
617 Color(anIter.Value(), theStyle, theMode, NULL, theImmediateStructLayerId);
620 if (!thePrsObj->HasOwnPresentations())
625 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode, Standard_True, theSelObj);
626 if (aPrs->MustBeUpdated())
628 Update (thePrsObj, theMode);
631 if (myImmediateModeOn > 0)
633 Handle(Prs3d_PresentationShadow) aShadow = new Prs3d_PresentationShadow (myStructureManager, aPrs);
634 aShadow->SetZLayer (theImmediateStructLayerId);
635 aShadow->SetClipPlanes (aPrs->ClipPlanes());
636 aShadow->CStructure()->IsForHighlight = 1;
637 aShadow->Highlight (theStyle);
638 AddToImmediateList (aShadow);
642 aPrs->Highlight (theStyle);
648 // =======================================================================
649 // function : updatePrsTransformation
650 // purpose : Internal function that scans thePrsList for shadow presentations
651 // and applies transformation theTrsf to them in case if parent ID
652 // of shadow presentation is equal to theRefId
653 // =======================================================================
654 void updatePrsTransformation (const PrsMgr_ListOfPresentations& thePrsList,
655 const Standard_Integer theRefId,
656 const Handle(Geom_Transformation)& theTrsf)
658 for (PrsMgr_ListOfPresentations::Iterator anIter (thePrsList); anIter.More(); anIter.Next())
660 const Handle(Prs3d_Presentation)& aPrs = anIter.Value();
664 Handle(Prs3d_PresentationShadow) aShadowPrs = Handle(Prs3d_PresentationShadow)::DownCast (aPrs);
665 if (aShadowPrs.IsNull() || aShadowPrs->ParentId() != theRefId)
668 aShadowPrs->CStructure()->SetTransformation (theTrsf);
673 // =======================================================================
674 // function : UpdateHighlightTrsf
676 // =======================================================================
677 void PrsMgr_PresentationManager::UpdateHighlightTrsf (const Handle(V3d_Viewer)& theViewer,
678 const Handle(PrsMgr_PresentableObject)& theObj,
679 const Standard_Integer theMode,
680 const Handle(PrsMgr_PresentableObject)& theSelObj)
685 Handle(PrsMgr_Presentation) aPrs = Presentation (!theSelObj.IsNull() ? theSelObj : theObj, theMode, Standard_False);
691 Handle(Geom_Transformation) aTrsf = theObj->LocalTransformationGeom();
692 const Standard_Integer aParentId = aPrs->CStructure()->Id;
693 updatePrsTransformation (myImmediateList, aParentId, aTrsf);
695 if (!myViewDependentImmediateList.IsEmpty())
697 for (V3d_ListOfViewIterator anActiveViewIter (theViewer->ActiveViewIterator()); anActiveViewIter.More(); anActiveViewIter.Next())
699 const Handle(Graphic3d_CView)& aView = anActiveViewIter.Value()->View();
700 Handle(Graphic3d_Structure) aViewDepParentPrs;
701 if (aView->IsComputed (aParentId, aViewDepParentPrs))
703 updatePrsTransformation (myViewDependentImmediateList,
704 aViewDepParentPrs->CStructure()->Id,