From 5398864f180f5d1a55dc5745772d974ad9d4d3c9 Mon Sep 17 00:00:00 2001 From: spo Date: Fri, 27 Nov 2015 16:31:17 +0300 Subject: [PATCH] Make XAO export --- src/ExchangePlugin/CMakeLists.txt | 1 + .../ExchangePlugin_ExportFeature.cpp | 83 ++++++++++++------- .../ExchangePlugin_ExportFeature.h | 29 +++++-- .../ExchangePlugin_ImportFeature.cpp | 14 ++-- src/ExchangePlugin/export_widget.xml | 33 ++++++++ src/ExchangePlugin/plugin-Exchange.xml | 22 +++-- src/GeomAlgoAPI/CMakeLists.txt | 2 + src/GeomAlgoAPI/GeomAlgoAPI_XAOExport.cpp | 46 ++++++++++ src/GeomAlgoAPI/GeomAlgoAPI_XAOExport.h | 27 ++++++ src/GeomAlgoAPI/GeomAlgoAPI_XAOImport.cpp | 9 +- 10 files changed, 212 insertions(+), 54 deletions(-) create mode 100644 src/ExchangePlugin/export_widget.xml create mode 100644 src/GeomAlgoAPI/GeomAlgoAPI_XAOExport.cpp create mode 100644 src/GeomAlgoAPI/GeomAlgoAPI_XAOExport.h diff --git a/src/ExchangePlugin/CMakeLists.txt b/src/ExchangePlugin/CMakeLists.txt index 1196750e7..64f4d863d 100644 --- a/src/ExchangePlugin/CMakeLists.txt +++ b/src/ExchangePlugin/CMakeLists.txt @@ -30,6 +30,7 @@ SET(PROJECT_SOURCES ) SET(XML_RESOURCES + export_widget.xml plugin-Exchange.xml ) diff --git a/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp b/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp index a399b646e..1084d213f 100644 --- a/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp +++ b/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp @@ -6,16 +6,23 @@ #include -#include +#include +#include +#include +#ifdef _DEBUG +#include +#include +#endif + +#include +#include #include #include #include #include #include - -#include -#include +#include #include @@ -25,14 +32,12 @@ #include #include #include +#include +#include -#include -#include -#include -#ifdef _DEBUG -#include -#include -#endif +#include + +#include ExchangePlugin_ExportFeature::ExchangePlugin_ExportFeature() { @@ -43,22 +48,18 @@ ExchangePlugin_ExportFeature::~ExchangePlugin_ExportFeature() // TODO Auto-generated destructor stub } -/* - * Returns the unique kind of a feature - */ -const std::string& ExchangePlugin_ExportFeature::getKind() -{ - return ExchangePlugin_ExportFeature::ID(); -} - /* * Request for initialization of data model of the feature: adding all attributes */ void ExchangePlugin_ExportFeature::initAttributes() { + data()->addAttribute(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID(), ModelAPI_AttributeString::typeId()); data()->addAttribute(ExchangePlugin_ExportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId()); data()->addAttribute(ExchangePlugin_ExportFeature::FILE_FORMAT_ID(), ModelAPI_AttributeString::typeId()); data()->addAttribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()); + data()->addAttribute(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID(), ModelAPI_AttributeString::typeId()); + + ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ExchangePlugin_ExportFeature::XAO_AUTHOR_ID()); } /* @@ -90,20 +91,20 @@ void ExchangePlugin_ExportFeature::execute() if (aCurShape.get() != NULL) aShapes.push_back(aCurShape); } - std::shared_ptr aShape = - GeomAlgoAPI_CompoundBuilder::compound(aShapes); + std::shared_ptr aShape = aShapes.size() == 1 + ? *aShapes.begin() + : GeomAlgoAPI_CompoundBuilder::compound(aShapes); exportFile(aFilePath, aFormat, aShape); } -bool ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName, +void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName, const std::string& theFormat, std::shared_ptr theShape) { - // retrieve the file and plugin library names std::string aFormatName = theFormat; - if (theFormat.empty()) { // look at extension + if (aFormatName.empty()) { // get default format for the extension // ".brep" -> "BREP" std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName); if (anExtension == "BREP" || anExtension == "BRP") { @@ -112,11 +113,18 @@ bool ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName, aFormatName = "STEP"; } else if (anExtension == "IGES" || anExtension == "IGS") { aFormatName = "IGES-5.1"; + } else if (anExtension == "XAO") { + aFormatName = "XAO"; } else { aFormatName = anExtension; } } + if (aFormatName == "XAO") { + exportXAO(theFileName, theShape); + return; + } + // Perform the export std::string anError; bool aResult = false; @@ -127,15 +135,28 @@ bool ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName, } else if (aFormatName.substr(0, 4) == "IGES") { aResult = IGESExport(theFileName, aFormatName, theShape, anError); } else { - anError = "Unsupported format " + aFormatName; + anError = "Unsupported format: " + aFormatName; } - if (!aResult) { - std::string aShapeError = - "An error occurred while exporting " + theFileName + ": " + anError; - setError(aShapeError); - return false; + if (!anError.empty()) { + setError("An error occurred while exporting " + theFileName + ": " + anError); + return; } +} + +void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName, + std::shared_ptr theShape) +{ + std::string anAuthor = string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value(); + + XAO::Xao aXao(anAuthor, "1.0"); - return true; + std::string anError; + XAOExport(theFileName, theShape, &aXao, anError); + + if (!anError.empty()) { + setError("An error occurred while exporting " + theFileName + ": " + anError); + return; + } } + diff --git a/src/ExchangePlugin/ExchangePlugin_ExportFeature.h b/src/ExchangePlugin/ExchangePlugin_ExportFeature.h index 87727711e..9a71b0561 100644 --- a/src/ExchangePlugin/ExchangePlugin_ExportFeature.h +++ b/src/ExchangePlugin/ExchangePlugin_ExportFeature.h @@ -29,6 +29,12 @@ public: static const std::string MY_EXPORT_ID("Export"); return MY_EXPORT_ID; } + /// attribute name for file format + inline static const std::string& EXPORT_TYPE_ID() + { + static const std::string MY_EXPORT_TYPE_ID("ExportType"); + return MY_EXPORT_TYPE_ID; + } /// attribute name of file path inline static const std::string& FILE_PATH_ID() { @@ -47,17 +53,26 @@ public: static const std::string MY_SELECTION_LIST_ID("selection_list"); return MY_SELECTION_LIST_ID; } + /// attribute name of author for XAO format + inline static const std::string& XAO_AUTHOR_ID() + { + static const std::string MY_XAO_AUTHOR_ID("xao_author"); + return MY_XAO_AUTHOR_ID; + } /// Default constructor EXCHANGEPLUGIN_EXPORT ExchangePlugin_ExportFeature(); /// Default destructor EXCHANGEPLUGIN_EXPORT virtual ~ExchangePlugin_ExportFeature(); + /// Returns the unique kind of a feature + EXCHANGEPLUGIN_EXPORT virtual const std::string& getKind() + { + return ExchangePlugin_ExportFeature::ID(); + } + /// Request for initialization of data model of the feature: adding all attributes EXCHANGEPLUGIN_EXPORT virtual void initAttributes(); - /// Returns the unique kind of a feature - EXCHANGEPLUGIN_EXPORT virtual const std::string& getKind(); - /// Computes or recomputes the results EXCHANGEPLUGIN_EXPORT virtual void execute(); @@ -68,10 +83,14 @@ public: EXCHANGEPLUGIN_EXPORT virtual bool isPreviewNeeded() const { return false; } protected: - /// Performs the export of the file - EXCHANGEPLUGIN_EXPORT bool exportFile(const std::string& theFileName, + /// Performs export of the file + EXCHANGEPLUGIN_EXPORT void exportFile(const std::string& theFileName, const std::string& theFormat, std::shared_ptr theShape); + + /// Performs export to XAO file + EXCHANGEPLUGIN_EXPORT void exportXAO(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 143bad9ac..8e50157e6 100644 --- a/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp +++ b/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp @@ -19,8 +19,8 @@ #include #include #include -#include #include +#include #include @@ -104,7 +104,7 @@ void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName) } else if (anExtension == "IGES" || anExtension == "IGS") { aGeomShape = IGESImport(theFileName, anExtension, anError); } else { - anError = "Cann't read files with extension: " + anExtension; + anError = "Unsupported format: " + anExtension; } // Check if shape is valid @@ -133,10 +133,14 @@ void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName) } XAO::Geometry* aXaoGeometry = aXao.getGeometry(); - data()->setName(aXaoGeometry->getName()); - std::shared_ptr aResultBody = createResultBody(aGeomShape); - aResultBody->data()->setName(aXaoGeometry->getName()); + // use the geometry name or the file name for the feature + std::string aBodyName = aXaoGeometry->getName().empty() + ? GeomAlgoAPI_Tools::File_Tools::name(theFileName) + : aXaoGeometry->getName(); + data()->setName(aBodyName); + + ResultBodyPtr aResultBody = createResultBody(aGeomShape); setResult(aResultBody); // Process groups diff --git a/src/ExchangePlugin/export_widget.xml b/src/ExchangePlugin/export_widget.xml new file mode 100644 index 000000000..1444d4643 --- /dev/null +++ b/src/ExchangePlugin/export_widget.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/src/ExchangePlugin/plugin-Exchange.xml b/src/ExchangePlugin/plugin-Exchange.xml index baae74347..d1552b67d 100644 --- a/src/ExchangePlugin/plugin-Exchange.xml +++ b/src/ExchangePlugin/plugin-Exchange.xml @@ -3,19 +3,23 @@ - + - - - - - - - + + - \ No newline at end of file + diff --git a/src/GeomAlgoAPI/CMakeLists.txt b/src/GeomAlgoAPI/CMakeLists.txt index 55496657e..125d5b43e 100644 --- a/src/GeomAlgoAPI/CMakeLists.txt +++ b/src/GeomAlgoAPI/CMakeLists.txt @@ -34,6 +34,7 @@ SET(PROJECT_HEADERS GeomAlgoAPI_ShapeTools.h GeomAlgoAPI_Partition.h GeomAlgoAPI_PaveFiller.h + GeomAlgoAPI_XAOExport.h GeomAlgoAPI_XAOImport.h ) @@ -65,6 +66,7 @@ SET(PROJECT_SOURCES GeomAlgoAPI_ShapeTools.cpp GeomAlgoAPI_Partition.cpp GeomAlgoAPI_PaveFiller.cpp + GeomAlgoAPI_XAOExport.cpp GeomAlgoAPI_XAOImport.cpp ) diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_XAOExport.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_XAOExport.cpp new file mode 100644 index 000000000..6bd812792 --- /dev/null +++ b/src/GeomAlgoAPI/GeomAlgoAPI_XAOExport.cpp @@ -0,0 +1,46 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + +// File: GEOMALGOAPI_XAOExport.cpp +// Created: Nov 27, 2015 +// Author: Sergey POKHODENKO + +#include + +#include "GeomAlgoAPI_Tools.h" + +#include + +#include +#include + +//============================================================================= +/*! + * + */ +//============================================================================= +bool XAOExport(const std::string& theFileName, + const std::shared_ptr& theShape, + XAO::Xao* theXao, + std::string& theError) +{ + #ifdef _DEBUG + std::cout << "Export XAO into file " << theFileName << std::endl; + #endif + + if (theFileName.empty() || !theShape.get() || !theXao) { + theError = "An invalid argument."; + return false; + } + + TopoDS_Shape aShape = theShape->impl(); + try { + XAO::BrepGeometry* aGeometry = new XAO::BrepGeometry; + theXao->setGeometry(aGeometry); + aGeometry->setTopoDS_Shape(aShape); + XAO::XaoExporter::saveToFile(theXao, theFileName); + } catch (XAO::XAO_Exception& e) { + theError = e.what(); + return false; + } + return true; +} diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_XAOExport.h b/src/GeomAlgoAPI/GeomAlgoAPI_XAOExport.h new file mode 100644 index 000000000..5f535cfec --- /dev/null +++ b/src/GeomAlgoAPI/GeomAlgoAPI_XAOExport.h @@ -0,0 +1,27 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + +// File: GEOMALGOAPI_XAOExport.h +// Created: Nov 27, 2015 +// Author: Sergey POKHODENKO + +#ifndef GEOMALGOAPI_XAOEXPORT_H_ +#define GEOMALGOAPI_XAOEXPORT_H_ + +#include + +#include + +#include + +namespace XAO { +class Xao; +} // namespace XAO + +/// Implementation of the export XAO files algorithms +GEOMALGOAPI_EXPORT +bool XAOExport(const std::string& theFileName, + const std::shared_ptr& theShape, + XAO::Xao* theXao, + std::string& theError); + +#endif /* GEOMALGOAPI_XAOEXPORT_H_ */ diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_XAOImport.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_XAOImport.cpp index b0309c273..7647f7122 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_XAOImport.cpp +++ b/src/GeomAlgoAPI/GeomAlgoAPI_XAOImport.cpp @@ -6,8 +6,6 @@ #include -#include - #include #include @@ -22,11 +20,14 @@ std::shared_ptr XAOImport(const std::string& theFileName, std::string& theError, XAO::Xao* theXao) { - assert(theXao); - #ifdef _DEBUG std::cout << "Import XAO from file " << theFileName << std::endl; #endif + if (theFileName.empty() || !theXao) { + theError = "An invalid argument."; + return std::shared_ptr(); + } + TopoDS_Shape aShape; try { if (XAO::XaoExporter::readFromFile(theFileName, theXao)) { -- 2.39.2