]> SALOME platform Git repositories - modules/geom.git/blob - src/STLImport/STLImport.cxx
Salome HOME
Update copyrights 2014.
[modules/geom.git] / src / STLImport / STLImport.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:        STLImport.cxx
24 // Author:      Sergey KHROMOV
25
26
27 #include "utilities.h"
28 #include <BRep_Builder.hxx>
29 #include <BRep_Tool.hxx>
30 #include <BRepClass3d_SolidClassifier.hxx>
31 #include <Precision.hxx>
32 #include <StlAPI_Reader.hxx>
33 #include <TCollection_AsciiString.hxx>
34 #include <TopAbs_ShapeEnum.hxx>
35 #include <TopoDS.hxx>
36 #include <TopoDS_Shape.hxx>
37 #include <TDF_Label.hxx>
38
39
40 #ifdef WIN32
41   #if defined STLIMPORT_EXPORTS || defined STLImport_EXPORTS
42     #define STLIMPORT_EXPORT __declspec( dllexport )
43   #else
44     #define STLIMPORT_EXPORT __declspec( dllimport )
45   #endif
46 #else
47   #define STLIMPORT_EXPORT
48 #endif
49
50
51 //=============================================================================
52 /*!
53  *
54  */
55 //=============================================================================
56
57 extern "C"
58 {
59 STLIMPORT_EXPORT
60   TopoDS_Shape Import (const TCollection_AsciiString& theFileName,
61                        const TCollection_AsciiString& /*theFormatName*/,
62                        TCollection_AsciiString&       theError,
63                        const TDF_Label&)
64   {
65     MESSAGE("Import STL from file " << theFileName.ToCString());
66
67     StlAPI_Reader aReader;
68     TopoDS_Shape aShape;
69
70     aReader.Read(aShape, theFileName.ToCString());
71
72     if (aShape.IsNull()) {
73       theError = "STL Import failed";
74     } else {
75       // Fix the orientation of closed shell or solid.
76       if (BRep_Tool::IsClosed(aShape)) {
77         TopAbs_ShapeEnum aType = aShape.ShapeType();
78
79         if (aType == TopAbs_SHELL || aType == TopAbs_SOLID) {
80           TopoDS_Solid aSolid;
81
82           if (aType == TopAbs_SHELL) {
83             // Create a solid.
84             BRep_Builder aBuilder;
85
86             aBuilder.MakeSolid(aSolid);
87             aBuilder.Add(aSolid, aShape);
88           } else {
89             aSolid = TopoDS::Solid(aShape);
90           }
91
92           // Classify infinite point against solid.
93           BRepClass3d_SolidClassifier aClassifier(aSolid);
94
95           aClassifier.PerformInfinitePoint(Precision::Confusion());
96
97           if (aClassifier.State() == TopAbs_IN) {
98             // The shape is inverted. Reverse it.
99             aShape.Reverse();
100           }
101         }
102       }
103     }
104
105     return aShape;
106   }
107 }