1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File: STLImport.cxx
24 // Author: Sergey KHROMOV
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>
36 #include <TopoDS_Shape.hxx>
37 #include <TDF_Label.hxx>
41 #if defined STLIMPORT_EXPORTS || defined STLImport_EXPORTS
42 #define STLIMPORT_EXPORT __declspec( dllexport )
44 #define STLIMPORT_EXPORT __declspec( dllimport )
47 #define STLIMPORT_EXPORT
51 //=============================================================================
55 //=============================================================================
60 TopoDS_Shape Import (const TCollection_AsciiString& theFileName,
61 const TCollection_AsciiString& /*theFormatName*/,
62 TCollection_AsciiString& theError,
65 MESSAGE("Import STL from file " << theFileName.ToCString());
67 StlAPI_Reader aReader;
70 aReader.Read(aShape, theFileName.ToCString());
72 if (aShape.IsNull()) {
73 theError = "STL Import failed";
75 // Fix the orientation of closed shell or solid.
76 if (BRep_Tool::IsClosed(aShape)) {
77 TopAbs_ShapeEnum aType = aShape.ShapeType();
79 if (aType == TopAbs_SHELL || aType == TopAbs_SOLID) {
82 if (aType == TopAbs_SHELL) {
84 BRep_Builder aBuilder;
86 aBuilder.MakeSolid(aSolid);
87 aBuilder.Add(aSolid, aShape);
89 aSolid = TopoDS::Solid(aShape);
92 // Classify infinite point against solid.
93 BRepClass3d_SolidClassifier aClassifier(aSolid);
95 aClassifier.PerformInfinitePoint(Precision::Confusion());
97 if (aClassifier.State() == TopAbs_IN) {
98 // The shape is inverted. Reverse it.