]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_ImportSinusXOp.cxx
Salome HOME
sinusX p.2
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportSinusXOp.cxx
1 // Copyright (C) 2007-2015  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 "HYDROGUI_ImportSinusXOp.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_UpdateFlags.h"
28 #include "HYDROGUI_Tool.h"
29 #include <HYDROData_PolylineXY.h>
30 #include <HYDROData_Polyline3D.h>
31 #include <HYDROGUI_DataObject.h>
32 #include <HYDROData_Bathymetry.h>
33 #include <HYDROData_Iterator.h>
34
35 #include <HYDROData_Profile.h>
36
37 #include <SUIT_Desktop.h>
38 #include <SUIT_FileDlg.h>
39 #include <LightApp_Application.h>
40
41 #include <QApplication>
42 #include <QFile>
43 #include <QFileInfo>
44 #include <QMessageBox>
45
46
47 HYDROGUI_ImportSinusXOp::HYDROGUI_ImportSinusXOp( HYDROGUI_Module* theModule )
48 : HYDROGUI_Operation( theModule )
49 {
50   setName( tr( "IMPORT_SINUSX" ) );
51 }
52
53 HYDROGUI_ImportSinusXOp::~HYDROGUI_ImportSinusXOp()
54 {
55 }
56
57 void HYDROGUI_ImportSinusXOp::startOperation()
58 {
59   HYDROGUI_Operation::startOperation();
60
61   myFileDlg = new SUIT_FileDlg( module()->getApp()->desktop(), true );
62   myFileDlg->setWindowTitle( getName() );
63   myFileDlg->setFileMode( SUIT_FileDlg::ExistingFiles );
64   myFileDlg->setFilter( tr("SINUSX_FILTER") );
65
66   connect( myFileDlg, SIGNAL( accepted() ), this, SLOT( onApply() ) );
67   connect( myFileDlg, SIGNAL( rejected() ), this, SLOT( onCancel() ) );
68
69   myFileDlg->exec();
70 }
71
72 void HYDROGUI_ImportSinusXOp::onApply()
73 {
74   if ( !myFileDlg )
75   {
76     abort();
77     return;
78   }
79
80   QString aFileName = myFileDlg->selectedFile();
81   if ( aFileName.isEmpty() )
82   {
83     abort();
84     return;
85   }
86
87   QString anExt = aFileName.split('.', QString::SkipEmptyParts).back();
88
89   if (anExt == "sx")
90   {
91     QFile aFile (aFileName);
92     aFile.open(QIODevice::ReadOnly);
93
94     Parse(aFile);
95
96     QApplication::setOverrideCursor( Qt::WaitCursor );
97
98     startDocOperation();
99
100     ProcessSX();
101
102     commitDocOperation();
103     commit();
104     aFile.close();
105   }
106
107   module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
108   
109   QApplication::restoreOverrideCursor();
110 }
111
112 void HYDROGUI_ImportSinusXOp::ProcessSX()
113 {
114   /*Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
115
116   Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( doc()->CreateObject( KIND_POLYLINE ) );
117   */
118   //TODO - move to hydro data
119   std::vector<Handle(HYDROData_Entity)> anEntities;
120   for ( int i = 0; i < myCurveBlocks.size(); i++ )
121   {
122     if (myCurveBlocks[i].myType == 4) ///  scatter plot -> to bathy
123     {
124       Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( doc()->CreateObject( KIND_BATHYMETRY ) );
125       HYDROData_Bathymetry::AltitudePoints aAPoints;
126       for (int j = 0; j < myCurveBlocks[i].myXYZPoints.size(); j++)
127         aAPoints.Append(gp_XYZ (myCurveBlocks[i].myXYZPoints[j]));
128
129       aBath->SetAltitudePoints(aAPoints);
130       aBath->SetName(myCurveBlocks[i].myName);
131       anEntities.push_back(aBath);
132     }
133     if (myCurveBlocks[i].myType == 1 || myCurveBlocks[i].myType == 3)
134     {
135       NCollection_Sequence<gp_XYZ> aPoints;
136       for (int j = 0; j < myCurveBlocks[i].myXYZPoints.size(); j++)
137         aPoints.Append(gp_XYZ (myCurveBlocks[i].myXYZPoints[j]));
138       if (myCurveBlocks[i].myIsClosed)
139         aPoints.Append(gp_XYZ (myCurveBlocks[i].myXYZPoints[0]));
140       Handle(HYDROData_ProfileUZ) aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( doc()->CreateObject( KIND_PROFILEUZ ) );
141       Handle(HYDROData_PolylineXY) aPolyXY = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
142       aPolyXY->AddSection( "",  myCurveBlocks[i].myIsSpline ? HYDROData_PolylineXY::SECTION_SPLINE : HYDROData_PolylineXY::SECTION_POLYLINE, false ); 
143       aProfileUZ->CalculateAndAddPoints(aPoints, aPolyXY);
144       Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( doc()->CreateObject( KIND_PROFILE ) );
145       aProfile->SetParametricPoints(aProfileUZ->GetPoints());
146       aPolyXY->SetName(myCurveBlocks[i].myName + "_polyXY");
147       aProfileUZ->SetName(myCurveBlocks[i].myName + "_profileUZ");
148       aProfile->SetName(myCurveBlocks[i].myName + "_profile");
149       anEntities.push_back(aPolyXY);
150       anEntities.push_back(aProfileUZ);
151       anEntities.push_back(aProfile);
152     }
153     if (myCurveBlocks[i].myType == 2)
154     {
155       if (myCurveBlocks[i].myCurvePlane == 2)
156       {
157         if (myCurveBlocks[i].myAdditionalCurveInfo.size() == 4)
158         {
159           Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( doc()->CreateObject( KIND_PROFILE ) );
160           HYDROData_ProfileUZ::PointsList aPointList;
161           for (int j = 0; j < myCurveBlocks[i].myXYZPoints.size(); j++)
162             aPointList.Append(gp_XY (myCurveBlocks[i].myXYZPoints[j].X(), myCurveBlocks[i].myXYZPoints[j].Z()));
163            
164           aProfile->SetParametricPoints(aPointList);
165           if ( ! (myCurveBlocks[i].myAdditionalCurveInfo[0] == 0 &&  myCurveBlocks[i].myAdditionalCurveInfo[1] == 0 && 
166             myCurveBlocks[i].myAdditionalCurveInfo[2] == 0 && myCurveBlocks[i].myAdditionalCurveInfo[3] == 0) )
167           {
168             aProfile->SetLeftPoint(gp_XY(myCurveBlocks[i].myAdditionalCurveInfo[0], myCurveBlocks[i].myAdditionalCurveInfo[1]));
169             aProfile->SetRightPoint(gp_XY(myCurveBlocks[i].myAdditionalCurveInfo[2], myCurveBlocks[i].myAdditionalCurveInfo[3]));
170             aProfile->Update();
171           }
172           aProfile->SetName(myCurveBlocks[i].myName + "_profile");
173           anEntities.push_back(aProfile);
174         }
175       }
176       if (myCurveBlocks[i].myCurvePlane == 0)
177       {
178          Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( doc()->CreateObject( KIND_PROFILE ) );
179          HYDROData_Profile::ProfilePoints aPointList;
180          for (int j = 0; j < myCurveBlocks[i].myXYZPoints.size(); j++)
181            aPointList.Append(myCurveBlocks[i].myXYZPoints[j]);
182          aProfile->SetProfilePoints(aPointList);
183          aProfile->SetName(myCurveBlocks[i].myName + "_profile");
184          anEntities.push_back(aProfile);
185       }
186     }
187   }
188
189   /////
190
191   size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
192   if ( anActiveViewId == 0 )
193     anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
194
195   for (int i = 0; i < anEntities.size() ; i++)
196   {
197     anEntities[i]->Update();
198     module()->setObjectVisible( anActiveViewId, anEntities[i], true );
199     module()->setIsToUpdate( anEntities[i] );
200   }
201 }
202  
203
204
205
206 bool HYDROGUI_ImportSinusXOp::Parse( QFile& theFile)
207 {
208   if ( !theFile.isOpen() )
209     return false;
210
211   QString aLine;
212   QString aBLine;
213   QStringList aList;
214   QStringList aBList;
215   myCurveBlocks.clear();
216   bool aTotStat = true;
217
218   aLine = theFile.readLine().simplified();
219   aList = aLine.split( ' ', QString::SkipEmptyParts );
220
221   for (;!theFile.atEnd();) 
222   {
223     if (aList[0] == "B" && (aList[1] == "C" || aList[1] == "P" || aList[1] == "N" || aList[1] == "S" ))
224     {  
225       HYDROGUI_CurveBlock aCurveBlockInfo;
226       if (aList[1] == "C")
227         aCurveBlockInfo.myType = 1;
228       else if (aList[1] == "P")
229         aCurveBlockInfo.myType = 2;
230       else if (aList[1] == "N")
231         aCurveBlockInfo.myType = 3;
232       else if (aList[1] == "S")
233         aCurveBlockInfo.myType = 4;
234
235       if (aList.size() == 9)
236       {
237         for (int j = 2; j < 8; j++)
238           aCurveBlockInfo.myRefCoords.push_back(aList[j].toDouble());
239         aCurveBlockInfo.myRefRatio = aList[8].toDouble();
240       }
241
242       QString Name;
243       do
244       {
245         aBLine = theFile.readLine().simplified();
246         aBList = aBLine.split( ' ', QString::SkipEmptyParts );
247          
248         if (aBList[0] == "CP")
249         {
250           if (aBList.size() == 2 && (aBList[1] == "0" || aBList[1] == "1" || aBList[1] == "2"))
251             aCurveBlockInfo.myCurvePlane = aBList[1].toInt();
252           else if (aBList.size() == 3 && (aBList[1] == "0" || aBList[1] == "1") && (aBList[2] == "0" || aBList[2] == "1"))
253           {
254             aCurveBlockInfo.myIsClosed = aBList[1].toInt();
255             aCurveBlockInfo.myIsSpline = aBList[2].toInt();
256           }
257           else
258           {
259             for (int j = 1; j < aBList.size(); j++)
260               aCurveBlockInfo.myAdditionalCurveInfo.push_back(aBList[j].toDouble());
261           }
262         }
263         if (aBList[0] == "CN")
264         {
265            for (int i = 1; i < aBList.size(); i++)
266              Name += aBList[i] + "_"; 
267            Name.remove(Name.size() - 1, 1);
268            aCurveBlockInfo.myName = Name;
269         }
270       } while (!theFile.atEnd() && aBLine[0] == 'C' );
271
272       bool aStat;
273       aTotStat = true;
274       do
275       {
276         if (aBList.size() >= 3 && aBLine[0] != 'B' && aBLine[0] != 'C') {
277           gp_XYZ anXYZ;
278           anXYZ.SetX (aBList[0].toDouble(&aStat));  
279           aTotStat = aTotStat && aStat;
280           anXYZ.SetY (aBList[1].toDouble(&aStat));
281           aTotStat = aTotStat && aStat;
282           anXYZ.SetZ (aBList[2].toDouble(&aStat));
283           aTotStat = aTotStat && aStat;
284
285           aCurveBlockInfo.myXYZPoints.push_back(anXYZ);
286           
287           aBLine = theFile.readLine().simplified();
288           aBList = aBLine.split( ' ', QString::SkipEmptyParts );
289         }
290         else 
291           break;
292     
293       } while (!theFile.atEnd() || !aBLine.isEmpty());
294       if (aTotStat)
295         myCurveBlocks.push_back(aCurveBlockInfo);
296
297       aLine = aBLine;
298       aList = aBList;
299
300     }
301     else
302     {
303       aLine = theFile.readLine().simplified();
304       aList = aLine.split( ' ', QString::SkipEmptyParts );
305     }
306
307   }
308
309   return true;
310
311 }
312