Salome HOME
Copyright update 2022
[modules/smesh.git] / src / DriverUNV / UNV2411_Structure.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 #include <fstream>
24 #include <cstdio>
25
26 #include "UNV2411_Structure.hxx"
27 #include "UNV_Utilities.hxx"
28
29 using namespace std;
30 using namespace UNV;
31 using namespace UNV2411;
32
33 static string _label_dataset = "2411";
34
35 UNV2411::TRecord::TRecord():
36   exp_coord_sys_num(1),
37   disp_coord_sys_num(1),
38   color(11)//(0) -  0019936: EDF 794 SMESH : Export UNV : Node color and group id
39 {
40   coord[1] = coord[2] = 0.0; // prepare to e.g. 2D mesh
41 }
42
43 void UNV2411::Read(std::ifstream& in_stream, TDataSet& theDataSet)
44 {
45   if(!in_stream.good())
46     EXCEPTION(runtime_error,"ERROR: Input file not good.");
47
48   /*
49    * adjust the \p istream to our
50    * position
51    */
52   if(!beginning_of_dataset(in_stream,_label_dataset))
53     EXCEPTION(runtime_error,"ERROR: Could not find "<<_label_dataset<<" dataset!");
54
55   /**
56    * always 3 coordinates in the UNV file, no matter
57    * which dimensionality libMesh is in
58    */
59   int dim = 3;
60   std::string num_buf;
61
62   // Issue 22638. Find out space dimension to read a 2D mesh from a file
63   // generated by SIMAIL from Simulog
64   if ( !in_stream.eof() )
65   {
66     int where  = in_stream.tellg();
67
68     TRecord aRec;
69     in_stream >> aRec.label ;
70     if ( aRec.label == -1 ) return; // dataset end 
71
72     dim = 0;
73     num_buf = read_line( in_stream );
74     for ( size_t i = 0; i < num_buf.size(); )
75     {
76       // skip spaces
77       while ( i < num_buf.size() && num_buf[i] == ' ' )
78         ++i;
79
80       dim += ( i < num_buf.size() );
81
82       // skip non-spaces
83       while ( i < num_buf.size() && num_buf[i] != ' ' )
84         ++i;
85     }
86     if ( dim == 0 )
87       return;
88
89     in_stream.seekg( where, in_stream.beg );
90   }
91
92   // read the rest records
93   while ( !in_stream.eof() )
94   {
95     TRecord aRec;
96     in_stream >> aRec.label ;
97     if ( aRec.label == -1 ) {
98       // end of dataset is reached
99       break;
100     }
101
102     in_stream>>aRec.exp_coord_sys_num;
103     in_stream>>aRec.disp_coord_sys_num;
104     in_stream>>aRec.color;
105
106     /*
107      * take care of the
108      * floating-point data
109      */
110     for(int d = 0; d < dim; d++){
111       in_stream>>num_buf;
112       aRec.coord[d] = D_to_e(num_buf);
113     }
114
115     theDataSet.push_back(aRec);
116   }
117 }
118
119
120 void UNV2411::Write(std::ofstream& out_stream, const TDataSet& theDataSet)
121 {
122   if(!out_stream.good())
123     EXCEPTION(runtime_error,"ERROR: Output file not good.");
124   
125   /*
126    * Write beginning of dataset
127    */
128   out_stream<<"    -1\n";
129   out_stream<<"  "<<_label_dataset<<"\n";
130
131   TDataSet::const_iterator anIter = theDataSet.begin();
132   for(; anIter != theDataSet.end(); anIter++)
133   {
134     const TRecord& aRec = *anIter;
135     char buf[78];
136     sprintf(buf, "%10d%10d%10d%10d\n", 
137             aRec.label,
138             aRec.exp_coord_sys_num,
139             aRec.disp_coord_sys_num,
140             aRec.color);
141     out_stream<<buf;
142
143     // the coordinates
144     sprintf(buf, "%25.16E%25.16E%25.16E\n", 
145             aRec.coord[0],
146             aRec.coord[1],
147             aRec.coord[2]);
148     out_stream<<buf;
149   }
150   
151   
152   /*
153    * Write end of dataset
154    */
155   out_stream<<"    -1\n";
156 }