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_Group.hxx>
37 #include <Graphic3d_Structure.hxx>
40 //=======================================================================
41 //function : AIS_InteractiveObject
43 //=======================================================================
45 AIS_InteractiveObject::
46 AIS_InteractiveObject(const PrsMgr_TypeOfPresentation3d aTypeOfPresentation3d):
47 SelectMgr_SelectableObject(aTypeOfPresentation3d),
48 myDrawer(new AIS_Drawer()),
50 myOwnColor(Quantity_NOC_WHITE),
51 myOwnMaterial(Graphic3d_NOM_DEFAULT),
54 myInfiniteState(Standard_False),
55 hasOwnColor(Standard_False),
56 hasOwnMaterial(Standard_False),
57 myCurrentFacingModel(Aspect_TOFM_BOTH_SIDE),
58 myRecomputeEveryPrs(Standard_True),
65 Handle (AIS_InteractiveContext) Bid;
66 myCTXPtr = Bid.operator->();
67 SetCurrentFacingModel();
70 //=======================================================================
71 //function : Redisplay
73 //=======================================================================
75 void AIS_InteractiveObject::Redisplay(const Standard_Boolean AllModes)
81 //=======================================================================
84 //=======================================================================
86 AIS_KindOfInteractive AIS_InteractiveObject::Type() const
87 {return AIS_KOI_None;}
89 //=======================================================================
90 //function : Signature
92 //=======================================================================
94 Standard_Integer AIS_InteractiveObject::Signature() const
97 //=======================================================================
98 //function : RecomputeEveryPrs
100 //=======================================================================
102 Standard_Boolean AIS_InteractiveObject::RecomputeEveryPrs() const
103 {return myRecomputeEveryPrs;}
105 //=======================================================================
108 //=======================================================================
109 Standard_Boolean AIS_InteractiveObject::HasInteractiveContext() const
111 Handle (AIS_InteractiveContext) aNull;
112 return (myCTXPtr != aNull.operator->());
115 //=======================================================================
118 //=======================================================================
119 Handle(AIS_InteractiveContext) AIS_InteractiveObject::GetContext() const
124 //=======================================================================
127 //=======================================================================
128 void AIS_InteractiveObject::SetContext(const Handle(AIS_InteractiveContext)& aCtx)
130 myCTXPtr = aCtx.operator->();
133 if (myDrawer.IsNull()) {
134 myDrawer = new AIS_Drawer;
136 cout << "AIS_InteractiveObject::SetContext DRAWER NUL!" << endl;
139 myDrawer->Link(aCtx->DefaultDrawer());
142 //=======================================================================
145 //=======================================================================
146 Standard_Boolean AIS_InteractiveObject::HasOwner() const
148 return (!myOwner.IsNull());
153 //=======================================================================
156 //=======================================================================
157 void AIS_InteractiveObject::ClearOwner()
162 //=======================================================================
165 //=======================================================================
166 Standard_Boolean AIS_InteractiveObject::HasUsers() const
168 return (!myUsers.IsEmpty());
172 //=======================================================================
175 //=======================================================================
176 void AIS_InteractiveObject::AddUser(const Handle(Standard_Transient)& aUser)
178 myUsers.Append(aUser);
181 //=======================================================================
184 //=======================================================================
185 void AIS_InteractiveObject::ClearUsers()
191 //=======================================================================
194 //=======================================================================
195 void AIS_InteractiveObject::SetDisplayMode(const Standard_Integer aMode)
197 if( AcceptDisplayMode(aMode) )
198 myDisplayMode = aMode;
202 //=======================================================================
205 //=======================================================================
206 void AIS_InteractiveObject::SetSelectionMode(const Standard_Integer aMode)
208 mySelectionMode = aMode;
213 //=======================================================================
216 //=======================================================================
217 void AIS_InteractiveObject::SetCurrentFacingModel(const Aspect_TypeOfFacingModel aModel) {
218 myCurrentFacingModel = aModel;
221 //=======================================================================
222 //function : CurrentFacingModel
224 //=======================================================================
226 Aspect_TypeOfFacingModel AIS_InteractiveObject::CurrentFacingModel() const {
227 return myCurrentFacingModel;
230 //=======================================================================
231 //function : SetColor
233 //=======================================================================
235 void AIS_InteractiveObject::SetColor(const Quantity_NameOfColor aColor)
237 SetColor(Quantity_Color(aColor));
240 //=======================================================================
241 //function : SetColor
243 //=======================================================================
245 void AIS_InteractiveObject::SetColor(const Quantity_Color &aColor)
248 hasOwnColor = Standard_True;
251 //=======================================================================
252 //function : UnsetColor
254 //=======================================================================
255 void AIS_InteractiveObject::UnsetColor()
257 hasOwnColor = Standard_False;
260 //=======================================================================
263 //=======================================================================
264 void AIS_InteractiveObject::SetWidth(const Standard_Real aValue)
269 //=======================================================================
272 //=======================================================================
273 void AIS_InteractiveObject::UnsetWidth()
279 //=======================================================================
282 //=======================================================================
284 void AIS_InteractiveObject::SetMaterial(const Graphic3d_NameOfMaterial aName)
285 //void AIS_InteractiveObject::SetMaterial(const Graphic3d_NameOfPhysicalMaterial aName)
287 if( HasColor() || IsTransparent() || HasMaterial() )
289 myDrawer->ShadingAspect()->SetMaterial(aName);
293 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
295 myDrawer->ShadingAspect()->SetMaterial(aName);
297 myOwnMaterial = aName;
298 hasOwnMaterial = Standard_True;
300 //=======================================================================
301 //function : SetMaterial
303 //=======================================================================
305 void AIS_InteractiveObject::SetMaterial(const Graphic3d_MaterialAspect& aMat)
307 if( HasColor() || IsTransparent() || HasMaterial() )
309 myDrawer->ShadingAspect()->SetMaterial(aMat);
313 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
314 myDrawer->ShadingAspect()->SetMaterial(aMat);
316 hasOwnMaterial = Standard_True;
319 //=======================================================================
322 //=======================================================================
323 void AIS_InteractiveObject::UnsetMaterial()
325 if( !HasMaterial() ) return;
326 if( HasColor() || IsTransparent()) {
327 myDrawer->ShadingAspect()->SetMaterial(
328 AIS_GraphicTool::GetMaterial(myDrawer->Link()));
329 if( HasColor() ) SetColor(myOwnColor);
330 if( IsTransparent() ) SetTransparency(myTransparency);
333 Handle(Prs3d_ShadingAspect) SA;
334 myDrawer->SetShadingAspect(SA);
336 hasOwnMaterial = Standard_False;
339 //=======================================================================
340 //function : SetTransparency
342 //=======================================================================
343 void AIS_InteractiveObject::SetTransparency(const Standard_Real aValue)
347 if(!HasColor() && !IsTransparent() && !HasMaterial() ) {
348 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
349 if(!myDrawer->Link().IsNull())
350 myDrawer->ShadingAspect()->SetMaterial(AIS_GraphicTool::GetMaterial(myDrawer->Link()));
352 Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
353 Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
354 FMat.SetTransparency(aValue); BMat.SetTransparency(aValue);
355 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
356 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
357 myTransparency = aValue;
360 //=======================================================================
361 //function : UnsetTransparency
363 //=======================================================================
364 void AIS_InteractiveObject::UnsetTransparency()
366 if(HasColor() || HasMaterial() )
368 Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
369 Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
370 FMat.SetTransparency(0.); BMat.SetTransparency(0.);
371 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
372 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
375 Handle (Prs3d_ShadingAspect) SA;
376 myDrawer->SetShadingAspect(SA);
380 //=======================================================================
381 //function : Transparency
383 //=======================================================================
384 Standard_Real AIS_InteractiveObject::Transparency() const
386 return (myTransparency<=0.05 ? 0 : myTransparency);
387 // Graphic3d_MaterialAspect Mat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
388 // return Mat.Transparency();
391 //=======================================================================
392 //function : SetAttributes
394 //=======================================================================
396 void AIS_InteractiveObject::SetAttributes(const Handle(AIS_Drawer)& aDrawer)
397 {myDrawer = aDrawer;}
400 //=======================================================================
401 //function : UnsetAttributes
403 //=======================================================================
404 void AIS_InteractiveObject::UnsetAttributes()
406 Handle(AIS_Drawer) dr = new AIS_Drawer();
407 if(myDrawer->HasLink())
408 dr->Link(myDrawer->Link());
410 hasOwnColor = Standard_False;
411 hasOwnMaterial = Standard_False;
413 myTransparency = 0.0;
416 //=======================================================================
419 //=======================================================================
420 void AIS_InteractiveObject::MustRecomputePrs(const Standard_Integer ) const
423 //=======================================================================
426 //=======================================================================
427 const TColStd_ListOfInteger& AIS_InteractiveObject::ListOfRecomputeModes() const
428 {return myToRecomputeModes;}
430 //=======================================================================
433 //=======================================================================
434 void AIS_InteractiveObject::SetRecomputeOk()
435 {myToRecomputeModes.Clear();}
438 //=======================================================================
439 //function : AcceptDisplayMode
441 //=======================================================================
443 Standard_Boolean AIS_InteractiveObject::AcceptDisplayMode(const Standard_Integer ) const
444 {return Standard_True;}
446 //=======================================================================
447 //function : DefaultDisplayMode
449 //=======================================================================
451 Standard_Integer AIS_InteractiveObject::DefaultDisplayMode() const
455 //=======================================================================
456 //function : SetInfiniteState
458 //=======================================================================
459 void AIS_InteractiveObject::SetInfiniteState(const Standard_Boolean aFlag)
461 myInfiniteState = aFlag;
462 Handle(Prs3d_Presentation) P;
464 for(Standard_Integer i =1; i<=myPresentations.Length();i++)
466 P = myPresentations(i).Presentation()->Presentation();
468 P->SetInfiniteState(myInfiniteState);
472 //=======================================================================
473 //function : HasPresentation
475 //=======================================================================
476 Standard_Boolean AIS_InteractiveObject::HasPresentation() const
478 return !GetContext().IsNull()
479 && GetContext()->MainPrsMgr()->HasPresentation (this, myDisplayMode);
482 //=======================================================================
483 //function : Presentation
485 //=======================================================================
486 Handle(Prs3d_Presentation) AIS_InteractiveObject::Presentation() const
488 return HasPresentation()
489 ? GetContext()->MainPrsMgr()->Presentation (this, myDisplayMode)->Presentation()
490 : Handle(Prs3d_Presentation)();
493 //=======================================================================
494 //function : SetAspect
496 //=======================================================================
497 void AIS_InteractiveObject::SetAspect(const Handle(Prs3d_BasicAspect)& anAspect,
498 const Standard_Boolean globalChange) {
500 if( HasPresentation() ) {
501 Handle(Prs3d_Presentation) prs = Presentation();
502 { Handle(Prs3d_ShadingAspect) aspect =
503 Handle(Prs3d_ShadingAspect)::DownCast(anAspect);
504 if( !aspect.IsNull() ) {
505 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
506 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
510 { Handle(Prs3d_LineAspect) aspect =
511 Handle(Prs3d_LineAspect)::DownCast(anAspect);
512 if( !aspect.IsNull() ) {
513 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
514 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
518 { Handle(Prs3d_PointAspect) aspect =
519 Handle(Prs3d_PointAspect)::DownCast(anAspect);
520 if( !aspect.IsNull() ) {
521 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
522 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
526 { Handle(Prs3d_TextAspect) aspect =
527 Handle(Prs3d_TextAspect)::DownCast(anAspect);
528 if( !aspect.IsNull() ) {
529 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
530 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
537 //=======================================================================
538 //function : SetPolygonOffsets
540 //=======================================================================
541 void AIS_InteractiveObject::SetPolygonOffsets(const Standard_Integer aMode,
542 const Standard_ShortReal aFactor,
543 const Standard_ShortReal aUnits)
545 if ( !HasPolygonOffsets() )
546 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
548 myDrawer->ShadingAspect()->Aspect()->SetPolygonOffsets( aMode, aFactor, aUnits );
550 // Modify existing presentations
551 for (Standard_Integer aPrsIter = 1, n = myPresentations.Length(); aPrsIter <= n; ++aPrsIter)
553 const Handle(PrsMgr_Presentation)& aPrs3d = myPresentations (aPrsIter).Presentation();
554 if ( !aPrs3d.IsNull() ) {
555 const Handle(Graphic3d_Structure)& aStruct = aPrs3d->Presentation();
556 if( !aStruct.IsNull() ) {
557 aStruct->SetPrimitivesAspect( myDrawer->ShadingAspect()->Aspect() );
558 // Workaround for issue 23115: Need to update also groups, because their
559 // face aspect ALWAYS overrides the structure's.
560 const Graphic3d_SequenceOfGroup& aGroups = aStruct->Groups();
561 for (Graphic3d_SequenceOfGroup::Iterator aGroupIter (aGroups); aGroupIter.More(); aGroupIter.Next())
563 Handle(Graphic3d_Group)& aGrp = aGroupIter.ChangeValue();
565 || !aGrp->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
570 Handle(Graphic3d_AspectFillArea3d) aFaceAsp = new Graphic3d_AspectFillArea3d();
571 Handle(Graphic3d_AspectLine3d) aLineAsp = new Graphic3d_AspectLine3d();
572 Handle(Graphic3d_AspectMarker3d) aPntAsp = new Graphic3d_AspectMarker3d();
573 Handle(Graphic3d_AspectText3d) aTextAsp = new Graphic3d_AspectText3d();
574 // TODO: Add methods for retrieving individual aspects from Graphic3d_Group
575 aGrp->GroupPrimitivesAspect(aLineAsp, aTextAsp, aPntAsp, aFaceAsp);
576 aFaceAsp->SetPolygonOffsets(aMode, aFactor, aUnits);
577 aGrp->SetGroupPrimitivesAspect(aFaceAsp);
584 //=======================================================================
585 //function : HasPolygonOffsets
587 //=======================================================================
588 Standard_Boolean AIS_InteractiveObject::HasPolygonOffsets() const
590 return !( myDrawer->ShadingAspect().IsNull() ||
591 ( !myDrawer->Link().IsNull() &&
592 myDrawer->ShadingAspect() == myDrawer->Link()->ShadingAspect() ) );
595 //=======================================================================
596 //function : PolygonOffsets
598 //=======================================================================
599 void AIS_InteractiveObject::PolygonOffsets(Standard_Integer& aMode,
600 Standard_ShortReal& aFactor,
601 Standard_ShortReal& aUnits) const
603 if( HasPolygonOffsets() )
604 myDrawer->ShadingAspect()->Aspect()->PolygonOffsets( aMode, aFactor, aUnits );