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 // Modified : 22/03/04 ; SAN : OCC4895 High-level interface for controlling polygon offsets
19 #include <AIS_GraphicTool.hxx>
20 #include <AIS_InteractiveContext.hxx>
21 #include <AIS_InteractiveObject.hxx>
22 #include <Aspect_PolygonOffsetMode.hxx>
23 #include <Bnd_Box.hxx>
24 #include <Graphic3d_AspectFillArea3d.hxx>
25 #include <Graphic3d_AspectLine3d.hxx>
26 #include <Graphic3d_AspectMarker3d.hxx>
27 #include <Graphic3d_AspectText3d.hxx>
28 #include <Graphic3d_BndBox4f.hxx>
29 #include <Graphic3d_CStructure.hxx>
30 #include <Graphic3d_Group.hxx>
31 #include <Graphic3d_MaterialAspect.hxx>
32 #include <Graphic3d_Structure.hxx>
33 #include <Prs3d_BasicAspect.hxx>
34 #include <Prs3d_LineAspect.hxx>
35 #include <Prs3d_PointAspect.hxx>
36 #include <Prs3d_Presentation.hxx>
37 #include <Prs3d_Root.hxx>
38 #include <Prs3d_ShadingAspect.hxx>
39 #include <Prs3d_TextAspect.hxx>
40 #include <PrsMgr_ModedPresentation.hxx>
41 #include <PrsMgr_PresentationManager3d.hxx>
42 #include <Quantity_Color.hxx>
43 #include <Standard_Transient.hxx>
44 #include <Standard_Type.hxx>
45 #include <TColStd_ListIteratorOfListOfInteger.hxx>
47 IMPLEMENT_STANDARD_RTTIEXT(AIS_InteractiveObject,SelectMgr_SelectableObject)
49 //=======================================================================
50 //function : AIS_InteractiveObject
52 //=======================================================================
53 AIS_InteractiveObject::
54 AIS_InteractiveObject(const PrsMgr_TypeOfPresentation3d aTypeOfPresentation3d):
55 SelectMgr_SelectableObject(aTypeOfPresentation3d),
57 myOwnColor(Quantity_NOC_WHITE),
58 myOwnMaterial(Graphic3d_NOM_DEFAULT),
61 myInfiniteState(Standard_False),
62 hasOwnColor(Standard_False),
63 hasOwnMaterial(Standard_False),
64 myCurrentFacingModel(Aspect_TOFM_BOTH_SIDE),
65 myRecomputeEveryPrs(Standard_True),
70 SetCurrentFacingModel();
73 //=======================================================================
74 //function : Redisplay
76 //=======================================================================
77 void AIS_InteractiveObject::Redisplay (const Standard_Boolean AllModes)
82 myCTXPtr->Redisplay (this, Standard_False, AllModes);
85 //=======================================================================
88 //=======================================================================
90 AIS_KindOfInteractive AIS_InteractiveObject::Type() const
91 {return AIS_KOI_None;}
93 //=======================================================================
94 //function : Signature
96 //=======================================================================
98 Standard_Integer AIS_InteractiveObject::Signature() const
101 //=======================================================================
102 //function : RecomputeEveryPrs
104 //=======================================================================
106 Standard_Boolean AIS_InteractiveObject::RecomputeEveryPrs() const
107 {return myRecomputeEveryPrs;}
109 //=======================================================================
112 //=======================================================================
113 Handle(AIS_InteractiveContext) AIS_InteractiveObject::GetContext() const
118 //=======================================================================
119 //function : SetContext
121 //=======================================================================
122 void AIS_InteractiveObject::SetContext (const Handle(AIS_InteractiveContext)& theCtx)
124 if (myCTXPtr == theCtx.get())
129 myCTXPtr = theCtx.get();
130 if (!theCtx.IsNull())
132 myDrawer->Link (theCtx->DefaultDrawer());
136 //=======================================================================
139 //=======================================================================
140 Standard_Boolean AIS_InteractiveObject::HasOwner() const
142 return (!myOwner.IsNull());
147 //=======================================================================
150 //=======================================================================
151 void AIS_InteractiveObject::ClearOwner()
156 //=======================================================================
159 //=======================================================================
160 void AIS_InteractiveObject::SetDisplayMode(const Standard_Integer aMode)
162 if( AcceptDisplayMode(aMode) )
163 myDisplayMode = aMode;
166 //=======================================================================
169 //=======================================================================
170 void AIS_InteractiveObject::SetCurrentFacingModel(const Aspect_TypeOfFacingModel aModel) {
171 myCurrentFacingModel = aModel;
174 //=======================================================================
175 //function : CurrentFacingModel
177 //=======================================================================
179 Aspect_TypeOfFacingModel AIS_InteractiveObject::CurrentFacingModel() const {
180 return myCurrentFacingModel;
183 //=======================================================================
184 //function : SetColor
186 //=======================================================================
188 void AIS_InteractiveObject::SetColor(const Quantity_NameOfColor aColor)
190 SetColor(Quantity_Color(aColor));
193 //=======================================================================
194 //function : SetColor
196 //=======================================================================
198 void AIS_InteractiveObject::SetColor(const Quantity_Color &aColor)
201 hasOwnColor = Standard_True;
204 //=======================================================================
205 //function : UnsetColor
207 //=======================================================================
208 void AIS_InteractiveObject::UnsetColor()
210 hasOwnColor = Standard_False;
213 //=======================================================================
216 //=======================================================================
217 void AIS_InteractiveObject::SetWidth(const Standard_Real aValue)
222 //=======================================================================
225 //=======================================================================
226 void AIS_InteractiveObject::UnsetWidth()
232 //=======================================================================
233 //function : SetMaterial
235 //=======================================================================
236 void AIS_InteractiveObject::SetMaterial (const Graphic3d_NameOfMaterial theName)
238 if (!myDrawer->HasOwnShadingAspect())
240 myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
241 if (myDrawer->HasLink())
243 *myDrawer->ShadingAspect()->Aspect() = *myDrawer->Link()->ShadingAspect()->Aspect();
246 myDrawer->ShadingAspect()->SetMaterial (theName);
247 myOwnMaterial = theName;
248 hasOwnMaterial = Standard_True;
251 //=======================================================================
252 //function : SetMaterial
254 //=======================================================================
255 void AIS_InteractiveObject::SetMaterial (const Graphic3d_MaterialAspect& theMaterial)
257 if (!myDrawer->HasOwnShadingAspect())
259 myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
260 if (myDrawer->HasLink())
262 *myDrawer->ShadingAspect()->Aspect() = *myDrawer->Link()->ShadingAspect()->Aspect();
265 myDrawer->ShadingAspect()->SetMaterial (theMaterial);
266 hasOwnMaterial = Standard_True;
269 //=======================================================================
270 //function : UnsetMaterial
272 //=======================================================================
273 void AIS_InteractiveObject::UnsetMaterial()
280 if (HasColor() || IsTransparent())
282 if(myDrawer->HasLink())
284 myDrawer->ShadingAspect()->SetMaterial (AIS_GraphicTool::GetMaterial (myDrawer->Link()));
289 SetColor (myOwnColor);
294 SetTransparency (myTransparency);
299 Handle(Prs3d_ShadingAspect) anAspect;
300 myDrawer->SetShadingAspect (anAspect);
303 hasOwnMaterial = Standard_False;
306 //=======================================================================
307 //function : SetTransparency
309 //=======================================================================
310 void AIS_InteractiveObject::SetTransparency(const Standard_Real aValue)
312 if (!myDrawer->HasOwnShadingAspect())
314 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
315 if(myDrawer->HasLink())
316 myDrawer->ShadingAspect()->SetMaterial(AIS_GraphicTool::GetMaterial(myDrawer->Link()));
318 Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
319 Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
320 FMat.SetTransparency(aValue); BMat.SetTransparency(aValue);
321 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
322 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
323 myTransparency = aValue;
326 //=======================================================================
327 //function : UnsetTransparency
329 //=======================================================================
330 void AIS_InteractiveObject::UnsetTransparency()
332 if(HasColor() || HasMaterial() )
334 Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
335 Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
336 FMat.SetTransparency(0.); BMat.SetTransparency(0.);
337 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
338 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
341 Handle (Prs3d_ShadingAspect) SA;
342 myDrawer->SetShadingAspect(SA);
346 //=======================================================================
347 //function : Transparency
349 //=======================================================================
350 Standard_Real AIS_InteractiveObject::Transparency() const
352 return (myTransparency<=0.05 ? 0 : myTransparency);
353 // Graphic3d_MaterialAspect Mat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
354 // return Mat.Transparency();
357 //=======================================================================
358 //function : UnsetAttributes
360 //=======================================================================
361 void AIS_InteractiveObject::UnsetAttributes()
363 SelectMgr_SelectableObject::UnsetAttributes();
365 hasOwnColor = Standard_False;
366 hasOwnMaterial = Standard_False;
368 myTransparency = 0.0;
371 //=======================================================================
374 //=======================================================================
375 void AIS_InteractiveObject::MustRecomputePrs(const Standard_Integer ) const
378 //=======================================================================
381 //=======================================================================
382 const TColStd_ListOfInteger& AIS_InteractiveObject::ListOfRecomputeModes() const
383 {return myToRecomputeModes;}
385 //=======================================================================
388 //=======================================================================
389 void AIS_InteractiveObject::SetRecomputeOk()
390 {myToRecomputeModes.Clear();}
393 //=======================================================================
394 //function : AcceptDisplayMode
396 //=======================================================================
398 Standard_Boolean AIS_InteractiveObject::AcceptDisplayMode(const Standard_Integer ) const
399 {return Standard_True;}
401 //=======================================================================
402 //function : DefaultDisplayMode
404 //=======================================================================
406 Standard_Integer AIS_InteractiveObject::DefaultDisplayMode() const
410 //=======================================================================
411 //function : SetInfiniteState
413 //=======================================================================
414 void AIS_InteractiveObject::SetInfiniteState(const Standard_Boolean aFlag)
416 myInfiniteState = aFlag;
417 Handle(Prs3d_Presentation) P;
419 for(Standard_Integer i =1; i<=myPresentations.Length();i++)
421 P = myPresentations(i).Presentation()->Presentation();
423 P->SetInfiniteState(myInfiniteState);
427 //=======================================================================
428 //function : HasPresentation
430 //=======================================================================
431 Standard_Boolean AIS_InteractiveObject::HasPresentation() const
433 return !GetContext().IsNull()
434 && GetContext()->MainPrsMgr()->HasPresentation (this, myDisplayMode);
437 //=======================================================================
438 //function : Presentation
440 //=======================================================================
441 Handle(Prs3d_Presentation) AIS_InteractiveObject::Presentation() const
443 return HasPresentation()
444 ? GetContext()->MainPrsMgr()->Presentation (this, myDisplayMode)->Presentation()
445 : Handle(Prs3d_Presentation)();
448 //=======================================================================
449 //function : SetAspect
451 //=======================================================================
452 void AIS_InteractiveObject::SetAspect(const Handle(Prs3d_BasicAspect)& anAspect)
455 if( HasPresentation() ) {
456 Handle(Prs3d_Presentation) prs = Presentation();
457 { Handle(Prs3d_ShadingAspect) aspect =
458 Handle(Prs3d_ShadingAspect)::DownCast(anAspect);
459 if( !aspect.IsNull() ) {
460 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
464 { Handle(Prs3d_LineAspect) aspect =
465 Handle(Prs3d_LineAspect)::DownCast(anAspect);
466 if( !aspect.IsNull() ) {
467 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
471 { Handle(Prs3d_PointAspect) aspect =
472 Handle(Prs3d_PointAspect)::DownCast(anAspect);
473 if( !aspect.IsNull() ) {
474 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
478 { Handle(Prs3d_TextAspect) aspect =
479 Handle(Prs3d_TextAspect)::DownCast(anAspect);
480 if( !aspect.IsNull() ) {
481 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
488 //=======================================================================
489 //function : SetPolygonOffsets
491 //=======================================================================
492 void AIS_InteractiveObject::SetPolygonOffsets(const Standard_Integer aMode,
493 const Standard_ShortReal aFactor,
494 const Standard_ShortReal aUnits)
496 if ( !HasPolygonOffsets() )
497 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
499 myDrawer->ShadingAspect()->Aspect()->SetPolygonOffsets( aMode, aFactor, aUnits );
501 // Modify existing presentations
502 for (Standard_Integer aPrsIter = 1, n = myPresentations.Length(); aPrsIter <= n; ++aPrsIter)
504 const Handle(PrsMgr_Presentation)& aPrs3d = myPresentations (aPrsIter).Presentation();
505 if ( !aPrs3d.IsNull() ) {
506 const Handle(Graphic3d_Structure)& aStruct = aPrs3d->Presentation();
507 if( !aStruct.IsNull() ) {
508 // Workaround for issue 23115: Need to update also groups, because their
509 // face aspect ALWAYS overrides the structure's.
510 const Graphic3d_SequenceOfGroup& aGroups = aStruct->Groups();
511 for (Graphic3d_SequenceOfGroup::Iterator aGroupIter (aGroups); aGroupIter.More(); aGroupIter.Next())
513 Handle(Graphic3d_Group)& aGrp = aGroupIter.ChangeValue();
515 || !aGrp->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
520 Handle(Graphic3d_AspectFillArea3d) aFaceAsp = new Graphic3d_AspectFillArea3d();
521 Handle(Graphic3d_AspectLine3d) aLineAsp = new Graphic3d_AspectLine3d();
522 Handle(Graphic3d_AspectMarker3d) aPntAsp = new Graphic3d_AspectMarker3d();
523 Handle(Graphic3d_AspectText3d) aTextAsp = new Graphic3d_AspectText3d();
524 // TODO: Add methods for retrieving individual aspects from Graphic3d_Group
525 aGrp->GroupPrimitivesAspect(aLineAsp, aTextAsp, aPntAsp, aFaceAsp);
526 aFaceAsp->SetPolygonOffsets(aMode, aFactor, aUnits);
527 aGrp->SetGroupPrimitivesAspect(aFaceAsp);
534 //=======================================================================
535 //function : HasPolygonOffsets
537 //=======================================================================
538 Standard_Boolean AIS_InteractiveObject::HasPolygonOffsets() const
540 return !( myDrawer->ShadingAspect().IsNull() ||
541 ( myDrawer->HasLink() &&
542 myDrawer->ShadingAspect() == myDrawer->Link()->ShadingAspect() ) );
545 //=======================================================================
546 //function : PolygonOffsets
548 //=======================================================================
549 void AIS_InteractiveObject::PolygonOffsets(Standard_Integer& aMode,
550 Standard_ShortReal& aFactor,
551 Standard_ShortReal& aUnits) const
553 if( HasPolygonOffsets() )
554 myDrawer->ShadingAspect()->Aspect()->PolygonOffsets( aMode, aFactor, aUnits );
557 //=======================================================================
558 //function : BoundingBox
559 //purpose : Returns bounding box of object correspondingly to its
560 // current display mode
561 //=======================================================================
562 void AIS_InteractiveObject::BoundingBox (Bnd_Box& theBndBox)
564 if (myDisplayMode == -1)
566 if (!myPresentations.IsEmpty())
568 const Handle(PrsMgr_Presentation)& aPrs3d = myPresentations.First().Presentation();
569 const Handle(Graphic3d_Structure)& aStruct = aPrs3d->Presentation();
570 const Graphic3d_BndBox3d& aBndBox = aStruct->CStructure()->BoundingBox();
572 if (!aBndBox.IsValid())
578 theBndBox.Update (aBndBox.CornerMin().x(), aBndBox.CornerMin().y(), aBndBox.CornerMin().z(),
579 aBndBox.CornerMax().x(), aBndBox.CornerMax().y(), aBndBox.CornerMax().z());
584 for (PrsMgr_ListOfPresentableObjectsIter aPrsIter (Children()); aPrsIter.More(); aPrsIter.Next())
586 Handle(AIS_InteractiveObject) aChild (Handle(AIS_InteractiveObject)::DownCast (aPrsIter.Value()));
592 aChild->BoundingBox (aBox);
593 theBndBox.Add (aBox);
600 for (Standard_Integer aPrsIter = 1; aPrsIter <= myPresentations.Length(); ++aPrsIter)
602 if (myPresentations (aPrsIter).Mode() == myDisplayMode)
604 const Handle(PrsMgr_Presentation)& aPrs3d = myPresentations (aPrsIter).Presentation();
605 const Handle(Graphic3d_Structure)& aStruct = aPrs3d->Presentation();
606 const Graphic3d_BndBox3d& aBndBox = aStruct->CStructure()->BoundingBox();
608 if (!aBndBox.IsValid())
614 theBndBox.Update (aBndBox.CornerMin().x(), aBndBox.CornerMin().y(), aBndBox.CornerMin().z(),
615 aBndBox.CornerMax().x(), aBndBox.CornerMax().y(), aBndBox.CornerMax().z());
622 //=======================================================================
623 //function : SetIsoOnTriangulation
624 //purpose : Enables or disables isoline on triangulation building
625 //=======================================================================
626 void AIS_InteractiveObject::SetIsoOnTriangulation (const Standard_Boolean theIsEnabled)
628 myDrawer->SetIsoOnTriangulation (theIsEnabled);
631 //=======================================================================
632 //function : SynchronizeAspects
634 //=======================================================================
635 void AIS_InteractiveObject::SynchronizeAspects()
637 for (PrsMgr_Presentations::Iterator aPrsIter (myPresentations); aPrsIter.More(); aPrsIter.Next())
639 const Handle(PrsMgr_Presentation)& aPrs3d = aPrsIter.ChangeValue().Presentation();
641 || aPrs3d->Presentation().IsNull())
646 for (Graphic3d_SequenceOfGroup::Iterator aGroupIter (aPrs3d->Presentation()->Groups()); aGroupIter.More(); aGroupIter.Next())
648 Handle(Graphic3d_Group)& aGrp = aGroupIter.ChangeValue();
654 Handle(Graphic3d_AspectLine3d) aLineAspect = aGrp->LineAspect();
655 Handle(Graphic3d_AspectFillArea3d) aFaceAspect = aGrp->FillAreaAspect();
656 Handle(Graphic3d_AspectMarker3d) aMarkerAspect = aGrp->MarkerAspect();
657 Handle(Graphic3d_AspectText3d) aTextAspect = aGrp->TextAspect();
658 if (!aLineAspect.IsNull())
660 aGrp->SetGroupPrimitivesAspect (aLineAspect);
662 if (!aFaceAspect.IsNull())
664 aGrp->SetGroupPrimitivesAspect (aFaceAspect);
666 if (!aMarkerAspect.IsNull())
668 aGrp->SetGroupPrimitivesAspect (aMarkerAspect);
670 if (!aTextAspect.IsNull())
672 aGrp->SetGroupPrimitivesAspect (aTextAspect);