Salome HOME
Mesh object not deleted in Python + deleting temporary folder when destructing SMESH_Mesh
[modules/smesh.git] / src / SMESH / SMESH_DriverStep.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_DriverStep.cxx
24 //  Author : Yoann AUDOUIN, EDF
25 //  Module : SMESH
26 //
27
28 #include "SMESH_DriverStep.hxx"
29
30 #include <STEPControl_Reader.hxx>
31 #include <STEPControl_Writer.hxx>
32 #include <Interface_Static.hxx>
33
34 //Occ include
35 #include <TopoDS.hxx>
36
37
38 /**
39  * @brief Compares two shape file (STEP)
40  *
41  * @param file1 first file
42  * @param file2 second file
43  *
44  * @return true if the files are the same
45  */
46 bool diffStepFile(std::string file1, std::string file2){
47   std::ifstream sfile1(file1);
48   std::ifstream sfile2(file2);
49   std::string line1, line2;
50   int nb_lines = 0;
51
52   while(!sfile1.eof() && !sfile2.eof()){
53     std::getline(sfile1, line1);
54     std::getline(sfile2, line2);
55     nb_lines++;
56     // Skipping 4th line contain date of creation
57     if (nb_lines==4){
58       std::cout << "Skipping line" << std::endl;
59       continue;
60     }
61
62     // if lines are different end of read
63     if(line1 != line2){
64       return false;
65     }
66
67   }
68   // True if we reached the end of both files
69   return sfile1.eof() && sfile2.eof();
70 }
71
72 /**
73  * @brief Import the content of a shape file (STEP) into a TopDS_Shape object
74  *
75  * @param shape_file the shape file
76  * @param aShape the object
77  *
78  * @return error code
79  */
80 int importShape(const std::string shape_file, TopoDS_Shape& aShape){
81
82   std::cout << "Importing shape from " << shape_file << std::endl;
83   STEPControl_Reader reader;
84   // Forcing Unit in meter
85   Interface_Static::SetCVal("xstep.cascade.unit","M");
86   Interface_Static::SetIVal("read.step.ideas", 1);
87   Interface_Static::SetIVal("read.step.nonmanifold", 1);
88   IFSelect_ReturnStatus aStat = reader.ReadFile(shape_file.c_str());
89   if(aStat != IFSelect_RetDone)
90     std::cout << "Reading error for "  << shape_file << std::endl;
91
92   int NbTrans = reader.TransferRoots();
93   // There should be only one shape within the file
94   assert(NbTrans==1);
95   aShape = reader.OneShape();
96
97   return true;
98 }
99
100 /**
101  * @brief Export the content of a TopoDS_Shape into a shape file (STEP)
102  *
103  * @param shape_file the shape file
104  * @param aShape the object
105  *
106  * @return error code
107  */
108 int exportShape(const std::string shape_file, const TopoDS_Shape& aShape){
109
110   std::cout << "Exporting shape to " << shape_file << std::endl;
111
112   STEPControl_Writer aWriter;
113   // Forcing Unit in meter
114   Interface_Static::SetCVal("xstep.cascade.unit","M");
115   Interface_Static::SetCVal("write.step.unit","M");
116   Interface_Static::SetIVal("write.step.nonmanifold", 1);
117
118   IFSelect_ReturnStatus aStat = aWriter.Transfer(aShape,STEPControl_AsIs);
119   if(aStat != IFSelect_RetDone)
120     std::cout << "Transfer error for "  << shape_file << std::endl;
121
122   aStat = aWriter.Write(shape_file.c_str());
123
124   if(aStat != IFSelect_RetDone)
125     std::cout << "Writing error for "  << shape_file << std::endl;
126
127   return aStat;
128 }