ExchangePlugin_ImportFeature.h
ExchangePlugin_ExportFeature.h
ExchangePlugin_Validators.h
+ ExchangePlugin_Tools.h
)
SET(PROJECT_SOURCES
ExchangePlugin_ImportFeature.cpp
ExchangePlugin_ExportFeature.cpp
ExchangePlugin_Validators.cpp
+ ExchangePlugin_Tools.cpp
)
SET(XML_RESOURCES
#define EXCHANGEPLUGIN_H
#if defined EXCHANGEPLUGIN_EXPORTS
-#if defined WIN32
-#define EXCHANGEPLUGIN_EXPORT __declspec( dllexport )
+# if defined WIN32
+# define EXCHANGEPLUGIN_EXPORT __declspec( dllexport )
+# else
+# define EXCHANGEPLUGIN_EXPORT
+# endif
#else
-#define EXCHANGEPLUGIN_EXPORT
-#endif
-#else
-#if defined WIN32
-#define EXCHANGEPLUGIN_EXPORT __declspec( dllimport )
-#else
-#define EXCHANGEPLUGIN_EXPORT
-#endif
+# if defined WIN32
+# define EXCHANGEPLUGIN_EXPORT __declspec( dllimport )
+# else
+# define EXCHANGEPLUGIN_EXPORT
+# endif
#endif
#endif
*/
#include <ExchangePlugin_ExportFeature.h>
+
+#include <ExchangePlugin_Tools.h>
+
#include <GeomAlgoAPI_BREPExport.h>
#include <GeomAlgoAPI_STEPExport.h>
#include <GeomAlgoAPI_IGESExport.h>
#include <GeomAPI_Shape.h>
-#include <ModelAPI_AttributeString.h>
#include <ModelAPI_AttributeSelectionList.h>
+#include <ModelAPI_AttributeString.h>
#include <ModelAPI_Data.h>
#include <ModelAPI_Document.h>
#include <ModelAPI_Object.h>
#include <ModelAPI_ResultBody.h>
+
#include <TCollection_AsciiString.hxx>
#include <TDF_Label.hxx>
#include <TopoDS_Shape.hxx>
#include <OSD_Path.hxx>
#include <algorithm>
+#include <iterator>
#include <string>
#ifdef _DEBUG
#include <iostream>
*/
void ExchangePlugin_ExportFeature::initAttributes()
{
+ data()->addAttribute(ExchangePlugin_ExportFeature::FILE_FORMAT_ID(), ModelAPI_AttributeString::typeId());
data()->addAttribute(ExchangePlugin_ExportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
data()->addAttribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
}
*/
void ExchangePlugin_ExportFeature::execute()
{
- AttributeStringPtr aFilePathAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
- data()->attribute(ExchangePlugin_ExportFeature::FILE_PATH_ID()));
+ AttributeStringPtr aFormatAttr =
+ this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
+ std::string aFormat = aFormatAttr->value();
+ if (aFormat.empty())
+ return;
+
+ AttributeStringPtr aFilePathAttr =
+ this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
std::string aFilePath = aFilePathAttr->value();
if (aFilePath.empty())
return;
AttributeSelectionListPtr aSelectionListAttr =
- std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(
- data()->attribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID()));
-
+ this->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
for ( int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i ) {
- std::shared_ptr<ModelAPI_AttributeSelection> anSelectionAttr = aSelectionListAttr->value(i);
- aShapes.push_back(anSelectionAttr->value());
+ aShapes.push_back(aSelectionListAttr->value(i)->value());
}
std::shared_ptr<GeomAPI_Shape> aShape =
GeomAlgoAPI_CompoundBuilder::compound(aShapes);
- exportFile(aFilePath, aShape);
+ exportFile(aFilePath, aFormat, aShape);
+}
+
+std::list<std::string> ExchangePlugin_ExportFeature::getFormats() const
+{
+ // This value is a copy of string_list attribute of format_choice in plugin-Exchange.xml
+ // XPath:plugin-Exchange.xml:string(//*[@id="format_choice"]/@string_list)
+ std::string aFormats = "BREP STEP IGES-5.1 IGES-5.3";
+ return ExchangePlugin_Tools::split(aFormats, ' ');
}
bool ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
+ const std::string& theFormat,
std::shared_ptr<GeomAPI_Shape> theShape)
{
// retrieve the file and plugin library names
TCollection_AsciiString aFileName(theFileName.c_str());
OSD_Path aPath(aFileName);
- TCollection_AsciiString aFormatName = aPath.Extension();
- // ".brep" -> "BREP", TCollection_AsciiString are numbered from 1
- aFormatName = aFormatName.SubString(2, aFormatName.Length());
- aFormatName.UpperCase();
+ TCollection_AsciiString aFormatName(theFormat.c_str());
// Perform the export
TCollection_AsciiString anError;
TDF_Label anUnknownLabel = TDF_Label();
TopoDS_Shape aShape(theShape->impl<TopoDS_Shape>());
- bool aResult = true;
+ bool aResult = false;
if (aFormatName == "BREP") {
aResult = BREPExport::Export(aFileName, aFormatName, aShape, anError, anUnknownLabel);
- } else if (aFormatName == "STEP" || aFormatName == "STP") {
+ } else if (aFormatName == "STEP") {
aResult = STEPExport::Export(aFileName, aFormatName, aShape, anError, anUnknownLabel);
- } else if (aFormatName == "IGES") {
+ } else if (aFormatName.SubString(1, 4) == "IGES") {
aResult = IGESExport::Export(aFileName, aFormatName, aShape, anError, anUnknownLabel);
+ } else {
+ anError = TCollection_AsciiString("Unsupported format ") + aFormatName;
}
- if ( !aResult ) {
- const static std::string aShapeError =
+ if (!aResult) {
+ std::string aShapeError =
"An error occurred while exporting " + theFileName + ": " + anError.ToCString();
setError(aShapeError);
return false;
static const std::string MY_EXPORT_ID("Export");
return MY_EXPORT_ID;
}
+ /// attribute name of file format
+ inline static const std::string& FILE_FORMAT_ID()
+ {
+ static const std::string MY_FILE_FORMAT_ID("export_file_format");
+ return MY_FILE_FORMAT_ID;
+ }
/// attribute name of file path
inline static const std::string& FILE_PATH_ID()
{
/// Reimplemented from ModelAPI_Feature::isPreviewNeeded(). Returns false.
MODELAPI_EXPORT virtual bool isPreviewNeeded() const { return false; }
- protected:
+protected:
/// Performs the export of the file
EXCHANGEPLUGIN_EXPORT bool exportFile(const std::string& theFileName,
+ const std::string& theFormat,
std::shared_ptr<GeomAPI_Shape> theShape);
};
--- /dev/null
+/*
+ * ExchangePlugin_Tools.cpp
+ *
+ * Created on: May 15, 2015
+ * Author: spo
+ */
+
+#include <ExchangePlugin_Tools.h>
+
+#include <sstream>
+
+std::list<std::string> ExchangePlugin_Tools::split(const std::string& theString, char theDelimiter)
+{
+ std::list<std::string> theResult;
+ std::istringstream aStream(theString);
+ std::string aSection;
+ while (std::getline(aStream, aSection, theDelimiter))
+ theResult.push_back(aSection);
+ return theResult;
+}
--- /dev/null
+/*
+ * ExchangePlugin_Tools.h
+ *
+ * Created on: May 15, 2015
+ * Author: spo
+ */
+
+#ifndef EXCHANGEPLUGIN_TOOLS_H_
+#define EXCHANGEPLUGIN_TOOLS_H_
+
+#include <ExchangePlugin.h>
+
+#include <list>
+#include <string>
+
+/**\class ExchangePlugin_Tools
+ * \ingroup Plugins
+ * \brief Internal tools for the plugin.
+ */
+class EXCHANGEPLUGIN_EXPORT ExchangePlugin_Tools {
+public:
+ /// Splits theString using theDelimiter.
+ static std::list<std::string> split(const std::string& theString,
+ char theDelimiter);
+
+};
+
+#endif /* EXCHANGEPLUGIN_TOOLS_H_ */
// Author: Vitaly SMETANNIKOV
#include <ExchangePlugin_Validators.h>
+
+#include <ExchangePlugin_Tools.h>
+
#include <ModelAPI_Feature.h>
#include <ModelAPI_Object.h>
#include <ModelAPI_Session.h>
#include <list>
#include <string>
-#include <sstream>
#include <algorithm>
bool ExchangePlugin_FormatValidator::parseFormats(const std::list<std::string>& theArguments,
result = false;
continue;
}
- std::string aFormatList = anArg.substr(0, aSepPos);
- std::transform(aFormatList.begin(), aFormatList.end(), aFormatList.begin(), toupper);
- std::istringstream aStream(aFormatList);
- std::string aFormat;
- while (std::getline(aStream, aFormat, '|'))
- outFormats.push_back(aFormat);
+ std::string aFormats = anArg.substr(0, aSepPos);
+ std::transform(aFormats.begin(), aFormats.end(), aFormats.begin(), toupper);
+ std::list<std::string> aFormatList = ExchangePlugin_Tools::split(aFormats, '|');
+ outFormats.insert(outFormats.end(), aFormatList.begin(), aFormatList.end());
}
return result;
}
<group id="Exchange">
<feature id="Import" title="Import" tooltip="Import a file" icon=":icons/import.png">
<file_selector id="import_file_selector" title="Import file" path="">
- <validator id="ExchangePlugin_ImportFormat" parameters="BREP:BREPImport,STEP|STP:STEPImport,IGES:IGESImport" />
+ <validator id="ExchangePlugin_ImportFormat" parameters="BREP:BREP,STEP|STP:STEP,IGES:IGES" />
</file_selector>
</feature>
<feature id="Export" title="Export" tooltip="Export to file" icon=":icons/export.png">
- <file_selector id="export_file_selector" type="save" title="Export file" path="">
- <validator id="ExchangePlugin_ExportFormat" parameters="BREP:BREPExport,STEP|STP:STEPExport,IGES:IGESExport" />
- </file_selector>
+ <export_file_selector id="export_file_selector" type="save" title="Export file" path="">
+ <validator id="ExchangePlugin_ExportFormat" parameters="BREP:BREP,STEP|STP:STEP,IGES:IGES-5.1,IGES:IGES-5.3" />
+ </export_file_selector>
<multi_selector id="selection_list" tooltip="Select a set of objects" type_choice="Vertices Edges Faces Solids" />
</feature>
</group>
//=============================================================================
//extern "C" {
+
namespace IGESExport {
bool Export(const TCollection_AsciiString& theFileName,
- const TCollection_AsciiString&,
+ const TCollection_AsciiString& theFormatName,
const TopoDS_Shape& theShape,
TCollection_AsciiString& theError, const TDF_Label&)
{
- //TODO(spo): pass version as argument of the function
- TCollection_AsciiString aVersion = "5.3";
+ // theFormatName expected "IGES-5.1", "IGES-5.3"...
+ TCollection_AsciiString aVersion = theFormatName.Token("-", 2);
+ #ifdef _DEBUG
+ if (!aVersion.IsEqual("5.1") || !aVersion.IsEqual("5.3"))
+ std::cout << "Warning: unrecognized version " << aVersion.ToCString()
+ << ". Default version: 5.1." << std::endl;
+ #endif
// define, whether to write only faces (5.1 IGES format)
// or shells and solids also (5.3 IGES format)
int aBrepMode = 0;
// perform shape writing
if( ICW.AddShape( theShape ) ) {
ICW.ComputeModel();
- return (bool)ICW.Write( theFileName.ToCString() );
+ return ICW.Write( theFileName.ToCString() );
}
return false;
}
namespace STEPExport {
-bool Export (const TCollection_AsciiString& theFileName,
- const TCollection_AsciiString& theFormatName,
- const TopoDS_Shape& theShape,
- TCollection_AsciiString& theError,
- const TDF_Label& theShapeLabel)
+bool Export(const TCollection_AsciiString& theFileName,
+ const TCollection_AsciiString& theFormatName,
+ const TopoDS_Shape& theShape,
+ TCollection_AsciiString& theError,
+ const TDF_Label& theShapeLabel)
{
#ifdef _DEBUG
std::cout << "Export STEP into file " << theFileName.ToCString() << std::endl;
void ModuleBase_WidgetFileSelector::onPathSelectionBtn()
{
- QString aDefaultPath = myPathField->text().isEmpty() ? myDefaultPath : myPathField->text();
+ QString aDefaultPath = myPathField->text().isEmpty()
+ ? myDefaultPath
+ : QFileInfo(myPathField->text()).absolutePath();
QString aFilter = filterString();
QString aFileName = (myType == WFS_SAVE)
- ? QFileDialog::getSaveFileName(this, myTitle, aDefaultPath, aFilter)
- : QFileDialog::getOpenFileName(this, myTitle, aDefaultPath, aFilter);
+ ? QFileDialog::getSaveFileName(this, myTitle, aDefaultPath, aFilter, &mySelectedFilter)
+ : QFileDialog::getOpenFileName(this, myTitle, aDefaultPath, aFilter, &mySelectedFilter);
if (!aFileName.isEmpty()) {
myPathField->setText(aFileName);
}
emit valuesChanged();
}
-QString ModuleBase_WidgetFileSelector::filterString() const
+QString ModuleBase_WidgetFileSelector::formatToFilter( const QString & theFormat )
{
- QStringList aResult;
- QStringList aValidatorFormats = getValidatorFormats();
+ if (theFormat.isEmpty() && !theFormat.contains(":"))
+ return QString();
- foreach(QString eachFormat, aValidatorFormats) {
- QStringList aFormatList = eachFormat.split("|");
- aResult << QString("%1 files (%2)").arg(aFormatList.value(0))
- .arg(QStringList(aFormatList).replaceInStrings(QRegExp("^(.*)$"), "*.\\1").join(" "));
- }
- aResult << QString("All files (*.*)");
- return aResult.join(";;");
+ QStringList aExtesionList = theFormat.section(':', 0, 0).split("|");
+ QString aFormat = theFormat.section(':', 1, 1);
+ return QString("%1 files (%2)").arg(aFormat)
+ .arg(QStringList(aExtesionList).replaceInStrings(QRegExp("^(.*)$"), "*.\\1").join(" "));
}
QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const
std::list<std::string> anArgumentList = allArguments.front();
std::list<std::string>::const_iterator it = anArgumentList.begin();
for (; it != anArgumentList.end(); ++it) {
- QString aFormat = getFormat(*it);
- if (!aFormat.isNull())
+ QString aFormat = QString::fromStdString(*it);
+ if (!aFormat.isEmpty())
aResult << aFormat;
}
return aResult;
}
-QString ModuleBase_WidgetFileSelector::getFormat( const std::string& theArgument ) const
+QString ModuleBase_WidgetFileSelector::filterString() const
{
- QString anArgument = QString::fromStdString(theArgument);
- if (!anArgument.contains(":"))
- return QString();
- return anArgument.section(":", 0, 0).toUpper();
-}
+ QStringList aResult;
+ QStringList aValidatorFormats = getValidatorFormats();
+ foreach(const QString & eachFormat, aValidatorFormats) {
+ aResult << formatToFilter(eachFormat);
+ }
+ if (myType == WFS_OPEN)
+ aResult << QString("All files (*.*)");
+ return aResult.join(";;");
+}
/// \return True in success
virtual bool storeValueCustom() const;
- protected:
- /// Returns string containing formats
- QString filterString() const;
+protected:
+ /// Converts format to filter string
+ static QString formatToFilter( const QString & theFormat );
/// Returns list of validator formats
QStringList getValidatorFormats() const;
- /// Returns a format received from theArgument
- QString getFormat( const std::string& theArgument ) const;
+ /// Returns string containing formats
+ QString filterString() const;
- private:
+protected:
/// A control for path input
QLineEdit* myPathField;
/// A title of open file dialog box
QString myTitle;
+ /// A current format
+ QString mySelectedFilter;
+
/// A title of open file dialog box
enum { WFS_OPEN, WFS_SAVE } myType;
PartSet_WidgetMultiSelector.h
PartSet_WidgetPoint2dDistance.h
PartSet_WidgetShapeSelector.h
+ PartSet_WidgetFileSelector.h
PartSet_Filters.h
PartSet_SketcherMgr.h
PartSet_MenuMgr.h
PartSet_WidgetPoint2dAngle.cpp
PartSet_WidgetPoint2dDistance.cpp
PartSet_WidgetShapeSelector.cpp
+ PartSet_WidgetFileSelector.cpp
PartSet_Filters.cpp
PartSet_SketcherMgr.cpp
PartSet_MenuMgr.cpp
#include "PartSet_WidgetPoint2dAngle.h"
#include "PartSet_WidgetMultiSelector.h"
#include "PartSet_WidgetEditor.h"
+#include "PartSet_WidgetFileSelector.h"
#include "PartSet_SketcherMgr.h"
#include "PartSet_MenuMgr.h"
}
if (theType == WDG_DOUBLEVALUE_EDITOR) {
aWgt = new PartSet_WidgetEditor(theParent, workshop(), theWidgetApi, theParentId);
+ } else if (theType == "export_file_selector") {
+ aWgt = new PartSet_WidgetFileSelector(theParent, workshop(), theWidgetApi, theParentId);
}
return aWgt;
}
}
aLabel->setPalette(aPalet);
}
-}
\ No newline at end of file
+}
--- /dev/null
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
+/*
+ * PartSet_WidgetFileSelector.cpp
+ *
+ * Created on: May 18, 2015
+ * Author: spo
+ */
+
+#include "PartSet_WidgetFileSelector.h"
+
+#include <ModelAPI_AttributeString.h>
+
+PartSet_WidgetFileSelector::PartSet_WidgetFileSelector(QWidget* theParent,
+ ModuleBase_IWorkshop* theWorkshop,
+ const Config_WidgetAPI* theData,
+ const std::string& theParentId)
+: ModuleBase_WidgetFileSelector(theParent, theData, theParentId)
+, myWorkshop(theWorkshop)
+{
+}
+
+bool PartSet_WidgetFileSelector::restoreValue()
+{
+ // A rare case when plugin was not loaded.
+ if (!myFeature)
+ return false;
+
+ DataPtr aData = myFeature->data();
+ AttributeStringPtr aStringAttr = aData->string("export_file_format");
+ mySelectedFilter = formatToFilter(shortFormatToFullFormat(QString::fromStdString(aStringAttr->value())));
+
+ return ModuleBase_WidgetFileSelector::restoreValue();
+}
+
+bool PartSet_WidgetFileSelector::storeValueCustom() const
+{
+ // A rare case when plugin was not loaded.
+ if (!myFeature)
+ return false;
+
+ DataPtr aData = myFeature->data();
+ AttributeStringPtr aStringAttr = aData->string("export_file_format");
+ aStringAttr->setValue(filterToShortFormat(mySelectedFilter).toStdString());
+
+ return ModuleBase_WidgetFileSelector::storeValueCustom();
+}
+
+QString PartSet_WidgetFileSelector::filterToShortFormat( const QString & theFilter )
+{
+ return theFilter.section(' ', 0, 0);
+}
+
+QString PartSet_WidgetFileSelector::shortFormatToFullFormat( const QString & theFormat ) const
+{
+ foreach(const QString & eachFormat, getValidatorFormats())
+ if (filterToShortFormat(formatToFilter(eachFormat)) == theFormat)
+ return eachFormat;
+ return QString();
+}
--- /dev/null
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
+/*
+ * PartSet_WidgetFileSelector.h
+ *
+ * Created on: May 18, 2015
+ * Author: spo
+ */
+
+#ifndef PARTSET_WIDGETFILESELECTOR_H_
+#define PARTSET_WIDGETFILESELECTOR_H_
+
+#include "PartSet.h"
+
+#include <ModuleBase_WidgetFileSelector.h>
+
+class ModuleBase_IWorkshop;
+
+/**
+* \ingroup Modules
+* Customization of ModuleBase_WidgetFileSelector in order to write
+* format of exported file.
+*/
+class PARTSET_EXPORT PartSet_WidgetFileSelector : public ModuleBase_WidgetFileSelector
+{
+ Q_OBJECT
+public:
+ /// Constructor
+ /// \param theParent the parent object
+ /// \param theWorkshop instance of workshop interface
+ /// \param theData the widget configuration. The attribute of the model widget is obtained from
+ /// \param theParentId is Id of a parent of the current attribute
+ PartSet_WidgetFileSelector(QWidget* theParent,
+ ModuleBase_IWorkshop* theWorkshop,
+ const Config_WidgetAPI* theData,
+ const std::string& theParentId);
+
+ virtual ~PartSet_WidgetFileSelector() {}
+
+ /// Reimplemented from ModuleBase_WidgetFileSelector::restoreValue()
+ virtual bool restoreValue();
+
+protected:
+ /// Reimplemented from ModuleBase_WidgetFileSelector::storeValueCustom()
+ virtual bool storeValueCustom() const;
+
+ /// Returns a short format string of theFilter string
+ static QString filterToShortFormat( const QString & theFilter );
+
+ /// Returns a full format string for the short format
+ QString shortFormatToFullFormat( const QString & theShortFormat ) const;
+
+private:
+ ModuleBase_IWorkshop* myWorkshop; // the current workshop
+};
+
+#endif /* PARTSET_WIDGETFILESELECTOR_H_ */