1 // Created on: 1996-12-18
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1996-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <AIS_InteractiveObject.hxx>
19 #include <AIS_GraphicTool.hxx>
20 #include <AIS_InteractiveContext.hxx>
21 #include <Aspect_PolygonOffsetMode.hxx>
22 #include <Bnd_Box.hxx>
23 #include <Graphic3d_AspectFillArea3d.hxx>
24 #include <Graphic3d_AspectLine3d.hxx>
25 #include <Graphic3d_AspectMarker3d.hxx>
26 #include <Graphic3d_AspectText3d.hxx>
27 #include <Graphic3d_BndBox4f.hxx>
28 #include <Graphic3d_CStructure.hxx>
29 #include <Graphic3d_Group.hxx>
30 #include <Graphic3d_MaterialAspect.hxx>
31 #include <Graphic3d_Structure.hxx>
32 #include <Prs3d_BasicAspect.hxx>
33 #include <Prs3d_LineAspect.hxx>
34 #include <Prs3d_PointAspect.hxx>
35 #include <Prs3d_Presentation.hxx>
36 #include <Prs3d_Root.hxx>
37 #include <Prs3d_ShadingAspect.hxx>
38 #include <Prs3d_TextAspect.hxx>
39 #include <PrsMgr_ModedPresentation.hxx>
40 #include <PrsMgr_PresentationManager3d.hxx>
41 #include <Quantity_Color.hxx>
42 #include <Standard_Transient.hxx>
43 #include <Standard_Type.hxx>
44 #include <TColStd_ListIteratorOfListOfInteger.hxx>
46 IMPLEMENT_STANDARD_RTTIEXT(AIS_InteractiveObject,SelectMgr_SelectableObject)
48 //=======================================================================
49 //function : AIS_InteractiveObject
51 //=======================================================================
52 AIS_InteractiveObject::AIS_InteractiveObject (const PrsMgr_TypeOfPresentation3d aTypeOfPresentation3d)
53 : SelectMgr_SelectableObject (aTypeOfPresentation3d),
56 myCurrentFacingModel (Aspect_TOFM_BOTH_SIDE),
57 myInfiniteState (Standard_False),
58 hasOwnColor (Standard_False),
59 hasOwnMaterial (Standard_False),
60 myRecomputeEveryPrs (Standard_True)
62 SetCurrentFacingModel();
65 //=======================================================================
66 //function : Redisplay
68 //=======================================================================
69 void AIS_InteractiveObject::Redisplay (const Standard_Boolean AllModes)
74 myCTXPtr->Redisplay (this, Standard_False, AllModes);
77 //=======================================================================
80 //=======================================================================
82 AIS_KindOfInteractive AIS_InteractiveObject::Type() const
83 {return AIS_KOI_None;}
85 //=======================================================================
86 //function : Signature
88 //=======================================================================
90 Standard_Integer AIS_InteractiveObject::Signature() const
93 //=======================================================================
94 //function : RecomputeEveryPrs
96 //=======================================================================
98 Standard_Boolean AIS_InteractiveObject::RecomputeEveryPrs() const
99 {return myRecomputeEveryPrs;}
101 //=======================================================================
104 //=======================================================================
105 Handle(AIS_InteractiveContext) AIS_InteractiveObject::GetContext() const
110 //=======================================================================
111 //function : SetContext
113 //=======================================================================
114 void AIS_InteractiveObject::SetContext (const Handle(AIS_InteractiveContext)& theCtx)
116 if (myCTXPtr == theCtx.get())
121 myCTXPtr = theCtx.get();
122 if (!theCtx.IsNull())
124 myDrawer->Link (theCtx->DefaultDrawer());
128 //=======================================================================
131 //=======================================================================
132 Standard_Boolean AIS_InteractiveObject::HasOwner() const
134 return (!myOwner.IsNull());
139 //=======================================================================
142 //=======================================================================
143 void AIS_InteractiveObject::ClearOwner()
148 //=======================================================================
149 //function : SetDisplayMode
151 //=======================================================================
152 void AIS_InteractiveObject::SetDisplayMode (const Standard_Integer theMode)
154 if (AcceptDisplayMode (theMode))
156 myDrawer->SetDisplayMode (theMode);
160 //=======================================================================
163 //=======================================================================
164 void AIS_InteractiveObject::SetCurrentFacingModel(const Aspect_TypeOfFacingModel aModel) {
165 myCurrentFacingModel = aModel;
168 //=======================================================================
169 //function : CurrentFacingModel
171 //=======================================================================
173 Aspect_TypeOfFacingModel AIS_InteractiveObject::CurrentFacingModel() const {
174 return myCurrentFacingModel;
177 //=======================================================================
178 //function : SetColor
180 //=======================================================================
182 void AIS_InteractiveObject::SetColor(const Quantity_Color& theColor)
184 myDrawer->SetColor (theColor);
185 hasOwnColor = Standard_True;
188 //=======================================================================
189 //function : UnsetColor
191 //=======================================================================
192 void AIS_InteractiveObject::UnsetColor()
194 hasOwnColor = Standard_False;
197 //=======================================================================
200 //=======================================================================
201 void AIS_InteractiveObject::SetWidth(const Standard_Real aValue)
206 //=======================================================================
209 //=======================================================================
210 void AIS_InteractiveObject::UnsetWidth()
215 //=======================================================================
216 //function : Material
218 //=======================================================================
219 Graphic3d_NameOfMaterial AIS_InteractiveObject::Material() const
221 return myDrawer->ShadingAspect()->Material().Name();
224 //=======================================================================
225 //function : SetMaterial
227 //=======================================================================
228 void AIS_InteractiveObject::SetMaterial (const Graphic3d_MaterialAspect& theMaterial)
230 if (!myDrawer->HasOwnShadingAspect())
232 myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
233 if (myDrawer->HasLink())
235 *myDrawer->ShadingAspect()->Aspect() = *myDrawer->Link()->ShadingAspect()->Aspect();
239 myDrawer->ShadingAspect()->SetMaterial (theMaterial);
240 hasOwnMaterial = Standard_True;
243 //=======================================================================
244 //function : UnsetMaterial
246 //=======================================================================
247 void AIS_InteractiveObject::UnsetMaterial()
254 if (HasColor() || IsTransparent())
256 if(myDrawer->HasLink())
258 myDrawer->ShadingAspect()->SetMaterial (AIS_GraphicTool::GetMaterial (myDrawer->Link()));
263 SetColor (myDrawer->Color());
268 SetTransparency (myDrawer->Transparency());
273 Handle(Prs3d_ShadingAspect) anAspect;
274 myDrawer->SetShadingAspect (anAspect);
277 hasOwnMaterial = Standard_False;
280 //=======================================================================
281 //function : SetTransparency
283 //=======================================================================
284 void AIS_InteractiveObject::SetTransparency (const Standard_Real theValue)
286 if (!myDrawer->HasOwnShadingAspect())
288 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
289 if(myDrawer->HasLink())
290 myDrawer->ShadingAspect()->SetMaterial(AIS_GraphicTool::GetMaterial(myDrawer->Link()));
293 myDrawer->ShadingAspect()->Aspect()->ChangeFrontMaterial().SetTransparency (Standard_ShortReal(theValue));
294 myDrawer->ShadingAspect()->Aspect()->ChangeBackMaterial() .SetTransparency (Standard_ShortReal(theValue));
295 myDrawer->SetTransparency (Standard_ShortReal(theValue));
298 //=======================================================================
299 //function : UnsetTransparency
301 //=======================================================================
302 void AIS_InteractiveObject::UnsetTransparency()
304 if(HasColor() || HasMaterial() )
306 myDrawer->ShadingAspect()->Aspect()->ChangeFrontMaterial().SetTransparency (0.0f);
307 myDrawer->ShadingAspect()->Aspect()->ChangeBackMaterial() .SetTransparency (0.0f);
310 Handle (Prs3d_ShadingAspect) SA;
311 myDrawer->SetShadingAspect(SA);
313 myDrawer->SetTransparency (0.0f);
315 //=======================================================================
316 //function : Transparency
318 //=======================================================================
319 Standard_Real AIS_InteractiveObject::Transparency() const
321 return (myDrawer->Transparency() <= 0.005f ? 0.0 : myDrawer->Transparency());
322 // Graphic3d_MaterialAspect Mat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
323 // return Mat.Transparency();
326 //=======================================================================
327 //function : UnsetAttributes
329 //=======================================================================
330 void AIS_InteractiveObject::UnsetAttributes()
332 SelectMgr_SelectableObject::UnsetAttributes();
334 hasOwnColor = Standard_False;
335 hasOwnMaterial = Standard_False;
337 myDrawer->SetTransparency (0.0f);
340 //=======================================================================
343 //=======================================================================
344 void AIS_InteractiveObject::MustRecomputePrs(const Standard_Integer ) const
347 //=======================================================================
350 //=======================================================================
351 const TColStd_ListOfInteger& AIS_InteractiveObject::ListOfRecomputeModes() const
352 {return myToRecomputeModes;}
354 //=======================================================================
357 //=======================================================================
358 void AIS_InteractiveObject::SetRecomputeOk()
359 {myToRecomputeModes.Clear();}
362 //=======================================================================
363 //function : AcceptDisplayMode
365 //=======================================================================
367 Standard_Boolean AIS_InteractiveObject::AcceptDisplayMode(const Standard_Integer ) const
368 {return Standard_True;}
370 //=======================================================================
371 //function : DefaultDisplayMode
373 //=======================================================================
375 Standard_Integer AIS_InteractiveObject::DefaultDisplayMode() const
379 //=======================================================================
380 //function : SetInfiniteState
382 //=======================================================================
383 void AIS_InteractiveObject::SetInfiniteState(const Standard_Boolean aFlag)
385 myInfiniteState = aFlag;
386 Handle(Prs3d_Presentation) P;
388 for(Standard_Integer i =1; i<=myPresentations.Length();i++)
390 P = myPresentations(i).Presentation()->Presentation();
392 P->SetInfiniteState(myInfiniteState);
396 //=======================================================================
397 //function : HasPresentation
399 //=======================================================================
400 Standard_Boolean AIS_InteractiveObject::HasPresentation() const
402 return HasInteractiveContext()
403 && myCTXPtr->MainPrsMgr()->HasPresentation (this, myDrawer->DisplayMode());
406 //=======================================================================
407 //function : Presentation
409 //=======================================================================
410 Handle(Prs3d_Presentation) AIS_InteractiveObject::Presentation() const
412 if (!HasInteractiveContext())
414 return Handle(Prs3d_Presentation)();
417 Handle(PrsMgr_Presentation) aPrs = myCTXPtr->MainPrsMgr()->Presentation (this, myDrawer->DisplayMode(), false);
418 return !aPrs.IsNull()
419 ? aPrs->Presentation()
420 : Handle(Prs3d_Presentation)();
423 //=======================================================================
424 //function : SetAspect
426 //=======================================================================
427 void AIS_InteractiveObject::SetAspect(const Handle(Prs3d_BasicAspect)& anAspect)
430 if( HasPresentation() ) {
431 Handle(Prs3d_Presentation) prs = Presentation();
432 { Handle(Prs3d_ShadingAspect) aspect =
433 Handle(Prs3d_ShadingAspect)::DownCast(anAspect);
434 if( !aspect.IsNull() ) {
435 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
439 { Handle(Prs3d_LineAspect) aspect =
440 Handle(Prs3d_LineAspect)::DownCast(anAspect);
441 if( !aspect.IsNull() ) {
442 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
446 { Handle(Prs3d_PointAspect) aspect =
447 Handle(Prs3d_PointAspect)::DownCast(anAspect);
448 if( !aspect.IsNull() ) {
449 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
453 { Handle(Prs3d_TextAspect) aspect =
454 Handle(Prs3d_TextAspect)::DownCast(anAspect);
455 if( !aspect.IsNull() ) {
456 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
463 //=======================================================================
464 //function : SetPolygonOffsets
466 //=======================================================================
467 void AIS_InteractiveObject::SetPolygonOffsets(const Standard_Integer aMode,
468 const Standard_ShortReal aFactor,
469 const Standard_ShortReal aUnits)
471 if ( !HasPolygonOffsets() )
472 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
474 myDrawer->ShadingAspect()->Aspect()->SetPolygonOffsets( aMode, aFactor, aUnits );
476 // Modify existing presentations
477 for (Standard_Integer aPrsIter = 1, n = myPresentations.Length(); aPrsIter <= n; ++aPrsIter)
479 const Handle(PrsMgr_Presentation)& aPrs3d = myPresentations (aPrsIter).Presentation();
480 if ( !aPrs3d.IsNull() ) {
481 const Handle(Graphic3d_Structure)& aStruct = aPrs3d->Presentation();
482 if( !aStruct.IsNull() ) {
483 // Workaround for issue 23115: Need to update also groups, because their
484 // face aspect ALWAYS overrides the structure's.
485 const Graphic3d_SequenceOfGroup& aGroups = aStruct->Groups();
486 for (Graphic3d_SequenceOfGroup::Iterator aGroupIter (aGroups); aGroupIter.More(); aGroupIter.Next())
488 Handle(Graphic3d_Group)& aGrp = aGroupIter.ChangeValue();
490 || !aGrp->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
495 Handle(Graphic3d_AspectFillArea3d) aFaceAsp = aGrp->FillAreaAspect();
496 aFaceAsp->SetPolygonOffsets(aMode, aFactor, aUnits);
497 aGrp->SetGroupPrimitivesAspect(aFaceAsp);
504 //=======================================================================
505 //function : HasPolygonOffsets
507 //=======================================================================
508 Standard_Boolean AIS_InteractiveObject::HasPolygonOffsets() const
510 return !( myDrawer->ShadingAspect().IsNull() ||
511 ( myDrawer->HasLink() &&
512 myDrawer->ShadingAspect() == myDrawer->Link()->ShadingAspect() ) );
515 //=======================================================================
516 //function : PolygonOffsets
518 //=======================================================================
519 void AIS_InteractiveObject::PolygonOffsets(Standard_Integer& aMode,
520 Standard_ShortReal& aFactor,
521 Standard_ShortReal& aUnits) const
523 if( HasPolygonOffsets() )
524 myDrawer->ShadingAspect()->Aspect()->PolygonOffsets( aMode, aFactor, aUnits );
527 //=======================================================================
528 //function : BoundingBox
529 //purpose : Returns bounding box of object correspondingly to its
530 // current display mode
531 //=======================================================================
532 void AIS_InteractiveObject::BoundingBox (Bnd_Box& theBndBox)
534 if (myDrawer->DisplayMode() == -1)
536 if (!myPresentations.IsEmpty())
538 const Handle(PrsMgr_Presentation)& aPrs3d = myPresentations.First().Presentation();
539 const Handle(Graphic3d_Structure)& aStruct = aPrs3d->Presentation();
540 const Graphic3d_BndBox3d& aBndBox = aStruct->CStructure()->BoundingBox();
542 if (!aBndBox.IsValid())
548 theBndBox.Update (aBndBox.CornerMin().x(), aBndBox.CornerMin().y(), aBndBox.CornerMin().z(),
549 aBndBox.CornerMax().x(), aBndBox.CornerMax().y(), aBndBox.CornerMax().z());
554 for (PrsMgr_ListOfPresentableObjectsIter aPrsIter (Children()); aPrsIter.More(); aPrsIter.Next())
556 Handle(AIS_InteractiveObject) aChild (Handle(AIS_InteractiveObject)::DownCast (aPrsIter.Value()));
562 aChild->BoundingBox (aBox);
563 theBndBox.Add (aBox);
570 for (Standard_Integer aPrsIter = 1; aPrsIter <= myPresentations.Length(); ++aPrsIter)
572 if (myPresentations (aPrsIter).Mode() == myDrawer->DisplayMode())
574 const Handle(PrsMgr_Presentation)& aPrs3d = myPresentations (aPrsIter).Presentation();
575 const Handle(Graphic3d_Structure)& aStruct = aPrs3d->Presentation();
576 const Graphic3d_BndBox3d& aBndBox = aStruct->CStructure()->BoundingBox();
578 if (!aBndBox.IsValid())
584 theBndBox.Update (aBndBox.CornerMin().x(), aBndBox.CornerMin().y(), aBndBox.CornerMin().z(),
585 aBndBox.CornerMax().x(), aBndBox.CornerMax().y(), aBndBox.CornerMax().z());
592 //=======================================================================
593 //function : SetIsoOnTriangulation
594 //purpose : Enables or disables isoline on triangulation building
595 //=======================================================================
596 void AIS_InteractiveObject::SetIsoOnTriangulation (const Standard_Boolean theIsEnabled)
598 myDrawer->SetIsoOnTriangulation (theIsEnabled);
601 //=======================================================================
602 //function : SynchronizeAspects
604 //=======================================================================
605 void AIS_InteractiveObject::SynchronizeAspects()
607 for (PrsMgr_Presentations::Iterator aPrsIter (myPresentations); aPrsIter.More(); aPrsIter.Next())
609 const Handle(PrsMgr_Presentation)& aPrs3d = aPrsIter.ChangeValue().Presentation();
611 || aPrs3d->Presentation().IsNull())
616 for (Graphic3d_SequenceOfGroup::Iterator aGroupIter (aPrs3d->Presentation()->Groups()); aGroupIter.More(); aGroupIter.Next())
618 Handle(Graphic3d_Group)& aGrp = aGroupIter.ChangeValue();
624 Handle(Graphic3d_AspectLine3d) aLineAspect = aGrp->LineAspect();
625 Handle(Graphic3d_AspectFillArea3d) aFaceAspect = aGrp->FillAreaAspect();
626 Handle(Graphic3d_AspectMarker3d) aMarkerAspect = aGrp->MarkerAspect();
627 Handle(Graphic3d_AspectText3d) aTextAspect = aGrp->TextAspect();
628 if (!aLineAspect.IsNull())
630 aGrp->SetGroupPrimitivesAspect (aLineAspect);
632 if (!aFaceAspect.IsNull())
634 aGrp->SetGroupPrimitivesAspect (aFaceAspect);
636 if (!aMarkerAspect.IsNull())
638 aGrp->SetGroupPrimitivesAspect (aMarkerAspect);
640 if (!aTextAspect.IsNull())
642 aGrp->SetGroupPrimitivesAspect (aTextAspect);