Salome HOME
7c9c03dd3b64a47de37ae724e5ee603243575adb
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_XAOExport.cpp
1 // Copyright (C) 2014-2021  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <GeomAlgoAPI_XAOExport.h>
21
22 #include "GeomAlgoAPI_Tools.h"
23
24 #include <TopoDS_Shape.hxx>
25
26 #include <XAO_XaoExporter.hxx>
27 #include <XAO_BrepGeometry.hxx>
28
29 //=============================================================================
30 bool SetShapeToXAO(const std::shared_ptr<GeomAPI_Shape>& theShape,
31                    XAO::Xao* theXao,
32                    std::string& theError)
33 {
34   if (!theShape.get() || !theXao) {
35     theError = "An invalid argument.";
36     return false;
37   }
38
39   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
40   try {
41     XAO::BrepGeometry* aGeometry = new XAO::BrepGeometry;
42     theXao->setGeometry(aGeometry);
43     aGeometry->setTopoDS_Shape(aShape);
44   } catch (XAO::XAO_Exception& e) {
45     theError = e.what();
46     return false;
47   }
48   return true;
49 }
50
51 //=============================================================================
52 /*!
53  *
54  */
55 //=============================================================================
56 bool XAOExport(const std::string& theFileName,
57                XAO::Xao* theXao,
58                std::string& theError)
59 {
60   #ifdef _DEBUG
61   std::cout << "Export XAO into file " << theFileName << std::endl;
62   #endif
63
64   if (theFileName.empty() || !theXao) {
65     theError = "An invalid argument.";
66     return false;
67   }
68
69   try {
70     XAO::BrepGeometry* aGeometry = dynamic_cast<XAO::BrepGeometry*>(theXao->getGeometry());
71     TopoDS_Shape aShape = aGeometry->getTopoDS_Shape();
72     bool aWasFree = aShape.Free(); // make top level topology free, same as imported
73     if (!aWasFree)
74       aShape.Free(Standard_True);
75     XAO::XaoExporter::saveToFile(theXao, theFileName, "");
76     if (!aWasFree)
77       aShape.Free(Standard_False);
78   } catch (XAO::XAO_Exception& e) {
79     theError = e.what();
80     return false;
81   }
82   return true;
83 }