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),
76 myHasTransformation(Standard_False)
78 Handle (AIS_InteractiveContext) Bid;
79 myCTXPtr = Bid.operator->();
81 SetCurrentFacingModel();
85 //=======================================================================
86 //function : Redisplay
88 //=======================================================================
90 void AIS_InteractiveObject::Redisplay(const Standard_Boolean AllModes)
96 //=======================================================================
99 //=======================================================================
101 AIS_KindOfInteractive AIS_InteractiveObject::Type() const
102 {return AIS_KOI_None;}
104 //=======================================================================
105 //function : Signature
107 //=======================================================================
109 Standard_Integer AIS_InteractiveObject::Signature() const
112 //=======================================================================
113 //function : RecomputeEveryPrs
115 //=======================================================================
117 Standard_Boolean AIS_InteractiveObject::RecomputeEveryPrs() const
118 {return myRecomputeEveryPrs;}
120 //=======================================================================
123 //=======================================================================
124 Standard_Boolean AIS_InteractiveObject::HasInteractiveContext() const
126 Handle (AIS_InteractiveContext) aNull;
127 return (myCTXPtr != aNull.operator->());
130 //=======================================================================
133 //=======================================================================
134 Handle(AIS_InteractiveContext) AIS_InteractiveObject::GetContext() const
139 //=======================================================================
142 //=======================================================================
143 void AIS_InteractiveObject::SetContext(const Handle(AIS_InteractiveContext)& aCtx)
145 myCTXPtr = aCtx.operator->();
148 if (myDrawer.IsNull()) {
149 myDrawer = new AIS_Drawer;
151 cout << "AIS_InteractiveObject::SetContext DRAWER NUL!" << endl;
154 myDrawer->Link(aCtx->DefaultDrawer());
157 //=======================================================================
160 //=======================================================================
161 Standard_Boolean AIS_InteractiveObject::HasOwner() const
163 return (!myOwner.IsNull());
168 //=======================================================================
171 //=======================================================================
172 void AIS_InteractiveObject::ClearOwner()
177 //=======================================================================
180 //=======================================================================
181 Standard_Boolean AIS_InteractiveObject::HasUsers() const
183 return (!myUsers.IsEmpty());
187 //=======================================================================
190 //=======================================================================
191 void AIS_InteractiveObject::AddUser(const Handle(Standard_Transient)& aUser)
193 myUsers.Append(aUser);
196 //=======================================================================
199 //=======================================================================
200 void AIS_InteractiveObject::ClearUsers()
206 //=======================================================================
209 //=======================================================================
210 void AIS_InteractiveObject::SetDisplayMode(const Standard_Integer aMode)
213 if( AcceptDisplayMode(aMode) )
215 myDisplayMode = aMode;
219 //=======================================================================
222 //=======================================================================
223 void AIS_InteractiveObject::SetSelectionMode(const Standard_Integer aMode)
225 mySelectionMode = aMode;
230 //=======================================================================
233 //=======================================================================
235 void AIS_InteractiveObject::SetCurrentFacingModel(const Aspect_TypeOfFacingModel aModel) {
236 myCurrentFacingModel = aModel;
239 //=======================================================================
240 //function : CurrentFacingModel
242 //=======================================================================
244 Aspect_TypeOfFacingModel AIS_InteractiveObject::CurrentFacingModel() const {
245 return myCurrentFacingModel;
249 //=======================================================================
250 //function : SetColor
252 //=======================================================================
254 void AIS_InteractiveObject::SetColor(const Quantity_NameOfColor aColor)
257 SetColor(Quantity_Color(aColor));
260 //=======================================================================
261 //function : SetColor
263 //=======================================================================
265 void AIS_InteractiveObject::SetColor(const Quantity_Color &aColor)
269 hasOwnColor = Standard_True;
272 //=======================================================================
273 //function : UnsetColor
275 //=======================================================================
276 void AIS_InteractiveObject::UnsetColor()
278 hasOwnColor = Standard_False;
281 //=======================================================================
284 //=======================================================================
285 void AIS_InteractiveObject::SetWidth(const Standard_Real aValue)
290 //=======================================================================
293 //=======================================================================
294 void AIS_InteractiveObject::UnsetWidth()
300 //=======================================================================
303 //=======================================================================
305 void AIS_InteractiveObject::SetMaterial(const Graphic3d_NameOfMaterial aName)
306 //void AIS_InteractiveObject::SetMaterial(const Graphic3d_NameOfPhysicalMaterial aName)
308 if( HasColor() || IsTransparent() || HasMaterial() )
310 myDrawer->ShadingAspect()->SetMaterial(aName);
314 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
316 myDrawer->ShadingAspect()->SetMaterial(aName);
317 #ifndef BUC60577 //???
318 myDrawer->ShadingAspect()->SetColor(AIS_GraphicTool::GetInteriorColor(myDrawer->Link()));
321 myOwnMaterial = aName;
322 hasOwnMaterial = Standard_True;
324 //=======================================================================
325 //function : SetMaterial
327 //=======================================================================
329 void AIS_InteractiveObject::SetMaterial(const Graphic3d_MaterialAspect& aMat)
332 if( HasColor() || IsTransparent() || HasMaterial() )
334 if(hasOwnColor ||(myTransparency==0.0) || hasOwnMaterial )
337 myDrawer->ShadingAspect()->SetMaterial(aMat);
341 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
342 myDrawer->ShadingAspect()->SetMaterial(aMat);
344 hasOwnMaterial = Standard_True;
347 //=======================================================================
350 //=======================================================================
351 void AIS_InteractiveObject::UnsetMaterial()
354 if( !HasMaterial() ) return;
355 if( HasColor() || IsTransparent()) {
356 myDrawer->ShadingAspect()->SetMaterial(
357 AIS_GraphicTool::GetMaterial(myDrawer->Link()));
358 if( HasColor() ) SetColor(myOwnColor);
359 if( IsTransparent() ) SetTransparency(myTransparency);
362 if(!hasOwnMaterial) return;
363 if(hasOwnColor ||(myTransparency==0.0))
365 myDrawer->ShadingAspect()->SetMaterial(AIS_GraphicTool::GetMaterial(myDrawer->Link()));
369 Handle(Prs3d_ShadingAspect) SA;
370 myDrawer->SetShadingAspect(SA);
372 hasOwnMaterial = Standard_False;
375 //=======================================================================
376 //function : SetTransparency
378 //=======================================================================
379 void AIS_InteractiveObject::SetTransparency(const Standard_Real aValue)
382 #ifdef BUC60577 // Back & Front material can be different !
384 if(!HasColor() && !IsTransparent() && !HasMaterial() ) {
385 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
386 if(!myDrawer->Link().IsNull())
387 myDrawer->ShadingAspect()->SetMaterial(AIS_GraphicTool::GetMaterial(myDrawer->Link()));
389 Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
390 Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
391 FMat.SetTransparency(aValue); BMat.SetTransparency(aValue);
392 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
393 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
395 if(aValue<0.0 || aValue>1.0) return;
404 if(hasOwnColor || hasOwnMaterial || myTransparency> 0.0)
406 Graphic3d_MaterialAspect Mat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
407 Mat.SetTransparency(aValue);
408 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(Mat);
409 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(Mat);
413 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
414 if(!myDrawer->Link().IsNull())
415 myDrawer->ShadingAspect()->SetMaterial(AIS_GraphicTool::GetMaterial(myDrawer->Link()));
416 Graphic3d_MaterialAspect Mat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
417 Mat.SetTransparency(aValue);
418 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(Mat);
419 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(Mat);
422 myTransparency = aValue;
425 //=======================================================================
426 //function : UnsetTransparency
428 //=======================================================================
429 void AIS_InteractiveObject::UnsetTransparency()
431 #ifdef BUC60577 // Back & Front material can be different !
432 if(HasColor() || HasMaterial() )
434 Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
435 Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
436 FMat.SetTransparency(0.); BMat.SetTransparency(0.);
437 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
438 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
441 if(hasOwnColor || hasOwnMaterial )
443 Graphic3d_MaterialAspect Mat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
444 Mat.SetTransparency(0.0);
445 // myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(Mat);
446 // myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(Mat);
447 myDrawer->ShadingAspect()->SetMaterial(Mat);
451 Handle (Prs3d_ShadingAspect) SA;
452 myDrawer->SetShadingAspect(SA);
456 //=======================================================================
457 //function : Transparency
459 //=======================================================================
460 Standard_Real AIS_InteractiveObject::Transparency() const
462 return (myTransparency<=0.05 ? 0 : myTransparency);
463 // Graphic3d_MaterialAspect Mat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
464 // return Mat.Transparency();
467 //=======================================================================
468 //function : SetAttributes
470 //=======================================================================
472 void AIS_InteractiveObject::SetAttributes(const Handle(AIS_Drawer)& aDrawer)
473 {myDrawer = aDrawer;}
476 //=======================================================================
477 //function : UnsetAttributes
479 //=======================================================================
480 void AIS_InteractiveObject::UnsetAttributes()
482 Handle(AIS_Drawer) dr = new AIS_Drawer();
483 if(myDrawer->HasLink())
484 dr->Link(myDrawer->Link());
486 hasOwnColor = Standard_False;
487 hasOwnMaterial = Standard_False;
489 myTransparency = 0.0;
492 //=======================================================================
495 //=======================================================================
496 void AIS_InteractiveObject::MustRecomputePrs(const Standard_Integer ) const
499 //=======================================================================
502 //=======================================================================
503 const TColStd_ListOfInteger& AIS_InteractiveObject::ListOfRecomputeModes() const
504 {return myToRecomputeModes;}
506 //=======================================================================
509 //=======================================================================
510 void AIS_InteractiveObject::SetRecomputeOk()
511 {myToRecomputeModes.Clear();}
514 //=======================================================================
515 //function : AcceptDisplayMode
517 //=======================================================================
519 Standard_Boolean AIS_InteractiveObject::AcceptDisplayMode(const Standard_Integer ) const
520 {return Standard_True;}
522 //=======================================================================
523 //function : DefaultDisplayMode
525 //=======================================================================
527 Standard_Integer AIS_InteractiveObject::DefaultDisplayMode() const
531 //=======================================================================
532 //function : SetInfiniteState
534 //=======================================================================
535 void AIS_InteractiveObject::SetInfiniteState(const Standard_Boolean aFlag)
537 myInfiniteState = aFlag;
538 Handle(Prs3d_Presentation) P;
540 for(Standard_Integer i =1; i<=myPresentations.Length();i++){
541 P = Handle(Prs3d_Presentation)::DownCast(myPresentations(i).Presentation());
543 P->SetInfiniteState(myInfiniteState);}
546 //=======================================================================
547 //function : SetTransformation
549 //=======================================================================
550 void AIS_InteractiveObject::SetTransformation (const Handle(Geom_Transformation)& theTrsf,
551 const Standard_Boolean theToPostConcatenate,
552 const Standard_Boolean theToUpdateSelection)
554 if (GetContext().IsNull())
559 const PrsMgr_Presentations& aPrsList = Presentations();
560 myHasTransformation = Standard_True;
561 for (Standard_Integer aPrsIter = 1; aPrsIter <= aPrsList.Length(); ++aPrsIter)
563 const Standard_Integer aMode = aPrsList (aPrsIter).Mode();
564 Handle(Prs3d_Presentation) aPrs = GetContext()->MainPrsMgr()->Presentation (this, aMode)->Presentation();
565 theToPostConcatenate ? aPrs->Multiply (theTrsf)
566 : aPrs->Transform (theTrsf);
567 if (theToUpdateSelection)
569 myCTXPtr->ClearSelected (Standard_True);
570 myCTXPtr->RecomputeSelectionOnly (this);
575 //=======================================================================
576 //function : SetTransformation
578 //=======================================================================
579 void AIS_InteractiveObject::UnsetTransformation() {
581 static Handle(Geom_Transformation) trsf = new Geom_Transformation( gp_Trsf() );
583 Handle(Geom_Transformation) trsf;
586 SetTransformation(trsf); // Set identity transformation
587 myHasTransformation = Standard_False;
590 //=======================================================================
591 //function : Transformation
593 //=======================================================================
594 Handle(Geom_Transformation) AIS_InteractiveObject::Transformation()
596 if (GetContext().IsNull())
598 return Handle(Geom_Transformation)();
601 const PrsMgr_Presentations& aPrsList = Presentations();
602 if (aPrsList.Length() > 0)
604 Handle(Prs3d_Presentation) aPrs = GetContext()->MainPrsMgr()->Presentation (this, 1)->Presentation();
605 return aPrs->Transformation();
607 return Handle(Geom_Transformation)();
610 //=======================================================================
611 //function : HasTransformation
613 //=======================================================================
614 Standard_Boolean AIS_InteractiveObject::HasTransformation() const {
616 return myHasTransformation;
619 //=======================================================================
620 //function : HasPresentation
622 //=======================================================================
623 Standard_Boolean AIS_InteractiveObject::HasPresentation() const
625 return !GetContext().IsNull()
626 && GetContext()->MainPrsMgr()->HasPresentation (this, myDisplayMode);
629 //=======================================================================
630 //function : Presentation
632 //=======================================================================
633 Handle(Prs3d_Presentation) AIS_InteractiveObject::Presentation() const
635 return HasPresentation()
636 ? GetContext()->MainPrsMgr()->Presentation (this, myDisplayMode)->Presentation()
637 : Handle(Prs3d_Presentation)();
640 //=======================================================================
641 //function : SetAspect
643 //=======================================================================
644 void AIS_InteractiveObject::SetAspect(const Handle(Prs3d_BasicAspect)& anAspect,
645 const Standard_Boolean globalChange) {
647 if( HasPresentation() ) {
648 Handle(Prs3d_Presentation) prs = Presentation();
649 { Handle(Prs3d_ShadingAspect) aspect =
650 Handle(Prs3d_ShadingAspect)::DownCast(anAspect);
651 if( !aspect.IsNull() ) {
652 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
653 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
657 { Handle(Prs3d_LineAspect) aspect =
658 Handle(Prs3d_LineAspect)::DownCast(anAspect);
659 if( !aspect.IsNull() ) {
660 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
661 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
665 { Handle(Prs3d_PointAspect) aspect =
666 Handle(Prs3d_PointAspect)::DownCast(anAspect);
667 if( !aspect.IsNull() ) {
668 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
669 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
673 { Handle(Prs3d_TextAspect) aspect =
674 Handle(Prs3d_TextAspect)::DownCast(anAspect);
675 if( !aspect.IsNull() ) {
676 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
677 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
684 //=======================================================================
685 //function : SetPolygonOffsets
687 //=======================================================================
688 void AIS_InteractiveObject::SetPolygonOffsets(const Standard_Integer aMode,
689 const Standard_ShortReal aFactor,
690 const Standard_ShortReal aUnits)
692 if ( !HasPolygonOffsets() )
693 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
695 myDrawer->ShadingAspect()->Aspect()->SetPolygonOffsets( aMode, aFactor, aUnits );
697 // Modify existing presentations
698 for (Standard_Integer aPrsIter = 1, n = myPresentations.Length(); aPrsIter <= n; ++aPrsIter)
700 const Handle(PrsMgr_Presentation)& aPrs3d = myPresentations (aPrsIter).Presentation();
701 if ( !aPrs3d.IsNull() ) {
702 const Handle(Graphic3d_Structure)& aStruct = aPrs3d->Presentation();
703 if( !aStruct.IsNull() ) {
704 aStruct->SetPrimitivesAspect( myDrawer->ShadingAspect()->Aspect() );
705 // Workaround for issue 23115: Need to update also groups, because their
706 // face aspect ALWAYS overrides the structure's.
707 const Graphic3d_SequenceOfGroup& aGroups = aStruct->Groups();
708 for (Graphic3d_SequenceOfGroup::Iterator aGroupIter (aGroups); aGroupIter.More(); aGroupIter.Next())
710 Handle(Graphic3d_Group)& aGrp = aGroupIter.ChangeValue();
712 || !aGrp->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
717 Handle(Graphic3d_AspectFillArea3d) aFaceAsp = new Graphic3d_AspectFillArea3d();
718 Handle(Graphic3d_AspectLine3d) aLineAsp = new Graphic3d_AspectLine3d();
719 Handle(Graphic3d_AspectMarker3d) aPntAsp = new Graphic3d_AspectMarker3d();
720 Handle(Graphic3d_AspectText3d) aTextAsp = new Graphic3d_AspectText3d();
721 // TODO: Add methods for retrieving individual aspects from Graphic3d_Group
722 aGrp->GroupPrimitivesAspect(aLineAsp, aTextAsp, aPntAsp, aFaceAsp);
723 aFaceAsp->SetPolygonOffsets(aMode, aFactor, aUnits);
724 aGrp->SetGroupPrimitivesAspect(aFaceAsp);
731 //=======================================================================
732 //function : HasPolygonOffsets
734 //=======================================================================
735 Standard_Boolean AIS_InteractiveObject::HasPolygonOffsets() const
737 return !( myDrawer->ShadingAspect().IsNull() ||
738 ( !myDrawer->Link().IsNull() &&
739 myDrawer->ShadingAspect() == myDrawer->Link()->ShadingAspect() ) );
742 //=======================================================================
743 //function : PolygonOffsets
745 //=======================================================================
746 void AIS_InteractiveObject::PolygonOffsets(Standard_Integer& aMode,
747 Standard_ShortReal& aFactor,
748 Standard_ShortReal& aUnits) const
750 if( HasPolygonOffsets() )
751 myDrawer->ShadingAspect()->Aspect()->PolygonOffsets( aMode, aFactor, aUnits );