Back to index

salome-geom  6.5.0
GEOM_AISShape.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  GEOM OBJECT : interactive object for Geometry entities visualization
00024 //  File   : GEOM_AISShape.cxx
00025 //  Author : Nicolas REJNERI
00026 //  Module : GEOM
00027 
00033 #include "GEOM_AISShape.ixx"
00034 #include "SALOME_InteractiveObject.hxx"
00035 #include "GEOM_AISVector.hxx"
00036 
00037 // Open CASCADE Includes
00038 #include <AIS_Drawer.hxx>
00039 #include <AIS_InteractiveContext.hxx>
00040 
00041 #include <BRep_Tool.hxx>
00042 
00043 #include <GCPnts_AbscissaPoint.hxx>
00044 #include <GeomAdaptor_Curve.hxx>
00045 #include <gp_Pnt.hxx>
00046 #include <gp_Dir.hxx>
00047 #include <gp_Vec.hxx>
00048 #include <Graphic3d_AspectFillArea3d.hxx>
00049 #include <Graphic3d_AspectLine3d.hxx>
00050 
00051 #include <Prs3d_ShadingAspect.hxx>
00052 #include <Prs3d_Arrow.hxx>
00053 #include <Prs3d_IsoAspect.hxx>
00054 
00055 #include <SelectBasics_SensitiveEntity.hxx>
00056 #include <SelectMgr_EntityOwner.hxx>
00057 #include <StdSelect_BRepOwner.hxx>
00058 #include <SelectMgr_IndexedMapOfOwner.hxx>
00059 #include <SelectMgr_Selection.hxx>
00060 #include <StdSelect_DisplayMode.hxx>
00061 #include <StdPrs_WFDeflectionShape.hxx>
00062 
00063 #include <TColStd_IndexedMapOfInteger.hxx>
00064 #include <TColStd_ListIteratorOfListOfInteger.hxx>
00065 #include <TColStd_ListOfInteger.hxx>
00066 #include <TopExp.hxx>
00067 #include <TopoDS_Shape.hxx>
00068 #include <TopTools_IndexedMapOfShape.hxx>
00069 #include <TopExp.hxx>
00070 #include <TopExp_Explorer.hxx>
00071 #include <TopoDS.hxx>
00072 #include <TopoDS_Edge.hxx>
00073 #include <TopoDS_Shape.hxx>
00074 #include <TopoDS_Vertex.hxx>
00075 
00076 #include <SalomeApp_Tools.h>
00077 
00078 #include <SUIT_Session.h>
00079 #include <SUIT_ResourceMgr.h>
00080 
00081 
00082 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
00083                              const Handle(AIS_InteractiveContext)& theIC,
00084                              SelectMgr_IndexedMapOfOwner& theMap )
00085 {
00086   if ( theObj.IsNull() || theIC.IsNull() )
00087     return;
00088 
00089   TColStd_ListOfInteger modes;
00090   theIC->ActivatedModes( theObj, modes );
00091 
00092   TColStd_ListIteratorOfListOfInteger itr( modes );
00093   for (; itr.More(); itr.Next() ) {
00094     int m = itr.Value();
00095     if ( !theObj->HasSelection( m ) )
00096       continue;
00097 
00098     Handle(SelectMgr_Selection) sel = theObj->Selection( m );
00099 
00100     for ( sel->Init(); sel->More(); sel->Next() ) {
00101       Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
00102       if ( entity.IsNull() )
00103         continue;
00104 
00105       Handle(SelectMgr_EntityOwner) owner =
00106         Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
00107       if ( !owner.IsNull() )
00108         theMap.Add( owner );
00109     }
00110   }
00111 }
00112 
00113 static void indicesToOwners( const TColStd_IndexedMapOfInteger& aIndexMap,
00114                              const TopoDS_Shape& aMainShape,
00115                              const SelectMgr_IndexedMapOfOwner& anAllMap, 
00116                              SelectMgr_IndexedMapOfOwner& aToHiliteMap )
00117 {
00118   TopTools_IndexedMapOfShape aMapOfShapes;
00119   TopExp::MapShapes(aMainShape, aMapOfShapes);
00120 
00121   for  ( Standard_Integer i = 1, n = anAllMap.Extent(); i <= n; i++ ) {
00122     Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(anAllMap( i ));
00123     if ( anOwner.IsNull() || !anOwner->HasShape() )
00124       continue;
00125 
00126     const TopoDS_Shape& aSubShape = anOwner->Shape();
00127     Standard_Integer aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
00128     if ( !aSubShapeId || !aIndexMap.Contains( aSubShapeId ) )
00129       continue;
00130     
00131     if ( !aToHiliteMap.Contains( anOwner ) )
00132       aToHiliteMap.Add( anOwner );
00133   }
00134 }
00135 
00136 GEOM_AISShape::GEOM_AISShape(const TopoDS_Shape& shape,
00137                              const Standard_CString aName)
00138   : SALOME_AISShape(shape), myName(aName), myDisplayVectors(false)
00139 {
00140   myShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
00141 
00142   storeBoundaryColors();
00143 
00144   myEdgesInShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
00145 
00146   myUIsoNumber = -1;
00147   myVIsoNumber = -1;
00148 
00149   myTopLevel = Standard_False;
00150   Graphic3d_MaterialAspect aMatAspect;
00151   if ( !HasMaterial() ) {
00152        aMatAspect.SetAmbient( 0.5 );
00153        aMatAspect.SetDiffuse( 0.5 );
00154        aMatAspect.SetEmissive( 0.5 );
00155        aMatAspect.SetShininess(0.5 );
00156        aMatAspect.SetSpecular( 0.5 );
00157        
00158        myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(aMatAspect);
00159        myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(aMatAspect);
00160   }
00161   myCurrentMaterial = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
00162 }
00163 
00164 void GEOM_AISShape::setIO(const Handle(SALOME_InteractiveObject)& io){
00165   SetOwner( io );
00166 }
00167 
00168 Handle(SALOME_InteractiveObject) GEOM_AISShape::getIO(){
00169   Handle(SALOME_InteractiveObject) IO;
00170   if ( !GetOwner().IsNull() )
00171     IO = Handle(SALOME_InteractiveObject)::DownCast( GetOwner() );
00172   return IO;
00173 }
00174 
00175 Standard_Boolean GEOM_AISShape::hasIO(){
00176   return !getIO().IsNull();
00177 }
00178 
00179 void GEOM_AISShape::setName(const Standard_CString aName)
00180 {
00181   myName = aName;
00182 
00183   Handle(SALOME_InteractiveObject) IO = getIO();
00184   if ( !IO.IsNull() )
00185     IO->setName(aName);
00186 }
00187 
00188 Standard_CString GEOM_AISShape::getName(){
00189   return myName.ToCString();
00190 }
00191 
00192 void GEOM_AISShape::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
00193                             const Handle(Prs3d_Presentation)& aPrs,
00194                             const Standard_Integer aMode)
00195 {  
00196   if (IsInfinite()) aPrs->SetInfiniteState(Standard_True); //pas de prise en compte lors du FITALL
00197 
00198   Handle(AIS_InteractiveContext) anIC = GetContext();
00199   
00200   //   StdSelect_DisplayMode d = (StdSelect_DisplayMode) aMode;
00201   switch (aMode) {
00202     case 0://StdSelect_DM_Wireframe: 
00203     {
00204 
00205       restoreIsoNumbers();
00206 
00207       // Restore wireframe edges colors
00208       restoreBoundaryColors();
00209 
00210 
00211       if(isTopLevel()) {
00212        SetColor(topLevelColor());
00213        Handle(Prs3d_LineAspect) anAspect = Attributes()->WireAspect();
00214        anAspect->SetColor( topLevelColor() );
00215        Attributes()->SetWireAspect( anAspect );
00216       }
00217 
00218       StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
00219       
00220       break;
00221     }
00222     case 1://StdSelect_DM_Shading:
00223     {
00224       restoreIsoNumbers();
00225 
00226       shadingMode(aPresentationManager, aPrs, aMode);
00227 
00228       // Store wireframe edges colors
00229       storeBoundaryColors();
00230 
00231       break;
00232     }
00233     case 3: //StdSelect_DM_HLR:
00234     {
00235       if(!isTopLevel())
00236        AIS_TexturedShape::Compute(aPresentationManager, aPrs, aMode);
00237       else 
00238        shadingMode(aPresentationManager, aPrs, AIS_Shaded);
00239       break;
00240     }
00241   }
00242 
00243   if ( aMode == ShadingWithEdges ) {
00244     // Temporary store number of iso lines in order to recover its later 
00245     // when display mode is achnged to 'Wirefame' or 'Shading'.
00246     // Iso lines are not displayed in 'Shading with edges' mode.
00247     storeIsoNumbers();
00248 
00249     // Reset number of iso lines to 0
00250     resetIsoNumbers();
00251 
00252     //Shaded faces
00253     shadingMode(aPresentationManager, aPrs, AIS_Shaded);
00254 
00255     // Store wireframe edges colors
00256     storeBoundaryColors();
00257 
00258     // Coloring edges
00259     Handle(Prs3d_LineAspect) anAspect = myDrawer->UnFreeBoundaryAspect();
00260     anAspect->SetColor( myEdgesInShadingColor );
00261     myDrawer->SetUnFreeBoundaryAspect( anAspect );
00262     
00263     anAspect = myDrawer->FreeBoundaryAspect();
00264     anAspect->SetColor( myEdgesInShadingColor );
00265     myDrawer->SetFreeBoundaryAspect( anAspect );
00266 
00267     // Add edges to presentation
00268     StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
00269   }
00270 
00271   if (isShowVectors())
00272   {
00273     const bool isVector = IsKind(STANDARD_TYPE(GEOM_AISVector));
00274     TopExp_Explorer Exp ( myshape, TopAbs_EDGE );
00275     for ( ; Exp.More(); Exp.Next() ) {
00276       TopoDS_Vertex aV1, aV2;
00277       TopoDS_Edge anEdgeE = TopoDS::Edge(Exp.Current());
00278       if ( !isVector )
00279         // draw curve direction (issue 0021087)
00280         anEdgeE.Orientation( TopAbs_FORWARD );
00281 
00282       if ( anEdgeE.IsNull() ) continue;
00283 
00284       TopExp::Vertices(anEdgeE, aV1, aV2);
00285       gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
00286       gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
00287 
00288       double fp,lp;
00289       gp_Vec aDirVec;
00290       Handle(Geom_Curve) C = BRep_Tool::Curve(anEdgeE,fp,lp);
00291 
00292       if ( C.IsNull() ) continue;
00293 
00294       if ( anEdgeE.Orientation() == TopAbs_FORWARD )
00295         C->D1(lp, aP2, aDirVec);
00296       else {
00297         C->D1(fp, aP1, aDirVec);
00298         aP2 = aP1;
00299       }
00300 
00301       GeomAdaptor_Curve aAdC;
00302       aAdC.Load(C, fp, lp);
00303       Standard_Real aDist = GCPnts_AbscissaPoint::Length(aAdC, fp, lp); 
00304      
00305       if (aDist > gp::Resolution()) {
00306         gp_Dir aDir;
00307         if ( anEdgeE.Orientation() == TopAbs_FORWARD )
00308           aDir = aDirVec;
00309         else
00310           aDir = -aDirVec;
00311 
00312         Prs3d_Arrow::Draw(aPrs, aP2, aDir, M_PI/180.*5., aDist/10.);
00313       }
00314     }
00315   }
00316   //  aPrs->ReCompute(); // for hidden line recomputation if necessary...
00317 }
00318 
00319 void GEOM_AISShape::SetTransparency(const Standard_Real aValue)
00320 {
00321   if(aValue<0.0 || aValue>1.0) return;
00322   
00323   if(aValue<=0.05) 
00324     {
00325       UnsetTransparency();
00326       return;
00327     }
00328 
00329   Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
00330   FMat.SetTransparency(aValue);
00331   myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
00332   myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(FMat);
00333   myCurrentMaterial = FMat;
00334   myTransparency = aValue;
00335 }
00336 
00337 void GEOM_AISShape::SetShadingColor(const Quantity_Color &aCol)
00338 {
00339   myShadingColor = aCol;
00340 }
00341 
00342 void GEOM_AISShape::SetEdgesInShadingColor(const Quantity_Color &aCol)
00343 {
00344   myEdgesInShadingColor = aCol;
00345 }
00346 
00347 void GEOM_AISShape::highlightSubShapes(const TColStd_IndexedMapOfInteger& aIndexMap, 
00348                                        const Standard_Boolean aHighlight )
00349 {
00350   Handle(AIS_InteractiveObject) anObj = this;
00351   Handle(AIS_InteractiveContext) anIC = GetContext();
00352   if ( anIC.IsNull() || !anIC->HasOpenedContext() ) 
00353     return;
00354 
00355   Standard_Boolean isAutoHilight = anIC->AutomaticHilight();
00356   anIC->SetAutomaticHilight( false );
00357 
00358   anIC->ClearSelected( false );
00359 
00360   if ( aHighlight ) {
00361     SelectMgr_IndexedMapOfOwner anAllMap, aToHiliteMap;
00362 
00363     // Get entity owners for all activated selection modes
00364     getEntityOwners( anObj, anIC, anAllMap );
00365 
00366     // Convert <aIndexMap> into the map of owners to highlight/unhighlight
00367     indicesToOwners( aIndexMap, Shape(), anAllMap, aToHiliteMap );
00368 
00369 
00370     for ( Standard_Integer i = 1, n = aToHiliteMap.Extent(); i <= n; i++ )
00371       anIC->AddOrRemoveSelected( aToHiliteMap( i ), false );
00372   }
00373 
00374   anIC->SetAutomaticHilight( isAutoHilight );
00375   anIC->HilightSelected( false );
00376 }
00377 
00378 void GEOM_AISShape::SetDisplayVectors(bool isDisplayed)
00379 {
00380   myDisplayVectors = isDisplayed;
00381 }
00382 
00383 void GEOM_AISShape::shadingMode(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
00384                             const Handle(Prs3d_Presentation)& aPrs,
00385                             const Standard_Integer aMode)
00386 {
00387   myDrawer->ShadingAspect()->Aspect()->SetDistinguishOn();
00388 
00389   Graphic3d_MaterialAspect aMatAspect(Graphic3d_NOM_PLASTIC);
00390   aMatAspect.SetTransparency(myTransparency);
00391   myCurrentMaterial = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
00392   myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial( isTopLevel() ? aMatAspect : myCurrentMaterial );
00393   myDrawer->ShadingAspect()->Aspect()->SetBackMaterial( isTopLevel() ? aMatAspect : myCurrentMaterial );
00394 
00395       //Handle(Graphic3d_AspectFillArea3d) a4bis = myDrawer->ShadingAspect()->Aspect();
00396       //       P->SetPrimitivesAspect(a4bis);
00397       //        G->SetGroupPrimitivesAspect(a4bis);
00398       //a4bis->SetInteriorColor(myShadingColor);
00399   if( isTopLevel() )
00400     myDrawer->ShadingAspect()->SetColor( topLevelColor() );
00401   else { 
00402     if(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().MaterialType( Graphic3d_MATERIAL_ASPECT ))
00403       myDrawer->ShadingAspect()->SetColor(myShadingColor);
00404     else
00405       myDrawer->ShadingAspect()->SetColor(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().AmbientColor());
00406   }
00407 
00408   // PAL12113: AIS_Shape::Compute() works correctly with shapes containing no faces
00409   //StdPrs_ShadedShape::Add(aPrs,myshape,myDrawer);
00410   AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
00411 }
00412 
00413 void GEOM_AISShape::storeIsoNumbers()
00414 {
00415   myUIsoNumber = myDrawer->UIsoAspect()->Number();
00416   myVIsoNumber = myDrawer->VIsoAspect()->Number();
00417 }
00418 
00419 void GEOM_AISShape::restoreIsoNumbers()
00420 {
00421   if ( myUIsoNumber > 0 ) {
00422     // Restore number of U iso lines
00423     Handle(Prs3d_IsoAspect) anAspect = myDrawer->UIsoAspect();
00424     anAspect->SetNumber( myUIsoNumber );
00425     myDrawer->SetUIsoAspect( anAspect );
00426   }
00427   
00428   if ( myVIsoNumber > 0 ) {
00429     // Restore number of V iso lines
00430     Handle(Prs3d_IsoAspect) anAspect = myDrawer->VIsoAspect();
00431     anAspect->SetNumber( myVIsoNumber );
00432     myDrawer->SetVIsoAspect( anAspect );
00433   }
00434 }
00435 
00436 void GEOM_AISShape::resetIsoNumbers()
00437 {
00438   Handle(Prs3d_IsoAspect) anAspect = myDrawer->UIsoAspect();
00439   anAspect->SetNumber( 0 );
00440   myDrawer->SetUIsoAspect( anAspect );
00441   
00442   anAspect = myDrawer->VIsoAspect();
00443   anAspect->SetNumber( 0 );
00444   myDrawer->SetVIsoAspect( anAspect );
00445 }
00446 
00447 void GEOM_AISShape::storeBoundaryColors()
00448 {
00449   Aspect_TypeOfLine aLT;
00450   Standard_Real aW;
00451 
00452   myDrawer->FreeBoundaryAspect()->Aspect()->Values( myFreeBoundaryColor, aLT, aW);
00453   myDrawer->UnFreeBoundaryAspect()->Aspect()->Values( myUnFreeBoundaryColor, aLT, aW);
00454 }
00455  
00456 void GEOM_AISShape::restoreBoundaryColors()
00457 {
00458   Handle(Prs3d_LineAspect) anAspect = myDrawer->FreeBoundaryAspect();
00459   anAspect->SetColor( myFreeBoundaryColor );
00460   myDrawer->SetFreeBoundaryAspect( anAspect );
00461 
00462   anAspect = myDrawer->UnFreeBoundaryAspect();
00463   anAspect->SetColor( myUnFreeBoundaryColor );
00464   myDrawer->SetUnFreeBoundaryAspect( anAspect );
00465 }
00466 
00467 
00468 Standard_Boolean GEOM_AISShape::isTopLevel() {
00469   return myTopLevel;
00470 }
00471 
00472 void GEOM_AISShape::setTopLevel(Standard_Boolean f) {
00473   myTopLevel = f;
00474 }
00475 
00476 Quantity_Color GEOM_AISShape::topLevelColor() {
00477   SUIT_Session* session = SUIT_Session::session();
00478   SUIT_ResourceMgr* resMgr = session->resourceMgr();
00479   QColor c = resMgr->colorValue( "Geometry", "toplevel_color", QColor( 170, 85, 0 ) );
00480   return SalomeApp_Tools::color(c);
00481 }