Salome HOME
[bos #40620] [CEA] XAO export for SHAPER: Allow export of brep in a separate file
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_ExportFeature.cpp
index f8fd34a8a452fdae87e740c119275135cbef6092..b536f54efffda46310111d1a82a450112e701059 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2022  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2024  CEA, EDF
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -27,7 +27,6 @@
 #include <ostream>
 #endif
 
-
 #include <Config_Common.h>
 #include <Config_PropManager.h>
 
@@ -39,6 +38,7 @@
 #include <GeomAlgoAPI_Tools.h>
 #include <GeomAlgoAPI_XAOExport.h>
 
+#include <GeomAPI_IndexedMapOfShape.h>
 #include <GeomAPI_Shape.h>
 #include <GeomAPI_ShapeExplorer.h>
 #include <GeomAPI_Trsf.h>
@@ -51,6 +51,7 @@
 #include <ModelAPI_AttributeIntArray.h>
 #include <ModelAPI_AttributeTables.h>
 #include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_AttributeBoolean.h>
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Document.h>
 #include <ModelAPI_Object.h>
@@ -118,6 +119,15 @@ void ExchangePlugin_ExportFeature::initAttributes()
   data()->addAttribute(ExchangePlugin_ExportFeature::STL_FILE_TYPE(),
    ModelAPI_AttributeString::typeId());
 
+  // export to memory buffer (implemented for XAO format only)
+  data()->addAttribute(ExchangePlugin_ExportFeature::MEMORY_BUFFER_ID(),
+                       ModelAPI_AttributeString::typeId());
+
+  data()->addAttribute(ExchangePlugin_ExportFeature::XAO_SHAPE_FILE_PATH_ID(),
+                       ModelAPI_AttributeString::typeId());
+  data()->addAttribute(ExchangePlugin_ExportFeature::XAO_SHAPE_FILE_SEPARATE_ID(),
+                       ModelAPI_AttributeBoolean::typeId());
+
   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
     ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID());
   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
@@ -128,11 +138,18 @@ void ExchangePlugin_ExportFeature::initAttributes()
     ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID());
   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
     ExchangePlugin_ExportFeature::XAO_SELECTION_LIST_ID());
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
+    ExchangePlugin_ExportFeature::MEMORY_BUFFER_ID());
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
+    ExchangePlugin_ExportFeature::XAO_SHAPE_FILE_PATH_ID());
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
+    ExchangePlugin_ExportFeature::XAO_SHAPE_FILE_SEPARATE_ID());
 
   // to support previous version of document, move the selection list
   // if the type of export operation is XAO
   AttributeStringPtr aTypeAttr = string(EXPORT_TYPE_ID());
