Salome HOME
Color Number (Color Group) parameter is returned for compatibility
[modules/smesh.git] / src / SMESH_I / SMESH_Gen_i.cxx
index cf94c0ec7530b86d02e5d21ad0d4a9aa5620cf4a..7d57ff91627cee4cfd92c22b0a7a1f9d54ce3a66 100644 (file)
@@ -1030,7 +1030,7 @@ SMESH::compute_error_array* SMESH_Gen_i::GetComputeErrors( SMESH::SMESH_Mesh_ptr
   Unexpect aCatch(SALOME_SalomeException);
   if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetComputeErrors()" );
 
-  if ( CORBA::is_nil( theSubObject ) )
+  if ( CORBA::is_nil( theSubObject ) && theMesh->HasShapeToMesh())
     THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
 
   if ( CORBA::is_nil( theMesh ) )
@@ -1040,7 +1040,12 @@ SMESH::compute_error_array* SMESH_Gen_i::GetComputeErrors( SMESH::SMESH_Mesh_ptr
   try {
     if ( SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh ))
     {
-      TopoDS_Shape shape = GeomObjectToShape( theSubObject );
+      TopoDS_Shape shape;
+      if(theMesh->HasShapeToMesh())
+        shape = GeomObjectToShape( theSubObject );
+      else
+        shape = SMESH_Mesh::PseudoShape();
+      
       ::SMESH_Mesh& mesh = meshServant->GetImpl();
 
       error_array->length( mesh.GetMeshDS()->MaxShapeIndex() );
@@ -1095,7 +1100,7 @@ SMESH::algo_error_array* SMESH_Gen_i::GetAlgoState( SMESH::SMESH_Mesh_ptr theMes
   Unexpect aCatch(SALOME_SalomeException);
   if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetAlgoState()" );
 
-  if ( CORBA::is_nil( theSubObject ) )
+  if ( CORBA::is_nil( theSubObject ) && theMesh->HasShapeToMesh())
     THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
 
   if ( CORBA::is_nil( theMesh ) )
@@ -1106,7 +1111,12 @@ SMESH::algo_error_array* SMESH_Gen_i::GetAlgoState( SMESH::SMESH_Mesh_ptr theMes
     SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
     ASSERT( meshServant );
     if ( meshServant ) {
-      TopoDS_Shape myLocShape = GeomObjectToShape( theSubObject );
+      TopoDS_Shape myLocShape;
+      if(theMesh->HasShapeToMesh())
+        myLocShape = GeomObjectToShape( theSubObject );
+      else
+        myLocShape = SMESH_Mesh::PseudoShape();
+      
       ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
       list< ::SMESH_Gen::TAlgoStateError > error_list;
       list< ::SMESH_Gen::TAlgoStateError >::iterator error;
@@ -1224,7 +1234,7 @@ CORBA::Boolean SMESH_Gen_i::Compute( SMESH::SMESH_Mesh_ptr theMesh,
   Unexpect aCatch(SALOME_SalomeException);
   if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Compute" );
 
-  if ( CORBA::is_nil( theShapeObject ) )
+  if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
     THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", 
                                   SALOME::BAD_PARAM );
 
@@ -1244,7 +1254,11 @@ CORBA::Boolean SMESH_Gen_i::Compute( SMESH::SMESH_Mesh_ptr theMesh,
       // NPAL16168: "geometrical group edition from a submesh don't modifiy mesh computation"
       meshServant->CheckGeomGroupModif();
       // get local TopoDS_Shape
-      TopoDS_Shape myLocShape = GeomObjectToShape( theShapeObject );
+      TopoDS_Shape myLocShape;
+      if(theMesh->HasShapeToMesh())
+        myLocShape = GeomObjectToShape( theShapeObject );
+      else
+        myLocShape = SMESH_Mesh::PseudoShape();
       // call implementation compute
       ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
       return myGen.Compute( myLocMesh, myLocShape);
@@ -1349,12 +1363,33 @@ SMESH_Gen_i::FindGeometryByMeshElement( SMESH::SMESH_Mesh_ptr  theMesh,
     ::SMESH_Mesh & mesh = meshServant->GetImpl();
     SMESHDS_Mesh* meshDS = mesh.GetMeshDS();
     // find the element in mesh
-    if ( const SMDS_MeshElement * elem = meshDS->FindElement( theElementID ) )
+    if ( const SMDS_MeshElement * elem = meshDS->FindElement( theElementID ) ) {
       // find a shape id by the element
       if ( int shapeID = ::SMESH_MeshEditor( &mesh ).FindShape( elem )) {
         // get a geom object by the shape id
         GEOM::GEOM_Object_var geom = ShapeToGeomObject( meshDS->IndexToShape( shapeID ));
         if ( geom->_is_nil() ) {
+          // try to find a published sub-shape
+          SALOMEDS::SObject_var mainSO = ObjectToSObject( myCurrentStudy, mainShape );
+          SALOMEDS::ChildIterator_var it;
+          if ( !mainSO->_is_nil() )
+            it = myCurrentStudy->NewChildIterator( mainSO );
+          if ( !it->_is_nil() ) {
+            for ( it->InitEx(true); it->More(); it->Next() ) {
+              GEOM::GEOM_Object_var subGeom =
+                GEOM::GEOM_Object::_narrow( SObjectToObject( it->Value() ));
+              if ( !subGeom->_is_nil() ) {
+                GEOM::ListOfLong_var subList = subGeom->GetSubShapeIndices();
+                if ( subList->length() == 1 && shapeID == subList[0] ) {
+                  geom = subGeom;
+                  break;
+                }
+              }
+            }
+          }
+        }
+        if ( geom->_is_nil() ) {
+          // explode
           GEOM::GEOM_IShapesOperations_var op =
             geomGen->GetIShapesOperations( GetCurrentStudyID() );
           if ( !op->_is_nil() )
@@ -1365,6 +1400,7 @@ SMESH_Gen_i::FindGeometryByMeshElement( SMESH::SMESH_Mesh_ptr  theMesh,
          return geom._retn();
         }
       }
+    }
   }
   return GEOM::GEOM_Object::_nil();
 }
@@ -1785,6 +1821,7 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
             int id = myStudyContext->findId( string( objStr.in() ) );
             ::SMESH_Mesh& myLocMesh = myImpl->GetImpl();
             SMESHDS_Mesh* mySMESHDSMesh = myLocMesh.GetMeshDS();
+            bool hasShape = myLocMesh.HasShapeToMesh();
 
             // for each mesh open the HDF group basing on its id
             char meshGrpName[ 30 ];
@@ -1807,6 +1844,18 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
             aDataset->WriteOnDisk( ( char* )( strHasData.c_str() ) );
             aDataset->CloseOnDisk();
 
+           // ouv : NPAL12872
+            // for each mesh open the HDF group basing on its auto color parameter
+           char meshAutoColorName[ 30 ];
+           sprintf( meshAutoColorName, "AutoColorMesh %d", id );
+           int anAutoColor[1];
+           anAutoColor[0] = myImpl->GetAutoColor();
+           aSize[ 0 ] = 1;
+           aDataset = new HDFdataset( meshAutoColorName, aTopGroup, HDF_INT32, aSize, 1 );
+           aDataset->CreateOnDisk();
+           aDataset->WriteOnDisk( anAutoColor );
+           aDataset->CloseOnDisk();
+
             // write reference on a shape if exists
             SALOMEDS::SObject_var myRef;
             bool shapeRefFound = false;
@@ -1830,10 +1879,10 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
             // write applied hypotheses if exist
             SALOMEDS::SObject_var myHypBranch;
             found = gotBranch->FindSubObject( GetRefOnAppliedHypothesisTag(), myHypBranch );
-            if ( found && !shapeRefFound ) { // remove applied hyps
+            if ( found && !shapeRefFound && hasShape) { // remove applied hyps
               myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( myHypBranch );
             }
-            if ( found && shapeRefFound ) {
+            if ( found && (shapeRefFound || !hasShape) ) {
               aGroup = new HDFgroup( "Applied Hypotheses", aTopGroup );
               aGroup->CreateOnDisk();
 
@@ -1873,10 +1922,10 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
             // write applied algorithms if exist
             SALOMEDS::SObject_var myAlgoBranch;
             found = gotBranch->FindSubObject( GetRefOnAppliedAlgorithmsTag(), myAlgoBranch );
-            if ( found && !shapeRefFound ) { // remove applied algos
+            if ( found && !shapeRefFound && hasShape) { // remove applied algos
               myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( myAlgoBranch );
             }
-            if ( found && shapeRefFound ) {
+            if ( found && (shapeRefFound || !hasShape)) {
               aGroup = new HDFgroup( "Applied Algorithms", aTopGroup );
               aGroup->CreateOnDisk();
 
@@ -2132,6 +2181,22 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
                    aDataset->WriteOnDisk( aUserName );
                    aDataset->CloseOnDisk();
 
+                   // ouv : NPAL12872
+                   // For each group, create a dataset named "Group <group_persistent_id> Color"
+                   // and store the group's color into it
+                   char grpColorName[ 30 ];
+                   sprintf( grpColorName, "ColorGroup %d", anId );
+                   SALOMEDS::Color aColor = myGroupImpl->GetColor();
+                   double anRGB[3];
+                   anRGB[ 0 ] = aColor.R;
+                   anRGB[ 1 ] = aColor.G;
+                   anRGB[ 2 ] = aColor.B;
+                   aSize[ 0 ] = 3;
+                   aDataset = new HDFdataset( grpColorName, aGroup, HDF_FLOAT64, aSize, 1 );
+                   aDataset->CreateOnDisk();
+                   aDataset->WriteOnDisk( anRGB );
+                   aDataset->CloseOnDisk();
+
                    // Store the group contents into MED file
                    if ( myLocMesh.GetGroup( myGroupImpl->GetLocalID() ) ) {
                      
@@ -2185,7 +2250,7 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
              myWriter.Perform();
              
              // maybe a shape was deleted in the study
-             if ( !shapeRefFound && !mySMESHDSMesh->ShapeToMesh().IsNull() ) {
+             if ( !shapeRefFound && !mySMESHDSMesh->ShapeToMesh().IsNull() && hasShape) {
                TopoDS_Shape nullShape;
                myLocMesh.ShapeToMesh( nullShape ); // remove shape referring data
              }
@@ -2806,6 +2871,21 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
          int newId = myStudyContext->findId( iorString );
          myStudyContext->mapOldToNew( id, newId );
 
+         // ouv : NPAL12872
+         // try to read and set auto color flag
+         char aMeshAutoColorName[ 30 ];
+         sprintf( aMeshAutoColorName, "AutoColorMesh %d", id);
+         if( aTopGroup->ExistInternalObject( aMeshAutoColorName ) )
+         {
+           aDataset = new HDFdataset( aMeshAutoColorName, aTopGroup );
+           aDataset->OpenOnDisk();
+           size = aDataset->GetSize();
+           int* anAutoColor = new int[ size ];
+           aDataset->ReadFromDisk( anAutoColor );
+           aDataset->CloseOnDisk();
+           myNewMeshImpl->SetAutoColor( (bool)anAutoColor[0] );
+         }
+
           // try to read and set reference to shape
           GEOM::GEOM_Object_var aShapeObject;
           if ( aTopGroup->ExistInternalObject( "Ref on shape" ) ) {
@@ -2911,7 +2991,8 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
               CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
               if ( !CORBA::is_nil( hypObject ) ) {
                 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
-                if ( !anHyp->_is_nil() && !aShapeObject->_is_nil() )
+                if ( !anHyp->_is_nil() && (!aShapeObject->_is_nil()
+                                           || !myNewMeshImpl->HasShapeToMesh()) )
                   myNewMeshImpl->addHypothesis( aShapeObject, anHyp );
               }
             }
@@ -2948,7 +3029,8 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
               CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
               if ( !CORBA::is_nil( hypObject ) ) {
                 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
-                if ( !anHyp->_is_nil() && !aShapeObject->_is_nil() )
+                if ( !anHyp->_is_nil() && (!aShapeObject->_is_nil()
+                                           || !myNewMeshImpl->HasShapeToMesh()) )
                   myNewMeshImpl->addHypothesis( aShapeObject, anHyp );
               }
             }
@@ -3314,9 +3396,14 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
       } // if ( hasData )
 
       // Recompute State (as computed sub-meshes are restored from MED)
-      if ( !aShapeObject->_is_nil() ) {
+      if ( !aShapeObject->_is_nil() || !myNewMeshImpl->HasShapeToMesh()) {
         MESSAGE("Compute State Engine ...");
-        TopoDS_Shape myLocShape = GeomObjectToShape( aShapeObject );
+        TopoDS_Shape myLocShape;
+        if(myNewMeshImpl->HasShapeToMesh())
+          myLocShape = GeomObjectToShape( aShapeObject );
+        else
+          myLocShape = SMESH_Mesh::PseudoShape();
+        
         myNewMeshImpl->GetImpl().GetSubMesh(myLocShape)->ComputeStateEngine
           (SMESH_subMesh::SUBMESH_RESTORED);
         MESSAGE("Compute State Engine finished");
@@ -3403,6 +3490,22 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
               SMESHDS_GroupBase* aGroupBaseDS = aLocalGroup->GetGroupDS();
               aGroupBaseDS->SetStoreName( name_dataset );
 
+             // ouv : NPAL12872
+             // Read color of the group
+              char aGroupColorName[ 30 ];
+              sprintf( aGroupColorName, "ColorGroup %d", subid);
+              if ( aGroup->ExistInternalObject( aGroupColorName ) )
+             {
+               aDataset = new HDFdataset( aGroupColorName, aGroup );
+               aDataset->OpenOnDisk();
+               size = aDataset->GetSize();
+               double* anRGB = new double[ size ];
+               aDataset->ReadFromDisk( anRGB );
+               aDataset->CloseOnDisk();
+               Quantity_Color aColor( anRGB[0], anRGB[1], anRGB[2], Quantity_TOC_RGB );
+               aGroupBaseDS->SetColor( aColor );
+             }
+
               // Fill group with contents from MED file
               SMESHDS_Group* aGrp = dynamic_cast<SMESHDS_Group*>( aGroupBaseDS );
               if ( aGrp )
@@ -3486,6 +3589,19 @@ void SMESH_Gen_i::Close( SALOMEDS::SComponent_ptr theComponent )
     delete myStudyContextMap[ studyId ];
     myStudyContextMap.erase( studyId );
   }
+
+  // delete SMESH_Mesh's
+  StudyContextStruct* context = myGen.GetStudyContext( studyId );
+  map< int, SMESH_Mesh* >::iterator i_mesh = context->mapMesh.begin();
+  for ( ; i_mesh != context->mapMesh.end(); ++i_mesh )
+    delete i_mesh->second;
+  // delete SMESHDS_Mesh's
+  // it's too long on big meshes
+//   if ( context->myDocument ) {
+//     delete context->myDocument;
+//     context->myDocument = 0;
+//   }
+  
   return;
 }