From a947922b16f7dd9ca2b3ef138ad2af9a09c8bb48 Mon Sep 17 00:00:00 2001 From: spo Date: Mon, 30 Nov 2015 16:10:03 +0300 Subject: [PATCH] Make export XAO with groups --- .../ExchangePlugin_ExportFeature.cpp | 128 +++++++++++++----- .../ExchangePlugin_ExportFeature.h | 6 +- .../ExchangePlugin_ImportFeature.cpp | 17 ++- src/ExchangePlugin/ExchangePlugin_Tools.cpp | 29 ++++ src/ExchangePlugin/ExchangePlugin_Tools.h | 6 + src/ExchangePlugin/export_widget.xml | 10 +- src/GeomAlgoAPI/GeomAlgoAPI_XAOExport.cpp | 29 +++- src/GeomAlgoAPI/GeomAlgoAPI_XAOExport.h | 7 +- 8 files changed, 184 insertions(+), 48 deletions(-) diff --git a/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp b/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp index 1084d213f..e96779bd7 100644 --- a/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp +++ b/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp @@ -32,9 +32,11 @@ #include #include #include +#include #include #include +#include #include #include @@ -59,6 +61,7 @@ void ExchangePlugin_ExportFeature::initAttributes() 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::SELECTION_LIST_ID()); ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ExchangePlugin_ExportFeature::XAO_AUTHOR_ID()); } @@ -70,9 +73,6 @@ void ExchangePlugin_ExportFeature::execute() AttributeStringPtr aFormatAttr = this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID()); std::string aFormat = aFormatAttr->value(); - // Format may be empty. In this case look at extension. -// if (aFormat.empty()) -// return; AttributeStringPtr aFilePathAttr = this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID()); @@ -80,27 +80,11 @@ void ExchangePlugin_ExportFeature::execute() if (aFilePath.empty()) return; - AttributeSelectionListPtr aSelectionListAttr = - this->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID()); - std::list > aShapes; - for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i) { - AttributeSelectionPtr anAttrSelection = aSelectionListAttr->value(i); - std::shared_ptr aCurShape = anAttrSelection->value(); - if (aCurShape.get() == NULL) - aCurShape = anAttrSelection->context()->shape(); - if (aCurShape.get() != NULL) - aShapes.push_back(aCurShape); - } - std::shared_ptr aShape = aShapes.size() == 1 - ? *aShapes.begin() - : GeomAlgoAPI_CompoundBuilder::compound(aShapes); - - exportFile(aFilePath, aFormat, aShape); + exportFile(aFilePath, aFormat); } void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName, - const std::string& theFormat, - std::shared_ptr theShape) + const std::string& theFormat) { std::string aFormatName = theFormat; @@ -121,19 +105,35 @@ void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName, } if (aFormatName == "XAO") { - exportXAO(theFileName, theShape); + exportXAO(theFileName); return; } + // make shape for export from selected shapes + AttributeSelectionListPtr aSelectionListAttr = + this->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID()); + std::list aShapes; + for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i) { + AttributeSelectionPtr anAttrSelection = aSelectionListAttr->value(i); + std::shared_ptr aCurShape = anAttrSelection->value(); + if (aCurShape.get() == NULL) + aCurShape = anAttrSelection->context()->shape(); + if (aCurShape.get() != NULL) + aShapes.push_back(aCurShape); + } + GeomShapePtr aShape = aShapes.size() == 1 + ? *aShapes.begin() + : GeomAlgoAPI_CompoundBuilder::compound(aShapes); + // Perform the export std::string anError; bool aResult = false; if (aFormatName == "BREP") { - aResult = BREPExport(theFileName, aFormatName, theShape, anError); + aResult = BREPExport(theFileName, aFormatName, aShape, anError); } else if (aFormatName == "STEP") { - aResult = STEPExport(theFileName, aFormatName, theShape, anError); + aResult = STEPExport(theFileName, aFormatName, aShape, anError); } else if (aFormatName.substr(0, 4) == "IGES") { - aResult = IGESExport(theFileName, aFormatName, theShape, anError); + aResult = IGESExport(theFileName, aFormatName, aShape, anError); } else { anError = "Unsupported format: " + aFormatName; } @@ -144,19 +144,85 @@ void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName, } } -void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName, - std::shared_ptr theShape) +void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName) { + try { + + std::string anError; + XAO::Xao aXao; + + // author + std::string anAuthor = string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value(); + aXao.setAuthor(anAuthor); + + // make shape for export from all results + std::list aShapes; + int aBodyCount = document()->size(ModelAPI_ResultBody::group()); + for (int aBodyIndex = 0; aBodyIndex < aBodyCount; ++aBodyIndex) { + ResultBodyPtr aResultBody = + std::dynamic_pointer_cast( + document()->object(ModelAPI_ResultBody::group(), aBodyIndex)); + if (!aResultBody.get()) + continue; + aShapes.push_back(aResultBody->shape()); + } + GeomShapePtr aShape = (aShapes.size() == 1) + ? *aShapes.begin() + : GeomAlgoAPI_CompoundBuilder::compound(aShapes); - XAO::Xao aXao(anAuthor, "1.0"); + SetShapeToXAO(aShape, &aXao, anError); - std::string anError; - XAOExport(theFileName, theShape, &aXao, anError); + if (!anError.empty()) { + setError("An error occurred while exporting " + theFileName + ": " + anError); + return; + } + + // groups + + int aGroupCount = document()->size(ModelAPI_ResultGroup::group()); + for (int aGroupIndex = 0; aGroupIndex < aGroupCount; ++aGroupIndex) { + ResultGroupPtr aResultGroup = + std::dynamic_pointer_cast( + document()->object(ModelAPI_ResultGroup::group(), aGroupIndex)); + + FeaturePtr aGroupFeature = document()->feature(aResultGroup); + + AttributeSelectionListPtr aSelectionList = + aGroupFeature->selectionList("group_list"); + + // conversion of dimension + std::string aSelectionType = aSelectionList->selectionType(); + std::string aDimensionString = ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType); + XAO::Dimension aGroupDimension = XAO::XaoUtils::stringToDimension(aDimensionString); + + XAO::Group* aXaoGroup = aXao.addGroup(aGroupDimension, + aResultGroup->data()->name()); + + for (int aSelectionIndex = 0; aSelectionIndex < aSelectionList->size(); ++aSelectionIndex) { + AttributeSelectionPtr aSelection = aSelectionList->value(aSelectionIndex); + + // complex conversion of reference id to element index + int aReferenceID = aSelection->Id(); + std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID); + int anElementID = aXao.getGeometry()->getElementIndexByReference(aGroupDimension, aReferenceString); + + aXaoGroup->add(anElementID); + } + } + + // exporting + + XAOExport(theFileName, &aXao, anError); if (!anError.empty()) { setError("An error occurred while exporting " + theFileName + ": " + anError); return; } -} + } catch (XAO::XAO_Exception& e) { + std::string anError = e.what(); + setError("An error occurred while importing " + theFileName + ": " + anError); + return; + } +} diff --git a/src/ExchangePlugin/ExchangePlugin_ExportFeature.h b/src/ExchangePlugin/ExchangePlugin_ExportFeature.h index 9a71b0561..8b57ed612 100644 --- a/src/ExchangePlugin/ExchangePlugin_ExportFeature.h +++ b/src/ExchangePlugin/ExchangePlugin_ExportFeature.h @@ -85,12 +85,10 @@ public: protected: /// Performs export of the file EXCHANGEPLUGIN_EXPORT void exportFile(const std::string& theFileName, - const std::string& theFormat, - std::shared_ptr theShape); + const std::string& theFormat); /// Performs export to XAO file - EXCHANGEPLUGIN_EXPORT void exportXAO(const std::string& theFileName, - std::shared_ptr theShape); + EXCHANGEPLUGIN_EXPORT void exportXAO(const std::string& theFileName); }; #endif /* EXPORT_EXPORTFEATURE_H_ */ diff --git a/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp b/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp index 8e50157e6..cecca95cf 100644 --- a/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp +++ b/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp @@ -40,6 +40,8 @@ #include #include +#include + ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature() { } @@ -122,6 +124,7 @@ void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName) void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName) { + try { std::string anError; XAO::Xao aXao; @@ -168,7 +171,13 @@ void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName) // fill selection AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list"); - aSelectionList->setSelectionType(XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension())); + + // conversion of dimension + XAO::Dimension aGroupDimension = aXaoGroup->getDimension(); + std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension()); + std::string aSelectionType = ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString); + + aSelectionList->setSelectionType(aSelectionType); for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) { aSelectionList->append(aResultBody, GeomShapePtr()); // complex conversion of element index to reference id @@ -184,6 +193,12 @@ void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName) document()->setCurrentFeature(aGroupFeature, true); } + + } catch (XAO::XAO_Exception& e) { + std::string anError = e.what(); + setError("An error occurred while importing " + theFileName + ": " + anError); + return; + } } //============================================================================ diff --git a/src/ExchangePlugin/ExchangePlugin_Tools.cpp b/src/ExchangePlugin/ExchangePlugin_Tools.cpp index 26391487d..51f22b82b 100644 --- a/src/ExchangePlugin/ExchangePlugin_Tools.cpp +++ b/src/ExchangePlugin/ExchangePlugin_Tools.cpp @@ -17,3 +17,32 @@ std::list ExchangePlugin_Tools::split(const std::string& theString, theResult.push_back(aSection); return theResult; } + +std::string ExchangePlugin_Tools::selectionType2xaoDimension(const std::string& theType) +{ + if (theType == "Vertices") + return "vertex"; + else if (theType == "Edges") + return "edge"; + else if (theType == "Faces") + return "face"; + else if (theType == "Solids") + return "solid"; + + return std::string(); +} + +std::string ExchangePlugin_Tools::xaoDimension2selectionType(const std::string& theDimension) +{ + if (theDimension == "vertex") + return "Vertices"; + else if (theDimension == "edge") + return "Edges"; + else if (theDimension == "face") + return "Faces"; + else if (theDimension == "solid") + return "Solids"; + + return std::string(); +} + diff --git a/src/ExchangePlugin/ExchangePlugin_Tools.h b/src/ExchangePlugin/ExchangePlugin_Tools.h index 58d0dd19f..c481d8ddc 100644 --- a/src/ExchangePlugin/ExchangePlugin_Tools.h +++ b/src/ExchangePlugin/ExchangePlugin_Tools.h @@ -22,6 +22,12 @@ public: static std::list split(const std::string& theString, char theDelimiter); + /// Converts string representation of selection type to XAO dimension. + static std::string selectionType2xaoDimension(const std::string& theString); + + /// Converts string representation of XAO dimension to selection type. + static std::string xaoDimension2selectionType(const std::string& theDimension); + }; #endif /* EXCHANGEPLUGIN_TOOLS_H_ */ diff --git a/src/ExchangePlugin/export_widget.xml b/src/ExchangePlugin/export_widget.xml index 1444d4643..d00baf07b 100644 --- a/src/ExchangePlugin/export_widget.xml +++ b/src/ExchangePlugin/export_widget.xml @@ -10,6 +10,11 @@ + + + - - - diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_XAOExport.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_XAOExport.cpp index 6bd812792..8bf37d538 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_XAOExport.cpp +++ b/src/GeomAlgoAPI/GeomAlgoAPI_XAOExport.cpp @@ -13,13 +13,34 @@ #include #include +//============================================================================= +bool SetShapeToXAO(const std::shared_ptr& theShape, + XAO::Xao* theXao, + std::string& theError) +{ + if (!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); + } catch (XAO::XAO_Exception& e) { + theError = e.what(); + return false; + } + return true; +} + //============================================================================= /*! * */ //============================================================================= bool XAOExport(const std::string& theFileName, - const std::shared_ptr& theShape, XAO::Xao* theXao, std::string& theError) { @@ -27,16 +48,12 @@ bool XAOExport(const std::string& theFileName, std::cout << "Export XAO into file " << theFileName << std::endl; #endif - if (theFileName.empty() || !theShape.get() || !theXao) { + if (theFileName.empty() || !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(); diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_XAOExport.h b/src/GeomAlgoAPI/GeomAlgoAPI_XAOExport.h index 5f535cfec..4ad789272 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_XAOExport.h +++ b/src/GeomAlgoAPI/GeomAlgoAPI_XAOExport.h @@ -17,10 +17,15 @@ namespace XAO { class Xao; } // namespace XAO +/// Defines shape for the XAO object +GEOMALGOAPI_EXPORT +bool SetShapeToXAO(const std::shared_ptr& theShape, + XAO::Xao* theXao, + std::string& theError); + /// 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); -- 2.39.2