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_InteractiveObject.ixx>
21 #include <Aspect_PolygonOffsetMode.hxx>
22 #include <Prs3d_ShadingAspect.hxx>
23 #include <Prs3d_LineAspect.hxx>
24 #include <Prs3d_PointAspect.hxx>
25 #include <Prs3d_TextAspect.hxx>
26 #include <Prs3d_Presentation.hxx>
27 #include <Prs3d_Root.hxx>
28 #include <PrsMgr_ModedPresentation.hxx>
29 #include <PrsMgr_PresentationManager3d.hxx>
30 #include <TColStd_ListIteratorOfListOfInteger.hxx>
31 #include <AIS_GraphicTool.hxx>
32 #include <Graphic3d_AspectFillArea3d.hxx>
33 #include <Graphic3d_AspectLine3d.hxx>
34 #include <Graphic3d_AspectMarker3d.hxx>
35 #include <Graphic3d_AspectText3d.hxx>
36 #include <Graphic3d_BndBox4f.hxx>
37 #include <Graphic3d_CStructure.hxx>
38 #include <Graphic3d_Group.hxx>
39 #include <Graphic3d_Structure.hxx>
42 //=======================================================================
43 //function : AIS_InteractiveObject
45 //=======================================================================
47 AIS_InteractiveObject::
48 AIS_InteractiveObject(const PrsMgr_TypeOfPresentation3d aTypeOfPresentation3d):
49 SelectMgr_SelectableObject(aTypeOfPresentation3d),
51 myOwnColor(Quantity_NOC_WHITE),
52 myOwnMaterial(Graphic3d_NOM_DEFAULT),
55 myInfiniteState(Standard_False),
56 hasOwnColor(Standard_False),
57 hasOwnMaterial(Standard_False),
58 myCurrentFacingModel(Aspect_TOFM_BOTH_SIDE),
59 myRecomputeEveryPrs(Standard_True),
66 Handle (AIS_InteractiveContext) Bid;
67 myCTXPtr = Bid.operator->();
68 SetCurrentFacingModel();
71 //=======================================================================
72 //function : Redisplay
74 //=======================================================================
75 void AIS_InteractiveObject::Redisplay (const Standard_Boolean AllModes)
80 myCTXPtr->Redisplay (this, Standard_False, AllModes);
83 //=======================================================================
86 //=======================================================================
88 AIS_KindOfInteractive AIS_InteractiveObject::Type() const
89 {return AIS_KOI_None;}
91 //=======================================================================
92 //function : Signature
94 //=======================================================================
96 Standard_Integer AIS_InteractiveObject::Signature() const
99 //=======================================================================
100 //function : RecomputeEveryPrs
102 //=======================================================================
104 Standard_Boolean AIS_InteractiveObject::RecomputeEveryPrs() const
105 {return myRecomputeEveryPrs;}
107 //=======================================================================
110 //=======================================================================
111 Standard_Boolean AIS_InteractiveObject::HasInteractiveContext() const
113 Handle (AIS_InteractiveContext) aNull;
114 return (myCTXPtr != aNull.operator->());
117 //=======================================================================
120 //=======================================================================
121 Handle(AIS_InteractiveContext) AIS_InteractiveObject::GetContext() const
126 //=======================================================================
129 //=======================================================================
130 void AIS_InteractiveObject::SetContext(const Handle(AIS_InteractiveContext)& aCtx)
132 myCTXPtr = aCtx.operator->();
135 myDrawer->Link(aCtx->DefaultDrawer());
138 //=======================================================================
141 //=======================================================================
142 Standard_Boolean AIS_InteractiveObject::HasOwner() const
144 return (!myOwner.IsNull());
149 //=======================================================================
152 //=======================================================================
153 void AIS_InteractiveObject::ClearOwner()
158 //=======================================================================
161 //=======================================================================
162 Standard_Boolean AIS_InteractiveObject::HasUsers() const
164 return (!myUsers.IsEmpty());
168 //=======================================================================
171 //=======================================================================
172 void AIS_InteractiveObject::AddUser(const Handle(Standard_Transient)& aUser)
174 myUsers.Append(aUser);
177 //=======================================================================
180 //=======================================================================
181 void AIS_InteractiveObject::ClearUsers()
187 //=======================================================================
190 //=======================================================================
191 void AIS_InteractiveObject::SetDisplayMode(const Standard_Integer aMode)
193 if( AcceptDisplayMode(aMode) )
194 myDisplayMode = aMode;
198 //=======================================================================
201 //=======================================================================
202 void AIS_InteractiveObject::SetSelectionMode(const Standard_Integer aMode)
204 mySelectionMode = aMode;
209 //=======================================================================
212 //=======================================================================
213 void AIS_InteractiveObject::SetCurrentFacingModel(const Aspect_TypeOfFacingModel aModel) {
214 myCurrentFacingModel = aModel;
217 //=======================================================================
218 //function : CurrentFacingModel
220 //=======================================================================
222 Aspect_TypeOfFacingModel AIS_InteractiveObject::CurrentFacingModel() const {
223 return myCurrentFacingModel;
226 //=======================================================================
227 //function : SetColor
229 //=======================================================================
231 void AIS_InteractiveObject::SetColor(const Quantity_NameOfColor aColor)
233 SetColor(Quantity_Color(aColor));
236 //=======================================================================
237 //function : SetColor
239 //=======================================================================
241 void AIS_InteractiveObject::SetColor(const Quantity_Color &aColor)
244 hasOwnColor = Standard_True;
247 //=======================================================================
248 //function : UnsetColor
250 //=======================================================================
251 void AIS_InteractiveObject::UnsetColor()
253 hasOwnColor = Standard_False;
256 //=======================================================================
259 //=======================================================================
260 void AIS_InteractiveObject::SetWidth(const Standard_Real aValue)
265 //=======================================================================
268 //=======================================================================
269 void AIS_InteractiveObject::UnsetWidth()
275 //=======================================================================
278 //=======================================================================
279 void AIS_InteractiveObject::SetMaterial(const Graphic3d_NameOfMaterial aName)
281 if( HasColor() || IsTransparent() || HasMaterial() )
283 myDrawer->ShadingAspect()->SetMaterial(aName);
287 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
288 myDrawer->ShadingAspect()->SetMaterial(aName);
290 myOwnMaterial = aName;
291 hasOwnMaterial = Standard_True;
294 //=======================================================================
295 //function : SetMaterial
297 //=======================================================================
298 void AIS_InteractiveObject::SetMaterial (const Graphic3d_MaterialAspect& theMaterial)
300 if (!HasColor() && !IsTransparent() && !HasMaterial())
302 myDrawer->SetShadingAspect (new Prs3d_ShadingAspect);
305 myDrawer->ShadingAspect()->SetMaterial (theMaterial);
307 hasOwnMaterial = Standard_True;
310 //=======================================================================
311 //function : UnsetMaterial
313 //=======================================================================
314 void AIS_InteractiveObject::UnsetMaterial()
321 if (HasColor() || IsTransparent())
323 if(myDrawer->HasLink())
325 myDrawer->ShadingAspect()->SetMaterial (AIS_GraphicTool::GetMaterial (myDrawer->Link()));
330 SetColor (myOwnColor);
335 SetTransparency (myTransparency);
340 Handle(Prs3d_ShadingAspect) anAspect;
341 myDrawer->SetShadingAspect (anAspect);
344 hasOwnMaterial = Standard_False;
347 //=======================================================================
348 //function : SetTransparency
350 //=======================================================================
351 void AIS_InteractiveObject::SetTransparency(const Standard_Real aValue)
353 if(!HasColor() && !IsTransparent() && !HasMaterial())
355 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
356 if(myDrawer->HasLink())
357 myDrawer->ShadingAspect()->SetMaterial(AIS_GraphicTool::GetMaterial(myDrawer->Link()));
359 Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
360 Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
361 FMat.SetTransparency(aValue); BMat.SetTransparency(aValue);
362 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
363 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
364 myTransparency = aValue;
367 //=======================================================================
368 //function : UnsetTransparency
370 //=======================================================================
371 void AIS_InteractiveObject::UnsetTransparency()
373 if(HasColor() || HasMaterial() )
375 Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
376 Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
377 FMat.SetTransparency(0.); BMat.SetTransparency(0.);
378 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
379 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
382 Handle (Prs3d_ShadingAspect) SA;
383 myDrawer->SetShadingAspect(SA);
387 //=======================================================================
388 //function : Transparency
390 //=======================================================================
391 Standard_Real AIS_InteractiveObject::Transparency() const
393 return (myTransparency<=0.05 ? 0 : myTransparency);
394 // Graphic3d_MaterialAspect Mat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
395 // return Mat.Transparency();
398 //=======================================================================
399 //function : UnsetAttributes
401 //=======================================================================
402 void AIS_InteractiveObject::UnsetAttributes()
404 SelectMgr_SelectableObject::UnsetAttributes();
406 hasOwnColor = Standard_False;
407 hasOwnMaterial = Standard_False;
409 myTransparency = 0.0;
412 //=======================================================================
415 //=======================================================================
416 void AIS_InteractiveObject::MustRecomputePrs(const Standard_Integer ) const
419 //=======================================================================
422 //=======================================================================
423 const TColStd_ListOfInteger& AIS_InteractiveObject::ListOfRecomputeModes() const
424 {return myToRecomputeModes;}
426 //=======================================================================
429 //=======================================================================
430 void AIS_InteractiveObject::SetRecomputeOk()
431 {myToRecomputeModes.Clear();}
434 //=======================================================================
435 //function : AcceptDisplayMode
437 //=======================================================================
439 Standard_Boolean AIS_InteractiveObject::AcceptDisplayMode(const Standard_Integer ) const
440 {return Standard_True;}
442 //=======================================================================
443 //function : DefaultDisplayMode
445 //=======================================================================
447 Standard_Integer AIS_InteractiveObject::DefaultDisplayMode() const
451 //=======================================================================
452 //function : SetInfiniteState
454 //=======================================================================
455 void AIS_InteractiveObject::SetInfiniteState(const Standard_Boolean aFlag)
457 myInfiniteState = aFlag;
458 Handle(Prs3d_Presentation) P;
460 for(Standard_Integer i =1; i<=myPresentations.Length();i++)
462 P = myPresentations(i).Presentation()->Presentation();
464 P->SetInfiniteState(myInfiniteState);
468 //=======================================================================
469 //function : HasPresentation
471 //=======================================================================
472 Standard_Boolean AIS_InteractiveObject::HasPresentation() const
474 return !GetContext().IsNull()
475 && GetContext()->MainPrsMgr()->HasPresentation (this, myDisplayMode);
478 //=======================================================================
479 //function : Presentation
481 //=======================================================================
482 Handle(Prs3d_Presentation) AIS_InteractiveObject::Presentation() const
484 return HasPresentation()
485 ? GetContext()->MainPrsMgr()->Presentation (this, myDisplayMode)->Presentation()
486 : Handle(Prs3d_Presentation)();
489 //=======================================================================
490 //function : SetAspect
492 //=======================================================================
493 void AIS_InteractiveObject::SetAspect(const Handle(Prs3d_BasicAspect)& anAspect,
494 const Standard_Boolean globalChange) {
496 if( HasPresentation() ) {
497 Handle(Prs3d_Presentation) prs = Presentation();
498 { Handle(Prs3d_ShadingAspect) aspect =
499 Handle(Prs3d_ShadingAspect)::DownCast(anAspect);
500 if( !aspect.IsNull() ) {
501 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
502 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
506 { Handle(Prs3d_LineAspect) aspect =
507 Handle(Prs3d_LineAspect)::DownCast(anAspect);
508 if( !aspect.IsNull() ) {
509 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
510 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
514 { Handle(Prs3d_PointAspect) aspect =
515 Handle(Prs3d_PointAspect)::DownCast(anAspect);
516 if( !aspect.IsNull() ) {
517 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
518 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
522 { Handle(Prs3d_TextAspect) aspect =
523 Handle(Prs3d_TextAspect)::DownCast(anAspect);
524 if( !aspect.IsNull() ) {
525 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
526 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
533 //=======================================================================
534 //function : SetPolygonOffsets
536 //=======================================================================
537 void AIS_InteractiveObject::SetPolygonOffsets(const Standard_Integer aMode,
538 const Standard_ShortReal aFactor,
539 const Standard_ShortReal aUnits)
541 if ( !HasPolygonOffsets() )
542 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
544 myDrawer->ShadingAspect()->Aspect()->SetPolygonOffsets( aMode, aFactor, aUnits );
546 // Modify existing presentations
547 for (Standard_Integer aPrsIter = 1, n = myPresentations.Length(); aPrsIter <= n; ++aPrsIter)
549 const Handle(PrsMgr_Presentation)& aPrs3d = myPresentations (aPrsIter).Presentation();
550 if ( !aPrs3d.IsNull() ) {
551 const Handle(Graphic3d_Structure)& aStruct = aPrs3d->Presentation();
552 if( !aStruct.IsNull() ) {
553 aStruct->SetPrimitivesAspect( myDrawer->ShadingAspect()->Aspect() );
554 // Workaround for issue 23115: Need to update also groups, because their
555 // face aspect ALWAYS overrides the structure's.
556 const Graphic3d_SequenceOfGroup& aGroups = aStruct->Groups();
557 for (Graphic3d_SequenceOfGroup::Iterator aGroupIter (aGroups); aGroupIter.More(); aGroupIter.Next())
559 Handle(Graphic3d_Group)& aGrp = aGroupIter.ChangeValue();
561 || !aGrp->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
566 Handle(Graphic3d_AspectFillArea3d) aFaceAsp = new Graphic3d_AspectFillArea3d();
567 Handle(Graphic3d_AspectLine3d) aLineAsp = new Graphic3d_AspectLine3d();
568 Handle(Graphic3d_AspectMarker3d) aPntAsp = new Graphic3d_AspectMarker3d();
569 Handle(Graphic3d_AspectText3d) aTextAsp = new Graphic3d_AspectText3d();
570 // TODO: Add methods for retrieving individual aspects from Graphic3d_Group
571 aGrp->GroupPrimitivesAspect(aLineAsp, aTextAsp, aPntAsp, aFaceAsp);
572 aFaceAsp->SetPolygonOffsets(aMode, aFactor, aUnits);
573 aGrp->SetGroupPrimitivesAspect(aFaceAsp);
580 //=======================================================================
581 //function : HasPolygonOffsets
583 //=======================================================================
584 Standard_Boolean AIS_InteractiveObject::HasPolygonOffsets() const
586 return !( myDrawer->ShadingAspect().IsNull() ||
587 ( myDrawer->HasLink() &&
588 myDrawer->ShadingAspect() == myDrawer->Link()->ShadingAspect() ) );
591 //=======================================================================
592 //function : PolygonOffsets
594 //=======================================================================
595 void AIS_InteractiveObject::PolygonOffsets(Standard_Integer& aMode,
596 Standard_ShortReal& aFactor,
597 Standard_ShortReal& aUnits) const
599 if( HasPolygonOffsets() )
600 myDrawer->ShadingAspect()->Aspect()->PolygonOffsets( aMode, aFactor, aUnits );
603 //=======================================================================
604 //function : BoundingBox
605 //purpose : Returns bounding box of object correspondingly to its
606 // current display mode
607 //=======================================================================
608 void AIS_InteractiveObject::BoundingBox (Bnd_Box& theBndBox)
610 if (myDisplayMode == -1)
612 if (!myPresentations.IsEmpty())
614 const Handle(PrsMgr_Presentation)& aPrs3d = myPresentations.First().Presentation();
615 const Handle(Graphic3d_Structure)& aStruct = aPrs3d->Presentation();
616 const Graphic3d_BndBox4f& aBndBox = aStruct->CStructure()->BoundingBox();
618 if (!aBndBox.IsValid())
624 theBndBox.Update (static_cast<Standard_Real> (aBndBox.CornerMin().x()),
625 static_cast<Standard_Real> (aBndBox.CornerMin().y()),
626 static_cast<Standard_Real> (aBndBox.CornerMin().z()),
627 static_cast<Standard_Real> (aBndBox.CornerMax().x()),
628 static_cast<Standard_Real> (aBndBox.CornerMax().y()),
629 static_cast<Standard_Real> (aBndBox.CornerMax().z()));
634 for (PrsMgr_ListOfPresentableObjectsIter aPrsIter (Children()); aPrsIter.More(); aPrsIter.Next())
636 Handle(AIS_InteractiveObject) aChild (Handle(AIS_InteractiveObject)::DownCast (aPrsIter.Value()));
642 aChild->BoundingBox (aBox);
643 theBndBox.Add (aBox);
650 for (Standard_Integer aPrsIter = 1; aPrsIter <= myPresentations.Length(); ++aPrsIter)
652 if (myPresentations (aPrsIter).Mode() == myDisplayMode)
654 const Handle(PrsMgr_Presentation)& aPrs3d = myPresentations (aPrsIter).Presentation();
655 const Handle(Graphic3d_Structure)& aStruct = aPrs3d->Presentation();
656 const Graphic3d_BndBox4f& aBndBox = aStruct->CStructure()->BoundingBox();
658 if (!aBndBox.IsValid())
664 theBndBox.Update (static_cast<Standard_Real> (aBndBox.CornerMin().x()),
665 static_cast<Standard_Real> (aBndBox.CornerMin().y()),
666 static_cast<Standard_Real> (aBndBox.CornerMin().z()),
667 static_cast<Standard_Real> (aBndBox.CornerMax().x()),
668 static_cast<Standard_Real> (aBndBox.CornerMax().y()),
669 static_cast<Standard_Real> (aBndBox.CornerMax().z()));