From 2e0d64ac1ff5f0d23c5bd6c0f38bc231483034c0 Mon Sep 17 00:00:00 2001 From: cgenraul Date: Fri, 2 Oct 2020 16:35:03 +0200 Subject: [PATCH] Add API Python to export ROOT and add translation to export ROOT --- src/ExchangeAPI/ExchangeAPI_Export.cpp | 58 ++++++++++++++++++- src/ExchangeAPI/ExchangeAPI_Export.h | 48 ++++++++++++--- src/ExchangePlugin/CMakeLists.txt | 2 + .../ExchangePlugin_ExportFeature.cpp | 25 +++++++- .../ExchangePlugin_ExportFeature.h | 12 ++++ .../ExchangePlugin_ExportRoot.cpp | 22 ++++++- .../ExchangePlugin_ExportRoot.h | 3 + src/ExchangePlugin/export_widget.xml | 15 ++++- src/GeomAlgoAPI/GeomAlgoAPI_ROOTExport.cpp | 28 +++++++-- src/GeomAlgoAPI/GeomAlgoAPI_ROOTExport.h | 7 ++- src/PythonAPI/model/exchange/__init__.py | 2 +- 11 files changed, 202 insertions(+), 20 deletions(-) diff --git a/src/ExchangeAPI/ExchangeAPI_Export.cpp b/src/ExchangeAPI/ExchangeAPI_Export.cpp index fa1ab009e..7cdb2ea25 100644 --- a/src/ExchangeAPI/ExchangeAPI_Export.cpp +++ b/src/ExchangeAPI/ExchangeAPI_Export.cpp @@ -72,7 +72,7 @@ ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr& } -/// Constructor with values for export in other formats than XAO. +/// Constructor with values for export in other formats than XAO or ROOT. ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr& theFeature, const std::string & theFilePath, const std::list & theSelectionList, @@ -89,6 +89,29 @@ ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr& apply(); // finish operation to make sure the export is done on the current state of the history } +/// Constructor with values for ROOT export. +ExchangeAPI_Export::ExchangeAPI_Export(const std::shared_ptr& theFeature, + const std::string & theFilePath, + const std::string & theManagerName, + const std::string & theManagerTitle, + const std::string & theMatFile, + const std::string & theRootNameFile, + const ModelHighAPI_Selection & theMainObject) +: ModelHighAPI_Interface(theFeature) +{ + initialize(); + fillAttribute("ROOT", theFeature->string(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID())); + fillAttribute(theFilePath, theFeature->string(ExchangePlugin_ExportFeature::ROOT_FILE_PATH_ID())); + fillAttribute(theManagerName, theFeature->string(ExchangePlugin_ExportFeature::ROOT_MANAGER_NAME_ID())); + fillAttribute(theManagerTitle, theFeature->string(ExchangePlugin_ExportFeature::ROOT_MANAGER_TITLE_ID())); + fillAttribute(theMatFile, theFeature->string(ExchangePlugin_ExportFeature::MAT_FILE_ID())); + fillAttribute(theRootNameFile, theFeature->string(ExchangePlugin_ExportFeature::EXP_NAME_FILE_ID())); + fillAttribute(theMainObject, theFeature->selection(ExchangePlugin_ExportFeature::MAIN_OBJECT_ID())); + fillAttribute("ROOT", theFeature->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID())); + execute(); + apply(); // finish operation to make sure the export is done on the current state of the history +} + ExchangeAPI_Export::~ExchangeAPI_Export() { } @@ -142,6 +165,24 @@ void ExchangeAPI_Export::dump(ModelHighAPI_Dumper& theDumper) const theDumper << ", '" << theGeometryName << "'"; theDumper << ")" << std::endl; } + else if (exportType == "ROOT") { + std::string aTmpROOTFile = + aBase->string(ExchangePlugin_ExportFeature::ROOT_FILE_PATH_ID())->value(); + correctSeparators(aTmpROOTFile); + theDumper << "exportToROOT(" << aDocName << ", '" << aTmpROOTFile << "'" ; + std::string theManagerName = aBase->string(ExchangePlugin_ExportFeature::ROOT_MANAGER_NAME_ID())->value(); + theDumper << ", '" << theManagerName << "'"; + std::string theManagerTitle =aBase->string(ExchangePlugin_ExportFeature::ROOT_MANAGER_TITLE_ID())->value(); + theDumper << ", '" << theManagerTitle << "'"; + std::string theMatFile = aBase->string(ExchangePlugin_ExportFeature::MAT_FILE_ID())->value(); + theDumper << ", '" << theMatFile << "'"; + std::string theExpNameFile = aBase->string(ExchangePlugin_ExportFeature::EXP_NAME_FILE_ID())->value(); + theDumper << ", '" << theExpNameFile << "'"; + AttributeSelectionPtr anAttrObject = + aBase->selection(ExchangePlugin_ExportFeature::MAIN_OBJECT_ID()); + theDumper << ", " << anAttrObject; + theDumper << ")" << std::endl; + } else { std::string aFilePath = aBase->string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->value(); correctSeparators(aFilePath); @@ -188,4 +229,19 @@ ExportPtr exportToXAO(const std::shared_ptr & thePart, return ExportPtr(new ExchangeAPI_Export(aFeature, theFilePath, theSelectedShape, "XAO")); } +ExportPtr exportToROOT(const std::shared_ptr & thePart, + const std::string & theFilePath, + const std::string & theManagerName, + const std::string & theManagerTitle, + const std::string & theMatFile, + const std::string & theRootNameFile, + const ModelHighAPI_Selection& theMainObject) +{ + apply(); // finish previous operation to make sure all previous operations are done + std::shared_ptr aFeature = + thePart->addFeature(ExchangePlugin_ExportFeature::ID()); + return ExportPtr(new ExchangeAPI_Export(aFeature, theFilePath, theManagerName, theManagerTitle, + theMatFile, theRootNameFile, theMainObject)); +} + //-------------------------------------------------------------------------------------- diff --git a/src/ExchangeAPI/ExchangeAPI_Export.h b/src/ExchangeAPI/ExchangeAPI_Export.h index 6cfddfd8f..2c8aacd7c 100644 --- a/src/ExchangeAPI/ExchangeAPI_Export.h +++ b/src/ExchangeAPI/ExchangeAPI_Export.h @@ -54,24 +54,33 @@ public: /// Constructor with values for XAO of selected result export. EXCHANGEAPI_EXPORT - explicit ExchangeAPI_Export(const std::shared_ptr& theFeature, - const std::string & theFilePath, - const ModelHighAPI_Selection& theResult, - const std::string & theAuthor, - const std::string & theGeometryName = std::string()); + explicit ExchangeAPI_Export(const std::shared_ptr& theFeature, + const std::string & theFilePath, + const ModelHighAPI_Selection& theResult, + const std::string & theAuthor, + const std::string & theGeometryName = std::string()); - /// Constructor with values for export in other formats than XAO. + /// Constructor with values for export in other formats than XAO or ROOT. EXCHANGEAPI_EXPORT explicit ExchangeAPI_Export(const std::shared_ptr& theFeature, const std::string & theFilePath, const std::list & theSelectionList, const std::string & theFileFormat = std::string()); + + EXCHANGEAPI_EXPORT + explicit ExchangeAPI_Export(const std::shared_ptr& theFeature, + const std::string & theFilePath, + const std::string & theManagerName, + const std::string & theManagerTitle, + const std::string & theMatFile, + const std::string & theRootNameFile, + const ModelHighAPI_Selection & theMainObject); /// Destructor. EXCHANGEAPI_EXPORT virtual ~ExchangeAPI_Export(); - INTERFACE_7(ExchangePlugin_ExportFeature::ID(), + INTERFACE_12(ExchangePlugin_ExportFeature::ID(), exportType, ExchangePlugin_ExportFeature::EXPORT_TYPE_ID(), ModelAPI_AttributeString, /** ExportType */, filePath, ExchangePlugin_ExportFeature::FILE_PATH_ID(), @@ -85,7 +94,18 @@ public: xaoAuthor, ExchangePlugin_ExportFeature::XAO_AUTHOR_ID(), ModelAPI_AttributeString, /** xao author */, xaoGeometryName, ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID(), - ModelAPI_AttributeString, /** xao geometry name */) + ModelAPI_AttributeString, /** xao geometry name */, + rootFilePath, ExchangePlugin_ExportFeature::ROOT_FILE_PATH_ID(), + ModelAPI_AttributeString, /** root file path */, + rootMainObject, ExchangePlugin_ExportFeature::MAIN_OBJECT_ID(), + ModelAPI_AttributeSelection, /**root main ovject */, + rootManagerName, ExchangePlugin_ExportFeature::ROOT_MANAGER_NAME_ID(), + ModelAPI_AttributeString, /** root manager name */, + rootManagerTitle, ExchangePlugin_ExportFeature::ROOT_MANAGER_TITLE_ID(), + ModelAPI_AttributeString, /** root manager title */, + rootNameFile , ExchangePlugin_ExportFeature::EXP_NAME_FILE_ID(), + ModelAPI_AttributeString, /** root name file */ + ) /// Dump wrapped feature EXCHANGEAPI_EXPORT @@ -123,6 +143,18 @@ ExportPtr exportToXAO(const std::shared_ptr & thePart, const std::string & theAuthor = std::string(), const std::string & theGeometryName = std::string()); + /**\ingroup CPPHighAPI + * \brief Exports to ROOT file all features of the current document. + */ + EXCHANGEAPI_EXPORT + ExportPtr exportToROOT(const std::shared_ptr & thePart, + const std::string & theFilePath, + const std::string & theManagerName, + const std::string & theManagerTitle, + const std::string & theMatFile, + const std::string & theRootNameFile, + const ModelHighAPI_Selection& theMainObject); + //-------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------- #endif /* SRC_EXCHANGEAPI_EXCHANGEAPI_EXPORT_H_ */ diff --git a/src/ExchangePlugin/CMakeLists.txt b/src/ExchangePlugin/CMakeLists.txt index cc82d7da6..5a727b1bc 100644 --- a/src/ExchangePlugin/CMakeLists.txt +++ b/src/ExchangePlugin/CMakeLists.txt @@ -29,6 +29,7 @@ INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/src/Events ${PROJECT_SOURCE_DIR}/src/XAO ${PROJECT_SOURCE_DIR}/src/PrimitivesPlugin ${PROJECT_SOURCE_DIR}/src/CollectionPlugin + ${PROJECT_SOURCE_DIR}/src/FeaturesPlugin ) SET(PROJECT_HEADERS @@ -88,5 +89,6 @@ ADD_UNIT_TESTS(TestImport.py Test2459.py TestExportToXAOWithFields.py TestExportToXAOWithGroupNotUpdated.py + TestExportToROOT.py TestExport_FiniteValidator.py ) diff --git a/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp b/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp index 0d2752db7..1734424ab 100644 --- a/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp +++ b/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp @@ -108,10 +108,15 @@ 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::EXP_NAME_FILE_ID(), + ModelAPI_AttributeString::typeId()); + data()->addAttribute(ExchangePlugin_ExportFeature::MAIN_OBJECT_ID(), + ModelAPI_AttributeSelection::typeId()); data()->addAttribute(ExchangePlugin_ExportFeature::MAT_FILE_ID(), ModelAPI_AttributeString::typeId()); @@ -626,6 +631,13 @@ void ExchangePlugin_ExportFeature::exportROOT(const std::string& theFileName) anAlgo->buildBox(anObjectName, anOx, anOy, anOz, aDx, aDy, aDz); aListNamesOfFeatures.push_back(anObjectName); aListNamesOfFeatures.push_back(aCurFeature->data()->name()); + } else if (aCurFeature->getKind() == "Translation") { + double aDx, aDy, aDz; + std::string anObjectName = aCurFeature->firstResult()->data()->name(); + ExchangePlugin_ExportRoot::computeTranslation(aCurFeature, aDx, aDy, aDz); + anAlgo->buildTranslation(anObjectName, aDx, aDy, aDz); + aListNamesOfFeatures.push_back(anObjectName); + aListNamesOfFeatures.push_back(aCurFeature->data()->name()); } } @@ -647,7 +659,18 @@ void ExchangePlugin_ExportFeature::exportROOT(const std::string& theFileName) } } - anAlgo->buildEnd(); + std::string aExportFileName = string(ExchangePlugin_ExportFeature::EXP_NAME_FILE_ID())->value(); + AttributeSelectionPtr anObjectAttr = selection(MAIN_OBJECT_ID()); + FeaturePtr aFeature = anObjectAttr->contextFeature(); + std::string aNameShape =""; + if (aFeature.get()) { + aNameShape = aFeature->firstResult()->data()->name(); + } else { + ObjectPtr anObject = anObjectAttr->contextObject(); + aNameShape = anObject->data()->name(); + } + + anAlgo->buildEnd(aNameShape, aExportFileName); // Create the file with the content anAlgo->write(); diff --git a/src/ExchangePlugin/ExchangePlugin_ExportFeature.h b/src/ExchangePlugin/ExchangePlugin_ExportFeature.h index 5d5060cbc..415212db9 100644 --- a/src/ExchangePlugin/ExchangePlugin_ExportFeature.h +++ b/src/ExchangePlugin/ExchangePlugin_ExportFeature.h @@ -103,6 +103,18 @@ public: static const std::string MY_ROOT_MANAGER_TITLE_ID("root_manager_title"); return MY_ROOT_MANAGER_TITLE_ID; } + /// attribute name of file export + inline static const std::string& EXP_NAME_FILE_ID() + { + static const std::string MY_EXP_NAME_FILE_ID("root_name_file"); + return MY_EXP_NAME_FILE_ID; + } + /// Attribute name of the main solid. + inline static const std::string& MAIN_OBJECT_ID() + { + static const std::string MY_MAIN_OBJECT_ID("root_main_object"); + return MY_MAIN_OBJECT_ID; + } /// attribute name of materials file inline static const std::string& MAT_FILE_ID() { diff --git a/src/ExchangePlugin/ExchangePlugin_ExportRoot.cpp b/src/ExchangePlugin/ExchangePlugin_ExportRoot.cpp index a4244a2db..ebab302dd 100644 --- a/src/ExchangePlugin/ExchangePlugin_ExportRoot.cpp +++ b/src/ExchangePlugin/ExchangePlugin_ExportRoot.cpp @@ -27,6 +27,7 @@ #include #include +#include #include #include @@ -81,7 +82,8 @@ void ExchangePlugin_ExportRoot::computeBox(FeaturePtr theCurFeature, double& OX, double& OY, double& OZ, double& DX, double& DY, double& DZ) { - std::string aMethodName = theCurFeature->data()->string(PrimitivesPlugin_Box::CREATION_METHOD())->value(); + std::string aMethodName = + theCurFeature->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; @@ -90,8 +92,10 @@ void ExchangePlugin_ExportRoot::computeBox(FeaturePtr theCurFeature, OY = DY; OZ = DZ; } else if (aMethodName == "BoxByTwoPoints") { - AttributeSelectionPtr aRef1 = theCurFeature->data()->selection(PrimitivesPlugin_Box::POINT_FIRST_ID()); - AttributeSelectionPtr aRef2 = theCurFeature->data()->selection(PrimitivesPlugin_Box::POINT_SECOND_ID()); + AttributeSelectionPtr aRef1 = + theCurFeature->data()->selection(PrimitivesPlugin_Box::POINT_FIRST_ID()); + AttributeSelectionPtr aRef2 = + theCurFeature->data()->selection(PrimitivesPlugin_Box::POINT_SECOND_ID()); GeomShapePtr aShape1 = aRef1->value(); if (!aShape1.get()) aShape1 = aRef1->context()->shape(); @@ -122,6 +126,18 @@ void ExchangePlugin_ExportRoot::computeBox(FeaturePtr theCurFeature, } } +void ExchangePlugin_ExportRoot::computeTranslation(FeaturePtr theCurFeature, + double& DX, double& DY, double& DZ) +{ + std::string aMethodName = + theCurFeature->data()->string(FeaturesPlugin_Translation::CREATION_METHOD())->value(); + if (aMethodName == "ByDimensions") { + DX = theCurFeature->data()->real(FeaturesPlugin_Translation::DX_ID())->value(); + DY = theCurFeature->data()->real(FeaturesPlugin_Translation::DY_ID())->value(); + DZ = theCurFeature->data()->real(FeaturesPlugin_Translation::DZ_ID())->value(); + } +} + void ExchangePlugin_ExportRoot::computeGroup(FeaturePtr theCurFeature, std::vector& theListNames) { diff --git a/src/ExchangePlugin/ExchangePlugin_ExportRoot.h b/src/ExchangePlugin/ExchangePlugin_ExportRoot.h index b82bf0954..66c3a76a9 100644 --- a/src/ExchangePlugin/ExchangePlugin_ExportRoot.h +++ b/src/ExchangePlugin/ExchangePlugin_ExportRoot.h @@ -40,6 +40,9 @@ public: static void computeBox(FeaturePtr theCurFeature, double& OX, double& OY, double& OZ, double& DX, double& DY, double& DZ); + /// Compute .... + static void computeTranslation(FeaturePtr theCurFeature, double& DX, double& DY, double& DZ); + /// Compute .... static void computeGroup(FeaturePtr theCurFeature, std::vector& theListNames); }; diff --git a/src/ExchangePlugin/export_widget.xml b/src/ExchangePlugin/export_widget.xml index c41380564..af10009e3 100644 --- a/src/ExchangePlugin/export_widget.xml +++ b/src/ExchangePlugin/export_widget.xml @@ -45,8 +45,21 @@ + placeholder="Please input the title of the title"> + + + + + diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_ROOTExport.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_ROOTExport.cpp index 31fc748eb..9bc8a2d85 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_ROOTExport.cpp +++ b/src/GeomAlgoAPI/GeomAlgoAPI_ROOTExport.cpp @@ -52,11 +52,21 @@ void GeomAlgoAPI_ROOTExport::buildBox(const std::string& theObjectName, { myContent += "Double_t point_"+theObjectName+"[3] = {"+doubleToString(theOX)+","; myContent += doubleToString(theOY)+","+doubleToString(theOZ)+"};\n"; - myContent += "TGeoBBox* " + theObjectName + "= new TGeoBBox(\"" +theObjectName + "\","; + myContent += "TGeoBBox *" + theObjectName + "_tmp = new TGeoBBox(\"" +theObjectName + "_tmp\","; myContent += doubleToString(theDX)+","+doubleToString(theDY)+","+doubleToString(theDZ)+",point_"; myContent += theObjectName + ");\n"; } +//================================================================================================= +void GeomAlgoAPI_ROOTExport::buildTranslation(const std::string& theObjectName, + const double theDX, const double theDY, + const double theDZ) +{ + myContent += "TGeoTranslation *" + theObjectName; + myContent += "_tmp = new TGeoTranslation(\"" + theObjectName + "_tmp\","; + myContent += doubleToString(theDX) + "," + doubleToString(theDY) + ","; + myContent += doubleToString(theDZ) + ");\n"; +} //================================================================================================= void GeomAlgoAPI_ROOTExport::buildMatAndMedium( @@ -85,13 +95,23 @@ 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"; + myContent += "TGeoVolume *" + theGeometryName + " = new TGeoVolume(\"" + theName; + myContent += "\"," + theGeometryName + "_tmp," + theMediumName + ");\n"; } //================================================================================================= -void GeomAlgoAPI_ROOTExport::buildEnd() +void GeomAlgoAPI_ROOTExport::buildEnd(const std::string theSolidName, + const std::string theExportName) { + myContent += "// ####################################\n"; + myContent += "geom->SetTopVolume(" + theSolidName + ");\n"; + myContent += "geom->CloseGeometry();\n"; + myContent += theSolidName + "->SetVisContainers(kTRUE);\n"; + myContent += "geom->SetTopVisible(kTRUE);\n"; + myContent += "geom->Export(\"" + theExportName + "\");\n"; + myContent += "geom->CheckOverlaps(0.0001);\n"; + myContent += "geom->PrintOverlaps();\n"; + myContent += theSolidName + "->Draw();\n"; myContent += "}"; } diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_ROOTExport.h b/src/GeomAlgoAPI/GeomAlgoAPI_ROOTExport.h index 7349a534b..4f0492eb3 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_ROOTExport.h +++ b/src/GeomAlgoAPI/GeomAlgoAPI_ROOTExport.h @@ -43,6 +43,11 @@ public: 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 translation + GEOMALGOAPI_EXPORT void buildTranslation(const std::string& theObjectName, + const double theDX, const double theDY, + const double theDZ); /// Build mat and medium GEOMALGOAPI_EXPORT void buildMatAndMedium( @@ -55,7 +60,7 @@ public: const std::string theMediumName); /// Build the end of file - GEOMALGOAPI_EXPORT void buildEnd(); + GEOMALGOAPI_EXPORT void buildEnd(const std::string theName, const std::string theExportName); /// Write the file GEOMALGOAPI_EXPORT bool write(); diff --git a/src/PythonAPI/model/exchange/__init__.py b/src/PythonAPI/model/exchange/__init__.py index 308e1e6f3..05901f874 100644 --- a/src/PythonAPI/model/exchange/__init__.py +++ b/src/PythonAPI/model/exchange/__init__.py @@ -19,4 +19,4 @@ """Package for Exchange plugin for the Parametric Geometry API of the Modeler. """ -from ExchangeAPI import addImport, exportToFile, exportToXAO \ No newline at end of file +from ExchangeAPI import addImport, exportToFile, exportToXAO, exportToROOT -- 2.39.2