Salome HOME
0021136: EDF 1748 SMESH: SetAutoColor has no effect in a python script
authorvsr <vsr@opencascade.com>
Fri, 21 Jan 2011 12:20:52 +0000 (12:20 +0000)
committervsr <vsr@opencascade.com>
Fri, 21 Jan 2011 12:20:52 +0000 (12:20 +0000)
src/SMESHGUI/SMESHGUI.cxx
src/SMESH_I/SMESH_Mesh_i.cxx

index f314db7f8614574a5a1fd3555aa921021de76dfa..7ef1989b45fa274ea0c60c004eec1575231351be 100644 (file)
     if( aMainObject->_is_nil() )
       return;
 
-    aMainObject->SetAutoColor( true );
-
-    QList<SALOMEDS::Color> aReservedColors;
+    aMainObject->SetAutoColor( true ); // mesh groups are re-colored here
 
     SMESH::ListOfGroups aListOfGroups = *aMainObject->GetGroups();
     for( int i = 0, n = aListOfGroups.length(); i < n; i++ )
     {
       SMESH::SMESH_GroupBase_var aGroupObject = aListOfGroups[i];
-      SALOMEDS::Color aCurrentColor = aGroupObject->GetColor();
-
-      SALOMEDS::Color aColor = SMESHGUI::getUniqueColor( aReservedColors );
-      aGroupObject->SetColor( aColor );
-      aReservedColors.append( aColor );
-
+      SALOMEDS::Color aColor = aGroupObject->GetColor();
       _PTR(SObject) aGroupSObject = SMESH::FindSObject(aGroupObject);
       if (aGroupSObject) {
         if(SMESH_Actor *anActor = SMESH::FindActorByEntry(aGroupSObject->GetID().c_str())) {
index 1733a315eeaacb11721b63ef43917214c1870a7b..1019e527302c08ddba0b3588f0e0a09a2d49c205 100644 (file)
@@ -2302,6 +2302,36 @@ CORBA::Boolean SMESH_Mesh_i::HasModificationsToDiscard() throw(SALOME::SALOME_Ex
   return _impl->HasModificationsToDiscard();
 }
 
+static SALOMEDS::Color getUniqueColor( const std::list<SALOMEDS::Color>& theReservedColors )
+{
+  const int MAX_ATTEMPTS = 100;
+  int cnt = 0;
+  double tolerance = 0.5;
+  SALOMEDS::Color col;
+
+  bool ok = false;
+  while ( !ok ) {
+    // generate random color
+    double red    = (double)rand() / RAND_MAX;
+    double green  = (double)rand() / RAND_MAX;
+    double blue   = (double)rand() / RAND_MAX;
+    // check existence in the list of the existing colors
+    bool matched = false;
+    std::list<SALOMEDS::Color>::const_iterator it;
+    for ( it = theReservedColors.begin(); it != theReservedColors.end() && !matched; ++it ) {
+      SALOMEDS::Color color = *it;
+      double tol = fabs( color.R - red ) + fabs( color.G - green ) + fabs( color.B  - blue  );
+      matched = tol < tolerance;
+    }
+    if ( (cnt+1) % 20 == 0 ) tolerance = tolerance/2;
+    ok = ( ++cnt == MAX_ATTEMPTS ) || !matched;
+    col.R = red;
+    col.G = green;
+    col.B = blue;
+  }
+  return col;
+}
+
 //=============================================================================
 /*!
  *
@@ -2311,6 +2341,15 @@ void SMESH_Mesh_i::SetAutoColor(CORBA::Boolean theAutoColor) throw(SALOME::SALOM
 {
   Unexpect aCatch(SALOME_SalomeException);
   _impl->SetAutoColor(theAutoColor);
+
+  std::list<SALOMEDS::Color> aReservedColors;
+  map<int, SMESH::SMESH_GroupBase_ptr>::iterator it = _mapGroups.begin();
+  for ( ; it != _mapGroups.end(); it++ ) {
+    if ( CORBA::is_nil( it->second )) continue;
+    SALOMEDS::Color aColor = getUniqueColor( aReservedColors );
+    it->second->SetColor( aColor );
+    aReservedColors.push_back( aColor );
+  }
 }
 
 //=============================================================================