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 #define BUC60577 //GG_101099 Enable to compute correctly
20 // transparency with more than one object in the view.
22 #define GER61351 //GG_171199 Enable to set an object RGB color
23 // instead a restricted object NameOfColor.
24 // Add SetCurrentFacingModel() method
26 #define BUC60632 //GG 15/03/00 Add protection on SetDisplayMode()
27 // method, compute only authorized presentation.
29 #define OCC708 //SAV unsetting transformation correctly
31 #include <AIS_InteractiveObject.ixx>
33 #include <Aspect_PolygonOffsetMode.hxx>
34 #include <Prs3d_ShadingAspect.hxx>
35 #include <Prs3d_LineAspect.hxx>
36 #include <Prs3d_PointAspect.hxx>
37 #include <Prs3d_TextAspect.hxx>
38 #include <Prs3d_Presentation.hxx>
39 #include <Prs3d_Root.hxx>
40 #include <PrsMgr_ModedPresentation.hxx>
41 #include <PrsMgr_PresentationManager3d.hxx>
42 #include <TColStd_ListIteratorOfListOfInteger.hxx>
43 #include <AIS_GraphicTool.hxx>
44 #include <Graphic3d_AspectFillArea3d.hxx>
45 #include <Graphic3d_AspectLine3d.hxx>
46 #include <Graphic3d_AspectMarker3d.hxx>
47 #include <Graphic3d_AspectText3d.hxx>
48 #include <Graphic3d_Group.hxx>
49 #include <Graphic3d_Structure.hxx>
52 //=======================================================================
53 //function : AIS_InteractiveObject
55 //=======================================================================
57 AIS_InteractiveObject::
58 AIS_InteractiveObject(const PrsMgr_TypeOfPresentation3d aTypeOfPresentation3d):
59 SelectMgr_SelectableObject(aTypeOfPresentation3d),
60 myDrawer(new AIS_Drawer()),
62 myOwnColor(Quantity_NOC_WHITE),
63 myOwnMaterial(Graphic3d_NOM_DEFAULT),
66 myInfiniteState(Standard_False),
67 hasOwnColor(Standard_False),
68 hasOwnMaterial(Standard_False),
69 myCurrentFacingModel(Aspect_TOFM_BOTH_SIDE),
70 myRecomputeEveryPrs(Standard_True),
77 Handle (AIS_InteractiveContext) Bid;
78 myCTXPtr = Bid.operator->();
80 SetCurrentFacingModel();
84 //=======================================================================
85 //function : Redisplay
87 //=======================================================================
89 void AIS_InteractiveObject::Redisplay(const Standard_Boolean AllModes)
95 //=======================================================================
98 //=======================================================================
100 AIS_KindOfInteractive AIS_InteractiveObject::Type() const
101 {return AIS_KOI_None;}
103 //=======================================================================
104 //function : Signature
106 //=======================================================================
108 Standard_Integer AIS_InteractiveObject::Signature() const
111 //=======================================================================
112 //function : RecomputeEveryPrs
114 //=======================================================================
116 Standard_Boolean AIS_InteractiveObject::RecomputeEveryPrs() const
117 {return myRecomputeEveryPrs;}
119 //=======================================================================
122 //=======================================================================
123 Standard_Boolean AIS_InteractiveObject::HasInteractiveContext() const
125 Handle (AIS_InteractiveContext) aNull;
126 return (myCTXPtr != aNull.operator->());
129 //=======================================================================
132 //=======================================================================
133 Handle(AIS_InteractiveContext) AIS_InteractiveObject::GetContext() const
138 //=======================================================================
141 //=======================================================================
142 void AIS_InteractiveObject::SetContext(const Handle(AIS_InteractiveContext)& aCtx)
144 myCTXPtr = aCtx.operator->();
147 if (myDrawer.IsNull()) {
148 myDrawer = new AIS_Drawer;
150 cout << "AIS_InteractiveObject::SetContext DRAWER NUL!" << endl;
153 myDrawer->Link(aCtx->DefaultDrawer());
156 //=======================================================================
159 //=======================================================================
160 Standard_Boolean AIS_InteractiveObject::HasOwner() const
162 return (!myOwner.IsNull());
167 //=======================================================================
170 //=======================================================================
171 void AIS_InteractiveObject::ClearOwner()
176 //=======================================================================
179 //=======================================================================
180 Standard_Boolean AIS_InteractiveObject::HasUsers() const
182 return (!myUsers.IsEmpty());
186 //=======================================================================
189 //=======================================================================
190 void AIS_InteractiveObject::AddUser(const Handle(Standard_Transient)& aUser)
192 myUsers.Append(aUser);
195 //=======================================================================
198 //=======================================================================
199 void AIS_InteractiveObject::ClearUsers()
205 //=======================================================================
208 //=======================================================================
209 void AIS_InteractiveObject::SetDisplayMode(const Standard_Integer aMode)
212 if( AcceptDisplayMode(aMode) )
214 myDisplayMode = aMode;
218 //=======================================================================
221 //=======================================================================
222 void AIS_InteractiveObject::SetSelectionMode(const Standard_Integer aMode)
224 mySelectionMode = aMode;
229 //=======================================================================
232 //=======================================================================
234 void AIS_InteractiveObject::SetCurrentFacingModel(const Aspect_TypeOfFacingModel aModel) {
235 myCurrentFacingModel = aModel;
238 //=======================================================================
239 //function : CurrentFacingModel
241 //=======================================================================
243 Aspect_TypeOfFacingModel AIS_InteractiveObject::CurrentFacingModel() const {
244 return myCurrentFacingModel;
248 //=======================================================================
249 //function : SetColor
251 //=======================================================================
253 void AIS_InteractiveObject::SetColor(const Quantity_NameOfColor aColor)
256 SetColor(Quantity_Color(aColor));
259 //=======================================================================
260 //function : SetColor
262 //=======================================================================
264 void AIS_InteractiveObject::SetColor(const Quantity_Color &aColor)
268 hasOwnColor = Standard_True;
271 //=======================================================================
272 //function : UnsetColor
274 //=======================================================================
275 void AIS_InteractiveObject::UnsetColor()
277 hasOwnColor = Standard_False;
280 //=======================================================================
283 //=======================================================================
284 void AIS_InteractiveObject::SetWidth(const Standard_Real aValue)
289 //=======================================================================
292 //=======================================================================
293 void AIS_InteractiveObject::UnsetWidth()
299 //=======================================================================
302 //=======================================================================
304 void AIS_InteractiveObject::SetMaterial(const Graphic3d_NameOfMaterial aName)
305 //void AIS_InteractiveObject::SetMaterial(const Graphic3d_NameOfPhysicalMaterial aName)
307 if( HasColor() || IsTransparent() || HasMaterial() )
309 myDrawer->ShadingAspect()->SetMaterial(aName);
313 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
315 myDrawer->ShadingAspect()->SetMaterial(aName);
316 #ifndef BUC60577 //???
317 myDrawer->ShadingAspect()->SetColor(AIS_GraphicTool::GetInteriorColor(myDrawer->Link()));
320 myOwnMaterial = aName;
321 hasOwnMaterial = Standard_True;
323 //=======================================================================
324 //function : SetMaterial
326 //=======================================================================
328 void AIS_InteractiveObject::SetMaterial(const Graphic3d_MaterialAspect& aMat)
331 if( HasColor() || IsTransparent() || HasMaterial() )
333 if(hasOwnColor ||(myTransparency==0.0) || hasOwnMaterial )
336 myDrawer->ShadingAspect()->SetMaterial(aMat);
340 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
341 myDrawer->ShadingAspect()->SetMaterial(aMat);
343 hasOwnMaterial = Standard_True;
346 //=======================================================================
349 //=======================================================================
350 void AIS_InteractiveObject::UnsetMaterial()
353 if( !HasMaterial() ) return;
354 if( HasColor() || IsTransparent()) {
355 myDrawer->ShadingAspect()->SetMaterial(
356 AIS_GraphicTool::GetMaterial(myDrawer->Link()));
357 if( HasColor() ) SetColor(myOwnColor);
358 if( IsTransparent() ) SetTransparency(myTransparency);
361 if(!hasOwnMaterial) return;
362 if(hasOwnColor ||(myTransparency==0.0))
364 myDrawer->ShadingAspect()->SetMaterial(AIS_GraphicTool::GetMaterial(myDrawer->Link()));
368 Handle(Prs3d_ShadingAspect) SA;
369 myDrawer->SetShadingAspect(SA);
371 hasOwnMaterial = Standard_False;
374 //=======================================================================
375 //function : SetTransparency
377 //=======================================================================
378 void AIS_InteractiveObject::SetTransparency(const Standard_Real aValue)
381 #ifdef BUC60577 // Back & Front material can be different !
383 if(!HasColor() && !IsTransparent() && !HasMaterial() ) {
384 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
385 if(!myDrawer->Link().IsNull())
386 myDrawer->ShadingAspect()->SetMaterial(AIS_GraphicTool::GetMaterial(myDrawer->Link()));
388 Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
389 Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
390 FMat.SetTransparency(aValue); BMat.SetTransparency(aValue);
391 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
392 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
394 if(aValue<0.0 || aValue>1.0) return;
403 if(hasOwnColor || hasOwnMaterial || myTransparency> 0.0)
405 Graphic3d_MaterialAspect Mat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
406 Mat.SetTransparency(aValue);
407 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(Mat);
408 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(Mat);
412 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
413 if(!myDrawer->Link().IsNull())
414 myDrawer->ShadingAspect()->SetMaterial(AIS_GraphicTool::GetMaterial(myDrawer->Link()));
415 Graphic3d_MaterialAspect Mat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
416 Mat.SetTransparency(aValue);
417 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(Mat);
418 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(Mat);
421 myTransparency = aValue;
424 //=======================================================================
425 //function : UnsetTransparency
427 //=======================================================================
428 void AIS_InteractiveObject::UnsetTransparency()
430 #ifdef BUC60577 // Back & Front material can be different !
431 if(HasColor() || HasMaterial() )
433 Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
434 Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
435 FMat.SetTransparency(0.); BMat.SetTransparency(0.);
436 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
437 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
440 if(hasOwnColor || hasOwnMaterial )
442 Graphic3d_MaterialAspect Mat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
443 Mat.SetTransparency(0.0);
444 // myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(Mat);
445 // myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(Mat);
446 myDrawer->ShadingAspect()->SetMaterial(Mat);
450 Handle (Prs3d_ShadingAspect) SA;
451 myDrawer->SetShadingAspect(SA);
455 //=======================================================================
456 //function : Transparency
458 //=======================================================================
459 Standard_Real AIS_InteractiveObject::Transparency() const
461 return (myTransparency<=0.05 ? 0 : myTransparency);
462 // Graphic3d_MaterialAspect Mat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
463 // return Mat.Transparency();
466 //=======================================================================
467 //function : SetAttributes
469 //=======================================================================
471 void AIS_InteractiveObject::SetAttributes(const Handle(AIS_Drawer)& aDrawer)
472 {myDrawer = aDrawer;}
475 //=======================================================================
476 //function : UnsetAttributes
478 //=======================================================================
479 void AIS_InteractiveObject::UnsetAttributes()
481 Handle(AIS_Drawer) dr = new AIS_Drawer();
482 if(myDrawer->HasLink())
483 dr->Link(myDrawer->Link());
485 hasOwnColor = Standard_False;
486 hasOwnMaterial = Standard_False;
488 myTransparency = 0.0;
491 //=======================================================================
494 //=======================================================================
495 void AIS_InteractiveObject::MustRecomputePrs(const Standard_Integer ) const
498 //=======================================================================
501 //=======================================================================
502 const TColStd_ListOfInteger& AIS_InteractiveObject::ListOfRecomputeModes() const
503 {return myToRecomputeModes;}
505 //=======================================================================
508 //=======================================================================
509 void AIS_InteractiveObject::SetRecomputeOk()
510 {myToRecomputeModes.Clear();}
513 //=======================================================================
514 //function : AcceptDisplayMode
516 //=======================================================================
518 Standard_Boolean AIS_InteractiveObject::AcceptDisplayMode(const Standard_Integer ) const
519 {return Standard_True;}
521 //=======================================================================
522 //function : DefaultDisplayMode
524 //=======================================================================
526 Standard_Integer AIS_InteractiveObject::DefaultDisplayMode() const
530 //=======================================================================
531 //function : SetInfiniteState
533 //=======================================================================
534 void AIS_InteractiveObject::SetInfiniteState(const Standard_Boolean aFlag)
536 myInfiniteState = aFlag;
537 Handle(Prs3d_Presentation) P;
539 for(Standard_Integer i =1; i<=myPresentations.Length();i++)
541 P = myPresentations(i).Presentation()->Presentation();
543 P->SetInfiniteState(myInfiniteState);
547 //=======================================================================
548 //function : HasPresentation
550 //=======================================================================
551 Standard_Boolean AIS_InteractiveObject::HasPresentation() const
553 return !GetContext().IsNull()
554 && GetContext()->MainPrsMgr()->HasPresentation (this, myDisplayMode);
557 //=======================================================================
558 //function : Presentation
560 //=======================================================================
561 Handle(Prs3d_Presentation) AIS_InteractiveObject::Presentation() const
563 return HasPresentation()
564 ? GetContext()->MainPrsMgr()->Presentation (this, myDisplayMode)->Presentation()
565 : Handle(Prs3d_Presentation)();
568 //=======================================================================
569 //function : SetAspect
571 //=======================================================================
572 void AIS_InteractiveObject::SetAspect(const Handle(Prs3d_BasicAspect)& anAspect,
573 const Standard_Boolean globalChange) {
575 if( HasPresentation() ) {
576 Handle(Prs3d_Presentation) prs = Presentation();
577 { Handle(Prs3d_ShadingAspect) aspect =
578 Handle(Prs3d_ShadingAspect)::DownCast(anAspect);
579 if( !aspect.IsNull() ) {
580 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
581 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
585 { Handle(Prs3d_LineAspect) aspect =
586 Handle(Prs3d_LineAspect)::DownCast(anAspect);
587 if( !aspect.IsNull() ) {
588 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
589 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
593 { Handle(Prs3d_PointAspect) aspect =
594 Handle(Prs3d_PointAspect)::DownCast(anAspect);
595 if( !aspect.IsNull() ) {
596 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
597 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
601 { Handle(Prs3d_TextAspect) aspect =
602 Handle(Prs3d_TextAspect)::DownCast(anAspect);
603 if( !aspect.IsNull() ) {
604 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
605 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
612 //=======================================================================
613 //function : SetPolygonOffsets
615 //=======================================================================
616 void AIS_InteractiveObject::SetPolygonOffsets(const Standard_Integer aMode,
617 const Standard_ShortReal aFactor,
618 const Standard_ShortReal aUnits)
620 if ( !HasPolygonOffsets() )
621 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
623 myDrawer->ShadingAspect()->Aspect()->SetPolygonOffsets( aMode, aFactor, aUnits );
625 // Modify existing presentations
626 for (Standard_Integer aPrsIter = 1, n = myPresentations.Length(); aPrsIter <= n; ++aPrsIter)
628 const Handle(PrsMgr_Presentation)& aPrs3d = myPresentations (aPrsIter).Presentation();
629 if ( !aPrs3d.IsNull() ) {
630 const Handle(Graphic3d_Structure)& aStruct = aPrs3d->Presentation();
631 if( !aStruct.IsNull() ) {
632 aStruct->SetPrimitivesAspect( myDrawer->ShadingAspect()->Aspect() );
633 // Workaround for issue 23115: Need to update also groups, because their
634 // face aspect ALWAYS overrides the structure's.
635 const Graphic3d_SequenceOfGroup& aGroups = aStruct->Groups();
636 for (Graphic3d_SequenceOfGroup::Iterator aGroupIter (aGroups); aGroupIter.More(); aGroupIter.Next())
638 Handle(Graphic3d_Group)& aGrp = aGroupIter.ChangeValue();
640 || !aGrp->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
645 Handle(Graphic3d_AspectFillArea3d) aFaceAsp = new Graphic3d_AspectFillArea3d();
646 Handle(Graphic3d_AspectLine3d) aLineAsp = new Graphic3d_AspectLine3d();
647 Handle(Graphic3d_AspectMarker3d) aPntAsp = new Graphic3d_AspectMarker3d();
648 Handle(Graphic3d_AspectText3d) aTextAsp = new Graphic3d_AspectText3d();
649 // TODO: Add methods for retrieving individual aspects from Graphic3d_Group
650 aGrp->GroupPrimitivesAspect(aLineAsp, aTextAsp, aPntAsp, aFaceAsp);
651 aFaceAsp->SetPolygonOffsets(aMode, aFactor, aUnits);
652 aGrp->SetGroupPrimitivesAspect(aFaceAsp);
659 //=======================================================================
660 //function : HasPolygonOffsets
662 //=======================================================================
663 Standard_Boolean AIS_InteractiveObject::HasPolygonOffsets() const
665 return !( myDrawer->ShadingAspect().IsNull() ||
666 ( !myDrawer->Link().IsNull() &&
667 myDrawer->ShadingAspect() == myDrawer->Link()->ShadingAspect() ) );
670 //=======================================================================
671 //function : PolygonOffsets
673 //=======================================================================
674 void AIS_InteractiveObject::PolygonOffsets(Standard_Integer& aMode,
675 Standard_ShortReal& aFactor,
676 Standard_ShortReal& aUnits) const
678 if( HasPolygonOffsets() )
679 myDrawer->ShadingAspect()->Aspect()->PolygonOffsets( aMode, aFactor, aUnits );