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