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 (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 Presentation (thePrsObj, theMode)->SetVisible (theValue);
160 // =======================================================================
161 // function : Highlight
163 // =======================================================================
164 void PrsMgr_PresentationManager::Highlight (const Handle(PrsMgr_PresentableObject)& thePrsObj,
165 const Standard_Integer theMode)
167 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
169 Highlight (anIter.Value(), theMode);
171 if (!thePrsObj->HasOwnPresentations())
176 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode, Standard_True);
177 if (aPrs->MustBeUpdated())
179 Update (thePrsObj, theMode);
182 if (myImmediateModeOn > 0)
184 Handle(Prs3d_PresentationShadow) aShadow = new Prs3d_PresentationShadow (myStructureManager, aPrs->Presentation());
185 aShadow->Highlight (Aspect_TOHM_COLOR, mySelectionColor);
186 AddToImmediateList (aShadow);
190 aPrs->Highlight (Aspect_TOHM_COLOR, mySelectionColor);
194 // =======================================================================
195 // function : Unhighlight
197 // =======================================================================
198 void PrsMgr_PresentationManager::Unhighlight (const Handle(PrsMgr_PresentableObject)& thePrsObj,
199 const Standard_Integer theMode)
201 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
203 Unhighlight (anIter.Value(), theMode);
206 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
213 // =======================================================================
214 // function : SetDisplayPriority
216 // =======================================================================
217 void PrsMgr_PresentationManager::SetDisplayPriority (const Handle(PrsMgr_PresentableObject)& thePrsObj,
218 const Standard_Integer theMode,
219 const Standard_Integer theNewPrior) const
221 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
223 SetDisplayPriority (anIter.Value(), theMode, theNewPrior);
226 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
229 aPrs->SetDisplayPriority (theNewPrior);
233 // =======================================================================
234 // function : DisplayPriority
236 // =======================================================================
237 Standard_Integer PrsMgr_PresentationManager::DisplayPriority (const Handle(PrsMgr_PresentableObject)& thePrsObj,
238 const Standard_Integer theMode) const
240 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
242 Standard_Integer aPriority = DisplayPriority (anIter.Value(), theMode);
249 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
250 return !aPrs.IsNull()
251 ? aPrs->DisplayPriority()
255 // =======================================================================
256 // function : IsDisplayed
258 // =======================================================================
259 Standard_Boolean PrsMgr_PresentationManager::IsDisplayed (const Handle(PrsMgr_PresentableObject)& thePrsObj,
260 const Standard_Integer theMode) const
262 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
264 if (IsDisplayed (anIter.Value(), theMode))
266 return Standard_True;
270 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
271 return !aPrs.IsNull()
272 && aPrs->IsDisplayed();
275 // =======================================================================
276 // function : IsHighlighted
278 // =======================================================================
279 Standard_Boolean PrsMgr_PresentationManager::IsHighlighted (const Handle(PrsMgr_PresentableObject)& thePrsObj,
280 const Standard_Integer theMode) const
282 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
284 if (IsHighlighted (anIter.Value(), theMode))
286 return Standard_True;
290 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
291 return !aPrs.IsNull()
292 && aPrs->IsHighlighted();
295 // =======================================================================
298 // =======================================================================
299 void PrsMgr_PresentationManager::Update (const Handle(PrsMgr_PresentableObject)& thePrsObj,
300 const Standard_Integer theMode) const
302 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
304 Update (anIter.Value(), theMode);
307 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
311 thePrsObj->Fill (this, aPrs, theMode);
312 aPrs->SetUpdateStatus (Standard_False);
316 // =======================================================================
317 // function : BeginImmediateDraw
319 // =======================================================================
320 void PrsMgr_PresentationManager::BeginImmediateDraw()
322 if (++myImmediateModeOn > 1)
327 ClearImmediateDraw();
330 // =======================================================================
331 // function : ClearImmediateDraw
333 // =======================================================================
334 void PrsMgr_PresentationManager::ClearImmediateDraw()
336 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
338 anIter.Value()->Erase();
341 for (PrsMgr_ListOfPresentations::Iterator anIter (myViewDependentImmediateList); anIter.More(); anIter.Next())
343 anIter.Value()->Erase();
346 myImmediateList.Clear();
347 myViewDependentImmediateList.Clear();
350 // =======================================================================
351 // function : displayImmediate
352 // purpose : Handles the structures from myImmediateList and its visibility
353 // in all views of the viewer given by setting proper affinity
354 // =======================================================================
355 void PrsMgr_PresentationManager::displayImmediate (const Handle(V3d_Viewer)& theViewer)
357 for (theViewer->InitActiveViews(); theViewer->MoreActiveViews(); theViewer->NextActiveViews())
359 const Handle(Graphic3d_CView)& aView = theViewer->ActiveView()->View();
360 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
362 const Handle(Prs3d_Presentation)& aPrs = anIter.Value();
366 Handle(Graphic3d_Structure) aViewDepPrs;
367 Handle(Prs3d_PresentationShadow) aShadowPrs = Handle(Prs3d_PresentationShadow)::DownCast (aPrs);
368 if (!aShadowPrs.IsNull() && aView->IsComputed (aShadowPrs->ParentId(), aViewDepPrs))
370 aShadowPrs.Nullify();
371 aShadowPrs = new Prs3d_PresentationShadow (myStructureManager,
372 Handle(Prs3d_Presentation)::DownCast (aViewDepPrs));
373 aShadowPrs->SetZLayer (aViewDepPrs->CStructure()->ZLayer());
374 aShadowPrs->SetClipPlanes (aViewDepPrs->ClipPlanes());
375 aShadowPrs->CStructure()->IsForHighlight = 1;
376 aShadowPrs->Highlight (Aspect_TOHM_COLOR, aPrs->HighlightColor());
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 (Standard_Integer aPrsIter = 1; aPrsIter <= aPrsList.Length(); ++aPrsIter)
476 const PrsMgr_ModedPresentation& aModedPrs = aPrsList.Value (aPrsIter);
477 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aModedPrs.Presentation()->PresentationManager();
478 if (theMode == aModedPrs.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 (Standard_Integer aPrsIter = 1; aPrsIter <= aPrsList.Length(); ++aPrsIter)
499 const PrsMgr_ModedPresentation& aModedPrs = aPrsList.Value (aPrsIter);
500 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aModedPrs.Presentation()->PresentationManager();
501 if (theMode == aModedPrs.Mode()
504 return aModedPrs.Presentation();
510 return Handle(PrsMgr_Presentation)();
513 Handle(PrsMgr_Presentation) aPrs = new PrsMgr_Presentation (this, thePrsObj);
514 aPrs->SetZLayer (thePrsObj->ZLayer());
515 aPrs->Presentation()->CStructure()->ViewAffinity = myStructureManager->ObjectAffinity (!theSelObj.IsNull() ? theSelObj : thePrsObj);
516 thePrsObj->Presentations().Append (PrsMgr_ModedPresentation (aPrs, theMode));
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 (Standard_Integer aPrsIter = 1; aPrsIter <= aPrsList.Length(); ++aPrsIter)
534 const PrsMgr_ModedPresentation& aModedPrs = aPrsList.Value (aPrsIter);
535 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aModedPrs.Presentation()->PresentationManager();
536 if (theMode == aPrsList (aPrsIter).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 Standard_Integer theLayerId)
553 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
555 SetZLayer (anIter.Value(), theLayerId);
557 if (!thePrsObj->HasOwnPresentations())
562 thePrsObj->SetZLayer (theLayerId);
565 // =======================================================================
566 // function : GetZLayer
568 // =======================================================================
569 Standard_Integer PrsMgr_PresentationManager::GetZLayer (const Handle(PrsMgr_PresentableObject)& thePrsObj) const
571 return thePrsObj->ZLayer();
574 // =======================================================================
575 // function : Connect
577 // =======================================================================
578 void PrsMgr_PresentationManager::Connect (const Handle(PrsMgr_PresentableObject)& thePrsObject,
579 const Handle(PrsMgr_PresentableObject)& theOtherObject,
580 const Standard_Integer theMode,
581 const Standard_Integer theOtherMode)
583 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObject, theMode, Standard_True);
584 Handle(PrsMgr_Presentation) aPrsOther = Presentation (theOtherObject, theOtherMode, Standard_True);
585 aPrs->Connect (aPrsOther);
588 // =======================================================================
589 // function : Transform
591 // =======================================================================
592 void PrsMgr_PresentationManager::Transform (const Handle(PrsMgr_PresentableObject)& thePrsObj,
593 const Handle(Geom_Transformation)& theTransformation,
594 const Standard_Integer theMode)
596 Presentation (thePrsObj, theMode)->Transform (theTransformation);
600 // =======================================================================
603 // =======================================================================
604 void PrsMgr_PresentationManager::Color (const Handle(PrsMgr_PresentableObject)& thePrsObj,
605 const Quantity_NameOfColor theColor,
606 const Standard_Integer theMode,
607 const Handle(PrsMgr_PresentableObject)& theSelObj,
608 const Standard_Integer theImmediateStructLayerId)
610 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
612 Color (anIter.Value(), theColor, theMode, NULL, theImmediateStructLayerId);
614 if (!thePrsObj->HasOwnPresentations())
619 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode, Standard_True, theSelObj);
620 if (aPrs->MustBeUpdated())
622 Update (thePrsObj, theMode);
625 if (myImmediateModeOn > 0)
627 Handle(Prs3d_PresentationShadow) aShadow = new Prs3d_PresentationShadow (myStructureManager, aPrs->Presentation());
628 aShadow->SetZLayer (theImmediateStructLayerId);
629 aShadow->SetClipPlanes (aPrs->Presentation()->ClipPlanes());
630 aShadow->CStructure()->IsForHighlight = 1;
631 aShadow->Highlight (Aspect_TOHM_COLOR, theColor);
632 AddToImmediateList (aShadow);
636 aPrs->Highlight (Aspect_TOHM_COLOR, theColor);
640 // =======================================================================
641 // function : BoundBox
643 // =======================================================================
644 void PrsMgr_PresentationManager::BoundBox (const Handle(PrsMgr_PresentableObject)& thePrsObj,
645 const Standard_Integer theMode)
647 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode, Standard_True);
648 if (aPrs->MustBeUpdated())
650 Update (thePrsObj, theMode);
652 aPrs->Highlight (Aspect_TOHM_BOUNDBOX, mySelectionColor);
657 // =======================================================================
658 // function : updatePrsTransformation
659 // purpose : Internal funtion that scans thePrsList for shadow presentations
660 // and applies transformation theTrsf to them in case if parent ID
661 // of shadow presentation is equal to theRefId
662 // =======================================================================
663 void updatePrsTransformation (const PrsMgr_ListOfPresentations& thePrsList,
664 const Standard_Integer theRefId,
665 const Graphic3d_Mat4& theTrsf)
667 for (PrsMgr_ListOfPresentations::Iterator anIter (thePrsList); anIter.More(); anIter.Next())
669 const Handle(Prs3d_Presentation)& aPrs = anIter.Value();
673 Handle(Prs3d_PresentationShadow) aShadowPrs = Handle(Prs3d_PresentationShadow)::DownCast (aPrs);
674 if (aShadowPrs.IsNull() || aShadowPrs->ParentId() != theRefId)
677 aShadowPrs->CStructure()->Transformation = theTrsf;
682 // =======================================================================
683 // function : UpdateHighlightTrsf
685 // =======================================================================
686 void PrsMgr_PresentationManager::UpdateHighlightTrsf (const Handle(V3d_Viewer)& theViewer,
687 const Handle(PrsMgr_PresentableObject)& theObj,
688 const Standard_Integer theMode,
689 const Handle(PrsMgr_PresentableObject)& theSelObj)
694 const Handle(Prs3d_Presentation)& aBasePrs = Presentation (theObj, theMode, Standard_False)->Presentation();
695 const Handle(Prs3d_Presentation)& aParentPrs = theSelObj.IsNull() ?
696 aBasePrs : Presentation (theSelObj, theMode, Standard_False)->Presentation();
697 const Standard_Integer aParentId = aParentPrs->CStructure()->Id;
699 updatePrsTransformation (myImmediateList, aParentId, aBasePrs->CStructure()->Transformation);
701 if (!myViewDependentImmediateList.IsEmpty())
703 for (theViewer->InitActiveViews(); theViewer->MoreActiveViews(); theViewer->NextActiveViews())
705 const Handle(Graphic3d_CView)& aView = theViewer->ActiveView()->View();
706 Handle(Graphic3d_Structure) aViewDepParentPrs;
707 if (aView->IsComputed (aParentId, aViewDepParentPrs))
709 updatePrsTransformation (myViewDependentImmediateList,
710 aViewDepParentPrs->CStructure()->Id,
711 aBasePrs->CStructure()->Transformation);