Version: 6.3.1

src/SMESH_I/SMESH_Gen_i_1.cxx

Go to the documentation of this file.
00001 // Copyright (C) 2007-2011  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 //  File      : SMESH_Gen_i_1.cxx
00023 //  Created   : Thu Oct 21 17:24:06 2004
00024 //  Author    : Edward AGAPOV (eap)
00025 //  Module    : SMESH
00026 //  $Header   : $
00027 
00028 #include "SMESH_Gen_i.hxx"
00029 
00030 #include "SMESH_Mesh_i.hxx"
00031 #include "SMESH_Hypothesis_i.hxx"
00032 #include "SMESH_Algo_i.hxx"
00033 #include "SMESH_Group_i.hxx"
00034 #include "SMESH_subMesh_i.hxx"
00035 
00036 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
00037 
00038 #include "utilities.h"
00039 #include "Utils_ExceptHandlers.hxx"
00040 
00041 #include <TCollection_AsciiString.hxx>
00042 
00043 #ifdef _DEBUG_
00044 static int MYDEBUG = 0;
00045 static int VARIABLE_DEBUG = 0;
00046 #else
00047 static int MYDEBUG = 0;
00048 static int VARIABLE_DEBUG = 0;
00049 #endif
00050 
00051 //=============================================================================
00057 //=============================================================================
00058 
00059 long SMESH_Gen_i::GetHypothesisRootTag()
00060 {
00061   return SMESH::Tag_HypothesisRoot;
00062 }
00063 
00064 long SMESH_Gen_i::GetAlgorithmsRootTag()
00065 {
00066   return SMESH::Tag_AlgorithmsRoot;
00067 }
00068 
00069 long SMESH_Gen_i::GetRefOnShapeTag()
00070 {
00071   return SMESH::Tag_RefOnShape;
00072 }
00073 
00074 long SMESH_Gen_i::GetRefOnAppliedHypothesisTag()
00075 {
00076   return SMESH::Tag_RefOnAppliedHypothesis;
00077 }
00078 
00079 long SMESH_Gen_i::GetRefOnAppliedAlgorithmsTag()
00080 {
00081   return SMESH::Tag_RefOnAppliedAlgorithms;
00082 }
00083 
00084 long SMESH_Gen_i::GetSubMeshOnVertexTag()
00085 {
00086   return SMESH::Tag_SubMeshOnVertex;
00087 }
00088 
00089 long SMESH_Gen_i::GetSubMeshOnEdgeTag()
00090 {
00091   return SMESH::Tag_SubMeshOnEdge;
00092 }
00093 
00094 long SMESH_Gen_i::GetSubMeshOnFaceTag()
00095 {
00096   return SMESH::Tag_SubMeshOnFace;
00097 }
00098 
00099 long SMESH_Gen_i::GetSubMeshOnSolidTag()
00100 {
00101   return SMESH::Tag_SubMeshOnSolid;
00102 }
00103 
00104 long SMESH_Gen_i::GetSubMeshOnCompoundTag()
00105 {
00106   return SMESH::Tag_SubMeshOnCompound;
00107 }
00108 
00109 long SMESH_Gen_i::GetSubMeshOnWireTag()
00110 {
00111   return SMESH::Tag_SubMeshOnWire;
00112 }
00113 
00114 long SMESH_Gen_i::GetSubMeshOnShellTag()
00115 {
00116   return SMESH::Tag_SubMeshOnShell;
00117 }
00118 
00119 long SMESH_Gen_i::GetNodeGroupsTag()
00120 {
00121   return SMESH::Tag_NodeGroups;
00122 }
00123 
00124 long SMESH_Gen_i::GetEdgeGroupsTag()
00125 {
00126   return SMESH::Tag_EdgeGroups;
00127 }
00128 
00129 long SMESH_Gen_i::GetFaceGroupsTag()
00130 {
00131   return SMESH::Tag_FaceGroups;
00132 }
00133 
00134 long SMESH_Gen_i::GetVolumeGroupsTag()
00135 {
00136   return SMESH::Tag_VolumeGroups;
00137 }
00138 
00139 long SMESH_Gen_i::Get0DElementsGroupsTag()
00140 {
00141   return SMESH::Tag_0DElementsGroups;
00142 }
00143 
00144 //=============================================================================
00150 //=============================================================================
00151 
00152 bool SMESH_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
00153 {
00154   if(MYDEBUG) MESSAGE("CanPublishInStudy - "<<!CORBA::is_nil(myCurrentStudy));
00155   if(CORBA::is_nil(myCurrentStudy))
00156     return false;
00157   
00158   SMESH::SMESH_Mesh_var aMesh       = SMESH::SMESH_Mesh::_narrow(theIOR);
00159   if( !aMesh->_is_nil() )
00160     return true;
00161 
00162   SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(theIOR);
00163   if( !aSubMesh->_is_nil() )
00164     return true;
00165 
00166   SMESH::SMESH_Hypothesis_var aHyp  = SMESH::SMESH_Hypothesis::_narrow(theIOR);
00167   if( !aHyp->_is_nil() )
00168     return true;
00169 
00170   SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
00171   if( !aGroup->_is_nil() )
00172     return true;
00173 
00174   if(MYDEBUG) MESSAGE("CanPublishInStudy--CANT");
00175   return false;
00176 }
00177 
00178 //=======================================================================
00179 //function : ObjectToSObject
00180 //purpose  : 
00181 //=======================================================================
00182 
00183 SALOMEDS::SObject_ptr SMESH_Gen_i::ObjectToSObject(SALOMEDS::Study_ptr theStudy,
00184                                                    CORBA::Object_ptr   theObject)
00185 {
00186   SALOMEDS::SObject_var aSO;
00187   if ( !CORBA::is_nil( theStudy ) && !CORBA::is_nil( theObject ))
00188   {
00189     CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theObject );
00190     aSO = theStudy->FindObjectIOR( objStr.in() );
00191   }
00192   return aSO._retn();
00193 }
00194 
00195 //=======================================================================
00196 //function : objectToServant
00197 //purpose  : 
00198 //=======================================================================
00199 
00200 template<typename T> static inline T* objectToServant( CORBA::Object_ptr theIOR )
00201 {
00202   return dynamic_cast<T*>( SMESH_Gen_i::GetServant( theIOR ).in() );
00203 }
00204 
00205 //=======================================================================
00206 //function : ShapeToGeomObject
00207 //purpose  : 
00208 //=======================================================================
00209 
00210 GEOM::GEOM_Object_ptr SMESH_Gen_i::ShapeToGeomObject (const TopoDS_Shape& theShape )
00211 {
00212   GEOM::GEOM_Object_var aShapeObj;
00213   if ( !theShape.IsNull() ) {
00214     GEOM_Client* aClient = GetShapeReader();
00215     TCollection_AsciiString IOR;
00216     if ( aClient && aClient->Find( theShape, IOR ))
00217     {
00218       CORBA::Object_var obj = GetORB()->string_to_object( IOR.ToCString() );
00219       aShapeObj = GEOM::GEOM_Object::_narrow ( obj );
00220     }
00221   }
00222   return aShapeObj._retn();
00223 }
00224 
00225 //=======================================================================
00226 //function : GeomObjectToShape
00227 //purpose  : 
00228 //=======================================================================
00229 
00230 TopoDS_Shape SMESH_Gen_i::GeomObjectToShape(GEOM::GEOM_Object_ptr theGeomObject)
00231 {
00232   TopoDS_Shape S;
00233   if ( !theGeomObject->_is_nil() ) {
00234     GEOM_Client* aClient = GetShapeReader();
00235     GEOM::GEOM_Gen_ptr aGeomEngine = GetGeomEngine();
00236     if ( aClient && !aGeomEngine->_is_nil () )
00237       S = aClient->GetShape( aGeomEngine, theGeomObject );
00238   }
00239   return S;
00240 }
00241 
00242 //=======================================================================
00243 //function : publish
00244 //purpose  : 
00245 //=======================================================================
00246 
00247 static SALOMEDS::SObject_ptr publish(SALOMEDS::Study_ptr   theStudy,
00248                                      CORBA::Object_ptr     theIOR,
00249                                      SALOMEDS::SObject_ptr theFatherObject,
00250                                      const int             theTag = 0,
00251                                      const char*           thePixMap = 0,
00252                                      const bool            theSelectable = true)
00253 {
00254   SALOMEDS::SObject_var SO = SMESH_Gen_i::ObjectToSObject( theStudy, theIOR );
00255   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
00256   if ( SO->_is_nil() ) {
00257     if ( theTag == 0 )
00258       SO = aStudyBuilder->NewObject( theFatherObject );
00259     else if ( !theFatherObject->FindSubObject( theTag, SO ))
00260       SO = aStudyBuilder->NewObjectToTag( theFatherObject, theTag );
00261   }
00262 
00263   SALOMEDS::GenericAttribute_var anAttr;
00264   if ( !CORBA::is_nil( theIOR )) {
00265     anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeIOR" );
00266     CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theIOR );
00267     SALOMEDS::AttributeIOR::_narrow(anAttr)->SetValue( objStr.in() );
00268   }
00269   if ( thePixMap ) {
00270     anAttr  = aStudyBuilder->FindOrCreateAttribute( SO, "AttributePixMap" );
00271     SALOMEDS::AttributePixMap_var pm = SALOMEDS::AttributePixMap::_narrow( anAttr );
00272     pm->SetPixMap( thePixMap );
00273   }
00274   if ( !theSelectable ) {
00275     anAttr   = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeSelectable" );
00276     SALOMEDS::AttributeSelectable::_narrow( anAttr )->SetSelectable( false );
00277   }
00278   return SO._retn();
00279 }
00280 
00281 //=======================================================================
00282 //function : setName
00283 //purpose  : 
00284 //=======================================================================
00285 
00286 void SMESH_Gen_i::SetName(SALOMEDS::SObject_ptr theSObject,
00287                           const char*           theName,
00288                           const char*           theDefaultName)
00289 {
00290   if ( !theSObject->_is_nil() ) {
00291     SALOMEDS::StudyBuilder_var aStudyBuilder = theSObject->GetStudy()->NewBuilder();
00292     SALOMEDS::GenericAttribute_var anAttr =
00293       aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributeName" );
00294     SALOMEDS::AttributeName_var aNameAttr = SALOMEDS::AttributeName::_narrow( anAttr );
00295     if ( theName && strlen( theName ) != 0 )
00296       aNameAttr->SetValue( theName );
00297     else {
00298       CORBA::String_var curName = CORBA::string_dup( aNameAttr->Value() );
00299       if ( strlen( curName ) == 0 ) {
00300         TCollection_AsciiString aName( (char*) theDefaultName );
00301         aName += TCollection_AsciiString("_") + TCollection_AsciiString( theSObject->Tag() );
00302         aNameAttr->SetValue( aName.ToCString() );
00303       }
00304     }
00305   }
00306 }
00307 
00308 //=======================================================================
00309 //function : SetPixMap
00310 //purpose  : 
00311 //=======================================================================
00312 
00313 void SMESH_Gen_i::SetPixMap(SALOMEDS::SObject_ptr theSObject,
00314                             const char*           thePixMap)
00315 {
00316   if ( !theSObject->_is_nil() && thePixMap && strlen( thePixMap ))
00317   {
00318     SALOMEDS::Study_var aStudy = theSObject->GetStudy();
00319     SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
00320     SALOMEDS::GenericAttribute_var anAttr =
00321       aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributePixMap" );
00322     SALOMEDS::AttributePixMap_var aPMAttr = SALOMEDS::AttributePixMap::_narrow( anAttr );
00323     aPMAttr->SetPixMap( thePixMap );
00324   }
00325 }
00326 
00327 //=======================================================================
00328 //function : addReference
00329 //purpose  : 
00330 //=======================================================================
00331 
00332 static void addReference (SALOMEDS::Study_ptr   theStudy,
00333                           SALOMEDS::SObject_ptr theSObject,
00334                           CORBA::Object_ptr     theToObject,
00335                           int                   theTag = 0)
00336 {
00337   SALOMEDS::SObject_var aToObjSO = SMESH_Gen_i::ObjectToSObject( theStudy, theToObject );
00338   if ( !aToObjSO->_is_nil() && !theSObject->_is_nil() ) {
00339     SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
00340     SALOMEDS::SObject_var aReferenceSO;
00341     if ( !theTag ) {
00342       // check if the reference to theToObject already exists
00343       // and find a free label for the reference object
00344       bool isReferred = false;
00345       int tag = 1;
00346       SALOMEDS::ChildIterator_var anIter = theStudy->NewChildIterator( theSObject );
00347       for ( ; !isReferred && anIter->More(); anIter->Next(), ++tag ) {
00348         if ( anIter->Value()->ReferencedObject( aReferenceSO )) {
00349           if ( strcmp( aReferenceSO->GetID(), aToObjSO->GetID() ) == 0 )
00350             isReferred = true;
00351         }
00352         else if ( !theTag ) {
00353           SALOMEDS::GenericAttribute_var anAttr;
00354           if ( !anIter->Value()->FindAttribute( anAttr, "AttributeIOR" ))
00355             theTag = tag;
00356         }
00357       }
00358       if ( isReferred )
00359         return;
00360       if ( !theTag )
00361         theTag = tag;
00362     }
00363     if ( !theSObject->FindSubObject( theTag, aReferenceSO ))
00364       aReferenceSO = aStudyBuilder->NewObjectToTag( theSObject, theTag );
00365     aStudyBuilder->Addreference( aReferenceSO, aToObjSO );
00366   }
00367 }
00368 
00369 //=============================================================================
00375 //=============================================================================
00376 
00377 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishInStudy(SALOMEDS::Study_ptr   theStudy,
00378                                                   SALOMEDS::SObject_ptr theSObject,
00379                                                   CORBA::Object_ptr     theIOR,
00380                                                   const char*           theName)
00381      throw (SALOME::SALOME_Exception)
00382 {
00383   Unexpect aCatch(SALOME_SalomeException);
00384   SALOMEDS::SObject_var aSO;
00385   if ( CORBA::is_nil( theStudy ) || CORBA::is_nil( theIOR ))
00386     return aSO._retn();
00387   if(MYDEBUG) MESSAGE("PublishInStudy");
00388 
00389   // Publishing a mesh
00390   SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( theIOR );
00391   if( !aMesh->_is_nil() )
00392     aSO = PublishMesh( theStudy, aMesh, theName );
00393 
00394   // Publishing a sub-mesh
00395   SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow( theIOR );
00396   if( aSO->_is_nil() && !aSubMesh->_is_nil() ) {
00397     GEOM::GEOM_Object_var aShapeObject = aSubMesh->GetSubShape();
00398     aMesh = aSubMesh->GetFather();
00399     aSO = PublishSubMesh( theStudy, aMesh, aSubMesh, aShapeObject, theName );
00400   }
00401 
00402   // Publishing a hypothesis or algorithm
00403   SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( theIOR );
00404   if ( aSO->_is_nil() && !aHyp->_is_nil() )
00405     aSO = PublishHypothesis( theStudy, aHyp );
00406 
00407   // Publishing a group
00408   SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
00409   if ( aSO->_is_nil() && !aGroup->_is_nil() ) {
00410     GEOM::GEOM_Object_var aShapeObject;
00411     aMesh = aGroup->GetMesh();
00412     aSO = PublishGroup( theStudy, aMesh, aGroup, aShapeObject, theName );
00413   }
00414   if(MYDEBUG) MESSAGE("PublishInStudy_END");
00415 
00416   return aSO._retn();
00417 }
00418 
00419 //=======================================================================
00420 //function : PublishComponent
00421 //purpose  : 
00422 //=======================================================================
00423 
00424 SALOMEDS::SComponent_ptr SMESH_Gen_i::PublishComponent(SALOMEDS::Study_ptr theStudy)
00425 {
00426   if ( CORBA::is_nil( theStudy ))
00427     return SALOMEDS::SComponent::_nil();
00428   if(MYDEBUG) MESSAGE("PublishComponent");
00429 
00430   SALOMEDS::SComponent_var father =
00431     SALOMEDS::SComponent::_narrow( theStudy->FindComponent( ComponentDataType() ) );
00432   if ( !CORBA::is_nil( father ) )
00433     return father._retn();
00434 
00435   SALOME_ModuleCatalog::ModuleCatalog_var aCat =
00436     SALOME_ModuleCatalog::ModuleCatalog::_narrow( GetNS()->Resolve("/Kernel/ModulCatalog") );
00437   if ( CORBA::is_nil( aCat ) )
00438     return father._retn();
00439 
00440   SALOME_ModuleCatalog::Acomponent_var aComp = aCat->GetComponent( ComponentDataType() );
00441   if ( CORBA::is_nil( aComp ) )
00442     return father._retn();
00443 
00444   SALOMEDS::StudyBuilder_var     aStudyBuilder = theStudy->NewBuilder(); 
00445   SALOMEDS::GenericAttribute_var anAttr;
00446   SALOMEDS::AttributePixMap_var  aPixmap;
00447 
00448   father  = aStudyBuilder->NewComponent( ComponentDataType() );
00449   aStudyBuilder->DefineComponentInstance( father, SMESH_Gen::_this() );
00450   anAttr  = aStudyBuilder->FindOrCreateAttribute( father, "AttributePixMap" );
00451   aPixmap = SALOMEDS::AttributePixMap::_narrow( anAttr );
00452   aPixmap ->SetPixMap( "ICON_OBJBROWSER_SMESH" );
00453   SetName( father, aComp->componentusername(), "MESH" );
00454   if(MYDEBUG) MESSAGE("PublishComponent--END");
00455 
00456   return father._retn();
00457 }
00458 
00459 //=============================================================================
00465 //=============================================================================
00466 
00467 static long findMaxChildTag( SALOMEDS::SObject_ptr theSObject )
00468 {
00469   long aTag = 0;
00470   if ( !theSObject->_is_nil() ) {
00471     SALOMEDS::Study_var aStudy = theSObject->GetStudy();
00472     if ( !aStudy->_is_nil() ) {
00473       SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator( theSObject );
00474       for ( ; anIter->More(); anIter->Next() ) {
00475         long nTag = anIter->Value()->Tag();
00476         if ( nTag > aTag )
00477           aTag = nTag;
00478       }
00479     }
00480   }
00481   return aTag;
00482 }
00483 
00484 //=======================================================================
00485 //function : PublishMesh
00486 //purpose  : 
00487 //=======================================================================
00488 
00489 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishMesh (SALOMEDS::Study_ptr   theStudy,
00490                                                 SMESH::SMESH_Mesh_ptr theMesh,
00491                                                 const char*           theName)
00492 {
00493   if ( CORBA::is_nil( theStudy ) ||
00494        CORBA::is_nil( theMesh ))
00495     return SALOMEDS::SComponent::_nil();
00496   if(MYDEBUG) MESSAGE("PublishMesh--IN");
00497 
00498   // find or publish a mesh
00499 
00500   SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
00501   if ( aMeshSO->_is_nil() )
00502   {
00503     SALOMEDS::SComponent_var father = PublishComponent( theStudy );
00504     if ( father->_is_nil() )
00505       return aMeshSO._retn();
00506 
00507     // Find correct free tag
00508     long aTag = findMaxChildTag( father.in() );
00509     if ( aTag <= GetAlgorithmsRootTag() )
00510       aTag = GetAlgorithmsRootTag() + 1;
00511     else
00512       aTag++;
00513 
00514     aMeshSO = publish (theStudy, theMesh, father, aTag, "ICON_SMESH_TREE_MESH_WARN" );
00515     if ( aMeshSO->_is_nil() )
00516       return aMeshSO._retn();
00517   }
00518   SetName( aMeshSO, theName, "Mesh" );
00519 
00520   // Add shape reference
00521 
00522   GEOM::GEOM_Object_var aShapeObject = theMesh->GetShapeToMesh();
00523   if ( !CORBA::is_nil( aShapeObject )) {
00524     addReference( theStudy, aMeshSO, aShapeObject, GetRefOnShapeTag() );
00525 
00526     // Publish global hypotheses
00527 
00528     SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( aShapeObject );
00529     for ( int i = 0; i < hypList->length(); i++ ) {
00530       SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( hypList[ i ]);
00531       PublishHypothesis( theStudy, aHyp );
00532       AddHypothesisToShape( theStudy, theMesh, aShapeObject, aHyp );
00533     }
00534   }
00535 
00536   // Publish submeshes
00537 
00538   SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
00539   if ( !mesh_i )
00540     return aMeshSO._retn();
00541   map<int, SMESH_subMesh_i*>& subMap = mesh_i->_mapSubMesh_i;
00542   map<int, SMESH_subMesh_i*>::iterator subIt = subMap.begin();
00543   for ( ; subIt != subMap.end(); subIt++ ) {
00544     SMESH::SMESH_subMesh_ptr aSubMesh = (*subIt).second->_this();
00545     if ( !CORBA::is_nil( aSubMesh )) {
00546       aShapeObject = aSubMesh->GetSubShape();
00547       PublishSubMesh( theStudy, theMesh, aSubMesh, aShapeObject );
00548     }
00549   }
00550 
00551   // Publish groups
00552   const map<int, SMESH::SMESH_GroupBase_ptr>& grMap = mesh_i->getGroups();
00553   map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator it = grMap.begin();
00554   for ( ; it != grMap.end(); it++ )
00555   {
00556     SMESH::SMESH_GroupBase_ptr aGroup = (*it).second;
00557     if ( !aGroup->_is_nil() ) {
00558       GEOM::GEOM_Object_var  aShapeObj;
00559       SMESH::SMESH_GroupOnGeom_var aGeomGroup =
00560         SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
00561       if ( !aGeomGroup->_is_nil() )
00562         aShapeObj = aGeomGroup->GetShape();
00563       PublishGroup( theStudy, theMesh, aGroup, aShapeObj );
00564     }
00565   }
00566 
00567   if(MYDEBUG) MESSAGE("PublishMesh_END");
00568   return aMeshSO._retn();
00569 }
00570 
00571 //=======================================================================
00572 //function : PublishSubMesh
00573 //purpose  : 
00574 //=======================================================================
00575 
00576 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishSubMesh (SALOMEDS::Study_ptr      theStudy,
00577                                                    SMESH::SMESH_Mesh_ptr    theMesh,
00578                                                    SMESH::SMESH_subMesh_ptr theSubMesh,
00579                                                    GEOM::GEOM_Object_ptr    theShapeObject,
00580                                                    const char*              theName)
00581 {
00582   if (theStudy->_is_nil() || theMesh->_is_nil() ||
00583       theSubMesh->_is_nil() || theShapeObject->_is_nil() )
00584     return SALOMEDS::SObject::_nil();
00585 
00586   SALOMEDS::SObject_var aSubMeshSO = ObjectToSObject( theStudy, theSubMesh );
00587   if ( aSubMeshSO->_is_nil() )
00588   {
00589     SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
00590     if ( aMeshSO->_is_nil() ) {
00591       aMeshSO = PublishMesh( theStudy, theMesh );
00592       if ( aMeshSO->_is_nil())
00593         return SALOMEDS::SObject::_nil();
00594     }
00595     // Find submesh sub-tree tag
00596     long aRootTag;
00597     const char* aRootName = "";
00598     switch ( theShapeObject->GetShapeType() ) {
00599     case GEOM::VERTEX:
00600       aRootTag  = GetSubMeshOnVertexTag();
00601       aRootName = "SubMeshes on Vertex";
00602       break;
00603     case GEOM::EDGE:
00604       aRootTag  = GetSubMeshOnEdgeTag();
00605       aRootName = "SubMeshes on Edge";
00606       break;
00607     case GEOM::WIRE:
00608       aRootTag  = GetSubMeshOnWireTag();
00609       aRootName = "SubMeshes on Wire";
00610       break;
00611     case GEOM::FACE:
00612       aRootTag  = GetSubMeshOnFaceTag();
00613       aRootName = "SubMeshes on Face";    
00614       break;
00615     case GEOM::SHELL:
00616       aRootTag  = GetSubMeshOnShellTag();
00617       aRootName = "SubMeshes on Shell";   
00618       break;
00619     case GEOM::SOLID:
00620       aRootTag  = GetSubMeshOnSolidTag();
00621       aRootName = "SubMeshes on Solid";
00622       break;
00623     default:
00624       aRootTag  = GetSubMeshOnCompoundTag();
00625       aRootName = "SubMeshes on Compound";
00626       break;
00627     }
00628 
00629     // Find or create submesh root
00630     SALOMEDS::SObject_var aRootSO = publish (theStudy, CORBA::Object::_nil(),
00631                                              aMeshSO, aRootTag, 0, false );
00632     SetName( aRootSO, aRootName );
00633 
00634     // Add new submesh to corresponding sub-tree
00635     SMESH::array_of_ElementType_var elemTypes = theSubMesh->GetTypes();
00636     const int isEmpty = ( elemTypes->length() == 0 );
00637     const char* pm[2] = { "ICON_SMESH_TREE_MESH", "ICON_SMESH_TREE_MESH_WARN" };
00638     aSubMeshSO = publish (theStudy, theSubMesh, aRootSO, 0, pm[isEmpty] );
00639     if ( aSubMeshSO->_is_nil() )
00640       return aSubMeshSO._retn();
00641   }
00642   SetName( aSubMeshSO, theName, "SubMesh" );
00643 
00644   // Add reference to theShapeObject
00645 
00646   addReference( theStudy, aSubMeshSO, theShapeObject, 1 );
00647 
00648   // Publish hypothesis
00649 
00650   SMESH::ListOfHypothesis * hypList = theMesh->GetHypothesisList( theShapeObject );
00651   if ( hypList )
00652     for ( int i = 0; i < hypList->length(); i++ ) {
00653       SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( (*hypList)[ i ]);
00654       PublishHypothesis( theStudy, aHyp );
00655       AddHypothesisToShape( theStudy, theMesh, theShapeObject, aHyp );
00656     }
00657 
00658   return aSubMeshSO._retn();
00659 }
00660 
00661 //=======================================================================
00662 //function : PublishGroup
00663 //purpose  : 
00664 //=======================================================================
00665 
00666 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishGroup (SALOMEDS::Study_ptr    theStudy,
00667                                                  SMESH::SMESH_Mesh_ptr  theMesh,
00668                                                  SMESH::SMESH_GroupBase_ptr theGroup,
00669                                                  GEOM::GEOM_Object_ptr  theShapeObject,
00670                                                  const char*            theName)
00671 {
00672   if (theStudy->_is_nil() || theMesh->_is_nil() || theGroup->_is_nil() )
00673     return SALOMEDS::SObject::_nil();
00674 
00675   SALOMEDS::SObject_var aGroupSO = ObjectToSObject( theStudy, theGroup );
00676   if ( aGroupSO->_is_nil() )
00677   {
00678     SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
00679     if ( aMeshSO->_is_nil() ) {
00680       aMeshSO = PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), theMesh, "");
00681       if ( aMeshSO->_is_nil())
00682         return SALOMEDS::SObject::_nil();
00683     }
00684     int aType = (int)theGroup->GetType();
00685     const char* aRootNames[] = {
00686       "Compound Groups", "Groups of Nodes", "Groups of Edges",
00687       "Groups of Faces", "Groups of Volumes", "Groups of 0D Elements" };
00688 
00689     // Currently, groups with heterogenous content are not supported
00690     if ( aType != SMESH::ALL ) {
00691       long aRootTag = GetNodeGroupsTag() + aType - 1;
00692 
00693       // Find or create groups root
00694       SALOMEDS::SObject_var aRootSO = publish (theStudy, CORBA::Object::_nil(),
00695                                                aMeshSO, aRootTag, 0, false );
00696       if ( aType < 6 )
00697         SetName( aRootSO, aRootNames[aType] );
00698 
00699       // Add new group to corresponding sub-tree
00700       SMESH::array_of_ElementType_var elemTypes = theGroup->GetTypes();
00701       const int isEmpty = ( elemTypes->length() == 0 );
00702       const char* pm[2] = { "ICON_SMESH_TREE_GROUP", "ICON_SMESH_TREE_MESH_WARN" };
00703       aGroupSO = publish (theStudy, theGroup, aRootSO, 0, pm[ isEmpty ] );
00704     }
00705     if ( aGroupSO->_is_nil() )
00706       return aGroupSO._retn();
00707   }
00708 
00709   SetName( aGroupSO, theName, "Group" );
00710 
00711   //Add reference to geometry
00712   if ( !theShapeObject->_is_nil() )
00713     addReference( theStudy, aGroupSO, theShapeObject, 1 );
00714 
00715   return aGroupSO._retn();
00716 }
00717 
00718 //=======================================================================
00719 //function : PublishHypothesis
00720 //purpose  : 
00721 //=======================================================================
00722 
00723 SALOMEDS::SObject_ptr
00724   SMESH_Gen_i::PublishHypothesis (SALOMEDS::Study_ptr         theStudy,
00725                                   SMESH::SMESH_Hypothesis_ptr theHyp,
00726                                   const char*                 theName)
00727 {
00728   if(MYDEBUG) MESSAGE("PublishHypothesis")
00729   if (theStudy->_is_nil() || theHyp->_is_nil())
00730     return SALOMEDS::SObject::_nil();
00731 
00732   SALOMEDS::SObject_var aHypSO = ObjectToSObject( theStudy, theHyp );
00733   if ( aHypSO->_is_nil() )
00734   {
00735     SALOMEDS::SComponent_var father = PublishComponent( theStudy );
00736     if ( father->_is_nil() )
00737       return aHypSO._retn();
00738 
00739     //Find or Create Hypothesis root
00740     bool isAlgo = ( !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil() );
00741     int aRootTag = isAlgo ? GetAlgorithmsRootTag() : GetHypothesisRootTag();
00742     SALOMEDS::SObject_var aRootSO =
00743       publish (theStudy, CORBA::Object::_nil(),father, aRootTag,
00744                isAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
00745     SetName( aRootSO, isAlgo ?  "Algorithms" : "Hypotheses" );
00746 
00747     // Add New Hypothesis
00748     string aPmName = isAlgo ? "ICON_SMESH_TREE_ALGO_" : "ICON_SMESH_TREE_HYPO_";
00749     aPmName += theHyp->GetName();
00750     // prepend plugin name to pixmap name
00751     string pluginName = myHypCreatorMap[string(theHyp->GetName())]->GetModuleName();
00752     if ( pluginName != "StdMeshers" )
00753       aPmName = pluginName + "::" + aPmName;
00754     aHypSO = publish( theStudy, theHyp, aRootSO, 0, aPmName.c_str() );
00755   }
00756 
00757   if ( !aHypSO->_is_nil() ) {
00758     CORBA::String_var aHypName = CORBA::string_dup( theHyp->GetName() );
00759     SetName( aHypSO, theName, aHypName );
00760   }
00761 
00762   if(MYDEBUG) MESSAGE("PublishHypothesis--END")
00763   return aHypSO._retn();
00764 }
00765 
00766 //=======================================================================
00767 //function : GetMeshOrSubmeshByShape
00768 //purpose  : 
00769 //=======================================================================
00770 
00771 SALOMEDS::SObject_ptr
00772   SMESH_Gen_i::GetMeshOrSubmeshByShape (SALOMEDS::Study_ptr   theStudy,
00773                                         SMESH::SMESH_Mesh_ptr theMesh,
00774                                         GEOM::GEOM_Object_ptr theShape)
00775 {
00776   if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape")
00777   SALOMEDS::SObject_var aMeshOrSubMesh;
00778   if (theMesh->_is_nil() || ( theShape->_is_nil() && theMesh->HasShapeToMesh()))
00779     return aMeshOrSubMesh._retn();
00780   
00781   TopoDS_Shape aShape;
00782   if(theMesh->HasShapeToMesh())
00783     aShape = GeomObjectToShape( theShape );
00784   else
00785     aShape = SMESH_Mesh::PseudoShape();
00786 
00787   SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
00788 
00789   if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
00790     SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
00791     if ( aShape.IsSame( meshDS->ShapeToMesh() ))
00792       aMeshOrSubMesh = ObjectToSObject( theStudy, theMesh );
00793     else {
00794       int shapeID = meshDS->ShapeToIndex( aShape );
00795       SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
00796       if ( !aSubMesh->_is_nil() )
00797         aMeshOrSubMesh = ObjectToSObject( theStudy, aSubMesh );
00798     }
00799   }
00800   if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape--END")
00801   return aMeshOrSubMesh._retn();
00802 }
00803 
00804 //=======================================================================
00805 //function : AddHypothesisToShape
00806 //purpose  : 
00807 //=======================================================================
00808 
00809 bool SMESH_Gen_i::AddHypothesisToShape(SALOMEDS::Study_ptr         theStudy,
00810                                        SMESH::SMESH_Mesh_ptr       theMesh,
00811                                        GEOM::GEOM_Object_ptr       theShape,
00812                                        SMESH::SMESH_Hypothesis_ptr theHyp)
00813 {
00814   if(MYDEBUG) MESSAGE("AddHypothesisToShape")
00815   if (theStudy->_is_nil() || theMesh->_is_nil() ||
00816       theHyp->_is_nil() || (theShape->_is_nil()
00817                             && theMesh->HasShapeToMesh()) )
00818     return false;
00819 
00820   SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
00821   if ( aMeshSO->_is_nil() )
00822     aMeshSO = PublishMesh( theStudy, theMesh );
00823   SALOMEDS::SObject_var aHypSO = PublishHypothesis( theStudy, theHyp );
00824   if ( aMeshSO->_is_nil() || aHypSO->_is_nil())
00825     return false;
00826 
00827   // Find a mesh or submesh refering to theShape
00828   SALOMEDS::SObject_var aMeshOrSubMesh =
00829     GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
00830   if ( aMeshOrSubMesh->_is_nil() )
00831   {
00832     // publish submesh
00833     TopoDS_Shape aShape = GeomObjectToShape( theShape );
00834     SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
00835     if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
00836       SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
00837       int shapeID = meshDS->ShapeToIndex( aShape );
00838       SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
00839       aMeshOrSubMesh = PublishSubMesh( theStudy, theMesh, aSubMesh, theShape );
00840     }
00841     if ( aMeshOrSubMesh->_is_nil() )
00842       return false;
00843   }
00844 
00845   //Find or Create Applied Hypothesis root
00846   bool aIsAlgo = !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil();
00847   SALOMEDS::SObject_var AHR =
00848     publish (theStudy, CORBA::Object::_nil(), aMeshOrSubMesh,
00849              aIsAlgo ? GetRefOnAppliedAlgorithmsTag() : GetRefOnAppliedHypothesisTag(),
00850              aIsAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
00851   SetName( AHR, aIsAlgo ? "Applied algorithms" : "Applied hypotheses" );
00852   if ( AHR->_is_nil() )
00853     return false;
00854 
00855   addReference( theStudy, AHR, theHyp );
00856   if(MYDEBUG) MESSAGE("AddHypothesisToShape--END")
00857   return true;
00858 }
00859 
00860 //=======================================================================
00861 //function : RemoveHypothesisFromShape
00862 //purpose  : 
00863 //=======================================================================
00864 
00865 bool SMESH_Gen_i::RemoveHypothesisFromShape(SALOMEDS::Study_ptr         theStudy,
00866                                             SMESH::SMESH_Mesh_ptr       theMesh,
00867                                             GEOM::GEOM_Object_ptr       theShape,
00868                                             SMESH::SMESH_Hypothesis_ptr theHyp)
00869 {
00870   if (theStudy->_is_nil() || theMesh->_is_nil() ||
00871       theHyp->_is_nil() || (theShape->_is_nil()
00872                             && theMesh->HasShapeToMesh()))
00873     return false;
00874 
00875   SALOMEDS::SObject_var aHypSO = ObjectToSObject( theStudy, theHyp );
00876   if ( aHypSO->_is_nil() )
00877     return false;
00878 
00879   // Find a mesh or submesh refering to theShape
00880   SALOMEDS::SObject_var aMeshOrSubMesh =
00881     GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
00882   if ( aMeshOrSubMesh->_is_nil() )
00883     return false;
00884 
00885   // Find and remove a reference to aHypSO
00886   SALOMEDS::SObject_var aRef, anObj;
00887   CORBA::String_var     anID = CORBA::string_dup( aHypSO->GetID() );
00888   SALOMEDS::ChildIterator_var it = theStudy->NewChildIterator( aMeshOrSubMesh );
00889   for ( it->InitEx( true ); it->More(); it->Next() ) {
00890     anObj = it->Value();
00891     if (anObj->ReferencedObject( aRef ) && strcmp( aRef->GetID(), anID ) == 0 ) {
00892       theStudy->NewBuilder()->RemoveObject( anObj );
00893       break;
00894     }
00895   }
00896   return true;
00897 }
00898 
00899 //=======================================================================
00900 //function : UpdateParameters
00901 //purpose  : 
00902 //=======================================================================
00903 void SMESH_Gen_i::UpdateParameters(CORBA::Object_ptr theObject, const char* theParameters)
00904 {
00905 
00906   if(VARIABLE_DEBUG)
00907     cout<<"UpdateParameters : "<<theParameters<<endl;
00908   SALOMEDS::Study_ptr aStudy = GetCurrentStudy();
00909   if(aStudy->_is_nil() || CORBA::is_nil(theObject)) 
00910     return;
00911 
00912   SALOMEDS::SObject_var aSObj =  ObjectToSObject(aStudy,theObject);
00913   if(aSObj->_is_nil())  
00914     return;
00915 
00916   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
00917 
00918   SALOMEDS::GenericAttribute_var aFindAttr;
00919   bool hasAttr = aSObj->FindAttribute(aFindAttr, "AttributeString");
00920   if(VARIABLE_DEBUG)
00921     cout<<"Find Attribute "<<hasAttr<<endl;
00922 
00923   SALOMEDS::GenericAttribute_var anAttr;
00924   anAttr = aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString");
00925   SALOMEDS::AttributeString_var aStringAttr = SALOMEDS::AttributeString::_narrow(anAttr);
00926 
00927   CORBA::String_var oldparVar = aStringAttr->Value();
00928   CORBA::String_var inpparVar = ParseParameters(theParameters);
00929   TCollection_AsciiString aNewParams;
00930   TCollection_AsciiString aOldParameters(oldparVar.inout());
00931   TCollection_AsciiString anInputParams(inpparVar.inout());
00932   if(!hasAttr)
00933     aNewParams = anInputParams;
00934   else 
00935     {
00936       int pos = aOldParameters.SearchFromEnd("|");
00937       if(pos==-1) pos = 0;
00938       TCollection_AsciiString previousParamFull(aOldParameters.Split(pos));
00939       TCollection_AsciiString previousParam(previousParamFull);
00940       TCollection_AsciiString theRepet("1");
00941       pos = previousParam.SearchFromEnd(";*=");
00942       if(pos >= 0)
00943         {
00944           theRepet = previousParam.Split(pos+2);
00945           pos = pos-1;
00946           if(pos==-1) pos = 0;
00947           previousParam.Split(pos);
00948         }
00949       if(previousParam == anInputParams)
00950         {
00951           theRepet = theRepet.IntegerValue()+1;
00952           aNewParams = aOldParameters + previousParam + ";*=" + theRepet;
00953         }
00954       else
00955         {
00956           aNewParams = aOldParameters + previousParamFull + "|" + anInputParams;
00957         }
00958     }
00959 
00960   if(VARIABLE_DEBUG)
00961   {
00962     cout<<"Input Parameters : "<<anInputParams<<endl;
00963     cout<<"Old Parameters : "<<aOldParameters<<endl;
00964     cout<<"New Parameters : "<<aNewParams<<endl;
00965   }
00966 
00967   aStringAttr->SetValue( aNewParams.ToCString() );
00968 }
00969 
00970 //=======================================================================
00971 //function : ParseParameters
00972 //purpose  : 
00973 //=======================================================================
00974 char* SMESH_Gen_i::ParseParameters(const char* theParameters)
00975 {
00976   //const char* aParameters = theParameters;
00977 //   const char* aParameters = CORBA::string_dup(theParameters);
00978   TCollection_AsciiString anInputParams;
00979   SALOMEDS::Study_var aStudy = GetCurrentStudy();
00980   if( !aStudy->_is_nil() ) {
00981 //     SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(theParameters);
00982 //     for(int j=0;j<aSections->length();j++) {
00983 //       SALOMEDS::ListOfStrings aVars= aSections[j];
00984 //       for(int i=0;i<aVars.length();i++ ) {
00985 //         anInputParams += aStudy->IsVariable(aVars[i].in()) ? 
00986 //           TCollection_AsciiString(aVars[i].in()) : TCollection_AsciiString("");
00987 //         if(i != aVars.length()-1)
00988 //           anInputParams+=":";
00989 //       }
00990 //       if(j!=aSections->length()-1)
00991 //         anInputParams+="|";
00992 //     }
00993     TCollection_AsciiString paramStr( theParameters );
00994     static TCollection_AsciiString separators(":|");
00995     int beg = 0, end;
00996     char sep, *pParams = (char*)paramStr.ToCString();
00997     while ( beg < paramStr.Length() )
00998     {
00999       end = beg-1;
01000       while ( ++end < paramStr.Length() )
01001         if ( pParams[end] == ':' || pParams[end] == '|')
01002           break;
01003       if ( end < paramStr.Length())
01004       {
01005         sep = pParams[end];
01006         pParams[end] = '\0';
01007       }
01008       if ( aStudy->IsVariable( pParams+beg ))
01009         anInputParams += pParams+beg;
01010       if ( end < paramStr.Length() )
01011         anInputParams += sep;
01012       else
01013         break;
01014       beg = end + 1;
01015     }
01016   }
01017   return CORBA::string_dup(anInputParams.ToCString());
01018 }
01019 
01020 //=======================================================================
01021 //function : GetParameters
01022 //purpose  : 
01023 //=======================================================================
01024 char* SMESH_Gen_i::GetParameters(CORBA::Object_ptr theObject)
01025 {
01026   TCollection_AsciiString aResult;
01027 
01028   SALOMEDS::Study_ptr aStudy = GetCurrentStudy();
01029   SALOMEDS::SObject_var aSObj =  ObjectToSObject(aStudy,theObject);
01030 
01031   if(!aStudy->_is_nil() && 
01032      !CORBA::is_nil(theObject) && 
01033      !aSObj->_is_nil()){
01034     
01035     SALOMEDS::GenericAttribute_var anAttr;
01036     if ( aSObj->FindAttribute(anAttr, "AttributeString")) {
01037       aResult = TCollection_AsciiString(SALOMEDS::AttributeString::_narrow(anAttr)->Value());
01038     }
01039   }
01040   
01041   return CORBA::string_dup( aResult.ToCString() );
01042 }
Copyright © 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
Copyright © 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS