1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SMESH_Gen_i.cxx
23 // Author : Paul RASCLE, EDF
27 #include <TopExp_Explorer.hxx>
29 #include <TopoDS_Iterator.hxx>
30 #include <TopoDS_Compound.hxx>
31 #include <TopoDS_CompSolid.hxx>
32 #include <TopoDS_Solid.hxx>
33 #include <TopoDS_Shell.hxx>
34 #include <TopoDS_Face.hxx>
35 #include <TopoDS_Wire.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopoDS_Vertex.hxx>
38 #include <TopoDS_Shape.hxx>
39 #include <TopTools_MapOfShape.hxx>
40 #include <TopTools_IndexedMapOfShape.hxx>
41 #include <TopTools_ListOfShape.hxx>
42 #include <TopTools_ListIteratorOfListOfShape.hxx>
44 #include <BRep_Tool.hxx>
45 #include <TCollection_AsciiString.hxx>
47 #include <BRepPrimAPI_MakeSphere.hxx>
48 #include <BRepPrimAPI_MakeCylinder.hxx>
49 #include <BRepPrimAPI_MakeBox.hxx>
57 #include <libgen.h> // for basename function
61 #define LibHandle HMODULE
62 #define LoadLib( name ) LoadLibrary( name )
63 #define GetProc GetProcAddress
64 #define UnLoadLib( handle ) FreeLibrary( handle );
66 #define LibHandle void*
67 #define LoadLib( name ) dlopen( name, RTLD_LAZY | RTLD_GLOBAL )
69 #define UnLoadLib( handle ) dlclose( handle );
72 #include "SMESH_Gen_i.hxx"
73 #include "SMESH_version.h"
75 #include "DriverMED_W_SMESHDS_Mesh.h"
76 #include "DriverMED_R_SMESHDS_Mesh.h"
78 #include "DriverCGNS_Read.hxx"
80 #include "MED_Factory.hxx"
81 #include "SMDS_EdgePosition.hxx"
82 #include "SMDS_FacePosition.hxx"
83 #include "SMDS_PolyhedralVolumeOfNodes.hxx"
84 #include "SMDS_SetIterator.hxx"
85 #include "SMDS_SpacePosition.hxx"
86 #include "SMDS_VertexPosition.hxx"
87 #include "SMESHDS_Document.hxx"
88 #include "SMESHDS_Group.hxx"
89 #include "SMESHDS_GroupOnGeom.hxx"
90 #include "SMESH_Algo_i.hxx"
91 #include "SMESH_File.hxx"
92 #include "SMESH_Group.hxx"
93 #include "SMESH_Group_i.hxx"
94 #include "SMESH_Hypothesis.hxx"
95 #include "SMESH_Hypothesis_i.hxx"
96 #include "SMESH_Mesh.hxx"
97 #include "SMESH_MeshEditor.hxx"
98 #include "SMESH_Mesh_i.hxx"
99 #include "SMESH_PreMeshInfo.hxx"
100 #include "SMESH_PythonDump.hxx"
101 #include "SMESH_ControlsDef.hxx"
102 #include "SMESH_TryCatch.hxx" // to include after OCC headers!
104 #include CORBA_SERVER_HEADER(SMESH_Group)
105 #include CORBA_SERVER_HEADER(SMESH_Filter)
106 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
109 #include <GEOM_Client.hxx>
111 #include <Basics_Utils.hxx>
112 #include <Basics_DirUtils.hxx>
114 #include <OpUtil.hxx>
115 #include <SALOMEDS_Tool.hxx>
116 #include <SALOME_Container_i.hxx>
117 #include <SALOME_LifeCycleCORBA.hxx>
118 #include <SALOME_NamingService.hxx>
119 #include <Utils_CorbaException.hxx>
120 #include <Utils_ExceptHandlers.hxx>
121 #include <Utils_SINGLETON.hxx>
122 #include <utilities.h>
124 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
125 #include CORBA_CLIENT_HEADER(SALOME_Session)
127 // helpers about SALOME::GenericObj
128 #include <SALOMEDS_wrap.hxx>
129 #include <SALOMEDS_Attributes_wrap.hxx>
130 #include <GEOM_wrap.hxx>
138 using SMESH::TPythonDump;
141 #define NUM_TMP_FILES 2
144 static int MYDEBUG = 0;
146 static int MYDEBUG = 0;
149 // Static variables definition
150 GEOM::GEOM_Gen_var SMESH_Gen_i::myGeomGen = GEOM::GEOM_Gen::_nil();
151 CORBA::ORB_var SMESH_Gen_i::myOrb;
152 PortableServer::POA_var SMESH_Gen_i::myPoa;
153 SALOME_NamingService* SMESH_Gen_i::myNS = NULL;
154 SALOME_LifeCycleCORBA* SMESH_Gen_i::myLCC = NULL;
155 SMESH_Gen_i* SMESH_Gen_i::mySMESHGen = NULL;
158 const int nbElemPerDiagonal = 10;
160 //=============================================================================
162 * GetServant [ static ]
164 * Get servant of the CORBA object
166 //=============================================================================
168 PortableServer::ServantBase_var SMESH_Gen_i::GetServant( CORBA::Object_ptr theObject )
170 if( CORBA::is_nil( theObject ) || CORBA::is_nil( GetPOA() ) )
173 PortableServer::Servant aServant = GetPOA()->reference_to_servant( theObject );
177 INFOS( "GetServant - Unknown exception was caught!!!" );
182 //=============================================================================
184 * SObjectToObject [ static ]
186 * Get CORBA object corresponding to the SALOMEDS::SObject
188 //=============================================================================
190 CORBA::Object_var SMESH_Gen_i::SObjectToObject( SALOMEDS::SObject_ptr theSObject )
192 SALOMEDS::GenericAttribute_wrap anAttr;
193 CORBA::Object_var anObj;
194 if ( !theSObject->_is_nil() ) {
196 if( theSObject->FindAttribute( anAttr.inout(), "AttributeIOR" ) ) {
197 SALOMEDS::AttributeIOR_wrap anIOR = anAttr;
198 CORBA::String_var aValue = anIOR->Value();
199 if( strcmp( aValue, "" ) != 0 )
200 anObj = GetORB()->string_to_object( aValue );
204 INFOS( "SObjectToObject - Unknown exception was caught!!!" );
210 //=============================================================================
214 * Get SALOME_NamingService object
216 //=============================================================================
218 SALOME_NamingService* SMESH_Gen_i::GetNS()
220 if ( myNS == NULL ) {
221 myNS = SINGLETON_<SALOME_NamingService>::Instance();
222 ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting());
223 myNS->init_orb( GetORB() );
228 //=============================================================================
232 * Get SALOME_LifeCycleCORBA object
234 //=============================================================================
235 SALOME_LifeCycleCORBA* SMESH_Gen_i::GetLCC() {
236 if ( myLCC == NULL ) {
237 myLCC = new SALOME_LifeCycleCORBA( GetNS() );
243 //=============================================================================
245 * GetGeomEngine [ static ]
247 * Get GEOM::GEOM_Gen reference
249 //=============================================================================
250 GEOM::GEOM_Gen_var SMESH_Gen_i::GetGeomEngine() {
251 //CCRT GEOM::GEOM_Gen_var aGeomEngine =
252 //CCRT GEOM::GEOM_Gen::_narrow( GetLCC()->FindOrLoad_Component("FactoryServer","GEOM") );
253 //CCRT return aGeomEngine._retn();
254 if(CORBA::is_nil(myGeomGen))
256 Engines::EngineComponent_ptr temp=GetLCC()->FindOrLoad_Component("FactoryServer","GEOM");
257 myGeomGen=GEOM::GEOM_Gen::_narrow(temp);
262 //=============================================================================
264 * SMESH_Gen_i::SMESH_Gen_i
266 * Default constructor: not for use
268 //=============================================================================
270 SMESH_Gen_i::SMESH_Gen_i()
274 //=============================================================================
276 * SMESH_Gen_i::SMESH_Gen_i
278 * Standard constructor, used with Container
280 //=============================================================================
282 SMESH_Gen_i::SMESH_Gen_i( CORBA::ORB_ptr orb,
283 PortableServer::POA_ptr poa,
284 PortableServer::ObjectId* contId,
285 const char* instanceName,
286 const char* interfaceName )
287 : Engines_Component_i( orb, poa, contId, instanceName, interfaceName )
290 myOrb = CORBA::ORB::_duplicate(orb);
291 myPoa = PortableServer::POA::_duplicate(poa);
294 _id = myPoa->activate_object( _thisObj );
296 myIsEmbeddedMode = false;
297 myShapeReader = NULL; // shape reader
299 myIsHistoricalPythonDump = true;
300 myToForgetMeshDataOnHypModif = false;
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 // delete hypothesis creators
339 map<string, GenericHypothesisCreator_i*>::iterator itHyp, itHyp2;
340 for (itHyp = myHypCreatorMap.begin(); itHyp != myHypCreatorMap.end(); itHyp++)
342 // same creator can be mapped under different names
343 GenericHypothesisCreator_i* creator = (*itHyp).second;
347 for (itHyp2 = itHyp; itHyp2 != myHypCreatorMap.end(); itHyp2++)
348 if ( creator == (*itHyp2).second )
349 (*itHyp2).second = 0;
351 myHypCreatorMap.clear();
353 // Clear study contexts data
354 map<int, StudyContext*>::iterator it;
355 for ( it = myStudyContextMap.begin(); it != myStudyContextMap.end(); ++it ) {
358 myStudyContextMap.clear();
359 // delete shape reader
361 delete myShapeReader;
363 //=============================================================================
365 * SMESH_Gen_i::getHypothesisCreator
367 * Get hypothesis creator
369 //=============================================================================
370 GenericHypothesisCreator_i* SMESH_Gen_i::getHypothesisCreator(const char* theHypName,
371 const char* theLibName,
372 std::string& thePlatformLibName)
373 throw (SALOME::SALOME_Exception)
375 std::string aPlatformLibName;
376 /* It's Need to tranlate lib name for WIN32 or X platform */
377 if ( theLibName && theLibName[0] != '\0' )
379 int libNameLen = strlen(theLibName);
380 //check for old format "libXXXXXXX.so"
381 if (libNameLen > 7 &&
382 !strncmp( theLibName, "lib", 3 ) &&
383 !strcmp( theLibName+libNameLen-3, ".so" ))
387 aPlatformLibName = std::string( theLibName+3, libNameLen-6 ) + ".dll";
388 #elif defined(__APPLE__)
389 aPlatformLibName = std::string( theLibName, libNameLen-3 ) + ".dylib";
391 aPlatformLibName = theLibName;
396 //try to use new format
398 aPlatformLibName = theLibName;
399 aPlatformLibName += ".dll";
400 #elif defined(__APPLE__)
401 aPlatformLibName = std::string( "lib" ) + std::string( theLibName ) + ".dylib";
403 aPlatformLibName = std::string( "lib" ) + std::string( theLibName ) + ".so";
407 thePlatformLibName = aPlatformLibName;
409 Unexpect aCatch(SALOME_SalomeException);
410 if(MYDEBUG) MESSAGE( "Create Hypothesis <" << theHypName << "> from " << aPlatformLibName);
412 typedef GenericHypothesisCreator_i* (*GetHypothesisCreator)(const char* );
413 GenericHypothesisCreator_i* aCreator;
416 // check, if creator for this hypothesis type already exists
417 if (myHypCreatorMap.find(string(theHypName)) == myHypCreatorMap.end())
419 // load plugin library
420 if(MYDEBUG) MESSAGE("Loading server meshers plugin library ...");
421 LibHandle libHandle = LoadLib( aPlatformLibName.c_str() );
424 // report any error, if occured
426 const char* anError = dlerror();
427 throw(SALOME_Exception(anError));
429 throw(SALOME_Exception(LOCALIZED( "Can't load server meshers plugin library" )));
433 // get method, returning hypothesis creator
434 if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
435 GetHypothesisCreator procHandle =
436 (GetHypothesisCreator)GetProc( libHandle, "GetHypothesisCreator" );
439 throw(SALOME_Exception(LOCALIZED("bad hypothesis plugin library")));
440 UnLoadLib(libHandle);
443 // get hypothesis creator
444 if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << theHypName);
445 aCreator = procHandle(theHypName);
448 throw(SALOME_Exception(LOCALIZED("no such a hypothesis in this plugin")));
450 // map hypothesis creator to a hypothesis name
451 myHypCreatorMap[string(theHypName)] = aCreator;
456 return myHypCreatorMap[string(theHypName)];
459 catch (SALOME_Exception& S_ex)
461 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
466 //=============================================================================
468 * SMESH_Gen_i::createHypothesis
470 * Create hypothesis of given type
472 //=============================================================================
473 SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::createHypothesis(const char* theHypName,
474 const char* theLibName)
476 SMESH_Hypothesis_i* myHypothesis_i = 0;
477 SMESH::SMESH_Hypothesis_var hypothesis_i;
478 std::string aPlatformLibName;
479 GenericHypothesisCreator_i* aCreator =
480 getHypothesisCreator(theHypName, theLibName, aPlatformLibName);
482 // create a new hypothesis object, store its ref. in studyContext
483 myHypothesis_i = aCreator->Create(myPoa, GetCurrentStudyID(), &myGen);
486 myHypothesis_i->SetLibName( aPlatformLibName.c_str() ); // for persistency assurance
487 CORBA::String_var hypName = myHypothesis_i->GetName();
488 myHypCreatorMap[ hypName.in() ] = aCreator;
490 // activate the CORBA servant of hypothesis
491 hypothesis_i = myHypothesis_i->_this();
492 int nextId = RegisterObject( hypothesis_i );
493 if(MYDEBUG) { MESSAGE( "Add hypo to map with id = "<< nextId ); }
494 else { nextId = 0; } // avoid "unused variable" warning in release mode
496 return hypothesis_i._retn();
499 //=============================================================================
501 * SMESH_Gen_i::createMesh
503 * Create empty mesh on shape
505 //=============================================================================
506 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::createMesh()
507 throw ( SALOME::SALOME_Exception )
509 Unexpect aCatch(SALOME_SalomeException);
510 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::createMesh" );
512 // Get or create the GEOM_Client instance
514 // create a new mesh object servant, store it in a map in study context
515 SMESH_Mesh_i* meshServant = new SMESH_Mesh_i( GetPOA(), this, GetCurrentStudyID() );
516 // create a new mesh object
517 if(MYDEBUG) MESSAGE("myIsEmbeddedMode " << myIsEmbeddedMode);
518 meshServant->SetImpl( myGen.CreateMesh( GetCurrentStudyID(), myIsEmbeddedMode ));
520 // activate the CORBA servant of Mesh
521 SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( meshServant->_this() );
522 int nextId = RegisterObject( mesh );
523 if(MYDEBUG) { MESSAGE( "Add mesh to map with id = "<< nextId); }
524 else { nextId = 0; } // avoid "unused variable" warning in release mode
527 catch (SALOME_Exception& S_ex) {
528 THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
530 return SMESH::SMESH_Mesh::_nil();
533 //=============================================================================
535 * SMESH_Gen_i::GetShapeReader
539 //=============================================================================
540 GEOM_Client* SMESH_Gen_i::GetShapeReader()
542 // create shape reader if necessary
543 if ( !myShapeReader )
544 myShapeReader = new GEOM_Client(GetContainerRef());
545 ASSERT( myShapeReader );
546 return myShapeReader;
549 //=============================================================================
551 * SMESH_Gen_i::SetGeomEngine
553 * Set GEOM::GEOM_Gen reference
555 //=============================================================================
556 //GEOM::GEOM_Gen_ptr SMESH_Gen_i::SetGeomEngine( const char* containerLoc )
557 void SMESH_Gen_i::SetGeomEngine( GEOM::GEOM_Gen_ptr geomcompo )
559 //Engines::Component_ptr temp=GetLCC()->FindOrLoad_Component(containerLoc,"GEOM");
560 //myGeomGen=GEOM::GEOM_Gen::_narrow(temp);
561 myGeomGen=GEOM::GEOM_Gen::_duplicate(geomcompo);
565 //=============================================================================
567 * SMESH_Gen_i::SetEmbeddedMode
571 //=============================================================================
573 void SMESH_Gen_i::SetEmbeddedMode( CORBA::Boolean theMode )
575 myIsEmbeddedMode = theMode;
577 if ( !myIsEmbeddedMode ) {
578 //PAL10867: disable signals catching with "noexcepthandler" option
579 char* envNoCatchSignals = getenv("NOT_INTERCEPT_SIGNALS");
580 if (!envNoCatchSignals || !atoi(envNoCatchSignals))
585 char* envDisableFPE = getenv("DISABLE_FPE");
586 if (envDisableFPE && atoi(envDisableFPE))
591 OSD::SetSignal( raiseFPE );
593 // else OSD::SetSignal() is called in GUI
597 //=============================================================================
599 * SMESH_Gen_i::IsEmbeddedMode
603 //=============================================================================
605 CORBA::Boolean SMESH_Gen_i::IsEmbeddedMode()
607 return myIsEmbeddedMode;
610 //=============================================================================
612 * SMESH_Gen_i::SetCurrentStudy
616 //=============================================================================
618 void SMESH_Gen_i::SetCurrentStudy( SALOMEDS::Study_ptr theStudy )
620 setCurrentStudy( theStudy );
623 void SMESH_Gen_i::setCurrentStudy( SALOMEDS::Study_ptr theStudy,
624 bool theStudyIsBeingClosed)
626 int curStudyId = GetCurrentStudyID();
627 myCurrentStudy = SALOMEDS::Study::_duplicate( theStudy );
628 // create study context, if it doesn't exist and set current study
629 int studyId = GetCurrentStudyID();
630 if ( myStudyContextMap.find( studyId ) == myStudyContextMap.end() )
631 myStudyContextMap[ studyId ] = new StudyContext;
633 // myCurrentStudy may be nil
634 if ( !theStudyIsBeingClosed && !CORBA::is_nil( myCurrentStudy ) ) {
635 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
636 SALOMEDS::SComponent_wrap GEOM_var = myCurrentStudy->FindComponent( "GEOM" );
637 if( !GEOM_var->_is_nil() )
638 aStudyBuilder->LoadWith( GEOM_var, GetGeomEngine() );
639 // NPAL16168, issue 0020210
640 // Let meshes update their data depending on GEOM groups that could change
641 if ( curStudyId != studyId )
643 CORBA::String_var compDataType = ComponentDataType();
644 SALOMEDS::SComponent_wrap me = myCurrentStudy->FindComponent( compDataType.in() );
645 if ( !me->_is_nil() ) {
646 SALOMEDS::ChildIterator_wrap anIter = myCurrentStudy->NewChildIterator( me );
647 for ( ; anIter->More(); anIter->Next() ) {
648 SALOMEDS::SObject_wrap so = anIter->Value();
649 CORBA::Object_var ior = SObjectToObject( so );
650 if ( SMESH_Mesh_i* mesh = SMESH::DownCast<SMESH_Mesh_i*>( ior ))
651 mesh->CheckGeomModif();
658 //=============================================================================
660 * SMESH_Gen_i::GetCurrentStudy
664 //=============================================================================
666 SALOMEDS::Study_ptr SMESH_Gen_i::GetCurrentStudy()
668 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetCurrentStudy: study Id = " << GetCurrentStudyID() );
669 if ( GetCurrentStudyID() < 0 )
670 return SALOMEDS::Study::_nil();
671 return SALOMEDS::Study::_duplicate( myCurrentStudy );
674 //=============================================================================
676 * SMESH_Gen_i::GetCurrentStudyContext
678 * Get current study context
680 //=============================================================================
681 StudyContext* SMESH_Gen_i::GetCurrentStudyContext()
683 if ( !CORBA::is_nil( myCurrentStudy ) &&
684 myStudyContextMap.find( GetCurrentStudyID() ) != myStudyContextMap.end() )
685 return myStudyContextMap[ myCurrentStudy->StudyId() ];
690 //=============================================================================
692 * SMESH_Gen_i::CreateHypothesis
694 * Create hypothesis/algorothm of given type and publish it in the study
696 //=============================================================================
698 SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::CreateHypothesis( const char* theHypName,
699 const char* theLibName )
700 throw ( SALOME::SALOME_Exception )
702 Unexpect aCatch(SALOME_SalomeException);
703 // Create hypothesis/algorithm
704 SMESH::SMESH_Hypothesis_var hyp = this->createHypothesis( theHypName, theLibName );
706 // Publish hypothesis/algorithm in the study
707 if ( CanPublishInStudy( hyp ) ) {
708 SALOMEDS::SObject_wrap aSO = PublishHypothesis( myCurrentStudy, hyp );
709 if ( !aSO->_is_nil() ) {
710 // Update Python script
711 TPythonDump() << aSO << " = " << this << ".CreateHypothesis('"
712 << theHypName << "', '" << theLibName << "')";
719 //================================================================================
721 * \brief Return a hypothesis holding parameter values corresponding either to the mesh
722 * existing on the given geometry or to size of the geometry.
723 * \param theHypType - hypothesis type name
724 * \param theLibName - plugin library name
725 * \param theMesh - The mesh of interest
726 * \param theGeom - The shape to get parameter values from
727 * \retval SMESH::SMESH_Hypothesis_ptr - The returned hypothesis may be the one existing
728 * in a study and used to compute the mesh, or a temporary one created just to pass
731 //================================================================================
733 SMESH::SMESH_Hypothesis_ptr
734 SMESH_Gen_i::GetHypothesisParameterValues (const char* theHypType,
735 const char* theLibName,
736 SMESH::SMESH_Mesh_ptr theMesh,
737 GEOM::GEOM_Object_ptr theGeom,
738 CORBA::Boolean byMesh)
739 throw ( SALOME::SALOME_Exception )
741 Unexpect aCatch(SALOME_SalomeException);
742 if ( byMesh && CORBA::is_nil( theMesh ) )
743 return SMESH::SMESH_Hypothesis::_nil();
744 if ( byMesh && CORBA::is_nil( theGeom ) )
745 return SMESH::SMESH_Hypothesis::_nil();
747 // -----------------------------------------------
748 // find hypothesis used to mesh theGeom
749 // -----------------------------------------------
751 // get mesh and shape
752 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
753 TopoDS_Shape shape = GeomObjectToShape( theGeom );
754 if ( byMesh && ( !meshServant || meshServant->NbNodes()==0 || shape.IsNull() ))
755 return SMESH::SMESH_Hypothesis::_nil();
756 ::SMESH_Mesh* mesh = meshServant ? &meshServant->GetImpl() : (::SMESH_Mesh*)0;
758 // create a temporary hypothesis to know its dimension
759 SMESH::SMESH_Hypothesis_var tmpHyp = this->createHypothesis( theHypType, theLibName );
760 SMESH_Hypothesis_i* hypServant = SMESH::DownCast<SMESH_Hypothesis_i*>( tmpHyp );
762 return SMESH::SMESH_Hypothesis::_nil();
763 ::SMESH_Hypothesis* hyp = hypServant->GetImpl();
766 // look for a hypothesis of theHypType used to mesh the shape
767 if ( myGen.GetShapeDim( shape ) == hyp->GetDim() )
770 SMESH::ListOfHypothesis_var aHypList = theMesh->GetHypothesisList( theGeom );
771 int nbLocalHyps = aHypList->length();
772 for ( int i = 0; i < nbLocalHyps; i++ ) {
773 CORBA::String_var hypName = aHypList[i]->GetName();
774 if ( strcmp( theHypType, hypName.in() ) == 0 ) // FOUND local!
775 return SMESH::SMESH_Hypothesis::_duplicate( aHypList[i] );
777 // check super shapes
778 TopTools_ListIteratorOfListOfShape itShape( mesh->GetAncestors( shape ));
779 while ( nbLocalHyps == 0 && itShape.More() ) {
780 GEOM::GEOM_Object_ptr geomObj = ShapeToGeomObject( itShape.Value() );
781 if ( ! CORBA::is_nil( geomObj )) {
782 SMESH::ListOfHypothesis_var aHypList = theMesh->GetHypothesisList( geomObj );
783 nbLocalHyps = aHypList->length();
784 for ( int i = 0; i < nbLocalHyps; i++ )
785 if ( strcmp( theHypType, aHypList[i]->GetName() ) == 0 ) // FOUND global!
786 return SMESH::SMESH_Hypothesis::_duplicate( aHypList[i] );
792 // let the temporary hypothesis find out somehow parameter values by mesh
793 if ( hyp->SetParametersByMesh( mesh, shape ))
794 return SMESH::SMESH_Hypothesis::_duplicate( tmpHyp );
799 diagonal = mesh->GetShapeDiagonalSize();
801 diagonal = ::SMESH_Mesh::GetShapeDiagonalSize( shape );
802 ::SMESH_Hypothesis::TDefaults dflts;
803 dflts._elemLength = diagonal / myGen.GetBoundaryBoxSegmentation();
804 dflts._nbSegments = myGen.GetDefaultNbSegments();
805 dflts._shape = &shape;
806 // let the temporary hypothesis initialize it's values
807 if ( hyp->SetParametersByDefaults( dflts, mesh ))
808 return SMESH::SMESH_Hypothesis::_duplicate( tmpHyp );
811 return SMESH::SMESH_Hypothesis::_nil();
814 //=============================================================================
816 * Returns \c True if a hypothesis is assigned to a sole sub-mesh in a current Study
817 * \param [in] theHyp - the hypothesis of interest
818 * \param [out] theMesh - the sole mesh using \a theHyp
819 * \param [out] theShape - the sole geometry \a theHyp is assigned to
820 * \return boolean - \c True if \a theMesh and \a theShape are sole using \a theHyp
822 * If two meshes on same shape have theHyp assigned to the same sub-shape, they are
823 * considered as SAME sub-mesh => result is \c true.
824 * This method ids used to initialize SMESHGUI_GenericHypothesisCreator with
825 * a shape to which an hyp being edited is assigned.
827 //=============================================================================
829 CORBA::Boolean SMESH_Gen_i::GetSoleSubMeshUsingHyp( SMESH::SMESH_Hypothesis_ptr theHyp,
830 SMESH::SMESH_Mesh_out theMesh,
831 GEOM::GEOM_Object_out theShape)
833 if ( GetCurrentStudyID() < 0 || CORBA::is_nil( theHyp ))
836 // get Mesh component SO
837 CORBA::String_var compDataType = ComponentDataType();
838 SALOMEDS::SComponent_wrap comp = myCurrentStudy->FindComponent( compDataType.in() );
839 if ( CORBA::is_nil( comp ))
842 // look for child SO of meshes
843 SMESH::SMESH_Mesh_var foundMesh;
844 TopoDS_Shape foundShape;
846 SALOMEDS::ChildIterator_wrap meshIter = myCurrentStudy->NewChildIterator( comp );
847 for ( ; meshIter->More() && isSole; meshIter->Next() )
849 SALOMEDS::SObject_wrap curSO = meshIter->Value();
850 CORBA::Object_var obj = SObjectToObject( curSO );
851 SMESH_Mesh_i* mesh_i = SMESH::DownCast< SMESH_Mesh_i* >( obj );
855 // look for a sole shape where theHyp is assigned
856 bool isHypFound = false;
857 const ShapeToHypothesis & s2hyps = mesh_i->GetImpl().GetMeshDS()->GetHypotheses();
858 ShapeToHypothesis::Iterator s2hypsIt( s2hyps );
859 for ( ; s2hypsIt.More() && isSole; s2hypsIt.Next() )
861 const THypList& hyps = s2hypsIt.Value();
862 THypList::const_iterator h = hyps.begin();
863 for ( ; h != hyps.end(); ++h )
864 if ( (*h)->GetID() == theHyp->GetId() )
866 if ( h != hyps.end()) // theHyp found
869 if ( ! foundShape.IsNull() &&
870 ! foundShape.IsSame( s2hypsIt.Key() )) // not a sole sub-shape
872 foundShape.Nullify();
876 foundShape = s2hypsIt.Key();
878 } // loop on assigned hyps
880 if ( isHypFound && !foundShape.IsNull() ) // a mesh using theHyp is found
882 if ( !foundMesh->_is_nil() ) // not a sole mesh
884 if ( !foundMesh->HasShapeToMesh() ||
885 !mesh_i ->HasShapeToMesh() )
887 isSole = ( foundMesh->HasShapeToMesh() == mesh_i->HasShapeToMesh() );
891 GEOM::GEOM_Object_var s1 = mesh_i ->GetShapeToMesh();
892 GEOM::GEOM_Object_var s2 = foundMesh->GetShapeToMesh();
893 isSole = s1->IsSame( s2 );
896 foundMesh = SMESH::SMESH_Mesh::_narrow( obj );
902 ! foundMesh->_is_nil() &&
903 ! foundShape.IsNull() )
905 theMesh = foundMesh._retn();
906 theShape = ShapeToGeomObject( foundShape );
907 return ( !theMesh->_is_nil() && !theShape->_is_nil() );
912 //=============================================================================
914 * Sets number of segments per diagonal of boundary box of geometry by which
915 * default segment length of appropriate 1D hypotheses is defined
917 //=============================================================================
919 void SMESH_Gen_i::SetBoundaryBoxSegmentation( CORBA::Long theNbSegments )
920 throw ( SALOME::SALOME_Exception )
922 if ( theNbSegments > 0 )
923 myGen.SetBoundaryBoxSegmentation( int( theNbSegments ));
925 THROW_SALOME_CORBA_EXCEPTION( "non-positive number of segments", SALOME::BAD_PARAM );
927 //=============================================================================
929 * \brief Sets default number of segments per edge
931 //=============================================================================
932 void SMESH_Gen_i::SetDefaultNbSegments(CORBA::Long theNbSegments)
933 throw ( SALOME::SALOME_Exception )
935 if ( theNbSegments > 0 )
936 myGen.SetDefaultNbSegments( int(theNbSegments) );
938 THROW_SALOME_CORBA_EXCEPTION( "non-positive number of segments", SALOME::BAD_PARAM );
941 //=============================================================================
945 //=============================================================================
947 void SMESH_Gen_i::SetOption(const char* name, const char* value)
949 if ( name && value && strlen( value ) > 0 )
952 if ( strcmp(name, "historical_python_dump") == 0 )
954 myIsHistoricalPythonDump = ( value[0] == '1' || toupper(value[0]) == 'T' ); // 1 || true
955 msgToGUI = "preferences/SMESH/historical_python_dump/";
956 msgToGUI += myIsHistoricalPythonDump ? "true" : "false";
958 else if ( strcmp(name, "forget_mesh_on_hyp_modif") == 0 )
960 myToForgetMeshDataOnHypModif = ( value[0] == '1' || toupper(value[0]) == 'T' ); // 1 || true
961 msgToGUI = "preferences/SMESH/forget_mesh_on_hyp_modif/";
962 msgToGUI += myToForgetMeshDataOnHypModif ? "true" : "false";
964 else if ( strcmp(name, "default_grp_color") == 0 )
968 // color must be presented as a string of following form:
969 if ( str.at(0) == '#' && str.length() == 7 ) { // hexadecimal color ("#ffaa00", for example)
971 for ( size_t i = 0; i < str.length()/2; i++ )
972 if ( str.at(i*2) >= '0' && str.at(i*2) <= 'f' && str.at(i*2+1) >= '0' && str.at(i*2+1) <= 'f' )
973 color.push_back( strtol( str.substr( i*2, 2 ).c_str(), NULL, 16 ) );
975 else if ( value ) { // rgb color ("255,170,0", for example)
976 string tempValue( value );
977 char* colorValue = strtok( &tempValue[0], "," );
978 while ( colorValue != NULL ) {
979 int c_value = atoi( colorValue );
980 if ( c_value >= 0 && c_value <= 255 )
981 color.push_back( c_value );
982 colorValue = strtok( NULL, "," );
985 if ( color.size() == 3 ) { // color must have three valid component
986 SMESHDS_GroupBase::SetDefaultColor( Quantity_Color( color[0]/255., color[1]/255., color[2]/255., Quantity_TOC_RGB ) );
987 myDefaultGroupColor = value;
988 msgToGUI = "preferences/SMESH/default_grp_color/";
993 // update preferences in case if SetOption() is invoked from python console
994 if ( !msgToGUI.empty() )
996 CORBA::Object_var obj = SMESH_Gen_i::GetNS()->Resolve( "/Kernel/Session" );
997 SALOME::Session_var session = SALOME::Session::_narrow( obj );
998 if ( !CORBA::is_nil( session ) )
999 session->emitMessageOneWay(msgToGUI.c_str());
1004 //=============================================================================
1006 Return an option value
1008 //=============================================================================
1010 char* SMESH_Gen_i::GetOption(const char* name)
1014 if ( strcmp(name, "historical_python_dump") == 0 )
1016 return CORBA::string_dup( myIsHistoricalPythonDump ? "true" : "false" );
1018 if ( strcmp(name, "forget_mesh_on_hyp_modif") == 0 )
1020 return CORBA::string_dup( myToForgetMeshDataOnHypModif ? "true" : "false" );
1022 if ( strcmp(name, "default_grp_color") == 0 )
1024 return CORBA::string_dup( myDefaultGroupColor.c_str() );
1027 return CORBA::string_dup( "" );
1030 //=============================================================================
1032 * SMESH_Gen_i::CreateMesh
1034 * Create empty mesh on a shape and publish it in the study
1036 //=============================================================================
1038 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMesh( GEOM::GEOM_Object_ptr theShapeObject )
1039 throw ( SALOME::SALOME_Exception )
1041 Unexpect aCatch(SALOME_SalomeException);
1042 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMesh" );
1044 SMESH::SMESH_Mesh_var mesh = this->createMesh();
1046 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( mesh );
1047 ASSERT( meshServant );
1048 meshServant->SetShape( theShapeObject );
1050 // publish mesh in the study
1051 if ( CanPublishInStudy( mesh ) ) {
1052 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1053 aStudyBuilder->NewCommand(); // There is a transaction
1054 SALOMEDS::SObject_wrap aSO = PublishMesh( myCurrentStudy, mesh.in() );
1055 aStudyBuilder->CommitCommand();
1056 if ( !aSO->_is_nil() ) {
1057 // Update Python script
1058 TPythonDump() << aSO << " = " << this << ".CreateMesh(" << theShapeObject << ")";
1062 return mesh._retn();
1065 //=============================================================================
1067 * SMESH_Gen_i::CreateEmptyMesh
1071 //=============================================================================
1073 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateEmptyMesh()
1074 throw ( SALOME::SALOME_Exception )
1076 Unexpect aCatch(SALOME_SalomeException);
1077 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMesh" );
1079 SMESH::SMESH_Mesh_var mesh = this->createMesh();
1081 // publish mesh in the study
1082 if ( CanPublishInStudy( mesh ) ) {
1083 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1084 aStudyBuilder->NewCommand(); // There is a transaction
1085 SALOMEDS::SObject_wrap aSO = PublishMesh( myCurrentStudy, mesh.in() );
1086 aStudyBuilder->CommitCommand();
1087 if ( !aSO->_is_nil() ) {
1088 // Update Python script
1089 TPythonDump() << aSO << " = " << this << ".CreateEmptyMesh()";
1093 return mesh._retn();
1098 //================================================================================
1100 * \brief Throws an exception in case if the file can't be read
1102 //================================================================================
1104 void checkFileReadable( const char* theFileName ) throw ( SALOME::SALOME_Exception )
1106 SMESH_File f ( theFileName );
1109 if ( !f.error().empty() )
1110 THROW_SALOME_CORBA_EXCEPTION( f.error().c_str(), SALOME::BAD_PARAM);
1112 THROW_SALOME_CORBA_EXCEPTION
1113 (( SMESH_Comment("Can't open for reading the file ") << theFileName ).c_str(),
1114 SALOME::BAD_PARAM );
1119 //=============================================================================
1121 * SMESH_Gen_i::CreateMeshFromUNV
1123 * Create mesh and import data from UNV file
1125 //=============================================================================
1127 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMeshesFromUNV( const char* theFileName )
1128 throw ( SALOME::SALOME_Exception )
1130 Unexpect aCatch(SALOME_SalomeException);
1132 checkFileReadable( theFileName );
1134 SMESH::SMESH_Mesh_var aMesh = createMesh();
1136 // publish mesh in the study
1137 if ( CanPublishInStudy( aMesh ) ) {
1138 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1139 aStudyBuilder->NewCommand(); // There is a transaction
1140 SALOMEDS::SObject_wrap aSO = PublishMesh( myCurrentStudy, aMesh.in(), aFileName.c_str() );
1141 aStudyBuilder->CommitCommand();
1142 if ( !aSO->_is_nil() ) {
1143 // Update Python script
1144 TPythonDump() << aSO << " = " << this << ".CreateMeshesFromUNV(r'" << theFileName << "')";
1148 SMESH_Mesh_i* aServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( aMesh ).in() );
1150 aServant->ImportUNVFile( theFileName );
1152 // Dump creation of groups
1153 SMESH::ListOfGroups_var groups = aServant->GetGroups();
1155 aServant->GetImpl().GetMeshDS()->Modified();
1156 return aMesh._retn();
1159 //=============================================================================
1161 * SMESH_Gen_i::CreateMeshFromMED
1163 * Create mesh and import data from MED file
1165 //=============================================================================
1167 SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromMEDorSAUV( const char* theFileName,
1168 SMESH::DriverMED_ReadStatus& theStatus,
1169 const char* theCommandNameForPython,
1170 const char* theFileNameForPython)
1173 char bname[ _MAX_FNAME ];
1174 _splitpath( theFileNameForPython, NULL, NULL, bname, NULL );
1175 string aFileName = bname;
1177 string aFileName = basename( const_cast<char *>(theFileNameForPython) );
1179 // Retrieve mesh names from the file
1180 DriverMED_R_SMESHDS_Mesh myReader;
1181 myReader.SetFile( theFileName );
1182 myReader.SetMeshId( -1 );
1183 Driver_Mesh::Status aStatus;
1184 list<string> aNames = myReader.GetMeshNames(aStatus);
1185 SMESH::mesh_array_var aResult = new SMESH::mesh_array();
1186 theStatus = (SMESH::DriverMED_ReadStatus)aStatus;
1188 { // open a new scope to make aPythonDump die before PythonDump in SMESH_Mesh::GetGroups()
1191 TPythonDump aPythonDump;
1192 aPythonDump << "([";
1194 if (theStatus == SMESH::DRS_OK) {
1195 SALOMEDS::StudyBuilder_var aStudyBuilder;
1196 if ( GetCurrentStudyID() > -1 )
1198 aStudyBuilder = myCurrentStudy->NewBuilder();
1199 aStudyBuilder->NewCommand(); // There is a transaction
1201 aResult->length( aNames.size() );
1204 // Iterate through all meshes and create mesh objects
1205 for ( list<string>::iterator it = aNames.begin(); it != aNames.end(); it++ )
1208 if (i > 0) aPythonDump << ", ";
1211 SMESH::SMESH_Mesh_var mesh = createMesh();
1213 // publish mesh in the study
1214 SALOMEDS::SObject_wrap aSO;
1215 if ( CanPublishInStudy( mesh ) )
1216 // little trick: for MED file theFileName and theFileNameForPython are the same, but they are different for SAUV
1217 // - as names of meshes are stored in MED file, we use them for data publishing
1218 // - as mesh name is not stored in UNV file, we use file name as name of mesh when publishing data
1219 aSO = PublishMesh( myCurrentStudy, mesh.in(), ( theFileName == theFileNameForPython ) ? (*it).c_str() : aFileName.c_str() );
1220 if ( !aSO->_is_nil() ) {
1225 aPythonDump << "mesh_" << i;
1228 // Read mesh data (groups are published automatically by ImportMEDFile())
1229 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
1230 ASSERT( meshServant );
1231 SMESH::DriverMED_ReadStatus status1 =
1232 meshServant->ImportMEDFile( theFileName, (*it).c_str() );
1233 if (status1 > theStatus)
1234 theStatus = status1;
1236 aResult[i++] = SMESH::SMESH_Mesh::_duplicate( mesh );
1237 meshServant->GetImpl().GetMeshDS()->Modified();
1239 if ( !aStudyBuilder->_is_nil() )
1240 aStudyBuilder->CommitCommand();
1243 // Update Python script
1244 aPythonDump << "], status) = " << this << "." << theCommandNameForPython << "(r'" << theFileNameForPython << "')";
1246 // Dump creation of groups
1247 for ( CORBA::ULong i = 0; i < aResult->length(); ++i )
1248 SMESH::ListOfGroups_var groups = aResult[ i ]->GetGroups();
1250 return aResult._retn();
1253 SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromMED( const char* theFileName,
1254 SMESH::DriverMED_ReadStatus& theStatus)
1255 throw ( SALOME::SALOME_Exception )
1257 Unexpect aCatch(SALOME_SalomeException);
1258 checkFileReadable( theFileName );
1260 SMESH::mesh_array* result = CreateMeshesFromMEDorSAUV(theFileName, theStatus, "CreateMeshesFromMED", theFileName);
1264 //=============================================================================
1266 * SMESH_Gen_i::CreateMeshFromSAUV
1268 * Create mesh and import data from SAUV file
1270 //=============================================================================
1272 SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromSAUV( const char* theFileName,
1273 SMESH::DriverMED_ReadStatus& theStatus)
1274 throw ( SALOME::SALOME_Exception )
1276 Unexpect aCatch(SALOME_SalomeException);
1277 checkFileReadable( theFileName );
1279 std::string sauvfilename(theFileName);
1280 std::string medfilename(theFileName);
1281 medfilename += ".med";
1284 cmd = "%PYTHONBIN% ";
1289 cmd += "from medutilities import convert ; convert(r'" + sauvfilename + "', 'GIBI', 'MED', 1, r'" + medfilename + "')";
1291 system(cmd.c_str());
1292 SMESH::mesh_array* result = CreateMeshesFromMEDorSAUV(medfilename.c_str(), theStatus, "CreateMeshesFromSAUV", sauvfilename.c_str());
1294 cmd = "%PYTHONBIN% ";
1299 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1301 system(cmd.c_str());
1305 //=============================================================================
1307 * SMESH_Gen_i::CreateMeshFromSTL
1309 * Create mesh and import data from STL file
1311 //=============================================================================
1313 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMeshesFromSTL( const char* theFileName )
1314 throw ( SALOME::SALOME_Exception )
1316 Unexpect aCatch(SALOME_SalomeException);
1317 checkFileReadable( theFileName );
1319 SMESH::SMESH_Mesh_var aMesh = createMesh();
1322 char bname[ _MAX_FNAME ];
1323 _splitpath( theFileName, NULL, NULL, bname, NULL );
1324 string aFileName = bname;
1326 string aFileName = basename( const_cast<char *>(theFileName) );
1328 // publish mesh in the study
1329 if ( CanPublishInStudy( aMesh ) ) {
1330 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1331 aStudyBuilder->NewCommand(); // There is a transaction
1332 SALOMEDS::SObject_wrap aSO = PublishInStudy
1333 ( myCurrentStudy, SALOMEDS::SObject::_nil(), aMesh.in(), aFileName.c_str() );
1334 aStudyBuilder->CommitCommand();
1335 if ( !aSO->_is_nil() ) {
1336 // Update Python script
1337 TPythonDump() << aSO << " = " << this << ".CreateMeshesFromSTL(r'" << theFileName << "')";
1341 SMESH_Mesh_i* aServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( aMesh ).in() );
1343 aServant->ImportSTLFile( theFileName );
1344 aServant->GetImpl().GetMeshDS()->Modified();
1345 return aMesh._retn();
1348 //================================================================================
1350 * \brief Create meshes and import data from the CGSN file
1352 //================================================================================
1354 SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromCGNS( const char* theFileName,
1355 SMESH::DriverMED_ReadStatus& theStatus)
1356 throw ( SALOME::SALOME_Exception )
1358 Unexpect aCatch(SALOME_SalomeException);
1359 checkFileReadable( theFileName );
1361 SMESH::mesh_array_var aResult = new SMESH::mesh_array();
1364 // Retrieve nb meshes from the file
1365 DriverCGNS_Read myReader;
1366 myReader.SetFile( theFileName );
1367 Driver_Mesh::Status aStatus;
1368 int nbMeshes = myReader.GetNbMeshes(aStatus);
1369 theStatus = (SMESH::DriverMED_ReadStatus)aStatus;
1371 aResult->length( nbMeshes );
1373 { // open a new scope to make aPythonDump die before PythonDump in SMESH_Mesh::GetGroups()
1376 TPythonDump aPythonDump;
1377 aPythonDump << "([";
1379 if (theStatus == SMESH::DRS_OK)
1381 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1382 aStudyBuilder->NewCommand(); // There is a transaction
1386 // Iterate through all meshes and create mesh objects
1387 for ( ; i < nbMeshes; ++i )
1390 if (i > 0) aPythonDump << ", ";
1393 SMESH::SMESH_Mesh_var mesh = createMesh();
1394 aResult[i] = SMESH::SMESH_Mesh::_duplicate( mesh );
1396 // Read mesh data (groups are published automatically by ImportMEDFile())
1397 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
1398 ASSERT( meshServant );
1400 SMESH::DriverMED_ReadStatus status1 =
1401 meshServant->ImportCGNSFile( theFileName, i, meshName );
1402 if (status1 > theStatus)
1403 theStatus = status1;
1405 meshServant->GetImpl().GetMeshDS()->Modified();
1406 // publish mesh in the study
1407 SALOMEDS::SObject_wrap aSO;
1408 if ( CanPublishInStudy( mesh ) )
1409 aSO = PublishMesh( myCurrentStudy, mesh.in(), meshName.c_str() );
1412 if ( !aSO->_is_nil() ) {
1416 aPythonDump << "mesh_" << i;
1419 aStudyBuilder->CommitCommand();
1422 aPythonDump << "], status) = " << this << ".CreateMeshesFromCGNS(r'" << theFileName << "')";
1424 // Dump creation of groups
1425 for ( CORBA::ULong i = 0; i < aResult->length(); ++i )
1426 SMESH::ListOfGroups_var groups = aResult[ i ]->GetGroups();
1428 THROW_SALOME_CORBA_EXCEPTION("CGNS library is unavailable", SALOME::INTERNAL_ERROR);
1431 return aResult._retn();
1434 //================================================================================
1436 * \brief Create a mesh and import data from a GMF file
1438 //================================================================================
1440 SMESH::SMESH_Mesh_ptr
1441 SMESH_Gen_i::CreateMeshesFromGMF( const char* theFileName,
1442 CORBA::Boolean theMakeRequiredGroups,
1443 SMESH::ComputeError_out theError)
1444 throw ( SALOME::SALOME_Exception )
1446 Unexpect aCatch(SALOME_SalomeException);
1447 checkFileReadable( theFileName );
1449 SMESH::SMESH_Mesh_var aMesh = createMesh();
1451 char bname[ _MAX_FNAME ];
1452 _splitpath( theFileName, NULL, NULL, bname, NULL );
1453 string aFileName = bname;
1455 string aFileName = basename( const_cast<char *>(theFileName) );
1457 // publish mesh in the study
1458 if ( CanPublishInStudy( aMesh ) ) {
1459 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1460 aStudyBuilder->NewCommand(); // There is a transaction
1461 SALOMEDS::SObject_wrap aSO = PublishInStudy
1462 ( myCurrentStudy, SALOMEDS::SObject::_nil(), aMesh.in(), aFileName.c_str() );
1463 aStudyBuilder->CommitCommand();
1464 if ( !aSO->_is_nil() ) {
1465 // Update Python script
1466 TPythonDump() << "("<< aSO << ", error) = " << this << ".CreateMeshesFromGMF(r'"
1467 << theFileName << "', "
1468 << theMakeRequiredGroups << " )";
1471 SMESH_Mesh_i* aServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( aMesh ).in() );
1473 theError = aServant->ImportGMFFile( theFileName, theMakeRequiredGroups );
1474 aServant->GetImpl().GetMeshDS()->Modified();
1475 return aMesh._retn();
1479 //=============================================================================
1481 * SMESH_Gen_i::IsReadyToCompute
1483 * Returns true if mesh contains enough data to be computed
1485 //=============================================================================
1487 CORBA::Boolean SMESH_Gen_i::IsReadyToCompute( SMESH::SMESH_Mesh_ptr theMesh,
1488 GEOM::GEOM_Object_ptr theShapeObject )
1489 throw ( SALOME::SALOME_Exception )
1491 Unexpect aCatch(SALOME_SalomeException);
1492 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::IsReadyToCompute" );
1494 if ( CORBA::is_nil( theShapeObject ) )
1495 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1496 SALOME::BAD_PARAM );
1498 if ( CORBA::is_nil( theMesh ) )
1499 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
1500 SALOME::BAD_PARAM );
1504 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
1505 ASSERT( meshServant );
1506 if ( meshServant ) {
1507 // get local TopoDS_Shape
1508 TopoDS_Shape myLocShape = GeomObjectToShape( theShapeObject );
1509 // call implementation
1510 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1511 return myGen.CheckAlgoState( myLocMesh, myLocShape );
1514 catch ( SALOME_Exception& S_ex ) {
1515 INFOS( "catch exception "<< S_ex.what() );
1520 //================================================================================
1522 * \brief Find SObject for an algo
1524 //================================================================================
1526 SALOMEDS::SObject_ptr SMESH_Gen_i::GetAlgoSO(const ::SMESH_Algo* algo)
1529 if ( !myCurrentStudy->_is_nil() ) {
1530 // find algo in the study
1531 CORBA::String_var compDataType = ComponentDataType();
1532 SALOMEDS::SComponent_wrap father = myCurrentStudy->FindComponent( compDataType.in() );
1533 if ( !father->_is_nil() ) {
1534 SALOMEDS::ChildIterator_wrap itBig = myCurrentStudy->NewChildIterator( father );
1535 for ( ; itBig->More(); itBig->Next() ) {
1536 SALOMEDS::SObject_wrap gotBranch = itBig->Value();
1537 if ( gotBranch->Tag() == GetAlgorithmsRootTag() ) {
1538 SALOMEDS::ChildIterator_wrap algoIt = myCurrentStudy->NewChildIterator( gotBranch );
1539 for ( ; algoIt->More(); algoIt->Next() ) {
1540 SALOMEDS::SObject_wrap algoSO = algoIt->Value();
1541 CORBA::Object_var algoIOR = SObjectToObject( algoSO );
1542 if ( !CORBA::is_nil( algoIOR )) {
1543 SMESH_Hypothesis_i* impl = SMESH::DownCast<SMESH_Hypothesis_i*>( algoIOR );
1544 if ( impl && impl->GetImpl() == algo )
1545 return algoSO._retn();
1547 } // loop on algo SO's
1550 } // SMESH component iterator
1554 return SALOMEDS::SObject::_nil();
1557 //================================================================================
1559 * \brief Return errors of mesh computation
1561 //================================================================================
1563 SMESH::compute_error_array* SMESH_Gen_i::GetComputeErrors( SMESH::SMESH_Mesh_ptr theMesh,
1564 GEOM::GEOM_Object_ptr theSubObject )
1565 throw ( SALOME::SALOME_Exception )
1567 Unexpect aCatch(SALOME_SalomeException);
1568 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetComputeErrors()" );
1570 if ( CORBA::is_nil( theSubObject ) && theMesh->HasShapeToMesh())
1571 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
1573 if ( CORBA::is_nil( theMesh ) )
1574 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1576 SMESH::compute_error_array_var error_array = new SMESH::compute_error_array;
1578 if ( SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh ))
1581 if(theMesh->HasShapeToMesh())
1582 shape = GeomObjectToShape( theSubObject );
1584 shape = SMESH_Mesh::PseudoShape();
1586 ::SMESH_Mesh& mesh = meshServant->GetImpl();
1588 error_array->length( mesh.GetMeshDS()->MaxShapeIndex() );
1591 SMESH_subMesh *sm = mesh.GetSubMesh(shape);
1592 const bool includeSelf = true, complexShapeFirst = true;
1593 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(includeSelf,
1595 while ( smIt->more() )
1598 // if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
1600 SMESH_ComputeErrorPtr error = sm->GetComputeError();
1601 if ( error && !error->IsOK() )
1603 if ( !( error->myAlgo ) &&
1604 !( error->myAlgo = sm->GetAlgo() ))
1606 SMESH::ComputeError & errStruct = error_array[ nbErr++ ];
1607 errStruct.code = -( error->myName < 0 ? error->myName + 1: error->myName ); // -1 -> 0
1608 errStruct.comment = error->myComment.c_str();
1609 errStruct.subShapeID = sm->GetId();
1610 SALOMEDS::SObject_wrap algoSO = GetAlgoSO( error->myAlgo );
1611 if ( !algoSO->_is_nil() ) {
1612 CORBA::String_var algoName = algoSO->GetName();
1613 errStruct.algoName = algoName;
1616 errStruct.algoName = error->myAlgo->GetName();
1618 errStruct.hasBadMesh = !error->myBadElements.empty();
1621 error_array->length( nbErr );
1624 catch ( SALOME_Exception& S_ex ) {
1625 INFOS( "catch exception "<< S_ex.what() );
1628 return error_array._retn();
1631 //================================================================================
1633 * \brief Return mesh elements preventing computation of a subshape
1635 //================================================================================
1637 SMESH::MeshPreviewStruct*
1638 SMESH_Gen_i::GetBadInputElements( SMESH::SMESH_Mesh_ptr theMesh,
1639 CORBA::Short theSubShapeID )
1640 throw ( SALOME::SALOME_Exception )
1642 Unexpect aCatch(SALOME_SalomeException);
1643 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetBadInputElements()" );
1645 if ( CORBA::is_nil( theMesh ) )
1646 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1648 SMESH::MeshPreviewStruct_var result = new SMESH::MeshPreviewStruct;
1651 if ( SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh ))
1653 // mesh implementation
1654 ::SMESH_Mesh& mesh = meshServant->GetImpl();
1655 // submesh by subshape id
1656 if ( SMESH_subMesh * sm = mesh.GetSubMeshContaining( theSubShapeID ))
1659 SMESH_ComputeErrorPtr error = sm->GetComputeError();
1660 if ( error && !error->myBadElements.empty())
1662 typedef map<const SMDS_MeshElement*, int > TNode2LocalIDMap;
1663 typedef TNode2LocalIDMap::iterator TNodeLocalID;
1665 // get nodes of elements and count elements
1666 TNode2LocalIDMap mapNode2LocalID;
1667 list< TNodeLocalID > connectivity;
1668 int i, nbElements = 0, nbConnNodes = 0;
1670 list<const SMDS_MeshElement*>::iterator elemIt = error->myBadElements.begin();
1671 list<const SMDS_MeshElement*>::iterator elemEnd = error->myBadElements.end();
1672 for ( ; elemIt != elemEnd; ++elemIt, ++nbElements )
1674 SMDS_ElemIteratorPtr nIt = (*elemIt)->nodesIterator();
1675 while ( nIt->more() )
1676 connectivity.push_back
1677 ( mapNode2LocalID.insert( make_pair( nIt->next(), ++nbConnNodes)).first );
1679 // fill node coords and assign local ids to the nodes
1680 int nbNodes = mapNode2LocalID.size();
1681 result->nodesXYZ.length( nbNodes );
1682 TNodeLocalID node2ID = mapNode2LocalID.begin();
1683 for ( i = 0; i < nbNodes; ++i, ++node2ID ) {
1684 node2ID->second = i;
1685 const SMDS_MeshNode* node = (const SMDS_MeshNode*) node2ID->first;
1686 result->nodesXYZ[i].x = node->X();
1687 result->nodesXYZ[i].y = node->Y();
1688 result->nodesXYZ[i].z = node->Z();
1690 // fill connectivity
1691 result->elementConnectivities.length( nbConnNodes );
1692 list< TNodeLocalID >::iterator connIt = connectivity.begin();
1693 for ( i = 0; i < nbConnNodes; ++i, ++connIt ) {
1694 result->elementConnectivities[i] = (*connIt)->second;
1696 // fill element types
1697 result->elementTypes.length( nbElements );
1698 for ( i = 0, elemIt = error->myBadElements.begin(); i <nbElements; ++i, ++elemIt )
1700 const SMDS_MeshElement* elem = *elemIt;
1701 result->elementTypes[i].SMDS_ElementType = (SMESH::ElementType) elem->GetType();
1702 result->elementTypes[i].isPoly = elem->IsPoly();
1703 result->elementTypes[i].nbNodesInElement = elem->NbNodes();
1709 catch ( SALOME_Exception& S_ex ) {
1710 INFOS( "catch exception "<< S_ex.what() );
1713 return result._retn();
1716 //================================================================================
1718 * \brief Create a group of elements preventing computation of a sub-shape
1720 //================================================================================
1722 SMESH::ListOfGroups*
1723 SMESH_Gen_i::MakeGroupsOfBadInputElements( SMESH::SMESH_Mesh_ptr theMesh,
1724 CORBA::Short theSubShapeID,
1725 const char* theGroupName )
1726 throw ( SALOME::SALOME_Exception )
1728 Unexpect aCatch(SALOME_SalomeException);
1730 SMESH::ListOfGroups_var groups;
1732 if ( CORBA::is_nil( theMesh ) )
1733 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1736 if ( SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh ))
1738 groups = meshServant->MakeGroupsOfBadInputElements( theSubShapeID, theGroupName );
1739 TPythonDump() << groups << " = " << this
1740 << ".MakeGroupsOfBadInputElements( "
1741 << theMesh << ", " << theSubShapeID << ", '" << theGroupName << "' )";
1744 catch ( SALOME_Exception& S_ex ) {
1745 INFOS( "catch exception "<< S_ex.what() );
1747 return groups._retn();
1750 //================================================================================
1752 * \brief Returns errors of hypotheses definintion
1753 * \param theMesh - the mesh
1754 * \param theSubObject - the main or sub- shape
1755 * \retval SMESH::algo_error_array* - sequence of errors
1757 //================================================================================
1759 SMESH::algo_error_array* SMESH_Gen_i::GetAlgoState( SMESH::SMESH_Mesh_ptr theMesh,
1760 GEOM::GEOM_Object_ptr theSubObject )
1761 throw ( SALOME::SALOME_Exception )
1763 Unexpect aCatch(SALOME_SalomeException);
1764 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetAlgoState()" );
1766 if ( CORBA::is_nil( theSubObject ) && theMesh->HasShapeToMesh())
1767 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
1769 if ( CORBA::is_nil( theMesh ) )
1770 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1772 SMESH::algo_error_array_var error_array = new SMESH::algo_error_array;
1774 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
1775 ASSERT( meshServant );
1776 if ( meshServant ) {
1777 TopoDS_Shape myLocShape;
1778 if(theMesh->HasShapeToMesh())
1779 myLocShape = GeomObjectToShape( theSubObject );
1781 myLocShape = SMESH_Mesh::PseudoShape();
1783 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1784 list< ::SMESH_Gen::TAlgoStateError > error_list;
1785 list< ::SMESH_Gen::TAlgoStateError >::iterator error;
1786 // call ::SMESH_Gen::GetAlgoState()
1787 myGen.GetAlgoState( myLocMesh, myLocShape, error_list );
1788 error_array->length( error_list.size() );
1790 for ( error = error_list.begin(); error != error_list.end(); ++error )
1792 // fill AlgoStateError structure
1793 SMESH::AlgoStateError & errStruct = error_array[ i++ ];
1794 errStruct.state = SMESH_Mesh_i::ConvertHypothesisStatus( error->_name );
1795 errStruct.algoDim = error->_algoDim;
1796 errStruct.isGlobalAlgo = error->_isGlobalAlgo;
1797 errStruct.algoName = "";
1798 SALOMEDS::SObject_wrap algoSO = GetAlgoSO( error->_algo );
1799 if ( !algoSO->_is_nil() ) {
1800 CORBA::String_var algoName = algoSO->GetName();
1801 errStruct.algoName = algoName.in();
1806 catch ( SALOME_Exception& S_ex ) {
1807 INFOS( "catch exception "<< S_ex.what() );
1809 return error_array._retn();
1812 //=============================================================================
1814 * SMESH_Gen_i::GetSubShapesId
1816 * Get sub-shapes unique ID's list
1818 //=============================================================================
1821 SMESH_Gen_i::GetSubShapesId( GEOM::GEOM_Object_ptr theMainShapeObject,
1822 const SMESH::object_array& theListOfSubShapeObject )
1823 throw ( SALOME::SALOME_Exception )
1825 Unexpect aCatch(SALOME_SalomeException);
1826 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetSubShapesId" );
1828 SMESH::long_array_var shapesId = new SMESH::long_array;
1831 if ( CORBA::is_nil( theMainShapeObject ) )
1832 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
1836 TopoDS_Shape myMainShape = GeomObjectToShape(theMainShapeObject);
1837 TopTools_IndexedMapOfShape myIndexToShape;
1838 TopExp::MapShapes(myMainShape,myIndexToShape);
1840 for ( CORBA::ULong i = 0; i < theListOfSubShapeObject.length(); i++ )
1842 GEOM::GEOM_Object_var aShapeObject
1843 = GEOM::GEOM_Object::_narrow(theListOfSubShapeObject[i]);
1844 if ( CORBA::is_nil( aShapeObject ) )
1845 THROW_SALOME_CORBA_EXCEPTION ("bad shape object reference", \
1846 SALOME::BAD_PARAM );
1848 TopoDS_Shape locShape = GeomObjectToShape(aShapeObject);
1849 for (TopExp_Explorer exp(locShape,TopAbs_FACE); exp.More(); exp.Next())
1851 const TopoDS_Face& F = TopoDS::Face(exp.Current());
1852 setId.insert(myIndexToShape.FindIndex(F));
1853 if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(F));
1855 for (TopExp_Explorer exp(locShape,TopAbs_EDGE); exp.More(); exp.Next())
1857 const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
1858 setId.insert(myIndexToShape.FindIndex(E));
1859 if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(E));
1861 for (TopExp_Explorer exp(locShape,TopAbs_VERTEX); exp.More(); exp.Next())
1863 const TopoDS_Vertex& V = TopoDS::Vertex(exp.Current());
1864 setId.insert(myIndexToShape.FindIndex(V));
1865 if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(V));
1868 shapesId->length(setId.size());
1869 set<int>::iterator iind;
1871 for (iind = setId.begin(); iind != setId.end(); iind++)
1873 if(MYDEBUG) SCRUTE((*iind));
1874 shapesId[i] = (*iind);
1875 if(MYDEBUG) SCRUTE(shapesId[i]);
1879 catch (SALOME_Exception& S_ex)
1881 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
1884 return shapesId._retn();
1887 //=============================================================================
1889 * SMESH_Gen_i::Compute
1891 * Compute mesh on a shape
1893 //=============================================================================
1895 CORBA::Boolean SMESH_Gen_i::Compute( SMESH::SMESH_Mesh_ptr theMesh,
1896 GEOM::GEOM_Object_ptr theShapeObject )
1897 throw ( SALOME::SALOME_Exception )
1900 Unexpect aCatch(SALOME_SalomeException);
1901 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Compute" );
1903 if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
1904 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1905 SALOME::BAD_PARAM );
1907 if ( CORBA::is_nil( theMesh ) )
1908 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
1909 SALOME::BAD_PARAM );
1911 // Update Python script
1912 TPythonDump() << "isDone = " << this << ".Compute( "
1913 << theMesh << ", " << theShapeObject << ")";
1917 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
1918 ASSERT( meshServant );
1919 if ( meshServant ) {
1920 meshServant->Load();
1921 // NPAL16168: "geometrical group edition from a submesh don't modifiy mesh computation"
1922 meshServant->CheckGeomModif();
1923 // get local TopoDS_Shape
1924 TopoDS_Shape myLocShape;
1925 if(theMesh->HasShapeToMesh())
1926 myLocShape = GeomObjectToShape( theShapeObject );
1928 myLocShape = SMESH_Mesh::PseudoShape();
1929 // call implementation compute
1930 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1931 myGen.PrepareCompute( myLocMesh, myLocShape );
1932 int how = ::SMESH_Gen::COMPACT_MESH;
1933 if ( myLocShape != myLocMesh.GetShapeToMesh() ) // compute a sub-mesh
1934 how |= ::SMESH_Gen::SHAPE_ONLY;
1935 bool ok = myGen.Compute( myLocMesh, myLocShape, how );
1936 meshServant->CreateGroupServants(); // algos can create groups (issue 0020918)
1937 myLocMesh.GetMeshDS()->Modified();
1941 catch ( std::bad_alloc ) {
1942 INFOS( "Compute(): lack of memory" );
1944 catch ( SALOME_Exception& S_ex ) {
1945 INFOS( "Compute(): catch exception "<< S_ex.what() );
1948 INFOS( "Compute(): unknown exception " );
1953 //=============================================================================
1955 * SMESH_Gen_i::CancelCompute
1957 * Cancel Compute mesh on a shape
1959 //=============================================================================
1961 void SMESH_Gen_i::CancelCompute( SMESH::SMESH_Mesh_ptr theMesh,
1962 GEOM::GEOM_Object_ptr theShapeObject )
1964 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
1965 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1966 TopoDS_Shape myLocShape;
1967 if(theMesh->HasShapeToMesh())
1968 myLocShape = GeomObjectToShape( theShapeObject );
1970 myLocShape = SMESH_Mesh::PseudoShape();
1971 myGen.CancelCompute( myLocMesh, myLocShape);
1974 //=============================================================================
1976 * SMESH_Gen_i::Precompute
1978 * Compute mesh as preview till indicated dimension on shape
1980 //=============================================================================
1982 SMESH::MeshPreviewStruct* SMESH_Gen_i::Precompute( SMESH::SMESH_Mesh_ptr theMesh,
1983 GEOM::GEOM_Object_ptr theShapeObject,
1984 SMESH::Dimension theDimension,
1985 SMESH::long_array& theShapesId)
1986 throw ( SALOME::SALOME_Exception )
1988 Unexpect aCatch(SALOME_SalomeException);
1989 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Precompute" );
1991 if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
1992 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1993 SALOME::BAD_PARAM );
1995 if ( CORBA::is_nil( theMesh ) )
1996 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
1997 SALOME::BAD_PARAM );
1999 SMESH::MeshPreviewStruct_var result = new SMESH::MeshPreviewStruct;
2002 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
2003 meshServant->Load();
2004 ASSERT( meshServant );
2005 if ( meshServant ) {
2006 // NPAL16168: "geometrical group edition from a submesh don't modifiy mesh computation"
2007 meshServant->CheckGeomModif();
2008 // get local TopoDS_Shape
2009 TopoDS_Shape myLocShape;
2010 if(theMesh->HasShapeToMesh())
2011 myLocShape = GeomObjectToShape( theShapeObject );
2013 return result._retn();;
2015 // call implementation compute
2016 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
2018 ::MeshDimension aDim = (MeshDimension)theDimension;
2019 if ( myGen.Compute( myLocMesh, myLocShape, ::SMESH_Gen::COMPACT_MESH, aDim, &shapeIds ) )
2021 int nbShapeId = shapeIds.size();
2022 theShapesId.length( nbShapeId );
2023 // iterates on shapes and collect mesh entities into mesh preview
2024 TSetOfInt::const_iterator idIt = shapeIds.begin();
2025 TSetOfInt::const_iterator idEnd = shapeIds.end();
2026 std::map< int, int > mapOfShIdNb;
2027 std::set< SMESH_TLink > setOfEdge;
2028 std::list< SMDSAbs_ElementType > listOfElemType;
2029 typedef map<const SMDS_MeshElement*, int > TNode2LocalIDMap;
2030 typedef TNode2LocalIDMap::iterator TNodeLocalID;
2031 TNode2LocalIDMap mapNode2LocalID;
2032 list< TNodeLocalID > connectivity;
2033 int i, nbConnNodes = 0;
2034 std::set< const SMESH_subMesh* > setOfVSubMesh;
2035 // iterates on shapes
2036 for ( ; idIt != idEnd; idIt++ )
2038 if ( mapOfShIdNb.find( *idIt ) != mapOfShIdNb.end() )
2040 SMESH_subMesh* sm = myLocMesh.GetSubMeshContaining(*idIt);
2041 if ( !sm || !sm->IsMeshComputed() )
2044 const TopoDS_Shape& aSh = sm->GetSubShape();
2045 const int shDim = myGen.GetShapeDim( aSh );
2046 if ( shDim < 1 || shDim > theDimension )
2049 mapOfShIdNb[ *idIt ] = 0;
2050 theShapesId[ mapOfShIdNb.size() - 1 ] = *idIt;
2052 SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
2053 if ( !smDS ) continue;
2055 if ( theDimension == SMESH::DIM_2D )
2057 SMDS_ElemIteratorPtr faceIt = smDS->GetElements();
2058 while ( faceIt->more() )
2060 const SMDS_MeshElement* face = faceIt->next();
2061 int aNbNode = face->NbNodes();
2063 aNbNode /= 2; // do not take into account additional middle nodes
2065 SMDS_MeshNode* node1 = (SMDS_MeshNode*)face->GetNode( 0 );
2066 for ( int nIndx = 0; nIndx < aNbNode; nIndx++ )
2068 SMDS_MeshNode* node2 = (SMDS_MeshNode*)face->GetNode( nIndx+1 < aNbNode ? nIndx+1 : 0 );
2069 if ( setOfEdge.insert( SMESH_TLink ( node1, node2 ) ).second )
2071 listOfElemType.push_back( SMDSAbs_Edge );
2072 connectivity.push_back
2073 ( mapNode2LocalID.insert( make_pair( node1, ++nbConnNodes)).first );
2074 connectivity.push_back
2075 ( mapNode2LocalID.insert( make_pair( node2, ++nbConnNodes)).first );
2081 else if ( theDimension == SMESH::DIM_1D )
2083 SMDS_NodeIteratorPtr nodeIt = smDS->GetNodes();
2084 while ( nodeIt->more() )
2086 listOfElemType.push_back( SMDSAbs_Node );
2087 connectivity.push_back
2088 ( mapNode2LocalID.insert( make_pair( nodeIt->next(), ++nbConnNodes)).first );
2090 // add corner nodes by first vertex from edge
2091 SMESH_subMeshIteratorPtr edgeSmIt =
2092 sm->getDependsOnIterator(/*includeSelf*/false,
2093 /*complexShapeFirst*/false);
2094 while ( edgeSmIt->more() )
2096 SMESH_subMesh* vertexSM = edgeSmIt->next();
2097 // check that vertex is not already treated
2098 if ( !setOfVSubMesh.insert( vertexSM ).second )
2100 if ( vertexSM->GetSubShape().ShapeType() != TopAbs_VERTEX )
2103 const SMESHDS_SubMesh* vertexSmDS = vertexSM->GetSubMeshDS();
2104 SMDS_NodeIteratorPtr nodeIt = vertexSmDS->GetNodes();
2105 while ( nodeIt->more() )
2107 listOfElemType.push_back( SMDSAbs_Node );
2108 connectivity.push_back
2109 ( mapNode2LocalID.insert( make_pair( nodeIt->next(), ++nbConnNodes)).first );
2115 // fill node coords and assign local ids to the nodes
2116 int nbNodes = mapNode2LocalID.size();
2117 result->nodesXYZ.length( nbNodes );
2118 TNodeLocalID node2ID = mapNode2LocalID.begin();
2119 for ( i = 0; i < nbNodes; ++i, ++node2ID ) {
2120 node2ID->second = i;
2121 const SMDS_MeshNode* node = (const SMDS_MeshNode*) node2ID->first;
2122 result->nodesXYZ[i].x = node->X();
2123 result->nodesXYZ[i].y = node->Y();
2124 result->nodesXYZ[i].z = node->Z();
2126 // fill connectivity
2127 result->elementConnectivities.length( nbConnNodes );
2128 list< TNodeLocalID >::iterator connIt = connectivity.begin();
2129 for ( i = 0; i < nbConnNodes; ++i, ++connIt ) {
2130 result->elementConnectivities[i] = (*connIt)->second;
2133 // fill element types
2134 result->elementTypes.length( listOfElemType.size() );
2135 std::list< SMDSAbs_ElementType >::const_iterator typeIt = listOfElemType.begin();
2136 std::list< SMDSAbs_ElementType >::const_iterator typeEnd = listOfElemType.end();
2137 for ( i = 0; typeIt != typeEnd; ++i, ++typeIt )
2139 SMDSAbs_ElementType elemType = *typeIt;
2140 result->elementTypes[i].SMDS_ElementType = (SMESH::ElementType)elemType;
2141 result->elementTypes[i].isPoly = false;
2142 result->elementTypes[i].nbNodesInElement = elemType == SMDSAbs_Edge ? 2 : 1;
2145 // correct number of shapes
2146 theShapesId.length( mapOfShIdNb.size() );
2150 catch ( std::bad_alloc ) {
2151 INFOS( "Precompute(): lack of memory" );
2153 catch ( SALOME_Exception& S_ex ) {
2154 INFOS( "Precompute(): catch exception "<< S_ex.what() );
2157 INFOS( "Precompute(): unknown exception " );
2159 return result._retn();
2163 //=============================================================================
2165 * SMESH_Gen_i::Evaluate
2167 * Evaluate mesh on a shape
2169 //=============================================================================
2171 SMESH::long_array* SMESH_Gen_i::Evaluate(SMESH::SMESH_Mesh_ptr theMesh,
2172 GEOM::GEOM_Object_ptr theShapeObject)
2173 // SMESH::long_array& theNbElems)
2174 throw ( SALOME::SALOME_Exception )
2176 Unexpect aCatch(SALOME_SalomeException);
2177 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Evaluate" );
2179 if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
2180 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
2181 SALOME::BAD_PARAM );
2183 if ( CORBA::is_nil( theMesh ) )
2184 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
2185 SALOME::BAD_PARAM );
2187 SMESH::long_array_var nbels = new SMESH::long_array;
2188 nbels->length(SMESH::Entity_Last);
2189 int i = SMESH::Entity_Node;
2190 for (; i < SMESH::Entity_Last; i++)
2193 // Update Python script
2194 TPythonDump() << "theNbElems = " << this << ".Evaluate( "
2195 << theMesh << ", " << theShapeObject << ")";
2199 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
2200 ASSERT( meshServant );
2201 if ( meshServant ) {
2202 meshServant->Load();
2203 // NPAL16168: "geometrical group edition from a submesh don't modifiy mesh computation"
2204 meshServant->CheckGeomModif();
2205 // get local TopoDS_Shape
2206 TopoDS_Shape myLocShape;
2207 if(theMesh->HasShapeToMesh())
2208 myLocShape = GeomObjectToShape( theShapeObject );
2210 myLocShape = SMESH_Mesh::PseudoShape();
2211 // call implementation compute
2212 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
2213 MapShapeNbElems aResMap;
2214 /*CORBA::Boolean ret =*/ myGen.Evaluate( myLocMesh, myLocShape, aResMap);
2215 MapShapeNbElemsItr anIt = aResMap.begin();
2216 for(; anIt!=aResMap.end(); anIt++) {
2217 const vector<int>& aVec = (*anIt).second;
2218 for ( i = SMESH::Entity_Node; i < (int)aVec.size(); i++ ) {
2219 int nbElem = aVec[i];
2220 if ( nbElem < 0 ) // algo failed, check that it has reported a message
2222 SMESH_subMesh* sm = anIt->first;
2223 SMESH_ComputeErrorPtr& error = sm->GetComputeError();
2224 const SMESH_Algo* algo = sm->GetAlgo();
2225 if ( (algo && !error.get()) || error->IsOK() )
2226 error.reset( new SMESH_ComputeError( COMPERR_ALGO_FAILED,"Failed to evaluate",algo));
2230 nbels[i] += aVec[i];
2234 return nbels._retn();
2237 catch ( std::bad_alloc ) {
2238 INFOS( "Evaluate(): lack of memory" );
2240 catch ( SALOME_Exception& S_ex ) {
2241 INFOS( "Evaluate(): catch exception "<< S_ex.what() );
2244 INFOS( "Evaluate(): unknown exception " );
2247 return nbels._retn();
2250 //================================================================================
2252 * \brief Return geometrical object the given element is built on
2253 * \param theMesh - the mesh the element is in
2254 * \param theElementID - the element ID
2255 * \param theGeomName - the name of the result geom object if it is not yet published
2256 * \retval GEOM::GEOM_Object_ptr - the found or just published geom object (no need to UnRegister())
2258 //================================================================================
2260 GEOM::GEOM_Object_ptr
2261 SMESH_Gen_i::GetGeometryByMeshElement( SMESH::SMESH_Mesh_ptr theMesh,
2262 CORBA::Long theElementID,
2263 const char* theGeomName)
2264 throw ( SALOME::SALOME_Exception )
2266 Unexpect aCatch(SALOME_SalomeException);
2268 GEOM::GEOM_Object_wrap geom = FindGeometryByMeshElement(theMesh, theElementID);
2269 if ( !geom->_is_nil() ) {
2270 GEOM::GEOM_Object_var mainShape = theMesh->GetShapeToMesh();
2271 GEOM::GEOM_Gen_ptr geomGen = GetGeomEngine();
2273 // try to find the corresponding SObject
2274 SALOMEDS::SObject_wrap SObj = ObjectToSObject( myCurrentStudy, geom.in() );
2275 if ( SObj->_is_nil() ) // submesh can be not found even if published
2277 // try to find published submesh
2278 GEOM::ListOfLong_var list = geom->GetSubShapeIndices();
2279 if ( !geom->IsMainShape() && list->length() == 1 ) {
2280 SALOMEDS::SObject_wrap mainSO = ObjectToSObject( myCurrentStudy, mainShape );
2281 SALOMEDS::ChildIterator_wrap it;
2282 if ( !mainSO->_is_nil() ) {
2283 it = myCurrentStudy->NewChildIterator( mainSO );
2285 if ( !it->_is_nil() ) {
2286 for ( it->InitEx(true); it->More(); it->Next() ) {
2287 SALOMEDS::SObject_wrap so = it->Value();
2288 CORBA::Object_var obj = SObjectToObject( so );
2289 GEOM::GEOM_Object_var subGeom = GEOM::GEOM_Object::_narrow( obj );
2290 if ( !subGeom->_is_nil() ) {
2291 GEOM::ListOfLong_var subList = subGeom->GetSubShapeIndices();
2292 if ( subList->length() == 1 && list[0] == subList[0] ) {
2302 if ( SObj->_is_nil() ) // publish a new subshape
2303 SObj = geomGen->AddInStudy( myCurrentStudy, geom, theGeomName, mainShape );
2305 // return only published geometry
2306 if ( !SObj->_is_nil() ) {
2307 //return geom._retn(); -- servant of geom must be UnRegister()ed;
2308 CORBA::Object_var obj = SObjectToObject( SObj );
2309 GEOM::GEOM_Object_var go = GEOM::GEOM_Object::_narrow( obj );
2313 return GEOM::GEOM_Object::_nil();
2316 //================================================================================
2318 * \brief Return geometrical object the given element is built on.
2319 * \param theMesh - the mesh the element is in
2320 * \param theElementID - the element ID
2321 * \retval GEOM::GEOM_Object_ptr - the found or created (UnRegister()!) geom object
2323 //================================================================================
2325 GEOM::GEOM_Object_ptr
2326 SMESH_Gen_i::FindGeometryByMeshElement( SMESH::SMESH_Mesh_ptr theMesh,
2327 CORBA::Long theElementID)
2328 throw ( SALOME::SALOME_Exception )
2330 Unexpect aCatch(SALOME_SalomeException);
2331 if ( CORBA::is_nil( theMesh ) )
2332 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference", SALOME::BAD_PARAM );
2334 GEOM::GEOM_Object_var mainShape = theMesh->GetShapeToMesh();
2335 GEOM::GEOM_Gen_ptr geomGen = GetGeomEngine();
2337 // get a core mesh DS
2338 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
2339 if ( meshServant && !geomGen->_is_nil() && !mainShape->_is_nil() )
2341 ::SMESH_Mesh & mesh = meshServant->GetImpl();
2342 SMESHDS_Mesh* meshDS = mesh.GetMeshDS();
2343 // find the element in mesh
2344 if ( const SMDS_MeshElement * elem = meshDS->FindElement( theElementID ) ) {
2345 // find a shape id by the element
2346 if ( int shapeID = ::SMESH_MeshEditor( &mesh ).FindShape( elem )) {
2347 // get a geom object by the shape id
2348 GEOM::GEOM_Object_var geom = ShapeToGeomObject( meshDS->IndexToShape( shapeID ));
2349 if ( geom->_is_nil() ) {
2350 // try to find a published sub-shape
2351 SALOMEDS::SObject_wrap mainSO = ObjectToSObject( myCurrentStudy, mainShape );
2352 SALOMEDS::ChildIterator_wrap it;
2353 if ( !mainSO->_is_nil() ) {
2354 it = myCurrentStudy->NewChildIterator( mainSO );
2356 if ( !it->_is_nil() ) {
2357 for ( it->InitEx(true); it->More(); it->Next() ) {
2358 SALOMEDS::SObject_wrap so = it->Value();
2359 CORBA::Object_var obj = SObjectToObject( so );
2360 GEOM::GEOM_Object_var subGeom = GEOM::GEOM_Object::_narrow( obj );
2361 if ( !subGeom->_is_nil() ) {
2362 GEOM::ListOfLong_var subList = subGeom->GetSubShapeIndices();
2363 if ( subList->length() == 1 && shapeID == subList[0] ) {
2371 if ( geom->_is_nil() ) {
2373 GEOM::GEOM_IShapesOperations_wrap op =
2374 geomGen->GetIShapesOperations( GetCurrentStudyID() );
2375 if ( !op->_is_nil() )
2376 geom = op->GetSubShape( mainShape, shapeID );
2381 if ( !geom->_is_nil() ) {
2382 GeomObjectToShape( geom ); // let geom client remember the found shape
2383 return geom._retn();
2388 return GEOM::GEOM_Object::_nil();
2391 //================================================================================
2393 * SMESH_Gen_i::Concatenate
2395 * Concatenate the given meshes into one mesh
2397 //================================================================================
2399 SMESH::SMESH_Mesh_ptr
2400 SMESH_Gen_i::Concatenate(const SMESH::ListOfIDSources& theMeshesArray,
2401 CORBA::Boolean theUniteIdenticalGroups,
2402 CORBA::Boolean theMergeNodesAndElements,
2403 CORBA::Double theMergeTolerance)
2404 throw ( SALOME::SALOME_Exception )
2406 return ConcatenateCommon(theMeshesArray,
2407 theUniteIdenticalGroups,
2408 theMergeNodesAndElements,
2413 //================================================================================
2415 * SMESH_Gen_i::ConcatenateWithGroups
2417 * Concatenate the given meshes into one mesh
2418 * Create the groups of all elements from initial meshes
2420 //================================================================================
2422 SMESH::SMESH_Mesh_ptr
2423 SMESH_Gen_i::ConcatenateWithGroups(const SMESH::ListOfIDSources& theMeshesArray,
2424 CORBA::Boolean theUniteIdenticalGroups,
2425 CORBA::Boolean theMergeNodesAndElements,
2426 CORBA::Double theMergeTolerance)
2427 throw ( SALOME::SALOME_Exception )
2429 return ConcatenateCommon(theMeshesArray,
2430 theUniteIdenticalGroups,
2431 theMergeNodesAndElements,
2436 //================================================================================
2438 * SMESH_Gen_i::ConcatenateCommon
2440 * Concatenate the given meshes into one mesh
2442 //================================================================================
2444 SMESH::SMESH_Mesh_ptr
2445 SMESH_Gen_i::ConcatenateCommon(const SMESH::ListOfIDSources& theMeshesArray,
2446 CORBA::Boolean theUniteIdenticalGroups,
2447 CORBA::Boolean theMergeNodesAndElements,
2448 CORBA::Double theMergeTolerance,
2449 CORBA::Boolean theCommonGroups)
2450 throw ( SALOME::SALOME_Exception )
2452 typedef list<SMESH::SMESH_Group_var> TListOfNewGroups;
2453 typedef map< pair<string, SMESH::ElementType>, TListOfNewGroups > TGroupsMap;
2455 TPythonDump* pPythonDump = new TPythonDump;
2456 TPythonDump& aPythonDump = *pPythonDump; // prevent dump of called methods
2459 SMESH::SMESH_Mesh_var aNewMesh = CreateEmptyMesh();
2461 if ( aNewMesh->_is_nil() )
2462 return aNewMesh._retn();
2464 SMESH_Mesh_i* aNewImpl = SMESH::DownCast<SMESH_Mesh_i*>( aNewMesh );
2466 return aNewMesh._retn();
2468 ::SMESH_Mesh& aLocMesh = aNewImpl->GetImpl();
2469 SMESHDS_Mesh* aNewMeshDS = aLocMesh.GetMeshDS();
2471 TGroupsMap aGroupsMap;
2472 TListOfNewGroups aListOfNewGroups;
2473 ::SMESH_MeshEditor aNewEditor(&aLocMesh);
2474 SMESH::ListOfGroups_var aListOfGroups;
2476 ::SMESH_MeshEditor::ElemFeatures elemType;
2477 std::vector<const SMDS_MeshNode*> aNodesArray;
2479 // loop on sub-meshes
2480 for ( CORBA::ULong i = 0; i < theMeshesArray.length(); i++)
2482 if ( CORBA::is_nil( theMeshesArray[i] )) continue;
2483 SMESH::SMESH_Mesh_var anInitMesh = theMeshesArray[i]->GetMesh();
2484 if ( anInitMesh->_is_nil() ) continue;
2485 SMESH_Mesh_i* anInitImpl = SMESH::DownCast<SMESH_Mesh_i*>( anInitMesh );
2486 if ( !anInitImpl ) continue;
2489 //::SMESH_Mesh& aInitLocMesh = anInitImpl->GetImpl();
2490 //SMESHDS_Mesh* anInitMeshDS = aInitLocMesh.GetMeshDS();
2492 // remember nb of elements before filling in
2493 SMESH::long_array_var prevState = aNewMesh->GetNbElementsByType();
2495 typedef std::map<const SMDS_MeshElement*, const SMDS_MeshElement*, TIDCompare > TEEMap;
2496 TEEMap elemsMap, nodesMap;
2498 // loop on elements of a sub-mesh
2499 SMDS_ElemIteratorPtr itElems = anInitImpl->GetElements( theMeshesArray[i], SMESH::ALL );
2500 const SMDS_MeshElement* anElem;
2501 const SMDS_MeshElement* aNewElem;
2502 const SMDS_MeshNode* aNode;
2503 const SMDS_MeshNode* aNewNode;
2506 while ( itElems->more() )
2508 anElem = itElems->next();
2509 anElemNbNodes = anElem->NbNodes();
2510 aNodesArray.resize( anElemNbNodes );
2512 // loop on nodes of an element
2513 SMDS_ElemIteratorPtr itNodes = anElem->nodesIterator();
2514 for ( int k = 0; itNodes->more(); k++)
2516 aNode = static_cast<const SMDS_MeshNode*>( itNodes->next() );
2517 TEEMap::iterator n2nnIt = nodesMap.find( aNode );
2518 if ( n2nnIt == nodesMap.end() )
2520 aNewNode = aNewMeshDS->AddNode(aNode->X(), aNode->Y(), aNode->Z());
2521 nodesMap.insert( make_pair( aNode, aNewNode ));
2525 aNewNode = static_cast<const SMDS_MeshNode*>( n2nnIt->second );
2527 aNodesArray[k] = aNewNode;
2530 // creates a corresponding element on existent nodes in new mesh
2531 if ( anElem->GetType() == SMDSAbs_Node )
2535 aNewEditor.AddElement( aNodesArray, elemType.Init( anElem, /*basicOnly=*/false ));
2538 elemsMap.insert( make_pair( anElem, aNewElem ));
2542 aNewEditor.ClearLastCreated(); // forget the history
2545 // create groups of just added elements
2546 SMESH::SMESH_Group_var aNewGroup;
2547 SMESH::ElementType aGroupType;
2548 if ( theCommonGroups )
2550 SMESH::long_array_var curState = aNewMesh->GetNbElementsByType();
2552 for( aGroupType = SMESH::NODE;
2553 aGroupType < SMESH::NB_ELEMENT_TYPES;
2554 aGroupType = (SMESH::ElementType)( aGroupType + 1 ))
2556 if ( curState[ aGroupType ] <= prevState[ aGroupType ])
2559 // make a group name
2560 const char* typeNames[] = { "All","Nodes","Edges","Faces","Volumes","0DElems","Balls" };
2561 { // check of typeNames: compilation failure mains that NB_ELEMENT_TYPES changed:
2562 const int nbNames = sizeof(typeNames) / sizeof(const char*);
2563 int _assert[( nbNames == SMESH::NB_ELEMENT_TYPES ) ? 2 : -1 ]; _assert[0]=_assert[1];
2565 string groupName = "Gr";
2566 SALOMEDS::SObject_wrap aMeshSObj = ObjectToSObject( myCurrentStudy, theMeshesArray[i] );
2568 CORBA::String_var name = aMeshSObj->GetName();
2572 groupName += typeNames[ aGroupType ];
2574 // make and fill a group
2575 TEEMap & e2neMap = ( aGroupType == SMESH::NODE ) ? nodesMap : elemsMap;
2576 aNewGroup = aNewImpl->CreateGroup( aGroupType, groupName.c_str() );
2577 if ( SMESH_Group_i* grp_i = SMESH::DownCast<SMESH_Group_i*>( aNewGroup ))
2579 if ( SMESHDS_Group* grpDS = dynamic_cast<SMESHDS_Group*>( grp_i->GetGroupDS() ))
2581 TEEMap::iterator e2neIt = e2neMap.begin();
2582 for ( ; e2neIt != e2neMap.end(); ++e2neIt )
2584 aNewElem = e2neIt->second;
2585 if ( aNewElem->GetType() == grpDS->GetType() )
2587 grpDS->Add( aNewElem );
2589 if ( prevState[ aGroupType ]++ >= curState[ aGroupType ] )
2595 aListOfNewGroups.clear();
2596 aListOfNewGroups.push_back(aNewGroup);
2597 aGroupsMap.insert(make_pair( make_pair(groupName, aGroupType), aListOfNewGroups ));
2601 if ( SMESH_Mesh_i* anSrcImpl = SMESH::DownCast<SMESH_Mesh_i*>( theMeshesArray[i] ))
2603 // copy orphan nodes
2604 if ( anSrcImpl->NbNodes() > (int)nodesMap.size() )
2606 SMDS_ElemIteratorPtr itNodes = anInitImpl->GetElements( theMeshesArray[i], SMESH::NODE );
2607 while ( itNodes->more() )
2609 const SMDS_MeshNode* aNode = static_cast< const SMDS_MeshNode* >( itNodes->next() );
2610 if ( aNode->NbInverseElements() == 0 )
2612 aNewNode = aNewMeshDS->AddNode(aNode->X(), aNode->Y(), aNode->Z());
2613 nodesMap.insert( make_pair( aNode, aNewNode ));
2620 SMESH::SMESH_GroupBase_ptr aGroup;
2621 CORBA::String_var aGroupName;
2622 SMESH::long_array_var anNewIDs = new SMESH::long_array();
2624 // loop on groups of a source mesh
2625 aListOfGroups = anSrcImpl->GetGroups();
2626 for ( CORBA::ULong iG = 0; iG < aListOfGroups->length(); iG++ )
2628 aGroup = aListOfGroups[iG];
2629 aGroupType = aGroup->GetType();
2630 aGroupName = aGroup->GetName();
2631 string aName = aGroupName.in();
2633 // convert a list of IDs
2634 anNewIDs->length( aGroup->Size() );
2635 TEEMap & e2neMap = ( aGroupType == SMESH::NODE ) ? nodesMap : elemsMap;
2636 SMDS_ElemIteratorPtr itGrElems = anSrcImpl->GetElements( aGroup, SMESH::ALL );
2638 while ( itGrElems->more() )
2640 anElem = itGrElems->next();
2641 TEEMap::iterator e2neIt = e2neMap.find( anElem );
2642 if ( e2neIt != e2neMap.end() )
2643 anNewIDs[ iElem++ ] = e2neIt->second->GetID();
2645 anNewIDs->length( iElem );
2647 // check a current group name and type don't have identical ones in final mesh
2648 aListOfNewGroups.clear();
2649 TGroupsMap::iterator anIter = aGroupsMap.find( make_pair( aName, aGroupType ));
2650 if ( anIter == aGroupsMap.end() ) {
2651 // add a new group in the mesh
2652 aNewGroup = aNewImpl->CreateGroup( aGroupType, aGroupName.in() );
2653 // add elements into new group
2654 aNewGroup->Add( anNewIDs );
2656 aListOfNewGroups.push_back(aNewGroup);
2657 aGroupsMap.insert(make_pair( make_pair(aName, aGroupType), aListOfNewGroups ));
2660 else if ( theUniteIdenticalGroups ) {
2661 // unite identical groups
2662 TListOfNewGroups& aNewGroups = anIter->second;
2663 aNewGroups.front()->Add( anNewIDs );
2667 // rename identical groups
2668 aNewGroup = aNewImpl->CreateGroup(aGroupType, aGroupName.in());
2669 aNewGroup->Add( anNewIDs );
2671 TListOfNewGroups& aNewGroups = anIter->second;
2672 string aNewGroupName;
2673 if (aNewGroups.size() == 1) {
2674 aNewGroupName = aName + "_1";
2675 aNewGroups.front()->SetName(aNewGroupName.c_str());
2677 char aGroupNum[128];
2678 sprintf(aGroupNum, "%u", (unsigned int)aNewGroups.size()+1);
2679 aNewGroupName = aName + "_" + string(aGroupNum);
2680 aNewGroup->SetName(aNewGroupName.c_str());
2681 aNewGroups.push_back(aNewGroup);
2684 } // if an IDSource is a mesh
2687 if (theMergeNodesAndElements) // merge nodes
2689 TIDSortedNodeSet aMeshNodes; // no input nodes
2690 SMESH_MeshEditor::TListOfListOfNodes aGroupsOfNodes;
2691 aNewEditor.FindCoincidentNodes( aMeshNodes, theMergeTolerance, aGroupsOfNodes,
2692 /*SeparateCornersAndMedium=*/ false );
2693 aNewEditor.MergeNodes( aGroupsOfNodes );
2695 aNewEditor.MergeEqualElements();
2698 // Update Python script
2699 aPythonDump << aNewMesh << " = " << this << "."
2700 << ( theCommonGroups ? "ConcatenateWithGroups" : "Concatenate" )
2702 for ( CORBA::ULong i = 0; i < theMeshesArray.length(); i++) {
2703 if (i > 0) aPythonDump << ", ";
2704 aPythonDump << theMeshesArray[i];
2706 aPythonDump << "], ";
2707 aPythonDump << theUniteIdenticalGroups << ", "
2708 << theMergeNodesAndElements << ", "
2709 << TVar( theMergeTolerance ) << ")";
2711 delete pPythonDump; // enable python dump from GetGroups()
2713 // 0020577: EDF 1164 SMESH: Bad dump of concatenate with create common groups
2714 if ( !aNewMesh->_is_nil() )
2716 SMESH::ListOfGroups_var groups = aNewMesh->GetGroups();
2719 // IPAL21468 Change icon of compound because it need not be computed.
2720 SALOMEDS::SObject_wrap aMeshSObj = ObjectToSObject( myCurrentStudy, aNewMesh );
2721 SetPixMap( aMeshSObj, "ICON_SMESH_TREE_MESH" );
2724 aNewMeshDS->Modified();
2726 return aNewMesh._retn();
2729 //================================================================================
2731 * \brief Create a mesh by copying a part of another mesh
2732 * \param meshPart - a part of mesh to copy
2733 * \param toCopyGroups - to create in the new mesh groups
2734 * the copied elements belongs to
2735 * \param toKeepIDs - to preserve IDs of the copied elements or not
2736 * \retval SMESH::SMESH_Mesh_ptr - the new mesh
2738 //================================================================================
2740 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CopyMesh(SMESH::SMESH_IDSource_ptr meshPart,
2741 const char* meshName,
2742 CORBA::Boolean toCopyGroups,
2743 CORBA::Boolean toKeepIDs)
2745 Unexpect aCatch(SALOME_SalomeException);
2747 TPythonDump* pyDump = new TPythonDump; // prevent dump from CreateMesh()
2749 // 1. Get source mesh
2751 if ( CORBA::is_nil( meshPart ))
2752 THROW_SALOME_CORBA_EXCEPTION( "bad IDSource", SALOME::BAD_PARAM );
2754 SMESH::SMESH_Mesh_var srcMesh = meshPart->GetMesh();
2755 SMESH_Mesh_i* srcMesh_i = SMESH::DownCast<SMESH_Mesh_i*>( srcMesh );
2757 THROW_SALOME_CORBA_EXCEPTION( "bad mesh of IDSource", SALOME::BAD_PARAM );
2759 SMESHDS_Mesh* srcMeshDS = srcMesh_i->GetImpl().GetMeshDS();
2761 // 2. Make a new mesh
2763 SMESH::SMESH_Mesh_var newMesh = CreateMesh(GEOM::GEOM_Object::_nil());
2764 SMESH_Mesh_i* newMesh_i = SMESH::DownCast<SMESH_Mesh_i*>( newMesh );
2766 THROW_SALOME_CORBA_EXCEPTION( "can't create a mesh", SALOME::INTERNAL_ERROR );
2767 SALOMEDS::SObject_wrap meshSO = ObjectToSObject(myCurrentStudy, newMesh );
2768 if ( !meshSO->_is_nil() )
2770 SetName( meshSO, meshName, "Mesh" );
2771 SetPixMap( meshSO, "ICON_SMESH_TREE_MESH_IMPORTED");
2773 SMESHDS_Mesh* newMeshDS = newMesh_i->GetImpl().GetMeshDS();
2774 ::SMESH_MeshEditor editor( &newMesh_i->GetImpl() );
2775 ::SMESH_MeshEditor::ElemFeatures elemType;
2777 // 3. Get elements to copy
2779 SMDS_ElemIteratorPtr srcElemIt; SMDS_NodeIteratorPtr srcNodeIt;
2780 TIDSortedElemSet srcElems;
2781 SMESH::array_of_ElementType_var srcElemTypes = meshPart->GetTypes();
2782 if ( SMESH::DownCast<SMESH_Mesh_i*>( meshPart ))
2785 srcElemIt = srcMeshDS->elementsIterator();
2786 srcNodeIt = srcMeshDS->nodesIterator();
2790 SMESH::long_array_var ids = meshPart->GetIDs();
2791 if ( srcElemTypes->length() == 1 && srcElemTypes[0] == SMESH::NODE ) // group of nodes
2793 for ( CORBA::ULong i=0; i < ids->length(); i++ )
2794 if ( const SMDS_MeshElement * elem = srcMeshDS->FindNode( ids[i] ))
2795 srcElems.insert( elem );
2799 for ( CORBA::ULong i = 0; i < ids->length(); i++ )
2800 if ( const SMDS_MeshElement * elem = srcMeshDS->FindElement( ids[i] ))
2801 srcElems.insert( elem );
2803 if ( srcElems.empty() )
2804 return newMesh._retn();
2806 typedef SMDS_SetIterator< SMDS_pElement, TIDSortedElemSet::const_iterator > ElIter;
2807 srcElemIt = SMDS_ElemIteratorPtr( new ElIter( srcElems.begin(), srcElems.end() ));
2812 typedef map<SMDS_pElement, SMDS_pElement, TIDCompare> TE2EMap;
2813 TE2EMap e2eMapByType[ SMDSAbs_NbElementTypes ];
2814 TE2EMap& n2nMap = e2eMapByType[ SMDSAbs_Node ];
2816 const SMDS_MeshNode *nSrc, *nTgt;
2817 vector< const SMDS_MeshNode* > nodes;
2818 while ( srcElemIt->more() )
2820 const SMDS_MeshElement * elem = srcElemIt->next();
2822 nodes.resize( elem->NbNodes());
2823 SMDS_ElemIteratorPtr nIt = elem->nodesIterator();
2825 for ( iN = 0; nIt->more(); ++iN )
2827 nSrc = static_cast<const SMDS_MeshNode*>( nIt->next() );
2828 nTgt = newMeshDS->FindNode( nSrc->GetID());
2830 nTgt = newMeshDS->AddNodeWithID( nSrc->X(), nSrc->Y(), nSrc->Z(), nSrc->GetID());
2835 for ( iN = 0; nIt->more(); ++iN )
2837 nSrc = static_cast<const SMDS_MeshNode*>( nIt->next() );
2838 TE2EMap::iterator n2n = n2nMap.insert( make_pair( nSrc, SMDS_pNode(0) )).first;
2840 n2n->second = newMeshDS->AddNode( nSrc->X(), nSrc->Y(), nSrc->Z() );
2841 nodes[ iN ] = (const SMDS_MeshNode*) n2n->second;
2845 if ( elem->GetType() != SMDSAbs_Node )
2847 elemType.Init( elem, /*basicOnly=*/false );
2848 if ( toKeepIDs ) elemType.SetID( elem->GetID() );
2850 const SMDS_MeshElement * newElem = editor.AddElement( nodes, elemType );
2851 if ( toCopyGroups && !toKeepIDs )
2852 e2eMapByType[ elem->GetType() ].insert( make_pair( elem, newElem ));
2854 } // while ( srcElemIt->more() )
2856 // 4(b). Copy free nodes
2858 if ( srcNodeIt && srcMeshDS->NbNodes() != newMeshDS->NbNodes() )
2860 while ( srcNodeIt->more() )
2862 nSrc = srcNodeIt->next();
2863 if ( nSrc->NbInverseElements() == 0 )
2866 nTgt = newMeshDS->AddNodeWithID( nSrc->X(), nSrc->Y(), nSrc->Z(), nSrc->GetID());
2868 n2nMap[ nSrc ] = newMeshDS->AddNode( nSrc->X(), nSrc->Y(), nSrc->Z() );
2875 int nbNewGroups = 0;
2878 SMESH_Mesh::GroupIteratorPtr gIt = srcMesh_i->GetImpl().GetGroups();
2879 while ( gIt->more() )
2881 SMESH_Group* group = gIt->next();
2882 const SMESHDS_GroupBase* groupDS = group->GetGroupDS();
2884 // Check group type. We copy nodal groups containing nodes of copied element
2885 SMDSAbs_ElementType groupType = groupDS->GetType();
2886 if ( groupType != SMDSAbs_Node &&
2887 newMeshDS->GetMeshInfo().NbElements( groupType ) == 0 )
2888 continue; // group type differs from types of meshPart
2890 // Find copied elements in the group
2891 vector< const SMDS_MeshElement* > groupElems;
2892 SMDS_ElemIteratorPtr eIt = groupDS->GetElements();
2895 const SMDS_MeshElement* foundElem;
2896 if ( groupType == SMDSAbs_Node )
2898 while ( eIt->more() )
2899 if (( foundElem = newMeshDS->FindNode( eIt->next()->GetID() )))
2900 groupElems.push_back( foundElem );
2904 while ( eIt->more() )
2905 if (( foundElem = newMeshDS->FindElement( eIt->next()->GetID() )))
2906 groupElems.push_back( foundElem );
2911 TE2EMap & e2eMap = e2eMapByType[ groupDS->GetType() ];
2912 if ( e2eMap.empty() ) continue;
2913 int minID = e2eMap.begin()->first->GetID();
2914 int maxID = e2eMap.rbegin()->first->GetID();
2915 TE2EMap::iterator e2e;
2916 while ( eIt->more() && groupElems.size() < e2eMap.size())
2918 const SMDS_MeshElement* e = eIt->next();
2919 if ( e->GetID() < minID || e->GetID() > maxID ) continue;
2920 if ((e2e = e2eMap.find( e )) != e2eMap.end())
2921 groupElems.push_back( e2e->second );
2925 if ( !groupElems.empty() )
2927 SMESH::SMESH_Group_var newGroupObj =
2928 newMesh->CreateGroup( SMESH::ElementType(groupType), group->GetName() );
2929 if ( SMESH_GroupBase_i* newGroup_i = SMESH::DownCast<SMESH_GroupBase_i*>( newGroupObj))
2931 SMESHDS_GroupBase * newGroupDS = newGroup_i->GetGroupDS();
2932 SMDS_MeshGroup& smdsGroup = ((SMESHDS_Group*)newGroupDS)->SMDSGroup();
2933 for ( unsigned i = 0; i < groupElems.size(); ++i )
2934 smdsGroup.Add( groupElems[i] );
2942 newMeshDS->Modified();
2944 *pyDump << newMesh << " = " << this
2945 << ".CopyMesh( " << meshPart << ", "
2946 << "'" << meshName << "', "
2947 << toCopyGroups << ", "
2948 << toKeepIDs << ")";
2950 delete pyDump; pyDump = 0; // allow dump in GetGroups()
2952 if ( nbNewGroups > 0 ) // dump created groups
2953 SMESH::ListOfGroups_var groups = newMesh->GetGroups();
2955 return newMesh._retn();
2958 //================================================================================
2960 * SMESH_Gen_i::GetMEDVersion
2962 * Get MED version of the file by its name
2964 //================================================================================
2965 CORBA::Boolean SMESH_Gen_i::GetMEDVersion(const char* theFileName,
2966 SMESH::MED_VERSION& theVersion)
2968 theVersion = SMESH::MED_V2_2;
2970 int major, minor, release;
2971 bool result = MED::GetMEDVersion( theFileName, major, minor, release );
2973 int version = major * 100 + minor;
2974 if ( version < 202 ) theVersion = SMESH::MED_V2_1;
2975 else theVersion = SMESH::MED_V2_2;
2981 //================================================================================
2983 * SMESH_Gen_i::GetMeshNames
2985 * Get names of meshes defined in file with the specified name
2987 //================================================================================
2988 SMESH::string_array* SMESH_Gen_i::GetMeshNames(const char* theFileName)
2990 SMESH::string_array_var aResult = new SMESH::string_array();
2991 MED::PWrapper aMed = MED::CrWrapperR( theFileName );
2993 MED::TInt aNbMeshes = aMed->GetNbMeshes( &anErr );
2995 aResult->length( aNbMeshes );
2996 for( MED::TInt i = 0; i < aNbMeshes; i++ ) {
2997 MED::PMeshInfo aMeshInfo = aMed->GetPMeshInfo( i+1 );
2998 aResult[i] = CORBA::string_dup( aMeshInfo->GetName().c_str() );
3001 return aResult._retn();
3004 //=============================================================================
3008 * Save SMESH module's data
3010 //=============================================================================
3011 SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
3015 // ASSERT( theComponent->GetStudy()->StudyId() == myCurrentStudy->StudyId() )
3016 // san -- in case <myCurrentStudy> differs from theComponent's study,
3017 // use that of the component
3018 if ( theComponent->GetStudy()->StudyId() != GetCurrentStudyID() )
3019 SetCurrentStudy( theComponent->GetStudy() );
3021 // Store study contents as a set of python commands
3022 SavePython(myCurrentStudy);
3024 StudyContext* myStudyContext = GetCurrentStudyContext();
3026 // Declare a byte stream
3027 SALOMEDS::TMPFile_var aStreamFile;
3029 // Obtain a temporary dir
3030 TCollection_AsciiString tmpDir =
3031 ( isMultiFile ) ? TCollection_AsciiString( ( char* )theURL ) : ( char* )SALOMEDS_Tool::GetTmpDir().c_str();
3033 // Create a sequence of files processed
3034 SALOMEDS::ListOfFileNames_var aFileSeq = new SALOMEDS::ListOfFileNames;
3035 aFileSeq->length( NUM_TMP_FILES );
3037 TCollection_AsciiString aStudyName( "" );
3039 aStudyName = ( (char*)SALOMEDS_Tool::GetNameFromPath( myCurrentStudy->URL() ).c_str() );
3041 // Set names of temporary files
3042 TCollection_AsciiString filename =
3043 aStudyName + TCollection_AsciiString( "_SMESH.hdf" ); // for SMESH data itself
3044 TCollection_AsciiString meshfile =
3045 aStudyName + TCollection_AsciiString( "_SMESH_Mesh.med" ); // for mesh data to be stored in MED file
3046 aFileSeq[ 0 ] = CORBA::string_dup( filename.ToCString() );
3047 aFileSeq[ 1 ] = CORBA::string_dup( meshfile.ToCString() );
3048 filename = tmpDir + filename;
3049 meshfile = tmpDir + meshfile;
3052 HDFdataset* aDataset;
3053 HDFgroup* aTopGroup;
3055 HDFgroup* aSubGroup;
3056 HDFgroup* aSubSubGroup;
3057 hdf_size aSize[ 1 ];
3060 //Remove the files if they exist: BugID: 11225
3061 #ifndef WIN32 /* unix functionality */
3062 TCollection_AsciiString cmd("rm -f \"");
3064 TCollection_AsciiString cmd("del /F \"");
3071 system(cmd.ToCString());
3073 // MED writer to be used by storage process
3074 DriverMED_W_SMESHDS_Mesh myWriter;
3075 myWriter.SetFile( meshfile.ToCString() );
3078 // SetStoreName() to groups before storing hypotheses to let them refer to
3079 // groups using "store name", which is "Group <group_persistent_id>"
3081 SALOMEDS::ChildIterator_wrap itBig = myCurrentStudy->NewChildIterator( theComponent );
3082 for ( ; itBig->More(); itBig->Next() ) {
3083 SALOMEDS::SObject_wrap gotBranch = itBig->Value();
3084 if ( gotBranch->Tag() > GetAlgorithmsRootTag() ) {
3085 CORBA::Object_var anObject = SObjectToObject( gotBranch );
3086 if ( !CORBA::is_nil( anObject ) ) {
3087 SMESH::SMESH_Mesh_var myMesh = SMESH::SMESH_Mesh::_narrow( anObject ) ;
3088 if ( !myMesh->_is_nil() ) {
3089 myMesh->Load(); // load from study file if not yet done
3090 TPythonDump pd; // not to dump GetGroups()
3091 SMESH::ListOfGroups_var groups = myMesh->GetGroups();
3092 pd << ""; // to avoid optimizing pd out
3093 for ( CORBA::ULong i = 0; i < groups->length(); ++i )
3095 SMESH_GroupBase_i* grImpl = SMESH::DownCast<SMESH_GroupBase_i*>( groups[i]);
3098 CORBA::String_var objStr = GetORB()->object_to_string( grImpl->_this() );
3099 int anId = myStudyContext->findId( string( objStr.in() ) );
3101 sprintf( grpName, "Group %d", anId );
3102 SMESHDS_GroupBase* aGrpBaseDS = grImpl->GetGroupDS();
3103 aGrpBaseDS->SetStoreName( grpName );
3113 // ---> create HDF file
3114 aFile = new HDFfile( (char*) filename.ToCString() );
3115 aFile->CreateOnDisk();
3117 // --> iterator for top-level objects
3118 SALOMEDS::ChildIterator_wrap itBig = myCurrentStudy->NewChildIterator( theComponent );
3119 for ( ; itBig->More(); itBig->Next() ) {
3120 SALOMEDS::SObject_wrap gotBranch = itBig->Value();
3122 // --> hypotheses root branch (only one for the study)
3123 if ( gotBranch->Tag() == GetHypothesisRootTag() ) {
3124 // create hypotheses root HDF group
3125 aTopGroup = new HDFgroup( "Hypotheses", aFile );
3126 aTopGroup->CreateOnDisk();
3128 // iterator for all hypotheses
3129 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( gotBranch );
3130 for ( ; it->More(); it->Next() ) {
3131 SALOMEDS::SObject_wrap mySObject = it->Value();
3132 CORBA::Object_var anObject = SObjectToObject( mySObject );
3133 if ( !CORBA::is_nil( anObject ) ) {
3134 SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
3135 if ( !myHyp->_is_nil() ) {
3136 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
3138 string hypname = string( myHyp->GetName() );
3139 string libname = string( myHyp->GetLibName() );
3141 // Needs for save crossplatform libname, i.e. parth of name ( ".dll" for
3142 // WIN32 and ".so" for X-system) must be deleted
3143 int libname_len = libname.length();
3145 if( libname_len > 4 )
3146 libname.resize( libname_len - 4 );
3148 // PAL17753 (Regresion: missing hypothesis in restored study)
3149 // "lib" also should be removed from the beginning
3150 //if( libname_len > 3 )
3151 //libname.resize( libname_len - 3 );
3152 if( libname_len > 6 )
3153 libname = libname.substr( 3, libname_len - 3 - 3 );
3155 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3156 int id = myStudyContext->findId( string( objStr.in() ) );
3157 string hypdata = string( myImpl->SaveTo() );
3159 // for each hypothesis create HDF group basing on its id
3160 char hypGrpName[30];
3161 sprintf( hypGrpName, "Hypothesis %d", id );
3162 aGroup = new HDFgroup( hypGrpName, aTopGroup );
3163 aGroup->CreateOnDisk();
3164 // --> type name of hypothesis
3165 aSize[ 0 ] = hypname.length() + 1;
3166 aDataset = new HDFdataset( "Name", aGroup, HDF_STRING, aSize, 1 );
3167 aDataset->CreateOnDisk();
3168 aDataset->WriteOnDisk( ( char* )( hypname.c_str() ) );
3169 aDataset->CloseOnDisk();
3170 // --> server plugin library name of hypothesis
3171 aSize[ 0 ] = libname.length() + 1;
3172 aDataset = new HDFdataset( "LibName", aGroup, HDF_STRING, aSize, 1 );
3173 aDataset->CreateOnDisk();
3174 aDataset->WriteOnDisk( ( char* )( libname.c_str() ) );
3175 aDataset->CloseOnDisk();
3176 // --> persistent data of hypothesis
3177 aSize[ 0 ] = hypdata.length() + 1;
3178 aDataset = new HDFdataset( "Data", aGroup, HDF_STRING, aSize, 1 );
3179 aDataset->CreateOnDisk();
3180 aDataset->WriteOnDisk( ( char* )( hypdata.c_str() ) );
3181 aDataset->CloseOnDisk();
3182 // close hypothesis HDF group
3183 aGroup->CloseOnDisk();
3188 // close hypotheses root HDF group
3189 aTopGroup->CloseOnDisk();
3191 // --> algorithms root branch (only one for the study)
3192 else if ( gotBranch->Tag() == GetAlgorithmsRootTag() ) {
3193 // create algorithms root HDF group
3194 aTopGroup = new HDFgroup( "Algorithms", aFile );
3195 aTopGroup->CreateOnDisk();
3197 // iterator for all algorithms
3198 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( gotBranch );
3199 for ( ; it->More(); it->Next() ) {
3200 SALOMEDS::SObject_wrap mySObject = it->Value();
3201 CORBA::Object_var anObject = SObjectToObject( mySObject );
3202 if ( !CORBA::is_nil( anObject ) ) {
3203 SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
3204 if ( !myHyp->_is_nil() ) {
3205 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
3207 string hypname = string( myHyp->GetName() );
3208 string libname = string( myHyp->GetLibName() );
3210 // Needs for save crossplatform libname, i.e. parth of name ( ".dll" for
3211 // WIN32 and ".so" for X-system) must be deleted
3212 int libname_len = libname.length();
3214 if( libname_len > 4 )
3215 libname.resize( libname_len - 4 );
3217 // PAL17753 (Regresion: missing hypothesis in restored study)
3218 // "lib" also should be removed from the beginning
3219 //if( libname_len > 3 )
3220 //libname.resize( libname_len - 3 );
3221 if( libname_len > 6 )
3222 libname = libname.substr( 3, libname_len - 3 - 3 );
3224 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3225 int id = myStudyContext->findId( string( objStr.in() ) );
3226 string hypdata = string( myImpl->SaveTo() );
3228 // for each algorithm create HDF group basing on its id
3229 char hypGrpName[30];
3230 sprintf( hypGrpName, "Algorithm %d", id );
3231 aGroup = new HDFgroup( hypGrpName, aTopGroup );
3232 aGroup->CreateOnDisk();
3233 // --> type name of algorithm
3234 aSize[0] = hypname.length() + 1;
3235 aDataset = new HDFdataset( "Name", aGroup, HDF_STRING, aSize, 1 );
3236 aDataset->CreateOnDisk();
3237 aDataset->WriteOnDisk( ( char* )( hypname.c_str() ) );
3238 aDataset->CloseOnDisk();
3239 // --> server plugin library name of hypothesis
3240 aSize[0] = libname.length() + 1;
3241 aDataset = new HDFdataset( "LibName", aGroup, HDF_STRING, aSize, 1 );
3242 aDataset->CreateOnDisk();
3243 aDataset->WriteOnDisk( ( char* )( libname.c_str() ) );
3244 aDataset->CloseOnDisk();
3245 // --> persistent data of algorithm
3246 aSize[0] = hypdata.length() + 1;
3247 aDataset = new HDFdataset( "Data", aGroup, HDF_STRING, aSize, 1 );
3248 aDataset->CreateOnDisk();
3249 aDataset->WriteOnDisk( ( char* )( hypdata.c_str() ) );
3250 aDataset->CloseOnDisk();
3251 // close algorithm HDF group
3252 aGroup->CloseOnDisk();
3257 // close algorithms root HDF group
3258 aTopGroup->CloseOnDisk();
3260 // --> mesh objects roots branches
3261 else if ( gotBranch->Tag() > GetAlgorithmsRootTag() ) {
3262 CORBA::Object_var anObject = SObjectToObject( gotBranch );
3263 if ( !CORBA::is_nil( anObject ) ) {
3264 SMESH::SMESH_Mesh_var myMesh = SMESH::SMESH_Mesh::_narrow( anObject ) ;
3265 if ( !myMesh->_is_nil() ) {
3266 SMESH_Mesh_i* myImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( myMesh ).in() );
3268 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3269 int id = myStudyContext->findId( string( objStr.in() ) );
3270 ::SMESH_Mesh& myLocMesh = myImpl->GetImpl();
3271 SMESHDS_Mesh* mySMESHDSMesh = myLocMesh.GetMeshDS();
3272 bool hasShape = myLocMesh.HasShapeToMesh();
3274 // for each mesh open the HDF group basing on its id
3275 char meshGrpName[ 30 ];
3276 sprintf( meshGrpName, "Mesh %d", id );
3277 aTopGroup = new HDFgroup( meshGrpName, aFile );
3278 aTopGroup->CreateOnDisk();
3280 // --> put dataset to hdf file which is a flag that mesh has data
3281 string strHasData = "0";
3282 // check if the mesh is not empty
3283 if ( mySMESHDSMesh->NbNodes() > 0 ) {
3284 // write mesh data to med file
3285 myWriter.SetMesh( mySMESHDSMesh );
3286 myWriter.SetMeshId( id );
3289 aSize[ 0 ] = strHasData.length() + 1;
3290 aDataset = new HDFdataset( "Has data", aTopGroup, HDF_STRING, aSize, 1 );
3291 aDataset->CreateOnDisk();
3292 aDataset->WriteOnDisk( ( char* )( strHasData.c_str() ) );
3293 aDataset->CloseOnDisk();
3296 // for each mesh open the HDF group basing on its auto color parameter
3297 char meshAutoColorName[ 30 ];
3298 sprintf( meshAutoColorName, "AutoColorMesh %d", id );
3300 anAutoColor[0] = myImpl->GetAutoColor();
3302 aDataset = new HDFdataset( meshAutoColorName, aTopGroup, HDF_INT32, aSize, 1 );
3303 aDataset->CreateOnDisk();
3304 aDataset->WriteOnDisk( anAutoColor );
3305 aDataset->CloseOnDisk();
3307 // issue 0020693. Store _isModified flag
3308 int isModified = myLocMesh.GetIsModified();
3310 aDataset = new HDFdataset( "_isModified", aTopGroup, HDF_INT32, aSize, 1 );
3311 aDataset->CreateOnDisk();
3312 aDataset->WriteOnDisk( &isModified );
3313 aDataset->CloseOnDisk();
3315 // issue 20918. Store Persistent Id of SMESHDS_Mesh
3316 int meshPersistentId = mySMESHDSMesh->GetPersistentId();
3318 aDataset = new HDFdataset( "meshPersistentId", aTopGroup, HDF_INT32, aSize, 1 );
3319 aDataset->CreateOnDisk();
3320 aDataset->WriteOnDisk( &meshPersistentId );
3321 aDataset->CloseOnDisk();
3323 // write reference on a shape if exists
3324 SALOMEDS::SObject_wrap myRef;
3325 bool shapeRefFound = false;
3326 bool found = gotBranch->FindSubObject( GetRefOnShapeTag(), myRef.inout() );
3328 SALOMEDS::SObject_wrap myShape;
3329 bool ok = myRef->ReferencedObject( myShape.inout() );
3331 shapeRefFound = (! CORBA::is_nil( myShape->GetObject() ));
3332 string myRefOnObject = myShape->GetID();
3333 if ( shapeRefFound && myRefOnObject.length() > 0 ) {
3334 aSize[ 0 ] = myRefOnObject.length() + 1;
3335 aDataset = new HDFdataset( "Ref on shape", aTopGroup, HDF_STRING, aSize, 1 );
3336 aDataset->CreateOnDisk();
3337 aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3338 aDataset->CloseOnDisk();
3343 // write applied hypotheses if exist
3344 SALOMEDS::SObject_wrap myHypBranch;
3345 found = gotBranch->FindSubObject( GetRefOnAppliedHypothesisTag(), myHypBranch.inout() );
3346 if ( found && !shapeRefFound && hasShape) { // remove applied hyps
3347 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( myHypBranch );
3349 if ( found && (shapeRefFound || !hasShape) ) {
3350 aGroup = new HDFgroup( "Applied Hypotheses", aTopGroup );
3351 aGroup->CreateOnDisk();
3353 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( myHypBranch );
3355 for ( ; it->More(); it->Next() ) {
3356 SALOMEDS::SObject_wrap mySObject = it->Value();
3357 SALOMEDS::SObject_wrap myRefOnHyp;
3358 bool ok = mySObject->ReferencedObject( myRefOnHyp.inout() );
3360 // san - it is impossible to recover applied hypotheses
3361 // using their entries within Load() method,
3362 // for there are no AttributeIORs in the study when Load() is working.
3363 // Hence, it is better to store persistent IDs of hypotheses as references to them
3365 //string myRefOnObject = myRefOnHyp->GetID();
3366 CORBA::Object_var anObject = SObjectToObject( myRefOnHyp );
3367 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3368 int id = myStudyContext->findId( string( objStr.in() ) );
3369 //if ( myRefOnObject.length() > 0 ) {
3370 //aSize[ 0 ] = myRefOnObject.length() + 1;
3371 char hypName[ 30 ], hypId[ 30 ];
3372 sprintf( hypName, "Hyp %d", ++hypNb );
3373 sprintf( hypId, "%d", id );
3374 aSize[ 0 ] = strlen( hypId ) + 1;
3375 aDataset = new HDFdataset( hypName, aGroup, HDF_STRING, aSize, 1 );
3376 aDataset->CreateOnDisk();
3377 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3378 aDataset->WriteOnDisk( hypId );
3379 aDataset->CloseOnDisk();
3383 aGroup->CloseOnDisk();
3386 // write applied algorithms if exist
3387 SALOMEDS::SObject_wrap myAlgoBranch;
3388 found = gotBranch->FindSubObject( GetRefOnAppliedAlgorithmsTag(),
3389 myAlgoBranch.inout() );
3390 if ( found && !shapeRefFound && hasShape) { // remove applied algos
3391 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( myAlgoBranch );
3393 if ( found && (shapeRefFound || !hasShape)) {
3394 aGroup = new HDFgroup( "Applied Algorithms", aTopGroup );
3395 aGroup->CreateOnDisk();
3397 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( myAlgoBranch );
3399 for ( ; it->More(); it->Next() ) {
3400 SALOMEDS::SObject_wrap mySObject = it->Value();
3401 SALOMEDS::SObject_wrap myRefOnAlgo;
3402 bool ok = mySObject->ReferencedObject( myRefOnAlgo.inout() );
3404 // san - it is impossible to recover applied algorithms
3405 // using their entries within Load() method,
3406 // for there are no AttributeIORs in the study when Load() is working.
3407 // Hence, it is better to store persistent IDs of algorithms as references to them
3409 //string myRefOnObject = myRefOnAlgo->GetID();
3410 CORBA::Object_var anObject = SObjectToObject( myRefOnAlgo );
3411 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3412 int id = myStudyContext->findId( string( objStr.in() ) );
3413 //if ( myRefOnObject.length() > 0 ) {
3414 //aSize[ 0 ] = myRefOnObject.length() + 1;
3415 char algoName[ 30 ], algoId[ 30 ];
3416 sprintf( algoName, "Algo %d", ++algoNb );
3417 sprintf( algoId, "%d", id );
3418 aSize[ 0 ] = strlen( algoId ) + 1;
3419 aDataset = new HDFdataset( algoName, aGroup, HDF_STRING, aSize, 1 );
3420 aDataset->CreateOnDisk();
3421 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3422 aDataset->WriteOnDisk( algoId );
3423 aDataset->CloseOnDisk();
3427 aGroup->CloseOnDisk();
3430 // --> submesh objects sub-branches
3432 for ( int i = GetSubMeshOnVertexTag(); i <= GetSubMeshOnCompoundTag(); i++ ) {
3433 SALOMEDS::SObject_wrap mySubmeshBranch;
3434 found = gotBranch->FindSubObject( i, mySubmeshBranch.inout() );
3436 if ( found ) // check if there is shape reference in submeshes
3438 bool hasShapeRef = false;
3439 SALOMEDS::ChildIterator_wrap itSM =
3440 myCurrentStudy->NewChildIterator( mySubmeshBranch );
3441 for ( ; itSM->More(); itSM->Next() ) {
3442 SALOMEDS::SObject_wrap mySubRef, myShape, mySObject = itSM->Value();
3443 if ( mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef.inout() ))
3444 mySubRef->ReferencedObject( myShape.inout() );
3445 if ( !CORBA::is_nil( myShape ) && !CORBA::is_nil( myShape->GetObject() ))
3448 { // remove one submesh
3449 if ( shapeRefFound )
3450 { // unassign hypothesis
3451 SMESH::SMESH_subMesh_var mySubMesh =
3452 SMESH::SMESH_subMesh::_narrow( SObjectToObject( mySObject ));
3453 if ( !mySubMesh->_is_nil() ) {
3454 int shapeID = mySubMesh->GetId();
3455 TopoDS_Shape S = mySMESHDSMesh->IndexToShape( shapeID );
3456 const list<const SMESHDS_Hypothesis*>& hypList =
3457 mySMESHDSMesh->GetHypothesis( S );
3458 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
3459 while ( hyp != hypList.end() ) {
3460 int hypID = (*hyp++)->GetID(); // goto next hyp here because
3461 myLocMesh.RemoveHypothesis( S, hypID ); // hypList changes here
3465 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( mySObject );
3467 } // loop on submeshes of a type
3468 if ( !shapeRefFound || !hasShapeRef ) { // remove the whole submeshes branch
3469 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( mySubmeshBranch );
3472 } // end check if there is shape reference in submeshes
3474 char name_meshgroup[ 30 ];
3475 if ( i == GetSubMeshOnVertexTag() )
3476 strcpy( name_meshgroup, "SubMeshes On Vertex" );
3477 else if ( i == GetSubMeshOnEdgeTag() )
3478 strcpy( name_meshgroup, "SubMeshes On Edge" );
3479 else if ( i == GetSubMeshOnWireTag() )
3480 strcpy( name_meshgroup, "SubMeshes On Wire" );
3481 else if ( i == GetSubMeshOnFaceTag() )
3482 strcpy( name_meshgroup, "SubMeshes On Face" );
3483 else if ( i == GetSubMeshOnShellTag() )
3484 strcpy( name_meshgroup, "SubMeshes On Shell" );
3485 else if ( i == GetSubMeshOnSolidTag() )
3486 strcpy( name_meshgroup, "SubMeshes On Solid" );
3487 else if ( i == GetSubMeshOnCompoundTag() )
3488 strcpy( name_meshgroup, "SubMeshes On Compound" );
3490 // for each type of submeshes create container HDF group
3491 aGroup = new HDFgroup( name_meshgroup, aTopGroup );
3492 aGroup->CreateOnDisk();
3494 // iterator for all submeshes of given type
3495 SALOMEDS::ChildIterator_wrap itSM = myCurrentStudy->NewChildIterator( mySubmeshBranch );
3496 for ( ; itSM->More(); itSM->Next() ) {
3497 SALOMEDS::SObject_wrap mySObject = itSM->Value();
3498 CORBA::Object_var anSubObject = SObjectToObject( mySObject );
3499 if ( !CORBA::is_nil( anSubObject ))
3501 SMESH::SMESH_subMesh_var mySubMesh = SMESH::SMESH_subMesh::_narrow( anSubObject ) ;
3502 CORBA::String_var objStr = GetORB()->object_to_string( anSubObject );
3503 int subid = myStudyContext->findId( string( objStr.in() ) );
3505 // for each mesh open the HDF group basing on its id
3506 char submeshGrpName[ 30 ];
3507 sprintf( submeshGrpName, "SubMesh %d", subid );
3508 aSubGroup = new HDFgroup( submeshGrpName, aGroup );
3509 aSubGroup->CreateOnDisk();
3511 // write reference on a shape, already checked if it exists
3512 SALOMEDS::SObject_wrap mySubRef, myShape;
3513 if ( mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef.inout() ))
3514 mySubRef->ReferencedObject( myShape.inout() );
3515 string myRefOnObject = myShape->GetID();
3516 if ( myRefOnObject.length() > 0 ) {
3517 aSize[ 0 ] = myRefOnObject.length() + 1;
3518 aDataset = new HDFdataset( "Ref on shape", aSubGroup, HDF_STRING, aSize, 1 );
3519 aDataset->CreateOnDisk();
3520 aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3521 aDataset->CloseOnDisk();
3524 // write applied hypotheses if exist
3525 SALOMEDS::SObject_wrap mySubHypBranch;
3526 found = mySObject->FindSubObject( GetRefOnAppliedHypothesisTag(),
3527 mySubHypBranch.inout() );
3529 aSubSubGroup = new HDFgroup( "Applied Hypotheses", aSubGroup );
3530 aSubSubGroup->CreateOnDisk();
3532 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( mySubHypBranch );
3534 for ( ; it->More(); it->Next() ) {
3535 SALOMEDS::SObject_wrap mySubSObject = it->Value();
3536 SALOMEDS::SObject_wrap myRefOnHyp;
3537 bool ok = mySubSObject->ReferencedObject( myRefOnHyp.inout() );
3539 //string myRefOnObject = myRefOnHyp->GetID();
3540 CORBA::Object_var anObject = SObjectToObject( myRefOnHyp );
3541 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3542 int id = myStudyContext->findId( string( objStr.in() ) );
3543 //if ( myRefOnObject.length() > 0 ) {
3544 //aSize[ 0 ] = myRefOnObject.length() + 1;
3545 char hypName[ 30 ], hypId[ 30 ];
3546 sprintf( hypName, "Hyp %d", ++hypNb );
3547 sprintf( hypId, "%d", id );
3548 aSize[ 0 ] = strlen( hypId ) + 1;
3549 aDataset = new HDFdataset( hypName, aSubSubGroup, HDF_STRING, aSize, 1 );
3550 aDataset->CreateOnDisk();
3551 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3552 aDataset->WriteOnDisk( hypId );
3553 aDataset->CloseOnDisk();
3557 aSubSubGroup->CloseOnDisk();
3560 // write applied algorithms if exist
3561 SALOMEDS::SObject_wrap mySubAlgoBranch;
3562 found = mySObject->FindSubObject( GetRefOnAppliedAlgorithmsTag(),
3563 mySubAlgoBranch.inout() );
3565 aSubSubGroup = new HDFgroup( "Applied Algorithms", aSubGroup );
3566 aSubSubGroup->CreateOnDisk();
3568 SALOMEDS::ChildIterator_wrap it =
3569 myCurrentStudy->NewChildIterator( mySubAlgoBranch );
3571 for ( ; it->More(); it->Next() ) {
3572 SALOMEDS::SObject_wrap mySubSObject = it->Value();
3573 SALOMEDS::SObject_wrap myRefOnAlgo;
3574 bool ok = mySubSObject->ReferencedObject( myRefOnAlgo.inout() );
3576 //string myRefOnObject = myRefOnAlgo->GetID();
3577 CORBA::Object_var anObject = SObjectToObject( myRefOnAlgo );
3578 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3579 int id = myStudyContext->findId( string( objStr.in() ) );
3580 //if ( myRefOnObject.length() > 0 ) {
3581 //aSize[ 0 ] = myRefOnObject.length() + 1;
3582 char algoName[ 30 ], algoId[ 30 ];
3583 sprintf( algoName, "Algo %d", ++algoNb );
3584 sprintf( algoId, "%d", id );
3585 aSize[ 0 ] = strlen( algoId ) + 1;
3586 aDataset = new HDFdataset( algoName, aSubSubGroup, HDF_STRING, aSize, 1 );
3587 aDataset->CreateOnDisk();
3588 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3589 aDataset->WriteOnDisk( algoId );
3590 aDataset->CloseOnDisk();
3594 aSubSubGroup->CloseOnDisk();
3596 // close submesh HDF group
3597 aSubGroup->CloseOnDisk();
3600 // close container of submeshes by type HDF group
3601 aGroup->CloseOnDisk();
3604 // All sub-meshes will be stored in MED file
3605 // .. will NOT (PAL 12992)
3606 //if ( shapeRefFound )
3607 //myWriter.AddAllSubMeshes();
3609 // store submesh order if any
3610 const TListOfListOfInt& theOrderIds = myLocMesh.GetMeshOrder();
3611 if ( theOrderIds.size() ) {
3612 char order_list[ 30 ];
3613 strcpy( order_list, "Mesh Order" );
3614 // count number of submesh ids
3616 TListOfListOfInt::const_iterator idIt = theOrderIds.begin();
3617 for ( ; idIt != theOrderIds.end(); idIt++ )
3618 nbIDs += (*idIt).size();
3619 // number of values = number of IDs +
3620 // number of lists (for separators) - 1
3621 int* smIDs = new int [ nbIDs + theOrderIds.size() - 1 ];
3622 idIt = theOrderIds.begin();
3623 for ( int i = 0; idIt != theOrderIds.end(); idIt++ ) {
3624 const TListOfInt& idList = *idIt;
3625 if (idIt != theOrderIds.begin()) // not first list
3626 smIDs[ i++ ] = -1/* *idList.size()*/; // separator between lists
3627 // dump submesh ids from current list
3628 TListOfInt::const_iterator id_smId = idList.begin();
3629 for( ; id_smId != idList.end(); id_smId++ )
3630 smIDs[ i++ ] = *id_smId;
3633 aSize[ 0 ] = nbIDs + theOrderIds.size() - 1;
3635 aDataset = new HDFdataset( order_list, aTopGroup, HDF_INT32, aSize, 1 );
3636 aDataset->CreateOnDisk();
3637 aDataset->WriteOnDisk( smIDs );
3638 aDataset->CloseOnDisk();
3643 // groups root sub-branch
3644 SALOMEDS::SObject_wrap myGroupsBranch;
3645 for ( int i = GetNodeGroupsTag(); i <= GetBallElementsGroupsTag(); i++ ) {
3646 found = gotBranch->FindSubObject( i, myGroupsBranch.inout() );
3648 char name_group[ 30 ];
3649 if ( i == GetNodeGroupsTag() )
3650 strcpy( name_group, "Groups of Nodes" );
3651 else if ( i == GetEdgeGroupsTag() )
3652 strcpy( name_group, "Groups of Edges" );
3653 else if ( i == GetFaceGroupsTag() )
3654 strcpy( name_group, "Groups of Faces" );
3655 else if ( i == GetVolumeGroupsTag() )
3656 strcpy( name_group, "Groups of Volumes" );
3657 else if ( i == Get0DElementsGroupsTag() )
3658 strcpy( name_group, "Groups of 0D Elements" );
3659 else if ( i == GetBallElementsGroupsTag() )
3660 strcpy( name_group, "Groups of Balls" );
3662 aGroup = new HDFgroup( name_group, aTopGroup );
3663 aGroup->CreateOnDisk();
3665 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( myGroupsBranch );
3666 for ( ; it->More(); it->Next() ) {
3667 SALOMEDS::SObject_wrap mySObject = it->Value();
3668 CORBA::Object_var aSubObject = SObjectToObject( mySObject );
3669 if ( !CORBA::is_nil( aSubObject ) ) {
3670 SMESH_GroupBase_i* myGroupImpl =
3671 dynamic_cast<SMESH_GroupBase_i*>( GetServant( aSubObject ).in() );
3674 SMESHDS_GroupBase* aGrpBaseDS = myGroupImpl->GetGroupDS();
3678 CORBA::String_var objStr = GetORB()->object_to_string( aSubObject );
3679 int anId = myStudyContext->findId( string( objStr.in() ) );
3681 // For each group, create a dataset named "Group <group_persistent_id>"
3682 // and store the group's user name into it
3683 const char* grpName = aGrpBaseDS->GetStoreName();
3684 char* aUserName = myGroupImpl->GetName();
3685 aSize[ 0 ] = strlen( aUserName ) + 1;
3687 aDataset = new HDFdataset( grpName, aGroup, HDF_STRING, aSize, 1 );
3688 aDataset->CreateOnDisk();
3689 aDataset->WriteOnDisk( aUserName );
3690 aDataset->CloseOnDisk();
3693 // For each group, create a dataset named "Group <group_persistent_id> Color"
3694 // and store the group's color into it
3695 char grpColorName[ 30 ];
3696 sprintf( grpColorName, "ColorGroup %d", anId );
3697 SALOMEDS::Color aColor = myGroupImpl->GetColor();
3699 anRGB[ 0 ] = aColor.R;
3700 anRGB[ 1 ] = aColor.G;
3701 anRGB[ 2 ] = aColor.B;
3703 aDataset = new HDFdataset( grpColorName, aGroup, HDF_FLOAT64, aSize, 1 );
3704 aDataset->CreateOnDisk();
3705 aDataset->WriteOnDisk( anRGB );
3706 aDataset->CloseOnDisk();
3708 // Pass SMESHDS_Group to MED writer
3709 SMESHDS_Group* aGrpDS = dynamic_cast<SMESHDS_Group*>( aGrpBaseDS );
3711 myWriter.AddGroup( aGrpDS );
3713 // write reference on a shape if exists
3714 SMESHDS_GroupOnGeom* aGeomGrp =
3715 dynamic_cast<SMESHDS_GroupOnGeom*>( aGrpBaseDS );
3717 SALOMEDS::SObject_wrap mySubRef, myShape;
3718 if (mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef.inout() ) &&
3719 mySubRef->ReferencedObject( myShape.inout() ) &&
3720 !CORBA::is_nil( myShape->GetObject() ))
3722 string myRefOnObject = myShape->GetID();
3723 if ( myRefOnObject.length() > 0 ) {
3724 char aRefName[ 30 ];
3725 sprintf( aRefName, "Ref on shape %d", anId);
3726 aSize[ 0 ] = myRefOnObject.length() + 1;
3727 aDataset = new HDFdataset(aRefName, aGroup, HDF_STRING, aSize, 1);
3728 aDataset->CreateOnDisk();
3729 aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3730 aDataset->CloseOnDisk();
3733 else // shape ref is invalid:
3735 // save a group on geometry as ordinary group
3736 myWriter.AddGroup( aGeomGrp );
3739 else if ( SMESH_GroupOnFilter_i* aFilterGrp_i =
3740 dynamic_cast<SMESH_GroupOnFilter_i*>( myGroupImpl ))
3742 std::string str = aFilterGrp_i->FilterToString();
3743 std::string hdfGrpName = "Filter " + SMESH_Comment(anId);
3744 aSize[ 0 ] = str.length() + 1;
3745 aDataset = new HDFdataset( hdfGrpName.c_str(), aGroup, HDF_STRING, aSize, 1);
3746 aDataset->CreateOnDisk();
3747 aDataset->WriteOnDisk( ( char* )( str.c_str() ) );
3748 aDataset->CloseOnDisk();
3752 aGroup->CloseOnDisk();
3756 if ( strcmp( strHasData.c_str(), "1" ) == 0 )
3758 // Flush current mesh information into MED file
3761 // save info on nb of elements
3762 SMESH_PreMeshInfo::SaveToFile( myImpl, id, aFile );
3764 // maybe a shape was deleted in the study
3765 if ( !shapeRefFound && !mySMESHDSMesh->ShapeToMesh().IsNull() && hasShape) {
3766 TopoDS_Shape nullShape;
3767 myLocMesh.ShapeToMesh( nullShape ); // remove shape referring data
3770 SMESHDS_SubMeshIteratorPtr smIt = mySMESHDSMesh->SubMeshes();
3775 aGroup = new HDFgroup( "Submeshes", aTopGroup );
3776 aGroup->CreateOnDisk();
3778 // each element belongs to one or none submesh,
3779 // so for each node/element, we store a submesh ID
3781 // Store submesh IDs
3782 for ( int isNode = 0; isNode < 2; ++isNode )
3784 SMDS_ElemIteratorPtr eIt =
3785 mySMESHDSMesh->elementsIterator( isNode ? SMDSAbs_Node : SMDSAbs_All );
3786 int nbElems = isNode ? mySMESHDSMesh->NbNodes() : mySMESHDSMesh->GetMeshInfo().NbElements();
3789 std::vector<int> smIDs; smIDs.reserve( nbElems );
3790 while ( eIt->more() )
3791 if ( const SMDS_MeshElement* e = eIt->next())
3792 smIDs.push_back( e->getshapeId() );
3794 aSize[ 0 ] = nbElems;
3795 string aDSName( isNode ? "Node Submeshes" : "Element Submeshes");
3796 aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_INT32, aSize, 1 );
3797 aDataset->CreateOnDisk();
3798 aDataset->WriteOnDisk( & smIDs[0] );
3799 aDataset->CloseOnDisk();
3802 aGroup->CloseOnDisk();
3804 // Store node positions on sub-shapes (SMDS_Position):
3805 // ----------------------------------------------------
3807 aGroup = new HDFgroup( "Node Positions", aTopGroup );
3808 aGroup->CreateOnDisk();
3810 // in aGroup, create 5 datasets to contain:
3811 // "Nodes on Edges" - ID of node on edge
3812 // "Edge positions" - U parameter on node on edge
3813 // "Nodes on Faces" - ID of node on face
3814 // "Face U positions" - U parameter of node on face
3815 // "Face V positions" - V parameter of node on face
3817 // Find out nb of nodes on edges and faces
3818 // Collect corresponing sub-meshes
3819 int nbEdgeNodes = 0, nbFaceNodes = 0;
3820 list<SMESHDS_SubMesh*> aEdgeSM, aFaceSM;
3821 // loop on SMESHDS_SubMesh'es
3822 while ( smIt->more() )
3824 SMESHDS_SubMesh* aSubMesh = const_cast< SMESHDS_SubMesh* >( smIt->next() );
3825 if ( aSubMesh->IsComplexSubmesh() )
3826 continue; // submesh containing other submeshs
3827 int nbNodes = aSubMesh->NbNodes();
3828 if ( nbNodes == 0 ) continue;
3830 int aShapeID = aSubMesh->GetID();
3831 if ( aShapeID < 1 || aShapeID > mySMESHDSMesh->MaxShapeIndex() )
3833 int aShapeType = mySMESHDSMesh->IndexToShape( aShapeID ).ShapeType();
3834 // write only SMDS_FacePosition and SMDS_EdgePosition
3835 switch ( aShapeType ) {
3837 nbFaceNodes += nbNodes;
3838 aFaceSM.push_back( aSubMesh );
3841 nbEdgeNodes += nbNodes;
3842 aEdgeSM.push_back( aSubMesh );
3848 // Treat positions on edges or faces
3849 for ( int onFace = 0; onFace < 2; onFace++ )
3851 // Create arrays to store in datasets
3852 int iNode = 0, nbNodes = ( onFace ? nbFaceNodes : nbEdgeNodes );
3853 if (!nbNodes) continue;
3854 int* aNodeIDs = new int [ nbNodes ];
3855 double* aUPos = new double [ nbNodes ];
3856 double* aVPos = ( onFace ? new double[ nbNodes ] : 0 );
3859 // loop on sub-meshes
3860 list<SMESHDS_SubMesh*> * pListSM = ( onFace ? &aFaceSM : &aEdgeSM );
3861 list<SMESHDS_SubMesh*>::iterator itSM = pListSM->begin();
3862 for ( ; itSM != pListSM->end(); itSM++ )
3864 SMESHDS_SubMesh* aSubMesh = (*itSM);
3866 SMDS_NodeIteratorPtr itNode = aSubMesh->GetNodes();
3867 // loop on nodes in aSubMesh
3868 while ( itNode->more() )
3871 const SMDS_MeshNode* node = itNode->next();
3872 aNodeIDs [ iNode ] = node->GetID();
3875 const SMDS_PositionPtr pos = node->GetPosition();
3876 if ( onFace ) { // on FACE
3877 const SMDS_FacePosition* fPos =
3878 dynamic_cast<const SMDS_FacePosition*>( pos );
3880 aUPos[ iNode ] = fPos->GetUParameter();
3881 aVPos[ iNode ] = fPos->GetVParameter();
3888 const SMDS_EdgePosition* ePos =
3889 dynamic_cast<const SMDS_EdgePosition*>( pos );
3891 aUPos[ iNode ] = ePos->GetUParameter();
3897 } // loop on nodes in aSubMesh
3898 } // loop on sub-meshes
3903 aSize[ 0 ] = nbNodes;
3905 string aDSName( onFace ? "Nodes on Faces" : "Nodes on Edges");
3906 aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_INT32, aSize, 1 );
3907 aDataset->CreateOnDisk();
3908 aDataset->WriteOnDisk( aNodeIDs );
3909 aDataset->CloseOnDisk();
3912 aDSName = ( onFace ? "Face U positions" : "Edge positions");
3913 aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_FLOAT64, aSize, 1);
3914 aDataset->CreateOnDisk();
3915 aDataset->WriteOnDisk( aUPos );
3916 aDataset->CloseOnDisk();
3919 aDataset = new HDFdataset( "Face V positions", aGroup, HDF_FLOAT64, aSize, 1);
3920 aDataset->CreateOnDisk();
3921 aDataset->WriteOnDisk( aVPos );
3922 aDataset->CloseOnDisk();
3927 if ( aVPos ) delete [] aVPos;
3929 } // treat positions on edges or faces
3931 // close "Node Positions" group
3932 aGroup->CloseOnDisk();
3934 } // if ( there are submeshes in SMESHDS_Mesh )
3937 // close mesh HDF group
3938 aTopGroup->CloseOnDisk();
3946 aFile->CloseOnDisk();
3949 // Convert temporary files to stream
3950 aStreamFile = SALOMEDS_Tool::PutFilesToStream( tmpDir.ToCString(), aFileSeq.in(), isMultiFile );
3952 // Remove temporary files and directory
3954 SALOMEDS_Tool::RemoveTemporaryFiles( tmpDir.ToCString(), aFileSeq.in(), true );
3956 return aStreamFile._retn();
3959 //=============================================================================
3961 * SMESH_Gen_i::SaveASCII
3963 * Save SMESH module's data in ASCII format
3965 //=============================================================================
3967 SALOMEDS::TMPFile* SMESH_Gen_i::SaveASCII( SALOMEDS::SComponent_ptr theComponent,
3969 bool isMultiFile ) {
3970 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::SaveASCII" );
3971 SALOMEDS::TMPFile_var aStreamFile = Save( theComponent, theURL, isMultiFile );
3972 return aStreamFile._retn();
3974 //after usual saving needs to encipher binary to text string
3975 //Any binary symbol will be represent as "|xx" () hexadecimal format number
3976 int size = aStreamFile.in().length();
3977 _CORBA_Octet* buffer = new _CORBA_Octet[size*3+1];
3978 for ( int i = 0; i < size; i++ )
3979 sprintf( (char*)&(buffer[i*3]), "|%02x", aStreamFile[i] );
3981 buffer[size * 3] = '\0';
3983 SALOMEDS::TMPFile_var anAsciiStreamFile = new SALOMEDS::TMPFile(size*3, size*3, buffer, 1);
3985 return anAsciiStreamFile._retn();
3988 //=============================================================================
3990 * SMESH_Gen_i::loadGeomData
3992 * Load GEOM module data
3994 //=============================================================================
3996 void SMESH_Gen_i::loadGeomData( SALOMEDS::SComponent_ptr theCompRoot )
3998 if ( theCompRoot->_is_nil() )
4001 SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow( theCompRoot->GetStudy() );
4002 if ( aStudy->_is_nil() )
4005 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
4006 aStudyBuilder->LoadWith( theCompRoot, GetGeomEngine() );
4009 //=============================================================================
4013 * Load SMESH module's data
4015 //=============================================================================
4017 bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
4018 const SALOMEDS::TMPFile& theStream,
4022 if ( theComponent->GetStudy()->StudyId() != GetCurrentStudyID() )
4023 SetCurrentStudy( theComponent->GetStudy() );
4025 /* if( !theComponent->_is_nil() )
4027 //SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow( theComponent->GetStudy() );
4028 if( !myCurrentStudy->FindComponent( "GEOM" )->_is_nil() )
4029 loadGeomData( myCurrentStudy->FindComponent( "GEOM" ) );
4032 StudyContext* myStudyContext = GetCurrentStudyContext();
4034 // Get temporary files location
4035 TCollection_AsciiString tmpDir =
4036 ( char* )( isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir().c_str() );
4038 // Convert the stream into sequence of files to process
4039 SALOMEDS::ListOfFileNames_var aFileSeq = SALOMEDS_Tool::PutStreamToFiles( theStream,
4042 TCollection_AsciiString aStudyName( "" );
4043 if ( isMultiFile ) {
4044 CORBA::String_var url = myCurrentStudy->URL();
4045 aStudyName = (char*)SALOMEDS_Tool::GetNameFromPath( url.in() ).c_str();
4047 // Set names of temporary files
4048 TCollection_AsciiString filename = tmpDir + aStudyName + "_SMESH.hdf";
4049 TCollection_AsciiString meshfile = tmpDir + aStudyName + "_SMESH_Mesh.med";
4053 HDFdataset* aDataset;
4054 HDFgroup* aTopGroup;
4056 HDFgroup* aSubGroup;
4057 HDFgroup* aSubSubGroup;
4060 // ---> open HDF file
4061 aFile = new HDFfile( (char*) filename.ToCString() );
4063 aFile->OpenOnDisk( HDF_RDONLY );
4065 catch ( HDFexception ) {
4066 INFOS( "Load(): " << filename << " not found!" );
4070 TPythonDump pd; // prevent dump during loading
4072 // DriverMED_R_SMESHDS_Mesh myReader;
4073 // myReader.SetFile( meshfile.ToCString() );
4075 // For PAL13473 ("Repetitive mesh") implementation.
4076 // New dependencies between SMESH objects are established:
4077 // now hypotheses can refer to meshes, shapes and other hypotheses.
4078 // To keep data consistent, the following order of data restoration
4080 // 1. Create hypotheses
4081 // 2. Create all meshes
4082 // 3. Load hypotheses' data
4085 list< pair< SMESH_Hypothesis_i*, string > > hypDataList;
4086 list< pair< SMESH_Mesh_i*, HDFgroup* > > meshGroupList;
4087 list< SMESH::Filter_var > filters;
4089 // get total number of top-level groups
4090 int aNbGroups = aFile->nInternalObjects();
4091 if ( aNbGroups > 0 ) {
4092 // --> in first turn we should read&create hypotheses
4093 if ( aFile->ExistInternalObject( "Hypotheses" ) ) {
4094 // open hypotheses root HDF group
4095 aTopGroup = new HDFgroup( "Hypotheses", aFile );
4096 aTopGroup->OpenOnDisk();
4098 // get number of hypotheses
4099 int aNbObjects = aTopGroup->nInternalObjects();
4100 for ( int j = 0; j < aNbObjects; j++ ) {
4101 // try to identify hypothesis
4102 char hypGrpName[ HDF_NAME_MAX_LEN+1 ];
4103 aTopGroup->InternalObjectIndentify( j, hypGrpName );
4105 if ( string( hypGrpName ).substr( 0, 10 ) == string( "Hypothesis" ) ) {
4106 // open hypothesis group
4107 aGroup = new HDFgroup( hypGrpName, aTopGroup );
4108 aGroup->OpenOnDisk();
4110 // --> get hypothesis id
4111 int id = atoi( string( hypGrpName ).substr( 10 ).c_str() );
4116 // get number of datasets
4117 int aNbSubObjects = aGroup->nInternalObjects();
4118 for ( int k = 0; k < aNbSubObjects; k++ ) {
4120 char name_of_subgroup[ HDF_NAME_MAX_LEN+1 ];
4121 aGroup->InternalObjectIndentify( k, name_of_subgroup );
4122 // --> get hypothesis name
4123 if ( strcmp( name_of_subgroup, "Name" ) == 0 ) {
4124 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4125 aDataset->OpenOnDisk();
4126 size = aDataset->GetSize();
4127 char* hypname_str = new char[ size ];
4128 aDataset->ReadFromDisk( hypname_str );
4129 hypname = string( hypname_str );
4130 delete [] hypname_str;
4131 aDataset->CloseOnDisk();
4133 // --> get hypothesis plugin library name
4134 if ( strcmp( name_of_subgroup, "LibName" ) == 0 ) {
4135 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4136 aDataset->OpenOnDisk();
4137 size = aDataset->GetSize();
4138 char* libname_str = new char[ size ];
4139 aDataset->ReadFromDisk( libname_str );
4140 if(MYDEBUG) SCRUTE( libname_str );
4141 libname = string( libname_str );
4142 delete [] libname_str;
4143 aDataset->CloseOnDisk();
4145 // --> get hypothesis data
4146 if ( strcmp( name_of_subgroup, "Data" ) == 0 ) {
4147 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4148 aDataset->OpenOnDisk();
4149 size = aDataset->GetSize();
4150 char* hypdata_str = new char[ size ];
4151 aDataset->ReadFromDisk( hypdata_str );
4152 hypdata = string( hypdata_str );
4153 delete [] hypdata_str;
4154 aDataset->CloseOnDisk();
4157 // close hypothesis HDF group
4158 aGroup->CloseOnDisk();
4160 // --> restore hypothesis from data
4161 if ( id > 0 && !hypname.empty()/* && !hypdata.empty()*/ ) { // VSR : persistent data can be empty
4162 if(MYDEBUG) MESSAGE("VSR - load hypothesis : id = " << id <<
4163 ", name = " << hypname.c_str() << ", persistent string = " << hypdata.c_str());
4164 SMESH::SMESH_Hypothesis_var myHyp;
4166 try { // protect persistence mechanism against exceptions
4167 myHyp = this->createHypothesis( hypname.c_str(), libname.c_str() );
4170 INFOS( "Exception during hypothesis creation" );
4173 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
4175 // myImpl->LoadFrom( hypdata.c_str() );
4176 hypDataList.push_back( make_pair( myImpl, hypdata ));
4177 CORBA::String_var iorString = GetORB()->object_to_string( myHyp );
4178 int newId = myStudyContext->findId( iorString.in() );
4179 myStudyContext->mapOldToNew( id, newId );
4182 if(MYDEBUG) MESSAGE( "VSR - SMESH_Gen::Load - can't get servant" );
4186 // close hypotheses root HDF group
4187 aTopGroup->CloseOnDisk();
4191 // --> then we should read&create algorithms
4192 if ( aFile->ExistInternalObject( "Algorithms" ) ) {
4193 // open algorithms root HDF group
4194 aTopGroup = new HDFgroup( "Algorithms", aFile );
4195 aTopGroup->OpenOnDisk();
4197 // get number of algorithms
4198 int aNbObjects = aTopGroup->nInternalObjects();
4199 for ( int j = 0; j < aNbObjects; j++ ) {
4200 // try to identify algorithm
4201 char hypGrpName[ HDF_NAME_MAX_LEN+1 ];
4202 aTopGroup->InternalObjectIndentify( j, hypGrpName );
4204 if ( string( hypGrpName ).substr( 0, 9 ) == string( "Algorithm" ) ) {
4205 // open algorithm group
4206 aGroup = new HDFgroup( hypGrpName, aTopGroup );
4207 aGroup->OpenOnDisk();
4209 // --> get algorithm id
4210 int id = atoi( string( hypGrpName ).substr( 9 ).c_str() );
4215 // get number of datasets
4216 int aNbSubObjects = aGroup->nInternalObjects();
4217 for ( int k = 0; k < aNbSubObjects; k++ ) {
4219 char name_of_subgroup[ HDF_NAME_MAX_LEN+1 ];
4220 aGroup->InternalObjectIndentify( k, name_of_subgroup );
4221 // --> get algorithm name
4222 if ( strcmp( name_of_subgroup, "Name" ) == 0 ) {
4223 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4224 aDataset->OpenOnDisk();
4225 size = aDataset->GetSize();
4226 char* hypname_str = new char[ size ];
4227 aDataset->ReadFromDisk( hypname_str );
4228 hypname = string( hypname_str );
4229 delete [] hypname_str;
4230 aDataset->CloseOnDisk();
4232 // --> get algorithm plugin library name
4233 if ( strcmp( name_of_subgroup, "LibName" ) == 0 ) {
4234 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4235 aDataset->OpenOnDisk();
4236 size = aDataset->GetSize();
4237 char* libname_str = new char[ size ];
4238 aDataset->ReadFromDisk( libname_str );
4239 if(MYDEBUG) SCRUTE( libname_str );
4240 libname = string( libname_str );
4241 delete [] libname_str;
4242 aDataset->CloseOnDisk();
4244 // --> get algorithm data
4245 if ( strcmp( name_of_subgroup, "Data" ) == 0 ) {
4246 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4247 aDataset->OpenOnDisk();
4248 size = aDataset->GetSize();
4249 char* hypdata_str = new char[ size ];
4250 aDataset->ReadFromDisk( hypdata_str );
4251 if(MYDEBUG) SCRUTE( hypdata_str );
4252 hypdata = string( hypdata_str );
4253 delete [] hypdata_str;
4254 aDataset->CloseOnDisk();
4257 // close algorithm HDF group
4258 aGroup->CloseOnDisk();
4260 // --> restore algorithm from data
4261 if ( id > 0 && !hypname.empty()/* && !hypdata.empty()*/ ) { // VSR : persistent data can be empty
4262 if(MYDEBUG) MESSAGE("VSR - load algo : id = " << id <<
4263 ", name = " << hypname.c_str() << ", persistent string = " << hypdata.c_str());
4264 SMESH::SMESH_Hypothesis_var myHyp;
4266 try { // protect persistence mechanism against exceptions
4267 myHyp = this->createHypothesis( hypname.c_str(), libname.c_str() );
4270 INFOS( "Exception during hypothesis creation" );
4273 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
4275 //myImpl->LoadFrom( hypdata.c_str() );
4276 hypDataList.push_back( make_pair( myImpl, hypdata ));
4277 CORBA::String_var iorString = GetORB()->object_to_string( myHyp );
4278 int newId = myStudyContext->findId( iorString.in() );
4279 myStudyContext->mapOldToNew( id, newId );
4282 if(MYDEBUG) MESSAGE( "VSR - SMESH_Gen::Load - can't get servant" );
4286 // close algorithms root HDF group
4287 aTopGroup->CloseOnDisk();
4291 // --> the rest groups should be meshes
4292 for ( int i = 0; i < aNbGroups; i++ ) {
4293 // identify next group
4294 char meshName[ HDF_NAME_MAX_LEN+1 ];
4295 aFile->InternalObjectIndentify( i, meshName );
4297 if ( string( meshName ).substr( 0, 4 ) == string( "Mesh" ) ) {
4299 int id = atoi( string( meshName ).substr( 4 ).c_str() );
4303 // open mesh HDF group
4304 aTopGroup = new HDFgroup( meshName, aFile );
4305 aTopGroup->OpenOnDisk();
4307 // get number of child HDF objects
4308 int aNbObjects = aTopGroup->nInternalObjects();
4309 if ( aNbObjects > 0 ) {
4311 if(MYDEBUG) MESSAGE( "VSR - load mesh : id = " << id );
4312 SMESH::SMESH_Mesh_var myNewMesh = this->createMesh();
4313 SMESH_Mesh_i* myNewMeshImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( myNewMesh ).in() );
4314 if ( !myNewMeshImpl )
4316 meshGroupList.push_back( make_pair( myNewMeshImpl, aTopGroup ));
4318 CORBA::String_var iorString = GetORB()->object_to_string( myNewMesh );
4319 int newId = myStudyContext->findId( iorString.in() );
4320 myStudyContext->mapOldToNew( id, newId );
4323 // try to read and set auto color flag
4324 char aMeshAutoColorName[ 30 ];
4325 sprintf( aMeshAutoColorName, "AutoColorMesh %d", id);
4326 if( aTopGroup->ExistInternalObject( aMeshAutoColorName ) )
4328 aDataset = new HDFdataset( aMeshAutoColorName, aTopGroup );
4329 aDataset->OpenOnDisk();
4330 size = aDataset->GetSize();
4331 int* anAutoColor = new int[ size ];
4332 aDataset->ReadFromDisk( anAutoColor );
4333 aDataset->CloseOnDisk();
4334 myNewMeshImpl->GetImpl().SetAutoColor( (bool)anAutoColor[0] );
4335 delete [] anAutoColor;
4338 // try to read and set reference to shape
4339 GEOM::GEOM_Object_var aShapeObject;
4340 if ( aTopGroup->ExistInternalObject( "Ref on shape" ) ) {
4341 // load mesh "Ref on shape" - it's an entry to SObject
4342 aDataset = new HDFdataset( "Ref on shape", aTopGroup );
4343 aDataset->OpenOnDisk();
4344 size = aDataset->GetSize();
4345 char* refFromFile = new char[ size ];
4346 aDataset->ReadFromDisk( refFromFile );
4347 aDataset->CloseOnDisk();
4348 if ( strlen( refFromFile ) > 0 ) {
4349 SALOMEDS::SObject_wrap shapeSO = myCurrentStudy->FindObjectID( refFromFile );
4351 // Make sure GEOM data are loaded first
4352 //loadGeomData( shapeSO->GetFatherComponent() );
4354 CORBA::Object_var shapeObject = SObjectToObject( shapeSO );
4355 if ( !CORBA::is_nil( shapeObject ) ) {
4356 aShapeObject = GEOM::GEOM_Object::_narrow( shapeObject );
4357 if ( !aShapeObject->_is_nil() )
4358 myNewMeshImpl->SetShape( aShapeObject );
4361 delete [] refFromFile;
4364 // issue 20918. Restore Persistent Id of SMESHDS_Mesh
4365 if( aTopGroup->ExistInternalObject( "meshPersistentId" ) )
4367 aDataset = new HDFdataset( "meshPersistentId", aTopGroup );
4368 aDataset->OpenOnDisk();
4369 size = aDataset->GetSize();
4370 int* meshPersistentId = new int[ size ];
4371 aDataset->ReadFromDisk( meshPersistentId );
4372 aDataset->CloseOnDisk();
4373 myNewMeshImpl->GetImpl().GetMeshDS()->SetPersistentId( *meshPersistentId );
4374 delete [] meshPersistentId;
4380 // As all object that can be referred by hypothesis are created,
4381 // we can restore hypothesis data
4383 list< pair< SMESH_Hypothesis_i*, string > >::iterator hyp_data;
4384 for ( hyp_data = hypDataList.begin(); hyp_data != hypDataList.end(); ++hyp_data )
4386 SMESH_Hypothesis_i* hyp = hyp_data->first;
4387 string & data = hyp_data->second;
4388 hyp->LoadFrom( data.c_str() );
4391 // Restore the rest mesh data
4393 list< pair< SMESH_Mesh_i*, HDFgroup* > >::iterator meshi_group;
4394 for ( meshi_group = meshGroupList.begin(); meshi_group != meshGroupList.end(); ++meshi_group )
4396 aTopGroup = meshi_group->second;
4397 SMESH_Mesh_i* myNewMeshImpl = meshi_group->first;
4398 //::SMESH_Mesh& myLocMesh = myNewMeshImpl->GetImpl();
4399 //SMESHDS_Mesh* mySMESHDSMesh = myLocMesh.GetMeshDS();
4401 GEOM::GEOM_Object_var aShapeObject = myNewMeshImpl->GetShapeToMesh();
4402 bool hasData = false;
4405 CORBA::String_var iorString = GetORB()->object_to_string( myNewMeshImpl->_this() );
4406 int newId = myStudyContext->findId( iorString.in() );
4407 int id = myStudyContext->getOldId( newId );
4409 // try to find mesh data dataset
4410 if ( aTopGroup->ExistInternalObject( "Has data" ) ) {
4411 // load mesh "has data" flag
4412 aDataset = new HDFdataset( "Has data", aTopGroup );
4413 aDataset->OpenOnDisk();
4414 size = aDataset->GetSize();
4415 char* strHasData = new char[ size ];
4416 aDataset->ReadFromDisk( strHasData );
4417 aDataset->CloseOnDisk();
4418 if ( strcmp( strHasData, "1") == 0 ) {
4419 // read mesh data from MED file
4420 // myReader.SetMesh( mySMESHDSMesh );
4421 // myReader.SetMeshId( id );
4422 // myReader.Perform();
4425 delete [] strHasData;
4428 // Try to get applied ALGORITHMS (mesh is not cleared by algo addition because
4429 // nodes and elements are not yet put into sub-meshes)
4430 if ( aTopGroup->ExistInternalObject( "Applied Algorithms" ) ) {
4431 aGroup = new HDFgroup( "Applied Algorithms", aTopGroup );
4432 aGroup->OpenOnDisk();
4433 // get number of applied algorithms
4434 int aNbSubObjects = aGroup->nInternalObjects();
4435 if(MYDEBUG) MESSAGE( "VSR - number of applied algos " << aNbSubObjects );
4436 for ( int j = 0; j < aNbSubObjects; j++ ) {
4437 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4438 aGroup->InternalObjectIndentify( j, name_dataset );
4439 // check if it is an algorithm
4440 if ( string( name_dataset ).substr( 0, 4 ) == string( "Algo" ) ) {
4441 aDataset = new HDFdataset( name_dataset, aGroup );
4442 aDataset->OpenOnDisk();
4443 size = aDataset->GetSize();
4444 char* refFromFile = new char[ size ];
4445 aDataset->ReadFromDisk( refFromFile );
4446 aDataset->CloseOnDisk();
4447 // san - it is impossible to recover applied algorithms using their entries within Load() method
4448 //SALOMEDS::SObject_wrap hypSO = myCurrentStudy->FindObjectID( refFromFile );
4449 //CORBA::Object_var hypObject = SObjectToObject( hypSO );
4450 int id = atoi( refFromFile );
4451 delete [] refFromFile;
4452 string anIOR = myStudyContext->getIORbyOldId( id );
4453 if ( !anIOR.empty() ) {
4454 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
4455 if ( !CORBA::is_nil( hypObject ) ) {
4456 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
4457 if ( !anHyp->_is_nil() && (!aShapeObject->_is_nil()
4458 || !myNewMeshImpl->HasShapeToMesh()) )
4459 myNewMeshImpl->addHypothesis( aShapeObject, anHyp );
4464 aGroup->CloseOnDisk();
4467 // try to get applied hypotheses
4468 if ( aTopGroup->ExistInternalObject( "Applied Hypotheses" ) ) {
4469 aGroup = new HDFgroup( "Applied Hypotheses", aTopGroup );
4470 aGroup->OpenOnDisk();
4471 // get number of applied hypotheses
4472 int aNbSubObjects = aGroup->nInternalObjects();
4473 for ( int j = 0; j < aNbSubObjects; j++ ) {
4474 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4475 aGroup->InternalObjectIndentify( j, name_dataset );
4476 // check if it is a hypothesis
4477 if ( string( name_dataset ).substr( 0, 3 ) == string( "Hyp" ) ) {
4478 aDataset = new HDFdataset( name_dataset, aGroup );
4479 aDataset->OpenOnDisk();
4480 size = aDataset->GetSize();
4481 char* refFromFile = new char[ size ];
4482 aDataset->ReadFromDisk( refFromFile );
4483 aDataset->CloseOnDisk();
4484 // san - it is impossible to recover applied hypotheses using their entries within Load() method
4485 //SALOMEDS::SObject_wrap hypSO = myCurrentStudy->FindObjectID( refFromFile );
4486 //CORBA::Object_var hypObject = SObjectToObject( hypSO );
4487 int id = atoi( refFromFile );
4488 delete [] refFromFile;
4489 string anIOR = myStudyContext->getIORbyOldId( id );
4490 if ( !anIOR.empty() ) {
4491 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
4492 if ( !CORBA::is_nil( hypObject ) ) {
4493 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
4494 if ( !anHyp->_is_nil() && (!aShapeObject->_is_nil()
4495 || !myNewMeshImpl->HasShapeToMesh()) )
4496 myNewMeshImpl->addHypothesis( aShapeObject, anHyp );
4501 aGroup->CloseOnDisk();
4504 // --> try to find SUB-MESHES containers for each type of submesh
4505 for ( int j = GetSubMeshOnVertexTag(); j <= GetSubMeshOnCompoundTag(); j++ ) {
4506 const char* name_meshgroup = 0;
4507 if ( j == GetSubMeshOnVertexTag() )
4508 name_meshgroup = "SubMeshes On Vertex";
4509 else if ( j == GetSubMeshOnEdgeTag() )
4510 name_meshgroup = "SubMeshes On Edge";
4511 else if ( j == GetSubMeshOnWireTag() )
4512 name_meshgroup = "SubMeshes On Wire";
4513 else if ( j == GetSubMeshOnFaceTag() )
4514 name_meshgroup = "SubMeshes On Face";
4515 else if ( j == GetSubMeshOnShellTag() )
4516 name_meshgroup = "SubMeshes On Shell";
4517 else if ( j == GetSubMeshOnSolidTag() )
4518 name_meshgroup = "SubMeshes On Solid";
4519 else if ( j == GetSubMeshOnCompoundTag() )
4520 name_meshgroup = "SubMeshes On Compound";
4522 // try to get submeshes container HDF group
4523 if ( aTopGroup->ExistInternalObject( name_meshgroup ) ) {
4524 // open submeshes containers HDF group
4525 aGroup = new HDFgroup( name_meshgroup, aTopGroup );
4526 aGroup->OpenOnDisk();
4528 // get number of submeshes
4529 int aNbSubMeshes = aGroup->nInternalObjects();
4530 for ( int k = 0; k < aNbSubMeshes; k++ ) {
4532 char name_submeshgroup[ HDF_NAME_MAX_LEN+1 ];
4533 aGroup->InternalObjectIndentify( k, name_submeshgroup );
4534 if ( strncmp( name_submeshgroup, "SubMesh", 7 ) == 0 ) {
4535 // --> get submesh id
4536 int subid = atoi( name_submeshgroup + 7 );
4539 // open submesh HDF group
4540 aSubGroup = new HDFgroup( name_submeshgroup, aGroup );
4541 aSubGroup->OpenOnDisk();
4543 // try to read and set reference to subshape
4544 GEOM::GEOM_Object_var aSubShapeObject;
4545 SMESH::SMESH_subMesh_var aSubMesh;
4547 if ( aSubGroup->ExistInternalObject( "Ref on shape" ) ) {
4548 // load submesh "Ref on shape" - it's an entry to SObject
4549 aDataset = new HDFdataset( "Ref on shape", aSubGroup );
4550 aDataset->OpenOnDisk();
4551 size = aDataset->GetSize();
4552 char* refFromFile = new char[ size ];
4553 aDataset->ReadFromDisk( refFromFile );
4554 aDataset->CloseOnDisk();
4555 if ( strlen( refFromFile ) > 0 ) {
4556 SALOMEDS::SObject_wrap subShapeSO = myCurrentStudy->FindObjectID( refFromFile );
4557 CORBA::Object_var subShapeObject = SObjectToObject( subShapeSO );
4558 if ( !CORBA::is_nil( subShapeObject ) ) {
4559 aSubShapeObject = GEOM::GEOM_Object::_narrow( subShapeObject );
4560 if ( !aSubShapeObject->_is_nil() )
4561 aSubMesh = SMESH::SMESH_subMesh::_duplicate
4562 ( myNewMeshImpl->createSubMesh( aSubShapeObject ) );
4563 if ( aSubMesh->_is_nil() )
4565 string iorSubString = GetORB()->object_to_string( aSubMesh );
4566 int newSubId = myStudyContext->findId( iorSubString );
4567 myStudyContext->mapOldToNew( subid, newSubId );
4572 if ( aSubMesh->_is_nil() )
4575 // try to get applied algorithms
4576 if ( aSubGroup->ExistInternalObject( "Applied Algorithms" ) ) {
4577 // open "applied algorithms" HDF group
4578 aSubSubGroup = new HDFgroup( "Applied Algorithms", aSubGroup );
4579 aSubSubGroup->OpenOnDisk();
4580 // get number of applied algorithms
4581 int aNbSubObjects = aSubSubGroup->nInternalObjects();
4582 for ( int l = 0; l < aNbSubObjects; l++ ) {
4583 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4584 aSubSubGroup->InternalObjectIndentify( l, name_dataset );
4585 // check if it is an algorithm
4586 if ( strncmp( name_dataset, "Algo", 4 ) == 0 ) {
4587 aDataset = new HDFdataset( name_dataset, aSubSubGroup );
4588 aDataset->OpenOnDisk();
4589 size = aDataset->GetSize();
4590 char* refFromFile = new char[ size ];
4591 aDataset->ReadFromDisk( refFromFile );
4592 aDataset->CloseOnDisk();
4594 int id = atoi( refFromFile );
4595 string anIOR = myStudyContext->getIORbyOldId( id );
4596 if ( !anIOR.empty() ) {
4597 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
4598 if ( !CORBA::is_nil( hypObject ) ) {
4599 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
4600 if ( !anHyp->_is_nil() && !aShapeObject->_is_nil() )
4601 myNewMeshImpl->addHypothesis( aSubShapeObject, anHyp );
4606 // close "applied algorithms" HDF group
4607 aSubSubGroup->CloseOnDisk();
4610 // try to get applied hypotheses
4611 if ( aSubGroup->ExistInternalObject( "Applied Hypotheses" ) ) {
4612 // open "applied hypotheses" HDF group
4613 aSubSubGroup = new HDFgroup( "Applied Hypotheses", aSubGroup );
4614 aSubSubGroup->OpenOnDisk();
4615 // get number of applied hypotheses
4616 int aNbSubObjects = aSubSubGroup->nInternalObjects();
4617 for ( int l = 0; l < aNbSubObjects; l++ ) {
4618 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4619 aSubSubGroup->InternalObjectIndentify( l, name_dataset );
4620 // check if it is a hypothesis
4621 if ( string( name_dataset ).substr( 0, 3 ) == string( "Hyp" ) ) {
4622 aDataset = new HDFdataset( name_dataset, aSubSubGroup );
4623 aDataset->OpenOnDisk();
4624 size = aDataset->GetSize();
4625 char* refFromFile = new char[ size ];
4626 aDataset->ReadFromDisk( refFromFile );
4627 aDataset->CloseOnDisk();
4629 int id = atoi( refFromFile );
4630 string anIOR = myStudyContext->getIORbyOldId( id );
4631 if ( !anIOR.empty() ) {
4632 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
4633 if ( !CORBA::is_nil( hypObject ) ) {
4634 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
4635 if ( !anHyp->_is_nil() && !aShapeObject->_is_nil() )
4636 myNewMeshImpl->addHypothesis( aSubShapeObject, anHyp );
4641 // close "APPLIED HYPOTHESES" hdf group
4642 aSubSubGroup->CloseOnDisk();
4645 // close SUB-MESH hdf group
4646 aSubGroup->CloseOnDisk();
4649 // close SUB-MESHES containers hdf group
4650 aGroup->CloseOnDisk();
4654 // try to get GROUPS
4655 for ( int ii = GetNodeGroupsTag(); ii <= GetBallElementsGroupsTag(); ii++ ) {
4656 char name_group[ 30 ];
4657 if ( ii == GetNodeGroupsTag() )
4658 strcpy( name_group, "Groups of Nodes" );
4659 else if ( ii == GetEdgeGroupsTag() )
4660 strcpy( name_group, "Groups of Edges" );
4661 else if ( ii == GetFaceGroupsTag() )
4662 strcpy( name_group, "Groups of Faces" );
4663 else if ( ii == GetVolumeGroupsTag() )
4664 strcpy( name_group, "Groups of Volumes" );
4665 else if ( ii == Get0DElementsGroupsTag() )
4666 strcpy( name_group, "Groups of 0D Elements" );
4667 else if ( ii == GetBallElementsGroupsTag() )
4668 strcpy( name_group, "Groups of Balls" );
4670 if ( aTopGroup->ExistInternalObject( name_group ) ) {
4671 aGroup = new HDFgroup( name_group, aTopGroup );
4672 aGroup->OpenOnDisk();
4673 // get number of groups
4674 int aNbSubObjects = aGroup->nInternalObjects();
4675 for ( int j = 0; j < aNbSubObjects; j++ ) {
4676 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4677 aGroup->InternalObjectIndentify( j, name_dataset );
4678 // check if it is an group
4679 if ( string( name_dataset ).substr( 0, 5 ) == string( "Group" ) ) {
4681 int subid = atoi( string( name_dataset ).substr( 5 ).c_str() );
4684 aDataset = new HDFdataset( name_dataset, aGroup );
4685 aDataset->OpenOnDisk();
4687 // Retrieve actual group name
4688 size = aDataset->GetSize();
4689 char* nameFromFile = new char[ size ];
4690 aDataset->ReadFromDisk( nameFromFile );
4691 aDataset->CloseOnDisk();
4693 // Try to find a shape reference
4694 TopoDS_Shape aShape;
4695 char aRefName[ 30 ];
4696 sprintf( aRefName, "Ref on shape %d", subid);
4697 if ( aGroup->ExistInternalObject( aRefName ) ) {
4698 // load mesh "Ref on shape" - it's an entry to SObject
4699 aDataset = new HDFdataset( aRefName, aGroup );
4700 aDataset->OpenOnDisk();
4701 size = aDataset->GetSize();
4702 char* refFromFile = new char[ size ];
4703 aDataset->ReadFromDisk( refFromFile );
4704 aDataset->CloseOnDisk();
4705 if ( strlen( refFromFile ) > 0 ) {
4706 SALOMEDS::SObject_wrap shapeSO = myCurrentStudy->FindObjectID( refFromFile );
4707 CORBA::Object_var shapeObject = SObjectToObject( shapeSO );
4708 if ( !CORBA::is_nil( shapeObject ) ) {
4709 aShapeObject = GEOM::GEOM_Object::_narrow( shapeObject );
4710 if ( !aShapeObject->_is_nil() )
4711 aShape = GeomObjectToShape( aShapeObject );
4715 // Try to read a filter of SMESH_GroupOnFilter
4716 SMESH::Filter_var filter;
4717 SMESH_PredicatePtr predicate;
4718 std::string hdfGrpName = "Filter " + SMESH_Comment(subid);
4719 if ( aGroup->ExistInternalObject( hdfGrpName.c_str() ))
4721 aDataset = new HDFdataset( hdfGrpName.c_str(), aGroup );
4722 aDataset->OpenOnDisk();
4723 size = aDataset->GetSize();
4724 char* persistStr = new char[ size ];
4725 aDataset->ReadFromDisk( persistStr );
4726 aDataset->CloseOnDisk();
4727 if ( strlen( persistStr ) > 0 ) {
4728 filter = SMESH_GroupOnFilter_i::StringToFilter( persistStr );
4729 predicate = SMESH_GroupOnFilter_i::GetPredicate( filter );
4730 filters.push_back( filter );
4734 // Create group servant
4735 SMESH::ElementType type = (SMESH::ElementType)(ii - GetNodeGroupsTag() + 1);
4736 SMESH::SMESH_GroupBase_var aNewGroup = SMESH::SMESH_GroupBase::_duplicate
4737 ( myNewMeshImpl->createGroup( type, nameFromFile, aShape, predicate ) );
4738 // Obtain a SMESHDS_Group object
4739 if ( aNewGroup->_is_nil() )
4742 string iorSubString = GetORB()->object_to_string( aNewGroup );
4743 int newSubId = myStudyContext->findId( iorSubString );
4744 myStudyContext->mapOldToNew( subid, newSubId );
4746 SMESH_GroupBase_i* aGroupImpl = SMESH::DownCast< SMESH_GroupBase_i*>( aNewGroup );
4750 if ( SMESH_GroupOnFilter_i* aFilterGroup =
4751 dynamic_cast< SMESH_GroupOnFilter_i*>( aGroupImpl ))
4753 aFilterGroup->SetFilter( filter );
4754 filter->UnRegister();
4756 SMESHDS_GroupBase* aGroupBaseDS = aGroupImpl->GetGroupDS();
4757 if ( !aGroupBaseDS )
4760 aGroupBaseDS->SetStoreName( name_dataset );
4763 // Read color of the group
4764 char aGroupColorName[ 30 ];
4765 sprintf( aGroupColorName, "ColorGroup %d", subid);
4766 if ( aGroup->ExistInternalObject( aGroupColorName ) )
4768 aDataset = new HDFdataset( aGroupColorName, aGroup );
4769 aDataset->OpenOnDisk();
4770 size = aDataset->GetSize();
4771 double* anRGB = new double[ size ];
4772 aDataset->ReadFromDisk( anRGB );
4773 aDataset->CloseOnDisk();
4774 Quantity_Color aColor( anRGB[0], anRGB[1], anRGB[2], Quantity_TOC_RGB );
4775 aGroupBaseDS->SetColor( aColor );
4779 aGroup->CloseOnDisk();
4783 // instead of reading mesh data, we read only brief information of all
4784 // objects: mesh, groups, sub-meshes (issue 0021208 )
4787 SMESH_PreMeshInfo::LoadFromFile( myNewMeshImpl, id,
4788 meshfile.ToCString(), filename.ToCString(),
4792 // read Sub-Mesh ORDER if any
4793 if( aTopGroup->ExistInternalObject( "Mesh Order" ) ) {
4794 aDataset = new HDFdataset( "Mesh Order", aTopGroup );
4795 aDataset->OpenOnDisk();
4796 size = aDataset->GetSize();
4797 int* smIDs = new int[ size ];
4798 aDataset->ReadFromDisk( smIDs );
4799 aDataset->CloseOnDisk();
4800 TListOfListOfInt anOrderIds;
4801 anOrderIds.push_back( TListOfInt() );
4802 for ( int i = 0; i < size; i++ )
4803 if ( smIDs[ i ] < 0 ) // is separator
4804 anOrderIds.push_back( TListOfInt() );
4806 anOrderIds.back().push_back(smIDs[ i ]);
4808 myNewMeshImpl->GetImpl().SetMeshOrder( anOrderIds );
4812 // update hyps needing full mesh data restored (issue 20918)
4813 for ( hyp_data = hypDataList.begin(); hyp_data != hypDataList.end(); ++hyp_data )
4815 SMESH_Hypothesis_i* hyp = hyp_data->first;
4816 hyp->UpdateAsMeshesRestored();
4819 // notify algos on completed restoration to set sub-mesh event listeners
4820 for ( meshi_group = meshGroupList.begin(); meshi_group != meshGroupList.end(); ++meshi_group )
4822 SMESH_Mesh_i* myNewMeshImpl = meshi_group->first;
4823 ::SMESH_Mesh& myLocMesh = myNewMeshImpl->GetImpl();
4825 TopoDS_Shape myLocShape;
4826 if(myLocMesh.HasShapeToMesh())
4827 myLocShape = myLocMesh.GetShapeToMesh();
4829 myLocShape = SMESH_Mesh::PseudoShape();
4831 myLocMesh.GetSubMesh(myLocShape)->
4832 ComputeStateEngine (SMESH_subMesh::SUBMESH_RESTORED);
4835 // let filters detect dependency on mesh groups via FT_BelongToMeshGroup predicate (22877)
4836 list< SMESH::Filter_var >::iterator f = filters.begin();
4837 for ( ; f != filters.end(); ++f )
4838 if ( SMESH::Filter_i * fi = SMESH::DownCast< SMESH::Filter_i*>( *f ))
4839 fi->FindBaseObjects();
4844 aTopGroup->CloseOnDisk();
4847 aFile->CloseOnDisk();
4850 // Remove temporary files created from the stream
4853 SMESH_File meshFile( meshfile.ToCString() );
4854 if ( !meshFile ) // no meshfile exists
4856 SALOMEDS_Tool::RemoveTemporaryFiles( tmpDir.ToCString(), aFileSeq.in(), true );
4860 Engines::Container_var container = GetContainerRef();
4861 if ( Engines_Container_i* container_i = SMESH::DownCast<Engines_Container_i*>( container ))
4863 container_i->registerTemporaryFile( filename.ToCString() );
4864 container_i->registerTemporaryFile( meshfile.ToCString() );
4865 container_i->registerTemporaryFile( tmpDir.ToCString() );
4869 pd << ""; // prevent optimizing pd out
4871 // creation of tree nodes for all data objects in the study
4872 // to support tree representation customization and drag-n-drop:
4873 SALOMEDS::Study_var study = theComponent->GetStudy();
4874 SALOMEDS::UseCaseBuilder_wrap useCaseBuilder = study->GetUseCaseBuilder();
4875 if ( !useCaseBuilder->IsUseCaseNode( theComponent ) ) {
4876 useCaseBuilder->SetRootCurrent();
4877 useCaseBuilder->Append( theComponent ); // component object is added as the top level item
4878 SALOMEDS::ChildIterator_wrap it = study->NewChildIterator( theComponent );
4879 for (it->InitEx(true); it->More(); it->Next()) {
4880 useCaseBuilder->AppendTo( it->Value()->GetFather(), it->Value() );
4887 //=============================================================================
4889 * SMESH_Gen_i::LoadASCII
4891 * Load SMESH module's data in ASCII format
4893 //=============================================================================
4895 bool SMESH_Gen_i::LoadASCII( SALOMEDS::SComponent_ptr theComponent,
4896 const SALOMEDS::TMPFile& theStream,
4898 bool isMultiFile ) {
4899 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::LoadASCII" );
4900 return Load( theComponent, theStream, theURL, isMultiFile );
4902 //before call main ::Load method it's need for decipher text format to
4903 //binary ( "|xx" => x' )
4904 int size = theStream.length();
4905 if ( int((size / 3 )*3) != size ) //error size of buffer
4908 int real_size = int(size / 3);
4910 _CORBA_Octet* buffer = new _CORBA_Octet[real_size];
4913 unsigned int c = -1;
4914 for ( int i = 0; i < real_size; i++ )
4916 memcpy( &(tmp[0]), &(theStream[i*3+1]), 2 );
4917 sscanf( tmp, "%x", &c );
4918 sprintf( (char*)&(buffer[i]), "%c", (char)c );
4921 SALOMEDS::TMPFile_var aRealStreamFile = new SALOMEDS::TMPFile(real_size, real_size, buffer, 1);
4923 return Load( theComponent, *(aRealStreamFile._retn()), theURL, isMultiFile );
4926 //=============================================================================
4928 * SMESH_Gen_i::Close
4930 * Clears study-connected data when it is closed
4932 //=============================================================================
4934 void SMESH_Gen_i::Close( SALOMEDS::SComponent_ptr theComponent )
4936 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Close" );
4938 // set correct current study
4939 SALOMEDS::Study_var study = theComponent->GetStudy();
4940 if ( study->StudyId() != GetCurrentStudyID())
4941 setCurrentStudy( study, /*IsBeingClosed=*/true );
4943 // Clear study contexts data
4944 int studyId = GetCurrentStudyID();
4945 if ( myStudyContextMap.find( studyId ) != myStudyContextMap.end() ) {
4946 delete myStudyContextMap[ studyId ];
4947 myStudyContextMap.erase( studyId );
4950 // remove the tmp files meshes are loaded from
4951 SMESH_PreMeshInfo::RemoveStudyFiles_TMP_METHOD( theComponent );
4953 myCurrentStudy = SALOMEDS::Study::_nil();
4957 //=============================================================================
4959 * SMESH_Gen_i::ComponentDataType
4961 * Get component data type
4963 //=============================================================================
4965 char* SMESH_Gen_i::ComponentDataType()
4967 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::ComponentDataType" );
4968 return CORBA::string_dup( "SMESH" );
4972 //=============================================================================
4974 * SMESH_Gen_i::IORToLocalPersistentID
4976 * Transform data from transient form to persistent
4978 //=============================================================================
4980 char* SMESH_Gen_i::IORToLocalPersistentID( SALOMEDS::SObject_ptr /*theSObject*/,
4981 const char* IORString,
4982 CORBA::Boolean /*isMultiFile*/,
4983 CORBA::Boolean /*isASCII*/ )
4985 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::IORToLocalPersistentID" );
4986 StudyContext* myStudyContext = GetCurrentStudyContext();
4988 if ( myStudyContext && strcmp( IORString, "" ) != 0 ) {
4989 int anId = myStudyContext->findId( IORString );
4991 if(MYDEBUG) MESSAGE( "VSR " << anId )
4993 sprintf( strId, "%d", anId );
4994 return CORBA::string_dup( strId );
4997 return CORBA::string_dup( "" );
5000 //=============================================================================
5002 * SMESH_Gen_i::LocalPersistentIDToIOR
5004 * Transform data from persistent form to transient
5006 //=============================================================================
5008 char* SMESH_Gen_i::LocalPersistentIDToIOR( SALOMEDS::SObject_ptr /*theSObject*/,
5009 const char* aLocalPersistentID,
5010 CORBA::Boolean /*isMultiFile*/,
5011 CORBA::Boolean /*isASCII*/ )
5013 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::LocalPersistentIDToIOR(): id = " << aLocalPersistentID );
5014 StudyContext* myStudyContext = GetCurrentStudyContext();
5016 if ( myStudyContext && strcmp( aLocalPersistentID, "" ) != 0 ) {
5017 int anId = atoi( aLocalPersistentID );
5018 return CORBA::string_dup( myStudyContext->getIORbyOldId( anId ).c_str() );
5020 return CORBA::string_dup( "" );
5023 //=======================================================================
5024 //function : RegisterObject
5026 //=======================================================================
5028 int SMESH_Gen_i::RegisterObject(CORBA::Object_ptr theObject)
5030 StudyContext* myStudyContext = GetCurrentStudyContext();
5031 if ( myStudyContext && !CORBA::is_nil( theObject )) {
5032 CORBA::String_var iorString = GetORB()->object_to_string( theObject );
5033 return myStudyContext->addObject( string( iorString.in() ) );
5038 //================================================================================
5040 * \brief Return id of registered object
5041 * \param theObject - the Object
5042 * \retval int - Object id
5044 //================================================================================
5046 CORBA::Long SMESH_Gen_i::GetObjectId(CORBA::Object_ptr theObject)
5048 StudyContext* myStudyContext = GetCurrentStudyContext();
5049 if ( myStudyContext && !CORBA::is_nil( theObject )) {
5050 string iorString = GetORB()->object_to_string( theObject );
5051 return myStudyContext->findId( iorString );
5056 //=============================================================================
5058 * SMESH_Gen_i::SetName
5060 * Set a new object name
5062 //=============================================================================
5063 void SMESH_Gen_i::SetName(const char* theIOR,
5064 const char* theName)
5066 if ( theIOR && strcmp( theIOR, "" ) ) {
5067 CORBA::Object_var anObject = GetORB()->string_to_object( theIOR );
5068 SALOMEDS::SObject_wrap aSO = ObjectToSObject( myCurrentStudy, anObject );
5069 if ( !aSO->_is_nil() ) {
5070 SetName( aSO, theName );
5075 int SMESH_Gen_i::GetCurrentStudyID()
5077 return myCurrentStudy->_is_nil() || myCurrentStudy->_non_existent() ? -1 : myCurrentStudy->StudyId();
5080 // Version information
5081 char* SMESH_Gen_i::getVersion()
5083 #if SMESH_DEVELOPMENT
5084 return CORBA::string_dup(SMESH_VERSION_STR"dev");
5086 return CORBA::string_dup(SMESH_VERSION_STR);
5090 //=================================================================================
5091 // function : Move()
5092 // purpose : Moves objects to the specified position.
5093 // Is used in the drag-n-drop functionality.
5094 //=================================================================================
5095 void SMESH_Gen_i::Move( const SMESH::sobject_list& what,
5096 SALOMEDS::SObject_ptr where,
5099 if ( CORBA::is_nil( where ) ) return;
5101 SALOMEDS::Study_var study = where->GetStudy();
5102 SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();
5103 SALOMEDS::UseCaseBuilder_var useCaseBuilder = study->GetUseCaseBuilder();
5104 SALOMEDS::SComponent_var father = where->GetFatherComponent();
5105 std::string dataType = father->ComponentDataType();
5106 if ( dataType != "SMESH" ) return; // not a SMESH component
5108 SALOMEDS::SObject_var objAfter;
5109 if ( row >= 0 && useCaseBuilder->HasChildren( where ) ) {
5110 // insert at given row -> find insertion position
5111 SALOMEDS::UseCaseIterator_var useCaseIt = useCaseBuilder->GetUseCaseIterator( where );
5113 for ( i = 0; i < row && useCaseIt->More(); i++, useCaseIt->Next() );
5114 if ( i == row && useCaseIt->More() ) {
5115 objAfter = useCaseIt->Value();
5119 for ( CORBA::ULong i = 0; i < what.length(); i++ ) {
5120 SALOMEDS::SObject_var sobj = what[i];
5121 if ( CORBA::is_nil( sobj ) ) continue; // skip bad object
5122 // insert the object to the use case tree
5123 if ( !CORBA::is_nil( objAfter ) )
5124 useCaseBuilder->InsertBefore( sobj, objAfter ); // insert at given row
5126 useCaseBuilder->AppendTo( where, sobj ); // append to the end of list
5129 //================================================================================
5131 * \brief Returns true if algorithm can be used to mesh a given geometry
5132 * \param [in] theAlgoType - the algorithm type
5133 * \param [in] theLibName - a name of the Plug-in library implementing the algorithm
5134 * \param [in] theGeomObject - the geometry to mesh
5135 * \param [in] toCheckAll - if \c True, returns \c True if all shapes are meshable,
5136 * else, returns \c True if at least one shape is meshable
5137 * \return CORBA::Boolean - can or can't
5139 //================================================================================
5141 CORBA::Boolean SMESH_Gen_i::IsApplicable ( const char* theAlgoType,
5142 const char* theLibName,
5143 GEOM::GEOM_Object_ptr theGeomObject,
5144 CORBA::Boolean toCheckAll)
5148 std::string aPlatformLibName;
5149 typedef GenericHypothesisCreator_i* (*GetHypothesisCreator)(const char*);
5150 GenericHypothesisCreator_i* aCreator =
5151 getHypothesisCreator(theAlgoType, theLibName, aPlatformLibName);
5154 TopoDS_Shape shape = GeomObjectToShape( theGeomObject );
5155 return shape.IsNull() || aCreator->IsApplicable( shape, toCheckAll );
5162 SMESH_CATCH( SMESH::doNothing );
5165 cout << "SMESH_Gen_i::IsApplicable(): exception in " << ( theAlgoType ? theAlgoType : "") << endl;
5170 //=================================================================================
5171 // function : GetInsideSphere
5172 // purpose : Collect indices of elements, which are located inside the sphere
5173 //=================================================================================
5174 SMESH::long_array* SMESH_Gen_i::GetInsideSphere( SMESH::SMESH_IDSource_ptr meshPart,
5175 SMESH::ElementType theElemType,
5181 SMESH::long_array_var aResult = new SMESH::long_array();
5182 if ( meshPart->_is_nil() )
5183 return aResult._retn();
5185 // 1. Create geometrical object
5186 gp_Pnt aP( theX, theY, theZ );
5187 TopoDS_Shape aShape = BRepPrimAPI_MakeSphere( aP, theR ).Shape();
5189 std::vector<long> lst =_GetInside(meshPart, theElemType, aShape);
5191 if ( lst.size() > 0 ) {
5192 aResult->length( lst.size() );
5193 for ( size_t i = 0; i < lst.size(); i++ ) {
5194 aResult[i] = lst[i];
5197 return aResult._retn();
5200 SMESH::long_array* SMESH_Gen_i::GetInsideBox( SMESH::SMESH_IDSource_ptr meshPart,
5201 SMESH::ElementType theElemType,
5202 CORBA::Double theX1,
5203 CORBA::Double theY1,
5204 CORBA::Double theZ1,
5205 CORBA::Double theX2,
5206 CORBA::Double theY2,
5207 CORBA::Double theZ2) {
5208 SMESH::long_array_var aResult = new SMESH::long_array();
5209 if( meshPart->_is_nil() )
5210 return aResult._retn();
5212 TopoDS_Shape aShape = BRepPrimAPI_MakeBox( gp_Pnt( theX1, theY1, theZ1 ), gp_Pnt( theX2, theY2, theZ2 ) ).Shape();
5214 std::vector<long> lst =_GetInside(meshPart, theElemType, aShape);
5216 if( lst.size() > 0 ) {
5217 aResult->length( lst.size() );
5218 for ( size_t i = 0; i < lst.size(); i++ ) {
5219 aResult[i] = lst[i];
5222 return aResult._retn();
5225 SMESH::long_array* SMESH_Gen_i::GetInsideCylinder( SMESH::SMESH_IDSource_ptr meshPart,
5226 SMESH::ElementType theElemType,
5230 CORBA::Double theDX,
5231 CORBA::Double theDY,
5232 CORBA::Double theDZ,
5234 CORBA::Double theR ){
5235 SMESH::long_array_var aResult = new SMESH::long_array();
5236 if( meshPart->_is_nil() )
5237 return aResult._retn();
5239 gp_Pnt aP( theX, theY, theZ );
5240 gp_Vec aV( theDX, theDY, theDZ );
5241 gp_Ax2 anAxes (aP, aV);
5243 TopoDS_Shape aShape = BRepPrimAPI_MakeCylinder(anAxes, theR, Abs(theH)).Shape();
5245 std::vector<long> lst =_GetInside(meshPart, theElemType, aShape);
5247 if( lst.size() > 0 ) {
5248 aResult->length( lst.size() );
5249 for ( size_t i = 0; i < lst.size(); i++ ) {
5250 aResult[i] = lst[i];
5253 return aResult._retn();
5256 SMESH::long_array* SMESH_Gen_i::GetInside( SMESH::SMESH_IDSource_ptr meshPart,
5257 SMESH::ElementType theElemType,
5258 GEOM::GEOM_Object_ptr theGeom,
5259 CORBA::Double theTolerance ) {
5260 SMESH::long_array_var aResult = new SMESH::long_array();
5261 if( meshPart->_is_nil() || theGeom->_is_nil() )
5262 return aResult._retn();
5264 TopoDS_Shape aShape = GeomObjectToShape( theGeom );
5266 std::vector<long> lst =_GetInside(meshPart, theElemType, aShape, &theTolerance);
5268 if( lst.size() > 0 ) {
5269 aResult->length( lst.size() );
5270 for ( size_t i = 0; i < lst.size(); i++ ) {
5271 aResult[i] = lst[i];
5274 return aResult._retn();
5279 std::vector<long> SMESH_Gen_i::_GetInside( SMESH::SMESH_IDSource_ptr meshPart,
5280 SMESH::ElementType theElemType,
5281 TopoDS_Shape& aShape,
5282 double* theTolerance) {
5284 std::vector<long> res;
5285 SMESH::SMESH_Mesh_var mesh = meshPart->GetMesh();
5287 if ( mesh->_is_nil() )
5290 SMESH_Mesh_i* anImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
5294 const SMDS_Mesh* meshDS = anImpl->GetImpl().GetMeshDS();
5299 SMDSAbs_ElementType aType = SMDSAbs_ElementType(theElemType);
5300 SMESH::Controls::ElementsOnShape* anElementsOnShape = new SMESH::Controls::ElementsOnShape();
5301 anElementsOnShape->SetAllNodes( true );
5302 anElementsOnShape->SetMesh( meshDS );
5303 anElementsOnShape->SetShape( aShape, aType );
5306 anElementsOnShape->SetTolerance(*theTolerance);
5308 SMESH::SMESH_Mesh_var msource = SMESH::SMESH_Mesh::_narrow(meshPart);
5309 if ( !msource->_is_nil() ) { // Mesh case
5310 SMDS_ElemIteratorPtr elemIt = meshDS->elementsIterator( aType );
5312 while ( elemIt->more() ) {
5313 const SMDS_MeshElement* anElem = elemIt->next();
5314 long anId = anElem->GetID();
5315 if ( anElementsOnShape->IsSatisfy( anId ) )
5316 res.push_back( anId );
5320 SMESH::SMESH_Group_var gsource = SMESH::SMESH_Group::_narrow(meshPart);
5321 if ( !gsource->_is_nil() ) {
5322 if(theElemType == SMESH::NODE) {
5323 SMESH::long_array_var nodes = gsource->GetNodeIDs();
5324 for ( CORBA::ULong i = 0; i < nodes->length(); ++i ) {
5325 if ( const SMDS_MeshNode* node = meshDS->FindNode( nodes[i] )) {
5326 long anId = node->GetID();
5327 if ( anElementsOnShape->IsSatisfy( anId ) )
5328 res.push_back( anId );
5331 } else if (gsource->GetType() == theElemType || theElemType == SMESH::ALL ) {
5332 SMESH::long_array_var elems = gsource->GetListOfID();
5333 for ( CORBA::ULong i = 0; i < elems->length(); ++i ) {
5334 if ( const SMDS_MeshElement* elem = meshDS->FindElement( elems[i] )) {
5335 long anId = elem->GetID();
5336 if ( anElementsOnShape->IsSatisfy( anId ) )
5337 res.push_back( anId );
5342 SMESH::SMESH_subMesh_var smsource = SMESH::SMESH_subMesh::_narrow(meshPart);
5343 if ( !smsource->_is_nil() ) {
5344 SMESH::long_array_var elems = smsource->GetElementsByType( theElemType );
5345 for ( CORBA::ULong i = 0; i < elems->length(); ++i ) {
5346 const SMDS_MeshElement* elem = ( theElemType == SMESH::NODE ) ? meshDS->FindNode( elems[i] ) : meshDS->FindElement( elems[i] );
5348 long anId = elem->GetID();
5349 if ( anElementsOnShape->IsSatisfy( anId ) )
5350 res.push_back( anId );
5358 //=============================================================================
5360 * SMESHEngine_factory
5362 * C factory, accessible with dlsym, after dlopen
5364 //=============================================================================
5368 PortableServer::ObjectId* SMESHEngine_factory( CORBA::ORB_ptr orb,
5369 PortableServer::POA_ptr poa,
5370 PortableServer::ObjectId* contId,
5371 const char* instanceName,
5372 const char* interfaceName )
5374 if(MYDEBUG) MESSAGE( "PortableServer::ObjectId* SMESHEngine_factory()" );
5375 if(MYDEBUG) SCRUTE(interfaceName);
5376 SMESH_Gen_i* aSMESHGen = new SMESH_Gen_i(orb, poa, contId, instanceName, interfaceName);
5377 return aSMESHGen->getId() ;