From 23fe0c1915e5023b57c24aa49bc2b3c44c540f01 Mon Sep 17 00:00:00 2001 From: Clarisse Genrault Date: Fri, 7 Aug 2020 14:25:13 +0200 Subject: [PATCH] Add export root for a box with material et media --- src/ExchangePlugin/CMakeLists.txt | 4 + .../ExchangePlugin_ExportFeature.cpp | 108 ++++++++++++++++ .../ExchangePlugin_ExportFeature.h | 29 +++++ .../ExchangePlugin_ExportRoot.cpp | 115 +++++++++++++++++ .../ExchangePlugin_ExportRoot.h | 47 +++++++ src/ExchangePlugin/export_widget.xml | 19 +++ src/GeomAlgoAPI/CMakeLists.txt | 2 + src/GeomAlgoAPI/GeomAlgoAPI_ROOTExport.cpp | 116 ++++++++++++++++++ src/GeomAlgoAPI/GeomAlgoAPI_ROOTExport.h | 68 ++++++++++ 9 files changed, 508 insertions(+) create mode 100644 src/ExchangePlugin/ExchangePlugin_ExportRoot.cpp create mode 100644 src/ExchangePlugin/ExchangePlugin_ExportRoot.h create mode 100644 src/GeomAlgoAPI/GeomAlgoAPI_ROOTExport.cpp create mode 100644 src/GeomAlgoAPI/GeomAlgoAPI_ROOTExport.h diff --git a/src/ExchangePlugin/CMakeLists.txt b/src/ExchangePlugin/CMakeLists.txt index 222f2e6ea..cc82d7da6 100644 --- a/src/ExchangePlugin/CMakeLists.txt +++ b/src/ExchangePlugin/CMakeLists.txt @@ -27,6 +27,8 @@ INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/src/Events ${PROJECT_SOURCE_DIR}/src/GeomAPI ${PROJECT_SOURCE_DIR}/src/GeomAlgoAPI ${PROJECT_SOURCE_DIR}/src/XAO + ${PROJECT_SOURCE_DIR}/src/PrimitivesPlugin + ${PROJECT_SOURCE_DIR}/src/CollectionPlugin ) SET(PROJECT_HEADERS @@ -37,6 +39,7 @@ SET(PROJECT_HEADERS ExchangePlugin_Validators.h ExchangePlugin_Tools.h ExchangePlugin_Dump.h + ExchangePlugin_ExportRoot.h ) SET(PROJECT_SOURCES @@ -46,6 +49,7 @@ SET(PROJECT_SOURCES ExchangePlugin_Validators.cpp ExchangePlugin_Tools.cpp ExchangePlugin_Dump.cpp + ExchangePlugin_ExportRoot.cpp ) SET(XML_RESOURCES diff --git a/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp b/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp index 4eedccc26..c87694870 100644 --- a/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp +++ b/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp @@ -26,6 +26,7 @@ #include #include #endif +#include #include #include @@ -33,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -41,6 +43,7 @@ #include #include +#include #include #include #include @@ -64,8 +67,17 @@ #include #include +#include #include +#include + +#ifdef WIN32 +# define _separator_ '\\' +#else +# define _separator_ '/' +#endif + ExchangePlugin_ExportFeature::ExchangePlugin_ExportFeature() { } @@ -86,6 +98,8 @@ void ExchangePlugin_ExportFeature::initAttributes() ModelAPI_AttributeString::typeId()); data()->addAttribute(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID(), ModelAPI_AttributeString::typeId()); + data()->addAttribute(ExchangePlugin_ExportFeature::ROOT_FILE_PATH_ID(), + ModelAPI_AttributeString::typeId()); data()->addAttribute(ExchangePlugin_ExportFeature::FILE_FORMAT_ID(), ModelAPI_AttributeString::typeId()); data()->addAttribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID(), @@ -94,6 +108,12 @@ void ExchangePlugin_ExportFeature::initAttributes() ModelAPI_AttributeString::typeId()); data()->addAttribute(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID(), ModelAPI_AttributeString::typeId()); + data()->addAttribute(ExchangePlugin_ExportFeature::ROOT_MANAGER_NAME_ID(), + ModelAPI_AttributeString::typeId()); + data()->addAttribute(ExchangePlugin_ExportFeature::ROOT_MANAGER_TITLE_ID(), + ModelAPI_AttributeString::typeId()); + data()->addAttribute(ExchangePlugin_ExportFeature::MAT_FILE_ID(), + ModelAPI_AttributeString::typeId()); ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID()); @@ -108,6 +128,9 @@ void ExchangePlugin_ExportFeature::attributeChanged(const std::string& theID) if (theID == XAO_FILE_PATH_ID()) { string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->setValue( string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID())->value()); + } else if (theID == ROOT_FILE_PATH_ID()) { + string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->setValue( + string(ExchangePlugin_ExportFeature::ROOT_FILE_PATH_ID())->value()); } } @@ -119,10 +142,14 @@ void ExchangePlugin_ExportFeature::execute() AttributeStringPtr aFormatAttr = this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID()); std::string aFormat = aFormatAttr->value(); + std::cout<<"FORMAT"<string(ExchangePlugin_ExportFeature::FILE_PATH_ID()); std::string aFilePath = aFilePathAttr->value(); + std::cout<<"FILE"<selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID()); @@ -558,6 +596,76 @@ void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName) // LCOV_EXCL_STOP } + +void ExchangePlugin_ExportFeature::exportROOT(const std::string& theFileName) +{ + std::cout<<"EXPORT ROOT ==> debut"<value(); + std::string aTitle = string(ExchangePlugin_ExportFeature::ROOT_MANAGER_TITLE_ID())->value(); + std::string aFileMat = string(ExchangePlugin_ExportFeature::MAT_FILE_ID())->value(); + + std::shared_ptr anAlgo(new GeomAlgoAPI_ROOTExport(theFileName)); + + std::list listNames = ExchangePlugin_Tools::split(theFileName, _separator_); + listNames = ExchangePlugin_Tools::split(listNames.back(), '.'); + + // Create the head of file + anAlgo->buildHead(listNames.front(), aName, aTitle); + + // Materials and medias + std::map > aMat; + std::map > aMedium; + ExchangePlugin_ExportRoot::readFileMat(aFileMat, aMat, aMedium); + std::cout<<"Size "<buildMatAndMedium(aMat, aMedium); + + // Add feature in the file + std::list theExport = document()->allFeatures(); + std::list::iterator itExport = theExport.begin(); + std::vector aListNamesOfFeatures; + for (; itExport != theExport.end(); ++itExport) + { + FeaturePtr aCurFeature = *itExport; + if (aCurFeature->getKind() == "Box") { + double anOx, anOy, anOz, aDx, aDy, aDz; + ExchangePlugin_ExportRoot::computeBox(aCurFeature, anOx, anOy, anOz, aDx, aDy, aDz); + //std::cout<<"BOX EN COURS (Feature) :: "<data()->name()<firstResult()->data()->name()<firstResult()->data()->name(); + anAlgo->buildBox(anObjectName, anOx, anOy, anOz, aDx, aDy, aDz); + aListNamesOfFeatures.push_back(anObjectName); + aListNamesOfFeatures.push_back(aCurFeature->data()->name()); + } + } + + std::cout<<"Nb of elements :: " << aListNamesOfFeatures.size() << std::endl; + + // Add all groups in the file + itExport = theExport.begin(); + for (; itExport != theExport.end(); ++itExport) + { + FeaturePtr aCurFeature = *itExport; + if (aCurFeature->getKind() == "Group") { + std::vector aListNames; + std::string anObjectName = aCurFeature->firstResult()->data()->name(); + ExchangePlugin_ExportRoot::computeGroup(aCurFeature, aListNames); + + //for (std::vector::iterator it = myvector.begin() ; it != myvector.end(); ++it) + for (std::vector::iterator it = aListNames.begin(); it != aListNames.end(); it++) { + std::string aName = anObjectName + "_" + *it; + anAlgo->BuildVolume(aName, *it, anObjectName); + } + } + } + + anAlgo->buildEnd(); + + // Create the file with the content + anAlgo->write(); +} + bool ExchangePlugin_ExportFeature::isMacro() const { if (!data().get() || !data()->isValid()) diff --git a/src/ExchangePlugin/ExchangePlugin_ExportFeature.h b/src/ExchangePlugin/ExchangePlugin_ExportFeature.h index c0a79a8c5..5d5060cbc 100644 --- a/src/ExchangePlugin/ExchangePlugin_ExportFeature.h +++ b/src/ExchangePlugin/ExchangePlugin_ExportFeature.h @@ -84,6 +84,32 @@ public: static const std::string MY_XAO_GEOMETRY_NAME_ID("xao_geometry_name"); return MY_XAO_GEOMETRY_NAME_ID; } + + /// attribute name of root file path + inline static const std::string& ROOT_FILE_PATH_ID() + { + static const std::string MY_ROOT_FILE_PATH_ID("root_file_path"); + return MY_ROOT_FILE_PATH_ID; + } + /// attribute name of author for XAO format + inline static const std::string& ROOT_MANAGER_NAME_ID() + { + static const std::string MY_ROOT_MANAGER_NAME_ID("root_manager_name"); + return MY_ROOT_MANAGER_NAME_ID; + } + /// attribute name of geometry name for XAO format + inline static const std::string& ROOT_MANAGER_TITLE_ID() + { + static const std::string MY_ROOT_MANAGER_TITLE_ID("root_manager_title"); + return MY_ROOT_MANAGER_TITLE_ID; + } + /// attribute name of materials file + inline static const std::string& MAT_FILE_ID() + { + static const std::string MY_MAT_FILE_ID("mat_file"); + return MY_MAT_FILE_ID; + } + /// Default constructor EXCHANGEPLUGIN_EXPORT ExchangePlugin_ExportFeature(); /// Default destructor @@ -123,6 +149,9 @@ protected: /// Performs export to XAO file EXCHANGEPLUGIN_EXPORT void exportXAO(const std::string& theFileName); + + /// Performs export to XAO file + EXCHANGEPLUGIN_EXPORT void exportROOT(const std::string& theFileName); }; #endif /* EXPORT_EXPORTFEATURE_H_ */ diff --git a/src/ExchangePlugin/ExchangePlugin_ExportRoot.cpp b/src/ExchangePlugin/ExchangePlugin_ExportRoot.cpp new file mode 100644 index 000000000..61aabc2a6 --- /dev/null +++ b/src/ExchangePlugin/ExchangePlugin_ExportRoot.cpp @@ -0,0 +1,115 @@ +// Copyright (C) 2014-2019 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#include + +#include +#include +#include + +#include +#include +#include + +#include +#include + +void ExchangePlugin_ExportRoot::readFileMat(const std::string theFileMat, + std::map >& aMat, + std::map >& aMedias) +{ + std::string aLine; + std::ifstream aFile (theFileMat); + if (aFile.is_open()) + { + while ( getline (aFile,aLine) ) + { + std::list aList = ExchangePlugin_Tools::split(aLine, ' '); + std::list::iterator itList = aList.begin(); + + bool isFirst = true; + std::string aName; + std::vector aData; + for (; itList != aList.end(); ++itList) + { + std::string anElem = *itList; + if (isFirst) { + aName = anElem; + isFirst = false; + } else { + aData.push_back(anElem); + } + } + std::cout<<"aType :: "<data()->string(PrimitivesPlugin_Box::CREATION_METHOD())->value(); + if (aMethodName == "BoxByDimensions") { + DX = (theCurFeature->data()->real(PrimitivesPlugin_Box::DX_ID())->value())/2; + DY = (theCurFeature->data()->real(PrimitivesPlugin_Box::DY_ID())->value())/2; + DZ = (theCurFeature->data()->real(PrimitivesPlugin_Box::DZ_ID())->value())/2; + OX = DX; + OY = DY; + OZ = DZ; + } else if (aMethodName == "BoxByTwoPoints") { + // A completer + } else if (aMethodName == "BoxByOnePointAndDims") { + DX = theCurFeature->data()->real(PrimitivesPlugin_Box::HALF_DX_ID())->value(); + DY = theCurFeature->data()->real(PrimitivesPlugin_Box::HALF_DY_ID())->value(); + DY = theCurFeature->data()->real(PrimitivesPlugin_Box::HALF_DZ_ID())->value(); + OX = theCurFeature->data()->real(PrimitivesPlugin_Box::OX_ID())->value(); + OY = theCurFeature->data()->real(PrimitivesPlugin_Box::OY_ID())->value(); + OZ = theCurFeature->data()->real(PrimitivesPlugin_Box::OZ_ID())->value(); + } +} + +void ExchangePlugin_ExportRoot::computeGroup(FeaturePtr theCurFeature, + std::vector& theListNames) +{ + std::cout<<"COMPUTE GROUP"<data()->selectionList(CollectionPlugin_Group::LIST_ID()); + + for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) { + std::shared_ptr anObjectAttr = + anObjectsSelList->value(anObjectsIndex); + ObjectPtr anObject = anObjectAttr->contextObject(); + std::string aName = anObject->data()->name(); + std::cout<<"NAME :: "< +#include + +#include + +/** + * \class ExchangePlugin_Tools + * \brief Internal tools for the ExchangePlugin. + */ +class EXCHANGEPLUGIN_EXPORT ExchangePlugin_ExportRoot { +public: + /// Compute the list of materials and medias + static void readFileMat(const std::string theFileMat, + std::map >& aMat, + std::map >& aMedias); + + /// Compute .... + static void computeBox(FeaturePtr theCurFeature, + double& OX, double& OY, double& OZ, double& DX, double& DY, double& DZ); + + /// Compute .... + static void computeGroup(FeaturePtr theCurFeature, std::vector& theListNames); +}; + +#endif /* EXCHANGEPLUGIN_EXPORTROOT_H_ */ diff --git a/src/ExchangePlugin/export_widget.xml b/src/ExchangePlugin/export_widget.xml index 9e134b805..c41380564 100644 --- a/src/ExchangePlugin/export_widget.xml +++ b/src/ExchangePlugin/export_widget.xml @@ -31,5 +31,24 @@ placeholder="Please input the geometry name"> + + + + + + + + + + + diff --git a/src/GeomAlgoAPI/CMakeLists.txt b/src/GeomAlgoAPI/CMakeLists.txt index 235851975..3349b7968 100644 --- a/src/GeomAlgoAPI/CMakeLists.txt +++ b/src/GeomAlgoAPI/CMakeLists.txt @@ -82,6 +82,7 @@ SET(PROJECT_HEADERS GeomAlgoAPI_SolidClassifier.h GeomAlgoAPI_MapShapesAndAncestors.h GeomAlgoAPI_Chamfer.h + GeomAlgoAPI_ROOTExport.h ) SET(PROJECT_SOURCES @@ -144,6 +145,7 @@ SET(PROJECT_SOURCES GeomAlgoAPI_MapShapesAndAncestors.cpp GeomAlgoAPI_Projection.cpp GeomAlgoAPI_Chamfer.cpp + GeomAlgoAPI_ROOTExport.cpp ) SET(PROJECT_LIBRARIES diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_ROOTExport.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_ROOTExport.cpp new file mode 100644 index 000000000..476b075eb --- /dev/null +++ b/src/GeomAlgoAPI/GeomAlgoAPI_ROOTExport.cpp @@ -0,0 +1,116 @@ +// Copyright (C) 2014-2019 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#include + +#include + +#include + +//================================================================================================= +GeomAlgoAPI_ROOTExport::GeomAlgoAPI_ROOTExport(const std::string& theFileName) +{ + myFileName = theFileName; + myContent = ""; +} + +//================================================================================================= +void GeomAlgoAPI_ROOTExport::buildHead(const std::string& theMethodName, + const std::string& theName, + const std::string& theTitle) +{ + std::string tmp = "void " + theMethodName + "() {\n"; + myContent += tmp; + tmp = "gSystem->Load(\"libGeom\");\n"; + myContent += tmp; + tmp = "TGeoManager *geom = new TGeoManager(\"" + theName + "\",\"" + theTitle +"\");\n"; + myContent += tmp; +} + +//================================================================================================= +void GeomAlgoAPI_ROOTExport::buildBox(const std::string& theObjectName, + const double theOX, const double theOY, const double theOZ, + const double theDX, const double theDY, const double theDZ) +{ + std::cout<<"buildBox"< > theMat, + const std::map > theMedium) +{ + std::map >::const_iterator anIt = theMat.begin(), + aLast = theMat.end(); + for (; anIt != aLast; anIt++) { + std::vector aValues = anIt->second; + myContent += "TGeoMaterial *" + anIt->first + " = new TGeoMaterial(\""; + myContent += aValues[1] + "\"," + aValues[2] + "," + aValues[3] + "," + aValues[4] + ") ;\n"; + } + + std::map >::const_iterator anIt2 = theMedium.begin(), + aLast2 = theMedium.end(); + for (; anIt2 != aLast2; anIt2++) { + std::vector aValues = anIt2->second; + myContent += "TGeoMedium *" + anIt2->first + " = new TGeoMedium(\""; + myContent += aValues[1] + "\"," + aValues[2] + "," + aValues[3] + ") ;\n"; + } +} + +//================================================================================================= +void GeomAlgoAPI_ROOTExport::BuildVolume(const std::string theName, + const std::string theGeometryName, + const std::string theMediumName) +{ + myContent += "TGeoVolume *" + theName + " = new TGeoVolume(\"" + theName; + myContent += "\"," + theGeometryName + "," + theMediumName + ");\n"; +} + +//================================================================================================= +void GeomAlgoAPI_ROOTExport::buildEnd() +{ + myContent += "}"; +} + +//================================================================================================= +bool GeomAlgoAPI_ROOTExport::write() +{ + std::ofstream aFile; + std::cout< + +#include +#include +#include + +/**\class GeomAlgoAPI_ROOTExport + * \ingroup DataAlgo + * \brief Allows to export results to a root file + */ +class GeomAlgoAPI_ROOTExport +{ +public: + GEOMALGOAPI_EXPORT GeomAlgoAPI_ROOTExport(const std::string& theFileName); + + /// Build the head of file + GEOMALGOAPI_EXPORT void buildHead(const std::string& theMethodName, const std::string& theName, + const std::string& theTitle); + + /// Build box + GEOMALGOAPI_EXPORT void buildBox(const std::string& theObjectName, + const double theOX, const double theOY, const double theOZ, + const double theDX, const double theDY, const double theDZ); + + /// Build mat and medium + GEOMALGOAPI_EXPORT void buildMatAndMedium( + const std::map > theMat, + const std::map > theMedium); + + /// + GEOMALGOAPI_EXPORT void BuildVolume(const std::string theName, + const std::string theGeometryName, + const std::string theMediumName); + + /// Build the end of file + GEOMALGOAPI_EXPORT void buildEnd(); + + /// Write the file + GEOMALGOAPI_EXPORT bool write(); + +private: + std::string myFileName; + std::string myContent; +}; + +#endif /* GEOMALGOAPI_ROOTEXPORT_H_ */ -- 2.39.2