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),
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->Presentation());
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 anIt (aPrsList); anIt.More();)
93 const PrsMgr_ModedPresentation& aModedPrs = anIt.Value();
94 if (aModedPrs.Presentation().IsNull())
100 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aModedPrs.Presentation()->PresentationManager();
101 if ((theMode == aModedPrs.Mode() || theMode == -1)
102 && (this == aPrsMgr))
104 aModedPrs.Presentation()->Erase();
106 aPrsList.Remove (anIt);
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,
168 const Standard_Integer theMode)
170 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
172 Unhighlight (anIter.Value(), theMode);
175 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
182 // =======================================================================
183 // function : SetDisplayPriority
185 // =======================================================================
186 void PrsMgr_PresentationManager::SetDisplayPriority (const Handle(PrsMgr_PresentableObject)& thePrsObj,
187 const Standard_Integer theMode,
188 const Standard_Integer theNewPrior) const
190 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
192 SetDisplayPriority (anIter.Value(), theMode, theNewPrior);
195 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
198 aPrs->SetDisplayPriority (theNewPrior);
202 // =======================================================================
203 // function : DisplayPriority
205 // =======================================================================
206 Standard_Integer PrsMgr_PresentationManager::DisplayPriority (const Handle(PrsMgr_PresentableObject)& thePrsObj,
207 const Standard_Integer theMode) const
209 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
211 Standard_Integer aPriority = DisplayPriority (anIter.Value(), theMode);
218 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
219 return !aPrs.IsNull()
220 ? aPrs->DisplayPriority()
224 // =======================================================================
225 // function : IsDisplayed
227 // =======================================================================
228 Standard_Boolean PrsMgr_PresentationManager::IsDisplayed (const Handle(PrsMgr_PresentableObject)& thePrsObj,
229 const Standard_Integer theMode) const
231 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
233 if (IsDisplayed (anIter.Value(), theMode))
235 return Standard_True;
239 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
240 return !aPrs.IsNull()
241 && aPrs->IsDisplayed();
244 // =======================================================================
245 // function : IsHighlighted
247 // =======================================================================
248 Standard_Boolean PrsMgr_PresentationManager::IsHighlighted (const Handle(PrsMgr_PresentableObject)& thePrsObj,
249 const Standard_Integer theMode) const
251 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
253 if (IsHighlighted (anIter.Value(), theMode))
255 return Standard_True;
259 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
260 return !aPrs.IsNull()
261 && aPrs->IsHighlighted();
264 // =======================================================================
267 // =======================================================================
268 void PrsMgr_PresentationManager::Update (const Handle(PrsMgr_PresentableObject)& thePrsObj,
269 const Standard_Integer theMode) const
271 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
273 Update (anIter.Value(), theMode);
276 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
280 thePrsObj->Fill (this, aPrs, theMode);
281 aPrs->SetUpdateStatus (Standard_False);
285 // =======================================================================
286 // function : BeginImmediateDraw
288 // =======================================================================
289 void PrsMgr_PresentationManager::BeginImmediateDraw()
291 if (++myImmediateModeOn > 1)
296 ClearImmediateDraw();
299 // =======================================================================
300 // function : ClearImmediateDraw
302 // =======================================================================
303 void PrsMgr_PresentationManager::ClearImmediateDraw()
305 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
307 anIter.Value()->Erase();
310 for (PrsMgr_ListOfPresentations::Iterator anIter (myViewDependentImmediateList); anIter.More(); anIter.Next())
312 anIter.Value()->Erase();
315 myImmediateList.Clear();
316 myViewDependentImmediateList.Clear();
319 // =======================================================================
320 // function : displayImmediate
321 // purpose : Handles the structures from myImmediateList and its visibility
322 // in all views of the viewer given by setting proper affinity
323 // =======================================================================
324 void PrsMgr_PresentationManager::displayImmediate (const Handle(V3d_Viewer)& theViewer)
326 for (V3d_ListOfViewIterator anActiveViewIter (theViewer->ActiveViewIterator()); anActiveViewIter.More(); anActiveViewIter.Next())
328 const Handle(Graphic3d_CView)& aView = anActiveViewIter.Value()->View();
329 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
331 const Handle(Prs3d_Presentation)& aPrs = anIter.Value();
335 Handle(Graphic3d_Structure) aViewDepPrs;
336 Handle(Prs3d_PresentationShadow) aShadowPrs = Handle(Prs3d_PresentationShadow)::DownCast (aPrs);
337 if (!aShadowPrs.IsNull() && aView->IsComputed (aShadowPrs->ParentId(), aViewDepPrs))
339 aShadowPrs.Nullify();
340 aShadowPrs = new Prs3d_PresentationShadow (myStructureManager,
341 Handle(Prs3d_Presentation)::DownCast (aViewDepPrs));
342 aShadowPrs->SetZLayer (aViewDepPrs->CStructure()->ZLayer());
343 aShadowPrs->SetClipPlanes (aViewDepPrs->ClipPlanes());
344 aShadowPrs->CStructure()->IsForHighlight = 1;
345 aShadowPrs->Highlight (aPrs->HighlightStyle());
346 myViewDependentImmediateList.Append (aShadowPrs);
348 // handles custom highlight presentations which were defined in overridden
349 // HilightOwnerWithColor method of a custom AIS objects and maintain its
350 // visibility in different views on their own
351 else if (aShadowPrs.IsNull())
357 if (!aShadowPrs->IsDisplayed())
359 aShadowPrs->CStructure()->ViewAffinity = new Graphic3d_ViewAffinity();
360 aShadowPrs->CStructure()->ViewAffinity->SetVisible (Standard_False);
361 aShadowPrs->Display();
364 Standard_Integer aViewId = aView->Identification();
365 bool isParentVisible = aShadowPrs->ParentAffinity().IsNull() ?
366 Standard_True : aShadowPrs->ParentAffinity()->IsVisible (aViewId);
367 aShadowPrs->CStructure()->ViewAffinity->SetVisible (aViewId, isParentVisible);
372 // =======================================================================
373 // function : EndImmediateDraw
375 // =======================================================================
376 void PrsMgr_PresentationManager::EndImmediateDraw (const Handle(V3d_Viewer)& theViewer)
378 if (--myImmediateModeOn > 0)
383 displayImmediate (theViewer);
386 // =======================================================================
387 // function : RedrawImmediate
388 // purpose : Clears all immediate structures and redisplays with proper
390 //=======================================================================
391 void PrsMgr_PresentationManager::RedrawImmediate (const Handle(V3d_Viewer)& theViewer)
393 if (myImmediateList.IsEmpty())
396 // Clear previously displayed structures
397 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
399 anIter.Value()->Erase();
401 for (PrsMgr_ListOfPresentations::Iterator anIter (myViewDependentImmediateList); anIter.More(); anIter.Next())
403 anIter.Value()->Erase();
405 myViewDependentImmediateList.Clear();
407 displayImmediate (theViewer);
410 // =======================================================================
411 // function : AddToImmediateList
413 //=======================================================================
414 void PrsMgr_PresentationManager::AddToImmediateList (const Handle(Prs3d_Presentation)& thePrs)
416 if (myImmediateModeOn < 1)
421 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
423 if (anIter.Value() == thePrs)
429 myImmediateList.Append (thePrs);
432 // =======================================================================
433 // function : HasPresentation
435 // =======================================================================
436 Standard_Boolean PrsMgr_PresentationManager::HasPresentation (const Handle(PrsMgr_PresentableObject)& thePrsObj,
437 const Standard_Integer theMode) const
439 if (!thePrsObj->HasOwnPresentations())
440 return Standard_False;
442 const PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
443 for (Standard_Integer aPrsIter = 1; aPrsIter <= aPrsList.Length(); ++aPrsIter)
445 const PrsMgr_ModedPresentation& aModedPrs = aPrsList.Value (aPrsIter);
446 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aModedPrs.Presentation()->PresentationManager();
447 if (theMode == aModedPrs.Mode()
450 return Standard_True;
453 return Standard_False;
456 // =======================================================================
457 // function : Presentation
459 // =======================================================================
460 Handle(PrsMgr_Presentation) PrsMgr_PresentationManager::Presentation (const Handle(PrsMgr_PresentableObject)& thePrsObj,
461 const Standard_Integer theMode,
462 const Standard_Boolean theToCreate,
463 const Handle(PrsMgr_PresentableObject)& theSelObj) const
465 const PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
466 for (Standard_Integer aPrsIter = 1; aPrsIter <= aPrsList.Length(); ++aPrsIter)
468 const PrsMgr_ModedPresentation& aModedPrs = aPrsList.Value (aPrsIter);
469 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aModedPrs.Presentation()->PresentationManager();
470 if (theMode == aModedPrs.Mode()
473 return aModedPrs.Presentation();
479 return Handle(PrsMgr_Presentation)();
482 Handle(PrsMgr_Presentation) aPrs = new PrsMgr_Presentation (this, thePrsObj);
483 aPrs->SetZLayer (thePrsObj->ZLayer());
484 aPrs->Presentation()->CStructure()->ViewAffinity = myStructureManager->ObjectAffinity (!theSelObj.IsNull() ? theSelObj : thePrsObj);
485 thePrsObj->Presentations().Append (PrsMgr_ModedPresentation (aPrs, theMode));
486 thePrsObj->Fill (this, aPrs, theMode);
488 // set layer index accordingly to object's presentations
489 aPrs->SetUpdateStatus (Standard_False);
493 // =======================================================================
494 // function : RemovePresentation
496 // =======================================================================
497 Standard_Boolean PrsMgr_PresentationManager::RemovePresentation (const Handle(PrsMgr_PresentableObject)& thePrsObj,
498 const Standard_Integer theMode)
500 PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
501 for (Standard_Integer aPrsIter = 1; aPrsIter <= aPrsList.Length(); ++aPrsIter)
503 const PrsMgr_ModedPresentation& aModedPrs = aPrsList.Value (aPrsIter);
504 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aModedPrs.Presentation()->PresentationManager();
505 if (theMode == aPrsList (aPrsIter).Mode()
508 aPrsList.Remove (aPrsIter);
509 return Standard_True;
512 return Standard_False;
515 // =======================================================================
516 // function : SetZLayer
518 // =======================================================================
519 void PrsMgr_PresentationManager::SetZLayer (const Handle(PrsMgr_PresentableObject)& thePrsObj,
520 const Standard_Integer theLayerId)
522 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
524 SetZLayer (anIter.Value(), theLayerId);
526 if (!thePrsObj->HasOwnPresentations())
531 thePrsObj->SetZLayer (theLayerId);
534 // =======================================================================
535 // function : GetZLayer
537 // =======================================================================
538 Standard_Integer PrsMgr_PresentationManager::GetZLayer (const Handle(PrsMgr_PresentableObject)& thePrsObj) const
540 return thePrsObj->ZLayer();
543 // =======================================================================
544 // function : Connect
546 // =======================================================================
547 void PrsMgr_PresentationManager::Connect (const Handle(PrsMgr_PresentableObject)& thePrsObject,
548 const Handle(PrsMgr_PresentableObject)& theOtherObject,
549 const Standard_Integer theMode,
550 const Standard_Integer theOtherMode)
552 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObject, theMode, Standard_True);
553 Handle(PrsMgr_Presentation) aPrsOther = Presentation (theOtherObject, theOtherMode, Standard_True);
554 aPrs->Connect (aPrsOther);
557 // =======================================================================
558 // function : Transform
560 // =======================================================================
561 void PrsMgr_PresentationManager::Transform (const Handle(PrsMgr_PresentableObject)& thePrsObj,
562 const Handle(Geom_Transformation)& theTransformation,
563 const Standard_Integer theMode)
565 Presentation (thePrsObj, theMode)->SetTransformation (theTransformation);
569 // =======================================================================
572 // =======================================================================
573 void PrsMgr_PresentationManager::Color (const Handle(PrsMgr_PresentableObject)& thePrsObj,
574 const Handle(Graphic3d_HighlightStyle)& theStyle,
575 const Standard_Integer theMode,
576 const Handle(PrsMgr_PresentableObject)& theSelObj,
577 const Standard_Integer theImmediateStructLayerId)
579 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
581 Color (anIter.Value(), theStyle, theMode, NULL, theImmediateStructLayerId);
583 if (!thePrsObj->HasOwnPresentations())
588 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode, Standard_True, theSelObj);
589 if (aPrs->MustBeUpdated())
591 Update (thePrsObj, theMode);
594 if (myImmediateModeOn > 0)
596 Handle(Prs3d_PresentationShadow) aShadow = new Prs3d_PresentationShadow (myStructureManager, aPrs->Presentation());
597 aShadow->SetZLayer (theImmediateStructLayerId);
598 aShadow->SetClipPlanes (aPrs->Presentation()->ClipPlanes());
599 aShadow->CStructure()->IsForHighlight = 1;
600 aShadow->Highlight (theStyle);
601 AddToImmediateList (aShadow);
605 aPrs->Highlight (theStyle);
611 // =======================================================================
612 // function : updatePrsTransformation
613 // purpose : Internal function that scans thePrsList for shadow presentations
614 // and applies transformation theTrsf to them in case if parent ID
615 // of shadow presentation is equal to theRefId
616 // =======================================================================
617 void updatePrsTransformation (const PrsMgr_ListOfPresentations& thePrsList,
618 const Standard_Integer theRefId,
619 const Handle(Geom_Transformation)& theTrsf)
621 for (PrsMgr_ListOfPresentations::Iterator anIter (thePrsList); anIter.More(); anIter.Next())
623 const Handle(Prs3d_Presentation)& aPrs = anIter.Value();
627 Handle(Prs3d_PresentationShadow) aShadowPrs = Handle(Prs3d_PresentationShadow)::DownCast (aPrs);
628 if (aShadowPrs.IsNull() || aShadowPrs->ParentId() != theRefId)
631 aShadowPrs->CStructure()->SetTransformation (theTrsf);
636 // =======================================================================
637 // function : UpdateHighlightTrsf
639 // =======================================================================
640 void PrsMgr_PresentationManager::UpdateHighlightTrsf (const Handle(V3d_Viewer)& theViewer,
641 const Handle(PrsMgr_PresentableObject)& theObj,
642 const Standard_Integer theMode,
643 const Handle(PrsMgr_PresentableObject)& theSelObj)
648 Handle(PrsMgr_Presentation) aPrs = Presentation (!theSelObj.IsNull() ? theSelObj : theObj, theMode, Standard_False);
654 Handle(Geom_Transformation) aTrsf = theObj->LocalTransformationGeom();
655 const Standard_Integer aParentId = aPrs->Presentation()->CStructure()->Id;
656 updatePrsTransformation (myImmediateList, aParentId, aTrsf);
658 if (!myViewDependentImmediateList.IsEmpty())
660 for (V3d_ListOfViewIterator anActiveViewIter (theViewer->ActiveViewIterator()); anActiveViewIter.More(); anActiveViewIter.Next())
662 const Handle(Graphic3d_CView)& aView = anActiveViewIter.Value()->View();
663 Handle(Graphic3d_Structure) aViewDepParentPrs;
664 if (aView->IsComputed (aParentId, aViewDepParentPrs))
666 updatePrsTransformation (myViewDependentImmediateList,
667 aViewDepParentPrs->CStructure()->Id,