From: Sergey POKHODENKO Date: Thu, 14 May 2015 11:25:27 +0000 (+0300) Subject: Issue #529 : 4.07. Import IGES, export to BREP, STEP, IGES - Export IGES X-Git-Tag: V_1.2.0~145^2~8 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=eaa34d7803a364b8da51b6dde8b0ee3c469ae27a;p=modules%2Fshaper.git Issue #529 : 4.07. Import IGES, export to BREP, STEP, IGES - Export IGES --- diff --git a/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp b/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp index 7af9d2af8..57843ebc3 100644 --- a/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp +++ b/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp @@ -10,13 +10,17 @@ #include //#include //#include -//#include +#include -#include #include #include +#include + +#include + #include +#include #include #include #include @@ -56,6 +60,7 @@ const std::string& ExchangePlugin_ExportFeature::getKind() void ExchangePlugin_ExportFeature::initAttributes() { data()->addAttribute(ExchangePlugin_ExportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId()); + data()->addAttribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()); } /* @@ -66,15 +71,29 @@ void ExchangePlugin_ExportFeature::execute() AttributeStringPtr aFilePathAttr = std::dynamic_pointer_cast( data()->attribute(ExchangePlugin_ExportFeature::FILE_PATH_ID())); std::string aFilePath = aFilePathAttr->value(); - if(aFilePath.empty()) + if (aFilePath.empty()) return; - exportFile(aFilePath); + + AttributeSelectionListPtr aSelectionListAttr = + std::dynamic_pointer_cast( + data()->attribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID())); + + std::list > aShapes; + for ( int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i ) { + std::shared_ptr anSelectionAttr = aSelectionListAttr->value(i); + aShapes.push_back(anSelectionAttr->value()); + } + std::shared_ptr aShape = + GeomAlgoAPI_CompoundBuilder::compound(aShapes); + + exportFile(aFilePath, aShape); } -bool ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName) +bool ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName, + std::shared_ptr theShape) { // retrieve the file and plugin library names - TCollection_AsciiString aFileName (theFileName.c_str()); + TCollection_AsciiString aFileName(theFileName.c_str()); OSD_Path aPath(aFileName); TCollection_AsciiString aFormatName = aPath.Extension(); // ".brep" -> "BREP", TCollection_AsciiString are numbered from 1 @@ -85,50 +104,22 @@ bool ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName) TCollection_AsciiString anError; TDF_Label anUnknownLabel = TDF_Label(); - TopoDS_Shape aShape; -// if (aFormatName == "BREP") { + TopoDS_Shape aShape(theShape->impl()); + bool aResult = true; + if (aFormatName == "BREP") { // aShape = BREPExport::Export(aFileName, aFormatName, anError, anUnknownLabel); -// } else if (aFormatName == "STEP" || aFormatName == "STP") { + } else if (aFormatName == "STEP" || aFormatName == "STP") { // aShape = STEPExport::Export(aFileName, aFormatName, anError, anUnknownLabel); -// } else if (aFormatName == "IGES") { -// aShape = IGESExport::Export(aFileName, aFormatName, anError, anUnknownLabel); -// } -// // Check if shape is valid -// if ( aShape.IsNull() ) { -// const static std::string aShapeError = -// "An error occurred while exporting " + theFileName + ": " + anError.ToCString(); -// setError(aShapeError); -// return false; -// } -// -// // Pass the results into the model -// std::string anObjectName = aPath.Name().ToCString(); -// data()->setName(anObjectName); -// std::shared_ptr aResultBody = document()->createBody(data()); -// std::shared_ptr aGeomShape(new GeomAPI_Shape); -// aGeomShape->setImpl(new TopoDS_Shape(aShape)); -// -// //LoadNamingDS of the exported shape -// loadNamingDS(aGeomShape, aResultBody); -// -// setResult(aResultBody); + } else if (aFormatName == "IGES") { + aResult = IGESExport::Export(aFileName, aFormatName, aShape, anError, anUnknownLabel); + } - return true; -} + if ( !aResult ) { + const static std::string aShapeError = + "An error occurred while exporting " + theFileName + ": " + anError.ToCString(); + setError(aShapeError); + return false; + } -//============================================================================ -void ExchangePlugin_ExportFeature::loadNamingDS( - std::shared_ptr theGeomShape, - std::shared_ptr theResultBody) -{ - //load result - theResultBody->store(theGeomShape); - - int aTag(1); - std::string aNameMS = "Shape"; - theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag); - //std::string aNameDE = "DiscEdges"; - //theResultBody->loadDisconnectedEdges(theGeomShape, aNameDE, aTag); - //std::string aNameDV = "DiscVertexes"; - //theResultBody->loadDisconnectedVertexes(theGeomShape, aNameDV, aTag); + return true; } diff --git a/src/ExchangePlugin/ExchangePlugin_ExportFeature.h b/src/ExchangePlugin/ExchangePlugin_ExportFeature.h index 94160a860..b308e12fc 100644 --- a/src/ExchangePlugin/ExchangePlugin_ExportFeature.h +++ b/src/ExchangePlugin/ExchangePlugin_ExportFeature.h @@ -18,18 +18,23 @@ class ExchangePlugin_ExportFeature : public ModelAPI_Feature { public: - /// Extrusion kind inline static const std::string& ID() { static const std::string MY_EXPORT_ID("Export"); return MY_EXPORT_ID; } - /// attribute name of referenced face + /// attribute name of file path inline static const std::string& FILE_PATH_ID() { static const std::string MY_FILE_PATH_ID("export_file_selector"); return MY_FILE_PATH_ID; } + /// attribute name of selection list + inline static const std::string& SELECTION_LIST_ID() + { + static const std::string MY_SELECTION_LIST_ID("selection_list"); + return MY_SELECTION_LIST_ID; + } /// default constructor EXCHANGEPLUGIN_EXPORT ExchangePlugin_ExportFeature(); /// default destructor @@ -51,12 +56,8 @@ class ExchangePlugin_ExportFeature : public ModelAPI_Feature protected: /// Performs the export of the file - EXCHANGEPLUGIN_EXPORT bool exportFile(const std::string& theFileName); - -private: - /// Loads Naming data structure to the document - void loadNamingDS(std::shared_ptr theGeomShape, - std::shared_ptr theResultBody); + EXCHANGEPLUGIN_EXPORT bool exportFile(const std::string& theFileName, + std::shared_ptr theShape); }; #endif /* EXPORT_EXPORTFEATURE_H_ */ diff --git a/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp b/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp index 86d0fae39..f5bf2bb11 100644 --- a/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp +++ b/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp @@ -74,7 +74,7 @@ void ExchangePlugin_ImportFeature::execute() bool ExchangePlugin_ImportFeature::importFile(const std::string& theFileName) { // retrieve the file and plugin library names - TCollection_AsciiString aFileName (theFileName.c_str()); + TCollection_AsciiString aFileName(theFileName.c_str()); OSD_Path aPath(aFileName); TCollection_AsciiString aFormatName = aPath.Extension(); // ".brep" -> "BREP", TCollection_AsciiString are numbered from 1 @@ -118,12 +118,12 @@ bool ExchangePlugin_ImportFeature::importFile(const std::string& theFileName) //============================================================================ void ExchangePlugin_ImportFeature::loadNamingDS( - std::shared_ptr theGeomShape, - std::shared_ptr theResultBody) -{ + std::shared_ptr theGeomShape, + std::shared_ptr theResultBody) +{ //load result theResultBody->store(theGeomShape); - + int aTag(1); std::string aNameMS = "Shape"; theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag); diff --git a/src/ExchangePlugin/ExchangePlugin_ImportFeature.h b/src/ExchangePlugin/ExchangePlugin_ImportFeature.h index c0f259f45..b1fc310ad 100644 --- a/src/ExchangePlugin/ExchangePlugin_ImportFeature.h +++ b/src/ExchangePlugin/ExchangePlugin_ImportFeature.h @@ -50,7 +50,7 @@ class ExchangePlugin_ImportFeature : public ModelAPI_Feature } protected: - /// POerforms the import of the file + /// Performs the import of the file EXCHANGEPLUGIN_EXPORT bool importFile(const std::string& theFileName); private: diff --git a/src/ExchangePlugin/ExchangePlugin_Plugin.cpp b/src/ExchangePlugin/ExchangePlugin_Plugin.cpp index 94a55fd8f..c843f6cc1 100644 --- a/src/ExchangePlugin/ExchangePlugin_Plugin.cpp +++ b/src/ExchangePlugin/ExchangePlugin_Plugin.cpp @@ -29,6 +29,8 @@ ExchangePlugin_Plugin::ExchangePlugin_Plugin() ModelAPI_ValidatorsFactory* aFactory = aSession->validators(); aFactory->registerValidator("ExchangePlugin_ImportFormat", new ExchangePlugin_ImportFormatValidator); + aFactory->registerValidator("ExchangePlugin_ExportFormat", + new ExchangePlugin_ExportFormatValidator); // register construction properties //Config_PropManager::registerProp("Visualization", "import_feature_color", "Imported feature color", diff --git a/src/ExchangePlugin/ExchangePlugin_Validators.cpp b/src/ExchangePlugin/ExchangePlugin_Validators.cpp index 5dc974e72..5a62fea8f 100644 --- a/src/ExchangePlugin/ExchangePlugin_Validators.cpp +++ b/src/ExchangePlugin/ExchangePlugin_Validators.cpp @@ -15,8 +15,8 @@ #include #include -bool ExchangePlugin_ImportFormatValidator::parseFormats(const std::list& theArguments, - std::list& outFormats) +bool ExchangePlugin_FormatValidator::parseFormats(const std::list& theArguments, + std::list& outFormats) { std::list::const_iterator it = theArguments.begin(); bool result = true; @@ -37,32 +37,32 @@ bool ExchangePlugin_ImportFormatValidator::parseFormats(const std::list& theArguments) const +bool ExchangePlugin_FormatValidator::isValid(const AttributePtr& theAttribute, + const std::list& theArguments) const { - SessionPtr aMgr = ModelAPI_Session::get(); - ModelAPI_ValidatorsFactory* aFactory = aMgr->validators(); - if (theAttribute->isInitialized()) { - const AttributeStringPtr aStrAttr = - std::dynamic_pointer_cast(theAttribute); - if(!aStrAttr) - return false; - std::string aFileName = aStrAttr->value(); - if (!aFileName.empty()) { - std::list aFormats; - ExchangePlugin_ImportFormatValidator::parseFormats(theArguments, aFormats); - std::list::const_iterator itFormats = aFormats.begin(); - size_t aFileNameLen = aFileName.length(); - std::transform(aFileName.begin(), aFileName.end(), aFileName.begin(), toupper); - // Is file name ends with the format - for (; itFormats != aFormats.end(); ++itFormats) { - size_t aFormatBeginPos = aFileNameLen - (*itFormats).length(); - if (aFileName.compare(aFormatBeginPos, std::string::npos, *itFormats) == 0) { - return true; - } - } + if (!theAttribute->isInitialized()) + return false; + + const AttributeStringPtr aStrAttr = + std::dynamic_pointer_cast(theAttribute); + if (!aStrAttr) + return false; + + std::string aFileName = aStrAttr->value(); + if (aFileName.empty()) + return false; + + std::list aFormats; + ExchangePlugin_FormatValidator::parseFormats(theArguments, aFormats); + std::list::const_iterator itFormats = aFormats.begin(); + size_t aFileNameLen = aFileName.length(); + std::transform(aFileName.begin(), aFileName.end(), aFileName.begin(), toupper); + // Is file name ends with the format + for (; itFormats != aFormats.end(); ++itFormats) { + size_t aFormatBeginPos = aFileNameLen - (*itFormats).length(); + if (aFileName.compare(aFormatBeginPos, std::string::npos, *itFormats) == 0) { + return true; } } return false; } - diff --git a/src/ExchangePlugin/ExchangePlugin_Validators.h b/src/ExchangePlugin/ExchangePlugin_Validators.h index 3f1bb793b..3e9ebb912 100644 --- a/src/ExchangePlugin/ExchangePlugin_Validators.h +++ b/src/ExchangePlugin/ExchangePlugin_Validators.h @@ -18,11 +18,11 @@ * about which formats are supported and the extension of the associated files. * This validator filters out files that are out of this description. */ -class ExchangePlugin_ImportFormatValidator : public ModelAPI_AttributeValidator +class ExchangePlugin_FormatValidator : public ModelAPI_AttributeValidator { /** - * Parses input arguments "BREP:BREPImport", "STEP:STEPImport" - * into list of file formats "BREP","STEP" + * Parses input arguments "BREP:BREPImport", "STEP|STP:STEPImport" + * into list of file formats "BREP","STEP","STP" * and list of corresponding plugins: "BREPImport", "STEPImport" */ static bool parseFormats(const std::list& theArguments, @@ -34,8 +34,15 @@ public: */ virtual bool isValid(const AttributePtr& theAttribute, const std::list& theArguments) const; +}; +class ExchangePlugin_ImportFormatValidator : public ExchangePlugin_FormatValidator +{ +}; + +class ExchangePlugin_ExportFormatValidator : public ExchangePlugin_FormatValidator +{ }; diff --git a/src/ExchangePlugin/plugin-Exchange.xml b/src/ExchangePlugin/plugin-Exchange.xml index a389bcec1..3c5111a6f 100644 --- a/src/ExchangePlugin/plugin-Exchange.xml +++ b/src/ExchangePlugin/plugin-Exchange.xml @@ -9,9 +9,10 @@ - - + + + diff --git a/src/GeomAlgoAPI/CMakeLists.txt b/src/GeomAlgoAPI/CMakeLists.txt index 944f43382..44dd3cb42 100644 --- a/src/GeomAlgoAPI/CMakeLists.txt +++ b/src/GeomAlgoAPI/CMakeLists.txt @@ -22,6 +22,7 @@ SET(PROJECT_HEADERS GeomAlgoAPI_BREPImport.h GeomAlgoAPI_STEPImport.h GeomAlgoAPI_IGESImport.h + GeomAlgoAPI_IGESExport.h ) SET(PROJECT_SOURCES @@ -40,6 +41,7 @@ SET(PROJECT_SOURCES GeomAlgoAPI_BREPImport.cpp GeomAlgoAPI_STEPImport.cpp GeomAlgoAPI_IGESImport.cpp + GeomAlgoAPI_IGESExport.cpp ) SET(PROJECT_LIBRARIES diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_IGESExport.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_IGESExport.cpp new file mode 100644 index 000000000..4a7b52bf6 --- /dev/null +++ b/src/GeomAlgoAPI/GeomAlgoAPI_IGESExport.cpp @@ -0,0 +1,134 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + +#include + +//// KERNEL includes +//#include +//#include + +// OOCT includes +#include +#include +#include + +#include +#include + +//============================================================================= +/*! + * + */ +//============================================================================= +/*! + * KindOfBRep + * \return 0 if theShape contains only simple entities (wires, edges and vertices), + * 1 if theShape contains only complex entities (shells, solids and compsolids) + * 2 if theShape contains only indifferent entities (faces) + * -1 if theShape contains both simple and complex entities (and in this case it + * cannot be saved without any loss neither in BRepMode == 0 nor in BRepMode == 1) + */ +//============================================================================= +int KindOfBRep (const TopoDS_Shape& theShape) +{ + int aKind = 2; + + switch (theShape.ShapeType()) + { + case TopAbs_COMPOUND: + { + bool isSimple = false; + bool isComplex = false; + TopoDS_Iterator anIt (theShape, Standard_True, Standard_True); + for (; anIt.More(); anIt.Next()) { + TopoDS_Shape aS = anIt.Value(); + int aKindSub = KindOfBRep(aS); + if (aKindSub == 0) + isSimple = true; + else if (aKindSub == 1) + isComplex = true; + else if (aKindSub == -1) { + return -1; // heterogeneous + } + } + if (isSimple && isComplex) + aKind = -1; // heterogeneous + else if (isSimple) + aKind = 0; + else if (isComplex) + aKind = 1; + } + break; + case TopAbs_COMPSOLID: + case TopAbs_SOLID: + case TopAbs_SHELL: + aKind = 1; + break; + case TopAbs_WIRE: + case TopAbs_EDGE: + case TopAbs_VERTEX: + aKind = 0; + break; + default: + aKind = 2; + } + + return aKind; +} + +//============================================================================= +//extern "C" { +namespace IGESExport { + +bool Export(const TCollection_AsciiString& theFileName, + const TCollection_AsciiString&, + const TopoDS_Shape& theShape, + TCollection_AsciiString& theError, const TDF_Label&) +{ + //TODO(spo): pass version as argument of the function + TCollection_AsciiString aVersion = "5.3"; + // define, whether to write only faces (5.1 IGES format) + // or shells and solids also (5.3 IGES format) + int aBrepMode = 0; + if( aVersion.IsEqual( "5.3" ) ) + aBrepMode = 1; + + #ifdef _DEBUG + std::cout << "Export IGES into file " << theFileName.ToCString() << std::endl; + #endif + + // Mantis issue 0021350: check being exported shape, as some stand-alone + // entities (edges, wires and vertices) cannot be saved in BRepMode + if( aBrepMode == 1 ) { + int aKind = KindOfBRep( theShape ); + if( aKind == -1 ) { + theError = "EXPORT_IGES_HETEROGENEOUS_COMPOUND"; + return false; + } else if( aKind == 2 ) + aBrepMode = 1; + else + aBrepMode = aKind; + } + +// // Set "C" numeric locale to save numbers correctly +// Kernel_Utils::Localizer loc; + + // initialize writer + IGESControl_Controller::Init(); + IGESControl_Writer ICW( "M", aBrepMode ); // export explicitly in meters + Interface_Static::SetCVal( "xstep.cascade.unit", "M" ); + + // 09.03.2010 skl for bug 0020726 + // change default value "Average" to "Max" + Interface_Static::SetCVal( "write.precision.mode", "Max" ); + + // perform shape writing + if( ICW.AddShape( theShape ) ) { + ICW.ComputeModel(); + return (bool)ICW.Write( theFileName.ToCString() ); + } + return false; +} + +} + +//} diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_IGESExport.h b/src/GeomAlgoAPI/GeomAlgoAPI_IGESExport.h new file mode 100644 index 000000000..7a7f28d41 --- /dev/null +++ b/src/GeomAlgoAPI/GeomAlgoAPI_IGESExport.h @@ -0,0 +1,30 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + +/* + * GEOMALGOAPI_IGESExport.h + * + * Created on: Dec 24, 2014 + * Author: sbh + */ + +#ifndef GEOMALGOAPI_IGESEXPORT_H_ +#define GEOMALGOAPI_IGESEXPORT_H_ + +#include + +#include +#include +#include + +namespace IGESExport { + +/// Implementation of the import IGES files algorithms +GEOMALGOAPI_EXPORT +bool Export(const TCollection_AsciiString& theFileName, + const TCollection_AsciiString& theFormatName, + const TopoDS_Shape& theShape, + TCollection_AsciiString& theError, const TDF_Label&); + +} + +#endif /* GEOMALGOAPI_IGESEXPORT_H_ */ diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_IGESImport.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_IGESImport.cpp index cc46fda83..481c13521 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_IGESImport.cpp +++ b/src/GeomAlgoAPI/GeomAlgoAPI_IGESImport.cpp @@ -21,25 +21,11 @@ TopoDS_Shape Import(const TCollection_AsciiString& theFileName, std::cout << "Import IGES from file " << theFileName << std::endl; #endif TopoDS_Shape aResShape; - -// bool anIsIgnoreUnits = false; - IGESControl_Reader aReader; try { IFSelect_ReturnStatus status = aReader.ReadFile( theFileName.ToCString() ); if (status == IFSelect_RetDone) { -// if( anIsIgnoreUnits ) { -// // need re-scale a model, set UnitFlag to 'meter' -// Handle(IGESData_IGESModel) aModel = -// Handle(IGESData_IGESModel)::DownCast( aReader.Model() ); -// if( !aModel.IsNull() ) { -// IGESData_GlobalSection aGS = aModel->GlobalSection(); -// aGS.SetUnitFlag(6); -// aModel->SetGlobalSection(aGS); -// } -// } - #ifdef _DEBUG std::cout << "ImportIGES : all Geometry Transfer" << std::endl; #endif @@ -68,7 +54,7 @@ TopoDS_Shape Import(const TCollection_AsciiString& theFileName, default: theError = "Wrong format of the imported file. Can't import file."; break; - } + } aResShape.Nullify(); } } diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_IGESImport.h b/src/GeomAlgoAPI/GeomAlgoAPI_IGESImport.h index 312b3fc1e..c01f7c1d5 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_IGESImport.h +++ b/src/GeomAlgoAPI/GeomAlgoAPI_IGESImport.h @@ -3,8 +3,8 @@ /* * GEOMALGOAPI_IGESImport.h * - * Created on: Dec 24, 2014 - * Author: sbh + * Created on: May 14, 2015 + * Author: spo */ #ifndef GEOMALGOAPI_IGESIMPORT_H_ diff --git a/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp b/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp index f997592d1..c7fcacd11 100644 --- a/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp +++ b/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp @@ -36,6 +36,7 @@ ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent, : ModuleBase_ModelWidget(theParent, theData, theParentId) { myTitle = QString::fromStdString(theData->getProperty("title")); + myType = (theData->getProperty("type") == "save") ? WFS_SAVE : WFS_OPEN; myDefaultPath = QString::fromStdString(theData->getProperty("path")); QGridLayout* aMainLay = new QGridLayout(this); @@ -68,7 +69,7 @@ ModuleBase_WidgetFileSelector::~ModuleBase_WidgetFileSelector() bool ModuleBase_WidgetFileSelector::storeValueCustom() const { // A rare case when plugin was not loaded. - if(!myFeature) + if (!myFeature) return false; DataPtr aData = myFeature->data(); AttributeStringPtr aStringAttr = aData->string(attributeID()); @@ -81,7 +82,7 @@ bool ModuleBase_WidgetFileSelector::storeValueCustom() const bool ModuleBase_WidgetFileSelector::restoreValue() { // A rare case when plugin was not loaded. - if(!myFeature) + if (!myFeature) return false; DataPtr aData = myFeature->data(); AttributeStringPtr aStringAttr = aData->string(attributeID()); @@ -104,15 +105,16 @@ QList ModuleBase_WidgetFileSelector::getControls() const bool ModuleBase_WidgetFileSelector::isCurrentPathValid() { - QFileInfo aFile (myPathField->text()); + QFileInfo aFile(myPathField->text()); return aFile.exists(); } - void ModuleBase_WidgetFileSelector::onPathSelectionBtn() { - QString aFilter = formatsString(); - QString aFileName = QFileDialog::getOpenFileName(this, myTitle, myDefaultPath, aFilter); + QString aFilter = filterString(); + QString aFileName = (myType == WFS_SAVE) + ? QFileDialog::getSaveFileName(this, myTitle, myDefaultPath, aFilter) + : QFileDialog::getOpenFileName(this, myTitle, myDefaultPath, aFilter); if (!aFileName.isEmpty()) { myPathField->setText(aFileName); } @@ -120,13 +122,13 @@ void ModuleBase_WidgetFileSelector::onPathSelectionBtn() void ModuleBase_WidgetFileSelector::onPathChanged() { - if(!isCurrentPathValid()) + if (myType == WFS_OPEN && !isCurrentPathValid()) return; storeValue(); emit valuesChanged(); } -QString ModuleBase_WidgetFileSelector::formatsString() const +QString ModuleBase_WidgetFileSelector::filterString() const { QStringList aResult; QStringList aValidatorFormats = getValidatorFormats(); @@ -147,21 +149,22 @@ QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const std::list allValidators; std::list > allArguments; aFactory->validators(myFeature->getKind(), myAttributeID, allValidators, allArguments); - //TODO(sbh): extract as separate method - if(allArguments.empty()) - return QStringList(); + QStringList aResult; std::list anArgumentList = allArguments.front(); std::list::const_iterator it = anArgumentList.begin(); - QStringList aResult; for (; it != anArgumentList.end(); ++it) { - std::string anArg = *it; - int aSepPos = anArg.find(":"); - if (aSepPos == std::string::npos) { - continue; - } - QString aFormat = QString::fromStdString(anArg.substr(0, aSepPos)); - aFormat = aFormat.toUpper(); - aResult.append(aFormat); + QString aFormat = getFormat(*it); + if (!aFormat.isNull()) + aResult << aFormat; } return aResult; } + +QString ModuleBase_WidgetFileSelector::getFormat( const std::string& theArgument ) const +{ + QString anArgument = QString::fromStdString(theArgument); + if (!anArgument.contains(":")) + return QString(); + return anArgument.section(":", 0, 0).toUpper(); +} + diff --git a/src/ModuleBase/ModuleBase_WidgetFileSelector.h b/src/ModuleBase/ModuleBase_WidgetFileSelector.h index 744770c61..b89531cde 100644 --- a/src/ModuleBase/ModuleBase_WidgetFileSelector.h +++ b/src/ModuleBase/ModuleBase_WidgetFileSelector.h @@ -27,6 +27,7 @@ class QLineEdit; * \code * * @@ -68,11 +69,14 @@ protected: protected: /// Returns string containing formats - QString formatsString() const; + QString filterString() const; - /// Return list of validator formats + /// Returns list of validator formats QStringList getValidatorFormats() const; + /// Returns a format received from theArgument + QString getFormat( const std::string& theArgument ) const; + private: /// A control for path input QLineEdit* myPathField; @@ -80,6 +84,9 @@ protected: /// A title of open file dialog box QString myTitle; + /// A title of open file dialog box + enum { WFS_OPEN, WFS_SAVE } myType; + /// Default path QString myDefaultPath; };