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)
61 SetCurrentFacingModel();
64 //=======================================================================
65 //function : Redisplay
67 //=======================================================================
68 void AIS_InteractiveObject::Redisplay (const Standard_Boolean AllModes)
73 myCTXPtr->Redisplay (this, Standard_False, AllModes);
76 //=======================================================================
79 //=======================================================================
81 AIS_KindOfInteractive AIS_InteractiveObject::Type() const
82 {return AIS_KOI_None;}
84 //=======================================================================
85 //function : Signature
87 //=======================================================================
89 Standard_Integer AIS_InteractiveObject::Signature() const
92 //=======================================================================
95 //=======================================================================
96 Handle(AIS_InteractiveContext) AIS_InteractiveObject::GetContext() const
101 //=======================================================================
102 //function : SetContext
104 //=======================================================================
105 void AIS_InteractiveObject::SetContext (const Handle(AIS_InteractiveContext)& theCtx)
107 if (myCTXPtr == theCtx.get())
112 myCTXPtr = theCtx.get();
113 if (!theCtx.IsNull())
115 myDrawer->Link (theCtx->DefaultDrawer());
119 //=======================================================================
122 //=======================================================================
123 Standard_Boolean AIS_InteractiveObject::HasOwner() const
125 return (!myOwner.IsNull());
130 //=======================================================================
133 //=======================================================================
134 void AIS_InteractiveObject::ClearOwner()
139 //=======================================================================
140 //function : SetDisplayMode
142 //=======================================================================
143 void AIS_InteractiveObject::SetDisplayMode (const Standard_Integer theMode)
145 if (AcceptDisplayMode (theMode))
147 myDrawer->SetDisplayMode (theMode);
151 //=======================================================================
154 //=======================================================================
155 void AIS_InteractiveObject::SetCurrentFacingModel(const Aspect_TypeOfFacingModel aModel) {
156 myCurrentFacingModel = aModel;
159 //=======================================================================
160 //function : CurrentFacingModel
162 //=======================================================================
164 Aspect_TypeOfFacingModel AIS_InteractiveObject::CurrentFacingModel() const {
165 return myCurrentFacingModel;
168 //=======================================================================
169 //function : SetColor
171 //=======================================================================
173 void AIS_InteractiveObject::SetColor(const Quantity_Color& theColor)
175 myDrawer->SetColor (theColor);
176 hasOwnColor = Standard_True;
179 //=======================================================================
180 //function : UnsetColor
182 //=======================================================================
183 void AIS_InteractiveObject::UnsetColor()
185 hasOwnColor = Standard_False;
188 //=======================================================================
191 //=======================================================================
192 void AIS_InteractiveObject::SetWidth(const Standard_Real aValue)
194 myOwnWidth = (Standard_ShortReal )aValue;
197 //=======================================================================
200 //=======================================================================
201 void AIS_InteractiveObject::UnsetWidth()
206 //=======================================================================
207 //function : Material
209 //=======================================================================
210 Graphic3d_NameOfMaterial AIS_InteractiveObject::Material() const
212 return myDrawer->ShadingAspect()->Material().Name();
215 //=======================================================================
216 //function : SetMaterial
218 //=======================================================================
219 void AIS_InteractiveObject::SetMaterial (const Graphic3d_MaterialAspect& theMaterial)
221 if (!myDrawer->HasOwnShadingAspect())
223 myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
224 if (myDrawer->HasLink())
226 *myDrawer->ShadingAspect()->Aspect() = *myDrawer->Link()->ShadingAspect()->Aspect();
230 myDrawer->ShadingAspect()->SetMaterial (theMaterial);
231 hasOwnMaterial = Standard_True;
234 //=======================================================================
235 //function : UnsetMaterial
237 //=======================================================================
238 void AIS_InteractiveObject::UnsetMaterial()
245 if (HasColor() || IsTransparent())
247 if(myDrawer->HasLink())
249 myDrawer->ShadingAspect()->SetMaterial (AIS_GraphicTool::GetMaterial (myDrawer->Link()));
254 SetColor (myDrawer->Color());
259 SetTransparency (myDrawer->Transparency());
264 Handle(Prs3d_ShadingAspect) anAspect;
265 myDrawer->SetShadingAspect (anAspect);
268 hasOwnMaterial = Standard_False;
271 //=======================================================================
272 //function : SetTransparency
274 //=======================================================================
275 void AIS_InteractiveObject::SetTransparency (const Standard_Real theValue)
277 if (!myDrawer->HasOwnShadingAspect())
279 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
280 if(myDrawer->HasLink())
281 myDrawer->ShadingAspect()->SetMaterial(AIS_GraphicTool::GetMaterial(myDrawer->Link()));
284 myDrawer->ShadingAspect()->Aspect()->ChangeFrontMaterial().SetTransparency (Standard_ShortReal(theValue));
285 myDrawer->ShadingAspect()->Aspect()->ChangeBackMaterial() .SetTransparency (Standard_ShortReal(theValue));
286 myDrawer->SetTransparency (Standard_ShortReal(theValue));
289 //=======================================================================
290 //function : UnsetTransparency
292 //=======================================================================
293 void AIS_InteractiveObject::UnsetTransparency()
295 if(HasColor() || HasMaterial() )
297 myDrawer->ShadingAspect()->Aspect()->ChangeFrontMaterial().SetTransparency (0.0f);
298 myDrawer->ShadingAspect()->Aspect()->ChangeBackMaterial() .SetTransparency (0.0f);
301 Handle (Prs3d_ShadingAspect) SA;
302 myDrawer->SetShadingAspect(SA);
304 myDrawer->SetTransparency (0.0f);
306 //=======================================================================
307 //function : Transparency
309 //=======================================================================
310 Standard_Real AIS_InteractiveObject::Transparency() const
312 return (myDrawer->Transparency() <= 0.005f ? 0.0 : myDrawer->Transparency());
313 // Graphic3d_MaterialAspect Mat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
314 // return Mat.Transparency();
317 //=======================================================================
318 //function : UnsetAttributes
320 //=======================================================================
321 void AIS_InteractiveObject::UnsetAttributes()
323 SelectMgr_SelectableObject::UnsetAttributes();
325 hasOwnColor = Standard_False;
326 hasOwnMaterial = Standard_False;
328 myDrawer->SetTransparency (0.0f);
331 //=======================================================================
332 //function : AcceptDisplayMode
334 //=======================================================================
336 Standard_Boolean AIS_InteractiveObject::AcceptDisplayMode(const Standard_Integer ) const
337 {return Standard_True;}
339 //=======================================================================
340 //function : DefaultDisplayMode
342 //=======================================================================
344 Standard_Integer AIS_InteractiveObject::DefaultDisplayMode() const
348 //=======================================================================
349 //function : SetInfiniteState
351 //=======================================================================
352 void AIS_InteractiveObject::SetInfiniteState(const Standard_Boolean aFlag)
354 myInfiniteState = aFlag;
355 Handle(Prs3d_Presentation) P;
357 for(Standard_Integer i =1; i<=myPresentations.Length();i++)
359 P = myPresentations(i).Presentation()->Presentation();
361 P->SetInfiniteState(myInfiniteState);
365 //=======================================================================
366 //function : HasPresentation
368 //=======================================================================
369 Standard_Boolean AIS_InteractiveObject::HasPresentation() const
371 return HasInteractiveContext()
372 && myCTXPtr->MainPrsMgr()->HasPresentation (this, myDrawer->DisplayMode());
375 //=======================================================================
376 //function : Presentation
378 //=======================================================================
379 Handle(Prs3d_Presentation) AIS_InteractiveObject::Presentation() const
381 if (!HasInteractiveContext())
383 return Handle(Prs3d_Presentation)();
386 Handle(PrsMgr_Presentation) aPrs = myCTXPtr->MainPrsMgr()->Presentation (this, myDrawer->DisplayMode(), false);
387 return !aPrs.IsNull()
388 ? aPrs->Presentation()
389 : Handle(Prs3d_Presentation)();
392 //=======================================================================
393 //function : SetAspect
395 //=======================================================================
396 void AIS_InteractiveObject::SetAspect(const Handle(Prs3d_BasicAspect)& anAspect)
399 if( HasPresentation() ) {
400 Handle(Prs3d_Presentation) prs = Presentation();
401 { Handle(Prs3d_ShadingAspect) aspect =
402 Handle(Prs3d_ShadingAspect)::DownCast(anAspect);
403 if( !aspect.IsNull() ) {
404 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
408 { Handle(Prs3d_LineAspect) aspect =
409 Handle(Prs3d_LineAspect)::DownCast(anAspect);
410 if( !aspect.IsNull() ) {
411 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
415 { Handle(Prs3d_PointAspect) aspect =
416 Handle(Prs3d_PointAspect)::DownCast(anAspect);
417 if( !aspect.IsNull() ) {
418 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
422 { Handle(Prs3d_TextAspect) aspect =
423 Handle(Prs3d_TextAspect)::DownCast(anAspect);
424 if( !aspect.IsNull() ) {
425 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
432 //=======================================================================
433 //function : SetPolygonOffsets
435 //=======================================================================
436 void AIS_InteractiveObject::SetPolygonOffsets(const Standard_Integer aMode,
437 const Standard_ShortReal aFactor,
438 const Standard_ShortReal aUnits)
440 if ( !HasPolygonOffsets() )
441 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
443 myDrawer->ShadingAspect()->Aspect()->SetPolygonOffsets( aMode, aFactor, aUnits );
444 SynchronizeAspects();
447 //=======================================================================
448 //function : HasPolygonOffsets
450 //=======================================================================
451 Standard_Boolean AIS_InteractiveObject::HasPolygonOffsets() const
453 return !( myDrawer->ShadingAspect().IsNull() ||
454 ( myDrawer->HasLink() &&
455 myDrawer->ShadingAspect() == myDrawer->Link()->ShadingAspect() ) );
458 //=======================================================================
459 //function : PolygonOffsets
461 //=======================================================================
462 void AIS_InteractiveObject::PolygonOffsets(Standard_Integer& aMode,
463 Standard_ShortReal& aFactor,
464 Standard_ShortReal& aUnits) const
466 if( HasPolygonOffsets() )
467 myDrawer->ShadingAspect()->Aspect()->PolygonOffsets( aMode, aFactor, aUnits );
470 //=======================================================================
471 //function : BoundingBox
472 //purpose : Returns bounding box of object correspondingly to its
473 // current display mode
474 //=======================================================================
475 void AIS_InteractiveObject::BoundingBox (Bnd_Box& theBndBox)
477 if (myDrawer->DisplayMode() == -1)
479 if (!myPresentations.IsEmpty())
481 const Handle(PrsMgr_Presentation)& aPrs3d = myPresentations.First().Presentation();
482 const Handle(Graphic3d_Structure)& aStruct = aPrs3d->Presentation();
483 const Graphic3d_BndBox3d& aBndBox = aStruct->CStructure()->BoundingBox();
485 if (!aBndBox.IsValid())
491 theBndBox.Update (aBndBox.CornerMin().x(), aBndBox.CornerMin().y(), aBndBox.CornerMin().z(),
492 aBndBox.CornerMax().x(), aBndBox.CornerMax().y(), aBndBox.CornerMax().z());
497 for (PrsMgr_ListOfPresentableObjectsIter aPrsIter (Children()); aPrsIter.More(); aPrsIter.Next())
499 Handle(AIS_InteractiveObject) aChild (Handle(AIS_InteractiveObject)::DownCast (aPrsIter.Value()));
505 aChild->BoundingBox (aBox);
506 theBndBox.Add (aBox);
513 for (Standard_Integer aPrsIter = 1; aPrsIter <= myPresentations.Length(); ++aPrsIter)
515 if (myPresentations (aPrsIter).Mode() == myDrawer->DisplayMode())
517 const Handle(PrsMgr_Presentation)& aPrs3d = myPresentations (aPrsIter).Presentation();
518 const Handle(Graphic3d_Structure)& aStruct = aPrs3d->Presentation();
519 const Graphic3d_BndBox3d& aBndBox = aStruct->CStructure()->BoundingBox();
521 if (!aBndBox.IsValid())
527 theBndBox.Update (aBndBox.CornerMin().x(), aBndBox.CornerMin().y(), aBndBox.CornerMin().z(),
528 aBndBox.CornerMax().x(), aBndBox.CornerMax().y(), aBndBox.CornerMax().z());
535 //=======================================================================
536 //function : SetIsoOnTriangulation
537 //purpose : Enables or disables isoline on triangulation building
538 //=======================================================================
539 void AIS_InteractiveObject::SetIsoOnTriangulation (const Standard_Boolean theIsEnabled)
541 myDrawer->SetIsoOnTriangulation (theIsEnabled);
544 //=======================================================================
545 //function : SynchronizeAspects
547 //=======================================================================
548 void AIS_InteractiveObject::SynchronizeAspects()
550 for (PrsMgr_Presentations::Iterator aPrsIter (myPresentations); aPrsIter.More(); aPrsIter.Next())
552 const Handle(PrsMgr_Presentation)& aPrs3d = aPrsIter.ChangeValue().Presentation();
554 || aPrs3d->Presentation().IsNull())
559 for (Graphic3d_SequenceOfGroup::Iterator aGroupIter (aPrs3d->Presentation()->Groups()); aGroupIter.More(); aGroupIter.Next())
561 if (!aGroupIter.Value().IsNull())
563 aGroupIter.ChangeValue()->SynchronizeAspects();
569 //=======================================================================
570 //function : replaceAspects
572 //=======================================================================
573 void AIS_InteractiveObject::replaceAspects (const Graphic3d_MapOfAspectsToAspects& theMap)
575 if (theMap.IsEmpty())
580 for (PrsMgr_Presentations::Iterator aPrsIter (myPresentations); aPrsIter.More(); aPrsIter.Next())
582 const Handle(PrsMgr_Presentation)& aPrs3d = aPrsIter.ChangeValue().Presentation();
584 || aPrs3d->Presentation().IsNull())
589 for (Graphic3d_SequenceOfGroup::Iterator aGroupIter (aPrs3d->Presentation()->Groups()); aGroupIter.More(); aGroupIter.Next())
591 if (!aGroupIter.Value().IsNull())
593 aGroupIter.ChangeValue()->ReplaceAspects (theMap);