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