Salome HOME
Adding support for BRep for shape
[modules/smesh.git] / src / SMESH / SMESH_DriverShape.cxx
1 // Copyright (C) 2007-2021  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   : SMESH_DriverShape.cxx
24 //  Author : Yoann AUDOUIN, EDF
25 //  Module : SMESH
26 //
27
28 #include "SMESH_DriverShape.hxx"
29
30 // step include
31 #include <STEPControl_Reader.hxx>
32 #include <STEPControl_Writer.hxx>
33 #include <Interface_Static.hxx>
34
35 // Brep include
36 #include <BRepTools.hxx>
37 #include <BRep_Builder.hxx>
38
39 //Occ include
40 #include <TopoDS.hxx>
41
42 #include <boost/filesystem.hpp>
43 #include <boost/algorithm/string.hpp>
44 namespace fs = boost::filesystem;
45
46 /**
47  * @brief Import the content of a shape file (STEP) into a TopDS_Shape object
48  *
49  * @param shape_file the shape file
50  * @param aShape the object
51  *
52  * @return error code
53  */
54 int importSTEPShape(const std::string shape_file, TopoDS_Shape& aShape){
55
56   std::cout << "Importing STEP shape from " << shape_file << std::endl;
57   STEPControl_Reader reader;
58   // Forcing Unit in meter
59   Interface_Static::SetCVal("xstep.cascade.unit","M");
60   Interface_Static::SetIVal("read.step.ideas", 1);
61   Interface_Static::SetIVal("read.step.nonmanifold", 1);
62   IFSelect_ReturnStatus aStat = reader.ReadFile(shape_file.c_str());
63   if(aStat != IFSelect_RetDone)
64     std::cout << "Reading error for "  << shape_file << std::endl;
65
66   int NbTrans = reader.TransferRoots();
67   // There should be only one shape within the file
68   assert(NbTrans==1);
69   aShape = reader.OneShape();
70
71   return true;
72 }
73
74 /**
75  * @brief Export the content of a TopoDS_Shape into a shape file (STEP)
76  *
77  * @param shape_file the shape file
78  * @param aShape the object
79  *
80  * @return error code
81  */
82 int exportSTEPShape(const std::string shape_file, const TopoDS_Shape& aShape){
83
84   std::cout << "Exporting STEP shape to " << shape_file << std::endl;
85
86   STEPControl_Writer aWriter;
87   // Forcing Unit in meter
88   Interface_Static::SetCVal("xstep.cascade.unit","M");
89   Interface_Static::SetCVal("write.step.unit","M");
90   Interface_Static::SetIVal("write.step.nonmanifold", 1);
91
92   IFSelect_ReturnStatus aStat = aWriter.Transfer(aShape,STEPControl_AsIs);
93   if(aStat != IFSelect_RetDone)
94     std::cout << "Transfer error for "  << shape_file << std::endl;
95
96   aStat = aWriter.Write(shape_file.c_str());
97
98   if(aStat != IFSelect_RetDone)
99     std::cout << "Writing error for "  << shape_file << std::endl;
100
101   return aStat;
102 }
103
104 /**
105  * @brief Import the content of a shape file (BREP) into a TopDS_Shape object
106  *
107  * @param shape_file the shape file
108  * @param aShape the object
109  *
110  * @return error code
111  */
112 int importBREPShape(const std::string shape_file, TopoDS_Shape& aShape){
113
114   std::cout << "Importing BREP shape from " << shape_file << std::endl;
115   BRep_Builder builder;
116   BRepTools::Read(aShape, shape_file.c_str(), builder);
117
118   return true;
119 }
120
121 /**
122  * @brief Export the content of a TopoDS_Shape into a shape file (BREP)
123  *
124  * @param shape_file the shape file
125  * @param aShape the object
126  *
127  * @return error code
128  */
129 int exportBREPShape(const std::string shape_file, const TopoDS_Shape& aShape){
130
131   std::cout << "Exporting BREP shape to " << shape_file << std::endl;
132   BRepTools::Write(aShape, shape_file.c_str());
133
134   return true;
135 }
136
137 /**
138  * @brief Import the content of a shape file into a TopDS_Shape object
139  *
140  * @param shape_file the shape file
141  * @param aShape the object
142  *
143  * @return error code
144  */
145 int importShape(const std::string shape_file, TopoDS_Shape& aShape){
146   std::string type = fs::path(shape_file).extension().string();
147   boost::algorithm::to_lower(type);
148   if (type == ".brep"){
149     return importBREPShape(shape_file, aShape);
150   } else if (type == ".step"){
151     return importSTEPShape(shape_file, aShape);
152   } else {
153     std::cout << "Unknow format: " << type << std::endl;
154     return false;
155   }
156 }
157
158 /**
159  * @brief Import the content of a shape file into a TopDS_Shape object
160  *
161  * @param shape_file the shape file
162  * @param aShape the object
163  *
164  * @return error code
165  */
166 int exportShape(const std::string shape_file, const TopoDS_Shape& aShape){
167   std::string type = fs::path(shape_file).extension().string();
168   boost::algorithm::to_lower(type);
169   if (type == ".brep"){
170     return exportBREPShape(shape_file, aShape);
171   } else if (type == ".step"){
172     return exportSTEPShape(shape_file, aShape);
173   } else {
174     std::cout << "Unknow format: " << type << std::endl;
175     return false;
176   }
177 }