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),
49 myOwnColor(Quantity_NOC_WHITE),
50 myOwnMaterial(Graphic3d_NOM_DEFAULT),
53 myInfiniteState(Standard_False),
54 hasOwnColor(Standard_False),
55 hasOwnMaterial(Standard_False),
56 myCurrentFacingModel(Aspect_TOFM_BOTH_SIDE),
57 myRecomputeEveryPrs(Standard_True),
64 Handle (AIS_InteractiveContext) Bid;
65 myCTXPtr = Bid.operator->();
66 SetCurrentFacingModel();
69 //=======================================================================
70 //function : Redisplay
72 //=======================================================================
74 void AIS_InteractiveObject::Redisplay(const Standard_Boolean AllModes)
80 //=======================================================================
83 //=======================================================================
85 AIS_KindOfInteractive AIS_InteractiveObject::Type() const
86 {return AIS_KOI_None;}
88 //=======================================================================
89 //function : Signature
91 //=======================================================================
93 Standard_Integer AIS_InteractiveObject::Signature() const
96 //=======================================================================
97 //function : RecomputeEveryPrs
99 //=======================================================================
101 Standard_Boolean AIS_InteractiveObject::RecomputeEveryPrs() const
102 {return myRecomputeEveryPrs;}
104 //=======================================================================
107 //=======================================================================
108 Standard_Boolean AIS_InteractiveObject::HasInteractiveContext() const
110 Handle (AIS_InteractiveContext) aNull;
111 return (myCTXPtr != aNull.operator->());
114 //=======================================================================
117 //=======================================================================
118 Handle(AIS_InteractiveContext) AIS_InteractiveObject::GetContext() const
123 //=======================================================================
126 //=======================================================================
127 void AIS_InteractiveObject::SetContext(const Handle(AIS_InteractiveContext)& aCtx)
129 myCTXPtr = aCtx.operator->();
132 myDrawer->Link(aCtx->DefaultDrawer());
135 //=======================================================================
138 //=======================================================================
139 Standard_Boolean AIS_InteractiveObject::HasOwner() const
141 return (!myOwner.IsNull());
146 //=======================================================================
149 //=======================================================================
150 void AIS_InteractiveObject::ClearOwner()
155 //=======================================================================
158 //=======================================================================
159 Standard_Boolean AIS_InteractiveObject::HasUsers() const
161 return (!myUsers.IsEmpty());
165 //=======================================================================
168 //=======================================================================
169 void AIS_InteractiveObject::AddUser(const Handle(Standard_Transient)& aUser)
171 myUsers.Append(aUser);
174 //=======================================================================
177 //=======================================================================
178 void AIS_InteractiveObject::ClearUsers()
184 //=======================================================================
187 //=======================================================================
188 void AIS_InteractiveObject::SetDisplayMode(const Standard_Integer aMode)
190 if( AcceptDisplayMode(aMode) )
191 myDisplayMode = aMode;
195 //=======================================================================
198 //=======================================================================
199 void AIS_InteractiveObject::SetSelectionMode(const Standard_Integer aMode)
201 mySelectionMode = aMode;
206 //=======================================================================
209 //=======================================================================
210 void AIS_InteractiveObject::SetCurrentFacingModel(const Aspect_TypeOfFacingModel aModel) {
211 myCurrentFacingModel = aModel;
214 //=======================================================================
215 //function : CurrentFacingModel
217 //=======================================================================
219 Aspect_TypeOfFacingModel AIS_InteractiveObject::CurrentFacingModel() const {
220 return myCurrentFacingModel;
223 //=======================================================================
224 //function : SetColor
226 //=======================================================================
228 void AIS_InteractiveObject::SetColor(const Quantity_NameOfColor aColor)
230 SetColor(Quantity_Color(aColor));
233 //=======================================================================
234 //function : SetColor
236 //=======================================================================
238 void AIS_InteractiveObject::SetColor(const Quantity_Color &aColor)
241 hasOwnColor = Standard_True;
244 //=======================================================================
245 //function : UnsetColor
247 //=======================================================================
248 void AIS_InteractiveObject::UnsetColor()
250 hasOwnColor = Standard_False;
253 //=======================================================================
256 //=======================================================================
257 void AIS_InteractiveObject::SetWidth(const Standard_Real aValue)
262 //=======================================================================
265 //=======================================================================
266 void AIS_InteractiveObject::UnsetWidth()
272 //=======================================================================
275 //=======================================================================
277 void AIS_InteractiveObject::SetMaterial(const Graphic3d_NameOfMaterial aName)
278 //void AIS_InteractiveObject::SetMaterial(const Graphic3d_NameOfPhysicalMaterial aName)
280 if( HasColor() || IsTransparent() || HasMaterial() )
282 myDrawer->ShadingAspect()->SetMaterial(aName);
286 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
287 myDrawer->ShadingAspect()->SetMaterial(aName);
289 myOwnMaterial = aName;
290 hasOwnMaterial = Standard_True;
292 //=======================================================================
293 //function : SetMaterial
295 //=======================================================================
297 void AIS_InteractiveObject::SetMaterial(const Graphic3d_MaterialAspect& aMat)
299 if (HasColor() || IsTransparent() || HasMaterial())
301 myDrawer->ShadingAspect()->SetMaterial(aMat);
305 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
306 myDrawer->ShadingAspect()->SetMaterial(aMat);
308 hasOwnMaterial = Standard_True;
310 //=======================================================================
313 //=======================================================================
314 void AIS_InteractiveObject::UnsetMaterial()
316 if( !HasMaterial() ) return;
317 if (HasColor() || IsTransparent())
319 if(myDrawer->HasLink())
321 myDrawer->ShadingAspect()->SetMaterial (AIS_GraphicTool::GetMaterial (myDrawer->Link()));
323 if (HasColor()) SetColor (myOwnColor);
324 if (IsTransparent()) SetTransparency (myTransparency);
327 Handle(Prs3d_ShadingAspect) SA;
328 myDrawer->SetShadingAspect(SA);
330 hasOwnMaterial = Standard_False;
333 //=======================================================================
334 //function : SetTransparency
336 //=======================================================================
337 void AIS_InteractiveObject::SetTransparency(const Standard_Real aValue)
339 if(!HasColor() && !IsTransparent() && !HasMaterial())
341 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
342 if(myDrawer->HasLink())
343 myDrawer->ShadingAspect()->SetMaterial(AIS_GraphicTool::GetMaterial(myDrawer->Link()));
345 Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
346 Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
347 FMat.SetTransparency(aValue); BMat.SetTransparency(aValue);
348 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
349 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
350 myTransparency = aValue;
353 //=======================================================================
354 //function : UnsetTransparency
356 //=======================================================================
357 void AIS_InteractiveObject::UnsetTransparency()
359 if(HasColor() || HasMaterial() )
361 Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
362 Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
363 FMat.SetTransparency(0.); BMat.SetTransparency(0.);
364 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
365 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
368 Handle (Prs3d_ShadingAspect) SA;
369 myDrawer->SetShadingAspect(SA);
373 //=======================================================================
374 //function : Transparency
376 //=======================================================================
377 Standard_Real AIS_InteractiveObject::Transparency() const
379 return (myTransparency<=0.05 ? 0 : myTransparency);
380 // Graphic3d_MaterialAspect Mat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
381 // return Mat.Transparency();
384 //=======================================================================
385 //function : UnsetAttributes
387 //=======================================================================
388 void AIS_InteractiveObject::UnsetAttributes()
390 SelectMgr_SelectableObject::UnsetAttributes();
392 hasOwnColor = Standard_False;
393 hasOwnMaterial = Standard_False;
395 myTransparency = 0.0;
398 //=======================================================================
401 //=======================================================================
402 void AIS_InteractiveObject::MustRecomputePrs(const Standard_Integer ) const
405 //=======================================================================
408 //=======================================================================
409 const TColStd_ListOfInteger& AIS_InteractiveObject::ListOfRecomputeModes() const
410 {return myToRecomputeModes;}
412 //=======================================================================
415 //=======================================================================
416 void AIS_InteractiveObject::SetRecomputeOk()
417 {myToRecomputeModes.Clear();}
420 //=======================================================================
421 //function : AcceptDisplayMode
423 //=======================================================================
425 Standard_Boolean AIS_InteractiveObject::AcceptDisplayMode(const Standard_Integer ) const
426 {return Standard_True;}
428 //=======================================================================
429 //function : DefaultDisplayMode
431 //=======================================================================
433 Standard_Integer AIS_InteractiveObject::DefaultDisplayMode() const
437 //=======================================================================
438 //function : SetInfiniteState
440 //=======================================================================
441 void AIS_InteractiveObject::SetInfiniteState(const Standard_Boolean aFlag)
443 myInfiniteState = aFlag;
444 Handle(Prs3d_Presentation) P;
446 for(Standard_Integer i =1; i<=myPresentations.Length();i++)
448 P = myPresentations(i).Presentation()->Presentation();
450 P->SetInfiniteState(myInfiniteState);
454 //=======================================================================
455 //function : HasPresentation
457 //=======================================================================
458 Standard_Boolean AIS_InteractiveObject::HasPresentation() const
460 return !GetContext().IsNull()
461 && GetContext()->MainPrsMgr()->HasPresentation (this, myDisplayMode);
464 //=======================================================================
465 //function : Presentation
467 //=======================================================================
468 Handle(Prs3d_Presentation) AIS_InteractiveObject::Presentation() const
470 return HasPresentation()
471 ? GetContext()->MainPrsMgr()->Presentation (this, myDisplayMode)->Presentation()
472 : Handle(Prs3d_Presentation)();
475 //=======================================================================
476 //function : SetAspect
478 //=======================================================================
479 void AIS_InteractiveObject::SetAspect(const Handle(Prs3d_BasicAspect)& anAspect,
480 const Standard_Boolean globalChange) {
482 if( HasPresentation() ) {
483 Handle(Prs3d_Presentation) prs = Presentation();
484 { Handle(Prs3d_ShadingAspect) aspect =
485 Handle(Prs3d_ShadingAspect)::DownCast(anAspect);
486 if( !aspect.IsNull() ) {
487 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
488 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
492 { Handle(Prs3d_LineAspect) aspect =
493 Handle(Prs3d_LineAspect)::DownCast(anAspect);
494 if( !aspect.IsNull() ) {
495 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
496 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
500 { Handle(Prs3d_PointAspect) aspect =
501 Handle(Prs3d_PointAspect)::DownCast(anAspect);
502 if( !aspect.IsNull() ) {
503 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
504 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
508 { Handle(Prs3d_TextAspect) aspect =
509 Handle(Prs3d_TextAspect)::DownCast(anAspect);
510 if( !aspect.IsNull() ) {
511 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
512 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
519 //=======================================================================
520 //function : SetPolygonOffsets
522 //=======================================================================
523 void AIS_InteractiveObject::SetPolygonOffsets(const Standard_Integer aMode,
524 const Standard_ShortReal aFactor,
525 const Standard_ShortReal aUnits)
527 if ( !HasPolygonOffsets() )
528 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
530 myDrawer->ShadingAspect()->Aspect()->SetPolygonOffsets( aMode, aFactor, aUnits );
532 // Modify existing presentations
533 for (Standard_Integer aPrsIter = 1, n = myPresentations.Length(); aPrsIter <= n; ++aPrsIter)
535 const Handle(PrsMgr_Presentation)& aPrs3d = myPresentations (aPrsIter).Presentation();
536 if ( !aPrs3d.IsNull() ) {
537 const Handle(Graphic3d_Structure)& aStruct = aPrs3d->Presentation();
538 if( !aStruct.IsNull() ) {
539 aStruct->SetPrimitivesAspect( myDrawer->ShadingAspect()->Aspect() );
540 // Workaround for issue 23115: Need to update also groups, because their
541 // face aspect ALWAYS overrides the structure's.
542 const Graphic3d_SequenceOfGroup& aGroups = aStruct->Groups();
543 for (Graphic3d_SequenceOfGroup::Iterator aGroupIter (aGroups); aGroupIter.More(); aGroupIter.Next())
545 Handle(Graphic3d_Group)& aGrp = aGroupIter.ChangeValue();
547 || !aGrp->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
552 Handle(Graphic3d_AspectFillArea3d) aFaceAsp = new Graphic3d_AspectFillArea3d();
553 Handle(Graphic3d_AspectLine3d) aLineAsp = new Graphic3d_AspectLine3d();
554 Handle(Graphic3d_AspectMarker3d) aPntAsp = new Graphic3d_AspectMarker3d();
555 Handle(Graphic3d_AspectText3d) aTextAsp = new Graphic3d_AspectText3d();
556 // TODO: Add methods for retrieving individual aspects from Graphic3d_Group
557 aGrp->GroupPrimitivesAspect(aLineAsp, aTextAsp, aPntAsp, aFaceAsp);
558 aFaceAsp->SetPolygonOffsets(aMode, aFactor, aUnits);
559 aGrp->SetGroupPrimitivesAspect(aFaceAsp);
566 //=======================================================================
567 //function : HasPolygonOffsets
569 //=======================================================================
570 Standard_Boolean AIS_InteractiveObject::HasPolygonOffsets() const
572 return !( myDrawer->ShadingAspect().IsNull() ||
573 ( myDrawer->HasLink() &&
574 myDrawer->ShadingAspect() == myDrawer->Link()->ShadingAspect() ) );
577 //=======================================================================
578 //function : PolygonOffsets
580 //=======================================================================
581 void AIS_InteractiveObject::PolygonOffsets(Standard_Integer& aMode,
582 Standard_ShortReal& aFactor,
583 Standard_ShortReal& aUnits) const
585 if( HasPolygonOffsets() )
586 myDrawer->ShadingAspect()->Aspect()->PolygonOffsets( aMode, aFactor, aUnits );