From 223c5b7f5d446e18785843d69964a4df5da7a9c5 Mon Sep 17 00:00:00 2001 From: eap Date: Fri, 25 Aug 2017 16:45:03 +0300 Subject: [PATCH] IPAL54303: CGNS export problems --- idl/SMESH_Mesh.idl | 3 +- src/DriverCGNS/DriverCGNS_Write.cxx | 38 ++++++++++++++++++++-- src/DriverCGNS/DriverCGNS_Write.hxx | 7 ++++ src/SMESH/SMESH_Mesh.cxx | 23 ++++++++++++- src/SMESH/SMESH_Mesh.hxx | 3 +- src/SMESHGUI/SMESHGUI.cxx | 21 +++++++++--- src/SMESHGUI/SMESHGUI_FieldSelectorWdg.cxx | 4 +-- src/SMESHGUI/SMESHGUI_FieldSelectorWdg.h | 6 ++-- src/SMESHGUI/SMESH_msg_en.ts | 4 +++ src/SMESHUtils/SMESH_TryCatch.hxx | 6 +++- src/SMESH_I/SMESH_Mesh_i.cxx | 9 +++-- src/SMESH_I/SMESH_Mesh_i.hxx | 3 +- src/SMESH_SWIG/smeshBuilder.py | 9 +++-- 13 files changed, 115 insertions(+), 21 deletions(-) diff --git a/idl/SMESH_Mesh.idl b/idl/SMESH_Mesh.idl index cef3831ee..d96d67db7 100644 --- a/idl/SMESH_Mesh.idl +++ b/idl/SMESH_Mesh.idl @@ -715,7 +715,8 @@ module SMESH in boolean isascii ) raises (SALOME::SALOME_Exception); void ExportCGNS( in SMESH_IDSource meshPart, in string file, - in boolean overwrite ) raises (SALOME::SALOME_Exception); + in boolean overwrite, + in boolean groupElemsByType) raises (SALOME::SALOME_Exception); void ExportGMF( in SMESH_IDSource meshPart, in string file, in boolean withRequiredGroups) raises (SALOME::SALOME_Exception); diff --git a/src/DriverCGNS/DriverCGNS_Write.cxx b/src/DriverCGNS/DriverCGNS_Write.cxx index 844bb863f..657cb529e 100644 --- a/src/DriverCGNS/DriverCGNS_Write.cxx +++ b/src/DriverCGNS/DriverCGNS_Write.cxx @@ -25,6 +25,7 @@ #include "DriverCGNS_Write.hxx" +#include "SMDS_IteratorOnIterators.hxx" #include "SMDS_MeshNode.hxx" #include "SMDS_VolumeTool.hxx" #include "SMESHDS_GroupBase.hxx" @@ -330,7 +331,24 @@ Driver_Mesh::Status DriverCGNS_Write::Perform() // write into a section all successive elements of one geom type int iSec; vector< cgsize_t > elemData; - SMDS_ElemIteratorPtr elemIt = myMesh->elementsIterator(); + SMDS_ElemIteratorPtr elemIt = myMesh->elementsIterator(); + vector< SMDS_ElemIteratorPtr > elemItVec; + if ( _elementsByType ) + { + // create an iterator returning all elements by type + for ( int type = SMDSEntity_Node + 1; type < SMDSEntity_Last; ++type ) + { + if ( type == SMDSEntity_Ball ) + continue; // not supported + elemIt = myMesh->elementEntityIterator( SMDSAbs_EntityType( type )); + if ( elemIt->more() ) + elemItVec.push_back( elemIt ); + } + typedef SMDS_IteratorOnIterators< const SMDS_MeshElement*, + vector< SMDS_ElemIteratorPtr > > TVecIterator; + elemIt.reset( new TVecIterator( elemItVec )); + } + const SMDS_MeshElement* elem = elemIt->next(); while ( elem ) { @@ -397,6 +415,15 @@ Driver_Mesh::Status DriverCGNS_Write::Perform() elem = elemIt->more() ? elemIt->next() : 0; continue; } + else // skip NOT SUPPORTED elements + { + while ( elemIt->more() ) + { + elem = elemIt->next(); + if ( elem->GetEntityType() != elemType ) + break; + } + } SMESH_Comment sectionName( cg_ElementTypeName( cgType )); sectionName << " " << startID << " - " << cgID-1; @@ -405,6 +432,7 @@ Driver_Mesh::Status DriverCGNS_Write::Perform() cgID-1, /*nbndry=*/0, &elemData[0], &iSec) != CG_OK ) return addMessage( cg_get_error(), /*fatal = */true ); } + // Write polyhedral volumes // ------------------------- @@ -531,6 +559,9 @@ Driver_Mesh::Status DriverCGNS_Write::Perform() CGNS_ENUMT( GridLocation_t ) location = CGNS_ENUMV( Vertex ); if ( group->GetType() != SMDSAbs_Node ) { +#if CGNS_VERSION > 3130 + location = CGNS_ENUMV( CellCenter ); +#else switch ( meshDim ) { case 3: switch ( group->GetType() ) { @@ -551,6 +582,7 @@ Driver_Mesh::Status DriverCGNS_Write::Perform() location = CGNS_ENUMV( EdgeCenter ); break; // ??? break; } +#endif } // try to extract type of boundary condition from the group name @@ -568,6 +600,8 @@ Driver_Mesh::Status DriverCGNS_Write::Perform() const SMDS_MeshElement* elem = elemIt->next(); pnts.push_back( cgnsID( elem, elem2cgIDByEntity[ elem->GetEntityType() ])); } + if ( pnts.size() == 0 ) + continue; // can't store empty group int iBC; if ( cg_boco_write( _fn, iBase, iZone, name.c_str(), bcType, CGNS_ENUMV( PointList ), pnts.size(), &pnts[0], &iBC) != CG_OK ) @@ -589,7 +623,7 @@ Driver_Mesh::Status DriverCGNS_Write::Perform() */ //================================================================================ -DriverCGNS_Write::DriverCGNS_Write(): _fn(0) +DriverCGNS_Write::DriverCGNS_Write(): _fn(0), _elementsByType( false ) { } diff --git a/src/DriverCGNS/DriverCGNS_Write.hxx b/src/DriverCGNS/DriverCGNS_Write.hxx index e427f9754..fd0ba2239 100644 --- a/src/DriverCGNS/DriverCGNS_Write.hxx +++ b/src/DriverCGNS/DriverCGNS_Write.hxx @@ -45,9 +45,16 @@ public: virtual Status Perform(); + // to export elements either in the order of their IDs or by geometric type + void SetElementsByType( bool isByType ) { _elementsByType = isByType; } + private: int _fn; //!< file index + + // if true all elements of same geometry are exported at ones, + // else elements are exported in order of their IDs + bool _elementsByType; }; #endif diff --git a/src/SMESH/SMESH_Mesh.cxx b/src/SMESH/SMESH_Mesh.cxx index 893a28aba..cf9f77f0a 100644 --- a/src/SMESH/SMESH_Mesh.cxx +++ b/src/SMESH/SMESH_Mesh.cxx @@ -1537,6 +1537,7 @@ void SMESH_Mesh::ExportUNV(const char * file, myWriter.SetMeshId(_id); // myWriter.SetGroups(_mapGroup); + // pass group names to SMESHDS if ( !meshPart ) { for ( map::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) { @@ -1581,17 +1582,37 @@ void SMESH_Mesh::ExportSTL(const char * file, void SMESH_Mesh::ExportCGNS(const char * file, const SMESHDS_Mesh* meshDS, - const char * meshName) + const char * meshName, + const bool groupElemsByType) { int res = Driver_Mesh::DRS_FAIL; + + // pass group names to SMESHDS + for ( map::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) { + SMESH_Group* group = it->second; + SMESHDS_GroupBase* groupDS = group->GetGroupDS(); + if ( groupDS ) { + string groupName = group->GetName(); + groupDS->SetStoreName( groupName.c_str() ); + } + } #ifdef WITH_CGNS + DriverCGNS_Write myWriter; myWriter.SetFile( file ); myWriter.SetMesh( const_cast( meshDS )); myWriter.SetMeshName( SMESH_Comment("Mesh_") << meshDS->GetPersistentId()); if ( meshName && meshName[0] ) myWriter.SetMeshName( meshName ); + myWriter.SetElementsByType( groupElemsByType ); res = myWriter.Perform(); + if ( res != Driver_Mesh::DRS_OK ) + { + SMESH_ComputeErrorPtr err = myWriter.GetError(); + if ( err && !err->IsOK() && !err->myComment.empty() ) + throw SALOME_Exception(("Export failed: " + err->myComment ).c_str() ); + } + #endif if ( res != Driver_Mesh::DRS_OK ) throw SALOME_Exception("Export failed"); diff --git a/src/SMESH/SMESH_Mesh.hxx b/src/SMESH/SMESH_Mesh.hxx index a8ec5dfae..91a1f2cdb 100644 --- a/src/SMESH/SMESH_Mesh.hxx +++ b/src/SMESH/SMESH_Mesh.hxx @@ -267,7 +267,8 @@ class SMESH_EXPORT SMESH_Mesh const SMESHDS_Mesh* meshPart = 0) throw(SALOME_Exception); void ExportCGNS(const char * file, const SMESHDS_Mesh* mesh, - const char * meshName = 0); + const char * meshName = 0, + const bool groupElemsByType = false); void ExportGMF(const char * file, const SMESHDS_Mesh* mesh, bool withRequiredGroups = true ); diff --git a/src/SMESHGUI/SMESHGUI.cxx b/src/SMESHGUI/SMESHGUI.cxx index 1145efb17..afe1b1f6c 100644 --- a/src/SMESHGUI/SMESHGUI.cxx +++ b/src/SMESHGUI/SMESHGUI.cxx @@ -684,7 +684,14 @@ namespace } else if ( isCGNS )// Export to CGNS { - SUIT_FileDlg* fd = new SUIT_FileDlg( SMESHGUI::desktop(), false, true, true ); + const char* theByTypeResource = "cgns_group_elems_by_type"; + toCreateGroups = SMESHGUI::resourceMgr()->booleanValue( "SMESH", theByTypeResource, false ); + + QStringList checkBoxes; + checkBoxes << QObject::tr("CGNS_EXPORT_ELEMS_BY_TYPE"); + + SalomeApp_CheckFileDlg* fd = + new SalomeApp_CheckFileDlg ( SMESHGUI::desktop(), false, checkBoxes, true, true ); fd->setWindowTitle( aTitle ); fd->setNameFilter( QObject::tr( "CGNS_FILES_FILTER" ) + " (*.cgns)" ); if ( !anInitialPath.isEmpty() ) @@ -692,10 +699,13 @@ namespace fd->selectFile(aMeshName); SMESHGUI_FileValidator* fv = new SMESHGUI_FileValidator( fd ); fd->setValidator( fv ); + fd->SetChecked( toCreateGroups, 0 ); if ( fd->exec() ) aFilename = fd->selectedFile(); - toOverwrite = fv->isOverwrite(); + toOverwrite = fv->isOverwrite(); + toCreateGroups = fd->IsChecked(0); + SMESHGUI::resourceMgr()->setValue("SMESH", theByTypeResource, toCreateGroups ); delete fd; } @@ -754,7 +764,7 @@ namespace SMESHGUI_FieldSelectorWdg* fieldSelWdg = new SMESHGUI_FieldSelectorWdg(); QList< QWidget* > wdgList; - if ( fieldSelWdg->GetAllFeilds( aMeshList, aFieldList )) + if ( fieldSelWdg->GetAllFields( aMeshList, aFieldList )) wdgList.append( fieldSelWdg ); SalomeApp_CheckFileDlg* fd = @@ -858,7 +868,7 @@ namespace } toCreateGroups = fd->IsChecked(0); toFindOutDim = fd->IsChecked(1); - fieldSelWdg->GetSelectedFeilds(); + fieldSelWdg->GetSelectedFields(); if ( !fieldSelWdg->parent() ) delete fieldSelWdg; delete fd; @@ -948,7 +958,8 @@ namespace SMESH::SMESH_Mesh_var aMeshItem = aMeshOrGroup->GetMesh(); aMeshItem->ExportCGNS( aMeshOrGroup, aFilename.toUtf8().data(), - toOverwrite && aMeshIndex == 0 ); + toOverwrite && aMeshIndex == 0, + toCreateGroups ); } } else if ( isGMF ) diff --git a/src/SMESHGUI/SMESHGUI_FieldSelectorWdg.cxx b/src/SMESHGUI/SMESHGUI_FieldSelectorWdg.cxx index 8b060f647..910344980 100644 --- a/src/SMESHGUI/SMESHGUI_FieldSelectorWdg.cxx +++ b/src/SMESHGUI/SMESHGUI_FieldSelectorWdg.cxx @@ -86,7 +86,7 @@ SMESHGUI_FieldSelectorWdg::SMESHGUI_FieldSelectorWdg( QWidget* p ) * \brief Retrieves all fields defined on geometry of given meshes */ bool SMESHGUI_FieldSelectorWdg:: -GetAllFeilds(const QList< QPair< SMESH::SMESH_IDSource_var, QString > >& meshes, +GetAllFields(const QList< QPair< SMESH::SMESH_IDSource_var, QString > >& meshes, QList< QPair< GEOM::ListOfFields_var, QString > >& fields) { myFields = & fields; @@ -166,7 +166,7 @@ GetAllFeilds(const QList< QPair< SMESH::SMESH_IDSource_var, QString > >& meshes, /*! * \brief Filter off not selected fields from myFields */ -bool SMESHGUI_FieldSelectorWdg::GetSelectedFeilds() +bool SMESHGUI_FieldSelectorWdg::GetSelectedFields() { int nbSelected = 0; if ( myTree->isEnabled() ) diff --git a/src/SMESHGUI/SMESHGUI_FieldSelectorWdg.h b/src/SMESHGUI/SMESHGUI_FieldSelectorWdg.h index 8a88bf41d..025a4de73 100644 --- a/src/SMESHGUI/SMESHGUI_FieldSelectorWdg.h +++ b/src/SMESHGUI/SMESHGUI_FieldSelectorWdg.h @@ -43,13 +43,13 @@ class SMESHGUI_EXPORT SMESHGUI_FieldSelectorWdg : public QGroupBox public: SMESHGUI_FieldSelectorWdg( QWidget* = 0 ); - bool GetAllFeilds(const QList< QPair< SMESH::SMESH_IDSource_var, QString > >& meshes, + bool GetAllFields(const QList< QPair< SMESH::SMESH_IDSource_var, QString > >& meshes, QList< QPair< GEOM::ListOfFields_var, QString > >& fields); - bool GetSelectedFeilds(); + bool GetSelectedFields(); private slots: - + void onItemCheck(QTreeWidgetItem * item, int column); private: diff --git a/src/SMESHGUI/SMESH_msg_en.ts b/src/SMESHGUI/SMESH_msg_en.ts index a5e80c659..bd744f709 100644 --- a/src/SMESHGUI/SMESH_msg_en.ts +++ b/src/SMESHGUI/SMESH_msg_en.ts @@ -39,6 +39,10 @@ CGNS_FILES_FILTER CGNS files + + CGNS_EXPORT_ELEMS_BY_TYPE + Group elements by type + GMF_ASCII_FILES_FILTER GMF ASCII files diff --git a/src/SMESHUtils/SMESH_TryCatch.hxx b/src/SMESHUtils/SMESH_TryCatch.hxx index ccdf3b1da..05d00a1b5 100644 --- a/src/SMESHUtils/SMESH_TryCatch.hxx +++ b/src/SMESHUtils/SMESH_TryCatch.hxx @@ -42,7 +42,11 @@ #define OCC_CATCH_SIGNALS #endif -// Define macros to catch and convert some of possible exceptions into text or SALOME_Exception +// Define macros to catch and convert some of possible exceptions into text or SALOME_Exception. +// WARNING: SALOME::SALOME_Exception (CORBA exception) is not treated here; to care about it add +// #define SMY_OWN_CATCH catch ( SALOME::SALOME_Exception & e ) { do_something(e); } +// before #include + //------------------------------------------------------------------------------------- #define SMESH_TRY \ diff --git a/src/SMESH_I/SMESH_Mesh_i.cxx b/src/SMESH_I/SMESH_Mesh_i.cxx index 268dfd829..163446233 100644 --- a/src/SMESH_I/SMESH_Mesh_i.cxx +++ b/src/SMESH_I/SMESH_Mesh_i.cxx @@ -3628,7 +3628,8 @@ void SMESH_Mesh_i::ExportPartToSTL(::SMESH::SMESH_IDSource_ptr meshPart, void SMESH_Mesh_i::ExportCGNS(::SMESH::SMESH_IDSource_ptr meshPart, const char* file, - CORBA::Boolean overwrite) + CORBA::Boolean overwrite, + CORBA::Boolean groupElemsByType) throw (SALOME::SALOME_Exception) { #ifdef WITH_CGNS @@ -3646,8 +3647,12 @@ void SMESH_Mesh_i::ExportCGNS(::SMESH::SMESH_IDSource_ptr meshPart, CORBA::String_var name = so->GetName(); meshName = name.in(); } + SMESH_TRY; + SMESH_MeshPartDS partDS( meshPart ); - _impl->ExportCGNS(file, &partDS, meshName.c_str() ); + _impl->ExportCGNS(file, &partDS, meshName.c_str(), groupElemsByType ); + + SMESH_CATCH( SMESH::throwCorbaException ); TPythonDump() << SMESH::SMESH_Mesh_var(_this()) << ".ExportCGNS( " << meshPart<< ", r'" << file << "', " << overwrite << ")"; diff --git a/src/SMESH_I/SMESH_Mesh_i.hxx b/src/SMESH_I/SMESH_Mesh_i.hxx index 62d08763d..39fd2aa1c 100644 --- a/src/SMESH_I/SMESH_Mesh_i.hxx +++ b/src/SMESH_I/SMESH_Mesh_i.hxx @@ -251,7 +251,8 @@ public: void ExportSTL( const char* file, bool isascii ) throw (SALOME::SALOME_Exception); void ExportCGNS(SMESH::SMESH_IDSource_ptr meshPart, const char* file, - CORBA::Boolean overwrite) throw (SALOME::SALOME_Exception); + CORBA::Boolean overwrite, + CORBA::Boolean groupElemsByType) throw (SALOME::SALOME_Exception); void ExportGMF(SMESH::SMESH_IDSource_ptr meshPart, const char* file, CORBA::Boolean withRequiredGroups) throw (SALOME::SALOME_Exception); diff --git a/src/SMESH_SWIG/smeshBuilder.py b/src/SMESH_SWIG/smeshBuilder.py index 8105c71c6..11ba7dcf7 100644 --- a/src/SMESH_SWIG/smeshBuilder.py +++ b/src/SMESH_SWIG/smeshBuilder.py @@ -1879,8 +1879,11 @@ class Mesh: # @param f is the file name # @param overwrite boolean parameter for overwriting/not overwriting the file # @param meshPart a part of mesh (group, sub-mesh) to export instead of the mesh + # @param groupElemsByType if true all elements of same entity type are exported at ones, + # else elements are exported in order of their IDs which can cause creation + # of multiple cgns sections # @ingroup l2_impexp - def ExportCGNS(self, f, overwrite=1, meshPart=None): + def ExportCGNS(self, f, overwrite=1, meshPart=None, groupElemsByType=False): unRegister = genObjUnRegister() if isinstance( meshPart, list ): meshPart = self.GetIDSource( meshPart, SMESH.ALL ) @@ -1889,7 +1892,7 @@ class Mesh: meshPart = meshPart.mesh elif not meshPart: meshPart = self.mesh - self.mesh.ExportCGNS(meshPart, f, overwrite) + self.mesh.ExportCGNS(meshPart, f, overwrite, groupElemsByType) ## Export the mesh in a file in GMF format. # GMF files must have .mesh extension for the ASCII format and .meshb for @@ -2010,6 +2013,8 @@ class Mesh: # @ingroup l2_grps_create def MakeGroupByIds(self, groupName, elementType, elemIDs): group = self.mesh.CreateGroup(elementType, groupName) + if isinstance( elemIDs, Mesh ): + elemIDs = elemIDs.GetMesh() if hasattr( elemIDs, "GetIDs" ): if hasattr( elemIDs, "SetMesh" ): elemIDs.SetMesh( self.GetMesh() ) -- 2.39.2