MED write/append with a lower major version of MED file format: second step

This commit is contained in:
Paul RASCLE 2018-10-09 15:06:17 +02:00
parent 9cef4666a1
commit 90761b482c
10 changed files with 77 additions and 38 deletions

View File

@ -61,12 +61,13 @@ DriverMED_W_SMESHDS_Mesh::DriverMED_W_SMESHDS_Mesh():
myDoGroupOfBalls(false), myDoGroupOfBalls(false),
myAutoDimension(false), myAutoDimension(false),
myAddODOnVertices(false), myAddODOnVertices(false),
myDoAllInGroups(false) myDoAllInGroups(false),
myVersion(-1)
{} {}
void DriverMED_W_SMESHDS_Mesh::SetFile(const std::string& theFileName, int theMinor) void DriverMED_W_SMESHDS_Mesh::SetFile(const std::string& theFileName, int theVersion)
{ {
myMinor = theMinor; myVersion = theVersion;
Driver_SMESHDS_Mesh::SetFile(theFileName); Driver_SMESHDS_Mesh::SetFile(theFileName);
} }
@ -74,7 +75,7 @@ void DriverMED_W_SMESHDS_Mesh::SetFile(const std::string& theFileName, int theMi
* MED version is either the latest available, or with an inferior minor, * MED version is either the latest available, or with an inferior minor,
* to ensure backward compatibility on writing med files. * to ensure backward compatibility on writing med files.
*/ */
string DriverMED_W_SMESHDS_Mesh::GetVersionString(int theVersion, int theNbDigits) string DriverMED_W_SMESHDS_Mesh::GetVersionString(int theMinor, int theNbDigits)
{ {
TInt majeur, mineur, release; TInt majeur, mineur, release;
majeur=MED_MAJOR_NUM; majeur=MED_MAJOR_NUM;
@ -82,12 +83,12 @@ string DriverMED_W_SMESHDS_Mesh::GetVersionString(int theVersion, int theNbDigit
release=MED_RELEASE_NUM; release=MED_RELEASE_NUM;
TInt imposedMineur = mineur; TInt imposedMineur = mineur;
if (theVersion < 0) if (theMinor < 0)
imposedMineur = mineur; imposedMineur = mineur;
else if (theVersion > MED_MINOR_NUM) else if (theMinor > MED_MINOR_NUM)
imposedMineur = mineur; imposedMineur = mineur;
else else
imposedMineur = theVersion; imposedMineur = theMinor;
ostringstream name; ostringstream name;
if ( theNbDigits > 0 ) if ( theNbDigits > 0 )
@ -457,7 +458,7 @@ Driver_Mesh::Status DriverMED_W_SMESHDS_Mesh::Perform()
} }
} }
MED::PWrapper myMed = CrWrapperW(myFile, myMinor); MED::PWrapper myMed = CrWrapperW(myFile, myVersion);
PMeshInfo aMeshInfo = myMed->CrMeshInfo(aMeshDimension,aSpaceDimension,aMeshName); PMeshInfo aMeshInfo = myMed->CrMeshInfo(aMeshDimension,aSpaceDimension,aMeshName);
//MESSAGE("Add - aMeshName : "<<aMeshName<<"; "<<aMeshInfo->GetName()); //MESSAGE("Add - aMeshName : "<<aMeshName<<"; "<<aMeshInfo->GetName());
myMed->SetMeshInfo(aMeshInfo); myMed->SetMeshInfo(aMeshInfo);

View File

@ -48,10 +48,10 @@ class MESHDRIVERMED_EXPORT DriverMED_W_SMESHDS_Mesh: public Driver_SMESHDS_Mesh
DriverMED_W_SMESHDS_Mesh(); DriverMED_W_SMESHDS_Mesh();
void SetFile(const std::string& theFileName, int theMinor=-1); void SetFile(const std::string& theFileName, int theVersion=-1);
void SetAutoDimension(bool toFindOutDimension) { myAutoDimension = toFindOutDimension; } void SetAutoDimension(bool toFindOutDimension) { myAutoDimension = toFindOutDimension; }
static std::string GetVersionString(int theVersion, int theNbDigits=2); static std::string GetVersionString(int theMinor, int theNbDigits=2);
void AddGroupOfNodes(); void AddGroupOfNodes();
void AddGroupOfEdges(); void AddGroupOfEdges();
@ -89,7 +89,7 @@ class MESHDRIVERMED_EXPORT DriverMED_W_SMESHDS_Mesh: public Driver_SMESHDS_Mesh
bool myAutoDimension; bool myAutoDimension;
bool myAddODOnVertices; bool myAddODOnVertices;
bool myDoAllInGroups; bool myDoAllInGroups;
int myMinor; int myVersion;
}; };
#endif #endif

