Salome HOME
ddcc0db21b1fa3c6bb171b160ec7c45f2f69feef
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_STLImport.cpp
1 // Copyright (C) 2014-2023  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_STLImport.h>
21
22 #include "GeomAlgoAPI_Tools.h"
23
24 #include <Basics_OCCTVersion.hxx>
25
26 #include <TopoDS.hxx>
27 #include <TopoDS_Shape.hxx>
28 #include <TopoDS_Shell.hxx>
29 #include <BRepBuilderAPI_MakeSolid.hxx>
30 #include <BRepBuilderAPI_Sewing.hxx>
31 #include <StlAPI_Reader.hxx>
32
33 std::shared_ptr<GeomAPI_Shape> STLImport(const std::string& theFileName,
34                                          std::string& theError)
35 {
36   TopoDS_Shape aResShape;
37   StlAPI_Reader aReader;
38
39   try
40   {
41     if(!aReader.Read(aResShape, theFileName.c_str()))
42     {
43       theError = "Can't import file.";
44       aResShape.Nullify();
45     }
46 #if OCC_VERSION_LARGE >= 0x07070000
47     BRepBuilderAPI_Sewing aSewingTool;
48     aSewingTool.Init(1.0e-06, Standard_True);
49     aSewingTool.Load(aResShape);
50     aSewingTool.Perform();
51     TopoDS_Shape aSewedShape = aSewingTool.SewedShape();
52     if (!aSewedShape.IsNull())
53       aResShape = aSewedShape;
54 #endif
55     if(aResShape.ShapeType() == TopAbs_SHELL)
56     {
57       BRepBuilderAPI_MakeSolid soliMaker(TopoDS::Shell(aResShape));
58       soliMaker.Build();
59       if(soliMaker.IsDone())
60         aResShape = soliMaker.Shape();
61     }
62   }
63   catch (Standard_Failure const& anException) {
64     theError = anException.GetMessageString();
65     aResShape.Nullify();
66   }
67   std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
68   aGeomShape->setImpl(new TopoDS_Shape(aResShape));
69   return aGeomShape;
70 }