1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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
22 // SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
23 // File : SMESH_Gen_i.cxx
24 // Author : Paul RASCLE, EDF
28 #include <TopExp_Explorer.hxx>
30 #include <TopoDS_Iterator.hxx>
31 #include <TopoDS_Compound.hxx>
32 #include <TopoDS_CompSolid.hxx>
33 #include <TopoDS_Solid.hxx>
34 #include <TopoDS_Shell.hxx>
35 #include <TopoDS_Face.hxx>
36 #include <TopoDS_Wire.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopoDS_Vertex.hxx>
39 #include <TopoDS_Shape.hxx>
40 #include <TopTools_MapOfShape.hxx>
41 #include <TopTools_IndexedMapOfShape.hxx>
42 #include <TopTools_ListOfShape.hxx>
43 #include <TopTools_ListIteratorOfListOfShape.hxx>
45 #include <BRep_Tool.hxx>
46 #include <TCollection_AsciiString.hxx>
49 #include "Utils_CorbaException.hxx"
51 #include "utilities.h"
63 #define LibHandle HMODULE
64 #define LoadLib( name ) LoadLibrary( name )
65 #define GetProc GetProcAddress
66 #define UnLoadLib( handle ) FreeLibrary( handle );
68 #define LibHandle void*
69 #define LoadLib( name ) dlopen( name, RTLD_LAZY )
71 #define UnLoadLib( handle ) dlclose( handle );
76 #include "SMESH_Gen_i.hxx"
77 #include "SMESH_Mesh_i.hxx"
78 #include "SMESH_Hypothesis_i.hxx"
79 #include "SMESH_Algo_i.hxx"
80 #include "SMESH_Group_i.hxx"
81 #include "SMESH_PythonDump.hxx"
83 #include "SMESHDS_Document.hxx"
84 #include "SMESHDS_Group.hxx"
85 #include "SMESHDS_GroupOnGeom.hxx"
86 #include "SMESH_Mesh.hxx"
87 #include "SMESH_Hypothesis.hxx"
88 #include "SMESH_Group.hxx"
89 #include "SMESH_MeshEditor.hxx"
91 #include "SMDS_EdgePosition.hxx"
92 #include "SMDS_FacePosition.hxx"
93 #include "SMDS_PolyhedralVolumeOfNodes.hxx"
94 #include "SMDS_SetIterator.hxx"
95 #include "SMDS_SpacePosition.hxx"
96 #include "SMDS_VertexPosition.hxx"
98 #include CORBA_SERVER_HEADER(SMESH_Group)
99 #include CORBA_SERVER_HEADER(SMESH_Filter)
100 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
102 #include "DriverMED_W_SMESHDS_Mesh.h"
103 #include "DriverMED_R_SMESHDS_Mesh.h"
105 #include "SALOMEDS_Tool.hxx"
106 #include "SALOME_NamingService.hxx"
107 #include "SALOME_LifeCycleCORBA.hxx"
108 #include "Utils_SINGLETON.hxx"
109 #include "OpUtil.hxx"
111 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
112 #include CORBA_CLIENT_HEADER(SALOME_Session)
114 #include "GEOM_Client.hxx"
115 #include "Utils_ExceptHandlers.hxx"
117 #include "Basics_Utils.hxx"
122 using SMESH::TPythonDump;
124 #define NUM_TMP_FILES 2
127 static int MYDEBUG = 0;
129 static int MYDEBUG = 0;
132 // Static variables definition
133 GEOM::GEOM_Gen_var SMESH_Gen_i::myGeomGen = GEOM::GEOM_Gen::_nil();
134 CORBA::ORB_var SMESH_Gen_i::myOrb;
135 PortableServer::POA_var SMESH_Gen_i::myPoa;
136 SALOME_NamingService* SMESH_Gen_i::myNS = NULL;
137 SALOME_LifeCycleCORBA* SMESH_Gen_i::myLCC = NULL;
138 SMESH_Gen_i* SMESH_Gen_i::mySMESHGen = NULL;
141 const int nbElemPerDiagonal = 10;
143 //=============================================================================
145 * GetServant [ static ]
147 * Get servant of the CORBA object
149 //=============================================================================
151 PortableServer::ServantBase_var SMESH_Gen_i::GetServant( CORBA::Object_ptr theObject )
153 if( CORBA::is_nil( theObject ) || CORBA::is_nil( GetPOA() ) )
156 PortableServer::Servant aServant = GetPOA()->reference_to_servant( theObject );
160 INFOS( "GetServant - Unknown exception was caught!!!" );
165 //=============================================================================
167 * SObjectToObject [ static ]
169 * Get CORBA object corresponding to the SALOMEDS::SObject
171 //=============================================================================
173 CORBA::Object_var SMESH_Gen_i::SObjectToObject( SALOMEDS::SObject_ptr theSObject )
175 SALOMEDS::GenericAttribute_var anAttr;
176 CORBA::Object_var anObj;
177 if ( !theSObject->_is_nil() ) {
179 if( theSObject->FindAttribute( anAttr, "AttributeIOR" ) ) {
180 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow( anAttr );
181 CORBA::String_var aValue = anIOR->Value();
182 if( strcmp( aValue, "" ) != 0 )
183 anObj = GetORB()->string_to_object( aValue );
187 INFOS( "SObjectToObject - Unknown exception was caught!!!" );
193 //=============================================================================
197 * Get SALOME_NamingService object
199 //=============================================================================
201 SALOME_NamingService* SMESH_Gen_i::GetNS()
203 if ( myNS == NULL ) {
204 myNS = SINGLETON_<SALOME_NamingService>::Instance();
205 ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting());
206 myNS->init_orb( GetORB() );
211 //=============================================================================
215 * Get SALOME_LifeCycleCORBA object
217 //=============================================================================
218 SALOME_LifeCycleCORBA* SMESH_Gen_i::GetLCC() {
219 if ( myLCC == NULL ) {
220 myLCC = new SALOME_LifeCycleCORBA( GetNS() );
226 //=============================================================================
228 * GetGeomEngine [ static ]
230 * Get GEOM::GEOM_Gen reference
232 //=============================================================================
233 GEOM::GEOM_Gen_ptr SMESH_Gen_i::GetGeomEngine() {
234 //CCRT GEOM::GEOM_Gen_var aGeomEngine =
235 //CCRT GEOM::GEOM_Gen::_narrow( GetLCC()->FindOrLoad_Component("FactoryServer","GEOM") );
236 //CCRT return aGeomEngine._retn();
237 if(CORBA::is_nil(myGeomGen))
239 Engines::EngineComponent_ptr temp=GetLCC()->FindOrLoad_Component("FactoryServer","GEOM");
240 myGeomGen=GEOM::GEOM_Gen::_narrow(temp);
245 //=============================================================================
247 * SMESH_Gen_i::SMESH_Gen_i
249 * Default constructor: not for use
251 //=============================================================================
253 SMESH_Gen_i::SMESH_Gen_i()
255 INFOS( "SMESH_Gen_i::SMESH_Gen_i : default constructor" );
258 //=============================================================================
260 * SMESH_Gen_i::SMESH_Gen_i
262 * Standard constructor, used with Container
264 //=============================================================================
266 SMESH_Gen_i::SMESH_Gen_i( CORBA::ORB_ptr orb,
267 PortableServer::POA_ptr poa,
268 PortableServer::ObjectId* contId,
269 const char* instanceName,
270 const char* interfaceName )
271 : Engines_Component_i( orb, poa, contId, instanceName, interfaceName )
273 MESSAGE( "SMESH_Gen_i::SMESH_Gen_i : standard constructor" );
275 myOrb = CORBA::ORB::_duplicate(orb);
276 myPoa = PortableServer::POA::_duplicate(poa);
279 _id = myPoa->activate_object( _thisObj );
281 myIsEmbeddedMode = false;
282 myShapeReader = NULL; // shape reader
285 // set it in standalone mode only
286 //OSD::SetSignal( true );
288 // 0020605: EDF 1190 SMESH: Display performance. 80 seconds for 52000 cells.
289 // find out mode (embedded or standalone) here else
290 // meshes created before calling SMESH_Client::GetSMESHGen(), which calls
291 // SMESH_Gen_i::SetEmbeddedMode(), have wrong IsEmbeddedMode flag
292 if ( SALOME_NamingService* ns = GetNS() )
294 CORBA::Object_var obj = ns->Resolve( "/Kernel/Session" );
295 SALOME::Session_var session = SALOME::Session::_narrow( obj ) ;
296 if ( !session->_is_nil() )
298 CORBA::String_var s_host = session->getHostname();
299 CORBA::Long s_pid = session->getPID();
300 string my_host = Kernel_Utils::GetHostname();
302 long my_pid = (long)_getpid();
304 long my_pid = (long) getpid();
306 SetEmbeddedMode( s_pid == my_pid && my_host == s_host.in() );
311 //=============================================================================
313 * SMESH_Gen_i::~SMESH_Gen_i
317 //=============================================================================
319 SMESH_Gen_i::~SMESH_Gen_i()
321 INFOS( "SMESH_Gen_i::~SMESH_Gen_i" );
323 // delete hypothesis creators
324 map<string, GenericHypothesisCreator_i*>::iterator itHyp;
325 for (itHyp = myHypCreatorMap.begin(); itHyp != myHypCreatorMap.end(); itHyp++)
327 delete (*itHyp).second;
329 myHypCreatorMap.clear();
331 // Clear study contexts data
332 map<int, StudyContext*>::iterator it;
333 for ( it = myStudyContextMap.begin(); it != myStudyContextMap.end(); ++it ) {
336 myStudyContextMap.clear();
337 // delete shape reader
338 if ( !myShapeReader )
339 delete myShapeReader;
342 //=============================================================================
344 * SMESH_Gen_i::createHypothesis
346 * Create hypothesis of given type
348 //=============================================================================
349 SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::createHypothesis(const char* theHypName,
350 const char* theLibName)
351 throw (SALOME::SALOME_Exception)
353 /* It's Need to tranlate lib name for WIN32 or X platform */
354 char* aPlatformLibName = 0;
355 if ( theLibName && theLibName[0] != '\0' )
357 int libNameLen = strlen(theLibName);
358 //check for old format "libXXXXXXX.so"
359 if (libNameLen > 7 &&
360 !strncmp( theLibName, "lib", 3 ) &&
361 !strcmp( theLibName+libNameLen-3, ".so" ))
365 aPlatformLibName = new char[libNameLen - 1];
366 aPlatformLibName[0] = '\0';
367 aPlatformLibName = strncat( aPlatformLibName, theLibName+3, libNameLen-6 );
368 aPlatformLibName = strcat( aPlatformLibName, ".dll" );
369 aPlatformLibName[libNameLen - 2] = '\0';
371 aPlatformLibName = new char[ libNameLen + 1];
372 aPlatformLibName[0] = '\0';
373 aPlatformLibName = strcat( aPlatformLibName, theLibName );
374 aPlatformLibName[libNameLen] = '\0';
379 //try to use new format
381 aPlatformLibName = new char[ libNameLen + 5 ];
382 aPlatformLibName[0] = '\0';
383 aPlatformLibName = strcat( aPlatformLibName, theLibName );
384 aPlatformLibName = strcat( aPlatformLibName, ".dll" );
386 aPlatformLibName = new char[ libNameLen + 7 ];
387 aPlatformLibName[0] = '\0';
388 aPlatformLibName = strcat( aPlatformLibName, "lib" );
389 aPlatformLibName = strcat( aPlatformLibName, theLibName );
390 aPlatformLibName = strcat( aPlatformLibName, ".so" );
396 Unexpect aCatch(SALOME_SalomeException);
397 if(MYDEBUG) MESSAGE( "Create Hypothesis <" << theHypName << "> from " << aPlatformLibName/*theLibName*/);
399 // create a new hypothesis object servant
400 SMESH_Hypothesis_i* myHypothesis_i = 0;
401 SMESH::SMESH_Hypothesis_var hypothesis_i;
405 // check, if creator for this hypothesis type already exists
406 if (myHypCreatorMap.find(string(theHypName)) == myHypCreatorMap.end())
408 // load plugin library
409 if(MYDEBUG) MESSAGE("Loading server meshers plugin library ...");
410 LibHandle libHandle = LoadLib( aPlatformLibName/*theLibName*/ );
413 // report any error, if occured
415 const char* anError = dlerror();
416 throw(SALOME_Exception(anError));
418 throw(SALOME_Exception(LOCALIZED( "Can't load server meshers plugin library" )));
422 // get method, returning hypothesis creator
423 if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
424 typedef GenericHypothesisCreator_i* (*GetHypothesisCreator)(const char* theHypName);
425 GetHypothesisCreator procHandle =
426 (GetHypothesisCreator)GetProc( libHandle, "GetHypothesisCreator" );
429 throw(SALOME_Exception(LOCALIZED("bad hypothesis plugin library")));
430 UnLoadLib(libHandle);
433 // get hypothesis creator
434 if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << theHypName);
435 GenericHypothesisCreator_i* aCreator = procHandle(theHypName);
438 throw(SALOME_Exception(LOCALIZED("no such a hypothesis in this plugin")));
441 // map hypothesis creator to a hypothesis name
442 myHypCreatorMap[string(theHypName)] = aCreator;
445 // create a new hypothesis object, store its ref. in studyContext
446 if(MYDEBUG) MESSAGE("Create Hypothesis " << theHypName);
448 myHypCreatorMap[string(theHypName)]->Create(myPoa, GetCurrentStudyID(), &myGen);
449 myHypothesis_i->SetLibName(aPlatformLibName/*theLibName*/); // for persistency assurance
451 catch (SALOME_Exception& S_ex)
453 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
456 if ( aPlatformLibName )
457 delete[] aPlatformLibName;
460 return hypothesis_i._retn();
462 // activate the CORBA servant of hypothesis
463 hypothesis_i = SMESH::SMESH_Hypothesis::_narrow( myHypothesis_i->_this() );
464 int nextId = RegisterObject( hypothesis_i );
465 if(MYDEBUG) MESSAGE( "Add hypo to map with id = "<< nextId );
467 return hypothesis_i._retn();
470 //=============================================================================
472 * SMESH_Gen_i::createMesh
474 * Create empty mesh on shape
476 //=============================================================================
477 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::createMesh()
478 throw ( SALOME::SALOME_Exception )
480 Unexpect aCatch(SALOME_SalomeException);
481 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::createMesh" );
483 // Get or create the GEOM_Client instance
485 // create a new mesh object servant, store it in a map in study context
486 SMESH_Mesh_i* meshServant = new SMESH_Mesh_i( GetPOA(), this, GetCurrentStudyID() );
487 // create a new mesh object
488 MESSAGE("myIsEmbeddedMode " << myIsEmbeddedMode);
489 meshServant->SetImpl( myGen.CreateMesh( GetCurrentStudyID(), myIsEmbeddedMode ));
491 // activate the CORBA servant of Mesh
492 SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( meshServant->_this() );
493 int nextId = RegisterObject( mesh );
494 if(MYDEBUG) MESSAGE( "Add mesh to map with id = "<< nextId);
497 catch (SALOME_Exception& S_ex) {
498 THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
500 return SMESH::SMESH_Mesh::_nil();
503 //=============================================================================
505 * SMESH_Gen_i::GetShapeReader
509 //=============================================================================
510 GEOM_Client* SMESH_Gen_i::GetShapeReader()
512 // create shape reader if necessary
513 if ( !myShapeReader )
514 myShapeReader = new GEOM_Client(GetContainerRef());
515 ASSERT( myShapeReader );
516 return myShapeReader;
519 //=============================================================================
521 * SMESH_Gen_i::SetGeomEngine
523 * Set GEOM::GEOM_Gen reference
525 //=============================================================================
526 //GEOM::GEOM_Gen_ptr SMESH_Gen_i::SetGeomEngine( const char* containerLoc )
527 void SMESH_Gen_i::SetGeomEngine( GEOM::GEOM_Gen_ptr geomcompo )
529 //Engines::Component_ptr temp=GetLCC()->FindOrLoad_Component(containerLoc,"GEOM");
530 //myGeomGen=GEOM::GEOM_Gen::_narrow(temp);
531 myGeomGen=GEOM::GEOM_Gen::_duplicate(geomcompo);
535 //=============================================================================
537 * SMESH_Gen_i::SetEmbeddedMode
541 //=============================================================================
543 void SMESH_Gen_i::SetEmbeddedMode( CORBA::Boolean theMode )
545 myIsEmbeddedMode = theMode;
546 MESSAGE("myIsEmbeddedMode " << myIsEmbeddedMode);
548 if ( !myIsEmbeddedMode ) {
549 //PAL10867: disable signals catching with "noexcepthandler" option
550 char* envNoCatchSignals = getenv("NOT_INTERCEPT_SIGNALS");
551 if (!envNoCatchSignals || !atoi(envNoCatchSignals))
556 char* envDisableFPE = getenv("DISABLE_FPE");
557 if (envDisableFPE && atoi(envDisableFPE))
562 OSD::SetSignal( raiseFPE );
564 // else OSD::SetSignal() is called in GUI
568 //=============================================================================
570 * SMESH_Gen_i::IsEmbeddedMode
574 //=============================================================================
576 CORBA::Boolean SMESH_Gen_i::IsEmbeddedMode()
578 return myIsEmbeddedMode;
581 //=============================================================================
583 * SMESH_Gen_i::SetCurrentStudy
587 //=============================================================================
589 void SMESH_Gen_i::SetCurrentStudy( SALOMEDS::Study_ptr theStudy )
591 int curStudyId = GetCurrentStudyID();
592 myCurrentStudy = SALOMEDS::Study::_duplicate( theStudy );
593 // create study context, if it doesn't exist and set current study
594 int studyId = GetCurrentStudyID();
595 if ( myStudyContextMap.find( studyId ) == myStudyContextMap.end() ) {
596 myStudyContextMap[ studyId ] = new StudyContext;
599 // myCurrentStudy may be nil
600 if ( !CORBA::is_nil( myCurrentStudy ) ) {
601 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
602 if( !myCurrentStudy->FindComponent( "GEOM" )->_is_nil() )
603 aStudyBuilder->LoadWith( myCurrentStudy->FindComponent( "GEOM" ), GetGeomEngine() );
605 // NPAL16168, issue 0020210
606 // Let meshes update their data depending on GEOM groups that could change
607 if ( curStudyId != studyId )
609 //SALOMEDS::SComponent_var me = PublishComponent( myCurrentStudy );
610 SALOMEDS::SComponent_var me = SALOMEDS::SComponent::_narrow
611 ( myCurrentStudy->FindComponent( ComponentDataType() ) );
612 if ( !me->_is_nil() ) {
613 SALOMEDS::ChildIterator_var anIter = myCurrentStudy->NewChildIterator( me );
614 for ( ; anIter->More(); anIter->Next() ) {
615 SALOMEDS::SObject_var so = anIter->Value();
616 CORBA::Object_var ior = SObjectToObject( so );
617 if ( SMESH_Mesh_i* mesh = SMESH::DownCast<SMESH_Mesh_i*>( ior ))
618 mesh->CheckGeomGroupModif();
625 //=============================================================================
627 * SMESH_Gen_i::GetCurrentStudy
631 //=============================================================================
633 SALOMEDS::Study_ptr SMESH_Gen_i::GetCurrentStudy()
635 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetCurrentStudy: study Id = " << GetCurrentStudyID() );
636 return SALOMEDS::Study::_duplicate( myCurrentStudy );
639 //=============================================================================
641 * SMESH_Gen_i::GetCurrentStudyContext
643 * Get current study context
645 //=============================================================================
646 StudyContext* SMESH_Gen_i::GetCurrentStudyContext()
648 if ( !CORBA::is_nil( myCurrentStudy ) &&
649 myStudyContextMap.find( GetCurrentStudyID() ) != myStudyContextMap.end() )
650 return myStudyContextMap[ myCurrentStudy->StudyId() ];
655 //=============================================================================
657 * SMESH_Gen_i::CreateHypothesis
659 * Create hypothesis/algorothm of given type and publish it in the study
661 //=============================================================================
663 SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::CreateHypothesis( const char* theHypName,
664 const char* theLibName )
665 throw ( SALOME::SALOME_Exception )
667 Unexpect aCatch(SALOME_SalomeException);
668 // Create hypothesis/algorithm
669 SMESH::SMESH_Hypothesis_var hyp = this->createHypothesis( theHypName, theLibName );
671 // Publish hypothesis/algorithm in the study
672 if ( CanPublishInStudy( hyp ) ) {
673 SALOMEDS::SObject_var aSO = PublishHypothesis( myCurrentStudy, hyp );
674 if ( !aSO->_is_nil() ) {
675 // Update Python script
676 TPythonDump() << aSO << " = " << this << ".CreateHypothesis('"
677 << theHypName << "', '" << theLibName << "')";
684 //================================================================================
686 * \brief Return a hypothesis holding parameter values corresponding either to the mesh
687 * existing on the given geometry or to size of the geometry.
688 * \param theHypType - hypothesis type name
689 * \param theLibName - plugin library name
690 * \param theMesh - The mesh of interest
691 * \param theGeom - The shape to get parameter values from
692 * \retval SMESH::SMESH_Hypothesis_ptr - The returned hypothesis may be the one existing
693 * in a study and used to compute the mesh, or a temporary one created just to pass
696 //================================================================================
698 SMESH::SMESH_Hypothesis_ptr
699 SMESH_Gen_i::GetHypothesisParameterValues (const char* theHypType,
700 const char* theLibName,
701 SMESH::SMESH_Mesh_ptr theMesh,
702 GEOM::GEOM_Object_ptr theGeom,
703 CORBA::Boolean byMesh)
704 throw ( SALOME::SALOME_Exception )
706 Unexpect aCatch(SALOME_SalomeException);
707 if ( byMesh && CORBA::is_nil( theMesh ) )
708 return SMESH::SMESH_Hypothesis::_nil();
709 if ( byMesh && CORBA::is_nil( theGeom ) )
710 return SMESH::SMESH_Hypothesis::_nil();
712 // -----------------------------------------------
713 // find hypothesis used to mesh theGeom
714 // -----------------------------------------------
716 // get mesh and shape
717 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
718 TopoDS_Shape shape = GeomObjectToShape( theGeom );
719 if ( byMesh && ( !meshServant || meshServant->NbNodes()==0 || shape.IsNull() ))
720 return SMESH::SMESH_Hypothesis::_nil();
721 ::SMESH_Mesh* mesh = meshServant ? &meshServant->GetImpl() : (::SMESH_Mesh*)0;
723 // create a temporary hypothesis to know its dimention
724 SMESH::SMESH_Hypothesis_var tmpHyp = this->createHypothesis( theHypType, theLibName );
725 SMESH_Hypothesis_i* hypServant = SMESH::DownCast<SMESH_Hypothesis_i*>( tmpHyp );
727 return SMESH::SMESH_Hypothesis::_nil();
728 ::SMESH_Hypothesis* hyp = hypServant->GetImpl();
731 // look for a hypothesis of theHypType used to mesh the shape
732 if ( myGen.GetShapeDim( shape ) == hyp->GetDim() )
735 SMESH::ListOfHypothesis_var aHypList = theMesh->GetHypothesisList( theGeom );
736 int nbLocalHyps = aHypList->length();
737 for ( int i = 0; i < nbLocalHyps; i++ )
738 if ( strcmp( theHypType, aHypList[i]->GetName() ) == 0 ) // FOUND local!
739 return SMESH::SMESH_Hypothesis::_duplicate( aHypList[i] );
740 // check super shapes
741 TopTools_ListIteratorOfListOfShape itShape( mesh->GetAncestors( shape ));
742 while ( nbLocalHyps == 0 && itShape.More() ) {
743 GEOM::GEOM_Object_ptr geomObj = ShapeToGeomObject( itShape.Value() );
744 if ( ! CORBA::is_nil( geomObj )) {
745 SMESH::ListOfHypothesis_var aHypList = theMesh->GetHypothesisList( geomObj );
746 nbLocalHyps = aHypList->length();
747 for ( int i = 0; i < nbLocalHyps; i++ )
748 if ( strcmp( theHypType, aHypList[i]->GetName() ) == 0 ) // FOUND global!
749 return SMESH::SMESH_Hypothesis::_duplicate( aHypList[i] );
755 // let the temporary hypothesis find out some how parameter values by mesh
756 if ( hyp->SetParametersByMesh( mesh, shape ))
757 return SMESH::SMESH_Hypothesis::_duplicate( tmpHyp );
762 diagonal = mesh->GetShapeDiagonalSize();
764 diagonal = ::SMESH_Mesh::GetShapeDiagonalSize( shape );
765 ::SMESH_Hypothesis::TDefaults dflts;
766 dflts._elemLength = diagonal / myGen.GetBoundaryBoxSegmentation();
767 dflts._nbSegments = myGen.GetDefaultNbSegments();
768 // let the temporary hypothesis initialize it's values
769 if ( hyp->SetParametersByDefaults( dflts, mesh ))
770 return SMESH::SMESH_Hypothesis::_duplicate( tmpHyp );
773 return SMESH::SMESH_Hypothesis::_nil();
776 //=============================================================================
778 * Sets number of segments per diagonal of boundary box of geometry by which
779 * default segment length of appropriate 1D hypotheses is defined
781 //=============================================================================
783 void SMESH_Gen_i::SetBoundaryBoxSegmentation( CORBA::Long theNbSegments )
784 throw ( SALOME::SALOME_Exception )
786 if ( theNbSegments > 0 )
787 myGen.SetBoundaryBoxSegmentation( int( theNbSegments ));
789 THROW_SALOME_CORBA_EXCEPTION( "non-positive number of segments", SALOME::BAD_PARAM );
791 //=============================================================================
793 * \brief Sets default number of segments per edge
795 //=============================================================================
796 void SMESH_Gen_i::SetDefaultNbSegments(CORBA::Long theNbSegments)
797 throw ( SALOME::SALOME_Exception )
799 if ( theNbSegments > 0 )
800 myGen.SetDefaultNbSegments( int(theNbSegments) );
802 THROW_SALOME_CORBA_EXCEPTION( "non-positive number of segments", SALOME::BAD_PARAM );
805 //=============================================================================
807 * SMESH_Gen_i::CreateMesh
809 * Create empty mesh on a shape and publish it in the study
811 //=============================================================================
813 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMesh( GEOM::GEOM_Object_ptr theShapeObject )
814 throw ( SALOME::SALOME_Exception )
816 Unexpect aCatch(SALOME_SalomeException);
817 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMesh" );
819 SMESH::SMESH_Mesh_var mesh = this->createMesh();
821 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( mesh );
822 ASSERT( meshServant );
823 meshServant->SetShape( theShapeObject );
825 // publish mesh in the study
826 if ( CanPublishInStudy( mesh ) ) {
827 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
828 aStudyBuilder->NewCommand(); // There is a transaction
829 SALOMEDS::SObject_var aSO = PublishMesh( myCurrentStudy, mesh.in() );
830 aStudyBuilder->CommitCommand();
831 if ( !aSO->_is_nil() ) {
832 // Update Python script
833 TPythonDump() << aSO << " = " << this << ".CreateMesh(" << theShapeObject << ")";
840 //=============================================================================
842 * SMESH_Gen_i::CreateEmptyMesh
846 //=============================================================================
848 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateEmptyMesh()
849 throw ( SALOME::SALOME_Exception )
851 Unexpect aCatch(SALOME_SalomeException);
852 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMesh" );
854 SMESH::SMESH_Mesh_var mesh = this->createMesh();
856 // publish mesh in the study
857 if ( CanPublishInStudy( mesh ) ) {
858 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
859 aStudyBuilder->NewCommand(); // There is a transaction
860 SALOMEDS::SObject_var aSO = PublishMesh( myCurrentStudy, mesh.in() );
861 aStudyBuilder->CommitCommand();
862 if ( !aSO->_is_nil() ) {
863 // Update Python script
864 TPythonDump() << aSO << " = " << this << ".CreateEmptyMesh()";
871 //=============================================================================
873 * SMESH_Gen_i::CreateMeshFromUNV
875 * Create mesh and import data from UNV file
877 //=============================================================================
879 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMeshesFromUNV( const char* theFileName )
880 throw ( SALOME::SALOME_Exception )
882 Unexpect aCatch(SALOME_SalomeException);
883 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMeshesFromUNV" );
885 SMESH::SMESH_Mesh_var aMesh = createMesh();
887 // publish mesh in the study
888 if ( CanPublishInStudy( aMesh ) ) {
889 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
890 aStudyBuilder->NewCommand(); // There is a transaction
891 SALOMEDS::SObject_var aSO = PublishMesh( myCurrentStudy, aMesh.in(), aFileName.c_str() );
892 aStudyBuilder->CommitCommand();
893 if ( !aSO->_is_nil() ) {
894 // Update Python script
895 TPythonDump() << aSO << " = smeshgen.CreateMeshesFromUNV(r'" << theFileName << "')";
899 SMESH_Mesh_i* aServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( aMesh ).in() );
901 aServant->ImportUNVFile( theFileName );
903 // Dump creation of groups
904 SMESH::ListOfGroups_var groups = aServant->GetGroups();
906 aServant->GetImpl().GetMeshDS()->Modified();
907 return aMesh._retn();
910 //=============================================================================
912 * SMESH_Gen_i::CreateMeshFromMED
914 * Create mesh and import data from MED file
916 //=============================================================================
918 SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromMED( const char* theFileName,
919 SMESH::DriverMED_ReadStatus& theStatus)
920 throw ( SALOME::SALOME_Exception )
922 Unexpect aCatch(SALOME_SalomeException);
923 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMeshFromMED" );
925 // Retrieve mesh names from the file
926 DriverMED_R_SMESHDS_Mesh myReader;
927 myReader.SetFile( theFileName );
928 myReader.SetMeshId( -1 );
929 Driver_Mesh::Status aStatus;
930 list<string> aNames = myReader.GetMeshNames(aStatus);
931 SMESH::mesh_array_var aResult = new SMESH::mesh_array();
932 theStatus = (SMESH::DriverMED_ReadStatus)aStatus;
934 { // open a new scope to make aPythonDump die before PythonDump in SMESH_Mesh::GetGroups()
937 TPythonDump aPythonDump;
940 if (theStatus == SMESH::DRS_OK) {
941 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
942 aStudyBuilder->NewCommand(); // There is a transaction
943 aResult->length( aNames.size() );
946 // Iterate through all meshes and create mesh objects
947 for ( list<string>::iterator it = aNames.begin(); it != aNames.end(); it++ ) {
949 if (i > 0) aPythonDump << ", ";
952 SMESH::SMESH_Mesh_var mesh = createMesh();
954 // publish mesh in the study
955 SALOMEDS::SObject_var aSO;
956 if ( CanPublishInStudy( mesh ) )
957 aSO = PublishMesh( myCurrentStudy, mesh.in(), (*it).c_str() );
958 if ( !aSO->_is_nil() ) {
963 aPythonDump << "mesh_" << i;
966 // Read mesh data (groups are published automatically by ImportMEDFile())
967 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
968 ASSERT( meshServant );
969 SMESH::DriverMED_ReadStatus status1 =
970 meshServant->ImportMEDFile( theFileName, (*it).c_str() );
971 if (status1 > theStatus)
974 aResult[i++] = SMESH::SMESH_Mesh::_duplicate( mesh );
975 meshServant->GetImpl().GetMeshDS()->Modified();
977 aStudyBuilder->CommitCommand();
980 // Update Python script
981 aPythonDump << "], status) = " << this << ".CreateMeshesFromMED(r'" << theFileName << "')";
983 // Dump creation of groups
984 for ( int i = 0; i < aResult->length(); ++i )
985 SMESH::ListOfGroups_var groups = aResult[ i ]->GetGroups();
987 return aResult._retn();
990 //=============================================================================
992 * SMESH_Gen_i::CreateMeshFromSTL
994 * Create mesh and import data from STL file
996 //=============================================================================
998 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMeshesFromSTL( const char* theFileName )
999 throw ( SALOME::SALOME_Exception )
1001 Unexpect aCatch(SALOME_SalomeException);
1002 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMeshesFromSTL" );
1004 SMESH::SMESH_Mesh_var aMesh = createMesh();
1006 // publish mesh in the study
1007 if ( CanPublishInStudy( aMesh ) ) {
1008 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1009 aStudyBuilder->NewCommand(); // There is a transaction
1010 SALOMEDS::SObject_var aSO = PublishInStudy
1011 ( myCurrentStudy, SALOMEDS::SObject::_nil(), aMesh.in(), aFileName.c_str() );
1012 aStudyBuilder->CommitCommand();
1013 if ( !aSO->_is_nil() ) {
1014 // Update Python script
1015 TPythonDump() << aSO << " = " << this << ".CreateMeshesFromSTL(r'" << theFileName << "')";
1019 SMESH_Mesh_i* aServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( aMesh ).in() );
1021 aServant->ImportSTLFile( theFileName );
1022 aServant->GetImpl().GetMeshDS()->Modified();
1023 return aMesh._retn();
1026 //=============================================================================
1028 * SMESH_Gen_i::IsReadyToCompute
1030 * Returns true if mesh contains enough data to be computed
1032 //=============================================================================
1034 CORBA::Boolean SMESH_Gen_i::IsReadyToCompute( SMESH::SMESH_Mesh_ptr theMesh,
1035 GEOM::GEOM_Object_ptr theShapeObject )
1036 throw ( SALOME::SALOME_Exception )
1038 Unexpect aCatch(SALOME_SalomeException);
1039 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::IsReadyToCompute" );
1041 if ( CORBA::is_nil( theShapeObject ) )
1042 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1043 SALOME::BAD_PARAM );
1045 if ( CORBA::is_nil( theMesh ) )
1046 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
1047 SALOME::BAD_PARAM );
1051 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
1052 ASSERT( meshServant );
1053 if ( meshServant ) {
1054 // get local TopoDS_Shape
1055 TopoDS_Shape myLocShape = GeomObjectToShape( theShapeObject );
1056 // call implementation
1057 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1058 return myGen.CheckAlgoState( myLocMesh, myLocShape );
1061 catch ( SALOME_Exception& S_ex ) {
1062 INFOS( "catch exception "<< S_ex.what() );
1067 //================================================================================
1069 * \brief Find SObject for an algo
1071 //================================================================================
1073 SALOMEDS::SObject_ptr SMESH_Gen_i::GetAlgoSO(const ::SMESH_Algo* algo)
1076 if ( !myCurrentStudy->_is_nil() ) {
1077 // find algo in the study
1078 SALOMEDS::SComponent_var father = SALOMEDS::SComponent::_narrow
1079 ( myCurrentStudy->FindComponent( ComponentDataType() ) );
1080 if ( !father->_is_nil() ) {
1081 SALOMEDS::ChildIterator_var itBig = myCurrentStudy->NewChildIterator( father );
1082 for ( ; itBig->More(); itBig->Next() ) {
1083 SALOMEDS::SObject_var gotBranch = itBig->Value();
1084 if ( gotBranch->Tag() == GetAlgorithmsRootTag() ) {
1085 SALOMEDS::ChildIterator_var algoIt = myCurrentStudy->NewChildIterator( gotBranch );
1086 for ( ; algoIt->More(); algoIt->Next() ) {
1087 SALOMEDS::SObject_var algoSO = algoIt->Value();
1088 CORBA::Object_var algoIOR = SObjectToObject( algoSO );
1089 if ( !CORBA::is_nil( algoIOR )) {
1090 SMESH_Hypothesis_i* impl = SMESH::DownCast<SMESH_Hypothesis_i*>( algoIOR );
1091 if ( impl && impl->GetImpl() == algo )
1092 return algoSO._retn();
1094 } // loop on algo SO's
1097 } // SMESH component iterator
1101 return SALOMEDS::SObject::_nil();
1104 //================================================================================
1106 * \brief Return errors of mesh computation
1108 //================================================================================
1110 SMESH::compute_error_array* SMESH_Gen_i::GetComputeErrors( SMESH::SMESH_Mesh_ptr theMesh,
1111 GEOM::GEOM_Object_ptr theSubObject )
1112 throw ( SALOME::SALOME_Exception )
1114 Unexpect aCatch(SALOME_SalomeException);
1115 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetComputeErrors()" );
1117 if ( CORBA::is_nil( theSubObject ) && theMesh->HasShapeToMesh())
1118 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
1120 if ( CORBA::is_nil( theMesh ) )
1121 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1123 SMESH::compute_error_array_var error_array = new SMESH::compute_error_array;
1125 if ( SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh ))
1128 if(theMesh->HasShapeToMesh())
1129 shape = GeomObjectToShape( theSubObject );
1131 shape = SMESH_Mesh::PseudoShape();
1133 ::SMESH_Mesh& mesh = meshServant->GetImpl();
1135 error_array->length( mesh.GetMeshDS()->MaxShapeIndex() );
1138 SMESH_subMesh *sm = mesh.GetSubMesh(shape);
1139 const bool includeSelf = true, complexShapeFirst = true;
1140 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(includeSelf,
1142 while ( smIt->more() )
1145 if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
1147 SMESH_ComputeErrorPtr error = sm->GetComputeError();
1148 if ( error && !error->IsOK() && error->myAlgo )
1150 SMESH::ComputeError & errStruct = error_array[ nbErr++ ];
1151 errStruct.code = -( error->myName < 0 ? error->myName + 1: error->myName ); // -1 -> 0
1152 errStruct.comment = error->myComment.c_str();
1153 errStruct.subShapeID = sm->GetId();
1154 SALOMEDS::SObject_var algoSO = GetAlgoSO( error->myAlgo );
1155 if ( !algoSO->_is_nil() )
1156 errStruct.algoName = algoSO->GetName();
1158 errStruct.algoName = error->myAlgo->GetName();
1159 errStruct.hasBadMesh = !error->myBadElements.empty();
1162 error_array->length( nbErr );
1165 catch ( SALOME_Exception& S_ex ) {
1166 INFOS( "catch exception "<< S_ex.what() );
1169 return error_array._retn();
1173 //================================================================================
1175 * \brief Return mesh elements preventing computation of a subshape
1177 //================================================================================
1179 SMESH::MeshPreviewStruct*
1180 SMESH_Gen_i::GetBadInputElements( SMESH::SMESH_Mesh_ptr theMesh,
1181 CORBA::Short theSubShapeID )
1182 throw ( SALOME::SALOME_Exception )
1184 Unexpect aCatch(SALOME_SalomeException);
1185 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetBadInputElements()" );
1187 if ( CORBA::is_nil( theMesh ) )
1188 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1190 SMESH::MeshPreviewStruct_var result = new SMESH::MeshPreviewStruct;
1193 if ( SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh ))
1195 // mesh implementation
1196 ::SMESH_Mesh& mesh = meshServant->GetImpl();
1197 // submesh by subshape id
1198 if ( SMESH_subMesh * sm = mesh.GetSubMeshContaining( theSubShapeID ))
1201 SMESH_ComputeErrorPtr error = sm->GetComputeError();
1202 if ( error && !error->myBadElements.empty())
1204 typedef map<const SMDS_MeshElement*, int > TNode2LocalIDMap;
1205 typedef TNode2LocalIDMap::iterator TNodeLocalID;
1207 // get nodes of elements and count elements
1208 TNode2LocalIDMap mapNode2LocalID;
1209 list< TNodeLocalID > connectivity;
1210 int i, nbElements = 0, nbConnNodes = 0;
1212 list<const SMDS_MeshElement*>::iterator elemIt = error->myBadElements.begin();
1213 list<const SMDS_MeshElement*>::iterator elemEnd = error->myBadElements.end();
1214 for ( ; elemIt != elemEnd; ++elemIt, ++nbElements )
1216 SMDS_ElemIteratorPtr nIt = (*elemIt)->nodesIterator();
1217 while ( nIt->more() )
1218 connectivity.push_back
1219 ( mapNode2LocalID.insert( make_pair( nIt->next(), ++nbConnNodes)).first );
1221 // fill node coords and assign local ids to the nodes
1222 int nbNodes = mapNode2LocalID.size();
1223 result->nodesXYZ.length( nbNodes );
1224 TNodeLocalID node2ID = mapNode2LocalID.begin();
1225 for ( i = 0; i < nbNodes; ++i, ++node2ID ) {
1226 node2ID->second = i;
1227 const SMDS_MeshNode* node = (const SMDS_MeshNode*) node2ID->first;
1228 result->nodesXYZ[i].x = node->X();
1229 result->nodesXYZ[i].y = node->Y();
1230 result->nodesXYZ[i].z = node->Z();
1232 // fill connectivity
1233 result->elementConnectivities.length( nbConnNodes );
1234 list< TNodeLocalID >::iterator connIt = connectivity.begin();
1235 for ( i = 0; i < nbConnNodes; ++i, ++connIt ) {
1236 result->elementConnectivities[i] = (*connIt)->second;
1238 // fill element types
1239 result->elementTypes.length( nbElements );
1240 for ( i = 0, elemIt = error->myBadElements.begin(); i <nbElements; ++i, ++elemIt )
1242 const SMDS_MeshElement* elem = *elemIt;
1243 result->elementTypes[i].SMDS_ElementType = (SMESH::ElementType) elem->GetType();
1244 result->elementTypes[i].isPoly = elem->IsPoly();
1245 result->elementTypes[i].nbNodesInElement = elem->NbNodes();
1251 catch ( SALOME_Exception& S_ex ) {
1252 INFOS( "catch exception "<< S_ex.what() );
1255 return result._retn();
1258 //================================================================================
1260 * \brief Returns errors of hypotheses definintion
1261 * \param theMesh - the mesh
1262 * \param theSubObject - the main or sub- shape
1263 * \retval SMESH::algo_error_array* - sequence of errors
1265 //================================================================================
1267 SMESH::algo_error_array* SMESH_Gen_i::GetAlgoState( SMESH::SMESH_Mesh_ptr theMesh,
1268 GEOM::GEOM_Object_ptr theSubObject )
1269 throw ( SALOME::SALOME_Exception )
1271 Unexpect aCatch(SALOME_SalomeException);
1272 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetAlgoState()" );
1274 if ( CORBA::is_nil( theSubObject ) && theMesh->HasShapeToMesh())
1275 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
1277 if ( CORBA::is_nil( theMesh ) )
1278 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1280 SMESH::algo_error_array_var error_array = new SMESH::algo_error_array;
1282 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
1283 ASSERT( meshServant );
1284 if ( meshServant ) {
1285 TopoDS_Shape myLocShape;
1286 if(theMesh->HasShapeToMesh())
1287 myLocShape = GeomObjectToShape( theSubObject );
1289 myLocShape = SMESH_Mesh::PseudoShape();
1291 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1292 list< ::SMESH_Gen::TAlgoStateError > error_list;
1293 list< ::SMESH_Gen::TAlgoStateError >::iterator error;
1294 // call ::SMESH_Gen::GetAlgoState()
1295 myGen.GetAlgoState( myLocMesh, myLocShape, error_list );
1296 error_array->length( error_list.size() );
1298 for ( error = error_list.begin(); error != error_list.end(); ++error )
1300 // fill AlgoStateError structure
1301 SMESH::AlgoStateError & errStruct = error_array[ i++ ];
1302 errStruct.state = SMESH_Mesh_i::ConvertHypothesisStatus( error->_name );
1303 errStruct.algoDim = error->_algoDim;
1304 errStruct.isGlobalAlgo = error->_isGlobalAlgo;
1305 errStruct.algoName = "";
1306 SALOMEDS::SObject_var algoSO = GetAlgoSO( error->_algo );
1307 if ( !algoSO->_is_nil() )
1308 errStruct.algoName = algoSO->GetName();
1312 catch ( SALOME_Exception& S_ex ) {
1313 INFOS( "catch exception "<< S_ex.what() );
1315 return error_array._retn();
1318 //=============================================================================
1320 * SMESH_Gen_i::GetSubShapesId
1322 * Get sub-shapes unique ID's list
1324 //=============================================================================
1326 SMESH::long_array* SMESH_Gen_i::GetSubShapesId( GEOM::GEOM_Object_ptr theMainShapeObject,
1327 const SMESH::object_array& theListOfSubShapeObject )
1328 throw ( SALOME::SALOME_Exception )
1330 Unexpect aCatch(SALOME_SalomeException);
1331 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetSubShapesId" );
1333 SMESH::long_array_var shapesId = new SMESH::long_array;
1336 if ( CORBA::is_nil( theMainShapeObject ) )
1337 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1338 SALOME::BAD_PARAM );
1342 TopoDS_Shape myMainShape = GeomObjectToShape(theMainShapeObject);
1343 TopTools_IndexedMapOfShape myIndexToShape;
1344 TopExp::MapShapes(myMainShape,myIndexToShape);
1346 for ( int i = 0; i < theListOfSubShapeObject.length(); i++ )
1348 GEOM::GEOM_Object_var aShapeObject
1349 = GEOM::GEOM_Object::_narrow(theListOfSubShapeObject[i]);
1350 if ( CORBA::is_nil( aShapeObject ) )
1351 THROW_SALOME_CORBA_EXCEPTION ("bad shape object reference", \
1352 SALOME::BAD_PARAM );
1354 TopoDS_Shape locShape = GeomObjectToShape(aShapeObject);
1355 for (TopExp_Explorer exp(locShape,TopAbs_FACE); exp.More(); exp.Next())
1357 const TopoDS_Face& F = TopoDS::Face(exp.Current());
1358 setId.insert(myIndexToShape.FindIndex(F));
1359 if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(F));
1361 for (TopExp_Explorer exp(locShape,TopAbs_EDGE); exp.More(); exp.Next())
1363 const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
1364 setId.insert(myIndexToShape.FindIndex(E));
1365 if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(E));
1367 for (TopExp_Explorer exp(locShape,TopAbs_VERTEX); exp.More(); exp.Next())
1369 const TopoDS_Vertex& V = TopoDS::Vertex(exp.Current());
1370 setId.insert(myIndexToShape.FindIndex(V));
1371 if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(V));
1374 shapesId->length(setId.size());
1375 set<int>::iterator iind;
1377 for (iind = setId.begin(); iind != setId.end(); iind++)
1379 if(MYDEBUG) SCRUTE((*iind));
1380 shapesId[i] = (*iind);
1381 if(MYDEBUG) SCRUTE(shapesId[i]);
1385 catch (SALOME_Exception& S_ex)
1387 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
1390 return shapesId._retn();
1393 //=============================================================================
1395 * SMESH_Gen_i::Compute
1397 * Compute mesh on a shape
1399 //=============================================================================
1401 CORBA::Boolean SMESH_Gen_i::Compute( SMESH::SMESH_Mesh_ptr theMesh,
1402 GEOM::GEOM_Object_ptr theShapeObject )
1403 throw ( SALOME::SALOME_Exception )
1406 Unexpect aCatch(SALOME_SalomeException);
1407 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Compute" );
1409 if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
1410 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1411 SALOME::BAD_PARAM );
1413 if ( CORBA::is_nil( theMesh ) )
1414 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
1415 SALOME::BAD_PARAM );
1417 // Update Python script
1418 TPythonDump() << "isDone = " << this << ".Compute( "
1419 << theMesh << ", " << theShapeObject << ")";
1423 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
1424 ASSERT( meshServant );
1425 if ( meshServant ) {
1426 // NPAL16168: "geometrical group edition from a submesh don't modifiy mesh computation"
1427 meshServant->CheckGeomGroupModif();
1428 // get local TopoDS_Shape
1429 TopoDS_Shape myLocShape;
1430 if(theMesh->HasShapeToMesh())
1431 myLocShape = GeomObjectToShape( theShapeObject );
1433 myLocShape = SMESH_Mesh::PseudoShape();
1434 // call implementation compute
1435 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1436 bool ok = myGen.Compute( myLocMesh, myLocShape);
1437 meshServant->CreateGroupServants(); // algos can create groups (issue 0020918)
1438 myLocMesh.GetMeshDS()->Modified();
1442 catch ( std::bad_alloc ) {
1443 INFOS( "Compute(): lack of memory" );
1445 catch ( SALOME_Exception& S_ex ) {
1446 INFOS( "Compute(): catch exception "<< S_ex.what() );
1449 INFOS( "Compute(): unknown exception " );
1454 //=============================================================================
1456 * SMESH_Gen_i::Precompute
1458 * Compute mesh as preview till indicated dimension on shape
1460 //=============================================================================
1462 SMESH::MeshPreviewStruct* SMESH_Gen_i::Precompute( SMESH::SMESH_Mesh_ptr theMesh,
1463 GEOM::GEOM_Object_ptr theShapeObject,
1464 SMESH::Dimension theDimension,
1465 SMESH::long_array& theShapesId)
1466 throw ( SALOME::SALOME_Exception )
1468 Unexpect aCatch(SALOME_SalomeException);
1469 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Precompute" );
1471 if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
1472 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1473 SALOME::BAD_PARAM );
1475 if ( CORBA::is_nil( theMesh ) )
1476 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
1477 SALOME::BAD_PARAM );
1479 SMESH::MeshPreviewStruct_var result = new SMESH::MeshPreviewStruct;
1482 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
1483 ASSERT( meshServant );
1484 if ( meshServant ) {
1485 // NPAL16168: "geometrical group edition from a submesh don't modifiy mesh computation"
1486 meshServant->CheckGeomGroupModif();
1487 // get local TopoDS_Shape
1488 TopoDS_Shape myLocShape;
1489 if(theMesh->HasShapeToMesh())
1490 myLocShape = GeomObjectToShape( theShapeObject );
1492 return result._retn();;
1494 // call implementation compute
1495 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1497 ::MeshDimension aDim = (MeshDimension)theDimension;
1498 if ( myGen.Compute( myLocMesh, myLocShape, false, aDim, &shapeIds ) )
1500 int nbShapeId = shapeIds.size();
1501 theShapesId.length( nbShapeId );
1502 // iterates on shapes and collect mesh entities into mesh preview
1503 TSetOfInt::const_iterator idIt = shapeIds.begin();
1504 TSetOfInt::const_iterator idEnd = shapeIds.end();
1505 std::map< int, int > mapOfShIdNb;
1506 std::set< SMESH_TLink > setOfEdge;
1507 std::list< SMDSAbs_ElementType > listOfElemType;
1508 typedef map<const SMDS_MeshElement*, int > TNode2LocalIDMap;
1509 typedef TNode2LocalIDMap::iterator TNodeLocalID;
1510 TNode2LocalIDMap mapNode2LocalID;
1511 list< TNodeLocalID > connectivity;
1512 int i, nbConnNodes = 0;
1513 std::set< const SMESH_subMesh* > setOfVSubMesh;
1514 // iterates on shapes
1515 for ( ; idIt != idEnd; idIt++ )
1517 if ( mapOfShIdNb.find( *idIt ) != mapOfShIdNb.end() )
1519 SMESH_subMesh* sm = myLocMesh.GetSubMeshContaining(*idIt);
1520 if ( !sm || !sm->IsMeshComputed() )
1523 const TopoDS_Shape& aSh = sm->GetSubShape();
1524 const int shDim = myGen.GetShapeDim( aSh );
1525 if ( shDim < 1 || shDim > theDimension )
1528 mapOfShIdNb[ *idIt ] = 0;
1529 theShapesId[ mapOfShIdNb.size() - 1 ] = *idIt;
1531 SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
1532 if ( !smDS ) continue;
1534 if ( theDimension == SMESH::DIM_2D )
1536 SMDS_ElemIteratorPtr faceIt = smDS->GetElements();
1537 while ( faceIt->more() )
1539 const SMDS_MeshElement* face = faceIt->next();
1540 int aNbNode = face->NbNodes();
1542 aNbNode /= 2; // do not take into account additional middle nodes
1544 SMDS_MeshNode* node1 = (SMDS_MeshNode*)face->GetNode( 0 );
1545 for ( int nIndx = 0; nIndx < aNbNode; nIndx++ )
1547 SMDS_MeshNode* node2 = (SMDS_MeshNode*)face->GetNode( nIndx+1 < aNbNode ? nIndx+1 : 0 );
1548 if ( setOfEdge.insert( SMESH_TLink ( node1, node2 ) ).second )
1550 listOfElemType.push_back( SMDSAbs_Edge );
1551 connectivity.push_back
1552 ( mapNode2LocalID.insert( make_pair( node1, ++nbConnNodes)).first );
1553 connectivity.push_back
1554 ( mapNode2LocalID.insert( make_pair( node2, ++nbConnNodes)).first );
1560 else if ( theDimension == SMESH::DIM_1D )
1562 SMDS_NodeIteratorPtr nodeIt = smDS->GetNodes();
1563 while ( nodeIt->more() )
1565 listOfElemType.push_back( SMDSAbs_Node );
1566 connectivity.push_back
1567 ( mapNode2LocalID.insert( make_pair( nodeIt->next(), ++nbConnNodes)).first );
1569 // add corner nodes by first vertex from edge
1570 SMESH_subMeshIteratorPtr edgeSmIt =
1571 sm->getDependsOnIterator(/*includeSelf*/false,
1572 /*complexShapeFirst*/false);
1573 while ( edgeSmIt->more() )
1575 SMESH_subMesh* vertexSM = edgeSmIt->next();
1576 // check that vertex is not already treated
1577 if ( !setOfVSubMesh.insert( vertexSM ).second )
1579 if ( vertexSM->GetSubShape().ShapeType() != TopAbs_VERTEX )
1582 const SMESHDS_SubMesh* vertexSmDS = vertexSM->GetSubMeshDS();
1583 SMDS_NodeIteratorPtr nodeIt = vertexSmDS->GetNodes();
1584 while ( nodeIt->more() )
1586 listOfElemType.push_back( SMDSAbs_Node );
1587 connectivity.push_back
1588 ( mapNode2LocalID.insert( make_pair( nodeIt->next(), ++nbConnNodes)).first );
1594 // fill node coords and assign local ids to the nodes
1595 int nbNodes = mapNode2LocalID.size();
1596 result->nodesXYZ.length( nbNodes );
1597 TNodeLocalID node2ID = mapNode2LocalID.begin();
1598 for ( i = 0; i < nbNodes; ++i, ++node2ID ) {
1599 node2ID->second = i;
1600 const SMDS_MeshNode* node = (const SMDS_MeshNode*) node2ID->first;
1601 result->nodesXYZ[i].x = node->X();
1602 result->nodesXYZ[i].y = node->Y();
1603 result->nodesXYZ[i].z = node->Z();
1605 // fill connectivity
1606 result->elementConnectivities.length( nbConnNodes );
1607 list< TNodeLocalID >::iterator connIt = connectivity.begin();
1608 for ( i = 0; i < nbConnNodes; ++i, ++connIt ) {
1609 result->elementConnectivities[i] = (*connIt)->second;
1612 // fill element types
1613 result->elementTypes.length( listOfElemType.size() );
1614 std::list< SMDSAbs_ElementType >::const_iterator typeIt = listOfElemType.begin();
1615 std::list< SMDSAbs_ElementType >::const_iterator typeEnd = listOfElemType.end();
1616 for ( i = 0; typeIt != typeEnd; ++i, ++typeIt )
1618 SMDSAbs_ElementType elemType = *typeIt;
1619 result->elementTypes[i].SMDS_ElementType = (SMESH::ElementType)elemType;
1620 result->elementTypes[i].isPoly = false;
1621 result->elementTypes[i].nbNodesInElement = elemType == SMDSAbs_Edge ? 2 : 1;
1624 // correct number of shapes
1625 theShapesId.length( mapOfShIdNb.size() );
1629 catch ( std::bad_alloc ) {
1630 INFOS( "Precompute(): lack of memory" );
1632 catch ( SALOME_Exception& S_ex ) {
1633 INFOS( "Precompute(): catch exception "<< S_ex.what() );
1636 INFOS( "Precompute(): unknown exception " );
1638 return result._retn();
1642 //=============================================================================
1644 * SMESH_Gen_i::Evaluate
1646 * Evaluate mesh on a shape
1648 //=============================================================================
1650 SMESH::long_array* SMESH_Gen_i::Evaluate(SMESH::SMESH_Mesh_ptr theMesh,
1651 GEOM::GEOM_Object_ptr theShapeObject)
1652 // SMESH::long_array& theNbElems)
1653 throw ( SALOME::SALOME_Exception )
1655 Unexpect aCatch(SALOME_SalomeException);
1656 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Evaluate" );
1658 if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
1659 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1660 SALOME::BAD_PARAM );
1662 if ( CORBA::is_nil( theMesh ) )
1663 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
1664 SALOME::BAD_PARAM );
1666 SMESH::long_array_var nbels = new SMESH::long_array;
1667 nbels->length(SMESH::Entity_Last);
1668 int i = SMESH::Entity_Node;
1669 for (; i < SMESH::Entity_Last; i++)
1672 // Update Python script
1673 TPythonDump() << "theNbElems = " << this << ".Evaluate( "
1674 << theMesh << ", " << theShapeObject << ")";
1678 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
1679 ASSERT( meshServant );
1680 if ( meshServant ) {
1681 // NPAL16168: "geometrical group edition from a submesh don't modifiy mesh computation"
1682 meshServant->CheckGeomGroupModif();
1683 // get local TopoDS_Shape
1684 TopoDS_Shape myLocShape;
1685 if(theMesh->HasShapeToMesh())
1686 myLocShape = GeomObjectToShape( theShapeObject );
1688 myLocShape = SMESH_Mesh::PseudoShape();
1689 // call implementation compute
1690 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1691 MapShapeNbElems aResMap;
1692 /*CORBA::Boolean ret =*/ myGen.Evaluate( myLocMesh, myLocShape, aResMap);
1693 MapShapeNbElemsItr anIt = aResMap.begin();
1694 for(; anIt!=aResMap.end(); anIt++) {
1695 const vector<int>& aVec = (*anIt).second;
1696 for(i = SMESH::Entity_Node; i < aVec.size(); i++) {
1697 int nbElem = aVec[i];
1698 if ( nbElem < 0 ) // algo failed, check that it has reported a message
1700 SMESH_subMesh* sm = anIt->first;
1701 SMESH_ComputeErrorPtr& error = sm->GetComputeError();
1702 const SMESH_Algo* algo = myGen.GetAlgo( myLocMesh, sm->GetSubShape());
1703 if ( algo && !error.get() || error->IsOK() )
1704 error.reset( new SMESH_ComputeError( COMPERR_ALGO_FAILED,"Failed to evaluate",algo));
1708 nbels[i] += aVec[i];
1712 return nbels._retn();
1715 catch ( std::bad_alloc ) {
1716 INFOS( "Evaluate(): lack of memory" );
1718 catch ( SALOME_Exception& S_ex ) {
1719 INFOS( "Evaluate(): catch exception "<< S_ex.what() );
1722 INFOS( "Evaluate(): unknown exception " );
1725 return nbels._retn();
1728 //================================================================================
1730 * \brief Return geometrical object the given element is built on
1731 * \param theMesh - the mesh the element is in
1732 * \param theElementID - the element ID
1733 * \param theGeomName - the name of the result geom object if it is not yet published
1734 * \retval GEOM::GEOM_Object_ptr - the found or just published geom object
1736 //================================================================================
1738 GEOM::GEOM_Object_ptr
1739 SMESH_Gen_i::GetGeometryByMeshElement( SMESH::SMESH_Mesh_ptr theMesh,
1740 CORBA::Long theElementID,
1741 const char* theGeomName)
1742 throw ( SALOME::SALOME_Exception )
1744 Unexpect aCatch(SALOME_SalomeException);
1746 GEOM::GEOM_Object_var geom = FindGeometryByMeshElement(theMesh, theElementID);
1747 if ( !geom->_is_nil() ) {
1748 GEOM::GEOM_Object_var mainShape = theMesh->GetShapeToMesh();
1749 GEOM::GEOM_Gen_ptr geomGen = GetGeomEngine();
1751 // try to find the corresponding SObject
1752 SALOMEDS::SObject_var SObj = ObjectToSObject( myCurrentStudy, geom.in() );
1753 if ( SObj->_is_nil() ) // submesh can be not found even if published
1755 // try to find published submesh
1756 GEOM::ListOfLong_var list = geom->GetSubShapeIndices();
1757 if ( !geom->IsMainShape() && list->length() == 1 ) {
1758 SALOMEDS::SObject_var mainSO = ObjectToSObject( myCurrentStudy, mainShape );
1759 SALOMEDS::ChildIterator_var it;
1760 if ( !mainSO->_is_nil() )
1761 it = myCurrentStudy->NewChildIterator( mainSO );
1762 if ( !it->_is_nil() ) {
1763 for ( it->InitEx(true); SObj->_is_nil() && it->More(); it->Next() ) {
1764 GEOM::GEOM_Object_var subGeom =
1765 GEOM::GEOM_Object::_narrow( SObjectToObject( it->Value() ));
1766 if ( !subGeom->_is_nil() ) {
1767 GEOM::ListOfLong_var subList = subGeom->GetSubShapeIndices();
1768 if ( subList->length() == 1 && list[0] == subList[0] ) {
1777 if ( SObj->_is_nil() ) // publish a new subshape
1778 SObj = geomGen->AddInStudy( myCurrentStudy, geom, theGeomName, mainShape );
1780 // return only published geometry
1781 if ( !SObj->_is_nil() )
1782 return geom._retn();
1784 return GEOM::GEOM_Object::_nil();
1787 //================================================================================
1789 * \brief Return geometrical object the given element is built on.
1790 * \param theMesh - the mesh the element is in
1791 * \param theElementID - the element ID
1792 * \retval GEOM::GEOM_Object_ptr - the found geom object
1794 //================================================================================
1796 GEOM::GEOM_Object_ptr
1797 SMESH_Gen_i::FindGeometryByMeshElement( SMESH::SMESH_Mesh_ptr theMesh,
1798 CORBA::Long theElementID)
1799 throw ( SALOME::SALOME_Exception )
1801 Unexpect aCatch(SALOME_SalomeException);
1802 if ( CORBA::is_nil( theMesh ) )
1803 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference", SALOME::BAD_PARAM );
1805 GEOM::GEOM_Object_var mainShape = theMesh->GetShapeToMesh();
1806 GEOM::GEOM_Gen_ptr geomGen = GetGeomEngine();
1808 // get a core mesh DS
1809 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
1810 if ( meshServant && !geomGen->_is_nil() && !mainShape->_is_nil() )
1812 ::SMESH_Mesh & mesh = meshServant->GetImpl();
1813 SMESHDS_Mesh* meshDS = mesh.GetMeshDS();
1814 // find the element in mesh
1815 if ( const SMDS_MeshElement * elem = meshDS->FindElement( theElementID ) ) {
1816 // find a shape id by the element
1817 if ( int shapeID = ::SMESH_MeshEditor( &mesh ).FindShape( elem )) {
1818 // get a geom object by the shape id
1819 GEOM::GEOM_Object_var geom = ShapeToGeomObject( meshDS->IndexToShape( shapeID ));
1820 if ( geom->_is_nil() ) {
1821 // try to find a published sub-shape
1822 SALOMEDS::SObject_var mainSO = ObjectToSObject( myCurrentStudy, mainShape );
1823 SALOMEDS::ChildIterator_var it;
1824 if ( !mainSO->_is_nil() )
1825 it = myCurrentStudy->NewChildIterator( mainSO );
1826 if ( !it->_is_nil() ) {
1827 for ( it->InitEx(true); it->More(); it->Next() ) {
1828 GEOM::GEOM_Object_var subGeom =
1829 GEOM::GEOM_Object::_narrow( SObjectToObject( it->Value() ));
1830 if ( !subGeom->_is_nil() ) {
1831 GEOM::ListOfLong_var subList = subGeom->GetSubShapeIndices();
1832 if ( subList->length() == 1 && shapeID == subList[0] ) {
1840 if ( geom->_is_nil() ) {
1842 GEOM::GEOM_IShapesOperations_var op =
1843 geomGen->GetIShapesOperations( GetCurrentStudyID() );
1844 if ( !op->_is_nil() )
1845 geom = op->GetSubShape( mainShape, shapeID );
1847 if ( !geom->_is_nil() ) {
1848 GeomObjectToShape( geom ); // let geom client remember the found shape
1849 return geom._retn();
1854 return GEOM::GEOM_Object::_nil();
1857 //================================================================================
1859 * SMESH_Gen_i::Concatenate
1861 * Concatenate the given meshes into one mesh
1863 //================================================================================
1865 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::Concatenate(const SMESH::mesh_array& theMeshesArray,
1866 CORBA::Boolean theUniteIdenticalGroups,
1867 CORBA::Boolean theMergeNodesAndElements,
1868 CORBA::Double theMergeTolerance)
1869 throw ( SALOME::SALOME_Exception )
1871 return ConcatenateCommon(theMeshesArray,
1872 theUniteIdenticalGroups,
1873 theMergeNodesAndElements,
1878 //================================================================================
1880 * SMESH_Gen_i::ConcatenateWithGroups
1882 * Concatenate the given meshes into one mesh
1883 * Create the groups of all elements from initial meshes
1885 //================================================================================
1887 SMESH::SMESH_Mesh_ptr
1888 SMESH_Gen_i::ConcatenateWithGroups(const SMESH::mesh_array& theMeshesArray,
1889 CORBA::Boolean theUniteIdenticalGroups,
1890 CORBA::Boolean theMergeNodesAndElements,
1891 CORBA::Double theMergeTolerance)
1892 throw ( SALOME::SALOME_Exception )
1894 return ConcatenateCommon(theMeshesArray,
1895 theUniteIdenticalGroups,
1896 theMergeNodesAndElements,
1901 //================================================================================
1903 * SMESH_Gen_i::ConcatenateCommon
1905 * Concatenate the given meshes into one mesh
1907 //================================================================================
1909 SMESH::SMESH_Mesh_ptr
1910 SMESH_Gen_i::ConcatenateCommon(const SMESH::mesh_array& theMeshesArray,
1911 CORBA::Boolean theUniteIdenticalGroups,
1912 CORBA::Boolean theMergeNodesAndElements,
1913 CORBA::Double theMergeTolerance,
1914 CORBA::Boolean theCommonGroups)
1915 throw ( SALOME::SALOME_Exception )
1917 typedef map<int, int> TIDsMap;
1918 typedef list<SMESH::SMESH_Group_var> TListOfNewGroups;
1919 typedef map< pair<string, SMESH::ElementType>, TListOfNewGroups > TGroupsMap;
1920 typedef std::set<SMESHDS_GroupBase*> TGroups;
1922 TPythonDump* pPythonDump = new TPythonDump;
1923 TPythonDump& aPythonDump = *pPythonDump; // prevent dump of called methods
1926 SMESH::SMESH_Mesh_var aNewMesh = CreateEmptyMesh();
1928 SMESHDS_Mesh* aNewMeshDS = 0;
1929 if ( !aNewMesh->_is_nil() ) {
1930 SMESH_Mesh_i* aNewImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( aNewMesh ).in() );
1932 ::SMESH_Mesh& aLocMesh = aNewImpl->GetImpl();
1933 aNewMeshDS = aLocMesh.GetMeshDS();
1935 TGroupsMap aGroupsMap;
1936 TListOfNewGroups aListOfNewGroups;
1937 SMESH_MeshEditor aNewEditor = ::SMESH_MeshEditor(&aLocMesh);
1938 SMESH::ListOfGroups_var aListOfGroups = new SMESH::ListOfGroups();
1941 for ( int i = 0; i < theMeshesArray.length(); i++) {
1942 SMESH::SMESH_Mesh_var anInitMesh = theMeshesArray[i];
1943 if ( !anInitMesh->_is_nil() ) {
1944 SMESH_Mesh_i* anInitImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( anInitMesh ).in() );
1946 ::SMESH_Mesh& aInitLocMesh = anInitImpl->GetImpl();
1947 SMESHDS_Mesh* anInitMeshDS = aInitLocMesh.GetMeshDS();
1952 // loop on elements of mesh
1953 SMDS_ElemIteratorPtr itElems = anInitMeshDS->elementsIterator();
1954 const SMDS_MeshElement* anElem = 0;
1955 const SMDS_MeshElement* aNewElem = 0;
1956 int anElemNbNodes = 0;
1961 int anNbVolumes = 0;
1963 SMESH::long_array_var anIDsNodes = new SMESH::long_array();
1964 SMESH::long_array_var anIDsEdges = new SMESH::long_array();
1965 SMESH::long_array_var anIDsFaces = new SMESH::long_array();
1966 SMESH::long_array_var anIDsVolumes = new SMESH::long_array();
1968 if( theCommonGroups ) {
1969 anIDsNodes->length( anInitMeshDS->NbNodes() );
1970 anIDsEdges->length( anInitMeshDS->NbEdges() );
1971 anIDsFaces->length( anInitMeshDS->NbFaces() );
1972 anIDsVolumes->length( anInitMeshDS->NbVolumes() );
1975 for ( int j = 0; itElems->more(); j++) {
1976 anElem = itElems->next();
1977 SMDSAbs_ElementType anElemType = anElem->GetType();
1978 anElemNbNodes = anElem->NbNodes();
1979 std::vector<const SMDS_MeshNode*> aNodesArray (anElemNbNodes);
1981 // loop on nodes of element
1982 const SMDS_MeshNode* aNode = 0;
1983 const SMDS_MeshNode* aNewNode = 0;
1984 SMDS_ElemIteratorPtr itNodes = anElem->nodesIterator();
1986 for ( int k = 0; itNodes->more(); k++) {
1987 aNode = static_cast<const SMDS_MeshNode*>(itNodes->next());
1988 if ( nodesMap.find(aNode->GetID()) == nodesMap.end() ) {
1989 aNewNode = aNewMeshDS->AddNode(aNode->X(), aNode->Y(), aNode->Z());
1990 nodesMap.insert( make_pair(aNode->GetID(), aNewNode->GetID()) );
1991 if( theCommonGroups )
1992 anIDsNodes[anNbNodes++] = aNewNode->GetID();
1995 aNewNode = aNewMeshDS->FindNode( nodesMap.find(aNode->GetID())->second );
1996 aNodesArray[k] = aNewNode;
1999 // creates a corresponding element on existent nodes in new mesh
2000 if ( anElem->IsPoly() && anElemType == SMDSAbs_Volume )
2002 const SMDS_VtkVolume* aVolume =
2003 dynamic_cast<const SMDS_VtkVolume*> (anElem);
2005 aNewElem = aNewMeshDS->AddPolyhedralVolume(aNodesArray,
2006 aVolume->GetQuantities());
2007 elemsMap.insert(make_pair(anElem->GetID(), aNewElem->GetID()));
2008 if( theCommonGroups )
2009 anIDsVolumes[anNbVolumes++] = aNewElem->GetID();
2014 aNewElem = aNewEditor.AddElement(aNodesArray,
2017 elemsMap.insert(make_pair(anElem->GetID(), aNewElem->GetID()));
2018 if( theCommonGroups ) {
2019 if( anElemType == SMDSAbs_Edge )
2020 anIDsEdges[anNbEdges++] = aNewElem->GetID();
2021 else if( anElemType == SMDSAbs_Face )
2022 anIDsFaces[anNbFaces++] = aNewElem->GetID();
2023 else if( anElemType == SMDSAbs_Volume )
2024 anIDsVolumes[anNbVolumes++] = aNewElem->GetID();
2029 // copy orphan nodes
2030 SMDS_NodeIteratorPtr itNodes = anInitMeshDS->nodesIterator();
2031 while ( itNodes->more() )
2033 const SMDS_MeshNode* aNode = itNodes->next();
2034 if ( aNode->NbInverseElements() == 0 )
2036 const SMDS_MeshNode* aNewNode =
2037 aNewMeshDS->AddNode(aNode->X(), aNode->Y(), aNode->Z());
2038 nodesMap.insert( make_pair(aNode->GetID(), aNewNode->GetID()) );
2039 if( theCommonGroups )
2040 anIDsNodes[anNbNodes++] = aNewNode->GetID();
2045 aListOfGroups = anInitImpl->GetGroups();
2046 SMESH::SMESH_GroupBase_ptr aGroup;
2048 // loop on groups of mesh
2049 SMESH::long_array_var anInitIDs = new SMESH::long_array();
2050 SMESH::long_array_var anNewIDs = new SMESH::long_array();
2051 SMESH::SMESH_Group_var aNewGroup;
2053 SMESH::ElementType aGroupType;
2054 CORBA::String_var aGroupName;
2055 if ( theCommonGroups ) {
2056 for(aGroupType=SMESH::NODE;aGroupType<=SMESH::VOLUME;aGroupType=(SMESH::ElementType)(aGroupType+1)) {
2058 SALOMEDS::SObject_var aMeshSObj = ObjectToSObject( myCurrentStudy, anInitMesh );
2060 str += aMeshSObj->GetName();
2065 switch(aGroupType) {
2068 anIDsNodes->length(anNbNodes);
2073 anIDsEdges->length(anNbEdges);
2078 anIDsFaces->length(anNbFaces);
2083 anIDsVolumes->length(anNbVolumes);
2084 anLen = anNbVolumes;
2091 aGroupName = str.c_str();
2093 // add a new group in the mesh
2094 aNewGroup = aNewImpl->CreateGroup(aGroupType, aGroupName);
2096 switch(aGroupType) {
2098 aNewGroup->Add( anIDsNodes );
2101 aNewGroup->Add( anIDsEdges );
2104 aNewGroup->Add( anIDsFaces );
2107 aNewGroup->Add( anIDsVolumes );
2113 aListOfNewGroups.clear();
2114 aListOfNewGroups.push_back(aNewGroup);
2115 aGroupsMap.insert(make_pair( make_pair(aGroupName, aGroupType), aListOfNewGroups ));
2120 // check that current group name and type don't have identical ones in union mesh
2121 for (int i = 0; i < aListOfGroups->length(); i++) {
2122 aGroup = aListOfGroups[i];
2123 aListOfNewGroups.clear();
2124 aGroupType = aGroup->GetType();
2125 aGroupName = aGroup->GetName();
2127 TGroupsMap::iterator anIter = aGroupsMap.find(make_pair(aGroupName, aGroupType));
2129 // convert a list of IDs
2130 anInitIDs = aGroup->GetListOfID();
2131 anNewIDs->length(anInitIDs->length());
2132 if ( aGroupType == SMESH::NODE )
2133 for (int j = 0; j < anInitIDs->length(); j++) {
2134 anNewIDs[j] = nodesMap.find(anInitIDs[j])->second;
2137 for (int j = 0; j < anInitIDs->length(); j++) {
2138 anNewIDs[j] = elemsMap.find(anInitIDs[j])->second;
2141 // check that current group name and type don't have identical ones in union mesh
2142 if ( anIter == aGroupsMap.end() ) {
2143 // add a new group in the mesh
2144 aNewGroup = aNewImpl->CreateGroup(aGroupType, aGroupName);
2145 // add elements into new group
2146 aNewGroup->Add( anNewIDs );
2148 aListOfNewGroups.push_back(aNewGroup);
2149 aGroupsMap.insert(make_pair( make_pair(aGroupName, aGroupType), aListOfNewGroups ));
2152 else if ( theUniteIdenticalGroups ) {
2153 // unite identical groups
2154 TListOfNewGroups& aNewGroups = anIter->second;
2155 aNewGroups.front()->Add( anNewIDs );
2159 // rename identical groups
2160 aNewGroup = aNewImpl->CreateGroup(aGroupType, aGroupName);
2161 aNewGroup->Add( anNewIDs );
2163 TListOfNewGroups& aNewGroups = anIter->second;
2164 string aNewGroupName;
2165 if (aNewGroups.size() == 1) {
2166 aNewGroupName = string(aGroupName) + "_1";
2167 aNewGroups.front()->SetName(aNewGroupName.c_str());
2169 char aGroupNum[128];
2170 sprintf(aGroupNum, "%u", aNewGroups.size()+1);
2171 aNewGroupName = string(aGroupName) + "_" + string(aGroupNum);
2172 aNewGroup->SetName(aNewGroupName.c_str());
2173 aNewGroups.push_back(aNewGroup);
2180 if (theMergeNodesAndElements) {
2182 TIDSortedNodeSet aMeshNodes; // no input nodes
2183 SMESH_MeshEditor::TListOfListOfNodes aGroupsOfNodes;
2184 aNewEditor.FindCoincidentNodes( aMeshNodes, theMergeTolerance, aGroupsOfNodes );
2185 aNewEditor.MergeNodes( aGroupsOfNodes );
2187 aNewEditor.MergeEqualElements();
2192 // Update Python script
2193 aPythonDump << aNewMesh << " = " << this;
2194 if( !theCommonGroups )
2195 aPythonDump << ".Concatenate(";
2197 aPythonDump << ".ConcatenateWithGroups(";
2199 for ( int i = 0; i < theMeshesArray.length(); i++) {
2200 if (i > 0) aPythonDump << ", ";
2201 aPythonDump << theMeshesArray[i];
2203 aPythonDump << "], ";
2204 aPythonDump << theUniteIdenticalGroups << ", "
2205 << theMergeNodesAndElements << ", "
2206 << theMergeTolerance << ")";
2208 delete pPythonDump; // enable python dump from GetGroups()
2210 // 0020577: EDF 1164 SMESH: Bad dump of concatenate with create common groups
2211 if ( !aNewMesh->_is_nil() )
2213 SMESH::ListOfGroups_var groups = aNewMesh->GetGroups();
2216 // IPAL21468 Change icon of compound because it need not be computed.
2217 SALOMEDS::SObject_var aMeshSObj = ObjectToSObject( myCurrentStudy, aNewMesh );
2218 if( !aMeshSObj->_is_nil() ) {
2219 SALOMEDS::GenericAttribute_var anAttr;
2220 SALOMEDS::StudyBuilder_var aBuilder = myCurrentStudy->NewBuilder();
2221 anAttr = aBuilder->FindOrCreateAttribute( aMeshSObj,"AttributePixMap" );
2222 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
2223 aPixmap->SetPixMap("ICON_SMESH_TREE_MESH");
2226 aNewMeshDS->Modified();
2227 return aNewMesh._retn();
2230 //================================================================================
2232 * \brief Create a mesh by copying a part of another mesh
2233 * \param meshPart - a part of mesh to copy
2234 * \param toCopyGroups - to create in the new mesh groups
2235 * the copied elements belongs to
2236 * \param toKeepIDs - to preserve IDs of the copied elements or not
2237 * \retval SMESH::SMESH_Mesh_ptr - the new mesh
2239 //================================================================================
2241 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CopyMesh(SMESH::SMESH_IDSource_ptr meshPart,
2242 const char* meshName,
2243 CORBA::Boolean toCopyGroups,
2244 CORBA::Boolean toKeepIDs)
2246 Unexpect aCatch(SALOME_SalomeException);
2248 TPythonDump* pyDump = new TPythonDump; // prevent dump from CreateMesh()
2250 // 1. Get source mesh
2252 if ( CORBA::is_nil( meshPart ))
2253 THROW_SALOME_CORBA_EXCEPTION( "bad IDSource", SALOME::BAD_PARAM );
2255 SMESH::SMESH_Mesh_var srcMesh = meshPart->GetMesh();
2256 SMESH_Mesh_i* srcMesh_i = SMESH::DownCast<SMESH_Mesh_i*>( srcMesh );
2258 THROW_SALOME_CORBA_EXCEPTION( "bad mesh of IDSource", SALOME::BAD_PARAM );
2260 SMESHDS_Mesh* srcMeshDS = srcMesh_i->GetImpl().GetMeshDS();
2262 // 2. Make a new mesh
2264 SMESH::SMESH_Mesh_var newMesh = CreateMesh(GEOM::GEOM_Object::_nil());
2265 SMESH_Mesh_i* newMesh_i = SMESH::DownCast<SMESH_Mesh_i*>( newMesh );
2267 THROW_SALOME_CORBA_EXCEPTION( "can't create a mesh", SALOME::INTERNAL_ERROR );
2268 SALOMEDS::SObject_var meshSO = ObjectToSObject(myCurrentStudy, newMesh );
2269 if ( !meshSO->_is_nil() )
2271 SetName( meshSO, meshName, "Mesh" );
2272 SetPixMap( meshSO, "ICON_SMESH_TREE_MESH_IMPORTED");
2274 SMESHDS_Mesh* newMeshDS = newMesh_i->GetImpl().GetMeshDS();
2275 ::SMESH_MeshEditor editor( &newMesh_i->GetImpl() );
2277 // 3. Get elements to copy
2279 SMDS_ElemIteratorPtr srcElemIt;
2280 TIDSortedElemSet srcElems;
2281 SMESH::array_of_ElementType_var srcElemTypes = meshPart->GetTypes();
2282 if ( SMESH::DownCast<SMESH_Mesh_i*>( meshPart ))
2284 srcElemIt = srcMeshDS->elementsIterator();
2288 SMESH::long_array_var ids = meshPart->GetIDs();
2289 if ( srcElemTypes->length() == 1 && srcElemTypes[0] == SMESH::NODE ) // group of nodes
2291 for (int i=0; i < ids->length(); i++)
2292 if ( const SMDS_MeshElement * elem = srcMeshDS->FindNode( ids[i] ))
2293 srcElems.insert( elem );
2297 for (int i=0; i < ids->length(); i++)
2298 if ( const SMDS_MeshElement * elem = srcMeshDS->FindElement( ids[i] ))
2299 srcElems.insert( elem );
2301 if ( srcElems.empty() )
2302 return newMesh._retn();
2304 typedef SMDS_SetIterator< SMDS_pElement, TIDSortedElemSet::const_iterator > ElIter;
2305 srcElemIt = SMDS_ElemIteratorPtr( new ElIter( srcElems.begin(), srcElems.end() ));
2310 typedef map<SMDS_pElement, SMDS_pElement, TIDCompare> TE2EMap;
2311 TE2EMap e2eMapByType[ SMDSAbs_NbElementTypes ];
2312 TE2EMap& n2nMap = e2eMapByType[ SMDSAbs_Node ];
2314 const SMDS_MeshNode *nSrc, *nTgt;
2315 vector< const SMDS_MeshNode* > nodes;
2316 while ( srcElemIt->more() )
2318 const SMDS_MeshElement * elem = srcElemIt->next();
2319 nodes.resize( elem->NbNodes());
2320 SMDS_ElemIteratorPtr nIt = elem->nodesIterator();
2322 for ( iN = 0; nIt->more(); ++iN )
2324 nSrc = static_cast<const SMDS_MeshNode*>( nIt->next() );
2325 nTgt = newMeshDS->FindNode( nSrc->GetID());
2327 nTgt = newMeshDS->AddNodeWithID( nSrc->X(), nSrc->Y(), nSrc->Z(), nSrc->GetID());
2332 for ( iN = 0; nIt->more(); ++iN )
2334 nSrc = static_cast<const SMDS_MeshNode*>( nIt->next() );
2335 TE2EMap::iterator n2n = n2nMap.insert( make_pair( nSrc, SMDS_pNode(0) )).first;
2337 n2n->second = newMeshDS->AddNode( nSrc->X(), nSrc->Y(), nSrc->Z() );
2338 nodes[ iN ] = (const SMDS_MeshNode*) n2n->second;
2341 if ( elem->GetType() != SMDSAbs_Node )
2343 int ID = toKeepIDs ? elem->GetID() : 0;
2344 const SMDS_MeshElement * newElem = editor.AddElement( nodes,
2348 if ( toCopyGroups && !toKeepIDs )
2349 e2eMapByType[ elem->GetType() ].insert( make_pair( elem, newElem ));
2355 int nbNewGroups = 0;
2358 SMESH_Mesh::GroupIteratorPtr gIt = srcMesh_i->GetImpl().GetGroups();
2359 while ( gIt->more() )
2361 SMESH_Group* group = gIt->next();
2362 const SMESHDS_GroupBase* groupDS = group->GetGroupDS();
2364 // Check group type. We copy nodal groups containing nodes of copied element
2365 SMDSAbs_ElementType groupType = groupDS->GetType();
2366 if ( groupType != SMDSAbs_Node &&
2367 newMeshDS->GetMeshInfo().NbElements( groupType ) == 0 )
2368 continue; // group type differs from types of meshPart
2370 // Find copied elements in the group
2371 vector< const SMDS_MeshElement* > groupElems;
2372 SMDS_ElemIteratorPtr eIt = groupDS->GetElements();
2375 const SMDS_MeshElement* foundElem;
2376 if ( groupType == SMDSAbs_Node )
2378 while ( eIt->more() )
2379 if (( foundElem = newMeshDS->FindNode( eIt->next()->GetID() )))
2380 groupElems.push_back( foundElem );
2384 while ( eIt->more() )
2385 if (( foundElem = newMeshDS->FindElement( eIt->next()->GetID() )))
2386 groupElems.push_back( foundElem );
2391 TE2EMap & e2eMap = e2eMapByType[ groupDS->GetType() ];
2392 if ( e2eMap.empty() ) continue;
2393 int minID = e2eMap.begin()->first->GetID();
2394 int maxID = e2eMap.rbegin()->first->GetID();
2395 TE2EMap::iterator e2e;
2396 while ( eIt->more() && groupElems.size() < e2eMap.size())
2398 const SMDS_MeshElement* e = eIt->next();
2399 if ( e->GetID() < minID || e->GetID() > maxID ) continue;
2400 if ((e2e = e2eMap.find( e )) != e2eMap.end())
2401 groupElems.push_back( e2e->second );
2405 if ( !groupElems.empty() )
2407 SMESH::SMESH_Group_var newGroupObj =
2408 newMesh->CreateGroup( SMESH::ElementType(groupType), group->GetName() );
2409 if ( SMESH_GroupBase_i* newGroup_i = SMESH::DownCast<SMESH_GroupBase_i*>( newGroupObj))
2411 SMESHDS_GroupBase * newGroupDS = newGroup_i->GetGroupDS();
2412 SMDS_MeshGroup& smdsGroup = ((SMESHDS_Group*)newGroupDS)->SMDSGroup();
2413 for ( unsigned i = 0; i < groupElems.size(); ++i )
2414 smdsGroup.Add( groupElems[i] );
2422 *pyDump << newMesh << " = " << this
2423 << ".CopyMesh( " << meshPart << ", "
2424 << "'" << meshName << "', "
2425 << toCopyGroups << ", "
2426 << toKeepIDs << ")";
2428 delete pyDump; pyDump = 0; // allow dump in GetGroups()
2430 if ( nbNewGroups > 0 ) // dump created groups
2431 SMESH::ListOfGroups_var groups = newMesh->GetGroups();
2433 return newMesh._retn();
2436 //================================================================================
2438 * SMESH_Gen_i::GetMEDVersion
2440 * Get MED version of the file by its name
2442 //================================================================================
2443 CORBA::Boolean SMESH_Gen_i::GetMEDVersion(const char* theFileName,
2444 SMESH::MED_VERSION& theVersion)
2446 theVersion = SMESH::MED_V2_1;
2447 MED::EVersion aVersion = MED::GetVersionId( theFileName );
2448 switch( aVersion ) {
2449 case MED::eV2_1 : theVersion = SMESH::MED_V2_1; return true;
2450 case MED::eV2_2 : theVersion = SMESH::MED_V2_2; return true;
2451 case MED::eVUnknown : return false;
2456 //================================================================================
2458 * SMESH_Gen_i::GetMeshNames
2460 * Get names of meshes defined in file with the specified name
2462 //================================================================================
2463 SMESH::string_array* SMESH_Gen_i::GetMeshNames(const char* theFileName)
2465 SMESH::string_array_var aResult = new SMESH::string_array();
2466 MED::PWrapper aMed = MED::CrWrapper( theFileName );
2468 MED::TInt aNbMeshes = aMed->GetNbMeshes( &anErr );
2470 aResult->length( aNbMeshes );
2471 for( MED::TInt i = 0; i < aNbMeshes; i++ ) {
2472 MED::PMeshInfo aMeshInfo = aMed->GetPMeshInfo( i+1 );
2473 aResult[i] = CORBA::string_dup( aMeshInfo->GetName().c_str() );
2476 return aResult._retn();
2479 //=============================================================================
2483 * Save SMESH module's data
2485 //=============================================================================
2486 SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
2490 INFOS( "SMESH_Gen_i::Save" );
2492 // ASSERT( theComponent->GetStudy()->StudyId() == myCurrentStudy->StudyId() )
2493 // san -- in case <myCurrentStudy> differs from theComponent's study,
2494 // use that of the component
2495 if ( myCurrentStudy->_is_nil() ||
2496 theComponent->GetStudy()->StudyId() != myCurrentStudy->StudyId() )
2497 SetCurrentStudy( theComponent->GetStudy() );
2499 // Store study contents as a set of python commands
2500 SavePython(myCurrentStudy);
2502 StudyContext* myStudyContext = GetCurrentStudyContext();
2504 // Declare a byte stream
2505 SALOMEDS::TMPFile_var aStreamFile;
2507 // Obtain a temporary dir
2508 TCollection_AsciiString tmpDir =
2509 ( isMultiFile ) ? TCollection_AsciiString( ( char* )theURL ) : ( char* )SALOMEDS_Tool::GetTmpDir().c_str();
2511 // Create a sequence of files processed
2512 SALOMEDS::ListOfFileNames_var aFileSeq = new SALOMEDS::ListOfFileNames;
2513 aFileSeq->length( NUM_TMP_FILES );
2515 TCollection_AsciiString aStudyName( "" );
2517 aStudyName = ( (char*)SALOMEDS_Tool::GetNameFromPath( myCurrentStudy->URL() ).c_str() );
2519 // Set names of temporary files
2520 TCollection_AsciiString filename =
2521 aStudyName + TCollection_AsciiString( "_SMESH.hdf" ); // for SMESH data itself
2522 TCollection_AsciiString meshfile =
2523 aStudyName + TCollection_AsciiString( "_SMESH_Mesh.med" ); // for mesh data to be stored in MED file
2524 aFileSeq[ 0 ] = CORBA::string_dup( filename.ToCString() );
2525 aFileSeq[ 1 ] = CORBA::string_dup( meshfile.ToCString() );
2526 filename = tmpDir + filename;
2527 meshfile = tmpDir + meshfile;
2530 HDFdataset* aDataset;
2531 HDFgroup* aTopGroup;
2533 HDFgroup* aSubGroup;
2534 HDFgroup* aSubSubGroup;
2535 hdf_size aSize[ 1 ];
2538 //Remove the files if they exist: BugID: 11225
2539 #ifndef WNT /* unix functionality */
2540 TCollection_AsciiString cmd("rm -f \"");
2542 TCollection_AsciiString cmd("del /F \"");
2549 system(cmd.ToCString());
2551 // MED writer to be used by storage process
2552 DriverMED_W_SMESHDS_Mesh myWriter;
2553 myWriter.SetFile( meshfile.ToCString() );
2556 // SetStoreName() to groups before storing hypotheses to let them refer to
2557 // groups using "store name", which is "Group <group_persistent_id>"
2559 SALOMEDS::ChildIterator_var itBig = myCurrentStudy->NewChildIterator( theComponent );
2560 for ( ; itBig->More(); itBig->Next() ) {
2561 SALOMEDS::SObject_var gotBranch = itBig->Value();
2562 if ( gotBranch->Tag() > GetAlgorithmsRootTag() ) {
2563 CORBA::Object_var anObject = SObjectToObject( gotBranch );
2564 if ( !CORBA::is_nil( anObject ) ) {
2565 SMESH::SMESH_Mesh_var myMesh = SMESH::SMESH_Mesh::_narrow( anObject ) ;
2566 if ( !myMesh->_is_nil() ) {
2567 SMESH::ListOfGroups_var groups = myMesh->GetGroups();
2568 for ( int i = 0; i < groups->length(); ++i )
2570 SMESH_GroupBase_i* grImpl = SMESH::DownCast<SMESH_GroupBase_i*>( groups[i]);
2573 CORBA::String_var objStr = GetORB()->object_to_string( grImpl->_this() );
2574 int anId = myStudyContext->findId( string( objStr.in() ) );
2576 sprintf( grpName, "Group %d", anId );
2577 SMESHDS_GroupBase* aGrpBaseDS = grImpl->GetGroupDS();
2578 aGrpBaseDS->SetStoreName( grpName );
2588 // ---> create HDF file
2589 aFile = new HDFfile( (char*) filename.ToCString() );
2590 aFile->CreateOnDisk();
2592 // --> iterator for top-level objects
2593 SALOMEDS::ChildIterator_var itBig = myCurrentStudy->NewChildIterator( theComponent );
2594 for ( ; itBig->More(); itBig->Next() ) {
2595 SALOMEDS::SObject_var gotBranch = itBig->Value();
2597 // --> hypotheses root branch (only one for the study)
2598 if ( gotBranch->Tag() == GetHypothesisRootTag() ) {
2599 // create hypotheses root HDF group
2600 aTopGroup = new HDFgroup( "Hypotheses", aFile );
2601 aTopGroup->CreateOnDisk();
2603 // iterator for all hypotheses
2604 SALOMEDS::ChildIterator_var it = myCurrentStudy->NewChildIterator( gotBranch );
2605 for ( ; it->More(); it->Next() ) {
2606 SALOMEDS::SObject_var mySObject = it->Value();
2607 CORBA::Object_var anObject = SObjectToObject( mySObject );
2608 if ( !CORBA::is_nil( anObject ) ) {
2609 SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
2610 if ( !myHyp->_is_nil() ) {
2611 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
2613 string hypname = string( myHyp->GetName() );
2614 string libname = string( myHyp->GetLibName() );
2616 // Needs for save crossplatform libname, i.e. parth of name ( ".dll" for
2617 // WNT and ".so" for X-system) must be deleted
2618 int libname_len = libname.length();
2620 if( libname_len > 4 )
2621 libname.resize( libname_len - 4 );
2623 // PAL17753 (Regresion: missing hypothesis in restored study)
2624 // "lib" also should be removed from the beginning
2625 //if( libname_len > 3 )
2626 //libname.resize( libname_len - 3 );
2627 if( libname_len > 6 )
2628 libname = libname.substr( 3, libname_len - 3 - 3 );
2630 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
2631 int id = myStudyContext->findId( string( objStr.in() ) );
2632 string hypdata = string( myImpl->SaveTo() );
2634 // for each hypothesis create HDF group basing on its id
2635 char hypGrpName[30];
2636 sprintf( hypGrpName, "Hypothesis %d", id );
2637 aGroup = new HDFgroup( hypGrpName, aTopGroup );
2638 aGroup->CreateOnDisk();
2639 // --> type name of hypothesis
2640 aSize[ 0 ] = hypname.length() + 1;
2641 aDataset = new HDFdataset( "Name", aGroup, HDF_STRING, aSize, 1 );
2642 aDataset->CreateOnDisk();
2643 aDataset->WriteOnDisk( ( char* )( hypname.c_str() ) );
2644 aDataset->CloseOnDisk();
2645 // --> server plugin library name of hypothesis
2646 aSize[ 0 ] = libname.length() + 1;
2647 aDataset = new HDFdataset( "LibName", aGroup, HDF_STRING, aSize, 1 );
2648 aDataset->CreateOnDisk();
2649 aDataset->WriteOnDisk( ( char* )( libname.c_str() ) );
2650 aDataset->CloseOnDisk();
2651 // --> persistent data of hypothesis
2652 aSize[ 0 ] = hypdata.length() + 1;
2653 aDataset = new HDFdataset( "Data", aGroup, HDF_STRING, aSize, 1 );
2654 aDataset->CreateOnDisk();
2655 aDataset->WriteOnDisk( ( char* )( hypdata.c_str() ) );
2656 aDataset->CloseOnDisk();
2657 // close hypothesis HDF group
2658 aGroup->CloseOnDisk();
2663 // close hypotheses root HDF group
2664 aTopGroup->CloseOnDisk();
2666 // --> algorithms root branch (only one for the study)
2667 else if ( gotBranch->Tag() == GetAlgorithmsRootTag() ) {
2668 // create algorithms root HDF group
2669 aTopGroup = new HDFgroup( "Algorithms", aFile );
2670 aTopGroup->CreateOnDisk();
2672 // iterator for all algorithms
2673 SALOMEDS::ChildIterator_var it = myCurrentStudy->NewChildIterator( gotBranch );
2674 for ( ; it->More(); it->Next() ) {
2675 SALOMEDS::SObject_var mySObject = it->Value();
2676 CORBA::Object_var anObject = SObjectToObject( mySObject );
2677 if ( !CORBA::is_nil( anObject ) ) {
2678 SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
2679 if ( !myHyp->_is_nil() ) {
2680 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
2682 string hypname = string( myHyp->GetName() );
2683 string libname = string( myHyp->GetLibName() );
2685 // Needs for save crossplatform libname, i.e. parth of name ( ".dll" for
2686 // WNT and ".so" for X-system) must be deleted
2687 int libname_len = libname.length();
2689 if( libname_len > 4 )
2690 libname.resize( libname_len - 4 );
2692 // PAL17753 (Regresion: missing hypothesis in restored study)
2693 // "lib" also should be removed from the beginning
2694 //if( libname_len > 3 )
2695 //libname.resize( libname_len - 3 );
2696 if( libname_len > 6 )
2697 libname = libname.substr( 3, libname_len - 3 - 3 );
2699 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
2700 int id = myStudyContext->findId( string( objStr.in() ) );
2701 string hypdata = string( myImpl->SaveTo() );
2703 // for each algorithm create HDF group basing on its id
2704 char hypGrpName[30];
2705 sprintf( hypGrpName, "Algorithm %d", id );
2706 aGroup = new HDFgroup( hypGrpName, aTopGroup );
2707 aGroup->CreateOnDisk();
2708 // --> type name of algorithm
2709 aSize[0] = hypname.length() + 1;
2710 aDataset = new HDFdataset( "Name", aGroup, HDF_STRING, aSize, 1 );
2711 aDataset->CreateOnDisk();
2712 aDataset->WriteOnDisk( ( char* )( hypname.c_str() ) );
2713 aDataset->CloseOnDisk();
2714 // --> server plugin library name of hypothesis
2715 aSize[0] = libname.length() + 1;
2716 aDataset = new HDFdataset( "LibName", aGroup, HDF_STRING, aSize, 1 );
2717 aDataset->CreateOnDisk();
2718 aDataset->WriteOnDisk( ( char* )( libname.c_str() ) );
2719 aDataset->CloseOnDisk();
2720 // --> persistent data of algorithm
2721 aSize[0] = hypdata.length() + 1;
2722 aDataset = new HDFdataset( "Data", aGroup, HDF_STRING, aSize, 1 );
2723 aDataset->CreateOnDisk();
2724 aDataset->WriteOnDisk( ( char* )( hypdata.c_str() ) );
2725 aDataset->CloseOnDisk();
2726 // close algorithm HDF group
2727 aGroup->CloseOnDisk();
2732 // close algorithms root HDF group
2733 aTopGroup->CloseOnDisk();
2735 // --> mesh objects roots branches
2736 else if ( gotBranch->Tag() > GetAlgorithmsRootTag() ) {
2737 CORBA::Object_var anObject = SObjectToObject( gotBranch );
2738 if ( !CORBA::is_nil( anObject ) ) {
2739 SMESH::SMESH_Mesh_var myMesh = SMESH::SMESH_Mesh::_narrow( anObject ) ;
2740 if ( !myMesh->_is_nil() ) {
2741 SMESH_Mesh_i* myImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( myMesh ).in() );
2743 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
2744 int id = myStudyContext->findId( string( objStr.in() ) );
2745 ::SMESH_Mesh& myLocMesh = myImpl->GetImpl();
2746 SMESHDS_Mesh* mySMESHDSMesh = myLocMesh.GetMeshDS();
2747 bool hasShape = myLocMesh.HasShapeToMesh();
2749 // for each mesh open the HDF group basing on its id
2750 char meshGrpName[ 30 ];
2751 sprintf( meshGrpName, "Mesh %d", id );
2752 aTopGroup = new HDFgroup( meshGrpName, aFile );
2753 aTopGroup->CreateOnDisk();
2755 // --> put dataset to hdf file which is a flag that mesh has data
2756 string strHasData = "0";
2757 // check if the mesh is not empty
2758 if ( mySMESHDSMesh->NbNodes() > 0 ) {
2759 // write mesh data to med file
2760 myWriter.SetMesh( mySMESHDSMesh );
2761 myWriter.SetMeshId( id );
2764 aSize[ 0 ] = strHasData.length() + 1;
2765 aDataset = new HDFdataset( "Has data", aTopGroup, HDF_STRING, aSize, 1 );
2766 aDataset->CreateOnDisk();
2767 aDataset->WriteOnDisk( ( char* )( strHasData.c_str() ) );
2768 aDataset->CloseOnDisk();
2771 // for each mesh open the HDF group basing on its auto color parameter
2772 char meshAutoColorName[ 30 ];
2773 sprintf( meshAutoColorName, "AutoColorMesh %d", id );
2775 anAutoColor[0] = myImpl->GetAutoColor();
2777 aDataset = new HDFdataset( meshAutoColorName, aTopGroup, HDF_INT32, aSize, 1 );
2778 aDataset->CreateOnDisk();
2779 aDataset->WriteOnDisk( anAutoColor );
2780 aDataset->CloseOnDisk();
2782 // issue 0020693. Store _isModified flag
2783 int isModified = myLocMesh.GetIsModified();
2785 aDataset = new HDFdataset( "_isModified", aTopGroup, HDF_INT32, aSize, 1 );
2786 aDataset->CreateOnDisk();
2787 aDataset->WriteOnDisk( &isModified );
2788 aDataset->CloseOnDisk();
2790 // issue 20918. Store Persistent Id of SMESHDS_Mesh
2791 int meshPersistentId = mySMESHDSMesh->GetPersistentId();
2793 aDataset = new HDFdataset( "meshPersistentId", aTopGroup, HDF_INT32, aSize, 1 );
2794 aDataset->CreateOnDisk();
2795 aDataset->WriteOnDisk( &meshPersistentId );
2796 aDataset->CloseOnDisk();
2798 // write reference on a shape if exists
2799 SALOMEDS::SObject_var myRef;
2800 bool shapeRefFound = false;
2801 bool found = gotBranch->FindSubObject( GetRefOnShapeTag(), myRef );
2803 SALOMEDS::SObject_var myShape;
2804 bool ok = myRef->ReferencedObject( myShape );
2806 shapeRefFound = (! CORBA::is_nil( myShape->GetObject() ));
2807 string myRefOnObject = myShape->GetID();
2808 if ( shapeRefFound && myRefOnObject.length() > 0 ) {
2809 aSize[ 0 ] = myRefOnObject.length() + 1;
2810 aDataset = new HDFdataset( "Ref on shape", aTopGroup, HDF_STRING, aSize, 1 );
2811 aDataset->CreateOnDisk();
2812 aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
2813 aDataset->CloseOnDisk();
2818 // write applied hypotheses if exist
2819 SALOMEDS::SObject_var myHypBranch;
2820 found = gotBranch->FindSubObject( GetRefOnAppliedHypothesisTag(), myHypBranch );
2821 if ( found && !shapeRefFound && hasShape) { // remove applied hyps
2822 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( myHypBranch );
2824 if ( found && (shapeRefFound || !hasShape) ) {
2825 aGroup = new HDFgroup( "Applied Hypotheses", aTopGroup );
2826 aGroup->CreateOnDisk();
2828 SALOMEDS::ChildIterator_var it = myCurrentStudy->NewChildIterator( myHypBranch );
2830 for ( ; it->More(); it->Next() ) {
2831 SALOMEDS::SObject_var mySObject = it->Value();
2832 SALOMEDS::SObject_var myRefOnHyp;
2833 bool ok = mySObject->ReferencedObject( myRefOnHyp );
2835 // san - it is impossible to recover applied hypotheses
2836 // using their entries within Load() method,
2837 // for there are no AttributeIORs in the study when Load() is working.
2838 // Hence, it is better to store persistent IDs of hypotheses as references to them
2840 //string myRefOnObject = myRefOnHyp->GetID();
2841 CORBA::Object_var anObject = SObjectToObject( myRefOnHyp );
2842 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
2843 int id = myStudyContext->findId( string( objStr.in() ) );
2844 //if ( myRefOnObject.length() > 0 ) {
2845 //aSize[ 0 ] = myRefOnObject.length() + 1;
2846 char hypName[ 30 ], hypId[ 30 ];
2847 sprintf( hypName, "Hyp %d", ++hypNb );
2848 sprintf( hypId, "%d", id );
2849 aSize[ 0 ] = strlen( hypId ) + 1;
2850 aDataset = new HDFdataset( hypName, aGroup, HDF_STRING, aSize, 1 );
2851 aDataset->CreateOnDisk();
2852 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
2853 aDataset->WriteOnDisk( hypId );
2854 aDataset->CloseOnDisk();
2858 aGroup->CloseOnDisk();
2861 // write applied algorithms if exist
2862 SALOMEDS::SObject_var myAlgoBranch;
2863 found = gotBranch->FindSubObject( GetRefOnAppliedAlgorithmsTag(), myAlgoBranch );
2864 if ( found && !shapeRefFound && hasShape) { // remove applied algos
2865 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( myAlgoBranch );
2867 if ( found && (shapeRefFound || !hasShape)) {
2868 aGroup = new HDFgroup( "Applied Algorithms", aTopGroup );
2869 aGroup->CreateOnDisk();
2871 SALOMEDS::ChildIterator_var it = myCurrentStudy->NewChildIterator( myAlgoBranch );
2873 for ( ; it->More(); it->Next() ) {
2874 SALOMEDS::SObject_var mySObject = it->Value();
2875 SALOMEDS::SObject_var myRefOnAlgo;
2876 bool ok = mySObject->ReferencedObject( myRefOnAlgo );
2878 // san - it is impossible to recover applied algorithms
2879 // using their entries within Load() method,
2880 // for there are no AttributeIORs in the study when Load() is working.
2881 // Hence, it is better to store persistent IDs of algorithms as references to them
2883 //string myRefOnObject = myRefOnAlgo->GetID();
2884 CORBA::Object_var anObject = SObjectToObject( myRefOnAlgo );
2885 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
2886 int id = myStudyContext->findId( string( objStr.in() ) );
2887 //if ( myRefOnObject.length() > 0 ) {
2888 //aSize[ 0 ] = myRefOnObject.length() + 1;
2889 char algoName[ 30 ], algoId[ 30 ];
2890 sprintf( algoName, "Algo %d", ++algoNb );
2891 sprintf( algoId, "%d", id );
2892 aSize[ 0 ] = strlen( algoId ) + 1;
2893 aDataset = new HDFdataset( algoName, aGroup, HDF_STRING, aSize, 1 );
2894 aDataset->CreateOnDisk();
2895 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
2896 aDataset->WriteOnDisk( algoId );
2897 aDataset->CloseOnDisk();
2901 aGroup->CloseOnDisk();
2904 // --> submesh objects sub-branches
2906 for ( int i = GetSubMeshOnVertexTag(); i <= GetSubMeshOnCompoundTag(); i++ ) {
2907 SALOMEDS::SObject_var mySubmeshBranch;
2908 found = gotBranch->FindSubObject( i, mySubmeshBranch );
2910 if ( found ) // check if there is shape reference in submeshes
2912 bool hasShapeRef = false;
2913 SALOMEDS::ChildIterator_var itSM =
2914 myCurrentStudy->NewChildIterator( mySubmeshBranch );
2915 for ( ; itSM->More(); itSM->Next() ) {
2916 SALOMEDS::SObject_var mySubRef, myShape, mySObject = itSM->Value();
2917 if ( mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef ))
2918 mySubRef->ReferencedObject( myShape );
2919 if ( !CORBA::is_nil( myShape ) && !CORBA::is_nil( myShape->GetObject() ))
2922 { // remove one submesh
2923 if ( shapeRefFound )
2924 { // unassign hypothesis
2925 SMESH::SMESH_subMesh_var mySubMesh =
2926 SMESH::SMESH_subMesh::_narrow( SObjectToObject( mySObject ));
2927 if ( !mySubMesh->_is_nil() ) {
2928 int shapeID = mySubMesh->GetId();
2929 TopoDS_Shape S = mySMESHDSMesh->IndexToShape( shapeID );
2930 const list<const SMESHDS_Hypothesis*>& hypList =
2931 mySMESHDSMesh->GetHypothesis( S );
2932 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
2933 while ( hyp != hypList.end() ) {
2934 int hypID = (*hyp++)->GetID(); // goto next hyp here because
2935 myLocMesh.RemoveHypothesis( S, hypID ); // hypList changes here
2939 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( mySObject );
2941 } // loop on submeshes of a type
2942 if ( !shapeRefFound || !hasShapeRef ) { // remove the whole submeshes branch
2943 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( mySubmeshBranch );
2946 } // end check if there is shape reference in submeshes
2948 char name_meshgroup[ 30 ];
2949 if ( i == GetSubMeshOnVertexTag() )
2950 strcpy( name_meshgroup, "SubMeshes On Vertex" );
2951 else if ( i == GetSubMeshOnEdgeTag() )
2952 strcpy( name_meshgroup, "SubMeshes On Edge" );
2953 else if ( i == GetSubMeshOnWireTag() )
2954 strcpy( name_meshgroup, "SubMeshes On Wire" );
2955 else if ( i == GetSubMeshOnFaceTag() )
2956 strcpy( name_meshgroup, "SubMeshes On Face" );
2957 else if ( i == GetSubMeshOnShellTag() )
2958 strcpy( name_meshgroup, "SubMeshes On Shell" );
2959 else if ( i == GetSubMeshOnSolidTag() )
2960 strcpy( name_meshgroup, "SubMeshes On Solid" );
2961 else if ( i == GetSubMeshOnCompoundTag() )
2962 strcpy( name_meshgroup, "SubMeshes On Compound" );
2964 // for each type of submeshes create container HDF group
2965 aGroup = new HDFgroup( name_meshgroup, aTopGroup );
2966 aGroup->CreateOnDisk();
2968 // iterator for all submeshes of given type
2969 SALOMEDS::ChildIterator_var itSM = myCurrentStudy->NewChildIterator( mySubmeshBranch );
2970 for ( ; itSM->More(); itSM->Next() ) {
2971 SALOMEDS::SObject_var mySObject = itSM->Value();
2972 CORBA::Object_var anSubObject = SObjectToObject( mySObject );
2973 if ( !CORBA::is_nil( anSubObject ))
2975 SMESH::SMESH_subMesh_var mySubMesh = SMESH::SMESH_subMesh::_narrow( anSubObject ) ;
2976 CORBA::String_var objStr = GetORB()->object_to_string( anSubObject );
2977 int subid = myStudyContext->findId( string( objStr.in() ) );
2979 // for each mesh open the HDF group basing on its id
2980 char submeshGrpName[ 30 ];
2981 sprintf( submeshGrpName, "SubMesh %d", subid );
2982 aSubGroup = new HDFgroup( submeshGrpName, aGroup );
2983 aSubGroup->CreateOnDisk();
2985 // write reference on a shape, already checked if it exists
2986 SALOMEDS::SObject_var mySubRef, myShape;
2987 if ( mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef ))
2988 mySubRef->ReferencedObject( myShape );
2989 string myRefOnObject = myShape->GetID();
2990 if ( myRefOnObject.length() > 0 ) {
2991 aSize[ 0 ] = myRefOnObject.length() + 1;
2992 aDataset = new HDFdataset( "Ref on shape", aSubGroup, HDF_STRING, aSize, 1 );
2993 aDataset->CreateOnDisk();
2994 aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
2995 aDataset->CloseOnDisk();
2998 // write applied hypotheses if exist
2999 SALOMEDS::SObject_var mySubHypBranch;
3000 found = mySObject->FindSubObject( GetRefOnAppliedHypothesisTag(), mySubHypBranch );
3002 aSubSubGroup = new HDFgroup( "Applied Hypotheses", aSubGroup );
3003 aSubSubGroup->CreateOnDisk();
3005 SALOMEDS::ChildIterator_var it = myCurrentStudy->NewChildIterator( mySubHypBranch );
3007 for ( ; it->More(); it->Next() ) {
3008 SALOMEDS::SObject_var mySubSObject = it->Value();
3009 SALOMEDS::SObject_var myRefOnHyp;
3010 bool ok = mySubSObject->ReferencedObject( myRefOnHyp );
3012 //string myRefOnObject = myRefOnHyp->GetID();
3013 CORBA::Object_var anObject = SObjectToObject( myRefOnHyp );
3014 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3015 int id = myStudyContext->findId( string( objStr.in() ) );
3016 //if ( myRefOnObject.length() > 0 ) {
3017 //aSize[ 0 ] = myRefOnObject.length() + 1;
3018 char hypName[ 30 ], hypId[ 30 ];
3019 sprintf( hypName, "Hyp %d", ++hypNb );
3020 sprintf( hypId, "%d", id );
3021 aSize[ 0 ] = strlen( hypId ) + 1;
3022 aDataset = new HDFdataset( hypName, aSubSubGroup, HDF_STRING, aSize, 1 );
3023 aDataset->CreateOnDisk();
3024 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3025 aDataset->WriteOnDisk( hypId );
3026 aDataset->CloseOnDisk();
3030 aSubSubGroup->CloseOnDisk();
3033 // write applied algorithms if exist
3034 SALOMEDS::SObject_var mySubAlgoBranch;
3035 found = mySObject->FindSubObject( GetRefOnAppliedAlgorithmsTag(), mySubAlgoBranch );
3037 aSubSubGroup = new HDFgroup( "Applied Algorithms", aSubGroup );
3038 aSubSubGroup->CreateOnDisk();
3040 SALOMEDS::ChildIterator_var it = myCurrentStudy->NewChildIterator( mySubAlgoBranch );
3042 for ( ; it->More(); it->Next() ) {
3043 SALOMEDS::SObject_var mySubSObject = it->Value();
3044 SALOMEDS::SObject_var myRefOnAlgo;
3045 bool ok = mySubSObject->ReferencedObject( myRefOnAlgo );
3047 //string myRefOnObject = myRefOnAlgo->GetID();
3048 CORBA::Object_var anObject = SObjectToObject( myRefOnAlgo );
3049 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3050 int id = myStudyContext->findId( string( objStr.in() ) );
3051 //if ( myRefOnObject.length() > 0 ) {
3052 //aSize[ 0 ] = myRefOnObject.length() + 1;
3053 char algoName[ 30 ], algoId[ 30 ];
3054 sprintf( algoName, "Algo %d", ++algoNb );
3055 sprintf( algoId, "%d", id );
3056 aSize[ 0 ] = strlen( algoId ) + 1;
3057 aDataset = new HDFdataset( algoName, aSubSubGroup, HDF_STRING, aSize, 1 );
3058 aDataset->CreateOnDisk();
3059 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3060 aDataset->WriteOnDisk( algoId );
3061 aDataset->CloseOnDisk();
3065 aSubSubGroup->CloseOnDisk();
3067 // close submesh HDF group
3068 aSubGroup->CloseOnDisk();
3071 // close container of submeshes by type HDF group
3072 aGroup->CloseOnDisk();
3075 // All sub-meshes will be stored in MED file
3076 // .. will NOT (PAL 12992)
3077 //if ( shapeRefFound )
3078 //myWriter.AddAllSubMeshes();
3080 // store submesh order if any
3081 const TListOfListOfInt& theOrderIds = myLocMesh.GetMeshOrder();
3082 if ( theOrderIds.size() ) {
3083 char order_list[ 30 ];
3084 strcpy( order_list, "Mesh Order" );
3085 // count number of submesh ids
3087 TListOfListOfInt::const_iterator idIt = theOrderIds.begin();
3088 for ( ; idIt != theOrderIds.end(); idIt++ )
3089 nbIDs += (*idIt).size();
3090 // number of values = number of IDs +
3091 // number of lists (for separators) - 1
3092 int* smIDs = new int [ nbIDs + theOrderIds.size() - 1 ];
3093 idIt = theOrderIds.begin();
3094 for ( int i = 0; idIt != theOrderIds.end(); idIt++ ) {
3095 const TListOfInt& idList = *idIt;
3096 if (idIt != theOrderIds.begin()) // not first list
3097 smIDs[ i++ ] = -1/* *idList.size()*/; // separator between lists
3098 // dump submesh ids from current list
3099 TListOfInt::const_iterator id_smId = idList.begin();
3100 for( ; id_smId != idList.end(); id_smId++ )
3101 smIDs[ i++ ] = *id_smId;
3104 aSize[ 0 ] = nbIDs + theOrderIds.size() - 1;
3106 aDataset = new HDFdataset( order_list, aTopGroup, HDF_INT32, aSize, 1 );
3107 aDataset->CreateOnDisk();
3108 aDataset->WriteOnDisk( smIDs );
3109 aDataset->CloseOnDisk();
3114 // groups root sub-branch
3115 SALOMEDS::SObject_var myGroupsBranch;
3116 for ( int i = GetNodeGroupsTag(); i <= GetVolumeGroupsTag(); i++ ) {
3117 found = gotBranch->FindSubObject( i, myGroupsBranch );
3119 char name_group[ 30 ];
3120 if ( i == GetNodeGroupsTag() )
3121 strcpy( name_group, "Groups of Nodes" );
3122 else if ( i == GetEdgeGroupsTag() )
3123 strcpy( name_group, "Groups of Edges" );
3124 else if ( i == GetFaceGroupsTag() )
3125 strcpy( name_group, "Groups of Faces" );
3126 else if ( i == GetVolumeGroupsTag() )
3127 strcpy( name_group, "Groups of Volumes" );
3129 aGroup = new HDFgroup( name_group, aTopGroup );
3130 aGroup->CreateOnDisk();
3132 SALOMEDS::ChildIterator_var it = myCurrentStudy->NewChildIterator( myGroupsBranch );
3133 for ( ; it->More(); it->Next() ) {
3134 SALOMEDS::SObject_var mySObject = it->Value();
3135 CORBA::Object_var aSubObject = SObjectToObject( mySObject );
3136 if ( !CORBA::is_nil( aSubObject ) ) {
3137 SMESH_GroupBase_i* myGroupImpl =
3138 dynamic_cast<SMESH_GroupBase_i*>( GetServant( aSubObject ).in() );
3141 SMESHDS_GroupBase* aGrpBaseDS = myGroupImpl->GetGroupDS();
3145 CORBA::String_var objStr = GetORB()->object_to_string( aSubObject );
3146 int anId = myStudyContext->findId( string( objStr.in() ) );
3148 // For each group, create a dataset named "Group <group_persistent_id>"
3149 // and store the group's user name into it
3150 const char* grpName = aGrpBaseDS->GetStoreName();
3151 char* aUserName = myGroupImpl->GetName();
3152 aSize[ 0 ] = strlen( aUserName ) + 1;
3154 aDataset = new HDFdataset( grpName, aGroup, HDF_STRING, aSize, 1 );
3155 aDataset->CreateOnDisk();
3156 aDataset->WriteOnDisk( aUserName );
3157 aDataset->CloseOnDisk();
3160 // For each group, create a dataset named "Group <group_persistent_id> Color"
3161 // and store the group's color into it
3162 char grpColorName[ 30 ];
3163 sprintf( grpColorName, "ColorGroup %d", anId );
3164 SALOMEDS::Color aColor = myGroupImpl->GetColor();
3166 anRGB[ 0 ] = aColor.R;
3167 anRGB[ 1 ] = aColor.G;
3168 anRGB[ 2 ] = aColor.B;
3170 aDataset = new HDFdataset( grpColorName, aGroup, HDF_FLOAT64, aSize, 1 );
3171 aDataset->CreateOnDisk();
3172 aDataset->WriteOnDisk( anRGB );
3173 aDataset->CloseOnDisk();
3175 // Pass SMESHDS_Group to MED writer
3176 SMESHDS_Group* aGrpDS = dynamic_cast<SMESHDS_Group*>( aGrpBaseDS );
3178 myWriter.AddGroup( aGrpDS );
3180 // write reference on a shape if exists
3181 SMESHDS_GroupOnGeom* aGeomGrp =
3182 dynamic_cast<SMESHDS_GroupOnGeom*>( aGrpBaseDS );
3184 SALOMEDS::SObject_var mySubRef, myShape;
3185 if (mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef ) &&
3186 mySubRef->ReferencedObject( myShape ) &&
3187 !CORBA::is_nil( myShape->GetObject() ))
3189 string myRefOnObject = myShape->GetID();
3190 if ( myRefOnObject.length() > 0 ) {
3191 char aRefName[ 30 ];
3192 sprintf( aRefName, "Ref on shape %d", anId);
3193 aSize[ 0 ] = myRefOnObject.length() + 1;
3194 aDataset = new HDFdataset(aRefName, aGroup, HDF_STRING, aSize, 1);
3195 aDataset->CreateOnDisk();
3196 aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3197 aDataset->CloseOnDisk();
3200 else // shape ref is invalid:
3202 // save a group on geometry as ordinary group
3203 myWriter.AddGroup( aGeomGrp );
3208 aGroup->CloseOnDisk();
3212 if ( strcmp( strHasData.c_str(), "1" ) == 0 )
3214 // Flush current mesh information into MED file
3217 // maybe a shape was deleted in the study
3218 if ( !shapeRefFound && !mySMESHDSMesh->ShapeToMesh().IsNull() && hasShape) {
3219 TopoDS_Shape nullShape;
3220 myLocMesh.ShapeToMesh( nullShape ); // remove shape referring data
3223 if ( !mySMESHDSMesh->SubMeshes().empty() )
3227 aGroup = new HDFgroup( "Submeshes", aTopGroup );
3228 aGroup->CreateOnDisk();
3230 // each element belongs to one or none submesh,
3231 // so for each node/element, we store a submesh ID
3233 // Make maps of submesh IDs of elements sorted by element IDs
3234 typedef int TElemID;
3235 typedef int TSubMID;
3236 map< TElemID, TSubMID > eId2smId, nId2smId;
3237 map< TElemID, TSubMID >::iterator hint; // insertion to map is done before hint
3238 const map<int,SMESHDS_SubMesh*>& aSubMeshes = mySMESHDSMesh->SubMeshes();
3239 map<int,SMESHDS_SubMesh*>::const_iterator itSubM ( aSubMeshes.begin() );
3240 SMDS_NodeIteratorPtr itNode;
3241 SMDS_ElemIteratorPtr itElem;
3242 for ( itSubM = aSubMeshes.begin(); itSubM != aSubMeshes.end() ; itSubM++ )
3244 TSubMID aSubMeID = itSubM->first;
3245 SMESHDS_SubMesh* aSubMesh = itSubM->second;
3246 if ( aSubMesh->IsComplexSubmesh() )
3247 continue; // submesh containing other submeshs
3249 hint = nId2smId.begin(); // optimize insertion basing on increasing order of elem Ids in submesh
3250 for ( itNode = aSubMesh->GetNodes(); itNode->more(); ++hint)
3251 hint = nId2smId.insert( hint, make_pair( itNode->next()->GetID(), aSubMeID ));
3253 hint = eId2smId.begin();
3254 for ( itElem = aSubMesh->GetElements(); itElem->more(); ++hint)
3255 hint = eId2smId.insert( hint, make_pair( itElem->next()->GetID(), aSubMeID ));
3258 // Care of elements that are not on submeshes
3259 if ( mySMESHDSMesh->NbNodes() != nId2smId.size() ) {
3260 for ( itNode = mySMESHDSMesh->nodesIterator(); itNode->more(); )
3261 /* --- stl_map.h says : */
3262 /* A %map relies on unique keys and thus a %pair is only inserted if its */
3263 /* first element (the key) is not already present in the %map. */
3264 nId2smId.insert( make_pair( itNode->next()->GetID(), 0 ));
3266 int nbElems = mySMESHDSMesh->NbEdges() + mySMESHDSMesh->NbFaces() + mySMESHDSMesh->NbVolumes();
3267 if ( nbElems != eId2smId.size() ) {
3268 for ( itElem = mySMESHDSMesh->elementsIterator(); itElem->more(); )
3269 eId2smId.insert( make_pair( itElem->next()->GetID(), 0 ));
3272 // Store submesh IDs
3273 for ( int isNode = 0; isNode < 2; ++isNode )
3275 map< TElemID, TSubMID >& id2smId = isNode ? nId2smId : eId2smId;
3276 if ( id2smId.empty() ) continue;
3277 map< TElemID, TSubMID >::const_iterator id_smId = id2smId.begin();
3278 // make and fill array of submesh IDs
3279 int* smIDs = new int [ id2smId.size() ];
3280 for ( int i = 0; id_smId != id2smId.end(); ++id_smId, ++i )
3281 smIDs[ i ] = id_smId->second;
3283 aSize[ 0 ] = id2smId.size();
3284 string aDSName( isNode ? "Node Submeshes" : "Element Submeshes");
3285 aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_INT32, aSize, 1 );
3286 aDataset->CreateOnDisk();
3287 aDataset->WriteOnDisk( smIDs );
3288 aDataset->CloseOnDisk();
3293 // Store node positions on sub-shapes (SMDS_Position):
3294 // ----------------------------------------------------
3296 aGroup = new HDFgroup( "Node Positions", aTopGroup );
3297 aGroup->CreateOnDisk();
3299 // in aGroup, create 5 datasets to contain:
3300 // "Nodes on Edges" - ID of node on edge
3301 // "Edge positions" - U parameter on node on edge
3302 // "Nodes on Faces" - ID of node on face
3303 // "Face U positions" - U parameter of node on face
3304 // "Face V positions" - V parameter of node on face
3306 // Find out nb of nodes on edges and faces
3307 // Collect corresponing sub-meshes
3308 int nbEdgeNodes = 0, nbFaceNodes = 0;
3309 list<SMESHDS_SubMesh*> aEdgeSM, aFaceSM;
3310 // loop on SMESHDS_SubMesh'es
3311 for ( itSubM = aSubMeshes.begin(); itSubM != aSubMeshes.end() ; itSubM++ )
3313 SMESHDS_SubMesh* aSubMesh = (*itSubM).second;
3314 if ( aSubMesh->IsComplexSubmesh() )
3315 continue; // submesh containing other submeshs
3316 int nbNodes = aSubMesh->NbNodes();
3317 if ( nbNodes == 0 ) continue;
3319 int aShapeID = (*itSubM).first;
3320 if ( aShapeID < 1 || aShapeID > mySMESHDSMesh->MaxShapeIndex() )
3322 int aShapeType = mySMESHDSMesh->IndexToShape( aShapeID ).ShapeType();
3323 // write only SMDS_FacePosition and SMDS_EdgePosition
3324 switch ( aShapeType ) {
3326 nbFaceNodes += nbNodes;
3327 aFaceSM.push_back( aSubMesh );
3330 nbEdgeNodes += nbNodes;
3331 aEdgeSM.push_back( aSubMesh );
3337 // Treat positions on edges or faces
3338 for ( int onFace = 0; onFace < 2; onFace++ )
3340 // Create arrays to store in datasets
3341 int iNode = 0, nbNodes = ( onFace ? nbFaceNodes : nbEdgeNodes );
3342 if (!nbNodes) continue;
3343 int* aNodeIDs = new int [ nbNodes ];
3344 double* aUPos = new double [ nbNodes ];
3345 double* aVPos = ( onFace ? new double[ nbNodes ] : 0 );
3348 // loop on sub-meshes
3349 list<SMESHDS_SubMesh*> * pListSM = ( onFace ? &aFaceSM : &aEdgeSM );
3350 list<SMESHDS_SubMesh*>::iterator itSM = pListSM->begin();
3351 for ( ; itSM != pListSM->end(); itSM++ )
3353 SMESHDS_SubMesh* aSubMesh = (*itSM);
3355 SMDS_NodeIteratorPtr itNode = aSubMesh->GetNodes();
3356 // loop on nodes in aSubMesh
3357 while ( itNode->more() )
3360 const SMDS_MeshNode* node = itNode->next();
3361 aNodeIDs [ iNode ] = node->GetID();
3364 const SMDS_PositionPtr pos = node->GetPosition();
3365 if ( onFace ) { // on FACE
3366 const SMDS_FacePosition* fPos =
3367 dynamic_cast<const SMDS_FacePosition*>( pos );
3369 aUPos[ iNode ] = fPos->GetUParameter();
3370 aVPos[ iNode ] = fPos->GetVParameter();
3377 const SMDS_EdgePosition* ePos =
3378 dynamic_cast<const SMDS_EdgePosition*>( pos );
3380 aUPos[ iNode ] = ePos->GetUParameter();
3386 } // loop on nodes in aSubMesh
3387 } // loop on sub-meshes
3392 aSize[ 0 ] = nbNodes;
3394 string aDSName( onFace ? "Nodes on Faces" : "Nodes on Edges");
3395 aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_INT32, aSize, 1 );
3396 aDataset->CreateOnDisk();
3397 aDataset->WriteOnDisk( aNodeIDs );
3398 aDataset->CloseOnDisk();
3401 aDSName = ( onFace ? "Face U positions" : "Edge positions");
3402 aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_FLOAT64, aSize, 1);
3403 aDataset->CreateOnDisk();
3404 aDataset->WriteOnDisk( aUPos );
3405 aDataset->CloseOnDisk();
3408 aDataset = new HDFdataset( "Face V positions", aGroup, HDF_FLOAT64, aSize, 1);
3409 aDataset->CreateOnDisk();
3410 aDataset->WriteOnDisk( aVPos );
3411 aDataset->CloseOnDisk();
3416 if ( aVPos ) delete [] aVPos;
3418 } // treat positions on edges or faces
3420 // close "Node Positions" group
3421 aGroup->CloseOnDisk();
3423 } // if ( there are submeshes in SMESHDS_Mesh )
3426 // close mesh HDF group
3427 aTopGroup->CloseOnDisk();
3435 aFile->CloseOnDisk();
3438 // Convert temporary files to stream
3439 aStreamFile = SALOMEDS_Tool::PutFilesToStream( tmpDir.ToCString(), aFileSeq.in(), isMultiFile );
3441 // Remove temporary files and directory
3443 SALOMEDS_Tool::RemoveTemporaryFiles( tmpDir.ToCString(), aFileSeq.in(), true );
3445 INFOS( "SMESH_Gen_i::Save() completed" );
3446 return aStreamFile._retn();
3449 //=============================================================================
3451 * SMESH_Gen_i::SaveASCII
3453 * Save SMESH module's data in ASCII format
3455 //=============================================================================
3457 SALOMEDS::TMPFile* SMESH_Gen_i::SaveASCII( SALOMEDS::SComponent_ptr theComponent,
3459 bool isMultiFile ) {
3460 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::SaveASCII" );
3461 SALOMEDS::TMPFile_var aStreamFile = Save( theComponent, theURL, isMultiFile );
3462 return aStreamFile._retn();
3464 //after usual saving needs to encipher binary to text string
3465 //Any binary symbol will be represent as "|xx" () hexadecimal format number
3466 int size = aStreamFile.in().length();
3467 _CORBA_Octet* buffer = new _CORBA_Octet[size*3+1];
3468 for ( int i = 0; i < size; i++ )
3469 sprintf( (char*)&(buffer[i*3]), "|%02x", (char*)(aStreamFile[i]) );
3471 buffer[size * 3] = '\0';
3473 SALOMEDS::TMPFile_var anAsciiStreamFile = new SALOMEDS::TMPFile(size*3, size*3, buffer, 1);
3475 return anAsciiStreamFile._retn();
3478 //=============================================================================
3480 * SMESH_Gen_i::loadGeomData
3482 * Load GEOM module data
3484 //=============================================================================
3486 void SMESH_Gen_i::loadGeomData( SALOMEDS::SComponent_ptr theCompRoot )
3488 if ( theCompRoot->_is_nil() )
3491 SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow( theCompRoot->GetStudy() );
3492 if ( aStudy->_is_nil() )
3495 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
3496 aStudyBuilder->LoadWith( theCompRoot, GetGeomEngine() );
3498 //=============================================================================
3500 * \brief Creates SMDS_Position according to shape type
3502 //=============================================================================
3504 class PositionCreator {
3506 SMDS_PositionPtr MakePosition(const TopAbs_ShapeEnum type) {
3507 return (this->*myFuncTable[ type ])();
3510 myFuncTable.resize( (size_t) TopAbs_SHAPE, & PositionCreator::defaultPosition );
3511 myFuncTable[ TopAbs_SOLID ] = & PositionCreator::volumePosition;
3512 myFuncTable[ TopAbs_FACE ] = & PositionCreator::facePosition;
3513 myFuncTable[ TopAbs_EDGE ] = & PositionCreator::edgePosition;
3514 myFuncTable[ TopAbs_VERTEX ] = & PositionCreator::vertexPosition;
3517 SMDS_PositionPtr edgePosition() const { return SMDS_PositionPtr( new SMDS_EdgePosition ); }
3518 SMDS_PositionPtr facePosition() const { return SMDS_PositionPtr( new SMDS_FacePosition ); }
3519 SMDS_PositionPtr volumePosition() const { return SMDS_PositionPtr( new SMDS_SpacePosition ); }
3520 SMDS_PositionPtr vertexPosition() const { return SMDS_PositionPtr( new SMDS_VertexPosition); }
3521 SMDS_PositionPtr defaultPosition() const { return SMDS_SpacePosition::originSpacePosition(); }
3522 typedef SMDS_PositionPtr (PositionCreator:: * FmakePos)() const;
3523 vector<FmakePos> myFuncTable;
3526 //=============================================================================
3530 * Load SMESH module's data
3532 //=============================================================================
3534 bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
3535 const SALOMEDS::TMPFile& theStream,
3539 INFOS( "SMESH_Gen_i::Load" );
3541 if ( myCurrentStudy->_is_nil() ||
3542 theComponent->GetStudy()->StudyId() != myCurrentStudy->StudyId() )
3543 SetCurrentStudy( theComponent->GetStudy() );
3545 /* if( !theComponent->_is_nil() )
3547 //SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow( theComponent->GetStudy() );
3548 if( !myCurrentStudy->FindComponent( "GEOM" )->_is_nil() )
3549 loadGeomData( myCurrentStudy->FindComponent( "GEOM" ) );
3552 StudyContext* myStudyContext = GetCurrentStudyContext();
3554 // Get temporary files location
3555 TCollection_AsciiString tmpDir =
3556 isMultiFile ? TCollection_AsciiString( ( char* )theURL ) : ( char* )SALOMEDS_Tool::GetTmpDir().c_str();
3558 INFOS( "THE URL++++++++++++++" )
3560 INFOS( "THE TMP PATH+++++++++" );
3563 // Convert the stream into sequence of files to process
3564 SALOMEDS::ListOfFileNames_var aFileSeq = SALOMEDS_Tool::PutStreamToFiles( theStream,
3567 TCollection_AsciiString aStudyName( "" );
3569 aStudyName = ( (char*)SALOMEDS_Tool::GetNameFromPath( myCurrentStudy->URL() ).c_str() );
3571 // Set names of temporary files
3572 TCollection_AsciiString filename = tmpDir + aStudyName + TCollection_AsciiString( "_SMESH.hdf" );
3573 TCollection_AsciiString meshfile = tmpDir + aStudyName + TCollection_AsciiString( "_SMESH_Mesh.med" );
3577 HDFdataset* aDataset;
3578 HDFgroup* aTopGroup;
3580 HDFgroup* aSubGroup;
3581 HDFgroup* aSubSubGroup;
3584 // ---> open HDF file
3585 aFile = new HDFfile( (char*) filename.ToCString() );
3587 aFile->OpenOnDisk( HDF_RDONLY );
3589 catch ( HDFexception ) {
3590 INFOS( "Load(): " << filename << " not found!" );
3594 DriverMED_R_SMESHDS_Mesh myReader;
3595 myReader.SetFile( meshfile.ToCString() );
3597 // For PAL13473 ("Repetitive mesh") implementation.
3598 // New dependencies between SMESH objects are established:
3599 // now hypotheses can refer to meshes, shapes and other hypotheses.
3600 // To keep data consistent, the following order of data restoration
3602 // 1. Create hypotheses
3603 // 2. Create all meshes
3604 // 3. Load hypotheses' data
3607 list< pair< SMESH_Hypothesis_i*, string > > hypDataList;
3608 list< pair< SMESH_Mesh_i*, HDFgroup* > > meshGroupList;
3610 // get total number of top-level groups
3611 int aNbGroups = aFile->nInternalObjects();
3612 if ( aNbGroups > 0 ) {
3613 // --> in first turn we should read&create hypotheses
3614 if ( aFile->ExistInternalObject( "Hypotheses" ) ) {
3615 // open hypotheses root HDF group
3616 aTopGroup = new HDFgroup( "Hypotheses", aFile );
3617 aTopGroup->OpenOnDisk();
3619 // get number of hypotheses
3620 int aNbObjects = aTopGroup->nInternalObjects();
3621 for ( int j = 0; j < aNbObjects; j++ ) {
3622 // try to identify hypothesis
3623 char hypGrpName[ HDF_NAME_MAX_LEN+1 ];
3624 aTopGroup->InternalObjectIndentify( j, hypGrpName );
3626 if ( string( hypGrpName ).substr( 0, 10 ) == string( "Hypothesis" ) ) {
3627 // open hypothesis group
3628 aGroup = new HDFgroup( hypGrpName, aTopGroup );
3629 aGroup->OpenOnDisk();
3631 // --> get hypothesis id
3632 int id = atoi( string( hypGrpName ).substr( 10 ).c_str() );
3637 // get number of datasets
3638 int aNbSubObjects = aGroup->nInternalObjects();
3639 for ( int k = 0; k < aNbSubObjects; k++ ) {
3641 char name_of_subgroup[ HDF_NAME_MAX_LEN+1 ];
3642 aGroup->InternalObjectIndentify( k, name_of_subgroup );
3643 // --> get hypothesis name
3644 if ( strcmp( name_of_subgroup, "Name" ) == 0 ) {
3645 aDataset = new HDFdataset( name_of_subgroup, aGroup );
3646 aDataset->OpenOnDisk();
3647 size = aDataset->GetSize();
3648 char* hypname_str = new char[ size ];
3649 aDataset->ReadFromDisk( hypname_str );
3650 hypname = string( hypname_str );
3651 delete [] hypname_str;
3652 aDataset->CloseOnDisk();
3654 // --> get hypothesis plugin library name
3655 if ( strcmp( name_of_subgroup, "LibName" ) == 0 ) {
3656 aDataset = new HDFdataset( name_of_subgroup, aGroup );
3657 aDataset->OpenOnDisk();
3658 size = aDataset->GetSize();
3659 char* libname_str = new char[ size ];
3660 aDataset->ReadFromDisk( libname_str );
3661 if(MYDEBUG) SCRUTE( libname_str );
3662 libname = string( libname_str );
3663 delete [] libname_str;
3664 aDataset->CloseOnDisk();
3666 // --> get hypothesis data
3667 if ( strcmp( name_of_subgroup, "Data" ) == 0 ) {
3668 aDataset = new HDFdataset( name_of_subgroup, aGroup );
3669 aDataset->OpenOnDisk();
3670 size = aDataset->GetSize();
3671 char* hypdata_str = new char[ size ];
3672 aDataset->ReadFromDisk( hypdata_str );
3673 hypdata = string( hypdata_str );
3674 delete [] hypdata_str;
3675 aDataset->CloseOnDisk();
3678 // close hypothesis HDF group
3679 aGroup->CloseOnDisk();
3681 // --> restore hypothesis from data
3682 if ( id > 0 && !hypname.empty()/* && !hypdata.empty()*/ ) { // VSR : persistent data can be empty
3683 if(MYDEBUG) MESSAGE("VSR - load hypothesis : id = " << id <<
3684 ", name = " << hypname.c_str() << ", persistent string = " << hypdata.c_str());
3685 SMESH::SMESH_Hypothesis_var myHyp;
3687 try { // protect persistence mechanism against exceptions
3688 myHyp = this->createHypothesis( hypname.c_str(), libname.c_str() );
3691 INFOS( "Exception during hypothesis creation" );
3694 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
3696 // myImpl->LoadFrom( hypdata.c_str() );
3697 hypDataList.push_back( make_pair( myImpl, hypdata ));
3698 string iorString = GetORB()->object_to_string( myHyp );
3699 int newId = myStudyContext->findId( iorString );
3700 myStudyContext->mapOldToNew( id, newId );
3703 if(MYDEBUG) MESSAGE( "VSR - SMESH_Gen::Load - can't get servant" );
3707 // close hypotheses root HDF group
3708 aTopGroup->CloseOnDisk();
3712 // --> then we should read&create algorithms
3713 if ( aFile->ExistInternalObject( "Algorithms" ) ) {
3714 // open algorithms root HDF group
3715 aTopGroup = new HDFgroup( "Algorithms", aFile );
3716 aTopGroup->OpenOnDisk();
3718 // get number of algorithms
3719 int aNbObjects = aTopGroup->nInternalObjects();
3720 for ( int j = 0; j < aNbObjects; j++ ) {
3721 // try to identify algorithm
3722 char hypGrpName[ HDF_NAME_MAX_LEN+1 ];
3723 aTopGroup->InternalObjectIndentify( j, hypGrpName );
3725 if ( string( hypGrpName ).substr( 0, 9 ) == string( "Algorithm" ) ) {
3726 // open algorithm group
3727 aGroup = new HDFgroup( hypGrpName, aTopGroup );
3728 aGroup->OpenOnDisk();
3730 // --> get algorithm id
3731 int id = atoi( string( hypGrpName ).substr( 9 ).c_str() );
3736 // get number of datasets
3737 int aNbSubObjects = aGroup->nInternalObjects();
3738 for ( int k = 0; k < aNbSubObjects; k++ ) {
3740 char name_of_subgroup[ HDF_NAME_MAX_LEN+1 ];
3741 aGroup->InternalObjectIndentify( k, name_of_subgroup );
3742 // --> get algorithm name
3743 if ( strcmp( name_of_subgroup, "Name" ) == 0 ) {
3744 aDataset = new HDFdataset( name_of_subgroup, aGroup );
3745 aDataset->OpenOnDisk();
3746 size = aDataset->GetSize();
3747 char* hypname_str = new char[ size ];
3748 aDataset->ReadFromDisk( hypname_str );
3749 hypname = string( hypname_str );
3750 delete [] hypname_str;
3751 aDataset->CloseOnDisk();
3753 // --> get algorithm plugin library name
3754 if ( strcmp( name_of_subgroup, "LibName" ) == 0 ) {
3755 aDataset = new HDFdataset( name_of_subgroup, aGroup );
3756 aDataset->OpenOnDisk();
3757 size = aDataset->GetSize();
3758 char* libname_str = new char[ size ];
3759 aDataset->ReadFromDisk( libname_str );
3760 if(MYDEBUG) SCRUTE( libname_str );
3761 libname = string( libname_str );
3762 delete [] libname_str;
3763 aDataset->CloseOnDisk();
3765 // --> get algorithm data
3766 if ( strcmp( name_of_subgroup, "Data" ) == 0 ) {
3767 aDataset = new HDFdataset( name_of_subgroup, aGroup );
3768 aDataset->OpenOnDisk();
3769 size = aDataset->GetSize();
3770 char* hypdata_str = new char[ size ];
3771 aDataset->ReadFromDisk( hypdata_str );
3772 if(MYDEBUG) SCRUTE( hypdata_str );
3773 hypdata = string( hypdata_str );
3774 delete [] hypdata_str;
3775 aDataset->CloseOnDisk();
3778 // close algorithm HDF group
3779 aGroup->CloseOnDisk();
3781 // --> restore algorithm from data
3782 if ( id > 0 && !hypname.empty()/* && !hypdata.empty()*/ ) { // VSR : persistent data can be empty
3783 if(MYDEBUG) MESSAGE("VSR - load algo : id = " << id <<
3784 ", name = " << hypname.c_str() << ", persistent string = " << hypdata.c_str());
3785 SMESH::SMESH_Hypothesis_var myHyp;
3787 try { // protect persistence mechanism against exceptions
3788 myHyp = this->createHypothesis( hypname.c_str(), libname.c_str() );
3791 INFOS( "Exception during hypothesis creation" );
3794 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
3796 //myImpl->LoadFrom( hypdata.c_str() );
3797 hypDataList.push_back( make_pair( myImpl, hypdata ));
3798 string iorString = GetORB()->object_to_string( myHyp );
3799 int newId = myStudyContext->findId( iorString );
3800 myStudyContext->mapOldToNew( id, newId );
3803 if(MYDEBUG) MESSAGE( "VSR - SMESH_Gen::Load - can't get servant" );
3807 // close algorithms root HDF group
3808 aTopGroup->CloseOnDisk();
3812 // --> the rest groups should be meshes
3813 for ( int i = 0; i < aNbGroups; i++ ) {
3814 // identify next group
3815 char meshName[ HDF_NAME_MAX_LEN+1 ];
3816 aFile->InternalObjectIndentify( i, meshName );
3818 if ( string( meshName ).substr( 0, 4 ) == string( "Mesh" ) ) {
3820 int id = atoi( string( meshName ).substr( 4 ).c_str() );
3824 // open mesh HDF group
3825 aTopGroup = new HDFgroup( meshName, aFile );
3826 aTopGroup->OpenOnDisk();
3828 // get number of child HDF objects
3829 int aNbObjects = aTopGroup->nInternalObjects();
3830 if ( aNbObjects > 0 ) {
3832 if(MYDEBUG) MESSAGE( "VSR - load mesh : id = " << id );
3833 SMESH::SMESH_Mesh_var myNewMesh = this->createMesh();
3834 SMESH_Mesh_i* myNewMeshImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( myNewMesh ).in() );
3835 if ( !myNewMeshImpl )
3837 meshGroupList.push_back( make_pair( myNewMeshImpl, aTopGroup ));
3839 string iorString = GetORB()->object_to_string( myNewMesh );
3840 int newId = myStudyContext->findId( iorString );
3841 myStudyContext->mapOldToNew( id, newId );
3844 // try to read and set auto color flag
3845 char aMeshAutoColorName[ 30 ];
3846 sprintf( aMeshAutoColorName, "AutoColorMesh %d", id);
3847 if( aTopGroup->ExistInternalObject( aMeshAutoColorName ) )
3849 aDataset = new HDFdataset( aMeshAutoColorName, aTopGroup );
3850 aDataset->OpenOnDisk();
3851 size = aDataset->GetSize();
3852 int* anAutoColor = new int[ size ];
3853 aDataset->ReadFromDisk( anAutoColor );
3854 aDataset->CloseOnDisk();
3855 myNewMeshImpl->SetAutoColor( (bool)anAutoColor[0] );
3858 // try to read and set reference to shape
3859 GEOM::GEOM_Object_var aShapeObject;
3860 if ( aTopGroup->ExistInternalObject( "Ref on shape" ) ) {
3861 // load mesh "Ref on shape" - it's an entry to SObject
3862 aDataset = new HDFdataset( "Ref on shape", aTopGroup );
3863 aDataset->OpenOnDisk();
3864 size = aDataset->GetSize();
3865 char* refFromFile = new char[ size ];
3866 aDataset->ReadFromDisk( refFromFile );
3867 aDataset->CloseOnDisk();
3868 if ( strlen( refFromFile ) > 0 ) {
3869 SALOMEDS::SObject_var shapeSO = myCurrentStudy->FindObjectID( refFromFile );
3871 // Make sure GEOM data are loaded first
3872 //loadGeomData( shapeSO->GetFatherComponent() );
3874 CORBA::Object_var shapeObject = SObjectToObject( shapeSO );
3875 if ( !CORBA::is_nil( shapeObject ) ) {
3876 aShapeObject = GEOM::GEOM_Object::_narrow( shapeObject );
3877 if ( !aShapeObject->_is_nil() )
3878 myNewMeshImpl->SetShape( aShapeObject );
3883 // issue 0020693. Restore _isModified flag
3884 if( aTopGroup->ExistInternalObject( "_isModified" ) )
3886 aDataset = new HDFdataset( "_isModified", aTopGroup );
3887 aDataset->OpenOnDisk();
3888 size = aDataset->GetSize();
3889 int* isModified = new int[ size ];
3890 aDataset->ReadFromDisk( isModified );
3891 aDataset->CloseOnDisk();
3892 myNewMeshImpl->GetImpl().SetIsModified( bool(*isModified));
3895 // issue 20918. Restore Persistent Id of SMESHDS_Mesh
3896 if( aTopGroup->ExistInternalObject( "meshPersistentId" ) )
3898 aDataset = new HDFdataset( "meshPersistentId", aTopGroup );
3899 aDataset->OpenOnDisk();
3900 size = aDataset->GetSize();
3901 int* meshPersistentId = new int[ size ];
3902 aDataset->ReadFromDisk( meshPersistentId );
3903 aDataset->CloseOnDisk();
3904 myNewMeshImpl->GetImpl().GetMeshDS()->SetPersistentId( *meshPersistentId );
3910 // As all object that can be referred by hypothesis are created,
3911 // we can restore hypothesis data
3913 list< pair< SMESH_Hypothesis_i*, string > >::iterator hyp_data;
3914 for ( hyp_data = hypDataList.begin(); hyp_data != hypDataList.end(); ++hyp_data )
3916 SMESH_Hypothesis_i* hyp = hyp_data->first;
3917 string & data = hyp_data->second;
3918 hyp->LoadFrom( data.c_str() );
3921 // Restore the rest mesh data
3923 list< pair< SMESH_Mesh_i*, HDFgroup* > >::iterator meshi_group;
3924 for ( meshi_group = meshGroupList.begin(); meshi_group != meshGroupList.end(); ++meshi_group )
3926 aTopGroup = meshi_group->second;
3927 SMESH_Mesh_i* myNewMeshImpl = meshi_group->first;
3928 ::SMESH_Mesh& myLocMesh = myNewMeshImpl->GetImpl();
3929 SMESHDS_Mesh* mySMESHDSMesh = myLocMesh.GetMeshDS();
3931 GEOM::GEOM_Object_var aShapeObject = myNewMeshImpl->GetShapeToMesh();
3932 bool hasData = false;
3935 string iorString = GetORB()->object_to_string( myNewMeshImpl->_this() );
3936 int newId = myStudyContext->findId( iorString );
3937 int id = myStudyContext->getOldId( newId );
3939 // try to find mesh data dataset
3940 if ( aTopGroup->ExistInternalObject( "Has data" ) ) {
3941 // load mesh "has data" flag
3942 aDataset = new HDFdataset( "Has data", aTopGroup );
3943 aDataset->OpenOnDisk();
3944 size = aDataset->GetSize();
3945 char* strHasData = new char[ size ];
3946 aDataset->ReadFromDisk( strHasData );
3947 aDataset->CloseOnDisk();
3948 if ( strcmp( strHasData, "1") == 0 ) {
3949 // read mesh data from MED file
3950 myReader.SetMesh( mySMESHDSMesh );
3951 myReader.SetMeshId( id );
3957 // try to get applied algorithms
3958 if ( aTopGroup->ExistInternalObject( "Applied Algorithms" ) ) {
3959 aGroup = new HDFgroup( "Applied Algorithms", aTopGroup );
3960 aGroup->OpenOnDisk();
3961 // get number of applied algorithms
3962 int aNbSubObjects = aGroup->nInternalObjects();
3963 if(MYDEBUG) MESSAGE( "VSR - number of applied algos " << aNbSubObjects );
3964 for ( int j = 0; j < aNbSubObjects; j++ ) {
3965 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
3966 aGroup->InternalObjectIndentify( j, name_dataset );
3967 // check if it is an algorithm
3968 if ( string( name_dataset ).substr( 0, 4 ) == string( "Algo" ) ) {
3969 aDataset = new HDFdataset( name_dataset, aGroup );
3970 aDataset->OpenOnDisk();
3971 size = aDataset->GetSize();
3972 char* refFromFile = new char[ size ];
3973 aDataset->ReadFromDisk( refFromFile );
3974 aDataset->CloseOnDisk();
3976 // san - it is impossible to recover applied algorithms using their entries within Load() method
3978 //SALOMEDS::SObject_var hypSO = myCurrentStudy->FindObjectID( refFromFile );
3979 //CORBA::Object_var hypObject = SObjectToObject( hypSO );
3980 int id = atoi( refFromFile );
3981 string anIOR = myStudyContext->getIORbyOldId( id );
3982 if ( !anIOR.empty() ) {
3983 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
3984 if ( !CORBA::is_nil( hypObject ) ) {
3985 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
3986 if ( !anHyp->_is_nil() && (!aShapeObject->_is_nil()
3987 || !myNewMeshImpl->HasShapeToMesh()) )
3988 myNewMeshImpl->addHypothesis( aShapeObject, anHyp );
3993 aGroup->CloseOnDisk();
3996 // try to get applied hypotheses
3997 if ( aTopGroup->ExistInternalObject( "Applied Hypotheses" ) ) {
3998 aGroup = new HDFgroup( "Applied Hypotheses", aTopGroup );
3999 aGroup->OpenOnDisk();
4000 // get number of applied hypotheses
4001 int aNbSubObjects = aGroup->nInternalObjects();
4002 for ( int j = 0; j < aNbSubObjects; j++ ) {
4003 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4004 aGroup->InternalObjectIndentify( j, name_dataset );
4005 // check if it is a hypothesis
4006 if ( string( name_dataset ).substr( 0, 3 ) == string( "Hyp" ) ) {
4007 aDataset = new HDFdataset( name_dataset, aGroup );
4008 aDataset->OpenOnDisk();
4009 size = aDataset->GetSize();
4010 char* refFromFile = new char[ size ];
4011 aDataset->ReadFromDisk( refFromFile );
4012 aDataset->CloseOnDisk();
4014 // san - it is impossible to recover applied hypotheses using their entries within Load() method
4016 //SALOMEDS::SObject_var hypSO = myCurrentStudy->FindObjectID( refFromFile );
4017 //CORBA::Object_var hypObject = SObjectToObject( hypSO );
4018 int id = atoi( refFromFile );
4019 string anIOR = myStudyContext->getIORbyOldId( id );
4020 if ( !anIOR.empty() ) {
4021 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
4022 if ( !CORBA::is_nil( hypObject ) ) {
4023 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
4024 if ( !anHyp->_is_nil() && (!aShapeObject->_is_nil()
4025 || !myNewMeshImpl->HasShapeToMesh()) )
4026 myNewMeshImpl->addHypothesis( aShapeObject, anHyp );
4031 aGroup->CloseOnDisk();
4034 // --> try to find submeshes containers for each type of submesh
4035 for ( int j = GetSubMeshOnVertexTag(); j <= GetSubMeshOnCompoundTag(); j++ ) {
4036 char name_meshgroup[ 30 ];
4037 if ( j == GetSubMeshOnVertexTag() )
4038 strcpy( name_meshgroup, "SubMeshes On Vertex" );
4039 else if ( j == GetSubMeshOnEdgeTag() )
4040 strcpy( name_meshgroup, "SubMeshes On Edge" );
4041 else if ( j == GetSubMeshOnWireTag() )
4042 strcpy( name_meshgroup, "SubMeshes On Wire" );
4043 else if ( j == GetSubMeshOnFaceTag() )
4044 strcpy( name_meshgroup, "SubMeshes On Face" );
4045 else if ( j == GetSubMeshOnShellTag() )
4046 strcpy( name_meshgroup, "SubMeshes On Shell" );
4047 else if ( j == GetSubMeshOnSolidTag() )
4048 strcpy( name_meshgroup, "SubMeshes On Solid" );
4049 else if ( j == GetSubMeshOnCompoundTag() )
4050 strcpy( name_meshgroup, "SubMeshes On Compound" );
4052 // try to get submeshes container HDF group
4053 if ( aTopGroup->ExistInternalObject( name_meshgroup ) ) {
4054 // open submeshes containers HDF group
4055 aGroup = new HDFgroup( name_meshgroup, aTopGroup );
4056 aGroup->OpenOnDisk();
4058 // get number of submeshes
4059 int aNbSubMeshes = aGroup->nInternalObjects();
4060 for ( int k = 0; k < aNbSubMeshes; k++ ) {
4062 char name_submeshgroup[ HDF_NAME_MAX_LEN+1 ];
4063 aGroup->InternalObjectIndentify( k, name_submeshgroup );
4064 if ( string( name_submeshgroup ).substr( 0, 7 ) == string( "SubMesh" ) ) {
4065 // --> get submesh id
4066 int subid = atoi( string( name_submeshgroup ).substr( 7 ).c_str() );
4069 // open submesh HDF group
4070 aSubGroup = new HDFgroup( name_submeshgroup, aGroup );
4071 aSubGroup->OpenOnDisk();
4073 // try to read and set reference to subshape
4074 GEOM::GEOM_Object_var aSubShapeObject;
4075 SMESH::SMESH_subMesh_var aSubMesh;
4077 if ( aSubGroup->ExistInternalObject( "Ref on shape" ) ) {
4078 // load submesh "Ref on shape" - it's an entry to SObject
4079 aDataset = new HDFdataset( "Ref on shape", aSubGroup );
4080 aDataset->OpenOnDisk();
4081 size = aDataset->GetSize();
4082 char* refFromFile = new char[ size ];
4083 aDataset->ReadFromDisk( refFromFile );
4084 aDataset->CloseOnDisk();
4085 if ( strlen( refFromFile ) > 0 ) {
4086 SALOMEDS::SObject_var subShapeSO = myCurrentStudy->FindObjectID( refFromFile );
4087 CORBA::Object_var subShapeObject = SObjectToObject( subShapeSO );
4088 if ( !CORBA::is_nil( subShapeObject ) ) {
4089 aSubShapeObject = GEOM::GEOM_Object::_narrow( subShapeObject );
4090 if ( !aSubShapeObject->_is_nil() )
4091 aSubMesh = SMESH::SMESH_subMesh::_duplicate
4092 ( myNewMeshImpl->createSubMesh( aSubShapeObject ) );
4093 if ( aSubMesh->_is_nil() )
4095 string iorSubString = GetORB()->object_to_string( aSubMesh );
4096 int newSubId = myStudyContext->findId( iorSubString );
4097 myStudyContext->mapOldToNew( subid, newSubId );
4102 if ( aSubMesh->_is_nil() )
4105 // VSR: Get submesh data from MED convertor
4106 // int anInternalSubmeshId = aSubMesh->GetId(); // this is not a persistent ID, it's an internal one computed from sub-shape
4107 // if (myNewMeshImpl->_mapSubMesh.find(anInternalSubmeshId) != myNewMeshImpl->_mapSubMesh.end()) {
4108 // if(MYDEBUG) MESSAGE("VSR - SMESH_Gen_i::Load(): loading from MED file submesh with ID = " <<
4109 // subid << " for subshape # " << anInternalSubmeshId);
4110 // SMESHDS_SubMesh* aSubMeshDS =
4111 // myNewMeshImpl->_mapSubMesh[anInternalSubmeshId]->CreateSubMeshDS();
4112 // if ( !aSubMeshDS ) {
4113 // if(MYDEBUG) MESSAGE("VSR - SMESH_Gen_i::Load(): FAILED to create a submesh for subshape # " <<
4114 // anInternalSubmeshId << " in current mesh!");
4117 // myReader.GetSubMesh( aSubMeshDS, subid );
4120 // try to get applied algorithms
4121 if ( aSubGroup->ExistInternalObject( "Applied Algorithms" ) ) {
4122 // open "applied algorithms" HDF group
4123 aSubSubGroup = new HDFgroup( "Applied Algorithms", aSubGroup );
4124 aSubSubGroup->OpenOnDisk();
4125 // get number of applied algorithms
4126 int aNbSubObjects = aSubSubGroup->nInternalObjects();
4127 for ( int l = 0; l < aNbSubObjects; l++ ) {
4128 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4129 aSubSubGroup->InternalObjectIndentify( l, name_dataset );
4130 // check if it is an algorithm
4131 if ( string( name_dataset ).substr( 0, 4 ) == string( "Algo" ) ) {
4132 aDataset = new HDFdataset( name_dataset, aSubSubGroup );
4133 aDataset->OpenOnDisk();
4134 size = aDataset->GetSize();
4135 char* refFromFile = new char[ size ];
4136 aDataset->ReadFromDisk( refFromFile );
4137 aDataset->CloseOnDisk();
4139 //SALOMEDS::SObject_var hypSO = myCurrentStudy->FindObjectID( refFromFile );
4140 //CORBA::Object_var hypObject = SObjectToObject( hypSO );
4141 int id = atoi( refFromFile );
4142 string anIOR = myStudyContext->getIORbyOldId( id );
4143 if ( !anIOR.empty() ) {
4144 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
4145 if ( !CORBA::is_nil( hypObject ) ) {
4146 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
4147 if ( !anHyp->_is_nil() && !aShapeObject->_is_nil() )
4148 myNewMeshImpl->addHypothesis( aSubShapeObject, anHyp );
4153 // close "applied algorithms" HDF group
4154 aSubSubGroup->CloseOnDisk();
4157 // try to get applied hypotheses
4158 if ( aSubGroup->ExistInternalObject( "Applied Hypotheses" ) ) {
4159 // open "applied hypotheses" HDF group
4160 aSubSubGroup = new HDFgroup( "Applied Hypotheses", aSubGroup );
4161 aSubSubGroup->OpenOnDisk();
4162 // get number of applied hypotheses
4163 int aNbSubObjects = aSubSubGroup->nInternalObjects();
4164 for ( int l = 0; l < aNbSubObjects; l++ ) {
4165 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4166 aSubSubGroup->InternalObjectIndentify( l, name_dataset );
4167 // check if it is a hypothesis
4168 if ( string( name_dataset ).substr( 0, 3 ) == string( "Hyp" ) ) {
4169 aDataset = new HDFdataset( name_dataset, aSubSubGroup );
4170 aDataset->OpenOnDisk();
4171 size = aDataset->GetSize();
4172 char* refFromFile = new char[ size ];
4173 aDataset->ReadFromDisk( refFromFile );
4174 aDataset->CloseOnDisk();
4176 //SALOMEDS::SObject_var hypSO = myCurrentStudy->FindObjectID( refFromFile );
4177 //CORBA::Object_var hypObject = SObjectToObject( hypSO );
4178 int id = atoi( refFromFile );
4179 string anIOR = myStudyContext->getIORbyOldId( id );
4180 if ( !anIOR.empty() ) {
4181 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
4182 if ( !CORBA::is_nil( hypObject ) ) {
4183 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
4184 if ( !anHyp->_is_nil() && !aShapeObject->_is_nil() )
4185 myNewMeshImpl->addHypothesis( aSubShapeObject, anHyp );
4190 // close "applied hypotheses" HDF group
4191 aSubSubGroup->CloseOnDisk();
4194 // close submesh HDF group
4195 aSubGroup->CloseOnDisk();
4198 // close submeshes containers HDF group
4199 aGroup->CloseOnDisk();
4205 // Read sub-meshes from MED
4206 // -------------------------
4207 if(MYDEBUG) MESSAGE("Create all sub-meshes");
4208 bool submeshesInFamilies = ( ! aTopGroup->ExistInternalObject( "Submeshes" ));
4209 if ( submeshesInFamilies )
4211 // old way working before fix of PAL 12992
4212 myReader.CreateAllSubMeshes();
4217 aGroup = new HDFgroup( "Submeshes", aTopGroup );
4218 aGroup->OpenOnDisk();
4220 int maxID = Max( mySMESHDSMesh->MaxSubMeshIndex(), mySMESHDSMesh->MaxShapeIndex() );
4221 vector< SMESHDS_SubMesh * > subMeshes( maxID + 1, (SMESHDS_SubMesh*) 0 );
4222 vector< TopAbs_ShapeEnum > smType ( maxID + 1, TopAbs_SHAPE );
4224 PositionCreator aPositionCreator;
4226 SMDS_NodeIteratorPtr nIt = mySMESHDSMesh->nodesIterator();
4227 SMDS_ElemIteratorPtr eIt = mySMESHDSMesh->elementsIterator();
4228 for ( int isNode = 0; isNode < 2; ++isNode )
4230 string aDSName( isNode ? "Node Submeshes" : "Element Submeshes");
4231 if ( aGroup->ExistInternalObject( (char*) aDSName.c_str() ))
4233 aDataset = new HDFdataset( (char*) aDSName.c_str(), aGroup );
4234 aDataset->OpenOnDisk();
4235 // read submesh IDs for all elements sorted by ID
4236 int nbElems = aDataset->GetSize();
4237 int* smIDs = new int [ nbElems ];
4238 aDataset->ReadFromDisk( smIDs );
4239 aDataset->CloseOnDisk();
4241 // get elements sorted by ID
4242 TIDSortedElemSet elemSet;
4244 while ( nIt->more() ) elemSet.insert( nIt->next() );
4246 while ( eIt->more() ) elemSet.insert( eIt->next() );
4247 //ASSERT( elemSet.size() == nbElems ); -- issue 20182
4248 // -- Most probably a bad study was saved when there were
4249 // not fixed bugs in SMDS_MeshInfo
4250 if ( elemSet.size() < nbElems ) {
4252 cout << "SMESH_Gen_i::Load(), warning: Node position data is invalid" << endl;
4254 nbElems = elemSet.size();
4256 // add elements to submeshes
4257 TIDSortedElemSet::iterator iE = elemSet.begin();
4258 for ( int i = 0; i < nbElems; ++i, ++iE )
4260 int smID = smIDs[ i ];
4261 if ( smID == 0 ) continue;
4262 const SMDS_MeshElement* elem = *iE;
4263 if( smID > maxID ) {
4264 // corresponding subshape no longer exists: maybe geom group has been edited
4265 if ( myNewMeshImpl->HasShapeToMesh() )
4266 mySMESHDSMesh->RemoveElement( elem );
4269 // get or create submesh
4270 SMESHDS_SubMesh* & sm = subMeshes[ smID ];
4272 sm = mySMESHDSMesh->NewSubMesh( smID );
4273 smType[ smID ] = mySMESHDSMesh->IndexToShape( smID ).ShapeType();
4277 SMDS_PositionPtr pos = aPositionCreator.MakePosition( smType[ smID ]);
4278 SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>( static_cast<const SMDS_MeshNode*>( elem ));
4279 node->SetPosition( pos );
4280 sm->AddNode( node );
4282 sm->AddElement( elem );
4288 } // end reading submeshes
4290 // Read node positions on sub-shapes (SMDS_Position)
4292 if ( aTopGroup->ExistInternalObject( "Node Positions" ))
4294 // There are 5 datasets to read:
4295 // "Nodes on Edges" - ID of node on edge
4296 // "Edge positions" - U parameter on node on edge
4297 // "Nodes on Faces" - ID of node on face
4298 // "Face U positions" - U parameter of node on face
4299 // "Face V positions" - V parameter of node on face
4300 const char* aEid_DSName = "Nodes on Edges";
4301 const char* aEu_DSName = "Edge positions";
4302 const char* aFu_DSName = "Face U positions";
4303 //char* aFid_DSName = "Nodes on Faces";
4304 //char* aFv_DSName = "Face V positions";
4307 int nbEids = 0, nbFids = 0;
4308 int *aEids = 0, *aFids = 0;
4309 double *aEpos = 0, *aFupos = 0, *aFvpos = 0;
4312 aGroup = new HDFgroup( "Node Positions", aTopGroup );
4313 aGroup->OpenOnDisk();
4315 // loop on 5 data sets
4316 int aNbObjects = aGroup->nInternalObjects();
4317 for ( int i = 0; i < aNbObjects; i++ )
4320 char aDSName[ HDF_NAME_MAX_LEN+1 ];
4321 aGroup->InternalObjectIndentify( i, aDSName );
4323 aDataset = new HDFdataset( aDSName, aGroup );
4324 aDataset->OpenOnDisk();
4325 if ( aDataset->GetType() == HDF_FLOAT64 ) // Positions
4327 double* pos = new double [ aDataset->GetSize() ];
4328 aDataset->ReadFromDisk( pos );
4330 if ( strncmp( aDSName, aEu_DSName, strlen( aEu_DSName )) == 0 )
4332 else if ( strncmp( aDSName, aFu_DSName, strlen( aFu_DSName )) == 0 )
4339 int aSize = aDataset->GetSize();
4341 // for reading files, created from 18.07.2005 till 10.10.2005
4342 if (aDataset->GetType() == HDF_STRING)
4343 aSize /= sizeof(int);
4345 int* ids = new int [aSize];
4346 aDataset->ReadFromDisk( ids );
4347 // on face or nodes?
4348 if ( strncmp( aDSName, aEid_DSName, strlen( aEid_DSName )) == 0 ) {
4357 aDataset->CloseOnDisk();
4358 } // loop on 5 datasets
4360 // Set node positions on edges or faces
4361 for ( int onFace = 0; onFace < 2; onFace++ )
4363 int nbNodes = ( onFace ? nbFids : nbEids );
4364 if ( nbNodes == 0 ) continue;
4365 int* aNodeIDs = ( onFace ? aFids : aEids );
4366 double* aUPos = ( onFace ? aFupos : aEpos );
4367 double* aVPos = ( onFace ? aFvpos : 0 );
4369 for ( int iNode = 0; iNode < nbNodes; iNode++ )
4371 const SMDS_MeshNode* node = mySMESHDSMesh->FindNode( aNodeIDs[ iNode ]);
4372 if ( !node ) continue; // maybe removed while Loading() if geometry changed
4373 SMDS_PositionPtr aPos = node->GetPosition();
4376 // ASSERT( aPos->GetTypeOfPosition() == SMDS_TOP_FACE );-- issue 20182
4377 // -- Most probably a bad study was saved when there were
4378 // not fixed bugs in SMDS_MeshInfo
4379 if ( aPos->GetTypeOfPosition() == SMDS_TOP_FACE ) {
4380 SMDS_FacePosition* fPos = const_cast<SMDS_FacePosition*>
4381 ( static_cast<const SMDS_FacePosition*>( aPos ));
4382 fPos->SetUParameter( aUPos[ iNode ]);
4383 fPos->SetVParameter( aVPos[ iNode ]);
4387 // ASSERT( aPos->GetTypeOfPosition() == SMDS_TOP_EDGE );-- issue 20182
4388 if ( aPos->GetTypeOfPosition() == SMDS_TOP_EDGE ) {
4389 SMDS_EdgePosition* fPos = const_cast<SMDS_EdgePosition*>
4390 ( static_cast<const SMDS_EdgePosition*>( aPos ));
4391 fPos->SetUParameter( aUPos[ iNode ]);
4396 if ( aEids ) delete [] aEids;
4397 if ( aFids ) delete [] aFids;
4398 if ( aEpos ) delete [] aEpos;
4399 if ( aFupos ) delete [] aFupos;
4400 if ( aFvpos ) delete [] aFvpos;
4402 aGroup->CloseOnDisk();
4404 } // if ( aTopGroup->ExistInternalObject( "Node Positions" ) )
4407 // try to get groups
4408 for ( int ii = GetNodeGroupsTag(); ii <= GetVolumeGroupsTag(); ii++ ) {
4409 char name_group[ 30 ];
4410 if ( ii == GetNodeGroupsTag() )
4411 strcpy( name_group, "Groups of Nodes" );
4412 else if ( ii == GetEdgeGroupsTag() )
4413 strcpy( name_group, "Groups of Edges" );
4414 else if ( ii == GetFaceGroupsTag() )
4415 strcpy( name_group, "Groups of Faces" );
4416 else if ( ii == GetVolumeGroupsTag() )
4417 strcpy( name_group, "Groups of Volumes" );
4419 if ( aTopGroup->ExistInternalObject( name_group ) ) {
4420 aGroup = new HDFgroup( name_group, aTopGroup );
4421 aGroup->OpenOnDisk();
4422 // get number of groups
4423 int aNbSubObjects = aGroup->nInternalObjects();
4424 for ( int j = 0; j < aNbSubObjects; j++ ) {
4425 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4426 aGroup->InternalObjectIndentify( j, name_dataset );
4427 // check if it is an group
4428 if ( string( name_dataset ).substr( 0, 5 ) == string( "Group" ) ) {
4430 int subid = atoi( string( name_dataset ).substr( 5 ).c_str() );
4433 aDataset = new HDFdataset( name_dataset, aGroup );
4434 aDataset->OpenOnDisk();
4436 // Retrieve actual group name
4437 size = aDataset->GetSize();
4438 char* nameFromFile = new char[ size ];
4439 aDataset->ReadFromDisk( nameFromFile );
4440 aDataset->CloseOnDisk();
4442 // Try to find a shape reference
4443 TopoDS_Shape aShape;
4444 char aRefName[ 30 ];
4445 sprintf( aRefName, "Ref on shape %d", subid);
4446 if ( aGroup->ExistInternalObject( aRefName ) ) {
4447 // load mesh "Ref on shape" - it's an entry to SObject
4448 aDataset = new HDFdataset( aRefName, aGroup );
4449 aDataset->OpenOnDisk();
4450 size = aDataset->GetSize();
4451 char* refFromFile = new char[ size ];
4452 aDataset->ReadFromDisk( refFromFile );
4453 aDataset->CloseOnDisk();
4454 if ( strlen( refFromFile ) > 0 ) {
4455 SALOMEDS::SObject_var shapeSO = myCurrentStudy->FindObjectID( refFromFile );
4456 CORBA::Object_var shapeObject = SObjectToObject( shapeSO );
4457 if ( !CORBA::is_nil( shapeObject ) ) {
4458 aShapeObject = GEOM::GEOM_Object::_narrow( shapeObject );
4459 if ( !aShapeObject->_is_nil() )
4460 aShape = GeomObjectToShape( aShapeObject );
4464 // Create group servant
4465 SMESH::ElementType type = (SMESH::ElementType)(ii - GetNodeGroupsTag() + 1);
4466 SMESH::SMESH_GroupBase_var aNewGroup = SMESH::SMESH_GroupBase::_duplicate
4467 ( myNewMeshImpl->createGroup( type, nameFromFile, aShape ) );
4468 // Obtain a SMESHDS_Group object
4469 if ( aNewGroup->_is_nil() )
4472 string iorSubString = GetORB()->object_to_string( aNewGroup );
4473 int newSubId = myStudyContext->findId( iorSubString );
4474 myStudyContext->mapOldToNew( subid, newSubId );
4476 SMESH_GroupBase_i* aGroupImpl =
4477 dynamic_cast<SMESH_GroupBase_i*>( GetServant( aNewGroup ).in() );
4481 SMESH_Group* aLocalGroup = myLocMesh.GetGroup( aGroupImpl->GetLocalID() );
4485 SMESHDS_GroupBase* aGroupBaseDS = aLocalGroup->GetGroupDS();
4486 aGroupBaseDS->SetStoreName( name_dataset );
4489 // Read color of the group
4490 char aGroupColorName[ 30 ];
4491 sprintf( aGroupColorName, "ColorGroup %d", subid);
4492 if ( aGroup->ExistInternalObject( aGroupColorName ) )
4494 aDataset = new HDFdataset( aGroupColorName, aGroup );
4495 aDataset->OpenOnDisk();
4496 size = aDataset->GetSize();
4497 double* anRGB = new double[ size ];
4498 aDataset->ReadFromDisk( anRGB );
4499 aDataset->CloseOnDisk();
4500 Quantity_Color aColor( anRGB[0], anRGB[1], anRGB[2], Quantity_TOC_RGB );
4501 aGroupBaseDS->SetColor( aColor );
4504 // Fill group with contents from MED file
4505 SMESHDS_Group* aGrp = dynamic_cast<SMESHDS_Group*>( aGroupBaseDS );
4507 myReader.GetGroup( aGrp );
4510 aGroup->CloseOnDisk();
4514 // read submeh order if any
4515 if( aTopGroup->ExistInternalObject( "Mesh Order" ) ) {
4516 aDataset = new HDFdataset( "Mesh Order", aTopGroup );
4517 aDataset->OpenOnDisk();
4518 size = aDataset->GetSize();
4519 int* smIDs = new int[ size ];
4520 aDataset->ReadFromDisk( smIDs );
4521 aDataset->CloseOnDisk();
4522 TListOfListOfInt anOrderIds;
4523 anOrderIds.push_back( TListOfInt() );
4524 for ( int i = 0; i < size; i++ )
4525 if ( smIDs[ i ] < 0 ) // is separator
4526 anOrderIds.push_back( TListOfInt() );
4528 anOrderIds.back().push_back(smIDs[ i ]);
4530 myNewMeshImpl->GetImpl().SetMeshOrder( anOrderIds );
4534 // notify algos on completed restoration
4535 for ( meshi_group = meshGroupList.begin(); meshi_group != meshGroupList.end(); ++meshi_group )
4537 SMESH_Mesh_i* myNewMeshImpl = meshi_group->first;
4538 ::SMESH_Mesh& myLocMesh = myNewMeshImpl->GetImpl();
4540 TopoDS_Shape myLocShape;
4541 if(myLocMesh.HasShapeToMesh())
4542 myLocShape = myLocMesh.GetShapeToMesh();
4544 myLocShape = SMESH_Mesh::PseudoShape();
4546 myLocMesh.GetSubMesh(myLocShape)->
4547 ComputeStateEngine (SMESH_subMesh::SUBMESH_RESTORED);
4550 for ( hyp_data = hypDataList.begin(); hyp_data != hypDataList.end(); ++hyp_data )
4552 SMESH_Hypothesis_i* hyp = hyp_data->first;
4553 hyp->UpdateAsMeshesRestored(); // for hyps needing full mesh data restored (issue 20918)
4558 aTopGroup->CloseOnDisk();
4561 aFile->CloseOnDisk();
4564 // Remove temporary files created from the stream
4566 SALOMEDS_Tool::RemoveTemporaryFiles( tmpDir.ToCString(), aFileSeq.in(), true );
4568 INFOS( "SMESH_Gen_i::Load completed" );
4572 //=============================================================================
4574 * SMESH_Gen_i::LoadASCII
4576 * Load SMESH module's data in ASCII format
4578 //=============================================================================
4580 bool SMESH_Gen_i::LoadASCII( SALOMEDS::SComponent_ptr theComponent,
4581 const SALOMEDS::TMPFile& theStream,
4583 bool isMultiFile ) {
4584 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::LoadASCII" );
4585 return Load( theComponent, theStream, theURL, isMultiFile );
4587 //before call main ::Load method it's need for decipher text format to
4588 //binary ( "|xx" => x' )
4589 int size = theStream.length();
4590 if ( int((size / 3 )*3) != size ) //error size of buffer
4593 int real_size = int(size / 3);
4595 _CORBA_Octet* buffer = new _CORBA_Octet[real_size];
4599 for ( int i = 0; i < real_size; i++ )
4601 memcpy( &(tmp[0]), &(theStream[i*3+1]), 2 );
4602 sscanf( tmp, "%x", &c );
4603 sprintf( (char*)&(buffer[i]), "%c", (char)c );
4606 SALOMEDS::TMPFile_var aRealStreamFile = new SALOMEDS::TMPFile(real_size, real_size, buffer, 1);
4608 return Load( theComponent, *(aRealStreamFile._retn()), theURL, isMultiFile );
4611 //=============================================================================
4613 * SMESH_Gen_i::Close
4615 * Clears study-connected data when it is closed
4617 //=============================================================================
4619 void SMESH_Gen_i::Close( SALOMEDS::SComponent_ptr theComponent )
4621 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Close" );
4623 // set correct current study
4624 if (theComponent->GetStudy()->StudyId() != GetCurrentStudyID())
4625 SetCurrentStudy(theComponent->GetStudy());
4627 // Clear study contexts data
4628 int studyId = GetCurrentStudyID();
4629 if ( myStudyContextMap.find( studyId ) != myStudyContextMap.end() ) {
4630 delete myStudyContextMap[ studyId ];
4631 myStudyContextMap.erase( studyId );
4634 // delete SMESH_Mesh's
4635 // See bug IPAL19437.
4637 // StudyContextStruct* context = myGen.GetStudyContext( studyId );
4638 // map< int, SMESH_Mesh* >::iterator i_mesh = context->mapMesh.begin();
4639 // for ( ; i_mesh != context->mapMesh.end(); ++i_mesh ) {
4640 // printf( "--------------------------- SMESH_Gen_i::Close, delete aGroup = %p \n", i_mesh->second );
4641 // delete i_mesh->second;
4645 // delete SMESHDS_Mesh's
4646 // it's too long on big meshes
4647 // if ( context->myDocument ) {
4648 // delete context->myDocument;
4649 // context->myDocument = 0;
4652 myCurrentStudy = SALOMEDS::Study::_nil();
4656 //=============================================================================
4658 * SMESH_Gen_i::ComponentDataType
4660 * Get component data type
4662 //=============================================================================
4664 char* SMESH_Gen_i::ComponentDataType()
4666 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::ComponentDataType" );
4667 return CORBA::string_dup( "SMESH" );
4671 //=============================================================================
4673 * SMESH_Gen_i::IORToLocalPersistentID
4675 * Transform data from transient form to persistent
4677 //=============================================================================
4679 char* SMESH_Gen_i::IORToLocalPersistentID( SALOMEDS::SObject_ptr /*theSObject*/,
4680 const char* IORString,
4681 CORBA::Boolean /*isMultiFile*/,
4682 CORBA::Boolean /*isASCII*/ )
4684 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::IORToLocalPersistentID" );
4685 StudyContext* myStudyContext = GetCurrentStudyContext();
4687 if ( myStudyContext && strcmp( IORString, "" ) != 0 ) {
4688 int anId = myStudyContext->findId( IORString );
4690 if(MYDEBUG) MESSAGE( "VSR " << anId )
4692 sprintf( strId, "%d", anId );
4693 return CORBA::string_dup( strId );
4696 return CORBA::string_dup( "" );
4699 //=============================================================================
4701 * SMESH_Gen_i::LocalPersistentIDToIOR
4703 * Transform data from persistent form to transient
4705 //=============================================================================
4707 char* SMESH_Gen_i::LocalPersistentIDToIOR( SALOMEDS::SObject_ptr /*theSObject*/,
4708 const char* aLocalPersistentID,
4709 CORBA::Boolean /*isMultiFile*/,
4710 CORBA::Boolean /*isASCII*/ )
4712 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::LocalPersistentIDToIOR(): id = " << aLocalPersistentID );
4713 StudyContext* myStudyContext = GetCurrentStudyContext();
4715 if ( myStudyContext && strcmp( aLocalPersistentID, "" ) != 0 ) {
4716 int anId = atoi( aLocalPersistentID );
4717 return CORBA::string_dup( myStudyContext->getIORbyOldId( anId ).c_str() );
4719 return CORBA::string_dup( "" );
4722 //=======================================================================
4723 //function : RegisterObject
4725 //=======================================================================
4727 int SMESH_Gen_i::RegisterObject(CORBA::Object_ptr theObject)
4729 StudyContext* myStudyContext = GetCurrentStudyContext();
4730 if ( myStudyContext && !CORBA::is_nil( theObject )) {
4731 CORBA::String_var iorString = GetORB()->object_to_string( theObject );
4732 return myStudyContext->addObject( string( iorString.in() ) );
4737 //================================================================================
4739 * \brief Return id of registered object
4740 * \param theObject - the Object
4741 * \retval int - Object id
4743 //================================================================================
4745 CORBA::Long SMESH_Gen_i::GetObjectId(CORBA::Object_ptr theObject)
4747 StudyContext* myStudyContext = GetCurrentStudyContext();
4748 if ( myStudyContext && !CORBA::is_nil( theObject )) {
4749 string iorString = GetORB()->object_to_string( theObject );
4750 return myStudyContext->findId( iorString );
4755 //=============================================================================
4757 * SMESH_Gen_i::SetName
4759 * Set a new object name
4761 //=============================================================================
4762 void SMESH_Gen_i::SetName(const char* theIOR,
4763 const char* theName)
4765 if ( theIOR && strcmp( theIOR, "" ) ) {
4766 CORBA::Object_var anObject = GetORB()->string_to_object( theIOR );
4767 SALOMEDS::SObject_var aSO = ObjectToSObject( myCurrentStudy, anObject );
4768 if ( !aSO->_is_nil() ) {
4769 SetName( aSO, theName );
4774 int SMESH_Gen_i::GetCurrentStudyID()
4776 return myCurrentStudy->_is_nil() || myCurrentStudy->_non_existent() ? -1 : myCurrentStudy->StudyId();
4779 //=============================================================================
4781 * SMESHEngine_factory
4783 * C factory, accessible with dlsym, after dlopen
4785 //=============================================================================
4789 PortableServer::ObjectId* SMESHEngine_factory( CORBA::ORB_ptr orb,
4790 PortableServer::POA_ptr poa,
4791 PortableServer::ObjectId* contId,
4792 const char* instanceName,
4793 const char* interfaceName )
4795 if(MYDEBUG) MESSAGE( "PortableServer::ObjectId* SMESHEngine_factory()" );
4796 if(MYDEBUG) SCRUTE(interfaceName);
4797 SMESH_Gen_i* aSMESHGen = new SMESH_Gen_i(orb, poa, contId, instanceName, interfaceName);
4798 return aSMESHGen->getId() ;