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 <Graphic3d_StructureManager.hxx>
19 #include <Prs3d_Presentation.hxx>
20 #include <Prs3d_PresentationShadow.hxx>
21 #include <Prs3d_ShadingAspect.hxx>
22 #include <PrsMgr_ModedPresentation.hxx>
23 #include <PrsMgr_PresentableObject.hxx>
24 #include <PrsMgr_Presentation.hxx>
25 #include <PrsMgr_PresentationManager.hxx>
26 #include <PrsMgr_Presentations.hxx>
27 #include <Standard_NoSuchObject.hxx>
28 #include <Standard_Type.hxx>
29 #include <TColStd_ListIteratorOfListOfTransient.hxx>
30 #include <V3d_View.hxx>
31 #include <Visual3d_View.hxx>
32 #include <Visual3d_ViewManager.hxx>
34 // =======================================================================
35 // function : PrsMgr_PresentationManager
37 // =======================================================================
38 PrsMgr_PresentationManager::PrsMgr_PresentationManager (const Handle(Visual3d_ViewManager)& theStructureManager)
39 : myStructureManager (theStructureManager),
40 myImmediateModeOn (0),
41 mySelectionColor (Quantity_NOC_GRAY99)
46 // =======================================================================
49 // =======================================================================
50 void PrsMgr_PresentationManager::Display (const Handle(PrsMgr_PresentableObject)& thePrsObj,
51 const Standard_Integer theMode)
53 if (thePrsObj->HasOwnPresentations())
55 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode, Standard_True);
56 if (aPrs->MustBeUpdated())
58 Update (thePrsObj, theMode);
61 if (myImmediateModeOn > 0)
63 AddToImmediateList (aPrs->Presentation());
72 thePrsObj->Compute (this, Handle(Prs3d_Presentation)(), theMode);
75 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
77 Display (anIter.Value(), theMode);
81 // =======================================================================
84 // =======================================================================
85 void PrsMgr_PresentationManager::Erase (const Handle(PrsMgr_PresentableObject)& thePrsObj,
86 const Standard_Integer theMode)
88 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
90 Erase (anIter.Value(), theMode);
93 PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
94 for (Standard_Integer aPrsIter = 1; aPrsIter <= aPrsList.Length(); ++aPrsIter)
96 const PrsMgr_ModedPresentation& aModedPrs = aPrsList.Value (aPrsIter);
97 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aModedPrs.Presentation()->PresentationManager();
98 if (theMode == aPrsList (aPrsIter).Mode()
101 if (!aModedPrs.Presentation().IsNull())
103 aModedPrs.Presentation()->Erase();
105 aPrsList.Remove (aPrsIter);
111 // =======================================================================
114 // =======================================================================
115 void PrsMgr_PresentationManager::Clear (const Handle(PrsMgr_PresentableObject)& thePrsObj,
116 const Standard_Integer theMode)
118 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
120 Clear (anIter.Value(), theMode);
123 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
130 // =======================================================================
131 // function : SetVisibility
133 // =======================================================================
134 void PrsMgr_PresentationManager::SetVisibility (const Handle(PrsMgr_PresentableObject)& thePrsObj,
135 const Standard_Integer theMode,
136 const Standard_Boolean theValue)
138 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
140 SetVisibility (anIter.Value(), theMode, theValue);
142 if (!thePrsObj->HasOwnPresentations())
147 Presentation (thePrsObj, theMode)->SetVisible (theValue);
150 // =======================================================================
151 // function : Highlight
153 // =======================================================================
154 void PrsMgr_PresentationManager::Highlight (const Handle(PrsMgr_PresentableObject)& thePrsObj,
155 const Standard_Integer theMode)
157 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
159 Highlight (anIter.Value(), theMode);
161 if (!thePrsObj->HasOwnPresentations())
166 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode, Standard_True);
167 if (aPrs->MustBeUpdated())
169 Update (thePrsObj, theMode);
172 if (myImmediateModeOn > 0)
174 Handle(Prs3d_PresentationShadow) aShadow = new Prs3d_PresentationShadow (myStructureManager, aPrs->Presentation());
175 aShadow->Highlight (Aspect_TOHM_COLOR, mySelectionColor);
176 AddToImmediateList (aShadow);
180 aPrs->Highlight (Aspect_TOHM_COLOR, mySelectionColor);
184 // =======================================================================
185 // function : Unhighlight
187 // =======================================================================
188 void PrsMgr_PresentationManager::Unhighlight (const Handle(PrsMgr_PresentableObject)& thePrsObj,
189 const Standard_Integer theMode)
191 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
193 Unhighlight (anIter.Value(), theMode);
196 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
203 // =======================================================================
204 // function : SetDisplayPriority
206 // =======================================================================
207 void PrsMgr_PresentationManager::SetDisplayPriority (const Handle(PrsMgr_PresentableObject)& thePrsObj,
208 const Standard_Integer theMode,
209 const Standard_Integer theNewPrior) const
211 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
213 SetDisplayPriority (anIter.Value(), theMode, theNewPrior);
216 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
219 aPrs->SetDisplayPriority (theNewPrior);
223 // =======================================================================
224 // function : DisplayPriority
226 // =======================================================================
227 Standard_Integer PrsMgr_PresentationManager::DisplayPriority (const Handle(PrsMgr_PresentableObject)& thePrsObj,
228 const Standard_Integer theMode) const
230 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
232 Standard_Integer aPriority = DisplayPriority (anIter.Value(), theMode);
239 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
240 return !aPrs.IsNull()
241 ? aPrs->DisplayPriority()
245 // =======================================================================
246 // function : IsDisplayed
248 // =======================================================================
249 Standard_Boolean PrsMgr_PresentationManager::IsDisplayed (const Handle(PrsMgr_PresentableObject)& thePrsObj,
250 const Standard_Integer theMode) const
252 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
254 if (IsDisplayed (anIter.Value(), theMode))
256 return Standard_True;
260 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
261 return !aPrs.IsNull()
262 && aPrs->IsDisplayed();
265 // =======================================================================
266 // function : IsHighlighted
268 // =======================================================================
269 Standard_Boolean PrsMgr_PresentationManager::IsHighlighted (const Handle(PrsMgr_PresentableObject)& thePrsObj,
270 const Standard_Integer theMode) const
272 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
274 if (IsHighlighted (anIter.Value(), theMode))
276 return Standard_True;
280 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
281 return !aPrs.IsNull()
282 && aPrs->IsHighlighted();
285 // =======================================================================
288 // =======================================================================
289 void PrsMgr_PresentationManager::Update (const Handle(PrsMgr_PresentableObject)& thePrsObj,
290 const Standard_Integer theMode) const
292 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
294 Update (anIter.Value(), theMode);
297 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
301 thePrsObj->Fill (this, aPrs, theMode);
302 aPrs->SetUpdateStatus (Standard_False);
306 // =======================================================================
307 // function : BeginImmediateDraw
309 // =======================================================================
310 void PrsMgr_PresentationManager::BeginImmediateDraw()
312 if (++myImmediateModeOn > 1)
317 ClearImmediateDraw();
320 // =======================================================================
321 // function : ClearImmediateDraw
323 // =======================================================================
324 void PrsMgr_PresentationManager::ClearImmediateDraw()
326 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
328 anIter.Value()->Erase();
331 for (PrsMgr_ListOfPresentations::Iterator anIter (myViewDependentImmediateList); anIter.More(); anIter.Next())
333 anIter.Value()->Erase();
336 myImmediateList.Clear();
337 myViewDependentImmediateList.Clear();
340 // =======================================================================
341 // function : displayImmediate
342 // purpose : Handles the structures from myImmediateList and its visibility
343 // in all views of the viewer given by setting proper affinity
344 // =======================================================================
345 void PrsMgr_PresentationManager::displayImmediate (const Handle(V3d_Viewer)& theViewer)
347 for (theViewer->InitActiveViews(); theViewer->MoreActiveViews(); theViewer->NextActiveViews())
349 const Handle(Visual3d_View)& aView = theViewer->ActiveView()->View();
350 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
352 const Handle(Prs3d_Presentation)& aPrs = anIter.Value();
356 Handle(Prs3d_Presentation) aViewDepPrs;
357 Handle(Prs3d_PresentationShadow) aShadowPrs = Handle(Prs3d_PresentationShadow)::DownCast (aPrs);
358 if (!aShadowPrs.IsNull() && aView->IsComputed (aShadowPrs->ParentId(), aViewDepPrs))
360 aShadowPrs.Nullify();
361 aShadowPrs = new Prs3d_PresentationShadow (myStructureManager, aViewDepPrs);
362 aShadowPrs->SetZLayer (aViewDepPrs->CStructure()->ZLayer());
363 aShadowPrs->SetClipPlanes (aViewDepPrs->GetClipPlanes());
364 aShadowPrs->CStructure()->IsForHighlight = 1;
365 aShadowPrs->Highlight (Aspect_TOHM_COLOR, aPrs->HighlightColor());
366 myViewDependentImmediateList.Append (aShadowPrs);
368 // handles custom highlight presentations which were defined in overriden
369 // HilightOwnerWithColor method of a custom AIS objects and maintain its
370 // visibility in different views on their own
371 else if (aShadowPrs.IsNull())
377 if (!aShadowPrs->IsDisplayed())
379 aShadowPrs->CStructure()->ViewAffinity = new Graphic3d_ViewAffinity();
380 aShadowPrs->CStructure()->ViewAffinity->SetVisible (Standard_False);
381 aShadowPrs->Display();
384 Standard_Integer aViewId = aView->Identification();
385 bool isParentVisible = aShadowPrs->ParentAffinity().IsNull() ?
386 Standard_True : aShadowPrs->ParentAffinity()->IsVisible (aViewId);
387 aShadowPrs->CStructure()->ViewAffinity->SetVisible (aViewId, isParentVisible);
392 // =======================================================================
393 // function : EndImmediateDraw
395 // =======================================================================
396 void PrsMgr_PresentationManager::EndImmediateDraw (const Handle(V3d_Viewer)& theViewer)
398 if (--myImmediateModeOn > 0)
403 displayImmediate (theViewer);
406 // =======================================================================
407 // function : RedrawImmediate
408 // purpose : Clears all immediate structures and redisplays with proper
410 //=======================================================================
411 void PrsMgr_PresentationManager::RedrawImmediate (const Handle(V3d_Viewer)& theViewer)
413 if (myImmediateList.IsEmpty())
416 // Clear previously displayed structures
417 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
419 anIter.Value()->Erase();
421 for (PrsMgr_ListOfPresentations::Iterator anIter (myViewDependentImmediateList); anIter.More(); anIter.Next())
423 anIter.Value()->Erase();
425 myViewDependentImmediateList.Clear();
427 displayImmediate (theViewer);
430 // =======================================================================
431 // function : AddToImmediateList
433 //=======================================================================
434 void PrsMgr_PresentationManager::AddToImmediateList (const Handle(Prs3d_Presentation)& thePrs)
436 if (myImmediateModeOn < 1)
441 for (PrsMgr_ListOfPresentations::Iterator anIter (myImmediateList); anIter.More(); anIter.Next())
443 if (anIter.Value() == thePrs)
449 myImmediateList.Append (thePrs);
452 // =======================================================================
453 // function : HasPresentation
455 // =======================================================================
456 Standard_Boolean PrsMgr_PresentationManager::HasPresentation (const Handle(PrsMgr_PresentableObject)& thePrsObj,
457 const Standard_Integer theMode) const
459 if (!thePrsObj->HasOwnPresentations())
460 return Standard_False;
462 const PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
463 for (Standard_Integer aPrsIter = 1; aPrsIter <= aPrsList.Length(); ++aPrsIter)
465 const PrsMgr_ModedPresentation& aModedPrs = aPrsList.Value (aPrsIter);
466 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aModedPrs.Presentation()->PresentationManager();
467 if (theMode == aModedPrs.Mode()
470 return Standard_True;
473 return Standard_False;
476 // =======================================================================
477 // function : Presentation
479 // =======================================================================
480 Handle(PrsMgr_Presentation) PrsMgr_PresentationManager::Presentation (const Handle(PrsMgr_PresentableObject)& thePrsObj,
481 const Standard_Integer theMode,
482 const Standard_Boolean theToCreate,
483 const Handle(PrsMgr_PresentableObject)& theSelObj) const
485 const PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
486 for (Standard_Integer aPrsIter = 1; aPrsIter <= aPrsList.Length(); ++aPrsIter)
488 const PrsMgr_ModedPresentation& aModedPrs = aPrsList.Value (aPrsIter);
489 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aModedPrs.Presentation()->PresentationManager();
490 if (theMode == aModedPrs.Mode()
493 return aModedPrs.Presentation();
499 return Handle(PrsMgr_Presentation)();
502 Handle(PrsMgr_Presentation) aPrs = new PrsMgr_Presentation (this, thePrsObj);
503 aPrs->SetZLayer (thePrsObj->ZLayer());
504 aPrs->Presentation()->CStructure()->ViewAffinity = myStructureManager->ObjectAffinity (!theSelObj.IsNull() ? theSelObj : thePrsObj);
505 thePrsObj->Presentations().Append (PrsMgr_ModedPresentation (aPrs, theMode));
506 thePrsObj->Fill (this, aPrs, theMode);
508 // set layer index accordingly to object's presentations
509 aPrs->SetUpdateStatus (Standard_False);
513 // =======================================================================
514 // function : RemovePresentation
516 // =======================================================================
517 Standard_Boolean PrsMgr_PresentationManager::RemovePresentation (const Handle(PrsMgr_PresentableObject)& thePrsObj,
518 const Standard_Integer theMode)
520 PrsMgr_Presentations& aPrsList = thePrsObj->Presentations();
521 for (Standard_Integer aPrsIter = 1; aPrsIter <= aPrsList.Length(); ++aPrsIter)
523 const PrsMgr_ModedPresentation& aModedPrs = aPrsList.Value (aPrsIter);
524 const Handle(PrsMgr_PresentationManager)& aPrsMgr = aModedPrs.Presentation()->PresentationManager();
525 if (theMode == aPrsList (aPrsIter).Mode()
528 aPrsList.Remove (aPrsIter);
529 return Standard_True;
532 return Standard_False;
535 // =======================================================================
536 // function : SetZLayer
538 // =======================================================================
539 void PrsMgr_PresentationManager::SetZLayer (const Handle(PrsMgr_PresentableObject)& thePrsObj,
540 const Standard_Integer theLayerId)
542 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
544 SetZLayer (anIter.Value(), theLayerId);
546 if (!thePrsObj->HasOwnPresentations())
551 thePrsObj->SetZLayer (theLayerId);
554 // =======================================================================
555 // function : GetZLayer
557 // =======================================================================
558 Standard_Integer PrsMgr_PresentationManager::GetZLayer (const Handle(PrsMgr_PresentableObject)& thePrsObj) const
560 return thePrsObj->ZLayer();
563 // =======================================================================
564 // function : Connect
566 // =======================================================================
567 void PrsMgr_PresentationManager::Connect (const Handle(PrsMgr_PresentableObject)& thePrsObject,
568 const Handle(PrsMgr_PresentableObject)& theOtherObject,
569 const Standard_Integer theMode,
570 const Standard_Integer theOtherMode)
572 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObject, theMode, Standard_True);
573 Handle(PrsMgr_Presentation) aPrsOther = Presentation (theOtherObject, theOtherMode, Standard_True);
574 aPrs->Connect (aPrsOther);
577 // =======================================================================
578 // function : Transform
580 // =======================================================================
581 void PrsMgr_PresentationManager::Transform (const Handle(PrsMgr_PresentableObject)& thePrsObj,
582 const Handle(Geom_Transformation)& theTransformation,
583 const Standard_Integer theMode)
585 Presentation (thePrsObj, theMode)->Transform (theTransformation);
589 // =======================================================================
592 // =======================================================================
593 void PrsMgr_PresentationManager::Color (const Handle(PrsMgr_PresentableObject)& thePrsObj,
594 const Quantity_NameOfColor theColor,
595 const Standard_Integer theMode,
596 const Handle(PrsMgr_PresentableObject)& theSelObj,
597 const Standard_Integer theImmediateStructLayerId)
599 for (PrsMgr_ListOfPresentableObjectsIter anIter (thePrsObj->Children()); anIter.More(); anIter.Next())
601 Color (anIter.Value(), theColor, theMode, NULL, theImmediateStructLayerId);
603 if (!thePrsObj->HasOwnPresentations())
608 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode, Standard_True, theSelObj);
609 if (aPrs->MustBeUpdated())
611 Update (thePrsObj, theMode);
614 if (myImmediateModeOn > 0)
616 Handle(Prs3d_PresentationShadow) aShadow = new Prs3d_PresentationShadow (myStructureManager, aPrs->Presentation());
617 aShadow->SetZLayer (theImmediateStructLayerId);
618 aShadow->SetClipPlanes (aPrs->Presentation()->GetClipPlanes());
619 aShadow->CStructure()->IsForHighlight = 1;
620 aShadow->Highlight (Aspect_TOHM_COLOR, theColor);
621 AddToImmediateList (aShadow);
625 aPrs->Highlight (Aspect_TOHM_COLOR, theColor);
629 // =======================================================================
630 // function : BoundBox
632 // =======================================================================
633 void PrsMgr_PresentationManager::BoundBox (const Handle(PrsMgr_PresentableObject)& thePrsObj,
634 const Standard_Integer theMode)
636 Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode, Standard_True);
637 if (aPrs->MustBeUpdated())
639 Update (thePrsObj, theMode);
641 aPrs->Highlight (Aspect_TOHM_BOUNDBOX, mySelectionColor);
644 // =======================================================================
645 // function : SetShadingAspect
647 // =======================================================================
648 void PrsMgr_PresentationManager::SetShadingAspect (const Handle(PrsMgr_PresentableObject)& thePrsObject,
649 const Quantity_NameOfColor theColor,
650 const Graphic3d_NameOfMaterial theMaterial,
651 const Standard_Integer theMode)
653 Handle(Prs3d_ShadingAspect) anAspect = new Prs3d_ShadingAspect();
654 anAspect->SetColor (theColor);
655 anAspect->SetMaterial (theMaterial);
656 SetShadingAspect (thePrsObject, anAspect, theMode);
659 // =======================================================================
660 // function : SetShadingAspect
662 // =======================================================================
663 void PrsMgr_PresentationManager::SetShadingAspect (const Handle(PrsMgr_PresentableObject)& thePrsObj,
664 const Handle(Prs3d_ShadingAspect)& theShadingAspect,
665 const Standard_Integer theMode)
667 const Handle(PrsMgr_Presentation) aPrs = Presentation (thePrsObj, theMode);
670 aPrs->SetShadingAspect (theShadingAspect);