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 //=======================================================================
280 void AIS_InteractiveObject::SetMaterial(const Graphic3d_NameOfMaterial aName)
281 //void AIS_InteractiveObject::SetMaterial(const Graphic3d_NameOfPhysicalMaterial aName)
283 if( HasColor() || IsTransparent() || HasMaterial() )
285 myDrawer->ShadingAspect()->SetMaterial(aName);
289 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
290 myDrawer->ShadingAspect()->SetMaterial(aName);
292 myOwnMaterial = aName;
293 hasOwnMaterial = Standard_True;
295 //=======================================================================
296 //function : SetMaterial
298 //=======================================================================
300 void AIS_InteractiveObject::SetMaterial(const Graphic3d_MaterialAspect& aMat)
302 if (HasColor() || IsTransparent() || HasMaterial())
304 myDrawer->ShadingAspect()->SetMaterial(aMat);
308 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
309 myDrawer->ShadingAspect()->SetMaterial(aMat);
311 hasOwnMaterial = Standard_True;
313 //=======================================================================
316 //=======================================================================
317 void AIS_InteractiveObject::UnsetMaterial()
319 if( !HasMaterial() ) return;
320 if (HasColor() || IsTransparent())
322 if(myDrawer->HasLink())
324 myDrawer->ShadingAspect()->SetMaterial (AIS_GraphicTool::GetMaterial (myDrawer->Link()));
326 if (HasColor()) SetColor (myOwnColor);
327 if (IsTransparent()) SetTransparency (myTransparency);
330 Handle(Prs3d_ShadingAspect) SA;
331 myDrawer->SetShadingAspect(SA);
333 hasOwnMaterial = Standard_False;
336 //=======================================================================
337 //function : SetTransparency
339 //=======================================================================
340 void AIS_InteractiveObject::SetTransparency(const Standard_Real aValue)
342 if(!HasColor() && !IsTransparent() && !HasMaterial())
344 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
345 if(myDrawer->HasLink())
346 myDrawer->ShadingAspect()->SetMaterial(AIS_GraphicTool::GetMaterial(myDrawer->Link()));
348 Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
349 Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
350 FMat.SetTransparency(aValue); BMat.SetTransparency(aValue);
351 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
352 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
353 myTransparency = aValue;
356 //=======================================================================
357 //function : UnsetTransparency
359 //=======================================================================
360 void AIS_InteractiveObject::UnsetTransparency()
362 if(HasColor() || HasMaterial() )
364 Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
365 Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
366 FMat.SetTransparency(0.); BMat.SetTransparency(0.);
367 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
368 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
371 Handle (Prs3d_ShadingAspect) SA;
372 myDrawer->SetShadingAspect(SA);
376 //=======================================================================
377 //function : Transparency
379 //=======================================================================
380 Standard_Real AIS_InteractiveObject::Transparency() const
382 return (myTransparency<=0.05 ? 0 : myTransparency);
383 // Graphic3d_MaterialAspect Mat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
384 // return Mat.Transparency();
387 //=======================================================================
388 //function : UnsetAttributes
390 //=======================================================================
391 void AIS_InteractiveObject::UnsetAttributes()
393 SelectMgr_SelectableObject::UnsetAttributes();
395 hasOwnColor = Standard_False;
396 hasOwnMaterial = Standard_False;
398 myTransparency = 0.0;
401 //=======================================================================
404 //=======================================================================
405 void AIS_InteractiveObject::MustRecomputePrs(const Standard_Integer ) const
408 //=======================================================================
411 //=======================================================================
412 const TColStd_ListOfInteger& AIS_InteractiveObject::ListOfRecomputeModes() const
413 {return myToRecomputeModes;}
415 //=======================================================================
418 //=======================================================================
419 void AIS_InteractiveObject::SetRecomputeOk()
420 {myToRecomputeModes.Clear();}
423 //=======================================================================
424 //function : AcceptDisplayMode
426 //=======================================================================
428 Standard_Boolean AIS_InteractiveObject::AcceptDisplayMode(const Standard_Integer ) const
429 {return Standard_True;}
431 //=======================================================================
432 //function : DefaultDisplayMode
434 //=======================================================================
436 Standard_Integer AIS_InteractiveObject::DefaultDisplayMode() const
440 //=======================================================================
441 //function : SetInfiniteState
443 //=======================================================================
444 void AIS_InteractiveObject::SetInfiniteState(const Standard_Boolean aFlag)
446 myInfiniteState = aFlag;
447 Handle(Prs3d_Presentation) P;
449 for(Standard_Integer i =1; i<=myPresentations.Length();i++)
451 P = myPresentations(i).Presentation()->Presentation();
453 P->SetInfiniteState(myInfiniteState);
457 //=======================================================================
458 //function : HasPresentation
460 //=======================================================================
461 Standard_Boolean AIS_InteractiveObject::HasPresentation() const
463 return !GetContext().IsNull()
464 && GetContext()->MainPrsMgr()->HasPresentation (this, myDisplayMode);
467 //=======================================================================
468 //function : Presentation
470 //=======================================================================
471 Handle(Prs3d_Presentation) AIS_InteractiveObject::Presentation() const
473 return HasPresentation()
474 ? GetContext()->MainPrsMgr()->Presentation (this, myDisplayMode)->Presentation()
475 : Handle(Prs3d_Presentation)();
478 //=======================================================================
479 //function : SetAspect
481 //=======================================================================
482 void AIS_InteractiveObject::SetAspect(const Handle(Prs3d_BasicAspect)& anAspect,
483 const Standard_Boolean globalChange) {
485 if( HasPresentation() ) {
486 Handle(Prs3d_Presentation) prs = Presentation();
487 { Handle(Prs3d_ShadingAspect) aspect =
488 Handle(Prs3d_ShadingAspect)::DownCast(anAspect);
489 if( !aspect.IsNull() ) {
490 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
491 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
495 { Handle(Prs3d_LineAspect) aspect =
496 Handle(Prs3d_LineAspect)::DownCast(anAspect);
497 if( !aspect.IsNull() ) {
498 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
499 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
503 { Handle(Prs3d_PointAspect) aspect =
504 Handle(Prs3d_PointAspect)::DownCast(anAspect);
505 if( !aspect.IsNull() ) {
506 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
507 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
511 { Handle(Prs3d_TextAspect) aspect =
512 Handle(Prs3d_TextAspect)::DownCast(anAspect);
513 if( !aspect.IsNull() ) {
514 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
515 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
522 //=======================================================================
523 //function : SetPolygonOffsets
525 //=======================================================================
526 void AIS_InteractiveObject::SetPolygonOffsets(const Standard_Integer aMode,
527 const Standard_ShortReal aFactor,
528 const Standard_ShortReal aUnits)
530 if ( !HasPolygonOffsets() )
531 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
533 myDrawer->ShadingAspect()->Aspect()->SetPolygonOffsets( aMode, aFactor, aUnits );
535 // Modify existing presentations
536 for (Standard_Integer aPrsIter = 1, n = myPresentations.Length(); aPrsIter <= n; ++aPrsIter)
538 const Handle(PrsMgr_Presentation)& aPrs3d = myPresentations (aPrsIter).Presentation();
539 if ( !aPrs3d.IsNull() ) {
540 const Handle(Graphic3d_Structure)& aStruct = aPrs3d->Presentation();
541 if( !aStruct.IsNull() ) {
542 aStruct->SetPrimitivesAspect( myDrawer->ShadingAspect()->Aspect() );
543 // Workaround for issue 23115: Need to update also groups, because their
544 // face aspect ALWAYS overrides the structure's.
545 const Graphic3d_SequenceOfGroup& aGroups = aStruct->Groups();
546 for (Graphic3d_SequenceOfGroup::Iterator aGroupIter (aGroups); aGroupIter.More(); aGroupIter.Next())
548 Handle(Graphic3d_Group)& aGrp = aGroupIter.ChangeValue();
550 || !aGrp->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
555 Handle(Graphic3d_AspectFillArea3d) aFaceAsp = new Graphic3d_AspectFillArea3d();
556 Handle(Graphic3d_AspectLine3d) aLineAsp = new Graphic3d_AspectLine3d();
557 Handle(Graphic3d_AspectMarker3d) aPntAsp = new Graphic3d_AspectMarker3d();
558 Handle(Graphic3d_AspectText3d) aTextAsp = new Graphic3d_AspectText3d();
559 // TODO: Add methods for retrieving individual aspects from Graphic3d_Group
560 aGrp->GroupPrimitivesAspect(aLineAsp, aTextAsp, aPntAsp, aFaceAsp);
561 aFaceAsp->SetPolygonOffsets(aMode, aFactor, aUnits);
562 aGrp->SetGroupPrimitivesAspect(aFaceAsp);
569 //=======================================================================
570 //function : HasPolygonOffsets
572 //=======================================================================
573 Standard_Boolean AIS_InteractiveObject::HasPolygonOffsets() const
575 return !( myDrawer->ShadingAspect().IsNull() ||
576 ( myDrawer->HasLink() &&
577 myDrawer->ShadingAspect() == myDrawer->Link()->ShadingAspect() ) );
580 //=======================================================================
581 //function : PolygonOffsets
583 //=======================================================================
584 void AIS_InteractiveObject::PolygonOffsets(Standard_Integer& aMode,
585 Standard_ShortReal& aFactor,
586 Standard_ShortReal& aUnits) const
588 if( HasPolygonOffsets() )
589 myDrawer->ShadingAspect()->Aspect()->PolygonOffsets( aMode, aFactor, aUnits );
592 //=======================================================================
593 //function : BoundingBox
594 //purpose : Returns bounding box of object correspondingly to its
595 // current display mode
596 //=======================================================================
597 void AIS_InteractiveObject::BoundingBox (Bnd_Box& theBndBox)
599 if (myDisplayMode == -1)
601 if (!myPresentations.IsEmpty())
603 const Handle(PrsMgr_Presentation)& aPrs3d = myPresentations.First().Presentation();
604 const Handle(Graphic3d_Structure)& aStruct = aPrs3d->Presentation();
605 const Graphic3d_BndBox4f& aBndBox = aStruct->CStructure()->BoundingBox();
607 if (!aBndBox.IsValid())
613 theBndBox.Update (static_cast<Standard_Real> (aBndBox.CornerMin().x()),
614 static_cast<Standard_Real> (aBndBox.CornerMin().y()),
615 static_cast<Standard_Real> (aBndBox.CornerMin().z()),
616 static_cast<Standard_Real> (aBndBox.CornerMax().x()),
617 static_cast<Standard_Real> (aBndBox.CornerMax().y()),
618 static_cast<Standard_Real> (aBndBox.CornerMax().z()));
623 for (PrsMgr_ListOfPresentableObjectsIter aPrsIter (Children()); aPrsIter.More(); aPrsIter.Next())
625 const Handle(AIS_InteractiveObject)& aChild = Handle(AIS_InteractiveObject)::DownCast (aPrsIter.Value());
631 aChild->BoundingBox (aBox);
632 theBndBox.Add (aBox);
639 for (Standard_Integer aPrsIter = 1; aPrsIter <= myPresentations.Length(); ++aPrsIter)
641 if (myPresentations (aPrsIter).Mode() == myDisplayMode)
643 const Handle(PrsMgr_Presentation)& aPrs3d = myPresentations (aPrsIter).Presentation();
644 const Handle(Graphic3d_Structure)& aStruct = aPrs3d->Presentation();
645 const Graphic3d_BndBox4f& aBndBox = aStruct->CStructure()->BoundingBox();
647 if (!aBndBox.IsValid())
653 theBndBox.Update (static_cast<Standard_Real> (aBndBox.CornerMin().x()),
654 static_cast<Standard_Real> (aBndBox.CornerMin().y()),
655 static_cast<Standard_Real> (aBndBox.CornerMin().z()),
656 static_cast<Standard_Real> (aBndBox.CornerMax().x()),
657 static_cast<Standard_Real> (aBndBox.CornerMax().y()),
658 static_cast<Standard_Real> (aBndBox.CornerMax().z()));