Salome HOME
Mantis issue 0021565: [CEA 557] Opposite of the pipe
[modules/geom.git] / src / GEOMGUI / GEOM_Displayer.cxx
index 4e1011d6910ef098b989d0594228480a5e17420a..7edbae5b250419101084af6f93bee54c5481c7d6 100644 (file)
@@ -39,6 +39,7 @@
 
 #include <GEOM_Actor.h>
 #include <GEOM_AISShape.hxx>
+#include <GEOM_TopWireframeShape.hxx>
 #include <GEOM_AISVector.hxx>
 #include <GEOM_AISTrihedron.hxx>
 #include <GEOM_VTKTrihedron.hxx>
 #include <Graphic3d_HArray1OfBytes.hxx>
 #endif
 
+// If the next macro is defined, autocolor feature works for all sub-shapes;
+// if it is undefined, autocolor feature works for groups only
+//#define GENERAL_AUTOCOLOR
+// Below macro, when uncommented, switches on simplified (more performant) algorithm
+// of auto-color picking up
+//#define SIMPLE_AUTOCOLOR
+
 //================================================================
 // Function : getActiveStudy
 // Purpose  : Get active study, returns 0 if no open study frame
@@ -673,13 +681,15 @@ void GEOM_Displayer::Update( SALOME_OCCPrs* prs )
           }
 
           // Setup shape properties here ..., e.g. display mode, color, transparency, etc
-          AISShape->SetDisplayMode( aPropMap.value(DISPLAY_MODE_PROP).toInt() );
-          AISShape->SetDisplayVectors(aPropMap.value(VECTOR_MODE_PROP).toInt());
+          Standard_Boolean isTopLevel = Standard_False;
           if(aPropMap.contains(TOP_LEVEL_PROP)) {
-            AISShape->setTopLevel( aPropMap.value(TOP_LEVEL_PROP).value<Standard_Boolean>() );
-          } 
+            isTopLevel = aPropMap.value(TOP_LEVEL_PROP).value<Standard_Boolean>();
+          }
+          AISShape->SetDisplayMode( aPropMap.value(DISPLAY_MODE_PROP).toInt() );
+          AISShape->setTopLevel(isTopLevel);
+          
+          AISShape->SetDisplayVectors(aPropMap.value(VECTOR_MODE_PROP).toInt()); 
         }else {
-          MESSAGE("myDisplayMode = "<< myDisplayMode)
           AISShape->SetDisplayMode( myDisplayMode );
           AISShape->SetShadingColor( myShadingColor );
         }
@@ -697,7 +707,7 @@ void GEOM_Displayer::Update( SALOME_OCCPrs* prs )
           QStringList uv =  anIsos.split(DIGIT_SEPARATOR);
           anUIsoNumber = uv[0].toInt();
           aVIsoNumber = uv[1].toInt();
-          //AISShape->SetTransparency(aPropMap.value(TRANSPARENCY_PROP).toDouble());
+          AISShape->SetTransparency(aPropMap.value(TRANSPARENCY_PROP).toDouble());
         } else {
           anUIsoNumber = aResMgr->integerValue("OCCViewer", "iso_number_u", 1);
           aVIsoNumber  = aResMgr->integerValue("OCCViewer", "iso_number_v", 1);
@@ -757,8 +767,7 @@ void GEOM_Displayer::Update( SALOME_OCCPrs* prs )
               useObjMarker = true;
             }
           }
-        }
-        
+        }        
         else
         {
           if ( onlyVertex )
@@ -968,30 +977,38 @@ void GEOM_Displayer::Update( SALOME_OCCPrs* prs )
             material.fromProperties( aPropMap.value(MATERIAL_PROP).toString() );
           } else {
             // Get material property from study and construct material model
-           QString mname = aResMgr->stringValue( "Geometry", "material", "Plastic" );
+                 QString mname = aResMgr->stringValue( "Geometry", "material", "Plastic" );
             material.fromResources( mname );
           }
 
           aStudy->setObjectProperty( aMgrId, anIO->getEntry(), MATERIAL_PROP, material.toProperties() );
 
           // Set material for the selected shape
