1 // Copyright (C) 2007-2022 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>
52 // Have to be included before std headers
54 #include <structmember.h>
61 #include <libgen.h> // for basename function
65 #define LibHandle HMODULE
66 #define LoadLib( name ) LoadLibrary( name )
67 #define GetProc GetProcAddress
68 #define UnLoadLib( handle ) FreeLibrary( handle );
70 #define LibHandle void*
72 #define LoadLib( name ) dlopen( name, RTLD_LAZY | RTLD_LOCAL )
73 #else // DYNLOAD_LOCAL
74 #define LoadLib( name ) dlopen( name, RTLD_LAZY | RTLD_GLOBAL )
75 #endif // DYNLOAD_LOCAL
77 #define UnLoadLib( handle ) dlclose( handle );
80 #include "SMESH_Gen_i.hxx"
81 #include "SMESH_version.h"
83 #include "DriverMED_W_SMESHDS_Mesh.h"
84 #include "DriverMED_R_SMESHDS_Mesh.h"
86 #include "DriverCGNS_Read.hxx"
88 #include "MED_Factory.hxx"
89 #include "SMDS_EdgePosition.hxx"
90 #include "SMDS_FacePosition.hxx"
91 #include "SMDS_SetIterator.hxx"
92 #include "SMDS_SpacePosition.hxx"
93 #include "SMDS_VertexPosition.hxx"
94 #include "SMESHDS_Document.hxx"
95 #include "SMESHDS_Group.hxx"
96 #include "SMESHDS_GroupOnGeom.hxx"
97 #include "SMESH_Algo_i.hxx"
98 #include "SMESH_File.hxx"
99 #include "SMESH_Group.hxx"
100 #include "SMESH_Group_i.hxx"
101 #include "SMESH_Hypothesis.hxx"
102 #include "SMESH_Hypothesis_i.hxx"
103 #include "SMESH_Mesh.hxx"
104 #include "SMESH_MeshEditor.hxx"
105 #include "SMESH_Mesh_i.hxx"
106 #include "SMESH_PreMeshInfo.hxx"
107 #include "SMESH_PythonDump.hxx"
108 #include "SMESH_ControlsDef.hxx"
109 #include <SMESH_BoostTxtArchive.hxx>
111 // to pass CORBA exception through SMESH_TRY
112 #define SMY_OWN_CATCH catch( SALOME::SALOME_Exception& se ) { throw se; }
113 #include "SMESH_TryCatch.hxx" // to include after OCC headers!
115 #include CORBA_SERVER_HEADER(SMESH_Group)
116 #include CORBA_SERVER_HEADER(SMESH_Filter)
117 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
120 #include <GEOMImpl_Types.hxx>
121 #include <GEOM_Client.hxx>
123 #include <Basics_Utils.hxx>
124 #include <Basics_DirUtils.hxx>
126 #include <OpUtil.hxx>
127 #include <SALOMEDS_Tool.hxx>
128 #include <SALOME_Container_i.hxx>
129 #include <SALOME_LifeCycleCORBA.hxx>
130 #include <SALOME_NamingService.hxx>
131 #include <Utils_CorbaException.hxx>
132 #include <Utils_ExceptHandlers.hxx>
133 #include <Utils_SINGLETON.hxx>
134 #include <utilities.h>
136 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
137 #include CORBA_CLIENT_HEADER(SALOME_Session)
139 // helpers about SALOME::GenericObj
140 #include <SALOMEDS_wrap.hxx>
141 #include <SALOMEDS_Attributes_wrap.hxx>
142 #include <GEOM_wrap.hxx>
151 #include <boost/archive/text_oarchive.hpp>
152 #include <boost/serialization/list.hpp>
153 #include <boost/serialization/string.hpp>
156 using SMESH::TPythonDump;
159 #define NUM_TMP_FILES 2
162 static int MYDEBUG = 0;
164 static int MYDEBUG = 0;
167 // Static variables definition
168 GEOM::GEOM_Gen_var SMESH_Gen_i::myGeomGen;
169 CORBA::ORB_var SMESH_Gen_i::myOrb;
170 PortableServer::POA_var SMESH_Gen_i::myPoa;
171 SALOME_NamingService_Abstract* SMESH_Gen_i::myNS = nullptr;
172 SALOME_LifeCycleCORBA* SMESH_Gen_i::myLCC = nullptr;
173 SMESH_Gen_i* SMESH_Gen_i::mySMESHGen = nullptr;
176 const int nbElemPerDiagonal = 10;
178 //=============================================================================
180 * GetServant [ static ]
182 * Get servant of the CORBA object
184 //=============================================================================
186 PortableServer::ServantBase_var SMESH_Gen_i::GetServant( CORBA::Object_ptr theObject )
188 if( CORBA::is_nil( theObject ) || CORBA::is_nil( GetPOA() ) )
191 PortableServer::Servant aServant = GetPOA()->reference_to_servant( theObject );
194 catch (PortableServer::POA::ObjectNotActive &ex)
196 MESSAGE("GetServant: ObjectNotActive");
199 catch (PortableServer::POA::WrongAdapter &ex)
201 MESSAGE("GetServant: WrongAdapter: OK when several servants used to build several mesh in parallel...");
204 catch (PortableServer::POA::WrongPolicy &ex)
206 MESSAGE("GetServant: WrongPolicy");
211 MESSAGE( "GetServant - Unknown exception was caught!!!" );
216 //=============================================================================
218 * SObjectToObject [ static ]
220 * Get CORBA object corresponding to the SALOMEDS::SObject
222 //=============================================================================
224 CORBA::Object_var SMESH_Gen_i::SObjectToObject( SALOMEDS::SObject_ptr theSObject )
226 SALOMEDS::GenericAttribute_wrap anAttr;
227 CORBA::Object_var anObj;
228 if ( !theSObject->_is_nil() ) {
230 if( theSObject->FindAttribute( anAttr.inout(), "AttributeIOR" ) ) {
231 SALOMEDS::AttributeIOR_wrap anIOR = anAttr;
232 CORBA::String_var aValue = anIOR->Value();
233 if( strcmp( aValue, "" ) != 0 )
234 anObj = GetORB()->string_to_object( aValue );
238 INFOS( "SObjectToObject - Unknown exception was caught!!!" );
244 // Set Naming Service object
245 void SMESH_Gen_i::SetNS(SALOME_NamingService_Abstract *ns)
252 //=============================================================================
256 * Get SALOME_NamingService object
258 //=============================================================================
260 SALOME_NamingService_Abstract* SMESH_Gen_i::GetNS()
263 myNS = SINGLETON_<SALOME_NamingService>::Instance();
264 ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting());
265 myNS->init_orb( GetORB() );
270 //=============================================================================
274 * Get SALOME_LifeCycleCORBA object
276 //=============================================================================
278 SALOME_LifeCycleCORBA* SMESH_Gen_i::GetLCC()
280 if ( myLCC == NULL ) {
281 myLCC = new SALOME_LifeCycleCORBA( GetNS() );
286 //=============================================================================
288 * GetGeomEngine [ static ]
290 * Get GEOM::GEOM_Gen reference
292 //=============================================================================
294 GEOM::GEOM_Gen_var SMESH_Gen_i::GetGeomEngine( GEOM::GEOM_Object_ptr go )
296 GEOM::GEOM_Gen_ptr gen = GEOM::GEOM_Gen::_nil();
297 if ( !CORBA::is_nil( go ))
302 //=============================================================================
304 * SMESH_Gen_i::SMESH_Gen_i
306 * Default constructor: not for use
308 //=============================================================================
310 SMESH_Gen_i::SMESH_Gen_i()
314 //=============================================================================
316 * SMESH_Gen_i::SMESH_Gen_i
318 * Standard constructor, used with Container
320 //=============================================================================
322 SMESH_Gen_i::SMESH_Gen_i( CORBA::ORB_ptr orb,
323 PortableServer::POA_ptr poa,
324 PortableServer::ObjectId* contId,
325 const char* instanceName,
326 const char* interfaceName,
328 : Engines_Component_i( orb, poa, contId, instanceName, interfaceName, false, checkNS )
331 myOrb = CORBA::ORB::_duplicate(orb);
332 myPoa = PortableServer::POA::_duplicate(poa);
335 _id = myPoa->activate_object( _thisObj );
337 myStudyContext = new StudyContext;
339 myIsEmbeddedMode = false;
340 myIsEnablePublish = true;
341 myShapeReader = NULL; // shape reader
343 myIsHistoricalPythonDump = true;
344 myToForgetMeshDataOnHypModif = false;
346 // set it in standalone mode only
347 //OSD::SetSignal( true );
349 // 0020605: EDF 1190 SMESH: Display performance. 80 seconds for 52000 cells.
350 // find out mode (embedded or standalone) here else
351 // meshes created before calling SMESH_Client::GetSMESHGen(), which calls
352 // SMESH_Gen_i::SetEmbeddedMode(), have wrong IsEmbeddedMode flag
355 if ( SALOME_NamingService_Abstract* ns = GetNS() )
357 CORBA::Object_var obj = ns->Resolve( "/Kernel/Session" );
358 SALOME::Session_var session = SALOME::Session::_narrow( obj ) ;
359 if ( !session->_is_nil() )
361 CORBA::String_var str_host = session->getHostname();
362 CORBA::Long s_pid = session->getPID();
363 string my_host = Kernel_Utils::GetHostname();
365 long my_pid = (long)_getpid();
367 long my_pid = (long) getpid();
369 SetEmbeddedMode( s_pid == my_pid && my_host == str_host.in() );
375 //=============================================================================
377 * SMESH_Gen_i::~SMESH_Gen_i
381 //=============================================================================
383 SMESH_Gen_i::~SMESH_Gen_i()
385 // delete hypothesis creators
386 map<string, GenericHypothesisCreator_i*>::iterator itHyp, itHyp2;
387 for (itHyp = myHypCreatorMap.begin(); itHyp != myHypCreatorMap.end(); itHyp++)
389 // same creator can be mapped under different names
390 GenericHypothesisCreator_i* creator = (*itHyp).second;
394 for (itHyp2 = itHyp; itHyp2 != myHypCreatorMap.end(); itHyp2++)
395 if ( creator == (*itHyp2).second )
396 (*itHyp2).second = 0;
398 myHypCreatorMap.clear();
400 // Clear study contexts data
401 delete myStudyContext;
403 // delete shape reader
405 delete myShapeReader;
408 //=============================================================================
410 * SMESH_Gen_i::getHypothesisCreator
412 * Get hypothesis creator
414 //=============================================================================
416 GenericHypothesisCreator_i* SMESH_Gen_i::getHypothesisCreator(const char* theHypName,
417 const char* theLibName,
418 std::string& thePlatformLibName)
420 std::string aPlatformLibName;
421 /* It's Need to translate lib name for WIN32 or X platform */
422 if ( theLibName && theLibName[0] != '\0' )
424 int libNameLen = strlen(theLibName);
425 //check for old format "libXXXXXXX.so"
426 if (libNameLen > 7 &&
427 !strncmp( theLibName, "lib", 3 ) &&
428 !strcmp( theLibName+libNameLen-3, ".so" ))
432 aPlatformLibName = std::string( theLibName+3, libNameLen-6 ) + ".dll";
433 #elif defined(__APPLE__)
434 aPlatformLibName = std::string( theLibName, libNameLen-3 ) + ".dylib";
436 aPlatformLibName = theLibName;
441 //try to use new format
443 aPlatformLibName = theLibName;
444 aPlatformLibName += ".dll";
445 #elif defined(__APPLE__)
446 aPlatformLibName = std::string( "lib" ) + std::string( theLibName ) + ".dylib";
448 aPlatformLibName = std::string( "lib" ) + std::string( theLibName ) + ".so";
452 thePlatformLibName = aPlatformLibName;
454 Unexpect aCatch(SALOME_SalomeException);
455 if(MYDEBUG) MESSAGE( "Create Hypothesis <" << theHypName << "> from " << aPlatformLibName);
457 typedef GenericHypothesisCreator_i* (*GetHypothesisCreator)(const char* );
458 GenericHypothesisCreator_i* aCreator;
461 // check, if creator for this hypothesis type already exists
462 if (myHypCreatorMap.find(string(theHypName)) == myHypCreatorMap.end())
464 // load plugin library
465 if(MYDEBUG) MESSAGE("Loading server meshers plugin library ...");
468 const wchar_t* path = Kernel_Utils::decode_s(aPlatformLibName);
469 SMESHUtils::ArrayDeleter<const wchar_t> deleter( path );
471 const char* path = aPlatformLibName.c_str();
474 const char* path = aPlatformLibName.c_str();
476 LibHandle libHandle = LoadLib( path );
480 // report any error, if occurred
482 const char* anError = dlerror();
483 throw(SALOME_Exception( anError ));
485 throw(SALOME_Exception ( SMESH_Comment("Can't load meshers plugin library " )
486 << aPlatformLibName));
490 // get method, returning hypothesis creator
491 if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
492 GetHypothesisCreator procHandle =
493 (GetHypothesisCreator)GetProc( libHandle, "GetHypothesisCreator" );
496 throw(SALOME_Exception(SMESH_Comment("bad hypothesis plugin library")
497 << aPlatformLibName ));
498 UnLoadLib(libHandle);
501 // get hypothesis creator
502 if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << theHypName);
503 aCreator = procHandle(theHypName);
506 throw(SALOME_Exception( SMESH_Comment( theHypName ) << " is missing from "
507 << aPlatformLibName));
509 // map hypothesis creator to a hypothesis name
510 myHypCreatorMap[string(theHypName)] = aCreator;
515 return myHypCreatorMap[string(theHypName)];
518 catch (SALOME_Exception& S_ex)
520 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
525 //=============================================================================
527 * SMESH_Gen_i::createHypothesis
529 * Create hypothesis of given type
531 //=============================================================================
533 SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::createHypothesis(const char* theHypName,
534 const char* theLibName)
536 SMESH_Hypothesis_i* myHypothesis_i = 0;
537 SMESH::SMESH_Hypothesis_var hypothesis_i;
538 std::string aPlatformLibName;
539 GenericHypothesisCreator_i* aCreator =
540 getHypothesisCreator(theHypName, theLibName, aPlatformLibName);
542 // create a new hypothesis object, store its ref. in studyContext
543 myHypothesis_i = aCreator->Create(myPoa, &myGen);
546 myHypothesis_i->SetLibName( aPlatformLibName.c_str() ); // for persistency assurance
547 CORBA::String_var hypName = myHypothesis_i->GetName();
548 myHypCreatorMap[ hypName.in() ] = aCreator;
550 // activate the CORBA servant of hypothesis
551 hypothesis_i = myHypothesis_i->_this();
552 int nextId = RegisterObject( hypothesis_i );
553 if(MYDEBUG) { MESSAGE( "Add hypo to map with id = "<< nextId ); }
554 else { (void)nextId; } // avoid "unused variable" warning in release mode
556 return hypothesis_i._retn();
559 //=============================================================================
561 * SMESH_Gen_i::createMesh
563 * Create empty mesh on shape
565 //=============================================================================
567 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::createMesh()
569 Unexpect aCatch(SALOME_SalomeException);
570 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::createMesh" );
572 // Get or create the GEOM_Client instance
574 // create a new mesh object servant, store it in a map in study context
575 SMESH_Mesh_i* meshServant = new SMESH_Mesh_i( GetPOA(), this );
576 // create a new mesh object
577 if(MYDEBUG) MESSAGE("myIsEmbeddedMode " << myIsEmbeddedMode);
578 meshServant->SetImpl( myGen.CreateMesh( myIsEmbeddedMode ));
580 // activate the CORBA servant of Mesh
581 SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( meshServant->_this() );
582 int nextId = RegisterObject( mesh );
583 if(MYDEBUG) { MESSAGE( "Add mesh to map with id = "<< nextId); }
584 else { (void)nextId; } // avoid "unused variable" warning in release mode
587 catch (SALOME_Exception& S_ex) {
588 THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
590 return SMESH::SMESH_Mesh::_nil();
593 //=============================================================================
595 * SMESH_Gen_i::GetShapeReader
599 //=============================================================================
601 GEOM_Client* SMESH_Gen_i::GetShapeReader()
603 // create shape reader if necessary
604 if ( !myShapeReader )
605 myShapeReader = new GEOM_Client(GetContainerRef());
606 ASSERT( myShapeReader );
607 return myShapeReader;
610 //=============================================================================
612 * SMESH_Gen_i::SetGeomEngine
614 * Set GEOM::GEOM_Gen reference
616 //=============================================================================
618 void SMESH_Gen_i::SetGeomEngine( GEOM::GEOM_Gen_ptr geomcompo )
620 myGeomGen = GEOM::GEOM_Gen::_duplicate( geomcompo );
623 //=============================================================================
625 * SMESH_Gen_i::SetEmbeddedMode
629 //=============================================================================
631 void SMESH_Gen_i::SetEmbeddedMode( CORBA::Boolean theMode )
633 myIsEmbeddedMode = theMode;
635 if ( !myIsEmbeddedMode ) {
636 //PAL10867: disable signals catching with "noexcepthandler" option
637 char* envNoCatchSignals = getenv("NOT_INTERCEPT_SIGNALS");
638 if (!envNoCatchSignals || !atoi(envNoCatchSignals))
643 char* envDisableFPE = getenv("DISABLE_FPE");
644 if (envDisableFPE && atoi(envDisableFPE))
649 OSD::SetSignal( raiseFPE );
651 // else OSD::SetSignal() is called in GUI
655 //=============================================================================
657 * SMESH_Gen_i::IsEmbeddedMode
661 //=============================================================================
663 CORBA::Boolean SMESH_Gen_i::IsEmbeddedMode()
665 return myIsEmbeddedMode;
668 //=============================================================================
670 * SMESH_Gen_i::SetEnablePublish
672 * Set enable publishing in the study
674 //=============================================================================
676 void SMESH_Gen_i::SetEnablePublish( CORBA::Boolean theIsEnablePublish )
678 myIsEnablePublish = theIsEnablePublish;
681 //=============================================================================
683 * SMESH_Gen_i::IsEnablePublish
685 * Check enable publishing
687 //=============================================================================
689 CORBA::Boolean SMESH_Gen_i::IsEnablePublish()
691 return myIsEnablePublish;
694 //=============================================================================
696 * SMESH_Gen_i::UpdateStudy
698 * Update study (needed at switching GEOM->SMESH)
700 //=============================================================================
702 void SMESH_Gen_i::UpdateStudy()
704 if ( !myStudyContext )
705 myStudyContext = new StudyContext;
707 SALOMEDS::Study_var aStudy = getStudyServant();
708 if ( !CORBA::is_nil( aStudy ) )
710 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
712 SALOMEDS::SComponent_wrap GEOM_var = aStudy->FindComponent( "GEOM" );
713 if( !GEOM_var->_is_nil() )
714 aStudyBuilder->LoadWith( GEOM_var, GetGeomEngine( /*isShaper=*/false ) );
716 GEOM_var = aStudy->FindComponent( "SHAPERSTUDY" );
717 if( !GEOM_var->_is_nil() )
718 aStudyBuilder->LoadWith( GEOM_var, GetGeomEngine( /*isShaper=*/true ) );
720 // NPAL16168, issue 0020210
721 // Let meshes update their data depending on GEOM groups that could change
722 CORBA::String_var compDataType = ComponentDataType();
723 SALOMEDS::SComponent_wrap me = aStudy->FindComponent( compDataType.in() );
724 if ( !me->_is_nil() ) {
725 SALOMEDS::ChildIterator_wrap anIter = aStudy->NewChildIterator( me );
726 for ( ; anIter->More(); anIter->Next() ) {
727 SALOMEDS::SObject_wrap so = anIter->Value();
728 CORBA::Object_var ior = SObjectToObject( so );
729 if ( SMESH_Mesh_i* mesh = SMESH::DownCast<SMESH_Mesh_i*>( ior ))
730 mesh->CheckGeomModif();
736 //================================================================================
738 * \brief Return true if mesh has ICON_SMESH_TREE_GEOM_MODIF icon
740 //================================================================================
742 bool SMESH_Gen_i::isGeomModifIcon( SMESH::SMESH_Mesh_ptr mesh )
744 SALOMEDS::SObject_wrap so = ObjectToSObject( mesh );
745 SALOMEDS::GenericAttribute_wrap attr;
746 if ( ! so->_is_nil() && so->FindAttribute( attr.inout(), "AttributePixMap" ))
748 SALOMEDS::AttributePixMap_wrap pm = attr;
749 CORBA::String_var ico = pm->GetPixMap();
750 return ( strcmp( ico.in(), "ICON_SMESH_TREE_GEOM_MODIF" ) == 0 );
755 //=================================================================================
756 // function : hasObjectInfo()
757 // purpose : shows if module provides information for its objects
758 //=================================================================================
760 bool SMESH_Gen_i::hasObjectInfo()
765 //=================================================================================
766 // function : getObjectInfo()
767 // purpose : returns an information for a given object by its entry
768 //=================================================================================
770 char* SMESH_Gen_i::getObjectInfo( const char* entry )
772 // for a mesh with icon == ICON_SMESH_TREE_GEOM_MODIF show a warning;
773 // for the rest, "module 'SMESH', ID=0:1:2:*"
777 SALOMEDS::SObject_wrap so = getStudyServant()->FindObjectID( entry );
778 CORBA::Object_var obj = SObjectToObject( so );
779 SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( obj );
780 if ( isGeomModifIcon( mesh ))
782 txt << "The geometry was changed and the mesh needs to be recomputed";
787 CORBA::String_var compType = ComponentDataType();
788 txt << "module '" << compType << "', ID=" << entry;
790 return CORBA::string_dup( txt );
793 //=============================================================================
795 * SMESH_Gen_i::GetStudyContext
799 //=============================================================================
801 StudyContext* SMESH_Gen_i::GetStudyContext()
803 return myStudyContext;
806 //=============================================================================
808 * SMESH_Gen_i::CreateHypothesis
810 * Create hypothesis/algorithm of given type and publish it in the study
812 //=============================================================================
814 SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::CreateHypothesis( const char* theHypName,
815 const char* theLibName )
817 Unexpect aCatch(SALOME_SalomeException);
818 // Create hypothesis/algorithm
819 SMESH::SMESH_Hypothesis_var hyp = this->createHypothesis( theHypName, theLibName );
821 // Publish hypothesis/algorithm in the study
822 if ( CanPublishInStudy( hyp ) ) {
823 SALOMEDS::SObject_wrap aSO = PublishHypothesis( hyp );
824 if ( !aSO->_is_nil() ) {
825 // Update Python script
826 TPythonDump(this) << aSO << " = " << this << ".CreateHypothesis('"
827 << theHypName << "', '" << theLibName << "')";
834 //================================================================================
836 * \brief Return a hypothesis initialized by given average length.
837 * \param theHypType - hypothesis type name
838 * \param theLibName - plugin library name
839 * \param theAverageLength - average length
840 * \param theQuadDominated - is quad-dominated flag
841 * \retval SMESH::SMESH_Hypothesis_ptr - the new hypothesis
843 //================================================================================
845 SMESH::SMESH_Hypothesis_ptr
846 SMESH_Gen_i::CreateHypothesisByAverageLength( const char* theHypType,
847 const char* theLibName,
848 CORBA::Double theAverageLength,
849 CORBA::Boolean theQuadDominated)
851 SMESH::HypInitParams initParams = { ::SMESH_Hypothesis::BY_AVERAGE_LENGTH,
852 theAverageLength, theQuadDominated };
854 SMESH::SMESH_Hypothesis_var hyp =
855 GetHypothesisParameterValues( theHypType, theLibName,
856 SMESH::SMESH_Mesh::_nil(),
857 GEOM::GEOM_Object::_nil(),
859 SALOMEDS::SObject_wrap so = PublishHypothesis( hyp );
861 TPythonDump(this) << hyp << " = " << this << ".CreateHypothesisByAverageLength( '"
862 << theHypType << "', '"
863 << theLibName << "', "
864 << theAverageLength << ", "
865 << theQuadDominated << " )";
870 //================================================================================
872 * \brief Return a hypothesis holding parameter values corresponding either to the mesh
873 * existing on the given geometry or to size of the geometry.
874 * \param theHypType - hypothesis type name
875 * \param theLibName - plugin library name
876 * \param theMesh - The mesh of interest
877 * \param theGeom - The shape to get parameter values from
878 * \retval SMESH::SMESH_Hypothesis_ptr - The returned hypothesis may be the one existing
879 * in a study and used to compute the mesh, or a temporary one created just to pass
882 //================================================================================
884 SMESH::SMESH_Hypothesis_ptr
885 SMESH_Gen_i::GetHypothesisParameterValues( const char* theHypType,
886 const char* theLibName,
887 SMESH::SMESH_Mesh_ptr theMesh,
888 GEOM::GEOM_Object_ptr theGeom,
889 const SMESH::HypInitParams& theParams)
891 Unexpect aCatch(SALOME_SalomeException);
893 const bool byMesh = ( theParams.way == ::SMESH_Hypothesis::BY_MESH );
894 if ( byMesh && CORBA::is_nil( theMesh ) )
895 return SMESH::SMESH_Hypothesis::_nil();
896 if ( byMesh && CORBA::is_nil( theGeom ) )
897 return SMESH::SMESH_Hypothesis::_nil();
899 // -----------------------------------------------
900 // find hypothesis used to mesh theGeom
901 // -----------------------------------------------
903 // get mesh and shape
904 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
905 TopoDS_Shape shape = GeomObjectToShape( theGeom );
906 if ( byMesh && ( !meshServant || meshServant->NbNodes()==0 || shape.IsNull() ))
907 return SMESH::SMESH_Hypothesis::_nil();
908 ::SMESH_Mesh* mesh = meshServant ? &meshServant->GetImpl() : (::SMESH_Mesh*)0;
910 // create a temporary hypothesis to know its dimension
911 SMESH::SMESH_Hypothesis_var tmpHyp = this->createHypothesis( theHypType, theLibName );
912 SMESH_Hypothesis_i* hypServant = SMESH::DownCast<SMESH_Hypothesis_i*>( tmpHyp );
914 return SMESH::SMESH_Hypothesis::_nil();
915 ::SMESH_Hypothesis* hyp = hypServant->GetImpl();
918 // look for a hypothesis of theHypType used to mesh the shape
919 if ( myGen.GetShapeDim( shape ) == hyp->GetDim() )
922 SMESH::ListOfHypothesis_var aHypList = theMesh->GetHypothesisList( theGeom );
923 int nbLocalHyps = aHypList->length();
924 for ( int i = 0; i < nbLocalHyps; i++ ) {
925 CORBA::String_var hypName = aHypList[i]->GetName();
926 if ( strcmp( theHypType, hypName.in() ) == 0 ) // FOUND local!
927 return SMESH::SMESH_Hypothesis::_duplicate( aHypList[i] );
929 // check super shapes
930 TopTools_ListIteratorOfListOfShape itShape( mesh->GetAncestors( shape ));
931 while ( nbLocalHyps == 0 && itShape.More() ) {
932 GEOM::GEOM_Object_ptr geomObj = ShapeToGeomObject( itShape.Value() );
933 if ( ! CORBA::is_nil( geomObj )) {
934 SMESH::ListOfHypothesis_var aHypList = theMesh->GetHypothesisList( geomObj );
935 nbLocalHyps = aHypList->length();
936 for ( int i = 0; i < nbLocalHyps; i++ )
937 if ( strcmp( theHypType, aHypList[i]->GetName() ) == 0 ) // FOUND global!
938 return SMESH::SMESH_Hypothesis::_duplicate( aHypList[i] );
944 // let the temporary hypothesis find out somehow parameter values by mesh
945 if ( hyp->SetParametersByMesh( mesh, shape ))
946 return SMESH::SMESH_Hypothesis::_duplicate( tmpHyp );
950 ::SMESH_Hypothesis::TDefaults dflts;
951 dflts._way = ( ::SMESH_Hypothesis::InitWay) theParams.way;
952 dflts._nbSegments = myGen.GetDefaultNbSegments();
953 dflts._elemLength = theParams.averageLength;
954 dflts._quadDominated = theParams.quadDominated;
955 if ( theParams.way == ::SMESH_Hypothesis::BY_GEOM )
958 dflts._diagonal = mesh->GetShapeDiagonalSize();
960 dflts._diagonal = ::SMESH_Mesh::GetShapeDiagonalSize( shape );
961 dflts._elemLength = dflts._diagonal / myGen.GetBoundaryBoxSegmentation();
962 dflts._shape = &shape;
965 // let the hypothesis initialize it's values
966 if ( hyp->SetParametersByDefaults( dflts, mesh ))
967 return SMESH::SMESH_Hypothesis::_duplicate( tmpHyp );
970 return SMESH::SMESH_Hypothesis::_nil();
973 //=============================================================================
975 * Returns \c True if a hypothesis is assigned to a sole sub-mesh in a current Study
976 * \param [in] theHyp - the hypothesis of interest
977 * \param [out] theMesh - the sole mesh using \a theHyp
978 * \param [out] theShape - the sole geometry \a theHyp is assigned to
979 * \return boolean - \c True if \a theMesh and \a theShape are sole using \a theHyp
981 * If two meshes on same shape have theHyp assigned to the same sub-shape, they are
982 * considered as SAME sub-mesh => result is \c true.
983 * This method ids used to initialize SMESHGUI_GenericHypothesisCreator with
984 * a shape to which an hyp being edited is assigned.
986 //=============================================================================
988 CORBA::Boolean SMESH_Gen_i::GetSoleSubMeshUsingHyp( SMESH::SMESH_Hypothesis_ptr theHyp,
989 SMESH::SMESH_Mesh_out theMesh,
990 GEOM::GEOM_Object_out theShape)
992 if ( CORBA::is_nil( theHyp ))
995 // get Mesh component SO
996 CORBA::String_var compDataType = ComponentDataType();
997 SALOMEDS::SComponent_wrap comp = getStudyServant()->FindComponent( compDataType.in() );
998 if ( CORBA::is_nil( comp ))
1001 // look for child SO of meshes
1002 SMESH::SMESH_Mesh_var foundMesh;
1003 TopoDS_Shape foundShape;
1005 SALOMEDS::ChildIterator_wrap meshIter = getStudyServant()->NewChildIterator( comp );
1006 for ( ; meshIter->More() && isSole; meshIter->Next() )
1008 SALOMEDS::SObject_wrap curSO = meshIter->Value();
1009 CORBA::Object_var obj = SObjectToObject( curSO );
1010 SMESH_Mesh_i* mesh_i = SMESH::DownCast< SMESH_Mesh_i* >( obj );
1014 // look for a sole shape where theHyp is assigned
1015 bool isHypFound = false;
1016 const ShapeToHypothesis & s2hyps = mesh_i->GetImpl().GetMeshDS()->GetHypotheses();
1017 ShapeToHypothesis::Iterator s2hypsIt( s2hyps );
1018 for ( ; s2hypsIt.More() && isSole; s2hypsIt.Next() )
1020 const THypList& hyps = s2hypsIt.Value();
1021 THypList::const_iterator h = hyps.begin();
1022 for ( ; h != hyps.end(); ++h )
1023 if ( (*h)->GetID() == theHyp->GetId() )
1025 if ( h != hyps.end()) // theHyp found
1028 if ( ! foundShape.IsNull() &&
1029 ! foundShape.IsSame( s2hypsIt.Key() )) // not a sole sub-shape
1031 foundShape.Nullify();
1035 foundShape = s2hypsIt.Key();
1037 } // loop on assigned hyps
1039 if ( isHypFound && !foundShape.IsNull() ) // a mesh using theHyp is found
1041 if ( !foundMesh->_is_nil() ) // not a sole mesh
1043 if ( !foundMesh->HasShapeToMesh() ||
1044 !mesh_i ->HasShapeToMesh() )
1046 isSole = ( foundMesh->HasShapeToMesh() == mesh_i->HasShapeToMesh() );
1050 GEOM::GEOM_Object_var s1 = mesh_i ->GetShapeToMesh();
1051 GEOM::GEOM_Object_var s2 = foundMesh->GetShapeToMesh();
1052 isSole = s1->IsSame( s2 );
1055 foundMesh = SMESH::SMESH_Mesh::_narrow( obj );
1061 ! foundMesh->_is_nil() &&
1062 ! foundShape.IsNull() )
1064 theMesh = foundMesh._retn();
1065 theShape = ShapeToGeomObject( foundShape );
1066 return ( !theMesh->_is_nil() && !theShape->_is_nil() );
1071 //=============================================================================
1073 * Set number of segments per diagonal of boundary box of geometry by which
1074 * default segment length of appropriate 1D hypotheses is defined
1076 //=============================================================================
1078 void SMESH_Gen_i::SetBoundaryBoxSegmentation( CORBA::Long theNbSegments )
1080 if ( theNbSegments > 0 )
1081 myGen.SetBoundaryBoxSegmentation( int( theNbSegments ));
1083 THROW_SALOME_CORBA_EXCEPTION( "non-positive number of segments", SALOME::BAD_PARAM );
1086 //=============================================================================
1088 * \brief Set default number of segments per edge
1090 //=============================================================================
1092 void SMESH_Gen_i::SetDefaultNbSegments(CORBA::Long theNbSegments)
1094 if ( theNbSegments > 0 )
1095 myGen.SetDefaultNbSegments( int(theNbSegments) );
1097 THROW_SALOME_CORBA_EXCEPTION( "non-positive number of segments", SALOME::BAD_PARAM );
1100 //=============================================================================
1102 * Set an option value
1104 //=============================================================================
1106 void SMESH_Gen_i::SetOption(const char* name, const char* value)
1108 if ( name && value && strlen( value ) > 0 )
1111 if ( strcmp(name, "historical_python_dump") == 0 )
1113 myIsHistoricalPythonDump = ( value[0] == '1' || toupper(value[0]) == 'T' ); // 1 || true
1114 msgToGUI = "preferences/SMESH/historical_python_dump/";
1115 msgToGUI += myIsHistoricalPythonDump ? "true" : "false";
1117 else if ( strcmp(name, "forget_mesh_on_hyp_modif") == 0 )
1119 myToForgetMeshDataOnHypModif = ( value[0] == '1' || toupper(value[0]) == 'T' ); // 1 || true
1120 msgToGUI = "preferences/SMESH/forget_mesh_on_hyp_modif/";
1121 msgToGUI += myToForgetMeshDataOnHypModif ? "true" : "false";
1123 else if ( strcmp(name, "default_grp_color") == 0 )
1127 // color must be presented as a string of following form:
1128 if ( str.at(0) == '#' && str.length() == 7 ) { // hexadecimal color ("#ffaa00", for example)
1129 str = str.substr(1);
1130 for ( size_t i = 0; i < str.length()/2; i++ )
1131 if ( str.at(i*2) >= '0' && str.at(i*2) <= 'f' && str.at(i*2+1) >= '0' && str.at(i*2+1) <= 'f' )
1132 color.push_back( strtol( str.substr( i*2, 2 ).c_str(), NULL, 16 ) );
1134 else if ( value ) { // rgb color ("255,170,0", for example)
1135 string tempValue( value );
1136 char* colorValue = strtok( &tempValue[0], "," );
1137 while ( colorValue != NULL ) {
1138 int c_value = atoi( colorValue );
1139 if ( c_value >= 0 && c_value <= 255 )
1140 color.push_back( c_value );
1141 colorValue = strtok( NULL, "," );
1144 if ( color.size() == 3 ) { // color must have three valid component
1145 SMESHDS_GroupBase::SetDefaultColor( Quantity_Color( color[0]/255., color[1]/255., color[2]/255., Quantity_TOC_RGB ) );
1146 myDefaultGroupColor = value;
1147 msgToGUI = "preferences/SMESH/default_grp_color/";
1152 // update preferences in case if SetOption() is invoked from python console
1153 if ( !msgToGUI.empty() )
1155 CORBA::Object_var obj = SMESH_Gen_i::GetNS()->Resolve( "/Kernel/Session" );
1156 SALOME::Session_var session = SALOME::Session::_narrow( obj );
1157 if ( !CORBA::is_nil( session ) )
1158 session->emitMessageOneWay(msgToGUI.c_str());
1163 //=============================================================================
1165 * Return an option value
1167 //=============================================================================
1169 char* SMESH_Gen_i::GetOption(const char* name)
1173 if ( strcmp(name, "historical_python_dump") == 0 )
1175 return CORBA::string_dup( myIsHistoricalPythonDump ? "true" : "false" );
1177 if ( strcmp(name, "forget_mesh_on_hyp_modif") == 0 )
1179 return CORBA::string_dup( myToForgetMeshDataOnHypModif ? "true" : "false" );
1181 if ( strcmp(name, "default_grp_color") == 0 )
1183 return CORBA::string_dup( myDefaultGroupColor.c_str() );
1186 return CORBA::string_dup( "" );
1189 //=============================================================================
1191 * SMESH_Gen_i::CreateMesh
1193 * Create empty mesh on a shape and publish it in the study
1195 //=============================================================================
1197 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMesh( GEOM::GEOM_Object_ptr theShapeObject )
1199 Unexpect aCatch(SALOME_SalomeException);
1200 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMesh" );
1202 SMESH::SMESH_Mesh_var mesh = this->createMesh();
1204 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( mesh );
1205 ASSERT( meshServant );
1206 meshServant->SetShape( theShapeObject );
1208 // publish mesh in the study
1209 if ( CanPublishInStudy( mesh ) ) {
1210 SALOMEDS::StudyBuilder_var aStudyBuilder = getStudyServant()->NewBuilder();
1211 aStudyBuilder->NewCommand(); // There is a transaction
1212 SALOMEDS::SObject_wrap aSO = PublishMesh( mesh.in() );
1213 aStudyBuilder->CommitCommand();
1214 if ( !aSO->_is_nil() ) {
1215 // Update Python script
1216 TPythonDump(this) << aSO << " = " << this << ".CreateMesh(" << theShapeObject << ")";
1220 return mesh._retn();
1223 //=============================================================================
1225 * SMESH_Gen_i::CreateEmptyMesh
1229 //=============================================================================
1231 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateEmptyMesh()
1233 Unexpect aCatch(SALOME_SalomeException);
1234 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMesh" );
1236 SMESH::SMESH_Mesh_var mesh = this->createMesh();
1238 // publish mesh in the study
1239 if ( CanPublishInStudy( mesh ) ) {
1240 SALOMEDS::StudyBuilder_var aStudyBuilder = getStudyServant()->NewBuilder();
1241 aStudyBuilder->NewCommand(); // There is a transaction
1242 SALOMEDS::SObject_wrap aSO = PublishMesh( mesh.in() );
1243 aStudyBuilder->CommitCommand();
1244 if ( !aSO->_is_nil() ) {
1245 // Update Python script
1246 TPythonDump(this) << aSO << " = " << this << ".CreateEmptyMesh()";
1250 return mesh._retn();
1255 //================================================================================
1257 * \brief Throw an exception in case if the file can't be read
1259 //================================================================================
1261 void checkFileReadable( const char* theFileName )
1263 SMESH_File f ( theFileName );
1266 if ( !f.error().empty() )
1267 THROW_SALOME_CORBA_EXCEPTION( f.error().c_str(), SALOME::BAD_PARAM);
1269 THROW_SALOME_CORBA_EXCEPTION
1270 (( SMESH_Comment("Can't open for reading the file ") << theFileName ).c_str(),
1271 SALOME::BAD_PARAM );
1276 //=============================================================================
1278 * SMESH_Gen_i::CreateMeshFromUNV
1280 * Create mesh and import data from UNV file
1282 //=============================================================================
1284 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMeshesFromUNV( const char* theFileName )
1286 Unexpect aCatch(SALOME_SalomeException);
1288 checkFileReadable( theFileName );
1290 SMESH::SMESH_Mesh_var aMesh = createMesh();
1292 // publish mesh in the study
1293 if ( CanPublishInStudy( aMesh ) ) {
1294 SALOMEDS::StudyBuilder_var aStudyBuilder = getStudyServant()->NewBuilder();
1295 aStudyBuilder->NewCommand(); // There is a transaction
1296 SALOMEDS::SObject_wrap aSO = PublishMesh( aMesh.in(), aFileName.c_str() );
1297 aStudyBuilder->CommitCommand();
1298 if ( !aSO->_is_nil() ) {
1299 // Update Python script
1300 TPythonDump(this) << aSO << " = " << this << ".CreateMeshesFromUNV(r'" << theFileName << "')";
1304 SMESH_Mesh_i* aServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( aMesh ).in() );
1306 aServant->ImportUNVFile( theFileName );
1308 // Dump creation of groups
1309 SMESH::ListOfGroups_var groups = aServant->GetGroups();
1311 aServant->GetImpl().GetMeshDS()->Modified();
1312 return aMesh._retn();
1315 //=============================================================================
1317 * SMESH_Gen_i::CreateMeshFromMED
1319 * Create mesh and import data from MED file
1321 //=============================================================================
1323 SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromMED( const char* theFileName,
1324 SMESH::DriverMED_ReadStatus& theStatus )
1326 checkFileReadable( theFileName );
1329 char bname[ _MAX_FNAME ];
1330 _splitpath( theFileName, NULL, NULL, bname, NULL );
1331 string aFileName = bname;
1333 string aFileName = basename( const_cast<char *>( theFileName ));
1335 // Retrieve mesh names from the file
1336 DriverMED_R_SMESHDS_Mesh myReader;
1337 myReader.SetFile( theFileName );
1338 myReader.SetMeshId( -1 );
1339 Driver_Mesh::Status aStatus;
1340 list<string> aNames = myReader.GetMeshNames(aStatus);
1341 SMESH::mesh_array_var aResult = new SMESH::mesh_array();
1342 theStatus = (SMESH::DriverMED_ReadStatus)aStatus;
1344 { // open a new scope to make aPythonDump die before PythonDump in SMESH_Mesh::GetGroups()
1347 TPythonDump aPythonDump(this);
1348 aPythonDump << "([";
1350 if (theStatus == SMESH::DRS_OK)
1352 SALOMEDS::StudyBuilder_var aStudyBuilder;
1353 aStudyBuilder = getStudyServant()->NewBuilder();
1354 aStudyBuilder->NewCommand(); // There is a transaction
1356 aResult->length( aNames.size() );
1359 // Iterate through all meshes and create mesh objects
1360 for ( const std::string & meshName : aNames )
1363 if (i > 0) aPythonDump << ", ";
1366 SMESH::SMESH_Mesh_var mesh = createMesh();
1368 // publish mesh in the study
1369 SALOMEDS::SObject_wrap aSO;
1370 if ( CanPublishInStudy( mesh ) )
1371 aSO = PublishMesh( mesh.in(), meshName.c_str() );
1374 if ( !aSO->_is_nil() ) {
1377 aPythonDump << "mesh_" << i;
1380 // Read mesh data (groups are published automatically by ImportMEDFile())
1381 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
1382 ASSERT( meshServant );
1383 SMESH::DriverMED_ReadStatus status1 =
1384 meshServant->ImportMEDFile( theFileName, meshName.c_str() );
1385 if (status1 > theStatus)
1386 theStatus = status1;
1388 aResult[i++] = SMESH::SMESH_Mesh::_duplicate( mesh );
1389 meshServant->GetImpl().GetMeshDS()->Modified();
1391 if ( !aStudyBuilder->_is_nil() )
1392 aStudyBuilder->CommitCommand();
1395 // Update Python script
1396 aPythonDump << "], status) = " << this << ".CreateMeshesFromMED( r'" << theFileName << "' )";
1398 // Dump creation of groups
1399 for ( CORBA::ULong i = 0; i < aResult->length(); ++i )
1400 SMESH::ListOfGroups_var groups = aResult[ i ]->GetGroups();
1402 return aResult._retn();
1405 //=============================================================================
1407 * SMESH_Gen_i::CreateMeshFromSTL
1409 * Create mesh and import data from STL file
1411 //=============================================================================
1413 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMeshesFromSTL( const char* theFileName )
1415 Unexpect aCatch(SALOME_SalomeException);
1416 checkFileReadable( theFileName );
1418 SMESH::SMESH_Mesh_var aMesh = createMesh();
1421 char bname[ _MAX_FNAME ];
1422 _splitpath( theFileName, NULL, NULL, bname, NULL );
1423 string aFileName = bname;
1425 string aFileName = basename( const_cast<char *>(theFileName) );
1427 // publish mesh in the study
1428 if ( CanPublishInStudy( aMesh ) ) {
1429 SALOMEDS::StudyBuilder_var aStudyBuilder = getStudyServant()->NewBuilder();
1430 aStudyBuilder->NewCommand(); // There is a transaction
1431 SALOMEDS::SObject_wrap aSO = PublishInStudy( SALOMEDS::SObject::_nil(), aMesh.in(), aFileName.c_str() );
1432 aStudyBuilder->CommitCommand();
1433 if ( !aSO->_is_nil() ) {
1434 // Update Python script
1435 TPythonDump(this) << aSO << " = " << this << ".CreateMeshesFromSTL(r'" << theFileName << "')";
1439 SMESH_Mesh_i* aServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( aMesh ).in() );
1441 aServant->ImportSTLFile( theFileName );
1442 aServant->GetImpl().GetMeshDS()->Modified();
1443 return aMesh._retn();
1446 //================================================================================
1448 * \brief Create meshes and import data from the CGSN file
1450 //================================================================================
1452 SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromCGNS( const char* theFileName,
1453 SMESH::DriverMED_ReadStatus& theStatus)
1455 Unexpect aCatch(SALOME_SalomeException);
1456 checkFileReadable( theFileName );
1458 SMESH::mesh_array_var aResult = new SMESH::mesh_array();
1461 // Retrieve nb meshes from the file
1462 DriverCGNS_Read myReader;
1463 myReader.SetFile( theFileName );
1464 Driver_Mesh::Status aStatus;
1465 int nbMeshes = myReader.GetNbMeshes(aStatus);
1466 theStatus = (SMESH::DriverMED_ReadStatus)aStatus;
1468 aResult->length( nbMeshes );
1470 { // open a new scope to make aPythonDump die before PythonDump in SMESH_Mesh::GetGroups()
1473 TPythonDump aPythonDump(this);
1474 aPythonDump << "([";
1476 if (theStatus == SMESH::DRS_OK)
1478 SALOMEDS::StudyBuilder_var aStudyBuilder = getStudyServant()->NewBuilder();
1479 aStudyBuilder->NewCommand(); // There is a transaction
1483 // Iterate through all meshes and create mesh objects
1484 for ( ; i < nbMeshes; ++i )
1487 if (i > 0) aPythonDump << ", ";
1490 SMESH::SMESH_Mesh_var mesh = createMesh();
1491 aResult[i] = SMESH::SMESH_Mesh::_duplicate( mesh );
1493 // Read mesh data (groups are published automatically by ImportMEDFile())
1494 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
1495 ASSERT( meshServant );
1497 SMESH::DriverMED_ReadStatus status1 =
1498 meshServant->ImportCGNSFile( theFileName, i, meshName );
1499 if (status1 > theStatus)
1500 theStatus = status1;
1502 meshServant->GetImpl().GetMeshDS()->Modified();
1503 // publish mesh in the study
1504 SALOMEDS::SObject_wrap aSO;
1505 if ( CanPublishInStudy( mesh ) )
1506 aSO = PublishMesh( mesh.in(), meshName.c_str() );
1509 if ( !aSO->_is_nil() ) {
1513 aPythonDump << "mesh_" << i;
1516 aStudyBuilder->CommitCommand();
1519 aPythonDump << "], status) = " << this << ".CreateMeshesFromCGNS(r'" << theFileName << "')";
1521 // Dump creation of groups
1522 for ( CORBA::ULong i = 0; i < aResult->length(); ++i )
1523 SMESH::ListOfGroups_var groups = aResult[ i ]->GetGroups();
1525 THROW_SALOME_CORBA_EXCEPTION("CGNS library is unavailable", SALOME::INTERNAL_ERROR);
1528 return aResult._retn();
1531 //================================================================================
1533 * \brief Create a mesh and import data from a GMF file
1535 //================================================================================
1537 SMESH::SMESH_Mesh_ptr
1538 SMESH_Gen_i::CreateMeshesFromGMF( const char* theFileName,
1539 CORBA::Boolean theMakeRequiredGroups,
1540 SMESH::ComputeError_out theError)
1542 Unexpect aCatch(SALOME_SalomeException);
1543 checkFileReadable( theFileName );
1545 SMESH::SMESH_Mesh_var aMesh = createMesh();
1547 char bname[ _MAX_FNAME ];
1548 _splitpath( theFileName, NULL, NULL, bname, NULL );
1549 string aFileName = bname;
1551 string aFileName = basename( const_cast<char *>(theFileName) );
1553 // publish mesh in the study
1554 if ( CanPublishInStudy( aMesh ) ) {
1555 SALOMEDS::StudyBuilder_var aStudyBuilder = getStudyServant()->NewBuilder();
1556 aStudyBuilder->NewCommand(); // There is a transaction
1557 SALOMEDS::SObject_wrap aSO = PublishInStudy( SALOMEDS::SObject::_nil(), aMesh.in(), aFileName.c_str() );
1558 aStudyBuilder->CommitCommand();
1559 if ( !aSO->_is_nil() ) {
1560 // Update Python script
1561 TPythonDump(this) << "("<< aSO << ", error) = " << this << ".CreateMeshesFromGMF(r'"
1562 << theFileName << "', "
1563 << theMakeRequiredGroups << " )";
1566 SMESH_Mesh_i* aServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( aMesh ).in() );
1568 theError = aServant->ImportGMFFile( theFileName, theMakeRequiredGroups );
1569 aServant->GetImpl().GetMeshDS()->Modified();
1570 return aMesh._retn();
1574 //=============================================================================
1576 * SMESH_Gen_i::IsReadyToCompute
1578 * Return true if mesh contains enough data to be computed
1580 //=============================================================================
1582 CORBA::Boolean SMESH_Gen_i::IsReadyToCompute( SMESH::SMESH_Mesh_ptr theMesh,
1583 GEOM::GEOM_Object_ptr theShapeObject )
1585 Unexpect aCatch(SALOME_SalomeException);
1586 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::IsReadyToCompute" );
1588 if ( CORBA::is_nil( theShapeObject ) )
1589 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1590 SALOME::BAD_PARAM );
1591 if ( CORBA::is_nil( theMesh ) )
1592 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
1593 SALOME::BAD_PARAM );
1596 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
1597 ASSERT( meshServant );
1598 if ( meshServant ) {
1599 // get local TopoDS_Shape
1600 TopoDS_Shape myLocShape = GeomObjectToShape( theShapeObject );
1601 // call implementation
1602 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1603 return myGen.CheckAlgoState( myLocMesh, myLocShape );
1606 catch ( SALOME_Exception& S_ex ) {
1607 INFOS( "catch exception "<< S_ex.what() );
1612 //================================================================================
1614 * \brief Find SObject for an algo
1616 //================================================================================
1618 SALOMEDS::SObject_ptr SMESH_Gen_i::GetAlgoSO(const ::SMESH_Algo* algo)
1621 SALOMEDS::Study_var aStudy = getStudyServant();
1622 if ( !aStudy->_is_nil() ) {
1623 // find algo in the study
1624 CORBA::String_var compDataType = ComponentDataType();
1625 SALOMEDS::SComponent_wrap father = aStudy->FindComponent( compDataType.in() );
1626 if ( !father->_is_nil() ) {
1627 SALOMEDS::ChildIterator_wrap itBig = aStudy->NewChildIterator( father );
1628 for ( ; itBig->More(); itBig->Next() ) {
1629 SALOMEDS::SObject_wrap gotBranch = itBig->Value();
1630 if ( gotBranch->Tag() == GetAlgorithmsRootTag() ) {
1631 SALOMEDS::ChildIterator_wrap algoIt = aStudy->NewChildIterator( gotBranch );
1632 for ( ; algoIt->More(); algoIt->Next() ) {
1633 SALOMEDS::SObject_wrap algoSO = algoIt->Value();
1634 CORBA::Object_var algoIOR = SObjectToObject( algoSO );
1635 if ( !CORBA::is_nil( algoIOR )) {
1636 SMESH_Hypothesis_i* impl = SMESH::DownCast<SMESH_Hypothesis_i*>( algoIOR );
1637 if ( impl && impl->GetImpl() == algo )
1638 return algoSO._retn();
1640 } // loop on algo SO's
1643 } // SMESH component iterator
1647 return SALOMEDS::SObject::_nil();
1650 //================================================================================
1652 * \brief Return errors of mesh computation
1654 //================================================================================
1656 SMESH::compute_error_array* SMESH_Gen_i::GetComputeErrors( SMESH::SMESH_Mesh_ptr theMesh,
1657 GEOM::GEOM_Object_ptr theSubObject )
1659 Unexpect aCatch(SALOME_SalomeException);
1660 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetComputeErrors()" );
1662 if ( CORBA::is_nil( theSubObject ) && theMesh->HasShapeToMesh())
1663 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
1665 if ( CORBA::is_nil( theMesh ) )
1666 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1668 SMESH::compute_error_array_var error_array = new SMESH::compute_error_array;
1670 if ( SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh ))
1673 if(theMesh->HasShapeToMesh())
1674 shape = GeomObjectToShape( theSubObject );
1676 shape = SMESH_Mesh::PseudoShape();
1678 ::SMESH_Mesh& mesh = meshServant->GetImpl();
1680 error_array->length( mesh.GetMeshDS()->MaxShapeIndex() );
1683 SMESH_subMesh *sm = mesh.GetSubMesh(shape);
1684 const bool includeSelf = true, complexShapeFirst = true;
1685 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(includeSelf,
1687 while ( smIt->more() )
1690 // if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
1692 SMESH_ComputeErrorPtr error = sm->GetComputeError();
1693 if ( error && !error->IsOK() )
1695 if ( !( error->myAlgo ) &&
1696 !( error->myAlgo = sm->GetAlgo() ))
1698 SMESH::ComputeError & errStruct = error_array[ nbErr++ ];
1699 errStruct.code = -( error->myName < 0 ? error->myName + 1: error->myName ); // -1 -> 0
1700 errStruct.comment = error->myComment.c_str();
1701 errStruct.subShapeID = sm->GetId();
1702 SALOMEDS::SObject_wrap algoSO = GetAlgoSO( error->myAlgo );
1703 if ( !algoSO->_is_nil() ) {
1704 CORBA::String_var algoName = algoSO->GetName();
1705 errStruct.algoName = algoName;
1708 errStruct.algoName = error->myAlgo->GetName();
1710 errStruct.hasBadMesh = error->HasBadElems();
1713 error_array->length( nbErr );
1716 catch ( SALOME_Exception& S_ex ) {
1717 INFOS( "catch exception "<< S_ex.what() );
1720 return error_array._retn();
1723 //================================================================================
1725 * \brief Return mesh elements preventing computation of a subshape
1727 //================================================================================
1729 SMESH::MeshPreviewStruct*
1730 SMESH_Gen_i::GetBadInputElements( SMESH::SMESH_Mesh_ptr theMesh,
1731 CORBA::Short theSubShapeID )
1733 Unexpect aCatch(SALOME_SalomeException);
1734 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetBadInputElements()" );
1736 if ( CORBA::is_nil( theMesh ) )
1737 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1739 SMESH::MeshPreviewStruct_var result = new SMESH::MeshPreviewStruct;
1742 if ( SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh ))
1744 // mesh implementation
1745 ::SMESH_Mesh& mesh = meshServant->GetImpl();
1746 // submesh by subshape id
1747 if ( SMESH_subMesh * sm = mesh.GetSubMeshContaining( theSubShapeID ))
1750 SMESH_ComputeErrorPtr error = sm->GetComputeError();
1751 if ( error && error->HasBadElems() )
1753 typedef map<const SMDS_MeshElement*, int > TNode2LocalIDMap;
1754 typedef TNode2LocalIDMap::iterator TNodeLocalID;
1756 // get nodes of elements and count elements
1757 TNode2LocalIDMap mapNode2LocalID;
1758 list< TNodeLocalID > connectivity;
1759 int i, nbElements = 0, nbConnNodes = 0;
1761 const list<const SMDS_MeshElement*>& badElems =
1762 static_cast<SMESH_BadInputElements*>( error.get() )->myBadElements;
1763 list<const SMDS_MeshElement*>::const_iterator elemIt = badElems.begin();
1764 list<const SMDS_MeshElement*>::const_iterator elemEnd = badElems.end();
1765 for ( ; elemIt != elemEnd; ++elemIt, ++nbElements )
1767 SMDS_ElemIteratorPtr nIt = (*elemIt)->nodesIterator();
1768 while ( nIt->more() )
1769 connectivity.push_back
1770 ( mapNode2LocalID.insert( make_pair( nIt->next(), ++nbConnNodes)).first );
1772 // fill node coords and assign local ids to the nodes
1773 int nbNodes = mapNode2LocalID.size();
1774 result->nodesXYZ.length( nbNodes );
1775 TNodeLocalID node2ID = mapNode2LocalID.begin();
1776 for ( i = 0; i < nbNodes; ++i, ++node2ID ) {
1777 node2ID->second = i;
1778 const SMDS_MeshNode* node = (const SMDS_MeshNode*) node2ID->first;
1779 result->nodesXYZ[i].x = node->X();
1780 result->nodesXYZ[i].y = node->Y();
1781 result->nodesXYZ[i].z = node->Z();
1783 // fill connectivity
1784 result->elementConnectivities.length( nbConnNodes );
1785 list< TNodeLocalID >::iterator connIt = connectivity.begin();
1786 for ( i = 0; i < nbConnNodes; ++i, ++connIt ) {
1787 result->elementConnectivities[i] = (*connIt)->second;
1789 // fill element types
1790 result->elementTypes.length( nbElements );
1791 for ( i = 0, elemIt = badElems.begin(); i <nbElements; ++i, ++elemIt )
1793 const SMDS_MeshElement* elem = *elemIt;
1794 result->elementTypes[i].SMDS_ElementType = (SMESH::ElementType) elem->GetType();
1795 result->elementTypes[i].isPoly = elem->IsPoly();
1796 result->elementTypes[i].nbNodesInElement = elem->NbNodes();
1802 catch ( SALOME_Exception& S_ex ) {
1803 INFOS( "catch exception "<< S_ex.what() );
1806 return result._retn();
1809 //================================================================================
1811 * \brief Create a group of elements preventing computation of a sub-shape
1813 //================================================================================
1815 SMESH::ListOfGroups*
1816 SMESH_Gen_i::MakeGroupsOfBadInputElements( SMESH::SMESH_Mesh_ptr theMesh,
1817 CORBA::Short theSubShapeID,
1818 const char* theGroupName )
1820 Unexpect aCatch(SALOME_SalomeException);
1822 SMESH::ListOfGroups_var groups;
1824 if ( CORBA::is_nil( theMesh ) )
1825 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1828 if ( SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh ))
1830 groups = meshServant->MakeGroupsOfBadInputElements( theSubShapeID, theGroupName );
1831 TPythonDump(this) << groups << " = " << this
1832 << ".MakeGroupsOfBadInputElements( "
1833 << theMesh << ", " << theSubShapeID << ", '" << theGroupName << "' )";
1836 catch ( SALOME_Exception& S_ex ) {
1837 INFOS( "catch exception "<< S_ex.what() );
1839 return groups._retn();
1842 //================================================================================
1844 * \brief Returns errors of hypotheses definition
1845 * \param theMesh - the mesh
1846 * \param theSubObject - the main or sub- shape
1847 * \retval SMESH::algo_error_array* - sequence of errors
1849 //================================================================================
1851 SMESH::algo_error_array* SMESH_Gen_i::GetAlgoState( SMESH::SMESH_Mesh_ptr theMesh,
1852 GEOM::GEOM_Object_ptr theSubObject )
1854 Unexpect aCatch(SALOME_SalomeException);
1855 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetAlgoState()" );
1857 if ( CORBA::is_nil( theSubObject ) && theMesh->HasShapeToMesh())
1858 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
1860 if ( CORBA::is_nil( theMesh ) )
1861 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
1863 SMESH::algo_error_array_var error_array = new SMESH::algo_error_array;
1865 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
1866 ASSERT( meshServant );
1867 if ( meshServant ) {
1868 TopoDS_Shape myLocShape;
1869 if(theMesh->HasShapeToMesh())
1870 myLocShape = GeomObjectToShape( theSubObject );
1872 myLocShape = SMESH_Mesh::PseudoShape();
1874 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1875 list< ::SMESH_Gen::TAlgoStateError > error_list;
1876 list< ::SMESH_Gen::TAlgoStateError >::iterator error;
1877 // call ::SMESH_Gen::GetAlgoState()
1878 myGen.GetAlgoState( myLocMesh, myLocShape, error_list );
1879 error_array->length( error_list.size() );
1881 for ( error = error_list.begin(); error != error_list.end(); ++error )
1883 // fill AlgoStateError structure
1884 SMESH::AlgoStateError & errStruct = error_array[ i++ ];
1885 errStruct.state = SMESH_Mesh_i::ConvertHypothesisStatus( error->_name );
1886 errStruct.algoDim = error->_algoDim;
1887 errStruct.isGlobalAlgo = error->_isGlobalAlgo;
1888 errStruct.algoName = "";
1889 SALOMEDS::SObject_wrap algoSO = GetAlgoSO( error->_algo );
1890 if ( !algoSO->_is_nil() ) {
1891 CORBA::String_var algoName = algoSO->GetName();
1892 errStruct.algoName = algoName.in();
1897 catch ( SALOME_Exception& S_ex ) {
1898 INFOS( "catch exception "<< S_ex.what() );
1900 return error_array._retn();
1903 //=============================================================================
1905 * SMESH_Gen_i::GetSubShapesId
1907 * Get sub-shapes unique ID's list
1909 //=============================================================================
1912 SMESH_Gen_i::GetSubShapesId( GEOM::GEOM_Object_ptr theMainShapeObject,
1913 const SMESH::object_array& theListOfSubShapeObject )
1915 Unexpect aCatch(SALOME_SalomeException);
1916 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetSubShapesId" );
1918 SMESH::long_array_var shapesId = new SMESH::long_array;
1921 if ( CORBA::is_nil( theMainShapeObject ) )
1922 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
1926 TopoDS_Shape myMainShape = GeomObjectToShape(theMainShapeObject);
1927 TopTools_IndexedMapOfShape myIndexToShape;
1928 TopExp::MapShapes(myMainShape,myIndexToShape);
1930 for ( CORBA::ULong i = 0; i < theListOfSubShapeObject.length(); i++ )
1932 GEOM::GEOM_Object_var aShapeObject
1933 = GEOM::GEOM_Object::_narrow(theListOfSubShapeObject[i]);
1934 if ( CORBA::is_nil( aShapeObject ) )
1935 THROW_SALOME_CORBA_EXCEPTION ("bad shape object reference", \
1936 SALOME::BAD_PARAM );
1938 TopoDS_Shape locShape = GeomObjectToShape(aShapeObject);
1939 for (TopExp_Explorer exp(locShape,TopAbs_FACE); exp.More(); exp.Next())
1941 const TopoDS_Face& F = TopoDS::Face(exp.Current());
1942 setId.insert(myIndexToShape.FindIndex(F));
1943 if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(F));
1945 for (TopExp_Explorer exp(locShape,TopAbs_EDGE); exp.More(); exp.Next())
1947 const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
1948 setId.insert(myIndexToShape.FindIndex(E));
1949 if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(E));
1951 for (TopExp_Explorer exp(locShape,TopAbs_VERTEX); exp.More(); exp.Next())
1953 const TopoDS_Vertex& V = TopoDS::Vertex(exp.Current());
1954 setId.insert(myIndexToShape.FindIndex(V));
1955 if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(V));
1958 shapesId->length(setId.size());
1959 set<int>::iterator iind;
1961 for (iind = setId.begin(); iind != setId.end(); iind++)
1963 if(MYDEBUG) SCRUTE((*iind));
1964 shapesId[i] = (*iind);
1965 if(MYDEBUG) SCRUTE(shapesId[i]);
1969 catch (SALOME_Exception& S_ex)
1971 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
1974 return shapesId._retn();
1977 //=============================================================================
1979 * SMESH_Gen_i::Compute
1981 * Compute mesh on a shape
1983 //=============================================================================
1985 CORBA::Boolean SMESH_Gen_i::Compute( SMESH::SMESH_Mesh_ptr theMesh,
1986 GEOM::GEOM_Object_ptr theShapeObject )
1989 Unexpect aCatch(SALOME_SalomeException);
1990 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Compute" );
1992 if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
1993 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
1994 SALOME::BAD_PARAM );
1996 if ( CORBA::is_nil( theMesh ) )
1997 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
1998 SALOME::BAD_PARAM );
2000 // Update Python script
2001 TPythonDump(this) << "isDone = " << this << ".Compute( "
2002 << theMesh << ", " << theShapeObject << ")";
2006 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
2007 ASSERT( meshServant );
2008 if ( meshServant ) {
2009 if ( isGeomModifIcon( theMesh ))
2010 meshServant->Clear();
2012 meshServant->Load();
2013 // NPAL16168: "geometrical group edition from a submesh don't modify mesh computation"
2014 meshServant->CheckGeomModif();
2015 // get local TopoDS_Shape
2016 TopoDS_Shape myLocShape;
2017 if(theMesh->HasShapeToMesh())
2018 myLocShape = GeomObjectToShape( theShapeObject );
2020 myLocShape = SMESH_Mesh::PseudoShape();
2021 // call implementation compute
2022 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
2023 myGen.PrepareCompute( myLocMesh, myLocShape );
2024 int how = ::SMESH_Gen::COMPACT_MESH;
2025 if ( myLocShape != myLocMesh.GetShapeToMesh() ) // compute a sub-mesh
2026 how |= ::SMESH_Gen::SHAPE_ONLY;
2027 bool ok = myGen.Compute( myLocMesh, myLocShape, how );
2028 meshServant->CreateGroupServants(); // algos can create groups (issue 0020918)
2029 myLocMesh.GetMeshDS()->Modified();
2030 UpdateIcons( theMesh );
2032 HighLightInvalid( theMesh, /*isInvalid=*/false );
2036 catch ( std::bad_alloc& ) {
2037 INFOS( "Compute(): lack of memory" );
2039 catch ( SALOME_Exception& S_ex ) {
2040 INFOS( "Compute(): catch exception "<< S_ex.what() );
2043 INFOS( "Compute(): unknown exception " );
2048 //=============================================================================
2050 * SMESH_Gen_i::CancelCompute
2052 * Cancel Compute mesh on a shape
2054 //=============================================================================
2056 void SMESH_Gen_i::CancelCompute( SMESH::SMESH_Mesh_ptr theMesh,
2057 GEOM::GEOM_Object_ptr theShapeObject )
2059 if ( SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() ))
2061 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
2062 TopoDS_Shape myLocShape;
2063 if(theMesh->HasShapeToMesh())
2064 myLocShape = GeomObjectToShape( theShapeObject );
2066 myLocShape = SMESH_Mesh::PseudoShape();
2067 myGen.CancelCompute( myLocMesh, myLocShape);
2071 //=============================================================================
2073 * SMESH_Gen_i::Precompute
2075 * Compute mesh as preview till indicated dimension on shape
2077 //=============================================================================
2079 SMESH::MeshPreviewStruct* SMESH_Gen_i::Precompute( SMESH::SMESH_Mesh_ptr theMesh,
2080 GEOM::GEOM_Object_ptr theShapeObject,
2081 SMESH::Dimension theDimension,
2082 SMESH::long_array& theShapesId)
2084 Unexpect aCatch(SALOME_SalomeException);
2085 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Precompute" );
2087 if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
2088 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
2089 SALOME::BAD_PARAM );
2091 if ( CORBA::is_nil( theMesh ) )
2092 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
2093 SALOME::BAD_PARAM );
2095 SMESH::MeshPreviewStruct_var result = new SMESH::MeshPreviewStruct;
2098 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
2099 meshServant->Load();
2100 ASSERT( meshServant );
2101 if ( meshServant ) {
2102 // NPAL16168: "geometrical group edition from a submesh don't modify mesh computation"
2103 meshServant->CheckGeomModif();
2104 // get local TopoDS_Shape
2105 TopoDS_Shape myLocShape;
2106 if(theMesh->HasShapeToMesh())
2107 myLocShape = GeomObjectToShape( theShapeObject );
2109 return result._retn();
2111 // call implementation compute
2112 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
2114 ::MeshDimension aDim = (MeshDimension)theDimension;
2115 if ( myGen.Compute( myLocMesh, myLocShape, ::SMESH_Gen::COMPACT_MESH, aDim, &shapeIds ) )
2117 int nbShapeId = shapeIds.size();
2118 theShapesId.length( nbShapeId );
2119 // iterates on shapes and collect mesh entities into mesh preview
2120 TSetOfInt::const_iterator idIt = shapeIds.begin();
2121 TSetOfInt::const_iterator idEnd = shapeIds.end();
2122 std::map< int, int > mapOfShIdNb;
2123 std::set< SMESH_TLink > setOfEdge;
2124 std::list< SMDSAbs_ElementType > listOfElemType;
2125 typedef map<const SMDS_MeshElement*, int > TNode2LocalIDMap;
2126 typedef TNode2LocalIDMap::iterator TNodeLocalID;
2127 TNode2LocalIDMap mapNode2LocalID;
2128 list< TNodeLocalID > connectivity;
2129 int i, nbConnNodes = 0;
2130 std::set< const SMESH_subMesh* > setOfVSubMesh;
2131 // iterates on shapes
2132 for ( ; idIt != idEnd; idIt++ )
2134 if ( mapOfShIdNb.find( *idIt ) != mapOfShIdNb.end() )
2136 SMESH_subMesh* sm = myLocMesh.GetSubMeshContaining(*idIt);
2137 if ( !sm || !sm->IsMeshComputed() )
2140 const TopoDS_Shape& aSh = sm->GetSubShape();
2141 const int shDim = myGen.GetShapeDim( aSh );
2142 if ( shDim < 1 || shDim > theDimension )
2145 mapOfShIdNb[ *idIt ] = 0;
2146 theShapesId[ mapOfShIdNb.size() - 1 ] = *idIt;
2148 SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
2149 if ( !smDS ) continue;
2151 if ( theDimension == SMESH::DIM_2D )
2153 SMDS_ElemIteratorPtr faceIt = smDS->GetElements();
2154 while ( faceIt->more() )
2156 const SMDS_MeshElement* face = faceIt->next();
2157 int aNbNode = face->NbNodes();
2159 aNbNode /= 2; // do not take into account additional middle nodes
2161 SMDS_MeshNode* node1 = (SMDS_MeshNode*)face->GetNode( 0 );
2162 for ( int nIndx = 0; nIndx < aNbNode; nIndx++ )
2164 SMDS_MeshNode* node2 = (SMDS_MeshNode*)face->GetNode( nIndx+1 < aNbNode ? nIndx+1 : 0 );
2165 if ( setOfEdge.insert( SMESH_TLink ( node1, node2 ) ).second )
2167 listOfElemType.push_back( SMDSAbs_Edge );
2168 connectivity.push_back
2169 ( mapNode2LocalID.insert( make_pair( node1, ++nbConnNodes)).first );
2170 connectivity.push_back
2171 ( mapNode2LocalID.insert( make_pair( node2, ++nbConnNodes)).first );
2177 else if ( theDimension == SMESH::DIM_1D )
2179 SMDS_NodeIteratorPtr nodeIt = smDS->GetNodes();
2180 while ( nodeIt->more() )
2182 listOfElemType.push_back( SMDSAbs_Node );
2183 connectivity.push_back
2184 ( mapNode2LocalID.insert( make_pair( nodeIt->next(), ++nbConnNodes)).first );
2186 // add corner nodes by first vertex from edge
2187 SMESH_subMeshIteratorPtr edgeSmIt =
2188 sm->getDependsOnIterator(/*includeSelf*/false,
2189 /*complexShapeFirst*/false);
2190 while ( edgeSmIt->more() )
2192 SMESH_subMesh* vertexSM = edgeSmIt->next();
2193 // check that vertex is not already treated
2194 if ( !setOfVSubMesh.insert( vertexSM ).second )
2196 if ( vertexSM->GetSubShape().ShapeType() != TopAbs_VERTEX )
2199 const SMESHDS_SubMesh* vertexSmDS = vertexSM->GetSubMeshDS();
2200 SMDS_NodeIteratorPtr nodeIt = vertexSmDS->GetNodes();
2201 while ( nodeIt->more() )
2203 listOfElemType.push_back( SMDSAbs_Node );
2204 connectivity.push_back
2205 ( mapNode2LocalID.insert( make_pair( nodeIt->next(), ++nbConnNodes)).first );
2211 // fill node coords and assign local ids to the nodes
2212 int nbNodes = mapNode2LocalID.size();
2213 result->nodesXYZ.length( nbNodes );
2214 TNodeLocalID node2ID = mapNode2LocalID.begin();
2215 for ( i = 0; i < nbNodes; ++i, ++node2ID ) {
2216 node2ID->second = i;
2217 const SMDS_MeshNode* node = (const SMDS_MeshNode*) node2ID->first;
2218 result->nodesXYZ[i].x = node->X();
2219 result->nodesXYZ[i].y = node->Y();
2220 result->nodesXYZ[i].z = node->Z();
2222 // fill connectivity
2223 result->elementConnectivities.length( nbConnNodes );
2224 list< TNodeLocalID >::iterator connIt = connectivity.begin();
2225 for ( i = 0; i < nbConnNodes; ++i, ++connIt ) {
2226 result->elementConnectivities[i] = (*connIt)->second;
2229 // fill element types
2230 result->elementTypes.length( listOfElemType.size() );
2231 std::list< SMDSAbs_ElementType >::const_iterator typeIt = listOfElemType.begin();
2232 std::list< SMDSAbs_ElementType >::const_iterator typeEnd = listOfElemType.end();
2233 for ( i = 0; typeIt != typeEnd; ++i, ++typeIt )
2235 SMDSAbs_ElementType elemType = *typeIt;
2236 result->elementTypes[i].SMDS_ElementType = (SMESH::ElementType)elemType;
2237 result->elementTypes[i].isPoly = false;
2238 result->elementTypes[i].nbNodesInElement = elemType == SMDSAbs_Edge ? 2 : 1;
2241 // correct number of shapes
2242 theShapesId.length( mapOfShIdNb.size() );
2246 catch ( std::bad_alloc& ) {
2247 INFOS( "Precompute(): lack of memory" );
2249 catch ( SALOME_Exception& S_ex ) {
2250 INFOS( "Precompute(): catch exception "<< S_ex.what() );
2253 INFOS( "Precompute(): unknown exception " );
2255 return result._retn();
2259 //=============================================================================
2261 * SMESH_Gen_i::Evaluate
2263 * Evaluate mesh on a shape
2265 //=============================================================================
2267 SMESH::smIdType_array* SMESH_Gen_i::Evaluate(SMESH::SMESH_Mesh_ptr theMesh,
2268 GEOM::GEOM_Object_ptr theShapeObject)
2270 Unexpect aCatch(SALOME_SalomeException);
2271 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Evaluate" );
2273 if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
2274 THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
2276 if ( CORBA::is_nil( theMesh ) )
2277 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference", SALOME::BAD_PARAM );
2279 SMESH::smIdType_array_var nbels = new SMESH::smIdType_array;
2280 nbels->length(SMESH::Entity_Last);
2281 int i = SMESH::Entity_Node;
2282 for (; i < SMESH::Entity_Last; i++)
2285 // Update Python script
2286 TPythonDump(this) << "theNbElems = " << this << ".Evaluate( "
2287 << theMesh << ", " << theShapeObject << ")";
2291 SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
2292 ASSERT( meshServant );
2293 if ( meshServant ) {
2294 meshServant->Load();
2295 // NPAL16168: "geometrical group edition from a submesh don't modify mesh computation"
2296 meshServant->CheckGeomModif();
2297 // get local TopoDS_Shape
2298 TopoDS_Shape myLocShape;
2299 if(theMesh->HasShapeToMesh())
2300 myLocShape = GeomObjectToShape( theShapeObject );
2302 myLocShape = SMESH_Mesh::PseudoShape();
2303 // call implementation compute
2304 ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
2305 MapShapeNbElems aResMap;
2306 /*CORBA::Boolean ret =*/ myGen.Evaluate( myLocMesh, myLocShape, aResMap);
2307 MapShapeNbElemsItr anIt = aResMap.begin();
2308 for(; anIt!=aResMap.end(); anIt++) {
2309 const vector<smIdType>& aVec = (*anIt).second;
2310 for ( i = SMESH::Entity_Node; i < (int)aVec.size(); i++ ) {
2311 smIdType nbElem = aVec[i];
2312 if ( nbElem < 0 ) // algo failed, check that it has reported a message
2314 SMESH_subMesh* sm = anIt->first;
2315 SMESH_ComputeErrorPtr& error = sm->GetComputeError();
2316 const SMESH_Algo* algo = sm->GetAlgo();
2317 if ( (algo && !error.get()) || error->IsOK() )
2318 error.reset( new SMESH_ComputeError( COMPERR_ALGO_FAILED,"Failed to evaluate",algo));
2322 nbels[i] += aVec[i];
2326 return nbels._retn();
2329 catch ( std::bad_alloc& ) {
2330 INFOS( "Evaluate(): lack of memory" );
2332 catch ( SALOME_Exception& S_ex ) {
2333 INFOS( "Evaluate(): catch exception "<< S_ex.what() );
2336 INFOS( "Evaluate(): unknown exception " );
2339 return nbels._retn();
2342 //================================================================================
2344 * \brief Return geometrical object the given element is built on
2345 * \param theMesh - the mesh the element is in
2346 * \param theElementID - the element ID
2347 * \param theGeomName - the name of the result geom object if it is not yet published
2348 * \retval GEOM::GEOM_Object_ptr - the found or just published geom object (no need to UnRegister())
2350 //================================================================================
2352 GEOM::GEOM_Object_ptr
2353 SMESH_Gen_i::GetGeometryByMeshElement( SMESH::SMESH_Mesh_ptr theMesh,
2354 SMESH::smIdType theElementID,
2355 const char* theGeomName)
2357 Unexpect aCatch(SALOME_SalomeException);
2359 GEOM::GEOM_Object_wrap geom = FindGeometryByMeshElement(theMesh, theElementID);
2360 if ( !geom->_is_nil() ) {
2361 GEOM::GEOM_Object_var mainShape = theMesh->GetShapeToMesh();
2362 GEOM::GEOM_Gen_var geomGen = GetGeomEngine( geom );
2364 // try to find the corresponding SObject
2365 SALOMEDS::SObject_wrap SObj = ObjectToSObject( geom.in() );
2366 if ( SObj->_is_nil() ) // submesh can be not found even if published
2368 // try to find published submesh
2369 GEOM::ListOfLong_var list = geom->GetSubShapeIndices();
2370 if ( !geom->IsMainShape() && list->length() == 1 ) {
2371 SALOMEDS::SObject_wrap mainSO = ObjectToSObject( mainShape );
2372 SALOMEDS::ChildIterator_wrap it;
2373 if ( !mainSO->_is_nil() ) {
2374 it = getStudyServant()->NewChildIterator( mainSO );
2376 if ( !it->_is_nil() ) {
2377 for ( it->InitEx(true); it->More(); it->Next() ) {
2378 SALOMEDS::SObject_wrap so = it->Value();
2379 CORBA::Object_var obj = SObjectToObject( so );
2380 GEOM::GEOM_Object_var subGeom = GEOM::GEOM_Object::_narrow( obj );
2381 if ( !subGeom->_is_nil() ) {
2382 GEOM::ListOfLong_var subList = subGeom->GetSubShapeIndices();
2383 if ( subList->length() == 1 && list[0] == subList[0] ) {
2393 if ( SObj->_is_nil() && !geomGen->_is_nil() ) // publish a new subshape
2394 SObj = geomGen->AddInStudy( geom, theGeomName, mainShape );
2396 // return only published geometry
2397 if ( !SObj->_is_nil() ) {
2398 //return geom._retn(); -- servant of geom must be UnRegister()ed;
2399 CORBA::Object_var obj = SObjectToObject( SObj );
2400 GEOM::GEOM_Object_var go = GEOM::GEOM_Object::_narrow( obj );
2404 return GEOM::GEOM_Object::_nil();
2407 //================================================================================
2409 * \brief Return geometrical object the given element is built on.
2410 * \param theMesh - the mesh the element is in
2411 * \param theElementID - the element ID
2412 * \retval GEOM::GEOM_Object_ptr - the found or created (UnRegister()!) geom object
2414 //================================================================================
2416 GEOM::GEOM_Object_ptr
2417 SMESH_Gen_i::FindGeometryByMeshElement( SMESH::SMESH_Mesh_ptr theMesh,
2418 SMESH::smIdType theElementID)
2420 Unexpect aCatch(SALOME_SalomeException);
2421 if ( CORBA::is_nil( theMesh ) )
2422 THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference", SALOME::BAD_PARAM );
2424 GEOM::GEOM_Object_var mainShape = theMesh->GetShapeToMesh();
2425 GEOM::GEOM_Gen_var geomGen = GetGeomEngine( mainShape );
2427 // get a core mesh DS
2428 SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
2429 if ( meshServant && !geomGen->_is_nil() && !mainShape->_is_nil() )
2431 ::SMESH_Mesh & mesh = meshServant->GetImpl();
2432 SMESHDS_Mesh* meshDS = mesh.GetMeshDS();
2433 // find the element in mesh
2434 if ( const SMDS_MeshElement * elem = meshDS->FindElement( theElementID ) ) {
2435 // find a shape id by the element
2436 if ( int shapeID = ::SMESH_MeshEditor( &mesh ).FindShape( elem )) {
2437 // get a geom object by the shape id
2438 GEOM::GEOM_Object_var geom = ShapeToGeomObject( meshDS->IndexToShape( shapeID ));
2439 if ( geom->_is_nil() ) {
2440 // try to find a published sub-shape
2441 SALOMEDS::SObject_wrap mainSO = ObjectToSObject( mainShape );
2442 SALOMEDS::ChildIterator_wrap it;
2443 if ( !mainSO->_is_nil() ) {
2444 it = getStudyServant()->NewChildIterator( mainSO );
2446 if ( !it->_is_nil() ) {
2447 for ( it->InitEx(true); it->More(); it->Next() ) {
2448 SALOMEDS::SObject_wrap so = it->Value();
2449 CORBA::Object_var obj = SObjectToObject( so );
2450 GEOM::GEOM_Object_var subGeom = GEOM::GEOM_Object::_narrow( obj );
2451 if ( !subGeom->_is_nil() ) {
2452 GEOM::ListOfLong_var subList = subGeom->GetSubShapeIndices();
2453 if ( subList->length() == 1 && shapeID == subList[0] ) {
2461 if ( geom->_is_nil() ) {
2463 GEOM::GEOM_IShapesOperations_wrap op = geomGen->GetIShapesOperations();
2464 if ( !op->_is_nil() )
2465 geom = op->GetSubShape( mainShape, shapeID );
2470 if ( !geom->_is_nil() ) {
2471 GeomObjectToShape( geom ); // let geom client remember the found shape
2472 return geom._retn();
2477 return GEOM::GEOM_Object::_nil();
2480 //================================================================================
2482 * SMESH_Gen_i::Concatenate
2484 * Concatenate the given meshes into one mesh
2486 //================================================================================
2488 SMESH::SMESH_Mesh_ptr
2489 SMESH_Gen_i::Concatenate(const SMESH::ListOfIDSources& theMeshesArray,
2490 CORBA::Boolean theUniteIdenticalGroups,
2491 CORBA::Boolean theMergeNodesAndElements,
2492 CORBA::Double theMergeTolerance,
2493 SMESH::SMESH_Mesh_ptr theMeshToAppendTo)
2495 return ConcatenateCommon(theMeshesArray,
2496 theUniteIdenticalGroups,
2497 theMergeNodesAndElements,
2503 //================================================================================
2505 * SMESH_Gen_i::ConcatenateWithGroups
2507 * Concatenate the given meshes into one mesh
2508 * Create the groups of all elements from initial meshes
2510 //================================================================================
2512 SMESH::SMESH_Mesh_ptr
2513 SMESH_Gen_i::ConcatenateWithGroups(const SMESH::ListOfIDSources& theMeshesArray,
2514 CORBA::Boolean theUniteIdenticalGroups,
2515 CORBA::Boolean theMergeNodesAndElements,
2516 CORBA::Double theMergeTolerance,
2517 SMESH::SMESH_Mesh_ptr theMeshToAppendTo)
2519 return ConcatenateCommon(theMeshesArray,
2520 theUniteIdenticalGroups,
2521 theMergeNodesAndElements,
2527 //================================================================================
2529 * SMESH_Gen_i::ConcatenateCommon
2531 * Concatenate the given meshes into one mesh
2533 //================================================================================
2535 SMESH::SMESH_Mesh_ptr
2536 SMESH_Gen_i::ConcatenateCommon(const SMESH::ListOfIDSources& theMeshesArray,
2537 CORBA::Boolean theUniteIdenticalGroups,
2538 CORBA::Boolean theMergeNodesAndElements,
2539 CORBA::Double theMergeTolerance,
2540 CORBA::Boolean theCommonGroups,
2541 SMESH::SMESH_Mesh_ptr theMeshToAppendTo)
2543 std::unique_ptr< TPythonDump > pPythonDump( new TPythonDump(this) );
2544 TPythonDump& pythonDump = *pPythonDump; // prevent dump of called methods
2546 // create mesh if theMeshToAppendTo not provided
2547 SMESH::SMESH_Mesh_var newMesh;
2548 if ( CORBA::is_nil( theMeshToAppendTo ))
2549 newMesh = CreateEmptyMesh();
2551 newMesh = SMESH::SMESH_Mesh::_duplicate( theMeshToAppendTo );
2552 SMESH_Mesh_i* newImpl = SMESH::DownCast<SMESH_Mesh_i*>( newMesh );
2553 if ( !newImpl ) return newMesh._retn();
2556 ::SMESH_Mesh& locMesh = newImpl->GetImpl();
2557 SMESHDS_Mesh* newMeshDS = locMesh.GetMeshDS();
2559 typedef std::list<SMESH::SMESH_Group_var> TListOfNewGroups;
2560 typedef std::pair<string, SMESH::ElementType > TNameAndType;
2561 typedef std::map< TNameAndType, TListOfNewGroups > TGroupsMap;
2562 TGroupsMap groupsMap;
2563 TListOfNewGroups listOfNewGroups;
2565 if ( !CORBA::is_nil( theMeshToAppendTo ))
2567 // fill groupsMap with existing groups
2568 SMESH::ListOfGroups_var groups = theMeshToAppendTo->GetGroups();
2569 for ( CORBA::ULong i = 0; i < groups->length(); ++i )
2571 SMESH::SMESH_Group_var group = SMESH::SMESH_Group::_narrow( groups[ i ]);
2572 if ( !group->_is_nil() )
2574 CORBA::String_var name = group->GetName();
2575 SMESH::ElementType type = group->GetType();
2576 groupsMap[ TNameAndType( name.in(), type ) ].push_back( group );
2581 ::SMESH_MeshEditor newEditor( &locMesh );
2582 ::SMESH_MeshEditor::ElemFeatures elemType;
2584 // loop on sub-meshes
2585 for ( CORBA::ULong i = 0; i < theMeshesArray.length(); i++ )
2587 if ( CORBA::is_nil( theMeshesArray[i] )) continue;
2588 SMESH::SMESH_Mesh_var initMesh = theMeshesArray[i]->GetMesh();
2589 SMESH_Mesh_i* initImpl = SMESH::DownCast<SMESH_Mesh_i*>( initMesh );
2590 if ( !initImpl ) continue;
2591 if ( initMesh->_is_equivalent( theMeshToAppendTo ))
2595 // assure that IDs increment by one during iteration
2596 ::SMESH_Mesh& initLocMesh = initImpl->GetImpl();
2597 SMESHDS_Mesh* initMeshDS = initLocMesh.GetMeshDS();
2598 if ( initMeshDS->MaxNodeID() > initMeshDS->NbNodes() ||
2599 initMeshDS->MaxElementID() > initMeshDS->NbElements() )
2601 initMeshDS->Modified();
2602 initMeshDS->CompactMesh();
2605 // remember nb of elements before filling in
2606 SMESH::smIdType_array_var prevState = newMesh->GetNbElementsByType();
2610 std::vector< const SMDS_MeshElement* > newNodes( initMeshDS->NbNodes() + 1, 0 );
2611 SMDS_ElemIteratorPtr elemIt = initImpl->GetElements( theMeshesArray[i], SMESH::NODE );
2612 while ( elemIt->more() )
2614 SMESH_NodeXYZ node = elemIt->next();
2615 newNodes[ node->GetID() ] = newMeshDS->AddNode( node.X(), node.Y(), node.Z() );
2620 std::vector< const SMDS_MeshElement* > newElems( initMeshDS->NbElements() + 1, 0 );
2621 SMESH::array_of_ElementType_var srcElemTypes = theMeshesArray[i]->GetTypes();
2622 bool hasElems = (( srcElemTypes->length() > 1 ) ||
2623 ( srcElemTypes->length() == 1 && srcElemTypes[0] != SMESH::NODE ));
2626 elemIt = initImpl->GetElements( theMeshesArray[i], SMESH::ALL );
2627 while ( elemIt->more() )
2629 const SMDS_MeshElement* elem = elemIt->next();
2630 elemType.myNodes.resize( elem->NbNodes() );
2632 SMDS_NodeIteratorPtr itNodes = elem->nodeIterator();
2633 for ( int k = 0; itNodes->more(); k++)
2635 const SMDS_MeshNode* node = itNodes->next();
2636 elemType.myNodes[ k ] = static_cast< const SMDS_MeshNode*> ( newNodes[ node->GetID() ]);
2639 // creates a corresponding element on existent nodes in new mesh
2640 newElems[ elem->GetID() ] =
2641 newEditor.AddElement( elemType.myNodes, elemType.Init( elem, /*basicOnly=*/false ));
2643 newEditor.ClearLastCreated(); // forget the history
2647 // create groups of just added elements
2648 SMESH::SMESH_Group_var newGroup;
2649 SMESH::ElementType groupType;
2650 if ( theCommonGroups )
2653 const char* typeNames[] = { "All","Nodes","Edges","Faces","Volumes","0DElems","Balls" };
2655 // check of typeNames: compilation failure mains that NB_ELEMENT_TYPES changed:
2656 static_assert( sizeof(typeNames) / sizeof(const char*) ==SMESH::NB_ELEMENT_TYPES,
2657 "Update names of ElementType's!!!" );
2659 SMESH::smIdType_array_var curState = newMesh->GetNbElementsByType();
2661 for( groupType = SMESH::NODE;
2662 groupType < SMESH::NB_ELEMENT_TYPES;
2663 groupType = (SMESH::ElementType)( groupType + 1 ))
2665 if ( curState[ groupType ] <= prevState[ groupType ])
2666 continue; // no elements of groupType added from the i-th mesh
2668 // make a group name
2669 std::string groupName = "Gr";
2670 SALOMEDS::SObject_wrap meshSO = ObjectToSObject( theMeshesArray[i] );
2672 CORBA::String_var name = meshSO->GetName();
2676 groupName += typeNames[ groupType ];
2678 // make and fill a group
2679 newGroup = newImpl->CreateGroup( groupType, groupName.c_str() );
2680 std::vector< const SMDS_MeshElement* > & elemVec =
2681 ( groupType == SMESH::NODE ) ? newNodes : newElems;
2682 if ( SMESH_Group_i* grp_i = SMESH::DownCast<SMESH_Group_i*>( newGroup ))
2684 if ( SMESHDS_Group* grpDS = dynamic_cast<SMESHDS_Group*>( grp_i->GetGroupDS() ))
2686 for ( size_t j = 0; j < elemVec.size(); ++j )
2688 if ( elemVec[j] && elemVec[j]->GetType() == grpDS->GetType() )
2689 grpDS->Add( elemVec[j] );
2693 listOfNewGroups.clear();
2694 listOfNewGroups.push_back( newGroup );
2695 groupsMap.insert( std::make_pair( TNameAndType( groupName, groupType ),
2700 if ( SMESH_Mesh_i* initImpl = SMESH::DownCast<SMESH_Mesh_i*>( theMeshesArray[i] ))
2704 SMESH::SMESH_GroupBase_ptr group;
2705 CORBA::String_var groupName;
2706 SMESH::smIdType_array_var newIDs = new SMESH::smIdType_array();
2708 // loop on groups of a source mesh
2709 SMESH::ListOfGroups_var listOfGroups = initImpl->GetGroups();
2710 for ( CORBA::ULong iG = 0; iG < listOfGroups->length(); iG++ )
2712 group = listOfGroups[iG];
2713 groupType = group->GetType();
2714 groupName = group->GetName();
2715 std::string name = groupName.in();
2717 // convert a list of IDs
2718 newIDs->length( group->Size() );
2719 std::vector< const SMDS_MeshElement* > & elemVec =
2720 ( groupType == SMESH::NODE ) ? newNodes : newElems;
2721 SMDS_ElemIteratorPtr itGrElems = initImpl->GetElements( group, SMESH::ALL );
2723 while ( itGrElems->more() )
2725 const SMDS_MeshElement* elem = itGrElems->next();
2726 const SMDS_MeshElement* newElem = elemVec[ elem->GetID() ];
2728 newIDs[ nbElems++ ] = newElem->GetID();
2730 newIDs->length( nbElems );
2732 // check that a current group name and type don't have identical ones in final mesh
2733 listOfNewGroups.clear();
2734 TNameAndType nameAndType( name, groupType );
2735 TGroupsMap::iterator anIter = groupsMap.find( nameAndType );
2736 if ( anIter == groupsMap.end() )
2738 // add a new group in the mesh
2739 newGroup = newImpl->CreateGroup( groupType, groupName.in() );
2740 newGroup->Add( newIDs );
2742 listOfNewGroups.push_back( newGroup );
2743 groupsMap.insert( std::make_pair( nameAndType, listOfNewGroups ));
2745 else if ( theUniteIdenticalGroups )
2747 // unite identical groups
2748 TListOfNewGroups& aNewGroups = anIter->second;
2749 aNewGroups.front()->Add( newIDs );
2753 // rename identical groups
2754 newGroup = newImpl->CreateGroup( groupType, groupName );
2755 newGroup->Add( newIDs );
2757 TListOfNewGroups& newGroups = anIter->second;
2758 std::string newGroupName;
2759 if ( newGroups.size() == 1 )
2761 newGroupName = name + "_1";
2762 newGroups.front()->SetName( newGroupName.c_str() );
2764 newGroupName = name + "_" + SMESH_Comment( newGroups.size() + 1 );
2765 newGroup->SetName( newGroupName.c_str() );
2766 newGroups.push_back( newGroup );
2769 } // if an IDSource is a mesh
2772 if ( theMergeNodesAndElements ) // merge nodes
2774 TIDSortedNodeSet meshNodes; // no input nodes == treat all
2775 SMESH_MeshEditor::TListOfListOfNodes groupsOfNodes;
2776 newEditor.FindCoincidentNodes( meshNodes, theMergeTolerance, groupsOfNodes,
2777 /*SeparateCornersAndMedium=*/ false );
2778 newEditor.MergeNodes( groupsOfNodes );
2780 newEditor.MergeEqualElements();
2783 // Update Python script
2784 pythonDump << newMesh << " = " << this
2785 << "." << ( theCommonGroups ? "ConcatenateWithGroups" : "Concatenate" ) << "( "
2786 << theMeshesArray << ", "
2787 << theUniteIdenticalGroups << ", "
2788 << theMergeNodesAndElements << ", "
2789 << TVar( theMergeTolerance ) << ", "
2790 << theMeshToAppendTo << " )";
2792 pPythonDump.reset(); // enable python dump from GetGroups()
2794 // 0020577: EDF 1164 SMESH: Bad dump of concatenate with create common groups
2795 if ( !newMesh->_is_nil() )
2797 SMESH::ListOfGroups_var groups = newMesh->GetGroups();
2800 // IPAL21468 Change icon of compound because it need not be computed.
2801 SALOMEDS::SObject_wrap meshSO = ObjectToSObject( newMesh );
2802 SetPixMap( meshSO, "ICON_SMESH_TREE_MESH" );
2804 newMeshDS->Modified();
2806 return newMesh._retn();
2810 //================================================================================
2812 * \brief Create a mesh by copying a part of another mesh
2813 * \param mesh - TetraHedron mesh
2814 * \param meshName Name of the created mesh
2815 * \retval SMESH::SMESH_Mesh_ptr - the new mesh
2817 //================================================================================
2819 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateDualMesh(SMESH::SMESH_IDSource_ptr mesh,
2820 const char* meshName)
2822 Unexpect aCatch(SALOME_SalomeException);
2824 TPythonDump* pyDump = new TPythonDump(this); // prevent dump from CreateMesh()
2825 std::unique_ptr<TPythonDump> pyDumpDeleter( pyDump );
2827 // 1. Get source mesh
2829 if ( CORBA::is_nil( mesh ))
2830 THROW_SALOME_CORBA_EXCEPTION( "bad IDSource", SALOME::BAD_PARAM );
2832 std::cout << mesh << std::endl;
2833 SMESH::SMESH_Mesh_var srcMesh = mesh->GetMesh();
2834 SMESH_Mesh_i* srcMesh_i = SMESH::DownCast<SMESH_Mesh_i*>( srcMesh );
2836 THROW_SALOME_CORBA_EXCEPTION( "bad mesh of IDSource", SALOME::BAD_PARAM );
2838 SMESH_Mesh& srcMesh2 = srcMesh_i->GetImpl();
2841 CORBA::String_var mesh_ior=GetORB()->object_to_string(mesh);
2842 std::string mesh_id = mesh_ior.in();
2843 std::string dual_mesh_file="/tmp/test_dual.med";
2844 std::string mesh_name = "MESH";
2846 std::string python_code;
2847 python_code += "import sys\n";
2848 python_code += "import salome\n";
2849 python_code += "import medcoupling as mc\n";
2850 python_code += "from math import pi\n";
2851 python_code += "salome.salome_init()\n";
2852 python_code += "import GEOM\n";
2853 python_code += "from salome.geom import geomBuilder\n";
2854 python_code += "geompy = geomBuilder.New()\n";
2855 python_code += "import SMESH, SALOMEDS\n";
2856 python_code += "from salome.smesh import smeshBuilder\n";
2857 python_code += "smesh = smeshBuilder.New()\n";
2858 python_code += "def create_dual_mesh(mesh_ior, output_file):\n";
2859 python_code += " mesh = salome.orb.string_to_object(mesh_ior)\n";
2860 python_code += " shape = mesh.GetShapeToMesh()\n";
2861 python_code += " if not mesh:\n";
2862 python_code += " raise Exception(\"Could not find mesh using id: \", mesh_id)\n";
2863 python_code += " int_ptr = mesh.ExportMEDCoupling(True, True)\n";
2864 python_code += " dab = mc.FromPyIntPtrToDataArrayByte(int_ptr)\n";
2865 python_code += " tetras = mc.MEDFileMesh.New(dab)[0]\n";
2866 python_code += " tetras = mc.MEDCoupling1SGTUMesh(tetras)\n";
2867 python_code += " polyh = tetras.computeDualMesh()\n";
2868 python_code += " skin = tetras.buildUnstructured().computeSkin()\n";
2869 python_code += " skin_polyh = polyh.buildUnstructured().computeSkin()\n";
2870 python_code += " allNodesOnSkinPolyh = skin_polyh.computeFetchedNodeIds()\n";
2871 python_code += " allNodesOnSkin = skin.computeFetchedNodeIds()\n";
2872 python_code += " ptsAdded = allNodesOnSkinPolyh.buildSubstraction(allNodesOnSkin)\n";
2873 python_code += " ptsAddedMesh = mc.MEDCouplingUMesh.Build0DMeshFromCoords( skin_polyh.getCoords()[ptsAdded] )\n";
2874 python_code += " ptsAddedCoo = ptsAddedMesh.getCoords()\n";
2875 python_code += " ptsAddedCooModified = ptsAddedCoo[:]\n";
2876 python_code += " polyh.setName(\"MESH\")\n";
2877 python_code += " polyh.write(output_file)\n";
2879 // Running Python script
2880 assert(Py_IsInitialized());
2881 PyGILState_STATE gstate;
2882 gstate = PyGILState_Ensure();
2883 PyRun_SimpleString(python_code.c_str());
2885 cmd += "create_dual_mesh(\"" + mesh_id + "\", \"" + dual_mesh_file + "\")";
2886 PyRun_SimpleString(cmd.c_str());
2888 PyGILState_Release(gstate);
2890 // Import created MED
2891 SMESH::SMESH_Mesh_var newMesh = CreateMesh(GEOM::GEOM_Object::_nil());
2892 SMESH_Mesh_i* newMesh_i = SMESH::DownCast<SMESH_Mesh_i*>( newMesh );
2894 THROW_SALOME_CORBA_EXCEPTION( "can't create a mesh", SALOME::INTERNAL_ERROR );
2895 SALOMEDS::SObject_wrap meshSO = ObjectToSObject( newMesh );
2896 if ( !meshSO->_is_nil() )
2898 SetName( meshSO, meshName, "Mesh" );
2899 SetPixMap( meshSO, "ICON_SMESH_TREE_MESH_IMPORTED");
2902 SMESH_Mesh& newMesh2 = newMesh_i->GetImpl();
2904 newMesh2.MEDToMesh(dual_mesh_file.c_str(), "MESH");
2906 SMESHDS_Mesh* newMeshDS = newMesh_i->GetImpl().GetMeshDS();
2908 newMeshDS->Modified();
2910 *pyDump << newMesh << " = " << this
2911 << ".CreateDualMesh( " << mesh << ", "
2912 << "'" << meshName << "') ";
2914 return newMesh._retn();
2917 //================================================================================
2919 * \brief Create a mesh by copying a part of another mesh
2920 * \param meshPart - a part of mesh to copy
2921 * \param toCopyGroups - to create in the new mesh groups
2922 * the copied elements belongs to
2923 * \param toKeepIDs - to preserve IDs of the copied elements or not
2924 * \retval SMESH::SMESH_Mesh_ptr - the new mesh
2926 //================================================================================
2928 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CopyMesh(SMESH::SMESH_IDSource_ptr meshPart,
2929 const char* meshName,
2930 CORBA::Boolean toCopyGroups,
2931 CORBA::Boolean toKeepIDs)
2933 Unexpect aCatch(SALOME_SalomeException);
2935 TPythonDump* pyDump = new TPythonDump(this); // prevent dump from CreateMesh()
2936 std::unique_ptr<TPythonDump> pyDumpDeleter( pyDump );
2938 // 1. Get source mesh
2940 if ( CORBA::is_nil( meshPart ))
2941 THROW_SALOME_CORBA_EXCEPTION( "bad IDSource", SALOME::BAD_PARAM );
2943 SMESH::SMESH_Mesh_var srcMesh = meshPart->GetMesh();
2944 SMESH_Mesh_i* srcMesh_i = SMESH::DownCast<SMESH_Mesh_i*>( srcMesh );
2946 THROW_SALOME_CORBA_EXCEPTION( "bad mesh of IDSource", SALOME::BAD_PARAM );
2948 SMESHDS_Mesh* srcMeshDS = srcMesh_i->GetImpl().GetMeshDS();
2950 // 2. Make a new mesh
2952 SMESH::SMESH_Mesh_var newMesh = CreateMesh(GEOM::GEOM_Object::_nil());
2953 SMESH_Mesh_i* newMesh_i = SMESH::DownCast<SMESH_Mesh_i*>( newMesh );
2955 THROW_SALOME_CORBA_EXCEPTION( "can't create a mesh", SALOME::INTERNAL_ERROR );
2956 SALOMEDS::SObject_wrap meshSO = ObjectToSObject( newMesh );
2957 if ( !meshSO->_is_nil() )
2959 SetName( meshSO, meshName, "Mesh" );
2960 SetPixMap( meshSO, "ICON_SMESH_TREE_MESH_IMPORTED");
2962 SMESHDS_Mesh* newMeshDS = newMesh_i->GetImpl().GetMeshDS();
2963 ::SMESH_MeshEditor editor( &newMesh_i->GetImpl() );
2964 ::SMESH_MeshEditor::ElemFeatures elemType;
2966 // 3. Get elements to copy
2968 SMDS_ElemIteratorPtr srcElemIt; SMDS_NodeIteratorPtr srcNodeIt;
2969 TIDSortedElemSet srcElems;
2970 SMESH::array_of_ElementType_var srcElemTypes = meshPart->GetTypes();
2971 if ( SMESH::DownCast<SMESH_Mesh_i*>( meshPart ))
2974 srcElemIt = srcMeshDS->elementsIterator();
2975 srcNodeIt = srcMeshDS->nodesIterator();
2979 SMESH::smIdType_array_var ids = meshPart->GetIDs();
2980 if ( srcElemTypes->length() == 1 && srcElemTypes[0] == SMESH::NODE ) // group of nodes
2982 for ( CORBA::ULong i=0; i < ids->length(); i++ )
2983 if ( const SMDS_MeshElement * elem = srcMeshDS->FindNode( ids[i] ))
2984 srcElems.insert( elem );
2988 for ( CORBA::ULong i = 0; i < ids->length(); i++ )
2989 if ( const SMDS_MeshElement * elem = srcMeshDS->FindElement( ids[i] ))
2990 srcElems.insert( elem );
2992 if ( srcElems.empty() )
2993 return newMesh._retn();
2995 typedef SMDS_SetIterator< SMDS_pElement, TIDSortedElemSet::const_iterator > ElIter;
2996 srcElemIt = SMDS_ElemIteratorPtr( new ElIter( srcElems.begin(), srcElems.end() ));
3001 typedef map<SMDS_pElement, SMDS_pElement, TIDCompare> TE2EMap;
3002 TE2EMap e2eMapByType[ SMDSAbs_NbElementTypes ];
3003 TE2EMap& n2nMap = e2eMapByType[ SMDSAbs_Node ];
3005 const SMDS_MeshNode *nSrc, *nTgt;
3006 vector< const SMDS_MeshNode* > nodes;
3007 while ( srcElemIt->more() )
3009 const SMDS_MeshElement * elem = srcElemIt->next();
3011 nodes.resize( elem->NbNodes());
3012 SMDS_ElemIteratorPtr nIt = elem->nodesIterator();
3014 for ( iN = 0; nIt->more(); ++iN )
3016 nSrc = static_cast<const SMDS_MeshNode*>( nIt->next() );
3017 nTgt = newMeshDS->FindNode( nSrc->GetID());
3019 nTgt = newMeshDS->AddNodeWithID( nSrc->X(), nSrc->Y(), nSrc->Z(), nSrc->GetID());
3024 for ( iN = 0; nIt->more(); ++iN )
3026 nSrc = static_cast<const SMDS_MeshNode*>( nIt->next() );
3027 TE2EMap::iterator n2n = n2nMap.insert( make_pair( nSrc, SMDS_pNode(0) )).first;
3029 n2n->second = newMeshDS->AddNode( nSrc->X(), nSrc->Y(), nSrc->Z() );
3030 nodes[ iN ] = (const SMDS_MeshNode*) n2n->second;
3034 if ( elem->GetType() != SMDSAbs_Node )
3036 elemType.Init( elem, /*basicOnly=*/false );
3037 if ( toKeepIDs ) elemType.SetID( elem->GetID() );
3039 const SMDS_MeshElement * newElem = editor.AddElement( nodes, elemType );
3040 if ( toCopyGroups && !toKeepIDs )
3041 e2eMapByType[ elem->GetType() ].insert( make_pair( elem, newElem ));
3043 } // while ( srcElemIt->more() )
3045 // 4(b). Copy free nodes
3047 if ( srcNodeIt && srcMeshDS->NbNodes() != newMeshDS->NbNodes() )
3049 while ( srcNodeIt->more() )
3051 nSrc = srcNodeIt->next();
3052 if ( nSrc->NbInverseElements() == 0 )
3055 nTgt = newMeshDS->AddNodeWithID( nSrc->X(), nSrc->Y(), nSrc->Z(), nSrc->GetID());
3057 n2nMap[ nSrc ] = newMeshDS->AddNode( nSrc->X(), nSrc->Y(), nSrc->Z() );
3064 int nbNewGroups = 0;
3067 SMESH_Mesh::GroupIteratorPtr gIt = srcMesh_i->GetImpl().GetGroups();
3068 while ( gIt->more() )
3070 SMESH_Group* group = gIt->next();
3071 const SMESHDS_GroupBase* groupDS = group->GetGroupDS();
3073 // Check group type. We copy nodal groups containing nodes of copied element
3074 SMDSAbs_ElementType groupType = groupDS->GetType();
3075 if ( groupType != SMDSAbs_Node &&
3076 newMeshDS->GetMeshInfo().NbElements( groupType ) == 0 )
3077 continue; // group type differs from types of meshPart
3079 // Find copied elements in the group
3080 vector< const SMDS_MeshElement* > groupElems;
3081 SMDS_ElemIteratorPtr eIt = groupDS->GetElements();
3084 const SMDS_MeshElement* foundElem;
3085 if ( groupType == SMDSAbs_Node )
3087 while ( eIt->more() )
3088 if (( foundElem = newMeshDS->FindNode( eIt->next()->GetID() )))
3089 groupElems.push_back( foundElem );
3093 while ( eIt->more() )
3094 if (( foundElem = newMeshDS->FindElement( eIt->next()->GetID() )))
3095 groupElems.push_back( foundElem );
3100 TE2EMap & e2eMap = e2eMapByType[ groupDS->GetType() ];
3101 if ( e2eMap.empty() ) continue;
3102 smIdType minID = e2eMap.begin()->first->GetID();
3103 smIdType maxID = e2eMap.rbegin()->first->GetID();
3104 TE2EMap::iterator e2e;
3105 while ( eIt->more() && groupElems.size() < e2eMap.size())
3107 const SMDS_MeshElement* e = eIt->next();
3108 if ( e->GetID() < minID || e->GetID() > maxID ) continue;
3109 if ((e2e = e2eMap.find( e )) != e2eMap.end())
3110 groupElems.push_back( e2e->second );
3114 if ( !groupElems.empty() )
3116 SMESH::SMESH_Group_var newGroupObj =
3117 newMesh->CreateGroup( SMESH::ElementType(groupType), group->GetName() );
3118 if ( SMESH_GroupBase_i* newGroup_i = SMESH::DownCast<SMESH_GroupBase_i*>( newGroupObj))
3120 SMESHDS_GroupBase * newGroupDS = newGroup_i->GetGroupDS();
3121 SMDS_MeshGroup& smdsGroup = ((SMESHDS_Group*)newGroupDS)->SMDSGroup();
3122 for ( unsigned i = 0; i < groupElems.size(); ++i )
3123 smdsGroup.Add( groupElems[i] );
3131 newMeshDS->Modified();
3133 *pyDump << newMesh << " = " << this
3134 << ".CopyMesh( " << meshPart << ", "
3135 << "'" << meshName << "', "
3136 << toCopyGroups << ", "
3137 << toKeepIDs << ")";
3139 pyDumpDeleter.reset(); // allow dump in GetGroups()
3141 if ( nbNewGroups > 0 ) // dump created groups
3142 SMESH::ListOfGroups_var groups = newMesh->GetGroups();
3144 return newMesh._retn();
3148 namespace // utils for CopyMeshWithGeom()
3150 typedef std::map< std::string, std::string > TStr2StrMap;
3151 typedef std::map< std::string, std::set< std::string > > TStr2StrSetMap;
3152 typedef std::map< std::set<int>, int > TIdSet2IndexMap;
3153 typedef std::map< std::string, int > TName2IndexMap;
3155 //================================================================================
3157 * \brief Return a new sub-shape corresponding to an old one
3159 //================================================================================
3163 SMESH_Mesh_i* mySrcMesh_i;
3164 SMESH_Mesh_i* myNewMesh_i;
3165 SMESH_Gen_i* myGen_i;
3169 TStr2StrMap myOld2NewEntryMap; // map of study entries
3171 GEOM::ListOfGO_var mySubshapes; // sub-shapes existing in the new geometry
3172 TIdSet2IndexMap myIds2SubshapeIndex; // to find an existing sub-shape
3173 TName2IndexMap myName2SubshapeIndex; // to find an existing sub-shape by name
3176 GEOM::ListOfListOfLong_var myGIPMap; // filled by GetInPlaceMap()
3178 // not directly relating to shape search
3179 TStr2StrSetMap myInvalidMap; // blame shape -> invalid objects
3181 //================================================================================
3183 * \brief Constructor
3185 ShapeMapper( SMESH_Mesh_i* srcMesh_i,
3186 SMESH_Mesh_i* newMesh_i,
3187 SMESH_Gen_i* smeshGen_i )
3188 : mySrcMesh_i( srcMesh_i ),
3189 myNewMesh_i( newMesh_i ),
3190 myGen_i ( smeshGen_i ),
3191 myToPublish( smeshGen_i->IsEnablePublish() ),
3192 myGIPMapDone( false )
3194 // retrieve from the study shape mapping made thanks to
3195 // "Set presentation parameters and sub-shapes from arguments" option
3197 GEOM::GEOM_Object_var mainShapeNew = myNewMesh_i->GetShapeToMesh();
3198 GEOM::GEOM_Object_var mainShapeOld = mySrcMesh_i->GetShapeToMesh();
3199 SALOMEDS::SObject_wrap oldSO = myGen_i->ObjectToSObject( mainShapeOld );
3200 SALOMEDS::SObject_wrap newSO = myGen_i->ObjectToSObject( mainShapeNew );
3201 if ( newSO->_is_nil() )
3203 myToPublish = false;
3206 if (( myIsSameGeom = mainShapeNew->_is_equivalent( mainShapeOld )))
3208 CORBA::String_var oldEntry = oldSO->GetID();
3209 CORBA::String_var newEntry = newSO->GetID();
3210 myOld2NewEntryMap.insert( std::make_pair( std::string( oldEntry.in() ),
3211 std::string( newEntry.in() )));
3212 std::string newMainEntry = newEntry.in();
3214 SALOMEDS::Study_var study = myGen_i->getStudyServant();
3215 GEOM::GEOM_Gen_var geomGen = myGen_i->GetGeomEngine( mainShapeNew );
3216 GEOM::GEOM_IShapesOperations_wrap op = geomGen->GetIShapesOperations();
3217 mySubshapes = op->GetExistingSubObjects( mainShapeNew,
3218 /*groupsOnly=*/false );
3219 for ( CORBA::ULong i = 0; i < mySubshapes->length(); ++i )
3221 newSO = myGen_i->ObjectToSObject( mySubshapes[ i ]);
3222 SALOMEDS::ChildIterator_wrap anIter = study->NewChildIterator( newSO );
3223 bool refFound = false;
3224 for ( ; anIter->More(); anIter->Next() )
3226 SALOMEDS::SObject_wrap so = anIter->Value();
3227 if ( so->ReferencedObject( oldSO.inout() ))
3229 oldEntry = oldSO->GetID();
3230 newEntry = newSO->GetID();
3231 if (( refFound = ( newMainEntry != oldEntry.in() )))
3232 myOld2NewEntryMap.insert( std::make_pair( std::string( oldEntry.in() ),
3233 std::string( newEntry.in() )));
3238 GEOM::GEOM_Object_var father = mySubshapes[ i ]->GetMainShape();
3239 if ( father->_is_equivalent( mainShapeNew ))
3241 GEOM::ListOfLong_var ids = mySubshapes[ i ]->GetSubShapeIndices();
3242 std::set< int > idSet( &ids[0] , &ids[0] + ids->length() );
3243 myIds2SubshapeIndex.insert( std::make_pair( idSet, i ));
3244 CORBA::String_var name = newSO->GetName();
3246 myName2SubshapeIndex.insert( std::make_pair( name.in(), i ));
3252 //================================================================================
3254 * \brief Find a new sub-shape corresponding to an old one
3256 GEOM::GEOM_Object_ptr FindNew( GEOM::GEOM_Object_ptr oldShape )
3259 return GEOM::GEOM_Object::_duplicate( oldShape );
3261 GEOM::GEOM_Object_var newShape;
3263 if ( CORBA::is_nil( oldShape ))
3264 return newShape._retn();
3266 if ( !isChildOfOld( oldShape ))
3267 return GEOM::GEOM_Object::_duplicate( oldShape ); // shape independent of the old shape
3269 GEOM::GEOM_Object_var mainShapeNew = myNewMesh_i->GetShapeToMesh();
3270 GEOM::GEOM_Gen_var geomGen = myGen_i->GetGeomEngine( mainShapeNew );
3272 // try to find by entry or name
3275 CORBA::String_var oldEntry = oldShape->GetStudyEntry();
3276 TStr2StrMap::iterator o2nID = myOld2NewEntryMap.find( oldEntry.in() );
3277 if ( o2nID != myOld2NewEntryMap.end() )
3279 newShape = getShapeByEntry( o2nID->second );
3281 if ( newShape->_is_nil() )
3283 CORBA::String_var name = oldShape->GetName();
3284 TName2IndexMap::iterator n2ind = myName2SubshapeIndex.find( name.in() );
3285 if ( n2ind != myName2SubshapeIndex.end() )
3287 newShape = GEOM::GEOM_Object::_duplicate( mySubshapes[ n2ind->second ]);
3288 GEOM::ListOfLong_var oldIndices = oldShape->GetSubShapeIndices();
3289 GEOM::ListOfLong_var newIndices = newShape->GetSubShapeIndices();
3290 if ( oldIndices->length() == 0 ||
3291 newIndices->length() == 0 ||
3292 getShapeType( myNewMesh_i, newIndices[0] ) !=
3293 getShapeType( mySrcMesh_i, oldIndices[0] ))
3294 newShape = GEOM::GEOM_Object::_nil();
3299 if ( newShape->_is_nil() )
3301 // try to construct a new sub-shape using myGIPMap
3303 std::vector< int > newIndices;
3304 GEOM::ListOfLong_var oldIndices = oldShape->GetSubShapeIndices();
3305 for ( CORBA::ULong i = 0; i < oldIndices->length(); ++i )
3307 findNewIDs( oldIndices[i], newIndices );
3309 if ( newIndices.size() < oldIndices->length() ) // issue #17096
3312 newShape = getInPlace( oldShape );
3314 if ( !newIndices.empty() && newShape->_is_nil() )
3316 // search for a sub-shape with same ids
3317 std::set< int > idSet( newIndices.begin(), newIndices.end() );
3318 TIdSet2IndexMap::iterator ids2ind = myIds2SubshapeIndex.find( idSet );
3319 if ( ids2ind != myIds2SubshapeIndex.end() ) {
3320 newShape = GEOM::GEOM_Object::_duplicate( mySubshapes[ ids2ind->second ]);
3322 if ( newShape->_is_nil() )
3325 // create a new shape
3326 if ( newIndices.size() > 1 || oldShape->GetType() == GEOM_GROUP )
3328 int groupType = getShapeType( myNewMesh_i, newIndices[0] );
3330 GEOM::GEOM_IGroupOperations_wrap grOp = geomGen->GetIGroupOperations();
3331 newShape = grOp->CreateGroup( mainShapeNew, groupType );
3333 GEOM::ListOfLong_var newIndicesList = new GEOM::ListOfLong();
3334 newIndicesList->length( newIndices.size() );
3335 for ( size_t i = 0; i < newIndices.size(); ++i )
3336 newIndicesList[ i ] = newIndices[ i ];
3337 grOp->UnionIDs( newShape, newIndicesList );
3341 GEOM::GEOM_IShapesOperations_wrap shOp = geomGen->GetIShapesOperations();
3342 newShape = shOp->GetSubShape( mainShapeNew, newIndices[0] );
3351 if ( !newShape->_is_nil() && myToPublish )
3353 CORBA::String_var oldEntry, newEntry = newShape->GetStudyEntry();
3354 if ( !newEntry.in() || !newEntry.in()[0] )
3356 CORBA::String_var name = oldShape->GetName();
3357 SALOMEDS::SObject_wrap so = geomGen->AddInStudy( newShape, name, mainShapeNew );
3358 newEntry = newShape->GetStudyEntry();
3359 oldEntry = oldShape->GetStudyEntry();
3360 myOld2NewEntryMap.insert( std::make_pair( std::string( oldEntry.in() ),
3361 std::string( newEntry.in() )));
3365 return newShape._retn();
3368 //================================================================================
3370 * \brief Return a study entry of a new shape by study entry of the old one
3372 std::string FindNew( const std::string & oldEntry )
3377 TStr2StrMap::iterator o2nID = myOld2NewEntryMap.find( oldEntry );
3378 if ( o2nID != myOld2NewEntryMap.end() )
3379 return o2nID->second;
3381 GEOM::GEOM_Object_var oldShape = getShapeByEntry( oldEntry );
3382 if ( oldShape->_is_nil() || !isChildOfOld( oldShape ))
3385 GEOM::GEOM_Object_ptr newShape = FindNew( oldShape );
3386 if ( newShape->_is_nil() )
3387 return std::string();
3389 CORBA::String_var newEntry = newShape->GetStudyEntry();
3390 return newEntry.in();
3393 //================================================================================
3395 * \brief Return a sub-shape ID of a new shape by a sub-shape ID of the old one.
3396 * Return zero if not found or there are more than one new ID
3398 int FindNew( int oldID )
3407 if ( 0 < oldID && oldID < (int)myGIPMap->length() )
3409 if ( myGIPMap[ oldID ].length() == 1 )
3411 newID = myGIPMap[ oldID ][ 0 ];
3413 else if ( myGIPMap[ oldID ].length() > 1 &&
3414 getShapeType( mySrcMesh_i, oldID ) == TopAbs_VERTEX )
3416 // select a meshed VERTEX
3417 SMESH_subMesh* newSM;
3418 for ( CORBA::ULong i = 0; i < myGIPMap[ oldID ].length() && !newID; ++i )
3419 if (( newSM = myNewMesh_i->GetImpl().GetSubMeshContaining( myGIPMap[ oldID ][ i ] )) &&
3420 ( !newSM->IsEmpty() ))
3421 newID = myGIPMap[ oldID ][ i ];
3427 //================================================================================
3429 * \brief Return a sub-shape ID of a new shape by an old sub-mesh.
3430 * Return zero if the old shape is not kept as is in the new shape.
3432 int FindNewNotChanged( SMESH_subMesh* oldSM )
3435 return oldSM->GetId();
3437 int newID = FindNew( oldSM->GetId() );
3441 SMESH_subMesh* newSM = myNewMesh_i->GetImpl().GetSubMeshContaining( newID );
3445 // consider a sub-shape as not changed if all its sub-shapes are mapped into
3446 // one new sub-shape of the same type.
3448 if ( oldSM->DependsOn().size() !=
3449 newSM->DependsOn().size() )
3452 SMESH_subMeshIteratorPtr srcSMIt = oldSM->getDependsOnIterator( /*includeSelf=*/true );
3453 while ( srcSMIt->more() )
3455 oldSM = srcSMIt->next();
3456 int newSubID = FindNew( oldSM->GetId() );
3457 if ( getShapeType( myNewMesh_i, newSubID ) !=
3458 getShapeType( mySrcMesh_i, oldSM->GetId() ))
3464 //================================================================================
3466 * \brief Return shape by study entry
3468 GEOM::GEOM_Object_ptr getShapeByEntry( const std::string & entry )
3470 GEOM::GEOM_Object_var shape;
3471 SALOMEDS::SObject_wrap so = myGen_i->getStudyServant()->FindObjectID( entry.c_str() );
3472 if ( !so->_is_nil() )
3474 CORBA::Object_var obj = so->GetObject();
3475 shape = GEOM::GEOM_Object::_narrow( obj );
3477 return shape._retn();
3480 //================================================================================
3482 * \brief Fill myGIPMap by calling GetInPlaceMap()
3486 if ( !myGIPMapDone )
3488 myGIPMapDone = true;
3490 GEOM::GEOM_Object_var mainShapeNew = myNewMesh_i->GetShapeToMesh();
3491 GEOM::GEOM_Object_var mainShapeOld = mySrcMesh_i->GetShapeToMesh();
3492 GEOM::GEOM_Gen_var geomGen = myGen_i->GetGeomEngine( mainShapeNew );
3493 GEOM::GEOM_IShapesOperations_wrap op = geomGen->GetIShapesOperations();
3496 myGIPMap = op->GetInPlaceMap( mainShapeNew, mainShapeOld );
3500 myGIPMap = new GEOM::ListOfListOfLong();
3505 //================================================================================
3507 * \brief Get new sub-shape by calling GetInPlace()
3509 GEOM::GEOM_Object_ptr getInPlace( GEOM::GEOM_Object_ptr oldShape )
3511 GEOM::GEOM_Object_var newShape;
3513 GEOM::GEOM_Object_var mainShapeNew = myNewMesh_i->GetShapeToMesh();
3514 GEOM::GEOM_Gen_var geomGen = myGen_i->GetGeomEngine( mainShapeNew );
3515 GEOM::GEOM_IShapesOperations_wrap op = geomGen->GetIShapesOperations();
3518 newShape = op->GetInPlace( mainShapeNew, oldShape );
3523 return newShape._retn();
3526 //================================================================================
3528 * \brief Find a new sub-shape indices by an old one in myGIPMap. Return
3529 * number of found IDs
3531 int findNewIDs( int oldID, std::vector< int >& newIDs )
3533 size_t prevNbIDs = newIDs.size();
3535 if ( 0 < oldID && oldID < (int) myGIPMap->length() )
3537 for ( CORBA::ULong i = 0; i < myGIPMap[ oldID ].length(); ++i )
3538 newIDs.push_back( myGIPMap[ oldID ][ i ]);
3540 return newIDs.size() - prevNbIDs;
3543 //================================================================================
3545 * \brief Check if an object relates to the old shape
3547 bool isChildOfOld( GEOM::GEOM_Object_ptr oldShape )
3549 if ( CORBA::is_nil( oldShape ))
3551 GEOM::GEOM_Object_var mainShapeOld1 = mySrcMesh_i->GetShapeToMesh();
3552 GEOM::GEOM_Object_var mainShapeOld2 = oldShape->GetMainShape();
3553 return ( mainShapeOld1->_is_equivalent( mainShapeOld2 ) ||
3554 mainShapeOld1->_is_equivalent( oldShape ));
3557 //================================================================================
3559 * \brief Return shape type by shape ID
3561 TopAbs_ShapeEnum getShapeType( SMESH_Mesh_i* mesh_i, int shapeID )
3563 SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
3564 const TopoDS_Shape& shape = meshDS->IndexToShape( shapeID );
3565 return shape.IsNull() ? TopAbs_SHAPE : shape.ShapeType();
3568 //================================================================================
3570 * \brief Store a source sub-shape for which a counterpart not found and
3571 * a smesh object invalid due to that
3573 void AddInvalid( GEOM::GEOM_Object_var srcShape,
3574 SALOMEDS::SObject_wrap smeshSO )
3576 CORBA::String_var geomEntry = srcShape->GetStudyEntry();
3577 if ( geomEntry.in()[0] && !smeshSO->_is_nil() )
3579 CORBA::String_var smeshEntry = smeshSO->GetID();
3580 myInvalidMap[ geomEntry.in() ].insert( smeshEntry.in() );
3584 //================================================================================
3586 * \brief Store a source sub-shape for which a counterpart not found and
3587 * a smesh object invalid due to that
3589 void AddInvalid( std::string geomEntry,
3590 SALOMEDS::SObject_wrap smeshSO )
3592 if ( !geomEntry.empty() )
3594 CORBA::String_var smeshEntry = smeshSO->GetID();
3595 myInvalidMap[ geomEntry ].insert( smeshEntry.in() );
3599 //================================================================================
3601 * \brief Store a source sub-shape for which a counterpart not found and
3602 * a smesh object invalid due to that
3604 void AddInvalid( int oldGeomID,
3605 SALOMEDS::SObject_wrap smeshSO )
3607 int shapeType = getShapeType( mySrcMesh_i, oldGeomID );
3608 if ( shapeType < 0 || shapeType > TopAbs_SHAPE )
3611 const char* typeName[] = { "COMPOUND","COMPSOLID","SOLID","SHELL",
3612 "FACE","WIRE","EDGE","VERTEX","SHAPE" };
3614 SMESH_Comment geomName( typeName[ shapeType ]);
3615 geomName << " #" << oldGeomID;
3617 CORBA::String_var smeshEntry = smeshSO->GetID();
3618 myInvalidMap[ geomName ].insert( smeshEntry.in() );
3621 //================================================================================
3623 * \brief Return entries of a source sub-shape for which a counterpart not found and
3624 * of smesh objects invalid due to that
3626 void GetInvalid( SMESH::string_array_out & theInvalidEntries,
3627 std::vector< SALOMEDS::SObject_wrap > & theInvalidMeshSObjects)
3630 TStr2StrSetMap::iterator entry2entrySet = myInvalidMap.begin();
3631 for ( ; entry2entrySet != myInvalidMap.end(); ++entry2entrySet )
3633 nbSO += 1 + entry2entrySet->second.size();
3635 int iSO = theInvalidMeshSObjects.size(), iEntry = 0;
3636 theInvalidEntries->length ( nbSO );
3637 theInvalidMeshSObjects.resize( theInvalidMeshSObjects.size() + nbSO - myInvalidMap.size() );
3639 entry2entrySet = myInvalidMap.begin();
3640 for ( ; entry2entrySet != myInvalidMap.end(); ++entry2entrySet )
3642 theInvalidEntries[ iEntry++ ] = entry2entrySet->first.c_str();
3644 std::set< std::string > & entrySet = entry2entrySet->second;
3645 std::set< std::string >::iterator entry = entrySet.begin();
3646 for ( ; entry != entrySet.end(); ++entry )
3648 theInvalidEntries[ iEntry++ ] = entry->c_str();
3650 SALOMEDS::SObject_wrap so = myGen_i->getStudyServant()->FindObjectID( entry->c_str() );
3651 if ( !so->_is_nil() )
3652 theInvalidMeshSObjects[ iSO++ ] = so;
3657 }; // struct ShapeMapper
3659 //================================================================================
3661 * \brief Append an item to a CORBA sequence
3663 template < class CORBA_seq, class ITEM >
3664 void append( CORBA_seq& seq, ITEM item )
3666 if ( !CORBA::is_nil( item ))
3668 seq->length( 1 + seq->length() );
3669 seq[ seq->length() - 1 ] = item;
3672 } // namespace // utils for CopyMeshWithGeom()
3674 //================================================================================
3676 * \brief Create a mesh by copying definitions of another mesh to a given geometry
3677 * \param [in] sourceMesh - a mesh to copy
3678 * \param [in] newGeometry - a new geometry
3679 * \param [in] toCopyGroups - to create groups in the new mesh
3680 * \param [in] toReuseHypotheses - if True, existing hypothesis will be used by the new mesh,
3681 * otherwise new hypotheses with the same parameters will be created for the new mesh.
3682 * \param [in] toCopyElements - to copy mesh elements of same sub-shapes of the two geometries
3683 * \param [out] newMesh - return a new mesh
3684 * \param [out] newGroups - return new groups
3685 * \param [out] newSubmeshes - return new sub-meshes
3686 * \param [out] newHypotheses - return new algorithms and hypotheses
3687 * \param [out] invalidEntries - return study entries of objects whose
3688 * counterparts are not found in the newGeometry, followed by entries
3689 * of mesh sub-objects that are invalid because they depend on a not found
3690 * preceding sub-shape
3691 * \return CORBA::Boolean - is a success
3693 //================================================================================
3695 CORBA::Boolean SMESH_Gen_i::CopyMeshWithGeom( SMESH::SMESH_Mesh_ptr theSourceMesh,
3696 GEOM::GEOM_Object_ptr theNewGeometry,
3697 const char* theMeshName,
3698 CORBA::Boolean theToCopyGroups,
3699 CORBA::Boolean theToReuseHypotheses,
3700 CORBA::Boolean theToCopyElements,
3701 SMESH::SMESH_Mesh_out theNewMesh,
3702 SMESH::ListOfGroups_out theNewGroups,
3703 SMESH::submesh_array_out theNewSubmeshes,
3704 SMESH::ListOfHypothesis_out theNewHypotheses,
3705 SMESH::string_array_out theInvalidEntries)
3707 if ( CORBA::is_nil( theSourceMesh ) ||
3708 CORBA::is_nil( theNewGeometry ))
3709 THROW_SALOME_CORBA_EXCEPTION( "NULL arguments", SALOME::BAD_PARAM );
3711 if ( !theSourceMesh->HasShapeToMesh() )
3712 THROW_SALOME_CORBA_EXCEPTION( "Source mesh not on geometry", SALOME::BAD_PARAM );
3717 TPythonDump pyDump(this); // prevent dump from CreateMesh()
3719 theNewMesh = CreateMesh( theNewGeometry );
3720 theNewGroups = new SMESH::ListOfGroups();
3721 theNewSubmeshes = new SMESH::submesh_array();
3722 theNewHypotheses = new SMESH::ListOfHypothesis();
3723 theInvalidEntries = new SMESH::string_array();
3725 std::vector< SALOMEDS::SObject_wrap > invalidSObjects;
3727 GEOM::GEOM_Object_var srcGeom = theSourceMesh->GetShapeToMesh();
3728 GEOM::GEOM_Object_var geom, newGeom;
3729 SALOMEDS::SObject_wrap so;
3731 SMESH_Mesh_i* srcMesh_i = SMESH::DownCast<SMESH_Mesh_i*>( theSourceMesh );
3732 SMESH_Mesh_i* newMesh_i = SMESH::DownCast<SMESH_Mesh_i*>( theNewMesh );
3735 ShapeMapper shapeMapper( srcMesh_i, newMesh_i, this );
3737 // treat hypotheses of mesh and sub-meshes
3738 SMESH::submesh_array_var smList = theSourceMesh->GetSubMeshes();
3739 for ( CORBA::ULong iSM = 0; iSM <= smList->length(); ++iSM )
3741 bool isSubMesh = ( iSM < smList->length() );
3744 // create a new sub-mesh
3745 SMESH::SMESH_subMesh_var newSM;
3746 geom = smList[iSM]->GetSubShape();
3747 so = ObjectToSObject( smList[iSM] );
3748 CORBA::String_var name;
3749 if ( !so->_is_nil() )
3750 name = so->GetName();
3751 newGeom = shapeMapper.FindNew( geom );
3752 if ( newGeom->_is_nil() )
3754 newSM = createInvalidSubMesh( theNewMesh, geom, name.in() );
3755 shapeMapper.AddInvalid( geom, ObjectToSObject( newSM ));
3760 newSM = theNewMesh->GetSubMesh( newGeom, name.in() );
3762 append( theNewSubmeshes, newSM );
3764 if ( newGeom->_is_nil() )
3765 continue; // don't assign hypotheses
3769 newGeom = GEOM::GEOM_Object::_duplicate( theNewGeometry );
3771 so = ObjectToSObject( theNewMesh );
3772 SetName( so, theMeshName, "Mesh" );
3775 // assign hypotheses
3776 SMESH::ListOfHypothesis_var hypList = theSourceMesh->GetHypothesisList( geom );
3777 for ( CORBA::ULong iHyp = 0; iHyp < hypList->length(); ++iHyp )
3779 SMESH::SMESH_Hypothesis_var hyp = hypList[ iHyp ];
3780 SMESH_Hypothesis_i* hyp_i = SMESH::DownCast< SMESH_Hypothesis_i* >( hyp );
3782 // get geometry hyp depends on
3783 std::vector< std::string > entryArray;
3784 std::vector< int > subIDArray;
3785 bool dependsOnGeom = hyp_i->getObjectsDependOn( entryArray, subIDArray );
3787 if ( !theToReuseHypotheses || dependsOnGeom )
3789 // create a new hypothesis
3790 CORBA::String_var type = hyp->GetName();
3791 CORBA::String_var lib = hyp->GetLibName();
3792 CORBA::String_var data = hyp_i->SaveTo();
3795 hyp = CreateHypothesis( type, lib );
3796 hyp_i = SMESH::DownCast< SMESH_Hypothesis_i* >( hyp );
3797 hyp_i->LoadFrom( data.in() );
3798 append( theNewHypotheses, hyp );
3802 // update geometry hyp depends on
3803 if ( dependsOnGeom )
3805 for ( size_t iGeo = 0; iGeo < entryArray.size(); ++iGeo )
3807 if ( !entryArray[ iGeo ].empty() )
3809 std::string newEntry = shapeMapper.FindNew( entryArray[ iGeo ]);
3810 if ( newEntry.empty() )
3813 shapeMapper.AddInvalid( entryArray[ iGeo ], ObjectToSObject( hyp ));
3814 shapeMapper.AddInvalid( entryArray[ iGeo ], so ); // sub-mesh
3816 entryArray[ iGeo ] = newEntry;
3819 for ( size_t iGeo = 0; iGeo < subIDArray.size(); ++iGeo )
3821 if ( subIDArray[ iGeo ] > 0 )
3823 int newID = shapeMapper.FindNew( subIDArray[ iGeo ]);
3827 shapeMapper.AddInvalid( subIDArray[ iGeo ], ObjectToSObject( hyp ));
3828 shapeMapper.AddInvalid( subIDArray[ iGeo ], so ); // sub-mesh
3830 subIDArray[ iGeo ] = newID;
3833 if ( !hyp_i->setObjectsDependOn( entryArray, subIDArray ))
3837 CORBA::String_var errorText;
3838 theNewMesh->AddHypothesis( newGeom, hyp, errorText.out() );
3839 if ( errorText.in()[0] )
3842 } // loop on hypotheses
3843 } // loop on sub-meshes and mesh
3846 // copy mesh elements, keeping IDs
3847 SMESHDS_Mesh* newMeshDS = newMesh_i->GetImpl().GetMeshDS();
3848 if ( theToCopyElements && theSourceMesh->NbNodes() > 0 )
3850 ::SMESH_MeshEditor editor( &newMesh_i->GetImpl() );
3851 ::SMESH_MeshEditor::ElemFeatures elemData;
3853 SMESH_subMesh* srcMainSM = srcMesh_i->GetImpl().GetSubMeshContaining( 1 );
3854 SMESH_subMeshIteratorPtr srcSMIt = srcMainSM->getDependsOnIterator( /*includeSelf=*/true,
3855 /*vertexLast=*/false);
3856 while ( srcSMIt->more() )
3858 SMESH_subMesh* srcSM = srcSMIt->next();
3859 if ( srcSM->IsEmpty() )
3860 continue; // not yet computed
3861 int newID = shapeMapper.FindNewNotChanged( srcSM );
3865 SMESHDS_SubMesh* srcSMDS = srcSM->GetSubMeshDS();
3866 SMDS_NodeIteratorPtr nIt = srcSMDS->GetNodes();
3867 while ( nIt->more() )
3869 SMESH_NodeXYZ node( nIt->next() );
3870 const SMDS_MeshNode* newNode = newMeshDS->AddNodeWithID( node.X(), node.Y(), node.Z(),
3872 const SMDS_PositionPtr pos = node->GetPosition();
3873 const double* uv = pos->GetParameters();
3874 switch ( pos->GetTypeOfPosition() )
3876 case SMDS_TOP_3DSPACE: newMeshDS->SetNodeInVolume( newNode, newID ); break;
3877 case SMDS_TOP_FACE: newMeshDS->SetNodeOnFace ( newNode, newID, uv[0], uv[1] ); break;
3878 case SMDS_TOP_EDGE: newMeshDS->SetNodeOnEdge ( newNode, newID, uv[0] ); break;
3879 case SMDS_TOP_VERTEX: newMeshDS->SetNodeOnVertex( newNode, newID ); break;
3883 SMDS_ElemIteratorPtr eIt = srcSMDS->GetElements();
3884 while( eIt->more() )
3886 const SMDS_MeshElement* e = eIt->next();
3887 elemData.Init( e, /*basicOnly=*/false );
3888 elemData.SetID( e->GetID() );
3889 elemData.myNodes.resize( e->NbNodes() );
3890 SMDS_NodeIteratorPtr nnIt = e->nodeIterator();
3892 for ( iN = 0; nnIt->more(); ++iN )
3894 const SMDS_MeshNode* srcNode = nnIt->next();
3895 elemData.myNodes[ iN ] = newMeshDS->FindNode( srcNode->GetID() );
3896 if ( !elemData.myNodes[ iN ])
3899 if ( iN == elemData.myNodes.size() )
3900 if ( const SMDS_MeshElement * newElem = editor.AddElement( elemData.myNodes, elemData ))
3901 newMeshDS->SetMeshElementOnShape( newElem, newID );
3903 if ( SMESH_subMesh* newSM = newMesh_i->GetImpl().GetSubMeshContaining( newID ))
3904 newSM->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
3907 newMeshDS->Modified();
3913 TStr2StrMap old2newGroupMap;
3915 SALOME::GenericObj_wrap< SMESH::FilterManager > filterMgr = CreateFilterManager();
3917 SMESH::ListOfGroups_var groups = theSourceMesh->GetGroups();
3918 CORBA::ULong nbGroups = theToCopyGroups ? groups->length() : 0, nbAddedGroups = 0;
3919 for ( CORBA::ULong i = 0; i < nbGroups + nbAddedGroups; ++i )
3921 SMESH::SMESH_Group_var stdlGroup = SMESH::SMESH_Group::_narrow ( groups[ i ]);
3922 SMESH::SMESH_GroupOnGeom_var geomGroup = SMESH::SMESH_GroupOnGeom::_narrow ( groups[ i ]);
3923 SMESH::SMESH_GroupOnFilter_var fltrGroup = SMESH::SMESH_GroupOnFilter::_narrow( groups[ i ]);
3925 CORBA::String_var name = groups[ i ]->GetName();
3926 SMESH::ElementType elemType = groups[ i ]->GetType();
3928 SMESH::SMESH_GroupBase_var newGroup;
3930 if ( !stdlGroup->_is_nil() )
3932 if ( newMeshDS->GetMeshInfo().NbElements( SMDSAbs_ElementType( elemType )) > 0 )
3934 SMESH::smIdType_array_var elemIDs = stdlGroup->GetIDs();
3935 const bool isElem = ( elemType != SMESH::NODE );
3936 CORBA::ULong iE = 0;
3937 for ( ; iE < elemIDs->length(); ++iE ) // check if any element has been copied
3938 if ( newMeshDS->GetElementType( elemIDs[ iE ], isElem ) != SMDSAbs_All )
3940 if ( iE < elemIDs->length() )
3942 stdlGroup = theNewMesh->CreateGroup( elemType, name );
3943 stdlGroup->Add( elemIDs );
3944 newGroup = SMESH::SMESH_GroupBase::_narrow( stdlGroup );
3948 else if ( !geomGroup->_is_nil() )
3950 GEOM::GEOM_Object_var geom = geomGroup->GetShape();
3951 GEOM::GEOM_Object_var newGeom = shapeMapper.FindNew( geom );
3952 if ( newGeom->_is_nil() )
3954 newGroup = theNewMesh->CreateGroup( elemType, name ); // just to notify the user
3955 shapeMapper.AddInvalid( geom, ObjectToSObject( newGroup ));
3960 newGroup = theNewMesh->CreateGroupFromGEOM( elemType, name, newGeom );
3963 else if ( !fltrGroup->_is_nil() )
3965 // replace geometry in a filter
3966 SMESH::Filter_var filter = fltrGroup->GetFilter();
3967 SMESH::Filter::Criteria_var criteria;
3968 filter->GetCriteria( criteria.out() );
3970 bool isMissingGroup = false;
3971 std::vector< std::string > badEntries;
3973 for ( CORBA::ULong iCr = 0; iCr < criteria->length(); ++iCr )
3975 const char* thresholdID = criteria[ iCr ].ThresholdID.in();
3976 switch ( criteria[ iCr ].Type )
3978 case SMESH::FT_BelongToMeshGroup:
3980 SALOME::GenericObj_wrap< SMESH::BelongToMeshGroup > btgg = filterMgr->CreateBelongToMeshGroup();
3981 btgg->SetGroupID( thresholdID );
3982 SMESH::SMESH_GroupBase_ptr refGroup = btgg->GetGroup();
3983 SALOMEDS::SObject_wrap refGroupSO = ObjectToSObject( refGroup );
3984 if ( refGroupSO->_is_nil() )
3986 CORBA::String_var refID = refGroupSO->GetID();
3987 TStr2StrMap::iterator o2nID = old2newGroupMap.find( refID.in() );
3988 if ( o2nID == old2newGroupMap.end() )
3990 isMissingGroup = true; // corresponding new group not yet created
3993 criteria[ iCr ].ThresholdID = o2nID->second.c_str();
3995 if ( o2nID->second.empty() ) // new referred group is invalid
3996 badEntries.push_back( refID.in() );
3999 case SMESH::FT_BelongToGeom:
4000 case SMESH::FT_BelongToPlane:
4001 case SMESH::FT_BelongToCylinder:
4002 case SMESH::FT_BelongToGenSurface:
4003 case SMESH::FT_LyingOnGeom:
4005 std::string newID = shapeMapper.FindNew( thresholdID );
4006 criteria[ iCr ].ThresholdID = newID.c_str();
4007 if ( newID.empty() )
4008 badEntries.push_back( thresholdID );
4011 case SMESH::FT_ConnectedElements:
4013 if ( thresholdID && thresholdID[0] )
4015 std::string newID = shapeMapper.FindNew( thresholdID );
4016 criteria[ iCr ].ThresholdID = newID.c_str();
4017 if ( newID.empty() )
4018 badEntries.push_back( thresholdID );
4024 } // loop on criteria
4026 if ( isMissingGroup && i < nbGroups )
4028 // to treat the group again
4029 append( groups, SMESH::SMESH_GroupBase::_duplicate( groups[ i ]));
4033 SMESH::Filter_var newFilter = filterMgr->CreateFilter();
4034 newFilter->SetCriteria( criteria );
4036 newGroup = theNewMesh->CreateGroupFromFilter( elemType, name, newFilter );
4037 newFilter->UnRegister();
4039 SALOMEDS::SObject_wrap newSO = ObjectToSObject( newGroup );
4040 for ( size_t iEnt = 0; iEnt < badEntries.size(); ++iEnt )
4041 shapeMapper.AddInvalid( badEntries[ iEnt ], newSO );
4043 if ( isMissingGroup ) // all groups treated but a referred groups still not found
4045 invalidSObjects.push_back( ObjectToSObject( newGroup ));
4048 if ( !badEntries.empty() )
4051 } // treat a group on filter
4053 append( theNewGroups, newGroup );
4055 // fill old2newGroupMap
4056 SALOMEDS::SObject_wrap srcSO = ObjectToSObject( groups[i] );
4057 SALOMEDS::SObject_wrap newSO = ObjectToSObject( newGroup );
4058 if ( !srcSO->_is_nil() )
4060 CORBA::String_var srcID, newID("");
4061 srcID = srcSO->GetID();
4062 if ( !newSO->_is_nil() )
4063 newID = newSO->GetID();
4064 old2newGroupMap.insert( std::make_pair( std::string( srcID.in() ),
4065 std::string( newID.in() )));
4068 if ( newGroup->_is_nil() )
4073 newMeshDS->CompactMesh();
4076 if ( !theMeshName || !theMeshName[0] )
4078 SALOMEDS::SObject_wrap soNew = ObjectToSObject( theNewMesh );
4079 SALOMEDS::SObject_wrap soOld = ObjectToSObject( theSourceMesh );
4080 CORBA::String_var oldName = soOld->GetName();
4081 SetName( soNew, oldName.in(), "Mesh" );
4083 // mark invalid objects
4084 shapeMapper.GetInvalid( theInvalidEntries, invalidSObjects );
4086 for ( size_t i = 0; i < invalidSObjects.size(); ++i )
4087 highLightInvalid( invalidSObjects[i].in(), true );
4090 << theNewMesh << ", "
4091 << theNewGroups << ", "
4092 << *theNewSubmeshes.ptr() << ", "
4093 << *theNewHypotheses.ptr() << ", "
4094 << "invalidEntries = " << this << ".CopyMeshWithGeom( "
4095 << theSourceMesh << ", "
4096 << theNewGeometry << ", "
4097 << "'" << theMeshName << "', "
4098 << theToCopyGroups << ", "
4099 << theToReuseHypotheses << ", "
4100 << theToCopyElements << " )";
4102 SMESH_CATCH( SMESH::throwCorbaException );
4107 //================================================================================
4109 * \brief Get version of MED format being used.
4111 //================================================================================
4113 char* SMESH_Gen_i::GetMEDFileVersion()
4115 MED::TInt majeur, mineur, release;
4116 majeur = mineur = release = 0;
4117 MED::GetVersionRelease(majeur, mineur, release);
4118 std::ostringstream version;
4119 version << majeur << "." << mineur << "." << release;
4120 return CORBA::string_dup( version.str().c_str() );
4123 //================================================================================
4125 * SMESH_Gen_i::GetMEDVersion
4127 * Get MED version of the file by its name
4129 //================================================================================
4131 char* SMESH_Gen_i::GetMEDVersion(const char* theFileName)
4133 std::string version = MED::GetMEDVersion( theFileName );
4134 return CORBA::string_dup( version.c_str() );
4137 //================================================================================
4139 * SMESH_Gen_i::CheckCompatibility
4141 * Check compatibility of file with MED format being used, read only.
4143 //================================================================================
4145 CORBA::Boolean SMESH_Gen_i::CheckCompatibility(const char* theFileName)
4147 return MED::CheckCompatibility( theFileName );
4150 //================================================================================
4152 * SMESH_Gen_i::CheckWriteCompatibility
4154 * Check compatibility of file with MED format being used, for append on write.
4156 //================================================================================
4158 CORBA::Boolean SMESH_Gen_i::CheckWriteCompatibility(const char* theFileName)
4160 return MED::CheckCompatibility( theFileName, true );
4163 //================================================================================
4165 * SMESH_Gen_i::GetMeshNames
4167 * Get names of meshes defined in file with the specified name
4169 //================================================================================
4170 SMESH::string_array* SMESH_Gen_i::GetMeshNames(const char* theFileName)
4173 SMESH::string_array_var aResult = new SMESH::string_array();
4174 MED::PWrapper aMed = MED::CrWrapperR( theFileName );
4176 MED::TInt aNbMeshes = aMed->GetNbMeshes( &anErr );
4178 aResult->length( aNbMeshes );
4179 for( MED::TInt i = 0; i < aNbMeshes; i++ ) {
4180 MED::PMeshInfo aMeshInfo = aMed->GetPMeshInfo( i+1 );
4181 aResult[i] = CORBA::string_dup( aMeshInfo->GetName().c_str() );
4184 return aResult._retn();
4187 //=============================================================================
4191 * Save SMESH module's data
4193 //=============================================================================
4195 SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
4200 Kernel_Utils::Localizer loc;
4202 if (!myStudyContext)
4205 // Store study contents as a set of python commands
4208 SALOMEDS::Study_var aStudy = getStudyServant();
4210 // Declare a byte stream
4211 SALOMEDS::TMPFile_var aStreamFile;
4213 // Obtain a temporary dir
4214 TCollection_AsciiString tmpDir =
4215 ( isMultiFile ) ? TCollection_AsciiString( ( char* )theURL ) : ( char* )SALOMEDS_Tool::GetTmpDir().c_str();
4217 // Create a sequence of files processed
4218 SALOMEDS_Tool::ListOfFiles aFileSeq;
4219 aFileSeq.reserve( NUM_TMP_FILES );
4221 TCollection_AsciiString aStudyName( "" );
4223 aStudyName = ( (char*)SALOMEDS_Tool::GetNameFromPath( Kernel_Utils::encode(aStudy->URL()) ).c_str() );
4225 // Set names of temporary files
4226 TCollection_AsciiString filename =
4227 aStudyName + TCollection_AsciiString( "_SMESH.hdf" ); // for SMESH data itself
4228 TCollection_AsciiString meshfile =
4229 aStudyName + TCollection_AsciiString( "_SMESH_Mesh.med" ); // for mesh data to be stored in MED file
4230 aFileSeq.push_back(CORBA::string_dup( filename.ToCString() ));
4231 aFileSeq.push_back(CORBA::string_dup( meshfile.ToCString() ));
4232 filename = tmpDir + filename;
4233 meshfile = tmpDir + meshfile;
4236 HDFdataset* aDataset;
4237 HDFgroup* aTopGroup;
4239 HDFgroup* aSubGroup;
4240 HDFgroup* aSubSubGroup;
4241 hdf_size aSize[ 1 ];
4244 //Remove the files if they exist: BugID: 11225
4245 #ifndef WIN32 /* unix functionality */
4246 TCollection_AsciiString cmd("rm -f \"");
4248 TCollection_AsciiString cmd("del /F \"");
4258 system(cmd.ToCString());
4260 // MED writer to be used by storage process
4261 DriverMED_W_SMESHDS_Mesh writer;
4262 writer.SetFile( meshfile.ToCString() );
4263 //writer.SetSaveNumbers( false ); // bos #24400 -- it leads to change of element IDs
4266 // SetStoreName() to groups before storing hypotheses to let them refer to
4267 // groups using "store name", which is "Group <group_persistent_id>"
4269 SALOMEDS::ChildIterator_wrap itBig = aStudy->NewChildIterator( theComponent );
4270 for ( ; itBig->More(); itBig->Next() ) {
4271 SALOMEDS::SObject_wrap gotBranch = itBig->Value();
4272 if ( gotBranch->Tag() > GetAlgorithmsRootTag() ) {
4273 CORBA::Object_var anObject = SObjectToObject( gotBranch );
4274 if ( !CORBA::is_nil( anObject ) ) {
4275 SMESH::SMESH_Mesh_var myMesh = SMESH::SMESH_Mesh::_narrow( anObject ) ;
4276 if ( !myMesh->_is_nil() ) {
4277 myMesh->Load(); // load from study file if not yet done
4278 TPythonDump pd(this); // not to dump GetGroups()
4279 SMESH::ListOfGroups_var groups = myMesh->GetGroups();
4280 for ( CORBA::ULong i = 0; i < groups->length(); ++i )
4282 SMESH_GroupBase_i* grImpl = SMESH::DownCast<SMESH_GroupBase_i*>( groups[i]);
4285 CORBA::String_var objStr = GetORB()->object_to_string( grImpl->_this() );
4286 int anId = myStudyContext->findId( string( objStr.in() ) );
4288 sprintf( grpName, "Group %d %d", anId, grImpl->GetLocalID() );
4289 SMESHDS_GroupBase* aGrpBaseDS = grImpl->GetGroupDS();
4290 aGrpBaseDS->SetStoreName( grpName );
4300 // ---> create HDF file
4301 aFile = new HDFfile( (char*) filename.ToCString() );
4302 aFile->CreateOnDisk();
4304 // --> iterator for top-level objects
4305 SALOMEDS::ChildIterator_wrap itBig = aStudy->NewChildIterator( theComponent );
4306 for ( ; itBig->More(); itBig->Next() ) {
4307 SALOMEDS::SObject_wrap gotBranch = itBig->Value();
4309 // --> hypotheses root branch (only one for the study)
4310 if ( gotBranch->Tag() == GetHypothesisRootTag() ) {
4311 // create hypotheses root HDF group
4312 aTopGroup = new HDFgroup( "Hypotheses", aFile );
4313 aTopGroup->CreateOnDisk();
4315 // iterator for all hypotheses
4316 SALOMEDS::ChildIterator_wrap it = aStudy->NewChildIterator( gotBranch );
4317 for ( ; it->More(); it->Next() ) {
4318 SALOMEDS::SObject_wrap mySObject = it->Value();
4319 CORBA::Object_var anObject = SObjectToObject( mySObject );
4320 if ( !CORBA::is_nil( anObject ) ) {
4321 SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
4322 if ( !myHyp->_is_nil() ) {
4323 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
4325 CORBA::String_var hn = myHyp->GetName(), ln = myHyp->GetLibName();
4326 std::string hypname = hn.in();
4327 std::string libname = ln.in();
4329 // Needs for save crossplatform libname, i.e. parth of name ( ".dll" for
4330 // WIN32 and ".so" for X-system) must be deleted
4331 int libname_len = libname.length();
4333 if( libname_len > 4 )
4334 libname.resize( libname_len - 4 );
4336 // PAL17753 (Regression: missing hypothesis in restored study)
4337 // "lib" also should be removed from the beginning
4338 if( libname_len > 6 )
4339 libname = libname.substr( 3, libname_len - 3 - 3 );
4341 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
4342 CORBA::String_var hypdata = myImpl->SaveTo();
4343 int id = myStudyContext->findId( string( objStr.in() ));
4345 // for each hypothesis create HDF group basing on its id
4346 char hypGrpName[30];
4347 sprintf( hypGrpName, "Hypothesis %d", id );
4348 aGroup = new HDFgroup( hypGrpName, aTopGroup );
4349 aGroup->CreateOnDisk();
4350 // --> type name of hypothesis
4351 aSize[ 0 ] = hypname.length() + 1;
4352 aDataset = new HDFdataset( "Name", aGroup, HDF_STRING, aSize, 1 );
4353 aDataset->CreateOnDisk();
4354 aDataset->WriteOnDisk( ( char* )( hypname.c_str() ) );
4355 aDataset->CloseOnDisk();
4356 // --> server plugin library name of hypothesis
4357 aSize[ 0 ] = libname.length() + 1;
4358 aDataset = new HDFdataset( "LibName", aGroup, HDF_STRING, aSize, 1 );
4359 aDataset->CreateOnDisk();
4360 aDataset->WriteOnDisk( ( char* )( libname.c_str() ) );
4361 aDataset->CloseOnDisk();
4362 // --> persistent data of hypothesis
4363 aSize[ 0 ] = strlen( hypdata.in() ) + 1;
4364 aDataset = new HDFdataset( "Data", aGroup, HDF_STRING, aSize, 1 );
4365 aDataset->CreateOnDisk();
4366 aDataset->WriteOnDisk( ( char* )( hypdata.in() ) );
4367 aDataset->CloseOnDisk();
4368 // close hypothesis HDF group
4369 aGroup->CloseOnDisk();
4374 // close hypotheses root HDF group
4375 aTopGroup->CloseOnDisk();
4377 // --> algorithms root branch (only one for the study)
4378 else if ( gotBranch->Tag() == GetAlgorithmsRootTag() ) {
4379 // create algorithms root HDF group
4380 aTopGroup = new HDFgroup( "Algorithms", aFile );
4381 aTopGroup->CreateOnDisk();
4383 // iterator for all algorithms
4384 SALOMEDS::ChildIterator_wrap it = aStudy->NewChildIterator( gotBranch );
4385 for ( ; it->More(); it->Next() ) {
4386 SALOMEDS::SObject_wrap mySObject = it->Value();
4387 CORBA::Object_var anObject = SObjectToObject( mySObject );
4388 if ( !CORBA::is_nil( anObject ) ) {
4389 SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
4390 if ( !myHyp->_is_nil() ) {
4391 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
4393 CORBA::String_var hn = myHyp->GetName(), ln = myHyp->GetLibName();
4394 std::string hypname = hn.in();
4395 std::string libname = ln.in();
4397 // Needs for save crossplatform libname, i.e. parth of name ( ".dll" for
4398 // WIN32 and ".so" for X-system) must be deleted
4399 int libname_len = libname.length();
4401 if( libname_len > 4 )
4402 libname.resize( libname_len - 4 );
4404 // PAL17753 (Regression: missing hypothesis in restored study)
4405 // "lib" also should be removed from the beginning
4406 if( libname_len > 6 )
4407 libname = libname.substr( 3, libname_len - 3 - 3 );
4409 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
4410 CORBA::String_var hypdata = myImpl->SaveTo();
4411 int id = myStudyContext->findId( string( objStr.in() ) );
4413 // for each algorithm create HDF group basing on its id
4414 char hypGrpName[30];
4415 sprintf( hypGrpName, "Algorithm %d", id );
4416 aGroup = new HDFgroup( hypGrpName, aTopGroup );
4417 aGroup->CreateOnDisk();
4418 // --> type name of algorithm
4419 aSize[0] = hypname.length() + 1;
4420 aDataset = new HDFdataset( "Name", aGroup, HDF_STRING, aSize, 1 );
4421 aDataset->CreateOnDisk();
4422 aDataset->WriteOnDisk( ( char* )( hypname.c_str() ) );
4423 aDataset->CloseOnDisk();
4424 // --> server plugin library name of hypothesis
4425 aSize[0] = libname.length() + 1;
4426 aDataset = new HDFdataset( "LibName", aGroup, HDF_STRING, aSize, 1 );
4427 aDataset->CreateOnDisk();
4428 aDataset->WriteOnDisk( ( char* )( libname.c_str() ) );
4429 aDataset->CloseOnDisk();
4430 // --> persistent data of algorithm
4431 aSize[0] = strlen( hypdata.in() ) + 1;
4432 aDataset = new HDFdataset( "Data", aGroup, HDF_STRING, aSize, 1 );
4433 aDataset->CreateOnDisk();
4434 aDataset->WriteOnDisk( ( char* )( hypdata.in() ));
4435 aDataset->CloseOnDisk();
4436 // close algorithm HDF group
4437 aGroup->CloseOnDisk();
4442 // close algorithms root HDF group
4443 aTopGroup->CloseOnDisk();
4445 // --> mesh objects roots branches
4446 else if ( gotBranch->Tag() > GetAlgorithmsRootTag() ) {
4447 CORBA::Object_var anObject = SObjectToObject( gotBranch );
4448 if ( !CORBA::is_nil( anObject ) ) {
4449 SMESH::SMESH_Mesh_var myMesh = SMESH::SMESH_Mesh::_narrow( anObject ) ;
4450 if ( !myMesh->_is_nil() ) {
4451 SMESH_Mesh_i* myImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( myMesh ).in() );
4453 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
4454 int id = myStudyContext->findId( string( objStr.in() ) );
4455 ::SMESH_Mesh& myLocMesh = myImpl->GetImpl();
4456 SMESHDS_Mesh* mySMESHDSMesh = myLocMesh.GetMeshDS();
4457 bool hasShape = myLocMesh.HasShapeToMesh();
4459 // for each mesh open the HDF group basing on its id
4460 char meshGrpName[ 30 ];
4461 sprintf( meshGrpName, "Mesh %d", id );
4462 aTopGroup = new HDFgroup( meshGrpName, aFile );
4463 aTopGroup->CreateOnDisk();
4465 // --> put dataset to hdf file which is a flag that mesh has data
4466 string strHasData = "0";
4467 // check if the mesh is not empty
4468 if ( mySMESHDSMesh->NbNodes() > 0 ) {
4469 // write mesh data to med file
4470 writer.SetMesh( mySMESHDSMesh );
4471 writer.SetMeshId( id );
4474 aSize[ 0 ] = strHasData.length() + 1;
4475 aDataset = new HDFdataset( "Has data", aTopGroup, HDF_STRING, aSize, 1 );
4476 aDataset->CreateOnDisk();
4477 aDataset->WriteOnDisk( ( char* )( strHasData.c_str() ) );
4478 aDataset->CloseOnDisk();
4481 // for each mesh open the HDF group basing on its auto color parameter
4482 char meshAutoColorName[ 30 ];
4483 sprintf( meshAutoColorName, "AutoColorMesh %d", id );
4485 anAutoColor[0] = myImpl->GetAutoColor();
4487 aDataset = new HDFdataset( meshAutoColorName, aTopGroup, HDF_INT32, aSize, 1 );
4488 aDataset->CreateOnDisk();
4489 aDataset->WriteOnDisk( anAutoColor );
4490 aDataset->CloseOnDisk();
4492 // issue 0020693. Store _isModified flag
4493 int isModified = myLocMesh.GetIsModified();
4495 aDataset = new HDFdataset( "_isModified", aTopGroup, HDF_INT32, aSize, 1 );
4496 aDataset->CreateOnDisk();
4497 aDataset->WriteOnDisk( &isModified );
4498 aDataset->CloseOnDisk();
4500 // issue 20918. Store Persistent Id of SMESHDS_Mesh
4501 int meshPersistentId = mySMESHDSMesh->GetPersistentId();
4503 aDataset = new HDFdataset( "meshPersistentId", aTopGroup, HDF_INT32, aSize, 1 );
4504 aDataset->CreateOnDisk();
4505 aDataset->WriteOnDisk( &meshPersistentId );
4506 aDataset->CloseOnDisk();
4508 // Store SMESH_Mesh_i::_mainShapeTick
4509 int shapeTick = myImpl->MainShapeTick();
4511 aDataset = new HDFdataset( "shapeTick", aTopGroup, HDF_INT32, aSize, 1 );
4512 aDataset->CreateOnDisk();
4513 aDataset->WriteOnDisk( &shapeTick );
4514 aDataset->CloseOnDisk();
4516 // write reference on a shape if exists
4517 SALOMEDS::SObject_wrap myRef;
4518 bool shapeRefFound = false;
4519 bool found = gotBranch->FindSubObject( (CORBA::Long)GetRefOnShapeTag(), myRef.inout() );
4521 SALOMEDS::SObject_wrap myShape;
4522 bool ok = myRef->ReferencedObject( myShape.inout() );
4524 CORBA::Object_var shapeObj = myShape->GetObject();
4525 shapeRefFound = (! CORBA::is_nil( shapeObj ));
4526 CORBA::String_var myRefOnObject = myShape->GetID();
4527 if ( shapeRefFound && myRefOnObject.in()[0] ) {
4528 aSize[ 0 ] = strlen( myRefOnObject.in() ) + 1;
4529 aDataset = new HDFdataset( "Ref on shape", aTopGroup, HDF_STRING, aSize, 1 );
4530 aDataset->CreateOnDisk();
4531 aDataset->WriteOnDisk( ( char* )( myRefOnObject.in() ) );
4532 aDataset->CloseOnDisk();
4538 std::string info = myImpl->FileInfoToString();
4539 if ( !info.empty() )
4541 aSize[ 0 ] = info.size();
4542 aDataset = new HDFdataset( "file info", aTopGroup, HDF_STRING, aSize, 1 );
4543 aDataset->CreateOnDisk();
4544 aDataset->WriteOnDisk( (char*) info.data() );
4545 aDataset->CloseOnDisk();
4548 // write applied hypotheses if exist
4549 SALOMEDS::SObject_wrap myHypBranch;
4550 found = gotBranch->FindSubObject( (CORBA::Long)GetRefOnAppliedHypothesisTag(), myHypBranch.inout() );
4551 if ( found && !shapeRefFound && hasShape ) { // remove applied hyps
4552 aStudy->NewBuilder()->RemoveObjectWithChildren( myHypBranch );
4554 if ( found && (shapeRefFound || !hasShape) ) {
4555 aGroup = new HDFgroup( "Applied Hypotheses", aTopGroup );
4556 aGroup->CreateOnDisk();
4558 SALOMEDS::ChildIterator_wrap it = aStudy->NewChildIterator( myHypBranch );
4560 for ( ; it->More(); it->Next() ) {
4561 SALOMEDS::SObject_wrap mySObject = it->Value();
4562 SALOMEDS::SObject_wrap myRefOnHyp;
4563 bool ok = mySObject->ReferencedObject( myRefOnHyp.inout() );
4565 // san - it is impossible to recover applied hypotheses
4566 // using their entries within Load() method,
4567 // for there are no AttributeIORs in the study when Load() is working.
4568 // Hence, it is better to store persistent IDs of hypotheses as references to them
4570 //string myRefOnObject = myRefOnHyp->GetID();
4571 CORBA::Object_var anObject = SObjectToObject( myRefOnHyp );
4572 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
4573 int id = myStudyContext->findId( string( objStr.in() ) );
4574 //if ( myRefOnObject.length() > 0 ) {
4575 //aSize[ 0 ] = myRefOnObject.length() + 1;
4576 char hypName[ 30 ], hypId[ 30 ];
4577 sprintf( hypName, "Hyp %d", ++hypNb );
4578 sprintf( hypId, "%d", id );
4579 aSize[ 0 ] = strlen( hypId ) + 1;
4580 aDataset = new HDFdataset( hypName, aGroup, HDF_STRING, aSize, 1 );
4581 aDataset->CreateOnDisk();
4582 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
4583 aDataset->WriteOnDisk( hypId );
4584 aDataset->CloseOnDisk();
4588 aGroup->CloseOnDisk();
4591 // write applied algorithms if exist
4592 SALOMEDS::SObject_wrap myAlgoBranch;
4593 found = gotBranch->FindSubObject( GetRefOnAppliedAlgorithmsTag(),
4594 myAlgoBranch.inout() );
4595 if ( found && !shapeRefFound && hasShape) { // remove applied algos
4596 aStudy->NewBuilder()->RemoveObjectWithChildren( myAlgoBranch );
4598 if ( found && (shapeRefFound || !hasShape)) {
4599 aGroup = new HDFgroup( "Applied Algorithms", aTopGroup );
4600 aGroup->CreateOnDisk();
4602 SALOMEDS::ChildIterator_wrap it = aStudy->NewChildIterator( myAlgoBranch );
4604 for ( ; it->More(); it->Next() ) {
4605 SALOMEDS::SObject_wrap mySObject = it->Value();
4606 SALOMEDS::SObject_wrap myRefOnAlgo;
4607 bool ok = mySObject->ReferencedObject( myRefOnAlgo.inout() );
4609 // san - it is impossible to recover applied algorithms
4610 // using their entries within Load() method,
4611 // for there are no AttributeIORs in the study when Load() is working.
4612 // Hence, it is better to store persistent IDs of algorithms as references to them
4614 //string myRefOnObject = myRefOnAlgo->GetID();
4615 CORBA::Object_var anObject = SObjectToObject( myRefOnAlgo );
4616 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
4617 int id = myStudyContext->findId( string( objStr.in() ) );
4618 //if ( myRefOnObject.length() > 0 ) {
4619 //aSize[ 0 ] = myRefOnObject.length() + 1;
4620 char algoName[ 30 ], algoId[ 30 ];
4621 sprintf( algoName, "Algo %d", ++algoNb );
4622 sprintf( algoId, "%d", id );
4623 aSize[ 0 ] = strlen( algoId ) + 1;
4624 aDataset = new HDFdataset( algoName, aGroup, HDF_STRING, aSize, 1 );
4625 aDataset->CreateOnDisk();
4626 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
4627 aDataset->WriteOnDisk( algoId );
4628 aDataset->CloseOnDisk();
4632 aGroup->CloseOnDisk();
4635 // --> submesh objects sub-branches
4637 for ( int i = GetSubMeshOnVertexTag(); i <= GetSubMeshOnCompoundTag(); i++ ) {
4638 SALOMEDS::SObject_wrap mySubmeshBranch;
4639 found = gotBranch->FindSubObject( i, mySubmeshBranch.inout() );
4641 if ( found ) // check if there is shape reference in submeshes
4643 bool hasShapeRef = false;
4644 SALOMEDS::ChildIterator_wrap itSM =
4645 aStudy->NewChildIterator( mySubmeshBranch );
4646 for ( ; itSM->More(); itSM->Next() ) {
4647 SALOMEDS::SObject_wrap mySubRef, myShape, mySObject = itSM->Value();
4648 if ( mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef.inout() ))
4649 mySubRef->ReferencedObject( myShape.inout() );
4650 if ( !CORBA::is_nil( myShape ) && !CORBA::is_nil( myShape->GetObject() ))
4653 { // remove one submesh
4654 if ( shapeRefFound )
4655 { // unassign hypothesis
4656 SMESH::SMESH_subMesh_var mySubMesh =
4657 SMESH::SMESH_subMesh::_narrow( SObjectToObject( mySObject ));
4658 if ( !mySubMesh->_is_nil() ) {
4659 int shapeID = mySubMesh->GetId();
4660 TopoDS_Shape S = mySMESHDSMesh->IndexToShape( shapeID );
4661 const list<const SMESHDS_Hypothesis*>& hypList =
4662 mySMESHDSMesh->GetHypothesis( S );
4663 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
4664 while ( hyp != hypList.end() ) {
4665 int hypID = (*hyp++)->GetID(); // goto next hyp here because
4666 myLocMesh.RemoveHypothesis( S, hypID ); // hypList changes here
4670 aStudy->NewBuilder()->RemoveObjectWithChildren( mySObject );
4672 } // loop on submeshes of a type
4673 if ( !shapeRefFound || !hasShapeRef ) { // remove the whole submeshes branch
4674 aStudy->NewBuilder()->RemoveObjectWithChildren( mySubmeshBranch );
4677 } // end check if there is shape reference in submeshes
4679 char name_meshgroup[ 30 ];
4680 if ( i == GetSubMeshOnVertexTag() )
4681 strcpy( name_meshgroup, "SubMeshes On Vertex" );
4682 else if ( i == GetSubMeshOnEdgeTag() )
4683 strcpy( name_meshgroup, "SubMeshes On Edge" );
4684 else if ( i == GetSubMeshOnWireTag() )
4685 strcpy( name_meshgroup, "SubMeshes On Wire" );
4686 else if ( i == GetSubMeshOnFaceTag() )
4687 strcpy( name_meshgroup, "SubMeshes On Face" );
4688 else if ( i == GetSubMeshOnShellTag() )
4689 strcpy( name_meshgroup, "SubMeshes On Shell" );
4690 else if ( i == GetSubMeshOnSolidTag() )
4691 strcpy( name_meshgroup, "SubMeshes On Solid" );
4692 else if ( i == GetSubMeshOnCompoundTag() )
4693 strcpy( name_meshgroup, "SubMeshes On Compound" );
4695 // for each type of submeshes create container HDF group
4696 aGroup = new HDFgroup( name_meshgroup, aTopGroup );
4697 aGroup->CreateOnDisk();
4699 // iterator for all submeshes of given type
4700 SALOMEDS::ChildIterator_wrap itSM = aStudy->NewChildIterator( mySubmeshBranch );
4701 for ( ; itSM->More(); itSM->Next() ) {
4702 SALOMEDS::SObject_wrap mySObject = itSM->Value();
4703 CORBA::Object_var anSubObject = SObjectToObject( mySObject );
4704 if ( !CORBA::is_nil( anSubObject ))
4706 SMESH::SMESH_subMesh_var mySubMesh = SMESH::SMESH_subMesh::_narrow( anSubObject ) ;
4707 CORBA::String_var objStr = GetORB()->object_to_string( anSubObject );
4708 int subid = myStudyContext->findId( string( objStr.in() ) );
4710 // for each mesh open the HDF group basing on its id
4711 char submeshGrpName[ 30 ];
4712 sprintf( submeshGrpName, "SubMesh %d", subid );
4713 aSubGroup = new HDFgroup( submeshGrpName, aGroup );
4714 aSubGroup->CreateOnDisk();
4716 // write reference on a shape, already checked if it exists
4717 SALOMEDS::SObject_wrap mySubRef, myShape;
4718 if ( mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef.inout() ))
4719 mySubRef->ReferencedObject( myShape.inout() );
4720 string myRefOnObject = myShape->GetID();
4721 if ( myRefOnObject.length() > 0 ) {
4722 aSize[ 0 ] = myRefOnObject.length() + 1;
4723 aDataset = new HDFdataset( "Ref on shape", aSubGroup, HDF_STRING, aSize, 1 );
4724 aDataset->CreateOnDisk();
4725 aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
4726 aDataset->CloseOnDisk();
4729 // write applied hypotheses if exist
4730 SALOMEDS::SObject_wrap mySubHypBranch;
4731 found = mySObject->FindSubObject( GetRefOnAppliedHypothesisTag(),
4732 mySubHypBranch.inout() );
4734 aSubSubGroup = new HDFgroup( "Applied Hypotheses", aSubGroup );
4735 aSubSubGroup->CreateOnDisk();
4737 SALOMEDS::ChildIterator_wrap it = aStudy->NewChildIterator( mySubHypBranch );
4739 for ( ; it->More(); it->Next() ) {
4740 SALOMEDS::SObject_wrap mySubSObject = it->Value();
4741 SALOMEDS::SObject_wrap myRefOnHyp;
4742 bool ok = mySubSObject->ReferencedObject( myRefOnHyp.inout() );
4744 //string myRefOnObject = myRefOnHyp->GetID();
4745 CORBA::Object_var anObject = SObjectToObject( myRefOnHyp );
4746 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
4747 int id = myStudyContext->findId( string( objStr.in() ) );
4748 //if ( myRefOnObject.length() > 0 ) {
4749 //aSize[ 0 ] = myRefOnObject.length() + 1;
4750 char hypName[ 30 ], hypId[ 30 ];
4751 sprintf( hypName, "Hyp %d", ++hypNb );
4752 sprintf( hypId, "%d", id );
4753 aSize[ 0 ] = strlen( hypId ) + 1;
4754 aDataset = new HDFdataset( hypName, aSubSubGroup, HDF_STRING, aSize, 1 );
4755 aDataset->CreateOnDisk();
4756 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
4757 aDataset->WriteOnDisk( hypId );
4758 aDataset->CloseOnDisk();
4762 aSubSubGroup->CloseOnDisk();
4765 // write applied algorithms if exist
4766 SALOMEDS::SObject_wrap mySubAlgoBranch;
4767 found = mySObject->FindSubObject( GetRefOnAppliedAlgorithmsTag(),
4768 mySubAlgoBranch.inout() );
4770 aSubSubGroup = new HDFgroup( "Applied Algorithms", aSubGroup );
4771 aSubSubGroup->CreateOnDisk();
4773 SALOMEDS::ChildIterator_wrap it =
4774 aStudy->NewChildIterator( mySubAlgoBranch );
4776 for ( ; it->More(); it->Next() ) {
4777 SALOMEDS::SObject_wrap mySubSObject = it->Value();
4778 SALOMEDS::SObject_wrap myRefOnAlgo;
4779 bool ok = mySubSObject->ReferencedObject( myRefOnAlgo.inout() );
4781 //string myRefOnObject = myRefOnAlgo->GetID();
4782 CORBA::Object_var anObject = SObjectToObject( myRefOnAlgo );
4783 CORBA::String_var objStr = GetORB()->object_to_string( anObject );
4784 int id = myStudyContext->findId( string( objStr.in() ) );
4785 //if ( myRefOnObject.length() > 0 ) {
4786 //aSize[ 0 ] = myRefOnObject.length() + 1;
4787 char algoName[ 30 ], algoId[ 30 ];
4788 sprintf( algoName, "Algo %d", ++algoNb );
4789 sprintf( algoId, "%d", id );
4790 aSize[ 0 ] = strlen( algoId ) + 1;
4791 aDataset = new HDFdataset( algoName, aSubSubGroup, HDF_STRING, aSize, 1 );
4792 aDataset->CreateOnDisk();
4793 //aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
4794 aDataset->WriteOnDisk( algoId );
4795 aDataset->CloseOnDisk();
4799 aSubSubGroup->CloseOnDisk();
4801 // close submesh HDF group
4802 aSubGroup->CloseOnDisk();
4805 // close container of submeshes by type HDF group
4806 aGroup->CloseOnDisk();
4810 // store submesh order if any
4811 const TListOfListOfInt& theOrderIds = myLocMesh.GetMeshOrder();
4812 const bool isNewOrederVersion = true; // old version saves ids, new one, entries
4813 if ( !theOrderIds.empty() && !isNewOrederVersion ) { // keep old version for reference
4814 char order_list[ 30 ];
4815 strcpy( order_list, "Mesh Order" );
4816 // count number of submesh ids
4818 TListOfListOfInt::const_iterator idIt = theOrderIds.begin();
4819 for ( ; idIt != theOrderIds.end(); idIt++ )
4820 nbIDs += (*idIt).size();
4821 // number of values = number of IDs +
4822 // number of lists (for separators) - 1
4823 int* smIDs = new int [ nbIDs + theOrderIds.size() - 1 ];
4824 idIt = theOrderIds.begin();
4825 for ( int i = 0; idIt != theOrderIds.end(); idIt++ ) {
4826 const TListOfInt& idList = *idIt;
4827 if (idIt != theOrderIds.begin()) // not first list
4828 smIDs[ i++ ] = -1/* *idList.size()*/; // separator between lists
4829 // dump submesh ids from current list
4830 TListOfInt::const_iterator id_smId = idList.begin();
4831 for( ; id_smId != idList.end(); id_smId++ )
4832 smIDs[ i++ ] = *id_smId;
4835 aSize[ 0 ] = nbIDs + theOrderIds.size() - 1;
4837 aDataset = new HDFdataset( order_list, aTopGroup, HDF_INT32, aSize, 1 );
4838 aDataset->CreateOnDisk();
4839 aDataset->WriteOnDisk( smIDs );
4840 aDataset->CloseOnDisk();
4844 if ( !theOrderIds.empty() && isNewOrederVersion )
4846 // convert ids to entries
4847 std::list< std::list< std::string > > orderEntryLists;
4848 for ( const TListOfInt& idList : theOrderIds )
4850 orderEntryLists.emplace_back();
4851 std::list< std::string > & entryList = orderEntryLists.back();
4852 for ( const int& id : idList )
4854 const TopoDS_Shape& shape = mySMESHDSMesh->IndexToShape( id );
4855 GEOM::GEOM_Object_var go = ShapeToGeomObject( shape );
4856 SALOMEDS::SObject_var so = ObjectToSObject( go );
4857 if ( !so->_is_nil() )
4859 CORBA::String_var entry = so->GetID();
4860 entryList.emplace_back( entry.in() );
4864 // convert orderEntryLists to string
4865 std::ostringstream ostream;
4866 boost::archive::text_oarchive( ostream ) << orderEntryLists;
4867 std::string orderEntryString = ostream.str();
4870 aSize[ 0 ] = orderEntryString.size() + 1;
4871 aDataset = new HDFdataset( "MeshOrder_new", aTopGroup, HDF_STRING, aSize, 1 );
4872 aDataset->CreateOnDisk();
4873 aDataset->WriteOnDisk((char*) orderEntryString.data() );
4874 aDataset->CloseOnDisk();
4877 // groups root sub-branch
4878 SALOMEDS::SObject_wrap myGroupsBranch;
4879 for ( int i = GetNodeGroupsTag(); i <= GetBallElementsGroupsTag(); i++ ) {
4880 found = gotBranch->FindSubObject( i, myGroupsBranch.inout() );
4882 char name_group[ 30 ];
4883 if ( i == GetNodeGroupsTag() )
4884 strcpy( name_group, "Groups of Nodes" );
4885 else if ( i == GetEdgeGroupsTag() )
4886 strcpy( name_group, "Groups of Edges" );
4887 else if ( i == GetFaceGroupsTag() )
4888 strcpy( name_group, "Groups of Faces" );
4889 else if ( i == GetVolumeGroupsTag() )
4890 strcpy( name_group, "Groups of Volumes" );
4891 else if ( i == Get0DElementsGroupsTag() )
4892 strcpy( name_group, "Groups of 0D Elements" );
4893 else if ( i == GetBallElementsGroupsTag() )
4894 strcpy( name_group, "Groups of Balls" );
4896 aGroup = new HDFgroup( name_group, aTopGroup );
4897 aGroup->CreateOnDisk();
4899 SALOMEDS::ChildIterator_wrap it = aStudy->NewChildIterator( myGroupsBranch );
4900 for ( ; it->More(); it->Next() ) {
4901 SALOMEDS::SObject_wrap mySObject = it->Value();
4902 CORBA::Object_var aSubObject = SObjectToObject( mySObject );
4903 if ( !CORBA::is_nil( aSubObject ) ) {
4904 SMESH_GroupBase_i* myGroupImpl =
4905 dynamic_cast<SMESH_GroupBase_i*>( GetServant( aSubObject ).in() );
4908 SMESHDS_GroupBase* aGrpBaseDS = myGroupImpl->GetGroupDS();
4912 CORBA::String_var objStr = GetORB()->object_to_string( aSubObject );
4913 int anId = myStudyContext->findId( string( objStr.in() ) );
4915 // For each group, create a dataset named "Group <group_persistent_id>"
4916 // and store the group's user name into it
4917 const char* grpName = aGrpBaseDS->GetStoreName();
4918 CORBA::String_var aUserName = myGroupImpl->GetName();
4919 aSize[ 0 ] = strlen( aUserName ) + 1;
4921 aDataset = new HDFdataset( grpName, aGroup, HDF_STRING, aSize, 1 );
4922 aDataset->CreateOnDisk();
4923 aDataset->WriteOnDisk( aUserName );
4924 aDataset->CloseOnDisk();
4927 // For each group, create a dataset named "Group <group_persistent_id> Color"
4928 // and store the group's color into it
4929 char grpColorName[ 30 ];
4930 sprintf( grpColorName, "ColorGroup %d", anId );
4931 SALOMEDS::Color aColor = myGroupImpl->GetColor();
4933 anRGB[ 0 ] = aColor.R;
4934 anRGB[ 1 ] = aColor.G;
4935 anRGB[ 2 ] = aColor.B;
4937 aDataset = new HDFdataset( grpColorName, aGroup, HDF_FLOAT64, aSize, 1 );
4938 aDataset->CreateOnDisk();
4939 aDataset->WriteOnDisk( anRGB );
4940 aDataset->CloseOnDisk();
4942 // Pass SMESHDS_Group to MED writer
4943 SMESHDS_Group* aGrpDS = dynamic_cast<SMESHDS_Group*>( aGrpBaseDS );
4945 writer.AddGroup( aGrpDS );
4947 // write reference on a shape if exists
4948 SMESHDS_GroupOnGeom* aGeomGrp =
4949 dynamic_cast<SMESHDS_GroupOnGeom*>( aGrpBaseDS );
4951 SALOMEDS::SObject_wrap mySubRef, myShape;
4952 if (mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef.inout() ) &&
4953 mySubRef->ReferencedObject( myShape.inout() ) &&
4954 !CORBA::is_nil( myShape->GetObject() ))
4956 CORBA::String_var myRefOnObject = myShape->GetID();
4957 if ( myRefOnObject.in()[0] ) {
4958 char aRefName[ 30 ];
4959 sprintf( aRefName, "Ref on shape %d", anId);
4960 aSize[ 0 ] = strlen( myRefOnObject.in() ) + 1;
4961 aDataset = new HDFdataset(aRefName, aGroup, HDF_STRING, aSize, 1);
4962 aDataset->CreateOnDisk();
4963 aDataset->WriteOnDisk( ( char* )( myRefOnObject.in() ));
4964 aDataset->CloseOnDisk();
4967 else // shape ref is invalid:
4969 // save a group on geometry as ordinary group
4970 writer.AddGroup( aGeomGrp );
4973 else if ( SMESH_GroupOnFilter_i* aFilterGrp_i =
4974 dynamic_cast<SMESH_GroupOnFilter_i*>( myGroupImpl ))
4976 std::string str = aFilterGrp_i->FilterToString();
4977 std::string hdfGrpName = "Filter " + SMESH_Comment(anId);
4978 aSize[ 0 ] = str.length() + 1;
4979 aDataset = new HDFdataset( hdfGrpName.c_str(), aGroup, HDF_STRING, aSize, 1);
4980 aDataset->CreateOnDisk();
4981 aDataset->WriteOnDisk( ( char* )( str.c_str() ) );
4982 aDataset->CloseOnDisk();
4986 aGroup->CloseOnDisk();
4990 if ( strcmp( strHasData.c_str(), "1" ) == 0 )
4992 // Flush current mesh information into MED file
4995 // save info on nb of elements
4996 SMESH_PreMeshInfo::SaveToFile( myImpl, id, aFile );
4998 // maybe a shape was deleted in the study
4999 if ( !shapeRefFound && !mySMESHDSMesh->ShapeToMesh().IsNull() && hasShape) {
5000 TopoDS_Shape nullShape;
5001 myLocMesh.ShapeToMesh( nullShape ); // remove shape referring data
5004 SMESHDS_SubMeshIteratorPtr smIt = mySMESHDSMesh->SubMeshes();
5009 aGroup = new HDFgroup( "Submeshes", aTopGroup );
5010 aGroup->CreateOnDisk();
5012 // each element belongs to one or none submesh,
5013 // so for each node/element, we store a submesh ID
5015 // Store submesh IDs
5016 for ( int isNode = 0; isNode < 2; ++isNode )
5018 SMDS_ElemIteratorPtr eIt =
5019 mySMESHDSMesh->elementsIterator( isNode ? SMDSAbs_Node : SMDSAbs_All );
5020 smIdType nbElems = isNode ? mySMESHDSMesh->NbNodes() : mySMESHDSMesh->GetMeshInfo().NbElements();
5023 std::vector<int> smIDs; smIDs.reserve( nbElems );
5024 while ( eIt->more() )
5025 if ( const SMDS_MeshElement* e = eIt->next())
5026 smIDs.push_back( e->getshapeId() );
5028 aSize[ 0 ] = nbElems;
5029 string aDSName( isNode ? "Node Submeshes" : "Element Submeshes");
5030 aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_INT32, aSize, 1 );
5031 aDataset->CreateOnDisk();
5032 aDataset->WriteOnDisk( & smIDs[0] );
5033 aDataset->CloseOnDisk();
5036 aGroup->CloseOnDisk();
5038 // Store node positions on sub-shapes (SMDS_Position):
5039 // ----------------------------------------------------
5041 aGroup = new HDFgroup( "Node Positions", aTopGroup );
5042 aGroup->CreateOnDisk();
5044 // in aGroup, create 5 datasets to contain:
5045 // "Nodes on Edges" - ID of node on edge
5046 // "Edge positions" - U parameter on node on edge
5047 // "Nodes on Faces" - ID of node on face
5048 // "Face U positions" - U parameter of node on face
5049 // "Face V positions" - V parameter of node on face
5051 // Find out nb of nodes on edges and faces
5052 // Collect corresponding sub-meshes
5053 int nbEdgeNodes = 0, nbFaceNodes = 0;
5054 list<SMESHDS_SubMesh*> aEdgeSM, aFaceSM;
5055 // loop on SMESHDS_SubMesh'es
5056 while ( smIt->more() )
5058 SMESHDS_SubMesh* aSubMesh = const_cast< SMESHDS_SubMesh* >( smIt->next() );
5059 if ( aSubMesh->IsComplexSubmesh() )
5060 continue; // submesh containing other submeshs
5061 smIdType nbNodes = aSubMesh->NbNodes();
5062 if ( nbNodes == 0 ) continue;
5064 int aShapeID = aSubMesh->GetID();
5065 if ( aShapeID < 1 || aShapeID > mySMESHDSMesh->MaxShapeIndex() )
5067 int aShapeType = mySMESHDSMesh->IndexToShape( aShapeID ).ShapeType();
5068 // write only SMDS_FacePosition and SMDS_EdgePosition
5069 switch ( aShapeType ) {
5071 nbFaceNodes += nbNodes;
5072 aFaceSM.push_back( aSubMesh );
5075 nbEdgeNodes += nbNodes;
5076 aEdgeSM.push_back( aSubMesh );
5082 // Treat positions on edges or faces
5083 for ( int onFace = 0; onFace < 2; onFace++ )
5085 // Create arrays to store in datasets
5086 int iNode = 0, nbNodes = ( onFace ? nbFaceNodes : nbEdgeNodes );
5087 if (!nbNodes) continue;
5088 int* aNodeIDs = new int [ nbNodes ];
5089 double* aUPos = new double [ nbNodes ];
5090 double* aVPos = ( onFace ? new double[ nbNodes ] : 0 );
5093 // loop on sub-meshes
5094 list<SMESHDS_SubMesh*> * pListSM = ( onFace ? &aFaceSM : &aEdgeSM );
5095 list<SMESHDS_SubMesh*>::iterator itSM = pListSM->begin();
5096 for ( ; itSM != pListSM->end(); itSM++ )
5098 SMESHDS_SubMesh* aSubMesh = (*itSM);
5100 SMDS_NodeIteratorPtr itNode = aSubMesh->GetNodes();
5101 // loop on nodes in aSubMesh
5102 while ( itNode->more() )
5105 const SMDS_MeshNode* node = itNode->next();
5106 aNodeIDs [ iNode ] = node->GetID();
5109 const SMDS_PositionPtr pos = node->GetPosition();
5110 if ( onFace ) { // on FACE
5111 SMDS_FacePositionPtr fPos = pos;
5113 aUPos[ iNode ] = fPos->GetUParameter();
5114 aVPos[ iNode ] = fPos->GetVParameter();
5121 SMDS_EdgePositionPtr ePos = pos;
5123 aUPos[ iNode ] = ePos->GetUParameter();
5129 } // loop on nodes in aSubMesh
5130 } // loop on sub-meshes
5135 aSize[ 0 ] = nbNodes;
5137 string aDSName( onFace ? "Nodes on Faces" : "Nodes on Edges");
5138 aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_INT32, aSize, 1 );
5139 aDataset->CreateOnDisk();
5140 aDataset->WriteOnDisk( aNodeIDs );
5141 aDataset->CloseOnDisk();
5144 aDSName = ( onFace ? "Face U positions" : "Edge positions");
5145 aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_FLOAT64, aSize, 1);
5146 aDataset->CreateOnDisk();
5147 aDataset->WriteOnDisk( aUPos );
5148 aDataset->CloseOnDisk();
5151 aDataset = new HDFdataset( "Face V positions", aGroup, HDF_FLOAT64, aSize, 1);
5152 aDataset->CreateOnDisk();
5153 aDataset->WriteOnDisk( aVPos );
5154 aDataset->CloseOnDisk();
5159 if ( aVPos ) delete [] aVPos;
5161 } // treat positions on edges or faces
5163 // close "Node Positions" group
5164 aGroup->CloseOnDisk();
5166 } // if ( there are submeshes in SMESHDS_Mesh )
5169 // close mesh HDF group
5170 aTopGroup->CloseOnDisk();
5178 aFile->CloseOnDisk();
5181 // Convert temporary files to stream
5182 aStreamFile = SALOMEDS_Tool::PutFilesToStream( tmpDir.ToCString(), aFileSeq, isMultiFile );
5184 // Remove temporary files and directory
5186 SALOMEDS_Tool::RemoveTemporaryFiles( tmpDir.ToCString(), aFileSeq, true );
5188 return aStreamFile._retn();
5191 //=============================================================================
5193 * SMESH_Gen_i::SaveASCII
5195 * Save SMESH module's data in ASCII format
5197 //=============================================================================
5199 SALOMEDS::TMPFile* SMESH_Gen_i::SaveASCII( SALOMEDS::SComponent_ptr theComponent,
5201 bool isMultiFile ) {
5202 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::SaveASCII" );
5203 SALOMEDS::TMPFile_var aStreamFile = Save( theComponent, theURL, isMultiFile );
5204 return aStreamFile._retn();
5206 //after usual saving needs to encipher binary to text string
5207 //Any binary symbol will be represent as "|xx" () hexadecimal format number
5208 int size = aStreamFile.in().length();
5209 _CORBA_Octet* buffer = new _CORBA_Octet[size*3+1];
5210 for ( int i = 0; i < size; i++ )
5211 sprintf( (char*)&(buffer[i*3]), "|%02x", aStreamFile[i] );
5213 buffer[size * 3] = '\0';
5215 SALOMEDS::TMPFile_var anAsciiStreamFile = new SALOMEDS::TMPFile(size*3, size*3, buffer, 1);
5217 return anAsciiStreamFile._retn();
5220 //=============================================================================
5224 * Load SMESH module's data
5226 //=============================================================================
5228 bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
5229 const SALOMEDS::TMPFile& theStream,
5233 UpdateStudy(); // load geom data
5234 Kernel_Utils::Localizer loc;
5236 SALOMEDS::Study_var aStudy = getStudyServant();
5238 // Get temporary files location
5239 TCollection_AsciiString tmpDir =
5240 ( char* )( isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir().c_str() );
5242 // Convert the stream into sequence of files to process
5243 SALOMEDS_Tool::ListOfFiles aFileSeq = SALOMEDS_Tool::PutStreamToFiles( theStream,
5246 TCollection_AsciiString aStudyName( "" );
5247 if ( isMultiFile ) {
5248 CORBA::WString_var url = aStudy->URL();
5249 SMESHUtils::ArrayDeleter<const char> urlMulibyte( Kernel_Utils::encode( url.in()) );
5250 aStudyName = (char*)SALOMEDS_Tool::GetNameFromPath( urlMulibyte.get() ).c_str();
5252 // Set names of temporary files
5253 TCollection_AsciiString filename = tmpDir + aStudyName + "_SMESH.hdf";
5254 TCollection_AsciiString meshfile = tmpDir + aStudyName + "_SMESH_Mesh.med";
5258 HDFdataset* aDataset;
5259 HDFgroup* aTopGroup;
5261 HDFgroup* aSubGroup;
5262 HDFgroup* aSubSubGroup;
5265 // ---> open HDF file
5266 aFile = new HDFfile( (char*) filename.ToCString() );
5268 aFile->OpenOnDisk( HDF_RDONLY );
5270 catch ( HDFexception ) {
5271 INFOS( "Load(): " << filename << " not found!" );
5275 TPythonDump pd(this); // prevent dump during loading
5277 // For PAL13473 ("Repetitive mesh") implementation.
5278 // New dependencies between SMESH objects are established:
5279 // now hypotheses can refer to meshes, shapes and other hypotheses.
5280 // To keep data consistent, the following order of data restoration
5282 // 1. Create hypotheses
5283 // 2. Create all meshes
5284 // 3. Load hypotheses' data
5287 list< pair< SMESH_Hypothesis_i*, string > > hypDataList;
5288 list< pair< SMESH_Mesh_i*, HDFgroup* > > meshGroupList;
5289 list< SMESH::Filter_var > filters;
5291 // get total number of top-level groups
5292 int aNbGroups = aFile->nInternalObjects();
5293 if ( aNbGroups > 0 ) {
5294 // --> in first turn we should read&create hypotheses
5295 if ( aFile->ExistInternalObject( "Hypotheses" ) ) {
5296 // open hypotheses root HDF group
5297 aTopGroup = new HDFgroup( "Hypotheses", aFile );
5298 aTopGroup->OpenOnDisk();
5300 // get number of hypotheses
5301 int aNbObjects = aTopGroup->nInternalObjects();
5302 for ( int j = 0; j < aNbObjects; j++ ) {
5303 // try to identify hypothesis
5304 char hypGrpName[ HDF_NAME_MAX_LEN+1 ];
5305 aTopGroup->InternalObjectIndentify( j, hypGrpName );
5307 if ( string( hypGrpName ).substr( 0, 10 ) == string( "Hypothesis" ) ) {
5308 // open hypothesis group
5309 aGroup = new HDFgroup( hypGrpName, aTopGroup );
5310 aGroup->OpenOnDisk();
5312 // --> get hypothesis id
5313 int id = atoi( string( hypGrpName ).substr( 10 ).c_str() );
5318 // get number of datasets
5319 int aNbSubObjects = aGroup->nInternalObjects();
5320 for ( int k = 0; k < aNbSubObjects; k++ ) {
5322 char name_of_subgroup[ HDF_NAME_MAX_LEN+1 ];
5323 aGroup->InternalObjectIndentify( k, name_of_subgroup );
5324 // --> get hypothesis name
5325 if ( strcmp( name_of_subgroup, "Name" ) == 0 ) {
5326 aDataset = new HDFdataset( name_of_subgroup, aGroup );
5327 aDataset->OpenOnDisk();
5328 size = aDataset->GetSize();
5329 char* hypname_str = new char[ size ];
5330 aDataset->ReadFromDisk( hypname_str );
5331 hypname = string( hypname_str );
5332 delete [] hypname_str;
5333 aDataset->CloseOnDisk();
5335 // --> get hypothesis plugin library name
5336 if ( strcmp( name_of_subgroup, "LibName" ) == 0 ) {
5337 aDataset = new HDFdataset( name_of_subgroup, aGroup );
5338 aDataset->OpenOnDisk();
5339 size = aDataset->GetSize();
5340 char* libname_str = new char[ size ];
5341 aDataset->ReadFromDisk( libname_str );
5342 if(MYDEBUG) SCRUTE( libname_str );
5343 libname = string( libname_str );
5344 delete [] libname_str;
5345 aDataset->CloseOnDisk();
5347 // --> get hypothesis data
5348 if ( strcmp( name_of_subgroup, "Data" ) == 0 ) {
5349 aDataset = new HDFdataset( name_of_subgroup, aGroup );
5350 aDataset->OpenOnDisk();
5351 size = aDataset->GetSize();
5352 char* hypdata_str = new char[ size ];
5353 aDataset->ReadFromDisk( hypdata_str );
5354 hypdata = string( hypdata_str );
5355 delete [] hypdata_str;
5356 aDataset->CloseOnDisk();
5359 // close hypothesis HDF group
5360 aGroup->CloseOnDisk();
5362 // --> restore hypothesis from data
5363 if ( id > 0 && !hypname.empty()/* && !hypdata.empty()*/ ) { // VSR : persistent data can be empty
5364 if(MYDEBUG) MESSAGE("VSR - load hypothesis : id = " << id <<
5365 ", name = " << hypname.c_str() << ", persistent string = " << hypdata.c_str());
5366 SMESH::SMESH_Hypothesis_var myHyp;
5368 try { // protect persistence mechanism against exceptions
5369 myHyp = this->createHypothesis( hypname.c_str(), libname.c_str() );
5372 INFOS( "Exception during hypothesis creation" );
5375 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
5377 // myImpl->LoadFrom( hypdata.c_str() );
5378 hypDataList.push_back( make_pair( myImpl, hypdata ));
5379 CORBA::String_var iorString = GetORB()->object_to_string( myHyp );
5380 int newId = myStudyContext->findId( iorString.in() );
5381 myStudyContext->mapOldToNew( id, newId );
5384 if(MYDEBUG) MESSAGE( "VSR - SMESH_Gen::Load - can't get servant" );
5388 // close hypotheses root HDF group
5389 aTopGroup->CloseOnDisk();
5393 // --> then we should read&create algorithms
5394 if ( aFile->ExistInternalObject( "Algorithms" ) ) {
5395 // open algorithms root HDF group
5396 aTopGroup = new HDFgroup( "Algorithms", aFile );
5397 aTopGroup->OpenOnDisk();
5399 // get number of algorithms
5400 int aNbObjects = aTopGroup->nInternalObjects();
5401 for ( int j = 0; j < aNbObjects; j++ ) {
5402 // try to identify algorithm
5403 char hypGrpName[ HDF_NAME_MAX_LEN+1 ];
5404 aTopGroup->InternalObjectIndentify( j, hypGrpName );
5406 if ( string( hypGrpName ).substr( 0, 9 ) == string( "Algorithm" ) ) {
5407 // open algorithm group
5408 aGroup = new HDFgroup( hypGrpName, aTopGroup );
5409 aGroup->OpenOnDisk();
5411 // --> get algorithm id
5412 int id = atoi( string( hypGrpName ).substr( 9 ).c_str() );
5417 // get number of datasets
5418 int aNbSubObjects = aGroup->nInternalObjects();
5419 for ( int k = 0; k < aNbSubObjects; k++ ) {
5421 char name_of_subgroup[ HDF_NAME_MAX_LEN+1 ];
5422 aGroup->InternalObjectIndentify( k, name_of_subgroup );
5423 // --> get algorithm name
5424 if ( strcmp( name_of_subgroup, "Name" ) == 0 ) {
5425 aDataset = new HDFdataset( name_of_subgroup, aGroup );
5426 aDataset->OpenOnDisk();
5427 size = aDataset->GetSize();
5428 char* hypname_str = new char[ size ];
5429 aDataset->ReadFromDisk( hypname_str );
5430 hypname = string( hypname_str );
5431 delete [] hypname_str;
5432 aDataset->CloseOnDisk();
5434 // --> get algorithm plugin library name
5435 if ( strcmp( name_of_subgroup, "LibName" ) == 0 ) {
5436 aDataset = new HDFdataset( name_of_subgroup, aGroup );
5437 aDataset->OpenOnDisk();
5438 size = aDataset->GetSize();
5439 char* libname_str = new char[ size ];
5440 aDataset->ReadFromDisk( libname_str );
5441 if(MYDEBUG) SCRUTE( libname_str );
5442 libname = string( libname_str );
5443 delete [] libname_str;
5444 aDataset->CloseOnDisk();
5446 // --> get algorithm data
5447 if ( strcmp( name_of_subgroup, "Data" ) == 0 ) {
5448 aDataset = new HDFdataset( name_of_subgroup, aGroup );
5449 aDataset->OpenOnDisk();
5450 size = aDataset->GetSize();
5451 char* hypdata_str = new char[ size ];
5452 aDataset->ReadFromDisk( hypdata_str );
5453 if(MYDEBUG) SCRUTE( hypdata_str );
5454 hypdata = string( hypdata_str );
5455 delete [] hypdata_str;
5456 aDataset->CloseOnDisk();
5459 // close algorithm HDF group
5460 aGroup->CloseOnDisk();
5462 // --> restore algorithm from data
5463 if ( id > 0 && !hypname.empty()/* && !hypdata.empty()*/ ) { // VSR : persistent data can be empty
5464 if(MYDEBUG) MESSAGE("VSR - load algo : id = " << id <<
5465 ", name = " << hypname.c_str() << ", persistent string = " << hypdata.c_str());
5466 SMESH::SMESH_Hypothesis_var myHyp;
5468 try { // protect persistence mechanism against exceptions
5469 myHyp = this->createHypothesis( hypname.c_str(), libname.c_str() );
5471 catch( SALOME::SALOME_Exception& ex )
5473 INFOS( "Exception during hypothesis creation: " << ex.details.text );
5476 INFOS( "Exception during hypothesis creation" );
5479 SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
5481 //myImpl->LoadFrom( hypdata.c_str() );
5482 hypDataList.push_back( make_pair( myImpl, hypdata ));
5483 CORBA::String_var iorString = GetORB()->object_to_string( myHyp );
5484 int newId = myStudyContext->findId( iorString.in() );
5485 myStudyContext->mapOldToNew( id, newId );
5488 if(MYDEBUG) MESSAGE( "VSR - SMESH_Gen::Load - can't get servant" );
5492 // close algorithms root HDF group
5493 aTopGroup->CloseOnDisk();
5497 // --> the rest groups should be meshes
5498 for ( int i = 0; i < aNbGroups; i++ ) {
5499 // identify next group
5500 char meshName[ HDF_NAME_MAX_LEN+1 ];
5501 aFile->InternalObjectIndentify( i, meshName );
5503 if ( string( meshName ).substr( 0, 4 ) == string( "Mesh" ) ) {
5505 int id = atoi( string( meshName ).substr( 4 ).c_str() );
5509 // open mesh HDF group
5510 aTopGroup = new HDFgroup( meshName, aFile );
5511 aTopGroup->OpenOnDisk();
5513 // get number of child HDF objects
5514 int aNbObjects = aTopGroup->nInternalObjects();
5515 if ( aNbObjects > 0 ) {
5517 if(MYDEBUG) MESSAGE( "VSR - load mesh : id = " << id );
5518 SMESH::SMESH_Mesh_var myNewMesh = this->createMesh();
5519 SMESH_Mesh_i* myNewMeshImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( myNewMesh ).in() );
5520 if ( !myNewMeshImpl )
5522 meshGroupList.push_back( make_pair( myNewMeshImpl, aTopGroup ));
5524 CORBA::String_var iorString = GetORB()->object_to_string( myNewMesh );
5525 int newId = myStudyContext->findId( iorString.in() );
5526 myStudyContext->mapOldToNew( id, newId );
5529 // try to read and set auto color flag
5530 char aMeshAutoColorName[ 30 ];
5531 sprintf( aMeshAutoColorName, "AutoColorMesh %d", id);
5532 if( aTopGroup->ExistInternalObject( aMeshAutoColorName ) )
5534 aDataset = new HDFdataset( aMeshAutoColorName, aTopGroup );
5535 aDataset->OpenOnDisk();
5536 size = aDataset->GetSize();
5537 int* anAutoColor = new int[ size ];
5538 aDataset->ReadFromDisk( anAutoColor );
5539 aDataset->CloseOnDisk();
5540 myNewMeshImpl->GetImpl().SetAutoColor( (bool)anAutoColor[0] );
5541 delete [] anAutoColor;
5544 // try to read and set reference to shape
5545 GEOM::GEOM_Object_var aShapeObject;
5546 if ( aTopGroup->ExistInternalObject( "Ref on shape" ) ) {
5547 // load mesh "Ref on shape" - it's an entry to SObject
5548 aDataset = new HDFdataset( "Ref on shape", aTopGroup );
5549 aDataset->OpenOnDisk();
5550 size = aDataset->GetSize();
5551 char* refFromFile = new char[ size ];
5552 aDataset->ReadFromDisk( refFromFile );
5553 aDataset->CloseOnDisk();
5554 if ( strlen( refFromFile ) > 0 ) {
5555 SALOMEDS::SObject_wrap shapeSO = aStudy->FindObjectID( refFromFile );
5556 CORBA::Object_var shapeObject = SObjectToObject( shapeSO );
5557 if ( !CORBA::is_nil( shapeObject ) ) {
5558 aShapeObject = GEOM::GEOM_Object::_narrow( shapeObject );
5559 if ( !aShapeObject->_is_nil() )
5560 myNewMeshImpl->SetShape( aShapeObject );
5563 delete [] refFromFile;
5566 // issue 20918. Restore Persistent Id of SMESHDS_Mesh
5567 if ( aTopGroup->ExistInternalObject( "meshPersistentId" ) )
5569 aDataset = new HDFdataset( "meshPersistentId", aTopGroup );
5570 aDataset->OpenOnDisk();
5571 size = aDataset->GetSize();
5572 int* meshPersistentId = new int[ size ];
5573 aDataset->ReadFromDisk( meshPersistentId );
5574 aDataset->CloseOnDisk();
5575 myNewMeshImpl->GetImpl().GetMeshDS()->SetPersistentId( *meshPersistentId );
5576 delete [] meshPersistentId;
5579 // Restore SMESH_Mesh_i::_mainShapeTick
5580 if ( aTopGroup->ExistInternalObject( "shapeTick" ))
5582 aDataset = new HDFdataset( "shapeTick", aTopGroup );
5583 aDataset->OpenOnDisk();
5584 int* shapeTick = & myNewMeshImpl->MainShapeTick();
5585 aDataset->ReadFromDisk( shapeTick );
5586 aDataset->CloseOnDisk();
5589 // Restore file info
5590 if ( aTopGroup->ExistInternalObject( "file info" ))
5592 aDataset = new HDFdataset( "file info", aTopGroup );
5593 aDataset->OpenOnDisk();
5594 size = aDataset->GetSize();
5595 std::string info( size, ' ');
5596 aDataset->ReadFromDisk( (char*) info.data() );
5597 aDataset->CloseOnDisk();
5598 myNewMeshImpl->FileInfoFromString( info );
5604 // As all objects that can be referred by hypothesis are created,
5605 // we can restore hypothesis data
5607 list< pair< SMESH_Hypothesis_i*, string > >::iterator hyp_data;
5608 for ( hyp_data = hypDataList.begin(); hyp_data != hypDataList.end(); ++hyp_data )
5610 SMESH_Hypothesis_i* hyp = hyp_data->first;
5611 string & data = hyp_data->second;
5612 hyp->LoadFrom( data.c_str() );
5615 // Restore the rest mesh data
5617 list< pair< SMESH_Mesh_i*, HDFgroup* > >::iterator meshi_group;
5618 for ( meshi_group = meshGroupList.begin(); meshi_group != meshGroupList.end(); ++meshi_group )
5620 aTopGroup = meshi_group->second;
5621 SMESH_Mesh_i* myNewMeshImpl = meshi_group->first;
5623 GEOM::GEOM_Object_var aShapeObject = myNewMeshImpl->GetShapeToMesh();
5624 bool hasData = false;
5627 CORBA::String_var iorString = GetORB()->object_to_string( myNewMeshImpl->_this() );
5628 int newId = myStudyContext->findId( iorString.in() );
5629 int meshOldId = myStudyContext->getOldId( newId );
5631 // try to find mesh data dataset
5632 if ( aTopGroup->ExistInternalObject( "Has data" ) ) {
5633 // load mesh "has data" flag
5634 aDataset = new HDFdataset( "Has data", aTopGroup );
5635 aDataset->OpenOnDisk();
5636 size = aDataset->GetSize();
5637 char* strHasData = new char[ size ];
5638 aDataset->ReadFromDisk( strHasData );
5639 aDataset->CloseOnDisk();
5640 if ( strcmp( strHasData, "1") == 0 ) {
5643 delete [] strHasData;
5646 // Try to get applied ALGORITHMS (mesh is not cleared by algo addition because
5647 // nodes and elements are not yet put into sub-meshes)
5648 if ( aTopGroup->ExistInternalObject( "Applied Algorithms" ) ) {
5649 aGroup = new HDFgroup( "Applied Algorithms", aTopGroup );
5650 aGroup->OpenOnDisk();
5651 // get number of applied algorithms
5652 int aNbSubObjects = aGroup->nInternalObjects();
5653 if(MYDEBUG) MESSAGE( "VSR - number of applied algos " << aNbSubObjects );
5654 for ( int j = 0; j < aNbSubObjects; j++ ) {
5655 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
5656 aGroup->InternalObjectIndentify( j, name_dataset );
5657 // check if it is an algorithm
5658 if ( string( name_dataset ).substr( 0, 4 ) == string( "Algo" ) ) {
5659 aDataset = new HDFdataset( name_dataset, aGroup );
5660 aDataset->OpenOnDisk();
5661 size = aDataset->GetSize();
5662 char* refFromFile = new char[ size ];
5663 aDataset->ReadFromDisk( refFromFile );
5664 aDataset->CloseOnDisk();
5665 // san - it is impossible to recover applied algorithms using their entries within Load() method
5666 int id = atoi( refFromFile );
5667 delete [] refFromFile;
5668 string anIOR = myStudyContext->getIORbyOldId( id );
5669 if ( !anIOR.empty() ) {
5670 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
5671 if ( !CORBA::is_nil( hypObject ) ) {
5672 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
5673 if ( !anHyp->_is_nil() && (!aShapeObject->_is_nil()
5674 || !myNewMeshImpl->HasShapeToMesh()) )
5675 myNewMeshImpl->addHypothesis( aShapeObject, anHyp );
5680 aGroup->CloseOnDisk();
5683 // try to get applied hypotheses
5684 if ( aTopGroup->ExistInternalObject( "Applied Hypotheses" ) ) {
5685 aGroup = new HDFgroup( "Applied Hypotheses", aTopGroup );
5686 aGroup->OpenOnDisk();
5687 // get number of applied hypotheses
5688 int aNbSubObjects = aGroup->nInternalObjects();
5689 for ( int j = 0; j < aNbSubObjects; j++ ) {
5690 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
5691 aGroup->InternalObjectIndentify( j, name_dataset );
5692 // check if it is a hypothesis
5693 if ( string( name_dataset ).substr( 0, 3 ) == string( "Hyp" ) ) {
5694 aDataset = new HDFdataset( name_dataset, aGroup );
5695 aDataset->OpenOnDisk();
5696 size = aDataset->GetSize();
5697 char* refFromFile = new char[ size ];
5698 aDataset->ReadFromDisk( refFromFile );
5699 aDataset->CloseOnDisk();
5700 // san - it is impossible to recover applied hypotheses using their entries within Load() method
5701 int id = atoi( refFromFile );
5702 delete [] refFromFile;
5703 string anIOR = myStudyContext->getIORbyOldId( id );
5704 if ( !anIOR.empty() ) {
5705 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
5706 if ( !CORBA::is_nil( hypObject ) ) {
5707 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
5708 if ( !anHyp->_is_nil() && (!aShapeObject->_is_nil()
5709 || !myNewMeshImpl->HasShapeToMesh()) )
5710 myNewMeshImpl->addHypothesis( aShapeObject, anHyp );
5715 aGroup->CloseOnDisk();
5718 // --> try to find SUB-MESHES containers for each type of submesh
5719 for ( int j = GetSubMeshOnVertexTag(); j <= GetSubMeshOnCompoundTag(); j++ ) {
5720 const char* name_meshgroup = 0;
5721 if ( j == GetSubMeshOnVertexTag() )
5722 name_meshgroup = "SubMeshes On Vertex";
5723 else if ( j == GetSubMeshOnEdgeTag() )
5724 name_meshgroup = "SubMeshes On Edge";
5725 else if ( j == GetSubMeshOnWireTag() )
5726 name_meshgroup = "SubMeshes On Wire";
5727 else if ( j == GetSubMeshOnFaceTag() )
5728 name_meshgroup = "SubMeshes On Face";
5729 else if ( j == GetSubMeshOnShellTag() )
5730 name_meshgroup = "SubMeshes On Shell";
5731 else if ( j == GetSubMeshOnSolidTag() )
5732 name_meshgroup = "SubMeshes On Solid";
5733 else if ( j == GetSubMeshOnCompoundTag() )
5734 name_meshgroup = "SubMeshes On Compound";
5736 // try to get submeshes container HDF group
5737 if ( aTopGroup->ExistInternalObject( name_meshgroup ) ) {
5738 // open submeshes containers HDF group
5739 aGroup = new HDFgroup( name_meshgroup, aTopGroup );
5740 aGroup->OpenOnDisk();
5742 // get number of submeshes
5743 int aNbSubMeshes = aGroup->nInternalObjects();
5744 for ( int k = 0; k < aNbSubMeshes; k++ ) {
5746 char name_submeshgroup[ HDF_NAME_MAX_LEN+1 ];
5747 aGroup->InternalObjectIndentify( k, name_submeshgroup );
5748 if ( strncmp( name_submeshgroup, "SubMesh", 7 ) == 0 ) {
5749 // --> get submesh id
5750 int subid = atoi( name_submeshgroup + 7 );
5753 // open submesh HDF group
5754 aSubGroup = new HDFgroup( name_submeshgroup, aGroup );
5755 aSubGroup->OpenOnDisk();
5757 // try to read and set reference to subshape
5758 GEOM::GEOM_Object_var aSubShapeObject;
5759 SMESH::SMESH_subMesh_var aSubMesh;
5761 if ( aSubGroup->ExistInternalObject( "Ref on shape" ) ) {
5762 // load submesh "Ref on shape" - it's an entry to SObject
5763 aDataset = new HDFdataset( "Ref on shape", aSubGroup );
5764 aDataset->OpenOnDisk();
5765 size = aDataset->GetSize();
5766 char* refFromFile = new char[ size ];
5767 aDataset->ReadFromDisk( refFromFile );
5768 aDataset->CloseOnDisk();
5769 if ( strlen( refFromFile ) > 0 ) {
5770 SALOMEDS::SObject_wrap subShapeSO = aStudy->FindObjectID( refFromFile );
5771 CORBA::Object_var subShapeObject = SObjectToObject( subShapeSO );
5772 if ( !CORBA::is_nil( subShapeObject ) ) {
5773 aSubShapeObject = GEOM::GEOM_Object::_narrow( subShapeObject );
5774 if ( !aSubShapeObject->_is_nil() )
5775 aSubMesh = SMESH::SMESH_subMesh::_duplicate
5776 ( myNewMeshImpl->createSubMesh( aSubShapeObject ) );
5777 if ( aSubMesh->_is_nil() )
5779 string iorSubString = GetORB()->object_to_string( aSubMesh );
5780 int newSubId = myStudyContext->findId( iorSubString );
5781 myStudyContext->mapOldToNew( subid, newSubId );
5784 delete [] refFromFile;
5787 if ( aSubMesh->_is_nil() )
5790 // try to get applied algorithms
5791 if ( aSubGroup->ExistInternalObject( "Applied Algorithms" ) ) {
5792 // open "applied algorithms" HDF group
5793 aSubSubGroup = new HDFgroup( "Applied Algorithms", aSubGroup );
5794 aSubSubGroup->OpenOnDisk();
5795 // get number of applied algorithms
5796 int aNbSubObjects = aSubSubGroup->nInternalObjects();
5797 for ( int l = 0; l < aNbSubObjects; l++ ) {
5798 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
5799 aSubSubGroup->InternalObjectIndentify( l, name_dataset );
5800 // check if it is an algorithm
5801 if ( strncmp( name_dataset, "Algo", 4 ) == 0 ) {
5802 aDataset = new HDFdataset( name_dataset, aSubSubGroup );
5803 aDataset->OpenOnDisk();
5804 size = aDataset->GetSize();
5805 char* refFromFile = new char[ size ];
5806 aDataset->ReadFromDisk( refFromFile );
5807 aDataset->CloseOnDisk();
5809 int id = atoi( refFromFile );
5810 string anIOR = myStudyContext->getIORbyOldId( id );
5811 if ( !anIOR.empty() ) {
5812 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
5813 if ( !CORBA::is_nil( hypObject ) ) {
5814 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
5815 if ( !anHyp->_is_nil() && !aShapeObject->_is_nil() )
5816 myNewMeshImpl->addHypothesis( aSubShapeObject, anHyp );
5819 delete [] refFromFile;
5822 // close "applied algorithms" HDF group
5823 aSubSubGroup->CloseOnDisk();
5826 // try to get applied hypotheses
5827 if ( aSubGroup->ExistInternalObject( "Applied Hypotheses" ) ) {
5828 // open "applied hypotheses" HDF group
5829 aSubSubGroup = new HDFgroup( "Applied Hypotheses", aSubGroup );
5830 aSubSubGroup->OpenOnDisk();
5831 // get number of applied hypotheses
5832 int aNbSubObjects = aSubSubGroup->nInternalObjects();
5833 for ( int l = 0; l < aNbSubObjects; l++ ) {
5834 char name_dataset[ HDF_NAME_MAX_LEN+1 ];
5835 aSubSubGroup->InternalObjectIndentify( l, name_dataset );
5836 // check if it is a hypothesis
5837 if ( string( name_dataset ).substr( 0, 3 ) == string( "Hyp" ) ) {
5838 aDataset = new HDFdataset( name_dataset, aSubSubGroup );
5839 aDataset->OpenOnDisk();
5840 size = aDataset->GetSize();
5841 char* refFromFile = new char[ size ];
5842 aDataset->ReadFromDisk( refFromFile );
5843 aDataset->CloseOnDisk();
5845 int id = atoi( refFromFile );
5846 string anIOR = myStudyContext->getIORbyOldId( id );
5847 if ( !anIOR.empty() ) {
5848 CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
5849 if ( !CORBA::is_nil( hypObject ) ) {
5850 SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
5851 if ( !anHyp->_is_nil() && !aShapeObject->_is_nil() )
5852 myNewMeshImpl->addHypothesis( aSubShapeObject, anHyp );
5855 delete [] refFromFile;
5858 // close "APPLIED HYPOTHESES" hdf group
5859 aSubSubGroup->CloseOnDisk();
5862 // close SUB-MESH hdf group
5863 aSubGroup->CloseOnDisk();
5866 // close SUB-MESHES containers hdf group
5867 aGroup->CloseOnDisk();
5871 // try to get GROUPS
5872 for ( int ii = GetNodeGroupsTag(); ii <= GetBallElementsGroupsTag(); ii++ ) {
5873 char name_group[ 30 ];
5874 if ( ii == GetNodeGroupsTag() )
5875 strcpy( name_group, "Groups of Nodes" );
5876 else if ( ii == GetEdgeGroupsTag() )
5877 strcpy( name_group, "Groups of Edges" );
5878 else if ( ii == GetFaceGroupsTag() )
5879 strcpy( name_group, "Groups of Faces" );
5880 else if ( ii == GetVolumeGroupsTag() )
5881 strcpy( name_group, "Groups of Volumes" );
5882 else if ( ii == Get0DElementsGroupsTag() )
5883 strcpy( name_group, "Groups of 0D Elements" );
5884 else if ( ii == GetBallElementsGroupsTag() )
5885 strcpy( name_group, "Groups of Balls" );
5887 if ( aTopGroup->ExistInternalObject( name_group ) ) {
5888 aGroup = new HDFgroup( name_group, aTopGroup );
5889 aGroup->OpenOnDisk();
5890 // PAL23514: get all names from the HDFgroup to avoid iteration on its contents
5891 // within aGroup->ExistInternalObject( name )
5892 std::vector< std::string > subNames;
5893 TColStd_MapOfAsciiString mapOfNames;
5894 aGroup->GetAllObjects( subNames );
5895 for ( size_t iN = 0; iN < subNames.size(); ++iN )
5896 mapOfNames.Add( subNames[ iN ].c_str() );
5898 for ( size_t j = 0; j < subNames.size(); j++ ) {
5899 const std::string& name_dataset = subNames[ j ];
5900 // check if it is a group
5901 if ( name_dataset.substr( 0, 5 ) == "Group" ) {
5904 int subid = strtol( name_dataset.data() + 5, &endptr, 10 );
5907 int groupID = -1; // group local ID (also persistent)
5909 groupID = atoi( endptr + 1 );
5910 aDataset = new HDFdataset( name_dataset.c_str(), aGroup );
5911 aDataset->OpenOnDisk();
5913 // Retrieve actual group name
5914 size = aDataset->GetSize();
5915 char* nameFromFile = new char[ size ];
5916 aDataset->ReadFromDisk( nameFromFile );
5917 aDataset->CloseOnDisk();
5919 // Try to find a shape reference
5920 TopoDS_Shape aShape;
5921 char aRefName[ 30 ];
5922 sprintf( aRefName, "Ref on shape %d", subid);
5923 if ( mapOfNames.Contains( aRefName ))
5925 // load mesh "Ref on shape" - it's an entry to SObject
5926 aDataset = new HDFdataset( aRefName, aGroup );
5927 aDataset->OpenOnDisk();
5928 size = aDataset->GetSize();
5929 char* refFromFile = new char[ size ];
5930 aDataset->ReadFromDisk( refFromFile );
5931 aDataset->CloseOnDisk();
5932 if ( strlen( refFromFile ) > 0 ) {
5933 SALOMEDS::SObject_wrap shapeSO = aStudy->FindObjectID( refFromFile );
5934 CORBA::Object_var shapeObject = SObjectToObject( shapeSO );
5935 if ( !CORBA::is_nil( shapeObject ) ) {
5936 aShapeObject = GEOM::GEOM_Object::_narrow( shapeObject );
5937 if ( !aShapeObject->_is_nil() )
5938 aShape = GeomObjectToShape( aShapeObject );
5941 delete [] refFromFile;
5943 // Try to read a filter of SMESH_GroupOnFilter
5944 SMESH::Filter_var filter;
5945 SMESH_PredicatePtr predicate;
5946 std::string hdfGrpName = ( SMESH_Comment( "Filter ") << subid );
5947 if ( mapOfNames.Contains( hdfGrpName.c_str() ))
5949 aDataset = new HDFdataset( hdfGrpName.c_str(), aGroup );
5950 aDataset->OpenOnDisk();
5951 size = aDataset->GetSize();
5952 char* persistStr = new char[ size ];
5953 aDataset->ReadFromDisk( persistStr );
5954 aDataset->CloseOnDisk();
5955 if ( strlen( persistStr ) > 0 ) {
5956 filter = SMESH_GroupOnFilter_i::StringToFilter( persistStr );
5957 predicate = SMESH_GroupOnFilter_i::GetPredicate( filter );
5958 filters.push_back( filter );
5960 delete [] persistStr;
5963 // Create group servant
5964 SMESH::ElementType type = (SMESH::ElementType)(ii - GetNodeGroupsTag() + 1);
5965 SMESH::SMESH_GroupBase_var aNewGroup = SMESH::SMESH_GroupBase::_duplicate
5966 ( myNewMeshImpl->createGroup( type, nameFromFile, groupID, aShape, predicate ) );
5967 delete [] nameFromFile;
5968 // Obtain a SMESHDS_Group object
5969 if ( aNewGroup->_is_nil() )
5972 CORBA::String_var iorSubStringVar = GetORB()->object_to_string( aNewGroup );
5973 string iorSubString(iorSubStringVar.in());
5974 int newSubId = myStudyContext->findId( iorSubString );
5975 myStudyContext->mapOldToNew( subid, newSubId );
5977 SMESH_GroupBase_i* aGroupImpl = SMESH::DownCast< SMESH_GroupBase_i*>( aNewGroup );
5981 if ( SMESH_GroupOnFilter_i* aFilterGroup =
5982 dynamic_cast< SMESH_GroupOnFilter_i*>( aGroupImpl ))
5984 aFilterGroup->SetFilter( filter );
5985 filter->UnRegister();
5987 SMESHDS_GroupBase* aGroupBaseDS = aGroupImpl->GetGroupDS();
5988 if ( !aGroupBaseDS )
5991 aGroupBaseDS->SetStoreName( name_dataset.c_str() );
5994 // Read color of the group
5995 char aGroupColorName[ 30 ];
5996 sprintf( aGroupColorName, "ColorGroup %d", subid);
5997 if ( mapOfNames.Contains( aGroupColorName ))
5999 aDataset = new HDFdataset( aGroupColorName, aGroup );
6000 aDataset->OpenOnDisk();
6001 size = aDataset->GetSize();
6002 double* anRGB = new double[ size ];
6003 aDataset->ReadFromDisk( anRGB );
6004 aDataset->CloseOnDisk();
6005 Quantity_Color aColor( anRGB[0], anRGB[1], anRGB[2], Quantity_TOC_RGB );
6006 aGroupBaseDS->SetColor( aColor );
6011 aGroup->CloseOnDisk();
6015 // instead of reading mesh data, we read only brief information of all
6016 // objects: mesh, groups, sub-meshes (issue 0021208)
6019 SMESH_PreMeshInfo::LoadFromFile( myNewMeshImpl, meshOldId,
6020 meshfile.ToCString(), filename.ToCString(),
6024 // read Sub-Mesh ORDER if any
6025 if ( aTopGroup->ExistInternalObject( "Mesh Order" )) { // old version keeps ids
6026 aDataset = new HDFdataset( "Mesh Order", aTopGroup );
6027 aDataset->OpenOnDisk();
6028 size = aDataset->GetSize();
6029 int* smIDs = new int[ size ];
6030 aDataset->ReadFromDisk( smIDs );
6031 aDataset->CloseOnDisk();
6032 TListOfListOfInt anOrderIds;
6033 anOrderIds.push_back( TListOfInt() );
6034 for ( int i = 0; i < size; i++ )
6035 if ( smIDs[ i ] < 0 ) // is separator
6036 anOrderIds.push_back( TListOfInt() );
6038 anOrderIds.back().push_back(smIDs[ i ]);
6040 myNewMeshImpl->GetImpl().SetMeshOrder( anOrderIds );
6043 if ( aTopGroup->ExistInternalObject( "MeshOrder_new" )) // new version keeps entries
6045 aDataset = new HDFdataset( "MeshOrder_new", aTopGroup );
6046 aDataset->OpenOnDisk();
6047 size = aDataset->GetSize();
6048 std::string dataString; dataString.resize( size );
6049 aDataset->ReadFromDisk((char*) dataString.data() );
6050 aDataset->CloseOnDisk();
6052 std::list< std::list< std::string > > orderEntryLists;
6053 SMESHUtils::BoostTxtArchive( dataString ) >> orderEntryLists;
6055 TListOfListOfInt anOrderIds;
6056 for ( const std::list< std::string >& entryList : orderEntryLists )
6058 anOrderIds.emplace_back();
6059 for ( const std::string & entry : entryList )
6061 GEOM::GEOM_Object_var go = GetGeomObjectByEntry( entry );
6062 TopoDS_Shape shape = GeomObjectToShape( go );
6063 if ( SMESH_subMesh* sm = myNewMeshImpl->GetImpl().GetSubMesh( shape ))
6064 anOrderIds.back().emplace_back( sm->GetId() );
6067 myNewMeshImpl->GetImpl().SetMeshOrder( anOrderIds );
6071 // update hyps needing full mesh data restored (issue 20918)
6072 for ( hyp_data = hypDataList.begin(); hyp_data != hypDataList.end(); ++hyp_data )
6074 SMESH_Hypothesis_i* hyp = hyp_data->first;
6075 hyp->UpdateAsMeshesRestored();
6078 // notify algos on completed restoration to set sub-mesh event listeners
6079 for ( meshi_group = meshGroupList.begin(); meshi_group != meshGroupList.end(); ++meshi_group )
6081 SMESH_Mesh_i* myNewMeshImpl = meshi_group->first;
6082 ::SMESH_Mesh& myLocMesh = myNewMeshImpl->GetImpl();
6084 TopoDS_Shape myLocShape;
6085 if(myLocMesh.HasShapeToMesh())
6086 myLocShape = myLocMesh.GetShapeToMesh();
6088 myLocShape = SMESH_Mesh::PseudoShape();
6090 myLocMesh.GetSubMesh(myLocShape)->
6091 ComputeStateEngine (SMESH_subMesh::SUBMESH_RESTORED);
6094 // let filters detect dependency on mesh groups via FT_BelongToMeshGroup predicate (22877)
6095 list< SMESH::Filter_var >::iterator f = filters.begin();
6096 for ( ; f != filters.end(); ++f )
6097 if ( SMESH::Filter_i * fi = SMESH::DownCast< SMESH::Filter_i*>( *f ))
6098 fi->FindBaseObjects();
6103 aTopGroup->CloseOnDisk();
6106 aFile->CloseOnDisk();
6109 // Remove temporary files created from the stream
6112 SMESH_File meshFile( meshfile.ToCString() );
6113 if ( !meshFile ) // no meshfile exists
6115 SALOMEDS_Tool::RemoveTemporaryFiles( tmpDir.ToCString(), aFileSeq, true );
6119 Engines::Container_var container = GetContainerRef();
6120 if ( Engines_Container_i* container_i = SMESH::DownCast<Engines_Container_i*>( container ))
6122 container_i->registerTemporaryFile( filename.ToCString() );
6123 container_i->registerTemporaryFile( meshfile.ToCString() );
6124 container_i->registerTemporaryFile( tmpDir.ToCString() );
6129 // creation of tree nodes for all data objects in the study
6130 // to support tree representation customization and drag-n-drop:
6131 SALOMEDS::UseCaseBuilder_wrap useCaseBuilder = aStudy->GetUseCaseBuilder();
6132 if ( !useCaseBuilder->IsUseCaseNode( theComponent ) ) {
6133 useCaseBuilder->SetRootCurrent();
6134 useCaseBuilder->Append( theComponent ); // component object is added as the top level item
6135 SALOMEDS::ChildIterator_wrap it = aStudy->NewChildIterator( theComponent );
6136 for (it->InitEx(true); it->More(); it->Next()) {
6137 useCaseBuilder->AppendTo( it->Value()->GetFather(), it->Value() );
6144 //=============================================================================
6146 * SMESH_Gen_i::LoadASCII
6148 * Load SMESH module's data in ASCII format
6150 //=============================================================================
6152 bool SMESH_Gen_i::LoadASCII( SALOMEDS::SComponent_ptr theComponent,
6153 const SALOMEDS::TMPFile& theStream,
6155 bool isMultiFile ) {
6156 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::LoadASCII" );
6157 return Load( theComponent, theStream, theURL, isMultiFile );
6159 //before call main ::Load method it's need for decipher text format to
6160 //binary ( "|xx" => x' )
6161 int size = theStream.length();
6162 if ( int((size / 3 )*3) != size ) //error size of buffer
6165 int real_size = int(size / 3);
6167 _CORBA_Octet* buffer = new _CORBA_Octet[real_size];
6170 unsigned int c = -1;
6171 for ( int i = 0; i < real_size; i++ )
6173 memcpy( &(tmp[0]), &(theStream[i*3+1]), 2 );
6174 sscanf( tmp, "%x", &c );
6175 sprintf( (char*)&(buffer[i]), "%c", (char)c );
6178 SALOMEDS::TMPFile_var aRealStreamFile = new SALOMEDS::TMPFile(real_size, real_size, buffer, 1);
6180 return Load( theComponent, *(aRealStreamFile._retn()), theURL, isMultiFile );
6183 //=============================================================================
6185 * SMESH_Gen_i::Close
6187 * Clears study-connected data when it is closed
6189 //=============================================================================
6191 void SMESH_Gen_i::Close( SALOMEDS::SComponent_ptr theComponent )
6193 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Close" );
6195 // Clear study contexts data
6196 myStudyContext->Clear();
6198 // remove the tmp files meshes are loaded from
6199 SMESH_PreMeshInfo::RemoveStudyFiles_TMP_METHOD( theComponent );
6201 // Clean trace of API methods calls
6207 //=============================================================================
6209 * SMESH_Gen_i::ComponentDataType
6211 * Get component data type
6213 //=============================================================================
6215 char* SMESH_Gen_i::ComponentDataType()
6217 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::ComponentDataType" );
6218 return CORBA::string_dup( "SMESH" );
6222 //=============================================================================
6224 * SMESH_Gen_i::IORToLocalPersistentID
6226 * Transform data from transient form to persistent
6228 //=============================================================================
6230 char* SMESH_Gen_i::IORToLocalPersistentID( SALOMEDS::SObject_ptr /*theSObject*/,
6231 const char* IORString,
6232 CORBA::Boolean /*isMultiFile*/,
6233 CORBA::Boolean /*isASCII*/ )
6235 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::IORToLocalPersistentID" );
6237 if ( myStudyContext && strcmp( IORString, "" ) != 0 ) {
6238 int anId = myStudyContext->findId( IORString );
6240 if(MYDEBUG) MESSAGE( "VSR " << anId )
6242 sprintf( strId, "%d", anId );
6243 return CORBA::string_dup( strId );
6246 return CORBA::string_dup( "" );
6249 //=============================================================================
6251 * SMESH_Gen_i::LocalPersistentIDToIOR
6253 * Transform data from persistent form to transient
6255 //=============================================================================
6257 char* SMESH_Gen_i::LocalPersistentIDToIOR( SALOMEDS::SObject_ptr /*theSObject*/,
6258 const char* aLocalPersistentID,
6259 CORBA::Boolean /*isMultiFile*/,
6260 CORBA::Boolean /*isASCII*/ )
6262 if(MYDEBUG) MESSAGE( "SMESH_Gen_i::LocalPersistentIDToIOR(): id = " << aLocalPersistentID );
6264 if ( myStudyContext && strcmp( aLocalPersistentID, "" ) != 0 ) {
6265 int anId = atoi( aLocalPersistentID );
6266 return CORBA::string_dup( myStudyContext->getIORbyOldId( anId ).c_str() );
6268 return CORBA::string_dup( "" );
6271 //=======================================================================
6272 //function : RegisterObject
6274 //=======================================================================
6276 int SMESH_Gen_i::RegisterObject(CORBA::Object_ptr theObject)
6278 if ( myStudyContext && !CORBA::is_nil( theObject )) {
6279 CORBA::String_var iorString = GetORB()->object_to_string( theObject );
6280 return myStudyContext->addObject( string( iorString.in() ) );
6285 //================================================================================
6287 * \brief Return id of registered object
6288 * \param theObject - the Object
6289 * \retval int - Object id
6291 //================================================================================
6293 CORBA::Long SMESH_Gen_i::GetObjectId(CORBA::Object_ptr theObject)
6295 if ( myStudyContext && !CORBA::is_nil( theObject )) {
6296 CORBA::String_var iorString = GetORB()->object_to_string( theObject );
6297 string iorStringCpp(iorString.in());
6298 return myStudyContext->findId( iorStringCpp );
6303 //=============================================================================
6305 * SMESH_Gen_i::SetName
6307 * Set a new object name
6309 //=============================================================================
6311 void SMESH_Gen_i::SetName(const char* theIOR,
6312 const char* theName)
6314 if ( theIOR && strcmp( theIOR, "" ) ) {
6315 CORBA::Object_var anObject = GetORB()->string_to_object( theIOR );
6316 SALOMEDS::SObject_wrap aSO = ObjectToSObject( anObject );
6317 if ( !aSO->_is_nil() ) {
6318 SetName( aSO, theName );
6323 // Version information
6324 char* SMESH_Gen_i::getVersion()
6326 #if SMESH_DEVELOPMENT
6327 return CORBA::string_dup(SMESH_VERSION_STR"dev");
6329 return CORBA::string_dup(SMESH_VERSION_STR);
6333 //=================================================================================
6334 // function : Move()
6335 // purpose : Moves objects to the specified position.
6336 // Is used in the drag-n-drop functionality.
6337 //=================================================================================
6339 void SMESH_Gen_i::Move( const SMESH::sobject_list& what,
6340 SALOMEDS::SObject_ptr where,
6343 if ( CORBA::is_nil( where ) ) return;
6345 SALOMEDS::StudyBuilder_var studyBuilder = getStudyServant()->NewBuilder();
6346 SALOMEDS::UseCaseBuilder_var useCaseBuilder = getStudyServant()->GetUseCaseBuilder();
6347 SALOMEDS::SComponent_var father = where->GetFatherComponent();
6348 std::string dataType = father->ComponentDataType();
6349 if ( dataType != "SMESH" ) return; // not a SMESH component
6351 SALOMEDS::SObject_var objAfter;
6352 if ( row >= 0 && useCaseBuilder->HasChildren( where ) ) {
6353 // insert at given row -> find insertion position
6354 SALOMEDS::UseCaseIterator_var useCaseIt = useCaseBuilder->GetUseCaseIterator( where );
6356 for ( i = 0; i < row && useCaseIt->More(); i++, useCaseIt->Next() );
6357 if ( i == row && useCaseIt->More() ) {
6358 objAfter = useCaseIt->Value();
6362 for ( CORBA::ULong i = 0; i < what.length(); i++ ) {
6363 SALOMEDS::SObject_var sobj = what[i];
6364 if ( CORBA::is_nil( sobj ) ) continue; // skip bad object
6365 // insert the object to the use case tree
6366 if ( !CORBA::is_nil( objAfter ) )
6367 useCaseBuilder->InsertBefore( sobj, objAfter ); // insert at given row
6369 useCaseBuilder->AppendTo( where, sobj ); // append to the end of list
6373 //================================================================================
6375 * \brief Collect indices of elements, which are located inside the sphere
6377 //================================================================================
6379 SMESH::long_array* SMESH_Gen_i::GetInsideSphere( SMESH::SMESH_IDSource_ptr meshPart,
6380 SMESH::ElementType theElemType,
6386 SMESH::long_array_var aResult = new SMESH::long_array();
6387 if ( meshPart->_is_nil() )
6388 return aResult._retn();
6390 // 1. Create geometrical object
6391 gp_Pnt aP( theX, theY, theZ );
6392 TopoDS_Shape aShape = BRepPrimAPI_MakeSphere( aP, theR ).Shape();
6394 std::vector<long> lst =_GetInside(meshPart, theElemType, aShape);
6396 if ( lst.size() > 0 ) {
6397 aResult->length( lst.size() );
6398 for ( size_t i = 0; i < lst.size(); i++ ) {
6399 aResult[i] = lst[i];
6402 return aResult._retn();
6405 //================================================================================
6407 * \brief Collect indices of elements, which are located inside the box
6409 //================================================================================
6411 SMESH::long_array* SMESH_Gen_i::GetInsideBox( SMESH::SMESH_IDSource_ptr meshPart,
6412 SMESH::ElementType theElemType,
6413 CORBA::Double theX1,
6414 CORBA::Double theY1,
6415 CORBA::Double theZ1,
6416 CORBA::Double theX2,
6417 CORBA::Double theY2,
6418 CORBA::Double theZ2)
6420 SMESH::long_array_var aResult = new SMESH::long_array();
6421 if( meshPart->_is_nil() )
6422 return aResult._retn();
6424 TopoDS_Shape aShape = BRepPrimAPI_MakeBox( gp_Pnt( theX1, theY1, theZ1 ),
6425 gp_Pnt( theX2, theY2, theZ2 ) ).Shape();
6427 std::vector<long> lst =_GetInside(meshPart, theElemType, aShape);
6429 if( lst.size() > 0 ) {
6430 aResult->length( lst.size() );
6431 for ( size_t i = 0; i < lst.size(); i++ ) {
6432 aResult[i] = lst[i];
6435 return aResult._retn();
6438 //================================================================================
6440 * \brief Collect indices of elements, which are located inside the cylinder
6442 //================================================================================
6444 SMESH::long_array* SMESH_Gen_i::GetInsideCylinder( SMESH::SMESH_IDSource_ptr meshPart,
6445 SMESH::ElementType theElemType,
6449 CORBA::Double theDX,
6450 CORBA::Double theDY,
6451 CORBA::Double theDZ,
6453 CORBA::Double theR )
6455 SMESH::long_array_var aResult = new SMESH::long_array();
6456 if( meshPart->_is_nil() )
6457 return aResult._retn();
6459 gp_Pnt aP( theX, theY, theZ );
6460 gp_Vec aV( theDX, theDY, theDZ );
6461 gp_Ax2 anAxes (aP, aV);
6463 TopoDS_Shape aShape = BRepPrimAPI_MakeCylinder(anAxes, theR, Abs(theH)).Shape();
6465 std::vector<long> lst =_GetInside(meshPart, theElemType, aShape);
6467 if( lst.size() > 0 ) {
6468 aResult->length( lst.size() );
6469 for ( size_t i = 0; i < lst.size(); i++ ) {
6470 aResult[i] = lst[i];
6473 return aResult._retn();
6476 //================================================================================
6478 * \brief Collect indices of elements, which are located inside the geom object
6480 //================================================================================
6482 SMESH::long_array* SMESH_Gen_i::GetInside( SMESH::SMESH_IDSource_ptr meshPart,
6483 SMESH::ElementType theElemType,
6484 GEOM::GEOM_Object_ptr theGeom,
6485 CORBA::Double theTolerance )
6487 SMESH::long_array_var aResult = new SMESH::long_array();
6488 if( meshPart->_is_nil() || theGeom->_is_nil() )
6489 return aResult._retn();
6491 TopoDS_Shape aShape = GeomObjectToShape( theGeom );
6493 std::vector<long> lst =_GetInside(meshPart, theElemType, aShape, &theTolerance);
6495 if( lst.size() > 0 ) {
6496 aResult->length( lst.size() );
6497 for ( size_t i = 0; i < lst.size(); i++ ) {
6498 aResult[i] = lst[i];
6501 return aResult._retn();
6504 //================================================================================
6506 * \brief Collect indices of elements, which are located inside the TopoDS_Shape
6508 //================================================================================
6510 std::vector<long> SMESH_Gen_i::_GetInside( SMESH::SMESH_IDSource_ptr meshPart,
6511 SMESH::ElementType theElemType,
6512 const TopoDS_Shape& theShape,
6513 double* theTolerance) {
6515 std::vector<long> res;
6516 SMESH::SMESH_Mesh_var mesh = meshPart->GetMesh();
6518 if ( mesh->_is_nil() )
6521 SMESH_Mesh_i* anImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
6525 const SMDS_Mesh* meshDS = anImpl->GetImpl().GetMeshDS();
6530 SMDSAbs_ElementType aType = SMDSAbs_ElementType(theElemType);
6531 SMESH::Controls::ElementsOnShape* anElementsOnShape = new SMESH::Controls::ElementsOnShape();
6532 anElementsOnShape->SetAllNodes( true );
6533 anElementsOnShape->SetMesh( meshDS );
6534 anElementsOnShape->SetShape( theShape, aType );
6537 anElementsOnShape->SetTolerance(*theTolerance);
6539 SMESH::SMESH_Mesh_var msource = SMESH::SMESH_Mesh::_narrow(meshPart);
6540 if ( !msource->_is_nil() ) { // Mesh case
6541 SMDS_ElemIteratorPtr elemIt = meshDS->elementsIterator( aType );
6543 while ( elemIt->more() ) {
6544 const SMDS_MeshElement* anElem = elemIt->next();
6545 long anId = anElem->GetID();
6546 if ( anElementsOnShape->IsSatisfy( anId ) )
6547 res.push_back( anId );
6551 SMESH::SMESH_Group_var gsource = SMESH::SMESH_Group::_narrow(meshPart);
6552 if ( !gsource->_is_nil() ) {
6553 if(theElemType == SMESH::NODE) {
6554 SMESH::smIdType_array_var nodes = gsource->GetNodeIDs();
6555 for ( CORBA::ULong i = 0; i < nodes->length(); ++i ) {
6556 if ( const SMDS_MeshNode* node = meshDS->FindNode( nodes[i] )) {
6557 long anId = node->GetID();
6558 if ( anElementsOnShape->IsSatisfy( anId ) )
6559 res.push_back( anId );
6562 } else if (gsource->GetType() == theElemType || theElemType == SMESH::ALL ) {
6563 SMESH::smIdType_array_var elems = gsource->GetListOfID();
6564 for ( CORBA::ULong i = 0; i < elems->length(); ++i ) {
6565 if ( const SMDS_MeshElement* elem = meshDS->FindElement( elems[i] )) {
6566 long anId = elem->GetID();
6567 if ( anElementsOnShape->IsSatisfy( anId ) )
6568 res.push_back( anId );
6573 SMESH::SMESH_subMesh_var smsource = SMESH::SMESH_subMesh::_narrow(meshPart);
6574 if ( !smsource->_is_nil() ) {
6575 SMESH::smIdType_array_var elems = smsource->GetElementsByType( theElemType );
6576 for ( CORBA::ULong i = 0; i < elems->length(); ++i ) {
6577 const SMDS_MeshElement* elem = ( theElemType == SMESH::NODE ) ? meshDS->FindNode( elems[i] ) : meshDS->FindElement( elems[i] );
6579 long anId = elem->GetID();
6580 if ( anElementsOnShape->IsSatisfy( anId ) )
6581 res.push_back( anId );
6588 //================================================================================
6590 * \brief Returns true if algorithm can be used to mesh a given geometry
6591 * \param [in] theAlgoType - the algorithm type
6592 * \param [in] theLibName - a name of the Plug-in library implementing the algorithm
6593 * \param [in] theGeomObject - the geometry to mesh
6594 * \param [in] toCheckAll - if \c True, returns \c True if all shapes are meshable,
6595 * else, returns \c True if at least one shape is meshable
6596 * \return CORBA::Boolean - can or can't
6598 //================================================================================
6600 #undef SMY_OWN_CATCH
6601 #define SMY_OWN_CATCH // prevent re-throwing SALOME::SALOME_Exception in IsApplicable()
6603 CORBA::Boolean SMESH_Gen_i::IsApplicable ( const char* theAlgoType,
6604 const char* theLibName,
6605 GEOM::GEOM_Object_ptr theGeomObject,
6606 CORBA::Boolean toCheckAll)
6610 std::string aPlatformLibName;
6611 GenericHypothesisCreator_i* aCreator =
6612 getHypothesisCreator(theAlgoType, theLibName, aPlatformLibName);
6615 TopoDS_Shape shape = GeomObjectToShape( theGeomObject );
6616 const SMESH_Algo::Features& feat = SMESH_Algo::GetFeatures( theAlgoType );
6617 return shape.IsNull() || aCreator->IsApplicable( shape, toCheckAll, feat._dim );
6624 SMESH_CATCH( SMESH::doNothing );
6627 cout << "SMESH_Gen_i::IsApplicable(): exception in " << ( theAlgoType ? theAlgoType : "") << endl;