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 IMP020200 //GG Add SetTransformation() method
28 #define IMP140200 //GG Add HasPresentation() and Presentation() methods
29 // Add SetAspect() method.
31 #define BUC60632 //GG 15/03/00 Add protection on SetDisplayMode()
32 // method, compute only authorized presentation.
34 #define IMP220501 //GG CADPAK_V2 Update selection properly
36 #define OCC708 //SAV unsetting transformation correctly
38 #include <AIS_InteractiveObject.ixx>
40 #include <Aspect_PolygonOffsetMode.hxx>
41 #include <Prs3d_ShadingAspect.hxx>
42 #include <Prs3d_LineAspect.hxx>
43 #include <Prs3d_PointAspect.hxx>
44 #include <Prs3d_TextAspect.hxx>
45 #include <Prs3d_Presentation.hxx>
46 #include <Prs3d_Root.hxx>
47 #include <PrsMgr_ModedPresentation.hxx>
48 #include <PrsMgr_PresentationManager3d.hxx>
49 #include <TColStd_ListIteratorOfListOfInteger.hxx>
50 #include <AIS_GraphicTool.hxx>
51 #include <Graphic3d_AspectFillArea3d.hxx>
52 #include <Graphic3d_AspectLine3d.hxx>
53 #include <Graphic3d_AspectMarker3d.hxx>
54 #include <Graphic3d_AspectText3d.hxx>
55 #include <Graphic3d_Group.hxx>
56 #include <Graphic3d_Structure.hxx>
59 //=======================================================================
60 //function : AIS_InteractiveObject
62 //=======================================================================
64 AIS_InteractiveObject::
65 AIS_InteractiveObject(const PrsMgr_TypeOfPresentation3d aTypeOfPresentation3d):
66 SelectMgr_SelectableObject(aTypeOfPresentation3d),
67 myDrawer(new AIS_Drawer()),
69 myOwnColor(Quantity_NOC_WHITE),
70 myOwnMaterial(Graphic3d_NOM_DEFAULT),
73 myInfiniteState(Standard_False),
74 hasOwnColor(Standard_False),
75 hasOwnMaterial(Standard_False),
76 myCurrentFacingModel(Aspect_TOFM_BOTH_SIDE),
77 myRecomputeEveryPrs(Standard_True),
83 myHasTransformation(Standard_False)
85 Handle (AIS_InteractiveContext) Bid;
86 myCTXPtr = Bid.operator->();
88 SetCurrentFacingModel();
92 //=======================================================================
93 //function : Redisplay
95 //=======================================================================
97 void AIS_InteractiveObject::Redisplay(const Standard_Boolean AllModes)
103 //=======================================================================
106 //=======================================================================
108 AIS_KindOfInteractive AIS_InteractiveObject::Type() const
109 {return AIS_KOI_None;}
111 //=======================================================================
112 //function : Signature
114 //=======================================================================
116 Standard_Integer AIS_InteractiveObject::Signature() const
119 //=======================================================================
120 //function : RecomputeEveryPrs
122 //=======================================================================
124 Standard_Boolean AIS_InteractiveObject::RecomputeEveryPrs() const
125 {return myRecomputeEveryPrs;}
127 //=======================================================================
130 //=======================================================================
131 Standard_Boolean AIS_InteractiveObject::HasInteractiveContext() const
133 Handle (AIS_InteractiveContext) aNull;
134 return (myCTXPtr != aNull.operator->());
137 //=======================================================================
140 //=======================================================================
141 Handle(AIS_InteractiveContext) AIS_InteractiveObject::GetContext() const
146 //=======================================================================
149 //=======================================================================
150 void AIS_InteractiveObject::SetContext(const Handle(AIS_InteractiveContext)& aCtx)
152 myCTXPtr = aCtx.operator->();
155 if (myDrawer.IsNull()) {
156 myDrawer = new AIS_Drawer;
158 cout << "AIS_InteractiveObject::SetContext DRAWER NUL!" << endl;
161 myDrawer->Link(aCtx->DefaultDrawer());
164 //=======================================================================
167 //=======================================================================
168 Standard_Boolean AIS_InteractiveObject::HasOwner() const
170 return (!myOwner.IsNull());
175 //=======================================================================
178 //=======================================================================
179 void AIS_InteractiveObject::ClearOwner()
184 //=======================================================================
187 //=======================================================================
188 Standard_Boolean AIS_InteractiveObject::HasUsers() const
190 return (!myUsers.IsEmpty());
194 //=======================================================================
197 //=======================================================================
198 void AIS_InteractiveObject::AddUser(const Handle(Standard_Transient)& aUser)
200 myUsers.Append(aUser);
203 //=======================================================================
206 //=======================================================================
207 void AIS_InteractiveObject::ClearUsers()
213 //=======================================================================
216 //=======================================================================
217 void AIS_InteractiveObject::SetDisplayMode(const Standard_Integer aMode)
220 if( AcceptDisplayMode(aMode) )
222 myDisplayMode = aMode;
226 //=======================================================================
229 //=======================================================================
230 void AIS_InteractiveObject::SetSelectionMode(const Standard_Integer aMode)
232 mySelectionMode = aMode;
237 //=======================================================================
240 //=======================================================================
242 void AIS_InteractiveObject::SetCurrentFacingModel(const Aspect_TypeOfFacingModel aModel) {
243 myCurrentFacingModel = aModel;
246 //=======================================================================
247 //function : CurrentFacingModel
249 //=======================================================================
251 Aspect_TypeOfFacingModel AIS_InteractiveObject::CurrentFacingModel() const {
252 return myCurrentFacingModel;
256 //=======================================================================
257 //function : SetColor
259 //=======================================================================
261 void AIS_InteractiveObject::SetColor(const Quantity_NameOfColor aColor)
264 SetColor(Quantity_Color(aColor));
267 //=======================================================================
268 //function : SetColor
270 //=======================================================================
272 void AIS_InteractiveObject::SetColor(const Quantity_Color &aColor)
276 hasOwnColor = Standard_True;
279 //=======================================================================
280 //function : UnsetColor
282 //=======================================================================
283 void AIS_InteractiveObject::UnsetColor()
285 hasOwnColor = Standard_False;
288 //=======================================================================
291 //=======================================================================
292 void AIS_InteractiveObject::SetWidth(const Standard_Real aValue)
297 //=======================================================================
300 //=======================================================================
301 void AIS_InteractiveObject::UnsetWidth()
307 //=======================================================================
310 //=======================================================================
312 void AIS_InteractiveObject::SetMaterial(const Graphic3d_NameOfMaterial aName)
313 //void AIS_InteractiveObject::SetMaterial(const Graphic3d_NameOfPhysicalMaterial aName)
315 if( HasColor() || IsTransparent() || HasMaterial() )
317 myDrawer->ShadingAspect()->SetMaterial(aName);
321 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
323 myDrawer->ShadingAspect()->SetMaterial(aName);
324 #ifndef BUC60577 //???
325 myDrawer->ShadingAspect()->SetColor(AIS_GraphicTool::GetInteriorColor(myDrawer->Link()));
328 myOwnMaterial = aName;
329 hasOwnMaterial = Standard_True;
331 //=======================================================================
332 //function : SetMaterial
334 //=======================================================================
336 void AIS_InteractiveObject::SetMaterial(const Graphic3d_MaterialAspect& aMat)
339 if( HasColor() || IsTransparent() || HasMaterial() )
341 if(hasOwnColor ||(myTransparency==0.0) || hasOwnMaterial )
344 myDrawer->ShadingAspect()->SetMaterial(aMat);
348 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
349 myDrawer->ShadingAspect()->SetMaterial(aMat);
351 hasOwnMaterial = Standard_True;
354 //=======================================================================
357 //=======================================================================
358 void AIS_InteractiveObject::UnsetMaterial()
361 if( !HasMaterial() ) return;
362 if( HasColor() || IsTransparent()) {
363 myDrawer->ShadingAspect()->SetMaterial(
364 AIS_GraphicTool::GetMaterial(myDrawer->Link()));
365 if( HasColor() ) SetColor(myOwnColor);
366 if( IsTransparent() ) SetTransparency(myTransparency);
369 if(!hasOwnMaterial) return;
370 if(hasOwnColor ||(myTransparency==0.0))
372 myDrawer->ShadingAspect()->SetMaterial(AIS_GraphicTool::GetMaterial(myDrawer->Link()));
376 Handle(Prs3d_ShadingAspect) SA;
377 myDrawer->SetShadingAspect(SA);
379 hasOwnMaterial = Standard_False;
382 //=======================================================================
383 //function : SetTransparency
385 //=======================================================================
386 void AIS_InteractiveObject::SetTransparency(const Standard_Real aValue)
389 #ifdef BUC60577 // Back & Front material can be different !
391 if(!HasColor() && !IsTransparent() && !HasMaterial() ) {
392 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
393 if(!myDrawer->Link().IsNull())
394 myDrawer->ShadingAspect()->SetMaterial(AIS_GraphicTool::GetMaterial(myDrawer->Link()));
396 Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
397 Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
398 FMat.SetTransparency(aValue); BMat.SetTransparency(aValue);
399 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
400 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
402 if(aValue<0.0 || aValue>1.0) return;
411 if(hasOwnColor || hasOwnMaterial || myTransparency> 0.0)
413 Graphic3d_MaterialAspect Mat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
414 Mat.SetTransparency(aValue);
415 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(Mat);
416 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(Mat);
420 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
421 if(!myDrawer->Link().IsNull())
422 myDrawer->ShadingAspect()->SetMaterial(AIS_GraphicTool::GetMaterial(myDrawer->Link()));
423 Graphic3d_MaterialAspect Mat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
424 Mat.SetTransparency(aValue);
425 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(Mat);
426 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(Mat);
429 myTransparency = aValue;
432 //=======================================================================
433 //function : UnsetTransparency
435 //=======================================================================
436 void AIS_InteractiveObject::UnsetTransparency()
438 #ifdef BUC60577 // Back & Front material can be different !
439 if(HasColor() || HasMaterial() )
441 Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
442 Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
443 FMat.SetTransparency(0.); BMat.SetTransparency(0.);
444 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
445 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
448 if(hasOwnColor || hasOwnMaterial )
450 Graphic3d_MaterialAspect Mat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
451 Mat.SetTransparency(0.0);
452 // myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(Mat);
453 // myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(Mat);
454 myDrawer->ShadingAspect()->SetMaterial(Mat);
458 Handle (Prs3d_ShadingAspect) SA;
459 myDrawer->SetShadingAspect(SA);
463 //=======================================================================
464 //function : Transparency
466 //=======================================================================
467 Standard_Real AIS_InteractiveObject::Transparency() const
469 return (myTransparency<=0.05 ? 0 : myTransparency);
470 // Graphic3d_MaterialAspect Mat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
471 // return Mat.Transparency();
474 //=======================================================================
475 //function : SetAttributes
477 //=======================================================================
479 void AIS_InteractiveObject::SetAttributes(const Handle(AIS_Drawer)& aDrawer)
480 {myDrawer = aDrawer;}
483 //=======================================================================
484 //function : UnsetAttributes
486 //=======================================================================
487 void AIS_InteractiveObject::UnsetAttributes()
489 Handle(AIS_Drawer) dr = new AIS_Drawer();
490 if(myDrawer->HasLink())
491 dr->Link(myDrawer->Link());
493 hasOwnColor = Standard_False;
494 hasOwnMaterial = Standard_False;
496 myTransparency = 0.0;
499 //=======================================================================
502 //=======================================================================
503 void AIS_InteractiveObject::MustRecomputePrs(const Standard_Integer ) const
506 //=======================================================================
509 //=======================================================================
510 const TColStd_ListOfInteger& AIS_InteractiveObject::ListOfRecomputeModes() const
511 {return myToRecomputeModes;}
513 //=======================================================================
516 //=======================================================================
517 void AIS_InteractiveObject::SetRecomputeOk()
518 {myToRecomputeModes.Clear();}
521 //=======================================================================
522 //function : AcceptDisplayMode
524 //=======================================================================
526 Standard_Boolean AIS_InteractiveObject::AcceptDisplayMode(const Standard_Integer ) const
527 {return Standard_True;}
529 //=======================================================================
530 //function : DefaultDisplayMode
532 //=======================================================================
534 Standard_Integer AIS_InteractiveObject::DefaultDisplayMode() const
538 //=======================================================================
539 //function : SetInfiniteState
541 //=======================================================================
542 void AIS_InteractiveObject::SetInfiniteState(const Standard_Boolean aFlag)
544 myInfiniteState = aFlag;
545 Handle(Prs3d_Presentation) P;
547 for(Standard_Integer i =1; i<=myPresentations.Length();i++){
548 P = Handle(Prs3d_Presentation)::DownCast(myPresentations(i).Presentation());
550 P->SetInfiniteState(myInfiniteState);}
554 //=======================================================================
555 //function : SetTransformation
557 //=======================================================================
558 void AIS_InteractiveObject::SetTransformation(const Handle(Geom_Transformation)& aTrsf, const Standard_Boolean postConcatenate, const Standard_Boolean updateSelection) {
560 if(!GetContext().IsNull()){
561 const PrsMgr_Presentations& prs = Presentations();
562 Handle(Prs3d_Presentation) P;
563 Standard_Integer mode;
564 myHasTransformation = Standard_True;
565 for( Standard_Integer i=1 ; i<=prs.Length() ; i++ ) {
566 mode = prs(i).Mode();
567 P = GetContext()->MainPrsMgr()->CastPresentation(this,mode)->Presentation();
568 if( postConcatenate ) P->Multiply(aTrsf);
569 else P->Transform(aTrsf);
570 if( updateSelection ) {
572 myCTXPtr->ClearSelected(Standard_True);
573 myCTXPtr->RecomputeSelectionOnly(this);
575 if( HasSelection(mode) ) {
576 UpdateSelection(mode);
584 //=======================================================================
585 //function : SetTransformation
587 //=======================================================================
588 void AIS_InteractiveObject::UnsetTransformation() {
590 static Handle(Geom_Transformation) trsf = new Geom_Transformation( gp_Trsf() );
592 Handle(Geom_Transformation) trsf;
595 SetTransformation(trsf); // Set identity transformation
596 myHasTransformation = Standard_False;
599 //=======================================================================
600 //function : Transformation
602 //=======================================================================
603 Handle(Geom_Transformation) AIS_InteractiveObject::Transformation() {
604 Handle(Geom_Transformation) trsf;
606 if(!GetContext().IsNull() ) {
607 const PrsMgr_Presentations& prs = Presentations();
608 if( prs.Length() > 0 ) {
609 Handle(Prs3d_Presentation) P =
610 GetContext()->MainPrsMgr()->CastPresentation(this,1)->Presentation();
611 trsf = P->Transformation();
618 //=======================================================================
619 //function : HasTransformation
621 //=======================================================================
622 Standard_Boolean AIS_InteractiveObject::HasTransformation() const {
624 return myHasTransformation;
629 //=======================================================================
630 //function : HasPresentation
632 //=======================================================================
633 Standard_Boolean AIS_InteractiveObject::HasPresentation() const {
635 if( !GetContext().IsNull() &&
636 GetContext()->MainPrsMgr()->HasPresentation(this,myDisplayMode) ) {
637 return Standard_True;
640 return Standard_False;
643 //=======================================================================
644 //function : Presentation
646 //=======================================================================
647 Handle(Prs3d_Presentation) AIS_InteractiveObject::Presentation() const {
648 Handle(Prs3d_Presentation) prs;
650 if( HasPresentation() ) {
651 prs = GetContext()->MainPrsMgr()->
652 CastPresentation(this,myDisplayMode)->Presentation();
658 //=======================================================================
659 //function : SetAspect
661 //=======================================================================
662 void AIS_InteractiveObject::SetAspect(const Handle(Prs3d_BasicAspect)& anAspect,
663 const Standard_Boolean globalChange) {
665 if( HasPresentation() ) {
666 Handle(Prs3d_Presentation) prs = Presentation();
667 { Handle(Prs3d_ShadingAspect) aspect =
668 Handle(Prs3d_ShadingAspect)::DownCast(anAspect);
669 if( !aspect.IsNull() ) {
670 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
671 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
675 { Handle(Prs3d_LineAspect) aspect =
676 Handle(Prs3d_LineAspect)::DownCast(anAspect);
677 if( !aspect.IsNull() ) {
678 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
679 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
683 { Handle(Prs3d_PointAspect) aspect =
684 Handle(Prs3d_PointAspect)::DownCast(anAspect);
685 if( !aspect.IsNull() ) {
686 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
687 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
691 { Handle(Prs3d_TextAspect) aspect =
692 Handle(Prs3d_TextAspect)::DownCast(anAspect);
693 if( !aspect.IsNull() ) {
694 if( globalChange ) prs->SetPrimitivesAspect(aspect->Aspect());
695 Prs3d_Root::CurrentGroup(prs)->SetGroupPrimitivesAspect(aspect->Aspect());
703 // OCC4895 SAN 22/03/04 High-level interface for controlling polygon offsets
705 //=======================================================================
706 //function : SetPolygonOffsets
708 //=======================================================================
709 void AIS_InteractiveObject::SetPolygonOffsets(const Standard_Integer aMode,
710 const Standard_ShortReal aFactor,
711 const Standard_ShortReal aUnits)
713 if ( !HasPolygonOffsets() )
714 myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
716 myDrawer->ShadingAspect()->Aspect()->SetPolygonOffsets( aMode, aFactor, aUnits );
718 // Modify existing presentations
719 Handle(Graphic3d_Structure) aStruct;
720 for( Standard_Integer i = 1, n = myPresentations.Length(); i <= n; i++ ) {
721 Handle(PrsMgr_Presentation3d) aPrs3d =
722 Handle(PrsMgr_Presentation3d)::DownCast( myPresentations(i).Presentation() );
723 if ( !aPrs3d.IsNull() ) {
724 aStruct = Handle(Graphic3d_Structure)::DownCast( aPrs3d->Presentation() );
725 if( !aStruct.IsNull() ) {
726 aStruct->SetPrimitivesAspect( myDrawer->ShadingAspect()->Aspect() );
727 // Workaround for issue 23115: Need to update also groups, because their
728 // face aspect ALWAYS overrides the structure's.
729 const Graphic3d_SequenceOfGroup& aGroups = aStruct->Groups();
730 for (Graphic3d_SequenceOfGroup::Iterator aGroupIter (aGroups); aGroupIter.More(); aGroupIter.Next())
732 Handle(Graphic3d_Group)& aGrp = aGroupIter.ChangeValue();
734 || !aGrp->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
739 Handle(Graphic3d_AspectFillArea3d) aFaceAsp = new Graphic3d_AspectFillArea3d();
740 Handle(Graphic3d_AspectLine3d) aLineAsp = new Graphic3d_AspectLine3d();
741 Handle(Graphic3d_AspectMarker3d) aPntAsp = new Graphic3d_AspectMarker3d();
742 Handle(Graphic3d_AspectText3d) aTextAsp = new Graphic3d_AspectText3d();
743 // TODO: Add methods for retrieving individual aspects from Graphic3d_Group
744 aGrp->GroupPrimitivesAspect(aLineAsp, aTextAsp, aPntAsp, aFaceAsp);
745 aFaceAsp->SetPolygonOffsets(aMode, aFactor, aUnits);
746 aGrp->SetGroupPrimitivesAspect(aFaceAsp);
754 //=======================================================================
755 //function : HasPolygonOffsets
757 //=======================================================================
758 Standard_Boolean AIS_InteractiveObject::HasPolygonOffsets() const
760 return !( myDrawer->ShadingAspect().IsNull() ||
761 ( !myDrawer->Link().IsNull() &&
762 myDrawer->ShadingAspect() == myDrawer->Link()->ShadingAspect() ) );
765 //=======================================================================
766 //function : PolygonOffsets
768 //=======================================================================
769 void AIS_InteractiveObject::PolygonOffsets(Standard_Integer& aMode,
770 Standard_ShortReal& aFactor,
771 Standard_ShortReal& aUnits) const
773 if( HasPolygonOffsets() )
774 myDrawer->ShadingAspect()->Aspect()->PolygonOffsets( aMode, aFactor, aUnits );
776 // OCC4895 SAN 22/03/04 High-level interface for controlling polygon offsets