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