-  if (aTypeAttr->isInitialized() && aTypeAttr->value() == "XAO") {
+  if (aTypeAttr->isInitialized() &&
+      (aTypeAttr->value() == "XAO" || aTypeAttr->value() == "XAOMem")) {
     bool aWasBlocked = data()->blockSendAttributeUpdated(true, false);
     AttributeSelectionListPtr aSelList = selectionList(SELECTION_LIST_ID());
     AttributeSelectionListPtr aXAOSelList = selectionList(XAO_SELECTION_LIST_ID());
@@ -153,7 +170,6 @@ void ExchangePlugin_ExportFeature::attributeChanged(const std::string& theID)
     string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->setValue(
       string(ExchangePlugin_ExportFeature::STL_FILE_PATH_ID())->value());
   }
-
 }
 
 /*
@@ -165,21 +181,29 @@ void ExchangePlugin_ExportFeature::execute()
       this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
   std::string aFormat = aFormatAttr->value();
 
+  bool isMemoryExport = false;
+  AttributeStringPtr aTypeAttr = string(EXPORT_TYPE_ID());
+  if (aTypeAttr->isInitialized() && aTypeAttr->value() == "XAOMem")
+    isMemoryExport = true;
+
   AttributeStringPtr aFilePathAttr =
       this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
   std::string aFilePath = aFilePathAttr->value();
-  if (aFilePath.empty())
+  if (aFilePath.empty() && !isMemoryExport)
     return;
 
-  exportFile(aFilePath, aFormat);
+  exportFile(aFilePath, aFormat, isMemoryExport);
 }
 
 void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
-                                              const std::string& theFormat)
+                                              const std::string& theFormat,
+                                              const bool         isMemoryExport)
 {
   std::string aFormatName = theFormat;
 
   if (aFormatName.empty()) { // get default format for the extension
+    if (isMemoryExport) return;
+
     // ".brep" -> "BREP"
     std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
     if (anExtension == "BREP" || anExtension == "BRP") {
@@ -194,9 +218,9 @@ void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
   }
 
   if (aFormatName == "XAO") {
-    exportXAO(theFileName);
+    exportXAO(theFileName, isMemoryExport);
     return;
-  }else if (aFormatName == "STL") {
+  } else if (aFormatName == "STL") {
     exportSTL(theFileName);
     return;
   }
@@ -205,6 +229,7 @@ void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
   AttributeSelectionListPtr aSelectionListAttr =
       this->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
   std::list<GeomShapePtr> aShapes;
+  std::list<ResultPtr> aContexts;
   for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i) {
     AttributeSelectionPtr anAttrSelection = aSelectionListAttr->value(i);
 
@@ -218,7 +243,10 @@ void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
     if (aCurShape.get() == NULL)
       aCurShape = anAttrSelection->context()->shape();
     if (aCurShape.get() != NULL)
+    {
       aShapes.push_back(aCurShape);
+      aContexts.push_back(anAttrSelection->context());
+    }
   }
 
   // Store compound if we have more than one shape.
@@ -231,7 +259,7 @@ void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
   if (aFormatName == "BREP") {
     aResult = BREPExport(theFileName, aFormatName, aShape, anError);
   } else if (aFormatName == "STEP") {
-    aResult = STEPExport(theFileName, aFormatName, aShape, anError);
+    aResult = STEPExport(theFileName, aShapes, aContexts, anError);
   } else if (aFormatName.substr(0, 4) == "IGES") {
     aResult = IGESExport(theFileName, aFormatName, aShape, anError);
   } else {
@@ -246,7 +274,8 @@ void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
 
 /// Returns XAO string by the value from the table
 static std::string valToString(const ModelAPI_AttributeTables::Value& theVal,
-  const ModelAPI_AttributeTables::ValueType& theType) {
+                               const ModelAPI_AttributeTables::ValueType& theType)
+{
   std::ostringstream aStr; // the resulting string value
   switch(theType) {
   case ModelAPI_AttributeTables::BOOLEAN:
@@ -307,7 +336,8 @@ void ExchangePlugin_ExportFeature::exportSTL(const std::string& theFileName)
 }
 
 
-void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
+void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName,
+                                             const bool         isMemoryExport)
 {
   try {
 
@@ -394,12 +424,22 @@ void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
     // get the name from the first result
     ResultPtr aResultBody = *aResults.begin();
     aGeometryName = Locale::Convert::toString(aResultBody->data()->name());
+    if (isMemoryExport) {
+      // for python dump
+      string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->setValue(aGeometryName);
+      // or
+      //data()->setName(Locale::Convert::toWString(aGeometryName));
+    }
   }
 
   aXao.getGeometry()->setName(aGeometryName);
 
   std::set<ResultPtr> allResultsCashed; // cash to speed up searching in all results selected
 
+  // [bos #38360] [CEA] improve performances of exportXAO and PublishToStudy
+  GeomAPI_IndexedMapOfShape aSubShapesMap;
+  bool isSubShapesMap = false; // we will init it only if required (for performance reason)
+
   // iterate all documents used
   if (aDocuments.empty())
     aDocuments.push_back(document());
@@ -438,7 +478,12 @@ void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
           GeomShapePtr aGroupShape = aGroupResExplorer.current();
           if (aDocTrsf.find(*aDoc) != aDocTrsf.end())
             aGroupShape->move(aDocTrsf[*aDoc]);
-          int aReferenceID = GeomAlgoAPI_CompoundBuilder::id(aShape, aGroupShape);
+
+          if (!isSubShapesMap) {
+            aSubShapesMap.MapShapes(aShape);
+            isSubShapesMap = true;
+          }
+          int aReferenceID = aSubShapesMap.FindIndexEqualLocations(aGroupShape);
           if (aReferenceID == 0) // selected value does not found in the exported shape
             continue;
           std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
@@ -514,7 +559,11 @@ void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
               if (!isWholePart) {
                 // element index actually is the ID of the selection
                 AttributeSelectionPtr aSel = aSelectionList->value(aRow - 1);
-                int aReferenceID = GeomAlgoAPI_CompoundBuilder::id(aShape, aSel->value());
+                if (!isSubShapesMap) {
+                  aSubShapesMap.MapShapes(aShape);
+                  isSubShapesMap = true;
+                }
+                int aReferenceID = aSubShapesMap.FindIndexEqualLocations(aSel->value());
                 if (aReferenceID == 0) // selected value does not found in the exported shape
                   continue;
 
@@ -561,7 +610,24 @@ void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
   }
 
   // exporting
-  XAOExport(theFileName, &aXao, anError);
+  if (isMemoryExport) {
+    string(ExchangePlugin_ExportFeature::MEMORY_BUFFER_ID())->setValue(XAOExportMem(&aXao, anError));
+  }
+  else {
+    std::string aShapeFile ("");
+    AttributeBooleanPtr aShapeSeparateAttr = boolean(XAO_SHAPE_FILE_SEPARATE_ID());
+    if (aShapeSeparateAttr->isInitialized() &&
+        aShapeSeparateAttr->value() == true) {
+      aShapeFile = theFileName + ".brep";
+    }
+    else {
+      AttributeStringPtr aShapeFileAttr = string(XAO_SHAPE_FILE_PATH_ID());
+      if (aShapeFileAttr->isInitialized())
+        aShapeFile = aShapeFileAttr->value();
+    }
+
+    XAOExport(theFileName, &aXao, anError, aShapeFile);
+  }
 
   if (!anError.empty()) {
     setError("An error occurred while exporting " + theFileName + ": " + anError);