-         AISShape->SetMaterial( material.getMaterialOCCAspect() );
-
-         if(HasWidth())
-           aStudy->setObjectProperty( aMgrId, anIO->getEntry(), EDGE_WIDTH_PROP, GetWidth() );
-
-         if(HasIsosWidth())
-           aStudy->setObjectProperty( aMgrId, anIO->getEntry(), ISOS_WIDTH_PROP, GetIsosWidth() );
-
-
+               AISShape->SetMaterial( material.getMaterialOCCAspect() );
+               if(HasWidth())
+                 aStudy->setObjectProperty( aMgrId, anIO->getEntry(), EDGE_WIDTH_PROP, GetWidth() );
+               if(HasIsosWidth())
+                 aStudy->setObjectProperty( aMgrId, anIO->getEntry(), ISOS_WIDTH_PROP, GetIsosWidth() );
         }
 
         // AISShape->SetName(???); ??? necessary to set name ???
         occPrs->AddObject( AISShape );
-
+        
         // In accordance with ToActivate() value object will be activated/deactivated
         // when it will be displayed
         occPrs->SetToActivate( ToActivate() );
+               
+               if( AISShape->isTopLevel() && AISShape->topLevelDisplayMode() == GEOM_AISShape::TopShowAdditionalWActor) {
+               //21671: EDF 1829 GEOM : Bring to front selected objects (continuation):
+               // Display wireframe presentation additionally
+                       Handle(GEOM_TopWireframeShape) aWirePrs = new GEOM_TopWireframeShape(myShape);
+                       aWirePrs->SetWidth(AISShape->Width());
+                       if ( !myIO.IsNull() ) {
+                               aWirePrs->setIO( myIO );
+                               aWirePrs->SetOwner( myIO );
+                       }
+                       occPrs->AddObject(aWirePrs);
+               }
       }
     }
     // if presentation is found -> set again shape for it
@@ -1813,6 +1830,40 @@ int GEOM_Displayer::UnsetDisplayMode()
   return aPrevMode;
 }
 
