1 // Copyright (C) 2007-2014 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 typedef GenericHypothesisCreator_i* (*GetHypothesisCreator)(const char* );
471 GenericHypothesisCreator_i* aCreator = getHypothesisCreator(theHypName, theLibName, aPlatformLibName);
472 // create a new hypothesis object, store its ref. in studyContext
473 if(MYDEBUG) MESSAGE("Create Hypothesis " << theHypName);
475 myHypCreatorMap[string(theHypName)]->Create(myPoa, GetCurrentStudyID(), &myGen);
476 myHypothesis_i->SetLibName(aPlatformLibName.c_str()); // for persistency assurance
479 return hypothesis_i._retn();
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->CheckGeomGroupModif();
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 return SALOMEDS::Study::_duplicate( myCurrentStudy );
664 //=============================================================================
666 * SMESH_Gen_i::GetCurrentStudyContext
668 * Get current study context
670 //=============================================================================
671 StudyContext* SMESH_Gen_i::GetCurrentStudyContext()
673 if ( !CORBA::is_nil( myCurrentStudy ) &&
674 myStudyContextMap.find( GetCurrentStudyID() ) != myStudyContextMap.end() )
675 return myStudyContextMap[ myCurrentStudy->StudyId() ];
680 //=============================================================================
682 * SMESH_Gen_i::CreateHypothesis
684 * Create hypothesis/algorothm of given type and publish it in the study
686 //=============================================================================
688 SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::CreateHypothesis( const char* theHypName,
689 const char* theLibName )
690 throw ( SALOME::SALOME_Exception )
692 Unexpect aCatch(SALOME_SalomeException);
693 // Create hypothesis/algorithm
694 SMESH::SMESH_Hypothesis_var hyp = this->createHypothesis( theHypName, theLibName );
696 // Publish hypothesis/algorithm in the study
697 if ( CanPublishInStudy( hyp ) ) {
698 SALOMEDS::SObject_wrap aSO = PublishHypothesis( myCurrentStudy, hyp );
699 if ( !aSO->_is_nil() ) {
700 // Update Python script
701 TPythonDump() << aSO << " = " << this << ".CreateHypothesis('"
702 << theHypName << "', '" << theLibName << "')";
709 //================================================================================
711 * \brief Return a hypothesis holding parameter values corresponding either to the mesh
712 * existing on the given geometry or to size of the geometry.
713 * \param theHypType - hypothesis type name
714 * \param theLibName - plugin library name
715 * \param theMesh - The mesh of interest
716 * \param theGeom - The shape to get parameter values from
717 * \retval SMESH::SMESH_Hypothesis_ptr - The returned hypothesis may be the one existing
718 * in a study and used to compute the mesh, or a temporary one created just to pass
721 //================================================================================
723 SMESH::SMESH_Hypothesis_ptr
724 SMESH_Gen_i::GetHypothesisParameterValues (const char* theHypType,
725 const char* theLibName,
726 SMESH::SMESH_Mesh_ptr theMesh,
727 GEOM::GEOM_Object_ptr theGeom,
728 CORBA::Boolean byMesh)
729 throw ( SALOME::SALOME_Exception )
731 Unexpect aCatch(SALOME_SalomeException);
732 if ( byMesh && CORBA::is_nil( theMesh ) )
733 return SMESH::SMESH_Hypothesis::_nil();
734 if ( byMesh && CORBA::is_nil( theGeom ) )
735 return SMESH::SMESH_Hypothesis::_nil();
737 // -----------------------------------------------
738 // find hypothesis used to mesh theGeom
739 // -----------------------------------------------
741 // get mesh and shape
742 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
743 TopoDS_Shape shape = GeomObjectToShape( theGeom );
744 if ( byMesh && ( !meshServant || meshServant->NbNodes()==0 || shape.IsNull() ))
745 return SMESH::SMESH_Hypothesis::_nil();
746 ::SMESH_Mesh* mesh = meshServant ? &meshServant->GetImpl() : (::SMESH_Mesh*)0;
748 // create a temporary hypothesis to know its dimention
749 SMESH::SMESH_Hypothesis_var tmpHyp = this->createHypothesis( theHypType, theLibName );
750 SMESH_Hypothesis_i* hypServant = SMESH::DownCast<SMESH_Hypothesis_i*>( tmpHyp );
752 return SMESH::SMESH_Hypothesis::_nil();
753 ::SMESH_Hypothesis* hyp = hypServant->GetImpl();
756 // look for a hypothesis of theHypType used to mesh the shape
757 if ( myGen.GetShapeDim( shape ) == hyp->GetDim() )
760 SMESH::ListOfHypothesis_var aHypList = theMesh->GetHypothesisList( theGeom );
761 int nbLocalHyps = aHypList->length();
762 for ( int i = 0; i < nbLocalHyps; i++ ) {
763 CORBA::String_var hypName = aHypList[i]->GetName();
764 if ( strcmp( theHypType, hypName.in() ) == 0 ) // FOUND local!
765 return SMESH::SMESH_Hypothesis::_duplicate( aHypList[i] );
767 // check super shapes
768 TopTools_ListIteratorOfListOfShape itShape( mesh->GetAncestors( shape ));
769 while ( nbLocalHyps == 0 && itShape.More() ) {
770 GEOM::GEOM_Object_ptr geomObj = ShapeToGeomObject( itShape.Value() );
771 if ( ! CORBA::is_nil( geomObj )) {
772 SMESH::ListOfHypothesis_var aHypList = theMesh->GetHypothesisList( geomObj );
773 nbLocalHyps = aHypList->length();
774 for ( int i = 0; i < nbLocalHyps; i++ )
775 if ( strcmp( theHypType, aHypList[i]->GetName() ) == 0 ) // FOUND global!
776 return SMESH::SMESH_Hypothesis::_duplicate( aHypList[i] );
782 // let the temporary hypothesis find out somehow parameter values by mesh
783 if ( hyp->SetParametersByMesh( mesh, shape ))
784 return SMESH::SMESH_Hypothesis::_duplicate( tmpHyp );
789 diagonal = mesh->GetShapeDiagonalSize();
791 diagonal = ::SMESH_Mesh::GetShapeDiagonalSize( shape );
792 ::SMESH_Hypothesis::TDefaults dflts;
793 dflts._elemLength = diagonal / myGen.GetBoundaryBoxSegmentation();
794 dflts._nbSegments = myGen.GetDefaultNbSegments();
795 dflts._shape = &shape;
796 // let the temporary hypothesis initialize it's values
797 if ( hyp->SetParametersByDefaults( dflts, mesh ))
798 return SMESH::SMESH_Hypothesis::_duplicate( tmpHyp );
801 return SMESH::SMESH_Hypothesis::_nil();
804 //=============================================================================
806 * Sets number of segments per diagonal of boundary box of geometry by which
807 * default segment length of appropriate 1D hypotheses is defined
809 //=============================================================================
811 void SMESH_Gen_i::SetBoundaryBoxSegmentation( CORBA::Long theNbSegments )
812 throw ( SALOME::SALOME_Exception )
814 if ( theNbSegments > 0 )
815 myGen.SetBoundaryBoxSegmentation( int( theNbSegments ));
817 THROW_SALOME_CORBA_EXCEPTION( "non-positive number of segments", SALOME::BAD_PARAM );
819 //=============================================================================
821 * \brief Sets default number of segments per edge
823 //=============================================================================
824 void SMESH_Gen_i::SetDefaultNbSegments(CORBA::Long theNbSegments)
825 throw ( SALOME::SALOME_Exception )
827 if ( theNbSegments > 0 )
828 myGen.SetDefaultNbSegments( int(theNbSegments) );
830 THROW_SALOME_CORBA_EXCEPTION( "non-positive number of segments", SALOME::BAD_PARAM );
833 //=============================================================================
837 //=============================================================================
839 void SMESH_Gen_i::SetOption(const char* name, const char* value)
841 if ( name && value && strlen( value ) > 0 )
844 if ( strcmp(name, "historical_python_dump") == 0 )
846 myIsHistoricalPythonDump = ( value[0] == '1' || toupper(value[0]) == 'T' ); // 1 || true
847 msgToGUI = "preferences/SMESH/historical_python_dump/";
848 msgToGUI += myIsHistoricalPythonDump ? "true" : "false";
850 else if ( strcmp(name, "forget_mesh_on_hyp_modif") == 0 )
852 myToForgetMeshDataOnHypModif = ( value[0] == '1' || toupper(value[0]) == 'T' ); // 1 || true
853 msgToGUI = "preferences/SMESH/forget_mesh_on_hyp_modif/";
854 msgToGUI += myToForgetMeshDataOnHypModif ? "true" : "false";
857 // update preferences in case if SetOption() is invoked from python console
858 if ( !msgToGUI.empty() )
860 CORBA::Object_var obj = SMESH_Gen_i::GetNS()->Resolve( "/Kernel/Session" );
861 SALOME::Session_var session = SALOME::Session::_narrow( obj );
862 if ( !CORBA::is_nil( session ) )
863 session->emitMessageOneWay(msgToGUI.c_str());
868 //=============================================================================
870 Return an option value
872 //=============================================================================
874 char* SMESH_Gen_i::GetOption(const char* name)
878 if ( strcmp(name, "historical_python_dump") == 0 )
880 return CORBA::string_dup( myIsHistoricalPythonDump ? "true" : "false" );
882 if ( strcmp(name, "forget_mesh_on_hyp_modif") == 0 )
884 return CORBA::string_dup( myToForgetMeshDataOnHypModif ? "true" : "false" );
887 return CORBA::string_dup( "" );
890 //=============================================================================
892 * SMESH_Gen_i::CreateMesh
894 * Create empty mesh on a shape and publish it in the study
896 //=============================================================================
898 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMesh( GEOM::GEOM_Object_ptr theShapeObject )
899 throw ( SALOME::SALOME_Exception )
901 Unexpect aCatch(SALOME_SalomeException);
902 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMesh" );
904 SMESH::SMESH_Mesh_var mesh = this->createMesh();
906 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( mesh );
907 ASSERT( meshServant );
908 meshServant->SetShape( theShapeObject );
910 // publish mesh in the study
911 if ( CanPublishInStudy( mesh ) ) {
912 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
913 aStudyBuilder->NewCommand(); // There is a transaction
914 SALOMEDS::SObject_wrap aSO = PublishMesh( myCurrentStudy, mesh.in() );
915 aStudyBuilder->CommitCommand();
916 if ( !aSO->_is_nil() ) {
917 // Update Python script
918 TPythonDump() << aSO << " = " << this << ".CreateMesh(" << theShapeObject << ")";
925 //=============================================================================
927 * SMESH_Gen_i::CreateEmptyMesh
931 //=============================================================================
933 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateEmptyMesh()
934 throw ( SALOME::SALOME_Exception )
936 Unexpect aCatch(SALOME_SalomeException);
937 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMesh" );
939 SMESH::SMESH_Mesh_var mesh = this->createMesh();
941 // publish mesh in the study
942 if ( CanPublishInStudy( mesh ) ) {
943 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
944 aStudyBuilder->NewCommand(); // There is a transaction
945 SALOMEDS::SObject_wrap aSO = PublishMesh( myCurrentStudy, mesh.in() );
946 aStudyBuilder->CommitCommand();
947 if ( !aSO->_is_nil() ) {
948 // Update Python script
949 TPythonDump() << aSO << " = " << this << ".CreateEmptyMesh()";
956 //=============================================================================
958 * SMESH_Gen_i::CreateMeshFromUNV
960 * Create mesh and import data from UNV file
962 //=============================================================================
964 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMeshesFromUNV( const char* theFileName )
965 throw ( SALOME::SALOME_Exception )
967 Unexpect aCatch(SALOME_SalomeException);
968 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMeshesFromUNV" );
970 SMESH::SMESH_Mesh_var aMesh = createMesh();
972 // publish mesh in the study
973 if ( CanPublishInStudy( aMesh ) ) {
974 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
975 aStudyBuilder->NewCommand(); // There is a transaction
976 SALOMEDS::SObject_wrap aSO = PublishMesh( myCurrentStudy, aMesh.in(), aFileName.c_str() );
977 aStudyBuilder->CommitCommand();
978 if ( !aSO->_is_nil() ) {
979 // Update Python script
980 TPythonDump() << aSO << " = " << this << ".CreateMeshesFromUNV(r'" << theFileName << "')";
984 SMESH_Mesh_i* aServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( aMesh ).in() );
986 aServant->ImportUNVFile( theFileName );
988 // Dump creation of groups
989 SMESH::ListOfGroups_var groups = aServant->GetGroups();
991 aServant->GetImpl().GetMeshDS()->Modified();
992 return aMesh._retn();
995 //=============================================================================
997 * SMESH_Gen_i::CreateMeshFromMED
999 * Create mesh and import data from MED file
1001 //=============================================================================
1003 SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromMEDorSAUV( const char* theFileName,
1004 SMESH::DriverMED_ReadStatus& theStatus,
1005 const char* theCommandNameForPython,
1006 const char* theFileNameForPython)
1009 char bname[ _MAX_FNAME ];
1010 _splitpath( theFileNameForPython, NULL, NULL, bname, NULL );
1011 string aFileName = bname;
1013 string aFileName = basename( theFileNameForPython );
1015 // Retrieve mesh names from the file
1016 DriverMED_R_SMESHDS_Mesh myReader;
1017 myReader.SetFile( theFileName );
1018 myReader.SetMeshId( -1 );
1019 Driver_Mesh::Status aStatus;
1020 list<string> aNames = myReader.GetMeshNames(aStatus);
1021 SMESH::mesh_array_var aResult = new SMESH::mesh_array();
1022 theStatus = (SMESH::DriverMED_ReadStatus)aStatus;
1024 { // open a new scope to make aPythonDump die before PythonDump in SMESH_Mesh::GetGroups()
1027 TPythonDump aPythonDump;
1028 aPythonDump << "([";
1030 if (theStatus == SMESH::DRS_OK) {
1031 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1032 aStudyBuilder->NewCommand(); // There is a transaction
1033 aResult->length( aNames.size() );
1036 // Iterate through all meshes and create mesh objects
1037 for ( list<string>::iterator it = aNames.begin(); it != aNames.end(); it++ ) {
1039 if (i > 0) aPythonDump << ", ";
1042 SMESH::SMESH_Mesh_var mesh = createMesh();
1044 // publish mesh in the study
1045 SALOMEDS::SObject_wrap aSO;
1046 if ( CanPublishInStudy( mesh ) )
1047 // little trick: for MED file theFileName and theFileNameForPython are the same, but they are different for SAUV
1048 // - as names of meshes are stored in MED file, we use them for data publishing
1049 // - as mesh name is not stored in UNV file, we use file name as name of mesh when publishing data
1050 aSO = PublishMesh( myCurrentStudy, mesh.in(), ( theFileName == theFileNameForPython ) ? (*it).c_str() : aFileName.c_str() );
1051 if ( !aSO->_is_nil() ) {
1056 aPythonDump << "mesh_" << i;
1059 // Read mesh data (groups are published automatically by ImportMEDFile())
1060 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
1061 ASSERT( meshServant );
1062 SMESH::DriverMED_ReadStatus status1 =
1063 meshServant->ImportMEDFile( theFileName, (*it).c_str() );
1064 if (status1 > theStatus)
1065 theStatus = status1;
1067 aResult[i++] = SMESH::SMESH_Mesh::_duplicate( mesh );
1068 meshServant->GetImpl().GetMeshDS()->Modified();
1070 aStudyBuilder->CommitCommand();
1073 // Update Python script
1074 aPythonDump << "], status) = " << this << "." << theCommandNameForPython << "(r'" << theFileNameForPython << "')";
1076 // Dump creation of groups
1077 for ( int i = 0; i < aResult->length(); ++i )
1078 SMESH::ListOfGroups_var groups = aResult[ i ]->GetGroups();
1080 return aResult._retn();
1083 SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromMED( const char* theFileName,
1084 SMESH::DriverMED_ReadStatus& theStatus)
1085 throw ( SALOME::SALOME_Exception )
1087 Unexpect aCatch(SALOME_SalomeException);
1088 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMeshFromMED" );
1089 SMESH::mesh_array* result = CreateMeshesFromMEDorSAUV(theFileName, theStatus, "CreateMeshesFromMED", theFileName);
1093 //=============================================================================
1095 * SMESH_Gen_i::CreateMeshFromSAUV
1097 * Create mesh and import data from SAUV file
1099 //=============================================================================
1101 SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromSAUV( const char* theFileName,
1102 SMESH::DriverMED_ReadStatus& theStatus)
1103 throw ( SALOME::SALOME_Exception )
1105 Unexpect aCatch(SALOME_SalomeException);
1106 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMeshFromSAUV" );
1107 std::string sauvfilename(theFileName);
1108 std::string medfilename(theFileName);
1109 medfilename += ".med";
1112 cmd = "%PYTHONBIN% ";
1117 cmd += "from medutilities import convert ; convert(r'" + sauvfilename + "', 'GIBI', 'MED', 1, r'" + medfilename + "')";
1119 system(cmd.c_str());
1120 SMESH::mesh_array* result = CreateMeshesFromMEDorSAUV(medfilename.c_str(), theStatus, "CreateMeshesFromSAUV", sauvfilename.c_str());
1122 cmd = "%PYTHONBIN% ";
1127 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1129 system(cmd.c_str());
1133 //=============================================================================
1135 * SMESH_Gen_i::CreateMeshFromSTL
1137 * Create mesh and import data from STL file
1139 //=============================================================================
1141 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMeshesFromSTL( const char* theFileName )
1142 throw ( SALOME::SALOME_Exception )
1144 Unexpect aCatch(SALOME_SalomeException);
1145 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMeshesFromSTL" );
1147 SMESH::SMESH_Mesh_var aMesh = createMesh();
1150 char bname[ _MAX_FNAME ];
1151 _splitpath( theFileName, NULL, NULL, bname, NULL );
1152 string aFileName = bname;
1154 string aFileName = basename( theFileName );
1156 // publish mesh in the study
1157 if ( CanPublishInStudy( aMesh ) ) {
1158 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1159 aStudyBuilder->NewCommand(); // There is a transaction
1160 SALOMEDS::SObject_wrap aSO = PublishInStudy
1161 ( myCurrentStudy, SALOMEDS::SObject::_nil(), aMesh.in(), aFileName.c_str() );
1162 aStudyBuilder->CommitCommand();
1163 if ( !aSO->_is_nil() ) {
1164 // Update Python script
1165 TPythonDump() << aSO << " = " << this << ".CreateMeshesFromSTL(r'" << theFileName << "')";
1169 SMESH_Mesh_i* aServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( aMesh ).in() );
1171 aServant->ImportSTLFile( theFileName );
1172 aServant->GetImpl().GetMeshDS()->Modified();
1173 return aMesh._retn();
1176 //================================================================================
1178 * \brief Create meshes and import data from the CGSN file
1180 //================================================================================
1182 SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromCGNS( const char* theFileName,
1183 SMESH::DriverMED_ReadStatus& theStatus)
1184 throw ( SALOME::SALOME_Exception )
1186 Unexpect aCatch(SALOME_SalomeException);
1188 SMESH::mesh_array_var aResult = new SMESH::mesh_array();
1191 // Retrieve nb meshes from the file
1192 DriverCGNS_Read myReader;
1193 myReader.SetFile( theFileName );
1194 Driver_Mesh::Status aStatus;
1195 int nbMeshes = myReader.GetNbMeshes(aStatus);
1196 theStatus = (SMESH::DriverMED_ReadStatus)aStatus;
1198 aResult->length( nbMeshes );
1200 { // open a new scope to make aPythonDump die before PythonDump in SMESH_Mesh::GetGroups()
1203 TPythonDump aPythonDump;
1204 aPythonDump << "([";
1206 if (theStatus == SMESH::DRS_OK)
1208 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1209 aStudyBuilder->NewCommand(); // There is a transaction
1213 // Iterate through all meshes and create mesh objects
1214 for ( ; i < nbMeshes; ++i )
1217 if (i > 0) aPythonDump << ", ";
1220 SMESH::SMESH_Mesh_var mesh = createMesh();
1221 aResult[i] = SMESH::SMESH_Mesh::_duplicate( mesh );
1223 // Read mesh data (groups are published automatically by ImportMEDFile())
1224 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
1225 ASSERT( meshServant );
1227 SMESH::DriverMED_ReadStatus status1 =
1228 meshServant->ImportCGNSFile( theFileName, i, meshName );
1229 if (status1 > theStatus)
1230 theStatus = status1;
1232 meshServant->GetImpl().GetMeshDS()->Modified();
1233 // publish mesh in the study
1234 SALOMEDS::SObject_wrap aSO;
1235 if ( CanPublishInStudy( mesh ) )
1236 aSO = PublishMesh( myCurrentStudy, mesh.in(), meshName.c_str() );
1239 if ( !aSO->_is_nil() ) {
1243 aPythonDump << "mesh_" << i;
1246 aStudyBuilder->CommitCommand();
1249 aPythonDump << "], status) = " << this << ".CreateMeshesFromCGNS(r'" << theFileName << "')";
1251 // Dump creation of groups
1252 for ( int i = 0; i < aResult->length(); ++i )
1253 SMESH::ListOfGroups_var groups = aResult[ i ]->GetGroups();
1255 THROW_SALOME_CORBA_EXCEPTION("CGNS library is unavailable", SALOME::INTERNAL_ERROR);
1258 return aResult._retn();
1261 //================================================================================
1263 * \brief Create a mesh and import data from a GMF file
1265 //================================================================================
1267 SMESH::SMESH_Mesh_ptr
1268 SMESH_Gen_i::CreateMeshesFromGMF( const char* theFileName,
1269 CORBA::Boolean theMakeRequiredGroups,
1270 SMESH::ComputeError_out theError)
1271 throw ( SALOME::SALOME_Exception )
1273 Unexpect aCatch(SALOME_SalomeException);
1275 SMESH::SMESH_Mesh_var aMesh = createMesh();
1277 char bname[ _MAX_FNAME ];
1278 _splitpath( theFileName, NULL, NULL, bname, NULL );
1279 string aFileName = bname;
1281 string aFileName = basename( theFileName );
1283 // publish mesh in the study
1284 if ( CanPublishInStudy( aMesh ) ) {
1285 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1286 aStudyBuilder->NewCommand(); // There is a transaction
1287 SALOMEDS::SObject_wrap aSO = PublishInStudy
1288 ( myCurrentStudy, SALOMEDS::SObject::_nil(), aMesh.in(), aFileName.c_str() );
1289 aStudyBuilder->CommitCommand();
1290 if ( !aSO->_is_nil() ) {
1291 // Update Python script
1292 TPythonDump() << "("<< aSO << ", error) = " << this << ".CreateMeshesFromGMF(r'"
1293 << theFileName << "', "
1294 << theMakeRequiredGroups << " )";
1297 SMESH_Mesh_i* aServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( aMesh ).in() );
1299 theError = aServant->ImportGMFFile( theFileName, theMakeRequiredGroups );
1300 aServant->GetImpl().GetMeshDS()->Modified();
1301 return aMesh._retn();
1305 //=============================================================================
1307 * SMESH_Gen_i::IsReadyToCompute
1309 * Returns true if mesh contains enough data to be computed
1311 //=============================================================================
1313 CORBA::Boolean SMESH_Gen_i::IsReadyToCompute( SMESH::SMESH_Mesh_ptr theMesh,
1314 GEOM::GEOM_Object_ptr theShapeObject )
1315 throw ( SALOME::SALOME_Exception )
1317 Unexpect aCatch(SALOME_SalomeException);
1318 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::IsReadyToCompute" );
1320 if ( CORBA::is_nil( theShapeObject ) )
1321 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1322 SALOME::BAD_PARAM );
1324 if ( CORBA::is_nil( theMesh ) )
1325 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
1326 SALOME::BAD_PARAM );
1330 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
1331 ASSERT( meshServant );
1332 if ( meshServant ) {
1333 // get local TopoDS_Shape
1334 TopoDS_Shape myLocShape = GeomObjectToShape( theShapeObject );
1335 // call implementation
1336 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1337 return myGen.CheckAlgoState( myLocMesh, myLocShape );
1340 catch ( SALOME_Exception& S_ex ) {
1341 INFOS( "catch exception "<< S_ex.what() );
1346 //================================================================================
1348 * \brief Find SObject for an algo
1350 //================================================================================
1352 SALOMEDS::SObject_ptr SMESH_Gen_i::GetAlgoSO(const ::SMESH_Algo* algo)
1355 if ( !myCurrentStudy->_is_nil() ) {
1356 // find algo in the study
1357 CORBA::String_var compDataType = ComponentDataType();
1358 SALOMEDS::SComponent_wrap father = myCurrentStudy->FindComponent( compDataType.in() );
1359 if ( !father->_is_nil() ) {
1360 SALOMEDS::ChildIterator_wrap itBig = myCurrentStudy->NewChildIterator( father );
1361 for ( ; itBig->More(); itBig->Next() ) {
1362 SALOMEDS::SObject_wrap gotBranch = itBig->Value();
1363 if ( gotBranch->Tag() == GetAlgorithmsRootTag() ) {
1364 SALOMEDS::ChildIterator_wrap algoIt = myCurrentStudy->NewChildIterator( gotBranch );
1365 for ( ; algoIt->More(); algoIt->Next() ) {
1366 SALOMEDS::SObject_wrap algoSO = algoIt->Value();
1367 CORBA::Object_var algoIOR = SObjectToObject( algoSO );
1368 if ( !CORBA::is_nil( algoIOR )) {
1369 SMESH_Hypothesis_i* impl = SMESH::DownCast<SMESH_Hypothesis_i*>( algoIOR );
1370 if ( impl && impl->GetImpl() == algo )
1371 return algoSO._retn();
1373 } // loop on algo SO's
1376 } // SMESH component iterator
1380 return SALOMEDS::SObject::_nil();
1383 //================================================================================
1385 * \brief Return errors of mesh computation
1387 //================================================================================
1389 SMESH::compute_error_array* SMESH_Gen_i::GetComputeErrors( SMESH::SMESH_Mesh_ptr theMesh,
1390 GEOM::GEOM_Object_ptr theSubObject )
1391 throw ( SALOME::SALOME_Exception )
1393 Unexpect aCatch(SALOME_SalomeException);
1394 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetComputeErrors()" );
1396 if ( CORBA::is_nil( theSubObject ) && theMesh->HasShapeToMesh())
1397 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
1399 if ( CORBA::is_nil( theMesh ) )
1400 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1402 SMESH::compute_error_array_var error_array = new SMESH::compute_error_array;
1404 if ( SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh ))
1407 if(theMesh->HasShapeToMesh())
1408 shape = GeomObjectToShape( theSubObject );
1410 shape = SMESH_Mesh::PseudoShape();
1412 ::SMESH_Mesh& mesh = meshServant->GetImpl();
1414 error_array->length( mesh.GetMeshDS()->MaxShapeIndex() );
1417 SMESH_subMesh *sm = mesh.GetSubMesh(shape);
1418 const bool includeSelf = true, complexShapeFirst = true;
1419 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(includeSelf,
1421 while ( smIt->more() )
1424 // if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
1426 SMESH_ComputeErrorPtr error = sm->GetComputeError();
1427 if ( error && !error->IsOK() && error->myAlgo )
1429 SMESH::ComputeError & errStruct = error_array[ nbErr++ ];
1430 errStruct.code = -( error->myName < 0 ? error->myName + 1: error->myName ); // -1 -> 0
1431 errStruct.comment = error->myComment.c_str();
1432 errStruct.subShapeID = sm->GetId();
1433 SALOMEDS::SObject_wrap algoSO = GetAlgoSO( error->myAlgo );
1434 if ( !algoSO->_is_nil() ) {
1435 CORBA::String_var algoName = algoSO->GetName();
1436 errStruct.algoName = algoName;
1439 errStruct.algoName = error->myAlgo->GetName();
1441 errStruct.hasBadMesh = !error->myBadElements.empty();
1444 error_array->length( nbErr );
1447 catch ( SALOME_Exception& S_ex ) {
1448 INFOS( "catch exception "<< S_ex.what() );
1451 return error_array._retn();
1454 //================================================================================
1456 * \brief Return mesh elements preventing computation of a subshape
1458 //================================================================================
1460 SMESH::MeshPreviewStruct*
1461 SMESH_Gen_i::GetBadInputElements( SMESH::SMESH_Mesh_ptr theMesh,
1462 CORBA::Short theSubShapeID )
1463 throw ( SALOME::SALOME_Exception )
1465 Unexpect aCatch(SALOME_SalomeException);
1466 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetBadInputElements()" );
1468 if ( CORBA::is_nil( theMesh ) )
1469 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1471 SMESH::MeshPreviewStruct_var result = new SMESH::MeshPreviewStruct;
1474 if ( SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh ))
1476 // mesh implementation
1477 ::SMESH_Mesh& mesh = meshServant->GetImpl();
1478 // submesh by subshape id
1479 if ( SMESH_subMesh * sm = mesh.GetSubMeshContaining( theSubShapeID ))
1482 SMESH_ComputeErrorPtr error = sm->GetComputeError();
1483 if ( error && !error->myBadElements.empty())
1485 typedef map<const SMDS_MeshElement*, int > TNode2LocalIDMap;
1486 typedef TNode2LocalIDMap::iterator TNodeLocalID;
1488 // get nodes of elements and count elements
1489 TNode2LocalIDMap mapNode2LocalID;
1490 list< TNodeLocalID > connectivity;
1491 int i, nbElements = 0, nbConnNodes = 0;
1493 list<const SMDS_MeshElement*>::iterator elemIt = error->myBadElements.begin();
1494 list<const SMDS_MeshElement*>::iterator elemEnd = error->myBadElements.end();
1495 for ( ; elemIt != elemEnd; ++elemIt, ++nbElements )
1497 SMDS_ElemIteratorPtr nIt = (*elemIt)->nodesIterator();
1498 while ( nIt->more() )
1499 connectivity.push_back
1500 ( mapNode2LocalID.insert( make_pair( nIt->next(), ++nbConnNodes)).first );
1502 // fill node coords and assign local ids to the nodes
1503 int nbNodes = mapNode2LocalID.size();
1504 result->nodesXYZ.length( nbNodes );
1505 TNodeLocalID node2ID = mapNode2LocalID.begin();
1506 for ( i = 0; i < nbNodes; ++i, ++node2ID ) {
1507 node2ID->second = i;
1508 const SMDS_MeshNode* node = (const SMDS_MeshNode*) node2ID->first;
1509 result->nodesXYZ[i].x = node->X();
1510 result->nodesXYZ[i].y = node->Y();
1511 result->nodesXYZ[i].z = node->Z();
1513 // fill connectivity
1514 result->elementConnectivities.length( nbConnNodes );
1515 list< TNodeLocalID >::iterator connIt = connectivity.begin();
1516 for ( i = 0; i < nbConnNodes; ++i, ++connIt ) {
1517 result->elementConnectivities[i] = (*connIt)->second;
1519 // fill element types
1520 result->elementTypes.length( nbElements );
1521 for ( i = 0, elemIt = error->myBadElements.begin(); i <nbElements; ++i, ++elemIt )
1523 const SMDS_MeshElement* elem = *elemIt;
1524 result->elementTypes[i].SMDS_ElementType = (SMESH::ElementType) elem->GetType();
1525 result->elementTypes[i].isPoly = elem->IsPoly();
1526 result->elementTypes[i].nbNodesInElement = elem->NbNodes();
1532 catch ( SALOME_Exception& S_ex ) {
1533 INFOS( "catch exception "<< S_ex.what() );
1536 return result._retn();
1539 //================================================================================
1541 * \brief Create a group of elements preventing computation of a sub-shape
1543 //================================================================================
1545 SMESH::ListOfGroups*
1546 SMESH_Gen_i::MakeGroupsOfBadInputElements( SMESH::SMESH_Mesh_ptr theMesh,
1547 CORBA::Short theSubShapeID,
1548 const char* theGroupName )
1549 throw ( SALOME::SALOME_Exception )
1551 Unexpect aCatch(SALOME_SalomeException);
1553 SMESH::ListOfGroups_var groups;
1555 if ( CORBA::is_nil( theMesh ) )
1556 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1559 if ( SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh ))
1561 groups = meshServant->MakeGroupsOfBadInputElements( theSubShapeID, theGroupName );
1562 TPythonDump() << groups << " = " << this
1563 << ".MakeGroupsOfBadInputElements( "
1564 << theMesh << ", " << theSubShapeID << ", '" << theGroupName << "' )";
1567 catch ( SALOME_Exception& S_ex ) {
1568 INFOS( "catch exception "<< S_ex.what() );
1570 return groups._retn();
1573 //================================================================================
1575 * \brief Returns errors of hypotheses definintion
1576 * \param theMesh - the mesh
1577 * \param theSubObject - the main or sub- shape
1578 * \retval SMESH::algo_error_array* - sequence of errors
1580 //================================================================================
1582 SMESH::algo_error_array* SMESH_Gen_i::GetAlgoState( SMESH::SMESH_Mesh_ptr theMesh,
1583 GEOM::GEOM_Object_ptr theSubObject )
1584 throw ( SALOME::SALOME_Exception )
1586 Unexpect aCatch(SALOME_SalomeException);
1587 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetAlgoState()" );
1589 if ( CORBA::is_nil( theSubObject ) && theMesh->HasShapeToMesh())
1590 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
1592 if ( CORBA::is_nil( theMesh ) )
1593 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1595 SMESH::algo_error_array_var error_array = new SMESH::algo_error_array;
1597 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
1598 ASSERT( meshServant );
1599 if ( meshServant ) {
1600 TopoDS_Shape myLocShape;
1601 if(theMesh->HasShapeToMesh())
1602 myLocShape = GeomObjectToShape( theSubObject );
1604 myLocShape = SMESH_Mesh::PseudoShape();
1606 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1607 list< ::SMESH_Gen::TAlgoStateError > error_list;
1608 list< ::SMESH_Gen::TAlgoStateError >::iterator error;
1609 // call ::SMESH_Gen::GetAlgoState()
1610 myGen.GetAlgoState( myLocMesh, myLocShape, error_list );
1611 error_array->length( error_list.size() );
1613 for ( error = error_list.begin(); error != error_list.end(); ++error )
1615 // fill AlgoStateError structure
1616 SMESH::AlgoStateError & errStruct = error_array[ i++ ];
1617 errStruct.state = SMESH_Mesh_i::ConvertHypothesisStatus( error->_name );
1618 errStruct.algoDim = error->_algoDim;
1619 errStruct.isGlobalAlgo = error->_isGlobalAlgo;
1620 errStruct.algoName = "";
1621 SALOMEDS::SObject_wrap algoSO = GetAlgoSO( error->_algo );
1622 if ( !algoSO->_is_nil() ) {
1623 CORBA::String_var algoName = algoSO->GetName();
1624 errStruct.algoName = algoName.in();
1629 catch ( SALOME_Exception& S_ex ) {
1630 INFOS( "catch exception "<< S_ex.what() );
1632 return error_array._retn();
1635 //=============================================================================
1637 * SMESH_Gen_i::GetSubShapesId
1639 * Get sub-shapes unique ID's list
1641 //=============================================================================
1643 SMESH::long_array* SMESH_Gen_i::GetSubShapesId( GEOM::GEOM_Object_ptr theMainShapeObject,
1644 const SMESH::object_array& theListOfSubShapeObject )
1645 throw ( SALOME::SALOME_Exception )
1647 Unexpect aCatch(SALOME_SalomeException);
1648 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetSubShapesId" );
1650 SMESH::long_array_var shapesId = new SMESH::long_array;
1653 if ( CORBA::is_nil( theMainShapeObject ) )
1654 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1655 SALOME::BAD_PARAM );
1659 TopoDS_Shape myMainShape = GeomObjectToShape(theMainShapeObject);
1660 TopTools_IndexedMapOfShape myIndexToShape;
1661 TopExp::MapShapes(myMainShape,myIndexToShape);
1663 for ( int i = 0; i < theListOfSubShapeObject.length(); i++ )
1665 GEOM::GEOM_Object_var aShapeObject
1666 = GEOM::GEOM_Object::_narrow(theListOfSubShapeObject[i]);
1667 if ( CORBA::is_nil( aShapeObject ) )
1668 THROW_SALOME_CORBA_EXCEPTION ("bad shape object reference", \
1669 SALOME::BAD_PARAM );
1671 TopoDS_Shape locShape = GeomObjectToShape(aShapeObject);
1672 for (TopExp_Explorer exp(locShape,TopAbs_FACE); exp.More(); exp.Next())
1674 const TopoDS_Face& F = TopoDS::Face(exp.Current());
1675 setId.insert(myIndexToShape.FindIndex(F));
1676 if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(F));
1678 for (TopExp_Explorer exp(locShape,TopAbs_EDGE); exp.More(); exp.Next())
1680 const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
1681 setId.insert(myIndexToShape.FindIndex(E));
1682 if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(E));
1684 for (TopExp_Explorer exp(locShape,TopAbs_VERTEX); exp.More(); exp.Next())
1686 const TopoDS_Vertex& V = TopoDS::Vertex(exp.Current());
1687 setId.insert(myIndexToShape.FindIndex(V));
1688 if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(V));
1691 shapesId->length(setId.size());
1692 set<int>::iterator iind;
1694 for (iind = setId.begin(); iind != setId.end(); iind++)
1696 if(MYDEBUG) SCRUTE((*iind));
1697 shapesId[i] = (*iind);
1698 if(MYDEBUG) SCRUTE(shapesId[i]);
1702 catch (SALOME_Exception& S_ex)
1704 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
1707 return shapesId._retn();
1710 //=============================================================================
1712 * SMESH_Gen_i::Compute
1714 * Compute mesh on a shape
1716 //=============================================================================
1718 CORBA::Boolean SMESH_Gen_i::Compute( SMESH::SMESH_Mesh_ptr theMesh,
1719 GEOM::GEOM_Object_ptr theShapeObject )
1720 throw ( SALOME::SALOME_Exception )
1723 Unexpect aCatch(SALOME_SalomeException);
1724 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Compute" );
1726 if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
1727 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1728 SALOME::BAD_PARAM );
1730 if ( CORBA::is_nil( theMesh ) )
1731 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
1732 SALOME::BAD_PARAM );
1734 // Update Python script
1735 TPythonDump() << "isDone = " << this << ".Compute( "
1736 << theMesh << ", " << theShapeObject << ")";
1740 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
1741 meshServant->Load();
1742 ASSERT( meshServant );
1743 if ( meshServant ) {
1744 // NPAL16168: "geometrical group edition from a submesh don't modifiy mesh computation"
1745 meshServant->CheckGeomGroupModif();
1746 // get local TopoDS_Shape
1747 TopoDS_Shape myLocShape;
1748 if(theMesh->HasShapeToMesh())
1749 myLocShape = GeomObjectToShape( theShapeObject );
1751 myLocShape = SMESH_Mesh::PseudoShape();
1752 // call implementation compute
1753 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1754 myGen.PrepareCompute( myLocMesh, myLocShape);
1755 bool ok = myGen.Compute( myLocMesh, myLocShape);
1756 meshServant->CreateGroupServants(); // algos can create groups (issue 0020918)
1757 myLocMesh.GetMeshDS()->Modified();
1761 catch ( std::bad_alloc ) {
1762 INFOS( "Compute(): lack of memory" );
1764 catch ( SALOME_Exception& S_ex ) {
1765 INFOS( "Compute(): catch exception "<< S_ex.what() );
1768 INFOS( "Compute(): unknown exception " );
1773 //=============================================================================
1775 * SMESH_Gen_i::CancelCompute
1777 * Cancel Compute mesh on a shape
1779 //=============================================================================
1781 void SMESH_Gen_i::CancelCompute( SMESH::SMESH_Mesh_ptr theMesh,
1782 GEOM::GEOM_Object_ptr theShapeObject )
1784 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
1785 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1786 TopoDS_Shape myLocShape;
1787 if(theMesh->HasShapeToMesh())
1788 myLocShape = GeomObjectToShape( theShapeObject );
1790 myLocShape = SMESH_Mesh::PseudoShape();
1791 myGen.CancelCompute( myLocMesh, myLocShape);
1794 //=============================================================================
1796 * SMESH_Gen_i::Precompute
1798 * Compute mesh as preview till indicated dimension on shape
1800 //=============================================================================
1802 SMESH::MeshPreviewStruct* SMESH_Gen_i::Precompute( SMESH::SMESH_Mesh_ptr theMesh,
1803 GEOM::GEOM_Object_ptr theShapeObject,
1804 SMESH::Dimension theDimension,
1805 SMESH::long_array& theShapesId)
1806 throw ( SALOME::SALOME_Exception )
1808 Unexpect aCatch(SALOME_SalomeException);
1809 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Precompute" );
1811 if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
1812 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1813 SALOME::BAD_PARAM );
1815 if ( CORBA::is_nil( theMesh ) )
1816 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
1817 SALOME::BAD_PARAM );
1819 SMESH::MeshPreviewStruct_var result = new SMESH::MeshPreviewStruct;
1822 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
1823 meshServant->Load();
1824 ASSERT( meshServant );
1825 if ( meshServant ) {
1826 // NPAL16168: "geometrical group edition from a submesh don't modifiy mesh computation"
1827 meshServant->CheckGeomGroupModif();
1828 // get local TopoDS_Shape
1829 TopoDS_Shape myLocShape;
1830 if(theMesh->HasShapeToMesh())
1831 myLocShape = GeomObjectToShape( theShapeObject );
1833 return result._retn();;
1835 // call implementation compute
1836 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1838 ::MeshDimension aDim = (MeshDimension)theDimension;
1839 if ( myGen.Compute( myLocMesh, myLocShape, false, false, aDim, &shapeIds ) )
1841 int nbShapeId = shapeIds.size();
1842 theShapesId.length( nbShapeId );
1843 // iterates on shapes and collect mesh entities into mesh preview
1844 TSetOfInt::const_iterator idIt = shapeIds.begin();
1845 TSetOfInt::const_iterator idEnd = shapeIds.end();
1846 std::map< int, int > mapOfShIdNb;
1847 std::set< SMESH_TLink > setOfEdge;
1848 std::list< SMDSAbs_ElementType > listOfElemType;
1849 typedef map<const SMDS_MeshElement*, int > TNode2LocalIDMap;
1850 typedef TNode2LocalIDMap::iterator TNodeLocalID;
1851 TNode2LocalIDMap mapNode2LocalID;
1852 list< TNodeLocalID > connectivity;
1853 int i, nbConnNodes = 0;
1854 std::set< const SMESH_subMesh* > setOfVSubMesh;
1855 // iterates on shapes
1856 for ( ; idIt != idEnd; idIt++ )
1858 if ( mapOfShIdNb.find( *idIt ) != mapOfShIdNb.end() )
1860 SMESH_subMesh* sm = myLocMesh.GetSubMeshContaining(*idIt);
1861 if ( !sm || !sm->IsMeshComputed() )
1864 const TopoDS_Shape& aSh = sm->GetSubShape();
1865 const int shDim = myGen.GetShapeDim( aSh );
1866 if ( shDim < 1 || shDim > theDimension )
1869 mapOfShIdNb[ *idIt ] = 0;
1870 theShapesId[ mapOfShIdNb.size() - 1 ] = *idIt;
1872 SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
1873 if ( !smDS ) continue;
1875 if ( theDimension == SMESH::DIM_2D )
1877 SMDS_ElemIteratorPtr faceIt = smDS->GetElements();
1878 while ( faceIt->more() )
1880 const SMDS_MeshElement* face = faceIt->next();
1881 int aNbNode = face->NbNodes();
1883 aNbNode /= 2; // do not take into account additional middle nodes
1885 SMDS_MeshNode* node1 = (SMDS_MeshNode*)face->GetNode( 0 );
1886 for ( int nIndx = 0; nIndx < aNbNode; nIndx++ )
1888 SMDS_MeshNode* node2 = (SMDS_MeshNode*)face->GetNode( nIndx+1 < aNbNode ? nIndx+1 : 0 );
1889 if ( setOfEdge.insert( SMESH_TLink ( node1, node2 ) ).second )
1891 listOfElemType.push_back( SMDSAbs_Edge );
1892 connectivity.push_back
1893 ( mapNode2LocalID.insert( make_pair( node1, ++nbConnNodes)).first );
1894 connectivity.push_back
1895 ( mapNode2LocalID.insert( make_pair( node2, ++nbConnNodes)).first );
1901 else if ( theDimension == SMESH::DIM_1D )
1903 SMDS_NodeIteratorPtr nodeIt = smDS->GetNodes();
1904 while ( nodeIt->more() )
1906 listOfElemType.push_back( SMDSAbs_Node );
1907 connectivity.push_back
1908 ( mapNode2LocalID.insert( make_pair( nodeIt->next(), ++nbConnNodes)).first );
1910 // add corner nodes by first vertex from edge
1911 SMESH_subMeshIteratorPtr edgeSmIt =
1912 sm->getDependsOnIterator(/*includeSelf*/false,
1913 /*complexShapeFirst*/false);
1914 while ( edgeSmIt->more() )
1916 SMESH_subMesh* vertexSM = edgeSmIt->next();
1917 // check that vertex is not already treated
1918 if ( !setOfVSubMesh.insert( vertexSM ).second )
1920 if ( vertexSM->GetSubShape().ShapeType() != TopAbs_VERTEX )
1923 const SMESHDS_SubMesh* vertexSmDS = vertexSM->GetSubMeshDS();
1924 SMDS_NodeIteratorPtr nodeIt = vertexSmDS->GetNodes();
1925 while ( nodeIt->more() )
1927 listOfElemType.push_back( SMDSAbs_Node );
1928 connectivity.push_back
1929 ( mapNode2LocalID.insert( make_pair( nodeIt->next(), ++nbConnNodes)).first );
1935 // fill node coords and assign local ids to the nodes
1936 int nbNodes = mapNode2LocalID.size();
1937 result->nodesXYZ.length( nbNodes );
1938 TNodeLocalID node2ID = mapNode2LocalID.begin();
1939 for ( i = 0; i < nbNodes; ++i, ++node2ID ) {
1940 node2ID->second = i;
1941 const SMDS_MeshNode* node = (const SMDS_MeshNode*) node2ID->first;
1942 result->nodesXYZ[i].x = node->X();
1943 result->nodesXYZ[i].y = node->Y();
1944 result->nodesXYZ[i].z = node->Z();
1946 // fill connectivity
1947 result->elementConnectivities.length( nbConnNodes );
1948 list< TNodeLocalID >::iterator connIt = connectivity.begin();
1949 for ( i = 0; i < nbConnNodes; ++i, ++connIt ) {
1950 result->elementConnectivities[i] = (*connIt)->second;
1953 // fill element types
1954 result->elementTypes.length( listOfElemType.size() );
1955 std::list< SMDSAbs_ElementType >::const_iterator typeIt = listOfElemType.begin();
1956 std::list< SMDSAbs_ElementType >::const_iterator typeEnd = listOfElemType.end();
1957 for ( i = 0; typeIt != typeEnd; ++i, ++typeIt )
1959 SMDSAbs_ElementType elemType = *typeIt;
1960 result->elementTypes[i].SMDS_ElementType = (SMESH::ElementType)elemType;
1961 result->elementTypes[i].isPoly = false;
1962 result->elementTypes[i].nbNodesInElement = elemType == SMDSAbs_Edge ? 2 : 1;
1965 // correct number of shapes
1966 theShapesId.length( mapOfShIdNb.size() );
1970 catch ( std::bad_alloc ) {
1971 INFOS( "Precompute(): lack of memory" );
1973 catch ( SALOME_Exception& S_ex ) {
1974 INFOS( "Precompute(): catch exception "<< S_ex.what() );
1977 INFOS( "Precompute(): unknown exception " );
1979 return result._retn();
1983 //=============================================================================
1985 * SMESH_Gen_i::Evaluate
1987 * Evaluate mesh on a shape
1989 //=============================================================================
1991 SMESH::long_array* SMESH_Gen_i::Evaluate(SMESH::SMESH_Mesh_ptr theMesh,
1992 GEOM::GEOM_Object_ptr theShapeObject)
1993 // SMESH::long_array& theNbElems)
1994 throw ( SALOME::SALOME_Exception )
1996 Unexpect aCatch(SALOME_SalomeException);
1997 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Evaluate" );
1999 if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
2000 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
2001 SALOME::BAD_PARAM );
2003 if ( CORBA::is_nil( theMesh ) )
2004 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
2005 SALOME::BAD_PARAM );
2007 SMESH::long_array_var nbels = new SMESH::long_array;
2008 nbels->length(SMESH::Entity_Last);
2009 int i = SMESH::Entity_Node;
2010 for (; i < SMESH::Entity_Last; i++)
2013 // Update Python script
2014 TPythonDump() << "theNbElems = " << this << ".Evaluate( "
2015 << theMesh << ", " << theShapeObject << ")";
2019 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
2020 ASSERT( meshServant );
2021 if ( meshServant ) {
2022 // NPAL16168: "geometrical group edition from a submesh don't modifiy mesh computation"
2023 meshServant->CheckGeomGroupModif();
2024 // get local TopoDS_Shape
2025 TopoDS_Shape myLocShape;
2026 if(theMesh->HasShapeToMesh())
2027 myLocShape = GeomObjectToShape( theShapeObject );
2029 myLocShape = SMESH_Mesh::PseudoShape();
2030 // call implementation compute
2031 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
2032 MapShapeNbElems aResMap;
2033 /*CORBA::Boolean ret =*/ myGen.Evaluate( myLocMesh, myLocShape, aResMap);
2034 MapShapeNbElemsItr anIt = aResMap.begin();
2035 for(; anIt!=aResMap.end(); anIt++) {
2036 const vector<int>& aVec = (*anIt).second;
2037 for(i = SMESH::Entity_Node; i < aVec.size(); i++) {
2038 int nbElem = aVec[i];
2039 if ( nbElem < 0 ) // algo failed, check that it has reported a message
2041 SMESH_subMesh* sm = anIt->first;
2042 SMESH_ComputeErrorPtr& error = sm->GetComputeError();
2043 const SMESH_Algo* algo = myGen.GetAlgo( myLocMesh, sm->GetSubShape());
2044 if ( (algo && !error.get()) || error->IsOK() )
2045 error.reset( new SMESH_ComputeError( COMPERR_ALGO_FAILED,"Failed to evaluate",algo));
2049 nbels[i] += aVec[i];
2053 return nbels._retn();
2056 catch ( std::bad_alloc ) {
2057 INFOS( "Evaluate(): lack of memory" );
2059 catch ( SALOME_Exception& S_ex ) {
2060 INFOS( "Evaluate(): catch exception "<< S_ex.what() );
2063 INFOS( "Evaluate(): unknown exception " );
2066 return nbels._retn();
2069 //================================================================================
2071 * \brief Return geometrical object the given element is built on
2072 * \param theMesh - the mesh the element is in
2073 * \param theElementID - the element ID
2074 * \param theGeomName - the name of the result geom object if it is not yet published
2075 * \retval GEOM::GEOM_Object_ptr - the found or just published geom object (no need to UnRegister())
2077 //================================================================================
2079 GEOM::GEOM_Object_ptr
2080 SMESH_Gen_i::GetGeometryByMeshElement( SMESH::SMESH_Mesh_ptr theMesh,
2081 CORBA::Long theElementID,
2082 const char* theGeomName)
2083 throw ( SALOME::SALOME_Exception )
2085 Unexpect aCatch(SALOME_SalomeException);
2087 GEOM::GEOM_Object_wrap geom = FindGeometryByMeshElement(theMesh, theElementID);
2088 if ( !geom->_is_nil() ) {
2089 GEOM::GEOM_Object_var mainShape = theMesh->GetShapeToMesh();
2090 GEOM::GEOM_Gen_ptr geomGen = GetGeomEngine();
2092 // try to find the corresponding SObject
2093 SALOMEDS::SObject_wrap SObj = ObjectToSObject( myCurrentStudy, geom.in() );
2094 if ( SObj->_is_nil() ) // submesh can be not found even if published
2096 // try to find published submesh
2097 GEOM::ListOfLong_var list = geom->GetSubShapeIndices();
2098 if ( !geom->IsMainShape() && list->length() == 1 ) {
2099 SALOMEDS::SObject_wrap mainSO = ObjectToSObject( myCurrentStudy, mainShape );
2100 SALOMEDS::ChildIterator_wrap it;
2101 if ( !mainSO->_is_nil() ) {
2102 it = myCurrentStudy->NewChildIterator( mainSO );
2104 if ( !it->_is_nil() ) {
2105 for ( it->InitEx(true); it->More(); it->Next() ) {
2106 SALOMEDS::SObject_wrap so = it->Value();
2107 CORBA::Object_var obj = SObjectToObject( so );
2108 GEOM::GEOM_Object_var subGeom = GEOM::GEOM_Object::_narrow( obj );
2109 if ( !subGeom->_is_nil() ) {
2110 GEOM::ListOfLong_var subList = subGeom->GetSubShapeIndices();
2111 if ( subList->length() == 1 && list[0] == subList[0] ) {
2121 if ( SObj->_is_nil() ) // publish a new subshape
2122 SObj = geomGen->AddInStudy( myCurrentStudy, geom, theGeomName, mainShape );
2124 // return only published geometry
2125 if ( !SObj->_is_nil() ) {
2126 //return geom._retn(); -- servant of geom must be UnRegister()ed;
2127 CORBA::Object_var obj = SObjectToObject( SObj );
2128 GEOM::GEOM_Object_var go = GEOM::GEOM_Object::_narrow( obj );
2132 return GEOM::GEOM_Object::_nil();
2135 //================================================================================
2137 * \brief Return geometrical object the given element is built on.
2138 * \param theMesh - the mesh the element is in
2139 * \param theElementID - the element ID
2140 * \retval GEOM::GEOM_Object_ptr - the found or created (UnRegister()!) geom object
2142 //================================================================================
2144 GEOM::GEOM_Object_ptr
2145 SMESH_Gen_i::FindGeometryByMeshElement( SMESH::SMESH_Mesh_ptr theMesh,
2146 CORBA::Long theElementID)
2147 throw ( SALOME::SALOME_Exception )
2149 Unexpect aCatch(SALOME_SalomeException);
2150 if ( CORBA::is_nil( theMesh ) )
2151 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference", SALOME::BAD_PARAM );
2153 GEOM::GEOM_Object_var mainShape = theMesh->GetShapeToMesh();
2154 GEOM::GEOM_Gen_ptr geomGen = GetGeomEngine();
2156 // get a core mesh DS
2157 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
2158 if ( meshServant && !geomGen->_is_nil() && !mainShape->_is_nil() )
2160 ::SMESH_Mesh & mesh = meshServant->GetImpl();
2161 SMESHDS_Mesh* meshDS = mesh.GetMeshDS();
2162 // find the element in mesh
2163 if ( const SMDS_MeshElement * elem = meshDS->FindElement( theElementID ) ) {
2164 // find a shape id by the element
2165 if ( int shapeID = ::SMESH_MeshEditor( &mesh ).FindShape( elem )) {
2166 // get a geom object by the shape id
2167 GEOM::GEOM_Object_var geom = ShapeToGeomObject( meshDS->IndexToShape( shapeID ));
2168 if ( geom->_is_nil() ) {
2169 // try to find a published sub-shape
2170 SALOMEDS::SObject_wrap mainSO = ObjectToSObject( myCurrentStudy, mainShape );
2171 SALOMEDS::ChildIterator_wrap it;
2172 if ( !mainSO->_is_nil() ) {
2173 it = myCurrentStudy->NewChildIterator( mainSO );
2175 if ( !it->_is_nil() ) {
2176 for ( it->InitEx(true); it->More(); it->Next() ) {
2177 SALOMEDS::SObject_wrap so = it->Value();
2178 CORBA::Object_var obj = SObjectToObject( so );
2179 GEOM::GEOM_Object_var subGeom = GEOM::GEOM_Object::_narrow( obj );
2180 if ( !subGeom->_is_nil() ) {
2181 GEOM::ListOfLong_var subList = subGeom->GetSubShapeIndices();
2182 if ( subList->length() == 1 && shapeID == subList[0] ) {
2190 if ( geom->_is_nil() ) {
2192 GEOM::GEOM_IShapesOperations_wrap op =
2193 geomGen->GetIShapesOperations( GetCurrentStudyID() );
2194 if ( !op->_is_nil() )
2195 geom = op->GetSubShape( mainShape, shapeID );
2200 if ( !geom->_is_nil() ) {
2201 GeomObjectToShape( geom ); // let geom client remember the found shape
2202 return geom._retn();
2207 return GEOM::GEOM_Object::_nil();
2210 //================================================================================
2212 * SMESH_Gen_i::Concatenate
2214 * Concatenate the given meshes into one mesh
2216 //================================================================================
2218 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::Concatenate(const SMESH::mesh_array& theMeshesArray,
2219 CORBA::Boolean theUniteIdenticalGroups,
2220 CORBA::Boolean theMergeNodesAndElements,
2221 CORBA::Double theMergeTolerance)
2222 throw ( SALOME::SALOME_Exception )
2224 return ConcatenateCommon(theMeshesArray,
2225 theUniteIdenticalGroups,
2226 theMergeNodesAndElements,
2231 //================================================================================
2233 * SMESH_Gen_i::ConcatenateWithGroups
2235 * Concatenate the given meshes into one mesh
2236 * Create the groups of all elements from initial meshes
2238 //================================================================================
2240 SMESH::SMESH_Mesh_ptr
2241 SMESH_Gen_i::ConcatenateWithGroups(const SMESH::mesh_array& theMeshesArray,
2242 CORBA::Boolean theUniteIdenticalGroups,
2243 CORBA::Boolean theMergeNodesAndElements,
2244 CORBA::Double theMergeTolerance)
2245 throw ( SALOME::SALOME_Exception )
2247 return ConcatenateCommon(theMeshesArray,
2248 theUniteIdenticalGroups,
2249 theMergeNodesAndElements,
2254 //================================================================================
2256 * SMESH_Gen_i::ConcatenateCommon
2258 * Concatenate the given meshes into one mesh
2260 //================================================================================
2262 SMESH::SMESH_Mesh_ptr
2263 SMESH_Gen_i::ConcatenateCommon(const SMESH::mesh_array& theMeshesArray,
2264 CORBA::Boolean theUniteIdenticalGroups,
2265 CORBA::Boolean theMergeNodesAndElements,
2266 CORBA::Double theMergeTolerance,
2267 CORBA::Boolean theCommonGroups)
2268 throw ( SALOME::SALOME_Exception )
2270 typedef map<int, int> TIDsMap;
2271 typedef list<SMESH::SMESH_Group_var> TListOfNewGroups;
2272 typedef map< pair<string, SMESH::ElementType>, TListOfNewGroups > TGroupsMap;
2273 typedef std::set<SMESHDS_GroupBase*> TGroups;
2275 TPythonDump* pPythonDump = new TPythonDump;
2276 TPythonDump& aPythonDump = *pPythonDump; // prevent dump of called methods
2279 SMESH::SMESH_Mesh_var aNewMesh = CreateEmptyMesh();
2281 SMESHDS_Mesh* aNewMeshDS = 0;
2282 if ( !aNewMesh->_is_nil() ) {
2283 SMESH_Mesh_i* aNewImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( aNewMesh ).in() );
2285 ::SMESH_Mesh& aLocMesh = aNewImpl->GetImpl();
2286 aNewMeshDS = aLocMesh.GetMeshDS();
2288 TGroupsMap aGroupsMap;
2289 TListOfNewGroups aListOfNewGroups;
2290 SMESH_MeshEditor aNewEditor = ::SMESH_MeshEditor(&aLocMesh);
2291 SMESH::ListOfGroups_var aListOfGroups = new SMESH::ListOfGroups();
2294 for ( int i = 0; i < theMeshesArray.length(); i++) {
2295 SMESH::SMESH_Mesh_var anInitMesh = theMeshesArray[i];
2296 if ( !anInitMesh->_is_nil() ) {
2297 SMESH_Mesh_i* anInitImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( anInitMesh ).in() );
2299 ::SMESH_Mesh& aInitLocMesh = anInitImpl->GetImpl();
2300 aInitLocMesh.Load();
2301 SMESHDS_Mesh* anInitMeshDS = aInitLocMesh.GetMeshDS();
2306 // loop on elements of mesh
2307 SMDS_ElemIteratorPtr itElems = anInitMeshDS->elementsIterator();
2308 const SMDS_MeshElement* anElem = 0;
2309 const SMDS_MeshElement* aNewElem = 0;
2310 int anElemNbNodes = 0;
2315 int anNbVolumes = 0;
2318 SMESH::long_array_var anIDsNodes = new SMESH::long_array();
2319 SMESH::long_array_var anIDsEdges = new SMESH::long_array();
2320 SMESH::long_array_var anIDsFaces = new SMESH::long_array();
2321 SMESH::long_array_var anIDsVolumes = new SMESH::long_array();
2322 SMESH::long_array_var anIDsBalls = new SMESH::long_array();
2324 if( theCommonGroups ) {
2325 anIDsNodes->length( anInitMeshDS->NbNodes() );
2326 anIDsEdges->length( anInitMeshDS->NbEdges() );
2327 anIDsFaces->length( anInitMeshDS->NbFaces() );
2328 anIDsVolumes->length( anInitMeshDS->NbVolumes() );
2329 anIDsBalls->length( anInitMeshDS->NbBalls() );
2332 for ( int j = 0; itElems->more(); j++) {
2333 anElem = itElems->next();
2334 SMDSAbs_ElementType anElemType = anElem->GetType();
2335 anElemNbNodes = anElem->NbNodes();
2336 std::vector<const SMDS_MeshNode*> aNodesArray (anElemNbNodes);
2338 // loop on nodes of element
2339 const SMDS_MeshNode* aNode = 0;
2340 const SMDS_MeshNode* aNewNode = 0;
2341 SMDS_ElemIteratorPtr itNodes = anElem->nodesIterator();
2343 for ( int k = 0; itNodes->more(); k++) {
2344 aNode = static_cast<const SMDS_MeshNode*>(itNodes->next());
2345 if ( nodesMap.find(aNode->GetID()) == nodesMap.end() ) {
2346 aNewNode = aNewMeshDS->AddNode(aNode->X(), aNode->Y(), aNode->Z());
2347 nodesMap.insert( make_pair(aNode->GetID(), aNewNode->GetID()) );
2348 if( theCommonGroups )
2349 anIDsNodes[anNbNodes++] = aNewNode->GetID();
2352 aNewNode = aNewMeshDS->FindNode( nodesMap.find(aNode->GetID())->second );
2353 aNodesArray[k] = aNewNode;
2356 // creates a corresponding element on existent nodes in new mesh
2357 switch ( anElem->GetEntityType() ) {
2358 case SMDSEntity_Polyhedra:
2359 if ( const SMDS_VtkVolume* aVolume =
2360 dynamic_cast<const SMDS_VtkVolume*> (anElem))
2362 aNewElem = aNewMeshDS->AddPolyhedralVolume(aNodesArray,
2363 aVolume->GetQuantities());
2364 elemsMap.insert(make_pair(anElem->GetID(), aNewElem->GetID()));
2365 if( theCommonGroups )
2366 anIDsVolumes[anNbVolumes++] = aNewElem->GetID();
2369 case SMDSEntity_Ball:
2370 if ( const SMDS_BallElement* aBall =
2371 dynamic_cast<const SMDS_BallElement*> (anElem))
2373 aNewElem = aNewEditor.AddElement(aNodesArray, SMDSAbs_Ball,
2374 /*isPoly=*/false, /*id=*/0,
2375 aBall->GetDiameter() );
2376 elemsMap.insert(make_pair(anElem->GetID(), aNewElem->GetID()));
2377 if( theCommonGroups )
2378 anIDsBalls[aNbBalls++] = aNewElem->GetID();
2383 aNewElem = aNewEditor.AddElement(aNodesArray,
2386 elemsMap.insert(make_pair(anElem->GetID(), aNewElem->GetID()));
2387 if( theCommonGroups ) {
2388 if( anElemType == SMDSAbs_Edge )
2389 anIDsEdges[anNbEdges++] = aNewElem->GetID();
2390 else if( anElemType == SMDSAbs_Face )
2391 anIDsFaces[anNbFaces++] = aNewElem->GetID();
2392 else if( anElemType == SMDSAbs_Volume )
2393 anIDsVolumes[anNbVolumes++] = aNewElem->GetID();
2399 // copy orphan nodes
2400 SMDS_NodeIteratorPtr itNodes = anInitMeshDS->nodesIterator();
2401 while ( itNodes->more() )
2403 const SMDS_MeshNode* aNode = itNodes->next();
2404 if ( aNode->NbInverseElements() == 0 )
2406 const SMDS_MeshNode* aNewNode =
2407 aNewMeshDS->AddNode(aNode->X(), aNode->Y(), aNode->Z());
2408 nodesMap.insert( make_pair(aNode->GetID(), aNewNode->GetID()) );
2409 if( theCommonGroups )
2410 anIDsNodes[anNbNodes++] = aNewNode->GetID();
2415 aListOfGroups = anInitImpl->GetGroups();
2416 SMESH::SMESH_GroupBase_ptr aGroup;
2418 // loop on groups of mesh
2419 SMESH::long_array_var anInitIDs = new SMESH::long_array();
2420 SMESH::long_array_var anNewIDs = new SMESH::long_array();
2421 SMESH::SMESH_Group_var aNewGroup;
2423 SMESH::ElementType aGroupType;
2424 CORBA::String_var aGroupName;
2425 if ( theCommonGroups ) {
2426 for(aGroupType=SMESH::NODE;aGroupType<=SMESH::BALL;aGroupType=(SMESH::ElementType)(aGroupType+1)) {
2428 SALOMEDS::SObject_wrap aMeshSObj = ObjectToSObject( myCurrentStudy, anInitMesh );
2430 str += aMeshSObj->GetName();
2435 switch(aGroupType) {
2438 anIDsNodes->length(anNbNodes);
2443 anIDsEdges->length(anNbEdges);
2448 anIDsFaces->length(anNbFaces);
2453 anIDsVolumes->length(anNbVolumes);
2454 anLen = anNbVolumes;
2458 anIDsBalls->length(aNbBalls);
2466 aGroupName = str.c_str();
2468 // add a new group in the mesh
2469 aNewGroup = aNewImpl->CreateGroup(aGroupType, aGroupName);
2471 switch(aGroupType) {
2473 aNewGroup->Add( anIDsNodes );
2476 aNewGroup->Add( anIDsEdges );
2479 aNewGroup->Add( anIDsFaces );
2482 aNewGroup->Add( anIDsVolumes );
2485 aNewGroup->Add( anIDsBalls );
2491 aListOfNewGroups.clear();
2492 aListOfNewGroups.push_back(aNewGroup);
2493 aGroupsMap.insert(make_pair( make_pair(aGroupName, aGroupType), aListOfNewGroups ));
2498 // check that current group name and type don't have identical ones in union mesh
2499 for (int iG = 0; iG < aListOfGroups->length(); iG++) {
2500 aGroup = aListOfGroups[iG];
2501 aListOfNewGroups.clear();
2502 aGroupType = aGroup->GetType();
2503 aGroupName = aGroup->GetName();
2505 TGroupsMap::iterator anIter = aGroupsMap.find(make_pair(aGroupName, aGroupType));
2507 // convert a list of IDs
2508 anInitIDs = aGroup->GetListOfID();
2509 anNewIDs->length(anInitIDs->length());
2510 if ( aGroupType == SMESH::NODE )
2511 for (int j = 0; j < anInitIDs->length(); j++) {
2512 anNewIDs[j] = nodesMap.find(anInitIDs[j])->second;
2515 for (int j = 0; j < anInitIDs->length(); j++) {
2516 anNewIDs[j] = elemsMap.find(anInitIDs[j])->second;
2519 // check that current group name and type don't have identical ones in union mesh
2520 if ( anIter == aGroupsMap.end() ) {
2521 // add a new group in the mesh
2522 aNewGroup = aNewImpl->CreateGroup(aGroupType, aGroupName);
2523 // add elements into new group
2524 aNewGroup->Add( anNewIDs );
2526 aListOfNewGroups.push_back(aNewGroup);
2527 aGroupsMap.insert(make_pair( make_pair(aGroupName, aGroupType), aListOfNewGroups ));
2530 else if ( theUniteIdenticalGroups ) {
2531 // unite identical groups
2532 TListOfNewGroups& aNewGroups = anIter->second;
2533 aNewGroups.front()->Add( anNewIDs );
2537 // rename identical groups
2538 aNewGroup = aNewImpl->CreateGroup(aGroupType, aGroupName);
2539 aNewGroup->Add( anNewIDs );
2541 TListOfNewGroups& aNewGroups = anIter->second;
2542 string aNewGroupName;
2543 if (aNewGroups.size() == 1) {
2544 aNewGroupName = string(aGroupName) + "_1";
2545 aNewGroups.front()->SetName(aNewGroupName.c_str());
2547 char aGroupNum[128];
2548 sprintf(aGroupNum, "%u", aNewGroups.size()+1);
2549 aNewGroupName = string(aGroupName) + "_" + string(aGroupNum);
2550 aNewGroup->SetName(aNewGroupName.c_str());
2551 aNewGroups.push_back(aNewGroup);
2558 if (theMergeNodesAndElements) {
2560 TIDSortedNodeSet aMeshNodes; // no input nodes
2561 SMESH_MeshEditor::TListOfListOfNodes aGroupsOfNodes;
2562 aNewEditor.FindCoincidentNodes( aMeshNodes, theMergeTolerance, aGroupsOfNodes );
2563 aNewEditor.MergeNodes( aGroupsOfNodes );
2565 aNewEditor.MergeEqualElements();
2570 // Update Python script
2571 aPythonDump << aNewMesh << " = " << this;
2572 if( !theCommonGroups )
2573 aPythonDump << ".Concatenate(";
2575 aPythonDump << ".ConcatenateWithGroups(";
2577 for ( int i = 0; i < theMeshesArray.length(); i++) {
2578 if (i > 0) aPythonDump << ", ";
2579 aPythonDump << theMeshesArray[i];
2581 aPythonDump << "], ";
2582 aPythonDump << theUniteIdenticalGroups << ", "
2583 << theMergeNodesAndElements << ", "
2584 << TVar( theMergeTolerance ) << ")";
2586 delete pPythonDump; // enable python dump from GetGroups()
2588 // 0020577: EDF 1164 SMESH: Bad dump of concatenate with create common groups
2589 if ( !aNewMesh->_is_nil() )
2591 SMESH::ListOfGroups_var groups = aNewMesh->GetGroups();
2594 // IPAL21468 Change icon of compound because it need not be computed.
2595 SALOMEDS::SObject_wrap aMeshSObj = ObjectToSObject( myCurrentStudy, aNewMesh );
2596 SetPixMap( aMeshSObj, "ICON_SMESH_TREE_MESH" );
2599 aNewMeshDS->Modified();
2600 return aNewMesh._retn();
2603 //================================================================================
2605 * \brief Create a mesh by copying a part of another mesh
2606 * \param meshPart - a part of mesh to copy
2607 * \param toCopyGroups - to create in the new mesh groups
2608 * the copied elements belongs to
2609 * \param toKeepIDs - to preserve IDs of the copied elements or not
2610 * \retval SMESH::SMESH_Mesh_ptr - the new mesh
2612 //================================================================================
2614 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CopyMesh(SMESH::SMESH_IDSource_ptr meshPart,
2615 const char* meshName,
2616 CORBA::Boolean toCopyGroups,
2617 CORBA::Boolean toKeepIDs)
2619 Unexpect aCatch(SALOME_SalomeException);
2621 TPythonDump* pyDump = new TPythonDump; // prevent dump from CreateMesh()
2623 // 1. Get source mesh
2625 if ( CORBA::is_nil( meshPart ))
2626 THROW_SALOME_CORBA_EXCEPTION( "bad IDSource", SALOME::BAD_PARAM );
2628 SMESH::SMESH_Mesh_var srcMesh = meshPart->GetMesh();
2629 SMESH_Mesh_i* srcMesh_i = SMESH::DownCast<SMESH_Mesh_i*>( srcMesh );
2631 THROW_SALOME_CORBA_EXCEPTION( "bad mesh of IDSource", SALOME::BAD_PARAM );
2633 SMESHDS_Mesh* srcMeshDS = srcMesh_i->GetImpl().GetMeshDS();
2635 // 2. Make a new mesh
2637 SMESH::SMESH_Mesh_var newMesh = CreateMesh(GEOM::GEOM_Object::_nil());
2638 SMESH_Mesh_i* newMesh_i = SMESH::DownCast<SMESH_Mesh_i*>( newMesh );
2640 THROW_SALOME_CORBA_EXCEPTION( "can't create a mesh", SALOME::INTERNAL_ERROR );
2641 SALOMEDS::SObject_wrap meshSO = ObjectToSObject(myCurrentStudy, newMesh );
2642 if ( !meshSO->_is_nil() )
2644 SetName( meshSO, meshName, "Mesh" );
2645 SetPixMap( meshSO, "ICON_SMESH_TREE_MESH_IMPORTED");
2647 SMESHDS_Mesh* newMeshDS = newMesh_i->GetImpl().GetMeshDS();
2648 ::SMESH_MeshEditor editor( &newMesh_i->GetImpl() );
2650 // 3. Get elements to copy
2652 SMDS_ElemIteratorPtr srcElemIt; SMDS_NodeIteratorPtr srcNodeIt;
2653 TIDSortedElemSet srcElems;
2654 SMESH::array_of_ElementType_var srcElemTypes = meshPart->GetTypes();
2655 if ( SMESH::DownCast<SMESH_Mesh_i*>( meshPart ))
2657 srcElemIt = srcMeshDS->elementsIterator();
2658 srcNodeIt = srcMeshDS->nodesIterator();
2662 SMESH::long_array_var ids = meshPart->GetIDs();
2663 if ( srcElemTypes->length() == 1 && srcElemTypes[0] == SMESH::NODE ) // group of nodes
2665 for (int i=0; i < ids->length(); i++)
2666 if ( const SMDS_MeshElement * elem = srcMeshDS->FindNode( ids[i] ))
2667 srcElems.insert( elem );
2671 for (int i=0; i < ids->length(); i++)
2672 if ( const SMDS_MeshElement * elem = srcMeshDS->FindElement( ids[i] ))
2673 srcElems.insert( elem );
2675 if ( srcElems.empty() )
2676 return newMesh._retn();
2678 typedef SMDS_SetIterator< SMDS_pElement, TIDSortedElemSet::const_iterator > ElIter;
2679 srcElemIt = SMDS_ElemIteratorPtr( new ElIter( srcElems.begin(), srcElems.end() ));
2684 typedef map<SMDS_pElement, SMDS_pElement, TIDCompare> TE2EMap;
2685 TE2EMap e2eMapByType[ SMDSAbs_NbElementTypes ];
2686 TE2EMap& n2nMap = e2eMapByType[ SMDSAbs_Node ];
2688 const SMDS_MeshNode *nSrc, *nTgt;
2689 vector< const SMDS_MeshNode* > nodes;
2690 while ( srcElemIt->more() )
2692 const SMDS_MeshElement * elem = srcElemIt->next();
2694 nodes.resize( elem->NbNodes());
2695 SMDS_ElemIteratorPtr nIt = elem->nodesIterator();
2697 for ( iN = 0; nIt->more(); ++iN )
2699 nSrc = static_cast<const SMDS_MeshNode*>( nIt->next() );
2700 nTgt = newMeshDS->FindNode( nSrc->GetID());
2702 nTgt = newMeshDS->AddNodeWithID( nSrc->X(), nSrc->Y(), nSrc->Z(), nSrc->GetID());
2707 for ( iN = 0; nIt->more(); ++iN )
2709 nSrc = static_cast<const SMDS_MeshNode*>( nIt->next() );
2710 TE2EMap::iterator n2n = n2nMap.insert( make_pair( nSrc, SMDS_pNode(0) )).first;
2712 n2n->second = newMeshDS->AddNode( nSrc->X(), nSrc->Y(), nSrc->Z() );
2713 nodes[ iN ] = (const SMDS_MeshNode*) n2n->second;
2717 if ( elem->GetType() != SMDSAbs_Node )
2719 int ID = toKeepIDs ? elem->GetID() : 0;
2720 const SMDS_MeshElement * newElem;
2721 switch ( elem->GetEntityType() ) {
2722 case SMDSEntity_Polyhedra:
2723 newElem = editor.GetMeshDS()->
2724 AddPolyhedralVolumeWithID( nodes,
2725 static_cast<const SMDS_VtkVolume*>(elem)->GetQuantities(),
2728 case SMDSEntity_Ball:
2729 newElem = editor.AddElement( nodes, SMDSAbs_Ball, false, ID,
2730 static_cast<const SMDS_BallElement*>(elem)->GetDiameter());
2733 newElem = editor.AddElement( nodes,elem->GetType(),elem->IsPoly(),ID);
2735 if ( toCopyGroups && !toKeepIDs )
2736 e2eMapByType[ elem->GetType() ].insert( make_pair( elem, newElem ));
2739 } // while ( srcElemIt->more() )
2741 // 4(b). Copy free nodes
2743 if ( srcNodeIt && srcMeshDS->NbNodes() != newMeshDS->NbNodes() )
2745 while ( srcNodeIt->more() )
2747 nSrc = srcNodeIt->next();
2748 if ( nSrc->NbInverseElements() == 0 )
2751 nTgt = newMeshDS->AddNodeWithID( nSrc->X(), nSrc->Y(), nSrc->Z(), nSrc->GetID());
2753 n2nMap[ nSrc ] = newMeshDS->AddNode( nSrc->X(), nSrc->Y(), nSrc->Z() );
2760 int nbNewGroups = 0;
2763 SMESH_Mesh::GroupIteratorPtr gIt = srcMesh_i->GetImpl().GetGroups();
2764 while ( gIt->more() )
2766 SMESH_Group* group = gIt->next();
2767 const SMESHDS_GroupBase* groupDS = group->GetGroupDS();
2769 // Check group type. We copy nodal groups containing nodes of copied element
2770 SMDSAbs_ElementType groupType = groupDS->GetType();
2771 if ( groupType != SMDSAbs_Node &&
2772 newMeshDS->GetMeshInfo().NbElements( groupType ) == 0 )
2773 continue; // group type differs from types of meshPart
2775 // Find copied elements in the group
2776 vector< const SMDS_MeshElement* > groupElems;
2777 SMDS_ElemIteratorPtr eIt = groupDS->GetElements();
2780 const SMDS_MeshElement* foundElem;
2781 if ( groupType == SMDSAbs_Node )
2783 while ( eIt->more() )
2784 if (( foundElem = newMeshDS->FindNode( eIt->next()->GetID() )))
2785 groupElems.push_back( foundElem );
2789 while ( eIt->more() )
2790 if (( foundElem = newMeshDS->FindElement( eIt->next()->GetID() )))
2791 groupElems.push_back( foundElem );
2796 TE2EMap & e2eMap = e2eMapByType[ groupDS->GetType() ];
2797 if ( e2eMap.empty() ) continue;
2798 int minID = e2eMap.begin()->first->GetID();
2799 int maxID = e2eMap.rbegin()->first->GetID();
2800 TE2EMap::iterator e2e;
2801 while ( eIt->more() && groupElems.size() < e2eMap.size())
2803 const SMDS_MeshElement* e = eIt->next();
2804 if ( e->GetID() < minID || e->GetID() > maxID ) continue;
2805 if ((e2e = e2eMap.find( e )) != e2eMap.end())
2806 groupElems.push_back( e2e->second );
2810 if ( !groupElems.empty() )
2812 SMESH::SMESH_Group_var newGroupObj =
2813 newMesh->CreateGroup( SMESH::ElementType(groupType), group->GetName() );
2814 if ( SMESH_GroupBase_i* newGroup_i = SMESH::DownCast<SMESH_GroupBase_i*>( newGroupObj))
2816 SMESHDS_GroupBase * newGroupDS = newGroup_i->GetGroupDS();
2817 SMDS_MeshGroup& smdsGroup = ((SMESHDS_Group*)newGroupDS)->SMDSGroup();
2818 for ( unsigned i = 0; i < groupElems.size(); ++i )
2819 smdsGroup.Add( groupElems[i] );
2827 newMeshDS->Modified();
2829 *pyDump << newMesh << " = " << this
2830 << ".CopyMesh( " << meshPart << ", "
2831 << "'" << meshName << "', "
2832 << toCopyGroups << ", "
2833 << toKeepIDs << ")";
2835 delete pyDump; pyDump = 0; // allow dump in GetGroups()
2837 if ( nbNewGroups > 0 ) // dump created groups
2838 SMESH::ListOfGroups_var groups = newMesh->GetGroups();
2840 return newMesh._retn();
2843 //================================================================================
2845 * SMESH_Gen_i::GetMEDVersion
2847 * Get MED version of the file by its name
2849 //================================================================================
2850 CORBA::Boolean SMESH_Gen_i::GetMEDVersion(const char* theFileName,
2851 SMESH::MED_VERSION& theVersion)
2853 theVersion = SMESH::MED_V2_1;
2854 MED::EVersion aVersion = MED::GetVersionId( theFileName );
2855 switch( aVersion ) {
2856 case MED::eV2_1 : theVersion = SMESH::MED_V2_1; return true;
2857 case MED::eV2_2 : theVersion = SMESH::MED_V2_2; return true;
2858 case MED::eVUnknown : return false;
2863 //================================================================================
2865 * SMESH_Gen_i::GetMeshNames
2867 * Get names of meshes defined in file with the specified name
2869 //================================================================================
2870 SMESH::string_array* SMESH_Gen_i::GetMeshNames(const char* theFileName)
2872 SMESH::string_array_var aResult = new SMESH::string_array();
2873 MED::PWrapper aMed = MED::CrWrapper( theFileName );
2875 MED::TInt aNbMeshes = aMed->GetNbMeshes( &anErr );
2877 aResult->length( aNbMeshes );
2878 for( MED::TInt i = 0; i < aNbMeshes; i++ ) {
2879 MED::PMeshInfo aMeshInfo = aMed->GetPMeshInfo( i+1 );
2880 aResult[i] = CORBA::string_dup( aMeshInfo->GetName().c_str() );
2883 return aResult._retn();
2886 //=============================================================================
2890 * Save SMESH module's data
2892 //=============================================================================
2893 SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
2897 INFOS( "SMESH_Gen_i::Save" );
2899 // ASSERT( theComponent->GetStudy()->StudyId() == myCurrentStudy->StudyId() )
2900 // san -- in case <myCurrentStudy> differs from theComponent's study,
2901 // use that of the component
2902 if ( myCurrentStudy->_is_nil() ||
2903 theComponent->GetStudy()->StudyId() != myCurrentStudy->StudyId() )
2904 SetCurrentStudy( theComponent->GetStudy() );
2906 // Store study contents as a set of python commands
2907 SavePython(myCurrentStudy);
2909 StudyContext* myStudyContext = GetCurrentStudyContext();
2911 // Declare a byte stream
2912 SALOMEDS::TMPFile_var aStreamFile;
2914 // Obtain a temporary dir
2915 TCollection_AsciiString tmpDir =
2916 ( isMultiFile ) ? TCollection_AsciiString( ( char* )theURL ) : ( char* )SALOMEDS_Tool::GetTmpDir().c_str();
2918 // Create a sequence of files processed
2919 SALOMEDS::ListOfFileNames_var aFileSeq = new SALOMEDS::ListOfFileNames;
2920 aFileSeq->length( NUM_TMP_FILES );
2922 TCollection_AsciiString aStudyName( "" );
2924 aStudyName = ( (char*)SALOMEDS_Tool::GetNameFromPath( myCurrentStudy->URL() ).c_str() );
2926 // Set names of temporary files
2927 TCollection_AsciiString filename =
2928 aStudyName + TCollection_AsciiString( "_SMESH.hdf" ); // for SMESH data itself
2929 TCollection_AsciiString meshfile =
2930 aStudyName + TCollection_AsciiString( "_SMESH_Mesh.med" ); // for mesh data to be stored in MED file
2931 aFileSeq[ 0 ] = CORBA::string_dup( filename.ToCString() );
2932 aFileSeq[ 1 ] = CORBA::string_dup( meshfile.ToCString() );
2933 filename = tmpDir + filename;
2934 meshfile = tmpDir + meshfile;
2937 HDFdataset* aDataset;
2938 HDFgroup* aTopGroup;
2940 HDFgroup* aSubGroup;
2941 HDFgroup* aSubSubGroup;
2942 hdf_size aSize[ 1 ];
2945 //Remove the files if they exist: BugID: 11225
2946 #ifndef WIN32 /* unix functionality */
2947 TCollection_AsciiString cmd("rm -f \"");
2949 TCollection_AsciiString cmd("del /F \"");
2956 system(cmd.ToCString());
2958 // MED writer to be used by storage process
2959 DriverMED_W_SMESHDS_Mesh myWriter;
2960 myWriter.SetFile( meshfile.ToCString() );
2963 // SetStoreName() to groups before storing hypotheses to let them refer to
2964 // groups using "store name", which is "Group <group_persistent_id>"
2966 SALOMEDS::ChildIterator_wrap itBig = myCurrentStudy->NewChildIterator( theComponent );
2967 for ( ; itBig->More(); itBig->Next() ) {
2968 SALOMEDS::SObject_wrap gotBranch = itBig->Value();
2969 if ( gotBranch->Tag() > GetAlgorithmsRootTag() ) {
2970 CORBA::Object_var anObject = SObjectToObject( gotBranch );
2971 if ( !CORBA::is_nil( anObject ) ) {
2972 SMESH::SMESH_Mesh_var myMesh = SMESH::SMESH_Mesh::_narrow( anObject ) ;
2973 if ( !myMesh->_is_nil() ) {
2974 myMesh->Load(); // load from study file if not yet done
2975 TPythonDump pd; // not to dump GetGroups()
2976 SMESH::ListOfGroups_var groups = myMesh->GetGroups();
2977 pd << ""; // to avoid optimizing pd out
2978 for ( int i = 0; i < groups->length(); ++i )
2980 SMESH_GroupBase_i* grImpl = SMESH::DownCast<SMESH_GroupBase_i*>( groups[i]);
2983 CORBA::String_var objStr = GetORB()->object_to_string( grImpl->_this() );
2984 int anId = myStudyContext->findId( string( objStr.in() ) );
2986 sprintf( grpName, "Group %d", anId );
2987 SMESHDS_GroupBase* aGrpBaseDS = grImpl->GetGroupDS();
2988 aGrpBaseDS->SetStoreName( grpName );
2998 // ---> create HDF file
2999 aFile = new HDFfile( (char*) filename.ToCString() );
3000 aFile->CreateOnDisk();
3002 // --> iterator for top-level objects
3003 SALOMEDS::ChildIterator_wrap itBig = myCurrentStudy->NewChildIterator( theComponent );
3004 for ( ; itBig->More(); itBig->Next() ) {
3005 SALOMEDS::SObject_wrap gotBranch = itBig->Value();
3007 // --> hypotheses root branch (only one for the study)
3008 if ( gotBranch->Tag() == GetHypothesisRootTag() ) {
3009 // create hypotheses root HDF group
3010 aTopGroup = new HDFgroup( "Hypotheses", aFile );
3011 aTopGroup->CreateOnDisk();
3013 // iterator for all hypotheses
3014 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( gotBranch );
3015 for ( ; it->More(); it->Next() ) {
3016 SALOMEDS::SObject_wrap mySObject = it->Value();
3017 CORBA::Object_var anObject = SObjectToObject( mySObject );
3018 if ( !CORBA::is_nil( anObject ) ) {
3019 SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
3020 if ( !myHyp->_is_nil() ) {
3021 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
3023 string hypname = string( myHyp->GetName() );
3024 string libname = string( myHyp->GetLibName() );
3026 // Needs for save crossplatform libname, i.e. parth of name ( ".dll" for
3027 // WIN32 and ".so" for X-system) must be deleted
3028 int libname_len = libname.length();
3030 if( libname_len > 4 )
3031 libname.resize( libname_len - 4 );
3033 // PAL17753 (Regresion: missing hypothesis in restored study)
3034 // "lib" also should be removed from the beginning
3035 //if( libname_len > 3 )
3036 //libname.resize( libname_len - 3 );
3037 if( libname_len > 6 )
3038 libname = libname.substr( 3, libname_len - 3 - 3 );
3040 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3041 int id = myStudyContext->findId( string( objStr.in() ) );
3042 string hypdata = string( myImpl->SaveTo() );
3044 // for each hypothesis create HDF group basing on its id
3045 char hypGrpName[30];
3046 sprintf( hypGrpName, "Hypothesis %d", id );
3047 aGroup = new HDFgroup( hypGrpName, aTopGroup );
3048 aGroup->CreateOnDisk();
3049 // --> type name of hypothesis
3050 aSize[ 0 ] = hypname.length() + 1;
3051 aDataset = new HDFdataset( "Name", aGroup, HDF_STRING, aSize, 1 );
3052 aDataset->CreateOnDisk();
3053 aDataset->WriteOnDisk( ( char* )( hypname.c_str() ) );
3054 aDataset->CloseOnDisk();
3055 // --> server plugin library name of hypothesis
3056 aSize[ 0 ] = libname.length() + 1;
3057 aDataset = new HDFdataset( "LibName", aGroup, HDF_STRING, aSize, 1 );
3058 aDataset->CreateOnDisk();
3059 aDataset->WriteOnDisk( ( char* )( libname.c_str() ) );
3060 aDataset->CloseOnDisk();
3061 // --> persistent data of hypothesis
3062 aSize[ 0 ] = hypdata.length() + 1;
3063 aDataset = new HDFdataset( "Data", aGroup, HDF_STRING, aSize, 1 );
3064 aDataset->CreateOnDisk();
3065 aDataset->WriteOnDisk( ( char* )( hypdata.c_str() ) );
3066 aDataset->CloseOnDisk();
3067 // close hypothesis HDF group
3068 aGroup->CloseOnDisk();
3073 // close hypotheses root HDF group
3074 aTopGroup->CloseOnDisk();
3076 // --> algorithms root branch (only one for the study)
3077 else if ( gotBranch->Tag() == GetAlgorithmsRootTag() ) {
3078 // create algorithms root HDF group
3079 aTopGroup = new HDFgroup( "Algorithms", aFile );
3080 aTopGroup->CreateOnDisk();
3082 // iterator for all algorithms
3083 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( gotBranch );
3084 for ( ; it->More(); it->Next() ) {
3085 SALOMEDS::SObject_wrap mySObject = it->Value();
3086 CORBA::Object_var anObject = SObjectToObject( mySObject );
3087 if ( !CORBA::is_nil( anObject ) ) {
3088 SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
3089 if ( !myHyp->_is_nil() ) {
3090 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
3092 string hypname = string( myHyp->GetName() );
3093 string libname = string( myHyp->GetLibName() );
3095 // Needs for save crossplatform libname, i.e. parth of name ( ".dll" for
3096 // WIN32 and ".so" for X-system) must be deleted
3097 int libname_len = libname.length();
3099 if( libname_len > 4 )
3100 libname.resize( libname_len - 4 );
3102 // PAL17753 (Regresion: missing hypothesis in restored study)
3103 // "lib" also should be removed from the beginning
3104 //if( libname_len > 3 )
3105 //libname.resize( libname_len - 3 );
3106 if( libname_len > 6 )
3107 libname = libname.substr( 3, libname_len - 3 - 3 );
3109 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3110 int id = myStudyContext->findId( string( objStr.in() ) );
3111 string hypdata = string( myImpl->SaveTo() );
3113 // for each algorithm create HDF group basing on its id
3114 char hypGrpName[30];
3115 sprintf( hypGrpName, "Algorithm %d", id );
3116 aGroup = new HDFgroup( hypGrpName, aTopGroup );
3117 aGroup->CreateOnDisk();
3118 // --> type name of algorithm
3119 aSize[0] = hypname.length() + 1;
3120 aDataset = new HDFdataset( "Name", aGroup, HDF_STRING, aSize, 1 );
3121 aDataset->CreateOnDisk();
3122 aDataset->WriteOnDisk( ( char* )( hypname.c_str() ) );
3123 aDataset->CloseOnDisk();
3124 // --> server plugin library name of hypothesis
3125 aSize[0] = libname.length() + 1;
3126 aDataset = new HDFdataset( "LibName", aGroup, HDF_STRING, aSize, 1 );
3127 aDataset->CreateOnDisk();
3128 aDataset->WriteOnDisk( ( char* )( libname.c_str() ) );
3129 aDataset->CloseOnDisk();
3130 // --> persistent data of algorithm
3131 aSize[0] = hypdata.length() + 1;
3132 aDataset = new HDFdataset( "Data", aGroup, HDF_STRING, aSize, 1 );
3133 aDataset->CreateOnDisk();
3134 aDataset->WriteOnDisk( ( char* )( hypdata.c_str() ) );
3135 aDataset->CloseOnDisk();
3136 // close algorithm HDF group
3137 aGroup->CloseOnDisk();
3142 // close algorithms root HDF group
3143 aTopGroup->CloseOnDisk();
3145 // --> mesh objects roots branches
3146 else if ( gotBranch->Tag() > GetAlgorithmsRootTag() ) {
3147 CORBA::Object_var anObject = SObjectToObject( gotBranch );
3148 if ( !CORBA::is_nil( anObject ) ) {
3149 SMESH::SMESH_Mesh_var myMesh = SMESH::SMESH_Mesh::_narrow( anObject ) ;
3150 if ( !myMesh->_is_nil() ) {
3151 SMESH_Mesh_i* myImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( myMesh ).in() );
3153 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3154 int id = myStudyContext->findId( string( objStr.in() ) );
3155 ::SMESH_Mesh& myLocMesh = myImpl->GetImpl();
3156 SMESHDS_Mesh* mySMESHDSMesh = myLocMesh.GetMeshDS();
3157 bool hasShape = myLocMesh.HasShapeToMesh();
3159 // for each mesh open the HDF group basing on its id
3160 char meshGrpName[ 30 ];
3161 sprintf( meshGrpName, "Mesh %d", id );
3162 aTopGroup = new HDFgroup( meshGrpName, aFile );
3163 aTopGroup->CreateOnDisk();
3165 // --> put dataset to hdf file which is a flag that mesh has data
3166 string strHasData = "0";
3167 // check if the mesh is not empty
3168 if ( mySMESHDSMesh->NbNodes() > 0 ) {
3169 // write mesh data to med file
3170 myWriter.SetMesh( mySMESHDSMesh );
3171 myWriter.SetMeshId( id );
3174 aSize[ 0 ] = strHasData.length() + 1;
3175 aDataset = new HDFdataset( "Has data", aTopGroup, HDF_STRING, aSize, 1 );
3176 aDataset->CreateOnDisk();
3177 aDataset->WriteOnDisk( ( char* )( strHasData.c_str() ) );
3178 aDataset->CloseOnDisk();
3181 // for each mesh open the HDF group basing on its auto color parameter
3182 char meshAutoColorName[ 30 ];
3183 sprintf( meshAutoColorName, "AutoColorMesh %d", id );
3185 anAutoColor[0] = myImpl->GetAutoColor();
3187 aDataset = new HDFdataset( meshAutoColorName, aTopGroup, HDF_INT32, aSize, 1 );
3188 aDataset->CreateOnDisk();
3189 aDataset->WriteOnDisk( anAutoColor );
3190 aDataset->CloseOnDisk();
3192 // issue 0020693. Store _isModified flag
3193 int isModified = myLocMesh.GetIsModified();
3195 aDataset = new HDFdataset( "_isModified", aTopGroup, HDF_INT32, aSize, 1 );
3196 aDataset->CreateOnDisk();
3197 aDataset->WriteOnDisk( &isModified );
3198 aDataset->CloseOnDisk();
3200 // issue 20918. Store Persistent Id of SMESHDS_Mesh
3201 int meshPersistentId = mySMESHDSMesh->GetPersistentId();
3203 aDataset = new HDFdataset( "meshPersistentId", aTopGroup, HDF_INT32, aSize, 1 );
3204 aDataset->CreateOnDisk();
3205 aDataset->WriteOnDisk( &meshPersistentId );
3206 aDataset->CloseOnDisk();
3208 // write reference on a shape if exists
3209 SALOMEDS::SObject_wrap myRef;
3210 bool shapeRefFound = false;
3211 bool found = gotBranch->FindSubObject( GetRefOnShapeTag(), myRef.inout() );
3213 SALOMEDS::SObject_wrap myShape;
3214 bool ok = myRef->ReferencedObject( myShape.inout() );
3216 shapeRefFound = (! CORBA::is_nil( myShape->GetObject() ));
3217 string myRefOnObject = myShape->GetID();
3218 if ( shapeRefFound && myRefOnObject.length() > 0 ) {
3219 aSize[ 0 ] = myRefOnObject.length() + 1;
3220 aDataset = new HDFdataset( "Ref on shape", aTopGroup, HDF_STRING, aSize, 1 );
3221 aDataset->CreateOnDisk();
3222 aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3223 aDataset->CloseOnDisk();
3228 // write applied hypotheses if exist
3229 SALOMEDS::SObject_wrap myHypBranch;
3230 found = gotBranch->FindSubObject( GetRefOnAppliedHypothesisTag(), myHypBranch.inout() );
3231 if ( found && !shapeRefFound && hasShape) { // remove applied hyps
3232 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( myHypBranch );
3234 if ( found && (shapeRefFound || !hasShape) ) {
3235 aGroup = new HDFgroup( "Applied Hypotheses", aTopGroup );
3236 aGroup->CreateOnDisk();
3238 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( myHypBranch );
3240 for ( ; it->More(); it->Next() ) {
3241 SALOMEDS::SObject_wrap mySObject = it->Value();
3242 SALOMEDS::SObject_wrap myRefOnHyp;
3243 bool ok = mySObject->ReferencedObject( myRefOnHyp.inout() );
3245 // san - it is impossible to recover applied hypotheses
3246 // using their entries within Load() method,
3247 // for there are no AttributeIORs in the study when Load() is working.
3248 // Hence, it is better to store persistent IDs of hypotheses as references to them
3250 //string myRefOnObject = myRefOnHyp->GetID();
3251 CORBA::Object_var anObject = SObjectToObject( myRefOnHyp );
3252 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3253 int id = myStudyContext->findId( string( objStr.in() ) );
3254 //if ( myRefOnObject.length() > 0 ) {
3255 //aSize[ 0 ] = myRefOnObject.length() + 1;
3256 char hypName[ 30 ], hypId[ 30 ];
3257 sprintf( hypName, "Hyp %d", ++hypNb );
3258 sprintf( hypId, "%d", id );
3259 aSize[ 0 ] = strlen( hypId ) + 1;
3260 aDataset = new HDFdataset( hypName, aGroup, HDF_STRING, aSize, 1 );
3261 aDataset->CreateOnDisk();
3262 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3263 aDataset->WriteOnDisk( hypId );
3264 aDataset->CloseOnDisk();
3268 aGroup->CloseOnDisk();
3271 // write applied algorithms if exist
3272 SALOMEDS::SObject_wrap myAlgoBranch;
3273 found = gotBranch->FindSubObject( GetRefOnAppliedAlgorithmsTag(),
3274 myAlgoBranch.inout() );
3275 if ( found && !shapeRefFound && hasShape) { // remove applied algos
3276 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( myAlgoBranch );
3278 if ( found && (shapeRefFound || !hasShape)) {
3279 aGroup = new HDFgroup( "Applied Algorithms", aTopGroup );
3280 aGroup->CreateOnDisk();
3282 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( myAlgoBranch );
3284 for ( ; it->More(); it->Next() ) {
3285 SALOMEDS::SObject_wrap mySObject = it->Value();
3286 SALOMEDS::SObject_wrap myRefOnAlgo;
3287 bool ok = mySObject->ReferencedObject( myRefOnAlgo.inout() );
3289 // san - it is impossible to recover applied algorithms
3290 // using their entries within Load() method,
3291 // for there are no AttributeIORs in the study when Load() is working.
3292 // Hence, it is better to store persistent IDs of algorithms as references to them
3294 //string myRefOnObject = myRefOnAlgo->GetID();
3295 CORBA::Object_var anObject = SObjectToObject( myRefOnAlgo );
3296 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3297 int id = myStudyContext->findId( string( objStr.in() ) );
3298 //if ( myRefOnObject.length() > 0 ) {
3299 //aSize[ 0 ] = myRefOnObject.length() + 1;
3300 char algoName[ 30 ], algoId[ 30 ];
3301 sprintf( algoName, "Algo %d", ++algoNb );
3302 sprintf( algoId, "%d", id );
3303 aSize[ 0 ] = strlen( algoId ) + 1;
3304 aDataset = new HDFdataset( algoName, aGroup, HDF_STRING, aSize, 1 );
3305 aDataset->CreateOnDisk();
3306 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3307 aDataset->WriteOnDisk( algoId );
3308 aDataset->CloseOnDisk();
3312 aGroup->CloseOnDisk();
3315 // --> submesh objects sub-branches
3317 for ( int i = GetSubMeshOnVertexTag(); i <= GetSubMeshOnCompoundTag(); i++ ) {
3318 SALOMEDS::SObject_wrap mySubmeshBranch;
3319 found = gotBranch->FindSubObject( i, mySubmeshBranch.inout() );
3321 if ( found ) // check if there is shape reference in submeshes
3323 bool hasShapeRef = false;
3324 SALOMEDS::ChildIterator_wrap itSM =
3325 myCurrentStudy->NewChildIterator( mySubmeshBranch );
3326 for ( ; itSM->More(); itSM->Next() ) {
3327 SALOMEDS::SObject_wrap mySubRef, myShape, mySObject = itSM->Value();
3328 if ( mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef.inout() ))
3329 mySubRef->ReferencedObject( myShape.inout() );
3330 if ( !CORBA::is_nil( myShape ) && !CORBA::is_nil( myShape->GetObject() ))
3333 { // remove one submesh
3334 if ( shapeRefFound )
3335 { // unassign hypothesis
3336 SMESH::SMESH_subMesh_var mySubMesh =
3337 SMESH::SMESH_subMesh::_narrow( SObjectToObject( mySObject ));
3338 if ( !mySubMesh->_is_nil() ) {
3339 int shapeID = mySubMesh->GetId();
3340 TopoDS_Shape S = mySMESHDSMesh->IndexToShape( shapeID );
3341 const list<const SMESHDS_Hypothesis*>& hypList =
3342 mySMESHDSMesh->GetHypothesis( S );
3343 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
3344 while ( hyp != hypList.end() ) {
3345 int hypID = (*hyp++)->GetID(); // goto next hyp here because
3346 myLocMesh.RemoveHypothesis( S, hypID ); // hypList changes here
3350 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( mySObject );
3352 } // loop on submeshes of a type
3353 if ( !shapeRefFound || !hasShapeRef ) { // remove the whole submeshes branch
3354 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( mySubmeshBranch );
3357 } // end check if there is shape reference in submeshes
3359 char name_meshgroup[ 30 ];
3360 if ( i == GetSubMeshOnVertexTag() )
3361 strcpy( name_meshgroup, "SubMeshes On Vertex" );
3362 else if ( i == GetSubMeshOnEdgeTag() )
3363 strcpy( name_meshgroup, "SubMeshes On Edge" );
3364 else if ( i == GetSubMeshOnWireTag() )
3365 strcpy( name_meshgroup, "SubMeshes On Wire" );
3366 else if ( i == GetSubMeshOnFaceTag() )
3367 strcpy( name_meshgroup, "SubMeshes On Face" );
3368 else if ( i == GetSubMeshOnShellTag() )
3369 strcpy( name_meshgroup, "SubMeshes On Shell" );
3370 else if ( i == GetSubMeshOnSolidTag() )
3371 strcpy( name_meshgroup, "SubMeshes On Solid" );
3372 else if ( i == GetSubMeshOnCompoundTag() )
3373 strcpy( name_meshgroup, "SubMeshes On Compound" );
3375 // for each type of submeshes create container HDF group
3376 aGroup = new HDFgroup( name_meshgroup, aTopGroup );
3377 aGroup->CreateOnDisk();
3379 // iterator for all submeshes of given type
3380 SALOMEDS::ChildIterator_wrap itSM = myCurrentStudy->NewChildIterator( mySubmeshBranch );
3381 for ( ; itSM->More(); itSM->Next() ) {
3382 SALOMEDS::SObject_wrap mySObject = itSM->Value();
3383 CORBA::Object_var anSubObject = SObjectToObject( mySObject );
3384 if ( !CORBA::is_nil( anSubObject ))
3386 SMESH::SMESH_subMesh_var mySubMesh = SMESH::SMESH_subMesh::_narrow( anSubObject ) ;
3387 CORBA::String_var objStr = GetORB()->object_to_string( anSubObject );
3388 int subid = myStudyContext->findId( string( objStr.in() ) );
3390 // for each mesh open the HDF group basing on its id
3391 char submeshGrpName[ 30 ];
3392 sprintf( submeshGrpName, "SubMesh %d", subid );
3393 aSubGroup = new HDFgroup( submeshGrpName, aGroup );
3394 aSubGroup->CreateOnDisk();
3396 // write reference on a shape, already checked if it exists
3397 SALOMEDS::SObject_wrap mySubRef, myShape;
3398 if ( mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef.inout() ))
3399 mySubRef->ReferencedObject( myShape.inout() );
3400 string myRefOnObject = myShape->GetID();
3401 if ( myRefOnObject.length() > 0 ) {
3402 aSize[ 0 ] = myRefOnObject.length() + 1;
3403 aDataset = new HDFdataset( "Ref on shape", aSubGroup, HDF_STRING, aSize, 1 );
3404 aDataset->CreateOnDisk();
3405 aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3406 aDataset->CloseOnDisk();
3409 // write applied hypotheses if exist
3410 SALOMEDS::SObject_wrap mySubHypBranch;
3411 found = mySObject->FindSubObject( GetRefOnAppliedHypothesisTag(),
3412 mySubHypBranch.inout() );
3414 aSubSubGroup = new HDFgroup( "Applied Hypotheses", aSubGroup );
3415 aSubSubGroup->CreateOnDisk();
3417 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( mySubHypBranch );
3419 for ( ; it->More(); it->Next() ) {
3420 SALOMEDS::SObject_wrap mySubSObject = it->Value();
3421 SALOMEDS::SObject_wrap myRefOnHyp;
3422 bool ok = mySubSObject->ReferencedObject( myRefOnHyp.inout() );
3424 //string myRefOnObject = myRefOnHyp->GetID();
3425 CORBA::Object_var anObject = SObjectToObject( myRefOnHyp );
3426 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3427 int id = myStudyContext->findId( string( objStr.in() ) );
3428 //if ( myRefOnObject.length() > 0 ) {
3429 //aSize[ 0 ] = myRefOnObject.length() + 1;
3430 char hypName[ 30 ], hypId[ 30 ];
3431 sprintf( hypName, "Hyp %d", ++hypNb );
3432 sprintf( hypId, "%d", id );
3433 aSize[ 0 ] = strlen( hypId ) + 1;
3434 aDataset = new HDFdataset( hypName, aSubSubGroup, HDF_STRING, aSize, 1 );
3435 aDataset->CreateOnDisk();
3436 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3437 aDataset->WriteOnDisk( hypId );
3438 aDataset->CloseOnDisk();
3442 aSubSubGroup->CloseOnDisk();
3445 // write applied algorithms if exist
3446 SALOMEDS::SObject_wrap mySubAlgoBranch;
3447 found = mySObject->FindSubObject( GetRefOnAppliedAlgorithmsTag(),
3448 mySubAlgoBranch.inout() );
3450 aSubSubGroup = new HDFgroup( "Applied Algorithms", aSubGroup );
3451 aSubSubGroup->CreateOnDisk();
3453 SALOMEDS::ChildIterator_wrap it =
3454 myCurrentStudy->NewChildIterator( mySubAlgoBranch );
3456 for ( ; it->More(); it->Next() ) {
3457 SALOMEDS::SObject_wrap mySubSObject = it->Value();
3458 SALOMEDS::SObject_wrap myRefOnAlgo;
3459 bool ok = mySubSObject->ReferencedObject( myRefOnAlgo.inout() );
3461 //string myRefOnObject = myRefOnAlgo->GetID();
3462 CORBA::Object_var anObject = SObjectToObject( myRefOnAlgo );
3463 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3464 int id = myStudyContext->findId( string( objStr.in() ) );
3465 //if ( myRefOnObject.length() > 0 ) {
3466 //aSize[ 0 ] = myRefOnObject.length() + 1;
3467 char algoName[ 30 ], algoId[ 30 ];
3468 sprintf( algoName, "Algo %d", ++algoNb );
3469 sprintf( algoId, "%d", id );
3470 aSize[ 0 ] = strlen( algoId ) + 1;
3471 aDataset = new HDFdataset( algoName, aSubSubGroup, HDF_STRING, aSize, 1 );
3472 aDataset->CreateOnDisk();
3473 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3474 aDataset->WriteOnDisk( algoId );
3475 aDataset->CloseOnDisk();
3479 aSubSubGroup->CloseOnDisk();
3481 // close submesh HDF group
3482 aSubGroup->CloseOnDisk();
3485 // close container of submeshes by type HDF group
3486 aGroup->CloseOnDisk();
3489 // All sub-meshes will be stored in MED file
3490 // .. will NOT (PAL 12992)
3491 //if ( shapeRefFound )
3492 //myWriter.AddAllSubMeshes();
3494 // store submesh order if any
3495 const TListOfListOfInt& theOrderIds = myLocMesh.GetMeshOrder();
3496 if ( theOrderIds.size() ) {
3497 char order_list[ 30 ];
3498 strcpy( order_list, "Mesh Order" );
3499 // count number of submesh ids
3501 TListOfListOfInt::const_iterator idIt = theOrderIds.begin();
3502 for ( ; idIt != theOrderIds.end(); idIt++ )
3503 nbIDs += (*idIt).size();
3504 // number of values = number of IDs +
3505 // number of lists (for separators) - 1
3506 int* smIDs = new int [ nbIDs + theOrderIds.size() - 1 ];
3507 idIt = theOrderIds.begin();
3508 for ( int i = 0; idIt != theOrderIds.end(); idIt++ ) {
3509 const TListOfInt& idList = *idIt;
3510 if (idIt != theOrderIds.begin()) // not first list
3511 smIDs[ i++ ] = -1/* *idList.size()*/; // separator between lists
3512 // dump submesh ids from current list
3513 TListOfInt::const_iterator id_smId = idList.begin();
3514 for( ; id_smId != idList.end(); id_smId++ )
3515 smIDs[ i++ ] = *id_smId;
3518 aSize[ 0 ] = nbIDs + theOrderIds.size() - 1;
3520 aDataset = new HDFdataset( order_list, aTopGroup, HDF_INT32, aSize, 1 );
3521 aDataset->CreateOnDisk();
3522 aDataset->WriteOnDisk( smIDs );
3523 aDataset->CloseOnDisk();
3528 // groups root sub-branch
3529 SALOMEDS::SObject_wrap myGroupsBranch;
3530 for ( int i = GetNodeGroupsTag(); i <= GetBallElementsGroupsTag(); i++ ) {
3531 found = gotBranch->FindSubObject( i, myGroupsBranch.inout() );
3533 char name_group[ 30 ];
3534 if ( i == GetNodeGroupsTag() )
3535 strcpy( name_group, "Groups of Nodes" );
3536 else if ( i == GetEdgeGroupsTag() )
3537 strcpy( name_group, "Groups of Edges" );
3538 else if ( i == GetFaceGroupsTag() )
3539 strcpy( name_group, "Groups of Faces" );
3540 else if ( i == GetVolumeGroupsTag() )
3541 strcpy( name_group, "Groups of Volumes" );
3542 else if ( i == Get0DElementsGroupsTag() )
3543 strcpy( name_group, "Groups of 0D Elements" );
3544 else if ( i == GetBallElementsGroupsTag() )
3545 strcpy( name_group, "Groups of Balls" );
3547 aGroup = new HDFgroup( name_group, aTopGroup );
3548 aGroup->CreateOnDisk();
3550 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( myGroupsBranch );
3551 for ( ; it->More(); it->Next() ) {
3552 SALOMEDS::SObject_wrap mySObject = it->Value();
3553 CORBA::Object_var aSubObject = SObjectToObject( mySObject );
3554 if ( !CORBA::is_nil( aSubObject ) ) {
3555 SMESH_GroupBase_i* myGroupImpl =
3556 dynamic_cast<SMESH_GroupBase_i*>( GetServant( aSubObject ).in() );
3559 SMESHDS_GroupBase* aGrpBaseDS = myGroupImpl->GetGroupDS();
3563 CORBA::String_var objStr = GetORB()->object_to_string( aSubObject );
3564 int anId = myStudyContext->findId( string( objStr.in() ) );
3566 // For each group, create a dataset named "Group <group_persistent_id>"
3567 // and store the group's user name into it
3568 const char* grpName = aGrpBaseDS->GetStoreName();
3569 char* aUserName = myGroupImpl->GetName();
3570 aSize[ 0 ] = strlen( aUserName ) + 1;
3572 aDataset = new HDFdataset( grpName, aGroup, HDF_STRING, aSize, 1 );
3573 aDataset->CreateOnDisk();
3574 aDataset->WriteOnDisk( aUserName );
3575 aDataset->CloseOnDisk();
3578 // For each group, create a dataset named "Group <group_persistent_id> Color"
3579 // and store the group's color into it
3580 char grpColorName[ 30 ];
3581 sprintf( grpColorName, "ColorGroup %d", anId );
3582 SALOMEDS::Color aColor = myGroupImpl->GetColor();
3584 anRGB[ 0 ] = aColor.R;
3585 anRGB[ 1 ] = aColor.G;
3586 anRGB[ 2 ] = aColor.B;
3588 aDataset = new HDFdataset( grpColorName, aGroup, HDF_FLOAT64, aSize, 1 );
3589 aDataset->CreateOnDisk();
3590 aDataset->WriteOnDisk( anRGB );
3591 aDataset->CloseOnDisk();
3593 // Pass SMESHDS_Group to MED writer
3594 SMESHDS_Group* aGrpDS = dynamic_cast<SMESHDS_Group*>( aGrpBaseDS );
3596 myWriter.AddGroup( aGrpDS );
3598 // write reference on a shape if exists
3599 SMESHDS_GroupOnGeom* aGeomGrp =
3600 dynamic_cast<SMESHDS_GroupOnGeom*>( aGrpBaseDS );
3602 SALOMEDS::SObject_wrap mySubRef, myShape;
3603 if (mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef.inout() ) &&
3604 mySubRef->ReferencedObject( myShape.inout() ) &&
3605 !CORBA::is_nil( myShape->GetObject() ))
3607 string myRefOnObject = myShape->GetID();
3608 if ( myRefOnObject.length() > 0 ) {
3609 char aRefName[ 30 ];
3610 sprintf( aRefName, "Ref on shape %d", anId);
3611 aSize[ 0 ] = myRefOnObject.length() + 1;
3612 aDataset = new HDFdataset(aRefName, aGroup, HDF_STRING, aSize, 1);
3613 aDataset->CreateOnDisk();
3614 aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3615 aDataset->CloseOnDisk();
3618 else // shape ref is invalid:
3620 // save a group on geometry as ordinary group
3621 myWriter.AddGroup( aGeomGrp );
3624 else if ( SMESH_GroupOnFilter_i* aFilterGrp_i =
3625 dynamic_cast<SMESH_GroupOnFilter_i*>( myGroupImpl ))
3627 std::string str = aFilterGrp_i->FilterToString();
3628 std::string hdfGrpName = "Filter " + SMESH_Comment(anId);
3629 aSize[ 0 ] = str.length() + 1;
3630 aDataset = new HDFdataset( hdfGrpName.c_str(), aGroup, HDF_STRING, aSize, 1);
3631 aDataset->CreateOnDisk();
3632 aDataset->WriteOnDisk( ( char* )( str.c_str() ) );
3633 aDataset->CloseOnDisk();
3637 aGroup->CloseOnDisk();
3641 if ( strcmp( strHasData.c_str(), "1" ) == 0 )
3643 // Flush current mesh information into MED file
3646 // save info on nb of elements
3647 SMESH_PreMeshInfo::SaveToFile( myImpl, id, aFile );
3649 // maybe a shape was deleted in the study
3650 if ( !shapeRefFound && !mySMESHDSMesh->ShapeToMesh().IsNull() && hasShape) {
3651 TopoDS_Shape nullShape;
3652 myLocMesh.ShapeToMesh( nullShape ); // remove shape referring data
3655 if ( !mySMESHDSMesh->SubMeshes().empty() )
3659 aGroup = new HDFgroup( "Submeshes", aTopGroup );
3660 aGroup->CreateOnDisk();
3662 // each element belongs to one or none submesh,
3663 // so for each node/element, we store a submesh ID
3665 // Make maps of submesh IDs of elements sorted by element IDs
3666 // typedef int TElemID;
3667 // typedef int TSubMID;
3668 // map< TElemID, TSubMID > eId2smId, nId2smId;
3669 const map<int,SMESHDS_SubMesh*>& aSubMeshes = mySMESHDSMesh->SubMeshes();
3670 map<int,SMESHDS_SubMesh*>::const_iterator itSubM ( aSubMeshes.begin() );
3671 // SMDS_NodeIteratorPtr itNode;
3672 // SMDS_ElemIteratorPtr itElem;
3673 // for ( itSubM = aSubMeshes.begin(); itSubM != aSubMeshes.end() ; itSubM++ )
3675 // TSubMID aSubMeID = itSubM->first;
3676 // SMESHDS_SubMesh* aSubMesh = itSubM->second;
3677 // if ( aSubMesh->IsComplexSubmesh() )
3678 // continue; // sub-mesh containing other sub-meshes
3680 // for ( itNode = aSubMesh->GetNodes(); itNode->more(); ++hint)
3681 // nId2smId.insert( nId2smId.back(), make_pair( itNode->next()->GetID(), aSubMeID ));
3683 // for ( itElem = aSubMesh->GetElements(); itElem->more(); ++hint)
3684 // hint = eId2smId.insert( eId2smId.back(), make_pair( itElem->next()->GetID(), aSubMeID ));
3687 // // Care of elements that are not on submeshes
3688 // if ( mySMESHDSMesh->NbNodes() != nId2smId.size() ) {
3689 // for ( itNode = mySMESHDSMesh->nodesIterator(); itNode->more(); )
3690 // /* --- stl_map.h says : */
3691 // /* A %map relies on unique keys and thus a %pair is only inserted if its */
3692 // /* first element (the key) is not already present in the %map. */
3693 // nId2smId.insert( make_pair( itNode->next()->GetID(), 0 ));
3695 // int nbElems = mySMESHDSMesh->GetMeshInfo().NbElements();
3696 // if ( nbElems != eId2smId.size() ) {
3697 // for ( itElem = mySMESHDSMesh->elementsIterator(); itElem->more(); )
3698 // eId2smId.insert( make_pair( itElem->next()->GetID(), 0 ));
3701 // Store submesh IDs
3702 for ( int isNode = 0; isNode < 2; ++isNode )
3704 // map< TElemID, TSubMID >& id2smId = isNode ? nId2smId : eId2smId;
3705 // if ( id2smId.empty() ) continue;
3706 // map< TElemID, TSubMID >::const_iterator id_smId = id2smId.begin();
3707 // // make and fill array of submesh IDs
3708 // int* smIDs = new int [ id2smId.size() ];
3709 // for ( int i = 0; id_smId != id2smId.end(); ++id_smId, ++i )
3710 // smIDs[ i ] = id_smId->second;
3711 SMDS_ElemIteratorPtr eIt =
3712 mySMESHDSMesh->elementsIterator( isNode ? SMDSAbs_Node : SMDSAbs_All );
3713 int nbElems = isNode ? mySMESHDSMesh->NbNodes() : mySMESHDSMesh->GetMeshInfo().NbElements();
3716 std::vector<int> smIDs; smIDs.reserve( nbElems );
3717 while ( eIt->more() )
3718 if ( const SMDS_MeshElement* e = eIt->next())
3719 smIDs.push_back( e->getshapeId() );
3721 aSize[ 0 ] = nbElems;
3722 string aDSName( isNode ? "Node Submeshes" : "Element Submeshes");
3723 aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_INT32, aSize, 1 );
3724 aDataset->CreateOnDisk();
3725 aDataset->WriteOnDisk( & smIDs[0] );
3726 aDataset->CloseOnDisk();
3729 aGroup->CloseOnDisk();
3731 // Store node positions on sub-shapes (SMDS_Position):
3732 // ----------------------------------------------------
3734 aGroup = new HDFgroup( "Node Positions", aTopGroup );
3735 aGroup->CreateOnDisk();
3737 // in aGroup, create 5 datasets to contain:
3738 // "Nodes on Edges" - ID of node on edge
3739 // "Edge positions" - U parameter on node on edge
3740 // "Nodes on Faces" - ID of node on face
3741 // "Face U positions" - U parameter of node on face
3742 // "Face V positions" - V parameter of node on face
3744 // Find out nb of nodes on edges and faces
3745 // Collect corresponing sub-meshes
3746 int nbEdgeNodes = 0, nbFaceNodes = 0;
3747 list<SMESHDS_SubMesh*> aEdgeSM, aFaceSM;
3748 // loop on SMESHDS_SubMesh'es
3749 for ( itSubM = aSubMeshes.begin(); itSubM != aSubMeshes.end() ; itSubM++ )
3751 SMESHDS_SubMesh* aSubMesh = (*itSubM).second;
3752 if ( aSubMesh->IsComplexSubmesh() )
3753 continue; // submesh containing other submeshs
3754 int nbNodes = aSubMesh->NbNodes();
3755 if ( nbNodes == 0 ) continue;
3757 int aShapeID = (*itSubM).first;
3758 if ( aShapeID < 1 || aShapeID > mySMESHDSMesh->MaxShapeIndex() )
3760 int aShapeType = mySMESHDSMesh->IndexToShape( aShapeID ).ShapeType();
3761 // write only SMDS_FacePosition and SMDS_EdgePosition
3762 switch ( aShapeType ) {
3764 nbFaceNodes += nbNodes;
3765 aFaceSM.push_back( aSubMesh );
3768 nbEdgeNodes += nbNodes;
3769 aEdgeSM.push_back( aSubMesh );
3775 // Treat positions on edges or faces
3776 for ( int onFace = 0; onFace < 2; onFace++ )
3778 // Create arrays to store in datasets
3779 int iNode = 0, nbNodes = ( onFace ? nbFaceNodes : nbEdgeNodes );
3780 if (!nbNodes) continue;
3781 int* aNodeIDs = new int [ nbNodes ];
3782 double* aUPos = new double [ nbNodes ];
3783 double* aVPos = ( onFace ? new double[ nbNodes ] : 0 );
3786 // loop on sub-meshes
3787 list<SMESHDS_SubMesh*> * pListSM = ( onFace ? &aFaceSM : &aEdgeSM );
3788 list<SMESHDS_SubMesh*>::iterator itSM = pListSM->begin();
3789 for ( ; itSM != pListSM->end(); itSM++ )
3791 SMESHDS_SubMesh* aSubMesh = (*itSM);
3793 SMDS_NodeIteratorPtr itNode = aSubMesh->GetNodes();
3794 // loop on nodes in aSubMesh
3795 while ( itNode->more() )
3798 const SMDS_MeshNode* node = itNode->next();
3799 aNodeIDs [ iNode ] = node->GetID();
3802 const SMDS_PositionPtr pos = node->GetPosition();
3803 if ( onFace ) { // on FACE
3804 const SMDS_FacePosition* fPos =
3805 dynamic_cast<const SMDS_FacePosition*>( pos );
3807 aUPos[ iNode ] = fPos->GetUParameter();
3808 aVPos[ iNode ] = fPos->GetVParameter();
3815 const SMDS_EdgePosition* ePos =
3816 dynamic_cast<const SMDS_EdgePosition*>( pos );
3818 aUPos[ iNode ] = ePos->GetUParameter();
3824 } // loop on nodes in aSubMesh
3825 } // loop on sub-meshes
3830 aSize[ 0 ] = nbNodes;
3832 string aDSName( onFace ? "Nodes on Faces" : "Nodes on Edges");
3833 aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_INT32, aSize, 1 );
3834 aDataset->CreateOnDisk();
3835 aDataset->WriteOnDisk( aNodeIDs );
3836 aDataset->CloseOnDisk();
3839 aDSName = ( onFace ? "Face U positions" : "Edge positions");
3840 aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_FLOAT64, aSize, 1);
3841 aDataset->CreateOnDisk();
3842 aDataset->WriteOnDisk( aUPos );
3843 aDataset->CloseOnDisk();
3846 aDataset = new HDFdataset( "Face V positions", aGroup, HDF_FLOAT64, aSize, 1);
3847 aDataset->CreateOnDisk();
3848 aDataset->WriteOnDisk( aVPos );
3849 aDataset->CloseOnDisk();
3854 if ( aVPos ) delete [] aVPos;
3856 } // treat positions on edges or faces
3858 // close "Node Positions" group
3859 aGroup->CloseOnDisk();
3861 } // if ( there are submeshes in SMESHDS_Mesh )
3864 // close mesh HDF group
3865 aTopGroup->CloseOnDisk();
3873 aFile->CloseOnDisk();
3876 // Convert temporary files to stream
3877 aStreamFile = SALOMEDS_Tool::PutFilesToStream( tmpDir.ToCString(), aFileSeq.in(), isMultiFile );
3879 // Remove temporary files and directory
3881 SALOMEDS_Tool::RemoveTemporaryFiles( tmpDir.ToCString(), aFileSeq.in(), true );
3883 INFOS( "SMESH_Gen_i::Save() completed" );
3884 return aStreamFile._retn();
3887 //=============================================================================
3889 * SMESH_Gen_i::SaveASCII
3891 * Save SMESH module's data in ASCII format
3893 //=============================================================================
3895 SALOMEDS::TMPFile* SMESH_Gen_i::SaveASCII( SALOMEDS::SComponent_ptr theComponent,
3897 bool isMultiFile ) {
3898 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::SaveASCII" );
3899 SALOMEDS::TMPFile_var aStreamFile = Save( theComponent, theURL, isMultiFile );
3900 return aStreamFile._retn();
3902 //after usual saving needs to encipher binary to text string
3903 //Any binary symbol will be represent as "|xx" () hexadecimal format number
3904 int size = aStreamFile.in().length();
3905 _CORBA_Octet* buffer = new _CORBA_Octet[size*3+1];
3906 for ( int i = 0; i < size; i++ )
3907 sprintf( (char*)&(buffer[i*3]), "|%02x", aStreamFile[i] );
3909 buffer[size * 3] = '\0';
3911 SALOMEDS::TMPFile_var anAsciiStreamFile = new SALOMEDS::TMPFile(size*3, size*3, buffer, 1);
3913 return anAsciiStreamFile._retn();
3916 //=============================================================================
3918 * SMESH_Gen_i::loadGeomData
3920 * Load GEOM module data
3922 //=============================================================================
3924 void SMESH_Gen_i::loadGeomData( SALOMEDS::SComponent_ptr theCompRoot )
3926 if ( theCompRoot->_is_nil() )
3929 SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow( theCompRoot->GetStudy() );
3930 if ( aStudy->_is_nil() )
3933 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
3934 aStudyBuilder->LoadWith( theCompRoot, GetGeomEngine() );
3937 //=============================================================================
3941 * Load SMESH module's data
3943 //=============================================================================
3945 bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
3946 const SALOMEDS::TMPFile& theStream,
3950 INFOS( "SMESH_Gen_i::Load" );
3952 if ( myCurrentStudy->_is_nil() ||
3953 theComponent->GetStudy()->StudyId() != myCurrentStudy->StudyId() )
3954 SetCurrentStudy( theComponent->GetStudy() );
3956 /* if( !theComponent->_is_nil() )
3958 //SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow( theComponent->GetStudy() );
3959 if( !myCurrentStudy->FindComponent( "GEOM" )->_is_nil() )
3960 loadGeomData( myCurrentStudy->FindComponent( "GEOM" ) );
3963 StudyContext* myStudyContext = GetCurrentStudyContext();
3965 // Get temporary files location
3966 TCollection_AsciiString tmpDir =
3967 ( char* )( isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir().c_str() );
3969 INFOS( "THE URL++++++++++++++" );
3971 INFOS( "THE TMP PATH+++++++++" );
3974 // Convert the stream into sequence of files to process
3975 SALOMEDS::ListOfFileNames_var aFileSeq = SALOMEDS_Tool::PutStreamToFiles( theStream,
3978 TCollection_AsciiString aStudyName( "" );
3979 if ( isMultiFile ) {
3980 CORBA::String_var url = myCurrentStudy->URL();
3981 aStudyName = (char*)SALOMEDS_Tool::GetNameFromPath( url.in() ).c_str();
3983 // Set names of temporary files
3984 TCollection_AsciiString filename = tmpDir + aStudyName + "_SMESH.hdf";
3985 TCollection_AsciiString meshfile = tmpDir + aStudyName + "_SMESH_Mesh.med";
3989 HDFdataset* aDataset;
3990 HDFgroup* aTopGroup;
3992 HDFgroup* aSubGroup;
3993 HDFgroup* aSubSubGroup;
3996 // ---> open HDF file
3997 aFile = new HDFfile( (char*) filename.ToCString() );
3999 aFile->OpenOnDisk( HDF_RDONLY );
4001 catch ( HDFexception ) {
4002 INFOS( "Load(): " << filename << " not found!" );
4006 TPythonDump pd; // prevent dump during loading
4008 // DriverMED_R_SMESHDS_Mesh myReader;
4009 // myReader.SetFile( meshfile.ToCString() );
4011 // For PAL13473 ("Repetitive mesh") implementation.
4012 // New dependencies between SMESH objects are established:
4013 // now hypotheses can refer to meshes, shapes and other hypotheses.
4014 // To keep data consistent, the following order of data restoration
4016 // 1. Create hypotheses
4017 // 2. Create all meshes
4018 // 3. Load hypotheses' data
4021 list< pair< SMESH_Hypothesis_i*, string > > hypDataList;
4022 list< pair< SMESH_Mesh_i*, HDFgroup* > > meshGroupList;
4024 // get total number of top-level groups
4025 int aNbGroups = aFile->nInternalObjects();
4026 if ( aNbGroups > 0 ) {
4027 // --> in first turn we should read&create hypotheses
4028 if ( aFile->ExistInternalObject( "Hypotheses" ) ) {
4029 // open hypotheses root HDF group
4030 aTopGroup = new HDFgroup( "Hypotheses", aFile );
4031 aTopGroup->OpenOnDisk();
4033 // get number of hypotheses
4034 int aNbObjects = aTopGroup->nInternalObjects();
4035 for ( int j = 0; j < aNbObjects; j++ ) {
4036 // try to identify hypothesis
4037 char hypGrpName[ HDF_NAME_MAX_LEN+1 ];
4038 aTopGroup->InternalObjectIndentify( j, hypGrpName );
4040 if ( string( hypGrpName ).substr( 0, 10 ) == string( "Hypothesis" ) ) {
4041 // open hypothesis group
4042 aGroup = new HDFgroup( hypGrpName, aTopGroup );
4043 aGroup->OpenOnDisk();
4045 // --> get hypothesis id
4046 int id = atoi( string( hypGrpName ).substr( 10 ).c_str() );
4051 // get number of datasets
4052 int aNbSubObjects = aGroup->nInternalObjects();
4053 for ( int k = 0; k < aNbSubObjects; k++ ) {
4055 char name_of_subgroup[ HDF_NAME_MAX_LEN+1 ];
4056 aGroup->InternalObjectIndentify( k, name_of_subgroup );
4057 // --> get hypothesis name
4058 if ( strcmp( name_of_subgroup, "Name" ) == 0 ) {
4059 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4060 aDataset->OpenOnDisk();
4061 size = aDataset->GetSize();
4062 char* hypname_str = new char[ size ];
4063 aDataset->ReadFromDisk( hypname_str );
4064 hypname = string( hypname_str );
4065 delete [] hypname_str;
4066 aDataset->CloseOnDisk();
4068 // --> get hypothesis plugin library name
4069 if ( strcmp( name_of_subgroup, "LibName" ) == 0 ) {
4070 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4071 aDataset->OpenOnDisk();
4072 size = aDataset->GetSize();
4073 char* libname_str = new char[ size ];
4074 aDataset->ReadFromDisk( libname_str );
4075 if(MYDEBUG) SCRUTE( libname_str );
4076 libname = string( libname_str );
4077 delete [] libname_str;
4078 aDataset->CloseOnDisk();
4080 // --> get hypothesis data
4081 if ( strcmp( name_of_subgroup, "Data" ) == 0 ) {
4082 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4083 aDataset->OpenOnDisk();
4084 size = aDataset->GetSize();
4085 char* hypdata_str = new char[ size ];
4086 aDataset->ReadFromDisk( hypdata_str );
4087 hypdata = string( hypdata_str );
4088 delete [] hypdata_str;
4089 aDataset->CloseOnDisk();
4092 // close hypothesis HDF group
4093 aGroup->CloseOnDisk();
4095 // --> restore hypothesis from data
4096 if ( id > 0 && !hypname.empty()/* && !hypdata.empty()*/ ) { // VSR : persistent data can be empty
4097 if(MYDEBUG) MESSAGE("VSR - load hypothesis : id = " << id <<
4098 ", name = " << hypname.c_str() << ", persistent string = " << hypdata.c_str());
4099 SMESH::SMESH_Hypothesis_var myHyp;
4101 try { // protect persistence mechanism against exceptions
4102 myHyp = this->createHypothesis( hypname.c_str(), libname.c_str() );
4105 INFOS( "Exception during hypothesis creation" );
4108 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
4110 // myImpl->LoadFrom( hypdata.c_str() );
4111 hypDataList.push_back( make_pair( myImpl, hypdata ));
4112 CORBA::String_var iorString = GetORB()->object_to_string( myHyp );
4113 int newId = myStudyContext->findId( iorString.in() );
4114 myStudyContext->mapOldToNew( id, newId );
4117 if(MYDEBUG) MESSAGE( "VSR - SMESH_Gen::Load - can't get servant" );
4121 // close hypotheses root HDF group
4122 aTopGroup->CloseOnDisk();
4126 // --> then we should read&create algorithms
4127 if ( aFile->ExistInternalObject( "Algorithms" ) ) {
4128 // open algorithms root HDF group
4129 aTopGroup = new HDFgroup( "Algorithms", aFile );
4130 aTopGroup->OpenOnDisk();
4132 // get number of algorithms
4133 int aNbObjects = aTopGroup->nInternalObjects();
4134 for ( int j = 0; j < aNbObjects; j++ ) {
4135 // try to identify algorithm
4136 char hypGrpName[ HDF_NAME_MAX_LEN+1 ];
4137 aTopGroup->InternalObjectIndentify( j, hypGrpName );
4139 if ( string( hypGrpName ).substr( 0, 9 ) == string( "Algorithm" ) ) {
4140 // open algorithm group
4141 aGroup = new HDFgroup( hypGrpName, aTopGroup );
4142 aGroup->OpenOnDisk();
4144 // --> get algorithm id
4145 int id = atoi( string( hypGrpName ).substr( 9 ).c_str() );
4150 // get number of datasets
4151 int aNbSubObjects = aGroup->nInternalObjects();
4152 for ( int k = 0; k < aNbSubObjects; k++ ) {
4154 char name_of_subgroup[ HDF_NAME_MAX_LEN+1 ];
4155 aGroup->InternalObjectIndentify( k, name_of_subgroup );
4156 // --> get algorithm name
4157 if ( strcmp( name_of_subgroup, "Name" ) == 0 ) {
4158 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4159 aDataset->OpenOnDisk();
4160 size = aDataset->GetSize();
4161 char* hypname_str = new char[ size ];
4162 aDataset->ReadFromDisk( hypname_str );
4163 hypname = string( hypname_str );
4164 delete [] hypname_str;
4165 aDataset->CloseOnDisk();
4167 // --> get algorithm plugin library name
4168 if ( strcmp( name_of_subgroup, "LibName" ) == 0 ) {
4169 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4170 aDataset->OpenOnDisk();
4171 size = aDataset->GetSize();
4172 char* libname_str = new char[ size ];
4173 aDataset->ReadFromDisk( libname_str );
4174 if(MYDEBUG) SCRUTE( libname_str );
4175 libname = string( libname_str );
4176 delete [] libname_str;
4177 aDataset->CloseOnDisk();
4179 // --> get algorithm data
4180 if ( strcmp( name_of_subgroup, "Data" ) == 0 ) {
4181 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4182 aDataset->OpenOnDisk();
4183 size = aDataset->GetSize();
4184 char* hypdata_str = new char[ size ];
4185 aDataset->ReadFromDisk( hypdata_str );
4186 if(MYDEBUG) SCRUTE( hypdata_str );
4187 hypdata = string( hypdata_str );
4188 delete [] hypdata_str;
4189 aDataset->CloseOnDisk();
4192 // close algorithm HDF group
4193 aGroup->CloseOnDisk();
4195 // --> restore algorithm from data
4196 if ( id > 0 && !hypname.empty()/* && !hypdata.empty()*/ ) { // VSR : persistent data can be empty
4197 if(MYDEBUG) MESSAGE("VSR - load algo : id = " << id <<
4198 ", name = " << hypname.c_str() << ", persistent string = " << hypdata.c_str());
4199 SMESH::SMESH_Hypothesis_var myHyp;
4201 try { // protect persistence mechanism against exceptions
4202 myHyp = this->createHypothesis( hypname.c_str(), libname.c_str() );
4205 INFOS( "Exception during hypothesis creation" );
4208 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
4210 //myImpl->LoadFrom( hypdata.c_str() );
4211 hypDataList.push_back( make_pair( myImpl, hypdata ));
4212 CORBA::String_var iorString = GetORB()->object_to_string( myHyp );
4213 int newId = myStudyContext->findId( iorString.in() );
4214 myStudyContext->mapOldToNew( id, newId );
4217 if(MYDEBUG) MESSAGE( "VSR - SMESH_Gen::Load - can't get servant" );
4221 // close algorithms root HDF group
4222 aTopGroup->CloseOnDisk();
4226 // --> the rest groups should be meshes
4227 for ( int i = 0; i < aNbGroups; i++ ) {
4228 // identify next group
4229 char meshName[ HDF_NAME_MAX_LEN+1 ];
4230 aFile->InternalObjectIndentify( i, meshName );
4232 if ( string( meshName ).substr( 0, 4 ) == string( "Mesh" ) ) {
4234 int id = atoi( string( meshName ).substr( 4 ).c_str() );
4238 // open mesh HDF group
4239 aTopGroup = new HDFgroup( meshName, aFile );
4240 aTopGroup->OpenOnDisk();
4242 // get number of child HDF objects
4243 int aNbObjects = aTopGroup->nInternalObjects();
4244 if ( aNbObjects > 0 ) {
4246 if(MYDEBUG) MESSAGE( "VSR - load mesh : id = " << id );
4247 SMESH::SMESH_Mesh_var myNewMesh = this->createMesh();
4248 SMESH_Mesh_i* myNewMeshImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( myNewMesh ).in() );
4249 if ( !myNewMeshImpl )
4251 meshGroupList.push_back( make_pair( myNewMeshImpl, aTopGroup ));
4253 CORBA::String_var iorString = GetORB()->object_to_string( myNewMesh );
4254 int newId = myStudyContext->findId( iorString.in() );
4255 myStudyContext->mapOldToNew( id, newId );
4258 // try to read and set auto color flag
4259 char aMeshAutoColorName[ 30 ];
4260 sprintf( aMeshAutoColorName, "AutoColorMesh %d", id);
4261 if( aTopGroup->ExistInternalObject( aMeshAutoColorName ) )
4263 aDataset = new HDFdataset( aMeshAutoColorName, aTopGroup );
4264 aDataset->OpenOnDisk();
4265 size = aDataset->GetSize();
4266 int* anAutoColor = new int[ size ];
4267 aDataset->ReadFromDisk( anAutoColor );
4268 aDataset->CloseOnDisk();
4269 myNewMeshImpl->GetImpl().SetAutoColor( (bool)anAutoColor[0] );
4270 delete [] anAutoColor;
4273 // try to read and set reference to shape
4274 GEOM::GEOM_Object_var aShapeObject;
4275 if ( aTopGroup->ExistInternalObject( "Ref on shape" ) ) {
4276 // load mesh "Ref on shape" - it's an entry to SObject
4277 aDataset = new HDFdataset( "Ref on shape", aTopGroup );
4278 aDataset->OpenOnDisk();
4279 size = aDataset->GetSize();
4280 char* refFromFile = new char[ size ];
4281 aDataset->ReadFromDisk( refFromFile );
4282 aDataset->CloseOnDisk();
4283 if ( strlen( refFromFile ) > 0 ) {
4284 SALOMEDS::SObject_wrap shapeSO = myCurrentStudy->FindObjectID( refFromFile );
4286 // Make sure GEOM data are loaded first
4287 //loadGeomData( shapeSO->GetFatherComponent() );
4289 CORBA::Object_var shapeObject = SObjectToObject( shapeSO );
4290 if ( !CORBA::is_nil( shapeObject ) ) {
4291 aShapeObject = GEOM::GEOM_Object::_narrow( shapeObject );
4292 if ( !aShapeObject->_is_nil() )
4293 myNewMeshImpl->SetShape( aShapeObject );
4296 delete [] refFromFile;
4299 // issue 20918. Restore Persistent Id of SMESHDS_Mesh
4300 if( aTopGroup->ExistInternalObject( "meshPersistentId" ) )
4302 aDataset = new HDFdataset( "meshPersistentId", aTopGroup );
4303 aDataset->OpenOnDisk();
4304 size = aDataset->GetSize();
4305 int* meshPersistentId = new int[ size ];
4306 aDataset->ReadFromDisk( meshPersistentId );
4307 aDataset->CloseOnDisk();
4308 myNewMeshImpl->GetImpl().GetMeshDS()->SetPersistentId( *meshPersistentId );
4309 delete [] meshPersistentId;
4315 // As all object that can be referred by hypothesis are created,
4316 // we can restore hypothesis data
4318 list< pair< SMESH_Hypothesis_i*, string > >::iterator hyp_data;
4319 for ( hyp_data = hypDataList.begin(); hyp_data != hypDataList.end(); ++hyp_data )
4321 SMESH_Hypothesis_i* hyp = hyp_data->first;
4322 string & data = hyp_data->second;
4323 hyp->LoadFrom( data.c_str() );
4326 // Restore the rest mesh data
4328 list< pair< SMESH_Mesh_i*, HDFgroup* > >::iterator meshi_group;
4329 for ( meshi_group = meshGroupList.begin(); meshi_group != meshGroupList.end(); ++meshi_group )
4331 aTopGroup = meshi_group->second;
4332 SMESH_Mesh_i* myNewMeshImpl = meshi_group->first;
4333 //::SMESH_Mesh& myLocMesh = myNewMeshImpl->GetImpl();
4334 //SMESHDS_Mesh* mySMESHDSMesh = myLocMesh.GetMeshDS();
4336 GEOM::GEOM_Object_var aShapeObject = myNewMeshImpl->GetShapeToMesh();
4337 bool hasData = false;
4340 CORBA::String_var iorString = GetORB()->object_to_string( myNewMeshImpl->_this() );
4341 int newId = myStudyContext->findId( iorString.in() );
4342 int id = myStudyContext->getOldId( newId );
4344 // try to find mesh data dataset
4345 if ( aTopGroup->ExistInternalObject( "Has data" ) ) {
4346 // load mesh "has data" flag
4347 aDataset = new HDFdataset( "Has data", aTopGroup );
4348 aDataset->OpenOnDisk();
4349 size = aDataset->GetSize();
4350 char* strHasData = new char[ size ];
4351 aDataset->ReadFromDisk( strHasData );
4352 aDataset->CloseOnDisk();
4353 if ( strcmp( strHasData, "1") == 0 ) {
4354 // read mesh data from MED file
4355 // myReader.SetMesh( mySMESHDSMesh );
4356 // myReader.SetMeshId( id );
4357 // myReader.Perform();
4360 delete [] strHasData;
4363 // Try to get applied ALGORITHMS (mesh is not cleared by algo addition because
4364 // nodes and elements are not yet put into sub-meshes)
4365 if ( aTopGroup->ExistInternalObject( "Applied Algorithms" ) ) {
4366 aGroup = new HDFgroup( "Applied Algorithms", aTopGroup );
4367 aGroup->OpenOnDisk();
4368 // get number of applied algorithms
4369 int aNbSubObjects = aGroup->nInternalObjects();
4370 if(MYDEBUG) MESSAGE( "VSR - number of applied algos " << aNbSubObjects );
4371 for ( int j = 0; j < aNbSubObjects; j++ ) {
4372 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4373 aGroup->InternalObjectIndentify( j, name_dataset );
4374 // check if it is an algorithm
4375 if ( string( name_dataset ).substr( 0, 4 ) == string( "Algo" ) ) {
4376 aDataset = new HDFdataset( name_dataset, aGroup );
4377 aDataset->OpenOnDisk();
4378 size = aDataset->GetSize();
4379 char* refFromFile = new char[ size ];
4380 aDataset->ReadFromDisk( refFromFile );
4381 aDataset->CloseOnDisk();
4382 // san - it is impossible to recover applied algorithms using their entries within Load() method
4383 //SALOMEDS::SObject_wrap hypSO = myCurrentStudy->FindObjectID( refFromFile );
4384 //CORBA::Object_var hypObject = SObjectToObject( hypSO );
4385 int id = atoi( refFromFile );
4386 delete [] refFromFile;
4387 string anIOR = myStudyContext->getIORbyOldId( id );
4388 if ( !anIOR.empty() ) {
4389 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
4390 if ( !CORBA::is_nil( hypObject ) ) {
4391 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
4392 if ( !anHyp->_is_nil() && (!aShapeObject->_is_nil()
4393 || !myNewMeshImpl->HasShapeToMesh()) )
4394 myNewMeshImpl->addHypothesis( aShapeObject, anHyp );
4399 aGroup->CloseOnDisk();
4402 // try to get applied hypotheses
4403 if ( aTopGroup->ExistInternalObject( "Applied Hypotheses" ) ) {
4404 aGroup = new HDFgroup( "Applied Hypotheses", aTopGroup );
4405 aGroup->OpenOnDisk();
4406 // get number of applied hypotheses
4407 int aNbSubObjects = aGroup->nInternalObjects();
4408 for ( int j = 0; j < aNbSubObjects; j++ ) {
4409 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4410 aGroup->InternalObjectIndentify( j, name_dataset );
4411 // check if it is a hypothesis
4412 if ( string( name_dataset ).substr( 0, 3 ) == string( "Hyp" ) ) {
4413 aDataset = new HDFdataset( name_dataset, aGroup );
4414 aDataset->OpenOnDisk();
4415 size = aDataset->GetSize();
4416 char* refFromFile = new char[ size ];
4417 aDataset->ReadFromDisk( refFromFile );
4418 aDataset->CloseOnDisk();
4419 // san - it is impossible to recover applied hypotheses using their entries within Load() method
4420 //SALOMEDS::SObject_wrap hypSO = myCurrentStudy->FindObjectID( refFromFile );
4421 //CORBA::Object_var hypObject = SObjectToObject( hypSO );
4422 int id = atoi( refFromFile );
4423 delete [] refFromFile;
4424 string anIOR = myStudyContext->getIORbyOldId( id );
4425 if ( !anIOR.empty() ) {
4426 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
4427 if ( !CORBA::is_nil( hypObject ) ) {
4428 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
4429 if ( !anHyp->_is_nil() && (!aShapeObject->_is_nil()
4430 || !myNewMeshImpl->HasShapeToMesh()) )
4431 myNewMeshImpl->addHypothesis( aShapeObject, anHyp );
4436 aGroup->CloseOnDisk();
4439 // --> try to find SUB-MESHES containers for each type of submesh
4440 for ( int j = GetSubMeshOnVertexTag(); j <= GetSubMeshOnCompoundTag(); j++ ) {
4441 const char* name_meshgroup;
4442 if ( j == GetSubMeshOnVertexTag() )
4443 name_meshgroup = "SubMeshes On Vertex";
4444 else if ( j == GetSubMeshOnEdgeTag() )
4445 name_meshgroup = "SubMeshes On Edge";
4446 else if ( j == GetSubMeshOnWireTag() )
4447 name_meshgroup = "SubMeshes On Wire";
4448 else if ( j == GetSubMeshOnFaceTag() )
4449 name_meshgroup = "SubMeshes On Face";
4450 else if ( j == GetSubMeshOnShellTag() )
4451 name_meshgroup = "SubMeshes On Shell";
4452 else if ( j == GetSubMeshOnSolidTag() )
4453 name_meshgroup = "SubMeshes On Solid";
4454 else if ( j == GetSubMeshOnCompoundTag() )
4455 name_meshgroup = "SubMeshes On Compound";
4457 // try to get submeshes container HDF group
4458 if ( aTopGroup->ExistInternalObject( name_meshgroup ) ) {
4459 // open submeshes containers HDF group
4460 aGroup = new HDFgroup( name_meshgroup, aTopGroup );
4461 aGroup->OpenOnDisk();
4463 // get number of submeshes
4464 int aNbSubMeshes = aGroup->nInternalObjects();
4465 for ( int k = 0; k < aNbSubMeshes; k++ ) {
4467 char name_submeshgroup[ HDF_NAME_MAX_LEN+1 ];
4468 aGroup->InternalObjectIndentify( k, name_submeshgroup );
4469 if ( strncmp( name_submeshgroup, "SubMesh", 7 ) == 0 ) {
4470 // --> get submesh id
4471 int subid = atoi( name_submeshgroup + 7 );
4474 // open submesh HDF group
4475 aSubGroup = new HDFgroup( name_submeshgroup, aGroup );
4476 aSubGroup->OpenOnDisk();
4478 // try to read and set reference to subshape
4479 GEOM::GEOM_Object_var aSubShapeObject;
4480 SMESH::SMESH_subMesh_var aSubMesh;
4482 if ( aSubGroup->ExistInternalObject( "Ref on shape" ) ) {
4483 // load submesh "Ref on shape" - it's an entry to SObject
4484 aDataset = new HDFdataset( "Ref on shape", aSubGroup );
4485 aDataset->OpenOnDisk();
4486 size = aDataset->GetSize();
4487 char* refFromFile = new char[ size ];
4488 aDataset->ReadFromDisk( refFromFile );
4489 aDataset->CloseOnDisk();
4490 if ( strlen( refFromFile ) > 0 ) {
4491 SALOMEDS::SObject_wrap subShapeSO = myCurrentStudy->FindObjectID( refFromFile );
4492 CORBA::Object_var subShapeObject = SObjectToObject( subShapeSO );
4493 if ( !CORBA::is_nil( subShapeObject ) ) {
4494 aSubShapeObject = GEOM::GEOM_Object::_narrow( subShapeObject );
4495 if ( !aSubShapeObject->_is_nil() )
4496 aSubMesh = SMESH::SMESH_subMesh::_duplicate
4497 ( myNewMeshImpl->createSubMesh( aSubShapeObject ) );
4498 if ( aSubMesh->_is_nil() )
4500 string iorSubString = GetORB()->object_to_string( aSubMesh );
4501 int newSubId = myStudyContext->findId( iorSubString );
4502 myStudyContext->mapOldToNew( subid, newSubId );
4507 if ( aSubMesh->_is_nil() )
4510 // try to get applied algorithms
4511 if ( aSubGroup->ExistInternalObject( "Applied Algorithms" ) ) {
4512 // open "applied algorithms" HDF group
4513 aSubSubGroup = new HDFgroup( "Applied Algorithms", aSubGroup );
4514 aSubSubGroup->OpenOnDisk();
4515 // get number of applied algorithms
4516 int aNbSubObjects = aSubSubGroup->nInternalObjects();
4517 for ( int l = 0; l < aNbSubObjects; l++ ) {
4518 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4519 aSubSubGroup->InternalObjectIndentify( l, name_dataset );
4520 // check if it is an algorithm
4521 if ( strncmp( name_dataset, "Algo", 4 ) == 0 ) {
4522 aDataset = new HDFdataset( name_dataset, aSubSubGroup );
4523 aDataset->OpenOnDisk();
4524 size = aDataset->GetSize();
4525 char* refFromFile = new char[ size ];
4526 aDataset->ReadFromDisk( refFromFile );
4527 aDataset->CloseOnDisk();
4529 int id = atoi( refFromFile );
4530 string anIOR = myStudyContext->getIORbyOldId( id );
4531 if ( !anIOR.empty() ) {
4532 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
4533 if ( !CORBA::is_nil( hypObject ) ) {
4534 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
4535 if ( !anHyp->_is_nil() && !aShapeObject->_is_nil() )
4536 myNewMeshImpl->addHypothesis( aSubShapeObject, anHyp );
4541 // close "applied algorithms" HDF group
4542 aSubSubGroup->CloseOnDisk();
4545 // try to get applied hypotheses
4546 if ( aSubGroup->ExistInternalObject( "Applied Hypotheses" ) ) {
4547 // open "applied hypotheses" HDF group
4548 aSubSubGroup = new HDFgroup( "Applied Hypotheses", aSubGroup );
4549 aSubSubGroup->OpenOnDisk();
4550 // get number of applied hypotheses
4551 int aNbSubObjects = aSubSubGroup->nInternalObjects();
4552 for ( int l = 0; l < aNbSubObjects; l++ ) {
4553 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4554 aSubSubGroup->InternalObjectIndentify( l, name_dataset );
4555 // check if it is a hypothesis
4556 if ( string( name_dataset ).substr( 0, 3 ) == string( "Hyp" ) ) {
4557 aDataset = new HDFdataset( name_dataset, aSubSubGroup );
4558 aDataset->OpenOnDisk();
4559 size = aDataset->GetSize();
4560 char* refFromFile = new char[ size ];
4561 aDataset->ReadFromDisk( refFromFile );
4562 aDataset->CloseOnDisk();
4564 int id = atoi( refFromFile );
4565 string anIOR = myStudyContext->getIORbyOldId( id );
4566 if ( !anIOR.empty() ) {
4567 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
4568 if ( !CORBA::is_nil( hypObject ) ) {
4569 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
4570 if ( !anHyp->_is_nil() && !aShapeObject->_is_nil() )
4571 myNewMeshImpl->addHypothesis( aSubShapeObject, anHyp );
4576 // close "APPLIED HYPOTHESES" hdf group
4577 aSubSubGroup->CloseOnDisk();
4580 // close SUB-MESH hdf group
4581 aSubGroup->CloseOnDisk();
4584 // close SUB-MESHES containers hdf group
4585 aGroup->CloseOnDisk();
4589 // try to get GROUPS
4590 for ( int ii = GetNodeGroupsTag(); ii <= GetBallElementsGroupsTag(); ii++ ) {
4591 char name_group[ 30 ];
4592 if ( ii == GetNodeGroupsTag() )
4593 strcpy( name_group, "Groups of Nodes" );
4594 else if ( ii == GetEdgeGroupsTag() )
4595 strcpy( name_group, "Groups of Edges" );
4596 else if ( ii == GetFaceGroupsTag() )
4597 strcpy( name_group, "Groups of Faces" );
4598 else if ( ii == GetVolumeGroupsTag() )
4599 strcpy( name_group, "Groups of Volumes" );
4600 else if ( ii == Get0DElementsGroupsTag() )
4601 strcpy( name_group, "Groups of 0D Elements" );
4602 else if ( ii == GetBallElementsGroupsTag() )
4603 strcpy( name_group, "Groups of Balls" );
4605 if ( aTopGroup->ExistInternalObject( name_group ) ) {
4606 aGroup = new HDFgroup( name_group, aTopGroup );
4607 aGroup->OpenOnDisk();
4608 // get number of groups
4609 int aNbSubObjects = aGroup->nInternalObjects();
4610 for ( int j = 0; j < aNbSubObjects; j++ ) {
4611 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4612 aGroup->InternalObjectIndentify( j, name_dataset );
4613 // check if it is an group
4614 if ( string( name_dataset ).substr( 0, 5 ) == string( "Group" ) ) {
4616 int subid = atoi( string( name_dataset ).substr( 5 ).c_str() );
4619 aDataset = new HDFdataset( name_dataset, aGroup );
4620 aDataset->OpenOnDisk();
4622 // Retrieve actual group name
4623 size = aDataset->GetSize();
4624 char* nameFromFile = new char[ size ];
4625 aDataset->ReadFromDisk( nameFromFile );
4626 aDataset->CloseOnDisk();
4628 // Try to find a shape reference
4629 TopoDS_Shape aShape;
4630 char aRefName[ 30 ];
4631 sprintf( aRefName, "Ref on shape %d", subid);
4632 if ( aGroup->ExistInternalObject( aRefName ) ) {
4633 // load mesh "Ref on shape" - it's an entry to SObject
4634 aDataset = new HDFdataset( aRefName, aGroup );
4635 aDataset->OpenOnDisk();
4636 size = aDataset->GetSize();
4637 char* refFromFile = new char[ size ];
4638 aDataset->ReadFromDisk( refFromFile );
4639 aDataset->CloseOnDisk();
4640 if ( strlen( refFromFile ) > 0 ) {
4641 SALOMEDS::SObject_wrap shapeSO = myCurrentStudy->FindObjectID( refFromFile );
4642 CORBA::Object_var shapeObject = SObjectToObject( shapeSO );
4643 if ( !CORBA::is_nil( shapeObject ) ) {
4644 aShapeObject = GEOM::GEOM_Object::_narrow( shapeObject );
4645 if ( !aShapeObject->_is_nil() )
4646 aShape = GeomObjectToShape( aShapeObject );
4650 // Try to read a filter of SMESH_GroupOnFilter
4651 SMESH::Filter_var filter;
4652 SMESH_PredicatePtr predicate;
4653 std::string hdfGrpName = "Filter " + SMESH_Comment(subid);
4654 if ( aGroup->ExistInternalObject( hdfGrpName.c_str() ))
4656 aDataset = new HDFdataset( hdfGrpName.c_str(), aGroup );
4657 aDataset->OpenOnDisk();
4658 size = aDataset->GetSize();
4659 char* persistStr = new char[ size ];
4660 aDataset->ReadFromDisk( persistStr );
4661 aDataset->CloseOnDisk();
4662 if ( strlen( persistStr ) > 0 ) {
4663 filter = SMESH_GroupOnFilter_i::StringToFilter( persistStr );
4664 predicate = SMESH_GroupOnFilter_i::GetPredicate( filter );
4668 // Create group servant
4669 SMESH::ElementType type = (SMESH::ElementType)(ii - GetNodeGroupsTag() + 1);
4670 SMESH::SMESH_GroupBase_var aNewGroup = SMESH::SMESH_GroupBase::_duplicate
4671 ( myNewMeshImpl->createGroup( type, nameFromFile, aShape, predicate ) );
4672 // Obtain a SMESHDS_Group object
4673 if ( aNewGroup->_is_nil() )
4676 string iorSubString = GetORB()->object_to_string( aNewGroup );
4677 int newSubId = myStudyContext->findId( iorSubString );
4678 myStudyContext->mapOldToNew( subid, newSubId );
4680 SMESH_GroupBase_i* aGroupImpl = SMESH::DownCast< SMESH_GroupBase_i*>( aNewGroup );
4684 if ( SMESH_GroupOnFilter_i* aFilterGroup =
4685 dynamic_cast< SMESH_GroupOnFilter_i*>( aGroupImpl ))
4687 aFilterGroup->SetFilter( filter );
4688 filter->UnRegister();
4690 SMESHDS_GroupBase* aGroupBaseDS = aGroupImpl->GetGroupDS();
4691 if ( !aGroupBaseDS )
4694 aGroupBaseDS->SetStoreName( name_dataset );
4697 // Read color of the group
4698 char aGroupColorName[ 30 ];
4699 sprintf( aGroupColorName, "ColorGroup %d", subid);
4700 if ( aGroup->ExistInternalObject( aGroupColorName ) )
4702 aDataset = new HDFdataset( aGroupColorName, aGroup );
4703 aDataset->OpenOnDisk();
4704 size = aDataset->GetSize();
4705 double* anRGB = new double[ size ];
4706 aDataset->ReadFromDisk( anRGB );
4707 aDataset->CloseOnDisk();
4708 Quantity_Color aColor( anRGB[0], anRGB[1], anRGB[2], Quantity_TOC_RGB );
4709 aGroupBaseDS->SetColor( aColor );
4712 // Fill group with contents from MED file
4713 // SMESHDS_Group* aGrp = dynamic_cast<SMESHDS_Group*>( aGroupBaseDS );
4715 // myReader.GetGroup( aGrp );
4718 aGroup->CloseOnDisk();
4722 // instead of reading mesh data, we read only brief information of all
4723 // objects: mesh, groups, sub-meshes (issue 0021208 )
4726 SMESH_PreMeshInfo::LoadFromFile( myNewMeshImpl, id,
4727 meshfile.ToCString(), filename.ToCString(),
4731 // read Sub-Mesh ORDER if any
4732 if( aTopGroup->ExistInternalObject( "Mesh Order" ) ) {
4733 aDataset = new HDFdataset( "Mesh Order", aTopGroup );
4734 aDataset->OpenOnDisk();
4735 size = aDataset->GetSize();
4736 int* smIDs = new int[ size ];
4737 aDataset->ReadFromDisk( smIDs );
4738 aDataset->CloseOnDisk();
4739 TListOfListOfInt anOrderIds;
4740 anOrderIds.push_back( TListOfInt() );
4741 for ( int i = 0; i < size; i++ )
4742 if ( smIDs[ i ] < 0 ) // is separator
4743 anOrderIds.push_back( TListOfInt() );
4745 anOrderIds.back().push_back(smIDs[ i ]);
4747 myNewMeshImpl->GetImpl().SetMeshOrder( anOrderIds );
4751 // update hyps needing full mesh data restored (issue 20918)
4752 for ( hyp_data = hypDataList.begin(); hyp_data != hypDataList.end(); ++hyp_data )
4754 SMESH_Hypothesis_i* hyp = hyp_data->first;
4755 hyp->UpdateAsMeshesRestored();
4758 // notify algos on completed restoration to set sub-mesh event listeners
4759 for ( meshi_group = meshGroupList.begin(); meshi_group != meshGroupList.end(); ++meshi_group )
4761 SMESH_Mesh_i* myNewMeshImpl = meshi_group->first;
4762 ::SMESH_Mesh& myLocMesh = myNewMeshImpl->GetImpl();
4764 TopoDS_Shape myLocShape;
4765 if(myLocMesh.HasShapeToMesh())
4766 myLocShape = myLocMesh.GetShapeToMesh();
4768 myLocShape = SMESH_Mesh::PseudoShape();
4770 myLocMesh.GetSubMesh(myLocShape)->
4771 ComputeStateEngine (SMESH_subMesh::SUBMESH_RESTORED);
4776 aTopGroup->CloseOnDisk();
4779 aFile->CloseOnDisk();
4782 // Remove temporary files created from the stream
4785 SMESH_File meshFile( meshfile.ToCString() );
4786 if ( !meshFile ) // no meshfile exists
4788 SALOMEDS_Tool::RemoveTemporaryFiles( tmpDir.ToCString(), aFileSeq.in(), true );
4792 Engines::Container_var container = GetContainerRef();
4793 if ( Engines_Container_i* container_i = SMESH::DownCast<Engines_Container_i*>( container ))
4795 container_i->registerTemporaryFile( filename.ToCString() );
4796 container_i->registerTemporaryFile( meshfile.ToCString() );
4797 container_i->registerTemporaryFile( tmpDir.ToCString() );
4801 pd << ""; // prevent optimizing pd out
4803 // creation of tree nodes for all data objects in the study
4804 // to support tree representation customization and drag-n-drop:
4805 SALOMEDS::Study_var study = theComponent->GetStudy();
4806 SALOMEDS::UseCaseBuilder_wrap useCaseBuilder = study->GetUseCaseBuilder();
4807 if ( !useCaseBuilder->IsUseCaseNode( theComponent ) ) {
4808 useCaseBuilder->SetRootCurrent();
4809 useCaseBuilder->Append( theComponent ); // component object is added as the top level item
4810 SALOMEDS::ChildIterator_wrap it = study->NewChildIterator( theComponent );
4811 for (it->InitEx(true); it->More(); it->Next()) {
4812 useCaseBuilder->AppendTo( it->Value()->GetFather(), it->Value() );
4816 INFOS( "SMESH_Gen_i::Load completed" );
4820 //=============================================================================
4822 * SMESH_Gen_i::LoadASCII
4824 * Load SMESH module's data in ASCII format
4826 //=============================================================================
4828 bool SMESH_Gen_i::LoadASCII( SALOMEDS::SComponent_ptr theComponent,
4829 const SALOMEDS::TMPFile& theStream,
4831 bool isMultiFile ) {
4832 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::LoadASCII" );
4833 return Load( theComponent, theStream, theURL, isMultiFile );
4835 //before call main ::Load method it's need for decipher text format to
4836 //binary ( "|xx" => x' )
4837 int size = theStream.length();
4838 if ( int((size / 3 )*3) != size ) //error size of buffer
4841 int real_size = int(size / 3);
4843 _CORBA_Octet* buffer = new _CORBA_Octet[real_size];
4847 for ( int i = 0; i < real_size; i++ )
4849 memcpy( &(tmp[0]), &(theStream[i*3+1]), 2 );
4850 sscanf( tmp, "%x", &c );
4851 sprintf( (char*)&(buffer[i]), "%c", (char)c );
4854 SALOMEDS::TMPFile_var aRealStreamFile = new SALOMEDS::TMPFile(real_size, real_size, buffer, 1);
4856 return Load( theComponent, *(aRealStreamFile._retn()), theURL, isMultiFile );
4859 //=============================================================================
4861 * SMESH_Gen_i::Close
4863 * Clears study-connected data when it is closed
4865 //=============================================================================
4867 void SMESH_Gen_i::Close( SALOMEDS::SComponent_ptr theComponent )
4869 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Close" );
4871 // set correct current study
4872 SALOMEDS::Study_var study = theComponent->GetStudy();
4873 if ( study->StudyId() != GetCurrentStudyID())
4874 setCurrentStudy( study, /*IsBeingClosed=*/true );
4876 // Clear study contexts data
4877 int studyId = GetCurrentStudyID();
4878 if ( myStudyContextMap.find( studyId ) != myStudyContextMap.end() ) {
4879 delete myStudyContextMap[ studyId ];
4880 myStudyContextMap.erase( studyId );
4883 // remove the tmp files meshes are loaded from
4884 SMESH_PreMeshInfo::RemoveStudyFiles_TMP_METHOD( theComponent );
4886 myCurrentStudy = SALOMEDS::Study::_nil();
4890 //=============================================================================
4892 * SMESH_Gen_i::ComponentDataType
4894 * Get component data type
4896 //=============================================================================
4898 char* SMESH_Gen_i::ComponentDataType()
4900 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::ComponentDataType" );
4901 return CORBA::string_dup( "SMESH" );
4905 //=============================================================================
4907 * SMESH_Gen_i::IORToLocalPersistentID
4909 * Transform data from transient form to persistent
4911 //=============================================================================
4913 char* SMESH_Gen_i::IORToLocalPersistentID( SALOMEDS::SObject_ptr /*theSObject*/,
4914 const char* IORString,
4915 CORBA::Boolean /*isMultiFile*/,
4916 CORBA::Boolean /*isASCII*/ )
4918 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::IORToLocalPersistentID" );
4919 StudyContext* myStudyContext = GetCurrentStudyContext();
4921 if ( myStudyContext && strcmp( IORString, "" ) != 0 ) {
4922 int anId = myStudyContext->findId( IORString );
4924 if(MYDEBUG) MESSAGE( "VSR " << anId )
4926 sprintf( strId, "%d", anId );
4927 return CORBA::string_dup( strId );
4930 return CORBA::string_dup( "" );
4933 //=============================================================================
4935 * SMESH_Gen_i::LocalPersistentIDToIOR
4937 * Transform data from persistent form to transient
4939 //=============================================================================
4941 char* SMESH_Gen_i::LocalPersistentIDToIOR( SALOMEDS::SObject_ptr /*theSObject*/,
4942 const char* aLocalPersistentID,
4943 CORBA::Boolean /*isMultiFile*/,
4944 CORBA::Boolean /*isASCII*/ )
4946 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::LocalPersistentIDToIOR(): id = " << aLocalPersistentID );
4947 StudyContext* myStudyContext = GetCurrentStudyContext();
4949 if ( myStudyContext && strcmp( aLocalPersistentID, "" ) != 0 ) {
4950 int anId = atoi( aLocalPersistentID );
4951 return CORBA::string_dup( myStudyContext->getIORbyOldId( anId ).c_str() );
4953 return CORBA::string_dup( "" );
4956 //=======================================================================
4957 //function : RegisterObject
4959 //=======================================================================
4961 int SMESH_Gen_i::RegisterObject(CORBA::Object_ptr theObject)
4963 StudyContext* myStudyContext = GetCurrentStudyContext();
4964 if ( myStudyContext && !CORBA::is_nil( theObject )) {
4965 if (GetCurrentStudyID() == myImportedStudyId)
4966 myImportedStudyChanged = true;
4967 CORBA::String_var iorString = GetORB()->object_to_string( theObject );
4968 return myStudyContext->addObject( string( iorString.in() ) );
4973 //================================================================================
4975 * \brief Return id of registered object
4976 * \param theObject - the Object
4977 * \retval int - Object id
4979 //================================================================================
4981 CORBA::Long SMESH_Gen_i::GetObjectId(CORBA::Object_ptr theObject)
4983 StudyContext* myStudyContext = GetCurrentStudyContext();
4984 if ( myStudyContext && !CORBA::is_nil( theObject )) {
4985 string iorString = GetORB()->object_to_string( theObject );
4986 return myStudyContext->findId( iorString );
4991 //=============================================================================
4993 * SMESH_Gen_i::SetName
4995 * Set a new object name
4997 //=============================================================================
4998 void SMESH_Gen_i::SetName(const char* theIOR,
4999 const char* theName)
5001 if ( theIOR && strcmp( theIOR, "" ) ) {
5002 CORBA::Object_var anObject = GetORB()->string_to_object( theIOR );
5003 SALOMEDS::SObject_wrap aSO = ObjectToSObject( myCurrentStudy, anObject );
5004 if ( !aSO->_is_nil() ) {
5005 SetName( aSO, theName );
5010 int SMESH_Gen_i::GetCurrentStudyID()
5012 return myCurrentStudy->_is_nil() || myCurrentStudy->_non_existent() ? -1 : myCurrentStudy->StudyId();
5015 // Version information
5016 char* SMESH_Gen_i::getVersion()
5018 #if SMESH_DEVELOPMENT
5019 return CORBA::string_dup(SMESH_VERSION_STR"dev");
5021 return CORBA::string_dup(SMESH_VERSION_STR);
5025 //=================================================================================
5026 // function : Move()
5027 // purpose : Moves objects to the specified position.
5028 // Is used in the drag-n-drop functionality.
5029 //=================================================================================
5030 void SMESH_Gen_i::Move( const SMESH::sobject_list& what,
5031 SALOMEDS::SObject_ptr where,
5034 if ( CORBA::is_nil( where ) ) return;
5036 SALOMEDS::Study_var study = where->GetStudy();
5037 SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();
5038 SALOMEDS::UseCaseBuilder_var useCaseBuilder = study->GetUseCaseBuilder();
5039 SALOMEDS::SComponent_var father = where->GetFatherComponent();
5040 std::string dataType = father->ComponentDataType();
5041 if ( dataType != "SMESH" ) return; // not a SMESH component
5043 SALOMEDS::SObject_var objAfter;
5044 if ( row >= 0 && useCaseBuilder->HasChildren( where ) ) {
5045 // insert at given row -> find insertion position
5046 SALOMEDS::UseCaseIterator_var useCaseIt = useCaseBuilder->GetUseCaseIterator( where );
5048 for ( i = 0; i < row && useCaseIt->More(); i++, useCaseIt->Next() );
5049 if ( i == row && useCaseIt->More() ) {
5050 objAfter = useCaseIt->Value();
5054 for ( int i = 0; i < what.length(); i++ ) {
5055 SALOMEDS::SObject_var sobj = what[i];
5056 if ( CORBA::is_nil( sobj ) ) continue; // skip bad object
5057 // insert the object to the use case tree
5058 if ( !CORBA::is_nil( objAfter ) )
5059 useCaseBuilder->InsertBefore( sobj, objAfter ); // insert at given row
5061 useCaseBuilder->AppendTo( where, sobj ); // append to the end of list
5064 //=================================================================================
5065 // function : IsApplicable
5066 // purpose : Return true if algorithm can be applied
5067 //=================================================================================
5068 CORBA::Boolean SMESH_Gen_i::IsApplicable ( const char* theAlgoType,
5069 const char* theLibName,
5070 GEOM::GEOM_Object_ptr theGeomObject,
5071 CORBA::Boolean toCheckAll)
5075 std::string aPlatformLibName;
5076 typedef GenericHypothesisCreator_i* (*GetHypothesisCreator)(const char*);
5077 GenericHypothesisCreator_i* aCreator = getHypothesisCreator(theAlgoType, theLibName, aPlatformLibName);
5080 TopoDS_Shape shape = GeomObjectToShape( theGeomObject );
5081 if ( !shape.IsNull() )
5082 return aCreator->IsApplicable( shape, toCheckAll );
5089 SMESH_CATCH( SMESH::doNothing );
5093 //=================================================================================
5094 // function : importData
5095 // purpose : imports mesh data file (the med one) into the SMESH internal data structure
5096 //=================================================================================
5097 Engines::ListOfIdentifiers* SMESH_Gen_i::importData(CORBA::Long studyId,
5098 Engines::DataContainer_ptr data,
5099 const Engines::ListOfOptions& options)
5101 Engines::ListOfIdentifiers_var aResultIds = new Engines::ListOfIdentifiers;
5102 list<string> aResultList;
5104 CORBA::Object_var aSMObject = myNS->Resolve( "/myStudyManager" );
5105 SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow( aSMObject );
5106 SALOMEDS::Study_var aStudy = aStudyManager->GetStudyByID( studyId );
5107 SetCurrentStudy(aStudy);
5109 // load and store temporary imported file
5110 string aFileName = Kernel_Utils::GetTmpFileName();
5111 aFileName += string(".") + data->extension();
5112 Engines::TMPFile* aFileStream = data->get();
5113 const char *aBuffer = (const char*)aFileStream->NP_data();
5115 std::ofstream aFile(aFileName.c_str(), std::ios::binary);
5117 std::ofstream aFile(aFileName.c_str());
5119 aFile.write(aBuffer, aFileStream->length());
5122 // Retrieve mesh names from the file
5123 DriverMED_R_SMESHDS_Mesh aReader;
5124 aReader.SetFile( aFileName );
5125 aReader.SetMeshId(-1);
5126 Driver_Mesh::Status aStatus;
5127 list<string> aNames = aReader.GetMeshNames(aStatus);
5128 SMESH::mesh_array_var aResult = new SMESH::mesh_array();
5129 SMESH::DriverMED_ReadStatus aStatus2 = (SMESH::DriverMED_ReadStatus)aStatus;
5130 if (aStatus2 == SMESH::DRS_OK) {
5131 // Iterate through all meshes and create mesh objects
5132 for ( list<string>::iterator it = aNames.begin(); it != aNames.end(); it++ ) {
5134 SMESH::SMESH_Mesh_var mesh = createMesh();
5136 // publish mesh in the study
5137 SALOMEDS::SObject_var aSO;
5138 if (CanPublishInStudy(mesh)) {
5139 aSO = PublishMesh(aStudy, mesh.in(), (*it).c_str());
5140 aResultList.push_back(aSO->GetID());
5142 // Read mesh data (groups are published automatically by ImportMEDFile())
5143 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
5144 ASSERT( meshServant );
5145 meshServant->ImportMEDFile( aFileName.c_str(), (*it).c_str() );
5146 //meshServant->GetImpl().GetMeshDS()->Modified();
5149 MESSAGE("Opening MED file problems "<<aFileName.c_str())
5150 return aResultIds._retn();
5153 // remove temporary file
5155 DeleteFileA(aFileName.c_str());
5157 unlink(aFileName.c_str());
5160 if (!aResultList.empty()) {
5161 aResultIds->length(aResultList.size());
5162 list<string>::iterator aListIter = aResultList.begin();
5163 for(int a = 0; aListIter != aResultList.end(); aListIter++, a++)
5164 aResultIds[a] = aListIter->c_str();
5167 myImportedStudyId = studyId;
5168 myImportedStudyChanged = false;
5170 return aResultIds._retn();
5173 //=================================================================================
5174 // function : getModifiedData
5175 // purpose : exports all geometry of this GEOM module into one BRep file
5176 //=================================================================================
5177 Engines::ListOfData* SMESH_Gen_i::getModifiedData(CORBA::Long studyId)
5179 Engines::ListOfData_var aResult = new Engines::ListOfData;
5181 if (!myImportedStudyChanged) {
5182 INFOS("SMESH module data was not changed")
5183 return aResult._retn();
5186 CORBA::Object_var aSMObject = myNS->Resolve("/myStudyManager");
5187 SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(aSMObject);
5188 SALOMEDS::Study_var aStudy = aStudyManager->GetStudyByID(studyId);
5189 SetCurrentStudy(aStudy);
5190 SALOMEDS::SComponent_var aComponent = aStudy->FindComponent("SMESH");
5192 if (CORBA::is_nil(aComponent))
5193 return aResult._retn();
5195 std::string aFullPath(Kernel_Utils::GetTmpFileName());
5196 aFullPath += ".med";
5197 StudyContext* myStudyContext = GetCurrentStudyContext();
5199 SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator(aComponent); // check only published meshes
5200 int aNumMeshes = 0; // number of meshes in result
5201 for(; anIter->More(); anIter->Next()) {
5202 SALOMEDS::SObject_var aSO = anIter->Value();
5203 CORBA::Object_var anObj = aSO->GetObject();
5204 if (!CORBA::is_nil(anObj)) {
5205 SMESH::SMESH_Mesh_var aCORBAMesh = SMESH::SMESH_Mesh::_narrow(anObj);
5206 if(!aCORBAMesh->_is_nil()) {
5207 SMESH_Mesh_i* myImpl = dynamic_cast<SMESH_Mesh_i*>(GetServant(aCORBAMesh).in());
5210 SMESH_Mesh& aMesh = myImpl->GetImpl();
5211 CORBA::String_var objName = aSO->GetName();
5212 aMesh.ExportMED(aFullPath.c_str(), objName.in(), false, MED::eV2_2, 0);
5218 if (aNumMeshes > 0) { // prepare a container to store files
5219 INFOS("Write "<<aNumMeshes<<" meshes to "<<aFullPath.c_str());
5221 Engines::DataContainer_var aData = (new Engines_DataContainer_i(
5222 aFullPath.c_str(), "", "", true))->_this();
5225 return aResult._retn();
5228 //=============================================================================
5230 * SMESHEngine_factory
5232 * C factory, accessible with dlsym, after dlopen
5234 //=============================================================================
5238 PortableServer::ObjectId* SMESHEngine_factory( CORBA::ORB_ptr orb,
5239 PortableServer::POA_ptr poa,
5240 PortableServer::ObjectId* contId,
5241 const char* instanceName,
5242 const char* interfaceName )
5244 if(MYDEBUG) MESSAGE( "PortableServer::ObjectId* SMESHEngine_factory()" );
5245 if(MYDEBUG) SCRUTE(interfaceName);
5246 SMESH_Gen_i* aSMESHGen = new SMESH_Gen_i(orb, poa, contId, instanceName, interfaceName);
5247 return aSMESHGen->getId() ;