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 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
74 Display (anIter.Value(), theMode);
78 // =======================================================================
81 // =======================================================================
82 void PrsMgr_PresentationManager::Erase (const Handle(PrsMgr_PresentableObject)& thePrsObj,
83 const Standard_Integer theMode)
85 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
87 Erase (anIter.Value(), theMode);
90 PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
91 for (PrsMgr_Presentations::Iterator aPrsIter (aPrsList); aPrsIter.More();)
93 const Handle(PrsMgr_Presentation)& aPrs = aPrsIter.Value();
100 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aPrs->PresentationManager();
101 if ((theMode == aPrs->Mode() || theMode == -1)
102 && (this == aPrsMgr))
106 aPrsList.Remove (aPrsIter);
120 // =======================================================================
123 // =======================================================================
124 void PrsMgr_PresentationManager::Clear (const Handle(PrsMgr_PresentableObject)& thePrsObj,
125 const Standard_Integer theMode)
127 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
129 Clear (anIter.Value(), theMode);
132 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
139 // =======================================================================
140 // function : SetVisibility
142 // =======================================================================
143 void PrsMgr_PresentationManager::SetVisibility (const Handle(PrsMgr_PresentableObject)& thePrsObj,
144 const Standard_Integer theMode,
145 const Standard_Boolean theValue)
147 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
149 SetVisibility (anIter.Value(), theMode, theValue);
151 if (!thePrsObj->HasOwnPresentations())
156 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
159 aPrs->SetVisible (theValue);
163 // =======================================================================
164 // function : Unhighlight
166 // =======================================================================
167 void PrsMgr_PresentationManager::Unhighlight (const Handle(PrsMgr_PresentableObject)& thePrsObj)
169 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
171 Unhighlight (anIter.Value());
174 const PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
175 for (PrsMgr_Presentations::Iterator aPrsIter (aPrsList); aPrsIter.More(); aPrsIter.Next())
177 const Handle(PrsMgr_Presentation)& aPrs = aPrsIter.Value();
178 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aPrs->PresentationManager();
180 && aPrs->IsHighlighted())
187 // =======================================================================
188 // function : SetDisplayPriority
190 // =======================================================================
191 void PrsMgr_PresentationManager::SetDisplayPriority (const Handle(PrsMgr_PresentableObject)& thePrsObj,
192 const Standard_Integer theMode,
193 const Standard_Integer theNewPrior) const
195 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
197 SetDisplayPriority (anIter.Value(), theMode, theNewPrior);
200 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
203 aPrs->SetDisplayPriority (theNewPrior);
207 // =======================================================================
208 // function : DisplayPriority
210 // =======================================================================
211 Standard_Integer PrsMgr_PresentationManager::DisplayPriority (const Handle(PrsMgr_PresentableObject)& thePrsObj,
212 const Standard_Integer theMode) const
214 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
216 Standard_Integer aPriority = DisplayPriority (anIter.Value(), theMode);
223 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
224 return !aPrs.IsNull()
225 ? aPrs->DisplayPriority()
229 // =======================================================================
230 // function : IsDisplayed
232 // =======================================================================
233 Standard_Boolean PrsMgr_PresentationManager::IsDisplayed (const Handle(PrsMgr_PresentableObject)& thePrsObj,
234 const Standard_Integer theMode) const
236 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
238 if (IsDisplayed (anIter.Value(), theMode))
240 return Standard_True;
244 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
245 return !aPrs.IsNull()
246 && aPrs->IsDisplayed();
249 // =======================================================================
250 // function : IsHighlighted
252 // =======================================================================
253 Standard_Boolean PrsMgr_PresentationManager::IsHighlighted (const Handle(PrsMgr_PresentableObject)& thePrsObj,
254 const Standard_Integer theMode) const
256 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
258 if (IsHighlighted (anIter.Value(), theMode))
260 return Standard_True;
264 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
265 return !aPrs.IsNull()
266 && aPrs->IsHighlighted();
269 // =======================================================================
272 // =======================================================================
273 void PrsMgr_PresentationManager::Update (const Handle(PrsMgr_PresentableObject)& thePrsObj,
274 const Standard_Integer theMode) const
276 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
278 Update (anIter.Value(), theMode);
281 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
285 thePrsObj->Fill (this, aPrs, theMode);
286 aPrs->SetUpdateStatus (Standard_False);
290 // =======================================================================
291 // function : BeginImmediateDraw
293 // =======================================================================
294 void PrsMgr_PresentationManager::BeginImmediateDraw()
296 if (++myImmediateModeOn > 1)
301 ClearImmediateDraw();
304 // =======================================================================
305 // function : ClearImmediateDraw
307 // =======================================================================
308 void PrsMgr_PresentationManager::ClearImmediateDraw()
310 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
312 anIter.Value()->Erase();
315 for (PrsMgr_ListOfPresentations::Iterator anIter (myViewDependentImmediateList); anIter.More(); anIter.Next())
317 anIter.Value()->Erase();
320 myImmediateList.Clear();
321 myViewDependentImmediateList.Clear();
324 // =======================================================================
325 // function : displayImmediate
326 // purpose : Handles the structures from myImmediateList and its visibility
327 // in all views of the viewer given by setting proper affinity
328 // =======================================================================
329 void PrsMgr_PresentationManager::displayImmediate (const Handle(V3d_Viewer)& theViewer)
331 for (V3d_ListOfViewIterator anActiveViewIter (theViewer->ActiveViewIterator()); anActiveViewIter.More(); anActiveViewIter.Next())
333 const Handle(Graphic3d_CView)& aView = anActiveViewIter.Value()->View();
334 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
336 const Handle(Prs3d_Presentation)& aPrs = anIter.Value();
340 Handle(Graphic3d_Structure) aViewDepPrs;
341 Handle(Prs3d_PresentationShadow) aShadowPrs = Handle(Prs3d_PresentationShadow)::DownCast (aPrs);
342 if (!aShadowPrs.IsNull() && aView->IsComputed (aShadowPrs->ParentId(), aViewDepPrs))
344 aShadowPrs.Nullify();
345 aShadowPrs = new Prs3d_PresentationShadow (myStructureManager, aViewDepPrs);
346 aShadowPrs->SetZLayer (aViewDepPrs->CStructure()->ZLayer());
347 aShadowPrs->SetClipPlanes (aViewDepPrs->ClipPlanes());
348 aShadowPrs->CStructure()->IsForHighlight = 1;
349 aShadowPrs->Highlight (aPrs->HighlightStyle());
350 myViewDependentImmediateList.Append (aShadowPrs);
352 // handles custom highlight presentations which were defined in overridden
353 // HilightOwnerWithColor method of a custom AIS objects and maintain its
354 // visibility in different views on their own
355 else if (aShadowPrs.IsNull())
361 if (!aShadowPrs->IsDisplayed())
363 aShadowPrs->CStructure()->ViewAffinity = new Graphic3d_ViewAffinity();
364 aShadowPrs->CStructure()->ViewAffinity->SetVisible (Standard_False);
365 aShadowPrs->Display();
368 Standard_Integer aViewId = aView->Identification();
369 bool isParentVisible = aShadowPrs->ParentAffinity().IsNull() ?
370 Standard_True : aShadowPrs->ParentAffinity()->IsVisible (aViewId);
371 aShadowPrs->CStructure()->ViewAffinity->SetVisible (aViewId, isParentVisible);
376 // =======================================================================
377 // function : EndImmediateDraw
379 // =======================================================================
380 void PrsMgr_PresentationManager::EndImmediateDraw (const Handle(V3d_Viewer)& theViewer)
382 if (--myImmediateModeOn > 0)
387 displayImmediate (theViewer);
390 // =======================================================================
391 // function : RedrawImmediate
392 // purpose : Clears all immediate structures and redisplays with proper
394 //=======================================================================
395 void PrsMgr_PresentationManager::RedrawImmediate (const Handle(V3d_Viewer)& theViewer)
397 if (myImmediateList.IsEmpty())
400 // Clear previously displayed structures
401 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
403 anIter.Value()->Erase();
405 for (PrsMgr_ListOfPresentations::Iterator anIter (myViewDependentImmediateList); anIter.More(); anIter.Next())
407 anIter.Value()->Erase();
409 myViewDependentImmediateList.Clear();
411 displayImmediate (theViewer);
414 // =======================================================================
415 // function : AddToImmediateList
417 //=======================================================================
418 void PrsMgr_PresentationManager::AddToImmediateList (const Handle(Prs3d_Presentation)& thePrs)
420 if (myImmediateModeOn < 1)
425 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
427 if (anIter.Value() == thePrs)
433 myImmediateList.Append (thePrs);
436 // =======================================================================
437 // function : HasPresentation
439 // =======================================================================
440 Standard_Boolean PrsMgr_PresentationManager::HasPresentation (const Handle(PrsMgr_PresentableObject)& thePrsObj,
441 const Standard_Integer theMode) const
443 if (!thePrsObj->HasOwnPresentations())
444 return Standard_False;
446 const PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
447 for (PrsMgr_Presentations::Iterator aPrsIter (aPrsList); aPrsIter.More(); aPrsIter.Next())
449 const Handle(PrsMgr_Presentation)& aPrs = aPrsIter.Value();
450 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aPrs->PresentationManager();
451 if (theMode == aPrs->Mode()
454 return Standard_True;
457 return Standard_False;
460 // =======================================================================
461 // function : Presentation
463 // =======================================================================
464 Handle(PrsMgr_Presentation) PrsMgr_PresentationManager::Presentation (const Handle(PrsMgr_PresentableObject)& thePrsObj,
465 const Standard_Integer theMode,
466 const Standard_Boolean theToCreate,
467 const Handle(PrsMgr_PresentableObject)& theSelObj) const
469 const PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
470 for (PrsMgr_Presentations::Iterator aPrsIter (aPrsList); aPrsIter.More(); aPrsIter.Next())
472 const Handle(PrsMgr_Presentation)& aPrs = aPrsIter.Value();
473 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aPrs->PresentationManager();
474 if (theMode == aPrs->Mode()
483 return Handle(PrsMgr_Presentation)();
486 Handle(PrsMgr_Presentation) aPrs = new PrsMgr_Presentation (this, thePrsObj, theMode);
487 aPrs->SetZLayer (thePrsObj->ZLayer());
488 aPrs->CStructure()->ViewAffinity = myStructureManager->ObjectAffinity (!theSelObj.IsNull() ? theSelObj : thePrsObj);
489 thePrsObj->Presentations().Append (aPrs);
490 thePrsObj->Fill (this, aPrs, theMode);
492 // set layer index accordingly to object's presentations
493 aPrs->SetUpdateStatus (Standard_False);
497 // =======================================================================
498 // function : RemovePresentation
500 // =======================================================================
501 Standard_Boolean PrsMgr_PresentationManager::RemovePresentation (const Handle(PrsMgr_PresentableObject)& thePrsObj,
502 const Standard_Integer theMode)
504 PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
505 for (PrsMgr_Presentations::Iterator aPrsIter (aPrsList); aPrsIter.More(); aPrsIter.Next())
507 const Handle(PrsMgr_Presentation)& aPrs = aPrsIter.Value();
508 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aPrs->PresentationManager();
509 if (theMode == aPrs->Mode()
512 aPrsList.Remove (aPrsIter);
513 return Standard_True;
516 return Standard_False;
519 // =======================================================================
520 // function : SetZLayer
522 // =======================================================================
523 void PrsMgr_PresentationManager::SetZLayer (const Handle(PrsMgr_PresentableObject)& thePrsObj,
524 const Graphic3d_ZLayerId theLayerId)
526 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
528 SetZLayer (anIter.Value(), theLayerId);
530 if (!thePrsObj->HasOwnPresentations())
535 thePrsObj->SetZLayer (theLayerId);
538 // =======================================================================
539 // function : GetZLayer
541 // =======================================================================
542 Graphic3d_ZLayerId PrsMgr_PresentationManager::GetZLayer (const Handle(PrsMgr_PresentableObject)& thePrsObj) const
544 return thePrsObj->ZLayer();
547 // =======================================================================
548 // function : Connect
550 // =======================================================================
551 void PrsMgr_PresentationManager::Connect (const Handle(PrsMgr_PresentableObject)& thePrsObject,
552 const Handle(PrsMgr_PresentableObject)& theOtherObject,
553 const Standard_Integer theMode,
554 const Standard_Integer theOtherMode)
556 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObject, theMode, Standard_True);
557 Handle(PrsMgr_Presentation) aPrsOther = Presentation (theOtherObject, theOtherMode, Standard_True);
558 aPrs->Connect (aPrsOther.get(), Graphic3d_TOC_DESCENDANT);
561 // =======================================================================
562 // function : Transform
564 // =======================================================================
565 void PrsMgr_PresentationManager::Transform (const Handle(PrsMgr_PresentableObject)& thePrsObj,
566 const Handle(Geom_Transformation)& theTransformation,
567 const Standard_Integer theMode)
569 Presentation (thePrsObj, theMode)->SetTransformation (theTransformation);
572 // =======================================================================
575 // =======================================================================
576 void PrsMgr_PresentationManager::Color (const Handle(PrsMgr_PresentableObject)& thePrsObj,
577 const Handle(Prs3d_Drawer)& theStyle,
578 const Standard_Integer theMode,
579 const Handle(PrsMgr_PresentableObject)& theSelObj,
580 const Standard_Integer theImmediateStructLayerId)
582 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
584 Color (anIter.Value(), theStyle, theMode, NULL, theImmediateStructLayerId);
586 if (!thePrsObj->HasOwnPresentations())
591 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode, Standard_True, theSelObj);
592 if (aPrs->MustBeUpdated())
594 Update (thePrsObj, theMode);
597 if (myImmediateModeOn > 0)
599 Handle(Prs3d_PresentationShadow) aShadow = new Prs3d_PresentationShadow (myStructureManager, aPrs);
600 aShadow->SetZLayer (theImmediateStructLayerId);
601 aShadow->SetClipPlanes (aPrs->ClipPlanes());
602 aShadow->CStructure()->IsForHighlight = 1;
603 aShadow->Highlight (theStyle);
604 AddToImmediateList (aShadow);
608 aPrs->Highlight (theStyle);
614 // =======================================================================
615 // function : updatePrsTransformation
616 // purpose : Internal function that scans thePrsList for shadow presentations
617 // and applies transformation theTrsf to them in case if parent ID
618 // of shadow presentation is equal to theRefId
619 // =======================================================================
620 void updatePrsTransformation (const PrsMgr_ListOfPresentations& thePrsList,
621 const Standard_Integer theRefId,
622 const Handle(Geom_Transformation)& theTrsf)
624 for (PrsMgr_ListOfPresentations::Iterator anIter (thePrsList); anIter.More(); anIter.Next())
626 const Handle(Prs3d_Presentation)& aPrs = anIter.Value();
630 Handle(Prs3d_PresentationShadow) aShadowPrs = Handle(Prs3d_PresentationShadow)::DownCast (aPrs);
631 if (aShadowPrs.IsNull() || aShadowPrs->ParentId() != theRefId)
634 aShadowPrs->CStructure()->SetTransformation (theTrsf);
639 // =======================================================================
640 // function : UpdateHighlightTrsf
642 // =======================================================================
643 void PrsMgr_PresentationManager::UpdateHighlightTrsf (const Handle(V3d_Viewer)& theViewer,
644 const Handle(PrsMgr_PresentableObject)& theObj,
645 const Standard_Integer theMode,
646 const Handle(PrsMgr_PresentableObject)& theSelObj)
651 Handle(PrsMgr_Presentation) aPrs = Presentation (!theSelObj.IsNull() ? theSelObj : theObj, theMode, Standard_False);
657 Handle(Geom_Transformation) aTrsf = theObj->LocalTransformationGeom();
658 const Standard_Integer aParentId = aPrs->CStructure()->Id;
659 updatePrsTransformation (myImmediateList, aParentId, aTrsf);
661 if (!myViewDependentImmediateList.IsEmpty())
663 for (V3d_ListOfViewIterator anActiveViewIter (theViewer->ActiveViewIterator()); anActiveViewIter.More(); anActiveViewIter.Next())
665 const Handle(Graphic3d_CView)& aView = anActiveViewIter.Value()->View();
666 Handle(Graphic3d_Structure) aViewDepParentPrs;
667 if (aView->IsComputed (aParentId, aViewDepParentPrs))
669 updatePrsTransformation (myViewDependentImmediateList,
670 aViewDepParentPrs->CStructure()->Id,