View File

@ -164,10 +164,45 @@ namespace MED
return new MED::TWrapper(fileName); return new MED::TWrapper(fileName);
} }
PWrapper CrWrapperW(const std::string& fileName, int theMinor) PWrapper CrWrapperW(const std::string& fileName, int theVersion)
{ {
bool isCreated = false;
if (!CheckCompatibility(fileName, true)) if (!CheckCompatibility(fileName, true))
{
remove(fileName.c_str()); remove(fileName.c_str());
return new MED::TWrapper(fileName, theMinor); isCreated = true;
}
int minor = -1;
if (isCreated)
{
med_int wantedMajor = MED_MAJOR_NUM;
med_int wantedMinor = MED_MINOR_NUM;
if (theVersion > 0)
{
wantedMajor = theVersion/10;
wantedMinor = theVersion%10;
}
if (wantedMajor == MED_MAJOR_NUM) // the med file will be actually created
{
if (wantedMinor < MED_MINOR_NUM)
minor = wantedMinor;
}
else // an empty existing med file of the right version will be used for append
{
int medVersionsOK[] = MED_VERSIONS_APPEND_COMPATIBLE;
bool isVersionOK = false;
for (int ii=0; ii < sizeof(medVersionsOK)/sizeof(int); ii++)
if (medVersionsOK[ii] == theVersion)
{
isVersionOK =true;
break;
}
if (isVersionOK) // copy an empty existing med file of the right version, for append
{
MESSAGE("copy an empty existing med file of the right version, for append" << theVersion);
}
}
}
return new MED::TWrapper(fileName, minor);
} }
} }

View File

@ -47,7 +47,7 @@ namespace MED
PWrapper CrWrapperR( const std::string& ); PWrapper CrWrapperR( const std::string& );
MEDWRAPPER_EXPORT MEDWRAPPER_EXPORT
PWrapper CrWrapperW( const std::string&, int theMinor=-1 ); PWrapper CrWrapperW( const std::string&, int theVersion=-1 );
} }
#endif // MED_Factory_HeaderFile #endif // MED_Factory_HeaderFile

View File

@ -52,7 +52,7 @@ namespace MED
TWrapper& operator=(const TWrapper&); TWrapper& operator=(const TWrapper&);
public: public:
TWrapper(const std::string& theFileName, TInt theMinor=-1); TWrapper(const std::string& theFileName, TInt theVersion=-1);
virtual virtual
~TWrapper(); ~TWrapper();

View File

