1 // SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 // File : SMESH_Gen_i.cxx
25 // Author : Paul RASCLE, EDF
30 #include <TopExp_Explorer.hxx>
32 #include <TopoDS_Iterator.hxx>
33 #include <TopoDS_Compound.hxx>
34 #include <TopoDS_CompSolid.hxx>
35 #include <TopoDS_Solid.hxx>
36 #include <TopoDS_Shell.hxx>
37 #include <TopoDS_Face.hxx>
38 #include <TopoDS_Wire.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Vertex.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <TopTools_MapOfShape.hxx>
43 #include <TopTools_IndexedMapOfShape.hxx>
44 #include <TopTools_ListOfShape.hxx>
45 #include <TopTools_ListIteratorOfListOfShape.hxx>
47 #include <BRep_Tool.hxx>
48 #include <TCollection_AsciiString.hxx>
51 #include "Utils_CorbaException.hxx"
53 #include "utilities.h"
60 #include "SMESH_Gen_i.hxx"
61 #include "SMESH_Mesh_i.hxx"
62 #include "SMESH_Hypothesis_i.hxx"
63 #include "SMESH_Algo_i.hxx"
64 #include "SMESH_Group_i.hxx"
65 #include "SMESH_PythonDump.hxx"
67 #include "SMESHDS_Document.hxx"
68 #include "SMESHDS_Group.hxx"
69 #include "SMESHDS_GroupOnGeom.hxx"
70 #include "SMESH_Mesh.hxx"
71 #include "SMESH_Hypothesis.hxx"
72 #include "SMESH_Group.hxx"
73 #include "SMESH_MeshEditor.hxx"
75 #include "SMDS_EdgePosition.hxx"
76 #include "SMDS_FacePosition.hxx"
77 #include "SMDS_VertexPosition.hxx"
78 #include "SMDS_SpacePosition.hxx"
79 #include "SMDS_PolyhedralVolumeOfNodes.hxx"
81 #include CORBA_SERVER_HEADER(SMESH_Group)
82 #include CORBA_SERVER_HEADER(SMESH_Filter)
84 #include "DriverMED_W_SMESHDS_Mesh.h"
85 #include "DriverMED_R_SMESHDS_Mesh.h"
87 #include "SALOMEDS_Tool.hxx"
88 #include "SALOME_NamingService.hxx"
89 #include "SALOME_LifeCycleCORBA.hxx"
90 #include "Utils_SINGLETON.hxx"
93 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
95 #include "GEOM_Client.hxx"
96 #include "Utils_ExceptHandlers.hxx"
101 using SMESH::TPythonDump;
103 #define NUM_TMP_FILES 2
106 static int MYDEBUG = 0;
108 static int MYDEBUG = 0;
111 // Static variables definition
112 GEOM::GEOM_Gen_var SMESH_Gen_i::myGeomGen = GEOM::GEOM_Gen::_nil();
113 CORBA::ORB_var SMESH_Gen_i::myOrb;
114 PortableServer::POA_var SMESH_Gen_i::myPoa;
115 SALOME_NamingService* SMESH_Gen_i::myNS = NULL;
116 SALOME_LifeCycleCORBA* SMESH_Gen_i::myLCC = NULL;
117 SMESH_Gen_i* SMESH_Gen_i::mySMESHGen = NULL;
119 //=============================================================================
121 * GetServant [ static ]
123 * Get servant of the CORBA object
125 //=============================================================================
127 PortableServer::ServantBase_var SMESH_Gen_i::GetServant( CORBA::Object_ptr theObject )
129 if( CORBA::is_nil( theObject ) || CORBA::is_nil( GetPOA() ) )
132 PortableServer::Servant aServant = GetPOA()->reference_to_servant( theObject );
136 INFOS( "GetServant - Unknown exception was caught!!!" );
141 //=============================================================================
143 * SObjectToObject [ static ]
145 * Get CORBA object corresponding to the SALOMEDS::SObject
147 //=============================================================================
149 CORBA::Object_var SMESH_Gen_i::SObjectToObject( SALOMEDS::SObject_ptr theSObject )
151 SALOMEDS::GenericAttribute_var anAttr;
152 CORBA::Object_var anObj;
153 if ( !theSObject->_is_nil() ) {
155 if( theSObject->FindAttribute( anAttr, "AttributeIOR" ) ) {
156 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow( anAttr );
157 CORBA::String_var aValue = anIOR->Value();
158 if( strcmp( aValue, "" ) != 0 )
159 anObj = GetORB()->string_to_object( aValue );
163 INFOS( "SObjectToObject - Unknown exception was caught!!!" );
169 //=============================================================================
173 * Get SALOME_NamingService object
175 //=============================================================================
177 SALOME_NamingService* SMESH_Gen_i::GetNS()
179 if ( myNS == NULL ) {
180 myNS = SINGLETON_<SALOME_NamingService>::Instance();
181 ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting());
182 myNS->init_orb( GetORB() );
187 //=============================================================================
191 * Get SALOME_LifeCycleCORBA object
193 //=============================================================================
194 SALOME_LifeCycleCORBA* SMESH_Gen_i::GetLCC() {
195 if ( myLCC == NULL ) {
196 myLCC = new SALOME_LifeCycleCORBA( GetNS() );
202 //=============================================================================
204 * GetGeomEngine [ static ]
206 * Get GEOM::GEOM_Gen reference
208 //=============================================================================
209 GEOM::GEOM_Gen_ptr SMESH_Gen_i::GetGeomEngine() {
210 //CCRT GEOM::GEOM_Gen_var aGeomEngine =
211 //CCRT GEOM::GEOM_Gen::_narrow( GetLCC()->FindOrLoad_Component("FactoryServer","GEOM") );
212 //CCRT return aGeomEngine._retn();
213 if(CORBA::is_nil(myGeomGen))
215 Engines::Component_ptr temp=GetLCC()->FindOrLoad_Component("FactoryServer","GEOM");
216 myGeomGen=GEOM::GEOM_Gen::_narrow(temp);
221 //=============================================================================
223 * SMESH_Gen_i::SMESH_Gen_i
225 * Default constructor: not for use
227 //=============================================================================
229 SMESH_Gen_i::SMESH_Gen_i()
231 INFOS( "SMESH_Gen_i::SMESH_Gen_i : default constructor" );
234 //=============================================================================
236 * SMESH_Gen_i::SMESH_Gen_i
238 * Standard constructor, used with Container
240 //=============================================================================
242 SMESH_Gen_i::SMESH_Gen_i( CORBA::ORB_ptr orb,
243 PortableServer::POA_ptr poa,
244 PortableServer::ObjectId* contId,
245 const char* instanceName,
246 const char* interfaceName )
247 : Engines_Component_i( orb, poa, contId, instanceName, interfaceName )
249 MESSAGE( "SMESH_Gen_i::SMESH_Gen_i : standard constructor" );
251 myOrb = CORBA::ORB::_duplicate(orb);
252 myPoa = PortableServer::POA::_duplicate(poa);
255 _id = myPoa->activate_object( _thisObj );
257 myIsEmbeddedMode = false;
258 myShapeReader = NULL; // shape reader
261 // set it in standalone mode only
262 //OSD::SetSignal( true );
265 //=============================================================================
267 * SMESH_Gen_i::~SMESH_Gen_i
271 //=============================================================================
273 SMESH_Gen_i::~SMESH_Gen_i()
275 INFOS( "SMESH_Gen_i::~SMESH_Gen_i" );
277 // delete hypothesis creators
278 map<string, GenericHypothesisCreator_i*>::iterator itHyp;
279 for (itHyp = myHypCreatorMap.begin(); itHyp != myHypCreatorMap.end(); itHyp++)
281 delete (*itHyp).second;
283 myHypCreatorMap.clear();
285 // Clear study contexts data
286 map<int, StudyContext*>::iterator it;
287 for ( it = myStudyContextMap.begin(); it != myStudyContextMap.end(); ++it ) {
290 myStudyContextMap.clear();
291 // delete shape reader
292 if ( !myShapeReader )
293 delete myShapeReader;
296 //=============================================================================
298 * SMESH_Gen_i::createHypothesis
300 * Create hypothesis of given type
302 //=============================================================================
303 SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::createHypothesis(const char* theHypName,
304 const char* theLibName)
305 throw (SALOME::SALOME_Exception)
307 Unexpect aCatch(SALOME_SalomeException);
308 if(MYDEBUG) MESSAGE( "Create Hypothesis <" << theHypName << "> from " << theLibName);
310 // create a new hypothesis object servant
311 SMESH_Hypothesis_i* myHypothesis_i = 0;
312 SMESH::SMESH_Hypothesis_var hypothesis_i;
316 // check, if creator for this hypothesis type already exists
317 if (myHypCreatorMap.find(string(theHypName)) == myHypCreatorMap.end())
319 // load plugin library
320 if(MYDEBUG) MESSAGE("Loading server meshers plugin library ...");
321 void* libHandle = dlopen (theLibName, RTLD_LAZY);
324 // report any error, if occured
325 const char* anError = dlerror();
326 throw(SALOME_Exception(anError));
329 // get method, returning hypothesis creator
330 if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
331 typedef GenericHypothesisCreator_i* (*GetHypothesisCreator)(const char* theHypName);
332 GetHypothesisCreator procHandle =
333 (GetHypothesisCreator)dlsym( libHandle, "GetHypothesisCreator" );
336 throw(SALOME_Exception(LOCALIZED("bad hypothesis plugin library")));
340 // get hypothesis creator
341 if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << theHypName);
342 GenericHypothesisCreator_i* aCreator = procHandle(theHypName);
345 throw(SALOME_Exception(LOCALIZED("no such a hypothesis in this plugin")));
348 // map hypothesis creator to a hypothesis name
349 myHypCreatorMap[string(theHypName)] = aCreator;
352 // create a new hypothesis object, store its ref. in studyContext
353 if(MYDEBUG) MESSAGE("Create Hypothesis " << theHypName);
355 myHypCreatorMap[string(theHypName)]->Create (myPoa, GetCurrentStudyID(), &myGen);
356 // _CS_gbo Explicit activation (no longer made in the constructor).
357 myHypothesis_i->Activate();
358 myHypothesis_i->SetLibName(theLibName); // for persistency assurance
360 catch (SALOME_Exception& S_ex)
362 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
366 return hypothesis_i._retn();
368 // activate the CORBA servant of hypothesis
369 hypothesis_i = SMESH::SMESH_Hypothesis::_narrow( myHypothesis_i->_this() );
370 int nextId = RegisterObject( hypothesis_i );
371 if(MYDEBUG) MESSAGE( "Add hypo to map with id = "<< nextId );
373 return hypothesis_i._retn();
376 //=============================================================================
378 * SMESH_Gen_i::createMesh
380 * Create empty mesh on shape
382 //=============================================================================
383 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::createMesh()
384 throw ( SALOME::SALOME_Exception )
386 Unexpect aCatch(SALOME_SalomeException);
387 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::createMesh" );
389 // Get or create the GEOM_Client instance
391 // create a new mesh object servant, store it in a map in study context
392 SMESH_Mesh_i* meshServant = new SMESH_Mesh_i( GetPOA(), this, GetCurrentStudyID() );
393 // create a new mesh object
394 meshServant->SetImpl( myGen.CreateMesh( GetCurrentStudyID(), myIsEmbeddedMode ));
396 // activate the CORBA servant of Mesh
397 SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( meshServant->_this() );
398 int nextId = RegisterObject( mesh );
399 if(MYDEBUG) MESSAGE( "Add mesh to map with id = "<< nextId);
402 catch (SALOME_Exception& S_ex) {
403 THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
405 return SMESH::SMESH_Mesh::_nil();
408 //=============================================================================
410 * SMESH_Gen_i::GetShapeReader
414 //=============================================================================
415 GEOM_Client* SMESH_Gen_i::GetShapeReader()
417 // create shape reader if necessary
418 if ( !myShapeReader )
419 myShapeReader = new GEOM_Client(GetContainerRef());
420 ASSERT( myShapeReader );
421 return myShapeReader;
424 //=============================================================================
426 * SMESH_Gen_i::SetGeomEngine
428 * Set GEOM::GEOM_Gen reference
430 //=============================================================================
431 GEOM::GEOM_Gen_ptr SMESH_Gen_i::SetGeomEngine( const char* containerLoc )
433 Engines::Component_ptr temp=GetLCC()->FindOrLoad_Component(containerLoc,"GEOM");
434 myGeomGen=GEOM::GEOM_Gen::_narrow(temp);
438 //=============================================================================
440 * SMESH_Gen_i::SetEmbeddedMode
444 //=============================================================================
446 void SMESH_Gen_i::SetEmbeddedMode( CORBA::Boolean theMode )
448 myIsEmbeddedMode = theMode;
450 if ( !myIsEmbeddedMode ) {
451 //PAL10867: disable signals catching with "noexcepthandler" option
452 char* envNoCatchSignals = getenv("NOT_INTERCEPT_SIGNALS");
453 if (!envNoCatchSignals || !atoi(envNoCatchSignals))
458 char* envDisableFPE = getenv("DISABLE_FPE");
459 if (envDisableFPE && atoi(envDisableFPE))
464 OSD::SetSignal( raiseFPE );
466 // else OSD::SetSignal() is called in GUI
470 //=============================================================================
472 * SMESH_Gen_i::IsEmbeddedMode
476 //=============================================================================
478 CORBA::Boolean SMESH_Gen_i::IsEmbeddedMode()
480 return myIsEmbeddedMode;
483 //=============================================================================
485 * SMESH_Gen_i::SetCurrentStudy
489 //=============================================================================
491 void SMESH_Gen_i::SetCurrentStudy( SALOMEDS::Study_ptr theStudy )
494 //MESSAGE( "SMESH_Gen_i::SetCurrentStudy" );
495 myCurrentStudy = SALOMEDS::Study::_duplicate( theStudy );
496 // create study context, if it doesn't exist and set current study
497 int studyId = GetCurrentStudyID();
498 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::SetCurrentStudy: study Id = " << studyId );
499 if ( myStudyContextMap.find( studyId ) == myStudyContextMap.end() ) {
500 myStudyContextMap[ studyId ] = new StudyContext;
503 // myCurrentStudy may be nil
504 if ( !CORBA::is_nil( myCurrentStudy ) ) {
505 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
506 if( !myCurrentStudy->FindComponent( "GEOM" )->_is_nil() )
507 aStudyBuilder->LoadWith( myCurrentStudy->FindComponent( "GEOM" ), GetGeomEngine() );
509 // set current study for geom engine
510 //if ( !CORBA::is_nil( GetGeomEngine() ) )
511 // GetGeomEngine()->GetCurrentStudy( myCurrentStudy->StudyId() );
515 //=============================================================================
517 * SMESH_Gen_i::GetCurrentStudy
521 //=============================================================================
523 SALOMEDS::Study_ptr SMESH_Gen_i::GetCurrentStudy()
525 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetCurrentStudy: study Id = " << GetCurrentStudyID() );
526 return SALOMEDS::Study::_duplicate( myCurrentStudy );
529 //=============================================================================
531 * SMESH_Gen_i::GetCurrentStudyContext
533 * Get current study context
535 //=============================================================================
536 StudyContext* SMESH_Gen_i::GetCurrentStudyContext()
538 if ( !CORBA::is_nil( myCurrentStudy ) &&
539 myStudyContextMap.find( GetCurrentStudyID() ) != myStudyContextMap.end() )
540 return myStudyContextMap[ myCurrentStudy->StudyId() ];
545 //=============================================================================
547 * SMESH_Gen_i::CreateHypothesis
549 * Create hypothesis/algorothm of given type and publish it in the study
551 //=============================================================================
553 SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::CreateHypothesis( const char* theHypName,
554 const char* theLibName )
555 throw ( SALOME::SALOME_Exception )
557 Unexpect aCatch(SALOME_SalomeException);
558 // Create hypothesis/algorithm
559 SMESH::SMESH_Hypothesis_var hyp = this->createHypothesis( theHypName, theLibName );
561 // Publish hypothesis/algorithm in the study
562 if ( CanPublishInStudy( hyp ) ) {
563 SALOMEDS::SObject_var aSO = PublishHypothesis( myCurrentStudy, hyp );
564 if ( !aSO->_is_nil() ) {
565 // Update Python script
566 TPythonDump() << aSO << " = " << this << ".CreateHypothesis('"
567 << theHypName << "', '" << theLibName << "')";
574 //================================================================================
576 * \brief Return hypothesis of given type holding parameter values of the existing mesh
577 * \param theHypType - hypothesis type name
578 * \param theLibName - plugin library name
579 * \param theMesh - The mesh of interest
580 * \param theGeom - The shape to get parameter values from
581 * \retval SMESH::SMESH_Hypothesis_ptr - The returned hypothesis may be the one existing
582 * in a study and used to compute the mesh, or a temporary one created just to pass
585 //================================================================================
587 SMESH::SMESH_Hypothesis_ptr
588 SMESH_Gen_i::GetHypothesisParameterValues (const char* theHypType,
589 const char* theLibName,
590 SMESH::SMESH_Mesh_ptr theMesh,
591 GEOM::GEOM_Object_ptr theGeom)
592 throw ( SALOME::SALOME_Exception )
594 Unexpect aCatch(SALOME_SalomeException);
595 if ( CORBA::is_nil( theMesh ) )
596 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference", SALOME::BAD_PARAM );
597 if ( CORBA::is_nil( theGeom ) )
598 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
600 // -----------------------------------------------
601 // find hypothesis used to mesh theGeom
602 // -----------------------------------------------
604 // get mesh and shape
605 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
606 TopoDS_Shape shape = GeomObjectToShape( theGeom );
607 if ( !meshServant || shape.IsNull() )
608 return SMESH::SMESH_Hypothesis::_nil();
609 ::SMESH_Mesh& mesh = meshServant->GetImpl();
611 if ( mesh.NbNodes() == 0 ) // empty mesh
612 return SMESH::SMESH_Hypothesis::_nil();
614 // create a temporary hypothesis to know its dimention
615 SMESH::SMESH_Hypothesis_var tmpHyp = this->createHypothesis( theHypType, theLibName );
616 SMESH_Hypothesis_i* hypServant = SMESH::DownCast<SMESH_Hypothesis_i*>( tmpHyp );
618 return SMESH::SMESH_Hypothesis::_nil();
619 ::SMESH_Hypothesis* hyp = hypServant->GetImpl();
621 // look for a hypothesis of theHypType used to mesh the shape
622 if ( myGen.GetShapeDim( shape ) == hyp->GetDim() )
625 SMESH::ListOfHypothesis_var aHypList = theMesh->GetHypothesisList( theGeom );
626 int nbLocalHyps = aHypList->length();
627 for ( int i = 0; i < nbLocalHyps; i++ )
628 if ( strcmp( theHypType, aHypList[i]->GetName() ) == 0 ) // FOUND local!
629 return SMESH::SMESH_Hypothesis::_duplicate( aHypList[i] );
630 // check super shapes
631 TopTools_ListIteratorOfListOfShape itShape( mesh.GetAncestors( shape ));
632 while ( nbLocalHyps == 0 && itShape.More() ) {
633 GEOM::GEOM_Object_ptr geomObj = ShapeToGeomObject( itShape.Value() );
634 if ( ! CORBA::is_nil( geomObj )) {
635 SMESH::ListOfHypothesis_var aHypList = theMesh->GetHypothesisList( geomObj );
636 nbLocalHyps = aHypList->length();
637 for ( int i = 0; i < nbLocalHyps; i++ )
638 if ( strcmp( theHypType, aHypList[i]->GetName() ) == 0 ) // FOUND global!
639 return SMESH::SMESH_Hypothesis::_duplicate( aHypList[i] );
645 // let the temporary hypothesis find out some how parameter values
646 if ( hyp->SetParametersByMesh( &mesh, shape ))
647 return SMESH::SMESH_Hypothesis::_duplicate( tmpHyp );
649 return SMESH::SMESH_Hypothesis::_nil();
652 //=============================================================================
654 * SMESH_Gen_i::CreateMesh
656 * Create empty mesh on a shape and publish it in the study
658 //=============================================================================
660 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMesh( GEOM::GEOM_Object_ptr theShapeObject )
661 throw ( SALOME::SALOME_Exception )
663 Unexpect aCatch(SALOME_SalomeException);
664 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMesh" );
666 SMESH::SMESH_Mesh_var mesh = this->createMesh();
668 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( mesh );
669 ASSERT( meshServant );
670 meshServant->SetShape( theShapeObject );
672 // publish mesh in the study
673 if ( CanPublishInStudy( mesh ) ) {
674 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
675 aStudyBuilder->NewCommand(); // There is a transaction
676 SALOMEDS::SObject_var aSO = PublishMesh( myCurrentStudy, mesh.in() );
677 aStudyBuilder->CommitCommand();
678 if ( !aSO->_is_nil() ) {
679 // Update Python script
680 TPythonDump() << aSO << " = " << this << ".CreateMesh(" << theShapeObject << ")";
687 //=============================================================================
689 * SMESH_Gen_i::CreateEmptyMesh
693 //=============================================================================
695 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateEmptyMesh()
696 throw ( SALOME::SALOME_Exception )
698 Unexpect aCatch(SALOME_SalomeException);
699 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMesh" );
701 SMESH::SMESH_Mesh_var mesh = this->createMesh();
703 // publish mesh in the study
704 if ( CanPublishInStudy( mesh ) ) {
705 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
706 aStudyBuilder->NewCommand(); // There is a transaction
707 SALOMEDS::SObject_var aSO = PublishMesh( myCurrentStudy, mesh.in() );
708 aStudyBuilder->CommitCommand();
709 if ( !aSO->_is_nil() ) {
710 // Update Python script
711 TPythonDump() << aSO << " = " << this << ".CreateEmptyMesh()";
718 //=============================================================================
720 * SMESH_Gen_i::CreateMeshFromUNV
722 * Create mesh and import data from UNV file
724 //=============================================================================
726 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMeshesFromUNV( const char* theFileName )
727 throw ( SALOME::SALOME_Exception )
729 Unexpect aCatch(SALOME_SalomeException);
730 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMeshesFromUNV" );
732 SMESH::SMESH_Mesh_var aMesh = createMesh();
734 // publish mesh in the study
735 if ( CanPublishInStudy( aMesh ) ) {
736 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
737 aStudyBuilder->NewCommand(); // There is a transaction
738 SALOMEDS::SObject_var aSO = PublishMesh( myCurrentStudy, aMesh.in(), aFileName.c_str() );
739 aStudyBuilder->CommitCommand();
740 if ( !aSO->_is_nil() ) {
741 // Update Python script
742 TPythonDump() << aSO << " = smeshgen.CreateMeshesFromUNV('" << theFileName << "')";
746 SMESH_Mesh_i* aServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( aMesh ).in() );
748 aServant->ImportUNVFile( theFileName );
750 // Dump creation of groups
751 aServant->GetGroups();
753 return aMesh._retn();
756 //=============================================================================
758 * SMESH_Gen_i::CreateMeshFromMED
760 * Create mesh and import data from MED file
762 //=============================================================================
764 SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromMED( const char* theFileName,
765 SMESH::DriverMED_ReadStatus& theStatus)
766 throw ( SALOME::SALOME_Exception )
768 Unexpect aCatch(SALOME_SalomeException);
769 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMeshFromMED" );
771 // Retrieve mesh names from the file
772 DriverMED_R_SMESHDS_Mesh myReader;
773 myReader.SetFile( theFileName );
774 myReader.SetMeshId( -1 );
775 Driver_Mesh::Status aStatus;
776 list<string> aNames = myReader.GetMeshNames(aStatus);
777 SMESH::mesh_array_var aResult = new SMESH::mesh_array();
778 theStatus = (SMESH::DriverMED_ReadStatus)aStatus;
780 { // open a new scope to make aPythonDump die before PythonDump in SMESH_Mesh::GetGroups()
783 TPythonDump aPythonDump;
785 //TCollection_AsciiString aStr ("([");
787 if (theStatus == SMESH::DRS_OK) {
788 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
789 aStudyBuilder->NewCommand(); // There is a transaction
790 aResult->length( aNames.size() );
793 // Iterate through all meshes and create mesh objects
794 for ( list<string>::iterator it = aNames.begin(); it != aNames.end(); it++ ) {
796 //if (i > 0) aStr += ", ";
797 if (i > 0) aPythonDump << ", ";
800 SMESH::SMESH_Mesh_var mesh = createMesh();
802 // publish mesh in the study
803 SALOMEDS::SObject_var aSO;
804 if ( CanPublishInStudy( mesh ) )
805 aSO = PublishMesh( myCurrentStudy, mesh.in(), (*it).c_str() );
806 if ( !aSO->_is_nil() ) {
809 //aStr += aSO->GetID();
812 aPythonDump << "mesh_" << i;
814 // aStr += TCollection_AsciiString(i);
817 // Read mesh data (groups are published automatically by ImportMEDFile())
818 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
819 ASSERT( meshServant );
820 SMESH::DriverMED_ReadStatus status1 =
821 meshServant->ImportMEDFile( theFileName, (*it).c_str() );
822 if (status1 > theStatus)
825 aResult[i++] = SMESH::SMESH_Mesh::_duplicate( mesh );
827 aStudyBuilder->CommitCommand();
830 // Update Python script
831 aPythonDump << "], status) = " << this << ".CreateMeshesFromMED('" << theFileName << "')";
833 // Dump creation of groups
834 for ( int i = 0; i < aResult->length(); ++i )
835 aResult[ i ]->GetGroups();
837 return aResult._retn();
840 //=============================================================================
842 * SMESH_Gen_i::CreateMeshFromSTL
844 * Create mesh and import data from STL file
846 //=============================================================================
848 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMeshesFromSTL( const char* theFileName )
849 throw ( SALOME::SALOME_Exception )
851 Unexpect aCatch(SALOME_SalomeException);
852 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMeshesFromSTL" );
854 SMESH::SMESH_Mesh_var aMesh = createMesh();
856 // publish mesh in the study
857 if ( CanPublishInStudy( aMesh ) ) {
858 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
859 aStudyBuilder->NewCommand(); // There is a transaction
860 SALOMEDS::SObject_var aSO = PublishInStudy
861 ( myCurrentStudy, SALOMEDS::SObject::_nil(), aMesh.in(), aFileName.c_str() );
862 aStudyBuilder->CommitCommand();
863 if ( !aSO->_is_nil() ) {
864 // Update Python script
865 TPythonDump() << aSO << " = " << this << ".CreateMeshesFromSTL('" << theFileName << "')";
869 SMESH_Mesh_i* aServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( aMesh ).in() );
871 aServant->ImportSTLFile( theFileName );
872 return aMesh._retn();
875 //=============================================================================
877 * SMESH_Gen_i::IsReadyToCompute
879 * Returns true if mesh contains enough data to be computed
881 //=============================================================================
883 CORBA::Boolean SMESH_Gen_i::IsReadyToCompute( SMESH::SMESH_Mesh_ptr theMesh,
884 GEOM::GEOM_Object_ptr theShapeObject )
885 throw ( SALOME::SALOME_Exception )
887 Unexpect aCatch(SALOME_SalomeException);
888 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::IsReadyToCompute" );
890 if ( CORBA::is_nil( theShapeObject ) )
891 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
894 if ( CORBA::is_nil( theMesh ) )
895 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
900 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
901 ASSERT( meshServant );
903 // get local TopoDS_Shape
904 TopoDS_Shape myLocShape = GeomObjectToShape( theShapeObject );
905 // call implementation
906 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
907 return myGen.CheckAlgoState( myLocMesh, myLocShape );
910 catch ( SALOME_Exception& S_ex ) {
911 INFOS( "catch exception "<< S_ex.what() );
916 //================================================================================
918 * \brief Find SObject for an algo
920 //================================================================================
922 SALOMEDS::SObject_ptr SMESH_Gen_i::GetAlgoSO(const ::SMESH_Algo* algo)
925 if ( !myCurrentStudy->_is_nil() ) {
926 // find algo in the study
927 SALOMEDS::SComponent_var father = SALOMEDS::SComponent::_narrow
928 ( myCurrentStudy->FindComponent( ComponentDataType() ) );
929 if ( !father->_is_nil() ) {
930 SALOMEDS::ChildIterator_var itBig = myCurrentStudy->NewChildIterator( father );
931 for ( ; itBig->More(); itBig->Next() ) {
932 SALOMEDS::SObject_var gotBranch = itBig->Value();
933 if ( gotBranch->Tag() == GetAlgorithmsRootTag() ) {
934 SALOMEDS::ChildIterator_var algoIt = myCurrentStudy->NewChildIterator( gotBranch );
935 for ( ; algoIt->More(); algoIt->Next() ) {
936 SALOMEDS::SObject_var algoSO = algoIt->Value();
937 CORBA::Object_var algoIOR = SObjectToObject( algoSO );
938 if ( !CORBA::is_nil( algoIOR )) {
939 SMESH_Hypothesis_i* impl = SMESH::DownCast<SMESH_Hypothesis_i*>( algoIOR );
940 if ( impl && impl->GetImpl() == algo )
941 return algoSO._retn();
943 } // loop on algo SO's
946 } // SMESH component iterator
950 return SALOMEDS::SObject::_nil();
953 //================================================================================
955 * \brief Return errors of mesh computation
957 //================================================================================
959 SMESH::compute_error_array* SMESH_Gen_i::GetComputeErrors( SMESH::SMESH_Mesh_ptr theMesh,
960 GEOM::GEOM_Object_ptr theSubObject )
961 throw ( SALOME::SALOME_Exception )
963 Unexpect aCatch(SALOME_SalomeException);
964 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetComputeErrors()" );
966 if ( CORBA::is_nil( theSubObject ) )
967 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
969 if ( CORBA::is_nil( theMesh ) )
970 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
972 SMESH::compute_error_array_var error_array = new SMESH::compute_error_array;
974 if ( SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh ))
976 TopoDS_Shape shape = GeomObjectToShape( theSubObject );
977 ::SMESH_Mesh& mesh = meshServant->GetImpl();
979 error_array->length( mesh.GetMeshDS()->MaxShapeIndex() );
982 SMESH_subMesh *sm = mesh.GetSubMesh(shape);
983 const bool includeSelf = true, complexShapeFirst = true;
984 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(includeSelf,
986 while ( smIt->more() )
989 if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
991 SMESH_ComputeErrorPtr error = sm->GetComputeError();
992 if ( error && !error->IsOK() && error->myAlgo )
994 SMESH::ComputeError & errStruct = error_array[ nbErr++ ];
995 errStruct.code = -( error->myName < 0 ? error->myName + 1: error->myName ); // -1 -> 0
996 errStruct.comment = error->myComment.c_str();
997 errStruct.subShapeID = sm->GetId();
998 SALOMEDS::SObject_var algoSO = GetAlgoSO( error->myAlgo );
999 if ( !algoSO->_is_nil() )
1000 errStruct.algoName = algoSO->GetName();
1002 errStruct.algoName = error->myAlgo->GetName();
1005 error_array->length( nbErr );
1008 catch ( SALOME_Exception& S_ex ) {
1009 INFOS( "catch exception "<< S_ex.what() );
1012 return error_array._retn();
1015 //================================================================================
1017 * \brief Returns errors of hypotheses definintion
1018 * \param theMesh - the mesh
1019 * \param theSubObject - the main or sub- shape
1020 * \retval SMESH::algo_error_array* - sequence of errors
1022 //================================================================================
1024 SMESH::algo_error_array* SMESH_Gen_i::GetAlgoState( SMESH::SMESH_Mesh_ptr theMesh,
1025 GEOM::GEOM_Object_ptr theSubObject )
1026 throw ( SALOME::SALOME_Exception )
1028 Unexpect aCatch(SALOME_SalomeException);
1029 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetAlgoState()" );
1031 if ( CORBA::is_nil( theSubObject ) )
1032 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
1034 if ( CORBA::is_nil( theMesh ) )
1035 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1037 SMESH::algo_error_array_var error_array = new SMESH::algo_error_array;
1039 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
1040 ASSERT( meshServant );
1041 if ( meshServant ) {
1042 TopoDS_Shape myLocShape = GeomObjectToShape( theSubObject );
1043 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1044 list< ::SMESH_Gen::TAlgoStateError > error_list;
1045 list< ::SMESH_Gen::TAlgoStateError >::iterator error;
1046 // call ::SMESH_Gen::GetAlgoState()
1047 myGen.GetAlgoState( myLocMesh, myLocShape, error_list );
1048 error_array->length( error_list.size() );
1050 for ( error = error_list.begin(); error != error_list.end(); ++error )
1052 // fill AlgoStateError structure
1053 SMESH::AlgoStateError & errStruct = error_array[ i++ ];
1054 errStruct.state = SMESH_Mesh_i::ConvertHypothesisStatus( error->_name );
1055 errStruct.algoDim = error->_algoDim;
1056 errStruct.isGlobalAlgo = error->_isGlobalAlgo;
1057 errStruct.algoName = "";
1058 SALOMEDS::SObject_var algoSO = GetAlgoSO( error->_algo );
1059 if ( !algoSO->_is_nil() )
1060 errStruct.algoName = algoSO->GetName();
1064 catch ( SALOME_Exception& S_ex ) {
1065 INFOS( "catch exception "<< S_ex.what() );
1067 return error_array._retn();
1070 //=============================================================================
1072 * SMESH_Gen_i::GetSubShapesId
1074 * Get sub-shapes unique ID's list
1076 //=============================================================================
1078 SMESH::long_array* SMESH_Gen_i::GetSubShapesId( GEOM::GEOM_Object_ptr theMainShapeObject,
1079 const SMESH::object_array& theListOfSubShapeObject )
1080 throw ( SALOME::SALOME_Exception )
1082 Unexpect aCatch(SALOME_SalomeException);
1083 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetSubShapesId" );
1085 SMESH::long_array_var shapesId = new SMESH::long_array;
1088 if ( CORBA::is_nil( theMainShapeObject ) )
1089 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1090 SALOME::BAD_PARAM );
1094 TopoDS_Shape myMainShape = GeomObjectToShape(theMainShapeObject);
1095 TopTools_IndexedMapOfShape myIndexToShape;
1096 TopExp::MapShapes(myMainShape,myIndexToShape);
1098 for ( int i = 0; i < theListOfSubShapeObject.length(); i++ )
1100 GEOM::GEOM_Object_var aShapeObject
1101 = GEOM::GEOM_Object::_narrow(theListOfSubShapeObject[i]);
1102 if ( CORBA::is_nil( aShapeObject ) )
1103 THROW_SALOME_CORBA_EXCEPTION ("bad shape object reference", \
1104 SALOME::BAD_PARAM );
1106 TopoDS_Shape locShape = GeomObjectToShape(aShapeObject);
1107 for (TopExp_Explorer exp(locShape,TopAbs_FACE); exp.More(); exp.Next())
1109 const TopoDS_Face& F = TopoDS::Face(exp.Current());
1110 setId.insert(myIndexToShape.FindIndex(F));
1111 if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(F));
1113 for (TopExp_Explorer exp(locShape,TopAbs_EDGE); exp.More(); exp.Next())
1115 const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
1116 setId.insert(myIndexToShape.FindIndex(E));
1117 if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(E));
1119 for (TopExp_Explorer exp(locShape,TopAbs_VERTEX); exp.More(); exp.Next())
1121 const TopoDS_Vertex& V = TopoDS::Vertex(exp.Current());
1122 setId.insert(myIndexToShape.FindIndex(V));
1123 if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(V));
1126 shapesId->length(setId.size());
1127 set<int>::iterator iind;
1129 for (iind = setId.begin(); iind != setId.end(); iind++)
1131 if(MYDEBUG) SCRUTE((*iind));
1132 shapesId[i] = (*iind);
1133 if(MYDEBUG) SCRUTE(shapesId[i]);
1137 catch (SALOME_Exception& S_ex)
1139 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
1142 return shapesId._retn();
1145 //=============================================================================
1147 * SMESH_Gen_i::Compute
1149 * Compute mesh on a shape
1151 //=============================================================================
1153 CORBA::Boolean SMESH_Gen_i::Compute( SMESH::SMESH_Mesh_ptr theMesh,
1154 GEOM::GEOM_Object_ptr theShapeObject )
1155 throw ( SALOME::SALOME_Exception )
1157 Unexpect aCatch(SALOME_SalomeException);
1158 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Compute" );
1160 if ( CORBA::is_nil( theShapeObject ) )
1161 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1162 SALOME::BAD_PARAM );
1164 if ( CORBA::is_nil( theMesh ) )
1165 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
1166 SALOME::BAD_PARAM );
1168 // Update Python script
1169 TPythonDump() << "isDone = " << this << ".Compute( "
1170 << theMesh << ", " << theShapeObject << ")";
1174 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
1175 ASSERT( meshServant );
1176 if ( meshServant ) {
1177 // NPAL16168: "geometrical group edition from a submesh don't modifiy mesh computation"
1178 meshServant->CheckGeomGroupModif();
1179 // get local TopoDS_Shape
1180 TopoDS_Shape myLocShape = GeomObjectToShape( theShapeObject );
1181 // call implementation compute
1182 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1183 return myGen.Compute( myLocMesh, myLocShape);
1186 catch ( std::bad_alloc ) {
1187 INFOS( "Compute(): lack of memory" );
1189 catch ( SALOME_Exception& S_ex ) {
1190 INFOS( "Compute(): catch exception "<< S_ex.what() );
1193 INFOS( "Compute(): unknown exception " );
1198 //================================================================================
1200 * \brief Return geometrical object the given element is built on
1201 * \param theMesh - the mesh the element is in
1202 * \param theElementID - the element ID
1203 * \param theGeomName - the name of the result geom object if it is not yet published
1204 * \retval GEOM::GEOM_Object_ptr - the found or just published geom object
1206 //================================================================================
1208 GEOM::GEOM_Object_ptr
1209 SMESH_Gen_i::GetGeometryByMeshElement( SMESH::SMESH_Mesh_ptr theMesh,
1210 CORBA::Long theElementID,
1211 const char* theGeomName)
1212 throw ( SALOME::SALOME_Exception )
1214 Unexpect aCatch(SALOME_SalomeException);
1216 GEOM::GEOM_Object_var geom = FindGeometryByMeshElement(theMesh, theElementID);
1217 if ( !geom->_is_nil() ) {
1218 GEOM::GEOM_Object_var mainShape = theMesh->GetShapeToMesh();
1219 GEOM::GEOM_Gen_ptr geomGen = GetGeomEngine();
1221 // try to find the corresponding SObject
1222 SALOMEDS::SObject_var SObj = ObjectToSObject( myCurrentStudy, geom.in() );
1223 if ( SObj->_is_nil() ) // submesh can be not found even if published
1225 // try to find published submesh
1226 GEOM::ListOfLong_var list = geom->GetSubShapeIndices();
1227 if ( !geom->IsMainShape() && list->length() == 1 ) {
1228 SALOMEDS::SObject_var mainSO = ObjectToSObject( myCurrentStudy, mainShape );
1229 SALOMEDS::ChildIterator_var it;
1230 if ( !mainSO->_is_nil() )
1231 it = myCurrentStudy->NewChildIterator( mainSO );
1232 if ( !it->_is_nil() ) {
1233 for ( it->InitEx(true); SObj->_is_nil() && it->More(); it->Next() ) {
1234 GEOM::GEOM_Object_var subGeom =
1235 GEOM::GEOM_Object::_narrow( SObjectToObject( it->Value() ));
1236 if ( !subGeom->_is_nil() ) {
1237 GEOM::ListOfLong_var subList = subGeom->GetSubShapeIndices();
1238 if ( subList->length() == 1 && list[0] == subList[0] ) {
1247 if ( SObj->_is_nil() ) // publish a new subshape
1248 SObj = geomGen->AddInStudy( myCurrentStudy, geom, theGeomName, mainShape );
1250 // return only published geometry
1251 if ( !SObj->_is_nil() )
1252 return geom._retn();
1254 return GEOM::GEOM_Object::_nil();
1257 //================================================================================
1259 * \brief Return geometrical object the given element is built on.
1260 * \param theMesh - the mesh the element is in
1261 * \param theElementID - the element ID
1262 * \retval GEOM::GEOM_Object_ptr - the found geom object
1264 //================================================================================
1266 GEOM::GEOM_Object_ptr
1267 SMESH_Gen_i::FindGeometryByMeshElement( SMESH::SMESH_Mesh_ptr theMesh,
1268 CORBA::Long theElementID)
1269 throw ( SALOME::SALOME_Exception )
1271 Unexpect aCatch(SALOME_SalomeException);
1272 if ( CORBA::is_nil( theMesh ) )
1273 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference", SALOME::BAD_PARAM );
1275 GEOM::GEOM_Object_var mainShape = theMesh->GetShapeToMesh();
1276 GEOM::GEOM_Gen_ptr geomGen = GetGeomEngine();
1278 // get a core mesh DS
1279 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
1280 if ( meshServant && !geomGen->_is_nil() && !mainShape->_is_nil() )
1282 ::SMESH_Mesh & mesh = meshServant->GetImpl();
1283 SMESHDS_Mesh* meshDS = mesh.GetMeshDS();
1284 // find the element in mesh
1285 if ( const SMDS_MeshElement * elem = meshDS->FindElement( theElementID ) )
1286 // find a shape id by the element
1287 if ( int shapeID = ::SMESH_MeshEditor( &mesh ).FindShape( elem )) {
1288 // get a geom object by the shape id
1289 GEOM::GEOM_Object_var geom = ShapeToGeomObject( meshDS->IndexToShape( shapeID ));
1290 if ( geom->_is_nil() ) {
1291 GEOM::GEOM_IShapesOperations_var op =
1292 geomGen->GetIShapesOperations( GetCurrentStudyID() );
1293 if ( !op->_is_nil() )
1294 geom = op->GetSubShape( mainShape, shapeID );
1296 if ( !geom->_is_nil() ) {
1297 GeomObjectToShape( geom ); // let geom client remember the found shape
1298 return geom._retn();
1302 return GEOM::GEOM_Object::_nil();
1305 //================================================================================
1307 * SMESH_Gen_i::Concatenate
1309 * Concatenate the given meshes into one mesh
1311 //================================================================================
1313 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::Concatenate(const SMESH::mesh_array& theMeshesArray,
1314 CORBA::Boolean theUniteIdenticalGroups,
1315 CORBA::Boolean theMergeNodesAndElements,
1316 CORBA::Double theMergeTolerance)
1317 throw ( SALOME::SALOME_Exception )
1319 typedef map<int, int> TIDsMap;
1320 typedef list<SMESH::SMESH_Group_var> TListOfNewGroups;
1321 typedef map< pair<string, SMESH::ElementType>, TListOfNewGroups > TGroupsMap;
1322 typedef std::set<SMESHDS_GroupBase*> TGroups;
1324 TPythonDump aPythonDump; // prevent dump of called methods
1327 SMESH::SMESH_Mesh_var aNewMesh = CreateEmptyMesh();
1329 if ( !aNewMesh->_is_nil() ) {
1330 SMESH_Mesh_i* aNewImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( aNewMesh ).in() );
1332 ::SMESH_Mesh& aLocMesh = aNewImpl->GetImpl();
1333 SMESHDS_Mesh* aNewMeshDS = aLocMesh.GetMeshDS();
1335 TGroupsMap aGroupsMap;
1336 TListOfNewGroups aListOfNewGroups;
1337 SMESH_MeshEditor aNewEditor = ::SMESH_MeshEditor(&aLocMesh);
1338 SMESH::ListOfGroups_var aListOfGroups = new SMESH::ListOfGroups();
1341 for ( int i = 0; i < theMeshesArray.length(); i++) {
1342 SMESH::SMESH_Mesh_var anInitMesh = theMeshesArray[i];
1343 if ( !anInitMesh->_is_nil() ) {
1344 SMESH_Mesh_i* anInitImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( anInitMesh ).in() );
1346 ::SMESH_Mesh& aInitLocMesh = anInitImpl->GetImpl();
1347 SMESHDS_Mesh* anInitMeshDS = aInitLocMesh.GetMeshDS();
1352 // loop on elements of mesh
1353 SMDS_ElemIteratorPtr itElems = anInitMeshDS->elementsIterator();
1354 const SMDS_MeshElement* anElem = 0;
1355 const SMDS_MeshElement* aNewElem = 0;
1356 int anElemNbNodes = 0;
1358 for ( int j = 0; itElems->more(); j++) {
1359 anElem = itElems->next();
1360 SMDSAbs_ElementType anElemType = anElem->GetType();
1361 anElemNbNodes = anElem->NbNodes();
1362 std::vector<const SMDS_MeshNode*> aNodesArray (anElemNbNodes);
1364 // loop on nodes of element
1365 const SMDS_MeshNode* aNode = 0;
1366 const SMDS_MeshNode* aNewNode = 0;
1367 SMDS_ElemIteratorPtr itNodes = anElem->nodesIterator();
1369 for ( int k = 0; itNodes->more(); k++) {
1370 aNode = static_cast<const SMDS_MeshNode*>(itNodes->next());
1371 if ( nodesMap.find(aNode->GetID()) == nodesMap.end() ) {
1372 aNewNode = aNewMeshDS->AddNode(aNode->X(), aNode->Y(), aNode->Z());
1373 nodesMap.insert( make_pair(aNode->GetID(), aNewNode->GetID()) );
1376 aNewNode = aNewMeshDS->FindNode( nodesMap.find(aNode->GetID())->second );
1377 aNodesArray[k] = aNewNode;
1380 // creates a corresponding element on existent nodes in new mesh
1381 if ( anElem->IsPoly() && anElemType == SMDSAbs_Volume )
1383 const SMDS_PolyhedralVolumeOfNodes* aVolume =
1384 dynamic_cast<const SMDS_PolyhedralVolumeOfNodes*> (anElem);
1386 aNewElem = aNewMeshDS->AddPolyhedralVolume(aNodesArray,
1387 aVolume->GetQuanities());
1388 elemsMap.insert(make_pair(anElem->GetID(), aNewElem->GetID()));
1393 aNewElem = aNewEditor.AddElement(aNodesArray,
1396 elemsMap.insert(make_pair(anElem->GetID(), aNewElem->GetID()));
1400 aListOfGroups = anInitImpl->GetGroups();
1401 SMESH::SMESH_GroupBase_ptr aGroup;
1403 // loop on groups of mesh
1404 SMESH::long_array_var anInitIDs = new SMESH::long_array();
1405 SMESH::long_array_var anNewIDs = new SMESH::long_array();
1406 SMESH::SMESH_Group_var aNewGroup;
1407 for (int i = 0; i < aListOfGroups->length(); i++) {
1408 aGroup = aListOfGroups[i];
1409 aListOfNewGroups.clear();
1410 SMESH::ElementType aGroupType = aGroup->GetType();
1411 CORBA::String_var aGroupName = aGroup->GetName();
1413 TGroupsMap::iterator anIter = aGroupsMap.find(make_pair(aGroupName, aGroupType));
1415 // convert a list of IDs
1416 anInitIDs = aGroup->GetListOfID();
1417 anNewIDs->length(anInitIDs->length());
1418 if ( aGroupType == SMESH::NODE )
1419 for (int j = 0; j < anInitIDs->length(); j++) {
1420 anNewIDs[j] = nodesMap.find(anInitIDs[j])->second;
1423 for (int j = 0; j < anInitIDs->length(); j++) {
1424 anNewIDs[j] = elemsMap.find(anInitIDs[j])->second;
1427 // check that current group name and type don't have identical ones in union mesh
1428 if ( anIter == aGroupsMap.end() ) {
1429 // add a new group in the mesh
1430 aNewGroup = aNewImpl->CreateGroup(aGroupType, aGroupName);
1431 // add elements into new group
1432 aNewGroup->Add( anNewIDs );
1434 aListOfNewGroups.push_back(aNewGroup);
1435 aGroupsMap.insert(make_pair( make_pair(aGroupName, aGroupType), aListOfNewGroups ));
1438 else if ( theUniteIdenticalGroups ) {
1439 // unite identical groups
1440 TListOfNewGroups& aNewGroups = anIter->second;
1441 aNewGroups.front()->Add( anNewIDs );
1445 // rename identical groups
1446 aNewGroup = aNewImpl->CreateGroup(aGroupType, aGroupName);
1447 aNewGroup->Add( anNewIDs );
1449 TListOfNewGroups& aNewGroups = anIter->second;
1450 string aNewGroupName;
1451 if (aNewGroups.size() == 1) {
1452 aNewGroupName = string(aGroupName) + "_1";
1453 aNewGroups.front()->SetName(aNewGroupName.c_str());
1455 char aGroupNum[128];
1456 sprintf(aGroupNum, "%u", aNewGroups.size()+1);
1457 aNewGroupName = string(aGroupName) + "_" + string(aGroupNum);
1458 aNewGroup->SetName(aNewGroupName.c_str());
1459 aNewGroups.push_back(aNewGroup);
1466 if (theMergeNodesAndElements) {
1468 set<const SMDS_MeshNode*> aMeshNodes; // no input nodes
1469 SMESH_MeshEditor::TListOfListOfNodes aGroupsOfNodes;
1470 aNewEditor.FindCoincidentNodes( aMeshNodes, theMergeTolerance, aGroupsOfNodes );
1471 aNewEditor.MergeNodes( aGroupsOfNodes );
1473 aNewEditor.MergeEqualElements();
1478 // Update Python script
1479 aPythonDump << aNewMesh << " = " << this << ".Concatenate(";
1481 for ( int i = 0; i < theMeshesArray.length(); i++) {
1482 if (i > 0) aPythonDump << ", ";
1483 aPythonDump << theMeshesArray[i];
1485 aPythonDump << "], ";
1486 aPythonDump << theUniteIdenticalGroups << ", "
1487 << theMergeNodesAndElements << ", "
1488 << theMergeTolerance << ")";
1490 return aNewMesh._retn();
1493 //=============================================================================
1497 * Save SMESH module's data
1499 //=============================================================================
1500 SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
1504 INFOS( "SMESH_Gen_i::Save" );
1506 // ASSERT( theComponent->GetStudy()->StudyId() == myCurrentStudy->StudyId() )
1507 // san -- in case <myCurrentStudy> differs from theComponent's study,
1508 // use that of the component
1509 if ( myCurrentStudy->_is_nil() ||
1510 theComponent->GetStudy()->StudyId() != myCurrentStudy->StudyId() )
1511 SetCurrentStudy( theComponent->GetStudy() );
1513 // Store study contents as a set of python commands
1514 SavePython(myCurrentStudy);
1516 StudyContext* myStudyContext = GetCurrentStudyContext();
1518 // Declare a byte stream
1519 SALOMEDS::TMPFile_var aStreamFile;
1521 // Obtain a temporary dir
1522 TCollection_AsciiString tmpDir =
1523 ( isMultiFile ) ? TCollection_AsciiString( ( char* )theURL ) : ( char* )SALOMEDS_Tool::GetTmpDir().c_str();
1525 // Create a sequence of files processed
1526 SALOMEDS::ListOfFileNames_var aFileSeq = new SALOMEDS::ListOfFileNames;
1527 aFileSeq->length( NUM_TMP_FILES );
1529 TCollection_AsciiString aStudyName( "" );
1531 aStudyName = ( (char*)SALOMEDS_Tool::GetNameFromPath( myCurrentStudy->URL() ).c_str() );
1533 // Set names of temporary files
1534 TCollection_AsciiString filename =
1535 aStudyName + TCollection_AsciiString( "_SMESH.hdf" ); // for SMESH data itself
1536 TCollection_AsciiString meshfile =
1537 aStudyName + TCollection_AsciiString( "_SMESH_Mesh.med" ); // for mesh data to be stored in MED file
1538 aFileSeq[ 0 ] = CORBA::string_dup( filename.ToCString() );
1539 aFileSeq[ 1 ] = CORBA::string_dup( meshfile.ToCString() );
1540 filename = tmpDir + filename;
1541 meshfile = tmpDir + meshfile;
1544 HDFdataset* aDataset;
1545 HDFgroup* aTopGroup;
1547 HDFgroup* aSubGroup;
1548 HDFgroup* aSubSubGroup;
1549 hdf_size aSize[ 1 ];
1552 //Remove the files if they exist: BugID: 11225
1553 TCollection_AsciiString cmd("rm -f \"");
1558 system(cmd.ToCString());
1560 // MED writer to be used by storage process
1561 DriverMED_W_SMESHDS_Mesh myWriter;
1562 myWriter.SetFile( meshfile.ToCString() );
1565 // ---> create HDF file
1566 aFile = new HDFfile( filename.ToCString() );
1567 aFile->CreateOnDisk();
1569 // --> iterator for top-level objects
1570 SALOMEDS::ChildIterator_var itBig = myCurrentStudy->NewChildIterator( theComponent );
1571 for ( ; itBig->More(); itBig->Next() ) {
1572 SALOMEDS::SObject_var gotBranch = itBig->Value();
1574 // --> hypotheses root branch (only one for the study)
1575 if ( gotBranch->Tag() == GetHypothesisRootTag() ) {
1576 // create hypotheses root HDF group
1577 aTopGroup = new HDFgroup( "Hypotheses", aFile );
1578 aTopGroup->CreateOnDisk();
1580 // iterator for all hypotheses
1581 SALOMEDS::ChildIterator_var it = myCurrentStudy->NewChildIterator( gotBranch );
1582 for ( ; it->More(); it->Next() ) {
1583 SALOMEDS::SObject_var mySObject = it->Value();
1584 CORBA::Object_var anObject = SObjectToObject( mySObject );
1585 if ( !CORBA::is_nil( anObject ) ) {
1586 SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
1587 if ( !myHyp->_is_nil() ) {
1588 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
1590 string hypname = string( myHyp->GetName() );
1591 string libname = string( myHyp->GetLibName() );
1592 int id = myStudyContext->findId( string( GetORB()->object_to_string( anObject ) ) );
1593 string hypdata = string( myImpl->SaveTo() );
1595 // for each hypothesis create HDF group basing on its id
1596 char hypGrpName[30];
1597 sprintf( hypGrpName, "Hypothesis %d", id );
1598 aGroup = new HDFgroup( hypGrpName, aTopGroup );
1599 aGroup->CreateOnDisk();
1600 // --> type name of hypothesis
1601 aSize[ 0 ] = hypname.length() + 1;
1602 aDataset = new HDFdataset( "Name", aGroup, HDF_STRING, aSize, 1 );
1603 aDataset->CreateOnDisk();
1604 aDataset->WriteOnDisk( ( char* )( hypname.c_str() ) );
1605 aDataset->CloseOnDisk();
1606 // --> server plugin library name of hypothesis
1607 aSize[ 0 ] = libname.length() + 1;
1608 aDataset = new HDFdataset( "LibName", aGroup, HDF_STRING, aSize, 1 );
1609 aDataset->CreateOnDisk();
1610 aDataset->WriteOnDisk( ( char* )( libname.c_str() ) );
1611 aDataset->CloseOnDisk();
1612 // --> persistent data of hypothesis
1613 aSize[ 0 ] = hypdata.length() + 1;
1614 aDataset = new HDFdataset( "Data", aGroup, HDF_STRING, aSize, 1 );
1615 aDataset->CreateOnDisk();
1616 aDataset->WriteOnDisk( ( char* )( hypdata.c_str() ) );
1617 aDataset->CloseOnDisk();
1618 // close hypothesis HDF group
1619 aGroup->CloseOnDisk();
1624 // close hypotheses root HDF group
1625 aTopGroup->CloseOnDisk();
1627 // --> algorithms root branch (only one for the study)
1628 else if ( gotBranch->Tag() == GetAlgorithmsRootTag() ) {
1629 // create algorithms root HDF group
1630 aTopGroup = new HDFgroup( "Algorithms", aFile );
1631 aTopGroup->CreateOnDisk();
1633 // iterator for all algorithms
1634 SALOMEDS::ChildIterator_var it = myCurrentStudy->NewChildIterator( gotBranch );
1635 for ( ; it->More(); it->Next() ) {
1636 SALOMEDS::SObject_var mySObject = it->Value();
1637 CORBA::Object_var anObject = SObjectToObject( mySObject );
1638 if ( !CORBA::is_nil( anObject ) ) {
1639 SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
1640 if ( !myHyp->_is_nil() ) {
1641 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
1643 string hypname = string( myHyp->GetName() );
1644 string libname = string( myHyp->GetLibName() );
1645 int id = myStudyContext->findId( string( GetORB()->object_to_string( anObject ) ) );
1646 string hypdata = string( myImpl->SaveTo() );
1648 // for each algorithm create HDF group basing on its id
1649 char hypGrpName[30];
1650 sprintf( hypGrpName, "Algorithm %d", id );
1651 aGroup = new HDFgroup( hypGrpName, aTopGroup );
1652 aGroup->CreateOnDisk();
1653 // --> type name of algorithm
1654 aSize[0] = hypname.length() + 1;
1655 aDataset = new HDFdataset( "Name", aGroup, HDF_STRING, aSize, 1 );
1656 aDataset->CreateOnDisk();
1657 aDataset->WriteOnDisk( ( char* )( hypname.c_str() ) );
1658 aDataset->CloseOnDisk();
1659 // --> server plugin library name of hypothesis
1660 aSize[0] = libname.length() + 1;
1661 aDataset = new HDFdataset( "LibName", aGroup, HDF_STRING, aSize, 1 );
1662 aDataset->CreateOnDisk();
1663 aDataset->WriteOnDisk( ( char* )( libname.c_str() ) );
1664 aDataset->CloseOnDisk();
1665 // --> persistent data of algorithm
1666 aSize[0] = hypdata.length() + 1;
1667 aDataset = new HDFdataset( "Data", aGroup, HDF_STRING, aSize, 1 );
1668 aDataset->CreateOnDisk();
1669 aDataset->WriteOnDisk( ( char* )( hypdata.c_str() ) );
1670 aDataset->CloseOnDisk();
1671 // close algorithm HDF group
1672 aGroup->CloseOnDisk();
1677 // close algorithms root HDF group
1678 aTopGroup->CloseOnDisk();
1680 // --> mesh objects roots branches
1681 else if ( gotBranch->Tag() > GetAlgorithmsRootTag() ) {
1682 CORBA::Object_var anObject = SObjectToObject( gotBranch );
1683 if ( !CORBA::is_nil( anObject ) ) {
1684 SMESH::SMESH_Mesh_var myMesh = SMESH::SMESH_Mesh::_narrow( anObject ) ;
1685 if ( !myMesh->_is_nil() ) {
1686 SMESH_Mesh_i* myImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( myMesh ).in() );
1688 int id = myStudyContext->findId( string( GetORB()->object_to_string( anObject ) ) );
1689 ::SMESH_Mesh& myLocMesh = myImpl->GetImpl();
1690 SMESHDS_Mesh* mySMESHDSMesh = myLocMesh.GetMeshDS();
1692 // for each mesh open the HDF group basing on its id
1693 char meshGrpName[ 30 ];
1694 sprintf( meshGrpName, "Mesh %d", id );
1695 aTopGroup = new HDFgroup( meshGrpName, aFile );
1696 aTopGroup->CreateOnDisk();
1698 // --> put dataset to hdf file which is a flag that mesh has data
1699 string strHasData = "0";
1700 // check if the mesh is not empty
1701 if ( mySMESHDSMesh->NbNodes() > 0 ) {
1702 // write mesh data to med file
1703 myWriter.SetMesh( mySMESHDSMesh );
1704 myWriter.SetMeshId( id );
1707 aSize[ 0 ] = strHasData.length() + 1;
1708 aDataset = new HDFdataset( "Has data", aTopGroup, HDF_STRING, aSize, 1 );
1709 aDataset->CreateOnDisk();
1710 aDataset->WriteOnDisk( ( char* )( strHasData.c_str() ) );
1711 aDataset->CloseOnDisk();
1713 // write reference on a shape if exists
1714 SALOMEDS::SObject_var myRef;
1715 bool shapeRefFound = false;
1716 bool found = gotBranch->FindSubObject( GetRefOnShapeTag(), myRef );
1718 SALOMEDS::SObject_var myShape;
1719 bool ok = myRef->ReferencedObject( myShape );
1721 shapeRefFound = (! CORBA::is_nil( myShape->GetObject() ));
1722 string myRefOnObject = myShape->GetID();
1723 if ( shapeRefFound && myRefOnObject.length() > 0 ) {
1724 aSize[ 0 ] = myRefOnObject.length() + 1;
1725 aDataset = new HDFdataset( "Ref on shape", aTopGroup, HDF_STRING, aSize, 1 );
1726 aDataset->CreateOnDisk();
1727 aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
1728 aDataset->CloseOnDisk();
1733 // write applied hypotheses if exist
1734 SALOMEDS::SObject_var myHypBranch;
1735 found = gotBranch->FindSubObject( GetRefOnAppliedHypothesisTag(), myHypBranch );
1736 if ( found && !shapeRefFound ) { // remove applied hyps
1737 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( myHypBranch );
1739 if ( found && shapeRefFound ) {
1740 aGroup = new HDFgroup( "Applied Hypotheses", aTopGroup );
1741 aGroup->CreateOnDisk();
1743 SALOMEDS::ChildIterator_var it = myCurrentStudy->NewChildIterator( myHypBranch );
1745 for ( ; it->More(); it->Next() ) {
1746 SALOMEDS::SObject_var mySObject = it->Value();
1747 SALOMEDS::SObject_var myRefOnHyp;
1748 bool ok = mySObject->ReferencedObject( myRefOnHyp );
1750 // san - it is impossible to recover applied hypotheses
1751 // using their entries within Load() method,
1752 // for there are no AttributeIORs in the study when Load() is working.
1753 // Hence, it is better to store persistent IDs of hypotheses as references to them
1755 //string myRefOnObject = myRefOnHyp->GetID();
1756 CORBA::Object_var anObject = SObjectToObject( myRefOnHyp );
1757 int id = myStudyContext->findId( string( GetORB()->object_to_string( anObject ) ) );
1758 //if ( myRefOnObject.length() > 0 ) {
1759 //aSize[ 0 ] = myRefOnObject.length() + 1;
1760 char hypName[ 30 ], hypId[ 30 ];
1761 sprintf( hypName, "Hyp %d", ++hypNb );
1762 sprintf( hypId, "%d", id );
1763 aSize[ 0 ] = strlen( hypId ) + 1;
1764 aDataset = new HDFdataset( hypName, aGroup, HDF_STRING, aSize, 1 );
1765 aDataset->CreateOnDisk();
1766 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
1767 aDataset->WriteOnDisk( hypId );
1768 aDataset->CloseOnDisk();
1772 aGroup->CloseOnDisk();
1775 // write applied algorithms if exist
1776 SALOMEDS::SObject_var myAlgoBranch;
1777 found = gotBranch->FindSubObject( GetRefOnAppliedAlgorithmsTag(), myAlgoBranch );
1778 if ( found && !shapeRefFound ) { // remove applied algos
1779 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( myAlgoBranch );
1781 if ( found && shapeRefFound ) {
1782 aGroup = new HDFgroup( "Applied Algorithms", aTopGroup );
1783 aGroup->CreateOnDisk();
1785 SALOMEDS::ChildIterator_var it = myCurrentStudy->NewChildIterator( myAlgoBranch );
1787 for ( ; it->More(); it->Next() ) {
1788 SALOMEDS::SObject_var mySObject = it->Value();
1789 SALOMEDS::SObject_var myRefOnAlgo;
1790 bool ok = mySObject->ReferencedObject( myRefOnAlgo );
1792 // san - it is impossible to recover applied algorithms
1793 // using their entries within Load() method,
1794 // for there are no AttributeIORs in the study when Load() is working.
1795 // Hence, it is better to store persistent IDs of algorithms as references to them
1797 //string myRefOnObject = myRefOnAlgo->GetID();
1798 CORBA::Object_var anObject = SObjectToObject( myRefOnAlgo );
1799 int id = myStudyContext->findId( string( GetORB()->object_to_string( anObject ) ) );
1800 //if ( myRefOnObject.length() > 0 ) {
1801 //aSize[ 0 ] = myRefOnObject.length() + 1;
1802 char algoName[ 30 ], algoId[ 30 ];
1803 sprintf( algoName, "Algo %d", ++algoNb );
1804 sprintf( algoId, "%d", id );
1805 aSize[ 0 ] = strlen( algoId ) + 1;
1806 aDataset = new HDFdataset( algoName, aGroup, HDF_STRING, aSize, 1 );
1807 aDataset->CreateOnDisk();
1808 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
1809 aDataset->WriteOnDisk( algoId );
1810 aDataset->CloseOnDisk();
1814 aGroup->CloseOnDisk();
1817 // --> submesh objects sub-branches
1819 for ( int i = GetSubMeshOnVertexTag(); i <= GetSubMeshOnCompoundTag(); i++ ) {
1820 SALOMEDS::SObject_var mySubmeshBranch;
1821 found = gotBranch->FindSubObject( i, mySubmeshBranch );
1823 if ( found ) // check if there is shape reference in submeshes
1825 bool hasShapeRef = false;
1826 SALOMEDS::ChildIterator_var itSM =
1827 myCurrentStudy->NewChildIterator( mySubmeshBranch );
1828 for ( ; itSM->More(); itSM->Next() ) {
1829 SALOMEDS::SObject_var mySubRef, myShape, mySObject = itSM->Value();
1830 if ( mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef ))
1831 mySubRef->ReferencedObject( myShape );
1832 if ( !CORBA::is_nil( myShape ) && !CORBA::is_nil( myShape->GetObject() ))
1835 { // remove one submesh
1836 if ( shapeRefFound )
1837 { // unassign hypothesis
1838 SMESH::SMESH_subMesh_var mySubMesh =
1839 SMESH::SMESH_subMesh::_narrow( SObjectToObject( mySObject ));
1840 if ( !mySubMesh->_is_nil() ) {
1841 int shapeID = mySubMesh->GetId();
1842 TopoDS_Shape S = mySMESHDSMesh->IndexToShape( shapeID );
1843 const list<const SMESHDS_Hypothesis*>& hypList =
1844 mySMESHDSMesh->GetHypothesis( S );
1845 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
1846 while ( hyp != hypList.end() ) {
1847 int hypID = (*hyp++)->GetID(); // goto next hyp here because
1848 myLocMesh.RemoveHypothesis( S, hypID ); // hypList changes here
1852 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( mySObject );
1854 } // loop on submeshes of a type
1855 if ( !shapeRefFound || !hasShapeRef ) { // remove the whole submeshes branch
1856 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( mySubmeshBranch );
1859 } // end check if there is shape reference in submeshes
1861 char name_meshgroup[ 30 ];
1862 if ( i == GetSubMeshOnVertexTag() )
1863 strcpy( name_meshgroup, "SubMeshes On Vertex" );
1864 else if ( i == GetSubMeshOnEdgeTag() )
1865 strcpy( name_meshgroup, "SubMeshes On Edge" );
1866 else if ( i == GetSubMeshOnWireTag() )
1867 strcpy( name_meshgroup, "SubMeshes On Wire" );
1868 else if ( i == GetSubMeshOnFaceTag() )
1869 strcpy( name_meshgroup, "SubMeshes On Face" );
1870 else if ( i == GetSubMeshOnShellTag() )
1871 strcpy( name_meshgroup, "SubMeshes On Shell" );
1872 else if ( i == GetSubMeshOnSolidTag() )
1873 strcpy( name_meshgroup, "SubMeshes On Solid" );
1874 else if ( i == GetSubMeshOnCompoundTag() )
1875 strcpy( name_meshgroup, "SubMeshes On Compound" );
1877 // for each type of submeshes create container HDF group
1878 aGroup = new HDFgroup( name_meshgroup, aTopGroup );
1879 aGroup->CreateOnDisk();
1881 // iterator for all submeshes of given type
1882 SALOMEDS::ChildIterator_var itSM = myCurrentStudy->NewChildIterator( mySubmeshBranch );
1883 for ( ; itSM->More(); itSM->Next() ) {
1884 SALOMEDS::SObject_var mySObject = itSM->Value();
1885 CORBA::Object_var anSubObject = SObjectToObject( mySObject );
1886 if ( !CORBA::is_nil( anSubObject ))
1888 SMESH::SMESH_subMesh_var mySubMesh = SMESH::SMESH_subMesh::_narrow( anSubObject ) ;
1889 int subid = myStudyContext->findId( string( GetORB()->object_to_string( anSubObject ) ) );
1891 // for each mesh open the HDF group basing on its id
1892 char submeshGrpName[ 30 ];
1893 sprintf( submeshGrpName, "SubMesh %d", subid );
1894 aSubGroup = new HDFgroup( submeshGrpName, aGroup );
1895 aSubGroup->CreateOnDisk();
1897 // write reference on a shape, already checked if it exists
1898 SALOMEDS::SObject_var mySubRef, myShape;
1899 if ( mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef ))
1900 mySubRef->ReferencedObject( myShape );
1901 string myRefOnObject = myShape->GetID();
1902 if ( myRefOnObject.length() > 0 ) {
1903 aSize[ 0 ] = myRefOnObject.length() + 1;
1904 aDataset = new HDFdataset( "Ref on shape", aSubGroup, HDF_STRING, aSize, 1 );
1905 aDataset->CreateOnDisk();
1906 aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
1907 aDataset->CloseOnDisk();
1910 // write applied hypotheses if exist
1911 SALOMEDS::SObject_var mySubHypBranch;
1912 found = mySObject->FindSubObject( GetRefOnAppliedHypothesisTag(), mySubHypBranch );
1914 aSubSubGroup = new HDFgroup( "Applied Hypotheses", aSubGroup );
1915 aSubSubGroup->CreateOnDisk();
1917 SALOMEDS::ChildIterator_var it = myCurrentStudy->NewChildIterator( mySubHypBranch );
1919 for ( ; it->More(); it->Next() ) {
1920 SALOMEDS::SObject_var mySubSObject = it->Value();
1921 SALOMEDS::SObject_var myRefOnHyp;
1922 bool ok = mySubSObject->ReferencedObject( myRefOnHyp );
1924 //string myRefOnObject = myRefOnHyp->GetID();
1925 CORBA::Object_var anObject = SObjectToObject( myRefOnHyp );
1926 int id = myStudyContext->findId( string( GetORB()->object_to_string( anObject ) ) );
1927 //if ( myRefOnObject.length() > 0 ) {
1928 //aSize[ 0 ] = myRefOnObject.length() + 1;
1929 char hypName[ 30 ], hypId[ 30 ];
1930 sprintf( hypName, "Hyp %d", ++hypNb );
1931 sprintf( hypId, "%d", id );
1932 aSize[ 0 ] = strlen( hypId ) + 1;
1933 aDataset = new HDFdataset( hypName, aSubSubGroup, HDF_STRING, aSize, 1 );
1934 aDataset->CreateOnDisk();
1935 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
1936 aDataset->WriteOnDisk( hypId );
1937 aDataset->CloseOnDisk();
1941 aSubSubGroup->CloseOnDisk();
1944 // write applied algorithms if exist
1945 SALOMEDS::SObject_var mySubAlgoBranch;
1946 found = mySObject->FindSubObject( GetRefOnAppliedAlgorithmsTag(), mySubAlgoBranch );
1948 aSubSubGroup = new HDFgroup( "Applied Algorithms", aSubGroup );
1949 aSubSubGroup->CreateOnDisk();
1951 SALOMEDS::ChildIterator_var it = myCurrentStudy->NewChildIterator( mySubAlgoBranch );
1953 for ( ; it->More(); it->Next() ) {
1954 SALOMEDS::SObject_var mySubSObject = it->Value();
1955 SALOMEDS::SObject_var myRefOnAlgo;
1956 bool ok = mySubSObject->ReferencedObject( myRefOnAlgo );
1958 //string myRefOnObject = myRefOnAlgo->GetID();
1959 CORBA::Object_var anObject = SObjectToObject( myRefOnAlgo );
1960 int id = myStudyContext->findId( string( GetORB()->object_to_string( anObject ) ) );
1961 //if ( myRefOnObject.length() > 0 ) {
1962 //aSize[ 0 ] = myRefOnObject.length() + 1;
1963 char algoName[ 30 ], algoId[ 30 ];
1964 sprintf( algoName, "Algo %d", ++algoNb );
1965 sprintf( algoId, "%d", id );
1966 aSize[ 0 ] = strlen( algoId ) + 1;
1967 aDataset = new HDFdataset( algoName, aSubSubGroup, HDF_STRING, aSize, 1 );
1968 aDataset->CreateOnDisk();
1969 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
1970 aDataset->WriteOnDisk( algoId );
1971 aDataset->CloseOnDisk();
1975 aSubSubGroup->CloseOnDisk();
1977 // close submesh HDF group
1978 aSubGroup->CloseOnDisk();
1981 // close container of submeshes by type HDF group
1982 aGroup->CloseOnDisk();
1985 // All sub-meshes will be stored in MED file
1986 // .. will NOT (PAL 12992)
1987 //if ( shapeRefFound )
1988 //myWriter.AddAllSubMeshes();
1990 // groups root sub-branch
1991 SALOMEDS::SObject_var myGroupsBranch;
1992 for ( int i = GetNodeGroupsTag(); i <= GetVolumeGroupsTag(); i++ ) {
1993 found = gotBranch->FindSubObject( i, myGroupsBranch );
1995 char name_group[ 30 ];
1996 if ( i == GetNodeGroupsTag() )
1997 strcpy( name_group, "Groups of Nodes" );
1998 else if ( i == GetEdgeGroupsTag() )
1999 strcpy( name_group, "Groups of Edges" );
2000 else if ( i == GetFaceGroupsTag() )
2001 strcpy( name_group, "Groups of Faces" );
2002 else if ( i == GetVolumeGroupsTag() )
2003 strcpy( name_group, "Groups of Volumes" );
2005 aGroup = new HDFgroup( name_group, aTopGroup );
2006 aGroup->CreateOnDisk();
2008 SALOMEDS::ChildIterator_var it = myCurrentStudy->NewChildIterator( myGroupsBranch );
2009 for ( ; it->More(); it->Next() ) {
2010 SALOMEDS::SObject_var mySObject = it->Value();
2011 CORBA::Object_var aSubObject = SObjectToObject( mySObject );
2012 if ( !CORBA::is_nil( aSubObject ) ) {
2013 SMESH_GroupBase_i* myGroupImpl =
2014 dynamic_cast<SMESH_GroupBase_i*>( GetServant( aSubObject ).in() );
2018 int anId = myStudyContext->findId( string( GetORB()->object_to_string( aSubObject ) ) );
2020 // For each group, create a dataset named "Group <group_persistent_id>"
2021 // and store the group's user name into it
2023 sprintf( grpName, "Group %d", anId );
2024 char* aUserName = myGroupImpl->GetName();
2025 aSize[ 0 ] = strlen( aUserName ) + 1;
2027 aDataset = new HDFdataset( grpName, aGroup, HDF_STRING, aSize, 1 );
2028 aDataset->CreateOnDisk();
2029 aDataset->WriteOnDisk( aUserName );
2030 aDataset->CloseOnDisk();
2032 // Store the group contents into MED file
2033 if ( myLocMesh.GetGroup( myGroupImpl->GetLocalID() ) ) {
2035 if(MYDEBUG) MESSAGE( "VSR - SMESH_Gen_i::Save(): saving group with StoreName = "
2036 << grpName << " to MED file" );
2037 SMESHDS_GroupBase* aGrpBaseDS =
2038 myLocMesh.GetGroup( myGroupImpl->GetLocalID() )->GetGroupDS();
2039 aGrpBaseDS->SetStoreName( grpName );
2041 // Pass SMESHDS_Group to MED writer
2042 SMESHDS_Group* aGrpDS = dynamic_cast<SMESHDS_Group*>( aGrpBaseDS );
2044 myWriter.AddGroup( aGrpDS );
2046 // write reference on a shape if exists
2047 SMESHDS_GroupOnGeom* aGeomGrp =
2048 dynamic_cast<SMESHDS_GroupOnGeom*>( aGrpBaseDS );
2050 SALOMEDS::SObject_var mySubRef, myShape;
2051 if (mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef ) &&
2052 mySubRef->ReferencedObject( myShape ) &&
2053 !CORBA::is_nil( myShape->GetObject() ))
2055 string myRefOnObject = myShape->GetID();
2056 if ( myRefOnObject.length() > 0 ) {
2057 char aRefName[ 30 ];
2058 sprintf( aRefName, "Ref on shape %d", anId);
2059 aSize[ 0 ] = myRefOnObject.length() + 1;
2060 aDataset = new HDFdataset(aRefName, aGroup, HDF_STRING, aSize, 1);
2061 aDataset->CreateOnDisk();
2062 aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
2063 aDataset->CloseOnDisk();
2066 else // shape ref is invalid:
2068 // save a group on geometry as ordinary group
2069 myWriter.AddGroup( aGeomGrp );
2075 aGroup->CloseOnDisk();
2079 if ( strcmp( strHasData.c_str(), "1" ) == 0 )
2081 // Flush current mesh information into MED file
2084 // maybe a shape was deleted in the study
2085 if ( !shapeRefFound && !mySMESHDSMesh->ShapeToMesh().IsNull() ) {
2086 TopoDS_Shape nullShape;
2087 myLocMesh.ShapeToMesh( nullShape ); // remove shape referring data
2090 if ( !mySMESHDSMesh->SubMeshes().empty() )
2094 aGroup = new HDFgroup( "Submeshes", aTopGroup );
2095 aGroup->CreateOnDisk();
2097 // each element belongs to one or none submesh,
2098 // so for each node/element, we store a submesh ID
2100 // Make maps of submesh IDs of elements sorted by element IDs
2101 typedef int TElemID;
2102 typedef int TSubMID;
2103 map< TElemID, TSubMID > eId2smId, nId2smId;
2104 map< TElemID, TSubMID >::iterator hint; // insertion to map is done before hint
2105 const map<int,SMESHDS_SubMesh*>& aSubMeshes = mySMESHDSMesh->SubMeshes();
2106 map<int,SMESHDS_SubMesh*>::const_iterator itSubM ( aSubMeshes.begin() );
2107 SMDS_NodeIteratorPtr itNode;
2108 SMDS_ElemIteratorPtr itElem;
2109 for ( itSubM = aSubMeshes.begin(); itSubM != aSubMeshes.end() ; itSubM++ )
2111 TSubMID aSubMeID = itSubM->first;
2112 SMESHDS_SubMesh* aSubMesh = itSubM->second;
2113 if ( aSubMesh->IsComplexSubmesh() )
2114 continue; // submesh containing other submeshs
2116 hint = nId2smId.begin(); // optimize insertion basing on increasing order of elem Ids in submesh
2117 for ( itNode = aSubMesh->GetNodes(); itNode->more(); ++hint)
2118 hint = nId2smId.insert( hint, make_pair( itNode->next()->GetID(), aSubMeID ));
2120 hint = eId2smId.begin();
2121 for ( itElem = aSubMesh->GetElements(); itElem->more(); ++hint)
2122 hint = eId2smId.insert( hint, make_pair( itElem->next()->GetID(), aSubMeID ));
2125 // Care of elements that are not on submeshes
2126 if ( mySMESHDSMesh->NbNodes() != nId2smId.size() ) {
2127 for ( itNode = mySMESHDSMesh->nodesIterator(); itNode->more(); )
2128 /* --- stl_map.h says : */
2129 /* A %map relies on unique keys and thus a %pair is only inserted if its */
2130 /* first element (the key) is not already present in the %map. */
2131 nId2smId.insert( make_pair( itNode->next()->GetID(), 0 ));
2133 int nbElems = mySMESHDSMesh->NbEdges() + mySMESHDSMesh->NbFaces() + mySMESHDSMesh->NbVolumes();
2134 if ( nbElems != eId2smId.size() ) {
2135 for ( itElem = mySMESHDSMesh->elementsIterator(); itElem->more(); )
2136 eId2smId.insert( make_pair( itElem->next()->GetID(), 0 ));
2139 // Store submesh IDs
2140 for ( int isNode = 0; isNode < 2; ++isNode )
2142 map< TElemID, TSubMID >& id2smId = isNode ? nId2smId : eId2smId;
2143 if ( id2smId.empty() ) continue;
2144 map< TElemID, TSubMID >::const_iterator id_smId = id2smId.begin();
2145 // make and fill array of submesh IDs
2146 int* smIDs = new int [ id2smId.size() ];
2147 for ( int i = 0; id_smId != id2smId.end(); ++id_smId, ++i )
2148 smIDs[ i ] = id_smId->second;
2150 aSize[ 0 ] = id2smId.size();
2151 string aDSName( isNode ? "Node Submeshes" : "Element Submeshes");
2152 aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_INT32, aSize, 1 );
2153 aDataset->CreateOnDisk();
2154 aDataset->WriteOnDisk( smIDs );
2155 aDataset->CloseOnDisk();
2160 // Store node positions on sub-shapes (SMDS_Position):
2161 // ----------------------------------------------------
2163 aGroup = new HDFgroup( "Node Positions", aTopGroup );
2164 aGroup->CreateOnDisk();
2166 // in aGroup, create 5 datasets to contain:
2167 // "Nodes on Edges" - ID of node on edge
2168 // "Edge positions" - U parameter on node on edge
2169 // "Nodes on Faces" - ID of node on face
2170 // "Face U positions" - U parameter of node on face
2171 // "Face V positions" - V parameter of node on face
2173 // Find out nb of nodes on edges and faces
2174 // Collect corresponing sub-meshes
2175 int nbEdgeNodes = 0, nbFaceNodes = 0;
2176 list<SMESHDS_SubMesh*> aEdgeSM, aFaceSM;
2177 // loop on SMESHDS_SubMesh'es
2178 for ( itSubM = aSubMeshes.begin(); itSubM != aSubMeshes.end() ; itSubM++ )
2180 SMESHDS_SubMesh* aSubMesh = (*itSubM).second;
2181 if ( aSubMesh->IsComplexSubmesh() )
2182 continue; // submesh containing other submeshs
2183 int nbNodes = aSubMesh->NbNodes();
2184 if ( nbNodes == 0 ) continue;
2186 int aShapeID = (*itSubM).first;
2187 int aShapeType = mySMESHDSMesh->IndexToShape( aShapeID ).ShapeType();
2188 // write only SMDS_FacePosition and SMDS_EdgePosition
2189 switch ( aShapeType ) {
2191 nbFaceNodes += nbNodes;
2192 aFaceSM.push_back( aSubMesh );
2195 nbEdgeNodes += nbNodes;
2196 aEdgeSM.push_back( aSubMesh );
2202 // Treat positions on edges or faces
2203 for ( int onFace = 0; onFace < 2; onFace++ )
2205 // Create arrays to store in datasets
2206 int iNode = 0, nbNodes = ( onFace ? nbFaceNodes : nbEdgeNodes );
2207 if (!nbNodes) continue;
2208 int* aNodeIDs = new int [ nbNodes ];
2209 double* aUPos = new double [ nbNodes ];
2210 double* aVPos = ( onFace ? new double[ nbNodes ] : 0 );
2213 // loop on sub-meshes
2214 list<SMESHDS_SubMesh*> * pListSM = ( onFace ? &aFaceSM : &aEdgeSM );
2215 list<SMESHDS_SubMesh*>::iterator itSM = pListSM->begin();
2216 for ( ; itSM != pListSM->end(); itSM++ )
2218 SMESHDS_SubMesh* aSubMesh = (*itSM);
2220 SMDS_NodeIteratorPtr itNode = aSubMesh->GetNodes();
2221 // loop on nodes in aSubMesh
2222 while ( itNode->more() )
2225 const SMDS_MeshNode* node = itNode->next();
2226 aNodeIDs [ iNode ] = node->GetID();
2229 const SMDS_PositionPtr pos = node->GetPosition();
2230 if ( onFace ) { // on FACE
2231 const SMDS_FacePosition* fPos =
2232 dynamic_cast<const SMDS_FacePosition*>( pos.get() );
2234 aUPos[ iNode ] = fPos->GetUParameter();
2235 aVPos[ iNode ] = fPos->GetVParameter();
2242 const SMDS_EdgePosition* ePos =
2243 dynamic_cast<const SMDS_EdgePosition*>( pos.get() );
2245 aUPos[ iNode ] = ePos->GetUParameter();
2251 } // loop on nodes in aSubMesh
2252 } // loop on sub-meshes
2257 aSize[ 0 ] = nbNodes;
2259 string aDSName( onFace ? "Nodes on Faces" : "Nodes on Edges");
2260 aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_INT32, aSize, 1 );
2261 aDataset->CreateOnDisk();
2262 aDataset->WriteOnDisk( aNodeIDs );
2263 aDataset->CloseOnDisk();
2266 aDSName = ( onFace ? "Face U positions" : "Edge positions");
2267 aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_FLOAT64, aSize, 1);
2268 aDataset->CreateOnDisk();
2269 aDataset->WriteOnDisk( aUPos );
2270 aDataset->CloseOnDisk();
2273 aDataset = new HDFdataset( "Face V positions", aGroup, HDF_FLOAT64, aSize, 1);
2274 aDataset->CreateOnDisk();
2275 aDataset->WriteOnDisk( aVPos );
2276 aDataset->CloseOnDisk();
2281 if ( aVPos ) delete [] aVPos;
2283 } // treat positions on edges or faces
2285 // close "Node Positions" group
2286 aGroup->CloseOnDisk();
2288 } // if ( there are submeshes in SMESHDS_Mesh )
2291 // close mesh HDF group
2292 aTopGroup->CloseOnDisk();
2300 aFile->CloseOnDisk();
2303 // Convert temporary files to stream
2304 aStreamFile = SALOMEDS_Tool::PutFilesToStream( tmpDir.ToCString(), aFileSeq.in(), isMultiFile );
2306 // Remove temporary files and directory
2308 SALOMEDS_Tool::RemoveTemporaryFiles( tmpDir.ToCString(), aFileSeq.in(), true );
2310 INFOS( "SMESH_Gen_i::Save() completed" );
2311 return aStreamFile._retn();
2314 //=============================================================================
2316 * SMESH_Gen_i::SaveASCII
2318 * Save SMESH module's data in ASCII format (not implemented yet)
2320 //=============================================================================
2322 SALOMEDS::TMPFile* SMESH_Gen_i::SaveASCII( SALOMEDS::SComponent_ptr theComponent,
2324 bool isMultiFile ) {
2325 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::SaveASCII" );
2326 SALOMEDS::TMPFile_var aStreamFile = Save( theComponent, theURL, isMultiFile );
2327 return aStreamFile._retn();
2330 //=============================================================================
2332 * SMESH_Gen_i::loadGeomData
2334 * Load GEOM module data
2336 //=============================================================================
2338 void SMESH_Gen_i::loadGeomData( SALOMEDS::SComponent_ptr theCompRoot )
2340 if ( theCompRoot->_is_nil() )
2343 SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow( theCompRoot->GetStudy() );
2344 if ( aStudy->_is_nil() )
2347 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
2348 aStudyBuilder->LoadWith( theCompRoot, GetGeomEngine() );
2350 //=============================================================================
2352 * \brief Creates SMDS_Position according to shape type
2354 //=============================================================================
2356 class PositionCreator {
2358 SMDS_PositionPtr MakePosition(const TopAbs_ShapeEnum type) {
2359 return (this->*myFuncTable[ type ])();
2362 myFuncTable.resize( (size_t) TopAbs_SHAPE, & PositionCreator::defaultPosition );
2363 myFuncTable[ TopAbs_FACE ] = & PositionCreator::facePosition;
2364 myFuncTable[ TopAbs_EDGE ] = & PositionCreator::edgePosition;
2365 myFuncTable[ TopAbs_VERTEX ] = & PositionCreator::vertexPosition;
2368 SMDS_PositionPtr edgePosition() const { return SMDS_PositionPtr( new SMDS_EdgePosition ); }
2369 SMDS_PositionPtr facePosition() const { return SMDS_PositionPtr( new SMDS_FacePosition ); }
2370 SMDS_PositionPtr vertexPosition() const { return SMDS_PositionPtr( new SMDS_VertexPosition); }
2371 SMDS_PositionPtr defaultPosition() const { return SMDS_SpacePosition::originSpacePosition(); }
2372 typedef SMDS_PositionPtr (PositionCreator:: * FmakePos)() const;
2373 vector<FmakePos> myFuncTable;
2376 //=============================================================================
2380 * Load SMESH module's data
2382 //=============================================================================
2384 bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
2385 const SALOMEDS::TMPFile& theStream,
2389 INFOS( "SMESH_Gen_i::Load" );
2391 if ( myCurrentStudy->_is_nil() ||
2392 theComponent->GetStudy()->StudyId() != myCurrentStudy->StudyId() )
2393 SetCurrentStudy( theComponent->GetStudy() );
2395 /* if( !theComponent->_is_nil() )
2397 //SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow( theComponent->GetStudy() );
2398 if( !myCurrentStudy->FindComponent( "GEOM" )->_is_nil() )
2399 loadGeomData( myCurrentStudy->FindComponent( "GEOM" ) );
2402 StudyContext* myStudyContext = GetCurrentStudyContext();
2404 // Get temporary files location
2405 TCollection_AsciiString tmpDir =
2406 isMultiFile ? TCollection_AsciiString( ( char* )theURL ) : ( char* )SALOMEDS_Tool::GetTmpDir().c_str();
2408 // Convert the stream into sequence of files to process
2409 SALOMEDS::ListOfFileNames_var aFileSeq = SALOMEDS_Tool::PutStreamToFiles( theStream,
2412 TCollection_AsciiString aStudyName( "" );
2414 aStudyName = ( (char*)SALOMEDS_Tool::GetNameFromPath( myCurrentStudy->URL() ).c_str() );
2416 // Set names of temporary files
2417 TCollection_AsciiString filename = tmpDir + aStudyName + TCollection_AsciiString( "_SMESH.hdf" );
2418 TCollection_AsciiString meshfile = tmpDir + aStudyName + TCollection_AsciiString( "_SMESH_Mesh.med" );
2422 HDFdataset* aDataset;
2423 HDFgroup* aTopGroup;
2425 HDFgroup* aSubGroup;
2426 HDFgroup* aSubSubGroup;
2429 // ---> open HDF file
2430 aFile = new HDFfile( filename.ToCString() );
2432 aFile->OpenOnDisk( HDF_RDONLY );
2434 catch ( HDFexception ) {
2435 INFOS( "Load(): " << filename << " not found!" );
2439 DriverMED_R_SMESHDS_Mesh myReader;
2440 myReader.SetFile( meshfile.ToCString() );
2442 // For PAL13473 ("Repetitive mesh") implementation.
2443 // New dependencies between SMESH objects are established:
2444 // now hypotheses can refer to meshes, shapes and other hypotheses.
2445 // To keep data consistent, the following order of data restoration
2447 // 1. Create hypotheses
2448 // 2. Create all meshes
2449 // 3. Load hypotheses' data
2452 list< pair< SMESH_Hypothesis_i*, string > > hypDataList;
2453 list< pair< SMESH_Mesh_i*, HDFgroup* > > meshGroupList;
2455 // get total number of top-level groups
2456 int aNbGroups = aFile->nInternalObjects();
2457 if ( aNbGroups > 0 ) {
2458 // --> in first turn we should read&create hypotheses
2459 if ( aFile->ExistInternalObject( "Hypotheses" ) ) {
2460 // open hypotheses root HDF group
2461 aTopGroup = new HDFgroup( "Hypotheses", aFile );
2462 aTopGroup->OpenOnDisk();
2464 // get number of hypotheses
2465 int aNbObjects = aTopGroup->nInternalObjects();
2466 for ( int j = 0; j < aNbObjects; j++ ) {
2467 // try to identify hypothesis
2468 char hypGrpName[ HDF_NAME_MAX_LEN+1 ];
2469 aTopGroup->InternalObjectIndentify( j, hypGrpName );
2471 if ( string( hypGrpName ).substr( 0, 10 ) == string( "Hypothesis" ) ) {
2472 // open hypothesis group
2473 aGroup = new HDFgroup( hypGrpName, aTopGroup );
2474 aGroup->OpenOnDisk();
2476 // --> get hypothesis id
2477 int id = atoi( string( hypGrpName ).substr( 10 ).c_str() );
2482 // get number of datasets
2483 int aNbSubObjects = aGroup->nInternalObjects();
2484 for ( int k = 0; k < aNbSubObjects; k++ ) {
2486 char name_of_subgroup[ HDF_NAME_MAX_LEN+1 ];
2487 aGroup->InternalObjectIndentify( k, name_of_subgroup );
2488 // --> get hypothesis name
2489 if ( strcmp( name_of_subgroup, "Name" ) == 0 ) {
2490 aDataset = new HDFdataset( name_of_subgroup, aGroup );
2491 aDataset->OpenOnDisk();
2492 size = aDataset->GetSize();
2493 char* hypname_str = new char[ size ];
2494 aDataset->ReadFromDisk( hypname_str );
2495 hypname = string( hypname_str );
2496 delete [] hypname_str;
2497 aDataset->CloseOnDisk();
2499 // --> get hypothesis plugin library name
2500 if ( strcmp( name_of_subgroup, "LibName" ) == 0 ) {
2501 aDataset = new HDFdataset( name_of_subgroup, aGroup );
2502 aDataset->OpenOnDisk();
2503 size = aDataset->GetSize();
2504 char* libname_str = new char[ size ];
2505 aDataset->ReadFromDisk( libname_str );
2506 if(MYDEBUG) SCRUTE( libname_str );
2507 libname = string( libname_str );
2508 delete [] libname_str;
2509 aDataset->CloseOnDisk();
2511 // --> get hypothesis data
2512 if ( strcmp( name_of_subgroup, "Data" ) == 0 ) {
2513 aDataset = new HDFdataset( name_of_subgroup, aGroup );
2514 aDataset->OpenOnDisk();
2515 size = aDataset->GetSize();
2516 char* hypdata_str = new char[ size ];
2517 aDataset->ReadFromDisk( hypdata_str );
2518 hypdata = string( hypdata_str );
2519 delete [] hypdata_str;
2520 aDataset->CloseOnDisk();
2523 // close hypothesis HDF group
2524 aGroup->CloseOnDisk();
2526 // --> restore hypothesis from data
2527 if ( id > 0 && !hypname.empty()/* && !hypdata.empty()*/ ) { // VSR : persistent data can be empty
2528 if(MYDEBUG) MESSAGE("VSR - load hypothesis : id = " << id <<
2529 ", name = " << hypname.c_str() << ", persistent string = " << hypdata.c_str());
2530 SMESH::SMESH_Hypothesis_var myHyp;
2532 try { // protect persistence mechanism against exceptions
2533 myHyp = this->createHypothesis( hypname.c_str(), libname.c_str() );
2536 INFOS( "Exception during hypothesis creation" );
2539 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
2541 // myImpl->LoadFrom( hypdata.c_str() );
2542 hypDataList.push_back( make_pair( myImpl, hypdata ));
2543 string iorString = GetORB()->object_to_string( myHyp );
2544 int newId = myStudyContext->findId( iorString );
2545 myStudyContext->mapOldToNew( id, newId );
2548 if(MYDEBUG) MESSAGE( "VSR - SMESH_Gen::Load - can't get servant" );
2552 // close hypotheses root HDF group
2553 aTopGroup->CloseOnDisk();
2557 // --> then we should read&create algorithms
2558 if ( aFile->ExistInternalObject( "Algorithms" ) ) {
2559 // open algorithms root HDF group
2560 aTopGroup = new HDFgroup( "Algorithms", aFile );
2561 aTopGroup->OpenOnDisk();
2563 // get number of algorithms
2564 int aNbObjects = aTopGroup->nInternalObjects();
2565 for ( int j = 0; j < aNbObjects; j++ ) {
2566 // try to identify algorithm
2567 char hypGrpName[ HDF_NAME_MAX_LEN+1 ];
2568 aTopGroup->InternalObjectIndentify( j, hypGrpName );
2570 if ( string( hypGrpName ).substr( 0, 9 ) == string( "Algorithm" ) ) {
2571 // open algorithm group
2572 aGroup = new HDFgroup( hypGrpName, aTopGroup );
2573 aGroup->OpenOnDisk();
2575 // --> get algorithm id
2576 int id = atoi( string( hypGrpName ).substr( 9 ).c_str() );
2581 // get number of datasets
2582 int aNbSubObjects = aGroup->nInternalObjects();
2583 for ( int k = 0; k < aNbSubObjects; k++ ) {
2585 char name_of_subgroup[ HDF_NAME_MAX_LEN+1 ];
2586 aGroup->InternalObjectIndentify( k, name_of_subgroup );
2587 // --> get algorithm name
2588 if ( strcmp( name_of_subgroup, "Name" ) == 0 ) {
2589 aDataset = new HDFdataset( name_of_subgroup, aGroup );
2590 aDataset->OpenOnDisk();
2591 size = aDataset->GetSize();
2592 char* hypname_str = new char[ size ];
2593 aDataset->ReadFromDisk( hypname_str );
2594 hypname = string( hypname_str );
2595 delete [] hypname_str;
2596 aDataset->CloseOnDisk();
2598 // --> get algorithm plugin library name
2599 if ( strcmp( name_of_subgroup, "LibName" ) == 0 ) {
2600 aDataset = new HDFdataset( name_of_subgroup, aGroup );
2601 aDataset->OpenOnDisk();
2602 size = aDataset->GetSize();
2603 char* libname_str = new char[ size ];
2604 aDataset->ReadFromDisk( libname_str );
2605 if(MYDEBUG) SCRUTE( libname_str );
2606 libname = string( libname_str );
2607 delete [] libname_str;
2608 aDataset->CloseOnDisk();
2610 // --> get algorithm data
2611 if ( strcmp( name_of_subgroup, "Data" ) == 0 ) {
2612 aDataset = new HDFdataset( name_of_subgroup, aGroup );
2613 aDataset->OpenOnDisk();
2614 size = aDataset->GetSize();
2615 char* hypdata_str = new char[ size ];
2616 aDataset->ReadFromDisk( hypdata_str );
2617 if(MYDEBUG) SCRUTE( hypdata_str );
2618 hypdata = string( hypdata_str );
2619 delete [] hypdata_str;
2620 aDataset->CloseOnDisk();
2623 // close algorithm HDF group
2624 aGroup->CloseOnDisk();
2626 // --> restore algorithm from data
2627 if ( id > 0 && !hypname.empty()/* && !hypdata.empty()*/ ) { // VSR : persistent data can be empty
2628 if(MYDEBUG) MESSAGE("VSR - load algo : id = " << id <<
2629 ", name = " << hypname.c_str() << ", persistent string = " << hypdata.c_str());
2630 SMESH::SMESH_Hypothesis_var myHyp;
2632 try { // protect persistence mechanism against exceptions
2633 myHyp = this->createHypothesis( hypname.c_str(), libname.c_str() );
2636 INFOS( "Exception during hypothesis creation" );
2639 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
2641 //myImpl->LoadFrom( hypdata.c_str() );
2642 hypDataList.push_back( make_pair( myImpl, hypdata ));
2643 string iorString = GetORB()->object_to_string( myHyp );
2644 int newId = myStudyContext->findId( iorString );
2645 myStudyContext->mapOldToNew( id, newId );
2648 if(MYDEBUG) MESSAGE( "VSR - SMESH_Gen::Load - can't get servant" );
2652 // close algorithms root HDF group
2653 aTopGroup->CloseOnDisk();
2657 // --> the rest groups should be meshes
2658 for ( int i = 0; i < aNbGroups; i++ ) {
2659 // identify next group
2660 char meshName[ HDF_NAME_MAX_LEN+1 ];
2661 aFile->InternalObjectIndentify( i, meshName );
2663 if ( string( meshName ).substr( 0, 4 ) == string( "Mesh" ) ) {
2665 int id = atoi( string( meshName ).substr( 4 ).c_str() );
2669 // open mesh HDF group
2670 aTopGroup = new HDFgroup( meshName, aFile );
2671 aTopGroup->OpenOnDisk();
2673 // get number of child HDF objects
2674 int aNbObjects = aTopGroup->nInternalObjects();
2675 if ( aNbObjects > 0 ) {
2677 if(MYDEBUG) MESSAGE( "VSR - load mesh : id = " << id );
2678 SMESH::SMESH_Mesh_var myNewMesh = this->createMesh();
2679 SMESH_Mesh_i* myNewMeshImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( myNewMesh ).in() );
2680 if ( !myNewMeshImpl )
2682 meshGroupList.push_back( make_pair( myNewMeshImpl, aTopGroup ));
2684 string iorString = GetORB()->object_to_string( myNewMesh );
2685 int newId = myStudyContext->findId( iorString );
2686 myStudyContext->mapOldToNew( id, newId );
2688 // try to read and set reference to shape
2689 GEOM::GEOM_Object_var aShapeObject;
2690 if ( aTopGroup->ExistInternalObject( "Ref on shape" ) ) {
2691 // load mesh "Ref on shape" - it's an entry to SObject
2692 aDataset = new HDFdataset( "Ref on shape", aTopGroup );
2693 aDataset->OpenOnDisk();
2694 size = aDataset->GetSize();
2695 char* refFromFile = new char[ size ];
2696 aDataset->ReadFromDisk( refFromFile );
2697 aDataset->CloseOnDisk();
2698 if ( strlen( refFromFile ) > 0 ) {
2699 SALOMEDS::SObject_var shapeSO = myCurrentStudy->FindObjectID( refFromFile );
2701 // Make sure GEOM data are loaded first
2702 //loadGeomData( shapeSO->GetFatherComponent() );
2704 CORBA::Object_var shapeObject = SObjectToObject( shapeSO );
2705 if ( !CORBA::is_nil( shapeObject ) ) {
2706 aShapeObject = GEOM::GEOM_Object::_narrow( shapeObject );
2707 if ( !aShapeObject->_is_nil() )
2708 myNewMeshImpl->SetShape( aShapeObject );
2717 // As all object that can be referred by hypothesis are created,
2718 // we can restore hypothesis data
2720 list< pair< SMESH_Hypothesis_i*, string > >::iterator hyp_data;
2721 for ( hyp_data = hypDataList.begin(); hyp_data != hypDataList.end(); ++hyp_data )
2723 SMESH_Hypothesis_i* hyp = hyp_data->first;
2724 string & data = hyp_data->second;
2725 hyp->LoadFrom( data.c_str() );
2728 // Restore the rest mesh data
2730 list< pair< SMESH_Mesh_i*, HDFgroup* > >::iterator meshi_group;
2731 for ( meshi_group = meshGroupList.begin(); meshi_group != meshGroupList.end(); ++meshi_group )
2733 aTopGroup = meshi_group->second;
2734 SMESH_Mesh_i* myNewMeshImpl = meshi_group->first;
2735 ::SMESH_Mesh& myLocMesh = myNewMeshImpl->GetImpl();
2736 SMESHDS_Mesh* mySMESHDSMesh = myLocMesh.GetMeshDS();
2738 GEOM::GEOM_Object_var aShapeObject = myNewMeshImpl->GetShapeToMesh();
2739 bool hasData = false;
2742 string iorString = GetORB()->object_to_string( myNewMeshImpl->_this() );
2743 int newId = myStudyContext->findId( iorString );
2744 int id = myStudyContext->getOldId( newId );
2746 // try to find mesh data dataset
2747 if ( aTopGroup->ExistInternalObject( "Has data" ) ) {
2748 // load mesh "has data" flag
2749 aDataset = new HDFdataset( "Has data", aTopGroup );
2750 aDataset->OpenOnDisk();
2751 size = aDataset->GetSize();
2752 char* strHasData = new char[ size ];
2753 aDataset->ReadFromDisk( strHasData );
2754 aDataset->CloseOnDisk();
2755 if ( strcmp( strHasData, "1") == 0 ) {
2756 // read mesh data from MED file
2757 myReader.SetMesh( mySMESHDSMesh );
2758 myReader.SetMeshId( id );
2764 // try to get applied algorithms
2765 if ( aTopGroup->ExistInternalObject( "Applied Algorithms" ) ) {
2766 aGroup = new HDFgroup( "Applied Algorithms", aTopGroup );
2767 aGroup->OpenOnDisk();
2768 // get number of applied algorithms
2769 int aNbSubObjects = aGroup->nInternalObjects();
2770 if(MYDEBUG) MESSAGE( "VSR - number of applied algos " << aNbSubObjects );
2771 for ( int j = 0; j < aNbSubObjects; j++ ) {
2772 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
2773 aGroup->InternalObjectIndentify( j, name_dataset );
2774 // check if it is an algorithm
2775 if ( string( name_dataset ).substr( 0, 4 ) == string( "Algo" ) ) {
2776 aDataset = new HDFdataset( name_dataset, aGroup );
2777 aDataset->OpenOnDisk();
2778 size = aDataset->GetSize();
2779 char* refFromFile = new char[ size ];
2780 aDataset->ReadFromDisk( refFromFile );
2781 aDataset->CloseOnDisk();
2783 // san - it is impossible to recover applied algorithms using their entries within Load() method
2785 //SALOMEDS::SObject_var hypSO = myCurrentStudy->FindObjectID( refFromFile );
2786 //CORBA::Object_var hypObject = SObjectToObject( hypSO );
2787 int id = atoi( refFromFile );
2788 string anIOR = myStudyContext->getIORbyOldId( id );
2789 if ( !anIOR.empty() ) {
2790 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
2791 if ( !CORBA::is_nil( hypObject ) ) {
2792 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
2793 if ( !anHyp->_is_nil() && !aShapeObject->_is_nil() )
2794 myNewMeshImpl->addHypothesis( aShapeObject, anHyp );
2799 aGroup->CloseOnDisk();
2802 // try to get applied hypotheses
2803 if ( aTopGroup->ExistInternalObject( "Applied Hypotheses" ) ) {
2804 aGroup = new HDFgroup( "Applied Hypotheses", aTopGroup );
2805 aGroup->OpenOnDisk();
2806 // get number of applied hypotheses
2807 int aNbSubObjects = aGroup->nInternalObjects();
2808 for ( int j = 0; j < aNbSubObjects; j++ ) {
2809 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
2810 aGroup->InternalObjectIndentify( j, name_dataset );
2811 // check if it is a hypothesis
2812 if ( string( name_dataset ).substr( 0, 3 ) == string( "Hyp" ) ) {
2813 aDataset = new HDFdataset( name_dataset, aGroup );
2814 aDataset->OpenOnDisk();
2815 size = aDataset->GetSize();
2816 char* refFromFile = new char[ size ];
2817 aDataset->ReadFromDisk( refFromFile );
2818 aDataset->CloseOnDisk();
2820 // san - it is impossible to recover applied hypotheses using their entries within Load() method
2822 //SALOMEDS::SObject_var hypSO = myCurrentStudy->FindObjectID( refFromFile );
2823 //CORBA::Object_var hypObject = SObjectToObject( hypSO );
2824 int id = atoi( refFromFile );
2825 string anIOR = myStudyContext->getIORbyOldId( id );
2826 if ( !anIOR.empty() ) {
2827 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
2828 if ( !CORBA::is_nil( hypObject ) ) {
2829 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
2830 if ( !anHyp->_is_nil() && !aShapeObject->_is_nil() )
2831 myNewMeshImpl->addHypothesis( aShapeObject, anHyp );
2836 aGroup->CloseOnDisk();
2839 // --> try to find submeshes containers for each type of submesh
2840 for ( int j = GetSubMeshOnVertexTag(); j <= GetSubMeshOnCompoundTag(); j++ ) {
2841 char name_meshgroup[ 30 ];
2842 if ( j == GetSubMeshOnVertexTag() )
2843 strcpy( name_meshgroup, "SubMeshes On Vertex" );
2844 else if ( j == GetSubMeshOnEdgeTag() )
2845 strcpy( name_meshgroup, "SubMeshes On Edge" );
2846 else if ( j == GetSubMeshOnWireTag() )
2847 strcpy( name_meshgroup, "SubMeshes On Wire" );
2848 else if ( j == GetSubMeshOnFaceTag() )
2849 strcpy( name_meshgroup, "SubMeshes On Face" );
2850 else if ( j == GetSubMeshOnShellTag() )
2851 strcpy( name_meshgroup, "SubMeshes On Shell" );
2852 else if ( j == GetSubMeshOnSolidTag() )
2853 strcpy( name_meshgroup, "SubMeshes On Solid" );
2854 else if ( j == GetSubMeshOnCompoundTag() )
2855 strcpy( name_meshgroup, "SubMeshes On Compound" );
2857 // try to get submeshes container HDF group
2858 if ( aTopGroup->ExistInternalObject( name_meshgroup ) ) {
2859 // open submeshes containers HDF group
2860 aGroup = new HDFgroup( name_meshgroup, aTopGroup );
2861 aGroup->OpenOnDisk();
2863 // get number of submeshes
2864 int aNbSubMeshes = aGroup->nInternalObjects();
2865 for ( int k = 0; k < aNbSubMeshes; k++ ) {
2867 char name_submeshgroup[ HDF_NAME_MAX_LEN+1 ];
2868 aGroup->InternalObjectIndentify( k, name_submeshgroup );
2869 if ( string( name_submeshgroup ).substr( 0, 7 ) == string( "SubMesh" ) ) {
2870 // --> get submesh id
2871 int subid = atoi( string( name_submeshgroup ).substr( 7 ).c_str() );
2874 // open submesh HDF group
2875 aSubGroup = new HDFgroup( name_submeshgroup, aGroup );
2876 aSubGroup->OpenOnDisk();
2878 // try to read and set reference to subshape
2879 GEOM::GEOM_Object_var aSubShapeObject;
2880 SMESH::SMESH_subMesh_var aSubMesh;
2882 if ( aSubGroup->ExistInternalObject( "Ref on shape" ) ) {
2883 // load submesh "Ref on shape" - it's an entry to SObject
2884 aDataset = new HDFdataset( "Ref on shape", aSubGroup );
2885 aDataset->OpenOnDisk();
2886 size = aDataset->GetSize();
2887 char* refFromFile = new char[ size ];
2888 aDataset->ReadFromDisk( refFromFile );
2889 aDataset->CloseOnDisk();
2890 if ( strlen( refFromFile ) > 0 ) {
2891 SALOMEDS::SObject_var subShapeSO = myCurrentStudy->FindObjectID( refFromFile );
2892 CORBA::Object_var subShapeObject = SObjectToObject( subShapeSO );
2893 if ( !CORBA::is_nil( subShapeObject ) ) {
2894 aSubShapeObject = GEOM::GEOM_Object::_narrow( subShapeObject );
2895 if ( !aSubShapeObject->_is_nil() )
2896 aSubMesh = SMESH::SMESH_subMesh::_duplicate
2897 ( myNewMeshImpl->createSubMesh( aSubShapeObject ) );
2898 if ( aSubMesh->_is_nil() )
2900 string iorSubString = GetORB()->object_to_string( aSubMesh );
2901 int newSubId = myStudyContext->findId( iorSubString );
2902 myStudyContext->mapOldToNew( subid, newSubId );
2907 if ( aSubMesh->_is_nil() )
2910 // VSR: Get submesh data from MED convertor
2911 // int anInternalSubmeshId = aSubMesh->GetId(); // this is not a persistent ID, it's an internal one computed from sub-shape
2912 // if (myNewMeshImpl->_mapSubMesh.find(anInternalSubmeshId) != myNewMeshImpl->_mapSubMesh.end()) {
2913 // if(MYDEBUG) MESSAGE("VSR - SMESH_Gen_i::Load(): loading from MED file submesh with ID = " <<
2914 // subid << " for subshape # " << anInternalSubmeshId);
2915 // SMESHDS_SubMesh* aSubMeshDS =
2916 // myNewMeshImpl->_mapSubMesh[anInternalSubmeshId]->CreateSubMeshDS();
2917 // if ( !aSubMeshDS ) {
2918 // if(MYDEBUG) MESSAGE("VSR - SMESH_Gen_i::Load(): FAILED to create a submesh for subshape # " <<
2919 // anInternalSubmeshId << " in current mesh!");
2922 // myReader.GetSubMesh( aSubMeshDS, subid );
2925 // try to get applied algorithms
2926 if ( aSubGroup->ExistInternalObject( "Applied Algorithms" ) ) {
2927 // open "applied algorithms" HDF group
2928 aSubSubGroup = new HDFgroup( "Applied Algorithms", aSubGroup );
2929 aSubSubGroup->OpenOnDisk();
2930 // get number of applied algorithms
2931 int aNbSubObjects = aSubSubGroup->nInternalObjects();
2932 for ( int l = 0; l < aNbSubObjects; l++ ) {
2933 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
2934 aSubSubGroup->InternalObjectIndentify( l, name_dataset );
2935 // check if it is an algorithm
2936 if ( string( name_dataset ).substr( 0, 4 ) == string( "Algo" ) ) {
2937 aDataset = new HDFdataset( name_dataset, aSubSubGroup );
2938 aDataset->OpenOnDisk();
2939 size = aDataset->GetSize();
2940 char* refFromFile = new char[ size ];
2941 aDataset->ReadFromDisk( refFromFile );
2942 aDataset->CloseOnDisk();
2944 //SALOMEDS::SObject_var hypSO = myCurrentStudy->FindObjectID( refFromFile );
2945 //CORBA::Object_var hypObject = SObjectToObject( hypSO );
2946 int id = atoi( refFromFile );
2947 string anIOR = myStudyContext->getIORbyOldId( id );
2948 if ( !anIOR.empty() ) {
2949 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
2950 if ( !CORBA::is_nil( hypObject ) ) {
2951 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
2952 if ( !anHyp->_is_nil() && !aShapeObject->_is_nil() )
2953 myNewMeshImpl->addHypothesis( aSubShapeObject, anHyp );
2958 // close "applied algorithms" HDF group
2959 aSubSubGroup->CloseOnDisk();
2962 // try to get applied hypotheses
2963 if ( aSubGroup->ExistInternalObject( "Applied Hypotheses" ) ) {
2964 // open "applied hypotheses" HDF group
2965 aSubSubGroup = new HDFgroup( "Applied Hypotheses", aSubGroup );
2966 aSubSubGroup->OpenOnDisk();
2967 // get number of applied hypotheses
2968 int aNbSubObjects = aSubSubGroup->nInternalObjects();
2969 for ( int l = 0; l < aNbSubObjects; l++ ) {
2970 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
2971 aSubSubGroup->InternalObjectIndentify( l, name_dataset );
2972 // check if it is a hypothesis
2973 if ( string( name_dataset ).substr( 0, 3 ) == string( "Hyp" ) ) {
2974 aDataset = new HDFdataset( name_dataset, aSubSubGroup );
2975 aDataset->OpenOnDisk();
2976 size = aDataset->GetSize();
2977 char* refFromFile = new char[ size ];
2978 aDataset->ReadFromDisk( refFromFile );
2979 aDataset->CloseOnDisk();
2981 //SALOMEDS::SObject_var hypSO = myCurrentStudy->FindObjectID( refFromFile );
2982 //CORBA::Object_var hypObject = SObjectToObject( hypSO );
2983 int id = atoi( refFromFile );
2984 string anIOR = myStudyContext->getIORbyOldId( id );
2985 if ( !anIOR.empty() ) {
2986 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
2987 if ( !CORBA::is_nil( hypObject ) ) {
2988 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
2989 if ( !anHyp->_is_nil() && !aShapeObject->_is_nil() )
2990 myNewMeshImpl->addHypothesis( aSubShapeObject, anHyp );
2995 // close "applied hypotheses" HDF group
2996 aSubSubGroup->CloseOnDisk();
2999 // close submesh HDF group
3000 aSubGroup->CloseOnDisk();
3003 // close submeshes containers HDF group
3004 aGroup->CloseOnDisk();
3010 // Read sub-meshes from MED
3011 // -------------------------
3012 if(MYDEBUG) MESSAGE("Create all sub-meshes");
3013 bool submeshesInFamilies = ( ! aTopGroup->ExistInternalObject( "Submeshes" ));
3014 if ( submeshesInFamilies )
3016 // old way working before fix of PAL 12992
3017 myReader.CreateAllSubMeshes();
3022 aGroup = new HDFgroup( "Submeshes", aTopGroup );
3023 aGroup->OpenOnDisk();
3025 int maxID = mySMESHDSMesh->MaxShapeIndex();
3026 vector< SMESHDS_SubMesh * > subMeshes( maxID + 1, (SMESHDS_SubMesh*) 0 );
3027 vector< TopAbs_ShapeEnum > smType ( maxID + 1, TopAbs_SHAPE );
3029 PositionCreator aPositionCreator;
3031 SMDS_NodeIteratorPtr nIt = mySMESHDSMesh->nodesIterator();
3032 SMDS_ElemIteratorPtr eIt = mySMESHDSMesh->elementsIterator();
3033 for ( int isNode = 0; isNode < 2; ++isNode )
3035 string aDSName( isNode ? "Node Submeshes" : "Element Submeshes");
3036 if ( aGroup->ExistInternalObject( (char*) aDSName.c_str() ))
3038 aDataset = new HDFdataset( (char*) aDSName.c_str(), aGroup );
3039 aDataset->OpenOnDisk();
3040 // read submesh IDs for all elements sorted by ID
3041 int nbElems = aDataset->GetSize();
3042 int* smIDs = new int [ nbElems ];
3043 aDataset->ReadFromDisk( smIDs );
3044 aDataset->CloseOnDisk();
3046 // get elements sorted by ID
3047 TIDSortedElemSet elemSet;
3049 while ( nIt->more() ) elemSet.insert( nIt->next() );
3051 while ( eIt->more() ) elemSet.insert( eIt->next() );
3052 ASSERT( elemSet.size() == nbElems );
3054 // add elements to submeshes
3055 TIDSortedElemSet::iterator iE = elemSet.begin();
3056 for ( int i = 0; i < nbElems; ++i, ++iE )
3058 int smID = smIDs[ i ];
3059 if ( smID == 0 ) continue;
3060 ASSERT( smID <= maxID );
3061 const SMDS_MeshElement* elem = *iE;
3062 // get or create submesh
3063 SMESHDS_SubMesh* & sm = subMeshes[ smID ];
3065 sm = mySMESHDSMesh->NewSubMesh( smID );
3066 smType[ smID ] = mySMESHDSMesh->IndexToShape( smID ).ShapeType();
3070 SMDS_PositionPtr pos = aPositionCreator.MakePosition( smType[ smID ]);
3071 pos->SetShapeId( smID );
3072 SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>( static_cast<const SMDS_MeshNode*>( elem ));
3073 node->SetPosition( pos );
3074 sm->AddNode( node );
3076 sm->AddElement( elem );
3082 } // end reading submeshes
3084 // Read node positions on sub-shapes (SMDS_Position)
3086 if ( aTopGroup->ExistInternalObject( "Node Positions" ))
3088 // There are 5 datasets to read:
3089 // "Nodes on Edges" - ID of node on edge
3090 // "Edge positions" - U parameter on node on edge
3091 // "Nodes on Faces" - ID of node on face
3092 // "Face U positions" - U parameter of node on face
3093 // "Face V positions" - V parameter of node on face
3094 char* aEid_DSName = "Nodes on Edges";
3095 char* aEu_DSName = "Edge positions";
3096 char* aFu_DSName = "Face U positions";
3097 //char* aFid_DSName = "Nodes on Faces";
3098 //char* aFv_DSName = "Face V positions";
3101 int nbEids = 0, nbFids = 0;
3102 int *aEids = 0, *aFids = 0;
3103 double *aEpos = 0, *aFupos = 0, *aFvpos = 0;
3106 aGroup = new HDFgroup( "Node Positions", aTopGroup );
3107 aGroup->OpenOnDisk();
3109 // loop on 5 data sets
3110 int aNbObjects = aGroup->nInternalObjects();
3111 for ( int i = 0; i < aNbObjects; i++ )
3114 char aDSName[ HDF_NAME_MAX_LEN+1 ];
3115 aGroup->InternalObjectIndentify( i, aDSName );
3117 aDataset = new HDFdataset( aDSName, aGroup );
3118 aDataset->OpenOnDisk();
3119 if ( aDataset->GetType() == HDF_FLOAT64 ) // Positions
3121 double* pos = new double [ aDataset->GetSize() ];
3122 aDataset->ReadFromDisk( pos );
3124 if ( strncmp( aDSName, aEu_DSName, strlen( aEu_DSName )) == 0 )
3126 else if ( strncmp( aDSName, aFu_DSName, strlen( aFu_DSName )) == 0 )
3133 int aSize = aDataset->GetSize();
3135 // for reading files, created from 18.07.2005 till 10.10.2005
3136 if (aDataset->GetType() == HDF_STRING)
3137 aSize /= sizeof(int);
3139 int* ids = new int [aSize];
3140 aDataset->ReadFromDisk( ids );
3141 // on face or nodes?
3142 if ( strncmp( aDSName, aEid_DSName, strlen( aEid_DSName )) == 0 ) {
3151 aDataset->CloseOnDisk();
3152 } // loop on 5 datasets
3154 // Set node positions on edges or faces
3155 for ( int onFace = 0; onFace < 2; onFace++ )
3157 int nbNodes = ( onFace ? nbFids : nbEids );
3158 if ( nbNodes == 0 ) continue;
3159 int* aNodeIDs = ( onFace ? aFids : aEids );
3160 double* aUPos = ( onFace ? aFupos : aEpos );
3161 double* aVPos = ( onFace ? aFvpos : 0 );
3163 for ( int iNode = 0; iNode < nbNodes; iNode++ )
3165 const SMDS_MeshNode* node = mySMESHDSMesh->FindNode( aNodeIDs[ iNode ]);
3167 SMDS_PositionPtr aPos = node->GetPosition();
3170 ASSERT( aPos->GetTypeOfPosition() == SMDS_TOP_FACE );
3171 SMDS_FacePosition* fPos = const_cast<SMDS_FacePosition*>
3172 ( static_cast<const SMDS_FacePosition*>( aPos.get() ));
3173 fPos->SetUParameter( aUPos[ iNode ]);
3174 fPos->SetVParameter( aVPos[ iNode ]);
3177 ASSERT( aPos->GetTypeOfPosition() == SMDS_TOP_EDGE );
3178 SMDS_EdgePosition* fPos = const_cast<SMDS_EdgePosition*>
3179 ( static_cast<const SMDS_EdgePosition*>( aPos.get() ));
3180 fPos->SetUParameter( aUPos[ iNode ]);
3184 if ( aEids ) delete [] aEids;
3185 if ( aFids ) delete [] aFids;
3186 if ( aEpos ) delete [] aEpos;
3187 if ( aFupos ) delete [] aFupos;
3188 if ( aFvpos ) delete [] aFvpos;
3190 aGroup->CloseOnDisk();
3192 } // if ( aTopGroup->ExistInternalObject( "Node Positions" ) )
3195 // Recompute State (as computed sub-meshes are restored from MED)
3196 if ( !aShapeObject->_is_nil() ) {
3197 MESSAGE("Compute State Engine ...");
3198 TopoDS_Shape myLocShape = GeomObjectToShape( aShapeObject );
3199 myNewMeshImpl->GetImpl().GetSubMesh(myLocShape)->ComputeStateEngine
3200 (SMESH_subMesh::SUBMESH_RESTORED);
3201 MESSAGE("Compute State Engine finished");
3204 // try to get groups
3205 for ( int ii = GetNodeGroupsTag(); ii <= GetVolumeGroupsTag(); ii++ ) {
3206 char name_group[ 30 ];
3207 if ( ii == GetNodeGroupsTag() )
3208 strcpy( name_group, "Groups of Nodes" );
3209 else if ( ii == GetEdgeGroupsTag() )
3210 strcpy( name_group, "Groups of Edges" );
3211 else if ( ii == GetFaceGroupsTag() )
3212 strcpy( name_group, "Groups of Faces" );
3213 else if ( ii == GetVolumeGroupsTag() )
3214 strcpy( name_group, "Groups of Volumes" );
3216 if ( aTopGroup->ExistInternalObject( name_group ) ) {
3217 aGroup = new HDFgroup( name_group, aTopGroup );
3218 aGroup->OpenOnDisk();
3219 // get number of groups
3220 int aNbSubObjects = aGroup->nInternalObjects();
3221 for ( int j = 0; j < aNbSubObjects; j++ ) {
3222 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
3223 aGroup->InternalObjectIndentify( j, name_dataset );
3224 // check if it is an group
3225 if ( string( name_dataset ).substr( 0, 5 ) == string( "Group" ) ) {
3227 int subid = atoi( string( name_dataset ).substr( 5 ).c_str() );
3230 aDataset = new HDFdataset( name_dataset, aGroup );
3231 aDataset->OpenOnDisk();
3233 // Retrieve actual group name
3234 size = aDataset->GetSize();
3235 char* nameFromFile = new char[ size ];
3236 aDataset->ReadFromDisk( nameFromFile );
3237 aDataset->CloseOnDisk();
3239 // Try to find a shape reference
3240 TopoDS_Shape aShape;
3241 char aRefName[ 30 ];
3242 sprintf( aRefName, "Ref on shape %d", subid);
3243 if ( aGroup->ExistInternalObject( aRefName ) ) {
3244 // load mesh "Ref on shape" - it's an entry to SObject
3245 aDataset = new HDFdataset( aRefName, aGroup );
3246 aDataset->OpenOnDisk();
3247 size = aDataset->GetSize();
3248 char* refFromFile = new char[ size ];
3249 aDataset->ReadFromDisk( refFromFile );
3250 aDataset->CloseOnDisk();
3251 if ( strlen( refFromFile ) > 0 ) {
3252 SALOMEDS::SObject_var shapeSO = myCurrentStudy->FindObjectID( refFromFile );
3253 CORBA::Object_var shapeObject = SObjectToObject( shapeSO );
3254 if ( !CORBA::is_nil( shapeObject ) ) {
3255 aShapeObject = GEOM::GEOM_Object::_narrow( shapeObject );
3256 if ( !aShapeObject->_is_nil() )
3257 aShape = GeomObjectToShape( aShapeObject );
3261 // Create group servant
3262 SMESH::ElementType type = (SMESH::ElementType)(ii - GetNodeGroupsTag() + 1);
3263 SMESH::SMESH_GroupBase_var aNewGroup = SMESH::SMESH_GroupBase::_duplicate
3264 ( myNewMeshImpl->createGroup( type, nameFromFile, aShape ) );
3265 // Obtain a SMESHDS_Group object
3266 if ( aNewGroup->_is_nil() )
3269 string iorSubString = GetORB()->object_to_string( aNewGroup );
3270 int newSubId = myStudyContext->findId( iorSubString );
3271 myStudyContext->mapOldToNew( subid, newSubId );
3273 SMESH_GroupBase_i* aGroupImpl =
3274 dynamic_cast<SMESH_GroupBase_i*>( GetServant( aNewGroup ).in() );
3278 SMESH_Group* aLocalGroup = myLocMesh.GetGroup( aGroupImpl->GetLocalID() );
3282 SMESHDS_GroupBase* aGroupBaseDS = aLocalGroup->GetGroupDS();
3283 aGroupBaseDS->SetStoreName( name_dataset );
3285 // Fill group with contents from MED file
3286 SMESHDS_Group* aGrp = dynamic_cast<SMESHDS_Group*>( aGroupBaseDS );
3288 myReader.GetGroup( aGrp );
3291 aGroup->CloseOnDisk();
3297 aTopGroup->CloseOnDisk();
3300 aFile->CloseOnDisk();
3303 // Remove temporary files created from the stream
3305 SALOMEDS_Tool::RemoveTemporaryFiles( tmpDir.ToCString(), aFileSeq.in(), true );
3307 INFOS( "SMESH_Gen_i::Load completed" );
3311 //=============================================================================
3313 * SMESH_Gen_i::LoadASCII
3315 * Load SMESH module's data in ASCII format (not implemented yet)
3317 //=============================================================================
3319 bool SMESH_Gen_i::LoadASCII( SALOMEDS::SComponent_ptr theComponent,
3320 const SALOMEDS::TMPFile& theStream,
3322 bool isMultiFile ) {
3323 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::LoadASCII" );
3324 return Load( theComponent, theStream, theURL, isMultiFile );
3327 //=============================================================================
3329 * SMESH_Gen_i::Close
3331 * Clears study-connected data when it is closed
3333 //=============================================================================
3335 void SMESH_Gen_i::Close( SALOMEDS::SComponent_ptr theComponent )
3337 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Close" );
3339 // Clear study contexts data
3340 int studyId = GetCurrentStudyID();
3341 if ( myStudyContextMap.find( studyId ) != myStudyContextMap.end() ) {
3342 delete myStudyContextMap[ studyId ];
3343 myStudyContextMap.erase( studyId );
3348 //=============================================================================
3350 * SMESH_Gen_i::ComponentDataType
3352 * Get component data type
3354 //=============================================================================
3356 char* SMESH_Gen_i::ComponentDataType()
3358 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::ComponentDataType" );
3359 return CORBA::string_dup( "SMESH" );
3363 //=============================================================================
3365 * SMESH_Gen_i::IORToLocalPersistentID
3367 * Transform data from transient form to persistent
3369 //=============================================================================
3371 char* SMESH_Gen_i::IORToLocalPersistentID( SALOMEDS::SObject_ptr /*theSObject*/,
3372 const char* IORString,
3373 CORBA::Boolean /*isMultiFile*/,
3374 CORBA::Boolean /*isASCII*/ )
3376 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::IORToLocalPersistentID" );
3377 StudyContext* myStudyContext = GetCurrentStudyContext();
3379 if ( myStudyContext && strcmp( IORString, "" ) != 0 ) {
3380 int anId = myStudyContext->findId( IORString );
3382 if(MYDEBUG) MESSAGE( "VSR " << anId )
3384 sprintf( strId, "%d", anId );
3385 return CORBA::string_dup( strId );
3388 return CORBA::string_dup( "" );
3391 //=============================================================================
3393 * SMESH_Gen_i::LocalPersistentIDToIOR
3395 * Transform data from persistent form to transient
3397 //=============================================================================
3399 char* SMESH_Gen_i::LocalPersistentIDToIOR( SALOMEDS::SObject_ptr /*theSObject*/,
3400 const char* aLocalPersistentID,
3401 CORBA::Boolean /*isMultiFile*/,
3402 CORBA::Boolean /*isASCII*/ )
3404 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::LocalPersistentIDToIOR(): id = " << aLocalPersistentID );
3405 StudyContext* myStudyContext = GetCurrentStudyContext();
3407 if ( myStudyContext && strcmp( aLocalPersistentID, "" ) != 0 ) {
3408 int anId = atoi( aLocalPersistentID );
3409 return CORBA::string_dup( myStudyContext->getIORbyOldId( anId ).c_str() );
3411 return CORBA::string_dup( "" );
3414 //=======================================================================
3415 //function : RegisterObject
3417 //=======================================================================
3419 int SMESH_Gen_i::RegisterObject(CORBA::Object_ptr theObject)
3421 StudyContext* myStudyContext = GetCurrentStudyContext();
3422 if ( myStudyContext && !CORBA::is_nil( theObject )) {
3423 string iorString = GetORB()->object_to_string( theObject );
3424 return myStudyContext->addObject( iorString );
3429 //================================================================================
3431 * \brief Return id of registered object
3432 * \param theObject - the Object
3433 * \retval int - Object id
3435 //================================================================================
3437 int SMESH_Gen_i::GetObjectId(CORBA::Object_ptr theObject)
3439 StudyContext* myStudyContext = GetCurrentStudyContext();
3440 if ( myStudyContext && !CORBA::is_nil( theObject )) {
3441 string iorString = GetORB()->object_to_string( theObject );
3442 return myStudyContext->findId( iorString );
3447 //=============================================================================
3449 * SMESHEngine_factory
3451 * C factory, accessible with dlsym, after dlopen
3453 //=============================================================================
3457 PortableServer::ObjectId* SMESHEngine_factory( CORBA::ORB_ptr orb,
3458 PortableServer::POA_ptr poa,
3459 PortableServer::ObjectId* contId,
3460 const char* instanceName,
3461 const char* interfaceName )
3463 if(MYDEBUG) MESSAGE( "PortableServer::ObjectId* SMESHEngine_factory()" );
3464 if(MYDEBUG) SCRUTE(interfaceName);
3465 SMESH_Gen_i* aSMESHGen = new SMESH_Gen_i(orb, poa, contId, instanceName, interfaceName);
3466 return aSMESHGen->getId() ;