Salome HOME
[bos #35140] [EDF] (2023-T1) Memory communication between SHAPER and GEOM
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_XAOImport.cpp
index b0309c2739b565b51c555a4d373d8236ae3ee271..5b241497642e6d8c6a15fcbedf5aa8bbd50893e4 100644 (file)
@@ -1,13 +1,24 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File:    GEOMALGOAPI_XAOImport.cpp
-// Created: Nov 25, 2015
-// Author:  Sergey POKHODENKO
+// Copyright (C) 2014-2023  CEA, EDF
+//
+// 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 <GeomAlgoAPI_XAOImport.h>
 
-#include <cassert>
-
 #include <TopoDS_Shape.hxx>
 
 #include <XAO_XaoExporter.hxx>
@@ -22,11 +33,14 @@ std::shared_ptr<GeomAPI_Shape> 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<GeomAPI_Shape>();
+  }
+
   TopoDS_Shape aShape;
   try {
     if (XAO::XaoExporter::readFromFile(theFileName, theXao)) {
@@ -52,3 +66,46 @@ std::shared_ptr<GeomAPI_Shape> XAOImport(const std::string& theFileName,
   aGeomShape->setImpl(new TopoDS_Shape(aShape));
   return aGeomShape;
 }
+
+//=============================================================================
+/*!
+ *
+ */
+//=============================================================================
+std::shared_ptr<GeomAPI_Shape> XAOImportMem(const std::string& theMemoryBuff,
+                                            std::string& theError,
+                                            XAO::Xao* theXao)
+{
+#ifdef _DEBUG
+  std::cout << "Import XAO from file " << theFileName << std::endl;
+#endif
+  if (theMemoryBuff.empty() || !theXao) {
+    theError = "An invalid argument.";
+    return std::shared_ptr<GeomAPI_Shape>();
+  }
+
+  TopoDS_Shape aShape;
+  try {
+    if (XAO::XaoExporter::setXML(theMemoryBuff, theXao)) {
+      XAO::Geometry* aGeometry = theXao->getGeometry();
+      XAO::Format aFormat = aGeometry->getFormat();
+      if (aFormat == XAO::BREP) {
+        if (XAO::BrepGeometry* aBrepGeometry = dynamic_cast<XAO::BrepGeometry*>(aGeometry))
+          aShape = aBrepGeometry->getTopoDS_Shape();
+      } else {
+        theError = "Unsupported XAO geometry format:" + XAO::XaoUtils::shapeFormatToString(aFormat);
+        aShape.Nullify();
+      }
+    } else {
+      theError = "XAO object was not read successful";
+      aShape.Nullify();
+    }
+  } catch (XAO::XAO_Exception& e) {
+    theError = e.what();
+    aShape.Nullify();
+  }
+
+  std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
+  aGeomShape->setImpl(new TopoDS_Shape(aShape));
+  return aGeomShape;
+}