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
26 #include <BRepPrimAPI_MakeBox.hxx>
27 #include <BRepPrimAPI_MakeCylinder.hxx>
28 #include <BRepPrimAPI_MakeSphere.hxx>
29 #include <BRep_Tool.hxx>
31 #include <TColStd_MapOfAsciiString.hxx>
32 #include <TCollection_AsciiString.hxx>
34 #include <TopExp_Explorer.hxx>
35 #include <TopTools_IndexedMapOfShape.hxx>
36 #include <TopTools_ListIteratorOfListOfShape.hxx>
37 #include <TopTools_ListOfShape.hxx>
38 #include <TopTools_MapOfShape.hxx>
40 #include <TopoDS_CompSolid.hxx>
41 #include <TopoDS_Compound.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <TopoDS_Face.hxx>
44 #include <TopoDS_Iterator.hxx>
45 #include <TopoDS_Shape.hxx>
46 #include <TopoDS_Shell.hxx>
47 #include <TopoDS_Solid.hxx>
48 #include <TopoDS_Vertex.hxx>
49 #include <TopoDS_Wire.hxx>
58 #include <libgen.h> // for basename function
62 #define LibHandle HMODULE
63 #define LoadLib( name ) LoadLibrary( name )
64 #define GetProc GetProcAddress
65 #define UnLoadLib( handle ) FreeLibrary( handle );
67 #define LibHandle void*
69 #define LoadLib( name ) dlopen( name, RTLD_LAZY | RTLD_LOCAL )
70 #else // DYNLOAD_LOCAL
71 #define LoadLib( name ) dlopen( name, RTLD_LAZY | RTLD_GLOBAL )
72 #endif // DYNLOAD_LOCAL
74 #define UnLoadLib( handle ) dlclose( handle );
77 #include "SMESH_Gen_i.hxx"
78 #include "SMESH_version.h"
80 #include "DriverMED_W_SMESHDS_Mesh.h"
81 #include "DriverMED_R_SMESHDS_Mesh.h"
83 #include "DriverCGNS_Read.hxx"
85 #include "MED_Factory.hxx"
86 #include "SMDS_EdgePosition.hxx"
87 #include "SMDS_FacePosition.hxx"
88 #include "SMDS_PolyhedralVolumeOfNodes.hxx"
89 #include "SMDS_SetIterator.hxx"
90 #include "SMDS_SpacePosition.hxx"
91 #include "SMDS_VertexPosition.hxx"
92 #include "SMESHDS_Document.hxx"
93 #include "SMESHDS_Group.hxx"
94 #include "SMESHDS_GroupOnGeom.hxx"
95 #include "SMESH_Algo_i.hxx"
96 #include "SMESH_File.hxx"
97 #include "SMESH_Group.hxx"
98 #include "SMESH_Group_i.hxx"
99 #include "SMESH_Hypothesis.hxx"
100 #include "SMESH_Hypothesis_i.hxx"
101 #include "SMESH_Mesh.hxx"
102 #include "SMESH_MeshEditor.hxx"
103 #include "SMESH_Mesh_i.hxx"
104 #include "SMESH_PreMeshInfo.hxx"
105 #include "SMESH_PythonDump.hxx"
106 #include "SMESH_ControlsDef.hxx"
107 #include "SMESH_TryCatch.hxx" // to include after OCC headers!
109 #include CORBA_SERVER_HEADER(SMESH_Group)
110 #include CORBA_SERVER_HEADER(SMESH_Filter)
111 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
114 #include <GEOM_Client.hxx>
116 #include <Basics_Utils.hxx>
117 #include <Basics_DirUtils.hxx>
119 #include <OpUtil.hxx>
120 #include <SALOMEDS_Tool.hxx>
121 #include <SALOME_Container_i.hxx>
122 #include <SALOME_LifeCycleCORBA.hxx>
123 #include <SALOME_NamingService.hxx>
124 #include <Utils_CorbaException.hxx>
125 #include <Utils_ExceptHandlers.hxx>
126 #include <Utils_SINGLETON.hxx>
127 #include <utilities.h>
129 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
130 #include CORBA_CLIENT_HEADER(SALOME_Session)
132 // helpers about SALOME::GenericObj
133 #include <SALOMEDS_wrap.hxx>
134 #include <SALOMEDS_Attributes_wrap.hxx>
135 #include <GEOM_wrap.hxx>
143 using SMESH::TPythonDump;
146 #define NUM_TMP_FILES 2
149 static int MYDEBUG = 0;
151 static int MYDEBUG = 0;
154 // Static variables definition
155 GEOM::GEOM_Gen_var SMESH_Gen_i::myGeomGen = GEOM::GEOM_Gen::_nil();
156 CORBA::ORB_var SMESH_Gen_i::myOrb;
157 PortableServer::POA_var SMESH_Gen_i::myPoa;
158 SALOME_NamingService* SMESH_Gen_i::myNS = NULL;
159 SALOME_LifeCycleCORBA* SMESH_Gen_i::myLCC = NULL;
160 SMESH_Gen_i* SMESH_Gen_i::mySMESHGen = NULL;
163 const int nbElemPerDiagonal = 10;
165 //=============================================================================
167 * GetServant [ static ]
169 * Get servant of the CORBA object
171 //=============================================================================
173 PortableServer::ServantBase_var SMESH_Gen_i::GetServant( CORBA::Object_ptr theObject )
175 if( CORBA::is_nil( theObject ) || CORBA::is_nil( GetPOA() ) )
178 PortableServer::Servant aServant = GetPOA()->reference_to_servant( theObject );
181 catch (PortableServer::POA::ObjectNotActive &ex)
183 INFOS("GetServant: ObjectNotActive");
186 catch (PortableServer::POA::WrongAdapter &ex)
188 INFOS("GetServant: WrongAdapter: OK when several servants used to build several mesh in parallel...");
191 catch (PortableServer::POA::WrongPolicy &ex)
193 INFOS("GetServant: WrongPolicy");
198 INFOS( "GetServant - Unknown exception was caught!!!" );
203 //=============================================================================
205 * SObjectToObject [ static ]
207 * Get CORBA object corresponding to the SALOMEDS::SObject
209 //=============================================================================
211 CORBA::Object_var SMESH_Gen_i::SObjectToObject( SALOMEDS::SObject_ptr theSObject )
213 SALOMEDS::GenericAttribute_wrap anAttr;
214 CORBA::Object_var anObj;
215 if ( !theSObject->_is_nil() ) {
217 if( theSObject->FindAttribute( anAttr.inout(), "AttributeIOR" ) ) {
218 SALOMEDS::AttributeIOR_wrap anIOR = anAttr;
219 CORBA::String_var aValue = anIOR->Value();
220 if( strcmp( aValue, "" ) != 0 )
221 anObj = GetORB()->string_to_object( aValue );
225 INFOS( "SObjectToObject - Unknown exception was caught!!!" );
231 //=============================================================================
235 * Get SALOME_NamingService object
237 //=============================================================================
239 SALOME_NamingService* SMESH_Gen_i::GetNS()
241 if ( myNS == NULL ) {
242 myNS = SINGLETON_<SALOME_NamingService>::Instance();
243 ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting());
244 myNS->init_orb( GetORB() );
249 //=============================================================================
253 * Get SALOME_LifeCycleCORBA object
255 //=============================================================================
256 SALOME_LifeCycleCORBA* SMESH_Gen_i::GetLCC() {
257 if ( myLCC == NULL ) {
258 myLCC = new SALOME_LifeCycleCORBA( GetNS() );
264 //=============================================================================
266 * GetGeomEngine [ static ]
268 * Get GEOM::GEOM_Gen reference
270 //=============================================================================
271 GEOM::GEOM_Gen_var SMESH_Gen_i::GetGeomEngine() {
272 //CCRT GEOM::GEOM_Gen_var aGeomEngine =
273 //CCRT GEOM::GEOM_Gen::_narrow( GetLCC()->FindOrLoad_Component("FactoryServer","GEOM") );
274 //CCRT return aGeomEngine._retn();
275 if(CORBA::is_nil(myGeomGen))
277 Engines::EngineComponent_ptr temp=GetLCC()->FindOrLoad_Component("FactoryServer","GEOM");
278 myGeomGen=GEOM::GEOM_Gen::_narrow(temp);
283 //=============================================================================
285 * SMESH_Gen_i::SMESH_Gen_i
287 * Default constructor: not for use
289 //=============================================================================
291 SMESH_Gen_i::SMESH_Gen_i()
295 //=============================================================================
297 * SMESH_Gen_i::SMESH_Gen_i
299 * Standard constructor, used with Container
301 //=============================================================================
303 SMESH_Gen_i::SMESH_Gen_i( CORBA::ORB_ptr orb,
304 PortableServer::POA_ptr poa,
305 PortableServer::ObjectId* contId,
306 const char* instanceName,
307 const char* interfaceName )
308 : Engines_Component_i( orb, poa, contId, instanceName, interfaceName )
311 myOrb = CORBA::ORB::_duplicate(orb);
312 myPoa = PortableServer::POA::_duplicate(poa);
315 _id = myPoa->activate_object( _thisObj );
317 myIsEmbeddedMode = false;
318 myShapeReader = NULL; // shape reader
320 myIsHistoricalPythonDump = true;
321 myToForgetMeshDataOnHypModif = false;
323 // set it in standalone mode only
324 //OSD::SetSignal( true );
326 // 0020605: EDF 1190 SMESH: Display performance. 80 seconds for 52000 cells.
327 // find out mode (embedded or standalone) here else
328 // meshes created before calling SMESH_Client::GetSMESHGen(), which calls
329 // SMESH_Gen_i::SetEmbeddedMode(), have wrong IsEmbeddedMode flag
330 if ( SALOME_NamingService* ns = GetNS() )
332 CORBA::Object_var obj = ns->Resolve( "/Kernel/Session" );
333 SALOME::Session_var session = SALOME::Session::_narrow( obj ) ;
334 if ( !session->_is_nil() )
336 CORBA::String_var str_host = session->getHostname();
337 CORBA::Long s_pid = session->getPID();
338 string my_host = Kernel_Utils::GetHostname();
340 long my_pid = (long)_getpid();
342 long my_pid = (long) getpid();
344 SetEmbeddedMode( s_pid == my_pid && my_host == str_host.in() );
349 //=============================================================================
351 * SMESH_Gen_i::~SMESH_Gen_i
355 //=============================================================================
357 SMESH_Gen_i::~SMESH_Gen_i()
359 // delete hypothesis creators
360 map<string, GenericHypothesisCreator_i*>::iterator itHyp, itHyp2;
361 for (itHyp = myHypCreatorMap.begin(); itHyp != myHypCreatorMap.end(); itHyp++)
363 // same creator can be mapped under different names
364 GenericHypothesisCreator_i* creator = (*itHyp).second;
368 for (itHyp2 = itHyp; itHyp2 != myHypCreatorMap.end(); itHyp2++)
369 if ( creator == (*itHyp2).second )
370 (*itHyp2).second = 0;
372 myHypCreatorMap.clear();
374 // Clear study contexts data
375 map<int, StudyContext*>::iterator it;
376 for ( it = myStudyContextMap.begin(); it != myStudyContextMap.end(); ++it ) {
379 myStudyContextMap.clear();
380 // delete shape reader
382 delete myShapeReader;
384 //=============================================================================
386 * SMESH_Gen_i::getHypothesisCreator
388 * Get hypothesis creator
390 //=============================================================================
391 GenericHypothesisCreator_i* SMESH_Gen_i::getHypothesisCreator(const char* theHypName,
392 const char* theLibName,
393 std::string& thePlatformLibName)
394 throw (SALOME::SALOME_Exception)
396 std::string aPlatformLibName;
397 /* It's Need to translate lib name for WIN32 or X platform */
398 if ( theLibName && theLibName[0] != '\0' )
400 int libNameLen = strlen(theLibName);
401 //check for old format "libXXXXXXX.so"
402 if (libNameLen > 7 &&
403 !strncmp( theLibName, "lib", 3 ) &&
404 !strcmp( theLibName+libNameLen-3, ".so" ))
408 aPlatformLibName = std::string( theLibName+3, libNameLen-6 ) + ".dll";
409 #elif defined(__APPLE__)
410 aPlatformLibName = std::string( theLibName, libNameLen-3 ) + ".dylib";
412 aPlatformLibName = theLibName;
417 //try to use new format
419 aPlatformLibName = theLibName;
420 aPlatformLibName += ".dll";
421 #elif defined(__APPLE__)
422 aPlatformLibName = std::string( "lib" ) + std::string( theLibName ) + ".dylib";
424 aPlatformLibName = std::string( "lib" ) + std::string( theLibName ) + ".so";
428 thePlatformLibName = aPlatformLibName;
430 Unexpect aCatch(SALOME_SalomeException);
431 if(MYDEBUG) MESSAGE( "Create Hypothesis <" << theHypName << "> from " << aPlatformLibName);
433 typedef GenericHypothesisCreator_i* (*GetHypothesisCreator)(const char* );
434 GenericHypothesisCreator_i* aCreator;
437 // check, if creator for this hypothesis type already exists
438 if (myHypCreatorMap.find(string(theHypName)) == myHypCreatorMap.end())
440 // load plugin library
441 if(MYDEBUG) MESSAGE("Loading server meshers plugin library ...");
442 LibHandle libHandle = LoadLib( aPlatformLibName.c_str() );
445 // report any error, if occurred
447 const char* anError = dlerror();
448 throw(SALOME_Exception(anError));
450 throw(SALOME_Exception(LOCALIZED( "Can't load server meshers plugin library" )));
454 // get method, returning hypothesis creator
455 if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
456 GetHypothesisCreator procHandle =
457 (GetHypothesisCreator)GetProc( libHandle, "GetHypothesisCreator" );
460 throw(SALOME_Exception(LOCALIZED("bad hypothesis plugin library")));
461 UnLoadLib(libHandle);
464 // get hypothesis creator
465 if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << theHypName);
466 aCreator = procHandle(theHypName);
469 throw(SALOME_Exception(LOCALIZED("no such a hypothesis in this plugin")));
471 // map hypothesis creator to a hypothesis name
472 myHypCreatorMap[string(theHypName)] = aCreator;
477 return myHypCreatorMap[string(theHypName)];
480 catch (SALOME_Exception& S_ex)
482 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
487 //=============================================================================
489 * SMESH_Gen_i::createHypothesis
491 * Create hypothesis of given type
493 //=============================================================================
494 SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::createHypothesis(const char* theHypName,
495 const char* theLibName)
497 SMESH_Hypothesis_i* myHypothesis_i = 0;
498 SMESH::SMESH_Hypothesis_var hypothesis_i;
499 std::string aPlatformLibName;
500 GenericHypothesisCreator_i* aCreator =
501 getHypothesisCreator(theHypName, theLibName, aPlatformLibName);
503 // create a new hypothesis object, store its ref. in studyContext
504 myHypothesis_i = aCreator->Create(myPoa, GetCurrentStudyID(), &myGen);
507 myHypothesis_i->SetLibName( aPlatformLibName.c_str() ); // for persistency assurance
508 CORBA::String_var hypName = myHypothesis_i->GetName();
509 myHypCreatorMap[ hypName.in() ] = aCreator;
511 // activate the CORBA servant of hypothesis
512 hypothesis_i = myHypothesis_i->_this();
513 int nextId = RegisterObject( hypothesis_i );
514 if(MYDEBUG) { MESSAGE( "Add hypo to map with id = "<< nextId ); }
515 else { nextId = 0; } // avoid "unused variable" warning in release mode
517 return hypothesis_i._retn();
520 //=============================================================================
522 * SMESH_Gen_i::createMesh
524 * Create empty mesh on shape
526 //=============================================================================
527 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::createMesh()
528 throw ( SALOME::SALOME_Exception )
530 Unexpect aCatch(SALOME_SalomeException);
531 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::createMesh" );
533 // Get or create the GEOM_Client instance
535 // create a new mesh object servant, store it in a map in study context
536 SMESH_Mesh_i* meshServant = new SMESH_Mesh_i( GetPOA(), this, GetCurrentStudyID() );
537 // create a new mesh object
538 if(MYDEBUG) MESSAGE("myIsEmbeddedMode " << myIsEmbeddedMode);
539 meshServant->SetImpl( myGen.CreateMesh( GetCurrentStudyID(), myIsEmbeddedMode ));
541 // activate the CORBA servant of Mesh
542 SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( meshServant->_this() );
543 int nextId = RegisterObject( mesh );
544 if(MYDEBUG) { MESSAGE( "Add mesh to map with id = "<< nextId); }
545 else { nextId = 0; } // avoid "unused variable" warning in release mode
548 catch (SALOME_Exception& S_ex) {
549 THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
551 return SMESH::SMESH_Mesh::_nil();
554 //=============================================================================
556 * SMESH_Gen_i::GetShapeReader
560 //=============================================================================
561 GEOM_Client* SMESH_Gen_i::GetShapeReader()
563 // create shape reader if necessary
564 if ( !myShapeReader )
565 myShapeReader = new GEOM_Client(GetContainerRef());
566 ASSERT( myShapeReader );
567 return myShapeReader;
570 //=============================================================================
572 * SMESH_Gen_i::SetGeomEngine
574 * Set GEOM::GEOM_Gen reference
576 //=============================================================================
577 //GEOM::GEOM_Gen_ptr SMESH_Gen_i::SetGeomEngine( const char* containerLoc )
578 void SMESH_Gen_i::SetGeomEngine( GEOM::GEOM_Gen_ptr geomcompo )
580 //Engines::Component_ptr temp=GetLCC()->FindOrLoad_Component(containerLoc,"GEOM");
581 //myGeomGen=GEOM::GEOM_Gen::_narrow(temp);
582 myGeomGen=GEOM::GEOM_Gen::_duplicate(geomcompo);
586 //=============================================================================
588 * SMESH_Gen_i::SetEmbeddedMode
592 //=============================================================================
594 void SMESH_Gen_i::SetEmbeddedMode( CORBA::Boolean theMode )
596 myIsEmbeddedMode = theMode;
598 if ( !myIsEmbeddedMode ) {
599 //PAL10867: disable signals catching with "noexcepthandler" option
600 char* envNoCatchSignals = getenv("NOT_INTERCEPT_SIGNALS");
601 if (!envNoCatchSignals || !atoi(envNoCatchSignals))
606 char* envDisableFPE = getenv("DISABLE_FPE");
607 if (envDisableFPE && atoi(envDisableFPE))
612 OSD::SetSignal( raiseFPE );
614 // else OSD::SetSignal() is called in GUI
618 //=============================================================================
620 * SMESH_Gen_i::IsEmbeddedMode
624 //=============================================================================
626 CORBA::Boolean SMESH_Gen_i::IsEmbeddedMode()
628 return myIsEmbeddedMode;
631 //=============================================================================
633 * SMESH_Gen_i::SetCurrentStudy
637 //=============================================================================
639 void SMESH_Gen_i::SetCurrentStudy( SALOMEDS::Study_ptr theStudy )
641 setCurrentStudy( theStudy );
644 void SMESH_Gen_i::setCurrentStudy( SALOMEDS::Study_ptr theStudy,
645 bool theStudyIsBeingClosed)
647 int curStudyId = GetCurrentStudyID();
648 MESSAGE("curStudyId " << curStudyId);
649 myCurrentStudy = SALOMEDS::Study::_duplicate( theStudy );
650 // create study context, if it doesn't exist and set current study
651 int studyId = GetCurrentStudyID();
652 MESSAGE("studyId " << studyId);
653 if ( myStudyContextMap.find( studyId ) == myStudyContextMap.end() )
654 myStudyContextMap[ studyId ] = new StudyContext;
656 // myCurrentStudy may be nil
657 if ( !theStudyIsBeingClosed && !CORBA::is_nil( myCurrentStudy ) ) {
658 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
659 SALOMEDS::SComponent_wrap GEOM_var = myCurrentStudy->FindComponent( "GEOM" );
660 if( !GEOM_var->_is_nil() )
661 aStudyBuilder->LoadWith( GEOM_var, GetGeomEngine() );
662 // NPAL16168, issue 0020210
663 // Let meshes update their data depending on GEOM groups that could change
664 if ( curStudyId != studyId )
666 MESSAGE("curStudyId " << curStudyId << " studyId " << studyId);
667 CORBA::String_var compDataType = ComponentDataType();
668 SALOMEDS::SComponent_wrap me = myCurrentStudy->FindComponent( compDataType.in() );
669 if ( !me->_is_nil() ) {
670 SALOMEDS::ChildIterator_wrap anIter = myCurrentStudy->NewChildIterator( me );
671 for ( ; anIter->More(); anIter->Next() ) {
672 SALOMEDS::SObject_wrap so = anIter->Value();
673 CORBA::Object_var ior = SObjectToObject( so );
674 if ( SMESH_Mesh_i* mesh = SMESH::DownCast<SMESH_Mesh_i*>( ior ))
675 mesh->CheckGeomModif();
682 //=============================================================================
684 * SMESH_Gen_i::GetCurrentStudy
688 //=============================================================================
690 SALOMEDS::Study_ptr SMESH_Gen_i::GetCurrentStudy()
692 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetCurrentStudy: study Id = " << GetCurrentStudyID() );
693 if ( GetCurrentStudyID() < 0 )
694 return SALOMEDS::Study::_nil();
695 return SALOMEDS::Study::_duplicate( myCurrentStudy );
698 //=============================================================================
700 * SMESH_Gen_i::GetCurrentStudyContext
702 * Get current study context
704 //=============================================================================
705 StudyContext* SMESH_Gen_i::GetCurrentStudyContext()
707 if ( !CORBA::is_nil( myCurrentStudy ) &&
708 myStudyContextMap.find( GetCurrentStudyID() ) != myStudyContextMap.end() )
709 return myStudyContextMap[ myCurrentStudy->StudyId() ];
714 //=============================================================================
716 * SMESH_Gen_i::CreateHypothesis
718 * Create hypothesis/algorothm of given type and publish it in the study
720 //=============================================================================
722 SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::CreateHypothesis( const char* theHypName,
723 const char* theLibName )
724 throw ( SALOME::SALOME_Exception )
726 Unexpect aCatch(SALOME_SalomeException);
727 // Create hypothesis/algorithm
728 SMESH::SMESH_Hypothesis_var hyp = this->createHypothesis( theHypName, theLibName );
730 // Publish hypothesis/algorithm in the study
731 if ( CanPublishInStudy( hyp ) ) {
732 SALOMEDS::SObject_wrap aSO = PublishHypothesis( myCurrentStudy, hyp );
733 if ( !aSO->_is_nil() ) {
734 // Update Python script
735 TPythonDump() << aSO << " = " << this << ".CreateHypothesis('"
736 << theHypName << "', '" << theLibName << "')";
743 //================================================================================
745 * \brief Return a hypothesis holding parameter values corresponding either to the mesh
746 * existing on the given geometry or to size of the geometry.
747 * \param theHypType - hypothesis type name
748 * \param theLibName - plugin library name
749 * \param theMesh - The mesh of interest
750 * \param theGeom - The shape to get parameter values from
751 * \retval SMESH::SMESH_Hypothesis_ptr - The returned hypothesis may be the one existing
752 * in a study and used to compute the mesh, or a temporary one created just to pass
755 //================================================================================
757 SMESH::SMESH_Hypothesis_ptr
758 SMESH_Gen_i::GetHypothesisParameterValues (const char* theHypType,
759 const char* theLibName,
760 SMESH::SMESH_Mesh_ptr theMesh,
761 GEOM::GEOM_Object_ptr theGeom,
762 CORBA::Boolean byMesh)
763 throw ( SALOME::SALOME_Exception )
765 Unexpect aCatch(SALOME_SalomeException);
766 if ( byMesh && CORBA::is_nil( theMesh ) )
767 return SMESH::SMESH_Hypothesis::_nil();
768 if ( byMesh && CORBA::is_nil( theGeom ) )
769 return SMESH::SMESH_Hypothesis::_nil();
771 // -----------------------------------------------
772 // find hypothesis used to mesh theGeom
773 // -----------------------------------------------
775 // get mesh and shape
776 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
777 TopoDS_Shape shape = GeomObjectToShape( theGeom );
778 if ( byMesh && ( !meshServant || meshServant->NbNodes()==0 || shape.IsNull() ))
779 return SMESH::SMESH_Hypothesis::_nil();
780 ::SMESH_Mesh* mesh = meshServant ? &meshServant->GetImpl() : (::SMESH_Mesh*)0;
782 // create a temporary hypothesis to know its dimension
783 SMESH::SMESH_Hypothesis_var tmpHyp = this->createHypothesis( theHypType, theLibName );
784 SMESH_Hypothesis_i* hypServant = SMESH::DownCast<SMESH_Hypothesis_i*>( tmpHyp );
786 return SMESH::SMESH_Hypothesis::_nil();
787 ::SMESH_Hypothesis* hyp = hypServant->GetImpl();
790 // look for a hypothesis of theHypType used to mesh the shape
791 if ( myGen.GetShapeDim( shape ) == hyp->GetDim() )
794 SMESH::ListOfHypothesis_var aHypList = theMesh->GetHypothesisList( theGeom );
795 int nbLocalHyps = aHypList->length();
796 for ( int i = 0; i < nbLocalHyps; i++ ) {
797 CORBA::String_var hypName = aHypList[i]->GetName();
798 if ( strcmp( theHypType, hypName.in() ) == 0 ) // FOUND local!
799 return SMESH::SMESH_Hypothesis::_duplicate( aHypList[i] );
801 // check super shapes
802 TopTools_ListIteratorOfListOfShape itShape( mesh->GetAncestors( shape ));
803 while ( nbLocalHyps == 0 && itShape.More() ) {
804 GEOM::GEOM_Object_ptr geomObj = ShapeToGeomObject( itShape.Value() );
805 if ( ! CORBA::is_nil( geomObj )) {
806 SMESH::ListOfHypothesis_var aHypList = theMesh->GetHypothesisList( geomObj );
807 nbLocalHyps = aHypList->length();
808 for ( int i = 0; i < nbLocalHyps; i++ )
809 if ( strcmp( theHypType, aHypList[i]->GetName() ) == 0 ) // FOUND global!
810 return SMESH::SMESH_Hypothesis::_duplicate( aHypList[i] );
816 // let the temporary hypothesis find out somehow parameter values by mesh
817 if ( hyp->SetParametersByMesh( mesh, shape ))
818 return SMESH::SMESH_Hypothesis::_duplicate( tmpHyp );
823 diagonal = mesh->GetShapeDiagonalSize();
825 diagonal = ::SMESH_Mesh::GetShapeDiagonalSize( shape );
826 ::SMESH_Hypothesis::TDefaults dflts;
827 dflts._elemLength = diagonal / myGen.GetBoundaryBoxSegmentation();
828 dflts._nbSegments = myGen.GetDefaultNbSegments();
829 dflts._shape = &shape;
830 // let the temporary hypothesis initialize it's values
831 if ( hyp->SetParametersByDefaults( dflts, mesh ))
832 return SMESH::SMESH_Hypothesis::_duplicate( tmpHyp );
835 return SMESH::SMESH_Hypothesis::_nil();
838 //=============================================================================
840 * Returns \c True if a hypothesis is assigned to a sole sub-mesh in a current Study
841 * \param [in] theHyp - the hypothesis of interest
842 * \param [out] theMesh - the sole mesh using \a theHyp
843 * \param [out] theShape - the sole geometry \a theHyp is assigned to
844 * \return boolean - \c True if \a theMesh and \a theShape are sole using \a theHyp
846 * If two meshes on same shape have theHyp assigned to the same sub-shape, they are
847 * considered as SAME sub-mesh => result is \c true.
848 * This method ids used to initialize SMESHGUI_GenericHypothesisCreator with
849 * a shape to which an hyp being edited is assigned.
851 //=============================================================================
853 CORBA::Boolean SMESH_Gen_i::GetSoleSubMeshUsingHyp( SMESH::SMESH_Hypothesis_ptr theHyp,
854 SMESH::SMESH_Mesh_out theMesh,
855 GEOM::GEOM_Object_out theShape)
857 if ( GetCurrentStudyID() < 0 || CORBA::is_nil( theHyp ))
860 // get Mesh component SO
861 CORBA::String_var compDataType = ComponentDataType();
862 SALOMEDS::SComponent_wrap comp = myCurrentStudy->FindComponent( compDataType.in() );
863 if ( CORBA::is_nil( comp ))
866 // look for child SO of meshes
867 SMESH::SMESH_Mesh_var foundMesh;
868 TopoDS_Shape foundShape;
870 SALOMEDS::ChildIterator_wrap meshIter = myCurrentStudy->NewChildIterator( comp );
871 for ( ; meshIter->More() && isSole; meshIter->Next() )
873 SALOMEDS::SObject_wrap curSO = meshIter->Value();
874 CORBA::Object_var obj = SObjectToObject( curSO );
875 SMESH_Mesh_i* mesh_i = SMESH::DownCast< SMESH_Mesh_i* >( obj );
879 // look for a sole shape where theHyp is assigned
880 bool isHypFound = false;
881 const ShapeToHypothesis & s2hyps = mesh_i->GetImpl().GetMeshDS()->GetHypotheses();
882 ShapeToHypothesis::Iterator s2hypsIt( s2hyps );
883 for ( ; s2hypsIt.More() && isSole; s2hypsIt.Next() )
885 const THypList& hyps = s2hypsIt.Value();
886 THypList::const_iterator h = hyps.begin();
887 for ( ; h != hyps.end(); ++h )
888 if ( (*h)->GetID() == theHyp->GetId() )
890 if ( h != hyps.end()) // theHyp found
893 if ( ! foundShape.IsNull() &&
894 ! foundShape.IsSame( s2hypsIt.Key() )) // not a sole sub-shape
896 foundShape.Nullify();
900 foundShape = s2hypsIt.Key();
902 } // loop on assigned hyps
904 if ( isHypFound && !foundShape.IsNull() ) // a mesh using theHyp is found
906 if ( !foundMesh->_is_nil() ) // not a sole mesh
908 if ( !foundMesh->HasShapeToMesh() ||
909 !mesh_i ->HasShapeToMesh() )
911 isSole = ( foundMesh->HasShapeToMesh() == mesh_i->HasShapeToMesh() );
915 GEOM::GEOM_Object_var s1 = mesh_i ->GetShapeToMesh();
916 GEOM::GEOM_Object_var s2 = foundMesh->GetShapeToMesh();
917 isSole = s1->IsSame( s2 );
920 foundMesh = SMESH::SMESH_Mesh::_narrow( obj );
926 ! foundMesh->_is_nil() &&
927 ! foundShape.IsNull() )
929 theMesh = foundMesh._retn();
930 theShape = ShapeToGeomObject( foundShape );
931 return ( !theMesh->_is_nil() && !theShape->_is_nil() );
936 //=============================================================================
938 * Sets number of segments per diagonal of boundary box of geometry by which
939 * default segment length of appropriate 1D hypotheses is defined
941 //=============================================================================
943 void SMESH_Gen_i::SetBoundaryBoxSegmentation( CORBA::Long theNbSegments )
944 throw ( SALOME::SALOME_Exception )
946 if ( theNbSegments > 0 )
947 myGen.SetBoundaryBoxSegmentation( int( theNbSegments ));
949 THROW_SALOME_CORBA_EXCEPTION( "non-positive number of segments", SALOME::BAD_PARAM );
951 //=============================================================================
953 * \brief Sets default number of segments per edge
955 //=============================================================================
956 void SMESH_Gen_i::SetDefaultNbSegments(CORBA::Long theNbSegments)
957 throw ( SALOME::SALOME_Exception )
959 if ( theNbSegments > 0 )
960 myGen.SetDefaultNbSegments( int(theNbSegments) );
962 THROW_SALOME_CORBA_EXCEPTION( "non-positive number of segments", SALOME::BAD_PARAM );
965 //=============================================================================
969 //=============================================================================
971 void SMESH_Gen_i::SetOption(const char* name, const char* value)
973 if ( name && value && strlen( value ) > 0 )
976 if ( strcmp(name, "historical_python_dump") == 0 )
978 myIsHistoricalPythonDump = ( value[0] == '1' || toupper(value[0]) == 'T' ); // 1 || true
979 msgToGUI = "preferences/SMESH/historical_python_dump/";
980 msgToGUI += myIsHistoricalPythonDump ? "true" : "false";
982 else if ( strcmp(name, "forget_mesh_on_hyp_modif") == 0 )
984 myToForgetMeshDataOnHypModif = ( value[0] == '1' || toupper(value[0]) == 'T' ); // 1 || true
985 msgToGUI = "preferences/SMESH/forget_mesh_on_hyp_modif/";
986 msgToGUI += myToForgetMeshDataOnHypModif ? "true" : "false";
988 else if ( strcmp(name, "default_grp_color") == 0 )
992 // color must be presented as a string of following form:
993 if ( str.at(0) == '#' && str.length() == 7 ) { // hexadecimal color ("#ffaa00", for example)
995 for ( size_t i = 0; i < str.length()/2; i++ )
996 if ( str.at(i*2) >= '0' && str.at(i*2) <= 'f' && str.at(i*2+1) >= '0' && str.at(i*2+1) <= 'f' )
997 color.push_back( strtol( str.substr( i*2, 2 ).c_str(), NULL, 16 ) );
999 else if ( value ) { // rgb color ("255,170,0", for example)
1000 string tempValue( value );
1001 char* colorValue = strtok( &tempValue[0], "," );
1002 while ( colorValue != NULL ) {
1003 int c_value = atoi( colorValue );
1004 if ( c_value >= 0 && c_value <= 255 )
1005 color.push_back( c_value );
1006 colorValue = strtok( NULL, "," );
1009 if ( color.size() == 3 ) { // color must have three valid component
1010 SMESHDS_GroupBase::SetDefaultColor( Quantity_Color( color[0]/255., color[1]/255., color[2]/255., Quantity_TOC_RGB ) );
1011 myDefaultGroupColor = value;
1012 msgToGUI = "preferences/SMESH/default_grp_color/";
1017 // update preferences in case if SetOption() is invoked from python console
1018 if ( !msgToGUI.empty() )
1020 CORBA::Object_var obj = SMESH_Gen_i::GetNS()->Resolve( "/Kernel/Session" );
1021 SALOME::Session_var session = SALOME::Session::_narrow( obj );
1022 if ( !CORBA::is_nil( session ) )
1023 session->emitMessageOneWay(msgToGUI.c_str());
1028 //=============================================================================
1030 Return an option value
1032 //=============================================================================
1034 char* SMESH_Gen_i::GetOption(const char* name)
1038 if ( strcmp(name, "historical_python_dump") == 0 )
1040 return CORBA::string_dup( myIsHistoricalPythonDump ? "true" : "false" );
1042 if ( strcmp(name, "forget_mesh_on_hyp_modif") == 0 )
1044 return CORBA::string_dup( myToForgetMeshDataOnHypModif ? "true" : "false" );
1046 if ( strcmp(name, "default_grp_color") == 0 )
1048 return CORBA::string_dup( myDefaultGroupColor.c_str() );
1051 return CORBA::string_dup( "" );
1054 //=============================================================================
1056 * SMESH_Gen_i::CreateMesh
1058 * Create empty mesh on a shape and publish it in the study
1060 //=============================================================================
1062 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMesh( GEOM::GEOM_Object_ptr theShapeObject )
1063 throw ( SALOME::SALOME_Exception )
1065 Unexpect aCatch(SALOME_SalomeException);
1066 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMesh" );
1068 SMESH::SMESH_Mesh_var mesh = this->createMesh();
1070 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( mesh );
1071 ASSERT( meshServant );
1072 meshServant->SetShape( theShapeObject );
1074 // publish mesh in the study
1075 if ( CanPublishInStudy( mesh ) ) {
1076 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1077 aStudyBuilder->NewCommand(); // There is a transaction
1078 SALOMEDS::SObject_wrap aSO = PublishMesh( myCurrentStudy, mesh.in() );
1079 aStudyBuilder->CommitCommand();
1080 if ( !aSO->_is_nil() ) {
1081 // Update Python script
1082 TPythonDump() << aSO << " = " << this << ".CreateMesh(" << theShapeObject << ")";
1086 return mesh._retn();
1089 //=============================================================================
1091 * SMESH_Gen_i::CreateEmptyMesh
1095 //=============================================================================
1097 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateEmptyMesh()
1098 throw ( SALOME::SALOME_Exception )
1100 Unexpect aCatch(SALOME_SalomeException);
1101 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMesh" );
1103 SMESH::SMESH_Mesh_var mesh = this->createMesh();
1105 // publish mesh in the study
1106 if ( CanPublishInStudy( mesh ) ) {
1107 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1108 aStudyBuilder->NewCommand(); // There is a transaction
1109 SALOMEDS::SObject_wrap aSO = PublishMesh( myCurrentStudy, mesh.in() );
1110 aStudyBuilder->CommitCommand();
1111 if ( !aSO->_is_nil() ) {
1112 // Update Python script
1113 TPythonDump() << aSO << " = " << this << ".CreateEmptyMesh()";
1117 return mesh._retn();
1122 //================================================================================
1124 * \brief Throws an exception in case if the file can't be read
1126 //================================================================================
1128 void checkFileReadable( const char* theFileName ) throw ( SALOME::SALOME_Exception )
1130 SMESH_File f ( theFileName );
1133 if ( !f.error().empty() )
1134 THROW_SALOME_CORBA_EXCEPTION( f.error().c_str(), SALOME::BAD_PARAM);
1136 THROW_SALOME_CORBA_EXCEPTION
1137 (( SMESH_Comment("Can't open for reading the file ") << theFileName ).c_str(),
1138 SALOME::BAD_PARAM );
1143 //=============================================================================
1145 * SMESH_Gen_i::CreateMeshFromUNV
1147 * Create mesh and import data from UNV file
1149 //=============================================================================
1151 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMeshesFromUNV( const char* theFileName )
1152 throw ( SALOME::SALOME_Exception )
1154 Unexpect aCatch(SALOME_SalomeException);
1156 checkFileReadable( theFileName );
1158 SMESH::SMESH_Mesh_var aMesh = createMesh();
1160 // publish mesh in the study
1161 if ( CanPublishInStudy( aMesh ) ) {
1162 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1163 aStudyBuilder->NewCommand(); // There is a transaction
1164 SALOMEDS::SObject_wrap aSO = PublishMesh( myCurrentStudy, aMesh.in(), aFileName.c_str() );
1165 aStudyBuilder->CommitCommand();
1166 if ( !aSO->_is_nil() ) {
1167 // Update Python script
1168 TPythonDump() << aSO << " = " << this << ".CreateMeshesFromUNV(r'" << theFileName << "')";
1172 SMESH_Mesh_i* aServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( aMesh ).in() );
1174 aServant->ImportUNVFile( theFileName );
1176 // Dump creation of groups
1177 SMESH::ListOfGroups_var groups = aServant->GetGroups();
1179 aServant->GetImpl().GetMeshDS()->Modified();
1180 return aMesh._retn();
1183 //=============================================================================
1185 * SMESH_Gen_i::CreateMeshFromMED
1187 * Create mesh and import data from MED file
1189 //=============================================================================
1191 SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromMEDorSAUV( const char* theFileName,
1192 SMESH::DriverMED_ReadStatus& theStatus,
1193 const char* theCommandNameForPython,
1194 const char* theFileNameForPython)
1197 char bname[ _MAX_FNAME ];
1198 _splitpath( theFileNameForPython, NULL, NULL, bname, NULL );
1199 string aFileName = bname;
1201 string aFileName = basename( const_cast<char *>(theFileNameForPython) );
1203 // Retrieve mesh names from the file
1204 DriverMED_R_SMESHDS_Mesh myReader;
1205 myReader.SetFile( theFileName );
1206 myReader.SetMeshId( -1 );
1207 Driver_Mesh::Status aStatus;
1208 list<string> aNames = myReader.GetMeshNames(aStatus);
1209 SMESH::mesh_array_var aResult = new SMESH::mesh_array();
1210 theStatus = (SMESH::DriverMED_ReadStatus)aStatus;
1212 { // open a new scope to make aPythonDump die before PythonDump in SMESH_Mesh::GetGroups()
1215 TPythonDump aPythonDump;
1216 aPythonDump << "([";
1218 if (theStatus == SMESH::DRS_OK) {
1219 SALOMEDS::StudyBuilder_var aStudyBuilder;
1220 if ( GetCurrentStudyID() > -1 )
1222 aStudyBuilder = myCurrentStudy->NewBuilder();
1223 aStudyBuilder->NewCommand(); // There is a transaction
1225 aResult->length( aNames.size() );
1228 // Iterate through all meshes and create mesh objects
1229 for ( list<string>::iterator it = aNames.begin(); it != aNames.end(); it++ )
1232 if (i > 0) aPythonDump << ", ";
1235 SMESH::SMESH_Mesh_var mesh = createMesh();
1237 // publish mesh in the study
1238 SALOMEDS::SObject_wrap aSO;
1239 if ( CanPublishInStudy( mesh ) )
1240 // little trick: for MED file theFileName and theFileNameForPython are the same, but they are different for SAUV
1241 // - as names of meshes are stored in MED file, we use them for data publishing
1242 // - as mesh name is not stored in UNV file, we use file name as name of mesh when publishing data
1243 aSO = PublishMesh( myCurrentStudy, mesh.in(), ( theFileName == theFileNameForPython ) ? (*it).c_str() : aFileName.c_str() );
1246 if ( !aSO->_is_nil() ) {
1249 aPythonDump << "mesh_" << i;
1252 // Read mesh data (groups are published automatically by ImportMEDFile())
1253 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
1254 ASSERT( meshServant );
1255 SMESH::DriverMED_ReadStatus status1 =
1256 meshServant->ImportMEDFile( theFileName, (*it).c_str() );
1257 if (status1 > theStatus)
1258 theStatus = status1;
1260 aResult[i++] = SMESH::SMESH_Mesh::_duplicate( mesh );
1261 meshServant->GetImpl().GetMeshDS()->Modified();
1263 if ( !aStudyBuilder->_is_nil() )
1264 aStudyBuilder->CommitCommand();
1267 // Update Python script
1268 aPythonDump << "], status) = " << this << "." << theCommandNameForPython << "(r'" << theFileNameForPython << "')";
1270 // Dump creation of groups
1271 for ( CORBA::ULong i = 0; i < aResult->length(); ++i )
1272 SMESH::ListOfGroups_var groups = aResult[ i ]->GetGroups();
1274 return aResult._retn();
1277 SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromMED( const char* theFileName,
1278 SMESH::DriverMED_ReadStatus& theStatus)
1279 throw ( SALOME::SALOME_Exception )
1281 Unexpect aCatch(SALOME_SalomeException);
1282 checkFileReadable( theFileName );
1284 SMESH::mesh_array* result = CreateMeshesFromMEDorSAUV(theFileName, theStatus, "CreateMeshesFromMED", theFileName);
1288 //=============================================================================
1290 * SMESH_Gen_i::CreateMeshFromSAUV
1292 * Create mesh and import data from SAUV file
1294 //=============================================================================
1296 SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromSAUV( const char* theFileName,
1297 SMESH::DriverMED_ReadStatus& theStatus)
1298 throw ( SALOME::SALOME_Exception )
1300 Unexpect aCatch(SALOME_SalomeException);
1301 checkFileReadable( theFileName );
1303 std::string sauvfilename(theFileName);
1304 std::string medfilename(theFileName);
1305 medfilename += ".med";
1308 cmd = "%PYTHONBIN% ";
1313 cmd += "from medutilities import convert ; convert(r'" + sauvfilename + "', 'GIBI', 'MED', 1, r'" + medfilename + "')";
1315 system(cmd.c_str());
1316 SMESH::mesh_array* result = CreateMeshesFromMEDorSAUV(medfilename.c_str(), theStatus, "CreateMeshesFromSAUV", sauvfilename.c_str());
1318 cmd = "%PYTHONBIN% ";
1323 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1325 system(cmd.c_str());
1329 //=============================================================================
1331 * SMESH_Gen_i::CreateMeshFromSTL
1333 * Create mesh and import data from STL file
1335 //=============================================================================
1337 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMeshesFromSTL( const char* theFileName )
1338 throw ( SALOME::SALOME_Exception )
1340 Unexpect aCatch(SALOME_SalomeException);
1341 checkFileReadable( theFileName );
1343 SMESH::SMESH_Mesh_var aMesh = createMesh();
1346 char bname[ _MAX_FNAME ];
1347 _splitpath( theFileName, NULL, NULL, bname, NULL );
1348 string aFileName = bname;
1350 string aFileName = basename( const_cast<char *>(theFileName) );
1352 // publish mesh in the study
1353 if ( CanPublishInStudy( aMesh ) ) {
1354 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1355 aStudyBuilder->NewCommand(); // There is a transaction
1356 SALOMEDS::SObject_wrap aSO = PublishInStudy
1357 ( myCurrentStudy, SALOMEDS::SObject::_nil(), aMesh.in(), aFileName.c_str() );
1358 aStudyBuilder->CommitCommand();
1359 if ( !aSO->_is_nil() ) {
1360 // Update Python script
1361 TPythonDump() << aSO << " = " << this << ".CreateMeshesFromSTL(r'" << theFileName << "')";
1365 SMESH_Mesh_i* aServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( aMesh ).in() );
1367 aServant->ImportSTLFile( theFileName );
1368 aServant->GetImpl().GetMeshDS()->Modified();
1369 return aMesh._retn();
1372 //================================================================================
1374 * \brief Create meshes and import data from the CGSN file
1376 //================================================================================
1378 SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromCGNS( const char* theFileName,
1379 SMESH::DriverMED_ReadStatus& theStatus)
1380 throw ( SALOME::SALOME_Exception )
1382 Unexpect aCatch(SALOME_SalomeException);
1383 checkFileReadable( theFileName );
1385 SMESH::mesh_array_var aResult = new SMESH::mesh_array();
1388 // Retrieve nb meshes from the file
1389 DriverCGNS_Read myReader;
1390 myReader.SetFile( theFileName );
1391 Driver_Mesh::Status aStatus;
1392 int nbMeshes = myReader.GetNbMeshes(aStatus);
1393 theStatus = (SMESH::DriverMED_ReadStatus)aStatus;
1395 aResult->length( nbMeshes );
1397 { // open a new scope to make aPythonDump die before PythonDump in SMESH_Mesh::GetGroups()
1400 TPythonDump aPythonDump;
1401 aPythonDump << "([";
1403 if (theStatus == SMESH::DRS_OK)
1405 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1406 aStudyBuilder->NewCommand(); // There is a transaction
1410 // Iterate through all meshes and create mesh objects
1411 for ( ; i < nbMeshes; ++i )
1414 if (i > 0) aPythonDump << ", ";
1417 SMESH::SMESH_Mesh_var mesh = createMesh();
1418 aResult[i] = SMESH::SMESH_Mesh::_duplicate( mesh );
1420 // Read mesh data (groups are published automatically by ImportMEDFile())
1421 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
1422 ASSERT( meshServant );
1424 SMESH::DriverMED_ReadStatus status1 =
1425 meshServant->ImportCGNSFile( theFileName, i, meshName );
1426 if (status1 > theStatus)
1427 theStatus = status1;
1429 meshServant->GetImpl().GetMeshDS()->Modified();
1430 // publish mesh in the study
1431 SALOMEDS::SObject_wrap aSO;
1432 if ( CanPublishInStudy( mesh ) )
1433 aSO = PublishMesh( myCurrentStudy, mesh.in(), meshName.c_str() );
1436 if ( !aSO->_is_nil() ) {
1440 aPythonDump << "mesh_" << i;
1443 aStudyBuilder->CommitCommand();
1446 aPythonDump << "], status) = " << this << ".CreateMeshesFromCGNS(r'" << theFileName << "')";
1448 // Dump creation of groups
1449 for ( CORBA::ULong i = 0; i < aResult->length(); ++i )
1450 SMESH::ListOfGroups_var groups = aResult[ i ]->GetGroups();
1452 THROW_SALOME_CORBA_EXCEPTION("CGNS library is unavailable", SALOME::INTERNAL_ERROR);
1455 return aResult._retn();
1458 //================================================================================
1460 * \brief Create a mesh and import data from a GMF file
1462 //================================================================================
1464 SMESH::SMESH_Mesh_ptr
1465 SMESH_Gen_i::CreateMeshesFromGMF( const char* theFileName,
1466 CORBA::Boolean theMakeRequiredGroups,
1467 SMESH::ComputeError_out theError)
1468 throw ( SALOME::SALOME_Exception )
1470 Unexpect aCatch(SALOME_SalomeException);
1471 checkFileReadable( theFileName );
1473 SMESH::SMESH_Mesh_var aMesh = createMesh();
1475 char bname[ _MAX_FNAME ];
1476 _splitpath( theFileName, NULL, NULL, bname, NULL );
1477 string aFileName = bname;
1479 string aFileName = basename( const_cast<char *>(theFileName) );
1481 // publish mesh in the study
1482 if ( CanPublishInStudy( aMesh ) ) {
1483 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1484 aStudyBuilder->NewCommand(); // There is a transaction
1485 SALOMEDS::SObject_wrap aSO = PublishInStudy
1486 ( myCurrentStudy, SALOMEDS::SObject::_nil(), aMesh.in(), aFileName.c_str() );
1487 aStudyBuilder->CommitCommand();
1488 if ( !aSO->_is_nil() ) {
1489 // Update Python script
1490 TPythonDump() << "("<< aSO << ", error) = " << this << ".CreateMeshesFromGMF(r'"
1491 << theFileName << "', "
1492 << theMakeRequiredGroups << " )";
1495 SMESH_Mesh_i* aServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( aMesh ).in() );
1497 theError = aServant->ImportGMFFile( theFileName, theMakeRequiredGroups );
1498 aServant->GetImpl().GetMeshDS()->Modified();
1499 return aMesh._retn();
1503 //=============================================================================
1505 * SMESH_Gen_i::IsReadyToCompute
1507 * Returns true if mesh contains enough data to be computed
1509 //=============================================================================
1511 CORBA::Boolean SMESH_Gen_i::IsReadyToCompute( SMESH::SMESH_Mesh_ptr theMesh,
1512 GEOM::GEOM_Object_ptr theShapeObject )
1513 throw ( SALOME::SALOME_Exception )
1515 Unexpect aCatch(SALOME_SalomeException);
1516 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::IsReadyToCompute" );
1518 if ( CORBA::is_nil( theShapeObject ) )
1519 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1520 SALOME::BAD_PARAM );
1522 if ( CORBA::is_nil( theMesh ) )
1523 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
1524 SALOME::BAD_PARAM );
1528 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
1529 ASSERT( meshServant );
1530 if ( meshServant ) {
1531 // get local TopoDS_Shape
1532 TopoDS_Shape myLocShape = GeomObjectToShape( theShapeObject );
1533 // call implementation
1534 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1535 return myGen.CheckAlgoState( myLocMesh, myLocShape );
1538 catch ( SALOME_Exception& S_ex ) {
1539 INFOS( "catch exception "<< S_ex.what() );
1544 //================================================================================
1546 * \brief Find SObject for an algo
1548 //================================================================================
1550 SALOMEDS::SObject_ptr SMESH_Gen_i::GetAlgoSO(const ::SMESH_Algo* algo)
1553 if ( !myCurrentStudy->_is_nil() ) {
1554 // find algo in the study
1555 CORBA::String_var compDataType = ComponentDataType();
1556 SALOMEDS::SComponent_wrap father = myCurrentStudy->FindComponent( compDataType.in() );
1557 if ( !father->_is_nil() ) {
1558 SALOMEDS::ChildIterator_wrap itBig = myCurrentStudy->NewChildIterator( father );
1559 for ( ; itBig->More(); itBig->Next() ) {
1560 SALOMEDS::SObject_wrap gotBranch = itBig->Value();
1561 if ( gotBranch->Tag() == GetAlgorithmsRootTag() ) {
1562 SALOMEDS::ChildIterator_wrap algoIt = myCurrentStudy->NewChildIterator( gotBranch );
1563 for ( ; algoIt->More(); algoIt->Next() ) {
1564 SALOMEDS::SObject_wrap algoSO = algoIt->Value();
1565 CORBA::Object_var algoIOR = SObjectToObject( algoSO );
1566 if ( !CORBA::is_nil( algoIOR )) {
1567 SMESH_Hypothesis_i* impl = SMESH::DownCast<SMESH_Hypothesis_i*>( algoIOR );
1568 if ( impl && impl->GetImpl() == algo )
1569 return algoSO._retn();
1571 } // loop on algo SO's
1574 } // SMESH component iterator
1578 return SALOMEDS::SObject::_nil();
1581 //================================================================================
1583 * \brief Return errors of mesh computation
1585 //================================================================================
1587 SMESH::compute_error_array* SMESH_Gen_i::GetComputeErrors( SMESH::SMESH_Mesh_ptr theMesh,
1588 GEOM::GEOM_Object_ptr theSubObject )
1589 throw ( SALOME::SALOME_Exception )
1591 Unexpect aCatch(SALOME_SalomeException);
1592 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetComputeErrors()" );
1594 if ( CORBA::is_nil( theSubObject ) && theMesh->HasShapeToMesh())
1595 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
1597 if ( CORBA::is_nil( theMesh ) )
1598 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1600 SMESH::compute_error_array_var error_array = new SMESH::compute_error_array;
1602 if ( SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh ))
1605 if(theMesh->HasShapeToMesh())
1606 shape = GeomObjectToShape( theSubObject );
1608 shape = SMESH_Mesh::PseudoShape();
1610 ::SMESH_Mesh& mesh = meshServant->GetImpl();
1612 error_array->length( mesh.GetMeshDS()->MaxShapeIndex() );
1615 SMESH_subMesh *sm = mesh.GetSubMesh(shape);
1616 const bool includeSelf = true, complexShapeFirst = true;
1617 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(includeSelf,
1619 while ( smIt->more() )
1622 // if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
1624 SMESH_ComputeErrorPtr error = sm->GetComputeError();
1625 if ( error && !error->IsOK() )
1627 if ( !( error->myAlgo ) &&
1628 !( error->myAlgo = sm->GetAlgo() ))
1630 SMESH::ComputeError & errStruct = error_array[ nbErr++ ];
1631 errStruct.code = -( error->myName < 0 ? error->myName + 1: error->myName ); // -1 -> 0
1632 errStruct.comment = error->myComment.c_str();
1633 errStruct.subShapeID = sm->GetId();
1634 SALOMEDS::SObject_wrap algoSO = GetAlgoSO( error->myAlgo );
1635 if ( !algoSO->_is_nil() ) {
1636 CORBA::String_var algoName = algoSO->GetName();
1637 errStruct.algoName = algoName;
1640 errStruct.algoName = error->myAlgo->GetName();
1642 errStruct.hasBadMesh = !error->myBadElements.empty();
1645 error_array->length( nbErr );
1648 catch ( SALOME_Exception& S_ex ) {
1649 INFOS( "catch exception "<< S_ex.what() );
1652 return error_array._retn();
1655 //================================================================================
1657 * \brief Return mesh elements preventing computation of a subshape
1659 //================================================================================
1661 SMESH::MeshPreviewStruct*
1662 SMESH_Gen_i::GetBadInputElements( SMESH::SMESH_Mesh_ptr theMesh,
1663 CORBA::Short theSubShapeID )
1664 throw ( SALOME::SALOME_Exception )
1666 Unexpect aCatch(SALOME_SalomeException);
1667 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetBadInputElements()" );
1669 if ( CORBA::is_nil( theMesh ) )
1670 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1672 SMESH::MeshPreviewStruct_var result = new SMESH::MeshPreviewStruct;
1675 if ( SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh ))
1677 // mesh implementation
1678 ::SMESH_Mesh& mesh = meshServant->GetImpl();
1679 // submesh by subshape id
1680 if ( SMESH_subMesh * sm = mesh.GetSubMeshContaining( theSubShapeID ))
1683 SMESH_ComputeErrorPtr error = sm->GetComputeError();
1684 if ( error && !error->myBadElements.empty())
1686 typedef map<const SMDS_MeshElement*, int > TNode2LocalIDMap;
1687 typedef TNode2LocalIDMap::iterator TNodeLocalID;
1689 // get nodes of elements and count elements
1690 TNode2LocalIDMap mapNode2LocalID;
1691 list< TNodeLocalID > connectivity;
1692 int i, nbElements = 0, nbConnNodes = 0;
1694 list<const SMDS_MeshElement*>::iterator elemIt = error->myBadElements.begin();
1695 list<const SMDS_MeshElement*>::iterator elemEnd = error->myBadElements.end();
1696 for ( ; elemIt != elemEnd; ++elemIt, ++nbElements )
1698 SMDS_ElemIteratorPtr nIt = (*elemIt)->nodesIterator();
1699 while ( nIt->more() )
1700 connectivity.push_back
1701 ( mapNode2LocalID.insert( make_pair( nIt->next(), ++nbConnNodes)).first );
1703 // fill node coords and assign local ids to the nodes
1704 int nbNodes = mapNode2LocalID.size();
1705 result->nodesXYZ.length( nbNodes );
1706 TNodeLocalID node2ID = mapNode2LocalID.begin();
1707 for ( i = 0; i < nbNodes; ++i, ++node2ID ) {
1708 node2ID->second = i;
1709 const SMDS_MeshNode* node = (const SMDS_MeshNode*) node2ID->first;
1710 result->nodesXYZ[i].x = node->X();
1711 result->nodesXYZ[i].y = node->Y();
1712 result->nodesXYZ[i].z = node->Z();
1714 // fill connectivity
1715 result->elementConnectivities.length( nbConnNodes );
1716 list< TNodeLocalID >::iterator connIt = connectivity.begin();
1717 for ( i = 0; i < nbConnNodes; ++i, ++connIt ) {
1718 result->elementConnectivities[i] = (*connIt)->second;
1720 // fill element types
1721 result->elementTypes.length( nbElements );
1722 for ( i = 0, elemIt = error->myBadElements.begin(); i <nbElements; ++i, ++elemIt )
1724 const SMDS_MeshElement* elem = *elemIt;
1725 result->elementTypes[i].SMDS_ElementType = (SMESH::ElementType) elem->GetType();
1726 result->elementTypes[i].isPoly = elem->IsPoly();
1727 result->elementTypes[i].nbNodesInElement = elem->NbNodes();
1733 catch ( SALOME_Exception& S_ex ) {
1734 INFOS( "catch exception "<< S_ex.what() );
1737 return result._retn();
1740 //================================================================================
1742 * \brief Create a group of elements preventing computation of a sub-shape
1744 //================================================================================
1746 SMESH::ListOfGroups*
1747 SMESH_Gen_i::MakeGroupsOfBadInputElements( SMESH::SMESH_Mesh_ptr theMesh,
1748 CORBA::Short theSubShapeID,
1749 const char* theGroupName )
1750 throw ( SALOME::SALOME_Exception )
1752 Unexpect aCatch(SALOME_SalomeException);
1754 SMESH::ListOfGroups_var groups;
1756 if ( CORBA::is_nil( theMesh ) )
1757 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1760 if ( SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh ))
1762 groups = meshServant->MakeGroupsOfBadInputElements( theSubShapeID, theGroupName );
1763 TPythonDump() << groups << " = " << this
1764 << ".MakeGroupsOfBadInputElements( "
1765 << theMesh << ", " << theSubShapeID << ", '" << theGroupName << "' )";
1768 catch ( SALOME_Exception& S_ex ) {
1769 INFOS( "catch exception "<< S_ex.what() );
1771 return groups._retn();
1774 //================================================================================
1776 * \brief Returns errors of hypotheses definition
1777 * \param theMesh - the mesh
1778 * \param theSubObject - the main or sub- shape
1779 * \retval SMESH::algo_error_array* - sequence of errors
1781 //================================================================================
1783 SMESH::algo_error_array* SMESH_Gen_i::GetAlgoState( SMESH::SMESH_Mesh_ptr theMesh,
1784 GEOM::GEOM_Object_ptr theSubObject )
1785 throw ( SALOME::SALOME_Exception )
1787 Unexpect aCatch(SALOME_SalomeException);
1788 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetAlgoState()" );
1790 if ( CORBA::is_nil( theSubObject ) && theMesh->HasShapeToMesh())
1791 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
1793 if ( CORBA::is_nil( theMesh ) )
1794 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1796 SMESH::algo_error_array_var error_array = new SMESH::algo_error_array;
1798 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
1799 ASSERT( meshServant );
1800 if ( meshServant ) {
1801 TopoDS_Shape myLocShape;
1802 if(theMesh->HasShapeToMesh())
1803 myLocShape = GeomObjectToShape( theSubObject );
1805 myLocShape = SMESH_Mesh::PseudoShape();
1807 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1808 list< ::SMESH_Gen::TAlgoStateError > error_list;
1809 list< ::SMESH_Gen::TAlgoStateError >::iterator error;
1810 // call ::SMESH_Gen::GetAlgoState()
1811 myGen.GetAlgoState( myLocMesh, myLocShape, error_list );
1812 error_array->length( error_list.size() );
1814 for ( error = error_list.begin(); error != error_list.end(); ++error )
1816 // fill AlgoStateError structure
1817 SMESH::AlgoStateError & errStruct = error_array[ i++ ];
1818 errStruct.state = SMESH_Mesh_i::ConvertHypothesisStatus( error->_name );
1819 errStruct.algoDim = error->_algoDim;
1820 errStruct.isGlobalAlgo = error->_isGlobalAlgo;
1821 errStruct.algoName = "";
1822 SALOMEDS::SObject_wrap algoSO = GetAlgoSO( error->_algo );
1823 if ( !algoSO->_is_nil() ) {
1824 CORBA::String_var algoName = algoSO->GetName();
1825 errStruct.algoName = algoName.in();
1830 catch ( SALOME_Exception& S_ex ) {
1831 INFOS( "catch exception "<< S_ex.what() );
1833 return error_array._retn();
1836 //=============================================================================
1838 * SMESH_Gen_i::GetSubShapesId
1840 * Get sub-shapes unique ID's list
1842 //=============================================================================
1845 SMESH_Gen_i::GetSubShapesId( GEOM::GEOM_Object_ptr theMainShapeObject,
1846 const SMESH::object_array& theListOfSubShapeObject )
1847 throw ( SALOME::SALOME_Exception )
1849 Unexpect aCatch(SALOME_SalomeException);
1850 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetSubShapesId" );
1852 SMESH::long_array_var shapesId = new SMESH::long_array;
1855 if ( CORBA::is_nil( theMainShapeObject ) )
1856 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
1860 TopoDS_Shape myMainShape = GeomObjectToShape(theMainShapeObject);
1861 TopTools_IndexedMapOfShape myIndexToShape;
1862 TopExp::MapShapes(myMainShape,myIndexToShape);
1864 for ( CORBA::ULong i = 0; i < theListOfSubShapeObject.length(); i++ )
1866 GEOM::GEOM_Object_var aShapeObject
1867 = GEOM::GEOM_Object::_narrow(theListOfSubShapeObject[i]);
1868 if ( CORBA::is_nil( aShapeObject ) )
1869 THROW_SALOME_CORBA_EXCEPTION ("bad shape object reference", \
1870 SALOME::BAD_PARAM );
1872 TopoDS_Shape locShape = GeomObjectToShape(aShapeObject);
1873 for (TopExp_Explorer exp(locShape,TopAbs_FACE); exp.More(); exp.Next())
1875 const TopoDS_Face& F = TopoDS::Face(exp.Current());
1876 setId.insert(myIndexToShape.FindIndex(F));
1877 if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(F));
1879 for (TopExp_Explorer exp(locShape,TopAbs_EDGE); exp.More(); exp.Next())
1881 const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
1882 setId.insert(myIndexToShape.FindIndex(E));
1883 if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(E));
1885 for (TopExp_Explorer exp(locShape,TopAbs_VERTEX); exp.More(); exp.Next())
1887 const TopoDS_Vertex& V = TopoDS::Vertex(exp.Current());
1888 setId.insert(myIndexToShape.FindIndex(V));
1889 if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(V));
1892 shapesId->length(setId.size());
1893 set<int>::iterator iind;
1895 for (iind = setId.begin(); iind != setId.end(); iind++)
1897 if(MYDEBUG) SCRUTE((*iind));
1898 shapesId[i] = (*iind);
1899 if(MYDEBUG) SCRUTE(shapesId[i]);
1903 catch (SALOME_Exception& S_ex)
1905 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
1908 return shapesId._retn();
1911 //=============================================================================
1913 * SMESH_Gen_i::Compute
1915 * Compute mesh on a shape
1917 //=============================================================================
1919 CORBA::Boolean SMESH_Gen_i::Compute( SMESH::SMESH_Mesh_ptr theMesh,
1920 GEOM::GEOM_Object_ptr theShapeObject )
1921 throw ( SALOME::SALOME_Exception )
1924 Unexpect aCatch(SALOME_SalomeException);
1925 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Compute" );
1927 if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
1928 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1929 SALOME::BAD_PARAM );
1931 if ( CORBA::is_nil( theMesh ) )
1932 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
1933 SALOME::BAD_PARAM );
1935 // Update Python script
1936 TPythonDump() << "isDone = " << this << ".Compute( "
1937 << theMesh << ", " << theShapeObject << ")";
1941 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
1942 ASSERT( meshServant );
1943 if ( meshServant ) {
1944 meshServant->Load();
1945 // NPAL16168: "geometrical group edition from a submesh don't modifiy mesh computation"
1946 meshServant->CheckGeomModif();
1947 // get local TopoDS_Shape
1948 TopoDS_Shape myLocShape;
1949 if(theMesh->HasShapeToMesh())
1950 myLocShape = GeomObjectToShape( theShapeObject );
1952 myLocShape = SMESH_Mesh::PseudoShape();
1953 // call implementation compute
1954 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1955 myGen.PrepareCompute( myLocMesh, myLocShape );
1956 int how = ::SMESH_Gen::COMPACT_MESH;
1957 if ( myLocShape != myLocMesh.GetShapeToMesh() ) // compute a sub-mesh
1958 how |= ::SMESH_Gen::SHAPE_ONLY;
1959 bool ok = myGen.Compute( myLocMesh, myLocShape, how );
1960 meshServant->CreateGroupServants(); // algos can create groups (issue 0020918)
1961 myLocMesh.GetMeshDS()->Modified();
1965 catch ( std::bad_alloc ) {
1966 INFOS( "Compute(): lack of memory" );
1968 catch ( SALOME_Exception& S_ex ) {
1969 INFOS( "Compute(): catch exception "<< S_ex.what() );
1972 INFOS( "Compute(): unknown exception " );
1977 //=============================================================================
1979 * SMESH_Gen_i::CancelCompute
1981 * Cancel Compute mesh on a shape
1983 //=============================================================================
1985 void SMESH_Gen_i::CancelCompute( SMESH::SMESH_Mesh_ptr theMesh,
1986 GEOM::GEOM_Object_ptr theShapeObject )
1988 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
1989 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1990 TopoDS_Shape myLocShape;
1991 if(theMesh->HasShapeToMesh())
1992 myLocShape = GeomObjectToShape( theShapeObject );
1994 myLocShape = SMESH_Mesh::PseudoShape();
1995 myGen.CancelCompute( myLocMesh, myLocShape);
1998 //=============================================================================
2000 * SMESH_Gen_i::Precompute
2002 * Compute mesh as preview till indicated dimension on shape
2004 //=============================================================================
2006 SMESH::MeshPreviewStruct* SMESH_Gen_i::Precompute( SMESH::SMESH_Mesh_ptr theMesh,
2007 GEOM::GEOM_Object_ptr theShapeObject,
2008 SMESH::Dimension theDimension,
2009 SMESH::long_array& theShapesId)
2010 throw ( SALOME::SALOME_Exception )
2012 Unexpect aCatch(SALOME_SalomeException);
2013 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Precompute" );
2015 if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
2016 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
2017 SALOME::BAD_PARAM );
2019 if ( CORBA::is_nil( theMesh ) )
2020 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
2021 SALOME::BAD_PARAM );
2023 SMESH::MeshPreviewStruct_var result = new SMESH::MeshPreviewStruct;
2026 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
2027 meshServant->Load();
2028 ASSERT( meshServant );
2029 if ( meshServant ) {
2030 // NPAL16168: "geometrical group edition from a submesh don't modifiy mesh computation"
2031 meshServant->CheckGeomModif();
2032 // get local TopoDS_Shape
2033 TopoDS_Shape myLocShape;
2034 if(theMesh->HasShapeToMesh())
2035 myLocShape = GeomObjectToShape( theShapeObject );
2037 return result._retn();;
2039 // call implementation compute
2040 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
2042 ::MeshDimension aDim = (MeshDimension)theDimension;
2043 if ( myGen.Compute( myLocMesh, myLocShape, ::SMESH_Gen::COMPACT_MESH, aDim, &shapeIds ) )
2045 int nbShapeId = shapeIds.size();
2046 theShapesId.length( nbShapeId );
2047 // iterates on shapes and collect mesh entities into mesh preview
2048 TSetOfInt::const_iterator idIt = shapeIds.begin();
2049 TSetOfInt::const_iterator idEnd = shapeIds.end();
2050 std::map< int, int > mapOfShIdNb;
2051 std::set< SMESH_TLink > setOfEdge;
2052 std::list< SMDSAbs_ElementType > listOfElemType;
2053 typedef map<const SMDS_MeshElement*, int > TNode2LocalIDMap;
2054 typedef TNode2LocalIDMap::iterator TNodeLocalID;
2055 TNode2LocalIDMap mapNode2LocalID;
2056 list< TNodeLocalID > connectivity;
2057 int i, nbConnNodes = 0;
2058 std::set< const SMESH_subMesh* > setOfVSubMesh;
2059 // iterates on shapes
2060 for ( ; idIt != idEnd; idIt++ )
2062 if ( mapOfShIdNb.find( *idIt ) != mapOfShIdNb.end() )
2064 SMESH_subMesh* sm = myLocMesh.GetSubMeshContaining(*idIt);
2065 if ( !sm || !sm->IsMeshComputed() )
2068 const TopoDS_Shape& aSh = sm->GetSubShape();
2069 const int shDim = myGen.GetShapeDim( aSh );
2070 if ( shDim < 1 || shDim > theDimension )
2073 mapOfShIdNb[ *idIt ] = 0;
2074 theShapesId[ mapOfShIdNb.size() - 1 ] = *idIt;
2076 SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
2077 if ( !smDS ) continue;
2079 if ( theDimension == SMESH::DIM_2D )
2081 SMDS_ElemIteratorPtr faceIt = smDS->GetElements();
2082 while ( faceIt->more() )
2084 const SMDS_MeshElement* face = faceIt->next();
2085 int aNbNode = face->NbNodes();
2087 aNbNode /= 2; // do not take into account additional middle nodes
2089 SMDS_MeshNode* node1 = (SMDS_MeshNode*)face->GetNode( 0 );
2090 for ( int nIndx = 0; nIndx < aNbNode; nIndx++ )
2092 SMDS_MeshNode* node2 = (SMDS_MeshNode*)face->GetNode( nIndx+1 < aNbNode ? nIndx+1 : 0 );
2093 if ( setOfEdge.insert( SMESH_TLink ( node1, node2 ) ).second )
2095 listOfElemType.push_back( SMDSAbs_Edge );
2096 connectivity.push_back
2097 ( mapNode2LocalID.insert( make_pair( node1, ++nbConnNodes)).first );
2098 connectivity.push_back
2099 ( mapNode2LocalID.insert( make_pair( node2, ++nbConnNodes)).first );
2105 else if ( theDimension == SMESH::DIM_1D )
2107 SMDS_NodeIteratorPtr nodeIt = smDS->GetNodes();
2108 while ( nodeIt->more() )
2110 listOfElemType.push_back( SMDSAbs_Node );
2111 connectivity.push_back
2112 ( mapNode2LocalID.insert( make_pair( nodeIt->next(), ++nbConnNodes)).first );
2114 // add corner nodes by first vertex from edge
2115 SMESH_subMeshIteratorPtr edgeSmIt =
2116 sm->getDependsOnIterator(/*includeSelf*/false,
2117 /*complexShapeFirst*/false);
2118 while ( edgeSmIt->more() )
2120 SMESH_subMesh* vertexSM = edgeSmIt->next();
2121 // check that vertex is not already treated
2122 if ( !setOfVSubMesh.insert( vertexSM ).second )
2124 if ( vertexSM->GetSubShape().ShapeType() != TopAbs_VERTEX )
2127 const SMESHDS_SubMesh* vertexSmDS = vertexSM->GetSubMeshDS();
2128 SMDS_NodeIteratorPtr nodeIt = vertexSmDS->GetNodes();
2129 while ( nodeIt->more() )
2131 listOfElemType.push_back( SMDSAbs_Node );
2132 connectivity.push_back
2133 ( mapNode2LocalID.insert( make_pair( nodeIt->next(), ++nbConnNodes)).first );
2139 // fill node coords and assign local ids to the nodes
2140 int nbNodes = mapNode2LocalID.size();
2141 result->nodesXYZ.length( nbNodes );
2142 TNodeLocalID node2ID = mapNode2LocalID.begin();
2143 for ( i = 0; i < nbNodes; ++i, ++node2ID ) {
2144 node2ID->second = i;
2145 const SMDS_MeshNode* node = (const SMDS_MeshNode*) node2ID->first;
2146 result->nodesXYZ[i].x = node->X();
2147 result->nodesXYZ[i].y = node->Y();
2148 result->nodesXYZ[i].z = node->Z();
2150 // fill connectivity
2151 result->elementConnectivities.length( nbConnNodes );
2152 list< TNodeLocalID >::iterator connIt = connectivity.begin();
2153 for ( i = 0; i < nbConnNodes; ++i, ++connIt ) {
2154 result->elementConnectivities[i] = (*connIt)->second;
2157 // fill element types
2158 result->elementTypes.length( listOfElemType.size() );
2159 std::list< SMDSAbs_ElementType >::const_iterator typeIt = listOfElemType.begin();
2160 std::list< SMDSAbs_ElementType >::const_iterator typeEnd = listOfElemType.end();
2161 for ( i = 0; typeIt != typeEnd; ++i, ++typeIt )
2163 SMDSAbs_ElementType elemType = *typeIt;
2164 result->elementTypes[i].SMDS_ElementType = (SMESH::ElementType)elemType;
2165 result->elementTypes[i].isPoly = false;
2166 result->elementTypes[i].nbNodesInElement = elemType == SMDSAbs_Edge ? 2 : 1;
2169 // correct number of shapes
2170 theShapesId.length( mapOfShIdNb.size() );
2174 catch ( std::bad_alloc ) {
2175 INFOS( "Precompute(): lack of memory" );
2177 catch ( SALOME_Exception& S_ex ) {
2178 INFOS( "Precompute(): catch exception "<< S_ex.what() );
2181 INFOS( "Precompute(): unknown exception " );
2183 return result._retn();
2187 //=============================================================================
2189 * SMESH_Gen_i::Evaluate
2191 * Evaluate mesh on a shape
2193 //=============================================================================
2195 SMESH::long_array* SMESH_Gen_i::Evaluate(SMESH::SMESH_Mesh_ptr theMesh,
2196 GEOM::GEOM_Object_ptr theShapeObject)
2197 // SMESH::long_array& theNbElems)
2198 throw ( SALOME::SALOME_Exception )
2200 Unexpect aCatch(SALOME_SalomeException);
2201 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Evaluate" );
2203 if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
2204 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
2205 SALOME::BAD_PARAM );
2207 if ( CORBA::is_nil( theMesh ) )
2208 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
2209 SALOME::BAD_PARAM );
2211 SMESH::long_array_var nbels = new SMESH::long_array;
2212 nbels->length(SMESH::Entity_Last);
2213 int i = SMESH::Entity_Node;
2214 for (; i < SMESH::Entity_Last; i++)
2217 // Update Python script
2218 TPythonDump() << "theNbElems = " << this << ".Evaluate( "
2219 << theMesh << ", " << theShapeObject << ")";
2223 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
2224 ASSERT( meshServant );
2225 if ( meshServant ) {
2226 meshServant->Load();
2227 // NPAL16168: "geometrical group edition from a submesh don't modifiy mesh computation"
2228 meshServant->CheckGeomModif();
2229 // get local TopoDS_Shape
2230 TopoDS_Shape myLocShape;
2231 if(theMesh->HasShapeToMesh())
2232 myLocShape = GeomObjectToShape( theShapeObject );
2234 myLocShape = SMESH_Mesh::PseudoShape();
2235 // call implementation compute
2236 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
2237 MapShapeNbElems aResMap;
2238 /*CORBA::Boolean ret =*/ myGen.Evaluate( myLocMesh, myLocShape, aResMap);
2239 MapShapeNbElemsItr anIt = aResMap.begin();
2240 for(; anIt!=aResMap.end(); anIt++) {
2241 const vector<int>& aVec = (*anIt).second;
2242 for ( i = SMESH::Entity_Node; i < (int)aVec.size(); i++ ) {
2243 int nbElem = aVec[i];
2244 if ( nbElem < 0 ) // algo failed, check that it has reported a message
2246 SMESH_subMesh* sm = anIt->first;
2247 SMESH_ComputeErrorPtr& error = sm->GetComputeError();
2248 const SMESH_Algo* algo = sm->GetAlgo();
2249 if ( (algo && !error.get()) || error->IsOK() )
2250 error.reset( new SMESH_ComputeError( COMPERR_ALGO_FAILED,"Failed to evaluate",algo));
2254 nbels[i] += aVec[i];
2258 return nbels._retn();
2261 catch ( std::bad_alloc ) {
2262 INFOS( "Evaluate(): lack of memory" );
2264 catch ( SALOME_Exception& S_ex ) {
2265 INFOS( "Evaluate(): catch exception "<< S_ex.what() );
2268 INFOS( "Evaluate(): unknown exception " );
2271 return nbels._retn();
2274 //================================================================================
2276 * \brief Return geometrical object the given element is built on
2277 * \param theMesh - the mesh the element is in
2278 * \param theElementID - the element ID
2279 * \param theGeomName - the name of the result geom object if it is not yet published
2280 * \retval GEOM::GEOM_Object_ptr - the found or just published geom object (no need to UnRegister())
2282 //================================================================================
2284 GEOM::GEOM_Object_ptr
2285 SMESH_Gen_i::GetGeometryByMeshElement( SMESH::SMESH_Mesh_ptr theMesh,
2286 CORBA::Long theElementID,
2287 const char* theGeomName)
2288 throw ( SALOME::SALOME_Exception )
2290 Unexpect aCatch(SALOME_SalomeException);
2292 GEOM::GEOM_Object_wrap geom = FindGeometryByMeshElement(theMesh, theElementID);
2293 if ( !geom->_is_nil() ) {
2294 GEOM::GEOM_Object_var mainShape = theMesh->GetShapeToMesh();
2295 GEOM::GEOM_Gen_ptr geomGen = GetGeomEngine();
2297 // try to find the corresponding SObject
2298 SALOMEDS::SObject_wrap SObj = ObjectToSObject( myCurrentStudy, geom.in() );
2299 if ( SObj->_is_nil() ) // submesh can be not found even if published
2301 // try to find published submesh
2302 GEOM::ListOfLong_var list = geom->GetSubShapeIndices();
2303 if ( !geom->IsMainShape() && list->length() == 1 ) {
2304 SALOMEDS::SObject_wrap mainSO = ObjectToSObject( myCurrentStudy, mainShape );
2305 SALOMEDS::ChildIterator_wrap it;
2306 if ( !mainSO->_is_nil() ) {
2307 it = myCurrentStudy->NewChildIterator( mainSO );
2309 if ( !it->_is_nil() ) {
2310 for ( it->InitEx(true); it->More(); it->Next() ) {
2311 SALOMEDS::SObject_wrap so = it->Value();
2312 CORBA::Object_var obj = SObjectToObject( so );
2313 GEOM::GEOM_Object_var subGeom = GEOM::GEOM_Object::_narrow( obj );
2314 if ( !subGeom->_is_nil() ) {
2315 GEOM::ListOfLong_var subList = subGeom->GetSubShapeIndices();
2316 if ( subList->length() == 1 && list[0] == subList[0] ) {
2326 if ( SObj->_is_nil() ) // publish a new subshape
2327 SObj = geomGen->AddInStudy( myCurrentStudy, geom, theGeomName, mainShape );
2329 // return only published geometry
2330 if ( !SObj->_is_nil() ) {
2331 //return geom._retn(); -- servant of geom must be UnRegister()ed;
2332 CORBA::Object_var obj = SObjectToObject( SObj );
2333 GEOM::GEOM_Object_var go = GEOM::GEOM_Object::_narrow( obj );
2337 return GEOM::GEOM_Object::_nil();
2340 //================================================================================
2342 * \brief Return geometrical object the given element is built on.
2343 * \param theMesh - the mesh the element is in
2344 * \param theElementID - the element ID
2345 * \retval GEOM::GEOM_Object_ptr - the found or created (UnRegister()!) geom object
2347 //================================================================================
2349 GEOM::GEOM_Object_ptr
2350 SMESH_Gen_i::FindGeometryByMeshElement( SMESH::SMESH_Mesh_ptr theMesh,
2351 CORBA::Long theElementID)
2352 throw ( SALOME::SALOME_Exception )
2354 Unexpect aCatch(SALOME_SalomeException);
2355 if ( CORBA::is_nil( theMesh ) )
2356 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference", SALOME::BAD_PARAM );
2358 GEOM::GEOM_Object_var mainShape = theMesh->GetShapeToMesh();
2359 GEOM::GEOM_Gen_ptr geomGen = GetGeomEngine();
2361 // get a core mesh DS
2362 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
2363 if ( meshServant && !geomGen->_is_nil() && !mainShape->_is_nil() )
2365 ::SMESH_Mesh & mesh = meshServant->GetImpl();
2366 SMESHDS_Mesh* meshDS = mesh.GetMeshDS();
2367 // find the element in mesh
2368 if ( const SMDS_MeshElement * elem = meshDS->FindElement( theElementID ) ) {
2369 // find a shape id by the element
2370 if ( int shapeID = ::SMESH_MeshEditor( &mesh ).FindShape( elem )) {
2371 // get a geom object by the shape id
2372 GEOM::GEOM_Object_var geom = ShapeToGeomObject( meshDS->IndexToShape( shapeID ));
2373 if ( geom->_is_nil() ) {
2374 // try to find a published sub-shape
2375 SALOMEDS::SObject_wrap mainSO = ObjectToSObject( myCurrentStudy, mainShape );
2376 SALOMEDS::ChildIterator_wrap it;
2377 if ( !mainSO->_is_nil() ) {
2378 it = myCurrentStudy->NewChildIterator( mainSO );
2380 if ( !it->_is_nil() ) {
2381 for ( it->InitEx(true); it->More(); it->Next() ) {
2382 SALOMEDS::SObject_wrap so = it->Value();
2383 CORBA::Object_var obj = SObjectToObject( so );
2384 GEOM::GEOM_Object_var subGeom = GEOM::GEOM_Object::_narrow( obj );
2385 if ( !subGeom->_is_nil() ) {
2386 GEOM::ListOfLong_var subList = subGeom->GetSubShapeIndices();
2387 if ( subList->length() == 1 && shapeID == subList[0] ) {
2395 if ( geom->_is_nil() ) {
2397 GEOM::GEOM_IShapesOperations_wrap op =
2398 geomGen->GetIShapesOperations( GetCurrentStudyID() );
2399 if ( !op->_is_nil() )
2400 geom = op->GetSubShape( mainShape, shapeID );
2405 if ( !geom->_is_nil() ) {
2406 GeomObjectToShape( geom ); // let geom client remember the found shape
2407 return geom._retn();
2412 return GEOM::GEOM_Object::_nil();
2415 //================================================================================
2417 * SMESH_Gen_i::Concatenate
2419 * Concatenate the given meshes into one mesh
2421 //================================================================================
2423 SMESH::SMESH_Mesh_ptr
2424 SMESH_Gen_i::Concatenate(const SMESH::ListOfIDSources& theMeshesArray,
2425 CORBA::Boolean theUniteIdenticalGroups,
2426 CORBA::Boolean theMergeNodesAndElements,
2427 CORBA::Double theMergeTolerance)
2428 throw ( SALOME::SALOME_Exception )
2430 return ConcatenateCommon(theMeshesArray,
2431 theUniteIdenticalGroups,
2432 theMergeNodesAndElements,
2437 //================================================================================
2439 * SMESH_Gen_i::ConcatenateWithGroups
2441 * Concatenate the given meshes into one mesh
2442 * Create the groups of all elements from initial meshes
2444 //================================================================================
2446 SMESH::SMESH_Mesh_ptr
2447 SMESH_Gen_i::ConcatenateWithGroups(const SMESH::ListOfIDSources& theMeshesArray,
2448 CORBA::Boolean theUniteIdenticalGroups,
2449 CORBA::Boolean theMergeNodesAndElements,
2450 CORBA::Double theMergeTolerance)
2451 throw ( SALOME::SALOME_Exception )
2453 return ConcatenateCommon(theMeshesArray,
2454 theUniteIdenticalGroups,
2455 theMergeNodesAndElements,
2460 //================================================================================
2462 * SMESH_Gen_i::ConcatenateCommon
2464 * Concatenate the given meshes into one mesh
2466 //================================================================================
2468 SMESH::SMESH_Mesh_ptr
2469 SMESH_Gen_i::ConcatenateCommon(const SMESH::ListOfIDSources& theMeshesArray,
2470 CORBA::Boolean theUniteIdenticalGroups,
2471 CORBA::Boolean theMergeNodesAndElements,
2472 CORBA::Double theMergeTolerance,
2473 CORBA::Boolean theCommonGroups)
2474 throw ( SALOME::SALOME_Exception )
2476 std::unique_ptr< TPythonDump > pPythonDump( new TPythonDump );
2477 TPythonDump& pythonDump = *pPythonDump; // prevent dump of called methods
2480 SMESH::SMESH_Mesh_var newMesh = CreateEmptyMesh();
2481 SMESH_Mesh_i* newImpl = SMESH::DownCast<SMESH_Mesh_i*>( newMesh );
2482 if ( !newImpl ) return newMesh._retn();
2484 ::SMESH_Mesh& locMesh = newImpl->GetImpl();
2485 SMESHDS_Mesh* newMeshDS = locMesh.GetMeshDS();
2487 typedef std::list<SMESH::SMESH_Group_var> TListOfNewGroups;
2488 typedef std::pair<string, SMESH::ElementType > TNameAndType;
2489 typedef std::map< TNameAndType, TListOfNewGroups > TGroupsMap;
2490 TGroupsMap groupsMap;
2491 TListOfNewGroups listOfNewGroups;
2493 ::SMESH_MeshEditor newEditor( &locMesh );
2494 ::SMESH_MeshEditor::ElemFeatures elemType;
2496 // loop on sub-meshes
2497 for ( CORBA::ULong i = 0; i < theMeshesArray.length(); i++ )
2499 if ( CORBA::is_nil( theMeshesArray[i] )) continue;
2500 SMESH::SMESH_Mesh_var initMesh = theMeshesArray[i]->GetMesh();
2501 SMESH_Mesh_i* initImpl = SMESH::DownCast<SMESH_Mesh_i*>( initMesh );
2502 if ( !initImpl ) continue;
2505 // assure that IDs increments by one during iteration
2506 ::SMESH_Mesh& initLocMesh = initImpl->GetImpl();
2507 SMESHDS_Mesh* initMeshDS = initLocMesh.GetMeshDS();
2508 if ( initMeshDS->MaxNodeID() > initMeshDS->NbNodes() ||
2509 initMeshDS->MaxElementID() > initMeshDS->NbElements() )
2511 initMeshDS->Modified();
2512 initMeshDS->CompactMesh();
2515 // remember nb of elements before filling in
2516 SMESH::long_array_var prevState = newMesh->GetNbElementsByType();
2520 std::vector< const SMDS_MeshElement* > newNodes( initMeshDS->NbNodes() + 1, 0 );
2521 SMDS_ElemIteratorPtr elemIt = initImpl->GetElements( theMeshesArray[i], SMESH::NODE );
2522 while ( elemIt->more() )
2524 SMESH_NodeXYZ node = elemIt->next();
2525 newNodes[ node->GetID() ] = newMeshDS->AddNode( node.X(), node.Y(), node.Z() );
2530 std::vector< const SMDS_MeshElement* > newElems( initMeshDS->NbElements() + 1, 0 );
2531 elemIt = initImpl->GetElements( theMeshesArray[i], SMESH::ALL );
2532 while ( elemIt->more() )
2534 const SMDS_MeshElement* elem = elemIt->next();
2535 elemType.myNodes.resize( elem->NbNodes() );
2537 SMDS_NodeIteratorPtr itNodes = elem->nodeIterator();
2538 for ( int k = 0; itNodes->more(); k++)
2540 const SMDS_MeshNode* node = itNodes->next();
2541 elemType.myNodes[ k ] = static_cast< const SMDS_MeshNode*> ( newNodes[ node->GetID() ]);
2544 // creates a corresponding element on existent nodes in new mesh
2545 newElems[ elem->GetID() ] =
2546 newEditor.AddElement( elemType.myNodes, elemType.Init( elem, /*basicOnly=*/false ));
2548 newEditor.ClearLastCreated(); // forget the history
2551 // create groups of just added elements
2552 SMESH::SMESH_Group_var newGroup;
2553 SMESH::ElementType groupType;
2554 if ( theCommonGroups )
2557 const char* typeNames[] = { "All","Nodes","Edges","Faces","Volumes","0DElems","Balls" };
2558 { // check of typeNames: compilation failure mains that NB_ELEMENT_TYPES changed:
2559 const int nbNames = sizeof(typeNames) / sizeof(const char*);
2560 int _assert[( nbNames == SMESH::NB_ELEMENT_TYPES ) ? 2 : -1 ]; _assert[0]=_assert[1]=0;
2563 SMESH::long_array_var curState = newMesh->GetNbElementsByType();
2565 for( groupType = SMESH::NODE;
2566 groupType < SMESH::NB_ELEMENT_TYPES;
2567 groupType = (SMESH::ElementType)( groupType + 1 ))
2569 if ( curState[ groupType ] <= prevState[ groupType ])
2570 continue; // no elements of groupType added from the i-th mesh
2572 // make a group name
2573 std::string groupName = "Gr";
2574 SALOMEDS::SObject_wrap meshSO = ObjectToSObject( myCurrentStudy, theMeshesArray[i] );
2576 CORBA::String_var name = meshSO->GetName();
2580 groupName += typeNames[ groupType ];
2582 // make and fill a group
2583 newGroup = newImpl->CreateGroup( groupType, groupName.c_str() );
2584 std::vector< const SMDS_MeshElement* > & elemVec =
2585 ( groupType == SMESH::NODE ) ? newNodes : newElems;
2586 if ( SMESH_Group_i* grp_i = SMESH::DownCast<SMESH_Group_i*>( newGroup ))
2588 if ( SMESHDS_Group* grpDS = dynamic_cast<SMESHDS_Group*>( grp_i->GetGroupDS() ))
2590 for ( size_t j = 0; j < elemVec.size(); ++j )
2592 if ( elemVec[j] && elemVec[j]->GetType() == grpDS->GetType() )
2593 grpDS->Add( elemVec[j] );
2597 listOfNewGroups.clear();
2598 listOfNewGroups.push_back( newGroup );
2599 groupsMap.insert( std::make_pair( TNameAndType( groupName, groupType ),
2604 if ( SMESH_Mesh_i* initImpl = SMESH::DownCast<SMESH_Mesh_i*>( theMeshesArray[i] ))
2608 SMESH::SMESH_GroupBase_ptr group;
2609 CORBA::String_var groupName;
2610 SMESH::long_array_var newIDs = new SMESH::long_array();
2612 // loop on groups of a source mesh
2613 SMESH::ListOfGroups_var listOfGroups = initImpl->GetGroups();
2614 for ( CORBA::ULong iG = 0; iG < listOfGroups->length(); iG++ )
2616 group = listOfGroups[iG];
2617 groupType = group->GetType();
2618 groupName = group->GetName();
2619 std::string name = groupName.in();
2621 // convert a list of IDs
2622 newIDs->length( group->Size() );
2623 std::vector< const SMDS_MeshElement* > & elemVec =
2624 ( groupType == SMESH::NODE ) ? newNodes : newElems;
2625 SMDS_ElemIteratorPtr itGrElems = initImpl->GetElements( group, SMESH::ALL );
2627 while ( itGrElems->more() )
2629 const SMDS_MeshElement* elem = itGrElems->next();
2630 const SMDS_MeshElement* newElem = elemVec[ elem->GetID() ];
2632 newIDs[ nbElems++ ] = newElem->GetID();
2634 newIDs->length( nbElems );
2636 // check that a current group name and type don't have identical ones in final mesh
2637 listOfNewGroups.clear();
2638 TNameAndType nameAndType( name, groupType );
2639 TGroupsMap::iterator anIter = groupsMap.find( nameAndType );
2640 if ( anIter == groupsMap.end() )
2642 // add a new group in the mesh
2643 newGroup = newImpl->CreateGroup( groupType, groupName.in() );
2644 newGroup->Add( newIDs );
2646 listOfNewGroups.push_back( newGroup );
2647 groupsMap.insert( std::make_pair( nameAndType, listOfNewGroups ));
2649 else if ( theUniteIdenticalGroups )
2651 // unite identical groups
2652 TListOfNewGroups& aNewGroups = anIter->second;
2653 aNewGroups.front()->Add( newIDs );
2657 // rename identical groups
2658 newGroup = newImpl->CreateGroup( groupType, groupName );
2659 newGroup->Add( newIDs );
2661 TListOfNewGroups& newGroups = anIter->second;
2662 std::string newGroupName;
2663 if ( newGroups.size() == 1 )
2665 newGroupName = name + "_1";
2666 newGroups.front()->SetName( newGroupName.c_str() );
2668 newGroupName = name + "_" + SMESH_Comment( newGroups.size() + 1 );
2669 newGroup->SetName( newGroupName.c_str() );
2670 newGroups.push_back( newGroup );
2673 } // if an IDSource is a mesh
2676 if ( theMergeNodesAndElements ) // merge nodes
2678 TIDSortedNodeSet meshNodes; // no input nodes == treat all
2679 SMESH_MeshEditor::TListOfListOfNodes groupsOfNodes;
2680 newEditor.FindCoincidentNodes( meshNodes, theMergeTolerance, groupsOfNodes,
2681 /*SeparateCornersAndMedium=*/ false );
2682 newEditor.MergeNodes( groupsOfNodes );
2684 newEditor.MergeEqualElements();
2687 // Update Python script
2688 pythonDump << newMesh << " = " << this
2689 << "." << ( theCommonGroups ? "ConcatenateWithGroups" : "Concatenate" ) << "("
2690 << theMeshesArray << ", "
2691 << theUniteIdenticalGroups << ", "
2692 << theMergeNodesAndElements << ", "
2693 << TVar( theMergeTolerance ) << ")";
2695 pPythonDump.reset(); // enable python dump from GetGroups()
2697 // 0020577: EDF 1164 SMESH: Bad dump of concatenate with create common groups
2698 if ( !newMesh->_is_nil() )
2700 SMESH::ListOfGroups_var groups = newMesh->GetGroups();
2703 // IPAL21468 Change icon of compound because it need not be computed.
2704 SALOMEDS::SObject_wrap meshSO = ObjectToSObject( myCurrentStudy, newMesh );
2705 SetPixMap( meshSO, "ICON_SMESH_TREE_MESH" );
2707 newMeshDS->Modified();
2709 return newMesh._retn();
2712 //================================================================================
2714 * \brief Create a mesh by copying a part of another mesh
2715 * \param meshPart - a part of mesh to copy
2716 * \param toCopyGroups - to create in the new mesh groups
2717 * the copied elements belongs to
2718 * \param toKeepIDs - to preserve IDs of the copied elements or not
2719 * \retval SMESH::SMESH_Mesh_ptr - the new mesh
2721 //================================================================================
2723 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CopyMesh(SMESH::SMESH_IDSource_ptr meshPart,
2724 const char* meshName,
2725 CORBA::Boolean toCopyGroups,
2726 CORBA::Boolean toKeepIDs)
2728 Unexpect aCatch(SALOME_SalomeException);
2730 TPythonDump* pyDump = new TPythonDump; // prevent dump from CreateMesh()
2732 // 1. Get source mesh
2734 if ( CORBA::is_nil( meshPart ))
2735 THROW_SALOME_CORBA_EXCEPTION( "bad IDSource", SALOME::BAD_PARAM );
2737 SMESH::SMESH_Mesh_var srcMesh = meshPart->GetMesh();
2738 SMESH_Mesh_i* srcMesh_i = SMESH::DownCast<SMESH_Mesh_i*>( srcMesh );
2740 THROW_SALOME_CORBA_EXCEPTION( "bad mesh of IDSource", SALOME::BAD_PARAM );
2742 SMESHDS_Mesh* srcMeshDS = srcMesh_i->GetImpl().GetMeshDS();
2744 // 2. Make a new mesh
2746 SMESH::SMESH_Mesh_var newMesh = CreateMesh(GEOM::GEOM_Object::_nil());
2747 SMESH_Mesh_i* newMesh_i = SMESH::DownCast<SMESH_Mesh_i*>( newMesh );
2749 THROW_SALOME_CORBA_EXCEPTION( "can't create a mesh", SALOME::INTERNAL_ERROR );
2750 SALOMEDS::SObject_wrap meshSO = ObjectToSObject(myCurrentStudy, newMesh );
2751 if ( !meshSO->_is_nil() )
2753 SetName( meshSO, meshName, "Mesh" );
2754 SetPixMap( meshSO, "ICON_SMESH_TREE_MESH_IMPORTED");
2756 SMESHDS_Mesh* newMeshDS = newMesh_i->GetImpl().GetMeshDS();
2757 ::SMESH_MeshEditor editor( &newMesh_i->GetImpl() );
2758 ::SMESH_MeshEditor::ElemFeatures elemType;
2760 // 3. Get elements to copy
2762 SMDS_ElemIteratorPtr srcElemIt; SMDS_NodeIteratorPtr srcNodeIt;
2763 TIDSortedElemSet srcElems;
2764 SMESH::array_of_ElementType_var srcElemTypes = meshPart->GetTypes();
2765 if ( SMESH::DownCast<SMESH_Mesh_i*>( meshPart ))
2768 srcElemIt = srcMeshDS->elementsIterator();
2769 srcNodeIt = srcMeshDS->nodesIterator();
2773 SMESH::long_array_var ids = meshPart->GetIDs();
2774 if ( srcElemTypes->length() == 1 && srcElemTypes[0] == SMESH::NODE ) // group of nodes
2776 for ( CORBA::ULong i=0; i < ids->length(); i++ )
2777 if ( const SMDS_MeshElement * elem = srcMeshDS->FindNode( ids[i] ))
2778 srcElems.insert( elem );
2782 for ( CORBA::ULong i = 0; i < ids->length(); i++ )
2783 if ( const SMDS_MeshElement * elem = srcMeshDS->FindElement( ids[i] ))
2784 srcElems.insert( elem );
2786 if ( srcElems.empty() )
2787 return newMesh._retn();
2789 typedef SMDS_SetIterator< SMDS_pElement, TIDSortedElemSet::const_iterator > ElIter;
2790 srcElemIt = SMDS_ElemIteratorPtr( new ElIter( srcElems.begin(), srcElems.end() ));
2795 typedef map<SMDS_pElement, SMDS_pElement, TIDCompare> TE2EMap;
2796 TE2EMap e2eMapByType[ SMDSAbs_NbElementTypes ];
2797 TE2EMap& n2nMap = e2eMapByType[ SMDSAbs_Node ];
2799 const SMDS_MeshNode *nSrc, *nTgt;
2800 vector< const SMDS_MeshNode* > nodes;
2801 while ( srcElemIt->more() )
2803 const SMDS_MeshElement * elem = srcElemIt->next();
2805 nodes.resize( elem->NbNodes());
2806 SMDS_ElemIteratorPtr nIt = elem->nodesIterator();
2808 for ( iN = 0; nIt->more(); ++iN )
2810 nSrc = static_cast<const SMDS_MeshNode*>( nIt->next() );
2811 nTgt = newMeshDS->FindNode( nSrc->GetID());
2813 nTgt = newMeshDS->AddNodeWithID( nSrc->X(), nSrc->Y(), nSrc->Z(), nSrc->GetID());
2818 for ( iN = 0; nIt->more(); ++iN )
2820 nSrc = static_cast<const SMDS_MeshNode*>( nIt->next() );
2821 TE2EMap::iterator n2n = n2nMap.insert( make_pair( nSrc, SMDS_pNode(0) )).first;
2823 n2n->second = newMeshDS->AddNode( nSrc->X(), nSrc->Y(), nSrc->Z() );
2824 nodes[ iN ] = (const SMDS_MeshNode*) n2n->second;
2828 if ( elem->GetType() != SMDSAbs_Node )
2830 elemType.Init( elem, /*basicOnly=*/false );
2831 if ( toKeepIDs ) elemType.SetID( elem->GetID() );
2833 const SMDS_MeshElement * newElem = editor.AddElement( nodes, elemType );
2834 if ( toCopyGroups && !toKeepIDs )
2835 e2eMapByType[ elem->GetType() ].insert( make_pair( elem, newElem ));
2837 } // while ( srcElemIt->more() )
2839 // 4(b). Copy free nodes
2841 if ( srcNodeIt && srcMeshDS->NbNodes() != newMeshDS->NbNodes() )
2843 while ( srcNodeIt->more() )
2845 nSrc = srcNodeIt->next();
2846 if ( nSrc->NbInverseElements() == 0 )
2849 nTgt = newMeshDS->AddNodeWithID( nSrc->X(), nSrc->Y(), nSrc->Z(), nSrc->GetID());
2851 n2nMap[ nSrc ] = newMeshDS->AddNode( nSrc->X(), nSrc->Y(), nSrc->Z() );
2858 int nbNewGroups = 0;
2861 SMESH_Mesh::GroupIteratorPtr gIt = srcMesh_i->GetImpl().GetGroups();
2862 while ( gIt->more() )
2864 SMESH_Group* group = gIt->next();
2865 const SMESHDS_GroupBase* groupDS = group->GetGroupDS();
2867 // Check group type. We copy nodal groups containing nodes of copied element
2868 SMDSAbs_ElementType groupType = groupDS->GetType();
2869 if ( groupType != SMDSAbs_Node &&
2870 newMeshDS->GetMeshInfo().NbElements( groupType ) == 0 )
2871 continue; // group type differs from types of meshPart
2873 // Find copied elements in the group
2874 vector< const SMDS_MeshElement* > groupElems;
2875 SMDS_ElemIteratorPtr eIt = groupDS->GetElements();
2878 const SMDS_MeshElement* foundElem;
2879 if ( groupType == SMDSAbs_Node )
2881 while ( eIt->more() )
2882 if (( foundElem = newMeshDS->FindNode( eIt->next()->GetID() )))
2883 groupElems.push_back( foundElem );
2887 while ( eIt->more() )
2888 if (( foundElem = newMeshDS->FindElement( eIt->next()->GetID() )))
2889 groupElems.push_back( foundElem );
2894 TE2EMap & e2eMap = e2eMapByType[ groupDS->GetType() ];
2895 if ( e2eMap.empty() ) continue;
2896 int minID = e2eMap.begin()->first->GetID();
2897 int maxID = e2eMap.rbegin()->first->GetID();
2898 TE2EMap::iterator e2e;
2899 while ( eIt->more() && groupElems.size() < e2eMap.size())
2901 const SMDS_MeshElement* e = eIt->next();
2902 if ( e->GetID() < minID || e->GetID() > maxID ) continue;
2903 if ((e2e = e2eMap.find( e )) != e2eMap.end())
2904 groupElems.push_back( e2e->second );
2908 if ( !groupElems.empty() )
2910 SMESH::SMESH_Group_var newGroupObj =
2911 newMesh->CreateGroup( SMESH::ElementType(groupType), group->GetName() );
2912 if ( SMESH_GroupBase_i* newGroup_i = SMESH::DownCast<SMESH_GroupBase_i*>( newGroupObj))
2914 SMESHDS_GroupBase * newGroupDS = newGroup_i->GetGroupDS();
2915 SMDS_MeshGroup& smdsGroup = ((SMESHDS_Group*)newGroupDS)->SMDSGroup();
2916 for ( unsigned i = 0; i < groupElems.size(); ++i )
2917 smdsGroup.Add( groupElems[i] );
2925 newMeshDS->Modified();
2927 *pyDump << newMesh << " = " << this
2928 << ".CopyMesh( " << meshPart << ", "
2929 << "'" << meshName << "', "
2930 << toCopyGroups << ", "
2931 << toKeepIDs << ")";
2933 delete pyDump; pyDump = 0; // allow dump in GetGroups()
2935 if ( nbNewGroups > 0 ) // dump created groups
2936 SMESH::ListOfGroups_var groups = newMesh->GetGroups();
2938 return newMesh._retn();
2941 //================================================================================
2943 * SMESH_Gen_i::GetMEDVersion
2945 * Get MED version of the file by its name
2947 //================================================================================
2948 CORBA::Boolean SMESH_Gen_i::GetMEDVersion(const char* theFileName,
2949 SMESH::MED_VERSION& theVersion)
2951 theVersion = SMESH::MED_V2_1;
2952 MED::EVersion aVersion = MED::GetVersionId( theFileName );
2953 switch( aVersion ) {
2954 case MED::eV2_1 : theVersion = SMESH::MED_V2_1; return true;
2955 case MED::eV2_2 : theVersion = SMESH::MED_V2_2; return true;
2956 case MED::eLATEST : theVersion = SMESH::MED_LATEST; return true;
2957 case MED::eMINOR_0 : theVersion = SMESH::MED_MINOR_0; return true;
2958 case MED::eMINOR_1 : theVersion = SMESH::MED_MINOR_1; return true;
2959 case MED::eMINOR_2 : theVersion = SMESH::MED_MINOR_2; return true;
2960 case MED::eMINOR_3 : theVersion = SMESH::MED_MINOR_3; return true;
2961 case MED::eMINOR_4 : theVersion = SMESH::MED_MINOR_4; return true;
2962 case MED::eMINOR_5 : theVersion = SMESH::MED_MINOR_5; return true;
2963 case MED::eMINOR_6 : theVersion = SMESH::MED_MINOR_6; return true;
2964 case MED::eMINOR_7 : theVersion = SMESH::MED_MINOR_7; return true;
2965 case MED::eMINOR_8 : theVersion = SMESH::MED_MINOR_8; return true;
2966 case MED::eMINOR_9 : theVersion = SMESH::MED_MINOR_9; return true;
2967 case MED::eVUnknown : return false;
2972 //================================================================================
2974 * SMESH_Gen_i::GetMeshNames
2976 * Get names of meshes defined in file with the specified name
2978 //================================================================================
2979 SMESH::string_array* SMESH_Gen_i::GetMeshNames(const char* theFileName)
2981 SMESH::string_array_var aResult = new SMESH::string_array();
2982 MED::PWrapper aMed = MED::CrWrapper( theFileName );
2984 MED::TInt aNbMeshes = aMed->GetNbMeshes( &anErr );
2986 aResult->length( aNbMeshes );
2987 for( MED::TInt i = 0; i < aNbMeshes; i++ ) {
2988 MED::PMeshInfo aMeshInfo = aMed->GetPMeshInfo( i+1 );
2989 aResult[i] = CORBA::string_dup( aMeshInfo->GetName().c_str() );
2992 return aResult._retn();
2995 //=============================================================================
2999 * Save SMESH module's data
3001 //=============================================================================
3002 SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
3006 // ASSERT( theComponent->GetStudy()->StudyId() == myCurrentStudy->StudyId() )
3007 // san -- in case <myCurrentStudy> differs from theComponent's study,
3008 // use that of the component
3009 if ( theComponent->GetStudy()->StudyId() != GetCurrentStudyID() )
3010 SetCurrentStudy( theComponent->GetStudy() );
3012 // Store study contents as a set of python commands
3013 SavePython(myCurrentStudy);
3015 StudyContext* myStudyContext = GetCurrentStudyContext();
3017 // Declare a byte stream
3018 SALOMEDS::TMPFile_var aStreamFile;
3020 // Obtain a temporary dir
3021 TCollection_AsciiString tmpDir =
3022 ( isMultiFile ) ? TCollection_AsciiString( ( char* )theURL ) : ( char* )SALOMEDS_Tool::GetTmpDir().c_str();
3024 // Create a sequence of files processed
3025 SALOMEDS::ListOfFileNames_var aFileSeq = new SALOMEDS::ListOfFileNames;
3026 aFileSeq->length( NUM_TMP_FILES );
3028 TCollection_AsciiString aStudyName( "" );
3030 aStudyName = ( (char*)SALOMEDS_Tool::GetNameFromPath( myCurrentStudy->URL() ).c_str() );
3032 // Set names of temporary files
3033 TCollection_AsciiString filename =
3034 aStudyName + TCollection_AsciiString( "_SMESH.hdf" ); // for SMESH data itself
3035 TCollection_AsciiString meshfile =
3036 aStudyName + TCollection_AsciiString( "_SMESH_Mesh.med" ); // for mesh data to be stored in MED file
3037 aFileSeq[ 0 ] = CORBA::string_dup( filename.ToCString() );
3038 aFileSeq[ 1 ] = CORBA::string_dup( meshfile.ToCString() );
3039 filename = tmpDir + filename;
3040 meshfile = tmpDir + meshfile;
3043 HDFdataset* aDataset;
3044 HDFgroup* aTopGroup;
3046 HDFgroup* aSubGroup;
3047 HDFgroup* aSubSubGroup;
3048 hdf_size aSize[ 1 ];
3051 //Remove the files if they exist: BugID: 11225
3052 #ifndef WIN32 /* unix functionality */
3053 TCollection_AsciiString cmd("rm -f \"");
3055 TCollection_AsciiString cmd("del /F \"");
3062 system(cmd.ToCString());
3064 // MED writer to be used by storage process
3065 DriverMED_W_SMESHDS_Mesh myWriter;
3066 myWriter.SetFile( meshfile.ToCString() );
3069 // SetStoreName() to groups before storing hypotheses to let them refer to
3070 // groups using "store name", which is "Group <group_persistent_id>"
3072 SALOMEDS::ChildIterator_wrap itBig = myCurrentStudy->NewChildIterator( theComponent );
3073 for ( ; itBig->More(); itBig->Next() ) {
3074 SALOMEDS::SObject_wrap gotBranch = itBig->Value();
3075 if ( gotBranch->Tag() > GetAlgorithmsRootTag() ) {
3076 CORBA::Object_var anObject = SObjectToObject( gotBranch );
3077 if ( !CORBA::is_nil( anObject ) ) {
3078 SMESH::SMESH_Mesh_var myMesh = SMESH::SMESH_Mesh::_narrow( anObject ) ;
3079 if ( !myMesh->_is_nil() ) {
3080 myMesh->Load(); // load from study file if not yet done
3081 TPythonDump pd; // not to dump GetGroups()
3082 SMESH::ListOfGroups_var groups = myMesh->GetGroups();
3083 pd << ""; // to avoid optimizing pd out
3084 for ( CORBA::ULong i = 0; i < groups->length(); ++i )
3086 SMESH_GroupBase_i* grImpl = SMESH::DownCast<SMESH_GroupBase_i*>( groups[i]);
3089 CORBA::String_var objStr = GetORB()->object_to_string( grImpl->_this() );
3090 int anId = myStudyContext->findId( string( objStr.in() ) );
3092 sprintf( grpName, "Group %d", anId );
3093 SMESHDS_GroupBase* aGrpBaseDS = grImpl->GetGroupDS();
3094 aGrpBaseDS->SetStoreName( grpName );
3104 // ---> create HDF file
3105 aFile = new HDFfile( (char*) filename.ToCString() );
3106 aFile->CreateOnDisk();
3108 // --> iterator for top-level objects
3109 SALOMEDS::ChildIterator_wrap itBig = myCurrentStudy->NewChildIterator( theComponent );
3110 for ( ; itBig->More(); itBig->Next() ) {
3111 SALOMEDS::SObject_wrap gotBranch = itBig->Value();
3113 // --> hypotheses root branch (only one for the study)
3114 if ( gotBranch->Tag() == GetHypothesisRootTag() ) {
3115 // create hypotheses root HDF group
3116 aTopGroup = new HDFgroup( "Hypotheses", aFile );
3117 aTopGroup->CreateOnDisk();
3119 // iterator for all hypotheses
3120 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( gotBranch );
3121 for ( ; it->More(); it->Next() ) {
3122 SALOMEDS::SObject_wrap mySObject = it->Value();
3123 CORBA::Object_var anObject = SObjectToObject( mySObject );
3124 if ( !CORBA::is_nil( anObject ) ) {
3125 SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
3126 if ( !myHyp->_is_nil() ) {
3127 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
3129 string hypname = string( myHyp->GetName() );
3130 string libname = string( myHyp->GetLibName() );
3132 // Needs for save crossplatform libname, i.e. parth of name ( ".dll" for
3133 // WIN32 and ".so" for X-system) must be deleted
3134 int libname_len = libname.length();
3136 if( libname_len > 4 )
3137 libname.resize( libname_len - 4 );
3139 // PAL17753 (Regression: missing hypothesis in restored study)
3140 // "lib" also should be removed from the beginning
3141 //if( libname_len > 3 )
3142 //libname.resize( libname_len - 3 );
3143 if( libname_len > 6 )
3144 libname = libname.substr( 3, libname_len - 3 - 3 );
3146 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3147 int id = myStudyContext->findId( string( objStr.in() ) );
3148 string hypdata = string( myImpl->SaveTo() );
3150 // for each hypothesis create HDF group basing on its id
3151 char hypGrpName[30];
3152 sprintf( hypGrpName, "Hypothesis %d", id );
3153 aGroup = new HDFgroup( hypGrpName, aTopGroup );
3154 aGroup->CreateOnDisk();
3155 // --> type name of hypothesis
3156 aSize[ 0 ] = hypname.length() + 1;
3157 aDataset = new HDFdataset( "Name", aGroup, HDF_STRING, aSize, 1 );
3158 aDataset->CreateOnDisk();
3159 aDataset->WriteOnDisk( ( char* )( hypname.c_str() ) );
3160 aDataset->CloseOnDisk();
3161 // --> server plugin library name of hypothesis
3162 aSize[ 0 ] = libname.length() + 1;
3163 aDataset = new HDFdataset( "LibName", aGroup, HDF_STRING, aSize, 1 );
3164 aDataset->CreateOnDisk();
3165 aDataset->WriteOnDisk( ( char* )( libname.c_str() ) );
3166 aDataset->CloseOnDisk();
3167 // --> persistent data of hypothesis
3168 aSize[ 0 ] = hypdata.length() + 1;
3169 aDataset = new HDFdataset( "Data", aGroup, HDF_STRING, aSize, 1 );
3170 aDataset->CreateOnDisk();
3171 aDataset->WriteOnDisk( ( char* )( hypdata.c_str() ) );
3172 aDataset->CloseOnDisk();
3173 // close hypothesis HDF group
3174 aGroup->CloseOnDisk();
3179 // close hypotheses root HDF group
3180 aTopGroup->CloseOnDisk();
3182 // --> algorithms root branch (only one for the study)
3183 else if ( gotBranch->Tag() == GetAlgorithmsRootTag() ) {
3184 // create algorithms root HDF group
3185 aTopGroup = new HDFgroup( "Algorithms", aFile );
3186 aTopGroup->CreateOnDisk();
3188 // iterator for all algorithms
3189 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( gotBranch );
3190 for ( ; it->More(); it->Next() ) {
3191 SALOMEDS::SObject_wrap mySObject = it->Value();
3192 CORBA::Object_var anObject = SObjectToObject( mySObject );
3193 if ( !CORBA::is_nil( anObject ) ) {
3194 SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
3195 if ( !myHyp->_is_nil() ) {
3196 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
3198 string hypname = string( myHyp->GetName() );
3199 string libname = string( myHyp->GetLibName() );
3201 // Needs for save crossplatform libname, i.e. parth of name ( ".dll" for
3202 // WIN32 and ".so" for X-system) must be deleted
3203 int libname_len = libname.length();
3205 if( libname_len > 4 )
3206 libname.resize( libname_len - 4 );
3208 // PAL17753 (Regression: missing hypothesis in restored study)
3209 // "lib" also should be removed from the beginning
3210 //if( libname_len > 3 )
3211 //libname.resize( libname_len - 3 );
3212 if( libname_len > 6 )
3213 libname = libname.substr( 3, libname_len - 3 - 3 );
3215 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3216 int id = myStudyContext->findId( string( objStr.in() ) );
3217 string hypdata = string( myImpl->SaveTo() );
3219 // for each algorithm create HDF group basing on its id
3220 char hypGrpName[30];
3221 sprintf( hypGrpName, "Algorithm %d", id );
3222 aGroup = new HDFgroup( hypGrpName, aTopGroup );
3223 aGroup->CreateOnDisk();
3224 // --> type name of algorithm
3225 aSize[0] = hypname.length() + 1;
3226 aDataset = new HDFdataset( "Name", aGroup, HDF_STRING, aSize, 1 );
3227 aDataset->CreateOnDisk();
3228 aDataset->WriteOnDisk( ( char* )( hypname.c_str() ) );
3229 aDataset->CloseOnDisk();
3230 // --> server plugin library name of hypothesis
3231 aSize[0] = libname.length() + 1;
3232 aDataset = new HDFdataset( "LibName", aGroup, HDF_STRING, aSize, 1 );
3233 aDataset->CreateOnDisk();
3234 aDataset->WriteOnDisk( ( char* )( libname.c_str() ) );
3235 aDataset->CloseOnDisk();
3236 // --> persistent data of algorithm
3237 aSize[0] = hypdata.length() + 1;
3238 aDataset = new HDFdataset( "Data", aGroup, HDF_STRING, aSize, 1 );
3239 aDataset->CreateOnDisk();
3240 aDataset->WriteOnDisk( ( char* )( hypdata.c_str() ) );
3241 aDataset->CloseOnDisk();
3242 // close algorithm HDF group
3243 aGroup->CloseOnDisk();
3248 // close algorithms root HDF group
3249 aTopGroup->CloseOnDisk();
3251 // --> mesh objects roots branches
3252 else if ( gotBranch->Tag() > GetAlgorithmsRootTag() ) {
3253 CORBA::Object_var anObject = SObjectToObject( gotBranch );
3254 if ( !CORBA::is_nil( anObject ) ) {
3255 SMESH::SMESH_Mesh_var myMesh = SMESH::SMESH_Mesh::_narrow( anObject ) ;
3256 if ( !myMesh->_is_nil() ) {
3257 SMESH_Mesh_i* myImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( myMesh ).in() );
3259 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3260 int id = myStudyContext->findId( string( objStr.in() ) );
3261 ::SMESH_Mesh& myLocMesh = myImpl->GetImpl();
3262 SMESHDS_Mesh* mySMESHDSMesh = myLocMesh.GetMeshDS();
3263 bool hasShape = myLocMesh.HasShapeToMesh();
3265 // for each mesh open the HDF group basing on its id
3266 char meshGrpName[ 30 ];
3267 sprintf( meshGrpName, "Mesh %d", id );
3268 aTopGroup = new HDFgroup( meshGrpName, aFile );
3269 aTopGroup->CreateOnDisk();
3271 // --> put dataset to hdf file which is a flag that mesh has data
3272 string strHasData = "0";
3273 // check if the mesh is not empty
3274 if ( mySMESHDSMesh->NbNodes() > 0 ) {
3275 // write mesh data to med file
3276 myWriter.SetMesh( mySMESHDSMesh );
3277 myWriter.SetMeshId( id );
3280 aSize[ 0 ] = strHasData.length() + 1;
3281 aDataset = new HDFdataset( "Has data", aTopGroup, HDF_STRING, aSize, 1 );
3282 aDataset->CreateOnDisk();
3283 aDataset->WriteOnDisk( ( char* )( strHasData.c_str() ) );
3284 aDataset->CloseOnDisk();
3287 // for each mesh open the HDF group basing on its auto color parameter
3288 char meshAutoColorName[ 30 ];
3289 sprintf( meshAutoColorName, "AutoColorMesh %d", id );
3291 anAutoColor[0] = myImpl->GetAutoColor();
3293 aDataset = new HDFdataset( meshAutoColorName, aTopGroup, HDF_INT32, aSize, 1 );
3294 aDataset->CreateOnDisk();
3295 aDataset->WriteOnDisk( anAutoColor );
3296 aDataset->CloseOnDisk();
3298 // issue 0020693. Store _isModified flag
3299 int isModified = myLocMesh.GetIsModified();
3301 aDataset = new HDFdataset( "_isModified", aTopGroup, HDF_INT32, aSize, 1 );
3302 aDataset->CreateOnDisk();
3303 aDataset->WriteOnDisk( &isModified );
3304 aDataset->CloseOnDisk();
3306 // issue 20918. Store Persistent Id of SMESHDS_Mesh
3307 int meshPersistentId = mySMESHDSMesh->GetPersistentId();
3309 aDataset = new HDFdataset( "meshPersistentId", aTopGroup, HDF_INT32, aSize, 1 );
3310 aDataset->CreateOnDisk();
3311 aDataset->WriteOnDisk( &meshPersistentId );
3312 aDataset->CloseOnDisk();
3314 // write reference on a shape if exists
3315 SALOMEDS::SObject_wrap myRef;
3316 bool shapeRefFound = false;
3317 bool found = gotBranch->FindSubObject( GetRefOnShapeTag(), myRef.inout() );
3319 SALOMEDS::SObject_wrap myShape;
3320 bool ok = myRef->ReferencedObject( myShape.inout() );
3322 shapeRefFound = (! CORBA::is_nil( myShape->GetObject() ));
3323 string myRefOnObject = myShape->GetID();
3324 if ( shapeRefFound && myRefOnObject.length() > 0 ) {
3325 aSize[ 0 ] = myRefOnObject.length() + 1;
3326 aDataset = new HDFdataset( "Ref on shape", aTopGroup, HDF_STRING, aSize, 1 );
3327 aDataset->CreateOnDisk();
3328 aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3329 aDataset->CloseOnDisk();
3334 // write applied hypotheses if exist
3335 SALOMEDS::SObject_wrap myHypBranch;
3336 found = gotBranch->FindSubObject( GetRefOnAppliedHypothesisTag(), myHypBranch.inout() );
3337 if ( found && !shapeRefFound && hasShape) { // remove applied hyps
3338 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( myHypBranch );
3340 if ( found && (shapeRefFound || !hasShape) ) {
3341 aGroup = new HDFgroup( "Applied Hypotheses", aTopGroup );
3342 aGroup->CreateOnDisk();
3344 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( myHypBranch );
3346 for ( ; it->More(); it->Next() ) {
3347 SALOMEDS::SObject_wrap mySObject = it->Value();
3348 SALOMEDS::SObject_wrap myRefOnHyp;
3349 bool ok = mySObject->ReferencedObject( myRefOnHyp.inout() );
3351 // san - it is impossible to recover applied hypotheses
3352 // using their entries within Load() method,
3353 // for there are no AttributeIORs in the study when Load() is working.
3354 // Hence, it is better to store persistent IDs of hypotheses as references to them
3356 //string myRefOnObject = myRefOnHyp->GetID();
3357 CORBA::Object_var anObject = SObjectToObject( myRefOnHyp );
3358 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3359 int id = myStudyContext->findId( string( objStr.in() ) );
3360 //if ( myRefOnObject.length() > 0 ) {
3361 //aSize[ 0 ] = myRefOnObject.length() + 1;
3362 char hypName[ 30 ], hypId[ 30 ];
3363 sprintf( hypName, "Hyp %d", ++hypNb );
3364 sprintf( hypId, "%d", id );
3365 aSize[ 0 ] = strlen( hypId ) + 1;
3366 aDataset = new HDFdataset( hypName, aGroup, HDF_STRING, aSize, 1 );
3367 aDataset->CreateOnDisk();
3368 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3369 aDataset->WriteOnDisk( hypId );
3370 aDataset->CloseOnDisk();
3374 aGroup->CloseOnDisk();
3377 // write applied algorithms if exist
3378 SALOMEDS::SObject_wrap myAlgoBranch;
3379 found = gotBranch->FindSubObject( GetRefOnAppliedAlgorithmsTag(),
3380 myAlgoBranch.inout() );
3381 if ( found && !shapeRefFound && hasShape) { // remove applied algos
3382 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( myAlgoBranch );
3384 if ( found && (shapeRefFound || !hasShape)) {
3385 aGroup = new HDFgroup( "Applied Algorithms", aTopGroup );
3386 aGroup->CreateOnDisk();
3388 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( myAlgoBranch );
3390 for ( ; it->More(); it->Next() ) {
3391 SALOMEDS::SObject_wrap mySObject = it->Value();
3392 SALOMEDS::SObject_wrap myRefOnAlgo;
3393 bool ok = mySObject->ReferencedObject( myRefOnAlgo.inout() );
3395 // san - it is impossible to recover applied algorithms
3396 // using their entries within Load() method,
3397 // for there are no AttributeIORs in the study when Load() is working.
3398 // Hence, it is better to store persistent IDs of algorithms as references to them
3400 //string myRefOnObject = myRefOnAlgo->GetID();
3401 CORBA::Object_var anObject = SObjectToObject( myRefOnAlgo );
3402 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3403 int id = myStudyContext->findId( string( objStr.in() ) );
3404 //if ( myRefOnObject.length() > 0 ) {
3405 //aSize[ 0 ] = myRefOnObject.length() + 1;
3406 char algoName[ 30 ], algoId[ 30 ];
3407 sprintf( algoName, "Algo %d", ++algoNb );
3408 sprintf( algoId, "%d", id );
3409 aSize[ 0 ] = strlen( algoId ) + 1;
3410 aDataset = new HDFdataset( algoName, aGroup, HDF_STRING, aSize, 1 );
3411 aDataset->CreateOnDisk();
3412 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3413 aDataset->WriteOnDisk( algoId );
3414 aDataset->CloseOnDisk();
3418 aGroup->CloseOnDisk();
3421 // --> submesh objects sub-branches
3423 for ( int i = GetSubMeshOnVertexTag(); i <= GetSubMeshOnCompoundTag(); i++ ) {
3424 SALOMEDS::SObject_wrap mySubmeshBranch;
3425 found = gotBranch->FindSubObject( i, mySubmeshBranch.inout() );
3427 if ( found ) // check if there is shape reference in submeshes
3429 bool hasShapeRef = false;
3430 SALOMEDS::ChildIterator_wrap itSM =
3431 myCurrentStudy->NewChildIterator( mySubmeshBranch );
3432 for ( ; itSM->More(); itSM->Next() ) {
3433 SALOMEDS::SObject_wrap mySubRef, myShape, mySObject = itSM->Value();
3434 if ( mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef.inout() ))
3435 mySubRef->ReferencedObject( myShape.inout() );
3436 if ( !CORBA::is_nil( myShape ) && !CORBA::is_nil( myShape->GetObject() ))
3439 { // remove one submesh
3440 if ( shapeRefFound )
3441 { // unassign hypothesis
3442 SMESH::SMESH_subMesh_var mySubMesh =
3443 SMESH::SMESH_subMesh::_narrow( SObjectToObject( mySObject ));
3444 if ( !mySubMesh->_is_nil() ) {
3445 int shapeID = mySubMesh->GetId();
3446 TopoDS_Shape S = mySMESHDSMesh->IndexToShape( shapeID );
3447 const list<const SMESHDS_Hypothesis*>& hypList =
3448 mySMESHDSMesh->GetHypothesis( S );
3449 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
3450 while ( hyp != hypList.end() ) {
3451 int hypID = (*hyp++)->GetID(); // goto next hyp here because
3452 myLocMesh.RemoveHypothesis( S, hypID ); // hypList changes here
3456 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( mySObject );
3458 } // loop on submeshes of a type
3459 if ( !shapeRefFound || !hasShapeRef ) { // remove the whole submeshes branch
3460 myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( mySubmeshBranch );
3463 } // end check if there is shape reference in submeshes
3465 char name_meshgroup[ 30 ];
3466 if ( i == GetSubMeshOnVertexTag() )
3467 strcpy( name_meshgroup, "SubMeshes On Vertex" );
3468 else if ( i == GetSubMeshOnEdgeTag() )
3469 strcpy( name_meshgroup, "SubMeshes On Edge" );
3470 else if ( i == GetSubMeshOnWireTag() )
3471 strcpy( name_meshgroup, "SubMeshes On Wire" );
3472 else if ( i == GetSubMeshOnFaceTag() )
3473 strcpy( name_meshgroup, "SubMeshes On Face" );
3474 else if ( i == GetSubMeshOnShellTag() )
3475 strcpy( name_meshgroup, "SubMeshes On Shell" );
3476 else if ( i == GetSubMeshOnSolidTag() )
3477 strcpy( name_meshgroup, "SubMeshes On Solid" );
3478 else if ( i == GetSubMeshOnCompoundTag() )
3479 strcpy( name_meshgroup, "SubMeshes On Compound" );
3481 // for each type of submeshes create container HDF group
3482 aGroup = new HDFgroup( name_meshgroup, aTopGroup );
3483 aGroup->CreateOnDisk();
3485 // iterator for all submeshes of given type
3486 SALOMEDS::ChildIterator_wrap itSM = myCurrentStudy->NewChildIterator( mySubmeshBranch );
3487 for ( ; itSM->More(); itSM->Next() ) {
3488 SALOMEDS::SObject_wrap mySObject = itSM->Value();
3489 CORBA::Object_var anSubObject = SObjectToObject( mySObject );
3490 if ( !CORBA::is_nil( anSubObject ))
3492 SMESH::SMESH_subMesh_var mySubMesh = SMESH::SMESH_subMesh::_narrow( anSubObject ) ;
3493 CORBA::String_var objStr = GetORB()->object_to_string( anSubObject );
3494 int subid = myStudyContext->findId( string( objStr.in() ) );
3496 // for each mesh open the HDF group basing on its id
3497 char submeshGrpName[ 30 ];
3498 sprintf( submeshGrpName, "SubMesh %d", subid );
3499 aSubGroup = new HDFgroup( submeshGrpName, aGroup );
3500 aSubGroup->CreateOnDisk();
3502 // write reference on a shape, already checked if it exists
3503 SALOMEDS::SObject_wrap mySubRef, myShape;
3504 if ( mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef.inout() ))
3505 mySubRef->ReferencedObject( myShape.inout() );
3506 string myRefOnObject = myShape->GetID();
3507 if ( myRefOnObject.length() > 0 ) {
3508 aSize[ 0 ] = myRefOnObject.length() + 1;
3509 aDataset = new HDFdataset( "Ref on shape", aSubGroup, HDF_STRING, aSize, 1 );
3510 aDataset->CreateOnDisk();
3511 aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3512 aDataset->CloseOnDisk();
3515 // write applied hypotheses if exist
3516 SALOMEDS::SObject_wrap mySubHypBranch;
3517 found = mySObject->FindSubObject( GetRefOnAppliedHypothesisTag(),
3518 mySubHypBranch.inout() );
3520 aSubSubGroup = new HDFgroup( "Applied Hypotheses", aSubGroup );
3521 aSubSubGroup->CreateOnDisk();
3523 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( mySubHypBranch );
3525 for ( ; it->More(); it->Next() ) {
3526 SALOMEDS::SObject_wrap mySubSObject = it->Value();
3527 SALOMEDS::SObject_wrap myRefOnHyp;
3528 bool ok = mySubSObject->ReferencedObject( myRefOnHyp.inout() );
3530 //string myRefOnObject = myRefOnHyp->GetID();
3531 CORBA::Object_var anObject = SObjectToObject( myRefOnHyp );
3532 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3533 int id = myStudyContext->findId( string( objStr.in() ) );
3534 //if ( myRefOnObject.length() > 0 ) {
3535 //aSize[ 0 ] = myRefOnObject.length() + 1;
3536 char hypName[ 30 ], hypId[ 30 ];
3537 sprintf( hypName, "Hyp %d", ++hypNb );
3538 sprintf( hypId, "%d", id );
3539 aSize[ 0 ] = strlen( hypId ) + 1;
3540 aDataset = new HDFdataset( hypName, aSubSubGroup, HDF_STRING, aSize, 1 );
3541 aDataset->CreateOnDisk();
3542 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3543 aDataset->WriteOnDisk( hypId );
3544 aDataset->CloseOnDisk();
3548 aSubSubGroup->CloseOnDisk();
3551 // write applied algorithms if exist
3552 SALOMEDS::SObject_wrap mySubAlgoBranch;
3553 found = mySObject->FindSubObject( GetRefOnAppliedAlgorithmsTag(),
3554 mySubAlgoBranch.inout() );
3556 aSubSubGroup = new HDFgroup( "Applied Algorithms", aSubGroup );
3557 aSubSubGroup->CreateOnDisk();
3559 SALOMEDS::ChildIterator_wrap it =
3560 myCurrentStudy->NewChildIterator( mySubAlgoBranch );
3562 for ( ; it->More(); it->Next() ) {
3563 SALOMEDS::SObject_wrap mySubSObject = it->Value();
3564 SALOMEDS::SObject_wrap myRefOnAlgo;
3565 bool ok = mySubSObject->ReferencedObject( myRefOnAlgo.inout() );
3567 //string myRefOnObject = myRefOnAlgo->GetID();
3568 CORBA::Object_var anObject = SObjectToObject( myRefOnAlgo );
3569 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
3570 int id = myStudyContext->findId( string( objStr.in() ) );
3571 //if ( myRefOnObject.length() > 0 ) {
3572 //aSize[ 0 ] = myRefOnObject.length() + 1;
3573 char algoName[ 30 ], algoId[ 30 ];
3574 sprintf( algoName, "Algo %d", ++algoNb );
3575 sprintf( algoId, "%d", id );
3576 aSize[ 0 ] = strlen( algoId ) + 1;
3577 aDataset = new HDFdataset( algoName, aSubSubGroup, HDF_STRING, aSize, 1 );
3578 aDataset->CreateOnDisk();
3579 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3580 aDataset->WriteOnDisk( algoId );
3581 aDataset->CloseOnDisk();
3585 aSubSubGroup->CloseOnDisk();
3587 // close submesh HDF group
3588 aSubGroup->CloseOnDisk();
3591 // close container of submeshes by type HDF group
3592 aGroup->CloseOnDisk();
3595 // All sub-meshes will be stored in MED file
3596 // .. will NOT (PAL 12992)
3597 //if ( shapeRefFound )
3598 //myWriter.AddAllSubMeshes();
3600 // store submesh order if any
3601 const TListOfListOfInt& theOrderIds = myLocMesh.GetMeshOrder();
3602 if ( theOrderIds.size() ) {
3603 char order_list[ 30 ];
3604 strcpy( order_list, "Mesh Order" );
3605 // count number of submesh ids
3607 TListOfListOfInt::const_iterator idIt = theOrderIds.begin();
3608 for ( ; idIt != theOrderIds.end(); idIt++ )
3609 nbIDs += (*idIt).size();
3610 // number of values = number of IDs +
3611 // number of lists (for separators) - 1
3612 int* smIDs = new int [ nbIDs + theOrderIds.size() - 1 ];
3613 idIt = theOrderIds.begin();
3614 for ( int i = 0; idIt != theOrderIds.end(); idIt++ ) {
3615 const TListOfInt& idList = *idIt;
3616 if (idIt != theOrderIds.begin()) // not first list
3617 smIDs[ i++ ] = -1/* *idList.size()*/; // separator between lists
3618 // dump submesh ids from current list
3619 TListOfInt::const_iterator id_smId = idList.begin();
3620 for( ; id_smId != idList.end(); id_smId++ )
3621 smIDs[ i++ ] = *id_smId;
3624 aSize[ 0 ] = nbIDs + theOrderIds.size() - 1;
3626 aDataset = new HDFdataset( order_list, aTopGroup, HDF_INT32, aSize, 1 );
3627 aDataset->CreateOnDisk();
3628 aDataset->WriteOnDisk( smIDs );
3629 aDataset->CloseOnDisk();
3634 // groups root sub-branch
3635 SALOMEDS::SObject_wrap myGroupsBranch;
3636 for ( int i = GetNodeGroupsTag(); i <= GetBallElementsGroupsTag(); i++ ) {
3637 found = gotBranch->FindSubObject( i, myGroupsBranch.inout() );
3639 char name_group[ 30 ];
3640 if ( i == GetNodeGroupsTag() )
3641 strcpy( name_group, "Groups of Nodes" );
3642 else if ( i == GetEdgeGroupsTag() )
3643 strcpy( name_group, "Groups of Edges" );
3644 else if ( i == GetFaceGroupsTag() )
3645 strcpy( name_group, "Groups of Faces" );
3646 else if ( i == GetVolumeGroupsTag() )
3647 strcpy( name_group, "Groups of Volumes" );
3648 else if ( i == Get0DElementsGroupsTag() )
3649 strcpy( name_group, "Groups of 0D Elements" );
3650 else if ( i == GetBallElementsGroupsTag() )
3651 strcpy( name_group, "Groups of Balls" );
3653 aGroup = new HDFgroup( name_group, aTopGroup );
3654 aGroup->CreateOnDisk();
3656 SALOMEDS::ChildIterator_wrap it = myCurrentStudy->NewChildIterator( myGroupsBranch );
3657 for ( ; it->More(); it->Next() ) {
3658 SALOMEDS::SObject_wrap mySObject = it->Value();
3659 CORBA::Object_var aSubObject = SObjectToObject( mySObject );
3660 if ( !CORBA::is_nil( aSubObject ) ) {
3661 SMESH_GroupBase_i* myGroupImpl =
3662 dynamic_cast<SMESH_GroupBase_i*>( GetServant( aSubObject ).in() );
3665 SMESHDS_GroupBase* aGrpBaseDS = myGroupImpl->GetGroupDS();
3669 CORBA::String_var objStr = GetORB()->object_to_string( aSubObject );
3670 int anId = myStudyContext->findId( string( objStr.in() ) );
3672 // For each group, create a dataset named "Group <group_persistent_id>"
3673 // and store the group's user name into it
3674 const char* grpName = aGrpBaseDS->GetStoreName();
3675 char* aUserName = myGroupImpl->GetName();
3676 aSize[ 0 ] = strlen( aUserName ) + 1;
3678 aDataset = new HDFdataset( grpName, aGroup, HDF_STRING, aSize, 1 );
3679 aDataset->CreateOnDisk();
3680 aDataset->WriteOnDisk( aUserName );
3681 aDataset->CloseOnDisk();
3684 // For each group, create a dataset named "Group <group_persistent_id> Color"
3685 // and store the group's color into it
3686 char grpColorName[ 30 ];
3687 sprintf( grpColorName, "ColorGroup %d", anId );
3688 SALOMEDS::Color aColor = myGroupImpl->GetColor();
3690 anRGB[ 0 ] = aColor.R;
3691 anRGB[ 1 ] = aColor.G;
3692 anRGB[ 2 ] = aColor.B;
3694 aDataset = new HDFdataset( grpColorName, aGroup, HDF_FLOAT64, aSize, 1 );
3695 aDataset->CreateOnDisk();
3696 aDataset->WriteOnDisk( anRGB );
3697 aDataset->CloseOnDisk();
3699 // Pass SMESHDS_Group to MED writer
3700 SMESHDS_Group* aGrpDS = dynamic_cast<SMESHDS_Group*>( aGrpBaseDS );
3702 myWriter.AddGroup( aGrpDS );
3704 // write reference on a shape if exists
3705 SMESHDS_GroupOnGeom* aGeomGrp =
3706 dynamic_cast<SMESHDS_GroupOnGeom*>( aGrpBaseDS );
3708 SALOMEDS::SObject_wrap mySubRef, myShape;
3709 if (mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef.inout() ) &&
3710 mySubRef->ReferencedObject( myShape.inout() ) &&
3711 !CORBA::is_nil( myShape->GetObject() ))
3713 string myRefOnObject = myShape->GetID();
3714 if ( myRefOnObject.length() > 0 ) {
3715 char aRefName[ 30 ];
3716 sprintf( aRefName, "Ref on shape %d", anId);
3717 aSize[ 0 ] = myRefOnObject.length() + 1;
3718 aDataset = new HDFdataset(aRefName, aGroup, HDF_STRING, aSize, 1);
3719 aDataset->CreateOnDisk();
3720 aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
3721 aDataset->CloseOnDisk();
3724 else // shape ref is invalid:
3726 // save a group on geometry as ordinary group
3727 myWriter.AddGroup( aGeomGrp );
3730 else if ( SMESH_GroupOnFilter_i* aFilterGrp_i =
3731 dynamic_cast<SMESH_GroupOnFilter_i*>( myGroupImpl ))
3733 std::string str = aFilterGrp_i->FilterToString();
3734 std::string hdfGrpName = "Filter " + SMESH_Comment(anId);
3735 aSize[ 0 ] = str.length() + 1;
3736 aDataset = new HDFdataset( hdfGrpName.c_str(), aGroup, HDF_STRING, aSize, 1);
3737 aDataset->CreateOnDisk();
3738 aDataset->WriteOnDisk( ( char* )( str.c_str() ) );
3739 aDataset->CloseOnDisk();
3743 aGroup->CloseOnDisk();
3747 if ( strcmp( strHasData.c_str(), "1" ) == 0 )
3749 // Flush current mesh information into MED file
3752 // save info on nb of elements
3753 SMESH_PreMeshInfo::SaveToFile( myImpl, id, aFile );
3755 // maybe a shape was deleted in the study
3756 if ( !shapeRefFound && !mySMESHDSMesh->ShapeToMesh().IsNull() && hasShape) {
3757 TopoDS_Shape nullShape;
3758 myLocMesh.ShapeToMesh( nullShape ); // remove shape referring data
3761 SMESHDS_SubMeshIteratorPtr smIt = mySMESHDSMesh->SubMeshes();
3766 aGroup = new HDFgroup( "Submeshes", aTopGroup );
3767 aGroup->CreateOnDisk();
3769 // each element belongs to one or none submesh,
3770 // so for each node/element, we store a submesh ID
3772 // Store submesh IDs
3773 for ( int isNode = 0; isNode < 2; ++isNode )
3775 SMDS_ElemIteratorPtr eIt =
3776 mySMESHDSMesh->elementsIterator( isNode ? SMDSAbs_Node : SMDSAbs_All );
3777 int nbElems = isNode ? mySMESHDSMesh->NbNodes() : mySMESHDSMesh->GetMeshInfo().NbElements();
3780 std::vector<int> smIDs; smIDs.reserve( nbElems );
3781 while ( eIt->more() )
3782 if ( const SMDS_MeshElement* e = eIt->next())
3783 smIDs.push_back( e->getshapeId() );
3785 aSize[ 0 ] = nbElems;
3786 string aDSName( isNode ? "Node Submeshes" : "Element Submeshes");
3787 aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_INT32, aSize, 1 );
3788 aDataset->CreateOnDisk();
3789 aDataset->WriteOnDisk( & smIDs[0] );
3790 aDataset->CloseOnDisk();
3793 aGroup->CloseOnDisk();
3795 // Store node positions on sub-shapes (SMDS_Position):
3796 // ----------------------------------------------------
3798 aGroup = new HDFgroup( "Node Positions", aTopGroup );
3799 aGroup->CreateOnDisk();
3801 // in aGroup, create 5 datasets to contain:
3802 // "Nodes on Edges" - ID of node on edge
3803 // "Edge positions" - U parameter on node on edge
3804 // "Nodes on Faces" - ID of node on face
3805 // "Face U positions" - U parameter of node on face
3806 // "Face V positions" - V parameter of node on face
3808 // Find out nb of nodes on edges and faces
3809 // Collect corresponding sub-meshes
3810 int nbEdgeNodes = 0, nbFaceNodes = 0;
3811 list<SMESHDS_SubMesh*> aEdgeSM, aFaceSM;
3812 // loop on SMESHDS_SubMesh'es
3813 while ( smIt->more() )
3815 SMESHDS_SubMesh* aSubMesh = const_cast< SMESHDS_SubMesh* >( smIt->next() );
3816 if ( aSubMesh->IsComplexSubmesh() )
3817 continue; // submesh containing other submeshs
3818 int nbNodes = aSubMesh->NbNodes();
3819 if ( nbNodes == 0 ) continue;
3821 int aShapeID = aSubMesh->GetID();
3822 if ( aShapeID < 1 || aShapeID > mySMESHDSMesh->MaxShapeIndex() )
3824 int aShapeType = mySMESHDSMesh->IndexToShape( aShapeID ).ShapeType();
3825 // write only SMDS_FacePosition and SMDS_EdgePosition
3826 switch ( aShapeType ) {
3828 nbFaceNodes += nbNodes;
3829 aFaceSM.push_back( aSubMesh );
3832 nbEdgeNodes += nbNodes;
3833 aEdgeSM.push_back( aSubMesh );
3839 // Treat positions on edges or faces
3840 for ( int onFace = 0; onFace < 2; onFace++ )
3842 // Create arrays to store in datasets
3843 int iNode = 0, nbNodes = ( onFace ? nbFaceNodes : nbEdgeNodes );
3844 if (!nbNodes) continue;
3845 int* aNodeIDs = new int [ nbNodes ];
3846 double* aUPos = new double [ nbNodes ];
3847 double* aVPos = ( onFace ? new double[ nbNodes ] : 0 );
3850 // loop on sub-meshes
3851 list<SMESHDS_SubMesh*> * pListSM = ( onFace ? &aFaceSM : &aEdgeSM );
3852 list<SMESHDS_SubMesh*>::iterator itSM = pListSM->begin();
3853 for ( ; itSM != pListSM->end(); itSM++ )
3855 SMESHDS_SubMesh* aSubMesh = (*itSM);
3857 SMDS_NodeIteratorPtr itNode = aSubMesh->GetNodes();
3858 // loop on nodes in aSubMesh
3859 while ( itNode->more() )
3862 const SMDS_MeshNode* node = itNode->next();
3863 aNodeIDs [ iNode ] = node->GetID();
3866 const SMDS_PositionPtr pos = node->GetPosition();
3867 if ( onFace ) { // on FACE
3868 const SMDS_FacePosition* fPos =
3869 dynamic_cast<const SMDS_FacePosition*>( pos );
3871 aUPos[ iNode ] = fPos->GetUParameter();
3872 aVPos[ iNode ] = fPos->GetVParameter();
3879 const SMDS_EdgePosition* ePos =
3880 dynamic_cast<const SMDS_EdgePosition*>( pos );
3882 aUPos[ iNode ] = ePos->GetUParameter();
3888 } // loop on nodes in aSubMesh
3889 } // loop on sub-meshes
3894 aSize[ 0 ] = nbNodes;
3896 string aDSName( onFace ? "Nodes on Faces" : "Nodes on Edges");
3897 aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_INT32, aSize, 1 );
3898 aDataset->CreateOnDisk();
3899 aDataset->WriteOnDisk( aNodeIDs );
3900 aDataset->CloseOnDisk();
3903 aDSName = ( onFace ? "Face U positions" : "Edge positions");
3904 aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_FLOAT64, aSize, 1);
3905 aDataset->CreateOnDisk();
3906 aDataset->WriteOnDisk( aUPos );
3907 aDataset->CloseOnDisk();
3910 aDataset = new HDFdataset( "Face V positions", aGroup, HDF_FLOAT64, aSize, 1);
3911 aDataset->CreateOnDisk();
3912 aDataset->WriteOnDisk( aVPos );
3913 aDataset->CloseOnDisk();
3918 if ( aVPos ) delete [] aVPos;
3920 } // treat positions on edges or faces
3922 // close "Node Positions" group
3923 aGroup->CloseOnDisk();
3925 } // if ( there are submeshes in SMESHDS_Mesh )
3928 // close mesh HDF group
3929 aTopGroup->CloseOnDisk();
3937 aFile->CloseOnDisk();
3940 // Convert temporary files to stream
3941 aStreamFile = SALOMEDS_Tool::PutFilesToStream( tmpDir.ToCString(), aFileSeq.in(), isMultiFile );
3943 // Remove temporary files and directory
3945 SALOMEDS_Tool::RemoveTemporaryFiles( tmpDir.ToCString(), aFileSeq.in(), true );
3947 return aStreamFile._retn();
3950 //=============================================================================
3952 * SMESH_Gen_i::SaveASCII
3954 * Save SMESH module's data in ASCII format
3956 //=============================================================================
3958 SALOMEDS::TMPFile* SMESH_Gen_i::SaveASCII( SALOMEDS::SComponent_ptr theComponent,
3960 bool isMultiFile ) {
3961 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::SaveASCII" );
3962 SALOMEDS::TMPFile_var aStreamFile = Save( theComponent, theURL, isMultiFile );
3963 return aStreamFile._retn();
3965 //after usual saving needs to encipher binary to text string
3966 //Any binary symbol will be represent as "|xx" () hexadecimal format number
3967 int size = aStreamFile.in().length();
3968 _CORBA_Octet* buffer = new _CORBA_Octet[size*3+1];
3969 for ( int i = 0; i < size; i++ )
3970 sprintf( (char*)&(buffer[i*3]), "|%02x", aStreamFile[i] );
3972 buffer[size * 3] = '\0';
3974 SALOMEDS::TMPFile_var anAsciiStreamFile = new SALOMEDS::TMPFile(size*3, size*3, buffer, 1);
3976 return anAsciiStreamFile._retn();
3979 //=============================================================================
3981 * SMESH_Gen_i::loadGeomData
3983 * Load GEOM module data
3985 //=============================================================================
3987 void SMESH_Gen_i::loadGeomData( SALOMEDS::SComponent_ptr theCompRoot )
3989 if ( theCompRoot->_is_nil() )
3992 SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow( theCompRoot->GetStudy() );
3993 if ( aStudy->_is_nil() )
3996 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
3997 aStudyBuilder->LoadWith( theCompRoot, GetGeomEngine() );
4000 //=============================================================================
4004 * Load SMESH module's data
4006 //=============================================================================
4008 bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
4009 const SALOMEDS::TMPFile& theStream,
4013 if ( theComponent->GetStudy()->StudyId() != GetCurrentStudyID() )
4014 SetCurrentStudy( theComponent->GetStudy() );
4016 /* if( !theComponent->_is_nil() )
4018 //SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow( theComponent->GetStudy() );
4019 if( !myCurrentStudy->FindComponent( "GEOM" )->_is_nil() )
4020 loadGeomData( myCurrentStudy->FindComponent( "GEOM" ) );
4023 StudyContext* myStudyContext = GetCurrentStudyContext();
4025 // Get temporary files location
4026 TCollection_AsciiString tmpDir =
4027 ( char* )( isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir().c_str() );
4029 // Convert the stream into sequence of files to process
4030 SALOMEDS::ListOfFileNames_var aFileSeq = SALOMEDS_Tool::PutStreamToFiles( theStream,
4033 TCollection_AsciiString aStudyName( "" );
4034 if ( isMultiFile ) {
4035 CORBA::String_var url = myCurrentStudy->URL();
4036 aStudyName = (char*)SALOMEDS_Tool::GetNameFromPath( url.in() ).c_str();
4038 // Set names of temporary files
4039 TCollection_AsciiString filename = tmpDir + aStudyName + "_SMESH.hdf";
4040 TCollection_AsciiString meshfile = tmpDir + aStudyName + "_SMESH_Mesh.med";
4044 HDFdataset* aDataset;
4045 HDFgroup* aTopGroup;
4047 HDFgroup* aSubGroup;
4048 HDFgroup* aSubSubGroup;
4051 // ---> open HDF file
4052 aFile = new HDFfile( (char*) filename.ToCString() );
4054 aFile->OpenOnDisk( HDF_RDONLY );
4056 catch ( HDFexception ) {
4057 INFOS( "Load(): " << filename << " not found!" );
4061 TPythonDump pd; // prevent dump during loading
4063 // DriverMED_R_SMESHDS_Mesh myReader;
4064 // myReader.SetFile( meshfile.ToCString() );
4066 // For PAL13473 ("Repetitive mesh") implementation.
4067 // New dependencies between SMESH objects are established:
4068 // now hypotheses can refer to meshes, shapes and other hypotheses.
4069 // To keep data consistent, the following order of data restoration
4071 // 1. Create hypotheses
4072 // 2. Create all meshes
4073 // 3. Load hypotheses' data
4076 list< pair< SMESH_Hypothesis_i*, string > > hypDataList;
4077 list< pair< SMESH_Mesh_i*, HDFgroup* > > meshGroupList;
4078 list< SMESH::Filter_var > filters;
4080 // get total number of top-level groups
4081 int aNbGroups = aFile->nInternalObjects();
4082 if ( aNbGroups > 0 ) {
4083 // --> in first turn we should read&create hypotheses
4084 if ( aFile->ExistInternalObject( "Hypotheses" ) ) {
4085 // open hypotheses root HDF group
4086 aTopGroup = new HDFgroup( "Hypotheses", aFile );
4087 aTopGroup->OpenOnDisk();
4089 // get number of hypotheses
4090 int aNbObjects = aTopGroup->nInternalObjects();
4091 for ( int j = 0; j < aNbObjects; j++ ) {
4092 // try to identify hypothesis
4093 char hypGrpName[ HDF_NAME_MAX_LEN+1 ];
4094 aTopGroup->InternalObjectIndentify( j, hypGrpName );
4096 if ( string( hypGrpName ).substr( 0, 10 ) == string( "Hypothesis" ) ) {
4097 // open hypothesis group
4098 aGroup = new HDFgroup( hypGrpName, aTopGroup );
4099 aGroup->OpenOnDisk();
4101 // --> get hypothesis id
4102 int id = atoi( string( hypGrpName ).substr( 10 ).c_str() );
4107 // get number of datasets
4108 int aNbSubObjects = aGroup->nInternalObjects();
4109 for ( int k = 0; k < aNbSubObjects; k++ ) {
4111 char name_of_subgroup[ HDF_NAME_MAX_LEN+1 ];
4112 aGroup->InternalObjectIndentify( k, name_of_subgroup );
4113 // --> get hypothesis name
4114 if ( strcmp( name_of_subgroup, "Name" ) == 0 ) {
4115 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4116 aDataset->OpenOnDisk();
4117 size = aDataset->GetSize();
4118 char* hypname_str = new char[ size ];
4119 aDataset->ReadFromDisk( hypname_str );
4120 hypname = string( hypname_str );
4121 delete [] hypname_str;
4122 aDataset->CloseOnDisk();
4124 // --> get hypothesis plugin library name
4125 if ( strcmp( name_of_subgroup, "LibName" ) == 0 ) {
4126 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4127 aDataset->OpenOnDisk();
4128 size = aDataset->GetSize();
4129 char* libname_str = new char[ size ];
4130 aDataset->ReadFromDisk( libname_str );
4131 if(MYDEBUG) SCRUTE( libname_str );
4132 libname = string( libname_str );
4133 delete [] libname_str;
4134 aDataset->CloseOnDisk();
4136 // --> get hypothesis data
4137 if ( strcmp( name_of_subgroup, "Data" ) == 0 ) {
4138 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4139 aDataset->OpenOnDisk();
4140 size = aDataset->GetSize();
4141 char* hypdata_str = new char[ size ];
4142 aDataset->ReadFromDisk( hypdata_str );
4143 hypdata = string( hypdata_str );
4144 delete [] hypdata_str;
4145 aDataset->CloseOnDisk();
4148 // close hypothesis HDF group
4149 aGroup->CloseOnDisk();
4151 // --> restore hypothesis from data
4152 if ( id > 0 && !hypname.empty()/* && !hypdata.empty()*/ ) { // VSR : persistent data can be empty
4153 if(MYDEBUG) MESSAGE("VSR - load hypothesis : id = " << id <<
4154 ", name = " << hypname.c_str() << ", persistent string = " << hypdata.c_str());
4155 SMESH::SMESH_Hypothesis_var myHyp;
4157 try { // protect persistence mechanism against exceptions
4158 myHyp = this->createHypothesis( hypname.c_str(), libname.c_str() );
4161 INFOS( "Exception during hypothesis creation" );
4164 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
4166 // myImpl->LoadFrom( hypdata.c_str() );
4167 hypDataList.push_back( make_pair( myImpl, hypdata ));
4168 CORBA::String_var iorString = GetORB()->object_to_string( myHyp );
4169 int newId = myStudyContext->findId( iorString.in() );
4170 myStudyContext->mapOldToNew( id, newId );
4173 if(MYDEBUG) MESSAGE( "VSR - SMESH_Gen::Load - can't get servant" );
4177 // close hypotheses root HDF group
4178 aTopGroup->CloseOnDisk();
4182 // --> then we should read&create algorithms
4183 if ( aFile->ExistInternalObject( "Algorithms" ) ) {
4184 // open algorithms root HDF group
4185 aTopGroup = new HDFgroup( "Algorithms", aFile );
4186 aTopGroup->OpenOnDisk();
4188 // get number of algorithms
4189 int aNbObjects = aTopGroup->nInternalObjects();
4190 for ( int j = 0; j < aNbObjects; j++ ) {
4191 // try to identify algorithm
4192 char hypGrpName[ HDF_NAME_MAX_LEN+1 ];
4193 aTopGroup->InternalObjectIndentify( j, hypGrpName );
4195 if ( string( hypGrpName ).substr( 0, 9 ) == string( "Algorithm" ) ) {
4196 // open algorithm group
4197 aGroup = new HDFgroup( hypGrpName, aTopGroup );
4198 aGroup->OpenOnDisk();
4200 // --> get algorithm id
4201 int id = atoi( string( hypGrpName ).substr( 9 ).c_str() );
4206 // get number of datasets
4207 int aNbSubObjects = aGroup->nInternalObjects();
4208 for ( int k = 0; k < aNbSubObjects; k++ ) {
4210 char name_of_subgroup[ HDF_NAME_MAX_LEN+1 ];
4211 aGroup->InternalObjectIndentify( k, name_of_subgroup );
4212 // --> get algorithm name
4213 if ( strcmp( name_of_subgroup, "Name" ) == 0 ) {
4214 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4215 aDataset->OpenOnDisk();
4216 size = aDataset->GetSize();
4217 char* hypname_str = new char[ size ];
4218 aDataset->ReadFromDisk( hypname_str );
4219 hypname = string( hypname_str );
4220 delete [] hypname_str;
4221 aDataset->CloseOnDisk();
4223 // --> get algorithm plugin library name
4224 if ( strcmp( name_of_subgroup, "LibName" ) == 0 ) {
4225 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4226 aDataset->OpenOnDisk();
4227 size = aDataset->GetSize();
4228 char* libname_str = new char[ size ];
4229 aDataset->ReadFromDisk( libname_str );
4230 if(MYDEBUG) SCRUTE( libname_str );
4231 libname = string( libname_str );
4232 delete [] libname_str;
4233 aDataset->CloseOnDisk();
4235 // --> get algorithm data
4236 if ( strcmp( name_of_subgroup, "Data" ) == 0 ) {
4237 aDataset = new HDFdataset( name_of_subgroup, aGroup );
4238 aDataset->OpenOnDisk();
4239 size = aDataset->GetSize();
4240 char* hypdata_str = new char[ size ];
4241 aDataset->ReadFromDisk( hypdata_str );
4242 if(MYDEBUG) SCRUTE( hypdata_str );
4243 hypdata = string( hypdata_str );
4244 delete [] hypdata_str;
4245 aDataset->CloseOnDisk();
4248 // close algorithm HDF group
4249 aGroup->CloseOnDisk();
4251 // --> restore algorithm from data
4252 if ( id > 0 && !hypname.empty()/* && !hypdata.empty()*/ ) { // VSR : persistent data can be empty
4253 if(MYDEBUG) MESSAGE("VSR - load algo : id = " << id <<
4254 ", name = " << hypname.c_str() << ", persistent string = " << hypdata.c_str());
4255 SMESH::SMESH_Hypothesis_var myHyp;
4257 try { // protect persistence mechanism against exceptions
4258 myHyp = this->createHypothesis( hypname.c_str(), libname.c_str() );
4261 INFOS( "Exception during hypothesis creation" );
4264 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
4266 //myImpl->LoadFrom( hypdata.c_str() );
4267 hypDataList.push_back( make_pair( myImpl, hypdata ));
4268 CORBA::String_var iorString = GetORB()->object_to_string( myHyp );
4269 int newId = myStudyContext->findId( iorString.in() );
4270 myStudyContext->mapOldToNew( id, newId );
4273 if(MYDEBUG) MESSAGE( "VSR - SMESH_Gen::Load - can't get servant" );
4277 // close algorithms root HDF group
4278 aTopGroup->CloseOnDisk();
4282 // --> the rest groups should be meshes
4283 for ( int i = 0; i < aNbGroups; i++ ) {
4284 // identify next group
4285 char meshName[ HDF_NAME_MAX_LEN+1 ];
4286 aFile->InternalObjectIndentify( i, meshName );
4288 if ( string( meshName ).substr( 0, 4 ) == string( "Mesh" ) ) {
4290 int id = atoi( string( meshName ).substr( 4 ).c_str() );
4294 // open mesh HDF group
4295 aTopGroup = new HDFgroup( meshName, aFile );
4296 aTopGroup->OpenOnDisk();
4298 // get number of child HDF objects
4299 int aNbObjects = aTopGroup->nInternalObjects();
4300 if ( aNbObjects > 0 ) {
4302 if(MYDEBUG) MESSAGE( "VSR - load mesh : id = " << id );
4303 SMESH::SMESH_Mesh_var myNewMesh = this->createMesh();
4304 SMESH_Mesh_i* myNewMeshImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( myNewMesh ).in() );
4305 if ( !myNewMeshImpl )
4307 meshGroupList.push_back( make_pair( myNewMeshImpl, aTopGroup ));
4309 CORBA::String_var iorString = GetORB()->object_to_string( myNewMesh );
4310 int newId = myStudyContext->findId( iorString.in() );
4311 myStudyContext->mapOldToNew( id, newId );
4314 // try to read and set auto color flag
4315 char aMeshAutoColorName[ 30 ];
4316 sprintf( aMeshAutoColorName, "AutoColorMesh %d", id);
4317 if( aTopGroup->ExistInternalObject( aMeshAutoColorName ) )
4319 aDataset = new HDFdataset( aMeshAutoColorName, aTopGroup );
4320 aDataset->OpenOnDisk();
4321 size = aDataset->GetSize();
4322 int* anAutoColor = new int[ size ];
4323 aDataset->ReadFromDisk( anAutoColor );
4324 aDataset->CloseOnDisk();
4325 myNewMeshImpl->GetImpl().SetAutoColor( (bool)anAutoColor[0] );
4326 delete [] anAutoColor;
4329 // try to read and set reference to shape
4330 GEOM::GEOM_Object_var aShapeObject;
4331 if ( aTopGroup->ExistInternalObject( "Ref on shape" ) ) {
4332 // load mesh "Ref on shape" - it's an entry to SObject
4333 aDataset = new HDFdataset( "Ref on shape", aTopGroup );
4334 aDataset->OpenOnDisk();
4335 size = aDataset->GetSize();
4336 char* refFromFile = new char[ size ];
4337 aDataset->ReadFromDisk( refFromFile );
4338 aDataset->CloseOnDisk();
4339 if ( strlen( refFromFile ) > 0 ) {
4340 SALOMEDS::SObject_wrap shapeSO = myCurrentStudy->FindObjectID( refFromFile );
4342 // Make sure GEOM data are loaded first
4343 //loadGeomData( shapeSO->GetFatherComponent() );
4345 CORBA::Object_var shapeObject = SObjectToObject( shapeSO );
4346 if ( !CORBA::is_nil( shapeObject ) ) {
4347 aShapeObject = GEOM::GEOM_Object::_narrow( shapeObject );
4348 if ( !aShapeObject->_is_nil() )
4349 myNewMeshImpl->SetShape( aShapeObject );
4352 delete [] refFromFile;
4355 // issue 20918. Restore Persistent Id of SMESHDS_Mesh
4356 if( aTopGroup->ExistInternalObject( "meshPersistentId" ) )
4358 aDataset = new HDFdataset( "meshPersistentId", aTopGroup );
4359 aDataset->OpenOnDisk();
4360 size = aDataset->GetSize();
4361 int* meshPersistentId = new int[ size ];
4362 aDataset->ReadFromDisk( meshPersistentId );
4363 aDataset->CloseOnDisk();
4364 myNewMeshImpl->GetImpl().GetMeshDS()->SetPersistentId( *meshPersistentId );
4365 delete [] meshPersistentId;
4371 // As all object that can be referred by hypothesis are created,
4372 // we can restore hypothesis data
4374 list< pair< SMESH_Hypothesis_i*, string > >::iterator hyp_data;
4375 for ( hyp_data = hypDataList.begin(); hyp_data != hypDataList.end(); ++hyp_data )
4377 SMESH_Hypothesis_i* hyp = hyp_data->first;
4378 string & data = hyp_data->second;
4379 hyp->LoadFrom( data.c_str() );
4382 // Restore the rest mesh data
4384 list< pair< SMESH_Mesh_i*, HDFgroup* > >::iterator meshi_group;
4385 for ( meshi_group = meshGroupList.begin(); meshi_group != meshGroupList.end(); ++meshi_group )
4387 aTopGroup = meshi_group->second;
4388 SMESH_Mesh_i* myNewMeshImpl = meshi_group->first;
4389 //::SMESH_Mesh& myLocMesh = myNewMeshImpl->GetImpl();
4390 //SMESHDS_Mesh* mySMESHDSMesh = myLocMesh.GetMeshDS();
4392 GEOM::GEOM_Object_var aShapeObject = myNewMeshImpl->GetShapeToMesh();
4393 bool hasData = false;
4396 CORBA::String_var iorString = GetORB()->object_to_string( myNewMeshImpl->_this() );
4397 int newId = myStudyContext->findId( iorString.in() );
4398 int id = myStudyContext->getOldId( newId );
4400 // try to find mesh data dataset
4401 if ( aTopGroup->ExistInternalObject( "Has data" ) ) {
4402 // load mesh "has data" flag
4403 aDataset = new HDFdataset( "Has data", aTopGroup );
4404 aDataset->OpenOnDisk();
4405 size = aDataset->GetSize();
4406 char* strHasData = new char[ size ];
4407 aDataset->ReadFromDisk( strHasData );
4408 aDataset->CloseOnDisk();
4409 if ( strcmp( strHasData, "1") == 0 ) {
4410 // read mesh data from MED file
4411 // myReader.SetMesh( mySMESHDSMesh );
4412 // myReader.SetMeshId( id );
4413 // myReader.Perform();
4416 delete [] strHasData;
4419 // Try to get applied ALGORITHMS (mesh is not cleared by algo addition because
4420 // nodes and elements are not yet put into sub-meshes)
4421 if ( aTopGroup->ExistInternalObject( "Applied Algorithms" ) ) {
4422 aGroup = new HDFgroup( "Applied Algorithms", aTopGroup );
4423 aGroup->OpenOnDisk();
4424 // get number of applied algorithms
4425 int aNbSubObjects = aGroup->nInternalObjects();
4426 if(MYDEBUG) MESSAGE( "VSR - number of applied algos " << aNbSubObjects );
4427 for ( int j = 0; j < aNbSubObjects; j++ ) {
4428 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4429 aGroup->InternalObjectIndentify( j, name_dataset );
4430 // check if it is an algorithm
4431 if ( string( name_dataset ).substr( 0, 4 ) == string( "Algo" ) ) {
4432 aDataset = new HDFdataset( name_dataset, aGroup );
4433 aDataset->OpenOnDisk();
4434 size = aDataset->GetSize();
4435 char* refFromFile = new char[ size ];
4436 aDataset->ReadFromDisk( refFromFile );
4437 aDataset->CloseOnDisk();
4438 // san - it is impossible to recover applied algorithms using their entries within Load() method
4439 //SALOMEDS::SObject_wrap hypSO = myCurrentStudy->FindObjectID( refFromFile );
4440 //CORBA::Object_var hypObject = SObjectToObject( hypSO );
4441 int id = atoi( refFromFile );
4442 delete [] refFromFile;
4443 string anIOR = myStudyContext->getIORbyOldId( id );
4444 if ( !anIOR.empty() ) {
4445 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
4446 if ( !CORBA::is_nil( hypObject ) ) {
4447 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
4448 if ( !anHyp->_is_nil() && (!aShapeObject->_is_nil()
4449 || !myNewMeshImpl->HasShapeToMesh()) )
4450 myNewMeshImpl->addHypothesis( aShapeObject, anHyp );
4455 aGroup->CloseOnDisk();
4458 // try to get applied hypotheses
4459 if ( aTopGroup->ExistInternalObject( "Applied Hypotheses" ) ) {
4460 aGroup = new HDFgroup( "Applied Hypotheses", aTopGroup );
4461 aGroup->OpenOnDisk();
4462 // get number of applied hypotheses
4463 int aNbSubObjects = aGroup->nInternalObjects();
4464 for ( int j = 0; j < aNbSubObjects; j++ ) {
4465 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4466 aGroup->InternalObjectIndentify( j, name_dataset );
4467 // check if it is a hypothesis
4468 if ( string( name_dataset ).substr( 0, 3 ) == string( "Hyp" ) ) {
4469 aDataset = new HDFdataset( name_dataset, aGroup );
4470 aDataset->OpenOnDisk();
4471 size = aDataset->GetSize();
4472 char* refFromFile = new char[ size ];
4473 aDataset->ReadFromDisk( refFromFile );
4474 aDataset->CloseOnDisk();
4475 // san - it is impossible to recover applied hypotheses using their entries within Load() method
4476 //SALOMEDS::SObject_wrap hypSO = myCurrentStudy->FindObjectID( refFromFile );
4477 //CORBA::Object_var hypObject = SObjectToObject( hypSO );
4478 int id = atoi( refFromFile );
4479 delete [] refFromFile;
4480 string anIOR = myStudyContext->getIORbyOldId( id );
4481 if ( !anIOR.empty() ) {
4482 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
4483 if ( !CORBA::is_nil( hypObject ) ) {
4484 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
4485 if ( !anHyp->_is_nil() && (!aShapeObject->_is_nil()
4486 || !myNewMeshImpl->HasShapeToMesh()) )
4487 myNewMeshImpl->addHypothesis( aShapeObject, anHyp );
4492 aGroup->CloseOnDisk();
4495 // --> try to find SUB-MESHES containers for each type of submesh
4496 for ( int j = GetSubMeshOnVertexTag(); j <= GetSubMeshOnCompoundTag(); j++ ) {
4497 const char* name_meshgroup = 0;
4498 if ( j == GetSubMeshOnVertexTag() )
4499 name_meshgroup = "SubMeshes On Vertex";
4500 else if ( j == GetSubMeshOnEdgeTag() )
4501 name_meshgroup = "SubMeshes On Edge";
4502 else if ( j == GetSubMeshOnWireTag() )
4503 name_meshgroup = "SubMeshes On Wire";
4504 else if ( j == GetSubMeshOnFaceTag() )
4505 name_meshgroup = "SubMeshes On Face";
4506 else if ( j == GetSubMeshOnShellTag() )
4507 name_meshgroup = "SubMeshes On Shell";
4508 else if ( j == GetSubMeshOnSolidTag() )
4509 name_meshgroup = "SubMeshes On Solid";
4510 else if ( j == GetSubMeshOnCompoundTag() )
4511 name_meshgroup = "SubMeshes On Compound";
4513 // try to get submeshes container HDF group
4514 if ( aTopGroup->ExistInternalObject( name_meshgroup ) ) {
4515 // open submeshes containers HDF group
4516 aGroup = new HDFgroup( name_meshgroup, aTopGroup );
4517 aGroup->OpenOnDisk();
4519 // get number of submeshes
4520 int aNbSubMeshes = aGroup->nInternalObjects();
4521 for ( int k = 0; k < aNbSubMeshes; k++ ) {
4523 char name_submeshgroup[ HDF_NAME_MAX_LEN+1 ];
4524 aGroup->InternalObjectIndentify( k, name_submeshgroup );
4525 if ( strncmp( name_submeshgroup, "SubMesh", 7 ) == 0 ) {
4526 // --> get submesh id
4527 int subid = atoi( name_submeshgroup + 7 );
4530 // open submesh HDF group
4531 aSubGroup = new HDFgroup( name_submeshgroup, aGroup );
4532 aSubGroup->OpenOnDisk();
4534 // try to read and set reference to subshape
4535 GEOM::GEOM_Object_var aSubShapeObject;
4536 SMESH::SMESH_subMesh_var aSubMesh;
4538 if ( aSubGroup->ExistInternalObject( "Ref on shape" ) ) {
4539 // load submesh "Ref on shape" - it's an entry to SObject
4540 aDataset = new HDFdataset( "Ref on shape", aSubGroup );
4541 aDataset->OpenOnDisk();
4542 size = aDataset->GetSize();
4543 char* refFromFile = new char[ size ];
4544 aDataset->ReadFromDisk( refFromFile );
4545 aDataset->CloseOnDisk();
4546 if ( strlen( refFromFile ) > 0 ) {
4547 SALOMEDS::SObject_wrap subShapeSO = myCurrentStudy->FindObjectID( refFromFile );
4548 CORBA::Object_var subShapeObject = SObjectToObject( subShapeSO );
4549 if ( !CORBA::is_nil( subShapeObject ) ) {
4550 aSubShapeObject = GEOM::GEOM_Object::_narrow( subShapeObject );
4551 if ( !aSubShapeObject->_is_nil() )
4552 aSubMesh = SMESH::SMESH_subMesh::_duplicate
4553 ( myNewMeshImpl->createSubMesh( aSubShapeObject ) );
4554 if ( aSubMesh->_is_nil() )
4556 string iorSubString = GetORB()->object_to_string( aSubMesh );
4557 int newSubId = myStudyContext->findId( iorSubString );
4558 myStudyContext->mapOldToNew( subid, newSubId );
4563 if ( aSubMesh->_is_nil() )
4566 // try to get applied algorithms
4567 if ( aSubGroup->ExistInternalObject( "Applied Algorithms" ) ) {
4568 // open "applied algorithms" HDF group
4569 aSubSubGroup = new HDFgroup( "Applied Algorithms", aSubGroup );
4570 aSubSubGroup->OpenOnDisk();
4571 // get number of applied algorithms
4572 int aNbSubObjects = aSubSubGroup->nInternalObjects();
4573 for ( int l = 0; l < aNbSubObjects; l++ ) {
4574 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4575 aSubSubGroup->InternalObjectIndentify( l, name_dataset );
4576 // check if it is an algorithm
4577 if ( strncmp( name_dataset, "Algo", 4 ) == 0 ) {
4578 aDataset = new HDFdataset( name_dataset, aSubSubGroup );
4579 aDataset->OpenOnDisk();
4580 size = aDataset->GetSize();
4581 char* refFromFile = new char[ size ];
4582 aDataset->ReadFromDisk( refFromFile );
4583 aDataset->CloseOnDisk();
4585 int id = atoi( refFromFile );
4586 string anIOR = myStudyContext->getIORbyOldId( id );
4587 if ( !anIOR.empty() ) {
4588 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
4589 if ( !CORBA::is_nil( hypObject ) ) {
4590 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
4591 if ( !anHyp->_is_nil() && !aShapeObject->_is_nil() )
4592 myNewMeshImpl->addHypothesis( aSubShapeObject, anHyp );
4597 // close "applied algorithms" HDF group
4598 aSubSubGroup->CloseOnDisk();
4601 // try to get applied hypotheses
4602 if ( aSubGroup->ExistInternalObject( "Applied Hypotheses" ) ) {
4603 // open "applied hypotheses" HDF group
4604 aSubSubGroup = new HDFgroup( "Applied Hypotheses", aSubGroup );
4605 aSubSubGroup->OpenOnDisk();
4606 // get number of applied hypotheses
4607 int aNbSubObjects = aSubSubGroup->nInternalObjects();
4608 for ( int l = 0; l < aNbSubObjects; l++ ) {
4609 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
4610 aSubSubGroup->InternalObjectIndentify( l, name_dataset );
4611 // check if it is a hypothesis
4612 if ( string( name_dataset ).substr( 0, 3 ) == string( "Hyp" ) ) {
4613 aDataset = new HDFdataset( name_dataset, aSubSubGroup );
4614 aDataset->OpenOnDisk();
4615 size = aDataset->GetSize();
4616 char* refFromFile = new char[ size ];
4617 aDataset->ReadFromDisk( refFromFile );
4618 aDataset->CloseOnDisk();
4620 int id = atoi( refFromFile );
4621 string anIOR = myStudyContext->getIORbyOldId( id );
4622 if ( !anIOR.empty() ) {
4623 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
4624 if ( !CORBA::is_nil( hypObject ) ) {
4625 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
4626 if ( !anHyp->_is_nil() && !aShapeObject->_is_nil() )
4627 myNewMeshImpl->addHypothesis( aSubShapeObject, anHyp );
4632 // close "APPLIED HYPOTHESES" hdf group
4633 aSubSubGroup->CloseOnDisk();
4636 // close SUB-MESH hdf group
4637 aSubGroup->CloseOnDisk();
4640 // close SUB-MESHES containers hdf group
4641 aGroup->CloseOnDisk();
4645 // try to get GROUPS
4646 for ( int ii = GetNodeGroupsTag(); ii <= GetBallElementsGroupsTag(); ii++ ) {
4647 char name_group[ 30 ];
4648 if ( ii == GetNodeGroupsTag() )
4649 strcpy( name_group, "Groups of Nodes" );
4650 else if ( ii == GetEdgeGroupsTag() )
4651 strcpy( name_group, "Groups of Edges" );
4652 else if ( ii == GetFaceGroupsTag() )
4653 strcpy( name_group, "Groups of Faces" );
4654 else if ( ii == GetVolumeGroupsTag() )
4655 strcpy( name_group, "Groups of Volumes" );
4656 else if ( ii == Get0DElementsGroupsTag() )
4657 strcpy( name_group, "Groups of 0D Elements" );
4658 else if ( ii == GetBallElementsGroupsTag() )
4659 strcpy( name_group, "Groups of Balls" );
4661 if ( aTopGroup->ExistInternalObject( name_group ) ) {
4662 aGroup = new HDFgroup( name_group, aTopGroup );
4663 aGroup->OpenOnDisk();
4664 // PAL23514: get all names from the HDFgroup to avoid iteration on its contents
4665 // within aGroup->ExistInternalObject( name )
4666 std::vector< std::string > subNames;
4667 TColStd_MapOfAsciiString mapOfNames;
4668 aGroup->GetAllObjects( subNames );
4669 for ( size_t iN = 0; iN < subNames.size(); ++iN )
4670 mapOfNames.Add( subNames[ iN ].c_str() );
4672 for ( size_t j = 0; j < subNames.size(); j++ ) {
4673 const std::string& name_dataset = subNames[ j ];
4674 // check if it is a group
4675 if ( name_dataset.substr( 0, 5 ) == "Group" ) {
4677 int subid = atoi( name_dataset.substr( 5 ).c_str() );
4680 aDataset = new HDFdataset( name_dataset.c_str(), aGroup );
4681 aDataset->OpenOnDisk();
4683 // Retrieve actual group name
4684 size = aDataset->GetSize();
4685 char* nameFromFile = new char[ size ];
4686 aDataset->ReadFromDisk( nameFromFile );
4687 aDataset->CloseOnDisk();
4689 // Try to find a shape reference
4690 TopoDS_Shape aShape;
4691 char aRefName[ 30 ];
4692 sprintf( aRefName, "Ref on shape %d", subid);
4693 if ( mapOfNames.Contains( aRefName ))
4695 // load mesh "Ref on shape" - it's an entry to SObject
4696 aDataset = new HDFdataset( aRefName, aGroup );
4697 aDataset->OpenOnDisk();
4698 size = aDataset->GetSize();
4699 char* refFromFile = new char[ size ];
4700 aDataset->ReadFromDisk( refFromFile );
4701 aDataset->CloseOnDisk();
4702 if ( strlen( refFromFile ) > 0 ) {
4703 SALOMEDS::SObject_wrap shapeSO = myCurrentStudy->FindObjectID( refFromFile );
4704 CORBA::Object_var shapeObject = SObjectToObject( shapeSO );
4705 if ( !CORBA::is_nil( shapeObject ) ) {
4706 aShapeObject = GEOM::GEOM_Object::_narrow( shapeObject );
4707 if ( !aShapeObject->_is_nil() )
4708 aShape = GeomObjectToShape( aShapeObject );
4712 // Try to read a filter of SMESH_GroupOnFilter
4713 SMESH::Filter_var filter;
4714 SMESH_PredicatePtr predicate;
4715 std::string hdfGrpName = ( SMESH_Comment( "Filter ") << subid );
4716 if ( mapOfNames.Contains( hdfGrpName.c_str() ))
4718 aDataset = new HDFdataset( hdfGrpName.c_str(), aGroup );
4719 aDataset->OpenOnDisk();
4720 size = aDataset->GetSize();
4721 char* persistStr = new char[ size ];
4722 aDataset->ReadFromDisk( persistStr );
4723 aDataset->CloseOnDisk();
4724 if ( strlen( persistStr ) > 0 ) {
4725 filter = SMESH_GroupOnFilter_i::StringToFilter( persistStr );
4726 predicate = SMESH_GroupOnFilter_i::GetPredicate( filter );
4727 filters.push_back( filter );
4731 // Create group servant
4732 SMESH::ElementType type = (SMESH::ElementType)(ii - GetNodeGroupsTag() + 1);
4733 SMESH::SMESH_GroupBase_var aNewGroup = SMESH::SMESH_GroupBase::_duplicate
4734 ( myNewMeshImpl->createGroup( type, nameFromFile, aShape, predicate ) );
4735 // Obtain a SMESHDS_Group object
4736 if ( aNewGroup->_is_nil() )
4739 string iorSubString = GetORB()->object_to_string( aNewGroup );
4740 int newSubId = myStudyContext->findId( iorSubString );
4741 myStudyContext->mapOldToNew( subid, newSubId );
4743 SMESH_GroupBase_i* aGroupImpl = SMESH::DownCast< SMESH_GroupBase_i*>( aNewGroup );
4747 if ( SMESH_GroupOnFilter_i* aFilterGroup =
4748 dynamic_cast< SMESH_GroupOnFilter_i*>( aGroupImpl ))
4750 aFilterGroup->SetFilter( filter );
4751 filter->UnRegister();
4753 SMESHDS_GroupBase* aGroupBaseDS = aGroupImpl->GetGroupDS();
4754 if ( !aGroupBaseDS )
4757 aGroupBaseDS->SetStoreName( name_dataset.c_str() );
4760 // Read color of the group
4761 char aGroupColorName[ 30 ];
4762 sprintf( aGroupColorName, "ColorGroup %d", subid);
4763 if ( mapOfNames.Contains( aGroupColorName ))
4765 aDataset = new HDFdataset( aGroupColorName, aGroup );
4766 aDataset->OpenOnDisk();
4767 size = aDataset->GetSize();
4768 double* anRGB = new double[ size ];
4769 aDataset->ReadFromDisk( anRGB );
4770 aDataset->CloseOnDisk();
4771 Quantity_Color aColor( anRGB[0], anRGB[1], anRGB[2], Quantity_TOC_RGB );
4772 aGroupBaseDS->SetColor( aColor );
4776 aGroup->CloseOnDisk();
4780 // instead of reading mesh data, we read only brief information of all
4781 // objects: mesh, groups, sub-meshes (issue 0021208 )
4784 SMESH_PreMeshInfo::LoadFromFile( myNewMeshImpl, id,
4785 meshfile.ToCString(), filename.ToCString(),
4789 // read Sub-Mesh ORDER if any
4790 if( aTopGroup->ExistInternalObject( "Mesh Order" ) ) {
4791 aDataset = new HDFdataset( "Mesh Order", aTopGroup );
4792 aDataset->OpenOnDisk();
4793 size = aDataset->GetSize();
4794 int* smIDs = new int[ size ];
4795 aDataset->ReadFromDisk( smIDs );
4796 aDataset->CloseOnDisk();
4797 TListOfListOfInt anOrderIds;
4798 anOrderIds.push_back( TListOfInt() );
4799 for ( int i = 0; i < size; i++ )
4800 if ( smIDs[ i ] < 0 ) // is separator
4801 anOrderIds.push_back( TListOfInt() );
4803 anOrderIds.back().push_back(smIDs[ i ]);
4805 myNewMeshImpl->GetImpl().SetMeshOrder( anOrderIds );
4809 // update hyps needing full mesh data restored (issue 20918)
4810 for ( hyp_data = hypDataList.begin(); hyp_data != hypDataList.end(); ++hyp_data )
4812 SMESH_Hypothesis_i* hyp = hyp_data->first;
4813 hyp->UpdateAsMeshesRestored();
4816 // notify algos on completed restoration to set sub-mesh event listeners
4817 for ( meshi_group = meshGroupList.begin(); meshi_group != meshGroupList.end(); ++meshi_group )
4819 SMESH_Mesh_i* myNewMeshImpl = meshi_group->first;
4820 ::SMESH_Mesh& myLocMesh = myNewMeshImpl->GetImpl();
4822 TopoDS_Shape myLocShape;
4823 if(myLocMesh.HasShapeToMesh())
4824 myLocShape = myLocMesh.GetShapeToMesh();
4826 myLocShape = SMESH_Mesh::PseudoShape();
4828 myLocMesh.GetSubMesh(myLocShape)->
4829 ComputeStateEngine (SMESH_subMesh::SUBMESH_RESTORED);
4832 // let filters detect dependency on mesh groups via FT_BelongToMeshGroup predicate (22877)
4833 list< SMESH::Filter_var >::iterator f = filters.begin();
4834 for ( ; f != filters.end(); ++f )
4835 if ( SMESH::Filter_i * fi = SMESH::DownCast< SMESH::Filter_i*>( *f ))
4836 fi->FindBaseObjects();
4841 aTopGroup->CloseOnDisk();
4844 aFile->CloseOnDisk();
4847 // Remove temporary files created from the stream
4850 SMESH_File meshFile( meshfile.ToCString() );
4851 if ( !meshFile ) // no meshfile exists
4853 SALOMEDS_Tool::RemoveTemporaryFiles( tmpDir.ToCString(), aFileSeq.in(), true );
4857 Engines::Container_var container = GetContainerRef();
4858 if ( Engines_Container_i* container_i = SMESH::DownCast<Engines_Container_i*>( container ))
4860 container_i->registerTemporaryFile( filename.ToCString() );
4861 container_i->registerTemporaryFile( meshfile.ToCString() );
4862 container_i->registerTemporaryFile( tmpDir.ToCString() );
4866 pd << ""; // prevent optimizing pd out
4868 // creation of tree nodes for all data objects in the study
4869 // to support tree representation customization and drag-n-drop:
4870 SALOMEDS::Study_var study = theComponent->GetStudy();
4871 SALOMEDS::UseCaseBuilder_wrap useCaseBuilder = study->GetUseCaseBuilder();
4872 if ( !useCaseBuilder->IsUseCaseNode( theComponent ) ) {
4873 useCaseBuilder->SetRootCurrent();
4874 useCaseBuilder->Append( theComponent ); // component object is added as the top level item
4875 SALOMEDS::ChildIterator_wrap it = study->NewChildIterator( theComponent );
4876 for (it->InitEx(true); it->More(); it->Next()) {
4877 useCaseBuilder->AppendTo( it->Value()->GetFather(), it->Value() );
4884 //=============================================================================
4886 * SMESH_Gen_i::LoadASCII
4888 * Load SMESH module's data in ASCII format
4890 //=============================================================================
4892 bool SMESH_Gen_i::LoadASCII( SALOMEDS::SComponent_ptr theComponent,
4893 const SALOMEDS::TMPFile& theStream,
4895 bool isMultiFile ) {
4896 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::LoadASCII" );
4897 return Load( theComponent, theStream, theURL, isMultiFile );
4899 //before call main ::Load method it's need for decipher text format to
4900 //binary ( "|xx" => x' )
4901 int size = theStream.length();
4902 if ( int((size / 3 )*3) != size ) //error size of buffer
4905 int real_size = int(size / 3);
4907 _CORBA_Octet* buffer = new _CORBA_Octet[real_size];
4910 unsigned int c = -1;
4911 for ( int i = 0; i < real_size; i++ )
4913 memcpy( &(tmp[0]), &(theStream[i*3+1]), 2 );
4914 sscanf( tmp, "%x", &c );
4915 sprintf( (char*)&(buffer[i]), "%c", (char)c );
4918 SALOMEDS::TMPFile_var aRealStreamFile = new SALOMEDS::TMPFile(real_size, real_size, buffer, 1);
4920 return Load( theComponent, *(aRealStreamFile._retn()), theURL, isMultiFile );
4923 //=============================================================================
4925 * SMESH_Gen_i::Close
4927 * Clears study-connected data when it is closed
4929 //=============================================================================
4931 void SMESH_Gen_i::Close( SALOMEDS::SComponent_ptr theComponent )
4933 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Close" );
4935 // set correct current study
4936 SALOMEDS::Study_var study = theComponent->GetStudy();
4937 if ( study->StudyId() != GetCurrentStudyID())
4938 setCurrentStudy( study, /*IsBeingClosed=*/true );
4940 // Clear study contexts data
4941 int studyId = GetCurrentStudyID();
4942 if ( myStudyContextMap.find( studyId ) != myStudyContextMap.end() ) {
4943 delete myStudyContextMap[ studyId ];
4944 myStudyContextMap.erase( studyId );
4947 // remove the tmp files meshes are loaded from
4948 SMESH_PreMeshInfo::RemoveStudyFiles_TMP_METHOD( theComponent );
4950 myCurrentStudy = SALOMEDS::Study::_nil();
4954 //=============================================================================
4956 * SMESH_Gen_i::ComponentDataType
4958 * Get component data type
4960 //=============================================================================
4962 char* SMESH_Gen_i::ComponentDataType()
4964 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::ComponentDataType" );
4965 return CORBA::string_dup( "SMESH" );
4969 //=============================================================================
4971 * SMESH_Gen_i::IORToLocalPersistentID
4973 * Transform data from transient form to persistent
4975 //=============================================================================
4977 char* SMESH_Gen_i::IORToLocalPersistentID( SALOMEDS::SObject_ptr /*theSObject*/,
4978 const char* IORString,
4979 CORBA::Boolean /*isMultiFile*/,
4980 CORBA::Boolean /*isASCII*/ )
4982 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::IORToLocalPersistentID" );
4983 StudyContext* myStudyContext = GetCurrentStudyContext();
4985 if ( myStudyContext && strcmp( IORString, "" ) != 0 ) {
4986 int anId = myStudyContext->findId( IORString );
4988 if(MYDEBUG) MESSAGE( "VSR " << anId )
4990 sprintf( strId, "%d", anId );
4991 return CORBA::string_dup( strId );
4994 return CORBA::string_dup( "" );
4997 //=============================================================================
4999 * SMESH_Gen_i::LocalPersistentIDToIOR
5001 * Transform data from persistent form to transient
5003 //=============================================================================
5005 char* SMESH_Gen_i::LocalPersistentIDToIOR( SALOMEDS::SObject_ptr /*theSObject*/,
5006 const char* aLocalPersistentID,
5007 CORBA::Boolean /*isMultiFile*/,
5008 CORBA::Boolean /*isASCII*/ )
5010 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::LocalPersistentIDToIOR(): id = " << aLocalPersistentID );
5011 StudyContext* myStudyContext = GetCurrentStudyContext();
5013 if ( myStudyContext && strcmp( aLocalPersistentID, "" ) != 0 ) {
5014 int anId = atoi( aLocalPersistentID );
5015 return CORBA::string_dup( myStudyContext->getIORbyOldId( anId ).c_str() );
5017 return CORBA::string_dup( "" );
5020 //=======================================================================
5021 //function : RegisterObject
5023 //=======================================================================
5025 int SMESH_Gen_i::RegisterObject(CORBA::Object_ptr theObject)
5027 StudyContext* myStudyContext = GetCurrentStudyContext();
5028 if ( myStudyContext && !CORBA::is_nil( theObject )) {
5029 CORBA::String_var iorString = GetORB()->object_to_string( theObject );
5030 return myStudyContext->addObject( string( iorString.in() ) );
5035 //================================================================================
5037 * \brief Return id of registered object
5038 * \param theObject - the Object
5039 * \retval int - Object id
5041 //================================================================================
5043 CORBA::Long SMESH_Gen_i::GetObjectId(CORBA::Object_ptr theObject)
5045 StudyContext* myStudyContext = GetCurrentStudyContext();
5046 if ( myStudyContext && !CORBA::is_nil( theObject )) {
5047 string iorString = GetORB()->object_to_string( theObject );
5048 return myStudyContext->findId( iorString );
5053 //=============================================================================
5055 * SMESH_Gen_i::SetName
5057 * Set a new object name
5059 //=============================================================================
5060 void SMESH_Gen_i::SetName(const char* theIOR,
5061 const char* theName)
5063 if ( theIOR && strcmp( theIOR, "" ) ) {
5064 CORBA::Object_var anObject = GetORB()->string_to_object( theIOR );
5065 SALOMEDS::SObject_wrap aSO = ObjectToSObject( myCurrentStudy, anObject );
5066 if ( !aSO->_is_nil() ) {
5067 SetName( aSO, theName );
5072 int SMESH_Gen_i::GetCurrentStudyID()
5074 return myCurrentStudy->_is_nil() || myCurrentStudy->_non_existent() ? -1 : myCurrentStudy->StudyId();
5077 // Version information
5078 char* SMESH_Gen_i::getVersion()
5080 #if SMESH_DEVELOPMENT
5081 return CORBA::string_dup(SMESH_VERSION_STR"dev");
5083 return CORBA::string_dup(SMESH_VERSION_STR);
5087 //=================================================================================
5088 // function : Move()
5089 // purpose : Moves objects to the specified position.
5090 // Is used in the drag-n-drop functionality.
5091 //=================================================================================
5092 void SMESH_Gen_i::Move( const SMESH::sobject_list& what,
5093 SALOMEDS::SObject_ptr where,
5096 if ( CORBA::is_nil( where ) ) return;
5098 SALOMEDS::Study_var study = where->GetStudy();
5099 SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();
5100 SALOMEDS::UseCaseBuilder_var useCaseBuilder = study->GetUseCaseBuilder();
5101 SALOMEDS::SComponent_var father = where->GetFatherComponent();
5102 std::string dataType = father->ComponentDataType();
5103 if ( dataType != "SMESH" ) return; // not a SMESH component
5105 SALOMEDS::SObject_var objAfter;
5106 if ( row >= 0 && useCaseBuilder->HasChildren( where ) ) {
5107 // insert at given row -> find insertion position
5108 SALOMEDS::UseCaseIterator_var useCaseIt = useCaseBuilder->GetUseCaseIterator( where );
5110 for ( i = 0; i < row && useCaseIt->More(); i++, useCaseIt->Next() );
5111 if ( i == row && useCaseIt->More() ) {
5112 objAfter = useCaseIt->Value();
5116 for ( CORBA::ULong i = 0; i < what.length(); i++ ) {
5117 SALOMEDS::SObject_var sobj = what[i];
5118 if ( CORBA::is_nil( sobj ) ) continue; // skip bad object
5119 // insert the object to the use case tree
5120 if ( !CORBA::is_nil( objAfter ) )
5121 useCaseBuilder->InsertBefore( sobj, objAfter ); // insert at given row
5123 useCaseBuilder->AppendTo( where, sobj ); // append to the end of list
5126 //================================================================================
5128 * \brief Returns true if algorithm can be used to mesh a given geometry
5129 * \param [in] theAlgoType - the algorithm type
5130 * \param [in] theLibName - a name of the Plug-in library implementing the algorithm
5131 * \param [in] theGeomObject - the geometry to mesh
5132 * \param [in] toCheckAll - if \c True, returns \c True if all shapes are meshable,
5133 * else, returns \c True if at least one shape is meshable
5134 * \return CORBA::Boolean - can or can't
5136 //================================================================================
5138 CORBA::Boolean SMESH_Gen_i::IsApplicable ( const char* theAlgoType,
5139 const char* theLibName,
5140 GEOM::GEOM_Object_ptr theGeomObject,
5141 CORBA::Boolean toCheckAll)
5145 std::string aPlatformLibName;
5146 typedef GenericHypothesisCreator_i* (*GetHypothesisCreator)(const char*);
5147 GenericHypothesisCreator_i* aCreator =
5148 getHypothesisCreator(theAlgoType, theLibName, aPlatformLibName);
5151 TopoDS_Shape shape = GeomObjectToShape( theGeomObject );
5152 return shape.IsNull() || aCreator->IsApplicable( shape, toCheckAll );
5159 SMESH_CATCH( SMESH::doNothing );
5162 cout << "SMESH_Gen_i::IsApplicable(): exception in " << ( theAlgoType ? theAlgoType : "") << endl;
5167 //=================================================================================
5168 // function : GetInsideSphere
5169 // purpose : Collect indices of elements, which are located inside the sphere
5170 //=================================================================================
5171 SMESH::long_array* SMESH_Gen_i::GetInsideSphere( SMESH::SMESH_IDSource_ptr meshPart,
5172 SMESH::ElementType theElemType,
5178 SMESH::long_array_var aResult = new SMESH::long_array();
5179 if ( meshPart->_is_nil() )
5180 return aResult._retn();
5182 // 1. Create geometrical object
5183 gp_Pnt aP( theX, theY, theZ );
5184 TopoDS_Shape aShape = BRepPrimAPI_MakeSphere( aP, theR ).Shape();
5186 std::vector<long> lst =_GetInside(meshPart, theElemType, aShape);
5188 if ( lst.size() > 0 ) {
5189 aResult->length( lst.size() );
5190 for ( size_t i = 0; i < lst.size(); i++ ) {
5191 aResult[i] = lst[i];
5194 return aResult._retn();
5197 SMESH::long_array* SMESH_Gen_i::GetInsideBox( SMESH::SMESH_IDSource_ptr meshPart,
5198 SMESH::ElementType theElemType,
5199 CORBA::Double theX1,
5200 CORBA::Double theY1,
5201 CORBA::Double theZ1,
5202 CORBA::Double theX2,
5203 CORBA::Double theY2,
5204 CORBA::Double theZ2) {
5205 SMESH::long_array_var aResult = new SMESH::long_array();
5206 if( meshPart->_is_nil() )
5207 return aResult._retn();
5209 TopoDS_Shape aShape = BRepPrimAPI_MakeBox( gp_Pnt( theX1, theY1, theZ1 ), gp_Pnt( theX2, theY2, theZ2 ) ).Shape();
5211 std::vector<long> lst =_GetInside(meshPart, theElemType, aShape);
5213 if( lst.size() > 0 ) {
5214 aResult->length( lst.size() );
5215 for ( size_t i = 0; i < lst.size(); i++ ) {
5216 aResult[i] = lst[i];
5219 return aResult._retn();
5222 SMESH::long_array* SMESH_Gen_i::GetInsideCylinder( SMESH::SMESH_IDSource_ptr meshPart,
5223 SMESH::ElementType theElemType,
5227 CORBA::Double theDX,
5228 CORBA::Double theDY,
5229 CORBA::Double theDZ,
5231 CORBA::Double theR ){
5232 SMESH::long_array_var aResult = new SMESH::long_array();
5233 if( meshPart->_is_nil() )
5234 return aResult._retn();
5236 gp_Pnt aP( theX, theY, theZ );
5237 gp_Vec aV( theDX, theDY, theDZ );
5238 gp_Ax2 anAxes (aP, aV);
5240 TopoDS_Shape aShape = BRepPrimAPI_MakeCylinder(anAxes, theR, Abs(theH)).Shape();
5242 std::vector<long> lst =_GetInside(meshPart, theElemType, aShape);
5244 if( lst.size() > 0 ) {
5245 aResult->length( lst.size() );
5246 for ( size_t i = 0; i < lst.size(); i++ ) {
5247 aResult[i] = lst[i];
5250 return aResult._retn();
5253 SMESH::long_array* SMESH_Gen_i::GetInside( SMESH::SMESH_IDSource_ptr meshPart,
5254 SMESH::ElementType theElemType,
5255 GEOM::GEOM_Object_ptr theGeom,
5256 CORBA::Double theTolerance ) {
5257 SMESH::long_array_var aResult = new SMESH::long_array();
5258 if( meshPart->_is_nil() || theGeom->_is_nil() )
5259 return aResult._retn();
5261 TopoDS_Shape aShape = GeomObjectToShape( theGeom );
5263 std::vector<long> lst =_GetInside(meshPart, theElemType, aShape, &theTolerance);
5265 if( lst.size() > 0 ) {
5266 aResult->length( lst.size() );
5267 for ( size_t i = 0; i < lst.size(); i++ ) {
5268 aResult[i] = lst[i];
5271 return aResult._retn();
5276 std::vector<long> SMESH_Gen_i::_GetInside( SMESH::SMESH_IDSource_ptr meshPart,
5277 SMESH::ElementType theElemType,
5278 TopoDS_Shape& aShape,
5279 double* theTolerance) {
5281 std::vector<long> res;
5282 SMESH::SMESH_Mesh_var mesh = meshPart->GetMesh();
5284 if ( mesh->_is_nil() )
5287 SMESH_Mesh_i* anImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
5291 const SMDS_Mesh* meshDS = anImpl->GetImpl().GetMeshDS();
5296 SMDSAbs_ElementType aType = SMDSAbs_ElementType(theElemType);
5297 SMESH::Controls::ElementsOnShape* anElementsOnShape = new SMESH::Controls::ElementsOnShape();
5298 anElementsOnShape->SetAllNodes( true );
5299 anElementsOnShape->SetMesh( meshDS );
5300 anElementsOnShape->SetShape( aShape, aType );
5303 anElementsOnShape->SetTolerance(*theTolerance);
5305 SMESH::SMESH_Mesh_var msource = SMESH::SMESH_Mesh::_narrow(meshPart);
5306 if ( !msource->_is_nil() ) { // Mesh case
5307 SMDS_ElemIteratorPtr elemIt = meshDS->elementsIterator( aType );
5309 while ( elemIt->more() ) {
5310 const SMDS_MeshElement* anElem = elemIt->next();
5311 long anId = anElem->GetID();
5312 if ( anElementsOnShape->IsSatisfy( anId ) )
5313 res.push_back( anId );
5317 SMESH::SMESH_Group_var gsource = SMESH::SMESH_Group::_narrow(meshPart);
5318 if ( !gsource->_is_nil() ) {
5319 if(theElemType == SMESH::NODE) {
5320 SMESH::long_array_var nodes = gsource->GetNodeIDs();
5321 for ( CORBA::ULong i = 0; i < nodes->length(); ++i ) {
5322 if ( const SMDS_MeshNode* node = meshDS->FindNode( nodes[i] )) {
5323 long anId = node->GetID();
5324 if ( anElementsOnShape->IsSatisfy( anId ) )
5325 res.push_back( anId );
5328 } else if (gsource->GetType() == theElemType || theElemType == SMESH::ALL ) {
5329 SMESH::long_array_var elems = gsource->GetListOfID();
5330 for ( CORBA::ULong i = 0; i < elems->length(); ++i ) {
5331 if ( const SMDS_MeshElement* elem = meshDS->FindElement( elems[i] )) {
5332 long anId = elem->GetID();
5333 if ( anElementsOnShape->IsSatisfy( anId ) )
5334 res.push_back( anId );
5339 SMESH::SMESH_subMesh_var smsource = SMESH::SMESH_subMesh::_narrow(meshPart);
5340 if ( !smsource->_is_nil() ) {
5341 SMESH::long_array_var elems = smsource->GetElementsByType( theElemType );
5342 for ( CORBA::ULong i = 0; i < elems->length(); ++i ) {
5343 const SMDS_MeshElement* elem = ( theElemType == SMESH::NODE ) ? meshDS->FindNode( elems[i] ) : meshDS->FindElement( elems[i] );
5345 long anId = elem->GetID();
5346 if ( anElementsOnShape->IsSatisfy( anId ) )
5347 res.push_back( anId );
5355 //=============================================================================
5357 * SMESHEngine_factory
5359 * C factory, accessible with dlsym, after dlopen
5361 //=============================================================================
5365 PortableServer::ObjectId* SMESHEngine_factory( CORBA::ORB_ptr orb,
5366 PortableServer::POA_ptr poa,
5367 PortableServer::ObjectId* contId,
5368 const char* instanceName,
5369 const char* interfaceName )
5371 if(MYDEBUG) MESSAGE( "PortableServer::ObjectId* SMESHEngine_factory()" );
5372 if(MYDEBUG) SCRUTE(interfaceName);
5373 SMESH_Gen_i* aSMESHGen = new SMESH_Gen_i(orb, poa, contId, instanceName, interfaceName);
5374 return aSMESHGen->getId() ;