1 // Copyright (C) 2014-2022 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include "STLPlugin_ImportDriver.hxx"
22 #include "STLPlugin_IImport.hxx"
25 #include <utilities.h>
26 #include <Basics_Utils.hxx>
27 #include <Basics_OCCTVersion.hxx>
30 #include "GEOM_Function.hxx"
31 #include "GEOMImpl_Types.hxx"
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>
41 #include <Precision.hxx>
42 #include <BRepClass3d_SolidClassifier.hxx>
44 //=======================================================================
47 //=======================================================================
48 const Standard_GUID& STLPlugin_ImportDriver::GetID()
50 static Standard_GUID aGUID("69e33dc7-9630-4a73-ac19-cc4da1f659b8");
54 //=======================================================================
55 //function : STLPlugin_ImportDriver
57 //=======================================================================
58 STLPlugin_ImportDriver::STLPlugin_ImportDriver()
62 //=======================================================================
65 //=======================================================================
66 Standard_Integer STLPlugin_ImportDriver::Execute(Handle(TFunction_Logbook)& log) const
68 if( Label().IsNull() ) return 0;
69 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction( Label() );
71 STLPlugin_IImport aData( aFunction );
73 TCollection_AsciiString aFileName = aData.GetFileName().ToCString();
75 MESSAGE( "Import STL from file " << aFileName );
77 // Set "C" numeric locale to save numbers correctly
78 Kernel_Utils::Localizer loc;
80 StlAPI_Reader aReader;
83 aReader.Read( aShape, aData.GetFileName().ToCString() );
85 if( aShape.IsNull() ) return 0;
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())
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 ) {
102 if( aType == TopAbs_SHELL ) {
104 BRep_Builder aBuilder;
105 aBuilder.MakeSolid( aSolid );
106 aBuilder.Add(aSolid, aShape );
109 aSolid = TopoDS::Solid( aShape );
111 // Classify infinite point against solid.
112 BRepClass3d_SolidClassifier aClassifier( aSolid );
113 aClassifier.PerformInfinitePoint(Precision::Confusion());
115 if( aClassifier.State() == TopAbs_IN ) {
116 // The shape is inverted. Reverse it.
122 aFunction->SetValue( aShape );
124 log->SetTouched(Label());
129 //================================================================================
131 * \brief Returns a name of creation operation and names and values of creation parameters
133 //================================================================================
135 bool STLPlugin_ImportDriver::
136 GetCreationInformation( std::string& theOperationName,
137 std::vector<GEOM_Param>& theParams )
139 if( Label().IsNull() ) return 0;
140 Handle(GEOM_Function) function = GEOM_Function::GetFunction( Label() );
142 STLPlugin_IImport aCI( function );
143 Standard_Integer aType = function->GetType();
145 theOperationName = "ImportSTL";
149 AddParam( theParams, "File name", aCI.GetFileName() );
157 IMPLEMENT_STANDARD_RTTIEXT( STLPlugin_ImportDriver, GEOM_BaseDriver )