1 // Copyright (C) 2007-2015 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>
56 #define LibHandle HMODULE
57 #define LoadLib( name ) LoadLibrary( name )
58 #define GetProc GetProcAddress
59 #define UnLoadLib( handle ) FreeLibrary( handle );
61 #define LibHandle void*
62 #define LoadLib( name ) dlopen( name, RTLD_LAZY )
64 #define UnLoadLib( handle ) dlclose( handle );
67 #include "SMESH_Gen_i.hxx"
68 #include "SMESH_version.h"
70 #include "DriverMED_W_SMESHDS_Mesh.h"
71 #include "DriverMED_R_SMESHDS_Mesh.h"
73 #include "DriverCGNS_Read.hxx"
75 #include "MED_Factory.hxx"
76 #include "SMDS_EdgePosition.hxx"
77 #include "SMDS_FacePosition.hxx"
78 #include "SMDS_PolyhedralVolumeOfNodes.hxx"
79 #include "SMDS_SetIterator.hxx"
80 #include "SMDS_SpacePosition.hxx"
81 #include "SMDS_VertexPosition.hxx"
82 #include "SMESHDS_Document.hxx"
83 #include "SMESHDS_Group.hxx"
84 #include "SMESHDS_GroupOnGeom.hxx"
85 #include "SMESH_Algo_i.hxx"
86 #include "SMESH_File.hxx"
87 #include "SMESH_Group.hxx"
88 #include "SMESH_Group_i.hxx"
89 #include "SMESH_Hypothesis.hxx"
90 #include "SMESH_Hypothesis_i.hxx"
91 #include "SMESH_Mesh.hxx"
92 #include "SMESH_MeshEditor.hxx"
93 #include "SMESH_Mesh_i.hxx"
94 #include "SMESH_PreMeshInfo.hxx"
95 #include "SMESH_PythonDump.hxx"
96 #include "SMESH_TryCatch.hxx" // to include after OCC headers!
98 #include CORBA_SERVER_HEADER(SMESH_Group)
99 #include CORBA_SERVER_HEADER(SMESH_Filter)
100 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
103 #include <GEOM_Client.hxx>
105 #include <Basics_Utils.hxx>
106 #include <Basics_DirUtils.hxx>
108 #include <OpUtil.hxx>
109 #include <SALOMEDS_Tool.hxx>
110 #include <SALOME_Container_i.hxx>
111 #include <SALOME_DataContainer_i.hxx>
112 #include <SALOME_LifeCycleCORBA.hxx>
113 #include <SALOME_NamingService.hxx>
114 #include <Utils_CorbaException.hxx>
115 #include <Utils_ExceptHandlers.hxx>
116 #include <Utils_SINGLETON.hxx>
117 #include <utilities.h>
119 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
120 #include CORBA_CLIENT_HEADER(SALOME_Session)
122 // helpers about SALOME::GenericObj
123 #include <SALOMEDS_wrap.hxx>
124 #include <SALOMEDS_Attributes_wrap.hxx>
125 #include <GEOM_wrap.hxx>
133 using SMESH::TPythonDump;
136 #define NUM_TMP_FILES 2
139 static int MYDEBUG = 0;
141 static int MYDEBUG = 0;
144 // Static variables definition
145 GEOM::GEOM_Gen_var SMESH_Gen_i::myGeomGen = GEOM::GEOM_Gen::_nil();
146 CORBA::ORB_var SMESH_Gen_i::myOrb;
147 PortableServer::POA_var SMESH_Gen_i::myPoa;
148 SALOME_NamingService* SMESH_Gen_i::myNS = NULL;
149 SALOME_LifeCycleCORBA* SMESH_Gen_i::myLCC = NULL;
150 SMESH_Gen_i* SMESH_Gen_i::mySMESHGen = NULL;
153 const int nbElemPerDiagonal = 10;
155 //=============================================================================
157 * GetServant [ static ]
159 * Get servant of the CORBA object
161 //=============================================================================
163 PortableServer::ServantBase_var SMESH_Gen_i::GetServant( CORBA::Object_ptr theObject )
165 if( CORBA::is_nil( theObject ) || CORBA::is_nil( GetPOA() ) )
168 PortableServer::Servant aServant = GetPOA()->reference_to_servant( theObject );
172 INFOS( "GetServant - Unknown exception was caught!!!" );
177 //=============================================================================
179 * SObjectToObject [ static ]
181 * Get CORBA object corresponding to the SALOMEDS::SObject
183 //=============================================================================
185 CORBA::Object_var SMESH_Gen_i::SObjectToObject( SALOMEDS::SObject_ptr theSObject )
187 SALOMEDS::GenericAttribute_wrap anAttr;
188 CORBA::Object_var anObj;
189 if ( !theSObject->_is_nil() ) {
191 if( theSObject->FindAttribute( anAttr.inout(), "AttributeIOR" ) ) {
192 SALOMEDS::AttributeIOR_wrap anIOR = anAttr;
193 CORBA::String_var aValue = anIOR->Value();
194 if( strcmp( aValue, "" ) != 0 )
195 anObj = GetORB()->string_to_object( aValue );
199 INFOS( "SObjectToObject - Unknown exception was caught!!!" );
205 //=============================================================================
209 * Get SALOME_NamingService object
211 //=============================================================================
213 SALOME_NamingService* SMESH_Gen_i::GetNS()
215 if ( myNS == NULL ) {
216 myNS = SINGLETON_<SALOME_NamingService>::Instance();
217 ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting());
218 myNS->init_orb( GetORB() );
223 //=============================================================================
227 * Get SALOME_LifeCycleCORBA object
229 //=============================================================================
230 SALOME_LifeCycleCORBA* SMESH_Gen_i::GetLCC() {
231 if ( myLCC == NULL ) {
232 myLCC = new SALOME_LifeCycleCORBA( GetNS() );
238 //=============================================================================
240 * GetGeomEngine [ static ]
242 * Get GEOM::GEOM_Gen reference
244 //=============================================================================
245 GEOM::GEOM_Gen_var SMESH_Gen_i::GetGeomEngine() {
246 //CCRT GEOM::GEOM_Gen_var aGeomEngine =
247 //CCRT GEOM::GEOM_Gen::_narrow( GetLCC()->FindOrLoad_Component("FactoryServer","GEOM") );
248 //CCRT return aGeomEngine._retn();
249 if(CORBA::is_nil(myGeomGen))
251 Engines::EngineComponent_ptr temp=GetLCC()->FindOrLoad_Component("FactoryServer","GEOM");
252 myGeomGen=GEOM::GEOM_Gen::_narrow(temp);
257 //=============================================================================
259 * SMESH_Gen_i::SMESH_Gen_i
261 * Default constructor: not for use
263 //=============================================================================
265 SMESH_Gen_i::SMESH_Gen_i()
267 INFOS( "SMESH_Gen_i::SMESH_Gen_i : default constructor" );
270 //=============================================================================
272 * SMESH_Gen_i::SMESH_Gen_i
274 * Standard constructor, used with Container
276 //=============================================================================
278 SMESH_Gen_i::SMESH_Gen_i( CORBA::ORB_ptr orb,
279 PortableServer::POA_ptr poa,
280 PortableServer::ObjectId* contId,
281 const char* instanceName,
282 const char* interfaceName )
283 : Engines_Component_i( orb, poa, contId, instanceName, interfaceName )
285 MESSAGE( "SMESH_Gen_i::SMESH_Gen_i : standard constructor" );
287 myOrb = CORBA::ORB::_duplicate(orb);
288 myPoa = PortableServer::POA::_duplicate(poa);
291 _id = myPoa->activate_object( _thisObj );
293 myIsEmbeddedMode = false;
294 myShapeReader = NULL; // shape reader
296 myIsHistoricalPythonDump = true;
297 myToForgetMeshDataOnHypModif = false;
299 myImportedStudyChanged = true;
300 myImportedStudyId = 0;
302 // set it in standalone mode only
303 //OSD::SetSignal( true );
305 // 0020605: EDF 1190 SMESH: Display performance. 80 seconds for 52000 cells.
306 // find out mode (embedded or standalone) here else
307 // meshes created before calling SMESH_Client::GetSMESHGen(), which calls
308 // SMESH_Gen_i::SetEmbeddedMode(), have wrong IsEmbeddedMode flag
309 if ( SALOME_NamingService* ns = GetNS() )
311 CORBA::Object_var obj = ns->Resolve( "/Kernel/Session" );
312 SALOME::Session_var session = SALOME::Session::_narrow( obj ) ;
313 if ( !session->_is_nil() )
315 CORBA::String_var str_host = session->getHostname();
316 CORBA::Long s_pid = session->getPID();
317 string my_host = Kernel_Utils::GetHostname();
319 long my_pid = (long)_getpid();
321 long my_pid = (long) getpid();
323 SetEmbeddedMode( s_pid == my_pid && my_host == str_host.in() );
328 //=============================================================================
330 * SMESH_Gen_i::~SMESH_Gen_i
334 //=============================================================================
336 SMESH_Gen_i::~SMESH_Gen_i()
338 MESSAGE( "SMESH_Gen_i::~SMESH_Gen_i" );
340 // delete hypothesis creators
341 map<string, GenericHypothesisCreator_i*>::iterator itHyp;
342 for (itHyp = myHypCreatorMap.begin(); itHyp != myHypCreatorMap.end(); itHyp++)
344 delete (*itHyp).second;
346 myHypCreatorMap.clear();
348 // Clear study contexts data
349 map<int, StudyContext*>::iterator it;
350 for ( it = myStudyContextMap.begin(); it != myStudyContextMap.end(); ++it ) {
353 myStudyContextMap.clear();
354 // delete shape reader
356 delete myShapeReader;
358 //=============================================================================
360 * SMESH_Gen_i::getHypothesisCreator
362 * Get hypothesis creator
364 //=============================================================================
365 GenericHypothesisCreator_i* SMESH_Gen_i::getHypothesisCreator(const char* theHypName,
366 const char* theLibName,
367 std::string& thePlatformLibName)
368 throw (SALOME::SALOME_Exception)
370 std::string aPlatformLibName;
371 /* It's Need to tranlate lib name for WIN32 or X platform */
372 if ( theLibName && theLibName[0] != '\0' )
374 int libNameLen = strlen(theLibName);
375 //check for old format "libXXXXXXX.so"
376 if (libNameLen > 7 &&
377 !strncmp( theLibName, "lib", 3 ) &&
378 !strcmp( theLibName+libNameLen-3, ".so" ))
382 aPlatformLibName = std::string( theLibName+3, libNameLen-6 ) + ".dll";
384 aPlatformLibName = theLibName;
389 //try to use new format
391 aPlatformLibName = theLibName;
392 aPlatformLibName += ".dll";
394 aPlatformLibName = "lib" + std::string( theLibName ) + ".so";
398 thePlatformLibName = aPlatformLibName;
400 Unexpect aCatch(SALOME_SalomeException);
401 if(MYDEBUG) MESSAGE( "Create Hypothesis <" << theHypName << "> from " << aPlatformLibName);
403 typedef GenericHypothesisCreator_i* (*GetHypothesisCreator)(const char* );
404 GenericHypothesisCreator_i* aCreator;
407 // check, if creator for this hypothesis type already exists
408 if (myHypCreatorMap.find(string(theHypName)) == myHypCreatorMap.end())
410 // load plugin library
411 if(MYDEBUG) MESSAGE("Loading server meshers plugin library ...");
412 LibHandle libHandle = LoadLib( aPlatformLibName.c_str() );
415 // report any error, if occured
417 const char* anError = dlerror();
418 throw(SALOME_Exception(anError));
420 throw(SALOME_Exception(LOCALIZED( "Can't load server meshers plugin library" )));
424 // get method, returning hypothesis creator
425 if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
426 GetHypothesisCreator procHandle =
427 (GetHypothesisCreator)GetProc( libHandle, "GetHypothesisCreator" );
430 throw(SALOME_Exception(LOCALIZED("bad hypothesis plugin library")));
431 UnLoadLib(libHandle);
434 // get hypothesis creator
435 if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << theHypName);
436 aCreator = procHandle(theHypName);
439 throw(SALOME_Exception(LOCALIZED("no such a hypothesis in this plugin")));
441 // map hypothesis creator to a hypothesis name
442 myHypCreatorMap[string(theHypName)] = aCreator;
447 return myHypCreatorMap[string(theHypName)];
450 catch (SALOME_Exception& S_ex)
452 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
457 //=============================================================================
459 * SMESH_Gen_i::createHypothesis
461 * Create hypothesis of given type
463 //=============================================================================
464 SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::createHypothesis(const char* theHypName,
465 const char* theLibName)
467 SMESH_Hypothesis_i* myHypothesis_i = 0;
468 SMESH::SMESH_Hypothesis_var hypothesis_i;
469 std::string aPlatformLibName;
470 GenericHypothesisCreator_i* aCreator =
471 getHypothesisCreator(theHypName, theLibName, aPlatformLibName);
473 // create a new hypothesis object, store its ref. in studyContext
474 myHypothesis_i = aCreator->Create(myPoa, GetCurrentStudyID(), &myGen);
477 myHypothesis_i->SetLibName( aPlatformLibName.c_str() ); // for persistency assurance
478 CORBA::String_var hypName = myHypothesis_i->GetName();
479 myHypCreatorMap[ hypName.in() ] = aCreator;
481 // activate the CORBA servant of hypothesis
482 hypothesis_i = myHypothesis_i->_this();
483 int nextId = RegisterObject( hypothesis_i );
484 if(MYDEBUG) { MESSAGE( "Add hypo to map with id = "<< nextId ); }
485 else { nextId = 0; } // avoid "unused variable" warning in release mode
487 return hypothesis_i._retn();
490 //=============================================================================
492 * SMESH_Gen_i::createMesh
494 * Create empty mesh on shape
496 //=============================================================================
497 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::createMesh()
498 throw ( SALOME::SALOME_Exception )
500 Unexpect aCatch(SALOME_SalomeException);
501 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::createMesh" );
503 // Get or create the GEOM_Client instance
505 // create a new mesh object servant, store it in a map in study context
506 SMESH_Mesh_i* meshServant = new SMESH_Mesh_i( GetPOA(), this, GetCurrentStudyID() );
507 // create a new mesh object
508 MESSAGE("myIsEmbeddedMode " << myIsEmbeddedMode);
509 meshServant->SetImpl( myGen.CreateMesh( GetCurrentStudyID(), myIsEmbeddedMode ));
511 // activate the CORBA servant of Mesh
512 SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( meshServant->_this() );
513 int nextId = RegisterObject( mesh );
514 if(MYDEBUG) { MESSAGE( "Add mesh to map with id = "<< nextId); }
515 else { nextId = 0; } // avoid "unused variable" warning in release mode
518 catch (SALOME_Exception& S_ex) {
519 THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
521 return SMESH::SMESH_Mesh::_nil();
524 //=============================================================================
526 * SMESH_Gen_i::GetShapeReader
530 //=============================================================================
531 GEOM_Client* SMESH_Gen_i::GetShapeReader()
533 // create shape reader if necessary
534 if ( !myShapeReader )
535 myShapeReader = new GEOM_Client(GetContainerRef());
536 ASSERT( myShapeReader );
537 return myShapeReader;
540 //=============================================================================
542 * SMESH_Gen_i::SetGeomEngine
544 * Set GEOM::GEOM_Gen reference
546 //=============================================================================
547 //GEOM::GEOM_Gen_ptr SMESH_Gen_i::SetGeomEngine( const char* containerLoc )
548 void SMESH_Gen_i::SetGeomEngine( GEOM::GEOM_Gen_ptr geomcompo )
550 //Engines::Component_ptr temp=GetLCC()->FindOrLoad_Component(containerLoc,"GEOM");
551 //myGeomGen=GEOM::GEOM_Gen::_narrow(temp);
552 myGeomGen=GEOM::GEOM_Gen::_duplicate(geomcompo);
556 //=============================================================================
558 * SMESH_Gen_i::SetEmbeddedMode
562 //=============================================================================
564 void SMESH_Gen_i::SetEmbeddedMode( CORBA::Boolean theMode )
566 myIsEmbeddedMode = theMode;
567 MESSAGE("myIsEmbeddedMode " << myIsEmbeddedMode);
569 if ( !myIsEmbeddedMode ) {
570 //PAL10867: disable signals catching with "noexcepthandler" option
571 char* envNoCatchSignals = getenv("NOT_INTERCEPT_SIGNALS");
572 if (!envNoCatchSignals || !atoi(envNoCatchSignals))
577 char* envDisableFPE = getenv("DISABLE_FPE");
578 if (envDisableFPE && atoi(envDisableFPE))
583 OSD::SetSignal( raiseFPE );
585 // else OSD::SetSignal() is called in GUI
589 //=============================================================================
591 * SMESH_Gen_i::IsEmbeddedMode
595 //=============================================================================
597 CORBA::Boolean SMESH_Gen_i::IsEmbeddedMode()
599 return myIsEmbeddedMode;
602 //=============================================================================
604 * SMESH_Gen_i::SetCurrentStudy
608 //=============================================================================
610 void SMESH_Gen_i::SetCurrentStudy( SALOMEDS::Study_ptr theStudy )
612 setCurrentStudy( theStudy );
615 void SMESH_Gen_i::setCurrentStudy( SALOMEDS::Study_ptr theStudy,
616 bool theStudyIsBeingClosed)
618 int curStudyId = GetCurrentStudyID();
619 myCurrentStudy = SALOMEDS::Study::_duplicate( theStudy );
620 // create study context, if it doesn't exist and set current study
621 int studyId = GetCurrentStudyID();
622 if ( myStudyContextMap.find( studyId ) == myStudyContextMap.end() )
623 myStudyContextMap[ studyId ] = new StudyContext;
625 // myCurrentStudy may be nil
626 if ( !theStudyIsBeingClosed && !CORBA::is_nil( myCurrentStudy ) ) {
627 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
628 SALOMEDS::SComponent_wrap GEOM_var = myCurrentStudy->FindComponent( "GEOM" );
629 if( !GEOM_var->_is_nil() )
630 aStudyBuilder->LoadWith( GEOM_var, GetGeomEngine() );
631 // NPAL16168, issue 0020210
632 // Let meshes update their data depending on GEOM groups that could change
633 if ( curStudyId != studyId )
635 CORBA::String_var compDataType = ComponentDataType();
636 SALOMEDS::SComponent_wrap me = myCurrentStudy->FindComponent( compDataType.in() );
637 if ( !me->_is_nil() ) {
638 SALOMEDS::ChildIterator_wrap anIter = myCurrentStudy->NewChildIterator( me );
639 for ( ; anIter->More(); anIter->Next() ) {
640 SALOMEDS::SObject_wrap so = anIter->Value();
641 CORBA::Object_var ior = SObjectToObject( so );
642 if ( SMESH_Mesh_i* mesh = SMESH::DownCast<SMESH_Mesh_i*>( ior ))
643 mesh->CheckGeomModif();
650 //=============================================================================
652 * SMESH_Gen_i::GetCurrentStudy
656 //=============================================================================
658 SALOMEDS::Study_ptr SMESH_Gen_i::GetCurrentStudy()
660 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetCurrentStudy: study Id = " << GetCurrentStudyID() );
661 if ( GetCurrentStudyID() < 0 )
662 return SALOMEDS::Study::_nil();
663 return SALOMEDS::Study::_duplicate( myCurrentStudy );
666 //=============================================================================
668 * SMESH_Gen_i::GetCurrentStudyContext
670 * Get current study context
672 //=============================================================================
673 StudyContext* SMESH_Gen_i::GetCurrentStudyContext()
675 if ( !CORBA::is_nil( myCurrentStudy ) &&
676 myStudyContextMap.find( GetCurrentStudyID() ) != myStudyContextMap.end() )
677 return myStudyContextMap[ myCurrentStudy->StudyId() ];
682 //=============================================================================
684 * SMESH_Gen_i::CreateHypothesis
686 * Create hypothesis/algorothm of given type and publish it in the study
688 //=============================================================================
690 SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::CreateHypothesis( const char* theHypName,
691 const char* theLibName )
692 throw ( SALOME::SALOME_Exception )
694 Unexpect aCatch(SALOME_SalomeException);
695 // Create hypothesis/algorithm
696 SMESH::SMESH_Hypothesis_var hyp = this->createHypothesis( theHypName, theLibName );
698 // Publish hypothesis/algorithm in the study
699 if ( CanPublishInStudy( hyp ) ) {
700 SALOMEDS::SObject_wrap aSO = PublishHypothesis( myCurrentStudy, hyp );
701 if ( !aSO->_is_nil() ) {
702 // Update Python script
703 TPythonDump() << aSO << " = " << this << ".CreateHypothesis('"
704 << theHypName << "', '" << theLibName << "')";
711 //================================================================================
713 * \brief Return a hypothesis holding parameter values corresponding either to the mesh
714 * existing on the given geometry or to size of the geometry.
715 * \param theHypType - hypothesis type name
716 * \param theLibName - plugin library name
717 * \param theMesh - The mesh of interest
718 * \param theGeom - The shape to get parameter values from
719 * \retval SMESH::SMESH_Hypothesis_ptr - The returned hypothesis may be the one existing
720 * in a study and used to compute the mesh, or a temporary one created just to pass
723 //================================================================================
725 SMESH::SMESH_Hypothesis_ptr
726 SMESH_Gen_i::GetHypothesisParameterValues (const char* theHypType,
727 const char* theLibName,
728 SMESH::SMESH_Mesh_ptr theMesh,
729 GEOM::GEOM_Object_ptr theGeom,
730 CORBA::Boolean byMesh)
731 throw ( SALOME::SALOME_Exception )
733 Unexpect aCatch(SALOME_SalomeException);
734 if ( byMesh && CORBA::is_nil( theMesh ) )
735 return SMESH::SMESH_Hypothesis::_nil();
736 if ( byMesh && CORBA::is_nil( theGeom ) )
737 return SMESH::SMESH_Hypothesis::_nil();
739 // -----------------------------------------------
740 // find hypothesis used to mesh theGeom
741 // -----------------------------------------------
743 // get mesh and shape
744 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
745 TopoDS_Shape shape = GeomObjectToShape( theGeom );
746 if ( byMesh && ( !meshServant || meshServant->NbNodes()==0 || shape.IsNull() ))
747 return SMESH::SMESH_Hypothesis::_nil();
748 ::SMESH_Mesh* mesh = meshServant ? &meshServant->GetImpl() : (::SMESH_Mesh*)0;
750 // create a temporary hypothesis to know its dimention
751 SMESH::SMESH_Hypothesis_var tmpHyp = this->createHypothesis( theHypType, theLibName );
752 SMESH_Hypothesis_i* hypServant = SMESH::DownCast<SMESH_Hypothesis_i*>( tmpHyp );
754 return SMESH::SMESH_Hypothesis::_nil();
755 ::SMESH_Hypothesis* hyp = hypServant->GetImpl();
758 // look for a hypothesis of theHypType used to mesh the shape
759 if ( myGen.GetShapeDim( shape ) == hyp->GetDim() )
762 SMESH::ListOfHypothesis_var aHypList = theMesh->GetHypothesisList( theGeom );
763 int nbLocalHyps = aHypList->length();
764 for ( int i = 0; i < nbLocalHyps; i++ ) {
765 CORBA::String_var hypName = aHypList[i]->GetName();
766 if ( strcmp( theHypType, hypName.in() ) == 0 ) // FOUND local!
767 return SMESH::SMESH_Hypothesis::_duplicate( aHypList[i] );
769 // check super shapes
770 TopTools_ListIteratorOfListOfShape itShape( mesh->GetAncestors( shape ));
771 while ( nbLocalHyps == 0 && itShape.More() ) {
772 GEOM::GEOM_Object_ptr geomObj = ShapeToGeomObject( itShape.Value() );
773 if ( ! CORBA::is_nil( geomObj )) {
774 SMESH::ListOfHypothesis_var aHypList = theMesh->GetHypothesisList( geomObj );
775 nbLocalHyps = aHypList->length();
776 for ( int i = 0; i < nbLocalHyps; i++ )
777 if ( strcmp( theHypType, aHypList[i]->GetName() ) == 0 ) // FOUND global!
778 return SMESH::SMESH_Hypothesis::_duplicate( aHypList[i] );
784 // let the temporary hypothesis find out somehow parameter values by mesh
785 if ( hyp->SetParametersByMesh( mesh, shape ))
786 return SMESH::SMESH_Hypothesis::_duplicate( tmpHyp );
791 diagonal = mesh->GetShapeDiagonalSize();
793 diagonal = ::SMESH_Mesh::GetShapeDiagonalSize( shape );
794 ::SMESH_Hypothesis::TDefaults dflts;
795 dflts._elemLength = diagonal / myGen.GetBoundaryBoxSegmentation();
796 dflts._nbSegments = myGen.GetDefaultNbSegments();
797 dflts._shape = &shape;
798 // let the temporary hypothesis initialize it's values
799 if ( hyp->SetParametersByDefaults( dflts, mesh ))
800 return SMESH::SMESH_Hypothesis::_duplicate( tmpHyp );
803 return SMESH::SMESH_Hypothesis::_nil();
806 //=============================================================================
808 * Returns \c True if a hypothesis is assigned to a sole sub-mesh in a current Study
809 * \param [in] theHyp - the hypothesis of interest
810 * \param [out] theMesh - the sole mesh using \a theHyp
811 * \param [out] theShape - the sole geometry \a theHyp is assigned to
812 * \return boolean - \c True if \a theMesh and \a theShape are sole using \a theHyp
814 * If two meshes on same shape have theHyp assigned to the same sub-shape, they are
815 * considered as SAME sub-mesh => result is \c true.
816 * This method ids used to initialize SMESHGUI_GenericHypothesisCreator with
817 * a shape to which an hyp being edited is assigned.
819 //=============================================================================
821 CORBA::Boolean SMESH_Gen_i::GetSoleSubMeshUsingHyp( SMESH::SMESH_Hypothesis_ptr theHyp,
822 SMESH::SMESH_Mesh_out theMesh,
823 GEOM::GEOM_Object_out theShape)
825 if ( GetCurrentStudyID() < 0 || CORBA::is_nil( theHyp ))
828 // get Mesh component SO
829 CORBA::String_var compDataType = ComponentDataType();
830 SALOMEDS::SComponent_wrap comp = myCurrentStudy->FindComponent( compDataType.in() );
831 if ( CORBA::is_nil( comp ))
834 // look for child SO of meshes
835 SMESH::SMESH_Mesh_var foundMesh;
836 TopoDS_Shape foundShape;
838 SALOMEDS::ChildIterator_wrap meshIter = myCurrentStudy->NewChildIterator( comp );
839 for ( ; meshIter->More() && isSole; meshIter->Next() )
841 SALOMEDS::SObject_wrap curSO = meshIter->Value();
842 CORBA::Object_var obj = SObjectToObject( curSO );
843 SMESH_Mesh_i* mesh_i = SMESH::DownCast< SMESH_Mesh_i* >( obj );
847 // look for a sole shape where theHyp is assigned
848 bool isHypFound = false;
849 const ShapeToHypothesis & s2hyps = mesh_i->GetImpl().GetMeshDS()->GetHypotheses();
850 ShapeToHypothesis::Iterator s2hypsIt( s2hyps );
851 for ( ; s2hypsIt.More() && isSole; s2hypsIt.Next() )
853 const THypList& hyps = s2hypsIt.Value();
854 THypList::const_iterator h = hyps.begin();
855 for ( ; h != hyps.end(); ++h )
856 if ( (*h)->GetID() == theHyp->GetId() )
858 if ( h != hyps.end()) // theHyp found
861 if ( ! foundShape.IsNull() &&
862 ! foundShape.IsSame( s2hypsIt.Key() )) // not a sole sub-shape
864 foundShape.Nullify();
868 foundShape = s2hypsIt.Key();
870 } // loop on assigned hyps
872 if ( isHypFound && !foundShape.IsNull() ) // a mesh using theHyp is found
874 if ( !foundMesh->_is_nil() ) // not a sole mesh
876 GEOM::GEOM_Object_var s1 = mesh_i ->GetShapeToMesh();
877 GEOM::GEOM_Object_var s2 = foundMesh->GetShapeToMesh();
878 if ( ! ( isSole = s1->IsSame( s2 )))
881 foundMesh = SMESH::SMESH_Mesh::_narrow( obj );
887 ! foundMesh->_is_nil() &&
888 ! foundShape.IsNull() )
890 theMesh = foundMesh._retn();
891 theShape = ShapeToGeomObject( foundShape );
892 return ( !theMesh->_is_nil() && !theShape->_is_nil() );
897 //=============================================================================
899 * Sets number of segments per diagonal of boundary box of geometry by which
900 * default segment length of appropriate 1D hypotheses is defined
902 //=============================================================================
904 void SMESH_Gen_i::SetBoundaryBoxSegmentation( CORBA::Long theNbSegments )
905 throw ( SALOME::SALOME_Exception )
907 if ( theNbSegments > 0 )
908 myGen.SetBoundaryBoxSegmentation( int( theNbSegments ));
910 THROW_SALOME_CORBA_EXCEPTION( "non-positive number of segments", SALOME::BAD_PARAM );
912 //=============================================================================
914 * \brief Sets default number of segments per edge
916 //=============================================================================
917 void SMESH_Gen_i::SetDefaultNbSegments(CORBA::Long theNbSegments)
918 throw ( SALOME::SALOME_Exception )
920 if ( theNbSegments > 0 )
921 myGen.SetDefaultNbSegments( int(theNbSegments) );
923 THROW_SALOME_CORBA_EXCEPTION( "non-positive number of segments", SALOME::BAD_PARAM );
926 //=============================================================================
930 //=============================================================================
932 void SMESH_Gen_i::SetOption(const char* name, const char* value)
934 if ( name && value && strlen( value ) > 0 )
937 if ( strcmp(name, "historical_python_dump") == 0 )
939 myIsHistoricalPythonDump = ( value[0] == '1' || toupper(value[0]) == 'T' ); // 1 || true
940 msgToGUI = "preferences/SMESH/historical_python_dump/";
941 msgToGUI += myIsHistoricalPythonDump ? "true" : "false";
943 else if ( strcmp(name, "forget_mesh_on_hyp_modif") == 0 )
945 myToForgetMeshDataOnHypModif = ( value[0] == '1' || toupper(value[0]) == 'T' ); // 1 || true
946 msgToGUI = "preferences/SMESH/forget_mesh_on_hyp_modif/";
947 msgToGUI += myToForgetMeshDataOnHypModif ? "true" : "false";
950 // update preferences in case if SetOption() is invoked from python console
951 if ( !msgToGUI.empty() )
953 CORBA::Object_var obj = SMESH_Gen_i::GetNS()->Resolve( "/Kernel/Session" );
954 SALOME::Session_var session = SALOME::Session::_narrow( obj );
955 if ( !CORBA::is_nil( session ) )
956 session->emitMessageOneWay(msgToGUI.c_str());
961 //=============================================================================
963 Return an option value
965 //=============================================================================
967 char* SMESH_Gen_i::GetOption(const char* name)
971 if ( strcmp(name, "historical_python_dump") == 0 )
973 return CORBA::string_dup( myIsHistoricalPythonDump ? "true" : "false" );
975 if ( strcmp(name, "forget_mesh_on_hyp_modif") == 0 )
977 return CORBA::string_dup( myToForgetMeshDataOnHypModif ? "true" : "false" );
980 return CORBA::string_dup( "" );
983 //=============================================================================
985 * SMESH_Gen_i::CreateMesh
987 * Create empty mesh on a shape and publish it in the study
989 //=============================================================================
991 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMesh( GEOM::GEOM_Object_ptr theShapeObject )
992 throw ( SALOME::SALOME_Exception )
994 Unexpect aCatch(SALOME_SalomeException);
995 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMesh" );
997 SMESH::SMESH_Mesh_var mesh = this->createMesh();
999 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( mesh );
1000 ASSERT( meshServant );
1001 meshServant->SetShape( theShapeObject );
1003 // publish mesh in the study
1004 if ( CanPublishInStudy( mesh ) ) {
1005 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1006 aStudyBuilder->NewCommand(); // There is a transaction
1007 SALOMEDS::SObject_wrap aSO = PublishMesh( myCurrentStudy, mesh.in() );
1008 aStudyBuilder->CommitCommand();
1009 if ( !aSO->_is_nil() ) {
1010 // Update Python script
1011 TPythonDump() << aSO << " = " << this << ".CreateMesh(" << theShapeObject << ")";
1015 return mesh._retn();
1018 //=============================================================================
1020 * SMESH_Gen_i::CreateEmptyMesh
1024 //=============================================================================
1026 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateEmptyMesh()
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 // publish mesh in the study
1035 if ( CanPublishInStudy( mesh ) ) {
1036 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1037 aStudyBuilder->NewCommand(); // There is a transaction
1038 SALOMEDS::SObject_wrap aSO = PublishMesh( myCurrentStudy, mesh.in() );
1039 aStudyBuilder->CommitCommand();
1040 if ( !aSO->_is_nil() ) {
1041 // Update Python script
1042 TPythonDump() << aSO << " = " << this << ".CreateEmptyMesh()";
1046 return mesh._retn();
1051 //================================================================================
1053 * \brief Throws an exception in case if the file can't be read
1055 //================================================================================
1057 void checkFileReadable( const char* theFileName ) throw ( SALOME::SALOME_Exception )
1059 SMESH_File f ( theFileName );
1062 if ( !f.error().empty() )
1063 THROW_SALOME_CORBA_EXCEPTION( f.error().c_str(), SALOME::BAD_PARAM);
1065 THROW_SALOME_CORBA_EXCEPTION
1066 (( SMESH_Comment("Can't open for reading the file ") << theFileName ).c_str(),
1067 SALOME::BAD_PARAM );
1072 //=============================================================================
1074 * SMESH_Gen_i::CreateMeshFromUNV
1076 * Create mesh and import data from UNV file
1078 //=============================================================================
1080 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMeshesFromUNV( const char* theFileName )
1081 throw ( SALOME::SALOME_Exception )
1083 Unexpect aCatch(SALOME_SalomeException);
1085 checkFileReadable( theFileName );
1087 SMESH::SMESH_Mesh_var aMesh = createMesh();
1089 // publish mesh in the study
1090 if ( CanPublishInStudy( aMesh ) ) {
1091 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1092 aStudyBuilder->NewCommand(); // There is a transaction
1093 SALOMEDS::SObject_wrap aSO = PublishMesh( myCurrentStudy, aMesh.in(), aFileName.c_str() );
1094 aStudyBuilder->CommitCommand();
1095 if ( !aSO->_is_nil() ) {
1096 // Update Python script
1097 TPythonDump() << aSO << " = " << this << ".CreateMeshesFromUNV(r'" << theFileName << "')";
1101 SMESH_Mesh_i* aServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( aMesh ).in() );
1103 aServant->ImportUNVFile( theFileName );
1105 // Dump creation of groups
1106 SMESH::ListOfGroups_var groups = aServant->GetGroups();
1108 aServant->GetImpl().GetMeshDS()->Modified();
1109 return aMesh._retn();
1112 //=============================================================================
1114 * SMESH_Gen_i::CreateMeshFromMED
1116 * Create mesh and import data from MED file
1118 //=============================================================================
1120 SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromMEDorSAUV( const char* theFileName,
1121 SMESH::DriverMED_ReadStatus& theStatus,
1122 const char* theCommandNameForPython,
1123 const char* theFileNameForPython)
1126 char bname[ _MAX_FNAME ];
1127 _splitpath( theFileNameForPython, NULL, NULL, bname, NULL );
1128 string aFileName = bname;
1130 string aFileName = basename( theFileNameForPython );
1132 // Retrieve mesh names from the file
1133 DriverMED_R_SMESHDS_Mesh myReader;
1134 myReader.SetFile( theFileName );
1135 myReader.SetMeshId( -1 );
1136 Driver_Mesh::Status aStatus;
1137 list<string> aNames = myReader.GetMeshNames(aStatus);
1138 SMESH::mesh_array_var aResult = new SMESH::mesh_array();
1139 theStatus = (SMESH::DriverMED_ReadStatus)aStatus;
1141 { // open a new scope to make aPythonDump die before PythonDump in SMESH_Mesh::GetGroups()
1144 TPythonDump aPythonDump;
1145 aPythonDump << "([";
1147 if (theStatus == SMESH::DRS_OK) {
1148 SALOMEDS::StudyBuilder_var aStudyBuilder;
1149 if ( GetCurrentStudyID() > -1 )
1151 aStudyBuilder = myCurrentStudy->NewBuilder();
1152 aStudyBuilder->NewCommand(); // There is a transaction
1154 aResult->length( aNames.size() );
1157 // Iterate through all meshes and create mesh objects
1158 for ( list<string>::iterator it = aNames.begin(); it != aNames.end(); it++ )
1161 if (i > 0) aPythonDump << ", ";
1164 SMESH::SMESH_Mesh_var mesh = createMesh();
1166 // publish mesh in the study
1167 SALOMEDS::SObject_wrap aSO;
1168 if ( CanPublishInStudy( mesh ) )
1169 // little trick: for MED file theFileName and theFileNameForPython are the same, but they are different for SAUV
1170 // - as names of meshes are stored in MED file, we use them for data publishing
1171 // - as mesh name is not stored in UNV file, we use file name as name of mesh when publishing data
1172 aSO = PublishMesh( myCurrentStudy, mesh.in(), ( theFileName == theFileNameForPython ) ? (*it).c_str() : aFileName.c_str() );
1173 if ( !aSO->_is_nil() ) {
1178 aPythonDump << "mesh_" << i;
1181 // Read mesh data (groups are published automatically by ImportMEDFile())
1182 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
1183 ASSERT( meshServant );
1184 SMESH::DriverMED_ReadStatus status1 =
1185 meshServant->ImportMEDFile( theFileName, (*it).c_str() );
1186 if (status1 > theStatus)
1187 theStatus = status1;
1189 aResult[i++] = SMESH::SMESH_Mesh::_duplicate( mesh );
1190 meshServant->GetImpl().GetMeshDS()->Modified();
1192 if ( !aStudyBuilder->_is_nil() )
1193 aStudyBuilder->CommitCommand();
1196 // Update Python script
1197 aPythonDump << "], status) = " << this << "." << theCommandNameForPython << "(r'" << theFileNameForPython << "')";
1199 // Dump creation of groups
1200 for ( int i = 0; i < aResult->length(); ++i )
1201 SMESH::ListOfGroups_var groups = aResult[ i ]->GetGroups();
1203 return aResult._retn();
1206 SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromMED( const char* theFileName,
1207 SMESH::DriverMED_ReadStatus& theStatus)
1208 throw ( SALOME::SALOME_Exception )
1210 Unexpect aCatch(SALOME_SalomeException);
1211 checkFileReadable( theFileName );
1213 SMESH::mesh_array* result = CreateMeshesFromMEDorSAUV(theFileName, theStatus, "CreateMeshesFromMED", theFileName);
1217 //=============================================================================
1219 * SMESH_Gen_i::CreateMeshFromSAUV
1221 * Create mesh and import data from SAUV file
1223 //=============================================================================
1225 SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromSAUV( const char* theFileName,
1226 SMESH::DriverMED_ReadStatus& theStatus)
1227 throw ( SALOME::SALOME_Exception )
1229 Unexpect aCatch(SALOME_SalomeException);
1230 checkFileReadable( theFileName );
1232 std::string sauvfilename(theFileName);
1233 std::string medfilename(theFileName);
1234 medfilename += ".med";
1237 cmd = "%PYTHONBIN% ";
1242 cmd += "from medutilities import convert ; convert(r'" + sauvfilename + "', 'GIBI', 'MED', 1, r'" + medfilename + "')";
1244 system(cmd.c_str());
1245 SMESH::mesh_array* result = CreateMeshesFromMEDorSAUV(medfilename.c_str(), theStatus, "CreateMeshesFromSAUV", sauvfilename.c_str());
1247 cmd = "%PYTHONBIN% ";
1252 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1254 system(cmd.c_str());
1258 //=============================================================================
1260 * SMESH_Gen_i::CreateMeshFromSTL
1262 * Create mesh and import data from STL file
1264 //=============================================================================
1266 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMeshesFromSTL( const char* theFileName )
1267 throw ( SALOME::SALOME_Exception )
1269 Unexpect aCatch(SALOME_SalomeException);
1270 checkFileReadable( theFileName );
1272 SMESH::SMESH_Mesh_var aMesh = createMesh();
1275 char bname[ _MAX_FNAME ];
1276 _splitpath( theFileName, NULL, NULL, bname, NULL );
1277 string aFileName = bname;
1279 string aFileName = basename( theFileName );
1281 // publish mesh in the study
1282 if ( CanPublishInStudy( aMesh ) ) {
1283 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1284 aStudyBuilder->NewCommand(); // There is a transaction
1285 SALOMEDS::SObject_wrap aSO = PublishInStudy
1286 ( myCurrentStudy, SALOMEDS::SObject::_nil(), aMesh.in(), aFileName.c_str() );
1287 aStudyBuilder->CommitCommand();
1288 if ( !aSO->_is_nil() ) {
1289 // Update Python script
1290 TPythonDump() << aSO << " = " << this << ".CreateMeshesFromSTL(r'" << theFileName << "')";
1294 SMESH_Mesh_i* aServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( aMesh ).in() );
1296 aServant->ImportSTLFile( theFileName );
1297 aServant->GetImpl().GetMeshDS()->Modified();
1298 return aMesh._retn();
1301 //================================================================================
1303 * \brief Create meshes and import data from the CGSN file
1305 //================================================================================
1307 SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromCGNS( const char* theFileName,
1308 SMESH::DriverMED_ReadStatus& theStatus)
1309 throw ( SALOME::SALOME_Exception )
1311 Unexpect aCatch(SALOME_SalomeException);
1312 checkFileReadable( theFileName );
1314 SMESH::mesh_array_var aResult = new SMESH::mesh_array();
1317 // Retrieve nb meshes from the file
1318 DriverCGNS_Read myReader;
1319 myReader.SetFile( theFileName );
1320 Driver_Mesh::Status aStatus;
1321 int nbMeshes = myReader.GetNbMeshes(aStatus);
1322 theStatus = (SMESH::DriverMED_ReadStatus)aStatus;
1324 aResult->length( nbMeshes );
1326 { // open a new scope to make aPythonDump die before PythonDump in SMESH_Mesh::GetGroups()
1329 TPythonDump aPythonDump;
1330 aPythonDump << "([";
1332 if (theStatus == SMESH::DRS_OK)
1334 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1335 aStudyBuilder->NewCommand(); // There is a transaction
1339 // Iterate through all meshes and create mesh objects
1340 for ( ; i < nbMeshes; ++i )
1343 if (i > 0) aPythonDump << ", ";
1346 SMESH::SMESH_Mesh_var mesh = createMesh();
1347 aResult[i] = SMESH::SMESH_Mesh::_duplicate( mesh );
1349 // Read mesh data (groups are published automatically by ImportMEDFile())
1350 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
1351 ASSERT( meshServant );
1353 SMESH::DriverMED_ReadStatus status1 =
1354 meshServant->ImportCGNSFile( theFileName, i, meshName );
1355 if (status1 > theStatus)
1356 theStatus = status1;
1358 meshServant->GetImpl().GetMeshDS()->Modified();
1359 // publish mesh in the study
1360 SALOMEDS::SObject_wrap aSO;
1361 if ( CanPublishInStudy( mesh ) )
1362 aSO = PublishMesh( myCurrentStudy, mesh.in(), meshName.c_str() );
1365 if ( !aSO->_is_nil() ) {
1369 aPythonDump << "mesh_" << i;
1372 aStudyBuilder->CommitCommand();
1375 aPythonDump << "], status) = " << this << ".CreateMeshesFromCGNS(r'" << theFileName << "')";
1377 // Dump creation of groups
1378 for ( int i = 0; i < aResult->length(); ++i )
1379 SMESH::ListOfGroups_var groups = aResult[ i ]->GetGroups();
1381 THROW_SALOME_CORBA_EXCEPTION("CGNS library is unavailable", SALOME::INTERNAL_ERROR);
1384 return aResult._retn();
1387 //================================================================================
1389 * \brief Create a mesh and import data from a GMF file
1391 //================================================================================
1393 SMESH::SMESH_Mesh_ptr
1394 SMESH_Gen_i::CreateMeshesFromGMF( const char* theFileName,
1395 CORBA::Boolean theMakeRequiredGroups,
1396 SMESH::ComputeError_out theError)
1397 throw ( SALOME::SALOME_Exception )
1399 Unexpect aCatch(SALOME_SalomeException);
1400 checkFileReadable( theFileName );
1402 SMESH::SMESH_Mesh_var aMesh = createMesh();
1404 char bname[ _MAX_FNAME ];
1405 _splitpath( theFileName, NULL, NULL, bname, NULL );
1406 string aFileName = bname;
1408 string aFileName = basename( theFileName );
1410 // publish mesh in the study
1411 if ( CanPublishInStudy( aMesh ) ) {
1412 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1413 aStudyBuilder->NewCommand(); // There is a transaction
1414 SALOMEDS::SObject_wrap aSO = PublishInStudy
1415 ( myCurrentStudy, SALOMEDS::SObject::_nil(), aMesh.in(), aFileName.c_str() );
1416 aStudyBuilder->CommitCommand();
1417 if ( !aSO->_is_nil() ) {
1418 // Update Python script
1419 TPythonDump() << "("<< aSO << ", error) = " << this << ".CreateMeshesFromGMF(r'"
1420 << theFileName << "', "
1421 << theMakeRequiredGroups << " )";
1424 SMESH_Mesh_i* aServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( aMesh ).in() );
1426 theError = aServant->ImportGMFFile( theFileName, theMakeRequiredGroups );
1427 aServant->GetImpl().GetMeshDS()->Modified();
1428 return aMesh._retn();
1432 //=============================================================================
1434 * SMESH_Gen_i::IsReadyToCompute
1436 * Returns true if mesh contains enough data to be computed
1438 //=============================================================================
1440 CORBA::Boolean SMESH_Gen_i::IsReadyToCompute( SMESH::SMESH_Mesh_ptr theMesh,
1441 GEOM::GEOM_Object_ptr theShapeObject )
1442 throw ( SALOME::SALOME_Exception )
1444 Unexpect aCatch(SALOME_SalomeException);
1445 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::IsReadyToCompute" );
1447 if ( CORBA::is_nil( theShapeObject ) )
1448 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1449 SALOME::BAD_PARAM );
1451 if ( CORBA::is_nil( theMesh ) )
1452 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
1453 SALOME::BAD_PARAM );
1457 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
1458 ASSERT( meshServant );
1459 if ( meshServant ) {
1460 // get local TopoDS_Shape
1461 TopoDS_Shape myLocShape = GeomObjectToShape( theShapeObject );
1462 // call implementation
1463 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1464 return myGen.CheckAlgoState( myLocMesh, myLocShape );
1467 catch ( SALOME_Exception& S_ex ) {
1468 INFOS( "catch exception "<< S_ex.what() );
1473 //================================================================================
1475 * \brief Find SObject for an algo
1477 //================================================================================
1479 SALOMEDS::SObject_ptr SMESH_Gen_i::GetAlgoSO(const ::SMESH_Algo* algo)
1482 if ( !myCurrentStudy->_is_nil() ) {
1483 // find algo in the study
1484 CORBA::String_var compDataType = ComponentDataType();
1485 SALOMEDS::SComponent_wrap father = myCurrentStudy->FindComponent( compDataType.in() );
1486 if ( !father->_is_nil() ) {
1487 SALOMEDS::ChildIterator_wrap itBig = myCurrentStudy->NewChildIterator( father );
1488 for ( ; itBig->More(); itBig->Next() ) {
1489 SALOMEDS::SObject_wrap gotBranch = itBig->Value();
1490 if ( gotBranch->Tag() == GetAlgorithmsRootTag() ) {
1491 SALOMEDS::ChildIterator_wrap algoIt = myCurrentStudy->NewChildIterator( gotBranch );
1492 for ( ; algoIt->More(); algoIt->Next() ) {
1493 SALOMEDS::SObject_wrap algoSO = algoIt->Value();
1494 CORBA::Object_var algoIOR = SObjectToObject( algoSO );
1495 if ( !CORBA::is_nil( algoIOR )) {
1496 SMESH_Hypothesis_i* impl = SMESH::DownCast<SMESH_Hypothesis_i*>( algoIOR );
1497 if ( impl && impl->GetImpl() == algo )
1498 return algoSO._retn();
1500 } // loop on algo SO's
1503 } // SMESH component iterator
1507 return SALOMEDS::SObject::_nil();
1510 //================================================================================
1512 * \brief Return errors of mesh computation
1514 //================================================================================
1516 SMESH::compute_error_array* SMESH_Gen_i::GetComputeErrors( SMESH::SMESH_Mesh_ptr theMesh,
1517 GEOM::GEOM_Object_ptr theSubObject )
1518 throw ( SALOME::SALOME_Exception )
1520 Unexpect aCatch(SALOME_SalomeException);
1521 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetComputeErrors()" );
1523 if ( CORBA::is_nil( theSubObject ) && theMesh->HasShapeToMesh())
1524 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
1526 if ( CORBA::is_nil( theMesh ) )
1527 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1529 SMESH::compute_error_array_var error_array = new SMESH::compute_error_array;
1531 if ( SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh ))
1534 if(theMesh->HasShapeToMesh())
1535 shape = GeomObjectToShape( theSubObject );
1537 shape = SMESH_Mesh::PseudoShape();
1539 ::SMESH_Mesh& mesh = meshServant->GetImpl();
1541 error_array->length( mesh.GetMeshDS()->MaxShapeIndex() );
1544 SMESH_subMesh *sm = mesh.GetSubMesh(shape);
1545 const bool includeSelf = true, complexShapeFirst = true;
1546 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(includeSelf,
1548 while ( smIt->more() )
1551 // if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
1553 SMESH_ComputeErrorPtr error = sm->GetComputeError();
1554 if ( error && !error->IsOK() )
1556 if ( !( error->myAlgo ) &&
1557 !( error->myAlgo = sm->GetAlgo() ))
1559 SMESH::ComputeError & errStruct = error_array[ nbErr++ ];
1560 errStruct.code = -( error->myName < 0 ? error->myName + 1: error->myName ); // -1 -> 0
1561 errStruct.comment = error->myComment.c_str();
1562 errStruct.subShapeID = sm->GetId();
1563 SALOMEDS::SObject_wrap algoSO = GetAlgoSO( error->myAlgo );
1564 if ( !algoSO->_is_nil() ) {
1565 CORBA::String_var algoName = algoSO->GetName();
1566 errStruct.algoName = algoName;
1569 errStruct.algoName = error->myAlgo->GetName();
1571 errStruct.hasBadMesh = !error->myBadElements.empty();
1574 error_array->length( nbErr );
1577 catch ( SALOME_Exception& S_ex ) {
1578 INFOS( "catch exception "<< S_ex.what() );
1581 return error_array._retn();
1584 //================================================================================
1586 * \brief Return mesh elements preventing computation of a subshape
1588 //================================================================================
1590 SMESH::MeshPreviewStruct*
1591 SMESH_Gen_i::GetBadInputElements( SMESH::SMESH_Mesh_ptr theMesh,
1592 CORBA::Short theSubShapeID )
1593 throw ( SALOME::SALOME_Exception )
1595 Unexpect aCatch(SALOME_SalomeException);
1596 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetBadInputElements()" );
1598 if ( CORBA::is_nil( theMesh ) )
1599 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1601 SMESH::MeshPreviewStruct_var result = new SMESH::MeshPreviewStruct;
1604 if ( SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh ))
1606 // mesh implementation
1607 ::SMESH_Mesh& mesh = meshServant->GetImpl();
1608 // submesh by subshape id
1609 if ( SMESH_subMesh * sm = mesh.GetSubMeshContaining( theSubShapeID ))
1612 SMESH_ComputeErrorPtr error = sm->GetComputeError();
1613 if ( error && !error->myBadElements.empty())
1615 typedef map<const SMDS_MeshElement*, int > TNode2LocalIDMap;
1616 typedef TNode2LocalIDMap::iterator TNodeLocalID;
1618 // get nodes of elements and count elements
1619 TNode2LocalIDMap mapNode2LocalID;
1620 list< TNodeLocalID > connectivity;
1621 int i, nbElements = 0, nbConnNodes = 0;
1623 list<const SMDS_MeshElement*>::iterator elemIt = error->myBadElements.begin();
1624 list<const SMDS_MeshElement*>::iterator elemEnd = error->myBadElements.end();
1625 for ( ; elemIt != elemEnd; ++elemIt, ++nbElements )
1627 SMDS_ElemIteratorPtr nIt = (*elemIt)->nodesIterator();
1628 while ( nIt->more() )
1629 connectivity.push_back
1630 ( mapNode2LocalID.insert( make_pair( nIt->next(), ++nbConnNodes)).first );
1632 // fill node coords and assign local ids to the nodes
1633 int nbNodes = mapNode2LocalID.size();
1634 result->nodesXYZ.length( nbNodes );
1635 TNodeLocalID node2ID = mapNode2LocalID.begin();
1636 for ( i = 0; i < nbNodes; ++i, ++node2ID ) {
1637 node2ID->second = i;
1638 const SMDS_MeshNode* node = (const SMDS_MeshNode*) node2ID->first;
1639 result->nodesXYZ[i].x = node->X();
1640 result->nodesXYZ[i].y = node->Y();
1641 result->nodesXYZ[i].z = node->Z();
1643 // fill connectivity
1644 result->elementConnectivities.length( nbConnNodes );
1645 list< TNodeLocalID >::iterator connIt = connectivity.begin();
1646 for ( i = 0; i < nbConnNodes; ++i, ++connIt ) {
1647 result->elementConnectivities[i] = (*connIt)->second;
1649 // fill element types
1650 result->elementTypes.length( nbElements );
1651 for ( i = 0, elemIt = error->myBadElements.begin(); i <nbElements; ++i, ++elemIt )
1653 const SMDS_MeshElement* elem = *elemIt;
1654 result->elementTypes[i].SMDS_ElementType = (SMESH::ElementType) elem->GetType();
1655 result->elementTypes[i].isPoly = elem->IsPoly();
1656 result->elementTypes[i].nbNodesInElement = elem->NbNodes();
1662 catch ( SALOME_Exception& S_ex ) {
1663 INFOS( "catch exception "<< S_ex.what() );
1666 return result._retn();
1669 //================================================================================
1671 * \brief Create a group of elements preventing computation of a sub-shape
1673 //================================================================================
1675 SMESH::ListOfGroups*
1676 SMESH_Gen_i::MakeGroupsOfBadInputElements( SMESH::SMESH_Mesh_ptr theMesh,
1677 CORBA::Short theSubShapeID,
1678 const char* theGroupName )
1679 throw ( SALOME::SALOME_Exception )
1681 Unexpect aCatch(SALOME_SalomeException);
1683 SMESH::ListOfGroups_var groups;
1685 if ( CORBA::is_nil( theMesh ) )
1686 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1689 if ( SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh ))
1691 groups = meshServant->MakeGroupsOfBadInputElements( theSubShapeID, theGroupName );
1692 TPythonDump() << groups << " = " << this
1693 << ".MakeGroupsOfBadInputElements( "
1694 << theMesh << ", " << theSubShapeID << ", '" << theGroupName << "' )";
1697 catch ( SALOME_Exception& S_ex ) {
1698 INFOS( "catch exception "<< S_ex.what() );
1700 return groups._retn();
1703 //================================================================================
1705 * \brief Returns errors of hypotheses definintion
1706 * \param theMesh - the mesh
1707 * \param theSubObject - the main or sub- shape
1708 * \retval SMESH::algo_error_array* - sequence of errors
1710 //================================================================================
1712 SMESH::algo_error_array* SMESH_Gen_i::GetAlgoState( SMESH::SMESH_Mesh_ptr theMesh,
1713 GEOM::GEOM_Object_ptr theSubObject )
1714 throw ( SALOME::SALOME_Exception )
1716 Unexpect aCatch(SALOME_SalomeException);
1717 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetAlgoState()" );
1719 if ( CORBA::is_nil( theSubObject ) && theMesh->HasShapeToMesh())
1720 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
1722 if ( CORBA::is_nil( theMesh ) )
1723 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1725 SMESH::algo_error_array_var error_array = new SMESH::algo_error_array;
1727 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
1728 ASSERT( meshServant );
1729 if ( meshServant ) {
1730 TopoDS_Shape myLocShape;
1731 if(theMesh->HasShapeToMesh())
1732 myLocShape = GeomObjectToShape( theSubObject );
1734 myLocShape = SMESH_Mesh::PseudoShape();
1736 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1737 list< ::SMESH_Gen::TAlgoStateError > error_list;
1738 list< ::SMESH_Gen::TAlgoStateError >::iterator error;
1739 // call ::SMESH_Gen::GetAlgoState()
1740 myGen.GetAlgoState( myLocMesh, myLocShape, error_list );
1741 error_array->length( error_list.size() );
1743 for ( error = error_list.begin(); error != error_list.end(); ++error )
1745 // fill AlgoStateError structure
1746 SMESH::AlgoStateError & errStruct = error_array[ i++ ];
1747 errStruct.state = SMESH_Mesh_i::ConvertHypothesisStatus( error->_name );
1748 errStruct.algoDim = error->_algoDim;
1749 errStruct.isGlobalAlgo = error->_isGlobalAlgo;
1750 errStruct.algoName = "";
1751 SALOMEDS::SObject_wrap algoSO = GetAlgoSO( error->_algo );
1752 if ( !algoSO->_is_nil() ) {
1753 CORBA::String_var algoName = algoSO->GetName();
1754 errStruct.algoName = algoName.in();
1759 catch ( SALOME_Exception& S_ex ) {
1760 INFOS( "catch exception "<< S_ex.what() );
1762 return error_array._retn();
1765 //=============================================================================
1767 * SMESH_Gen_i::GetSubShapesId
1769 * Get sub-shapes unique ID's list
1771 //=============================================================================
1773 SMESH::long_array* SMESH_Gen_i::GetSubShapesId( GEOM::GEOM_Object_ptr theMainShapeObject,
1774 const SMESH::object_array& theListOfSubShapeObject )
1775 throw ( SALOME::SALOME_Exception )
1777 Unexpect aCatch(SALOME_SalomeException);
1778 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetSubShapesId" );
1780 SMESH::long_array_var shapesId = new SMESH::long_array;
1783 if ( CORBA::is_nil( theMainShapeObject ) )
1784 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1785 SALOME::BAD_PARAM );
1789 TopoDS_Shape myMainShape = GeomObjectToShape(theMainShapeObject);
1790 TopTools_IndexedMapOfShape myIndexToShape;
1791 TopExp::MapShapes(myMainShape,myIndexToShape);
1793 for ( int i = 0; i < theListOfSubShapeObject.length(); i++ )
1795 GEOM::GEOM_Object_var aShapeObject
1796 = GEOM::GEOM_Object::_narrow(theListOfSubShapeObject[i]);
1797 if ( CORBA::is_nil( aShapeObject ) )
1798 THROW_SALOME_CORBA_EXCEPTION ("bad shape object reference", \
1799 SALOME::BAD_PARAM );
1801 TopoDS_Shape locShape = GeomObjectToShape(aShapeObject);
1802 for (TopExp_Explorer exp(locShape,TopAbs_FACE); exp.More(); exp.Next())
1804 const TopoDS_Face& F = TopoDS::Face(exp.Current());
1805 setId.insert(myIndexToShape.FindIndex(F));
1806 if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(F));
1808 for (TopExp_Explorer exp(locShape,TopAbs_EDGE); exp.More(); exp.Next())
1810 const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
1811 setId.insert(myIndexToShape.FindIndex(E));
1812 if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(E));
1814 for (TopExp_Explorer exp(locShape,TopAbs_VERTEX); exp.More(); exp.Next())
1816 const TopoDS_Vertex& V = TopoDS::Vertex(exp.Current());
1817 setId.insert(myIndexToShape.FindIndex(V));
1818 if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(V));
1821 shapesId->length(setId.size());
1822 set<int>::iterator iind;
1824 for (iind = setId.begin(); iind != setId.end(); iind++)
1826 if(MYDEBUG) SCRUTE((*iind));
1827 shapesId[i] = (*iind);
1828 if(MYDEBUG) SCRUTE(shapesId[i]);
1832 catch (SALOME_Exception& S_ex)
1834 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
1837 return shapesId._retn();
1840 //=============================================================================
1842 * SMESH_Gen_i::Compute
1844 * Compute mesh on a shape
1846 //=============================================================================
1848 CORBA::Boolean SMESH_Gen_i::Compute( SMESH::SMESH_Mesh_ptr theMesh,
1849 GEOM::GEOM_Object_ptr theShapeObject )
1850 throw ( SALOME::SALOME_Exception )
1853 Unexpect aCatch(SALOME_SalomeException);
1854 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Compute" );
1856 if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
1857 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1858 SALOME::BAD_PARAM );
1860 if ( CORBA::is_nil( theMesh ) )
1861 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
1862 SALOME::BAD_PARAM );
1864 // Update Python script
1865 TPythonDump() << "isDone = " << this << ".Compute( "
1866 << theMesh << ", " << theShapeObject << ")";
1870 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
1871 meshServant->Load();
1872 ASSERT( meshServant );
1873 if ( meshServant ) {
1874 // NPAL16168: "geometrical group edition from a submesh don't modifiy mesh computation"
1875 meshServant->CheckGeomModif();
1876 // get local TopoDS_Shape
1877 TopoDS_Shape myLocShape;
1878 if(theMesh->HasShapeToMesh())
1879 myLocShape = GeomObjectToShape( theShapeObject );
1881 myLocShape = SMESH_Mesh::PseudoShape();
1882 // call implementation compute
1883 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1884 myGen.PrepareCompute( myLocMesh, myLocShape);
1885 bool ok = myGen.Compute( myLocMesh, myLocShape);
1886 meshServant->CreateGroupServants(); // algos can create groups (issue 0020918)
1887 myLocMesh.GetMeshDS()->Modified();
1891 catch ( std::bad_alloc ) {
1892 INFOS( "Compute(): lack of memory" );
1894 catch ( SALOME_Exception& S_ex ) {
1895 INFOS( "Compute(): catch exception "<< S_ex.what() );
1898 INFOS( "Compute(): unknown exception " );
1903 //=============================================================================
1905 * SMESH_Gen_i::CancelCompute
1907 * Cancel Compute mesh on a shape
1909 //=============================================================================
1911 void SMESH_Gen_i::CancelCompute( SMESH::SMESH_Mesh_ptr theMesh,
1912 GEOM::GEOM_Object_ptr theShapeObject )
1914 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
1915 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1916 TopoDS_Shape myLocShape;
1917 if(theMesh->HasShapeToMesh())
1918 myLocShape = GeomObjectToShape( theShapeObject );
1920 myLocShape = SMESH_Mesh::PseudoShape();
1921 myGen.CancelCompute( myLocMesh, myLocShape);
1924 //=============================================================================
1926 * SMESH_Gen_i::Precompute
1928 * Compute mesh as preview till indicated dimension on shape
1930 //=============================================================================
1932 SMESH::MeshPreviewStruct* SMESH_Gen_i::Precompute( SMESH::SMESH_Mesh_ptr theMesh,
1933 GEOM::GEOM_Object_ptr theShapeObject,
1934 SMESH::Dimension theDimension,
1935 SMESH::long_array& theShapesId)
1936 throw ( SALOME::SALOME_Exception )
1938 Unexpect aCatch(SALOME_SalomeException);
1939 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Precompute" );
1941 if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
1942 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1943 SALOME::BAD_PARAM );
1945 if ( CORBA::is_nil( theMesh ) )
1946 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
1947 SALOME::BAD_PARAM );
1949 SMESH::MeshPreviewStruct_var result = new SMESH::MeshPreviewStruct;
1952 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
1953 meshServant->Load();
1954 ASSERT( meshServant );
1955 if ( meshServant ) {
1956 // NPAL16168: "geometrical group edition from a submesh don't modifiy mesh computation"
1957 meshServant->CheckGeomModif();
1958 // get local TopoDS_Shape
1959 TopoDS_Shape myLocShape;
1960 if(theMesh->HasShapeToMesh())
1961 myLocShape = GeomObjectToShape( theShapeObject );
1963 return result._retn();;
1965 // call implementation compute
1966 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1968 ::MeshDimension aDim = (MeshDimension)theDimension;
1969 if ( myGen.Compute( myLocMesh, myLocShape, false, false, aDim, &shapeIds ) )
1971 int nbShapeId = shapeIds.size();
1972 theShapesId.length( nbShapeId );
1973 // iterates on shapes and collect mesh entities into mesh preview
1974 TSetOfInt::const_iterator idIt = shapeIds.begin();
1975 TSetOfInt::const_iterator idEnd = shapeIds.end();
1976 std::map< int, int > mapOfShIdNb;
1977 std::set< SMESH_TLink > setOfEdge;
1978 std::list< SMDSAbs_ElementType > listOfElemType;
1979 typedef map<const SMDS_MeshElement*, int > TNode2LocalIDMap;
1980 typedef TNode2LocalIDMap::iterator TNodeLocalID;
1981 TNode2LocalIDMap mapNode2LocalID;
1982 list< TNodeLocalID > connectivity;
1983 int i, nbConnNodes = 0;
1984 std::set< const SMESH_subMesh* > setOfVSubMesh;
1985 // iterates on shapes
1986 for ( ; idIt != idEnd; idIt++ )
1988 if ( mapOfShIdNb.find( *idIt ) != mapOfShIdNb.end() )
1990 SMESH_subMesh* sm = myLocMesh.GetSubMeshContaining(*idIt);
1991 if ( !sm || !sm->IsMeshComputed() )
1994 const TopoDS_Shape& aSh = sm->GetSubShape();
1995 const int shDim = myGen.GetShapeDim( aSh );
1996 if ( shDim < 1 || shDim > theDimension )
1999 mapOfShIdNb[ *idIt ] = 0;
2000 theShapesId[ mapOfShIdNb.size() - 1 ] = *idIt;
2002 SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
2003 if ( !smDS ) continue;
2005 if ( theDimension == SMESH::DIM_2D )
2007 SMDS_ElemIteratorPtr faceIt = smDS->GetElements();
2008 while ( faceIt->more() )
2010 const SMDS_MeshElement* face = faceIt->next();
2011 int aNbNode = face->NbNodes();
2013 aNbNode /= 2; // do not take into account additional middle nodes
2015 SMDS_MeshNode* node1 = (SMDS_MeshNode*)face->GetNode( 0 );
2016 for ( int nIndx = 0; nIndx < aNbNode; nIndx++ )
2018 SMDS_MeshNode* node2 = (SMDS_MeshNode*)face->GetNode( nIndx+1 < aNbNode ? nIndx+1 : 0 );
2019 if ( setOfEdge.insert( SMESH_TLink ( node1, node2 ) ).second )
2021 listOfElemType.push_back( SMDSAbs_Edge );
2022 connectivity.push_back
2023 ( mapNode2LocalID.insert( make_pair( node1, ++nbConnNodes)).first );
2024 connectivity.push_back
2025 ( mapNode2LocalID.insert( make_pair( node2, ++nbConnNodes)).first );
2031 else if ( theDimension == SMESH::DIM_1D )
2033 SMDS_NodeIteratorPtr nodeIt = smDS->GetNodes();
2034 while ( nodeIt->more() )
2036 listOfElemType.push_back( SMDSAbs_Node );
2037 connectivity.push_back
2038 ( mapNode2LocalID.insert( make_pair( nodeIt->next(), ++nbConnNodes)).first );
2040 // add corner nodes by first vertex from edge
2041 SMESH_subMeshIteratorPtr edgeSmIt =
2042 sm->getDependsOnIterator(/*includeSelf*/false,
2043 /*complexShapeFirst*/false);
2044 while ( edgeSmIt->more() )
2046 SMESH_subMesh* vertexSM = edgeSmIt->next();
2047 // check that vertex is not already treated
2048 if ( !setOfVSubMesh.insert( vertexSM ).second )
2050 if ( vertexSM->GetSubShape().ShapeType() != TopAbs_VERTEX )
2053 const SMESHDS_SubMesh* vertexSmDS = vertexSM->GetSubMeshDS();
2054 SMDS_NodeIteratorPtr nodeIt = vertexSmDS->GetNodes();
2055 while ( nodeIt->more() )
2057 listOfElemType.push_back( SMDSAbs_Node );
2058 connectivity.push_back
2059 ( mapNode2LocalID.insert( make_pair( nodeIt->next(), ++nbConnNodes)).first );
2065 // fill node coords and assign local ids to the nodes
2066 int nbNodes = mapNode2LocalID.size();
2067 result->nodesXYZ.length( nbNodes );
2068 TNodeLocalID node2ID = mapNode2LocalID.begin();
2069 for ( i = 0; i < nbNodes; ++i, ++node2ID ) {
2070 node2ID->second = i;
2071 const SMDS_MeshNode* node = (const SMDS_MeshNode*) node2ID->first;
2072 result->nodesXYZ[i].x = node->X();
2073 result->nodesXYZ[i].y = node->Y();
2074 result->nodesXYZ[i].z = node->Z();
2076 // fill connectivity
2077 result->elementConnectivities.length( nbConnNodes );
2078 list< TNodeLocalID >::iterator connIt = connectivity.begin();
2079 for ( i = 0; i < nbConnNodes; ++i, ++connIt ) {
2080 result->elementConnectivities[i] = (*connIt)->second;
2083 // fill element types
2084 result->elementTypes.length( listOfElemType.size() );
2085 std::list< SMDSAbs_ElementType >::const_iterator typeIt = listOfElemType.begin();
2086 std::list< SMDSAbs_ElementType >::const_iterator typeEnd = listOfElemType.end();
2087 for ( i = 0; typeIt != typeEnd; ++i, ++typeIt )
2089 SMDSAbs_ElementType elemType = *typeIt;
2090 result->elementTypes[i].SMDS_ElementType = (SMESH::ElementType)elemType;
2091 result->elementTypes[i].isPoly = false;
2092 result->elementTypes[i].nbNodesInElement = elemType == SMDSAbs_Edge ? 2 : 1;
2095 // correct number of shapes
2096 theShapesId.length( mapOfShIdNb.size() );
2100 catch ( std::bad_alloc ) {
2101 INFOS( "Precompute(): lack of memory" );
2103 catch ( SALOME_Exception& S_ex ) {
2104 INFOS( "Precompute(): catch exception "<< S_ex.what() );
2107 INFOS( "Precompute(): unknown exception " );
2109 return result._retn();
2113 //=============================================================================
2115 * SMESH_Gen_i::Evaluate
2117 * Evaluate mesh on a shape
2119 //=============================================================================
2121 SMESH::long_array* SMESH_Gen_i::Evaluate(SMESH::SMESH_Mesh_ptr theMesh,
2122 GEOM::GEOM_Object_ptr theShapeObject)
2123 // SMESH::long_array& theNbElems)
2124 throw ( SALOME::SALOME_Exception )
2126 Unexpect aCatch(SALOME_SalomeException);
2127 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Evaluate" );
2129 if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
2130 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
2131 SALOME::BAD_PARAM );
2133 if ( CORBA::is_nil( theMesh ) )
2134 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
2135 SALOME::BAD_PARAM );
2137 SMESH::long_array_var nbels = new SMESH::long_array;
2138 nbels->length(SMESH::Entity_Last);
2139 int i = SMESH::Entity_Node;
2140 for (; i < SMESH::Entity_Last; i++)
2143 // Update Python script
2144 TPythonDump() << "theNbElems = " << this << ".Evaluate( "
2145 << theMesh << ", " << theShapeObject << ")";
2149 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
2150 ASSERT( meshServant );
2151 if ( meshServant ) {
2152 // NPAL16168: "geometrical group edition from a submesh don't modifiy mesh computation"
2153 meshServant->CheckGeomModif();
2154 // get local TopoDS_Shape
2155 TopoDS_Shape myLocShape;
2156 if(theMesh->HasShapeToMesh())
2157 myLocShape = GeomObjectToShape( theShapeObject );
2159 myLocShape = SMESH_Mesh::PseudoShape();
2160 // call implementation compute
2161 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
2162 MapShapeNbElems aResMap;
2163 /*CORBA::Boolean ret =*/ myGen.Evaluate( myLocMesh, myLocShape, aResMap);
2164 MapShapeNbElemsItr anIt = aResMap.begin();
2165 for(; anIt!=aResMap.end(); anIt++) {
2166 const vector<int>& aVec = (*anIt).second;
2167 for(i = SMESH::Entity_Node; i < aVec.size(); i++) {
2168 int nbElem = aVec[i];
2169 if ( nbElem < 0 ) // algo failed, check that it has reported a message
2171 SMESH_subMesh* sm = anIt->first;
2172 SMESH_ComputeErrorPtr& error = sm->GetComputeError();
2173 const SMESH_Algo* algo = myGen.GetAlgo( myLocMesh, sm->GetSubShape());
2174 if ( (algo && !error.get()) || error->IsOK() )
2175 error.reset( new SMESH_ComputeError( COMPERR_ALGO_FAILED,"Failed to evaluate",algo));
2179 nbels[i] += aVec[i];
2183 return nbels._retn();
2186 catch ( std::bad_alloc ) {
2187 INFOS( "Evaluate(): lack of memory" );
2189 catch ( SALOME_Exception& S_ex ) {
2190 INFOS( "Evaluate(): catch exception "<< S_ex.what() );
2193 INFOS( "Evaluate(): unknown exception " );
2196 return nbels._retn();
2199 //================================================================================
2201 * \brief Return geometrical object the given element is built on
2202 * \param theMesh - the mesh the element is in
2203 * \param theElementID - the element ID
2204 * \param theGeomName - the name of the result geom object if it is not yet published
2205 * \retval GEOM::GEOM_Object_ptr - the found or just published geom object (no need to UnRegister())
2207 //================================================================================
2209 GEOM::GEOM_Object_ptr
2210 SMESH_Gen_i::GetGeometryByMeshElement( SMESH::SMESH_Mesh_ptr theMesh,
2211 CORBA::Long theElementID,
2212 const char* theGeomName)
2213 throw ( SALOME::SALOME_Exception )
2215 Unexpect aCatch(SALOME_SalomeException);
2217 GEOM::GEOM_Object_wrap geom = FindGeometryByMeshElement(theMesh, theElementID);
2218 if ( !geom->_is_nil() ) {
2219 GEOM::GEOM_Object_var mainShape = theMesh->GetShapeToMesh();
2220 GEOM::GEOM_Gen_ptr geomGen = GetGeomEngine();
2222 // try to find the corresponding SObject
2223 SALOMEDS::SObject_wrap SObj = ObjectToSObject( myCurrentStudy, geom.in() );
2224 if ( SObj->_is_nil() ) // submesh can be not found even if published
2226 // try to find published submesh
2227 GEOM::ListOfLong_var list = geom->GetSubShapeIndices();
2228 if ( !geom->IsMainShape() && list->length() == 1 ) {
2229 SALOMEDS::SObject_wrap mainSO = ObjectToSObject( myCurrentStudy, mainShape );
2230 SALOMEDS::ChildIterator_wrap it;
2231 if ( !mainSO->_is_nil() ) {
2232 it = myCurrentStudy->NewChildIterator( mainSO );
2234 if ( !it->_is_nil() ) {
2235 for ( it->InitEx(true); it->More(); it->Next() ) {
2236 SALOMEDS::SObject_wrap so = it->Value();
2237 CORBA::Object_var obj = SObjectToObject( so );
2238 GEOM::GEOM_Object_var subGeom = GEOM::GEOM_Object::_narrow( obj );
2239 if ( !subGeom->_is_nil() ) {
2240 GEOM::ListOfLong_var subList = subGeom->GetSubShapeIndices();
2241 if ( subList->length() == 1 && list[0] == subList[0] ) {
2251 if ( SObj->_is_nil() ) // publish a new subshape
2252 SObj = geomGen->AddInStudy( myCurrentStudy, geom, theGeomName, mainShape );
2254 // return only published geometry
2255 if ( !SObj->_is_nil() ) {
2256 //return geom._retn(); -- servant of geom must be UnRegister()ed;
2257 CORBA::Object_var obj = SObjectToObject( SObj );
2258 GEOM::GEOM_Object_var go = GEOM::GEOM_Object::_narrow( obj );
2262 return GEOM::GEOM_Object::_nil();
2265 //================================================================================
2267 * \brief Return geometrical object the given element is built on.
2268 * \param theMesh - the mesh the element is in
2269 * \param theElementID - the element ID
2270 * \retval GEOM::GEOM_Object_ptr - the found or created (UnRegister()!) geom object
2272 //================================================================================
2274 GEOM::GEOM_Object_ptr
2275 SMESH_Gen_i::FindGeometryByMeshElement( SMESH::SMESH_Mesh_ptr theMesh,
2276 CORBA::Long theElementID)
2277 throw ( SALOME::SALOME_Exception )
2279 Unexpect aCatch(SALOME_SalomeException);
2280 if ( CORBA::is_nil( theMesh ) )
2281 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference", SALOME::BAD_PARAM );
2283 GEOM::GEOM_Object_var mainShape = theMesh->GetShapeToMesh();
2284 GEOM::GEOM_Gen_ptr geomGen = GetGeomEngine();
2286 // get a core mesh DS
2287 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
2288 if ( meshServant && !geomGen->_is_nil() && !mainShape->_is_nil() )
2290 ::SMESH_Mesh & mesh = meshServant->GetImpl();
2291 SMESHDS_Mesh* meshDS = mesh.GetMeshDS();
2292 // find the element in mesh
2293 if ( const SMDS_MeshElement * elem = meshDS->FindElement( theElementID ) ) {
2294 // find a shape id by the element
2295 if ( int shapeID = ::SMESH_MeshEditor( &mesh ).FindShape( elem )) {
2296 // get a geom object by the shape id
2297 GEOM::GEOM_Object_var geom = ShapeToGeomObject( meshDS->IndexToShape( shapeID ));
2298 if ( geom->_is_nil() ) {
2299 // try to find a published sub-shape
2300 SALOMEDS::SObject_wrap mainSO = ObjectToSObject( myCurrentStudy, mainShape );
2301 SALOMEDS::ChildIterator_wrap it;
2302 if ( !mainSO->_is_nil() ) {
2303 it = myCurrentStudy->NewChildIterator( mainSO );
2305 if ( !it->_is_nil() ) {
2306 for ( it->InitEx(true); it->More(); it->Next() ) {
2307 SALOMEDS::SObject_wrap so = it->Value();
2308 CORBA::Object_var obj = SObjectToObject( so );
2309 GEOM::GEOM_Object_var subGeom = GEOM::GEOM_Object::_narrow( obj );
2310 if ( !subGeom->_is_nil() ) {
2311 GEOM::ListOfLong_var subList = subGeom->GetSubShapeIndices();
2312 if ( subList->length() == 1 && shapeID == subList[0] ) {
2320 if ( geom->_is_nil() ) {
2322 GEOM::GEOM_IShapesOperations_wrap op =
2323 geomGen->GetIShapesOperations( GetCurrentStudyID() );
2324 if ( !op->_is_nil() )
2325 geom = op->GetSubShape( mainShape, shapeID );
2330 if ( !geom->_is_nil() ) {
2331 GeomObjectToShape( geom ); // let geom client remember the found shape
2332 return geom._retn();
2337 return GEOM::GEOM_Object::_nil();
2340 //================================================================================
2342 * SMESH_Gen_i::Concatenate
2344 * Concatenate the given meshes into one mesh
2346 //================================================================================
2348 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::Concatenate(const SMESH::mesh_array& theMeshesArray,
2349 CORBA::Boolean theUniteIdenticalGroups,
2350 CORBA::Boolean theMergeNodesAndElements,
2351 CORBA::Double theMergeTolerance)
2352 throw ( SALOME::SALOME_Exception )
2354 return ConcatenateCommon(theMeshesArray,
2355 theUniteIdenticalGroups,
2356 theMergeNodesAndElements,
2361 //================================================================================
2363 * SMESH_Gen_i::ConcatenateWithGroups
2365 * Concatenate the given meshes into one mesh
2366 * Create the groups of all elements from initial meshes
2368 //================================================================================
2370 SMESH::SMESH_Mesh_ptr
2371 SMESH_Gen_i::ConcatenateWithGroups(const SMESH::mesh_array& theMeshesArray,
2372 CORBA::Boolean theUniteIdenticalGroups,
2373 CORBA::Boolean theMergeNodesAndElements,
2374 CORBA::Double theMergeTolerance)
2375 throw ( SALOME::SALOME_Exception )
2377 return ConcatenateCommon(theMeshesArray,
2378 theUniteIdenticalGroups,
2379 theMergeNodesAndElements,
2384 //================================================================================
2386 * SMESH_Gen_i::ConcatenateCommon
2388 * Concatenate the given meshes into one mesh
2390 //================================================================================
2392 SMESH::SMESH_Mesh_ptr
2393 SMESH_Gen_i::ConcatenateCommon(const SMESH::mesh_array& theMeshesArray,
2394 CORBA::Boolean theUniteIdenticalGroups,
2395 CORBA::Boolean theMergeNodesAndElements,
2396 CORBA::Double theMergeTolerance,
2397 CORBA::Boolean theCommonGroups)
2398 throw ( SALOME::SALOME_Exception )
2400 typedef map<int, int> TIDsMap;
2401 typedef list<SMESH::SMESH_Group_var> TListOfNewGroups;
2402 typedef map< pair<string, SMESH::ElementType>, TListOfNewGroups > TGroupsMap;
2404 TPythonDump* pPythonDump = new TPythonDump;
2405 TPythonDump& aPythonDump = *pPythonDump; // prevent dump of called methods
2408 SMESH::SMESH_Mesh_var aNewMesh = CreateEmptyMesh();
2410 SMESHDS_Mesh* aNewMeshDS = 0;
2411 if ( !aNewMesh->_is_nil() ) {
2412 SMESH_Mesh_i* aNewImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( aNewMesh ).in() );
2414 ::SMESH_Mesh& aLocMesh = aNewImpl->GetImpl();
2415 aNewMeshDS = aLocMesh.GetMeshDS();
2417 TGroupsMap aGroupsMap;
2418 TListOfNewGroups aListOfNewGroups;
2419 ::SMESH_MeshEditor aNewEditor(&aLocMesh);
2420 SMESH::ListOfGroups_var aListOfGroups = new SMESH::ListOfGroups();
2423 for ( int i = 0; i < theMeshesArray.length(); i++) {
2424 SMESH::SMESH_Mesh_var anInitMesh = theMeshesArray[i];
2425 if ( !anInitMesh->_is_nil() ) {
2426 SMESH_Mesh_i* anInitImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( anInitMesh ).in() );
2428 ::SMESH_Mesh& aInitLocMesh = anInitImpl->GetImpl();
2429 aInitLocMesh.Load();
2430 SMESHDS_Mesh* anInitMeshDS = aInitLocMesh.GetMeshDS();
2435 // loop on elements of mesh
2436 SMDS_ElemIteratorPtr itElems = anInitMeshDS->elementsIterator();
2437 const SMDS_MeshElement* anElem = 0;
2438 const SMDS_MeshElement* aNewElem = 0;
2439 int anElemNbNodes = 0;
2444 int anNbVolumes = 0;
2447 SMESH::long_array_var anIDsNodes = new SMESH::long_array();
2448 SMESH::long_array_var anIDsEdges = new SMESH::long_array();
2449 SMESH::long_array_var anIDsFaces = new SMESH::long_array();
2450 SMESH::long_array_var anIDsVolumes = new SMESH::long_array();
2451 SMESH::long_array_var anIDsBalls = new SMESH::long_array();
2453 if( theCommonGroups ) {
2454 anIDsNodes->length( anInitMeshDS->NbNodes() );
2455 anIDsEdges->length( anInitMeshDS->NbEdges() );
2456 anIDsFaces->length( anInitMeshDS->NbFaces() );
2457 anIDsVolumes->length( anInitMeshDS->NbVolumes() );
2458 anIDsBalls->length( anInitMeshDS->NbBalls() );
2461 for ( int j = 0; itElems->more(); j++) {
2462 anElem = itElems->next();
2463 SMDSAbs_ElementType anElemType = anElem->GetType();
2464 anElemNbNodes = anElem->NbNodes();
2465 std::vector<const SMDS_MeshNode*> aNodesArray (anElemNbNodes);
2467 // loop on nodes of element
2468 const SMDS_MeshNode* aNode = 0;
2469 const SMDS_MeshNode* aNewNode = 0;
2470 SMDS_ElemIteratorPtr itNodes = anElem->nodesIterator();
2472 for ( int k = 0; itNodes->more(); k++) {
2473 aNode = static_cast<const SMDS_MeshNode*>(itNodes->next());
2474 if ( nodesMap.find(aNode->GetID()) == nodesMap.end() ) {
2475 aNewNode = aNewMeshDS->AddNode(aNode->X(), aNode->Y(), aNode->Z());
2476 nodesMap.insert( make_pair(aNode->GetID(), aNewNode->GetID()) );
2477 if( theCommonGroups )
2478 anIDsNodes[anNbNodes++] = aNewNode->GetID();
2481 aNewNode = aNewMeshDS->FindNode( nodesMap.find(aNode->GetID())->second );
2482 aNodesArray[k] = aNewNode;
2485 // creates a corresponding element on existent nodes in new mesh
2486 switch ( anElem->GetEntityType() ) {
2487 case SMDSEntity_Polyhedra:
2488 if ( const SMDS_VtkVolume* aVolume =
2489 dynamic_cast<const SMDS_VtkVolume*> (anElem))
2491 aNewElem = aNewMeshDS->AddPolyhedralVolume(aNodesArray,
2492 aVolume->GetQuantities());
2493 elemsMap.insert(make_pair(anElem->GetID(), aNewElem->GetID()));
2494 if( theCommonGroups )
2495 anIDsVolumes[anNbVolumes++] = aNewElem->GetID();
2498 case SMDSEntity_Ball:
2499 if ( const SMDS_BallElement* aBall =
2500 dynamic_cast<const SMDS_BallElement*> (anElem))
2502 aNewElem = aNewEditor.AddElement(aNodesArray, SMDSAbs_Ball,
2503 /*isPoly=*/false, /*id=*/0,
2504 aBall->GetDiameter() );
2505 elemsMap.insert(make_pair(anElem->GetID(), aNewElem->GetID()));
2506 if( theCommonGroups )
2507 anIDsBalls[aNbBalls++] = aNewElem->GetID();
2512 aNewElem = aNewEditor.AddElement(aNodesArray,
2515 elemsMap.insert(make_pair(anElem->GetID(), aNewElem->GetID()));
2516 if( theCommonGroups ) {
2517 if( anElemType == SMDSAbs_Edge )
2518 anIDsEdges[anNbEdges++] = aNewElem->GetID();
2519 else if( anElemType == SMDSAbs_Face )
2520 anIDsFaces[anNbFaces++] = aNewElem->GetID();
2521 else if( anElemType == SMDSAbs_Volume )
2522 anIDsVolumes[anNbVolumes++] = aNewElem->GetID();
2528 aNewEditor.CrearLastCreated(); // forget the history
2530 // copy orphan nodes
2531 SMDS_NodeIteratorPtr itNodes = anInitMeshDS->nodesIterator();
2532 while ( itNodes->more() )
2534 const SMDS_MeshNode* aNode = itNodes->next();
2535 if ( aNode->NbInverseElements() == 0 )
2537 const SMDS_MeshNode* aNewNode =
2538 aNewMeshDS->AddNode(aNode->X(), aNode->Y(), aNode->Z());
2539 nodesMap.insert( make_pair(aNode->GetID(), aNewNode->GetID()) );
2540 if( theCommonGroups )
2541 anIDsNodes[anNbNodes++] = aNewNode->GetID();
2546 aListOfGroups = anInitImpl->GetGroups();
2547 SMESH::SMESH_GroupBase_ptr aGroup;
2549 // loop on groups of mesh
2550 SMESH::long_array_var anInitIDs = new SMESH::long_array();
2551 SMESH::long_array_var anNewIDs = new SMESH::long_array();
2552 SMESH::SMESH_Group_var aNewGroup;
2554 SMESH::ElementType aGroupType;
2555 CORBA::String_var aGroupName;
2556 if ( theCommonGroups ) {
2557 for(aGroupType=SMESH::NODE;aGroupType<=SMESH::BALL;aGroupType=(SMESH::ElementType)(aGroupType+1)) {
2559 SALOMEDS::SObject_wrap aMeshSObj = ObjectToSObject( myCurrentStudy, anInitMesh );
2561 str += aMeshSObj->GetName();
2566 switch(aGroupType) {
2569 anIDsNodes->length(anNbNodes);
2574 anIDsEdges->length(anNbEdges);
2579 anIDsFaces->length(anNbFaces);
2584 anIDsVolumes->length(anNbVolumes);
2585 anLen = anNbVolumes;
2589 anIDsBalls->length(aNbBalls);
2597 aGroupName = str.c_str();
2599 // add a new group in the mesh
2600 aNewGroup = aNewImpl->CreateGroup(aGroupType, aGroupName);
2602 switch(aGroupType) {
2604 aNewGroup->Add( anIDsNodes );
2607 aNewGroup->Add( anIDsEdges );
2610 aNewGroup->Add( anIDsFaces );
2613 aNewGroup->Add( anIDsVolumes );
2616 aNewGroup->Add( anIDsBalls );
2622 aListOfNewGroups.clear();
2623 aListOfNewGroups.push_back(aNewGroup);
2624 aGroupsMap.insert(make_pair( make_pair(aGroupName, aGroupType), aListOfNewGroups ));
2629 // check that current group name and type don't have identical ones in union mesh
2630 for (int iG = 0; iG < aListOfGroups->length(); iG++) {
2631 aGroup = aListOfGroups[iG];
2632 aListOfNewGroups.clear();
2633 aGroupType = aGroup->GetType();
2634 aGroupName = aGroup->GetName();
2636 TGroupsMap::iterator anIter = aGroupsMap.find(make_pair(aGroupName, aGroupType));
2638 // convert a list of IDs
2639 anInitIDs = aGroup->GetListOfID();
2640 anNewIDs->length(anInitIDs->length());
2641 if ( aGroupType == SMESH::NODE )
2642 for (int j = 0; j < anInitIDs->length(); j++) {
2643 anNewIDs[j] = nodesMap.find(anInitIDs[j])->second;
2646 for (int j = 0; j < anInitIDs->length(); j++) {
2647 anNewIDs[j] = elemsMap.find(anInitIDs[j])->second;
2650 // check that current group name and type don't have identical ones in union mesh
2651 if ( anIter == aGroupsMap.end() ) {
2652 // add a new group in the mesh
2653 aNewGroup = aNewImpl->CreateGroup(aGroupType, aGroupName);
2654 // add elements into new group
2655 aNewGroup->Add( anNewIDs );
2657 aListOfNewGroups.push_back(aNewGroup);
2658 aGroupsMap.insert(make_pair( make_pair(aGroupName, aGroupType), aListOfNewGroups ));
2661 else if ( theUniteIdenticalGroups ) {
2662 // unite identical groups
2663 TListOfNewGroups& aNewGroups = anIter->second;
2664 aNewGroups.front()->Add( anNewIDs );
2668 // rename identical groups
2669 aNewGroup = aNewImpl->CreateGroup(aGroupType, aGroupName);
2670 aNewGroup->Add( anNewIDs );
2672 TListOfNewGroups& aNewGroups = anIter->second;
2673 string aNewGroupName;
2674 if (aNewGroups.size() == 1) {
2675 aNewGroupName = string(aGroupName) + "_1";
2676 aNewGroups.front()->SetName(aNewGroupName.c_str());
2678 char aGroupNum[128];
2679 sprintf(aGroupNum, "%u", aNewGroups.size()+1);
2680 aNewGroupName = string(aGroupName) + "_" + string(aGroupNum);
2681 aNewGroup->SetName(aNewGroupName.c_str());
2682 aNewGroups.push_back(aNewGroup);
2689 if (theMergeNodesAndElements) {
2691 TIDSortedNodeSet aMeshNodes; // no input nodes
2692 SMESH_MeshEditor::TListOfListOfNodes aGroupsOfNodes;
2693 aNewEditor.FindCoincidentNodes( aMeshNodes, theMergeTolerance, aGroupsOfNodes );
2694 aNewEditor.MergeNodes( aGroupsOfNodes );
2696 aNewEditor.MergeEqualElements();
2701 // Update Python script
2702 aPythonDump << aNewMesh << " = " << this;
2703 if( !theCommonGroups )
2704 aPythonDump << ".Concatenate(";
2706 aPythonDump << ".ConcatenateWithGroups(";
2708 for ( int i = 0; i < theMeshesArray.length(); i++) {
2709 if (i > 0) aPythonDump << ", ";
2710 aPythonDump << theMeshesArray[i];
2712 aPythonDump << "], ";
2713 aPythonDump << theUniteIdenticalGroups << ", "
2714 << theMergeNodesAndElements << ", "
2715 << TVar( theMergeTolerance ) << ")";
2717 delete pPythonDump; // enable python dump from GetGroups()
2719 // 0020577: EDF 1164 SMESH: Bad dump of concatenate with create common groups
2720 if ( !aNewMesh->_is_nil() )
2722 SMESH::ListOfGroups_var groups = aNewMesh->GetGroups();
2725 // IPAL21468 Change icon of compound because it need not be computed.
2726 SALOMEDS::SObject_wrap aMeshSObj = ObjectToSObject( myCurrentStudy, aNewMesh );
2727 SetPixMap( aMeshSObj, "ICON_SMESH_TREE_MESH" );
2730 aNewMeshDS->Modified();
2731 return aNewMesh._retn();
2734 //================================================================================
2736 * \brief Create a mesh by copying a part of another mesh
2737 * \param meshPart - a part of mesh to copy
2738 * \param toCopyGroups - to create in the new mesh groups
2739 * the copied elements belongs to
2740 * \param toKeepIDs - to preserve IDs of the copied elements or not
2741 * \retval SMESH::SMESH_Mesh_ptr - the new mesh
2743 //================================================================================
2745 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CopyMesh(SMESH::SMESH_IDSource_ptr meshPart,
2746 const char* meshName,
2747 CORBA::Boolean toCopyGroups,
2748 CORBA::Boolean toKeepIDs)
2750 Unexpect aCatch(SALOME_SalomeException);
2752 TPythonDump* pyDump = new TPythonDump; // prevent dump from CreateMesh()
2754 // 1. Get source mesh
2756 if ( CORBA::is_nil( meshPart ))
2757 THROW_SALOME_CORBA_EXCEPTION( "bad IDSource", SALOME::BAD_PARAM );
2759 SMESH::SMESH_Mesh_var srcMesh = meshPart->GetMesh();
2760 SMESH_Mesh_i* srcMesh_i = SMESH::DownCast<SMESH_Mesh_i*>( srcMesh );
2762 THROW_SALOME_CORBA_EXCEPTION( "bad mesh of IDSource", SALOME::BAD_PARAM );
2764 SMESHDS_Mesh* srcMeshDS = srcMesh_i->GetImpl().GetMeshDS();
2766 // 2. Make a new mesh
2768 SMESH::SMESH_Mesh_var newMesh = CreateMesh(GEOM::GEOM_Object::_nil());
2769 SMESH_Mesh_i* newMesh_i = SMESH::DownCast<SMESH_Mesh_i*>( newMesh );
2771 THROW_SALOME_CORBA_EXCEPTION( "can't create a mesh", SALOME::INTERNAL_ERROR );
2772 SALOMEDS::SObject_wrap meshSO = ObjectToSObject(myCurrentStudy, newMesh );
2773 if ( !meshSO->_is_nil() )
2775 SetName( meshSO, meshName, "Mesh" );
2776 SetPixMap( meshSO, "ICON_SMESH_TREE_MESH_IMPORTED");
2778 SMESHDS_Mesh* newMeshDS = newMesh_i->GetImpl().GetMeshDS();
2779 ::SMESH_MeshEditor editor( &newMesh_i->GetImpl() );
2781 // 3. Get elements to copy
2783 SMDS_ElemIteratorPtr srcElemIt; SMDS_NodeIteratorPtr srcNodeIt;
2784 TIDSortedElemSet srcElems;
2785 SMESH::array_of_ElementType_var srcElemTypes = meshPart->GetTypes();
2786 if ( SMESH::DownCast<SMESH_Mesh_i*>( meshPart ))
2789 srcElemIt = srcMeshDS->elementsIterator();
2790 srcNodeIt = srcMeshDS->nodesIterator();
2794 SMESH::long_array_var ids = meshPart->GetIDs();
2795 if ( srcElemTypes->length() == 1 && srcElemTypes[0] == SMESH::NODE ) // group of nodes
2797 for (int i=0; i < ids->length(); i++)
2798 if ( const SMDS_MeshElement * elem = srcMeshDS->FindNode( ids[i] ))
2799 srcElems.insert( elem );
2803 for (int i=0; i < ids->length(); i++)
2804 if ( const SMDS_MeshElement * elem = srcMeshDS->FindElement( ids[i] ))
2805 srcElems.insert( elem );
2807 if ( srcElems.empty() )
2808 return newMesh._retn();
2810 typedef SMDS_SetIterator< SMDS_pElement, TIDSortedElemSet::const_iterator > ElIter;
2811 srcElemIt = SMDS_ElemIteratorPtr( new ElIter( srcElems.begin(), srcElems.end() ));
2816 typedef map<SMDS_pElement, SMDS_pElement, TIDCompare> TE2EMap;
2817 TE2EMap e2eMapByType[ SMDSAbs_NbElementTypes ];
2818 TE2EMap& n2nMap = e2eMapByType[ SMDSAbs_Node ];
2820 const SMDS_MeshNode *nSrc, *nTgt;
2821 vector< const SMDS_MeshNode* > nodes;
2822 while ( srcElemIt->more() )
2824 const SMDS_MeshElement * elem = srcElemIt->next();
2826 nodes.resize( elem->NbNodes());
2827 SMDS_ElemIteratorPtr nIt = elem->nodesIterator();
2829 for ( iN = 0; nIt->more(); ++iN )
2831 nSrc = static_cast<const SMDS_MeshNode*>( nIt->next() );
2832 nTgt = newMeshDS->FindNode( nSrc->GetID());
2834 nTgt = newMeshDS->AddNodeWithID( nSrc->X(), nSrc->Y(), nSrc->Z(), nSrc->GetID());
2839 for ( iN = 0; nIt->more(); ++iN )
2841 nSrc = static_cast<const SMDS_MeshNode*>( nIt->next() );
2842 TE2EMap::iterator n2n = n2nMap.insert( make_pair( nSrc, SMDS_pNode(0) )).first;
2844 n2n->second = newMeshDS->AddNode( nSrc->X(), nSrc->Y(), nSrc->Z() );
2845 nodes[ iN ] = (const SMDS_MeshNode*) n2n->second;
2849 if ( elem->GetType() != SMDSAbs_Node )
2851 int ID = toKeepIDs ? elem->GetID() : 0;
2852 const SMDS_MeshElement * newElem;
2853 switch ( elem->GetEntityType() ) {
2854 case SMDSEntity_Polyhedra:
2856 newElem = editor.GetMeshDS()->
2857 AddPolyhedralVolumeWithID( nodes,
2858 static_cast<const SMDS_VtkVolume*>(elem)->GetQuantities(),
2861 newElem = editor.GetMeshDS()->
2862 AddPolyhedralVolume( nodes,
2863 static_cast<const SMDS_VtkVolume*>(elem)->GetQuantities());
2865 case SMDSEntity_Ball:
2866 newElem = editor.AddElement( nodes, SMDSAbs_Ball, false, ID,
2867 static_cast<const SMDS_BallElement*>(elem)->GetDiameter());
2870 newElem = editor.AddElement( nodes,elem->GetType(),elem->IsPoly(),ID);
2872 if ( toCopyGroups && !toKeepIDs )
2873 e2eMapByType[ elem->GetType() ].insert( make_pair( elem, newElem ));
2876 } // while ( srcElemIt->more() )
2878 // 4(b). Copy free nodes
2880 if ( srcNodeIt && srcMeshDS->NbNodes() != newMeshDS->NbNodes() )
2882 while ( srcNodeIt->more() )
2884 nSrc = srcNodeIt->next();
2885 if ( nSrc->NbInverseElements() == 0 )
2888 nTgt = newMeshDS->AddNodeWithID( nSrc->X(), nSrc->Y(), nSrc->Z(), nSrc->GetID());
2890 n2nMap[ nSrc ] = newMeshDS->AddNode( nSrc->X(), nSrc->Y(), nSrc->Z() );
2897 int nbNewGroups = 0;
2900 SMESH_Mesh::GroupIteratorPtr gIt = srcMesh_i->GetImpl().GetGroups();
2901 while ( gIt->more() )
2903 SMESH_Group* group = gIt->next();
2904 const SMESHDS_GroupBase* groupDS = group->GetGroupDS();
2906 // Check group type. We copy nodal groups containing nodes of copied element
2907 SMDSAbs_ElementType groupType = groupDS->GetType();
2908 if ( groupType != SMDSAbs_Node &&
2909 newMeshDS->GetMeshInfo().NbElements( groupType ) == 0 )
2910 continue; // group type differs from types of meshPart
2912 // Find copied elements in the group
2913 vector< const SMDS_MeshElement* > groupElems;
2914 SMDS_ElemIteratorPtr eIt = groupDS->GetElements();
2917 const SMDS_MeshElement* foundElem;
2918 if ( groupType == SMDSAbs_Node )
2920 while ( eIt->more() )
2921 if (( foundElem = newMeshDS->FindNode( eIt->next()->GetID() )))
2922 groupElems.push_back( foundElem );
2926 while ( eIt->more() )
2927 if (( foundElem = newMeshDS->FindElement( eIt->next()->GetID() )))
2928 groupElems.push_back( foundElem );
2933 TE2EMap & e2eMap = e2eMapByType[ groupDS->GetType() ];
2934 if ( e2eMap.empty() ) continue;
2935 int minID = e2eMap.begin()->first->GetID();
2936 int maxID = e2eMap.rbegin()->first->GetID();
2937 TE2EMap::iterator e2e;
2938 while ( eIt->more() && groupElems.size() < e2eMap.size())
2940 const SMDS_MeshElement* e = eIt->next();
2941 if ( e->GetID() < minID || e->GetID() > maxID ) continue;
2942 if ((e2e = e2eMap.find( e )) != e2eMap.end())
2943 groupElems.push_back( e2e->second );
2947 if ( !groupElems.empty() )
2949 SMESH::SMESH_Group_var newGroupObj =
2950 newMesh->CreateGroup( SMESH::ElementType(groupType), group->GetName() );
2951 if ( SMESH_GroupBase_i* newGroup_i = SMESH::DownCast<SMESH_GroupBase_i*>( newGroupObj))
2953 SMESHDS_GroupBase * newGroupDS = newGroup_i->GetGroupDS();
2954 SMDS_MeshGroup& smdsGroup = ((SMESHDS_Group*)newGroupDS)->SMDSGroup();
2955 for ( unsigned i = 0; i < groupElems.size(); ++i )
2956 smdsGroup.Add( groupElems[i] );
2964 newMeshDS->Modified();
2966 *pyDump << newMesh << " = " << this
2967 << ".CopyMesh( " << meshPart << ", "
2968 << "'" << meshName << "', "
2969 << toCopyGroups << ", "
2970 << toKeepIDs << ")";
2972 delete pyDump; pyDump = 0; // allow dump in GetGroups()
2974 if ( nbNewGroups > 0 ) // dump created groups
2975 SMESH::ListOfGroups_var groups = newMesh->GetGroups();
2977 return newMesh._retn();
2980 //================================================================================
2982 * SMESH_Gen_i::GetMEDVersion
2984 * Get MED version of the file by its name
2986 //================================================================================
2987 CORBA::Boolean SMESH_Gen_i::GetMEDVersion(const char* theFileName,
2988 SMESH::MED_VERSION& theVersion)
2990 theVersion = SMESH::MED_V2_1;
2991 MED::EVersion aVersion = MED::GetVersionId( theFileName );
2992 switch( aVersion ) {
2993 case MED::eV2_1 : theVersion = SMESH::MED_V2_1; return true;
2994 case MED::eV2_2 : theVersion = SMESH::MED_V2_2; return true;
2995 case MED::eVUnknown : return false;
3000 //================================================================================
3002 * SMESH_Gen_i::GetMeshNames
3004 * Get names of meshes defined in file with the specified name
3006 //================================================================================
3007 SMESH::string_array* SMESH_Gen_i::GetMeshNames(const char* theFileName)
3009 SMESH::string_array_var aResult = new SMESH::string_array();
3010 MED::PWrapper aMed = MED::CrWrapper( theFileName );
3012 MED::TInt aNbMeshes = aMed->GetNbMeshes( &anErr );
3014 aResult->length( aNbMeshes );
3015 for( MED::TInt i = 0; i < aNbMeshes; i++ ) {
3016 MED::PMeshInfo aMeshInfo = aMed->GetPMeshInfo( i+1 );
3017 aResult[i] = CORBA::string_dup( aMeshInfo->GetName().c_str() );
3020 return aResult._retn();
3023 //=============================================================================
3027 * Save SMESH module's data
3029 //=============================================================================
3030 SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
3034 INFOS( "SMESH_Gen_i::Save" );
3036 // ASSERT( theComponent->GetStudy()->StudyId() == myCurrentStudy->StudyId() )
3037 // san -- in case <myCurrentStudy> differs from theComponent's study,
3038 // use that of the component
3039 if ( theComponent->GetStudy()->StudyId() != GetCurrentStudyID() )
3040 SetCurrentStudy( theComponent->GetStudy() );
3042 // Store study contents as a set of python commands
3043 SavePython(myCurrentStudy);
3045 StudyContext* myStudyContext = GetCurrentStudyContext();
3047 // Declare a byte stream
3048 SALOMEDS::TMPFile_var aStreamFile;
3050 // Obtain a temporary dir
3051 TCollection_AsciiString tmpDir =
3052 ( isMultiFile ) ? TCollection_AsciiString( ( char* )theURL ) : ( char* )SALOMEDS_Tool::GetTmpDir().c_str();
3054 // Create a sequence of files processed
3055 SALOMEDS::ListOfFileNames_var aFileSeq = new SALOMEDS::ListOfFileNames;
3056 aFileSeq->length( NUM_TMP_FILES );
3058 TCollection_AsciiString aStudyName( "" );
3060 aStudyName = ( (char*)SALOMEDS_Tool::GetNameFromPath( myCurrentStudy->URL() ).c_str() );
3062 // Set names of temporary files
3063 TCollection_AsciiString filename =
3064 aStudyName + TCollection_AsciiString( "_SMESH.hdf" ); // for SMESH data itself
3065 TCollection_AsciiString meshfile =
3066 aStudyName + TCollection_AsciiString( "_SMESH_Mesh.med" ); // for mesh data to be stored in MED file
3067 aFileSeq[ 0 ] = CORBA::string_dup( filename.ToCString() );
3068 aFileSeq[ 1 ] = CORBA::string_dup( meshfile.ToCString() );
3069 filename = tmpDir + filename;
3070 meshfile = tmpDir + meshfile;
3073 HDFdataset* aDataset;
3074 HDFgroup* aTopGroup;
3076 HDFgroup* aSubGroup;
3077 HDFgroup* aSubSubGroup;
3078 hdf_size aSize[ 1 ];
3081 //Remove the files if they exist: BugID: 11225
3082 #ifndef WIN32 /* unix functionality */
3083 TCollection_AsciiString cmd("rm -f \"");
3085 TCollection_AsciiString cmd("del /F \"");
3092 system(cmd.ToCString());
3094 // MED writer to be used by storage process
3095 DriverMED_W_SMESHDS_Mesh myWriter;
3096 myWriter.SetFile( meshfile.ToCString() );
3099 // SetStoreName() to groups before storing hypotheses to let them refer to
3100 // groups using "store name", which is "Group <group_persistent_id>"
3102 SALOMEDS::ChildIterator_wrap itBig = myCurrentStudy->NewChildIterator( theComponent );
3103 for ( ; itBig->More(); itBig->Next() ) {
3104 SALOMEDS::SObject_wrap gotBranch = itBig->Value();
3105 if ( gotBranch->Tag() > GetAlgorithmsRootTag() ) {
3106 CORBA::Object_var anObject = SObjectToObject( gotBranch );
3107 if ( !CORBA::is_nil( anObject ) ) {
3108 SMESH::SMESH_Mesh_var myMesh = SMESH::SMESH_Mesh::_narrow( anObject ) ;
3109 if ( !myMesh->_is_nil() ) {
3110 myMesh->Load(); // load from study file if not yet done
3111 TPythonDump pd; // not to dump GetGroups()
3112 SMESH::ListOfGroups_var groups = myMesh->GetGroups();
3113 pd << ""; // to avoid optimizing pd out
3114 for ( int i = 0; i < groups->length(); ++i )
3116 SMESH_GroupBase_i* grImpl = SMESH::DownCast<SMESH_GroupBase_i*>( groups[i]);
3119 CORBA::String_var objStr = GetORB()->object_to_string( grImpl->_this() );
3120 int anId = myStudyContext->findId( string( objStr.in() ) );
3122 sprintf( grpName, "Group %d", anId );
3123 SMESHDS_GroupBase* aGrpBaseDS = grImpl->GetGroupDS();
3124 aGrpBaseDS->SetStoreName( grpName );
3134 // ---> create HDF file
3135 aFile = new HDFfile( (char*) filename.ToCString() );
3136 aFile->CreateOnDisk();
3138 // --> iterator for top-level objects
3139 SALOMEDS::ChildIterator_wrap itBig = myCurrentStudy->NewChildIterator( theComponent );
3140 for ( ; itBig->More(); itBig->Next() ) {
3141 SALOMEDS::SObject_wrap gotBranch = itBig->Value();
3143 // --> hypotheses root branch (only one for the study)
3144 if ( gotBranch->Tag() == GetHypothesisRootTag() ) {
3145 // create hypotheses root HDF group
3146 aTopGroup = new HDFgroup( "Hypotheses", aFile );
3147 aTopGroup->CreateOnDisk();
3149 // iterator for all hypotheses
3150 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( gotBranch );
3151 for ( ; it->More(); it->Next() ) {
3152 SALOMEDS::SObject_wrap mySObject = it->Value();
3153 CORBA::Object_var anObject = SObjectToObject( mySObject );
3154 if ( !CORBA::is_nil( anObject ) ) {
3155 SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
3156 if ( !myHyp->_is_nil() ) {
3157 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
3159 string hypname = string( myHyp->GetName() );
3160 string libname = string( myHyp->GetLibName() );
3162 // Needs for save crossplatform libname, i.e. parth of name ( ".dll" for
3163 // WIN32 and ".so" for X-system) must be deleted
3164 int libname_len = libname.length();
3166 if( libname_len > 4 )
3167 libname.resize( libname_len - 4 );
3169 // PAL17753 (Regresion: missing hypothesis in restored study)
3170 // "lib" also should be removed from the beginning
3171 //if( libname_len > 3 )
3172 //libname.resize( libname_len - 3 );
3173 if( libname_len > 6 )
3174 libname = libname.substr( 3, libname_len - 3 - 3 );
3176 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3177 int id = myStudyContext->findId( string( objStr.in() ) );
3178 string hypdata = string( myImpl->SaveTo() );
3180 // for each hypothesis create HDF group basing on its id
3181 char hypGrpName[30];
3182 sprintf( hypGrpName, "Hypothesis %d", id );
3183 aGroup = new HDFgroup( hypGrpName, aTopGroup );
3184 aGroup->CreateOnDisk();
3185 // --> type name of hypothesis
3186 aSize[ 0 ] = hypname.length() + 1;
3187 aDataset = new HDFdataset( "Name", aGroup, HDF_STRING, aSize, 1 );
3188 aDataset->CreateOnDisk();
3189 aDataset->WriteOnDisk( ( char* )( hypname.c_str() ) );
3190 aDataset->CloseOnDisk();
3191 // --> server plugin library name of hypothesis
3192 aSize[ 0 ] = libname.length() + 1;
3193 aDataset = new HDFdataset( "LibName", aGroup, HDF_STRING, aSize, 1 );
3194 aDataset->CreateOnDisk();
3195 aDataset->WriteOnDisk( ( char* )( libname.c_str() ) );
3196 aDataset->CloseOnDisk();
3197 // --> persistent data of hypothesis
3198 aSize[ 0 ] = hypdata.length() + 1;
3199 aDataset = new HDFdataset( "Data", aGroup, HDF_STRING, aSize, 1 );
3200 aDataset->CreateOnDisk();
3201 aDataset->WriteOnDisk( ( char* )( hypdata.c_str() ) );
3202 aDataset->CloseOnDisk();
3203 // close hypothesis HDF group
3204 aGroup->CloseOnDisk();
3209 // close hypotheses root HDF group
3210 aTopGroup->CloseOnDisk();
3212 // --> algorithms root branch (only one for the study)
3213 else if ( gotBranch->Tag() == GetAlgorithmsRootTag() ) {
3214 // create algorithms root HDF group
3215 aTopGroup = new HDFgroup( "Algorithms", aFile );
3216 aTopGroup->CreateOnDisk();
3218 // iterator for all algorithms
3219 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( gotBranch );
3220 for ( ; it->More(); it->Next() ) {
3221 SALOMEDS::SObject_wrap mySObject = it->Value();
3222 CORBA::Object_var anObject = SObjectToObject( mySObject );
3223 if ( !CORBA::is_nil( anObject ) ) {
3224 SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
3225 if ( !myHyp->_is_nil() ) {
3226 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
3228 string hypname = string( myHyp->GetName() );
3229 string libname = string( myHyp->GetLibName() );
3231 // Needs for save crossplatform libname, i.e. parth of name ( ".dll" for
3232 // WIN32 and ".so" for X-system) must be deleted
3233 int libname_len = libname.length();
3235 if( libname_len > 4 )
3236 libname.resize( libname_len - 4 );
3238 // PAL17753 (Regresion: missing hypothesis in restored study)
3239 // "lib" also should be removed from the beginning
3240 //if( libname_len > 3 )
3241 //libname.resize( libname_len - 3 );
3242 if( libname_len > 6 )
3243 libname = libname.substr( 3, libname_len - 3 - 3 );
3245 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3246 int id = myStudyContext->findId( string( objStr.in() ) );
3247 string hypdata = string( myImpl->SaveTo() );
3249 // for each algorithm create HDF group basing on its id
3250 char hypGrpName[30];
3251 sprintf( hypGrpName, "Algorithm %d", id );
3252 aGroup = new HDFgroup( hypGrpName, aTopGroup );
3253 aGroup->CreateOnDisk();
3254 // --> type name of algorithm
3255 aSize[0] = hypname.length() + 1;
3256 aDataset = new HDFdataset( "Name", aGroup, HDF_STRING, aSize, 1 );
3257 aDataset->CreateOnDisk();
3258 aDataset->WriteOnDisk( ( char* )( hypname.c_str() ) );
3259 aDataset->CloseOnDisk();
3260 // --> server plugin library name of hypothesis
3261 aSize[0] = libname.length() + 1;
3262 aDataset = new HDFdataset( "LibName", aGroup, HDF_STRING, aSize, 1 );
3263 aDataset->CreateOnDisk();
3264 aDataset->WriteOnDisk( ( char* )( libname.c_str() ) );
3265 aDataset->CloseOnDisk();
3266 // --> persistent data of algorithm
3267 aSize[0] = hypdata.length() + 1;
3268 aDataset = new HDFdataset( "Data", aGroup, HDF_STRING, aSize, 1 );
3269 aDataset->CreateOnDisk();
3270 aDataset->WriteOnDisk( ( char* )( hypdata.c_str() ) );
3271 aDataset->CloseOnDisk();
3272 // close algorithm HDF group
3273 aGroup->CloseOnDisk();
3278 // close algorithms root HDF group
3279 aTopGroup->CloseOnDisk();
3281 // --> mesh objects roots branches
3282 else if ( gotBranch->Tag() > GetAlgorithmsRootTag() ) {
3283 CORBA::Object_var anObject = SObjectToObject( gotBranch );
3284 if ( !CORBA::is_nil( anObject ) ) {
3285 SMESH::SMESH_Mesh_var myMesh = SMESH::SMESH_Mesh::_narrow( anObject ) ;
3286 if ( !myMesh->_is_nil() ) {
3287 SMESH_Mesh_i* myImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( myMesh ).in() );
3289 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3290 int id = myStudyContext->findId( string( objStr.in() ) );
3291 ::SMESH_Mesh& myLocMesh = myImpl->GetImpl();
3292 SMESHDS_Mesh* mySMESHDSMesh = myLocMesh.GetMeshDS();
3293 bool hasShape = myLocMesh.HasShapeToMesh();
3295 // for each mesh open the HDF group basing on its id
3296 char meshGrpName[ 30 ];
3297 sprintf( meshGrpName, "Mesh %d", id );
3298 aTopGroup = new HDFgroup( meshGrpName, aFile );
3299 aTopGroup->CreateOnDisk();
3301 // --> put dataset to hdf file which is a flag that mesh has data
3302 string strHasData = "0";
3303 // check if the mesh is not empty
3304 if ( mySMESHDSMesh->NbNodes() > 0 ) {
3305 // write mesh data to med file
3306 myWriter.SetMesh( mySMESHDSMesh );
3307 myWriter.SetMeshId( id );
3310 aSize[ 0 ] = strHasData.length() + 1;
3311 aDataset = new HDFdataset( "Has data", aTopGroup, HDF_STRING, aSize, 1 );
3312 aDataset->CreateOnDisk();
3313 aDataset->WriteOnDisk( ( char* )( strHasData.c_str() ) );
3314 aDataset->CloseOnDisk();
3317 // for each mesh open the HDF group basing on its auto color parameter
3318 char meshAutoColorName[ 30 ];
3319 sprintf( meshAutoColorName, "AutoColorMesh %d", id );
3321 anAutoColor[0] = myImpl->GetAutoColor();
3323 aDataset = new HDFdataset( meshAutoColorName, aTopGroup, HDF_INT32, aSize, 1 );
3324 aDataset->CreateOnDisk();
3325 aDataset->WriteOnDisk( anAutoColor );
3326 aDataset->CloseOnDisk();
3328 // issue 0020693. Store _isModified flag
3329 int isModified = myLocMesh.GetIsModified();
3331 aDataset = new HDFdataset( "_isModified", aTopGroup, HDF_INT32, aSize, 1 );
3332 aDataset->CreateOnDisk();
3333 aDataset->WriteOnDisk( &isModified );
3334 aDataset->CloseOnDisk();
3336 // issue 20918. Store Persistent Id of SMESHDS_Mesh
3337 int meshPersistentId = mySMESHDSMesh->GetPersistentId();
3339 aDataset = new HDFdataset( "meshPersistentId", aTopGroup, HDF_INT32, aSize, 1 );
3340 aDataset->CreateOnDisk();
3341 aDataset->WriteOnDisk( &meshPersistentId );
3342 aDataset->CloseOnDisk();
3344 // write reference on a shape if exists
3345 SALOMEDS::SObject_wrap myRef;
3346 bool shapeRefFound = false;
3347 bool found = gotBranch->FindSubObject( GetRefOnShapeTag(), myRef.inout() );
3349 SALOMEDS::SObject_wrap myShape;
3350 bool ok = myRef->ReferencedObject( myShape.inout() );
3352 shapeRefFound = (! CORBA::is_nil( myShape->GetObject() ));
3353 string myRefOnObject = myShape->GetID();
3354 if ( shapeRefFound && myRefOnObject.length() > 0 ) {
3355 aSize[ 0 ] = myRefOnObject.length() + 1;
3356 aDataset = new HDFdataset( "Ref on shape", aTopGroup, HDF_STRING, aSize, 1 );
3357 aDataset->CreateOnDisk();
3358 aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3359 aDataset->CloseOnDisk();
3364 // write applied hypotheses if exist
3365 SALOMEDS::SObject_wrap myHypBranch;
3366 found = gotBranch->FindSubObject( GetRefOnAppliedHypothesisTag(), myHypBranch.inout() );
3367 if ( found && !shapeRefFound && hasShape) { // remove applied hyps
3368 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( myHypBranch );
3370 if ( found && (shapeRefFound || !hasShape) ) {
3371 aGroup = new HDFgroup( "Applied Hypotheses", aTopGroup );
3372 aGroup->CreateOnDisk();
3374 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( myHypBranch );
3376 for ( ; it->More(); it->Next() ) {
3377 SALOMEDS::SObject_wrap mySObject = it->Value();
3378 SALOMEDS::SObject_wrap myRefOnHyp;
3379 bool ok = mySObject->ReferencedObject( myRefOnHyp.inout() );
3381 // san - it is impossible to recover applied hypotheses
3382 // using their entries within Load() method,
3383 // for there are no AttributeIORs in the study when Load() is working.
3384 // Hence, it is better to store persistent IDs of hypotheses as references to them
3386 //string myRefOnObject = myRefOnHyp->GetID();
3387 CORBA::Object_var anObject = SObjectToObject( myRefOnHyp );
3388 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3389 int id = myStudyContext->findId( string( objStr.in() ) );
3390 //if ( myRefOnObject.length() > 0 ) {
3391 //aSize[ 0 ] = myRefOnObject.length() + 1;
3392 char hypName[ 30 ], hypId[ 30 ];
3393 sprintf( hypName, "Hyp %d", ++hypNb );
3394 sprintf( hypId, "%d", id );
3395 aSize[ 0 ] = strlen( hypId ) + 1;
3396 aDataset = new HDFdataset( hypName, aGroup, HDF_STRING, aSize, 1 );
3397 aDataset->CreateOnDisk();
3398 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3399 aDataset->WriteOnDisk( hypId );
3400 aDataset->CloseOnDisk();
3404 aGroup->CloseOnDisk();
3407 // write applied algorithms if exist
3408 SALOMEDS::SObject_wrap myAlgoBranch;
3409 found = gotBranch->FindSubObject( GetRefOnAppliedAlgorithmsTag(),
3410 myAlgoBranch.inout() );
3411 if ( found && !shapeRefFound && hasShape) { // remove applied algos
3412 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( myAlgoBranch );
3414 if ( found && (shapeRefFound || !hasShape)) {
3415 aGroup = new HDFgroup( "Applied Algorithms", aTopGroup );
3416 aGroup->CreateOnDisk();
3418 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( myAlgoBranch );
3420 for ( ; it->More(); it->Next() ) {
3421 SALOMEDS::SObject_wrap mySObject = it->Value();
3422 SALOMEDS::SObject_wrap myRefOnAlgo;
3423 bool ok = mySObject->ReferencedObject( myRefOnAlgo.inout() );
3425 // san - it is impossible to recover applied algorithms
3426 // using their entries within Load() method,
3427 // for there are no AttributeIORs in the study when Load() is working.
3428 // Hence, it is better to store persistent IDs of algorithms as references to them
3430 //string myRefOnObject = myRefOnAlgo->GetID();
3431 CORBA::Object_var anObject = SObjectToObject( myRefOnAlgo );
3432 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3433 int id = myStudyContext->findId( string( objStr.in() ) );
3434 //if ( myRefOnObject.length() > 0 ) {
3435 //aSize[ 0 ] = myRefOnObject.length() + 1;
3436 char algoName[ 30 ], algoId[ 30 ];
3437 sprintf( algoName, "Algo %d", ++algoNb );
3438 sprintf( algoId, "%d", id );
3439 aSize[ 0 ] = strlen( algoId ) + 1;
3440 aDataset = new HDFdataset( algoName, aGroup, HDF_STRING, aSize, 1 );
3441 aDataset->CreateOnDisk();
3442 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3443 aDataset->WriteOnDisk( algoId );
3444 aDataset->CloseOnDisk();
3448 aGroup->CloseOnDisk();
3451 // --> submesh objects sub-branches
3453 for ( int i = GetSubMeshOnVertexTag(); i <= GetSubMeshOnCompoundTag(); i++ ) {
3454 SALOMEDS::SObject_wrap mySubmeshBranch;
3455 found = gotBranch->FindSubObject( i, mySubmeshBranch.inout() );
3457 if ( found ) // check if there is shape reference in submeshes
3459 bool hasShapeRef = false;
3460 SALOMEDS::ChildIterator_wrap itSM =
3461 myCurrentStudy->NewChildIterator( mySubmeshBranch );
3462 for ( ; itSM->More(); itSM->Next() ) {
3463 SALOMEDS::SObject_wrap mySubRef, myShape, mySObject = itSM->Value();
3464 if ( mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef.inout() ))
3465 mySubRef->ReferencedObject( myShape.inout() );
3466 if ( !CORBA::is_nil( myShape ) && !CORBA::is_nil( myShape->GetObject() ))
3469 { // remove one submesh
3470 if ( shapeRefFound )
3471 { // unassign hypothesis
3472 SMESH::SMESH_subMesh_var mySubMesh =
3473 SMESH::SMESH_subMesh::_narrow( SObjectToObject( mySObject ));
3474 if ( !mySubMesh->_is_nil() ) {
3475 int shapeID = mySubMesh->GetId();
3476 TopoDS_Shape S = mySMESHDSMesh->IndexToShape( shapeID );
3477 const list<const SMESHDS_Hypothesis*>& hypList =
3478 mySMESHDSMesh->GetHypothesis( S );
3479 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
3480 while ( hyp != hypList.end() ) {
3481 int hypID = (*hyp++)->GetID(); // goto next hyp here because
3482 myLocMesh.RemoveHypothesis( S, hypID ); // hypList changes here
3486 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( mySObject );
3488 } // loop on submeshes of a type
3489 if ( !shapeRefFound || !hasShapeRef ) { // remove the whole submeshes branch
3490 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( mySubmeshBranch );
3493 } // end check if there is shape reference in submeshes
3495 char name_meshgroup[ 30 ];
3496 if ( i == GetSubMeshOnVertexTag() )
3497 strcpy( name_meshgroup, "SubMeshes On Vertex" );
3498 else if ( i == GetSubMeshOnEdgeTag() )
3499 strcpy( name_meshgroup, "SubMeshes On Edge" );
3500 else if ( i == GetSubMeshOnWireTag() )
3501 strcpy( name_meshgroup, "SubMeshes On Wire" );
3502 else if ( i == GetSubMeshOnFaceTag() )
3503 strcpy( name_meshgroup, "SubMeshes On Face" );
3504 else if ( i == GetSubMeshOnShellTag() )
3505 strcpy( name_meshgroup, "SubMeshes On Shell" );
3506 else if ( i == GetSubMeshOnSolidTag() )
3507 strcpy( name_meshgroup, "SubMeshes On Solid" );
3508 else if ( i == GetSubMeshOnCompoundTag() )
3509 strcpy( name_meshgroup, "SubMeshes On Compound" );
3511 // for each type of submeshes create container HDF group
3512 aGroup = new HDFgroup( name_meshgroup, aTopGroup );
3513 aGroup->CreateOnDisk();
3515 // iterator for all submeshes of given type
3516 SALOMEDS::ChildIterator_wrap itSM = myCurrentStudy->NewChildIterator( mySubmeshBranch );
3517 for ( ; itSM->More(); itSM->Next() ) {
3518 SALOMEDS::SObject_wrap mySObject = itSM->Value();
3519 CORBA::Object_var anSubObject = SObjectToObject( mySObject );
3520 if ( !CORBA::is_nil( anSubObject ))
3522 SMESH::SMESH_subMesh_var mySubMesh = SMESH::SMESH_subMesh::_narrow( anSubObject ) ;
3523 CORBA::String_var objStr = GetORB()->object_to_string( anSubObject );
3524 int subid = myStudyContext->findId( string( objStr.in() ) );
3526 // for each mesh open the HDF group basing on its id
3527 char submeshGrpName[ 30 ];
3528 sprintf( submeshGrpName, "SubMesh %d", subid );
3529 aSubGroup = new HDFgroup( submeshGrpName, aGroup );
3530 aSubGroup->CreateOnDisk();
3532 // write reference on a shape, already checked if it exists
3533 SALOMEDS::SObject_wrap mySubRef, myShape;
3534 if ( mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef.inout() ))
3535 mySubRef->ReferencedObject( myShape.inout() );
3536 string myRefOnObject = myShape->GetID();
3537 if ( myRefOnObject.length() > 0 ) {
3538 aSize[ 0 ] = myRefOnObject.length() + 1;
3539 aDataset = new HDFdataset( "Ref on shape", aSubGroup, HDF_STRING, aSize, 1 );
3540 aDataset->CreateOnDisk();
3541 aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3542 aDataset->CloseOnDisk();
3545 // write applied hypotheses if exist
3546 SALOMEDS::SObject_wrap mySubHypBranch;
3547 found = mySObject->FindSubObject( GetRefOnAppliedHypothesisTag(),
3548 mySubHypBranch.inout() );
3550 aSubSubGroup = new HDFgroup( "Applied Hypotheses", aSubGroup );
3551 aSubSubGroup->CreateOnDisk();
3553 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( mySubHypBranch );
3555 for ( ; it->More(); it->Next() ) {
3556 SALOMEDS::SObject_wrap mySubSObject = it->Value();
3557 SALOMEDS::SObject_wrap myRefOnHyp;
3558 bool ok = mySubSObject->ReferencedObject( myRefOnHyp.inout() );
3560 //string myRefOnObject = myRefOnHyp->GetID();
3561 CORBA::Object_var anObject = SObjectToObject( myRefOnHyp );
3562 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3563 int id = myStudyContext->findId( string( objStr.in() ) );
3564 //if ( myRefOnObject.length() > 0 ) {
3565 //aSize[ 0 ] = myRefOnObject.length() + 1;
3566 char hypName[ 30 ], hypId[ 30 ];
3567 sprintf( hypName, "Hyp %d", ++hypNb );
3568 sprintf( hypId, "%d", id );
3569 aSize[ 0 ] = strlen( hypId ) + 1;
3570 aDataset = new HDFdataset( hypName, aSubSubGroup, HDF_STRING, aSize, 1 );
3571 aDataset->CreateOnDisk();
3572 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3573 aDataset->WriteOnDisk( hypId );
3574 aDataset->CloseOnDisk();
3578 aSubSubGroup->CloseOnDisk();
3581 // write applied algorithms if exist
3582 SALOMEDS::SObject_wrap mySubAlgoBranch;
3583 found = mySObject->FindSubObject( GetRefOnAppliedAlgorithmsTag(),
3584 mySubAlgoBranch.inout() );
3586 aSubSubGroup = new HDFgroup( "Applied Algorithms", aSubGroup );
3587 aSubSubGroup->CreateOnDisk();
3589 SALOMEDS::ChildIterator_wrap it =
3590 myCurrentStudy->NewChildIterator( mySubAlgoBranch );
3592 for ( ; it->More(); it->Next() ) {
3593 SALOMEDS::SObject_wrap mySubSObject = it->Value();
3594 SALOMEDS::SObject_wrap myRefOnAlgo;
3595 bool ok = mySubSObject->ReferencedObject( myRefOnAlgo.inout() );
3597 //string myRefOnObject = myRefOnAlgo->GetID();
3598 CORBA::Object_var anObject = SObjectToObject( myRefOnAlgo );
3599 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3600 int id = myStudyContext->findId( string( objStr.in() ) );
3601 //if ( myRefOnObject.length() > 0 ) {
3602 //aSize[ 0 ] = myRefOnObject.length() + 1;
3603 char algoName[ 30 ], algoId[ 30 ];
3604 sprintf( algoName, "Algo %d", ++algoNb );
3605 sprintf( algoId, "%d", id );
3606 aSize[ 0 ] = strlen( algoId ) + 1;
3607 aDataset = new HDFdataset( algoName, aSubSubGroup, HDF_STRING, aSize, 1 );
3608 aDataset->CreateOnDisk();
3609 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3610 aDataset->WriteOnDisk( algoId );
3611 aDataset->CloseOnDisk();
3615 aSubSubGroup->CloseOnDisk();
3617 // close submesh HDF group
3618 aSubGroup->CloseOnDisk();
3621 // close container of submeshes by type HDF group
3622 aGroup->CloseOnDisk();
3625 // All sub-meshes will be stored in MED file
3626 // .. will NOT (PAL 12992)
3627 //if ( shapeRefFound )
3628 //myWriter.AddAllSubMeshes();
3630 // store submesh order if any
3631 const TListOfListOfInt& theOrderIds = myLocMesh.GetMeshOrder();
3632 if ( theOrderIds.size() ) {
3633 char order_list[ 30 ];
3634 strcpy( order_list, "Mesh Order" );
3635 // count number of submesh ids
3637 TListOfListOfInt::const_iterator idIt = theOrderIds.begin();
3638 for ( ; idIt != theOrderIds.end(); idIt++ )
3639 nbIDs += (*idIt).size();
3640 // number of values = number of IDs +
3641 // number of lists (for separators) - 1
3642 int* smIDs = new int [ nbIDs + theOrderIds.size() - 1 ];
3643 idIt = theOrderIds.begin();
3644 for ( int i = 0; idIt != theOrderIds.end(); idIt++ ) {
3645 const TListOfInt& idList = *idIt;
3646 if (idIt != theOrderIds.begin()) // not first list
3647 smIDs[ i++ ] = -1/* *idList.size()*/; // separator between lists
3648 // dump submesh ids from current list
3649 TListOfInt::const_iterator id_smId = idList.begin();
3650 for( ; id_smId != idList.end(); id_smId++ )
3651 smIDs[ i++ ] = *id_smId;
3654 aSize[ 0 ] = nbIDs + theOrderIds.size() - 1;
3656 aDataset = new HDFdataset( order_list, aTopGroup, HDF_INT32, aSize, 1 );
3657 aDataset->CreateOnDisk();
3658 aDataset->WriteOnDisk( smIDs );
3659 aDataset->CloseOnDisk();
3664 // groups root sub-branch
3665 SALOMEDS::SObject_wrap myGroupsBranch;
3666 for ( int i = GetNodeGroupsTag(); i <= GetBallElementsGroupsTag(); i++ ) {
3667 found = gotBranch->FindSubObject( i, myGroupsBranch.inout() );
3669 char name_group[ 30 ];
3670 if ( i == GetNodeGroupsTag() )
3671 strcpy( name_group, "Groups of Nodes" );
3672 else if ( i == GetEdgeGroupsTag() )
3673 strcpy( name_group, "Groups of Edges" );
3674 else if ( i == GetFaceGroupsTag() )
3675 strcpy( name_group, "Groups of Faces" );
3676 else if ( i == GetVolumeGroupsTag() )
3677 strcpy( name_group, "Groups of Volumes" );
3678 else if ( i == Get0DElementsGroupsTag() )
3679 strcpy( name_group, "Groups of 0D Elements" );
3680 else if ( i == GetBallElementsGroupsTag() )
3681 strcpy( name_group, "Groups of Balls" );
3683 aGroup = new HDFgroup( name_group, aTopGroup );
3684 aGroup->CreateOnDisk();
3686 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( myGroupsBranch );
3687 for ( ; it->More(); it->Next() ) {
3688 SALOMEDS::SObject_wrap mySObject = it->Value();
3689 CORBA::Object_var aSubObject = SObjectToObject( mySObject );
3690 if ( !CORBA::is_nil( aSubObject ) ) {
3691 SMESH_GroupBase_i* myGroupImpl =
3692 dynamic_cast<SMESH_GroupBase_i*>( GetServant( aSubObject ).in() );
3695 SMESHDS_GroupBase* aGrpBaseDS = myGroupImpl->GetGroupDS();
3699 CORBA::String_var objStr = GetORB()->object_to_string( aSubObject );
3700 int anId = myStudyContext->findId( string( objStr.in() ) );
3702 // For each group, create a dataset named "Group <group_persistent_id>"
3703 // and store the group's user name into it
3704 const char* grpName = aGrpBaseDS->GetStoreName();
3705 char* aUserName = myGroupImpl->GetName();
3706 aSize[ 0 ] = strlen( aUserName ) + 1;
3708 aDataset = new HDFdataset( grpName, aGroup, HDF_STRING, aSize, 1 );
3709 aDataset->CreateOnDisk();
3710 aDataset->WriteOnDisk( aUserName );
3711 aDataset->CloseOnDisk();
3714 // For each group, create a dataset named "Group <group_persistent_id> Color"
3715 // and store the group's color into it
3716 char grpColorName[ 30 ];
3717 sprintf( grpColorName, "ColorGroup %d", anId );
3718 SALOMEDS::Color aColor = myGroupImpl->GetColor();
3720 anRGB[ 0 ] = aColor.R;
3721 anRGB[ 1 ] = aColor.G;
3722 anRGB[ 2 ] = aColor.B;
3724 aDataset = new HDFdataset( grpColorName, aGroup, HDF_FLOAT64, aSize, 1 );
3725 aDataset->CreateOnDisk();
3726 aDataset->WriteOnDisk( anRGB );
3727 aDataset->CloseOnDisk();
3729 // Pass SMESHDS_Group to MED writer
3730 SMESHDS_Group* aGrpDS = dynamic_cast<SMESHDS_Group*>( aGrpBaseDS );
3732 myWriter.AddGroup( aGrpDS );
3734 // write reference on a shape if exists
3735 SMESHDS_GroupOnGeom* aGeomGrp =
3736 dynamic_cast<SMESHDS_GroupOnGeom*>( aGrpBaseDS );
3738 SALOMEDS::SObject_wrap mySubRef, myShape;
3739 if (mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef.inout() ) &&
3740 mySubRef->ReferencedObject( myShape.inout() ) &&
3741 !CORBA::is_nil( myShape->GetObject() ))
3743 string myRefOnObject = myShape->GetID();
3744 if ( myRefOnObject.length() > 0 ) {
3745 char aRefName[ 30 ];
3746 sprintf( aRefName, "Ref on shape %d", anId);
3747 aSize[ 0 ] = myRefOnObject.length() + 1;
3748 aDataset = new HDFdataset(aRefName, aGroup, HDF_STRING, aSize, 1);
3749 aDataset->CreateOnDisk();
3750 aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3751 aDataset->CloseOnDisk();
3754 else // shape ref is invalid:
3756 // save a group on geometry as ordinary group
3757 myWriter.AddGroup( aGeomGrp );
3760 else if ( SMESH_GroupOnFilter_i* aFilterGrp_i =
3761 dynamic_cast<SMESH_GroupOnFilter_i*>( myGroupImpl ))
3763 std::string str = aFilterGrp_i->FilterToString();
3764 std::string hdfGrpName = "Filter " + SMESH_Comment(anId);
3765 aSize[ 0 ] = str.length() + 1;
3766 aDataset = new HDFdataset( hdfGrpName.c_str(), aGroup, HDF_STRING, aSize, 1);
3767 aDataset->CreateOnDisk();
3768 aDataset->WriteOnDisk( ( char* )( str.c_str() ) );
3769 aDataset->CloseOnDisk();
3773 aGroup->CloseOnDisk();
3777 if ( strcmp( strHasData.c_str(), "1" ) == 0 )
3779 // Flush current mesh information into MED file
3782 // save info on nb of elements
3783 SMESH_PreMeshInfo::SaveToFile( myImpl, id, aFile );
3785 // maybe a shape was deleted in the study
3786 if ( !shapeRefFound && !mySMESHDSMesh->ShapeToMesh().IsNull() && hasShape) {
3787 TopoDS_Shape nullShape;
3788 myLocMesh.ShapeToMesh( nullShape ); // remove shape referring data
3791 SMESHDS_SubMeshIteratorPtr smIt = mySMESHDSMesh->SubMeshes();
3796 aGroup = new HDFgroup( "Submeshes", aTopGroup );
3797 aGroup->CreateOnDisk();
3799 // each element belongs to one or none submesh,
3800 // so for each node/element, we store a submesh ID
3802 // Store submesh IDs
3803 for ( int isNode = 0; isNode < 2; ++isNode )
3805 SMDS_ElemIteratorPtr eIt =
3806 mySMESHDSMesh->elementsIterator( isNode ? SMDSAbs_Node : SMDSAbs_All );
3807 int nbElems = isNode ? mySMESHDSMesh->NbNodes() : mySMESHDSMesh->GetMeshInfo().NbElements();
3810 std::vector<int> smIDs; smIDs.reserve( nbElems );
3811 while ( eIt->more() )
3812 if ( const SMDS_MeshElement* e = eIt->next())
3813 smIDs.push_back( e->getshapeId() );
3815 aSize[ 0 ] = nbElems;
3816 string aDSName( isNode ? "Node Submeshes" : "Element Submeshes");
3817 aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_INT32, aSize, 1 );
3818 aDataset->CreateOnDisk();
3819 aDataset->WriteOnDisk( & smIDs[0] );
3820 aDataset->CloseOnDisk();
3823 aGroup->CloseOnDisk();
3825 // Store node positions on sub-shapes (SMDS_Position):
3826 // ----------------------------------------------------
3828 aGroup = new HDFgroup( "Node Positions", aTopGroup );
3829 aGroup->CreateOnDisk();
3831 // in aGroup, create 5 datasets to contain:
3832 // "Nodes on Edges" - ID of node on edge
3833 // "Edge positions" - U parameter on node on edge
3834 // "Nodes on Faces" - ID of node on face
3835 // "Face U positions" - U parameter of node on face
3836 // "Face V positions" - V parameter of node on face
3838 // Find out nb of nodes on edges and faces
3839 // Collect corresponing sub-meshes
3840 int nbEdgeNodes = 0, nbFaceNodes = 0;
3841 list<SMESHDS_SubMesh*> aEdgeSM, aFaceSM;
3842 // loop on SMESHDS_SubMesh'es
3843 while ( smIt->more() )
3845 SMESHDS_SubMesh* aSubMesh = const_cast< SMESHDS_SubMesh* >( smIt->next() );
3846 if ( aSubMesh->IsComplexSubmesh() )
3847 continue; // submesh containing other submeshs
3848 int nbNodes = aSubMesh->NbNodes();
3849 if ( nbNodes == 0 ) continue;
3851 int aShapeID = aSubMesh->GetID();
3852 if ( aShapeID < 1 || aShapeID > mySMESHDSMesh->MaxShapeIndex() )
3854 int aShapeType = mySMESHDSMesh->IndexToShape( aShapeID ).ShapeType();
3855 // write only SMDS_FacePosition and SMDS_EdgePosition
3856 switch ( aShapeType ) {
3858 nbFaceNodes += nbNodes;
3859 aFaceSM.push_back( aSubMesh );
3862 nbEdgeNodes += nbNodes;
3863 aEdgeSM.push_back( aSubMesh );
3869 // Treat positions on edges or faces
3870 for ( int onFace = 0; onFace < 2; onFace++ )
3872 // Create arrays to store in datasets
3873 int iNode = 0, nbNodes = ( onFace ? nbFaceNodes : nbEdgeNodes );
3874 if (!nbNodes) continue;
3875 int* aNodeIDs = new int [ nbNodes ];
3876 double* aUPos = new double [ nbNodes ];
3877 double* aVPos = ( onFace ? new double[ nbNodes ] : 0 );
3880 // loop on sub-meshes
3881 list<SMESHDS_SubMesh*> * pListSM = ( onFace ? &aFaceSM : &aEdgeSM );
3882 list<SMESHDS_SubMesh*>::iterator itSM = pListSM->begin();
3883 for ( ; itSM != pListSM->end(); itSM++ )
3885 SMESHDS_SubMesh* aSubMesh = (*itSM);
3887 SMDS_NodeIteratorPtr itNode = aSubMesh->GetNodes();
3888 // loop on nodes in aSubMesh
3889 while ( itNode->more() )
3892 const SMDS_MeshNode* node = itNode->next();
3893 aNodeIDs [ iNode ] = node->GetID();
3896 const SMDS_PositionPtr pos = node->GetPosition();
3897 if ( onFace ) { // on FACE
3898 const SMDS_FacePosition* fPos =
3899 dynamic_cast<const SMDS_FacePosition*>( pos );
3901 aUPos[ iNode ] = fPos->GetUParameter();
3902 aVPos[ iNode ] = fPos->GetVParameter();
3909 const SMDS_EdgePosition* ePos =
3910 dynamic_cast<const SMDS_EdgePosition*>( pos );
3912 aUPos[ iNode ] = ePos->GetUParameter();
3918 } // loop on nodes in aSubMesh
3919 } // loop on sub-meshes
3924 aSize[ 0 ] = nbNodes;
3926 string aDSName( onFace ? "Nodes on Faces" : "Nodes on Edges");
3927 aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_INT32, aSize, 1 );
3928 aDataset->CreateOnDisk();
3929 aDataset->WriteOnDisk( aNodeIDs );
3930 aDataset->CloseOnDisk();
3933 aDSName = ( onFace ? "Face U positions" : "Edge positions");
3934 aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_FLOAT64, aSize, 1);
3935 aDataset->CreateOnDisk();
3936 aDataset->WriteOnDisk( aUPos );
3937 aDataset->CloseOnDisk();
3940 aDataset = new HDFdataset( "Face V positions", aGroup, HDF_FLOAT64, aSize, 1);
3941 aDataset->CreateOnDisk();
3942 aDataset->WriteOnDisk( aVPos );
3943 aDataset->CloseOnDisk();
3948 if ( aVPos ) delete [] aVPos;
3950 } // treat positions on edges or faces
3952 // close "Node Positions" group
3953 aGroup->CloseOnDisk();
3955 } // if ( there are submeshes in SMESHDS_Mesh )
3958 // close mesh HDF group
3959 aTopGroup->CloseOnDisk();
3967 aFile->CloseOnDisk();
3970 // Convert temporary files to stream
3971 aStreamFile = SALOMEDS_Tool::PutFilesToStream( tmpDir.ToCString(), aFileSeq.in(), isMultiFile );
3973 // Remove temporary files and directory
3975 SALOMEDS_Tool::RemoveTemporaryFiles( tmpDir.ToCString(), aFileSeq.in(), true );
3977 INFOS( "SMESH_Gen_i::Save() completed" );
3978 return aStreamFile._retn();
3981 //=============================================================================
3983 * SMESH_Gen_i::SaveASCII
3985 * Save SMESH module's data in ASCII format
3987 //=============================================================================
3989 SALOMEDS::TMPFile* SMESH_Gen_i::SaveASCII( SALOMEDS::SComponent_ptr theComponent,
3991 bool isMultiFile ) {
3992 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::SaveASCII" );
3993 SALOMEDS::TMPFile_var aStreamFile = Save( theComponent, theURL, isMultiFile );
3994 return aStreamFile._retn();
3996 //after usual saving needs to encipher binary to text string
3997 //Any binary symbol will be represent as "|xx" () hexadecimal format number
3998 int size = aStreamFile.in().length();
3999 _CORBA_Octet* buffer = new _CORBA_Octet[size*3+1];
4000 for ( int i = 0; i < size; i++ )
4001 sprintf( (char*)&(buffer[i*3]), "|%02x", aStreamFile[i] );
4003 buffer[size * 3] = '\0';
4005 SALOMEDS::TMPFile_var anAsciiStreamFile = new SALOMEDS::TMPFile(size*3, size*3, buffer, 1);
4007 return anAsciiStreamFile._retn();
4010 //=============================================================================
4012 * SMESH_Gen_i::loadGeomData
4014 * Load GEOM module data
4016 //=============================================================================
4018 void SMESH_Gen_i::loadGeomData( SALOMEDS::SComponent_ptr theCompRoot )
4020 if ( theCompRoot->_is_nil() )
4023 SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow( theCompRoot->GetStudy() );
4024 if ( aStudy->_is_nil() )
4027 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
4028 aStudyBuilder->LoadWith( theCompRoot, GetGeomEngine() );
4031 //=============================================================================
4035 * Load SMESH module's data
4037 //=============================================================================
4039 bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
4040 const SALOMEDS::TMPFile& theStream,
4044 INFOS( "SMESH_Gen_i::Load" );
4046 if ( theComponent->GetStudy()->StudyId() != GetCurrentStudyID() )
4047 SetCurrentStudy( theComponent->GetStudy() );
4049 /* if( !theComponent->_is_nil() )
4051 //SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow( theComponent->GetStudy() );
4052 if( !myCurrentStudy->FindComponent( "GEOM" )->_is_nil() )
4053 loadGeomData( myCurrentStudy->FindComponent( "GEOM" ) );
4056 StudyContext* myStudyContext = GetCurrentStudyContext();
4058 // Get temporary files location
4059 TCollection_AsciiString tmpDir =
4060 ( char* )( isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir().c_str() );
4062 INFOS( "THE URL++++++++++++++" );
4064 INFOS( "THE TMP PATH+++++++++" );
4067 // Convert the stream into sequence of files to process
4068 SALOMEDS::ListOfFileNames_var aFileSeq = SALOMEDS_Tool::PutStreamToFiles( theStream,
4071 TCollection_AsciiString aStudyName( "" );
4072 if ( isMultiFile ) {
4073 CORBA::String_var url = myCurrentStudy->URL();
4074 aStudyName = (char*)SALOMEDS_Tool::GetNameFromPath( url.in() ).c_str();
4076 // Set names of temporary files
4077 TCollection_AsciiString filename = tmpDir + aStudyName + "_SMESH.hdf";
4078 TCollection_AsciiString meshfile = tmpDir + aStudyName + "_SMESH_Mesh.med";
4082 HDFdataset* aDataset;
4083 HDFgroup* aTopGroup;
4085 HDFgroup* aSubGroup;
4086 HDFgroup* aSubSubGroup;
4089 // ---> open HDF file
4090 aFile = new HDFfile( (char*) filename.ToCString() );
4092 aFile->OpenOnDisk( HDF_RDONLY );
4094 catch ( HDFexception ) {
4095 INFOS( "Load(): " << filename << " not found!" );
4099 TPythonDump pd; // prevent dump during loading
4101 // DriverMED_R_SMESHDS_Mesh myReader;
4102 // myReader.SetFile( meshfile.ToCString() );
4104 // For PAL13473 ("Repetitive mesh") implementation.
4105 // New dependencies between SMESH objects are established:
4106 // now hypotheses can refer to meshes, shapes and other hypotheses.
4107 // To keep data consistent, the following order of data restoration
4109 // 1. Create hypotheses
4110 // 2. Create all meshes
4111 // 3. Load hypotheses' data
4114 list< pair< SMESH_Hypothesis_i*, string > > hypDataList;
4115 list< pair< SMESH_Mesh_i*, HDFgroup* > > meshGroupList;
4117 // get total number of top-level groups
4118 int aNbGroups = aFile->nInternalObjects();
4119 if ( aNbGroups > 0 ) {
4120 // --> in first turn we should read&create hypotheses
4121 if ( aFile->ExistInternalObject( "Hypotheses" ) ) {
4122 // open hypotheses root HDF group
4123 aTopGroup = new HDFgroup( "Hypotheses", aFile );
4124 aTopGroup->OpenOnDisk();
4126 // get number of hypotheses
4127 int aNbObjects = aTopGroup->nInternalObjects();
4128 for ( int j = 0; j < aNbObjects; j++ ) {
4129 // try to identify hypothesis
4130 char hypGrpName[ HDF_NAME_MAX_LEN+1 ];
4131 aTopGroup->InternalObjectIndentify( j, hypGrpName );
4133 if ( string( hypGrpName ).substr( 0, 10 ) == string( "Hypothesis" ) ) {
4134 // open hypothesis group
4135 aGroup = new HDFgroup( hypGrpName, aTopGroup );
4136 aGroup->OpenOnDisk();
4138 // --> get hypothesis id
4139 int id = atoi( string( hypGrpName ).substr( 10 ).c_str() );
4144 // get number of datasets
4145 int aNbSubObjects = aGroup->nInternalObjects();
4146 for ( int k = 0; k < aNbSubObjects; k++ ) {
4148 char name_of_subgroup[ HDF_NAME_MAX_LEN+1 ];
4149 aGroup->InternalObjectIndentify( k, name_of_subgroup );
4150 // --> get hypothesis name
4151 if ( strcmp( name_of_subgroup, "Name" ) == 0 ) {
4152 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4153 aDataset->OpenOnDisk();
4154 size = aDataset->GetSize();
4155 char* hypname_str = new char[ size ];
4156 aDataset->ReadFromDisk( hypname_str );
4157 hypname = string( hypname_str );
4158 delete [] hypname_str;
4159 aDataset->CloseOnDisk();
4161 // --> get hypothesis plugin library name
4162 if ( strcmp( name_of_subgroup, "LibName" ) == 0 ) {
4163 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4164 aDataset->OpenOnDisk();
4165 size = aDataset->GetSize();
4166 char* libname_str = new char[ size ];
4167 aDataset->ReadFromDisk( libname_str );
4168 if(MYDEBUG) SCRUTE( libname_str );
4169 libname = string( libname_str );
4170 delete [] libname_str;
4171 aDataset->CloseOnDisk();
4173 // --> get hypothesis data
4174 if ( strcmp( name_of_subgroup, "Data" ) == 0 ) {
4175 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4176 aDataset->OpenOnDisk();
4177 size = aDataset->GetSize();
4178 char* hypdata_str = new char[ size ];
4179 aDataset->ReadFromDisk( hypdata_str );
4180 hypdata = string( hypdata_str );
4181 delete [] hypdata_str;
4182 aDataset->CloseOnDisk();
4185 // close hypothesis HDF group
4186 aGroup->CloseOnDisk();
4188 // --> restore hypothesis from data
4189 if ( id > 0 && !hypname.empty()/* && !hypdata.empty()*/ ) { // VSR : persistent data can be empty
4190 if(MYDEBUG) MESSAGE("VSR - load hypothesis : id = " << id <<
4191 ", name = " << hypname.c_str() << ", persistent string = " << hypdata.c_str());
4192 SMESH::SMESH_Hypothesis_var myHyp;
4194 try { // protect persistence mechanism against exceptions
4195 myHyp = this->createHypothesis( hypname.c_str(), libname.c_str() );
4198 INFOS( "Exception during hypothesis creation" );
4201 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
4203 // myImpl->LoadFrom( hypdata.c_str() );
4204 hypDataList.push_back( make_pair( myImpl, hypdata ));
4205 CORBA::String_var iorString = GetORB()->object_to_string( myHyp );
4206 int newId = myStudyContext->findId( iorString.in() );
4207 myStudyContext->mapOldToNew( id, newId );
4210 if(MYDEBUG) MESSAGE( "VSR - SMESH_Gen::Load - can't get servant" );
4214 // close hypotheses root HDF group
4215 aTopGroup->CloseOnDisk();
4219 // --> then we should read&create algorithms
4220 if ( aFile->ExistInternalObject( "Algorithms" ) ) {
4221 // open algorithms root HDF group
4222 aTopGroup = new HDFgroup( "Algorithms", aFile );
4223 aTopGroup->OpenOnDisk();
4225 // get number of algorithms
4226 int aNbObjects = aTopGroup->nInternalObjects();
4227 for ( int j = 0; j < aNbObjects; j++ ) {
4228 // try to identify algorithm
4229 char hypGrpName[ HDF_NAME_MAX_LEN+1 ];
4230 aTopGroup->InternalObjectIndentify( j, hypGrpName );
4232 if ( string( hypGrpName ).substr( 0, 9 ) == string( "Algorithm" ) ) {
4233 // open algorithm group
4234 aGroup = new HDFgroup( hypGrpName, aTopGroup );
4235 aGroup->OpenOnDisk();
4237 // --> get algorithm id
4238 int id = atoi( string( hypGrpName ).substr( 9 ).c_str() );
4243 // get number of datasets
4244 int aNbSubObjects = aGroup->nInternalObjects();
4245 for ( int k = 0; k < aNbSubObjects; k++ ) {
4247 char name_of_subgroup[ HDF_NAME_MAX_LEN+1 ];
4248 aGroup->InternalObjectIndentify( k, name_of_subgroup );
4249 // --> get algorithm name
4250 if ( strcmp( name_of_subgroup, "Name" ) == 0 ) {
4251 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4252 aDataset->OpenOnDisk();
4253 size = aDataset->GetSize();
4254 char* hypname_str = new char[ size ];
4255 aDataset->ReadFromDisk( hypname_str );
4256 hypname = string( hypname_str );
4257 delete [] hypname_str;
4258 aDataset->CloseOnDisk();
4260 // --> get algorithm plugin library name
4261 if ( strcmp( name_of_subgroup, "LibName" ) == 0 ) {
4262 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4263 aDataset->OpenOnDisk();
4264 size = aDataset->GetSize();
4265 char* libname_str = new char[ size ];
4266 aDataset->ReadFromDisk( libname_str );
4267 if(MYDEBUG) SCRUTE( libname_str );
4268 libname = string( libname_str );
4269 delete [] libname_str;
4270 aDataset->CloseOnDisk();
4272 // --> get algorithm data
4273 if ( strcmp( name_of_subgroup, "Data" ) == 0 ) {
4274 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4275 aDataset->OpenOnDisk();
4276 size = aDataset->GetSize();
4277 char* hypdata_str = new char[ size ];
4278 aDataset->ReadFromDisk( hypdata_str );
4279 if(MYDEBUG) SCRUTE( hypdata_str );
4280 hypdata = string( hypdata_str );
4281 delete [] hypdata_str;
4282 aDataset->CloseOnDisk();
4285 // close algorithm HDF group
4286 aGroup->CloseOnDisk();
4288 // --> restore algorithm from data
4289 if ( id > 0 && !hypname.empty()/* && !hypdata.empty()*/ ) { // VSR : persistent data can be empty
4290 if(MYDEBUG) MESSAGE("VSR - load algo : id = " << id <<
4291 ", name = " << hypname.c_str() << ", persistent string = " << hypdata.c_str());
4292 SMESH::SMESH_Hypothesis_var myHyp;
4294 try { // protect persistence mechanism against exceptions
4295 myHyp = this->createHypothesis( hypname.c_str(), libname.c_str() );
4298 INFOS( "Exception during hypothesis creation" );
4301 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
4303 //myImpl->LoadFrom( hypdata.c_str() );
4304 hypDataList.push_back( make_pair( myImpl, hypdata ));
4305 CORBA::String_var iorString = GetORB()->object_to_string( myHyp );
4306 int newId = myStudyContext->findId( iorString.in() );
4307 myStudyContext->mapOldToNew( id, newId );
4310 if(MYDEBUG) MESSAGE( "VSR - SMESH_Gen::Load - can't get servant" );
4314 // close algorithms root HDF group
4315 aTopGroup->CloseOnDisk();
4319 // --> the rest groups should be meshes
4320 for ( int i = 0; i < aNbGroups; i++ ) {
4321 // identify next group
4322 char meshName[ HDF_NAME_MAX_LEN+1 ];
4323 aFile->InternalObjectIndentify( i, meshName );
4325 if ( string( meshName ).substr( 0, 4 ) == string( "Mesh" ) ) {
4327 int id = atoi( string( meshName ).substr( 4 ).c_str() );
4331 // open mesh HDF group
4332 aTopGroup = new HDFgroup( meshName, aFile );
4333 aTopGroup->OpenOnDisk();
4335 // get number of child HDF objects
4336 int aNbObjects = aTopGroup->nInternalObjects();
4337 if ( aNbObjects > 0 ) {
4339 if(MYDEBUG) MESSAGE( "VSR - load mesh : id = " << id );
4340 SMESH::SMESH_Mesh_var myNewMesh = this->createMesh();
4341 SMESH_Mesh_i* myNewMeshImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( myNewMesh ).in() );
4342 if ( !myNewMeshImpl )
4344 meshGroupList.push_back( make_pair( myNewMeshImpl, aTopGroup ));
4346 CORBA::String_var iorString = GetORB()->object_to_string( myNewMesh );
4347 int newId = myStudyContext->findId( iorString.in() );
4348 myStudyContext->mapOldToNew( id, newId );
4351 // try to read and set auto color flag
4352 char aMeshAutoColorName[ 30 ];
4353 sprintf( aMeshAutoColorName, "AutoColorMesh %d", id);
4354 if( aTopGroup->ExistInternalObject( aMeshAutoColorName ) )
4356 aDataset = new HDFdataset( aMeshAutoColorName, aTopGroup );
4357 aDataset->OpenOnDisk();
4358 size = aDataset->GetSize();
4359 int* anAutoColor = new int[ size ];
4360 aDataset->ReadFromDisk( anAutoColor );
4361 aDataset->CloseOnDisk();
4362 myNewMeshImpl->GetImpl().SetAutoColor( (bool)anAutoColor[0] );
4363 delete [] anAutoColor;
4366 // try to read and set reference to shape
4367 GEOM::GEOM_Object_var aShapeObject;
4368 if ( aTopGroup->ExistInternalObject( "Ref on shape" ) ) {
4369 // load mesh "Ref on shape" - it's an entry to SObject
4370 aDataset = new HDFdataset( "Ref on shape", aTopGroup );
4371 aDataset->OpenOnDisk();
4372 size = aDataset->GetSize();
4373 char* refFromFile = new char[ size ];
4374 aDataset->ReadFromDisk( refFromFile );
4375 aDataset->CloseOnDisk();
4376 if ( strlen( refFromFile ) > 0 ) {
4377 SALOMEDS::SObject_wrap shapeSO = myCurrentStudy->FindObjectID( refFromFile );
4379 // Make sure GEOM data are loaded first
4380 //loadGeomData( shapeSO->GetFatherComponent() );
4382 CORBA::Object_var shapeObject = SObjectToObject( shapeSO );
4383 if ( !CORBA::is_nil( shapeObject ) ) {
4384 aShapeObject = GEOM::GEOM_Object::_narrow( shapeObject );
4385 if ( !aShapeObject->_is_nil() )
4386 myNewMeshImpl->SetShape( aShapeObject );
4389 delete [] refFromFile;
4392 // issue 20918. Restore Persistent Id of SMESHDS_Mesh
4393 if( aTopGroup->ExistInternalObject( "meshPersistentId" ) )
4395 aDataset = new HDFdataset( "meshPersistentId", aTopGroup );
4396 aDataset->OpenOnDisk();
4397 size = aDataset->GetSize();
4398 int* meshPersistentId = new int[ size ];
4399 aDataset->ReadFromDisk( meshPersistentId );
4400 aDataset->CloseOnDisk();
4401 myNewMeshImpl->GetImpl().GetMeshDS()->SetPersistentId( *meshPersistentId );
4402 delete [] meshPersistentId;
4408 // As all object that can be referred by hypothesis are created,
4409 // we can restore hypothesis data
4411 list< pair< SMESH_Hypothesis_i*, string > >::iterator hyp_data;
4412 for ( hyp_data = hypDataList.begin(); hyp_data != hypDataList.end(); ++hyp_data )
4414 SMESH_Hypothesis_i* hyp = hyp_data->first;
4415 string & data = hyp_data->second;
4416 hyp->LoadFrom( data.c_str() );
4419 // Restore the rest mesh data
4421 list< pair< SMESH_Mesh_i*, HDFgroup* > >::iterator meshi_group;
4422 for ( meshi_group = meshGroupList.begin(); meshi_group != meshGroupList.end(); ++meshi_group )
4424 aTopGroup = meshi_group->second;
4425 SMESH_Mesh_i* myNewMeshImpl = meshi_group->first;
4426 //::SMESH_Mesh& myLocMesh = myNewMeshImpl->GetImpl();
4427 //SMESHDS_Mesh* mySMESHDSMesh = myLocMesh.GetMeshDS();
4429 GEOM::GEOM_Object_var aShapeObject = myNewMeshImpl->GetShapeToMesh();
4430 bool hasData = false;
4433 CORBA::String_var iorString = GetORB()->object_to_string( myNewMeshImpl->_this() );
4434 int newId = myStudyContext->findId( iorString.in() );
4435 int id = myStudyContext->getOldId( newId );
4437 // try to find mesh data dataset
4438 if ( aTopGroup->ExistInternalObject( "Has data" ) ) {
4439 // load mesh "has data" flag
4440 aDataset = new HDFdataset( "Has data", aTopGroup );
4441 aDataset->OpenOnDisk();
4442 size = aDataset->GetSize();
4443 char* strHasData = new char[ size ];
4444 aDataset->ReadFromDisk( strHasData );
4445 aDataset->CloseOnDisk();
4446 if ( strcmp( strHasData, "1") == 0 ) {
4447 // read mesh data from MED file
4448 // myReader.SetMesh( mySMESHDSMesh );
4449 // myReader.SetMeshId( id );
4450 // myReader.Perform();
4453 delete [] strHasData;
4456 // Try to get applied ALGORITHMS (mesh is not cleared by algo addition because
4457 // nodes and elements are not yet put into sub-meshes)
4458 if ( aTopGroup->ExistInternalObject( "Applied Algorithms" ) ) {
4459 aGroup = new HDFgroup( "Applied Algorithms", aTopGroup );
4460 aGroup->OpenOnDisk();
4461 // get number of applied algorithms
4462 int aNbSubObjects = aGroup->nInternalObjects();
4463 if(MYDEBUG) MESSAGE( "VSR - number of applied algos " << aNbSubObjects );
4464 for ( int j = 0; j < aNbSubObjects; j++ ) {
4465 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4466 aGroup->InternalObjectIndentify( j, name_dataset );
4467 // check if it is an algorithm
4468 if ( string( name_dataset ).substr( 0, 4 ) == string( "Algo" ) ) {
4469 aDataset = new HDFdataset( name_dataset, aGroup );
4470 aDataset->OpenOnDisk();
4471 size = aDataset->GetSize();
4472 char* refFromFile = new char[ size ];
4473 aDataset->ReadFromDisk( refFromFile );
4474 aDataset->CloseOnDisk();
4475 // san - it is impossible to recover applied algorithms using their entries within Load() method
4476 //SALOMEDS::SObject_wrap hypSO = myCurrentStudy->FindObjectID( refFromFile );
4477 //CORBA::Object_var hypObject = SObjectToObject( hypSO );
4478 int id = atoi( refFromFile );
4479 delete [] refFromFile;
4480 string anIOR = myStudyContext->getIORbyOldId( id );
4481 if ( !anIOR.empty() ) {
4482 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
4483 if ( !CORBA::is_nil( hypObject ) ) {
4484 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
4485 if ( !anHyp->_is_nil() && (!aShapeObject->_is_nil()
4486 || !myNewMeshImpl->HasShapeToMesh()) )
4487 myNewMeshImpl->addHypothesis( aShapeObject, anHyp );
4492 aGroup->CloseOnDisk();
4495 // try to get applied hypotheses
4496 if ( aTopGroup->ExistInternalObject( "Applied Hypotheses" ) ) {
4497 aGroup = new HDFgroup( "Applied Hypotheses", aTopGroup );
4498 aGroup->OpenOnDisk();
4499 // get number of applied hypotheses
4500 int aNbSubObjects = aGroup->nInternalObjects();
4501 for ( int j = 0; j < aNbSubObjects; j++ ) {
4502 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4503 aGroup->InternalObjectIndentify( j, name_dataset );
4504 // check if it is a hypothesis
4505 if ( string( name_dataset ).substr( 0, 3 ) == string( "Hyp" ) ) {
4506 aDataset = new HDFdataset( name_dataset, aGroup );
4507 aDataset->OpenOnDisk();
4508 size = aDataset->GetSize();
4509 char* refFromFile = new char[ size ];
4510 aDataset->ReadFromDisk( refFromFile );
4511 aDataset->CloseOnDisk();
4512 // san - it is impossible to recover applied hypotheses using their entries within Load() method
4513 //SALOMEDS::SObject_wrap hypSO = myCurrentStudy->FindObjectID( refFromFile );
4514 //CORBA::Object_var hypObject = SObjectToObject( hypSO );
4515 int id = atoi( refFromFile );
4516 delete [] refFromFile;
4517 string anIOR = myStudyContext->getIORbyOldId( id );
4518 if ( !anIOR.empty() ) {
4519 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
4520 if ( !CORBA::is_nil( hypObject ) ) {
4521 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
4522 if ( !anHyp->_is_nil() && (!aShapeObject->_is_nil()
4523 || !myNewMeshImpl->HasShapeToMesh()) )
4524 myNewMeshImpl->addHypothesis( aShapeObject, anHyp );
4529 aGroup->CloseOnDisk();
4532 // --> try to find SUB-MESHES containers for each type of submesh
4533 for ( int j = GetSubMeshOnVertexTag(); j <= GetSubMeshOnCompoundTag(); j++ ) {
4534 const char* name_meshgroup;
4535 if ( j == GetSubMeshOnVertexTag() )
4536 name_meshgroup = "SubMeshes On Vertex";
4537 else if ( j == GetSubMeshOnEdgeTag() )
4538 name_meshgroup = "SubMeshes On Edge";
4539 else if ( j == GetSubMeshOnWireTag() )
4540 name_meshgroup = "SubMeshes On Wire";
4541 else if ( j == GetSubMeshOnFaceTag() )
4542 name_meshgroup = "SubMeshes On Face";
4543 else if ( j == GetSubMeshOnShellTag() )
4544 name_meshgroup = "SubMeshes On Shell";
4545 else if ( j == GetSubMeshOnSolidTag() )
4546 name_meshgroup = "SubMeshes On Solid";
4547 else if ( j == GetSubMeshOnCompoundTag() )
4548 name_meshgroup = "SubMeshes On Compound";
4550 // try to get submeshes container HDF group
4551 if ( aTopGroup->ExistInternalObject( name_meshgroup ) ) {
4552 // open submeshes containers HDF group
4553 aGroup = new HDFgroup( name_meshgroup, aTopGroup );
4554 aGroup->OpenOnDisk();
4556 // get number of submeshes
4557 int aNbSubMeshes = aGroup->nInternalObjects();
4558 for ( int k = 0; k < aNbSubMeshes; k++ ) {
4560 char name_submeshgroup[ HDF_NAME_MAX_LEN+1 ];
4561 aGroup->InternalObjectIndentify( k, name_submeshgroup );
4562 if ( strncmp( name_submeshgroup, "SubMesh", 7 ) == 0 ) {
4563 // --> get submesh id
4564 int subid = atoi( name_submeshgroup + 7 );
4567 // open submesh HDF group
4568 aSubGroup = new HDFgroup( name_submeshgroup, aGroup );
4569 aSubGroup->OpenOnDisk();
4571 // try to read and set reference to subshape
4572 GEOM::GEOM_Object_var aSubShapeObject;
4573 SMESH::SMESH_subMesh_var aSubMesh;
4575 if ( aSubGroup->ExistInternalObject( "Ref on shape" ) ) {
4576 // load submesh "Ref on shape" - it's an entry to SObject
4577 aDataset = new HDFdataset( "Ref on shape", aSubGroup );
4578 aDataset->OpenOnDisk();
4579 size = aDataset->GetSize();
4580 char* refFromFile = new char[ size ];
4581 aDataset->ReadFromDisk( refFromFile );
4582 aDataset->CloseOnDisk();
4583 if ( strlen( refFromFile ) > 0 ) {
4584 SALOMEDS::SObject_wrap subShapeSO = myCurrentStudy->FindObjectID( refFromFile );
4585 CORBA::Object_var subShapeObject = SObjectToObject( subShapeSO );
4586 if ( !CORBA::is_nil( subShapeObject ) ) {
4587 aSubShapeObject = GEOM::GEOM_Object::_narrow( subShapeObject );
4588 if ( !aSubShapeObject->_is_nil() )
4589 aSubMesh = SMESH::SMESH_subMesh::_duplicate
4590 ( myNewMeshImpl->createSubMesh( aSubShapeObject ) );
4591 if ( aSubMesh->_is_nil() )
4593 string iorSubString = GetORB()->object_to_string( aSubMesh );
4594 int newSubId = myStudyContext->findId( iorSubString );
4595 myStudyContext->mapOldToNew( subid, newSubId );
4600 if ( aSubMesh->_is_nil() )
4603 // try to get applied algorithms
4604 if ( aSubGroup->ExistInternalObject( "Applied Algorithms" ) ) {
4605 // open "applied algorithms" HDF group
4606 aSubSubGroup = new HDFgroup( "Applied Algorithms", aSubGroup );
4607 aSubSubGroup->OpenOnDisk();
4608 // get number of applied algorithms
4609 int aNbSubObjects = aSubSubGroup->nInternalObjects();
4610 for ( int l = 0; l < aNbSubObjects; l++ ) {
4611 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4612 aSubSubGroup->InternalObjectIndentify( l, name_dataset );
4613 // check if it is an algorithm
4614 if ( strncmp( name_dataset, "Algo", 4 ) == 0 ) {
4615 aDataset = new HDFdataset( name_dataset, aSubSubGroup );
4616 aDataset->OpenOnDisk();
4617 size = aDataset->GetSize();
4618 char* refFromFile = new char[ size ];
4619 aDataset->ReadFromDisk( refFromFile );
4620 aDataset->CloseOnDisk();
4622 int id = atoi( refFromFile );
4623 string anIOR = myStudyContext->getIORbyOldId( id );
4624 if ( !anIOR.empty() ) {
4625 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
4626 if ( !CORBA::is_nil( hypObject ) ) {
4627 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
4628 if ( !anHyp->_is_nil() && !aShapeObject->_is_nil() )
4629 myNewMeshImpl->addHypothesis( aSubShapeObject, anHyp );
4634 // close "applied algorithms" HDF group
4635 aSubSubGroup->CloseOnDisk();
4638 // try to get applied hypotheses
4639 if ( aSubGroup->ExistInternalObject( "Applied Hypotheses" ) ) {
4640 // open "applied hypotheses" HDF group
4641 aSubSubGroup = new HDFgroup( "Applied Hypotheses", aSubGroup );
4642 aSubSubGroup->OpenOnDisk();
4643 // get number of applied hypotheses
4644 int aNbSubObjects = aSubSubGroup->nInternalObjects();
4645 for ( int l = 0; l < aNbSubObjects; l++ ) {
4646 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4647 aSubSubGroup->InternalObjectIndentify( l, name_dataset );
4648 // check if it is a hypothesis
4649 if ( string( name_dataset ).substr( 0, 3 ) == string( "Hyp" ) ) {
4650 aDataset = new HDFdataset( name_dataset, aSubSubGroup );
4651 aDataset->OpenOnDisk();
4652 size = aDataset->GetSize();
4653 char* refFromFile = new char[ size ];
4654 aDataset->ReadFromDisk( refFromFile );
4655 aDataset->CloseOnDisk();
4657 int id = atoi( refFromFile );
4658 string anIOR = myStudyContext->getIORbyOldId( id );
4659 if ( !anIOR.empty() ) {
4660 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
4661 if ( !CORBA::is_nil( hypObject ) ) {
4662 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
4663 if ( !anHyp->_is_nil() && !aShapeObject->_is_nil() )
4664 myNewMeshImpl->addHypothesis( aSubShapeObject, anHyp );
4669 // close "APPLIED HYPOTHESES" hdf group
4670 aSubSubGroup->CloseOnDisk();
4673 // close SUB-MESH hdf group
4674 aSubGroup->CloseOnDisk();
4677 // close SUB-MESHES containers hdf group
4678 aGroup->CloseOnDisk();
4682 // try to get GROUPS
4683 for ( int ii = GetNodeGroupsTag(); ii <= GetBallElementsGroupsTag(); ii++ ) {
4684 char name_group[ 30 ];
4685 if ( ii == GetNodeGroupsTag() )
4686 strcpy( name_group, "Groups of Nodes" );
4687 else if ( ii == GetEdgeGroupsTag() )
4688 strcpy( name_group, "Groups of Edges" );
4689 else if ( ii == GetFaceGroupsTag() )
4690 strcpy( name_group, "Groups of Faces" );
4691 else if ( ii == GetVolumeGroupsTag() )
4692 strcpy( name_group, "Groups of Volumes" );
4693 else if ( ii == Get0DElementsGroupsTag() )
4694 strcpy( name_group, "Groups of 0D Elements" );
4695 else if ( ii == GetBallElementsGroupsTag() )
4696 strcpy( name_group, "Groups of Balls" );
4698 if ( aTopGroup->ExistInternalObject( name_group ) ) {
4699 aGroup = new HDFgroup( name_group, aTopGroup );
4700 aGroup->OpenOnDisk();
4701 // get number of groups
4702 int aNbSubObjects = aGroup->nInternalObjects();
4703 for ( int j = 0; j < aNbSubObjects; j++ ) {
4704 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4705 aGroup->InternalObjectIndentify( j, name_dataset );
4706 // check if it is an group
4707 if ( string( name_dataset ).substr( 0, 5 ) == string( "Group" ) ) {
4709 int subid = atoi( string( name_dataset ).substr( 5 ).c_str() );
4712 aDataset = new HDFdataset( name_dataset, aGroup );
4713 aDataset->OpenOnDisk();
4715 // Retrieve actual group name
4716 size = aDataset->GetSize();
4717 char* nameFromFile = new char[ size ];
4718 aDataset->ReadFromDisk( nameFromFile );
4719 aDataset->CloseOnDisk();
4721 // Try to find a shape reference
4722 TopoDS_Shape aShape;
4723 char aRefName[ 30 ];
4724 sprintf( aRefName, "Ref on shape %d", subid);
4725 if ( aGroup->ExistInternalObject( aRefName ) ) {
4726 // load mesh "Ref on shape" - it's an entry to SObject
4727 aDataset = new HDFdataset( aRefName, aGroup );
4728 aDataset->OpenOnDisk();
4729 size = aDataset->GetSize();
4730 char* refFromFile = new char[ size ];
4731 aDataset->ReadFromDisk( refFromFile );
4732 aDataset->CloseOnDisk();
4733 if ( strlen( refFromFile ) > 0 ) {
4734 SALOMEDS::SObject_wrap shapeSO = myCurrentStudy->FindObjectID( refFromFile );
4735 CORBA::Object_var shapeObject = SObjectToObject( shapeSO );
4736 if ( !CORBA::is_nil( shapeObject ) ) {
4737 aShapeObject = GEOM::GEOM_Object::_narrow( shapeObject );
4738 if ( !aShapeObject->_is_nil() )
4739 aShape = GeomObjectToShape( aShapeObject );
4743 // Try to read a filter of SMESH_GroupOnFilter
4744 SMESH::Filter_var filter;
4745 SMESH_PredicatePtr predicate;
4746 std::string hdfGrpName = "Filter " + SMESH_Comment(subid);
4747 if ( aGroup->ExistInternalObject( hdfGrpName.c_str() ))
4749 aDataset = new HDFdataset( hdfGrpName.c_str(), aGroup );
4750 aDataset->OpenOnDisk();
4751 size = aDataset->GetSize();
4752 char* persistStr = new char[ size ];
4753 aDataset->ReadFromDisk( persistStr );
4754 aDataset->CloseOnDisk();
4755 if ( strlen( persistStr ) > 0 ) {
4756 filter = SMESH_GroupOnFilter_i::StringToFilter( persistStr );
4757 predicate = SMESH_GroupOnFilter_i::GetPredicate( filter );
4761 // Create group servant
4762 SMESH::ElementType type = (SMESH::ElementType)(ii - GetNodeGroupsTag() + 1);
4763 SMESH::SMESH_GroupBase_var aNewGroup = SMESH::SMESH_GroupBase::_duplicate
4764 ( myNewMeshImpl->createGroup( type, nameFromFile, aShape, predicate ) );
4765 // Obtain a SMESHDS_Group object
4766 if ( aNewGroup->_is_nil() )
4769 string iorSubString = GetORB()->object_to_string( aNewGroup );
4770 int newSubId = myStudyContext->findId( iorSubString );
4771 myStudyContext->mapOldToNew( subid, newSubId );
4773 SMESH_GroupBase_i* aGroupImpl = SMESH::DownCast< SMESH_GroupBase_i*>( aNewGroup );
4777 if ( SMESH_GroupOnFilter_i* aFilterGroup =
4778 dynamic_cast< SMESH_GroupOnFilter_i*>( aGroupImpl ))
4780 aFilterGroup->SetFilter( filter );
4781 filter->UnRegister();
4783 SMESHDS_GroupBase* aGroupBaseDS = aGroupImpl->GetGroupDS();
4784 if ( !aGroupBaseDS )
4787 aGroupBaseDS->SetStoreName( name_dataset );
4790 // Read color of the group
4791 char aGroupColorName[ 30 ];
4792 sprintf( aGroupColorName, "ColorGroup %d", subid);
4793 if ( aGroup->ExistInternalObject( aGroupColorName ) )
4795 aDataset = new HDFdataset( aGroupColorName, aGroup );
4796 aDataset->OpenOnDisk();
4797 size = aDataset->GetSize();
4798 double* anRGB = new double[ size ];
4799 aDataset->ReadFromDisk( anRGB );
4800 aDataset->CloseOnDisk();
4801 Quantity_Color aColor( anRGB[0], anRGB[1], anRGB[2], Quantity_TOC_RGB );
4802 aGroupBaseDS->SetColor( aColor );
4805 // Fill group with contents from MED file
4806 // SMESHDS_Group* aGrp = dynamic_cast<SMESHDS_Group*>( aGroupBaseDS );
4808 // myReader.GetGroup( aGrp );
4811 aGroup->CloseOnDisk();
4815 // instead of reading mesh data, we read only brief information of all
4816 // objects: mesh, groups, sub-meshes (issue 0021208 )
4819 SMESH_PreMeshInfo::LoadFromFile( myNewMeshImpl, id,
4820 meshfile.ToCString(), filename.ToCString(),
4824 // read Sub-Mesh ORDER if any
4825 if( aTopGroup->ExistInternalObject( "Mesh Order" ) ) {
4826 aDataset = new HDFdataset( "Mesh Order", aTopGroup );
4827 aDataset->OpenOnDisk();
4828 size = aDataset->GetSize();
4829 int* smIDs = new int[ size ];
4830 aDataset->ReadFromDisk( smIDs );
4831 aDataset->CloseOnDisk();
4832 TListOfListOfInt anOrderIds;
4833 anOrderIds.push_back( TListOfInt() );
4834 for ( int i = 0; i < size; i++ )
4835 if ( smIDs[ i ] < 0 ) // is separator
4836 anOrderIds.push_back( TListOfInt() );
4838 anOrderIds.back().push_back(smIDs[ i ]);
4840 myNewMeshImpl->GetImpl().SetMeshOrder( anOrderIds );
4844 // update hyps needing full mesh data restored (issue 20918)
4845 for ( hyp_data = hypDataList.begin(); hyp_data != hypDataList.end(); ++hyp_data )
4847 SMESH_Hypothesis_i* hyp = hyp_data->first;
4848 hyp->UpdateAsMeshesRestored();
4851 // notify algos on completed restoration to set sub-mesh event listeners
4852 for ( meshi_group = meshGroupList.begin(); meshi_group != meshGroupList.end(); ++meshi_group )
4854 SMESH_Mesh_i* myNewMeshImpl = meshi_group->first;
4855 ::SMESH_Mesh& myLocMesh = myNewMeshImpl->GetImpl();
4857 TopoDS_Shape myLocShape;
4858 if(myLocMesh.HasShapeToMesh())
4859 myLocShape = myLocMesh.GetShapeToMesh();
4861 myLocShape = SMESH_Mesh::PseudoShape();
4863 myLocMesh.GetSubMesh(myLocShape)->
4864 ComputeStateEngine (SMESH_subMesh::SUBMESH_RESTORED);
4869 aTopGroup->CloseOnDisk();
4872 aFile->CloseOnDisk();
4875 // Remove temporary files created from the stream
4878 SMESH_File meshFile( meshfile.ToCString() );
4879 if ( !meshFile ) // no meshfile exists
4881 SALOMEDS_Tool::RemoveTemporaryFiles( tmpDir.ToCString(), aFileSeq.in(), true );
4885 Engines::Container_var container = GetContainerRef();
4886 if ( Engines_Container_i* container_i = SMESH::DownCast<Engines_Container_i*>( container ))
4888 container_i->registerTemporaryFile( filename.ToCString() );
4889 container_i->registerTemporaryFile( meshfile.ToCString() );
4890 container_i->registerTemporaryFile( tmpDir.ToCString() );
4894 pd << ""; // prevent optimizing pd out
4896 // creation of tree nodes for all data objects in the study
4897 // to support tree representation customization and drag-n-drop:
4898 SALOMEDS::Study_var study = theComponent->GetStudy();
4899 SALOMEDS::UseCaseBuilder_wrap useCaseBuilder = study->GetUseCaseBuilder();
4900 if ( !useCaseBuilder->IsUseCaseNode( theComponent ) ) {
4901 useCaseBuilder->SetRootCurrent();
4902 useCaseBuilder->Append( theComponent ); // component object is added as the top level item
4903 SALOMEDS::ChildIterator_wrap it = study->NewChildIterator( theComponent );
4904 for (it->InitEx(true); it->More(); it->Next()) {
4905 useCaseBuilder->AppendTo( it->Value()->GetFather(), it->Value() );
4909 INFOS( "SMESH_Gen_i::Load completed" );
4913 //=============================================================================
4915 * SMESH_Gen_i::LoadASCII
4917 * Load SMESH module's data in ASCII format
4919 //=============================================================================
4921 bool SMESH_Gen_i::LoadASCII( SALOMEDS::SComponent_ptr theComponent,
4922 const SALOMEDS::TMPFile& theStream,
4924 bool isMultiFile ) {
4925 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::LoadASCII" );
4926 return Load( theComponent, theStream, theURL, isMultiFile );
4928 //before call main ::Load method it's need for decipher text format to
4929 //binary ( "|xx" => x' )
4930 int size = theStream.length();
4931 if ( int((size / 3 )*3) != size ) //error size of buffer
4934 int real_size = int(size / 3);
4936 _CORBA_Octet* buffer = new _CORBA_Octet[real_size];
4940 for ( int i = 0; i < real_size; i++ )
4942 memcpy( &(tmp[0]), &(theStream[i*3+1]), 2 );
4943 sscanf( tmp, "%x", &c );
4944 sprintf( (char*)&(buffer[i]), "%c", (char)c );
4947 SALOMEDS::TMPFile_var aRealStreamFile = new SALOMEDS::TMPFile(real_size, real_size, buffer, 1);
4949 return Load( theComponent, *(aRealStreamFile._retn()), theURL, isMultiFile );
4952 //=============================================================================
4954 * SMESH_Gen_i::Close
4956 * Clears study-connected data when it is closed
4958 //=============================================================================
4960 void SMESH_Gen_i::Close( SALOMEDS::SComponent_ptr theComponent )
4962 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Close" );
4964 // set correct current study
4965 SALOMEDS::Study_var study = theComponent->GetStudy();
4966 if ( study->StudyId() != GetCurrentStudyID())
4967 setCurrentStudy( study, /*IsBeingClosed=*/true );
4969 // Clear study contexts data
4970 int studyId = GetCurrentStudyID();
4971 if ( myStudyContextMap.find( studyId ) != myStudyContextMap.end() ) {
4972 delete myStudyContextMap[ studyId ];
4973 myStudyContextMap.erase( studyId );
4976 // remove the tmp files meshes are loaded from
4977 SMESH_PreMeshInfo::RemoveStudyFiles_TMP_METHOD( theComponent );
4979 myCurrentStudy = SALOMEDS::Study::_nil();
4983 //=============================================================================
4985 * SMESH_Gen_i::ComponentDataType
4987 * Get component data type
4989 //=============================================================================
4991 char* SMESH_Gen_i::ComponentDataType()
4993 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::ComponentDataType" );
4994 return CORBA::string_dup( "SMESH" );
4998 //=============================================================================
5000 * SMESH_Gen_i::IORToLocalPersistentID
5002 * Transform data from transient form to persistent
5004 //=============================================================================
5006 char* SMESH_Gen_i::IORToLocalPersistentID( SALOMEDS::SObject_ptr /*theSObject*/,
5007 const char* IORString,
5008 CORBA::Boolean /*isMultiFile*/,
5009 CORBA::Boolean /*isASCII*/ )
5011 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::IORToLocalPersistentID" );
5012 StudyContext* myStudyContext = GetCurrentStudyContext();
5014 if ( myStudyContext && strcmp( IORString, "" ) != 0 ) {
5015 int anId = myStudyContext->findId( IORString );
5017 if(MYDEBUG) MESSAGE( "VSR " << anId )
5019 sprintf( strId, "%d", anId );
5020 return CORBA::string_dup( strId );
5023 return CORBA::string_dup( "" );
5026 //=============================================================================
5028 * SMESH_Gen_i::LocalPersistentIDToIOR
5030 * Transform data from persistent form to transient
5032 //=============================================================================
5034 char* SMESH_Gen_i::LocalPersistentIDToIOR( SALOMEDS::SObject_ptr /*theSObject*/,
5035 const char* aLocalPersistentID,
5036 CORBA::Boolean /*isMultiFile*/,
5037 CORBA::Boolean /*isASCII*/ )
5039 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::LocalPersistentIDToIOR(): id = " << aLocalPersistentID );
5040 StudyContext* myStudyContext = GetCurrentStudyContext();
5042 if ( myStudyContext && strcmp( aLocalPersistentID, "" ) != 0 ) {
5043 int anId = atoi( aLocalPersistentID );
5044 return CORBA::string_dup( myStudyContext->getIORbyOldId( anId ).c_str() );
5046 return CORBA::string_dup( "" );
5049 //=======================================================================
5050 //function : RegisterObject
5052 //=======================================================================
5054 int SMESH_Gen_i::RegisterObject(CORBA::Object_ptr theObject)
5056 StudyContext* myStudyContext = GetCurrentStudyContext();
5057 if ( myStudyContext && !CORBA::is_nil( theObject )) {
5058 if (GetCurrentStudyID() == myImportedStudyId)
5059 myImportedStudyChanged = true;
5060 CORBA::String_var iorString = GetORB()->object_to_string( theObject );
5061 return myStudyContext->addObject( string( iorString.in() ) );
5066 //================================================================================
5068 * \brief Return id of registered object
5069 * \param theObject - the Object
5070 * \retval int - Object id
5072 //================================================================================
5074 CORBA::Long SMESH_Gen_i::GetObjectId(CORBA::Object_ptr theObject)
5076 StudyContext* myStudyContext = GetCurrentStudyContext();
5077 if ( myStudyContext && !CORBA::is_nil( theObject )) {
5078 string iorString = GetORB()->object_to_string( theObject );
5079 return myStudyContext->findId( iorString );
5084 //=============================================================================
5086 * SMESH_Gen_i::SetName
5088 * Set a new object name
5090 //=============================================================================
5091 void SMESH_Gen_i::SetName(const char* theIOR,
5092 const char* theName)
5094 if ( theIOR && strcmp( theIOR, "" ) ) {
5095 CORBA::Object_var anObject = GetORB()->string_to_object( theIOR );
5096 SALOMEDS::SObject_wrap aSO = ObjectToSObject( myCurrentStudy, anObject );
5097 if ( !aSO->_is_nil() ) {
5098 SetName( aSO, theName );
5103 int SMESH_Gen_i::GetCurrentStudyID()
5105 return myCurrentStudy->_is_nil() || myCurrentStudy->_non_existent() ? -1 : myCurrentStudy->StudyId();
5108 // Version information
5109 char* SMESH_Gen_i::getVersion()
5111 #if SMESH_DEVELOPMENT
5112 return CORBA::string_dup(SMESH_VERSION_STR"dev");
5114 return CORBA::string_dup(SMESH_VERSION_STR);
5118 //=================================================================================
5119 // function : Move()
5120 // purpose : Moves objects to the specified position.
5121 // Is used in the drag-n-drop functionality.
5122 //=================================================================================
5123 void SMESH_Gen_i::Move( const SMESH::sobject_list& what,
5124 SALOMEDS::SObject_ptr where,
5127 if ( CORBA::is_nil( where ) ) return;
5129 SALOMEDS::Study_var study = where->GetStudy();
5130 SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();
5131 SALOMEDS::UseCaseBuilder_var useCaseBuilder = study->GetUseCaseBuilder();
5132 SALOMEDS::SComponent_var father = where->GetFatherComponent();
5133 std::string dataType = father->ComponentDataType();
5134 if ( dataType != "SMESH" ) return; // not a SMESH component
5136 SALOMEDS::SObject_var objAfter;
5137 if ( row >= 0 && useCaseBuilder->HasChildren( where ) ) {
5138 // insert at given row -> find insertion position
5139 SALOMEDS::UseCaseIterator_var useCaseIt = useCaseBuilder->GetUseCaseIterator( where );
5141 for ( i = 0; i < row && useCaseIt->More(); i++, useCaseIt->Next() );
5142 if ( i == row && useCaseIt->More() ) {
5143 objAfter = useCaseIt->Value();
5147 for ( int i = 0; i < what.length(); i++ ) {
5148 SALOMEDS::SObject_var sobj = what[i];
5149 if ( CORBA::is_nil( sobj ) ) continue; // skip bad object
5150 // insert the object to the use case tree
5151 if ( !CORBA::is_nil( objAfter ) )
5152 useCaseBuilder->InsertBefore( sobj, objAfter ); // insert at given row
5154 useCaseBuilder->AppendTo( where, sobj ); // append to the end of list
5157 //================================================================================
5159 * \brief Returns true if algorithm can be used to mesh a given geometry
5160 * \param [in] theAlgoType - the algorithm type
5161 * \param [in] theLibName - a name of the Plug-in library implementing the algorithm
5162 * \param [in] theGeomObject - the geometry to mesh
5163 * \param [in] toCheckAll - if \c True, returns \c True if all shapes are meshable,
5164 * else, returns \c True if at least one shape is meshable
5165 * \return CORBA::Boolean - can or can't
5167 //================================================================================
5169 CORBA::Boolean SMESH_Gen_i::IsApplicable ( const char* theAlgoType,
5170 const char* theLibName,
5171 GEOM::GEOM_Object_ptr theGeomObject,
5172 CORBA::Boolean toCheckAll)
5176 std::string aPlatformLibName;
5177 typedef GenericHypothesisCreator_i* (*GetHypothesisCreator)(const char*);
5178 GenericHypothesisCreator_i* aCreator =
5179 getHypothesisCreator(theAlgoType, theLibName, aPlatformLibName);
5182 TopoDS_Shape shape = GeomObjectToShape( theGeomObject );
5183 if ( !shape.IsNull() )
5184 return aCreator->IsApplicable( shape, toCheckAll );
5191 SMESH_CATCH( SMESH::doNothing );
5195 //=================================================================================
5196 // function : importData
5197 // purpose : imports mesh data file (the med one) into the SMESH internal data structure
5198 //=================================================================================
5199 Engines::ListOfIdentifiers* SMESH_Gen_i::importData(CORBA::Long studyId,
5200 Engines::DataContainer_ptr data,
5201 const Engines::ListOfOptions& options)
5203 Engines::ListOfIdentifiers_var aResultIds = new Engines::ListOfIdentifiers;
5204 list<string> aResultList;
5206 CORBA::Object_var aSMObject = myNS->Resolve( "/myStudyManager" );
5207 SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow( aSMObject );
5208 SALOMEDS::Study_var aStudy = aStudyManager->GetStudyByID( studyId );
5209 SetCurrentStudy(aStudy);
5211 // load and store temporary imported file
5212 string aFileName = Kernel_Utils::GetTmpFileName();
5213 aFileName += string(".") + data->extension();
5214 Engines::TMPFile* aFileStream = data->get();
5215 const char *aBuffer = (const char*)aFileStream->NP_data();
5217 std::ofstream aFile(aFileName.c_str(), std::ios::binary);
5219 std::ofstream aFile(aFileName.c_str());
5221 aFile.write(aBuffer, aFileStream->length());
5224 // Retrieve mesh names from the file
5225 DriverMED_R_SMESHDS_Mesh aReader;
5226 aReader.SetFile( aFileName );
5227 aReader.SetMeshId(-1);
5228 Driver_Mesh::Status aStatus;
5229 list<string> aNames = aReader.GetMeshNames(aStatus);
5230 SMESH::mesh_array_var aResult = new SMESH::mesh_array();
5231 SMESH::DriverMED_ReadStatus aStatus2 = (SMESH::DriverMED_ReadStatus)aStatus;
5232 if (aStatus2 == SMESH::DRS_OK) {
5233 // Iterate through all meshes and create mesh objects
5234 for ( list<string>::iterator it = aNames.begin(); it != aNames.end(); it++ ) {
5236 SMESH::SMESH_Mesh_var mesh = createMesh();
5238 // publish mesh in the study
5239 SALOMEDS::SObject_var aSO;
5240 if (CanPublishInStudy(mesh)) {
5241 aSO = PublishMesh(aStudy, mesh.in(), (*it).c_str());
5242 aResultList.push_back(aSO->GetID());
5244 // Read mesh data (groups are published automatically by ImportMEDFile())
5245 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
5246 ASSERT( meshServant );
5247 meshServant->ImportMEDFile( aFileName.c_str(), (*it).c_str() );
5248 //meshServant->GetImpl().GetMeshDS()->Modified();
5251 MESSAGE("Opening MED file problems "<<aFileName.c_str())
5252 return aResultIds._retn();
5255 // remove temporary file
5257 DeleteFileA(aFileName.c_str());
5259 unlink(aFileName.c_str());
5262 if (!aResultList.empty()) {
5263 aResultIds->length(aResultList.size());
5264 list<string>::iterator aListIter = aResultList.begin();
5265 for(int a = 0; aListIter != aResultList.end(); aListIter++, a++)
5266 aResultIds[a] = aListIter->c_str();
5269 myImportedStudyId = studyId;
5270 myImportedStudyChanged = false;
5272 return aResultIds._retn();
5275 //=================================================================================
5276 // function : getModifiedData
5277 // purpose : exports all geometry of this GEOM module into one BRep file
5278 //=================================================================================
5279 Engines::ListOfData* SMESH_Gen_i::getModifiedData(CORBA::Long studyId)
5281 Engines::ListOfData_var aResult = new Engines::ListOfData;
5283 if (!myImportedStudyChanged) {
5284 INFOS("SMESH module data was not changed")
5285 return aResult._retn();
5288 CORBA::Object_var aSMObject = myNS->Resolve("/myStudyManager");
5289 SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(aSMObject);
5290 SALOMEDS::Study_var aStudy = aStudyManager->GetStudyByID(studyId);
5291 SetCurrentStudy(aStudy);
5292 SALOMEDS::SComponent_var aComponent = aStudy->FindComponent("SMESH");
5294 if (CORBA::is_nil(aComponent))
5295 return aResult._retn();
5297 std::string aFullPath(Kernel_Utils::GetTmpFileName());
5298 aFullPath += ".med";
5299 StudyContext* myStudyContext = GetCurrentStudyContext();
5301 SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator(aComponent); // check only published meshes
5302 int aNumMeshes = 0; // number of meshes in result
5303 for(; anIter->More(); anIter->Next()) {
5304 SALOMEDS::SObject_var aSO = anIter->Value();
5305 CORBA::Object_var anObj = aSO->GetObject();
5306 if (!CORBA::is_nil(anObj)) {
5307 SMESH::SMESH_Mesh_var aCORBAMesh = SMESH::SMESH_Mesh::_narrow(anObj);
5308 if(!aCORBAMesh->_is_nil()) {
5309 SMESH_Mesh_i* myImpl = dynamic_cast<SMESH_Mesh_i*>(GetServant(aCORBAMesh).in());
5312 SMESH_Mesh& aMesh = myImpl->GetImpl();
5313 CORBA::String_var objName = aSO->GetName();
5314 aMesh.ExportMED(aFullPath.c_str(), objName.in(), false, MED::eV2_2, 0);
5320 if (aNumMeshes > 0) { // prepare a container to store files
5321 INFOS("Write "<<aNumMeshes<<" meshes to "<<aFullPath.c_str());
5323 Engines::DataContainer_var aData = (new Engines_DataContainer_i(
5324 aFullPath.c_str(), "", "", true))->_this();
5327 return aResult._retn();
5330 //=============================================================================
5332 * SMESHEngine_factory
5334 * C factory, accessible with dlsym, after dlopen
5336 //=============================================================================
5340 PortableServer::ObjectId* SMESHEngine_factory( CORBA::ORB_ptr orb,
5341 PortableServer::POA_ptr poa,
5342 PortableServer::ObjectId* contId,
5343 const char* instanceName,
5344 const char* interfaceName )
5346 if(MYDEBUG) MESSAGE( "PortableServer::ObjectId* SMESHEngine_factory()" );
5347 if(MYDEBUG) SCRUTE(interfaceName);
5348 SMESH_Gen_i* aSMESHGen = new SMESH_Gen_i(orb, poa, contId, instanceName, interfaceName);
5349 return aSMESHGen->getId() ;