]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_ImportSinusXOp.cxx
Salome HOME
sinusX p.1
[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   Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( doc()->CreateObject( KIND_BATHYMETRY ) );
119   HYDROData_Bathymetry::AltitudePoints aAPoints;
120
121   int aNSect = myCurveBlocks.size();
122   for ( int i = 0 ; i < aNSect ; i++ )
123   {
124     bool aSectClosure = true;
125     HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE; 
126     aPolylineXY->AddSection( TCollection_AsciiString(myCurveBlocks[i].myName.toStdString().c_str()), aSectType, myCurveBlocks[i].myIsConnected );
127   
128     for ( int k = 0 ; k < myCurveBlocks[i].myXYZPoints.size() ; k+=3 )
129     {
130       HYDROData_PolylineXY::Point aSectPoint;
131       aSectPoint.SetX( myCurveBlocks[i].myXYZPoints[k].X() );
132       aSectPoint.SetY( myCurveBlocks[i].myXYZPoints[k].Y() );
133       aPolylineXY->AddPoint( i, aSectPoint );
134   
135       aAPoints.Append(myCurveBlocks[i].myXYZPoints[k]);
136     }
137   }
138   QString aFileName = myFileDlg->selectedFile();
139   QFileInfo aFileInfo(aFileName);
140   QString aBaseFileName = aFileInfo.baseName();
141   QString aBathName = aBaseFileName + "_bath_1";
142   QString aPolyXYName = aBaseFileName + "_polyXY_1";
143   QString aPoly3DName = aBaseFileName + "_poly3D_1";
144   
145   int anInd = 2;
146   for (;HYDROGUI_Tool::FindObjectByName( module(), aBathName, KIND_BATHYMETRY) || 
147         HYDROGUI_Tool::FindObjectByName( module(), aPolyXYName, KIND_POLYLINEXY) ||
148         HYDROGUI_Tool::FindObjectByName( module(), aPoly3DName, KIND_POLYLINE);)
149   {
150     aBathName = aBaseFileName + "_bath_" + QString::number(anInd);
151     aPolyXYName = aBaseFileName + "_polyXY_" + QString::number(anInd);
152     aPoly3DName = aBaseFileName + "_poly3D_" + QString::number(anInd);
153     anInd++;
154   }
155   
156   aPolylineXY->SetName( aPolyXYName );
157   aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
158   aPolylineXY->Update();
159   
160   aBath->SetAltitudePoints(aAPoints);
161   aBath->SetName( aBathName );
162   
163   aPolylineObj->SetPolylineXY (aPolylineXY, false);
164   aPolylineObj->SetAltitudeObject(aBath);
165   
166   aPolylineObj->SetBorderColor( HYDROData_Polyline3D::DefaultBorderColor() );
167   aPolylineObj->SetName( aPoly3DName );
168   
169   aPolylineObj->Update();
170   
171   size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
172   if ( anActiveViewId == 0 )
173     anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
174   
175   module()->setObjectVisible( anActiveViewId, aPolylineXY, true );
176   module()->setObjectVisible( anActiveViewId, aPolylineObj, true );
177   
178   module()->setIsToUpdate( aPolylineObj );
179 }
180
181
182 bool HYDROGUI_ImportSinusXOp::Parse( QFile& theFile)
183 {
184   if ( !theFile.isOpen() )
185     return false;
186
187   QString aLine;
188   QString aBLine;
189   QStringList aList;
190   QStringList aBList;
191   myCurveBlocks.clear();
192   bool aTotStat = true;
193
194   aLine = theFile.readLine().simplified();
195   aList = aLine.split( ' ', QString::SkipEmptyParts );
196
197   for (;!theFile.atEnd();) 
198   {
199     if (aList[0] == "B" && (aList[1] == "C" || aList[1] == "P" || aList[1] == "N"))
200     {  
201       HYDROGUI_CurveBlock aCurveBlockInfo;
202       if (aList[1] == "C")
203         aCurveBlockInfo.myType = 1;
204       else if (aList[1] == "P")
205         aCurveBlockInfo.myType = 2;
206       else if (aList[1] == "N")
207         aCurveBlockInfo.myType = 2;
208
209       if (aList.size() == 9)
210       {
211         for (int j = 2; j < 8; j++)
212           aCurveBlockInfo.myRefCoords.push_back(aList[j].toDouble());
213         aCurveBlockInfo.myRefRatio = aList[8].toDouble();
214       }
215
216       QString Name;
217       do
218       {
219         aBLine = theFile.readLine().simplified();
220         aBList = aBLine.split( ' ', QString::SkipEmptyParts );
221          
222         if (aBList[0] == "CP")
223         {
224           if (aBList.size() == 2 && (aBList[1] == "0" || aBList[1] == "1" || aBList[1] == "2"))
225             aCurveBlockInfo.myCurvePlane = aBList[1].toInt();
226           else if (aBList.size() == 3 && (aBList[1] == "0" || aBList[1] == "1") && (aBList[2] == "0" || aBList[2] == "1"))
227           {
228             aCurveBlockInfo.myIsClosed = aBList[1].toInt();
229             aCurveBlockInfo.myIsClosed = aBList[2].toInt();
230           }
231           else
232           {
233             for (int j = 1; j < aBList.size(); j++)
234               aCurveBlockInfo.myAdditionalCurveInfo.push_back(aBList[j].toDouble());
235           }
236         }
237         if (aBList[0] == "CN")
238         {
239            for (int i = 1; i < aBList.size(); i++)
240              Name += aBList[i] + "_"; 
241            Name.remove(Name.size() - 1, 1);
242            aCurveBlockInfo.myName = Name;
243         }
244       } while (!theFile.atEnd() && aBLine[0] == 'C' );
245
246       bool aStat;
247       aTotStat = true;
248       do
249       {
250         if (aBList.size() >= 3 && aBLine[0] != 'B' && aBLine[0] != 'C') {
251           gp_XYZ anXYZ;
252           anXYZ.SetX (aBList[0].toDouble(&aStat));  
253           aTotStat = aTotStat && aStat;
254           anXYZ.SetY (aBList[1].toDouble(&aStat));
255           aTotStat = aTotStat && aStat;
256           anXYZ.SetZ (aBList[2].toDouble(&aStat));
257           aTotStat = aTotStat && aStat;
258
259           aCurveBlockInfo.myXYZPoints.push_back(anXYZ);
260           
261           aBLine = theFile.readLine().simplified();
262           aBList = aBLine.split( ' ', QString::SkipEmptyParts );
263         }
264         else 
265           break;
266     
267       } while (!theFile.atEnd()/* && aBLine[0] == 'B'*/ );
268       if (aTotStat)
269         myCurveBlocks.push_back(aCurveBlockInfo);
270
271     }
272     else
273     {
274       aLine = theFile.readLine().simplified();
275       aList = aLine.split( ' ', QString::SkipEmptyParts );
276     }
277
278   }
279
280   return true;
281
282 }
283