@ -1421,18 +1421,18 @@ bool SMESH_Mesh::HasDuplicatedGroupNamesMED()
void SMESH_Mesh::ExportMED(const char * file, void SMESH_Mesh::ExportMED(const char * file,
const char* theMeshName, const char* theMeshName,
bool theAutoGroups, bool theAutoGroups,
int theMinor, int theVersion,
const SMESHDS_Mesh* meshPart, const SMESHDS_Mesh* meshPart,
bool theAutoDimension, bool theAutoDimension,
bool theAddODOnVertices, bool theAddODOnVertices,
bool theAllElemsToGroup) bool theAllElemsToGroup)
throw(SALOME_Exception) throw(SALOME_Exception)
{ {
//MESSAGE("MED_VERSION:"<< theVersion); MESSAGE("MED_VERSION:"<< theVersion);
SMESH_TRY; SMESH_TRY;
DriverMED_W_SMESHDS_Mesh myWriter; DriverMED_W_SMESHDS_Mesh myWriter;
myWriter.SetFile ( file , theMinor); myWriter.SetFile ( file , theVersion);
myWriter.SetMesh ( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS ); myWriter.SetMesh ( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
myWriter.SetAutoDimension( theAutoDimension ); myWriter.SetAutoDimension( theAutoDimension );
myWriter.AddODOnVertices ( theAddODOnVertices ); myWriter.AddODOnVertices ( theAddODOnVertices );

View File

@ -254,7 +254,7 @@ class SMESH_EXPORT SMESH_Mesh
void ExportMED(const char * theFile, void ExportMED(const char * theFile,
const char* theMeshName = NULL, const char* theMeshName = NULL,
bool theAutoGroups = true, bool theAutoGroups = true,
int TheMinor = -1, int theVersion = -1,
const SMESHDS_Mesh* theMeshPart = 0, const SMESHDS_Mesh* theMeshPart = 0,
bool theAutoDimension = false, bool theAutoDimension = false,
bool theAddODOnVertices = false, bool theAddODOnVertices = false,

View File

@ -655,7 +655,7 @@ namespace
// Get parameters of export operation // Get parameters of export operation
QString aFilename; QString aFilename;
int aFormat =-1; // for MED minor versions int aFormat =-1; // for MED version used for write
bool isOkToWrite = true; // to check MED file version compatibility before adding a mesh in an existing file bool isOkToWrite = true; // to check MED file version compatibility before adding a mesh in an existing file
// Init the parameters with the default values // Init the parameters with the default values
@ -744,36 +744,39 @@ namespace
} }
else if ( isMED || isSAUV ) // Export to MED or SAUV else if ( isMED || isSAUV ) // Export to MED or SAUV
{ {
int defaultVersion = 0;
QMap<QString, int> aFilterMap; QMap<QString, int> aFilterMap;
if ( isMED ) { if ( isMED ) {
//filters << QObject::tr( "MED_FILES_FILTER" ) + " (*.med)"; //filters << QObject::tr( "MED_FILES_FILTER" ) + " (*.med)";
//QString vmed (aMesh->GetVersionString(-1, 2)); //QString vmed (aMesh->GetVersionString(-1, 2));
//MESSAGE("MED version: " << vmed.toStdString()); //MESSAGE("MED version: " << vmed.toStdString());
SMESH::long_array_var mvok = aMesh->GetMEDVersionsCompatibleForAppend(); SMESH::long_array_var mvok = aMesh->GetMEDVersionsCompatibleForAppend();
for ( int i = 0; i < mvok->length(); ++i ) for ( int i = 0; i < mvok->length(); ++i ) // i=0 must correspond to the current version to set the default filter on it
{ {
int versionInt = mvok[i]; int versionInt = mvok[i];
if (i == 0)
defaultVersion = versionInt;
std::ostringstream vss; std::ostringstream vss;
vss << versionInt/10; vss << versionInt/10;
vss << "."; vss << ".";
vss << versionInt%10; vss << versionInt%10;
QString vs = vss.str().c_str(); QString vs = vss.str().c_str();
MESSAGE("MED version: " << vs.toStdString()); MESSAGE("MED version: " << vs.toStdString());
aFilterMap.insert( QObject::tr( "MED_VX_FILES_FILTER" ).arg( vs ) + " (*.med)", i); aFilterMap.insert( QObject::tr( "MED_VX_FILES_FILTER" ).arg( vs ) + " (*.med)", versionInt);
} }
} }
else { // isSAUV else { // isSAUV
aFilterMap.insert("All files (*)", -1 ); aFilterMap.insert("All files (*)", -1 );
aFilterMap.insert("SAUV files (*.sauv)", 0 ); aFilterMap.insert("SAUV files (*.sauv)", defaultVersion ); // 0 = default filter (defaultVersion)
aFilterMap.insert("SAUV files (*.sauve)", -1 ); aFilterMap.insert("SAUV files (*.sauve)", -1 );
} }
MESSAGE("default version="<< defaultVersion);
QStringList filters; QStringList filters;
QMap<QString, int>::const_iterator it = aFilterMap.begin(); QMap<QString, int>::const_iterator it = aFilterMap.begin();
QString aDefaultFilter = it.key(); QString aDefaultFilter = it.key();
for ( ; it != aFilterMap.end(); ++it ) { for ( ; it != aFilterMap.end(); ++it ) {
filters.push_back( it.key() ); filters.push_back( it.key() );
if (it.value() == 0) // explicit default for MED = current MED version if (it.value() == defaultVersion) // explicit default for MED = current MED version
aDefaultFilter = it.key(); aDefaultFilter = it.key();
} }
QStringList checkBoxes; QStringList checkBoxes;
@ -819,13 +822,13 @@ namespace
break; break;
} }
aFormat = aFilterMap[fd->selectedNameFilter()]; aFormat = aFilterMap[fd->selectedNameFilter()];
MESSAGE("selected minor: " << aFormat << " file: " << aFilename.toUtf8().constData()); MESSAGE("selected version: " << aFormat << " file: " << aFilename.toUtf8().constData());
toOverwrite = fv->isOverwrite(aFilename); toOverwrite = fv->isOverwrite(aFilename);
MESSAGE("toOverwrite:" << toOverwrite); MESSAGE("toOverwrite:" << toOverwrite);
is_ok = true; is_ok = true;
if ( !aFilename.isEmpty() ) { if ( !aFilename.isEmpty() ) {
if( !toOverwrite ) { if( !toOverwrite ) {
// can't append to an existing using other format // append is only possible if the existing file format is compatible
bool isVersionOk = SMESHGUI::GetSMESHGen()->CheckWriteCompatibility( aFilename.toUtf8().constData() ); bool isVersionOk = SMESHGUI::GetSMESHGen()->CheckWriteCompatibility( aFilename.toUtf8().constData() );
MESSAGE("Append check, isVersionOk:" << isVersionOk); MESSAGE("Append check, isVersionOk:" << isVersionOk);
if ( !isVersionOk ) { if ( !isVersionOk ) {

View File

@ -3079,7 +3079,7 @@ string SMESH_Mesh_i::prepareMeshNameAndGroups(const char* file,
void SMESH_Mesh_i::ExportMED(const char* file, void SMESH_Mesh_i::ExportMED(const char* file,
CORBA::Boolean auto_groups, CORBA::Boolean auto_groups,
CORBA::Long minor, CORBA::Long version,
CORBA::Boolean overwrite, CORBA::Boolean overwrite,
CORBA::Boolean autoDimension) CORBA::Boolean autoDimension)
throw(SALOME::SALOME_Exception) throw(SALOME::SALOME_Exception)
@ -3090,12 +3090,12 @@ void SMESH_Mesh_i::ExportMED(const char* file,
_preMeshInfo->FullLoadFromFile(); _preMeshInfo->FullLoadFromFile();
string aMeshName = prepareMeshNameAndGroups(file, overwrite); string aMeshName = prepareMeshNameAndGroups(file, overwrite);
_impl->ExportMED( file, aMeshName.c_str(), auto_groups, minor, 0, autoDimension ); _impl->ExportMED( file, aMeshName.c_str(), auto_groups, version, 0, autoDimension );
TPythonDump() << SMESH::SMESH_Mesh_var(_this()) << ".ExportMED( r'" TPythonDump() << SMESH::SMESH_Mesh_var(_this()) << ".ExportMED( r'"
<< file << "', " << file << "', "
<< "auto_groups=" <<auto_groups << ", " << "auto_groups=" <<auto_groups << ", "
<< "minor=" << minor << ", " << "minor=" << version << ", "
<< "overwrite=" << overwrite << ", " << "overwrite=" << overwrite << ", "
<< "meshPart=None, " << "meshPart=None, "
<< "autoDimension=" << autoDimension << " )"; << "autoDimension=" << autoDimension << " )";
@ -3208,14 +3208,14 @@ void SMESH_Mesh_i::ExportSTL (const char *file, const bool isascii)
void SMESH_Mesh_i::ExportPartToMED(SMESH::SMESH_IDSource_ptr meshPart, void SMESH_Mesh_i::ExportPartToMED(SMESH::SMESH_IDSource_ptr meshPart,
const char* file, const char* file,
CORBA::Boolean auto_groups, CORBA::Boolean auto_groups,
CORBA::Long minor, CORBA::Long version,
CORBA::Boolean overwrite, CORBA::Boolean overwrite,
CORBA::Boolean autoDimension, CORBA::Boolean autoDimension,
const GEOM::ListOfFields& fields, const GEOM::ListOfFields& fields,
const char* geomAssocFields) const char* geomAssocFields)
throw (SALOME::SALOME_Exception) throw (SALOME::SALOME_Exception)
{ {
//MESSAGE("MED minor version: "<< minor); MESSAGE("MED version: "<< version);
SMESH_TRY; SMESH_TRY;
if ( _preMeshInfo ) if ( _preMeshInfo )
_preMeshInfo->FullLoadFromFile(); _preMeshInfo->FullLoadFromFile();
@ -3262,7 +3262,7 @@ void SMESH_Mesh_i::ExportPartToMED(SMESH::SMESH_IDSource_ptr meshPart,
SMESH::DownCast< SMESH_Mesh_i* >( meshPart )) SMESH::DownCast< SMESH_Mesh_i* >( meshPart ))
{ {
aMeshName = prepareMeshNameAndGroups(file, overwrite); aMeshName = prepareMeshNameAndGroups(file, overwrite);
_impl->ExportMED( file, aMeshName.c_str(), auto_groups, minor, _impl->ExportMED( file, aMeshName.c_str(), auto_groups, version,
0, autoDimension, /*addODOnVertices=*/have0dField); 0, autoDimension, /*addODOnVertices=*/have0dField);
meshDS = _impl->GetMeshDS(); meshDS = _impl->GetMeshDS();
} }
@ -3280,7 +3280,7 @@ void SMESH_Mesh_i::ExportPartToMED(SMESH::SMESH_IDSource_ptr meshPart,
} }
SMESH_MeshPartDS* partDS = new SMESH_MeshPartDS( meshPart ); SMESH_MeshPartDS* partDS = new SMESH_MeshPartDS( meshPart );
_impl->ExportMED( file, aMeshName.c_str(), auto_groups, minor, _impl->ExportMED( file, aMeshName.c_str(), auto_groups, version,
partDS, autoDimension, /*addODOnVertices=*/have0dField); partDS, autoDimension, /*addODOnVertices=*/have0dField);
meshDS = tmpDSDeleter._obj = partDS; meshDS = tmpDSDeleter._obj = partDS;
} }
@ -3309,7 +3309,7 @@ void SMESH_Mesh_i::ExportPartToMED(SMESH::SMESH_IDSource_ptr meshPart,
<< meshPart << ", r'" << meshPart << ", r'"
<< file << "', " << file << "', "
<< auto_groups << ", " << auto_groups << ", "
<< minor << ", " << version << ", "
<< overwrite << ", " << overwrite << ", "
<< autoDimension << ", " << autoDimension << ", "
<< goList << ", '" << goList << ", '"

View File

@ -239,7 +239,7 @@ public:
void ExportMED( const char* file, void ExportMED( const char* file,
CORBA::Boolean auto_groups, CORBA::Boolean auto_groups,
CORBA::Long minor, CORBA::Long version,
CORBA::Boolean overwrite, CORBA::Boolean overwrite,
CORBA::Boolean autoDimension = true) throw (SALOME::SALOME_Exception); CORBA::Boolean autoDimension = true) throw (SALOME::SALOME_Exception);
@ -259,7 +259,7 @@ public:
void ExportPartToMED(SMESH::SMESH_IDSource_ptr meshPart, void ExportPartToMED(SMESH::SMESH_IDSource_ptr meshPart,
const char* file, const char* file,
CORBA::Boolean auto_groups, CORBA::Boolean auto_groups,
CORBA::Long minor, CORBA::Long version,
CORBA::Boolean overwrite, CORBA::Boolean overwrite,
CORBA::Boolean autoDim, CORBA::Boolean autoDim,
const GEOM::ListOfFields& fields, const GEOM::ListOfFields& fields,