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_ModedPresentation.hxx>
23 #include <PrsMgr_PresentableObject.hxx>
24 #include <PrsMgr_Presentation.hxx>
25 #include <PrsMgr_Presentations.hxx>
26 #include <Standard_NoSuchObject.hxx>
27 #include <Standard_Type.hxx>
28 #include <TColStd_ListIteratorOfListOfTransient.hxx>
29 #include <V3d_View.hxx>
31 IMPLEMENT_STANDARD_RTTIEXT(PrsMgr_PresentationManager, Standard_Transient)
33 // =======================================================================
34 // function : PrsMgr_PresentationManager
36 // =======================================================================
37 PrsMgr_PresentationManager::PrsMgr_PresentationManager (const Handle(Graphic3d_StructureManager)& theStructureManager)
38 : myStructureManager (theStructureManager),
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 (PrsMgr_Presentations::Iterator anIt (aPrsList); anIt.More();)
94 const PrsMgr_ModedPresentation& aModedPrs = anIt.Value();
95 if (aModedPrs.Presentation().IsNull())
101 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aModedPrs.Presentation()->PresentationManager();
102 if ((theMode == aModedPrs.Mode() || theMode == -1)
103 && (this == aPrsMgr))
105 aModedPrs.Presentation()->Erase();
107 aPrsList.Remove (anIt);
121 // =======================================================================
124 // =======================================================================
125 void PrsMgr_PresentationManager::Clear (const Handle(PrsMgr_PresentableObject)& thePrsObj,
126 const Standard_Integer theMode)
128 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
130 Clear (anIter.Value(), theMode);
133 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
140 // =======================================================================
141 // function : SetVisibility
143 // =======================================================================
144 void PrsMgr_PresentationManager::SetVisibility (const Handle(PrsMgr_PresentableObject)& thePrsObj,
145 const Standard_Integer theMode,
146 const Standard_Boolean theValue)
148 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
150 SetVisibility (anIter.Value(), theMode, theValue);
152 if (!thePrsObj->HasOwnPresentations())
157 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
160 aPrs->SetVisible (theValue);
164 // =======================================================================
165 // function : Unhighlight
167 // =======================================================================
168 void PrsMgr_PresentationManager::Unhighlight (const Handle(PrsMgr_PresentableObject)& thePrsObj)
170 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
172 Unhighlight (anIter.Value());
175 const PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
176 for (Standard_Integer aPrsIter = 1; aPrsIter <= aPrsList.Length(); ++aPrsIter)
178 const PrsMgr_ModedPresentation& aModedPrs = aPrsList.Value (aPrsIter);
179 const Handle(PrsMgr_Presentation)& aPrs = aModedPrs.Presentation();
180 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aPrs->PresentationManager();
182 && aPrs->IsHighlighted())
189 // =======================================================================
190 // function : SetDisplayPriority
192 // =======================================================================
193 void PrsMgr_PresentationManager::SetDisplayPriority (const Handle(PrsMgr_PresentableObject)& thePrsObj,
194 const Standard_Integer theMode,
195 const Standard_Integer theNewPrior) const
197 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
199 SetDisplayPriority (anIter.Value(), theMode, theNewPrior);
202 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
205 aPrs->SetDisplayPriority (theNewPrior);
209 // =======================================================================
210 // function : DisplayPriority
212 // =======================================================================
213 Standard_Integer PrsMgr_PresentationManager::DisplayPriority (const Handle(PrsMgr_PresentableObject)& thePrsObj,
214 const Standard_Integer theMode) const
216 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
218 Standard_Integer aPriority = DisplayPriority (anIter.Value(), theMode);
225 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
226 return !aPrs.IsNull()
227 ? aPrs->DisplayPriority()
231 // =======================================================================
232 // function : IsDisplayed
234 // =======================================================================
235 Standard_Boolean PrsMgr_PresentationManager::IsDisplayed (const Handle(PrsMgr_PresentableObject)& thePrsObj,
236 const Standard_Integer theMode) const
238 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
240 if (IsDisplayed (anIter.Value(), theMode))
242 return Standard_True;
246 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
247 return !aPrs.IsNull()
248 && aPrs->IsDisplayed();
251 // =======================================================================
252 // function : IsHighlighted
254 // =======================================================================
255 Standard_Boolean PrsMgr_PresentationManager::IsHighlighted (const Handle(PrsMgr_PresentableObject)& thePrsObj,
256 const Standard_Integer theMode) const
258 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
260 if (IsHighlighted (anIter.Value(), theMode))
262 return Standard_True;
266 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
267 return !aPrs.IsNull()
268 && aPrs->IsHighlighted();
271 // =======================================================================
274 // =======================================================================
275 void PrsMgr_PresentationManager::Update (const Handle(PrsMgr_PresentableObject)& thePrsObj,
276 const Standard_Integer theMode) const
278 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
280 Update (anIter.Value(), theMode);
283 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
287 thePrsObj->Fill (this, aPrs, theMode);
288 aPrs->SetUpdateStatus (Standard_False);
292 // =======================================================================
293 // function : BeginImmediateDraw
295 // =======================================================================
296 void PrsMgr_PresentationManager::BeginImmediateDraw()
298 if (++myImmediateModeOn > 1)
303 ClearImmediateDraw();
306 // =======================================================================
307 // function : ClearImmediateDraw
309 // =======================================================================
310 void PrsMgr_PresentationManager::ClearImmediateDraw()
312 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
314 anIter.Value()->Erase();
317 for (PrsMgr_ListOfPresentations::Iterator anIter (myViewDependentImmediateList); anIter.More(); anIter.Next())
319 anIter.Value()->Erase();
322 myImmediateList.Clear();
323 myViewDependentImmediateList.Clear();
326 // =======================================================================
327 // function : displayImmediate
328 // purpose : Handles the structures from myImmediateList and its visibility
329 // in all views of the viewer given by setting proper affinity
330 // =======================================================================
331 void PrsMgr_PresentationManager::displayImmediate (const Handle(V3d_Viewer)& theViewer)
333 for (V3d_ListOfViewIterator anActiveViewIter (theViewer->ActiveViewIterator()); anActiveViewIter.More(); anActiveViewIter.Next())
335 const Handle(Graphic3d_CView)& aView = anActiveViewIter.Value()->View();
336 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
338 const Handle(Prs3d_Presentation)& aPrs = anIter.Value();
342 Handle(Graphic3d_Structure) aViewDepPrs;
343 Handle(Prs3d_PresentationShadow) aShadowPrs = Handle(Prs3d_PresentationShadow)::DownCast (aPrs);
344 if (!aShadowPrs.IsNull() && aView->IsComputed (aShadowPrs->ParentId(), aViewDepPrs))
346 aShadowPrs.Nullify();
347 aShadowPrs = new Prs3d_PresentationShadow (myStructureManager,
348 Handle(Prs3d_Presentation)::DownCast (aViewDepPrs));
349 aShadowPrs->SetZLayer (aViewDepPrs->CStructure()->ZLayer());
350 aShadowPrs->SetClipPlanes (aViewDepPrs->ClipPlanes());
351 aShadowPrs->CStructure()->IsForHighlight = 1;
352 aShadowPrs->Highlight (aPrs->HighlightStyle());
353 myViewDependentImmediateList.Append (aShadowPrs);
355 // handles custom highlight presentations which were defined in overridden
356 // HilightOwnerWithColor method of a custom AIS objects and maintain its
357 // visibility in different views on their own
358 else if (aShadowPrs.IsNull())
364 if (!aShadowPrs->IsDisplayed())
366 aShadowPrs->CStructure()->ViewAffinity = new Graphic3d_ViewAffinity();
367 aShadowPrs->CStructure()->ViewAffinity->SetVisible (Standard_False);
368 aShadowPrs->Display();
371 Standard_Integer aViewId = aView->Identification();
372 bool isParentVisible = aShadowPrs->ParentAffinity().IsNull() ?
373 Standard_True : aShadowPrs->ParentAffinity()->IsVisible (aViewId);
374 aShadowPrs->CStructure()->ViewAffinity->SetVisible (aViewId, isParentVisible);
379 // =======================================================================
380 // function : EndImmediateDraw
382 // =======================================================================
383 void PrsMgr_PresentationManager::EndImmediateDraw (const Handle(V3d_Viewer)& theViewer)
385 if (--myImmediateModeOn > 0)
390 displayImmediate (theViewer);
393 // =======================================================================
394 // function : RedrawImmediate
395 // purpose : Clears all immediate structures and redisplays with proper
397 //=======================================================================
398 void PrsMgr_PresentationManager::RedrawImmediate (const Handle(V3d_Viewer)& theViewer)
400 if (myImmediateList.IsEmpty())
403 // Clear previously displayed structures
404 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
406 anIter.Value()->Erase();
408 for (PrsMgr_ListOfPresentations::Iterator anIter (myViewDependentImmediateList); anIter.More(); anIter.Next())
410 anIter.Value()->Erase();
412 myViewDependentImmediateList.Clear();
414 displayImmediate (theViewer);
417 // =======================================================================
418 // function : AddToImmediateList
420 //=======================================================================
421 void PrsMgr_PresentationManager::AddToImmediateList (const Handle(Prs3d_Presentation)& thePrs)
423 if (myImmediateModeOn < 1)
428 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
430 if (anIter.Value() == thePrs)
436 myImmediateList.Append (thePrs);
439 // =======================================================================
440 // function : HasPresentation
442 // =======================================================================
443 Standard_Boolean PrsMgr_PresentationManager::HasPresentation (const Handle(PrsMgr_PresentableObject)& thePrsObj,
444 const Standard_Integer theMode) const
446 if (!thePrsObj->HasOwnPresentations())
447 return Standard_False;
449 const PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
450 for (Standard_Integer aPrsIter = 1; aPrsIter <= aPrsList.Length(); ++aPrsIter)
452 const PrsMgr_ModedPresentation& aModedPrs = aPrsList.Value (aPrsIter);
453 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aModedPrs.Presentation()->PresentationManager();
454 if (theMode == aModedPrs.Mode()
457 return Standard_True;
460 return Standard_False;
463 // =======================================================================
464 // function : Presentation
466 // =======================================================================
467 Handle(PrsMgr_Presentation) PrsMgr_PresentationManager::Presentation (const Handle(PrsMgr_PresentableObject)& thePrsObj,
468 const Standard_Integer theMode,
469 const Standard_Boolean theToCreate,
470 const Handle(PrsMgr_PresentableObject)& theSelObj) const
472 const PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
473 for (Standard_Integer aPrsIter = 1; aPrsIter <= aPrsList.Length(); ++aPrsIter)
475 const PrsMgr_ModedPresentation& aModedPrs = aPrsList.Value (aPrsIter);
476 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aModedPrs.Presentation()->PresentationManager();
477 if (theMode == aModedPrs.Mode()
480 return aModedPrs.Presentation();
486 return Handle(PrsMgr_Presentation)();
489 Handle(PrsMgr_Presentation) aPrs = new PrsMgr_Presentation (this, thePrsObj);
490 aPrs->SetZLayer (thePrsObj->ZLayer());
491 aPrs->Presentation()->CStructure()->ViewAffinity = myStructureManager->ObjectAffinity (!theSelObj.IsNull() ? theSelObj : thePrsObj);
492 thePrsObj->Presentations().Append (PrsMgr_ModedPresentation (aPrs, theMode));
493 thePrsObj->Fill (this, aPrs, theMode);
495 // set layer index accordingly to object's presentations
496 aPrs->SetUpdateStatus (Standard_False);
500 // =======================================================================
501 // function : RemovePresentation
503 // =======================================================================
504 Standard_Boolean PrsMgr_PresentationManager::RemovePresentation (const Handle(PrsMgr_PresentableObject)& thePrsObj,
505 const Standard_Integer theMode)
507 PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
508 for (Standard_Integer aPrsIter = 1; aPrsIter <= aPrsList.Length(); ++aPrsIter)
510 const PrsMgr_ModedPresentation& aModedPrs = aPrsList.Value (aPrsIter);
511 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aModedPrs.Presentation()->PresentationManager();
512 if (theMode == aPrsList (aPrsIter).Mode()
515 aPrsList.Remove (aPrsIter);
516 return Standard_True;
519 return Standard_False;
522 // =======================================================================
523 // function : SetZLayer
525 // =======================================================================
526 void PrsMgr_PresentationManager::SetZLayer (const Handle(PrsMgr_PresentableObject)& thePrsObj,
527 const Graphic3d_ZLayerId theLayerId)
529 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
531 SetZLayer (anIter.Value(), theLayerId);
533 if (!thePrsObj->HasOwnPresentations())
538 thePrsObj->SetZLayer (theLayerId);
541 // =======================================================================
542 // function : GetZLayer
544 // =======================================================================
545 Graphic3d_ZLayerId PrsMgr_PresentationManager::GetZLayer (const Handle(PrsMgr_PresentableObject)& thePrsObj) const
547 return thePrsObj->ZLayer();
550 // =======================================================================
551 // function : Connect
553 // =======================================================================
554 void PrsMgr_PresentationManager::Connect (const Handle(PrsMgr_PresentableObject)& thePrsObject,
555 const Handle(PrsMgr_PresentableObject)& theOtherObject,
556 const Standard_Integer theMode,
557 const Standard_Integer theOtherMode)
559 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObject, theMode, Standard_True);
560 Handle(PrsMgr_Presentation) aPrsOther = Presentation (theOtherObject, theOtherMode, Standard_True);
561 aPrs->Connect (aPrsOther);
564 // =======================================================================
565 // function : Transform
567 // =======================================================================
568 void PrsMgr_PresentationManager::Transform (const Handle(PrsMgr_PresentableObject)& thePrsObj,
569 const Handle(Geom_Transformation)& theTransformation,
570 const Standard_Integer theMode)
572 Presentation (thePrsObj, theMode)->SetTransformation (theTransformation);
575 // =======================================================================
578 // =======================================================================
579 void PrsMgr_PresentationManager::Color (const Handle(PrsMgr_PresentableObject)& thePrsObj,
580 const Handle(Prs3d_Drawer)& theStyle,
581 const Standard_Integer theMode,
582 const Handle(PrsMgr_PresentableObject)& theSelObj,
583 const Standard_Integer theImmediateStructLayerId)
585 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
587 Color (anIter.Value(), theStyle, theMode, NULL, theImmediateStructLayerId);
589 if (!thePrsObj->HasOwnPresentations())
594 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode, Standard_True, theSelObj);
595 if (aPrs->MustBeUpdated())
597 Update (thePrsObj, theMode);
600 if (myImmediateModeOn > 0)
602 Handle(Prs3d_PresentationShadow) aShadow = new Prs3d_PresentationShadow (myStructureManager, aPrs->Presentation());
603 aShadow->SetZLayer (theImmediateStructLayerId);
604 aShadow->SetClipPlanes (aPrs->Presentation()->ClipPlanes());
605 aShadow->CStructure()->IsForHighlight = 1;
606 aShadow->Highlight (theStyle);
607 AddToImmediateList (aShadow);
611 aPrs->Highlight (theStyle);
617 // =======================================================================
618 // function : updatePrsTransformation
619 // purpose : Internal function that scans thePrsList for shadow presentations
620 // and applies transformation theTrsf to them in case if parent ID
621 // of shadow presentation is equal to theRefId
622 // =======================================================================
623 void updatePrsTransformation (const PrsMgr_ListOfPresentations& thePrsList,
624 const Standard_Integer theRefId,
625 const Handle(Geom_Transformation)& theTrsf)
627 for (PrsMgr_ListOfPresentations::Iterator anIter (thePrsList); anIter.More(); anIter.Next())
629 const Handle(Prs3d_Presentation)& aPrs = anIter.Value();
633 Handle(Prs3d_PresentationShadow) aShadowPrs = Handle(Prs3d_PresentationShadow)::DownCast (aPrs);
634 if (aShadowPrs.IsNull() || aShadowPrs->ParentId() != theRefId)
637 aShadowPrs->CStructure()->SetTransformation (theTrsf);
642 // =======================================================================
643 // function : UpdateHighlightTrsf
645 // =======================================================================
646 void PrsMgr_PresentationManager::UpdateHighlightTrsf (const Handle(V3d_Viewer)& theViewer,
647 const Handle(PrsMgr_PresentableObject)& theObj,
648 const Standard_Integer theMode,
649 const Handle(PrsMgr_PresentableObject)& theSelObj)
654 Handle(PrsMgr_Presentation) aPrs = Presentation (!theSelObj.IsNull() ? theSelObj : theObj, theMode, Standard_False);
660 Handle(Geom_Transformation) aTrsf = theObj->LocalTransformationGeom();
661 const Standard_Integer aParentId = aPrs->Presentation()->CStructure()->Id;
662 updatePrsTransformation (myImmediateList, aParentId, aTrsf);
664 if (!myViewDependentImmediateList.IsEmpty())
666 for (V3d_ListOfViewIterator anActiveViewIter (theViewer->ActiveViewIterator()); anActiveViewIter.More(); anActiveViewIter.Next())
668 const Handle(Graphic3d_CView)& aView = anActiveViewIter.Value()->View();
669 Handle(Graphic3d_Structure) aViewDepParentPrs;
670 if (aView->IsComputed (aParentId, aViewDepParentPrs))
672 updatePrsTransformation (myViewDependentImmediateList,
673 aViewDepParentPrs->CStructure()->Id,