Salome HOME
Copyright update 2022
[modules/geom.git] / src / STLPlugin / STLPlugin_ImportDriver.cxx
1 // Copyright (C) 2014-2022  CEA/DEN, EDF R&D, OPEN CASCADE
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 // internal includes
21 #include "STLPlugin_ImportDriver.hxx"
22 #include "STLPlugin_IImport.hxx"
23
24 // KERNEL includes
25 #include <utilities.h>
26 #include <Basics_Utils.hxx>
27
28 // GEOM includes
29 #include "GEOM_Function.hxx"
30 #include "GEOMImpl_Types.hxx"
31
32 // OOCT includes
33 #include <StlAPI_Reader.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <TopoDS_Solid.hxx>
36 #include <BRep_Tool.hxx>
37 #include <BRep_Builder.hxx>
38 #include <TopoDS.hxx>
39 #include <Precision.hxx>
40 #include <BRepClass3d_SolidClassifier.hxx>
41
42 //=======================================================================
43 //function : GetID
44 //purpose  :
45 //=======================================================================
46 const Standard_GUID& STLPlugin_ImportDriver::GetID()
47 {
48   static Standard_GUID aGUID("69e33dc7-9630-4a73-ac19-cc4da1f659b8");
49   return aGUID;
50 }
51
52 //=======================================================================
53 //function : STLPlugin_ImportDriver
54 //purpose  :
55 //=======================================================================
56 STLPlugin_ImportDriver::STLPlugin_ImportDriver()
57 {
58 }
59
60 //=======================================================================
61 //function : Execute
62 //purpose  :
63 //=======================================================================
64 Standard_Integer STLPlugin_ImportDriver::Execute(Handle(TFunction_Logbook)& log) const
65 {
66   if( Label().IsNull() ) return 0;
67   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction( Label() );
68
69   STLPlugin_IImport aData( aFunction );
70
71   TCollection_AsciiString aFileName = aData.GetFileName().ToCString();
72
73   MESSAGE( "Import STL to file " << aFileName );
74
75   // Set "C" numeric locale to save numbers correctly
76   Kernel_Utils::Localizer loc;
77
78   StlAPI_Reader aReader;
79   TopoDS_Shape aShape;
80
81   aReader.Read( aShape, aData.GetFileName().ToCString() );
82
83   if( aShape.IsNull() ) return 0;
84
85   // Fix the orientation of closed shell or solid.
86   if( BRep_Tool::IsClosed( aShape ) ) {
87     TopAbs_ShapeEnum aType = aShape.ShapeType();
88     if( aType == TopAbs_SHELL || aType == TopAbs_SOLID ) {
89       TopoDS_Solid aSolid;
90       if( aType == TopAbs_SHELL ) {
91         // Create a solid.
92         BRep_Builder aBuilder;
93         aBuilder.MakeSolid( aSolid );
94         aBuilder.Add(aSolid, aShape );
95       }
96       else
97         aSolid = TopoDS::Solid( aShape );
98
99       // Classify infinite point against solid.
100       BRepClass3d_SolidClassifier aClassifier( aSolid );
101       aClassifier.PerformInfinitePoint(Precision::Confusion());
102
103       if( aClassifier.State() == TopAbs_IN ) {
104         // The shape is inverted. Reverse it.
105         aShape.Reverse();
106       }
107     }
108   }
109
110   aFunction->SetValue( aShape );
111
112   log->SetTouched(Label());
113
114   return 1;
115 }
116
117 //================================================================================
118 /*!
119  * \brief Returns a name of creation operation and names and values of creation parameters
120  */
121 //================================================================================
122
123 bool STLPlugin_ImportDriver::
124 GetCreationInformation( std::string&             theOperationName,
125                         std::vector<GEOM_Param>& theParams )
126 {
127   if( Label().IsNull() ) return 0;
128   Handle(GEOM_Function) function = GEOM_Function::GetFunction( Label() );
129
130   STLPlugin_IImport aCI( function );
131   Standard_Integer aType = function->GetType();
132
133   theOperationName = "ImportSTL";
134
135   switch ( aType ) {
136   case IMPORT_SHAPE:
137     AddParam( theParams, "File name", aCI.GetFileName() );
138     break;
139   default:
140     return false;
141   }
142   return true;
143 }
144
145 IMPLEMENT_STANDARD_RTTIEXT( STLPlugin_ImportDriver, GEOM_BaseDriver )