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.ixx>
17 #include <Graphic3d_GraphicDriver.hxx>
18 #include <Prs3d_PresentationShadow.hxx>
19 #include <PrsMgr_PresentableObject.hxx>
20 #include <PrsMgr_Presentation.hxx>
21 #include <PrsMgr_Presentations.hxx>
22 #include <PrsMgr_ModedPresentation.hxx>
23 #include <TColStd_ListIteratorOfListOfTransient.hxx>
24 #include <V3d_View.hxx>
25 #include <Visual3d_View.hxx>
26 #include <Visual3d_Layer.hxx>
28 // =======================================================================
29 // function : PrsMgr_PresentationManager
31 // =======================================================================
32 PrsMgr_PresentationManager::PrsMgr_PresentationManager (const Handle(Graphic3d_StructureManager)& theStructureManager)
33 : myStructureManager (theStructureManager),
39 // =======================================================================
42 // =======================================================================
43 void PrsMgr_PresentationManager::Display (const Handle(PrsMgr_PresentableObject)& thePrsObj,
44 const Standard_Integer theMode)
46 if (thePrsObj->HasOwnPresentations())
48 if (!HasPresentation (thePrsObj, theMode))
50 AddPresentation (thePrsObj, theMode);
53 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
55 if (aPrs.IsNull()) return;
57 if (aPrs->MustBeUpdated())
59 Update (thePrsObj, theMode);
62 if (myImmediateModeOn > 0)
64 AddToImmediateList (aPrs->Presentation());
73 thePrsObj->Compute (this, Handle(Prs3d_Presentation)(), theMode);
76 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
78 Display (anIter.Value(), theMode);
82 // =======================================================================
85 // =======================================================================
86 void PrsMgr_PresentationManager::Erase (const Handle(PrsMgr_PresentableObject)& thePrsObj,
87 const Standard_Integer theMode)
89 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
91 Erase (anIter.Value(), theMode);
94 if (HasPresentation (thePrsObj, theMode))
96 Presentation (thePrsObj, theMode)->Erase();
97 RemovePresentation (thePrsObj, theMode);
101 // =======================================================================
104 // =======================================================================
105 void PrsMgr_PresentationManager::Clear (const Handle(PrsMgr_PresentableObject)& thePrsObj,
106 const Standard_Integer theMode)
108 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
110 Clear (anIter.Value(), theMode);
113 if (HasPresentation (thePrsObj, theMode))
115 Presentation (thePrsObj, theMode)->Clear();
119 // =======================================================================
120 // function : SetVisibility
122 // =======================================================================
123 void PrsMgr_PresentationManager::SetVisibility (const Handle(PrsMgr_PresentableObject)& thePrsObj,
124 const Standard_Integer theMode,
125 const Standard_Boolean theValue)
127 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
129 SetVisibility (anIter.Value(), theMode, theValue);
131 if (!thePrsObj->HasOwnPresentations())
136 Presentation (thePrsObj, theMode)->SetVisible (theValue);
139 // =======================================================================
140 // function : Highlight
142 // =======================================================================
143 void PrsMgr_PresentationManager::Highlight (const Handle(PrsMgr_PresentableObject)& thePrsObj,
144 const Standard_Integer theMode)
146 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
148 Highlight (anIter.Value(), theMode);
150 if (!thePrsObj->HasOwnPresentations())
155 if (!HasPresentation (thePrsObj, theMode))
157 AddPresentation (thePrsObj, theMode);
160 if (!HasPresentation (thePrsObj, theMode)) return;
162 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
163 if (aPrs->MustBeUpdated())
165 Update (thePrsObj, theMode);
168 if (myImmediateModeOn > 0)
170 Handle(Prs3d_PresentationShadow) aShadow = new Prs3d_PresentationShadow (myStructureManager, aPrs->Presentation());
171 aShadow->Highlight();
172 AddToImmediateList (aShadow);
180 // =======================================================================
181 // function : Unhighlight
183 // =======================================================================
184 void PrsMgr_PresentationManager::Unhighlight (const Handle(PrsMgr_PresentableObject)& thePrsObj,
185 const Standard_Integer theMode)
187 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
189 Unhighlight (anIter.Value(), theMode);
192 if (HasPresentation (thePrsObj, theMode))
194 Presentation (thePrsObj, theMode)->Unhighlight();
198 // =======================================================================
199 // function : SetDisplayPriority
201 // =======================================================================
202 void PrsMgr_PresentationManager::SetDisplayPriority (const Handle(PrsMgr_PresentableObject)& thePrsObj,
203 const Standard_Integer theMode,
204 const Standard_Integer theNewPrior) const
206 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
208 SetDisplayPriority (anIter.Value(), theMode, theNewPrior);
211 if (HasPresentation (thePrsObj, theMode))
213 Presentation (thePrsObj, theMode)->SetDisplayPriority (theNewPrior);
217 // =======================================================================
218 // function : DisplayPriority
220 // =======================================================================
221 Standard_Integer PrsMgr_PresentationManager::DisplayPriority (const Handle(PrsMgr_PresentableObject)& thePrsObj,
222 const Standard_Integer theMode) const
224 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
226 Standard_Integer aPriority = DisplayPriority (anIter.Value(), theMode);
233 return HasPresentation (thePrsObj, theMode)
234 ? Presentation (thePrsObj, theMode)->DisplayPriority()
238 // =======================================================================
239 // function : IsDisplayed
241 // =======================================================================
242 Standard_Boolean PrsMgr_PresentationManager::IsDisplayed (const Handle(PrsMgr_PresentableObject)& thePrsObj,
243 const Standard_Integer theMode) const
245 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
247 if (IsDisplayed (anIter.Value(), theMode))
249 return Standard_True;
253 return HasPresentation (thePrsObj, theMode)
254 && Presentation (thePrsObj, theMode)->IsDisplayed();
257 // =======================================================================
258 // function : IsHighlighted
260 // =======================================================================
261 Standard_Boolean PrsMgr_PresentationManager::IsHighlighted (const Handle(PrsMgr_PresentableObject)& thePrsObj,
262 const Standard_Integer theMode) const
264 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
266 if (IsHighlighted (anIter.Value(), theMode))
268 return Standard_True;
272 return HasPresentation (thePrsObj, theMode)
273 && Presentation (thePrsObj, theMode)->IsHighlighted();
276 // =======================================================================
279 // =======================================================================
280 void PrsMgr_PresentationManager::Update (const Handle(PrsMgr_PresentableObject)& thePrsObj,
281 const Standard_Integer theMode) const
283 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
285 Update (anIter.Value(), theMode);
287 if (!HasPresentation(thePrsObj, theMode))
292 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
296 thePrsObj->Fill (this, aPrs, theMode);
297 aPrs->SetUpdateStatus (Standard_False);
301 // =======================================================================
302 // function : BeginImmediateDraw
304 // =======================================================================
305 void PrsMgr_PresentationManager::BeginImmediateDraw()
307 if (++myImmediateModeOn > 1)
312 ClearImmediateDraw();
315 // =======================================================================
316 // function : ClearImmediateDraw
318 // =======================================================================
319 void PrsMgr_PresentationManager::ClearImmediateDraw()
321 if (myImmediateView.IsNull())
323 myImmediateList.Clear();
327 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
329 myImmediateView->View()->EraseImmediate (anIter.Value());
332 myImmediateList.Clear();
333 myImmediateView.Nullify();
336 // =======================================================================
337 // function : EndImmediateDraw
339 // =======================================================================
340 void PrsMgr_PresentationManager::EndImmediateDraw (const Handle(V3d_View)& theView)
342 if (--myImmediateModeOn > 0)
347 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
349 theView->View()->DisplayImmediate (anIter.Value(), Standard_True);
351 if (!myImmediateList.IsEmpty())
353 myImmediateView = theView;
357 // =======================================================================
358 // function : AddToImmediateList
360 //=======================================================================
361 void PrsMgr_PresentationManager::AddToImmediateList (const Handle(Prs3d_Presentation)& thePrs)
363 if (myImmediateModeOn < 1)
368 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
370 if (anIter.Value() == thePrs)
376 myImmediateList.Append (thePrs);
379 // =======================================================================
380 // function : HasPresentation
382 // =======================================================================
383 Standard_Boolean PrsMgr_PresentationManager::HasPresentation (const Handle(PrsMgr_PresentableObject)& thePrsObj,
384 const Standard_Integer theMode) const
386 if (!thePrsObj->HasOwnPresentations())
387 return Standard_False;
389 const PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
390 for (Standard_Integer aPrsIter = 1; aPrsIter <= aPrsList.Length(); ++aPrsIter)
392 const PrsMgr_ModedPresentation& aModedPrs = aPrsList.Value (aPrsIter);
393 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aModedPrs.Presentation()->PresentationManager();
394 if (theMode == aModedPrs.Mode()
397 return Standard_True;
400 return Standard_False;
403 // =======================================================================
404 // function : Presentation
406 // =======================================================================
407 Handle(PrsMgr_Presentation) PrsMgr_PresentationManager::Presentation (const Handle(PrsMgr_PresentableObject)& thePrsObj,
408 const Standard_Integer theMode) const
410 const PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
411 if (aPrsList.IsEmpty())
413 return Handle(PrsMgr_Presentation)();
416 for (Standard_Integer aPrsIter = 1; aPrsIter <= aPrsList.Length(); ++aPrsIter)
418 const PrsMgr_ModedPresentation& aModedPrs = aPrsList.Value (aPrsIter);
419 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aModedPrs.Presentation()->PresentationManager();
420 if (theMode == aModedPrs.Mode()
423 return aModedPrs.Presentation();
427 // To be changed within dedicated patch
428 ///return Handle(PrsMgr_Presentation)();
429 return aPrsList.Last().Presentation();
432 // =======================================================================
433 // function : AddPresentation
435 // =======================================================================
436 void PrsMgr_PresentationManager::AddPresentation (const Handle(PrsMgr_PresentableObject)& thePrsObj,
437 const Standard_Integer theMode)
439 Handle(PrsMgr_Presentation) aPrs = new PrsMgr_Presentation (this, thePrsObj);
440 thePrsObj->Presentations().Append (PrsMgr_ModedPresentation (aPrs, theMode));
441 thePrsObj->Fill (this, aPrs, theMode);
443 // set layer index accordingly to object's presentations
444 const Standard_Integer aZLayerId = GetZLayer (thePrsObj);
447 aPrs->SetZLayer (aZLayerId);
449 aPrs->SetUpdateStatus (Standard_False);
452 // =======================================================================
453 // function : RemovePresentation
455 // =======================================================================
456 void PrsMgr_PresentationManager::RemovePresentation (const Handle(PrsMgr_PresentableObject)& thePrsObj,
457 const Standard_Integer theMode)
459 PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
460 for (Standard_Integer aPrsIter = 1; aPrsIter <= aPrsList.Length(); ++aPrsIter)
462 if (theMode == aPrsList (aPrsIter).Mode())
463 // && this == aPrsMgr) ??
465 aPrsList.Remove (aPrsIter);
471 // =======================================================================
472 // function : SetZLayer
474 // =======================================================================
475 void PrsMgr_PresentationManager::SetZLayer (const Handle(PrsMgr_PresentableObject)& thePrsObj,
476 const Standard_Integer theLayerId)
478 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
480 SetZLayer (anIter.Value(), theLayerId);
482 if (!thePrsObj->HasOwnPresentations())
486 PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
487 for (Standard_Integer aPrsIter = 1; aPrsIter <= aPrsList.Length(); ++aPrsIter)
489 Handle(PrsMgr_Presentation) aPrs = aPrsList.ChangeValue (aPrsIter).Presentation();
490 if (aPrs->PresentationManager() == this)
492 aPrs->SetZLayer (theLayerId);
497 // =======================================================================
498 // function : GetZLayer
500 // =======================================================================
501 Standard_Integer PrsMgr_PresentationManager::GetZLayer (const Handle(PrsMgr_PresentableObject)& thePrsObj) const
503 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
505 Standard_Integer aLayer = GetZLayer (anIter.Value());
511 if (!thePrsObj->HasOwnPresentations())
515 const PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
516 for (Standard_Integer aPrsIter = 1; aPrsIter <= aPrsList.Length(); ++aPrsIter)
518 Handle(PrsMgr_Presentation) aPrs = aPrsList.Value (aPrsIter).Presentation();
519 if (aPrs->PresentationManager() == this)
521 return aPrs->GetZLayer();
527 // =======================================================================
528 // function : Connect
530 // =======================================================================
531 void PrsMgr_PresentationManager::Connect (const Handle(PrsMgr_PresentableObject)& thePrsObject,
532 const Handle(PrsMgr_PresentableObject)& theOtherObject,
533 const Standard_Integer theMode,
534 const Standard_Integer theOtherMode)
536 if (!HasPresentation (thePrsObject, theMode))
538 AddPresentation (thePrsObject, theMode);
540 if (!HasPresentation (theOtherObject, theOtherMode))
542 AddPresentation (theOtherObject, theOtherMode);
544 Presentation (thePrsObject, theMode)->Connect (Presentation (theOtherObject, theMode));
547 // =======================================================================
548 // function : Transform
550 // =======================================================================
551 void PrsMgr_PresentationManager::Transform (const Handle(PrsMgr_PresentableObject)& thePrsObj,
552 const Handle(Geom_Transformation)& theTransformation,
553 const Standard_Integer theMode)
555 Presentation (thePrsObj, theMode)->Transform (theTransformation);
559 // =======================================================================
562 // =======================================================================
563 void PrsMgr_PresentationManager::Color (const Handle(PrsMgr_PresentableObject)& thePrsObj,
564 const Quantity_NameOfColor theColor,
565 const Standard_Integer theMode)
567 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
569 Color (anIter.Value(), theColor, theMode);
571 if (!thePrsObj->HasOwnPresentations())
576 if (!HasPresentation (thePrsObj, theMode))
578 AddPresentation (thePrsObj, theMode);
581 if (!HasPresentation (thePrsObj, theMode)) return;
583 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
584 if (aPrs->MustBeUpdated())
586 Update (thePrsObj, theMode);
589 if (myImmediateModeOn > 0)
591 Handle(Prs3d_PresentationShadow) aShadow = new Prs3d_PresentationShadow (myStructureManager, aPrs->Presentation());
592 aShadow->Color (theColor);
593 AddToImmediateList (aShadow);
597 aPrs->Color (theColor);
601 // =======================================================================
602 // function : BoundBox
604 // =======================================================================
605 void PrsMgr_PresentationManager::BoundBox (const Handle(PrsMgr_PresentableObject)& thePrsObject,
606 const Standard_Integer theMode)
608 if (!HasPresentation (thePrsObject, theMode))
610 AddPresentation (thePrsObject, theMode);
612 else if (Presentation (thePrsObject, theMode)->MustBeUpdated())
614 Update (thePrsObject, theMode);
616 Presentation (thePrsObject, theMode)->BoundBox();
619 // =======================================================================
620 // function : SetShadingAspect
622 // =======================================================================
623 void PrsMgr_PresentationManager::SetShadingAspect (const Handle(PrsMgr_PresentableObject)& thePrsObject,
624 const Quantity_NameOfColor theColor,
625 const Graphic3d_NameOfMaterial theMaterial,
626 const Standard_Integer theMode)
628 Handle(Prs3d_ShadingAspect) anAspect = new Prs3d_ShadingAspect();
629 anAspect->SetColor (theColor);
630 anAspect->SetMaterial (theMaterial);
631 SetShadingAspect (thePrsObject, anAspect, theMode);
634 // =======================================================================
635 // function : SetShadingAspect
637 // =======================================================================
638 void PrsMgr_PresentationManager::SetShadingAspect (const Handle(PrsMgr_PresentableObject)& thePrsObject,
639 const Handle(Prs3d_ShadingAspect)& theShadingAspect,
640 const Standard_Integer theMode)
642 if (HasPresentation (thePrsObject, theMode))
644 Presentation (thePrsObject, theMode)->SetShadingAspect (theShadingAspect);