1 // Copyright (C) 2007-2016 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, or (at your option) any later version.
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 // File : SMESH_Gen_i.cxx
23 // Author : Paul RASCLE, EDF
27 #include <TopExp_Explorer.hxx>
29 #include <TopoDS_Iterator.hxx>
30 #include <TopoDS_Compound.hxx>
31 #include <TopoDS_CompSolid.hxx>
32 #include <TopoDS_Solid.hxx>
33 #include <TopoDS_Shell.hxx>
34 #include <TopoDS_Face.hxx>
35 #include <TopoDS_Wire.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopoDS_Vertex.hxx>
38 #include <TopoDS_Shape.hxx>
39 #include <TopTools_MapOfShape.hxx>
40 #include <TopTools_IndexedMapOfShape.hxx>
41 #include <TopTools_ListOfShape.hxx>
42 #include <TopTools_ListIteratorOfListOfShape.hxx>
44 #include <BRep_Tool.hxx>
45 #include <TCollection_AsciiString.hxx>
47 #include <BRepPrimAPI_MakeSphere.hxx>
48 #include <BRepPrimAPI_MakeCylinder.hxx>
49 #include <BRepPrimAPI_MakeBox.hxx>
60 #define LibHandle HMODULE
61 #define LoadLib( name ) LoadLibrary( name )
62 #define GetProc GetProcAddress
63 #define UnLoadLib( handle ) FreeLibrary( handle );
65 #define LibHandle void*
66 #define LoadLib( name ) dlopen( name, RTLD_LAZY )
68 #define UnLoadLib( handle ) dlclose( handle );
71 #include "SMESH_Gen_i.hxx"
72 #include "SMESH_version.h"
74 #include "DriverMED_W_SMESHDS_Mesh.h"
75 #include "DriverMED_R_SMESHDS_Mesh.h"
77 #include "DriverCGNS_Read.hxx"
79 #include "MED_Factory.hxx"
80 #include "SMDS_EdgePosition.hxx"
81 #include "SMDS_FacePosition.hxx"
82 #include "SMDS_PolyhedralVolumeOfNodes.hxx"
83 #include "SMDS_SetIterator.hxx"
84 #include "SMDS_SpacePosition.hxx"
85 #include "SMDS_VertexPosition.hxx"
86 #include "SMESHDS_Document.hxx"
87 #include "SMESHDS_Group.hxx"
88 #include "SMESHDS_GroupOnGeom.hxx"
89 #include "SMESH_Algo_i.hxx"
90 #include "SMESH_File.hxx"
91 #include "SMESH_Group.hxx"
92 #include "SMESH_Group_i.hxx"
93 #include "SMESH_Hypothesis.hxx"
94 #include "SMESH_Hypothesis_i.hxx"
95 #include "SMESH_Mesh.hxx"
96 #include "SMESH_MeshEditor.hxx"
97 #include "SMESH_Mesh_i.hxx"
98 #include "SMESH_PreMeshInfo.hxx"
99 #include "SMESH_PythonDump.hxx"
100 #include "SMESH_ControlsDef.hxx"
101 #include "SMESH_TryCatch.hxx" // to include after OCC headers!
103 #include CORBA_SERVER_HEADER(SMESH_Group)
104 #include CORBA_SERVER_HEADER(SMESH_Filter)
105 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
108 #include <GEOM_Client.hxx>
110 #include <Basics_Utils.hxx>
111 #include <Basics_DirUtils.hxx>
113 #include <OpUtil.hxx>
114 #include <SALOMEDS_Tool.hxx>
115 #include <SALOME_Container_i.hxx>
116 #include <SALOME_LifeCycleCORBA.hxx>
117 #include <SALOME_NamingService.hxx>
118 #include <Utils_CorbaException.hxx>
119 #include <Utils_ExceptHandlers.hxx>
120 #include <Utils_SINGLETON.hxx>
121 #include <utilities.h>
123 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
124 #include CORBA_CLIENT_HEADER(SALOME_Session)
126 // helpers about SALOME::GenericObj
127 #include <SALOMEDS_wrap.hxx>
128 #include <SALOMEDS_Attributes_wrap.hxx>
129 #include <GEOM_wrap.hxx>
137 using SMESH::TPythonDump;
140 #define NUM_TMP_FILES 2
143 static int MYDEBUG = 0;
145 static int MYDEBUG = 0;
148 // Static variables definition
149 GEOM::GEOM_Gen_var SMESH_Gen_i::myGeomGen = GEOM::GEOM_Gen::_nil();
150 CORBA::ORB_var SMESH_Gen_i::myOrb;
151 PortableServer::POA_var SMESH_Gen_i::myPoa;
152 SALOME_NamingService* SMESH_Gen_i::myNS = NULL;
153 SALOME_LifeCycleCORBA* SMESH_Gen_i::myLCC = NULL;
154 SMESH_Gen_i* SMESH_Gen_i::mySMESHGen = NULL;
157 const int nbElemPerDiagonal = 10;
159 //=============================================================================
161 * GetServant [ static ]
163 * Get servant of the CORBA object
165 //=============================================================================
167 PortableServer::ServantBase_var SMESH_Gen_i::GetServant( CORBA::Object_ptr theObject )
169 if( CORBA::is_nil( theObject ) || CORBA::is_nil( GetPOA() ) )
172 PortableServer::Servant aServant = GetPOA()->reference_to_servant( theObject );
176 INFOS( "GetServant - Unknown exception was caught!!!" );
181 //=============================================================================
183 * SObjectToObject [ static ]
185 * Get CORBA object corresponding to the SALOMEDS::SObject
187 //=============================================================================
189 CORBA::Object_var SMESH_Gen_i::SObjectToObject( SALOMEDS::SObject_ptr theSObject )
191 SALOMEDS::GenericAttribute_wrap anAttr;
192 CORBA::Object_var anObj;
193 if ( !theSObject->_is_nil() ) {
195 if( theSObject->FindAttribute( anAttr.inout(), "AttributeIOR" ) ) {
196 SALOMEDS::AttributeIOR_wrap anIOR = anAttr;
197 CORBA::String_var aValue = anIOR->Value();
198 if( strcmp( aValue, "" ) != 0 )
199 anObj = GetORB()->string_to_object( aValue );
203 INFOS( "SObjectToObject - Unknown exception was caught!!!" );
209 //=============================================================================
213 * Get SALOME_NamingService object
215 //=============================================================================
217 SALOME_NamingService* SMESH_Gen_i::GetNS()
219 if ( myNS == NULL ) {
220 myNS = SINGLETON_<SALOME_NamingService>::Instance();
221 ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting());
222 myNS->init_orb( GetORB() );
227 //=============================================================================
231 * Get SALOME_LifeCycleCORBA object
233 //=============================================================================
234 SALOME_LifeCycleCORBA* SMESH_Gen_i::GetLCC() {
235 if ( myLCC == NULL ) {
236 myLCC = new SALOME_LifeCycleCORBA( GetNS() );
242 //=============================================================================
244 * GetGeomEngine [ static ]
246 * Get GEOM::GEOM_Gen reference
248 //=============================================================================
249 GEOM::GEOM_Gen_var SMESH_Gen_i::GetGeomEngine() {
250 //CCRT GEOM::GEOM_Gen_var aGeomEngine =
251 //CCRT GEOM::GEOM_Gen::_narrow( GetLCC()->FindOrLoad_Component("FactoryServer","GEOM") );
252 //CCRT return aGeomEngine._retn();
253 if(CORBA::is_nil(myGeomGen))
255 Engines::EngineComponent_ptr temp=GetLCC()->FindOrLoad_Component("FactoryServer","GEOM");
256 myGeomGen=GEOM::GEOM_Gen::_narrow(temp);
261 //=============================================================================
263 * SMESH_Gen_i::SMESH_Gen_i
265 * Default constructor: not for use
267 //=============================================================================
269 SMESH_Gen_i::SMESH_Gen_i()
273 //=============================================================================
275 * SMESH_Gen_i::SMESH_Gen_i
277 * Standard constructor, used with Container
279 //=============================================================================
281 SMESH_Gen_i::SMESH_Gen_i( CORBA::ORB_ptr orb,
282 PortableServer::POA_ptr poa,
283 PortableServer::ObjectId* contId,
284 const char* instanceName,
285 const char* interfaceName )
286 : Engines_Component_i( orb, poa, contId, instanceName, interfaceName )
289 myOrb = CORBA::ORB::_duplicate(orb);
290 myPoa = PortableServer::POA::_duplicate(poa);
293 _id = myPoa->activate_object( _thisObj );
295 myIsEmbeddedMode = false;
296 myShapeReader = NULL; // shape reader
298 myIsHistoricalPythonDump = true;
299 myToForgetMeshDataOnHypModif = false;
301 // set it in standalone mode only
302 //OSD::SetSignal( true );
304 // 0020605: EDF 1190 SMESH: Display performance. 80 seconds for 52000 cells.
305 // find out mode (embedded or standalone) here else
306 // meshes created before calling SMESH_Client::GetSMESHGen(), which calls
307 // SMESH_Gen_i::SetEmbeddedMode(), have wrong IsEmbeddedMode flag
308 if ( SALOME_NamingService* ns = GetNS() )
310 CORBA::Object_var obj = ns->Resolve( "/Kernel/Session" );
311 SALOME::Session_var session = SALOME::Session::_narrow( obj ) ;
312 if ( !session->_is_nil() )
314 CORBA::String_var str_host = session->getHostname();
315 CORBA::Long s_pid = session->getPID();
316 string my_host = Kernel_Utils::GetHostname();
318 long my_pid = (long)_getpid();
320 long my_pid = (long) getpid();
322 SetEmbeddedMode( s_pid == my_pid && my_host == str_host.in() );
327 //=============================================================================
329 * SMESH_Gen_i::~SMESH_Gen_i
333 //=============================================================================
335 SMESH_Gen_i::~SMESH_Gen_i()
337 // delete hypothesis creators
338 map<string, GenericHypothesisCreator_i*>::iterator itHyp, itHyp2;
339 for (itHyp = myHypCreatorMap.begin(); itHyp != myHypCreatorMap.end(); itHyp++)
341 // same creator can be mapped under different names
342 GenericHypothesisCreator_i* creator = (*itHyp).second;
346 for (itHyp2 = itHyp; itHyp2 != myHypCreatorMap.end(); itHyp2++)
347 if ( creator == (*itHyp2).second )
348 (*itHyp2).second = 0;
350 myHypCreatorMap.clear();
352 // Clear study contexts data
353 map<int, StudyContext*>::iterator it;
354 for ( it = myStudyContextMap.begin(); it != myStudyContextMap.end(); ++it ) {
357 myStudyContextMap.clear();
358 // delete shape reader
360 delete myShapeReader;
362 //=============================================================================
364 * SMESH_Gen_i::getHypothesisCreator
366 * Get hypothesis creator
368 //=============================================================================
369 GenericHypothesisCreator_i* SMESH_Gen_i::getHypothesisCreator(const char* theHypName,
370 const char* theLibName,
371 std::string& thePlatformLibName)
372 throw (SALOME::SALOME_Exception)
374 std::string aPlatformLibName;
375 /* It's Need to tranlate lib name for WIN32 or X platform */
376 if ( theLibName && theLibName[0] != '\0' )
378 int libNameLen = strlen(theLibName);
379 //check for old format "libXXXXXXX.so"
380 if (libNameLen > 7 &&
381 !strncmp( theLibName, "lib", 3 ) &&
382 !strcmp( theLibName+libNameLen-3, ".so" ))
386 aPlatformLibName = std::string( theLibName+3, libNameLen-6 ) + ".dll";
388 aPlatformLibName = theLibName;
393 //try to use new format
395 aPlatformLibName = theLibName;
396 aPlatformLibName += ".dll";
398 aPlatformLibName = "lib" + std::string( theLibName ) + ".so";
402 thePlatformLibName = aPlatformLibName;
404 Unexpect aCatch(SALOME_SalomeException);
405 if(MYDEBUG) MESSAGE( "Create Hypothesis <" << theHypName << "> from " << aPlatformLibName);
407 typedef GenericHypothesisCreator_i* (*GetHypothesisCreator)(const char* );
408 GenericHypothesisCreator_i* aCreator;
411 // check, if creator for this hypothesis type already exists
412 if (myHypCreatorMap.find(string(theHypName)) == myHypCreatorMap.end())
414 // load plugin library
415 if(MYDEBUG) MESSAGE("Loading server meshers plugin library ...");
416 LibHandle libHandle = LoadLib( aPlatformLibName.c_str() );
419 // report any error, if occured
421 const char* anError = dlerror();
422 throw(SALOME_Exception(anError));
424 throw(SALOME_Exception(LOCALIZED( "Can't load server meshers plugin library" )));
428 // get method, returning hypothesis creator
429 if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
430 GetHypothesisCreator procHandle =
431 (GetHypothesisCreator)GetProc( libHandle, "GetHypothesisCreator" );
434 throw(SALOME_Exception(LOCALIZED("bad hypothesis plugin library")));
435 UnLoadLib(libHandle);
438 // get hypothesis creator
439 if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << theHypName);
440 aCreator = procHandle(theHypName);
443 throw(SALOME_Exception(LOCALIZED("no such a hypothesis in this plugin")));
445 // map hypothesis creator to a hypothesis name
446 myHypCreatorMap[string(theHypName)] = aCreator;
451 return myHypCreatorMap[string(theHypName)];
454 catch (SALOME_Exception& S_ex)
456 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
461 //=============================================================================
463 * SMESH_Gen_i::createHypothesis
465 * Create hypothesis of given type
467 //=============================================================================
468 SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::createHypothesis(const char* theHypName,
469 const char* theLibName)
471 SMESH_Hypothesis_i* myHypothesis_i = 0;
472 SMESH::SMESH_Hypothesis_var hypothesis_i;
473 std::string aPlatformLibName;
474 GenericHypothesisCreator_i* aCreator =
475 getHypothesisCreator(theHypName, theLibName, aPlatformLibName);
477 // create a new hypothesis object, store its ref. in studyContext
478 myHypothesis_i = aCreator->Create(myPoa, GetCurrentStudyID(), &myGen);
481 myHypothesis_i->SetLibName( aPlatformLibName.c_str() ); // for persistency assurance
482 CORBA::String_var hypName = myHypothesis_i->GetName();
483 myHypCreatorMap[ hypName.in() ] = aCreator;
485 // activate the CORBA servant of hypothesis
486 hypothesis_i = myHypothesis_i->_this();
487 int nextId = RegisterObject( hypothesis_i );
488 if(MYDEBUG) { MESSAGE( "Add hypo to map with id = "<< nextId ); }
489 else { nextId = 0; } // avoid "unused variable" warning in release mode
491 return hypothesis_i._retn();
494 //=============================================================================
496 * SMESH_Gen_i::createMesh
498 * Create empty mesh on shape
500 //=============================================================================
501 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::createMesh()
502 throw ( SALOME::SALOME_Exception )
504 Unexpect aCatch(SALOME_SalomeException);
505 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::createMesh" );
507 // Get or create the GEOM_Client instance
509 // create a new mesh object servant, store it in a map in study context
510 SMESH_Mesh_i* meshServant = new SMESH_Mesh_i( GetPOA(), this, GetCurrentStudyID() );
511 // create a new mesh object
512 if(MYDEBUG) MESSAGE("myIsEmbeddedMode " << myIsEmbeddedMode);
513 meshServant->SetImpl( myGen.CreateMesh( GetCurrentStudyID(), myIsEmbeddedMode ));
515 // activate the CORBA servant of Mesh
516 SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( meshServant->_this() );
517 int nextId = RegisterObject( mesh );
518 if(MYDEBUG) { MESSAGE( "Add mesh to map with id = "<< nextId); }
519 else { nextId = 0; } // avoid "unused variable" warning in release mode
522 catch (SALOME_Exception& S_ex) {
523 THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
525 return SMESH::SMESH_Mesh::_nil();
528 //=============================================================================
530 * SMESH_Gen_i::GetShapeReader
534 //=============================================================================
535 GEOM_Client* SMESH_Gen_i::GetShapeReader()
537 // create shape reader if necessary
538 if ( !myShapeReader )
539 myShapeReader = new GEOM_Client(GetContainerRef());
540 ASSERT( myShapeReader );
541 return myShapeReader;
544 //=============================================================================
546 * SMESH_Gen_i::SetGeomEngine
548 * Set GEOM::GEOM_Gen reference
550 //=============================================================================
551 //GEOM::GEOM_Gen_ptr SMESH_Gen_i::SetGeomEngine( const char* containerLoc )
552 void SMESH_Gen_i::SetGeomEngine( GEOM::GEOM_Gen_ptr geomcompo )
554 //Engines::Component_ptr temp=GetLCC()->FindOrLoad_Component(containerLoc,"GEOM");
555 //myGeomGen=GEOM::GEOM_Gen::_narrow(temp);
556 myGeomGen=GEOM::GEOM_Gen::_duplicate(geomcompo);
560 //=============================================================================
562 * SMESH_Gen_i::SetEmbeddedMode
566 //=============================================================================
568 void SMESH_Gen_i::SetEmbeddedMode( CORBA::Boolean theMode )
570 myIsEmbeddedMode = theMode;
572 if ( !myIsEmbeddedMode ) {
573 //PAL10867: disable signals catching with "noexcepthandler" option
574 char* envNoCatchSignals = getenv("NOT_INTERCEPT_SIGNALS");
575 if (!envNoCatchSignals || !atoi(envNoCatchSignals))
580 char* envDisableFPE = getenv("DISABLE_FPE");
581 if (envDisableFPE && atoi(envDisableFPE))
586 OSD::SetSignal( raiseFPE );
588 // else OSD::SetSignal() is called in GUI
592 //=============================================================================
594 * SMESH_Gen_i::IsEmbeddedMode
598 //=============================================================================
600 CORBA::Boolean SMESH_Gen_i::IsEmbeddedMode()
602 return myIsEmbeddedMode;
605 //=============================================================================
607 * SMESH_Gen_i::SetCurrentStudy
611 //=============================================================================
613 void SMESH_Gen_i::SetCurrentStudy( SALOMEDS::Study_ptr theStudy )
615 setCurrentStudy( theStudy );
618 void SMESH_Gen_i::setCurrentStudy( SALOMEDS::Study_ptr theStudy,
619 bool theStudyIsBeingClosed)
621 int curStudyId = GetCurrentStudyID();
622 myCurrentStudy = SALOMEDS::Study::_duplicate( theStudy );
623 // create study context, if it doesn't exist and set current study
624 int studyId = GetCurrentStudyID();
625 if ( myStudyContextMap.find( studyId ) == myStudyContextMap.end() )
626 myStudyContextMap[ studyId ] = new StudyContext;
628 // myCurrentStudy may be nil
629 if ( !theStudyIsBeingClosed && !CORBA::is_nil( myCurrentStudy ) ) {
630 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
631 SALOMEDS::SComponent_wrap GEOM_var = myCurrentStudy->FindComponent( "GEOM" );
632 if( !GEOM_var->_is_nil() )
633 aStudyBuilder->LoadWith( GEOM_var, GetGeomEngine() );
634 // NPAL16168, issue 0020210
635 // Let meshes update their data depending on GEOM groups that could change
636 if ( curStudyId != studyId )
638 CORBA::String_var compDataType = ComponentDataType();
639 SALOMEDS::SComponent_wrap me = myCurrentStudy->FindComponent( compDataType.in() );
640 if ( !me->_is_nil() ) {
641 SALOMEDS::ChildIterator_wrap anIter = myCurrentStudy->NewChildIterator( me );
642 for ( ; anIter->More(); anIter->Next() ) {
643 SALOMEDS::SObject_wrap so = anIter->Value();
644 CORBA::Object_var ior = SObjectToObject( so );
645 if ( SMESH_Mesh_i* mesh = SMESH::DownCast<SMESH_Mesh_i*>( ior ))
646 mesh->CheckGeomModif();
653 //=============================================================================
655 * SMESH_Gen_i::GetCurrentStudy
659 //=============================================================================
661 SALOMEDS::Study_ptr SMESH_Gen_i::GetCurrentStudy()
663 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetCurrentStudy: study Id = " << GetCurrentStudyID() );
664 if ( GetCurrentStudyID() < 0 )
665 return SALOMEDS::Study::_nil();
666 return SALOMEDS::Study::_duplicate( myCurrentStudy );
669 //=============================================================================
671 * SMESH_Gen_i::GetCurrentStudyContext
673 * Get current study context
675 //=============================================================================
676 StudyContext* SMESH_Gen_i::GetCurrentStudyContext()
678 if ( !CORBA::is_nil( myCurrentStudy ) &&
679 myStudyContextMap.find( GetCurrentStudyID() ) != myStudyContextMap.end() )
680 return myStudyContextMap[ myCurrentStudy->StudyId() ];
685 //=============================================================================
687 * SMESH_Gen_i::CreateHypothesis
689 * Create hypothesis/algorothm of given type and publish it in the study
691 //=============================================================================
693 SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::CreateHypothesis( const char* theHypName,
694 const char* theLibName )
695 throw ( SALOME::SALOME_Exception )
697 Unexpect aCatch(SALOME_SalomeException);
698 // Create hypothesis/algorithm
699 SMESH::SMESH_Hypothesis_var hyp = this->createHypothesis( theHypName, theLibName );
701 // Publish hypothesis/algorithm in the study
702 if ( CanPublishInStudy( hyp ) ) {
703 SALOMEDS::SObject_wrap aSO = PublishHypothesis( myCurrentStudy, hyp );
704 if ( !aSO->_is_nil() ) {
705 // Update Python script
706 TPythonDump() << aSO << " = " << this << ".CreateHypothesis('"
707 << theHypName << "', '" << theLibName << "')";
714 //================================================================================
716 * \brief Return a hypothesis holding parameter values corresponding either to the mesh
717 * existing on the given geometry or to size of the geometry.
718 * \param theHypType - hypothesis type name
719 * \param theLibName - plugin library name
720 * \param theMesh - The mesh of interest
721 * \param theGeom - The shape to get parameter values from
722 * \retval SMESH::SMESH_Hypothesis_ptr - The returned hypothesis may be the one existing
723 * in a study and used to compute the mesh, or a temporary one created just to pass
726 //================================================================================
728 SMESH::SMESH_Hypothesis_ptr
729 SMESH_Gen_i::GetHypothesisParameterValues (const char* theHypType,
730 const char* theLibName,
731 SMESH::SMESH_Mesh_ptr theMesh,
732 GEOM::GEOM_Object_ptr theGeom,
733 CORBA::Boolean byMesh)
734 throw ( SALOME::SALOME_Exception )
736 Unexpect aCatch(SALOME_SalomeException);
737 if ( byMesh && CORBA::is_nil( theMesh ) )
738 return SMESH::SMESH_Hypothesis::_nil();
739 if ( byMesh && CORBA::is_nil( theGeom ) )
740 return SMESH::SMESH_Hypothesis::_nil();
742 // -----------------------------------------------
743 // find hypothesis used to mesh theGeom
744 // -----------------------------------------------
746 // get mesh and shape
747 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
748 TopoDS_Shape shape = GeomObjectToShape( theGeom );
749 if ( byMesh && ( !meshServant || meshServant->NbNodes()==0 || shape.IsNull() ))
750 return SMESH::SMESH_Hypothesis::_nil();
751 ::SMESH_Mesh* mesh = meshServant ? &meshServant->GetImpl() : (::SMESH_Mesh*)0;
753 // create a temporary hypothesis to know its dimension
754 SMESH::SMESH_Hypothesis_var tmpHyp = this->createHypothesis( theHypType, theLibName );
755 SMESH_Hypothesis_i* hypServant = SMESH::DownCast<SMESH_Hypothesis_i*>( tmpHyp );
757 return SMESH::SMESH_Hypothesis::_nil();
758 ::SMESH_Hypothesis* hyp = hypServant->GetImpl();
761 // look for a hypothesis of theHypType used to mesh the shape
762 if ( myGen.GetShapeDim( shape ) == hyp->GetDim() )
765 SMESH::ListOfHypothesis_var aHypList = theMesh->GetHypothesisList( theGeom );
766 int nbLocalHyps = aHypList->length();
767 for ( int i = 0; i < nbLocalHyps; i++ ) {
768 CORBA::String_var hypName = aHypList[i]->GetName();
769 if ( strcmp( theHypType, hypName.in() ) == 0 ) // FOUND local!
770 return SMESH::SMESH_Hypothesis::_duplicate( aHypList[i] );
772 // check super shapes
773 TopTools_ListIteratorOfListOfShape itShape( mesh->GetAncestors( shape ));
774 while ( nbLocalHyps == 0 && itShape.More() ) {
775 GEOM::GEOM_Object_ptr geomObj = ShapeToGeomObject( itShape.Value() );
776 if ( ! CORBA::is_nil( geomObj )) {
777 SMESH::ListOfHypothesis_var aHypList = theMesh->GetHypothesisList( geomObj );
778 nbLocalHyps = aHypList->length();
779 for ( int i = 0; i < nbLocalHyps; i++ )
780 if ( strcmp( theHypType, aHypList[i]->GetName() ) == 0 ) // FOUND global!
781 return SMESH::SMESH_Hypothesis::_duplicate( aHypList[i] );
787 // let the temporary hypothesis find out somehow parameter values by mesh
788 if ( hyp->SetParametersByMesh( mesh, shape ))
789 return SMESH::SMESH_Hypothesis::_duplicate( tmpHyp );
794 diagonal = mesh->GetShapeDiagonalSize();
796 diagonal = ::SMESH_Mesh::GetShapeDiagonalSize( shape );
797 ::SMESH_Hypothesis::TDefaults dflts;
798 dflts._elemLength = diagonal / myGen.GetBoundaryBoxSegmentation();
799 dflts._nbSegments = myGen.GetDefaultNbSegments();
800 dflts._shape = &shape;
801 // let the temporary hypothesis initialize it's values
802 if ( hyp->SetParametersByDefaults( dflts, mesh ))
803 return SMESH::SMESH_Hypothesis::_duplicate( tmpHyp );
806 return SMESH::SMESH_Hypothesis::_nil();
809 //=============================================================================
811 * Returns \c True if a hypothesis is assigned to a sole sub-mesh in a current Study
812 * \param [in] theHyp - the hypothesis of interest
813 * \param [out] theMesh - the sole mesh using \a theHyp
814 * \param [out] theShape - the sole geometry \a theHyp is assigned to
815 * \return boolean - \c True if \a theMesh and \a theShape are sole using \a theHyp
817 * If two meshes on same shape have theHyp assigned to the same sub-shape, they are
818 * considered as SAME sub-mesh => result is \c true.
819 * This method ids used to initialize SMESHGUI_GenericHypothesisCreator with
820 * a shape to which an hyp being edited is assigned.
822 //=============================================================================
824 CORBA::Boolean SMESH_Gen_i::GetSoleSubMeshUsingHyp( SMESH::SMESH_Hypothesis_ptr theHyp,
825 SMESH::SMESH_Mesh_out theMesh,
826 GEOM::GEOM_Object_out theShape)
828 if ( GetCurrentStudyID() < 0 || CORBA::is_nil( theHyp ))
831 // get Mesh component SO
832 CORBA::String_var compDataType = ComponentDataType();
833 SALOMEDS::SComponent_wrap comp = myCurrentStudy->FindComponent( compDataType.in() );
834 if ( CORBA::is_nil( comp ))
837 // look for child SO of meshes
838 SMESH::SMESH_Mesh_var foundMesh;
839 TopoDS_Shape foundShape;
841 SALOMEDS::ChildIterator_wrap meshIter = myCurrentStudy->NewChildIterator( comp );
842 for ( ; meshIter->More() && isSole; meshIter->Next() )
844 SALOMEDS::SObject_wrap curSO = meshIter->Value();
845 CORBA::Object_var obj = SObjectToObject( curSO );
846 SMESH_Mesh_i* mesh_i = SMESH::DownCast< SMESH_Mesh_i* >( obj );
850 // look for a sole shape where theHyp is assigned
851 bool isHypFound = false;
852 const ShapeToHypothesis & s2hyps = mesh_i->GetImpl().GetMeshDS()->GetHypotheses();
853 ShapeToHypothesis::Iterator s2hypsIt( s2hyps );
854 for ( ; s2hypsIt.More() && isSole; s2hypsIt.Next() )
856 const THypList& hyps = s2hypsIt.Value();
857 THypList::const_iterator h = hyps.begin();
858 for ( ; h != hyps.end(); ++h )
859 if ( (*h)->GetID() == theHyp->GetId() )
861 if ( h != hyps.end()) // theHyp found
864 if ( ! foundShape.IsNull() &&
865 ! foundShape.IsSame( s2hypsIt.Key() )) // not a sole sub-shape
867 foundShape.Nullify();
871 foundShape = s2hypsIt.Key();
873 } // loop on assigned hyps
875 if ( isHypFound && !foundShape.IsNull() ) // a mesh using theHyp is found
877 if ( !foundMesh->_is_nil() ) // not a sole mesh
879 GEOM::GEOM_Object_var s1 = mesh_i ->GetShapeToMesh();
880 GEOM::GEOM_Object_var s2 = foundMesh->GetShapeToMesh();
881 if ( ! ( isSole = s1->IsSame( s2 )))
884 foundMesh = SMESH::SMESH_Mesh::_narrow( obj );
890 ! foundMesh->_is_nil() &&
891 ! foundShape.IsNull() )
893 theMesh = foundMesh._retn();
894 theShape = ShapeToGeomObject( foundShape );
895 return ( !theMesh->_is_nil() && !theShape->_is_nil() );
900 //=============================================================================
902 * Sets number of segments per diagonal of boundary box of geometry by which
903 * default segment length of appropriate 1D hypotheses is defined
905 //=============================================================================
907 void SMESH_Gen_i::SetBoundaryBoxSegmentation( CORBA::Long theNbSegments )
908 throw ( SALOME::SALOME_Exception )
910 if ( theNbSegments > 0 )
911 myGen.SetBoundaryBoxSegmentation( int( theNbSegments ));
913 THROW_SALOME_CORBA_EXCEPTION( "non-positive number of segments", SALOME::BAD_PARAM );
915 //=============================================================================
917 * \brief Sets default number of segments per edge
919 //=============================================================================
920 void SMESH_Gen_i::SetDefaultNbSegments(CORBA::Long theNbSegments)
921 throw ( SALOME::SALOME_Exception )
923 if ( theNbSegments > 0 )
924 myGen.SetDefaultNbSegments( int(theNbSegments) );
926 THROW_SALOME_CORBA_EXCEPTION( "non-positive number of segments", SALOME::BAD_PARAM );
929 //=============================================================================
933 //=============================================================================
935 void SMESH_Gen_i::SetOption(const char* name, const char* value)
937 if ( name && value && strlen( value ) > 0 )
940 if ( strcmp(name, "historical_python_dump") == 0 )
942 myIsHistoricalPythonDump = ( value[0] == '1' || toupper(value[0]) == 'T' ); // 1 || true
943 msgToGUI = "preferences/SMESH/historical_python_dump/";
944 msgToGUI += myIsHistoricalPythonDump ? "true" : "false";
946 else if ( strcmp(name, "forget_mesh_on_hyp_modif") == 0 )
948 myToForgetMeshDataOnHypModif = ( value[0] == '1' || toupper(value[0]) == 'T' ); // 1 || true
949 msgToGUI = "preferences/SMESH/forget_mesh_on_hyp_modif/";
950 msgToGUI += myToForgetMeshDataOnHypModif ? "true" : "false";
952 else if ( strcmp(name, "default_grp_color") == 0 )
956 // color must be presented as a string of next form:
957 if ( str.at(0) == '#' && str.length() == 7 ) { // hexadecimal color ("#ffaa00", for example)
959 for ( size_t i = 0; i < str.length()/2; i++ )
960 if ( str.at(i*2) >= '0' && str.at(i*2) <= 'f' && str.at(i*2+1) >= '0' && str.at(i*2+1) <= 'f' )
961 color.push_back( strtol( str.substr( i*2, 2 ).c_str(), NULL, 16 ) );
963 else if ( value ) { // rgb color ("255,170,0", for example)
964 string tempValue( value );
965 char* colorValue = strtok( &tempValue[0], "," );
966 while ( colorValue != NULL ) {
967 int c_value = atoi( colorValue );
968 if ( c_value >= 0 && c_value <= 255 )
969 color.push_back( c_value );
970 colorValue = strtok( NULL, "," );
973 if ( color.size() == 3 ) { // color must have three valid component
974 SMESHDS_GroupBase::SetDefaultColor( Quantity_Color( color[0]/255., color[1]/255., color[2]/255., Quantity_TOC_RGB ) );
975 myDefaultGroupColor = value;
976 msgToGUI = "preferences/SMESH/default_grp_color/";
981 // update preferences in case if SetOption() is invoked from python console
982 if ( !msgToGUI.empty() )
984 CORBA::Object_var obj = SMESH_Gen_i::GetNS()->Resolve( "/Kernel/Session" );
985 SALOME::Session_var session = SALOME::Session::_narrow( obj );
986 if ( !CORBA::is_nil( session ) )
987 session->emitMessageOneWay(msgToGUI.c_str());
992 //=============================================================================
994 Return an option value
996 //=============================================================================
998 char* SMESH_Gen_i::GetOption(const char* name)
1002 if ( strcmp(name, "historical_python_dump") == 0 )
1004 return CORBA::string_dup( myIsHistoricalPythonDump ? "true" : "false" );
1006 if ( strcmp(name, "forget_mesh_on_hyp_modif") == 0 )
1008 return CORBA::string_dup( myToForgetMeshDataOnHypModif ? "true" : "false" );
1010 if ( strcmp(name, "default_grp_color") == 0 )
1012 return CORBA::string_dup( myDefaultGroupColor.c_str() );
1015 return CORBA::string_dup( "" );
1018 //=============================================================================
1020 * SMESH_Gen_i::CreateMesh
1022 * Create empty mesh on a shape and publish it in the study
1024 //=============================================================================
1026 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMesh( GEOM::GEOM_Object_ptr theShapeObject )
1027 throw ( SALOME::SALOME_Exception )
1029 Unexpect aCatch(SALOME_SalomeException);
1030 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMesh" );
1032 SMESH::SMESH_Mesh_var mesh = this->createMesh();
1034 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( mesh );
1035 ASSERT( meshServant );
1036 meshServant->SetShape( theShapeObject );
1038 // publish mesh in the study
1039 if ( CanPublishInStudy( mesh ) ) {
1040 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1041 aStudyBuilder->NewCommand(); // There is a transaction
1042 SALOMEDS::SObject_wrap aSO = PublishMesh( myCurrentStudy, mesh.in() );
1043 aStudyBuilder->CommitCommand();
1044 if ( !aSO->_is_nil() ) {
1045 // Update Python script
1046 TPythonDump() << aSO << " = " << this << ".CreateMesh(" << theShapeObject << ")";
1050 return mesh._retn();
1053 //=============================================================================
1055 * SMESH_Gen_i::CreateEmptyMesh
1059 //=============================================================================
1061 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateEmptyMesh()
1062 throw ( SALOME::SALOME_Exception )
1064 Unexpect aCatch(SALOME_SalomeException);
1065 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMesh" );
1067 SMESH::SMESH_Mesh_var mesh = this->createMesh();
1069 // publish mesh in the study
1070 if ( CanPublishInStudy( mesh ) ) {
1071 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1072 aStudyBuilder->NewCommand(); // There is a transaction
1073 SALOMEDS::SObject_wrap aSO = PublishMesh( myCurrentStudy, mesh.in() );
1074 aStudyBuilder->CommitCommand();
1075 if ( !aSO->_is_nil() ) {
1076 // Update Python script
1077 TPythonDump() << aSO << " = " << this << ".CreateEmptyMesh()";
1081 return mesh._retn();
1086 //================================================================================
1088 * \brief Throws an exception in case if the file can't be read
1090 //================================================================================
1092 void checkFileReadable( const char* theFileName ) throw ( SALOME::SALOME_Exception )
1094 SMESH_File f ( theFileName );
1097 if ( !f.error().empty() )
1098 THROW_SALOME_CORBA_EXCEPTION( f.error().c_str(), SALOME::BAD_PARAM);
1100 THROW_SALOME_CORBA_EXCEPTION
1101 (( SMESH_Comment("Can't open for reading the file ") << theFileName ).c_str(),
1102 SALOME::BAD_PARAM );
1107 //=============================================================================
1109 * SMESH_Gen_i::CreateMeshFromUNV
1111 * Create mesh and import data from UNV file
1113 //=============================================================================
1115 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMeshesFromUNV( const char* theFileName )
1116 throw ( SALOME::SALOME_Exception )
1118 Unexpect aCatch(SALOME_SalomeException);
1120 checkFileReadable( theFileName );
1122 SMESH::SMESH_Mesh_var aMesh = createMesh();
1124 // publish mesh in the study
1125 if ( CanPublishInStudy( aMesh ) ) {
1126 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1127 aStudyBuilder->NewCommand(); // There is a transaction
1128 SALOMEDS::SObject_wrap aSO = PublishMesh( myCurrentStudy, aMesh.in(), aFileName.c_str() );
1129 aStudyBuilder->CommitCommand();
1130 if ( !aSO->_is_nil() ) {
1131 // Update Python script
1132 TPythonDump() << aSO << " = " << this << ".CreateMeshesFromUNV(r'" << theFileName << "')";
1136 SMESH_Mesh_i* aServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( aMesh ).in() );
1138 aServant->ImportUNVFile( theFileName );
1140 // Dump creation of groups
1141 SMESH::ListOfGroups_var groups = aServant->GetGroups();
1143 aServant->GetImpl().GetMeshDS()->Modified();
1144 return aMesh._retn();
1147 //=============================================================================
1149 * SMESH_Gen_i::CreateMeshFromMED
1151 * Create mesh and import data from MED file
1153 //=============================================================================
1155 SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromMEDorSAUV( const char* theFileName,
1156 SMESH::DriverMED_ReadStatus& theStatus,
1157 const char* theCommandNameForPython,
1158 const char* theFileNameForPython)
1161 char bname[ _MAX_FNAME ];
1162 _splitpath( theFileNameForPython, NULL, NULL, bname, NULL );
1163 string aFileName = bname;
1165 string aFileName = basename( theFileNameForPython );
1167 // Retrieve mesh names from the file
1168 DriverMED_R_SMESHDS_Mesh myReader;
1169 myReader.SetFile( theFileName );
1170 myReader.SetMeshId( -1 );
1171 Driver_Mesh::Status aStatus;
1172 list<string> aNames = myReader.GetMeshNames(aStatus);
1173 SMESH::mesh_array_var aResult = new SMESH::mesh_array();
1174 theStatus = (SMESH::DriverMED_ReadStatus)aStatus;
1176 { // open a new scope to make aPythonDump die before PythonDump in SMESH_Mesh::GetGroups()
1179 TPythonDump aPythonDump;
1180 aPythonDump << "([";
1182 if (theStatus == SMESH::DRS_OK) {
1183 SALOMEDS::StudyBuilder_var aStudyBuilder;
1184 if ( GetCurrentStudyID() > -1 )
1186 aStudyBuilder = myCurrentStudy->NewBuilder();
1187 aStudyBuilder->NewCommand(); // There is a transaction
1189 aResult->length( aNames.size() );
1192 // Iterate through all meshes and create mesh objects
1193 for ( list<string>::iterator it = aNames.begin(); it != aNames.end(); it++ )
1196 if (i > 0) aPythonDump << ", ";
1199 SMESH::SMESH_Mesh_var mesh = createMesh();
1201 // publish mesh in the study
1202 SALOMEDS::SObject_wrap aSO;
1203 if ( CanPublishInStudy( mesh ) )
1204 // little trick: for MED file theFileName and theFileNameForPython are the same, but they are different for SAUV
1205 // - as names of meshes are stored in MED file, we use them for data publishing
1206 // - as mesh name is not stored in UNV file, we use file name as name of mesh when publishing data
1207 aSO = PublishMesh( myCurrentStudy, mesh.in(), ( theFileName == theFileNameForPython ) ? (*it).c_str() : aFileName.c_str() );
1208 if ( !aSO->_is_nil() ) {
1213 aPythonDump << "mesh_" << i;
1216 // Read mesh data (groups are published automatically by ImportMEDFile())
1217 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
1218 ASSERT( meshServant );
1219 SMESH::DriverMED_ReadStatus status1 =
1220 meshServant->ImportMEDFile( theFileName, (*it).c_str() );
1221 if (status1 > theStatus)
1222 theStatus = status1;
1224 aResult[i++] = SMESH::SMESH_Mesh::_duplicate( mesh );
1225 meshServant->GetImpl().GetMeshDS()->Modified();
1227 if ( !aStudyBuilder->_is_nil() )
1228 aStudyBuilder->CommitCommand();
1231 // Update Python script
1232 aPythonDump << "], status) = " << this << "." << theCommandNameForPython << "(r'" << theFileNameForPython << "')";
1234 // Dump creation of groups
1235 for ( CORBA::ULong i = 0; i < aResult->length(); ++i )
1236 SMESH::ListOfGroups_var groups = aResult[ i ]->GetGroups();
1238 return aResult._retn();
1241 SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromMED( const char* theFileName,
1242 SMESH::DriverMED_ReadStatus& theStatus)
1243 throw ( SALOME::SALOME_Exception )
1245 Unexpect aCatch(SALOME_SalomeException);
1246 checkFileReadable( theFileName );
1248 SMESH::mesh_array* result = CreateMeshesFromMEDorSAUV(theFileName, theStatus, "CreateMeshesFromMED", theFileName);
1252 //=============================================================================
1254 * SMESH_Gen_i::CreateMeshFromSAUV
1256 * Create mesh and import data from SAUV file
1258 //=============================================================================
1260 SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromSAUV( const char* theFileName,
1261 SMESH::DriverMED_ReadStatus& theStatus)
1262 throw ( SALOME::SALOME_Exception )
1264 Unexpect aCatch(SALOME_SalomeException);
1265 checkFileReadable( theFileName );
1267 std::string sauvfilename(theFileName);
1268 std::string medfilename(theFileName);
1269 medfilename += ".med";
1272 cmd = "%PYTHONBIN% ";
1277 cmd += "from medutilities import convert ; convert(r'" + sauvfilename + "', 'GIBI', 'MED', 1, r'" + medfilename + "')";
1279 system(cmd.c_str());
1280 SMESH::mesh_array* result = CreateMeshesFromMEDorSAUV(medfilename.c_str(), theStatus, "CreateMeshesFromSAUV", sauvfilename.c_str());
1282 cmd = "%PYTHONBIN% ";
1287 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1289 system(cmd.c_str());
1293 //=============================================================================
1295 * SMESH_Gen_i::CreateMeshFromSTL
1297 * Create mesh and import data from STL file
1299 //=============================================================================
1301 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMeshesFromSTL( const char* theFileName )
1302 throw ( SALOME::SALOME_Exception )
1304 Unexpect aCatch(SALOME_SalomeException);
1305 checkFileReadable( theFileName );
1307 SMESH::SMESH_Mesh_var aMesh = createMesh();
1310 char bname[ _MAX_FNAME ];
1311 _splitpath( theFileName, NULL, NULL, bname, NULL );
1312 string aFileName = bname;
1314 string aFileName = basename( theFileName );
1316 // publish mesh in the study
1317 if ( CanPublishInStudy( aMesh ) ) {
1318 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1319 aStudyBuilder->NewCommand(); // There is a transaction
1320 SALOMEDS::SObject_wrap aSO = PublishInStudy
1321 ( myCurrentStudy, SALOMEDS::SObject::_nil(), aMesh.in(), aFileName.c_str() );
1322 aStudyBuilder->CommitCommand();
1323 if ( !aSO->_is_nil() ) {
1324 // Update Python script
1325 TPythonDump() << aSO << " = " << this << ".CreateMeshesFromSTL(r'" << theFileName << "')";
1329 SMESH_Mesh_i* aServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( aMesh ).in() );
1331 aServant->ImportSTLFile( theFileName );
1332 aServant->GetImpl().GetMeshDS()->Modified();
1333 return aMesh._retn();
1336 //================================================================================
1338 * \brief Create meshes and import data from the CGSN file
1340 //================================================================================
1342 SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromCGNS( const char* theFileName,
1343 SMESH::DriverMED_ReadStatus& theStatus)
1344 throw ( SALOME::SALOME_Exception )
1346 Unexpect aCatch(SALOME_SalomeException);
1347 checkFileReadable( theFileName );
1349 SMESH::mesh_array_var aResult = new SMESH::mesh_array();
1352 // Retrieve nb meshes from the file
1353 DriverCGNS_Read myReader;
1354 myReader.SetFile( theFileName );
1355 Driver_Mesh::Status aStatus;
1356 int nbMeshes = myReader.GetNbMeshes(aStatus);
1357 theStatus = (SMESH::DriverMED_ReadStatus)aStatus;
1359 aResult->length( nbMeshes );
1361 { // open a new scope to make aPythonDump die before PythonDump in SMESH_Mesh::GetGroups()
1364 TPythonDump aPythonDump;
1365 aPythonDump << "([";
1367 if (theStatus == SMESH::DRS_OK)
1369 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1370 aStudyBuilder->NewCommand(); // There is a transaction
1374 // Iterate through all meshes and create mesh objects
1375 for ( ; i < nbMeshes; ++i )
1378 if (i > 0) aPythonDump << ", ";
1381 SMESH::SMESH_Mesh_var mesh = createMesh();
1382 aResult[i] = SMESH::SMESH_Mesh::_duplicate( mesh );
1384 // Read mesh data (groups are published automatically by ImportMEDFile())
1385 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
1386 ASSERT( meshServant );
1388 SMESH::DriverMED_ReadStatus status1 =
1389 meshServant->ImportCGNSFile( theFileName, i, meshName );
1390 if (status1 > theStatus)
1391 theStatus = status1;
1393 meshServant->GetImpl().GetMeshDS()->Modified();
1394 // publish mesh in the study
1395 SALOMEDS::SObject_wrap aSO;
1396 if ( CanPublishInStudy( mesh ) )
1397 aSO = PublishMesh( myCurrentStudy, mesh.in(), meshName.c_str() );
1400 if ( !aSO->_is_nil() ) {
1404 aPythonDump << "mesh_" << i;
1407 aStudyBuilder->CommitCommand();
1410 aPythonDump << "], status) = " << this << ".CreateMeshesFromCGNS(r'" << theFileName << "')";
1412 // Dump creation of groups
1413 for ( CORBA::ULong i = 0; i < aResult->length(); ++i )
1414 SMESH::ListOfGroups_var groups = aResult[ i ]->GetGroups();
1416 THROW_SALOME_CORBA_EXCEPTION("CGNS library is unavailable", SALOME::INTERNAL_ERROR);
1419 return aResult._retn();
1422 //================================================================================
1424 * \brief Create a mesh and import data from a GMF file
1426 //================================================================================
1428 SMESH::SMESH_Mesh_ptr
1429 SMESH_Gen_i::CreateMeshesFromGMF( const char* theFileName,
1430 CORBA::Boolean theMakeRequiredGroups,
1431 SMESH::ComputeError_out theError)
1432 throw ( SALOME::SALOME_Exception )
1434 Unexpect aCatch(SALOME_SalomeException);
1435 checkFileReadable( theFileName );
1437 SMESH::SMESH_Mesh_var aMesh = createMesh();
1439 char bname[ _MAX_FNAME ];
1440 _splitpath( theFileName, NULL, NULL, bname, NULL );
1441 string aFileName = bname;
1443 string aFileName = basename( theFileName );
1445 // publish mesh in the study
1446 if ( CanPublishInStudy( aMesh ) ) {
1447 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1448 aStudyBuilder->NewCommand(); // There is a transaction
1449 SALOMEDS::SObject_wrap aSO = PublishInStudy
1450 ( myCurrentStudy, SALOMEDS::SObject::_nil(), aMesh.in(), aFileName.c_str() );
1451 aStudyBuilder->CommitCommand();
1452 if ( !aSO->_is_nil() ) {
1453 // Update Python script
1454 TPythonDump() << "("<< aSO << ", error) = " << this << ".CreateMeshesFromGMF(r'"
1455 << theFileName << "', "
1456 << theMakeRequiredGroups << " )";
1459 SMESH_Mesh_i* aServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( aMesh ).in() );
1461 theError = aServant->ImportGMFFile( theFileName, theMakeRequiredGroups );
1462 aServant->GetImpl().GetMeshDS()->Modified();
1463 return aMesh._retn();
1467 //=============================================================================
1469 * SMESH_Gen_i::IsReadyToCompute
1471 * Returns true if mesh contains enough data to be computed
1473 //=============================================================================
1475 CORBA::Boolean SMESH_Gen_i::IsReadyToCompute( SMESH::SMESH_Mesh_ptr theMesh,
1476 GEOM::GEOM_Object_ptr theShapeObject )
1477 throw ( SALOME::SALOME_Exception )
1479 Unexpect aCatch(SALOME_SalomeException);
1480 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::IsReadyToCompute" );
1482 if ( CORBA::is_nil( theShapeObject ) )
1483 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1484 SALOME::BAD_PARAM );
1486 if ( CORBA::is_nil( theMesh ) )
1487 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
1488 SALOME::BAD_PARAM );
1492 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
1493 ASSERT( meshServant );
1494 if ( meshServant ) {
1495 // get local TopoDS_Shape
1496 TopoDS_Shape myLocShape = GeomObjectToShape( theShapeObject );
1497 // call implementation
1498 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1499 return myGen.CheckAlgoState( myLocMesh, myLocShape );
1502 catch ( SALOME_Exception& S_ex ) {
1503 INFOS( "catch exception "<< S_ex.what() );
1508 //================================================================================
1510 * \brief Find SObject for an algo
1512 //================================================================================
1514 SALOMEDS::SObject_ptr SMESH_Gen_i::GetAlgoSO(const ::SMESH_Algo* algo)
1517 if ( !myCurrentStudy->_is_nil() ) {
1518 // find algo in the study
1519 CORBA::String_var compDataType = ComponentDataType();
1520 SALOMEDS::SComponent_wrap father = myCurrentStudy->FindComponent( compDataType.in() );
1521 if ( !father->_is_nil() ) {
1522 SALOMEDS::ChildIterator_wrap itBig = myCurrentStudy->NewChildIterator( father );
1523 for ( ; itBig->More(); itBig->Next() ) {
1524 SALOMEDS::SObject_wrap gotBranch = itBig->Value();
1525 if ( gotBranch->Tag() == GetAlgorithmsRootTag() ) {
1526 SALOMEDS::ChildIterator_wrap algoIt = myCurrentStudy->NewChildIterator( gotBranch );
1527 for ( ; algoIt->More(); algoIt->Next() ) {
1528 SALOMEDS::SObject_wrap algoSO = algoIt->Value();
1529 CORBA::Object_var algoIOR = SObjectToObject( algoSO );
1530 if ( !CORBA::is_nil( algoIOR )) {
1531 SMESH_Hypothesis_i* impl = SMESH::DownCast<SMESH_Hypothesis_i*>( algoIOR );
1532 if ( impl && impl->GetImpl() == algo )
1533 return algoSO._retn();
1535 } // loop on algo SO's
1538 } // SMESH component iterator
1542 return SALOMEDS::SObject::_nil();
1545 //================================================================================
1547 * \brief Return errors of mesh computation
1549 //================================================================================
1551 SMESH::compute_error_array* SMESH_Gen_i::GetComputeErrors( SMESH::SMESH_Mesh_ptr theMesh,
1552 GEOM::GEOM_Object_ptr theSubObject )
1553 throw ( SALOME::SALOME_Exception )
1555 Unexpect aCatch(SALOME_SalomeException);
1556 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetComputeErrors()" );
1558 if ( CORBA::is_nil( theSubObject ) && theMesh->HasShapeToMesh())
1559 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
1561 if ( CORBA::is_nil( theMesh ) )
1562 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1564 SMESH::compute_error_array_var error_array = new SMESH::compute_error_array;
1566 if ( SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh ))
1569 if(theMesh->HasShapeToMesh())
1570 shape = GeomObjectToShape( theSubObject );
1572 shape = SMESH_Mesh::PseudoShape();
1574 ::SMESH_Mesh& mesh = meshServant->GetImpl();
1576 error_array->length( mesh.GetMeshDS()->MaxShapeIndex() );
1579 SMESH_subMesh *sm = mesh.GetSubMesh(shape);
1580 const bool includeSelf = true, complexShapeFirst = true;
1581 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(includeSelf,
1583 while ( smIt->more() )
1586 // if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
1588 SMESH_ComputeErrorPtr error = sm->GetComputeError();
1589 if ( error && !error->IsOK() )
1591 if ( !( error->myAlgo ) &&
1592 !( error->myAlgo = sm->GetAlgo() ))
1594 SMESH::ComputeError & errStruct = error_array[ nbErr++ ];
1595 errStruct.code = -( error->myName < 0 ? error->myName + 1: error->myName ); // -1 -> 0
1596 errStruct.comment = error->myComment.c_str();
1597 errStruct.subShapeID = sm->GetId();
1598 SALOMEDS::SObject_wrap algoSO = GetAlgoSO( error->myAlgo );
1599 if ( !algoSO->_is_nil() ) {
1600 CORBA::String_var algoName = algoSO->GetName();
1601 errStruct.algoName = algoName;
1604 errStruct.algoName = error->myAlgo->GetName();
1606 errStruct.hasBadMesh = !error->myBadElements.empty();
1609 error_array->length( nbErr );
1612 catch ( SALOME_Exception& S_ex ) {
1613 INFOS( "catch exception "<< S_ex.what() );
1616 return error_array._retn();
1619 //================================================================================
1621 * \brief Return mesh elements preventing computation of a subshape
1623 //================================================================================
1625 SMESH::MeshPreviewStruct*
1626 SMESH_Gen_i::GetBadInputElements( SMESH::SMESH_Mesh_ptr theMesh,
1627 CORBA::Short theSubShapeID )
1628 throw ( SALOME::SALOME_Exception )
1630 Unexpect aCatch(SALOME_SalomeException);
1631 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetBadInputElements()" );
1633 if ( CORBA::is_nil( theMesh ) )
1634 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1636 SMESH::MeshPreviewStruct_var result = new SMESH::MeshPreviewStruct;
1639 if ( SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh ))
1641 // mesh implementation
1642 ::SMESH_Mesh& mesh = meshServant->GetImpl();
1643 // submesh by subshape id
1644 if ( SMESH_subMesh * sm = mesh.GetSubMeshContaining( theSubShapeID ))
1647 SMESH_ComputeErrorPtr error = sm->GetComputeError();
1648 if ( error && !error->myBadElements.empty())
1650 typedef map<const SMDS_MeshElement*, int > TNode2LocalIDMap;
1651 typedef TNode2LocalIDMap::iterator TNodeLocalID;
1653 // get nodes of elements and count elements
1654 TNode2LocalIDMap mapNode2LocalID;
1655 list< TNodeLocalID > connectivity;
1656 int i, nbElements = 0, nbConnNodes = 0;
1658 list<const SMDS_MeshElement*>::iterator elemIt = error->myBadElements.begin();
1659 list<const SMDS_MeshElement*>::iterator elemEnd = error->myBadElements.end();
1660 for ( ; elemIt != elemEnd; ++elemIt, ++nbElements )
1662 SMDS_ElemIteratorPtr nIt = (*elemIt)->nodesIterator();
1663 while ( nIt->more() )
1664 connectivity.push_back
1665 ( mapNode2LocalID.insert( make_pair( nIt->next(), ++nbConnNodes)).first );
1667 // fill node coords and assign local ids to the nodes
1668 int nbNodes = mapNode2LocalID.size();
1669 result->nodesXYZ.length( nbNodes );
1670 TNodeLocalID node2ID = mapNode2LocalID.begin();
1671 for ( i = 0; i < nbNodes; ++i, ++node2ID ) {
1672 node2ID->second = i;
1673 const SMDS_MeshNode* node = (const SMDS_MeshNode*) node2ID->first;
1674 result->nodesXYZ[i].x = node->X();
1675 result->nodesXYZ[i].y = node->Y();
1676 result->nodesXYZ[i].z = node->Z();
1678 // fill connectivity
1679 result->elementConnectivities.length( nbConnNodes );
1680 list< TNodeLocalID >::iterator connIt = connectivity.begin();
1681 for ( i = 0; i < nbConnNodes; ++i, ++connIt ) {
1682 result->elementConnectivities[i] = (*connIt)->second;
1684 // fill element types
1685 result->elementTypes.length( nbElements );
1686 for ( i = 0, elemIt = error->myBadElements.begin(); i <nbElements; ++i, ++elemIt )
1688 const SMDS_MeshElement* elem = *elemIt;
1689 result->elementTypes[i].SMDS_ElementType = (SMESH::ElementType) elem->GetType();
1690 result->elementTypes[i].isPoly = elem->IsPoly();
1691 result->elementTypes[i].nbNodesInElement = elem->NbNodes();
1697 catch ( SALOME_Exception& S_ex ) {
1698 INFOS( "catch exception "<< S_ex.what() );
1701 return result._retn();
1704 //================================================================================
1706 * \brief Create a group of elements preventing computation of a sub-shape
1708 //================================================================================
1710 SMESH::ListOfGroups*
1711 SMESH_Gen_i::MakeGroupsOfBadInputElements( SMESH::SMESH_Mesh_ptr theMesh,
1712 CORBA::Short theSubShapeID,
1713 const char* theGroupName )
1714 throw ( SALOME::SALOME_Exception )
1716 Unexpect aCatch(SALOME_SalomeException);
1718 SMESH::ListOfGroups_var groups;
1720 if ( CORBA::is_nil( theMesh ) )
1721 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1724 if ( SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh ))
1726 groups = meshServant->MakeGroupsOfBadInputElements( theSubShapeID, theGroupName );
1727 TPythonDump() << groups << " = " << this
1728 << ".MakeGroupsOfBadInputElements( "
1729 << theMesh << ", " << theSubShapeID << ", '" << theGroupName << "' )";
1732 catch ( SALOME_Exception& S_ex ) {
1733 INFOS( "catch exception "<< S_ex.what() );
1735 return groups._retn();
1738 //================================================================================
1740 * \brief Returns errors of hypotheses definintion
1741 * \param theMesh - the mesh
1742 * \param theSubObject - the main or sub- shape
1743 * \retval SMESH::algo_error_array* - sequence of errors
1745 //================================================================================
1747 SMESH::algo_error_array* SMESH_Gen_i::GetAlgoState( SMESH::SMESH_Mesh_ptr theMesh,
1748 GEOM::GEOM_Object_ptr theSubObject )
1749 throw ( SALOME::SALOME_Exception )
1751 Unexpect aCatch(SALOME_SalomeException);
1752 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetAlgoState()" );
1754 if ( CORBA::is_nil( theSubObject ) && theMesh->HasShapeToMesh())
1755 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
1757 if ( CORBA::is_nil( theMesh ) )
1758 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1760 SMESH::algo_error_array_var error_array = new SMESH::algo_error_array;
1762 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
1763 ASSERT( meshServant );
1764 if ( meshServant ) {
1765 TopoDS_Shape myLocShape;
1766 if(theMesh->HasShapeToMesh())
1767 myLocShape = GeomObjectToShape( theSubObject );
1769 myLocShape = SMESH_Mesh::PseudoShape();
1771 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1772 list< ::SMESH_Gen::TAlgoStateError > error_list;
1773 list< ::SMESH_Gen::TAlgoStateError >::iterator error;
1774 // call ::SMESH_Gen::GetAlgoState()
1775 myGen.GetAlgoState( myLocMesh, myLocShape, error_list );
1776 error_array->length( error_list.size() );
1778 for ( error = error_list.begin(); error != error_list.end(); ++error )
1780 // fill AlgoStateError structure
1781 SMESH::AlgoStateError & errStruct = error_array[ i++ ];
1782 errStruct.state = SMESH_Mesh_i::ConvertHypothesisStatus( error->_name );
1783 errStruct.algoDim = error->_algoDim;
1784 errStruct.isGlobalAlgo = error->_isGlobalAlgo;
1785 errStruct.algoName = "";
1786 SALOMEDS::SObject_wrap algoSO = GetAlgoSO( error->_algo );
1787 if ( !algoSO->_is_nil() ) {
1788 CORBA::String_var algoName = algoSO->GetName();
1789 errStruct.algoName = algoName.in();
1794 catch ( SALOME_Exception& S_ex ) {
1795 INFOS( "catch exception "<< S_ex.what() );
1797 return error_array._retn();
1800 //=============================================================================
1802 * SMESH_Gen_i::GetSubShapesId
1804 * Get sub-shapes unique ID's list
1806 //=============================================================================
1809 SMESH_Gen_i::GetSubShapesId( GEOM::GEOM_Object_ptr theMainShapeObject,
1810 const SMESH::object_array& theListOfSubShapeObject )
1811 throw ( SALOME::SALOME_Exception )
1813 Unexpect aCatch(SALOME_SalomeException);
1814 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetSubShapesId" );
1816 SMESH::long_array_var shapesId = new SMESH::long_array;
1819 if ( CORBA::is_nil( theMainShapeObject ) )
1820 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
1824 TopoDS_Shape myMainShape = GeomObjectToShape(theMainShapeObject);
1825 TopTools_IndexedMapOfShape myIndexToShape;
1826 TopExp::MapShapes(myMainShape,myIndexToShape);
1828 for ( CORBA::ULong i = 0; i < theListOfSubShapeObject.length(); i++ )
1830 GEOM::GEOM_Object_var aShapeObject
1831 = GEOM::GEOM_Object::_narrow(theListOfSubShapeObject[i]);
1832 if ( CORBA::is_nil( aShapeObject ) )
1833 THROW_SALOME_CORBA_EXCEPTION ("bad shape object reference", \
1834 SALOME::BAD_PARAM );
1836 TopoDS_Shape locShape = GeomObjectToShape(aShapeObject);
1837 for (TopExp_Explorer exp(locShape,TopAbs_FACE); exp.More(); exp.Next())
1839 const TopoDS_Face& F = TopoDS::Face(exp.Current());
1840 setId.insert(myIndexToShape.FindIndex(F));
1841 if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(F));
1843 for (TopExp_Explorer exp(locShape,TopAbs_EDGE); exp.More(); exp.Next())
1845 const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
1846 setId.insert(myIndexToShape.FindIndex(E));
1847 if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(E));
1849 for (TopExp_Explorer exp(locShape,TopAbs_VERTEX); exp.More(); exp.Next())
1851 const TopoDS_Vertex& V = TopoDS::Vertex(exp.Current());
1852 setId.insert(myIndexToShape.FindIndex(V));
1853 if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(V));
1856 shapesId->length(setId.size());
1857 set<int>::iterator iind;
1859 for (iind = setId.begin(); iind != setId.end(); iind++)
1861 if(MYDEBUG) SCRUTE((*iind));
1862 shapesId[i] = (*iind);
1863 if(MYDEBUG) SCRUTE(shapesId[i]);
1867 catch (SALOME_Exception& S_ex)
1869 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
1872 return shapesId._retn();
1875 //=============================================================================
1877 * SMESH_Gen_i::Compute
1879 * Compute mesh on a shape
1881 //=============================================================================
1883 CORBA::Boolean SMESH_Gen_i::Compute( SMESH::SMESH_Mesh_ptr theMesh,
1884 GEOM::GEOM_Object_ptr theShapeObject )
1885 throw ( SALOME::SALOME_Exception )
1888 Unexpect aCatch(SALOME_SalomeException);
1889 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Compute" );
1891 if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
1892 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1893 SALOME::BAD_PARAM );
1895 if ( CORBA::is_nil( theMesh ) )
1896 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
1897 SALOME::BAD_PARAM );
1899 // Update Python script
1900 TPythonDump() << "isDone = " << this << ".Compute( "
1901 << theMesh << ", " << theShapeObject << ")";
1905 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
1906 ASSERT( meshServant );
1907 if ( meshServant ) {
1908 meshServant->Load();
1909 // NPAL16168: "geometrical group edition from a submesh don't modifiy mesh computation"
1910 meshServant->CheckGeomModif();
1911 // get local TopoDS_Shape
1912 TopoDS_Shape myLocShape;
1913 if(theMesh->HasShapeToMesh())
1914 myLocShape = GeomObjectToShape( theShapeObject );
1916 myLocShape = SMESH_Mesh::PseudoShape();
1917 // call implementation compute
1918 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1919 myGen.PrepareCompute( myLocMesh, myLocShape );
1920 bool ok = myGen.Compute( myLocMesh, myLocShape, myLocShape != myLocMesh.GetShapeToMesh());
1921 meshServant->CreateGroupServants(); // algos can create groups (issue 0020918)
1922 myLocMesh.GetMeshDS()->Modified();
1926 catch ( std::bad_alloc ) {
1927 INFOS( "Compute(): lack of memory" );
1929 catch ( SALOME_Exception& S_ex ) {
1930 INFOS( "Compute(): catch exception "<< S_ex.what() );
1933 INFOS( "Compute(): unknown exception " );
1938 //=============================================================================
1940 * SMESH_Gen_i::CancelCompute
1942 * Cancel Compute mesh on a shape
1944 //=============================================================================
1946 void SMESH_Gen_i::CancelCompute( SMESH::SMESH_Mesh_ptr theMesh,
1947 GEOM::GEOM_Object_ptr theShapeObject )
1949 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
1950 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1951 TopoDS_Shape myLocShape;
1952 if(theMesh->HasShapeToMesh())
1953 myLocShape = GeomObjectToShape( theShapeObject );
1955 myLocShape = SMESH_Mesh::PseudoShape();
1956 myGen.CancelCompute( myLocMesh, myLocShape);
1959 //=============================================================================
1961 * SMESH_Gen_i::Precompute
1963 * Compute mesh as preview till indicated dimension on shape
1965 //=============================================================================
1967 SMESH::MeshPreviewStruct* SMESH_Gen_i::Precompute( SMESH::SMESH_Mesh_ptr theMesh,
1968 GEOM::GEOM_Object_ptr theShapeObject,
1969 SMESH::Dimension theDimension,
1970 SMESH::long_array& theShapesId)
1971 throw ( SALOME::SALOME_Exception )
1973 Unexpect aCatch(SALOME_SalomeException);
1974 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Precompute" );
1976 if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
1977 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1978 SALOME::BAD_PARAM );
1980 if ( CORBA::is_nil( theMesh ) )
1981 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
1982 SALOME::BAD_PARAM );
1984 SMESH::MeshPreviewStruct_var result = new SMESH::MeshPreviewStruct;
1987 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
1988 meshServant->Load();
1989 ASSERT( meshServant );
1990 if ( meshServant ) {
1991 // NPAL16168: "geometrical group edition from a submesh don't modifiy mesh computation"
1992 meshServant->CheckGeomModif();
1993 // get local TopoDS_Shape
1994 TopoDS_Shape myLocShape;
1995 if(theMesh->HasShapeToMesh())
1996 myLocShape = GeomObjectToShape( theShapeObject );
1998 return result._retn();;
2000 // call implementation compute
2001 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
2003 ::MeshDimension aDim = (MeshDimension)theDimension;
2004 if ( myGen.Compute( myLocMesh, myLocShape, false, false, aDim, &shapeIds ) )
2006 int nbShapeId = shapeIds.size();
2007 theShapesId.length( nbShapeId );
2008 // iterates on shapes and collect mesh entities into mesh preview
2009 TSetOfInt::const_iterator idIt = shapeIds.begin();
2010 TSetOfInt::const_iterator idEnd = shapeIds.end();
2011 std::map< int, int > mapOfShIdNb;
2012 std::set< SMESH_TLink > setOfEdge;
2013 std::list< SMDSAbs_ElementType > listOfElemType;
2014 typedef map<const SMDS_MeshElement*, int > TNode2LocalIDMap;
2015 typedef TNode2LocalIDMap::iterator TNodeLocalID;
2016 TNode2LocalIDMap mapNode2LocalID;
2017 list< TNodeLocalID > connectivity;
2018 int i, nbConnNodes = 0;
2019 std::set< const SMESH_subMesh* > setOfVSubMesh;
2020 // iterates on shapes
2021 for ( ; idIt != idEnd; idIt++ )
2023 if ( mapOfShIdNb.find( *idIt ) != mapOfShIdNb.end() )
2025 SMESH_subMesh* sm = myLocMesh.GetSubMeshContaining(*idIt);
2026 if ( !sm || !sm->IsMeshComputed() )
2029 const TopoDS_Shape& aSh = sm->GetSubShape();
2030 const int shDim = myGen.GetShapeDim( aSh );
2031 if ( shDim < 1 || shDim > theDimension )
2034 mapOfShIdNb[ *idIt ] = 0;
2035 theShapesId[ mapOfShIdNb.size() - 1 ] = *idIt;
2037 SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
2038 if ( !smDS ) continue;
2040 if ( theDimension == SMESH::DIM_2D )
2042 SMDS_ElemIteratorPtr faceIt = smDS->GetElements();
2043 while ( faceIt->more() )
2045 const SMDS_MeshElement* face = faceIt->next();
2046 int aNbNode = face->NbNodes();
2048 aNbNode /= 2; // do not take into account additional middle nodes
2050 SMDS_MeshNode* node1 = (SMDS_MeshNode*)face->GetNode( 0 );
2051 for ( int nIndx = 0; nIndx < aNbNode; nIndx++ )
2053 SMDS_MeshNode* node2 = (SMDS_MeshNode*)face->GetNode( nIndx+1 < aNbNode ? nIndx+1 : 0 );
2054 if ( setOfEdge.insert( SMESH_TLink ( node1, node2 ) ).second )
2056 listOfElemType.push_back( SMDSAbs_Edge );
2057 connectivity.push_back
2058 ( mapNode2LocalID.insert( make_pair( node1, ++nbConnNodes)).first );
2059 connectivity.push_back
2060 ( mapNode2LocalID.insert( make_pair( node2, ++nbConnNodes)).first );
2066 else if ( theDimension == SMESH::DIM_1D )
2068 SMDS_NodeIteratorPtr nodeIt = smDS->GetNodes();
2069 while ( nodeIt->more() )
2071 listOfElemType.push_back( SMDSAbs_Node );
2072 connectivity.push_back
2073 ( mapNode2LocalID.insert( make_pair( nodeIt->next(), ++nbConnNodes)).first );
2075 // add corner nodes by first vertex from edge
2076 SMESH_subMeshIteratorPtr edgeSmIt =
2077 sm->getDependsOnIterator(/*includeSelf*/false,
2078 /*complexShapeFirst*/false);
2079 while ( edgeSmIt->more() )
2081 SMESH_subMesh* vertexSM = edgeSmIt->next();
2082 // check that vertex is not already treated
2083 if ( !setOfVSubMesh.insert( vertexSM ).second )
2085 if ( vertexSM->GetSubShape().ShapeType() != TopAbs_VERTEX )
2088 const SMESHDS_SubMesh* vertexSmDS = vertexSM->GetSubMeshDS();
2089 SMDS_NodeIteratorPtr nodeIt = vertexSmDS->GetNodes();
2090 while ( nodeIt->more() )
2092 listOfElemType.push_back( SMDSAbs_Node );
2093 connectivity.push_back
2094 ( mapNode2LocalID.insert( make_pair( nodeIt->next(), ++nbConnNodes)).first );
2100 // fill node coords and assign local ids to the nodes
2101 int nbNodes = mapNode2LocalID.size();
2102 result->nodesXYZ.length( nbNodes );
2103 TNodeLocalID node2ID = mapNode2LocalID.begin();
2104 for ( i = 0; i < nbNodes; ++i, ++node2ID ) {
2105 node2ID->second = i;
2106 const SMDS_MeshNode* node = (const SMDS_MeshNode*) node2ID->first;
2107 result->nodesXYZ[i].x = node->X();
2108 result->nodesXYZ[i].y = node->Y();
2109 result->nodesXYZ[i].z = node->Z();
2111 // fill connectivity
2112 result->elementConnectivities.length( nbConnNodes );
2113 list< TNodeLocalID >::iterator connIt = connectivity.begin();
2114 for ( i = 0; i < nbConnNodes; ++i, ++connIt ) {
2115 result->elementConnectivities[i] = (*connIt)->second;
2118 // fill element types
2119 result->elementTypes.length( listOfElemType.size() );
2120 std::list< SMDSAbs_ElementType >::const_iterator typeIt = listOfElemType.begin();
2121 std::list< SMDSAbs_ElementType >::const_iterator typeEnd = listOfElemType.end();
2122 for ( i = 0; typeIt != typeEnd; ++i, ++typeIt )
2124 SMDSAbs_ElementType elemType = *typeIt;
2125 result->elementTypes[i].SMDS_ElementType = (SMESH::ElementType)elemType;
2126 result->elementTypes[i].isPoly = false;
2127 result->elementTypes[i].nbNodesInElement = elemType == SMDSAbs_Edge ? 2 : 1;
2130 // correct number of shapes
2131 theShapesId.length( mapOfShIdNb.size() );
2135 catch ( std::bad_alloc ) {
2136 INFOS( "Precompute(): lack of memory" );
2138 catch ( SALOME_Exception& S_ex ) {
2139 INFOS( "Precompute(): catch exception "<< S_ex.what() );
2142 INFOS( "Precompute(): unknown exception " );
2144 return result._retn();
2148 //=============================================================================
2150 * SMESH_Gen_i::Evaluate
2152 * Evaluate mesh on a shape
2154 //=============================================================================
2156 SMESH::long_array* SMESH_Gen_i::Evaluate(SMESH::SMESH_Mesh_ptr theMesh,
2157 GEOM::GEOM_Object_ptr theShapeObject)
2158 // SMESH::long_array& theNbElems)
2159 throw ( SALOME::SALOME_Exception )
2161 Unexpect aCatch(SALOME_SalomeException);
2162 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Evaluate" );
2164 if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
2165 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
2166 SALOME::BAD_PARAM );
2168 if ( CORBA::is_nil( theMesh ) )
2169 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
2170 SALOME::BAD_PARAM );
2172 SMESH::long_array_var nbels = new SMESH::long_array;
2173 nbels->length(SMESH::Entity_Last);
2174 int i = SMESH::Entity_Node;
2175 for (; i < SMESH::Entity_Last; i++)
2178 // Update Python script
2179 TPythonDump() << "theNbElems = " << this << ".Evaluate( "
2180 << theMesh << ", " << theShapeObject << ")";
2184 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
2185 ASSERT( meshServant );
2186 if ( meshServant ) {
2187 meshServant->Load();
2188 // NPAL16168: "geometrical group edition from a submesh don't modifiy mesh computation"
2189 meshServant->CheckGeomModif();
2190 // get local TopoDS_Shape
2191 TopoDS_Shape myLocShape;
2192 if(theMesh->HasShapeToMesh())
2193 myLocShape = GeomObjectToShape( theShapeObject );
2195 myLocShape = SMESH_Mesh::PseudoShape();
2196 // call implementation compute
2197 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
2198 MapShapeNbElems aResMap;
2199 /*CORBA::Boolean ret =*/ myGen.Evaluate( myLocMesh, myLocShape, aResMap);
2200 MapShapeNbElemsItr anIt = aResMap.begin();
2201 for(; anIt!=aResMap.end(); anIt++) {
2202 const vector<int>& aVec = (*anIt).second;
2203 for ( i = SMESH::Entity_Node; i < (int)aVec.size(); i++ ) {
2204 int nbElem = aVec[i];
2205 if ( nbElem < 0 ) // algo failed, check that it has reported a message
2207 SMESH_subMesh* sm = anIt->first;
2208 SMESH_ComputeErrorPtr& error = sm->GetComputeError();
2209 const SMESH_Algo* algo = sm->GetAlgo();
2210 if ( (algo && !error.get()) || error->IsOK() )
2211 error.reset( new SMESH_ComputeError( COMPERR_ALGO_FAILED,"Failed to evaluate",algo));
2215 nbels[i] += aVec[i];
2219 return nbels._retn();
2222 catch ( std::bad_alloc ) {
2223 INFOS( "Evaluate(): lack of memory" );
2225 catch ( SALOME_Exception& S_ex ) {
2226 INFOS( "Evaluate(): catch exception "<< S_ex.what() );
2229 INFOS( "Evaluate(): unknown exception " );
2232 return nbels._retn();
2235 //================================================================================
2237 * \brief Return geometrical object the given element is built on
2238 * \param theMesh - the mesh the element is in
2239 * \param theElementID - the element ID
2240 * \param theGeomName - the name of the result geom object if it is not yet published
2241 * \retval GEOM::GEOM_Object_ptr - the found or just published geom object (no need to UnRegister())
2243 //================================================================================
2245 GEOM::GEOM_Object_ptr
2246 SMESH_Gen_i::GetGeometryByMeshElement( SMESH::SMESH_Mesh_ptr theMesh,
2247 CORBA::Long theElementID,
2248 const char* theGeomName)
2249 throw ( SALOME::SALOME_Exception )
2251 Unexpect aCatch(SALOME_SalomeException);
2253 GEOM::GEOM_Object_wrap geom = FindGeometryByMeshElement(theMesh, theElementID);
2254 if ( !geom->_is_nil() ) {
2255 GEOM::GEOM_Object_var mainShape = theMesh->GetShapeToMesh();
2256 GEOM::GEOM_Gen_ptr geomGen = GetGeomEngine();
2258 // try to find the corresponding SObject
2259 SALOMEDS::SObject_wrap SObj = ObjectToSObject( myCurrentStudy, geom.in() );
2260 if ( SObj->_is_nil() ) // submesh can be not found even if published
2262 // try to find published submesh
2263 GEOM::ListOfLong_var list = geom->GetSubShapeIndices();
2264 if ( !geom->IsMainShape() && list->length() == 1 ) {
2265 SALOMEDS::SObject_wrap mainSO = ObjectToSObject( myCurrentStudy, mainShape );
2266 SALOMEDS::ChildIterator_wrap it;
2267 if ( !mainSO->_is_nil() ) {
2268 it = myCurrentStudy->NewChildIterator( mainSO );
2270 if ( !it->_is_nil() ) {
2271 for ( it->InitEx(true); it->More(); it->Next() ) {
2272 SALOMEDS::SObject_wrap so = it->Value();
2273 CORBA::Object_var obj = SObjectToObject( so );
2274 GEOM::GEOM_Object_var subGeom = GEOM::GEOM_Object::_narrow( obj );
2275 if ( !subGeom->_is_nil() ) {
2276 GEOM::ListOfLong_var subList = subGeom->GetSubShapeIndices();
2277 if ( subList->length() == 1 && list[0] == subList[0] ) {
2287 if ( SObj->_is_nil() ) // publish a new subshape
2288 SObj = geomGen->AddInStudy( myCurrentStudy, geom, theGeomName, mainShape );
2290 // return only published geometry
2291 if ( !SObj->_is_nil() ) {
2292 //return geom._retn(); -- servant of geom must be UnRegister()ed;
2293 CORBA::Object_var obj = SObjectToObject( SObj );
2294 GEOM::GEOM_Object_var go = GEOM::GEOM_Object::_narrow( obj );
2298 return GEOM::GEOM_Object::_nil();
2301 //================================================================================
2303 * \brief Return geometrical object the given element is built on.
2304 * \param theMesh - the mesh the element is in
2305 * \param theElementID - the element ID
2306 * \retval GEOM::GEOM_Object_ptr - the found or created (UnRegister()!) geom object
2308 //================================================================================
2310 GEOM::GEOM_Object_ptr
2311 SMESH_Gen_i::FindGeometryByMeshElement( SMESH::SMESH_Mesh_ptr theMesh,
2312 CORBA::Long theElementID)
2313 throw ( SALOME::SALOME_Exception )
2315 Unexpect aCatch(SALOME_SalomeException);
2316 if ( CORBA::is_nil( theMesh ) )
2317 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference", SALOME::BAD_PARAM );
2319 GEOM::GEOM_Object_var mainShape = theMesh->GetShapeToMesh();
2320 GEOM::GEOM_Gen_ptr geomGen = GetGeomEngine();
2322 // get a core mesh DS
2323 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
2324 if ( meshServant && !geomGen->_is_nil() && !mainShape->_is_nil() )
2326 ::SMESH_Mesh & mesh = meshServant->GetImpl();
2327 SMESHDS_Mesh* meshDS = mesh.GetMeshDS();
2328 // find the element in mesh
2329 if ( const SMDS_MeshElement * elem = meshDS->FindElement( theElementID ) ) {
2330 // find a shape id by the element
2331 if ( int shapeID = ::SMESH_MeshEditor( &mesh ).FindShape( elem )) {
2332 // get a geom object by the shape id
2333 GEOM::GEOM_Object_var geom = ShapeToGeomObject( meshDS->IndexToShape( shapeID ));
2334 if ( geom->_is_nil() ) {
2335 // try to find a published sub-shape
2336 SALOMEDS::SObject_wrap mainSO = ObjectToSObject( myCurrentStudy, mainShape );
2337 SALOMEDS::ChildIterator_wrap it;
2338 if ( !mainSO->_is_nil() ) {
2339 it = myCurrentStudy->NewChildIterator( mainSO );
2341 if ( !it->_is_nil() ) {
2342 for ( it->InitEx(true); it->More(); it->Next() ) {
2343 SALOMEDS::SObject_wrap so = it->Value();
2344 CORBA::Object_var obj = SObjectToObject( so );
2345 GEOM::GEOM_Object_var subGeom = GEOM::GEOM_Object::_narrow( obj );
2346 if ( !subGeom->_is_nil() ) {
2347 GEOM::ListOfLong_var subList = subGeom->GetSubShapeIndices();
2348 if ( subList->length() == 1 && shapeID == subList[0] ) {
2356 if ( geom->_is_nil() ) {
2358 GEOM::GEOM_IShapesOperations_wrap op =
2359 geomGen->GetIShapesOperations( GetCurrentStudyID() );
2360 if ( !op->_is_nil() )
2361 geom = op->GetSubShape( mainShape, shapeID );
2366 if ( !geom->_is_nil() ) {
2367 GeomObjectToShape( geom ); // let geom client remember the found shape
2368 return geom._retn();
2373 return GEOM::GEOM_Object::_nil();
2376 //================================================================================
2378 * SMESH_Gen_i::Concatenate
2380 * Concatenate the given meshes into one mesh
2382 //================================================================================
2384 SMESH::SMESH_Mesh_ptr
2385 SMESH_Gen_i::Concatenate(const SMESH::ListOfIDSources& theMeshesArray,
2386 CORBA::Boolean theUniteIdenticalGroups,
2387 CORBA::Boolean theMergeNodesAndElements,
2388 CORBA::Double theMergeTolerance)
2389 throw ( SALOME::SALOME_Exception )
2391 return ConcatenateCommon(theMeshesArray,
2392 theUniteIdenticalGroups,
2393 theMergeNodesAndElements,
2398 //================================================================================
2400 * SMESH_Gen_i::ConcatenateWithGroups
2402 * Concatenate the given meshes into one mesh
2403 * Create the groups of all elements from initial meshes
2405 //================================================================================
2407 SMESH::SMESH_Mesh_ptr
2408 SMESH_Gen_i::ConcatenateWithGroups(const SMESH::ListOfIDSources& theMeshesArray,
2409 CORBA::Boolean theUniteIdenticalGroups,
2410 CORBA::Boolean theMergeNodesAndElements,
2411 CORBA::Double theMergeTolerance)
2412 throw ( SALOME::SALOME_Exception )
2414 return ConcatenateCommon(theMeshesArray,
2415 theUniteIdenticalGroups,
2416 theMergeNodesAndElements,
2421 //================================================================================
2423 * SMESH_Gen_i::ConcatenateCommon
2425 * Concatenate the given meshes into one mesh
2427 //================================================================================
2429 SMESH::SMESH_Mesh_ptr
2430 SMESH_Gen_i::ConcatenateCommon(const SMESH::ListOfIDSources& theMeshesArray,
2431 CORBA::Boolean theUniteIdenticalGroups,
2432 CORBA::Boolean theMergeNodesAndElements,
2433 CORBA::Double theMergeTolerance,
2434 CORBA::Boolean theCommonGroups)
2435 throw ( SALOME::SALOME_Exception )
2437 typedef list<SMESH::SMESH_Group_var> TListOfNewGroups;
2438 typedef map< pair<string, SMESH::ElementType>, TListOfNewGroups > TGroupsMap;
2440 TPythonDump* pPythonDump = new TPythonDump;
2441 TPythonDump& aPythonDump = *pPythonDump; // prevent dump of called methods
2444 SMESH::SMESH_Mesh_var aNewMesh = CreateEmptyMesh();
2446 if ( aNewMesh->_is_nil() )
2447 return aNewMesh._retn();
2449 SMESH_Mesh_i* aNewImpl = SMESH::DownCast<SMESH_Mesh_i*>( aNewMesh );
2451 return aNewMesh._retn();
2453 ::SMESH_Mesh& aLocMesh = aNewImpl->GetImpl();
2454 SMESHDS_Mesh* aNewMeshDS = aLocMesh.GetMeshDS();
2456 TGroupsMap aGroupsMap;
2457 TListOfNewGroups aListOfNewGroups;
2458 ::SMESH_MeshEditor aNewEditor(&aLocMesh);
2459 SMESH::ListOfGroups_var aListOfGroups;
2461 ::SMESH_MeshEditor::ElemFeatures elemType;
2462 std::vector<const SMDS_MeshNode*> aNodesArray;
2464 // loop on sub-meshes
2465 for ( CORBA::ULong i = 0; i < theMeshesArray.length(); i++)
2467 if ( CORBA::is_nil( theMeshesArray[i] )) continue;
2468 SMESH::SMESH_Mesh_var anInitMesh = theMeshesArray[i]->GetMesh();
2469 if ( anInitMesh->_is_nil() ) continue;
2470 SMESH_Mesh_i* anInitImpl = SMESH::DownCast<SMESH_Mesh_i*>( anInitMesh );
2471 if ( !anInitImpl ) continue;
2474 //::SMESH_Mesh& aInitLocMesh = anInitImpl->GetImpl();
2475 //SMESHDS_Mesh* anInitMeshDS = aInitLocMesh.GetMeshDS();
2477 // remember nb of elements before filling in
2478 SMESH::long_array_var prevState = aNewMesh->GetNbElementsByType();
2480 typedef std::map<const SMDS_MeshElement*, const SMDS_MeshElement*, TIDCompare > TEEMap;
2481 TEEMap elemsMap, nodesMap;
2483 // loop on elements of a sub-mesh
2484 SMDS_ElemIteratorPtr itElems = anInitImpl->GetElements( theMeshesArray[i], SMESH::ALL );
2485 const SMDS_MeshElement* anElem;
2486 const SMDS_MeshElement* aNewElem;
2487 const SMDS_MeshNode* aNode;
2488 const SMDS_MeshNode* aNewNode;
2491 while ( itElems->more() )
2493 anElem = itElems->next();
2494 anElemNbNodes = anElem->NbNodes();
2495 aNodesArray.resize( anElemNbNodes );
2497 // loop on nodes of an element
2498 SMDS_ElemIteratorPtr itNodes = anElem->nodesIterator();
2499 for ( int k = 0; itNodes->more(); k++)
2501 aNode = static_cast<const SMDS_MeshNode*>( itNodes->next() );
2502 TEEMap::iterator n2nnIt = nodesMap.find( aNode );
2503 if ( n2nnIt == nodesMap.end() )
2505 aNewNode = aNewMeshDS->AddNode(aNode->X(), aNode->Y(), aNode->Z());
2506 nodesMap.insert( make_pair( aNode, aNewNode ));
2510 aNewNode = static_cast<const SMDS_MeshNode*>( n2nnIt->second );
2512 aNodesArray[k] = aNewNode;
2515 // creates a corresponding element on existent nodes in new mesh
2516 if ( anElem->GetType() == SMDSAbs_Node )
2520 aNewEditor.AddElement( aNodesArray, elemType.Init( anElem, /*basicOnly=*/false ));
2523 elemsMap.insert( make_pair( anElem, aNewElem ));
2527 aNewEditor.ClearLastCreated(); // forget the history
2530 // create groups of just added elements
2531 SMESH::SMESH_Group_var aNewGroup;
2532 SMESH::ElementType aGroupType;
2533 if ( theCommonGroups )
2535 SMESH::long_array_var curState = aNewMesh->GetNbElementsByType();
2537 for( aGroupType = SMESH::NODE;
2538 aGroupType < SMESH::NB_ELEMENT_TYPES;
2539 aGroupType = (SMESH::ElementType)( aGroupType + 1 ))
2541 if ( curState[ aGroupType ] <= prevState[ aGroupType ])
2544 // make a group name
2545 const char* typeNames[] = { "All","Nodes","Edges","Faces","Volumes","0DElems","Balls" };
2546 { // check of typeNames: compilation failure mains that NB_ELEMENT_TYPES changed:
2547 const int nbNames = sizeof(typeNames) / sizeof(const char*);
2548 int _assert[( nbNames == SMESH::NB_ELEMENT_TYPES ) ? 2 : -1 ]; _assert[0]=_assert[1];
2550 string groupName = "Gr";
2551 SALOMEDS::SObject_wrap aMeshSObj = ObjectToSObject( myCurrentStudy, theMeshesArray[i] );
2553 CORBA::String_var name = aMeshSObj->GetName();
2557 groupName += typeNames[ aGroupType ];
2559 // make and fill a group
2560 TEEMap & e2neMap = ( aGroupType == SMESH::NODE ) ? nodesMap : elemsMap;
2561 aNewGroup = aNewImpl->CreateGroup( aGroupType, groupName.c_str() );
2562 if ( SMESH_Group_i* grp_i = SMESH::DownCast<SMESH_Group_i*>( aNewGroup ))
2564 if ( SMESHDS_Group* grpDS = dynamic_cast<SMESHDS_Group*>( grp_i->GetGroupDS() ))
2566 TEEMap::iterator e2neIt = e2neMap.begin();
2567 for ( ; e2neIt != e2neMap.end(); ++e2neIt )
2569 aNewElem = e2neIt->second;
2570 if ( aNewElem->GetType() == grpDS->GetType() )
2572 grpDS->Add( aNewElem );
2574 if ( prevState[ aGroupType ]++ >= curState[ aGroupType ] )
2580 aListOfNewGroups.clear();
2581 aListOfNewGroups.push_back(aNewGroup);
2582 aGroupsMap.insert(make_pair( make_pair(groupName, aGroupType), aListOfNewGroups ));
2586 if ( SMESH_Mesh_i* anSrcImpl = SMESH::DownCast<SMESH_Mesh_i*>( theMeshesArray[i] ))
2588 // copy orphan nodes
2589 if ( anSrcImpl->NbNodes() > (int)nodesMap.size() )
2591 SMDS_ElemIteratorPtr itNodes = anInitImpl->GetElements( theMeshesArray[i], SMESH::NODE );
2592 while ( itNodes->more() )
2594 const SMDS_MeshNode* aNode = static_cast< const SMDS_MeshNode* >( itNodes->next() );
2595 if ( aNode->NbInverseElements() == 0 )
2597 aNewNode = aNewMeshDS->AddNode(aNode->X(), aNode->Y(), aNode->Z());
2598 nodesMap.insert( make_pair( aNode, aNewNode ));
2605 SMESH::SMESH_GroupBase_ptr aGroup;
2606 CORBA::String_var aGroupName;
2607 SMESH::long_array_var anNewIDs = new SMESH::long_array();
2609 // loop on groups of a source mesh
2610 aListOfGroups = anSrcImpl->GetGroups();
2611 for ( CORBA::ULong iG = 0; iG < aListOfGroups->length(); iG++ )
2613 aGroup = aListOfGroups[iG];
2614 aGroupType = aGroup->GetType();
2615 aGroupName = aGroup->GetName();
2616 string aName = aGroupName.in();
2618 // convert a list of IDs
2619 anNewIDs->length( aGroup->Size() );
2620 TEEMap & e2neMap = ( aGroupType == SMESH::NODE ) ? nodesMap : elemsMap;
2621 SMDS_ElemIteratorPtr itGrElems = anSrcImpl->GetElements( aGroup, SMESH::ALL );
2623 while ( itGrElems->more() )
2625 anElem = itGrElems->next();
2626 TEEMap::iterator e2neIt = e2neMap.find( anElem );
2627 if ( e2neIt != e2neMap.end() )
2628 anNewIDs[ iElem++ ] = e2neIt->second->GetID();
2630 anNewIDs->length( iElem );
2632 // check a current group name and type don't have identical ones in final mesh
2633 aListOfNewGroups.clear();
2634 TGroupsMap::iterator anIter = aGroupsMap.find( make_pair( aName, aGroupType ));
2635 if ( anIter == aGroupsMap.end() ) {
2636 // add a new group in the mesh
2637 aNewGroup = aNewImpl->CreateGroup( aGroupType, aGroupName.in() );
2638 // add elements into new group
2639 aNewGroup->Add( anNewIDs );
2641 aListOfNewGroups.push_back(aNewGroup);
2642 aGroupsMap.insert(make_pair( make_pair(aName, aGroupType), aListOfNewGroups ));
2645 else if ( theUniteIdenticalGroups ) {
2646 // unite identical groups
2647 TListOfNewGroups& aNewGroups = anIter->second;
2648 aNewGroups.front()->Add( anNewIDs );
2652 // rename identical groups
2653 aNewGroup = aNewImpl->CreateGroup(aGroupType, aGroupName.in());
2654 aNewGroup->Add( anNewIDs );
2656 TListOfNewGroups& aNewGroups = anIter->second;
2657 string aNewGroupName;
2658 if (aNewGroups.size() == 1) {
2659 aNewGroupName = aName + "_1";
2660 aNewGroups.front()->SetName(aNewGroupName.c_str());
2662 char aGroupNum[128];
2663 sprintf(aGroupNum, "%u", (unsigned int)aNewGroups.size()+1);
2664 aNewGroupName = aName + "_" + string(aGroupNum);
2665 aNewGroup->SetName(aNewGroupName.c_str());
2666 aNewGroups.push_back(aNewGroup);
2669 } // if an IDSource is a mesh
2672 if (theMergeNodesAndElements) // merge nodes
2674 TIDSortedNodeSet aMeshNodes; // no input nodes
2675 SMESH_MeshEditor::TListOfListOfNodes aGroupsOfNodes;
2676 aNewEditor.FindCoincidentNodes( aMeshNodes, theMergeTolerance, aGroupsOfNodes,
2677 /*SeparateCornersAndMedium=*/ false );
2678 aNewEditor.MergeNodes( aGroupsOfNodes );
2680 aNewEditor.MergeEqualElements();
2683 // Update Python script
2684 aPythonDump << aNewMesh << " = " << this << "."
2685 << ( theCommonGroups ? "ConcatenateWithGroups" : "Concatenate" )
2687 for ( CORBA::ULong i = 0; i < theMeshesArray.length(); i++) {
2688 if (i > 0) aPythonDump << ", ";
2689 aPythonDump << theMeshesArray[i];
2691 aPythonDump << "], ";
2692 aPythonDump << theUniteIdenticalGroups << ", "
2693 << theMergeNodesAndElements << ", "
2694 << TVar( theMergeTolerance ) << ")";
2696 delete pPythonDump; // enable python dump from GetGroups()
2698 // 0020577: EDF 1164 SMESH: Bad dump of concatenate with create common groups
2699 if ( !aNewMesh->_is_nil() )
2701 SMESH::ListOfGroups_var groups = aNewMesh->GetGroups();
2704 // IPAL21468 Change icon of compound because it need not be computed.
2705 SALOMEDS::SObject_wrap aMeshSObj = ObjectToSObject( myCurrentStudy, aNewMesh );
2706 SetPixMap( aMeshSObj, "ICON_SMESH_TREE_MESH" );
2709 aNewMeshDS->Modified();
2711 return aNewMesh._retn();
2714 //================================================================================
2716 * \brief Create a mesh by copying a part of another mesh
2717 * \param meshPart - a part of mesh to copy
2718 * \param toCopyGroups - to create in the new mesh groups
2719 * the copied elements belongs to
2720 * \param toKeepIDs - to preserve IDs of the copied elements or not
2721 * \retval SMESH::SMESH_Mesh_ptr - the new mesh
2723 //================================================================================
2725 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CopyMesh(SMESH::SMESH_IDSource_ptr meshPart,
2726 const char* meshName,
2727 CORBA::Boolean toCopyGroups,
2728 CORBA::Boolean toKeepIDs)
2730 Unexpect aCatch(SALOME_SalomeException);
2732 TPythonDump* pyDump = new TPythonDump; // prevent dump from CreateMesh()
2734 // 1. Get source mesh
2736 if ( CORBA::is_nil( meshPart ))
2737 THROW_SALOME_CORBA_EXCEPTION( "bad IDSource", SALOME::BAD_PARAM );
2739 SMESH::SMESH_Mesh_var srcMesh = meshPart->GetMesh();
2740 SMESH_Mesh_i* srcMesh_i = SMESH::DownCast<SMESH_Mesh_i*>( srcMesh );
2742 THROW_SALOME_CORBA_EXCEPTION( "bad mesh of IDSource", SALOME::BAD_PARAM );
2744 SMESHDS_Mesh* srcMeshDS = srcMesh_i->GetImpl().GetMeshDS();
2746 // 2. Make a new mesh
2748 SMESH::SMESH_Mesh_var newMesh = CreateMesh(GEOM::GEOM_Object::_nil());
2749 SMESH_Mesh_i* newMesh_i = SMESH::DownCast<SMESH_Mesh_i*>( newMesh );
2751 THROW_SALOME_CORBA_EXCEPTION( "can't create a mesh", SALOME::INTERNAL_ERROR );
2752 SALOMEDS::SObject_wrap meshSO = ObjectToSObject(myCurrentStudy, newMesh );
2753 if ( !meshSO->_is_nil() )
2755 SetName( meshSO, meshName, "Mesh" );
2756 SetPixMap( meshSO, "ICON_SMESH_TREE_MESH_IMPORTED");
2758 SMESHDS_Mesh* newMeshDS = newMesh_i->GetImpl().GetMeshDS();
2759 ::SMESH_MeshEditor editor( &newMesh_i->GetImpl() );
2760 ::SMESH_MeshEditor::ElemFeatures elemType;
2762 // 3. Get elements to copy
2764 SMDS_ElemIteratorPtr srcElemIt; SMDS_NodeIteratorPtr srcNodeIt;
2765 TIDSortedElemSet srcElems;
2766 SMESH::array_of_ElementType_var srcElemTypes = meshPart->GetTypes();
2767 if ( SMESH::DownCast<SMESH_Mesh_i*>( meshPart ))
2770 srcElemIt = srcMeshDS->elementsIterator();
2771 srcNodeIt = srcMeshDS->nodesIterator();
2775 SMESH::long_array_var ids = meshPart->GetIDs();
2776 if ( srcElemTypes->length() == 1 && srcElemTypes[0] == SMESH::NODE ) // group of nodes
2778 for ( CORBA::ULong i=0; i < ids->length(); i++ )
2779 if ( const SMDS_MeshElement * elem = srcMeshDS->FindNode( ids[i] ))
2780 srcElems.insert( elem );
2784 for ( CORBA::ULong i = 0; i < ids->length(); i++ )
2785 if ( const SMDS_MeshElement * elem = srcMeshDS->FindElement( ids[i] ))
2786 srcElems.insert( elem );
2788 if ( srcElems.empty() )
2789 return newMesh._retn();
2791 typedef SMDS_SetIterator< SMDS_pElement, TIDSortedElemSet::const_iterator > ElIter;
2792 srcElemIt = SMDS_ElemIteratorPtr( new ElIter( srcElems.begin(), srcElems.end() ));
2797 typedef map<SMDS_pElement, SMDS_pElement, TIDCompare> TE2EMap;
2798 TE2EMap e2eMapByType[ SMDSAbs_NbElementTypes ];
2799 TE2EMap& n2nMap = e2eMapByType[ SMDSAbs_Node ];
2801 const SMDS_MeshNode *nSrc, *nTgt;
2802 vector< const SMDS_MeshNode* > nodes;
2803 while ( srcElemIt->more() )
2805 const SMDS_MeshElement * elem = srcElemIt->next();
2807 nodes.resize( elem->NbNodes());
2808 SMDS_ElemIteratorPtr nIt = elem->nodesIterator();
2810 for ( iN = 0; nIt->more(); ++iN )
2812 nSrc = static_cast<const SMDS_MeshNode*>( nIt->next() );
2813 nTgt = newMeshDS->FindNode( nSrc->GetID());
2815 nTgt = newMeshDS->AddNodeWithID( nSrc->X(), nSrc->Y(), nSrc->Z(), nSrc->GetID());
2820 for ( iN = 0; nIt->more(); ++iN )
2822 nSrc = static_cast<const SMDS_MeshNode*>( nIt->next() );
2823 TE2EMap::iterator n2n = n2nMap.insert( make_pair( nSrc, SMDS_pNode(0) )).first;
2825 n2n->second = newMeshDS->AddNode( nSrc->X(), nSrc->Y(), nSrc->Z() );
2826 nodes[ iN ] = (const SMDS_MeshNode*) n2n->second;
2830 if ( elem->GetType() != SMDSAbs_Node )
2832 elemType.Init( elem, /*basicOnly=*/false );
2833 if ( toKeepIDs ) elemType.SetID( elem->GetID() );
2835 const SMDS_MeshElement * newElem = editor.AddElement( nodes, elemType );
2836 if ( toCopyGroups && !toKeepIDs )
2837 e2eMapByType[ elem->GetType() ].insert( make_pair( elem, newElem ));
2839 } // while ( srcElemIt->more() )
2841 // 4(b). Copy free nodes
2843 if ( srcNodeIt && srcMeshDS->NbNodes() != newMeshDS->NbNodes() )
2845 while ( srcNodeIt->more() )
2847 nSrc = srcNodeIt->next();
2848 if ( nSrc->NbInverseElements() == 0 )
2851 nTgt = newMeshDS->AddNodeWithID( nSrc->X(), nSrc->Y(), nSrc->Z(), nSrc->GetID());
2853 n2nMap[ nSrc ] = newMeshDS->AddNode( nSrc->X(), nSrc->Y(), nSrc->Z() );
2860 int nbNewGroups = 0;
2863 SMESH_Mesh::GroupIteratorPtr gIt = srcMesh_i->GetImpl().GetGroups();
2864 while ( gIt->more() )
2866 SMESH_Group* group = gIt->next();
2867 const SMESHDS_GroupBase* groupDS = group->GetGroupDS();
2869 // Check group type. We copy nodal groups containing nodes of copied element
2870 SMDSAbs_ElementType groupType = groupDS->GetType();
2871 if ( groupType != SMDSAbs_Node &&
2872 newMeshDS->GetMeshInfo().NbElements( groupType ) == 0 )
2873 continue; // group type differs from types of meshPart
2875 // Find copied elements in the group
2876 vector< const SMDS_MeshElement* > groupElems;
2877 SMDS_ElemIteratorPtr eIt = groupDS->GetElements();
2880 const SMDS_MeshElement* foundElem;
2881 if ( groupType == SMDSAbs_Node )
2883 while ( eIt->more() )
2884 if (( foundElem = newMeshDS->FindNode( eIt->next()->GetID() )))
2885 groupElems.push_back( foundElem );
2889 while ( eIt->more() )
2890 if (( foundElem = newMeshDS->FindElement( eIt->next()->GetID() )))
2891 groupElems.push_back( foundElem );
2896 TE2EMap & e2eMap = e2eMapByType[ groupDS->GetType() ];
2897 if ( e2eMap.empty() ) continue;
2898 int minID = e2eMap.begin()->first->GetID();
2899 int maxID = e2eMap.rbegin()->first->GetID();
2900 TE2EMap::iterator e2e;
2901 while ( eIt->more() && groupElems.size() < e2eMap.size())
2903 const SMDS_MeshElement* e = eIt->next();
2904 if ( e->GetID() < minID || e->GetID() > maxID ) continue;
2905 if ((e2e = e2eMap.find( e )) != e2eMap.end())
2906 groupElems.push_back( e2e->second );
2910 if ( !groupElems.empty() )
2912 SMESH::SMESH_Group_var newGroupObj =
2913 newMesh->CreateGroup( SMESH::ElementType(groupType), group->GetName() );
2914 if ( SMESH_GroupBase_i* newGroup_i = SMESH::DownCast<SMESH_GroupBase_i*>( newGroupObj))
2916 SMESHDS_GroupBase * newGroupDS = newGroup_i->GetGroupDS();
2917 SMDS_MeshGroup& smdsGroup = ((SMESHDS_Group*)newGroupDS)->SMDSGroup();
2918 for ( unsigned i = 0; i < groupElems.size(); ++i )
2919 smdsGroup.Add( groupElems[i] );
2927 newMeshDS->Modified();
2929 *pyDump << newMesh << " = " << this
2930 << ".CopyMesh( " << meshPart << ", "
2931 << "'" << meshName << "', "
2932 << toCopyGroups << ", "
2933 << toKeepIDs << ")";
2935 delete pyDump; pyDump = 0; // allow dump in GetGroups()
2937 if ( nbNewGroups > 0 ) // dump created groups
2938 SMESH::ListOfGroups_var groups = newMesh->GetGroups();
2940 return newMesh._retn();
2943 //================================================================================
2945 * SMESH_Gen_i::GetMEDVersion
2947 * Get MED version of the file by its name
2949 //================================================================================
2950 CORBA::Boolean SMESH_Gen_i::GetMEDVersion(const char* theFileName,
2951 SMESH::MED_VERSION& theVersion)
2953 theVersion = SMESH::MED_V2_1;
2954 MED::EVersion aVersion = MED::GetVersionId( theFileName );
2955 switch( aVersion ) {
2956 case MED::eV2_1 : theVersion = SMESH::MED_V2_1; return true;
2957 case MED::eV2_2 : theVersion = SMESH::MED_V2_2; return true;
2958 case MED::eVUnknown : return false;
2963 //================================================================================
2965 * SMESH_Gen_i::GetMeshNames
2967 * Get names of meshes defined in file with the specified name
2969 //================================================================================
2970 SMESH::string_array* SMESH_Gen_i::GetMeshNames(const char* theFileName)
2972 SMESH::string_array_var aResult = new SMESH::string_array();
2973 MED::PWrapper aMed = MED::CrWrapper( theFileName );
2975 MED::TInt aNbMeshes = aMed->GetNbMeshes( &anErr );
2977 aResult->length( aNbMeshes );
2978 for( MED::TInt i = 0; i < aNbMeshes; i++ ) {
2979 MED::PMeshInfo aMeshInfo = aMed->GetPMeshInfo( i+1 );
2980 aResult[i] = CORBA::string_dup( aMeshInfo->GetName().c_str() );
2983 return aResult._retn();
2986 //=============================================================================
2990 * Save SMESH module's data
2992 //=============================================================================
2993 SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
2997 // ASSERT( theComponent->GetStudy()->StudyId() == myCurrentStudy->StudyId() )
2998 // san -- in case <myCurrentStudy> differs from theComponent's study,
2999 // use that of the component
3000 if ( theComponent->GetStudy()->StudyId() != GetCurrentStudyID() )
3001 SetCurrentStudy( theComponent->GetStudy() );
3003 // Store study contents as a set of python commands
3004 SavePython(myCurrentStudy);
3006 StudyContext* myStudyContext = GetCurrentStudyContext();
3008 // Declare a byte stream
3009 SALOMEDS::TMPFile_var aStreamFile;
3011 // Obtain a temporary dir
3012 TCollection_AsciiString tmpDir =
3013 ( isMultiFile ) ? TCollection_AsciiString( ( char* )theURL ) : ( char* )SALOMEDS_Tool::GetTmpDir().c_str();
3015 // Create a sequence of files processed
3016 SALOMEDS::ListOfFileNames_var aFileSeq = new SALOMEDS::ListOfFileNames;
3017 aFileSeq->length( NUM_TMP_FILES );
3019 TCollection_AsciiString aStudyName( "" );
3021 aStudyName = ( (char*)SALOMEDS_Tool::GetNameFromPath( myCurrentStudy->URL() ).c_str() );
3023 // Set names of temporary files
3024 TCollection_AsciiString filename =
3025 aStudyName + TCollection_AsciiString( "_SMESH.hdf" ); // for SMESH data itself
3026 TCollection_AsciiString meshfile =
3027 aStudyName + TCollection_AsciiString( "_SMESH_Mesh.med" ); // for mesh data to be stored in MED file
3028 aFileSeq[ 0 ] = CORBA::string_dup( filename.ToCString() );
3029 aFileSeq[ 1 ] = CORBA::string_dup( meshfile.ToCString() );
3030 filename = tmpDir + filename;
3031 meshfile = tmpDir + meshfile;
3034 HDFdataset* aDataset;
3035 HDFgroup* aTopGroup;
3037 HDFgroup* aSubGroup;
3038 HDFgroup* aSubSubGroup;
3039 hdf_size aSize[ 1 ];
3042 //Remove the files if they exist: BugID: 11225
3043 #ifndef WIN32 /* unix functionality */
3044 TCollection_AsciiString cmd("rm -f \"");
3046 TCollection_AsciiString cmd("del /F \"");
3053 system(cmd.ToCString());
3055 // MED writer to be used by storage process
3056 DriverMED_W_SMESHDS_Mesh myWriter;
3057 myWriter.SetFile( meshfile.ToCString() );
3060 // SetStoreName() to groups before storing hypotheses to let them refer to
3061 // groups using "store name", which is "Group <group_persistent_id>"
3063 SALOMEDS::ChildIterator_wrap itBig = myCurrentStudy->NewChildIterator( theComponent );
3064 for ( ; itBig->More(); itBig->Next() ) {
3065 SALOMEDS::SObject_wrap gotBranch = itBig->Value();
3066 if ( gotBranch->Tag() > GetAlgorithmsRootTag() ) {
3067 CORBA::Object_var anObject = SObjectToObject( gotBranch );
3068 if ( !CORBA::is_nil( anObject ) ) {
3069 SMESH::SMESH_Mesh_var myMesh = SMESH::SMESH_Mesh::_narrow( anObject ) ;
3070 if ( !myMesh->_is_nil() ) {
3071 myMesh->Load(); // load from study file if not yet done
3072 TPythonDump pd; // not to dump GetGroups()
3073 SMESH::ListOfGroups_var groups = myMesh->GetGroups();
3074 pd << ""; // to avoid optimizing pd out
3075 for ( CORBA::ULong i = 0; i < groups->length(); ++i )
3077 SMESH_GroupBase_i* grImpl = SMESH::DownCast<SMESH_GroupBase_i*>( groups[i]);
3080 CORBA::String_var objStr = GetORB()->object_to_string( grImpl->_this() );
3081 int anId = myStudyContext->findId( string( objStr.in() ) );
3083 sprintf( grpName, "Group %d", anId );
3084 SMESHDS_GroupBase* aGrpBaseDS = grImpl->GetGroupDS();
3085 aGrpBaseDS->SetStoreName( grpName );
3095 // ---> create HDF file
3096 aFile = new HDFfile( (char*) filename.ToCString() );
3097 aFile->CreateOnDisk();
3099 // --> iterator for top-level objects
3100 SALOMEDS::ChildIterator_wrap itBig = myCurrentStudy->NewChildIterator( theComponent );
3101 for ( ; itBig->More(); itBig->Next() ) {
3102 SALOMEDS::SObject_wrap gotBranch = itBig->Value();
3104 // --> hypotheses root branch (only one for the study)
3105 if ( gotBranch->Tag() == GetHypothesisRootTag() ) {
3106 // create hypotheses root HDF group
3107 aTopGroup = new HDFgroup( "Hypotheses", aFile );
3108 aTopGroup->CreateOnDisk();
3110 // iterator for all hypotheses
3111 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( gotBranch );
3112 for ( ; it->More(); it->Next() ) {
3113 SALOMEDS::SObject_wrap mySObject = it->Value();
3114 CORBA::Object_var anObject = SObjectToObject( mySObject );
3115 if ( !CORBA::is_nil( anObject ) ) {
3116 SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
3117 if ( !myHyp->_is_nil() ) {
3118 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
3120 string hypname = string( myHyp->GetName() );
3121 string libname = string( myHyp->GetLibName() );
3123 // Needs for save crossplatform libname, i.e. parth of name ( ".dll" for
3124 // WIN32 and ".so" for X-system) must be deleted
3125 int libname_len = libname.length();
3127 if( libname_len > 4 )
3128 libname.resize( libname_len - 4 );
3130 // PAL17753 (Regresion: missing hypothesis in restored study)
3131 // "lib" also should be removed from the beginning
3132 //if( libname_len > 3 )
3133 //libname.resize( libname_len - 3 );
3134 if( libname_len > 6 )
3135 libname = libname.substr( 3, libname_len - 3 - 3 );
3137 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3138 int id = myStudyContext->findId( string( objStr.in() ) );
3139 string hypdata = string( myImpl->SaveTo() );
3141 // for each hypothesis create HDF group basing on its id
3142 char hypGrpName[30];
3143 sprintf( hypGrpName, "Hypothesis %d", id );
3144 aGroup = new HDFgroup( hypGrpName, aTopGroup );
3145 aGroup->CreateOnDisk();
3146 // --> type name of hypothesis
3147 aSize[ 0 ] = hypname.length() + 1;
3148 aDataset = new HDFdataset( "Name", aGroup, HDF_STRING, aSize, 1 );
3149 aDataset->CreateOnDisk();
3150 aDataset->WriteOnDisk( ( char* )( hypname.c_str() ) );
3151 aDataset->CloseOnDisk();
3152 // --> server plugin library name of hypothesis
3153 aSize[ 0 ] = libname.length() + 1;
3154 aDataset = new HDFdataset( "LibName", aGroup, HDF_STRING, aSize, 1 );
3155 aDataset->CreateOnDisk();
3156 aDataset->WriteOnDisk( ( char* )( libname.c_str() ) );
3157 aDataset->CloseOnDisk();
3158 // --> persistent data of hypothesis
3159 aSize[ 0 ] = hypdata.length() + 1;
3160 aDataset = new HDFdataset( "Data", aGroup, HDF_STRING, aSize, 1 );
3161 aDataset->CreateOnDisk();
3162 aDataset->WriteOnDisk( ( char* )( hypdata.c_str() ) );
3163 aDataset->CloseOnDisk();
3164 // close hypothesis HDF group
3165 aGroup->CloseOnDisk();
3170 // close hypotheses root HDF group
3171 aTopGroup->CloseOnDisk();
3173 // --> algorithms root branch (only one for the study)
3174 else if ( gotBranch->Tag() == GetAlgorithmsRootTag() ) {
3175 // create algorithms root HDF group
3176 aTopGroup = new HDFgroup( "Algorithms", aFile );
3177 aTopGroup->CreateOnDisk();
3179 // iterator for all algorithms
3180 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( gotBranch );
3181 for ( ; it->More(); it->Next() ) {
3182 SALOMEDS::SObject_wrap mySObject = it->Value();
3183 CORBA::Object_var anObject = SObjectToObject( mySObject );
3184 if ( !CORBA::is_nil( anObject ) ) {
3185 SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
3186 if ( !myHyp->_is_nil() ) {
3187 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
3189 string hypname = string( myHyp->GetName() );
3190 string libname = string( myHyp->GetLibName() );
3192 // Needs for save crossplatform libname, i.e. parth of name ( ".dll" for
3193 // WIN32 and ".so" for X-system) must be deleted
3194 int libname_len = libname.length();
3196 if( libname_len > 4 )
3197 libname.resize( libname_len - 4 );
3199 // PAL17753 (Regresion: missing hypothesis in restored study)
3200 // "lib" also should be removed from the beginning
3201 //if( libname_len > 3 )
3202 //libname.resize( libname_len - 3 );
3203 if( libname_len > 6 )
3204 libname = libname.substr( 3, libname_len - 3 - 3 );
3206 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3207 int id = myStudyContext->findId( string( objStr.in() ) );
3208 string hypdata = string( myImpl->SaveTo() );
3210 // for each algorithm create HDF group basing on its id
3211 char hypGrpName[30];
3212 sprintf( hypGrpName, "Algorithm %d", id );
3213 aGroup = new HDFgroup( hypGrpName, aTopGroup );
3214 aGroup->CreateOnDisk();
3215 // --> type name of algorithm
3216 aSize[0] = hypname.length() + 1;
3217 aDataset = new HDFdataset( "Name", aGroup, HDF_STRING, aSize, 1 );
3218 aDataset->CreateOnDisk();
3219 aDataset->WriteOnDisk( ( char* )( hypname.c_str() ) );
3220 aDataset->CloseOnDisk();
3221 // --> server plugin library name of hypothesis
3222 aSize[0] = libname.length() + 1;
3223 aDataset = new HDFdataset( "LibName", aGroup, HDF_STRING, aSize, 1 );
3224 aDataset->CreateOnDisk();
3225 aDataset->WriteOnDisk( ( char* )( libname.c_str() ) );
3226 aDataset->CloseOnDisk();
3227 // --> persistent data of algorithm
3228 aSize[0] = hypdata.length() + 1;
3229 aDataset = new HDFdataset( "Data", aGroup, HDF_STRING, aSize, 1 );
3230 aDataset->CreateOnDisk();
3231 aDataset->WriteOnDisk( ( char* )( hypdata.c_str() ) );
3232 aDataset->CloseOnDisk();
3233 // close algorithm HDF group
3234 aGroup->CloseOnDisk();
3239 // close algorithms root HDF group
3240 aTopGroup->CloseOnDisk();
3242 // --> mesh objects roots branches
3243 else if ( gotBranch->Tag() > GetAlgorithmsRootTag() ) {
3244 CORBA::Object_var anObject = SObjectToObject( gotBranch );
3245 if ( !CORBA::is_nil( anObject ) ) {
3246 SMESH::SMESH_Mesh_var myMesh = SMESH::SMESH_Mesh::_narrow( anObject ) ;
3247 if ( !myMesh->_is_nil() ) {
3248 SMESH_Mesh_i* myImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( myMesh ).in() );
3250 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3251 int id = myStudyContext->findId( string( objStr.in() ) );
3252 ::SMESH_Mesh& myLocMesh = myImpl->GetImpl();
3253 SMESHDS_Mesh* mySMESHDSMesh = myLocMesh.GetMeshDS();
3254 bool hasShape = myLocMesh.HasShapeToMesh();
3256 // for each mesh open the HDF group basing on its id
3257 char meshGrpName[ 30 ];
3258 sprintf( meshGrpName, "Mesh %d", id );
3259 aTopGroup = new HDFgroup( meshGrpName, aFile );
3260 aTopGroup->CreateOnDisk();
3262 // --> put dataset to hdf file which is a flag that mesh has data
3263 string strHasData = "0";
3264 // check if the mesh is not empty
3265 if ( mySMESHDSMesh->NbNodes() > 0 ) {
3266 // write mesh data to med file
3267 myWriter.SetMesh( mySMESHDSMesh );
3268 myWriter.SetMeshId( id );
3271 aSize[ 0 ] = strHasData.length() + 1;
3272 aDataset = new HDFdataset( "Has data", aTopGroup, HDF_STRING, aSize, 1 );
3273 aDataset->CreateOnDisk();
3274 aDataset->WriteOnDisk( ( char* )( strHasData.c_str() ) );
3275 aDataset->CloseOnDisk();
3278 // for each mesh open the HDF group basing on its auto color parameter
3279 char meshAutoColorName[ 30 ];
3280 sprintf( meshAutoColorName, "AutoColorMesh %d", id );
3282 anAutoColor[0] = myImpl->GetAutoColor();
3284 aDataset = new HDFdataset( meshAutoColorName, aTopGroup, HDF_INT32, aSize, 1 );
3285 aDataset->CreateOnDisk();
3286 aDataset->WriteOnDisk( anAutoColor );
3287 aDataset->CloseOnDisk();
3289 // issue 0020693. Store _isModified flag
3290 int isModified = myLocMesh.GetIsModified();
3292 aDataset = new HDFdataset( "_isModified", aTopGroup, HDF_INT32, aSize, 1 );
3293 aDataset->CreateOnDisk();
3294 aDataset->WriteOnDisk( &isModified );
3295 aDataset->CloseOnDisk();
3297 // issue 20918. Store Persistent Id of SMESHDS_Mesh
3298 int meshPersistentId = mySMESHDSMesh->GetPersistentId();
3300 aDataset = new HDFdataset( "meshPersistentId", aTopGroup, HDF_INT32, aSize, 1 );
3301 aDataset->CreateOnDisk();
3302 aDataset->WriteOnDisk( &meshPersistentId );
3303 aDataset->CloseOnDisk();
3305 // write reference on a shape if exists
3306 SALOMEDS::SObject_wrap myRef;
3307 bool shapeRefFound = false;
3308 bool found = gotBranch->FindSubObject( GetRefOnShapeTag(), myRef.inout() );
3310 SALOMEDS::SObject_wrap myShape;
3311 bool ok = myRef->ReferencedObject( myShape.inout() );
3313 shapeRefFound = (! CORBA::is_nil( myShape->GetObject() ));
3314 string myRefOnObject = myShape->GetID();
3315 if ( shapeRefFound && myRefOnObject.length() > 0 ) {
3316 aSize[ 0 ] = myRefOnObject.length() + 1;
3317 aDataset = new HDFdataset( "Ref on shape", aTopGroup, HDF_STRING, aSize, 1 );
3318 aDataset->CreateOnDisk();
3319 aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3320 aDataset->CloseOnDisk();
3325 // write applied hypotheses if exist
3326 SALOMEDS::SObject_wrap myHypBranch;
3327 found = gotBranch->FindSubObject( GetRefOnAppliedHypothesisTag(), myHypBranch.inout() );
3328 if ( found && !shapeRefFound && hasShape) { // remove applied hyps
3329 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( myHypBranch );
3331 if ( found && (shapeRefFound || !hasShape) ) {
3332 aGroup = new HDFgroup( "Applied Hypotheses", aTopGroup );
3333 aGroup->CreateOnDisk();
3335 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( myHypBranch );
3337 for ( ; it->More(); it->Next() ) {
3338 SALOMEDS::SObject_wrap mySObject = it->Value();
3339 SALOMEDS::SObject_wrap myRefOnHyp;
3340 bool ok = mySObject->ReferencedObject( myRefOnHyp.inout() );
3342 // san - it is impossible to recover applied hypotheses
3343 // using their entries within Load() method,
3344 // for there are no AttributeIORs in the study when Load() is working.
3345 // Hence, it is better to store persistent IDs of hypotheses as references to them
3347 //string myRefOnObject = myRefOnHyp->GetID();
3348 CORBA::Object_var anObject = SObjectToObject( myRefOnHyp );
3349 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3350 int id = myStudyContext->findId( string( objStr.in() ) );
3351 //if ( myRefOnObject.length() > 0 ) {
3352 //aSize[ 0 ] = myRefOnObject.length() + 1;
3353 char hypName[ 30 ], hypId[ 30 ];
3354 sprintf( hypName, "Hyp %d", ++hypNb );
3355 sprintf( hypId, "%d", id );
3356 aSize[ 0 ] = strlen( hypId ) + 1;
3357 aDataset = new HDFdataset( hypName, aGroup, HDF_STRING, aSize, 1 );
3358 aDataset->CreateOnDisk();
3359 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3360 aDataset->WriteOnDisk( hypId );
3361 aDataset->CloseOnDisk();
3365 aGroup->CloseOnDisk();
3368 // write applied algorithms if exist
3369 SALOMEDS::SObject_wrap myAlgoBranch;
3370 found = gotBranch->FindSubObject( GetRefOnAppliedAlgorithmsTag(),
3371 myAlgoBranch.inout() );
3372 if ( found && !shapeRefFound && hasShape) { // remove applied algos
3373 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( myAlgoBranch );
3375 if ( found && (shapeRefFound || !hasShape)) {
3376 aGroup = new HDFgroup( "Applied Algorithms", aTopGroup );
3377 aGroup->CreateOnDisk();
3379 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( myAlgoBranch );
3381 for ( ; it->More(); it->Next() ) {
3382 SALOMEDS::SObject_wrap mySObject = it->Value();
3383 SALOMEDS::SObject_wrap myRefOnAlgo;
3384 bool ok = mySObject->ReferencedObject( myRefOnAlgo.inout() );
3386 // san - it is impossible to recover applied algorithms
3387 // using their entries within Load() method,
3388 // for there are no AttributeIORs in the study when Load() is working.
3389 // Hence, it is better to store persistent IDs of algorithms as references to them
3391 //string myRefOnObject = myRefOnAlgo->GetID();
3392 CORBA::Object_var anObject = SObjectToObject( myRefOnAlgo );
3393 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3394 int id = myStudyContext->findId( string( objStr.in() ) );
3395 //if ( myRefOnObject.length() > 0 ) {
3396 //aSize[ 0 ] = myRefOnObject.length() + 1;
3397 char algoName[ 30 ], algoId[ 30 ];
3398 sprintf( algoName, "Algo %d", ++algoNb );
3399 sprintf( algoId, "%d", id );
3400 aSize[ 0 ] = strlen( algoId ) + 1;
3401 aDataset = new HDFdataset( algoName, aGroup, HDF_STRING, aSize, 1 );
3402 aDataset->CreateOnDisk();
3403 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3404 aDataset->WriteOnDisk( algoId );
3405 aDataset->CloseOnDisk();
3409 aGroup->CloseOnDisk();
3412 // --> submesh objects sub-branches
3414 for ( int i = GetSubMeshOnVertexTag(); i <= GetSubMeshOnCompoundTag(); i++ ) {
3415 SALOMEDS::SObject_wrap mySubmeshBranch;
3416 found = gotBranch->FindSubObject( i, mySubmeshBranch.inout() );
3418 if ( found ) // check if there is shape reference in submeshes
3420 bool hasShapeRef = false;
3421 SALOMEDS::ChildIterator_wrap itSM =
3422 myCurrentStudy->NewChildIterator( mySubmeshBranch );
3423 for ( ; itSM->More(); itSM->Next() ) {
3424 SALOMEDS::SObject_wrap mySubRef, myShape, mySObject = itSM->Value();
3425 if ( mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef.inout() ))
3426 mySubRef->ReferencedObject( myShape.inout() );
3427 if ( !CORBA::is_nil( myShape ) && !CORBA::is_nil( myShape->GetObject() ))
3430 { // remove one submesh
3431 if ( shapeRefFound )
3432 { // unassign hypothesis
3433 SMESH::SMESH_subMesh_var mySubMesh =
3434 SMESH::SMESH_subMesh::_narrow( SObjectToObject( mySObject ));
3435 if ( !mySubMesh->_is_nil() ) {
3436 int shapeID = mySubMesh->GetId();
3437 TopoDS_Shape S = mySMESHDSMesh->IndexToShape( shapeID );
3438 const list<const SMESHDS_Hypothesis*>& hypList =
3439 mySMESHDSMesh->GetHypothesis( S );
3440 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
3441 while ( hyp != hypList.end() ) {
3442 int hypID = (*hyp++)->GetID(); // goto next hyp here because
3443 myLocMesh.RemoveHypothesis( S, hypID ); // hypList changes here
3447 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( mySObject );
3449 } // loop on submeshes of a type
3450 if ( !shapeRefFound || !hasShapeRef ) { // remove the whole submeshes branch
3451 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( mySubmeshBranch );
3454 } // end check if there is shape reference in submeshes
3456 char name_meshgroup[ 30 ];
3457 if ( i == GetSubMeshOnVertexTag() )
3458 strcpy( name_meshgroup, "SubMeshes On Vertex" );
3459 else if ( i == GetSubMeshOnEdgeTag() )
3460 strcpy( name_meshgroup, "SubMeshes On Edge" );
3461 else if ( i == GetSubMeshOnWireTag() )
3462 strcpy( name_meshgroup, "SubMeshes On Wire" );
3463 else if ( i == GetSubMeshOnFaceTag() )
3464 strcpy( name_meshgroup, "SubMeshes On Face" );
3465 else if ( i == GetSubMeshOnShellTag() )
3466 strcpy( name_meshgroup, "SubMeshes On Shell" );
3467 else if ( i == GetSubMeshOnSolidTag() )
3468 strcpy( name_meshgroup, "SubMeshes On Solid" );
3469 else if ( i == GetSubMeshOnCompoundTag() )
3470 strcpy( name_meshgroup, "SubMeshes On Compound" );
3472 // for each type of submeshes create container HDF group
3473 aGroup = new HDFgroup( name_meshgroup, aTopGroup );
3474 aGroup->CreateOnDisk();
3476 // iterator for all submeshes of given type
3477 SALOMEDS::ChildIterator_wrap itSM = myCurrentStudy->NewChildIterator( mySubmeshBranch );
3478 for ( ; itSM->More(); itSM->Next() ) {
3479 SALOMEDS::SObject_wrap mySObject = itSM->Value();
3480 CORBA::Object_var anSubObject = SObjectToObject( mySObject );
3481 if ( !CORBA::is_nil( anSubObject ))
3483 SMESH::SMESH_subMesh_var mySubMesh = SMESH::SMESH_subMesh::_narrow( anSubObject ) ;
3484 CORBA::String_var objStr = GetORB()->object_to_string( anSubObject );
3485 int subid = myStudyContext->findId( string( objStr.in() ) );
3487 // for each mesh open the HDF group basing on its id
3488 char submeshGrpName[ 30 ];
3489 sprintf( submeshGrpName, "SubMesh %d", subid );
3490 aSubGroup = new HDFgroup( submeshGrpName, aGroup );
3491 aSubGroup->CreateOnDisk();
3493 // write reference on a shape, already checked if it exists
3494 SALOMEDS::SObject_wrap mySubRef, myShape;
3495 if ( mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef.inout() ))
3496 mySubRef->ReferencedObject( myShape.inout() );
3497 string myRefOnObject = myShape->GetID();
3498 if ( myRefOnObject.length() > 0 ) {
3499 aSize[ 0 ] = myRefOnObject.length() + 1;
3500 aDataset = new HDFdataset( "Ref on shape", aSubGroup, HDF_STRING, aSize, 1 );
3501 aDataset->CreateOnDisk();
3502 aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3503 aDataset->CloseOnDisk();
3506 // write applied hypotheses if exist
3507 SALOMEDS::SObject_wrap mySubHypBranch;
3508 found = mySObject->FindSubObject( GetRefOnAppliedHypothesisTag(),
3509 mySubHypBranch.inout() );
3511 aSubSubGroup = new HDFgroup( "Applied Hypotheses", aSubGroup );
3512 aSubSubGroup->CreateOnDisk();
3514 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( mySubHypBranch );
3516 for ( ; it->More(); it->Next() ) {
3517 SALOMEDS::SObject_wrap mySubSObject = it->Value();
3518 SALOMEDS::SObject_wrap myRefOnHyp;
3519 bool ok = mySubSObject->ReferencedObject( myRefOnHyp.inout() );
3521 //string myRefOnObject = myRefOnHyp->GetID();
3522 CORBA::Object_var anObject = SObjectToObject( myRefOnHyp );
3523 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3524 int id = myStudyContext->findId( string( objStr.in() ) );
3525 //if ( myRefOnObject.length() > 0 ) {
3526 //aSize[ 0 ] = myRefOnObject.length() + 1;
3527 char hypName[ 30 ], hypId[ 30 ];
3528 sprintf( hypName, "Hyp %d", ++hypNb );
3529 sprintf( hypId, "%d", id );
3530 aSize[ 0 ] = strlen( hypId ) + 1;
3531 aDataset = new HDFdataset( hypName, aSubSubGroup, HDF_STRING, aSize, 1 );
3532 aDataset->CreateOnDisk();
3533 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3534 aDataset->WriteOnDisk( hypId );
3535 aDataset->CloseOnDisk();
3539 aSubSubGroup->CloseOnDisk();
3542 // write applied algorithms if exist
3543 SALOMEDS::SObject_wrap mySubAlgoBranch;
3544 found = mySObject->FindSubObject( GetRefOnAppliedAlgorithmsTag(),
3545 mySubAlgoBranch.inout() );
3547 aSubSubGroup = new HDFgroup( "Applied Algorithms", aSubGroup );
3548 aSubSubGroup->CreateOnDisk();
3550 SALOMEDS::ChildIterator_wrap it =
3551 myCurrentStudy->NewChildIterator( mySubAlgoBranch );
3553 for ( ; it->More(); it->Next() ) {
3554 SALOMEDS::SObject_wrap mySubSObject = it->Value();
3555 SALOMEDS::SObject_wrap myRefOnAlgo;
3556 bool ok = mySubSObject->ReferencedObject( myRefOnAlgo.inout() );
3558 //string myRefOnObject = myRefOnAlgo->GetID();
3559 CORBA::Object_var anObject = SObjectToObject( myRefOnAlgo );
3560 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3561 int id = myStudyContext->findId( string( objStr.in() ) );
3562 //if ( myRefOnObject.length() > 0 ) {
3563 //aSize[ 0 ] = myRefOnObject.length() + 1;
3564 char algoName[ 30 ], algoId[ 30 ];
3565 sprintf( algoName, "Algo %d", ++algoNb );
3566 sprintf( algoId, "%d", id );
3567 aSize[ 0 ] = strlen( algoId ) + 1;
3568 aDataset = new HDFdataset( algoName, aSubSubGroup, HDF_STRING, aSize, 1 );
3569 aDataset->CreateOnDisk();
3570 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3571 aDataset->WriteOnDisk( algoId );
3572 aDataset->CloseOnDisk();
3576 aSubSubGroup->CloseOnDisk();
3578 // close submesh HDF group
3579 aSubGroup->CloseOnDisk();
3582 // close container of submeshes by type HDF group
3583 aGroup->CloseOnDisk();
3586 // All sub-meshes will be stored in MED file
3587 // .. will NOT (PAL 12992)
3588 //if ( shapeRefFound )
3589 //myWriter.AddAllSubMeshes();
3591 // store submesh order if any
3592 const TListOfListOfInt& theOrderIds = myLocMesh.GetMeshOrder();
3593 if ( theOrderIds.size() ) {
3594 char order_list[ 30 ];
3595 strcpy( order_list, "Mesh Order" );
3596 // count number of submesh ids
3598 TListOfListOfInt::const_iterator idIt = theOrderIds.begin();
3599 for ( ; idIt != theOrderIds.end(); idIt++ )
3600 nbIDs += (*idIt).size();
3601 // number of values = number of IDs +
3602 // number of lists (for separators) - 1
3603 int* smIDs = new int [ nbIDs + theOrderIds.size() - 1 ];
3604 idIt = theOrderIds.begin();
3605 for ( int i = 0; idIt != theOrderIds.end(); idIt++ ) {
3606 const TListOfInt& idList = *idIt;
3607 if (idIt != theOrderIds.begin()) // not first list
3608 smIDs[ i++ ] = -1/* *idList.size()*/; // separator between lists
3609 // dump submesh ids from current list
3610 TListOfInt::const_iterator id_smId = idList.begin();
3611 for( ; id_smId != idList.end(); id_smId++ )
3612 smIDs[ i++ ] = *id_smId;
3615 aSize[ 0 ] = nbIDs + theOrderIds.size() - 1;
3617 aDataset = new HDFdataset( order_list, aTopGroup, HDF_INT32, aSize, 1 );
3618 aDataset->CreateOnDisk();
3619 aDataset->WriteOnDisk( smIDs );
3620 aDataset->CloseOnDisk();
3625 // groups root sub-branch
3626 SALOMEDS::SObject_wrap myGroupsBranch;
3627 for ( int i = GetNodeGroupsTag(); i <= GetBallElementsGroupsTag(); i++ ) {
3628 found = gotBranch->FindSubObject( i, myGroupsBranch.inout() );
3630 char name_group[ 30 ];
3631 if ( i == GetNodeGroupsTag() )
3632 strcpy( name_group, "Groups of Nodes" );
3633 else if ( i == GetEdgeGroupsTag() )
3634 strcpy( name_group, "Groups of Edges" );
3635 else if ( i == GetFaceGroupsTag() )
3636 strcpy( name_group, "Groups of Faces" );
3637 else if ( i == GetVolumeGroupsTag() )
3638 strcpy( name_group, "Groups of Volumes" );
3639 else if ( i == Get0DElementsGroupsTag() )
3640 strcpy( name_group, "Groups of 0D Elements" );
3641 else if ( i == GetBallElementsGroupsTag() )
3642 strcpy( name_group, "Groups of Balls" );
3644 aGroup = new HDFgroup( name_group, aTopGroup );
3645 aGroup->CreateOnDisk();
3647 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( myGroupsBranch );
3648 for ( ; it->More(); it->Next() ) {
3649 SALOMEDS::SObject_wrap mySObject = it->Value();
3650 CORBA::Object_var aSubObject = SObjectToObject( mySObject );
3651 if ( !CORBA::is_nil( aSubObject ) ) {
3652 SMESH_GroupBase_i* myGroupImpl =
3653 dynamic_cast<SMESH_GroupBase_i*>( GetServant( aSubObject ).in() );
3656 SMESHDS_GroupBase* aGrpBaseDS = myGroupImpl->GetGroupDS();
3660 CORBA::String_var objStr = GetORB()->object_to_string( aSubObject );
3661 int anId = myStudyContext->findId( string( objStr.in() ) );
3663 // For each group, create a dataset named "Group <group_persistent_id>"
3664 // and store the group's user name into it
3665 const char* grpName = aGrpBaseDS->GetStoreName();
3666 char* aUserName = myGroupImpl->GetName();
3667 aSize[ 0 ] = strlen( aUserName ) + 1;
3669 aDataset = new HDFdataset( grpName, aGroup, HDF_STRING, aSize, 1 );
3670 aDataset->CreateOnDisk();
3671 aDataset->WriteOnDisk( aUserName );
3672 aDataset->CloseOnDisk();
3675 // For each group, create a dataset named "Group <group_persistent_id> Color"
3676 // and store the group's color into it
3677 char grpColorName[ 30 ];
3678 sprintf( grpColorName, "ColorGroup %d", anId );
3679 SALOMEDS::Color aColor = myGroupImpl->GetColor();
3681 anRGB[ 0 ] = aColor.R;
3682 anRGB[ 1 ] = aColor.G;
3683 anRGB[ 2 ] = aColor.B;
3685 aDataset = new HDFdataset( grpColorName, aGroup, HDF_FLOAT64, aSize, 1 );
3686 aDataset->CreateOnDisk();
3687 aDataset->WriteOnDisk( anRGB );
3688 aDataset->CloseOnDisk();
3690 // Pass SMESHDS_Group to MED writer
3691 SMESHDS_Group* aGrpDS = dynamic_cast<SMESHDS_Group*>( aGrpBaseDS );
3693 myWriter.AddGroup( aGrpDS );
3695 // write reference on a shape if exists
3696 SMESHDS_GroupOnGeom* aGeomGrp =
3697 dynamic_cast<SMESHDS_GroupOnGeom*>( aGrpBaseDS );
3699 SALOMEDS::SObject_wrap mySubRef, myShape;
3700 if (mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef.inout() ) &&
3701 mySubRef->ReferencedObject( myShape.inout() ) &&
3702 !CORBA::is_nil( myShape->GetObject() ))
3704 string myRefOnObject = myShape->GetID();
3705 if ( myRefOnObject.length() > 0 ) {
3706 char aRefName[ 30 ];
3707 sprintf( aRefName, "Ref on shape %d", anId);
3708 aSize[ 0 ] = myRefOnObject.length() + 1;
3709 aDataset = new HDFdataset(aRefName, aGroup, HDF_STRING, aSize, 1);
3710 aDataset->CreateOnDisk();
3711 aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3712 aDataset->CloseOnDisk();
3715 else // shape ref is invalid:
3717 // save a group on geometry as ordinary group
3718 myWriter.AddGroup( aGeomGrp );
3721 else if ( SMESH_GroupOnFilter_i* aFilterGrp_i =
3722 dynamic_cast<SMESH_GroupOnFilter_i*>( myGroupImpl ))
3724 std::string str = aFilterGrp_i->FilterToString();
3725 std::string hdfGrpName = "Filter " + SMESH_Comment(anId);
3726 aSize[ 0 ] = str.length() + 1;
3727 aDataset = new HDFdataset( hdfGrpName.c_str(), aGroup, HDF_STRING, aSize, 1);
3728 aDataset->CreateOnDisk();
3729 aDataset->WriteOnDisk( ( char* )( str.c_str() ) );
3730 aDataset->CloseOnDisk();
3734 aGroup->CloseOnDisk();
3738 if ( strcmp( strHasData.c_str(), "1" ) == 0 )
3740 // Flush current mesh information into MED file
3743 // save info on nb of elements
3744 SMESH_PreMeshInfo::SaveToFile( myImpl, id, aFile );
3746 // maybe a shape was deleted in the study
3747 if ( !shapeRefFound && !mySMESHDSMesh->ShapeToMesh().IsNull() && hasShape) {
3748 TopoDS_Shape nullShape;
3749 myLocMesh.ShapeToMesh( nullShape ); // remove shape referring data
3752 SMESHDS_SubMeshIteratorPtr smIt = mySMESHDSMesh->SubMeshes();
3757 aGroup = new HDFgroup( "Submeshes", aTopGroup );
3758 aGroup->CreateOnDisk();
3760 // each element belongs to one or none submesh,
3761 // so for each node/element, we store a submesh ID
3763 // Store submesh IDs
3764 for ( int isNode = 0; isNode < 2; ++isNode )
3766 SMDS_ElemIteratorPtr eIt =
3767 mySMESHDSMesh->elementsIterator( isNode ? SMDSAbs_Node : SMDSAbs_All );
3768 int nbElems = isNode ? mySMESHDSMesh->NbNodes() : mySMESHDSMesh->GetMeshInfo().NbElements();
3771 std::vector<int> smIDs; smIDs.reserve( nbElems );
3772 while ( eIt->more() )
3773 if ( const SMDS_MeshElement* e = eIt->next())
3774 smIDs.push_back( e->getshapeId() );
3776 aSize[ 0 ] = nbElems;
3777 string aDSName( isNode ? "Node Submeshes" : "Element Submeshes");
3778 aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_INT32, aSize, 1 );
3779 aDataset->CreateOnDisk();
3780 aDataset->WriteOnDisk( & smIDs[0] );
3781 aDataset->CloseOnDisk();
3784 aGroup->CloseOnDisk();
3786 // Store node positions on sub-shapes (SMDS_Position):
3787 // ----------------------------------------------------
3789 aGroup = new HDFgroup( "Node Positions", aTopGroup );
3790 aGroup->CreateOnDisk();
3792 // in aGroup, create 5 datasets to contain:
3793 // "Nodes on Edges" - ID of node on edge
3794 // "Edge positions" - U parameter on node on edge
3795 // "Nodes on Faces" - ID of node on face
3796 // "Face U positions" - U parameter of node on face
3797 // "Face V positions" - V parameter of node on face
3799 // Find out nb of nodes on edges and faces
3800 // Collect corresponing sub-meshes
3801 int nbEdgeNodes = 0, nbFaceNodes = 0;
3802 list<SMESHDS_SubMesh*> aEdgeSM, aFaceSM;
3803 // loop on SMESHDS_SubMesh'es
3804 while ( smIt->more() )
3806 SMESHDS_SubMesh* aSubMesh = const_cast< SMESHDS_SubMesh* >( smIt->next() );
3807 if ( aSubMesh->IsComplexSubmesh() )
3808 continue; // submesh containing other submeshs
3809 int nbNodes = aSubMesh->NbNodes();
3810 if ( nbNodes == 0 ) continue;
3812 int aShapeID = aSubMesh->GetID();
3813 if ( aShapeID < 1 || aShapeID > mySMESHDSMesh->MaxShapeIndex() )
3815 int aShapeType = mySMESHDSMesh->IndexToShape( aShapeID ).ShapeType();
3816 // write only SMDS_FacePosition and SMDS_EdgePosition
3817 switch ( aShapeType ) {
3819 nbFaceNodes += nbNodes;
3820 aFaceSM.push_back( aSubMesh );
3823 nbEdgeNodes += nbNodes;
3824 aEdgeSM.push_back( aSubMesh );
3830 // Treat positions on edges or faces
3831 for ( int onFace = 0; onFace < 2; onFace++ )
3833 // Create arrays to store in datasets
3834 int iNode = 0, nbNodes = ( onFace ? nbFaceNodes : nbEdgeNodes );
3835 if (!nbNodes) continue;
3836 int* aNodeIDs = new int [ nbNodes ];
3837 double* aUPos = new double [ nbNodes ];
3838 double* aVPos = ( onFace ? new double[ nbNodes ] : 0 );
3841 // loop on sub-meshes
3842 list<SMESHDS_SubMesh*> * pListSM = ( onFace ? &aFaceSM : &aEdgeSM );
3843 list<SMESHDS_SubMesh*>::iterator itSM = pListSM->begin();
3844 for ( ; itSM != pListSM->end(); itSM++ )
3846 SMESHDS_SubMesh* aSubMesh = (*itSM);
3848 SMDS_NodeIteratorPtr itNode = aSubMesh->GetNodes();
3849 // loop on nodes in aSubMesh
3850 while ( itNode->more() )
3853 const SMDS_MeshNode* node = itNode->next();
3854 aNodeIDs [ iNode ] = node->GetID();
3857 const SMDS_PositionPtr pos = node->GetPosition();
3858 if ( onFace ) { // on FACE
3859 const SMDS_FacePosition* fPos =
3860 dynamic_cast<const SMDS_FacePosition*>( pos );
3862 aUPos[ iNode ] = fPos->GetUParameter();
3863 aVPos[ iNode ] = fPos->GetVParameter();
3870 const SMDS_EdgePosition* ePos =
3871 dynamic_cast<const SMDS_EdgePosition*>( pos );
3873 aUPos[ iNode ] = ePos->GetUParameter();
3879 } // loop on nodes in aSubMesh
3880 } // loop on sub-meshes
3885 aSize[ 0 ] = nbNodes;
3887 string aDSName( onFace ? "Nodes on Faces" : "Nodes on Edges");
3888 aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_INT32, aSize, 1 );
3889 aDataset->CreateOnDisk();
3890 aDataset->WriteOnDisk( aNodeIDs );
3891 aDataset->CloseOnDisk();
3894 aDSName = ( onFace ? "Face U positions" : "Edge positions");
3895 aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_FLOAT64, aSize, 1);
3896 aDataset->CreateOnDisk();
3897 aDataset->WriteOnDisk( aUPos );
3898 aDataset->CloseOnDisk();
3901 aDataset = new HDFdataset( "Face V positions", aGroup, HDF_FLOAT64, aSize, 1);
3902 aDataset->CreateOnDisk();
3903 aDataset->WriteOnDisk( aVPos );
3904 aDataset->CloseOnDisk();
3909 if ( aVPos ) delete [] aVPos;
3911 } // treat positions on edges or faces
3913 // close "Node Positions" group
3914 aGroup->CloseOnDisk();
3916 } // if ( there are submeshes in SMESHDS_Mesh )
3919 // close mesh HDF group
3920 aTopGroup->CloseOnDisk();
3928 aFile->CloseOnDisk();
3931 // Convert temporary files to stream
3932 aStreamFile = SALOMEDS_Tool::PutFilesToStream( tmpDir.ToCString(), aFileSeq.in(), isMultiFile );
3934 // Remove temporary files and directory
3936 SALOMEDS_Tool::RemoveTemporaryFiles( tmpDir.ToCString(), aFileSeq.in(), true );
3938 return aStreamFile._retn();
3941 //=============================================================================
3943 * SMESH_Gen_i::SaveASCII
3945 * Save SMESH module's data in ASCII format
3947 //=============================================================================
3949 SALOMEDS::TMPFile* SMESH_Gen_i::SaveASCII( SALOMEDS::SComponent_ptr theComponent,
3951 bool isMultiFile ) {
3952 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::SaveASCII" );
3953 SALOMEDS::TMPFile_var aStreamFile = Save( theComponent, theURL, isMultiFile );
3954 return aStreamFile._retn();
3956 //after usual saving needs to encipher binary to text string
3957 //Any binary symbol will be represent as "|xx" () hexadecimal format number
3958 int size = aStreamFile.in().length();
3959 _CORBA_Octet* buffer = new _CORBA_Octet[size*3+1];
3960 for ( int i = 0; i < size; i++ )
3961 sprintf( (char*)&(buffer[i*3]), "|%02x", aStreamFile[i] );
3963 buffer[size * 3] = '\0';
3965 SALOMEDS::TMPFile_var anAsciiStreamFile = new SALOMEDS::TMPFile(size*3, size*3, buffer, 1);
3967 return anAsciiStreamFile._retn();
3970 //=============================================================================
3972 * SMESH_Gen_i::loadGeomData
3974 * Load GEOM module data
3976 //=============================================================================
3978 void SMESH_Gen_i::loadGeomData( SALOMEDS::SComponent_ptr theCompRoot )
3980 if ( theCompRoot->_is_nil() )
3983 SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow( theCompRoot->GetStudy() );
3984 if ( aStudy->_is_nil() )
3987 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
3988 aStudyBuilder->LoadWith( theCompRoot, GetGeomEngine() );
3991 //=============================================================================
3995 * Load SMESH module's data
3997 //=============================================================================
3999 bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
4000 const SALOMEDS::TMPFile& theStream,
4004 if ( theComponent->GetStudy()->StudyId() != GetCurrentStudyID() )
4005 SetCurrentStudy( theComponent->GetStudy() );
4007 /* if( !theComponent->_is_nil() )
4009 //SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow( theComponent->GetStudy() );
4010 if( !myCurrentStudy->FindComponent( "GEOM" )->_is_nil() )
4011 loadGeomData( myCurrentStudy->FindComponent( "GEOM" ) );
4014 StudyContext* myStudyContext = GetCurrentStudyContext();
4016 // Get temporary files location
4017 TCollection_AsciiString tmpDir =
4018 ( char* )( isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir().c_str() );
4020 // Convert the stream into sequence of files to process
4021 SALOMEDS::ListOfFileNames_var aFileSeq = SALOMEDS_Tool::PutStreamToFiles( theStream,
4024 TCollection_AsciiString aStudyName( "" );
4025 if ( isMultiFile ) {
4026 CORBA::String_var url = myCurrentStudy->URL();
4027 aStudyName = (char*)SALOMEDS_Tool::GetNameFromPath( url.in() ).c_str();
4029 // Set names of temporary files
4030 TCollection_AsciiString filename = tmpDir + aStudyName + "_SMESH.hdf";
4031 TCollection_AsciiString meshfile = tmpDir + aStudyName + "_SMESH_Mesh.med";
4035 HDFdataset* aDataset;
4036 HDFgroup* aTopGroup;
4038 HDFgroup* aSubGroup;
4039 HDFgroup* aSubSubGroup;
4042 // ---> open HDF file
4043 aFile = new HDFfile( (char*) filename.ToCString() );
4045 aFile->OpenOnDisk( HDF_RDONLY );
4047 catch ( HDFexception ) {
4048 INFOS( "Load(): " << filename << " not found!" );
4052 TPythonDump pd; // prevent dump during loading
4054 // DriverMED_R_SMESHDS_Mesh myReader;
4055 // myReader.SetFile( meshfile.ToCString() );
4057 // For PAL13473 ("Repetitive mesh") implementation.
4058 // New dependencies between SMESH objects are established:
4059 // now hypotheses can refer to meshes, shapes and other hypotheses.
4060 // To keep data consistent, the following order of data restoration
4062 // 1. Create hypotheses
4063 // 2. Create all meshes
4064 // 3. Load hypotheses' data
4067 list< pair< SMESH_Hypothesis_i*, string > > hypDataList;
4068 list< pair< SMESH_Mesh_i*, HDFgroup* > > meshGroupList;
4069 list< SMESH::Filter_var > filters;
4071 // get total number of top-level groups
4072 int aNbGroups = aFile->nInternalObjects();
4073 if ( aNbGroups > 0 ) {
4074 // --> in first turn we should read&create hypotheses
4075 if ( aFile->ExistInternalObject( "Hypotheses" ) ) {
4076 // open hypotheses root HDF group
4077 aTopGroup = new HDFgroup( "Hypotheses", aFile );
4078 aTopGroup->OpenOnDisk();
4080 // get number of hypotheses
4081 int aNbObjects = aTopGroup->nInternalObjects();
4082 for ( int j = 0; j < aNbObjects; j++ ) {
4083 // try to identify hypothesis
4084 char hypGrpName[ HDF_NAME_MAX_LEN+1 ];
4085 aTopGroup->InternalObjectIndentify( j, hypGrpName );
4087 if ( string( hypGrpName ).substr( 0, 10 ) == string( "Hypothesis" ) ) {
4088 // open hypothesis group
4089 aGroup = new HDFgroup( hypGrpName, aTopGroup );
4090 aGroup->OpenOnDisk();
4092 // --> get hypothesis id
4093 int id = atoi( string( hypGrpName ).substr( 10 ).c_str() );
4098 // get number of datasets
4099 int aNbSubObjects = aGroup->nInternalObjects();
4100 for ( int k = 0; k < aNbSubObjects; k++ ) {
4102 char name_of_subgroup[ HDF_NAME_MAX_LEN+1 ];
4103 aGroup->InternalObjectIndentify( k, name_of_subgroup );
4104 // --> get hypothesis name
4105 if ( strcmp( name_of_subgroup, "Name" ) == 0 ) {
4106 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4107 aDataset->OpenOnDisk();
4108 size = aDataset->GetSize();
4109 char* hypname_str = new char[ size ];
4110 aDataset->ReadFromDisk( hypname_str );
4111 hypname = string( hypname_str );
4112 delete [] hypname_str;
4113 aDataset->CloseOnDisk();
4115 // --> get hypothesis plugin library name
4116 if ( strcmp( name_of_subgroup, "LibName" ) == 0 ) {
4117 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4118 aDataset->OpenOnDisk();
4119 size = aDataset->GetSize();
4120 char* libname_str = new char[ size ];
4121 aDataset->ReadFromDisk( libname_str );
4122 if(MYDEBUG) SCRUTE( libname_str );
4123 libname = string( libname_str );
4124 delete [] libname_str;
4125 aDataset->CloseOnDisk();
4127 // --> get hypothesis data
4128 if ( strcmp( name_of_subgroup, "Data" ) == 0 ) {
4129 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4130 aDataset->OpenOnDisk();
4131 size = aDataset->GetSize();
4132 char* hypdata_str = new char[ size ];
4133 aDataset->ReadFromDisk( hypdata_str );
4134 hypdata = string( hypdata_str );
4135 delete [] hypdata_str;
4136 aDataset->CloseOnDisk();
4139 // close hypothesis HDF group
4140 aGroup->CloseOnDisk();
4142 // --> restore hypothesis from data
4143 if ( id > 0 && !hypname.empty()/* && !hypdata.empty()*/ ) { // VSR : persistent data can be empty
4144 if(MYDEBUG) MESSAGE("VSR - load hypothesis : id = " << id <<
4145 ", name = " << hypname.c_str() << ", persistent string = " << hypdata.c_str());
4146 SMESH::SMESH_Hypothesis_var myHyp;
4148 try { // protect persistence mechanism against exceptions
4149 myHyp = this->createHypothesis( hypname.c_str(), libname.c_str() );
4152 INFOS( "Exception during hypothesis creation" );
4155 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
4157 // myImpl->LoadFrom( hypdata.c_str() );
4158 hypDataList.push_back( make_pair( myImpl, hypdata ));
4159 CORBA::String_var iorString = GetORB()->object_to_string( myHyp );
4160 int newId = myStudyContext->findId( iorString.in() );
4161 myStudyContext->mapOldToNew( id, newId );
4164 if(MYDEBUG) MESSAGE( "VSR - SMESH_Gen::Load - can't get servant" );
4168 // close hypotheses root HDF group
4169 aTopGroup->CloseOnDisk();
4173 // --> then we should read&create algorithms
4174 if ( aFile->ExistInternalObject( "Algorithms" ) ) {
4175 // open algorithms root HDF group
4176 aTopGroup = new HDFgroup( "Algorithms", aFile );
4177 aTopGroup->OpenOnDisk();
4179 // get number of algorithms
4180 int aNbObjects = aTopGroup->nInternalObjects();
4181 for ( int j = 0; j < aNbObjects; j++ ) {
4182 // try to identify algorithm
4183 char hypGrpName[ HDF_NAME_MAX_LEN+1 ];
4184 aTopGroup->InternalObjectIndentify( j, hypGrpName );
4186 if ( string( hypGrpName ).substr( 0, 9 ) == string( "Algorithm" ) ) {
4187 // open algorithm group
4188 aGroup = new HDFgroup( hypGrpName, aTopGroup );
4189 aGroup->OpenOnDisk();
4191 // --> get algorithm id
4192 int id = atoi( string( hypGrpName ).substr( 9 ).c_str() );
4197 // get number of datasets
4198 int aNbSubObjects = aGroup->nInternalObjects();
4199 for ( int k = 0; k < aNbSubObjects; k++ ) {
4201 char name_of_subgroup[ HDF_NAME_MAX_LEN+1 ];
4202 aGroup->InternalObjectIndentify( k, name_of_subgroup );
4203 // --> get algorithm name
4204 if ( strcmp( name_of_subgroup, "Name" ) == 0 ) {
4205 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4206 aDataset->OpenOnDisk();
4207 size = aDataset->GetSize();
4208 char* hypname_str = new char[ size ];
4209 aDataset->ReadFromDisk( hypname_str );
4210 hypname = string( hypname_str );
4211 delete [] hypname_str;
4212 aDataset->CloseOnDisk();
4214 // --> get algorithm plugin library name
4215 if ( strcmp( name_of_subgroup, "LibName" ) == 0 ) {
4216 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4217 aDataset->OpenOnDisk();
4218 size = aDataset->GetSize();
4219 char* libname_str = new char[ size ];
4220 aDataset->ReadFromDisk( libname_str );
4221 if(MYDEBUG) SCRUTE( libname_str );
4222 libname = string( libname_str );
4223 delete [] libname_str;
4224 aDataset->CloseOnDisk();
4226 // --> get algorithm data
4227 if ( strcmp( name_of_subgroup, "Data" ) == 0 ) {
4228 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4229 aDataset->OpenOnDisk();
4230 size = aDataset->GetSize();
4231 char* hypdata_str = new char[ size ];
4232 aDataset->ReadFromDisk( hypdata_str );
4233 if(MYDEBUG) SCRUTE( hypdata_str );
4234 hypdata = string( hypdata_str );
4235 delete [] hypdata_str;
4236 aDataset->CloseOnDisk();
4239 // close algorithm HDF group
4240 aGroup->CloseOnDisk();
4242 // --> restore algorithm from data
4243 if ( id > 0 && !hypname.empty()/* && !hypdata.empty()*/ ) { // VSR : persistent data can be empty
4244 if(MYDEBUG) MESSAGE("VSR - load algo : id = " << id <<
4245 ", name = " << hypname.c_str() << ", persistent string = " << hypdata.c_str());
4246 SMESH::SMESH_Hypothesis_var myHyp;
4248 try { // protect persistence mechanism against exceptions
4249 myHyp = this->createHypothesis( hypname.c_str(), libname.c_str() );
4252 INFOS( "Exception during hypothesis creation" );
4255 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
4257 //myImpl->LoadFrom( hypdata.c_str() );
4258 hypDataList.push_back( make_pair( myImpl, hypdata ));
4259 CORBA::String_var iorString = GetORB()->object_to_string( myHyp );
4260 int newId = myStudyContext->findId( iorString.in() );
4261 myStudyContext->mapOldToNew( id, newId );
4264 if(MYDEBUG) MESSAGE( "VSR - SMESH_Gen::Load - can't get servant" );
4268 // close algorithms root HDF group
4269 aTopGroup->CloseOnDisk();
4273 // --> the rest groups should be meshes
4274 for ( int i = 0; i < aNbGroups; i++ ) {
4275 // identify next group
4276 char meshName[ HDF_NAME_MAX_LEN+1 ];
4277 aFile->InternalObjectIndentify( i, meshName );
4279 if ( string( meshName ).substr( 0, 4 ) == string( "Mesh" ) ) {
4281 int id = atoi( string( meshName ).substr( 4 ).c_str() );
4285 // open mesh HDF group
4286 aTopGroup = new HDFgroup( meshName, aFile );
4287 aTopGroup->OpenOnDisk();
4289 // get number of child HDF objects
4290 int aNbObjects = aTopGroup->nInternalObjects();
4291 if ( aNbObjects > 0 ) {
4293 if(MYDEBUG) MESSAGE( "VSR - load mesh : id = " << id );
4294 SMESH::SMESH_Mesh_var myNewMesh = this->createMesh();
4295 SMESH_Mesh_i* myNewMeshImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( myNewMesh ).in() );
4296 if ( !myNewMeshImpl )
4298 meshGroupList.push_back( make_pair( myNewMeshImpl, aTopGroup ));
4300 CORBA::String_var iorString = GetORB()->object_to_string( myNewMesh );
4301 int newId = myStudyContext->findId( iorString.in() );
4302 myStudyContext->mapOldToNew( id, newId );
4305 // try to read and set auto color flag
4306 char aMeshAutoColorName[ 30 ];
4307 sprintf( aMeshAutoColorName, "AutoColorMesh %d", id);
4308 if( aTopGroup->ExistInternalObject( aMeshAutoColorName ) )
4310 aDataset = new HDFdataset( aMeshAutoColorName, aTopGroup );
4311 aDataset->OpenOnDisk();
4312 size = aDataset->GetSize();
4313 int* anAutoColor = new int[ size ];
4314 aDataset->ReadFromDisk( anAutoColor );
4315 aDataset->CloseOnDisk();
4316 myNewMeshImpl->GetImpl().SetAutoColor( (bool)anAutoColor[0] );
4317 delete [] anAutoColor;
4320 // try to read and set reference to shape
4321 GEOM::GEOM_Object_var aShapeObject;
4322 if ( aTopGroup->ExistInternalObject( "Ref on shape" ) ) {
4323 // load mesh "Ref on shape" - it's an entry to SObject
4324 aDataset = new HDFdataset( "Ref on shape", aTopGroup );
4325 aDataset->OpenOnDisk();
4326 size = aDataset->GetSize();
4327 char* refFromFile = new char[ size ];
4328 aDataset->ReadFromDisk( refFromFile );
4329 aDataset->CloseOnDisk();
4330 if ( strlen( refFromFile ) > 0 ) {
4331 SALOMEDS::SObject_wrap shapeSO = myCurrentStudy->FindObjectID( refFromFile );
4333 // Make sure GEOM data are loaded first
4334 //loadGeomData( shapeSO->GetFatherComponent() );
4336 CORBA::Object_var shapeObject = SObjectToObject( shapeSO );
4337 if ( !CORBA::is_nil( shapeObject ) ) {
4338 aShapeObject = GEOM::GEOM_Object::_narrow( shapeObject );
4339 if ( !aShapeObject->_is_nil() )
4340 myNewMeshImpl->SetShape( aShapeObject );
4343 delete [] refFromFile;
4346 // issue 20918. Restore Persistent Id of SMESHDS_Mesh
4347 if( aTopGroup->ExistInternalObject( "meshPersistentId" ) )
4349 aDataset = new HDFdataset( "meshPersistentId", aTopGroup );
4350 aDataset->OpenOnDisk();
4351 size = aDataset->GetSize();
4352 int* meshPersistentId = new int[ size ];
4353 aDataset->ReadFromDisk( meshPersistentId );
4354 aDataset->CloseOnDisk();
4355 myNewMeshImpl->GetImpl().GetMeshDS()->SetPersistentId( *meshPersistentId );
4356 delete [] meshPersistentId;
4362 // As all object that can be referred by hypothesis are created,
4363 // we can restore hypothesis data
4365 list< pair< SMESH_Hypothesis_i*, string > >::iterator hyp_data;
4366 for ( hyp_data = hypDataList.begin(); hyp_data != hypDataList.end(); ++hyp_data )
4368 SMESH_Hypothesis_i* hyp = hyp_data->first;
4369 string & data = hyp_data->second;
4370 hyp->LoadFrom( data.c_str() );
4373 // Restore the rest mesh data
4375 list< pair< SMESH_Mesh_i*, HDFgroup* > >::iterator meshi_group;
4376 for ( meshi_group = meshGroupList.begin(); meshi_group != meshGroupList.end(); ++meshi_group )
4378 aTopGroup = meshi_group->second;
4379 SMESH_Mesh_i* myNewMeshImpl = meshi_group->first;
4380 //::SMESH_Mesh& myLocMesh = myNewMeshImpl->GetImpl();
4381 //SMESHDS_Mesh* mySMESHDSMesh = myLocMesh.GetMeshDS();
4383 GEOM::GEOM_Object_var aShapeObject = myNewMeshImpl->GetShapeToMesh();
4384 bool hasData = false;
4387 CORBA::String_var iorString = GetORB()->object_to_string( myNewMeshImpl->_this() );
4388 int newId = myStudyContext->findId( iorString.in() );
4389 int id = myStudyContext->getOldId( newId );
4391 // try to find mesh data dataset
4392 if ( aTopGroup->ExistInternalObject( "Has data" ) ) {
4393 // load mesh "has data" flag
4394 aDataset = new HDFdataset( "Has data", aTopGroup );
4395 aDataset->OpenOnDisk();
4396 size = aDataset->GetSize();
4397 char* strHasData = new char[ size ];
4398 aDataset->ReadFromDisk( strHasData );
4399 aDataset->CloseOnDisk();
4400 if ( strcmp( strHasData, "1") == 0 ) {
4401 // read mesh data from MED file
4402 // myReader.SetMesh( mySMESHDSMesh );
4403 // myReader.SetMeshId( id );
4404 // myReader.Perform();
4407 delete [] strHasData;
4410 // Try to get applied ALGORITHMS (mesh is not cleared by algo addition because
4411 // nodes and elements are not yet put into sub-meshes)
4412 if ( aTopGroup->ExistInternalObject( "Applied Algorithms" ) ) {
4413 aGroup = new HDFgroup( "Applied Algorithms", aTopGroup );
4414 aGroup->OpenOnDisk();
4415 // get number of applied algorithms
4416 int aNbSubObjects = aGroup->nInternalObjects();
4417 if(MYDEBUG) MESSAGE( "VSR - number of applied algos " << aNbSubObjects );
4418 for ( int j = 0; j < aNbSubObjects; j++ ) {
4419 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4420 aGroup->InternalObjectIndentify( j, name_dataset );
4421 // check if it is an algorithm
4422 if ( string( name_dataset ).substr( 0, 4 ) == string( "Algo" ) ) {
4423 aDataset = new HDFdataset( name_dataset, aGroup );
4424 aDataset->OpenOnDisk();
4425 size = aDataset->GetSize();
4426 char* refFromFile = new char[ size ];
4427 aDataset->ReadFromDisk( refFromFile );
4428 aDataset->CloseOnDisk();
4429 // san - it is impossible to recover applied algorithms using their entries within Load() method
4430 //SALOMEDS::SObject_wrap hypSO = myCurrentStudy->FindObjectID( refFromFile );
4431 //CORBA::Object_var hypObject = SObjectToObject( hypSO );
4432 int id = atoi( refFromFile );
4433 delete [] refFromFile;
4434 string anIOR = myStudyContext->getIORbyOldId( id );
4435 if ( !anIOR.empty() ) {
4436 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
4437 if ( !CORBA::is_nil( hypObject ) ) {
4438 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
4439 if ( !anHyp->_is_nil() && (!aShapeObject->_is_nil()
4440 || !myNewMeshImpl->HasShapeToMesh()) )
4441 myNewMeshImpl->addHypothesis( aShapeObject, anHyp );
4446 aGroup->CloseOnDisk();
4449 // try to get applied hypotheses
4450 if ( aTopGroup->ExistInternalObject( "Applied Hypotheses" ) ) {
4451 aGroup = new HDFgroup( "Applied Hypotheses", aTopGroup );
4452 aGroup->OpenOnDisk();
4453 // get number of applied hypotheses
4454 int aNbSubObjects = aGroup->nInternalObjects();
4455 for ( int j = 0; j < aNbSubObjects; j++ ) {
4456 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4457 aGroup->InternalObjectIndentify( j, name_dataset );
4458 // check if it is a hypothesis
4459 if ( string( name_dataset ).substr( 0, 3 ) == string( "Hyp" ) ) {
4460 aDataset = new HDFdataset( name_dataset, aGroup );
4461 aDataset->OpenOnDisk();
4462 size = aDataset->GetSize();
4463 char* refFromFile = new char[ size ];
4464 aDataset->ReadFromDisk( refFromFile );
4465 aDataset->CloseOnDisk();
4466 // san - it is impossible to recover applied hypotheses using their entries within Load() method
4467 //SALOMEDS::SObject_wrap hypSO = myCurrentStudy->FindObjectID( refFromFile );
4468 //CORBA::Object_var hypObject = SObjectToObject( hypSO );
4469 int id = atoi( refFromFile );
4470 delete [] refFromFile;
4471 string anIOR = myStudyContext->getIORbyOldId( id );
4472 if ( !anIOR.empty() ) {
4473 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
4474 if ( !CORBA::is_nil( hypObject ) ) {
4475 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
4476 if ( !anHyp->_is_nil() && (!aShapeObject->_is_nil()
4477 || !myNewMeshImpl->HasShapeToMesh()) )
4478 myNewMeshImpl->addHypothesis( aShapeObject, anHyp );
4483 aGroup->CloseOnDisk();
4486 // --> try to find SUB-MESHES containers for each type of submesh
4487 for ( int j = GetSubMeshOnVertexTag(); j <= GetSubMeshOnCompoundTag(); j++ ) {
4488 const char* name_meshgroup = 0;
4489 if ( j == GetSubMeshOnVertexTag() )
4490 name_meshgroup = "SubMeshes On Vertex";
4491 else if ( j == GetSubMeshOnEdgeTag() )
4492 name_meshgroup = "SubMeshes On Edge";
4493 else if ( j == GetSubMeshOnWireTag() )
4494 name_meshgroup = "SubMeshes On Wire";
4495 else if ( j == GetSubMeshOnFaceTag() )
4496 name_meshgroup = "SubMeshes On Face";
4497 else if ( j == GetSubMeshOnShellTag() )
4498 name_meshgroup = "SubMeshes On Shell";
4499 else if ( j == GetSubMeshOnSolidTag() )
4500 name_meshgroup = "SubMeshes On Solid";
4501 else if ( j == GetSubMeshOnCompoundTag() )
4502 name_meshgroup = "SubMeshes On Compound";
4504 // try to get submeshes container HDF group
4505 if ( aTopGroup->ExistInternalObject( name_meshgroup ) ) {
4506 // open submeshes containers HDF group
4507 aGroup = new HDFgroup( name_meshgroup, aTopGroup );
4508 aGroup->OpenOnDisk();
4510 // get number of submeshes
4511 int aNbSubMeshes = aGroup->nInternalObjects();
4512 for ( int k = 0; k < aNbSubMeshes; k++ ) {
4514 char name_submeshgroup[ HDF_NAME_MAX_LEN+1 ];
4515 aGroup->InternalObjectIndentify( k, name_submeshgroup );
4516 if ( strncmp( name_submeshgroup, "SubMesh", 7 ) == 0 ) {
4517 // --> get submesh id
4518 int subid = atoi( name_submeshgroup + 7 );
4521 // open submesh HDF group
4522 aSubGroup = new HDFgroup( name_submeshgroup, aGroup );
4523 aSubGroup->OpenOnDisk();
4525 // try to read and set reference to subshape
4526 GEOM::GEOM_Object_var aSubShapeObject;
4527 SMESH::SMESH_subMesh_var aSubMesh;
4529 if ( aSubGroup->ExistInternalObject( "Ref on shape" ) ) {
4530 // load submesh "Ref on shape" - it's an entry to SObject
4531 aDataset = new HDFdataset( "Ref on shape", aSubGroup );
4532 aDataset->OpenOnDisk();
4533 size = aDataset->GetSize();
4534 char* refFromFile = new char[ size ];
4535 aDataset->ReadFromDisk( refFromFile );
4536 aDataset->CloseOnDisk();
4537 if ( strlen( refFromFile ) > 0 ) {
4538 SALOMEDS::SObject_wrap subShapeSO = myCurrentStudy->FindObjectID( refFromFile );
4539 CORBA::Object_var subShapeObject = SObjectToObject( subShapeSO );
4540 if ( !CORBA::is_nil( subShapeObject ) ) {
4541 aSubShapeObject = GEOM::GEOM_Object::_narrow( subShapeObject );
4542 if ( !aSubShapeObject->_is_nil() )
4543 aSubMesh = SMESH::SMESH_subMesh::_duplicate
4544 ( myNewMeshImpl->createSubMesh( aSubShapeObject ) );
4545 if ( aSubMesh->_is_nil() )
4547 string iorSubString = GetORB()->object_to_string( aSubMesh );
4548 int newSubId = myStudyContext->findId( iorSubString );
4549 myStudyContext->mapOldToNew( subid, newSubId );
4554 if ( aSubMesh->_is_nil() )
4557 // try to get applied algorithms
4558 if ( aSubGroup->ExistInternalObject( "Applied Algorithms" ) ) {
4559 // open "applied algorithms" HDF group
4560 aSubSubGroup = new HDFgroup( "Applied Algorithms", aSubGroup );
4561 aSubSubGroup->OpenOnDisk();
4562 // get number of applied algorithms
4563 int aNbSubObjects = aSubSubGroup->nInternalObjects();
4564 for ( int l = 0; l < aNbSubObjects; l++ ) {
4565 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4566 aSubSubGroup->InternalObjectIndentify( l, name_dataset );
4567 // check if it is an algorithm
4568 if ( strncmp( name_dataset, "Algo", 4 ) == 0 ) {
4569 aDataset = new HDFdataset( name_dataset, aSubSubGroup );
4570 aDataset->OpenOnDisk();
4571 size = aDataset->GetSize();
4572 char* refFromFile = new char[ size ];
4573 aDataset->ReadFromDisk( refFromFile );
4574 aDataset->CloseOnDisk();
4576 int id = atoi( refFromFile );
4577 string anIOR = myStudyContext->getIORbyOldId( id );
4578 if ( !anIOR.empty() ) {
4579 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
4580 if ( !CORBA::is_nil( hypObject ) ) {
4581 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
4582 if ( !anHyp->_is_nil() && !aShapeObject->_is_nil() )
4583 myNewMeshImpl->addHypothesis( aSubShapeObject, anHyp );
4588 // close "applied algorithms" HDF group
4589 aSubSubGroup->CloseOnDisk();
4592 // try to get applied hypotheses
4593 if ( aSubGroup->ExistInternalObject( "Applied Hypotheses" ) ) {
4594 // open "applied hypotheses" HDF group
4595 aSubSubGroup = new HDFgroup( "Applied Hypotheses", aSubGroup );
4596 aSubSubGroup->OpenOnDisk();
4597 // get number of applied hypotheses
4598 int aNbSubObjects = aSubSubGroup->nInternalObjects();
4599 for ( int l = 0; l < aNbSubObjects; l++ ) {
4600 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4601 aSubSubGroup->InternalObjectIndentify( l, name_dataset );
4602 // check if it is a hypothesis
4603 if ( string( name_dataset ).substr( 0, 3 ) == string( "Hyp" ) ) {
4604 aDataset = new HDFdataset( name_dataset, aSubSubGroup );
4605 aDataset->OpenOnDisk();
4606 size = aDataset->GetSize();
4607 char* refFromFile = new char[ size ];
4608 aDataset->ReadFromDisk( refFromFile );
4609 aDataset->CloseOnDisk();
4611 int id = atoi( refFromFile );
4612 string anIOR = myStudyContext->getIORbyOldId( id );
4613 if ( !anIOR.empty() ) {
4614 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
4615 if ( !CORBA::is_nil( hypObject ) ) {
4616 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
4617 if ( !anHyp->_is_nil() && !aShapeObject->_is_nil() )
4618 myNewMeshImpl->addHypothesis( aSubShapeObject, anHyp );
4623 // close "APPLIED HYPOTHESES" hdf group
4624 aSubSubGroup->CloseOnDisk();
4627 // close SUB-MESH hdf group
4628 aSubGroup->CloseOnDisk();
4631 // close SUB-MESHES containers hdf group
4632 aGroup->CloseOnDisk();
4636 // try to get GROUPS
4637 for ( int ii = GetNodeGroupsTag(); ii <= GetBallElementsGroupsTag(); ii++ ) {
4638 char name_group[ 30 ];
4639 if ( ii == GetNodeGroupsTag() )
4640 strcpy( name_group, "Groups of Nodes" );
4641 else if ( ii == GetEdgeGroupsTag() )
4642 strcpy( name_group, "Groups of Edges" );
4643 else if ( ii == GetFaceGroupsTag() )
4644 strcpy( name_group, "Groups of Faces" );
4645 else if ( ii == GetVolumeGroupsTag() )
4646 strcpy( name_group, "Groups of Volumes" );
4647 else if ( ii == Get0DElementsGroupsTag() )
4648 strcpy( name_group, "Groups of 0D Elements" );
4649 else if ( ii == GetBallElementsGroupsTag() )
4650 strcpy( name_group, "Groups of Balls" );
4652 if ( aTopGroup->ExistInternalObject( name_group ) ) {
4653 aGroup = new HDFgroup( name_group, aTopGroup );
4654 aGroup->OpenOnDisk();
4655 // get number of groups
4656 int aNbSubObjects = aGroup->nInternalObjects();
4657 for ( int j = 0; j < aNbSubObjects; j++ ) {
4658 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4659 aGroup->InternalObjectIndentify( j, name_dataset );
4660 // check if it is an group
4661 if ( string( name_dataset ).substr( 0, 5 ) == string( "Group" ) ) {
4663 int subid = atoi( string( name_dataset ).substr( 5 ).c_str() );
4666 aDataset = new HDFdataset( name_dataset, aGroup );
4667 aDataset->OpenOnDisk();
4669 // Retrieve actual group name
4670 size = aDataset->GetSize();
4671 char* nameFromFile = new char[ size ];
4672 aDataset->ReadFromDisk( nameFromFile );
4673 aDataset->CloseOnDisk();
4675 // Try to find a shape reference
4676 TopoDS_Shape aShape;
4677 char aRefName[ 30 ];
4678 sprintf( aRefName, "Ref on shape %d", subid);
4679 if ( aGroup->ExistInternalObject( aRefName ) ) {
4680 // load mesh "Ref on shape" - it's an entry to SObject
4681 aDataset = new HDFdataset( aRefName, aGroup );
4682 aDataset->OpenOnDisk();
4683 size = aDataset->GetSize();
4684 char* refFromFile = new char[ size ];
4685 aDataset->ReadFromDisk( refFromFile );
4686 aDataset->CloseOnDisk();
4687 if ( strlen( refFromFile ) > 0 ) {
4688 SALOMEDS::SObject_wrap shapeSO = myCurrentStudy->FindObjectID( refFromFile );
4689 CORBA::Object_var shapeObject = SObjectToObject( shapeSO );
4690 if ( !CORBA::is_nil( shapeObject ) ) {
4691 aShapeObject = GEOM::GEOM_Object::_narrow( shapeObject );
4692 if ( !aShapeObject->_is_nil() )
4693 aShape = GeomObjectToShape( aShapeObject );
4697 // Try to read a filter of SMESH_GroupOnFilter
4698 SMESH::Filter_var filter;
4699 SMESH_PredicatePtr predicate;
4700 std::string hdfGrpName = "Filter " + SMESH_Comment(subid);
4701 if ( aGroup->ExistInternalObject( hdfGrpName.c_str() ))
4703 aDataset = new HDFdataset( hdfGrpName.c_str(), aGroup );
4704 aDataset->OpenOnDisk();
4705 size = aDataset->GetSize();
4706 char* persistStr = new char[ size ];
4707 aDataset->ReadFromDisk( persistStr );
4708 aDataset->CloseOnDisk();
4709 if ( strlen( persistStr ) > 0 ) {
4710 filter = SMESH_GroupOnFilter_i::StringToFilter( persistStr );
4711 predicate = SMESH_GroupOnFilter_i::GetPredicate( filter );
4712 filters.push_back( filter );
4716 // Create group servant
4717 SMESH::ElementType type = (SMESH::ElementType)(ii - GetNodeGroupsTag() + 1);
4718 SMESH::SMESH_GroupBase_var aNewGroup = SMESH::SMESH_GroupBase::_duplicate
4719 ( myNewMeshImpl->createGroup( type, nameFromFile, aShape, predicate ) );
4720 // Obtain a SMESHDS_Group object
4721 if ( aNewGroup->_is_nil() )
4724 string iorSubString = GetORB()->object_to_string( aNewGroup );
4725 int newSubId = myStudyContext->findId( iorSubString );
4726 myStudyContext->mapOldToNew( subid, newSubId );
4728 SMESH_GroupBase_i* aGroupImpl = SMESH::DownCast< SMESH_GroupBase_i*>( aNewGroup );
4732 if ( SMESH_GroupOnFilter_i* aFilterGroup =
4733 dynamic_cast< SMESH_GroupOnFilter_i*>( aGroupImpl ))
4735 aFilterGroup->SetFilter( filter );
4736 filter->UnRegister();
4738 SMESHDS_GroupBase* aGroupBaseDS = aGroupImpl->GetGroupDS();
4739 if ( !aGroupBaseDS )
4742 aGroupBaseDS->SetStoreName( name_dataset );
4745 // Read color of the group
4746 char aGroupColorName[ 30 ];
4747 sprintf( aGroupColorName, "ColorGroup %d", subid);
4748 if ( aGroup->ExistInternalObject( aGroupColorName ) )
4750 aDataset = new HDFdataset( aGroupColorName, aGroup );
4751 aDataset->OpenOnDisk();
4752 size = aDataset->GetSize();
4753 double* anRGB = new double[ size ];
4754 aDataset->ReadFromDisk( anRGB );
4755 aDataset->CloseOnDisk();
4756 Quantity_Color aColor( anRGB[0], anRGB[1], anRGB[2], Quantity_TOC_RGB );
4757 aGroupBaseDS->SetColor( aColor );
4761 aGroup->CloseOnDisk();
4765 // instead of reading mesh data, we read only brief information of all
4766 // objects: mesh, groups, sub-meshes (issue 0021208 )
4769 SMESH_PreMeshInfo::LoadFromFile( myNewMeshImpl, id,
4770 meshfile.ToCString(), filename.ToCString(),
4774 // read Sub-Mesh ORDER if any
4775 if( aTopGroup->ExistInternalObject( "Mesh Order" ) ) {
4776 aDataset = new HDFdataset( "Mesh Order", aTopGroup );
4777 aDataset->OpenOnDisk();
4778 size = aDataset->GetSize();
4779 int* smIDs = new int[ size ];
4780 aDataset->ReadFromDisk( smIDs );
4781 aDataset->CloseOnDisk();
4782 TListOfListOfInt anOrderIds;
4783 anOrderIds.push_back( TListOfInt() );
4784 for ( int i = 0; i < size; i++ )
4785 if ( smIDs[ i ] < 0 ) // is separator
4786 anOrderIds.push_back( TListOfInt() );
4788 anOrderIds.back().push_back(smIDs[ i ]);
4790 myNewMeshImpl->GetImpl().SetMeshOrder( anOrderIds );
4794 // update hyps needing full mesh data restored (issue 20918)
4795 for ( hyp_data = hypDataList.begin(); hyp_data != hypDataList.end(); ++hyp_data )
4797 SMESH_Hypothesis_i* hyp = hyp_data->first;
4798 hyp->UpdateAsMeshesRestored();
4801 // notify algos on completed restoration to set sub-mesh event listeners
4802 for ( meshi_group = meshGroupList.begin(); meshi_group != meshGroupList.end(); ++meshi_group )
4804 SMESH_Mesh_i* myNewMeshImpl = meshi_group->first;
4805 ::SMESH_Mesh& myLocMesh = myNewMeshImpl->GetImpl();
4807 TopoDS_Shape myLocShape;
4808 if(myLocMesh.HasShapeToMesh())
4809 myLocShape = myLocMesh.GetShapeToMesh();
4811 myLocShape = SMESH_Mesh::PseudoShape();
4813 myLocMesh.GetSubMesh(myLocShape)->
4814 ComputeStateEngine (SMESH_subMesh::SUBMESH_RESTORED);
4817 // let filters detect dependency on mesh groups via FT_BelongToMeshGroup predicate (22877)
4818 list< SMESH::Filter_var >::iterator f = filters.begin();
4819 for ( ; f != filters.end(); ++f )
4820 if ( SMESH::Filter_i * fi = SMESH::DownCast< SMESH::Filter_i*>( *f ))
4821 fi->FindBaseObjects();
4826 aTopGroup->CloseOnDisk();
4829 aFile->CloseOnDisk();
4832 // Remove temporary files created from the stream
4835 SMESH_File meshFile( meshfile.ToCString() );
4836 if ( !meshFile ) // no meshfile exists
4838 SALOMEDS_Tool::RemoveTemporaryFiles( tmpDir.ToCString(), aFileSeq.in(), true );
4842 Engines::Container_var container = GetContainerRef();
4843 if ( Engines_Container_i* container_i = SMESH::DownCast<Engines_Container_i*>( container ))
4845 container_i->registerTemporaryFile( filename.ToCString() );
4846 container_i->registerTemporaryFile( meshfile.ToCString() );
4847 container_i->registerTemporaryFile( tmpDir.ToCString() );
4851 pd << ""; // prevent optimizing pd out
4853 // creation of tree nodes for all data objects in the study
4854 // to support tree representation customization and drag-n-drop:
4855 SALOMEDS::Study_var study = theComponent->GetStudy();
4856 SALOMEDS::UseCaseBuilder_wrap useCaseBuilder = study->GetUseCaseBuilder();
4857 if ( !useCaseBuilder->IsUseCaseNode( theComponent ) ) {
4858 useCaseBuilder->SetRootCurrent();
4859 useCaseBuilder->Append( theComponent ); // component object is added as the top level item
4860 SALOMEDS::ChildIterator_wrap it = study->NewChildIterator( theComponent );
4861 for (it->InitEx(true); it->More(); it->Next()) {
4862 useCaseBuilder->AppendTo( it->Value()->GetFather(), it->Value() );
4869 //=============================================================================
4871 * SMESH_Gen_i::LoadASCII
4873 * Load SMESH module's data in ASCII format
4875 //=============================================================================
4877 bool SMESH_Gen_i::LoadASCII( SALOMEDS::SComponent_ptr theComponent,
4878 const SALOMEDS::TMPFile& theStream,
4880 bool isMultiFile ) {
4881 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::LoadASCII" );
4882 return Load( theComponent, theStream, theURL, isMultiFile );
4884 //before call main ::Load method it's need for decipher text format to
4885 //binary ( "|xx" => x' )
4886 int size = theStream.length();
4887 if ( int((size / 3 )*3) != size ) //error size of buffer
4890 int real_size = int(size / 3);
4892 _CORBA_Octet* buffer = new _CORBA_Octet[real_size];
4895 unsigned int c = -1;
4896 for ( int i = 0; i < real_size; i++ )
4898 memcpy( &(tmp[0]), &(theStream[i*3+1]), 2 );
4899 sscanf( tmp, "%x", &c );
4900 sprintf( (char*)&(buffer[i]), "%c", (char)c );
4903 SALOMEDS::TMPFile_var aRealStreamFile = new SALOMEDS::TMPFile(real_size, real_size, buffer, 1);
4905 return Load( theComponent, *(aRealStreamFile._retn()), theURL, isMultiFile );
4908 //=============================================================================
4910 * SMESH_Gen_i::Close
4912 * Clears study-connected data when it is closed
4914 //=============================================================================
4916 void SMESH_Gen_i::Close( SALOMEDS::SComponent_ptr theComponent )
4918 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Close" );
4920 // set correct current study
4921 SALOMEDS::Study_var study = theComponent->GetStudy();
4922 if ( study->StudyId() != GetCurrentStudyID())
4923 setCurrentStudy( study, /*IsBeingClosed=*/true );
4925 // Clear study contexts data
4926 int studyId = GetCurrentStudyID();
4927 if ( myStudyContextMap.find( studyId ) != myStudyContextMap.end() ) {
4928 delete myStudyContextMap[ studyId ];
4929 myStudyContextMap.erase( studyId );
4932 // remove the tmp files meshes are loaded from
4933 SMESH_PreMeshInfo::RemoveStudyFiles_TMP_METHOD( theComponent );
4935 myCurrentStudy = SALOMEDS::Study::_nil();
4939 //=============================================================================
4941 * SMESH_Gen_i::ComponentDataType
4943 * Get component data type
4945 //=============================================================================
4947 char* SMESH_Gen_i::ComponentDataType()
4949 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::ComponentDataType" );
4950 return CORBA::string_dup( "SMESH" );
4954 //=============================================================================
4956 * SMESH_Gen_i::IORToLocalPersistentID
4958 * Transform data from transient form to persistent
4960 //=============================================================================
4962 char* SMESH_Gen_i::IORToLocalPersistentID( SALOMEDS::SObject_ptr /*theSObject*/,
4963 const char* IORString,
4964 CORBA::Boolean /*isMultiFile*/,
4965 CORBA::Boolean /*isASCII*/ )
4967 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::IORToLocalPersistentID" );
4968 StudyContext* myStudyContext = GetCurrentStudyContext();
4970 if ( myStudyContext && strcmp( IORString, "" ) != 0 ) {
4971 int anId = myStudyContext->findId( IORString );
4973 if(MYDEBUG) MESSAGE( "VSR " << anId )
4975 sprintf( strId, "%d", anId );
4976 return CORBA::string_dup( strId );
4979 return CORBA::string_dup( "" );
4982 //=============================================================================
4984 * SMESH_Gen_i::LocalPersistentIDToIOR
4986 * Transform data from persistent form to transient
4988 //=============================================================================
4990 char* SMESH_Gen_i::LocalPersistentIDToIOR( SALOMEDS::SObject_ptr /*theSObject*/,
4991 const char* aLocalPersistentID,
4992 CORBA::Boolean /*isMultiFile*/,
4993 CORBA::Boolean /*isASCII*/ )
4995 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::LocalPersistentIDToIOR(): id = " << aLocalPersistentID );
4996 StudyContext* myStudyContext = GetCurrentStudyContext();
4998 if ( myStudyContext && strcmp( aLocalPersistentID, "" ) != 0 ) {
4999 int anId = atoi( aLocalPersistentID );
5000 return CORBA::string_dup( myStudyContext->getIORbyOldId( anId ).c_str() );
5002 return CORBA::string_dup( "" );
5005 //=======================================================================
5006 //function : RegisterObject
5008 //=======================================================================
5010 int SMESH_Gen_i::RegisterObject(CORBA::Object_ptr theObject)
5012 StudyContext* myStudyContext = GetCurrentStudyContext();
5013 if ( myStudyContext && !CORBA::is_nil( theObject )) {
5014 CORBA::String_var iorString = GetORB()->object_to_string( theObject );
5015 return myStudyContext->addObject( string( iorString.in() ) );
5020 //================================================================================
5022 * \brief Return id of registered object
5023 * \param theObject - the Object
5024 * \retval int - Object id
5026 //================================================================================
5028 CORBA::Long SMESH_Gen_i::GetObjectId(CORBA::Object_ptr theObject)
5030 StudyContext* myStudyContext = GetCurrentStudyContext();
5031 if ( myStudyContext && !CORBA::is_nil( theObject )) {
5032 string iorString = GetORB()->object_to_string( theObject );
5033 return myStudyContext->findId( iorString );
5038 //=============================================================================
5040 * SMESH_Gen_i::SetName
5042 * Set a new object name
5044 //=============================================================================
5045 void SMESH_Gen_i::SetName(const char* theIOR,
5046 const char* theName)
5048 if ( theIOR && strcmp( theIOR, "" ) ) {
5049 CORBA::Object_var anObject = GetORB()->string_to_object( theIOR );
5050 SALOMEDS::SObject_wrap aSO = ObjectToSObject( myCurrentStudy, anObject );
5051 if ( !aSO->_is_nil() ) {
5052 SetName( aSO, theName );
5057 int SMESH_Gen_i::GetCurrentStudyID()
5059 return myCurrentStudy->_is_nil() || myCurrentStudy->_non_existent() ? -1 : myCurrentStudy->StudyId();
5062 // Version information
5063 char* SMESH_Gen_i::getVersion()
5065 #if SMESH_DEVELOPMENT
5066 return CORBA::string_dup(SMESH_VERSION_STR"dev");
5068 return CORBA::string_dup(SMESH_VERSION_STR);
5072 //=================================================================================
5073 // function : Move()
5074 // purpose : Moves objects to the specified position.
5075 // Is used in the drag-n-drop functionality.
5076 //=================================================================================
5077 void SMESH_Gen_i::Move( const SMESH::sobject_list& what,
5078 SALOMEDS::SObject_ptr where,
5081 if ( CORBA::is_nil( where ) ) return;
5083 SALOMEDS::Study_var study = where->GetStudy();
5084 SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();
5085 SALOMEDS::UseCaseBuilder_var useCaseBuilder = study->GetUseCaseBuilder();
5086 SALOMEDS::SComponent_var father = where->GetFatherComponent();
5087 std::string dataType = father->ComponentDataType();
5088 if ( dataType != "SMESH" ) return; // not a SMESH component
5090 SALOMEDS::SObject_var objAfter;
5091 if ( row >= 0 && useCaseBuilder->HasChildren( where ) ) {
5092 // insert at given row -> find insertion position
5093 SALOMEDS::UseCaseIterator_var useCaseIt = useCaseBuilder->GetUseCaseIterator( where );
5095 for ( i = 0; i < row && useCaseIt->More(); i++, useCaseIt->Next() );
5096 if ( i == row && useCaseIt->More() ) {
5097 objAfter = useCaseIt->Value();
5101 for ( CORBA::ULong i = 0; i < what.length(); i++ ) {
5102 SALOMEDS::SObject_var sobj = what[i];
5103 if ( CORBA::is_nil( sobj ) ) continue; // skip bad object
5104 // insert the object to the use case tree
5105 if ( !CORBA::is_nil( objAfter ) )
5106 useCaseBuilder->InsertBefore( sobj, objAfter ); // insert at given row
5108 useCaseBuilder->AppendTo( where, sobj ); // append to the end of list
5111 //================================================================================
5113 * \brief Returns true if algorithm can be used to mesh a given geometry
5114 * \param [in] theAlgoType - the algorithm type
5115 * \param [in] theLibName - a name of the Plug-in library implementing the algorithm
5116 * \param [in] theGeomObject - the geometry to mesh
5117 * \param [in] toCheckAll - if \c True, returns \c True if all shapes are meshable,
5118 * else, returns \c True if at least one shape is meshable
5119 * \return CORBA::Boolean - can or can't
5121 //================================================================================
5123 CORBA::Boolean SMESH_Gen_i::IsApplicable ( const char* theAlgoType,
5124 const char* theLibName,
5125 GEOM::GEOM_Object_ptr theGeomObject,
5126 CORBA::Boolean toCheckAll)
5130 std::string aPlatformLibName;
5131 typedef GenericHypothesisCreator_i* (*GetHypothesisCreator)(const char*);
5132 GenericHypothesisCreator_i* aCreator =
5133 getHypothesisCreator(theAlgoType, theLibName, aPlatformLibName);
5136 TopoDS_Shape shape = GeomObjectToShape( theGeomObject );
5137 return shape.IsNull() || aCreator->IsApplicable( shape, toCheckAll );
5144 SMESH_CATCH( SMESH::doNothing );
5147 cout << "SMESH_Gen_i::IsApplicable(): exception in " << ( theAlgoType ? theAlgoType : "") << endl;
5152 //=================================================================================
5153 // function : GetInsideSphere
5154 // purpose : Collect indices of elements, which are located inside the sphere
5155 //=================================================================================
5156 SMESH::long_array* SMESH_Gen_i::GetInsideSphere( SMESH::SMESH_IDSource_ptr meshPart,
5157 SMESH::ElementType theElemType,
5163 SMESH::long_array_var aResult = new SMESH::long_array();
5164 if ( meshPart->_is_nil() )
5165 return aResult._retn();
5167 // 1. Create geometrical object
5168 gp_Pnt aP( theX, theY, theZ );
5169 TopoDS_Shape aShape = BRepPrimAPI_MakeSphere( aP, theR ).Shape();
5171 std::vector<long> lst =_GetInside(meshPart, theElemType, aShape);
5173 if ( lst.size() > 0 ) {
5174 aResult->length( lst.size() );
5175 for ( size_t i = 0; i < lst.size(); i++ ) {
5176 aResult[i] = lst[i];
5179 return aResult._retn();
5182 SMESH::long_array* SMESH_Gen_i::GetInsideBox( SMESH::SMESH_IDSource_ptr meshPart,
5183 SMESH::ElementType theElemType,
5184 CORBA::Double theX1,
5185 CORBA::Double theY1,
5186 CORBA::Double theZ1,
5187 CORBA::Double theX2,
5188 CORBA::Double theY2,
5189 CORBA::Double theZ2) {
5190 SMESH::long_array_var aResult = new SMESH::long_array();
5191 if( meshPart->_is_nil() )
5192 return aResult._retn();
5194 TopoDS_Shape aShape = BRepPrimAPI_MakeBox( gp_Pnt( theX1, theY1, theZ1 ), gp_Pnt( theX2, theY2, theZ2 ) ).Shape();
5196 std::vector<long> lst =_GetInside(meshPart, theElemType, aShape);
5198 if( lst.size() > 0 ) {
5199 aResult->length( lst.size() );
5200 for ( size_t i = 0; i < lst.size(); i++ ) {
5201 aResult[i] = lst[i];
5204 return aResult._retn();
5207 SMESH::long_array* SMESH_Gen_i::GetInsideCylinder( SMESH::SMESH_IDSource_ptr meshPart,
5208 SMESH::ElementType theElemType,
5212 CORBA::Double theDX,
5213 CORBA::Double theDY,
5214 CORBA::Double theDZ,
5216 CORBA::Double theR ){
5217 SMESH::long_array_var aResult = new SMESH::long_array();
5218 if( meshPart->_is_nil() )
5219 return aResult._retn();
5221 gp_Pnt aP( theX, theY, theZ );
5222 gp_Vec aV( theDX, theDY, theDZ );
5223 gp_Ax2 anAxes (aP, aV);
5225 TopoDS_Shape aShape = BRepPrimAPI_MakeCylinder(anAxes, theR, Abs(theH)).Shape();
5227 std::vector<long> lst =_GetInside(meshPart, theElemType, aShape);
5229 if( lst.size() > 0 ) {
5230 aResult->length( lst.size() );
5231 for ( size_t i = 0; i < lst.size(); i++ ) {
5232 aResult[i] = lst[i];
5235 return aResult._retn();
5238 SMESH::long_array* SMESH_Gen_i::GetInside( SMESH::SMESH_IDSource_ptr meshPart,
5239 SMESH::ElementType theElemType,
5240 GEOM::GEOM_Object_ptr theGeom,
5241 CORBA::Double theTolerance ) {
5242 SMESH::long_array_var aResult = new SMESH::long_array();
5243 if( meshPart->_is_nil() || theGeom->_is_nil() )
5244 return aResult._retn();
5246 TopoDS_Shape aShape = GeomObjectToShape( theGeom );
5248 std::vector<long> lst =_GetInside(meshPart, theElemType, aShape, &theTolerance);
5250 if( lst.size() > 0 ) {
5251 aResult->length( lst.size() );
5252 for ( size_t i = 0; i < lst.size(); i++ ) {
5253 aResult[i] = lst[i];
5256 return aResult._retn();
5261 std::vector<long> SMESH_Gen_i::_GetInside( SMESH::SMESH_IDSource_ptr meshPart,
5262 SMESH::ElementType theElemType,
5263 TopoDS_Shape& aShape,
5264 double* theTolerance) {
5266 std::vector<long> res;
5267 SMESH::SMESH_Mesh_var mesh = meshPart->GetMesh();
5269 if ( mesh->_is_nil() )
5272 SMESH_Mesh_i* anImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
5276 const SMDS_Mesh* meshDS = anImpl->GetImpl().GetMeshDS();
5281 SMDSAbs_ElementType aType = SMDSAbs_ElementType(theElemType);
5282 SMESH::Controls::ElementsOnShape* anElementsOnShape = new SMESH::Controls::ElementsOnShape();
5283 anElementsOnShape->SetAllNodes( true );
5284 anElementsOnShape->SetMesh( meshDS );
5285 anElementsOnShape->SetShape( aShape, aType );
5288 anElementsOnShape->SetTolerance(*theTolerance);
5290 SMESH::SMESH_Mesh_var msource = SMESH::SMESH_Mesh::_narrow(meshPart);
5291 if ( !msource->_is_nil() ) { // Mesh case
5292 SMDS_ElemIteratorPtr elemIt = meshDS->elementsIterator( aType );
5294 while ( elemIt->more() ) {
5295 const SMDS_MeshElement* anElem = elemIt->next();
5296 long anId = anElem->GetID();
5297 if ( anElementsOnShape->IsSatisfy( anId ) )
5298 res.push_back( anId );
5302 SMESH::SMESH_Group_var gsource = SMESH::SMESH_Group::_narrow(meshPart);
5303 if ( !gsource->_is_nil() ) {
5304 if(theElemType == SMESH::NODE) {
5305 SMESH::long_array_var nodes = gsource->GetNodeIDs();
5306 for ( CORBA::ULong i = 0; i < nodes->length(); ++i ) {
5307 if ( const SMDS_MeshNode* node = meshDS->FindNode( nodes[i] )) {
5308 long anId = node->GetID();
5309 if ( anElementsOnShape->IsSatisfy( anId ) )
5310 res.push_back( anId );
5313 } else if (gsource->GetType() == theElemType || theElemType == SMESH::ALL ) {
5314 SMESH::long_array_var elems = gsource->GetListOfID();
5315 for ( CORBA::ULong i = 0; i < elems->length(); ++i ) {
5316 if ( const SMDS_MeshElement* elem = meshDS->FindElement( elems[i] )) {
5317 long anId = elem->GetID();
5318 if ( anElementsOnShape->IsSatisfy( anId ) )
5319 res.push_back( anId );
5324 SMESH::SMESH_subMesh_var smsource = SMESH::SMESH_subMesh::_narrow(meshPart);
5325 if ( !smsource->_is_nil() ) {
5326 SMESH::long_array_var elems = smsource->GetElementsByType( theElemType );
5327 for ( CORBA::ULong i = 0; i < elems->length(); ++i ) {
5328 const SMDS_MeshElement* elem = ( theElemType == SMESH::NODE ) ? meshDS->FindNode( elems[i] ) : meshDS->FindElement( elems[i] );
5330 long anId = elem->GetID();
5331 if ( anElementsOnShape->IsSatisfy( anId ) )
5332 res.push_back( anId );
5340 //=============================================================================
5342 * SMESHEngine_factory
5344 * C factory, accessible with dlsym, after dlopen
5346 //=============================================================================
5350 PortableServer::ObjectId* SMESHEngine_factory( CORBA::ORB_ptr orb,
5351 PortableServer::POA_ptr poa,
5352 PortableServer::ObjectId* contId,
5353 const char* instanceName,
5354 const char* interfaceName )
5356 if(MYDEBUG) MESSAGE( "PortableServer::ObjectId* SMESHEngine_factory()" );
5357 if(MYDEBUG) SCRUTE(interfaceName);
5358 SMESH_Gen_i* aSMESHGen = new SMESH_Gen_i(orb, poa, contId, instanceName, interfaceName);
5359 return aSMESHGen->getId() ;