+SALOMEDS::Color GEOM_Displayer::getPredefinedUniqueColor()
+{
+  static QList<QColor> colors;
+
+  if ( colors.isEmpty() ) {
+    const int rfactor = 4; // red   color component split factor, must be > 0
+    const int gfactor = 4; // green color component split factor, must be > 0
+    const int bfactor = 3; // blue  color component split factor, must be > 0
+                           // -
+                           // total number of pre-defined colors is defined as
+                           // nbcolors = rfactor * gfactor * bfactor
+                           // NB: all three factors should not have same values
+                           // otherwise all colors will be greyish
+    
+    for ( int g = 0; g < gfactor; g++ ) {
+      for ( int r = 0; r < rfactor; r++ ) {
+       for ( int b = 0; b < bfactor; b++ )
+         colors.append( QColor( qRgb( r * 255 / (rfactor-1), g * 255 / (gfactor-1), b * 255 / (bfactor-1) ) ) );
+      }
+    }
+  }
+
+  static int currentColor = 0;
+
+  SALOMEDS::Color color;
+  color.R = (double)colors[currentColor].red()   / 255.0;
+  color.G = (double)colors[currentColor].green() / 255.0;
+  color.B = (double)colors[currentColor].blue()  / 255.0;
+
+  currentColor = (currentColor+1) % colors.count();
+
+  return color;
+}
+
 SALOMEDS::Color GEOM_Displayer::getUniqueColor( const QList<SALOMEDS::Color>& theReservedColors )
 {
   int aHue = -1;
@@ -1993,48 +2044,50 @@ SALOMEDS::Color GEOM_Displayer::getColor(GEOM::GEOM_Object_var theGeomObject, bo
   hasColor = false;
 
   SUIT_Session* session = SUIT_Session::session();
-  SUIT_Application* app = session->activeApplication();
+  SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
 
   if ( app && !theGeomObject->_is_nil()) {
     SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
 
     if ( study ) {
+      _PTR(Study) aStudy = study->studyDS();
       aSColor = theGeomObject->GetColor();
       hasColor = aSColor.R >= 0 && aSColor.G >= 0 && aSColor.B >= 0;
-      if( !hasColor && theGeomObject->GetType() == GEOM_GROUP ) { // auto color for group
-        GEOM::GEOM_Gen_var theGeomGen = GeometryGUI::GetGeomGen();
-        GEOM::GEOM_IGroupOperations_var anOperations = theGeomGen->GetIGroupOperations( study->id() );
-        GEOM::GEOM_Object_var aMainObject = anOperations->GetMainShape( theGeomObject );
-        if ( !aMainObject->_is_nil() && aMainObject->GetAutoColor() )
-          {
-            QList<SALOMEDS::Color> aReservedColors;
-
-            SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( app );
-            CORBA::String_var IOR = app->orb()->object_to_string( aMainObject );
-            if ( strcmp(IOR.in(), "") != 0 )
-              {
-                _PTR(Study) aStudy = study->studyDS();
-                _PTR(SObject) aMainSObject( aStudy->FindObjectIOR( std::string(IOR) ) );
-                _PTR(ChildIterator) it( aStudy->NewChildIterator( aMainSObject ) );
-                for( ; it->More(); it->Next() )
-                  {
-                    _PTR(SObject) aChildSObject( it->Value() );
-                    GEOM::GEOM_Object_var aChildObject =
-                      GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(aChildSObject));
-                    if( CORBA::is_nil( aChildObject ) )
-                      continue;
-
-                    if( aChildObject->GetType() != GEOM_GROUP )
-                      continue;
-
-                    SALOMEDS::Color aReservedColor = aChildObject->GetColor();
-                    aReservedColors.append( aReservedColor );
-                  }
-              }
-
-            aSColor = getUniqueColor( aReservedColors );
-            hasColor = true;
-          }
+      if ( !hasColor ) {
+#ifdef GENERAL_AUTOCOLOR // auto-color for all sub-shapes
+       bool general_autocolor = true;
+#else                    // auto-color for groups only
+       bool general_autocolor = false;
+#endif                   // GENERAL_AUTOCOLOR
+       if ( general_autocolor || theGeomObject->GetType() == GEOM_GROUP ) {
+         GEOM::GEOM_Object_var aMainObject = theGeomObject->GetMainShape();
+         if ( !CORBA::is_nil( aMainObject ) && aMainObject->GetAutoColor() ) {
+#ifdef SIMPLE_AUTOCOLOR  // simplified algorithm for auto-colors
+           aSColor = getPredefinedUniqueColor();
+           hasColor = true;
+#else                    // old algorithm  for auto-colors
+           QList<SALOMEDS::Color> aReservedColors;
+           CORBA::String_var IOR = app->orb()->object_to_string( aMainObject );
+           _PTR(SObject) aMainSObject( aStudy->FindObjectIOR( IOR.in() ) );
+           if ( aMainSObject ) {
+             _PTR(ChildIterator) it( aStudy->NewChildIterator( aMainSObject ) );
+             for ( ; it->More(); it->Next() ) {
+               _PTR(SObject) aChildSObject( it->Value() );
+               GEOM::GEOM_Object_var aChildObject =
+                 GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(aChildSObject));
+               if ( CORBA::is_nil( aChildObject ) )
+                 continue;
+               
+               SALOMEDS::Color aReservedColor = aChildObject->GetColor();
+               if ( aReservedColor.R >= 0 && aReservedColor.G >= 0 && aReservedColor.B >= 0 )
+                 aReservedColors.append( aReservedColor );
+             }
+           }
+           aSColor = getUniqueColor( aReservedColors );
+           hasColor = true;
+#endif                   // SIMPLE_AUTOCOLOR
+         }
+       }
       }
     }
   }