Salome HOME
Merge remote-tracking branch 'origin/BR_LAND_COVER' into BR_v14_rc
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportPolylineOp.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_ImportPolylineOp.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_ImportPolylineOp::HYDROGUI_ImportPolylineOp( HYDROGUI_Module* theModule )
48 : HYDROGUI_Operation( theModule )
49 {
50   setName( tr( "IMPORT_POLYLINE" ) );
51 }
52
53 HYDROGUI_ImportPolylineOp::~HYDROGUI_ImportPolylineOp()
54 {
55 }
56
57 void HYDROGUI_ImportPolylineOp::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("POLYLINE_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_ImportPolylineOp::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::SplitBehavior::SkipEmptyParts).back();
88
89   if (anExt == "shp")
90   {
91     SHPHandle aHSHP;
92     aHSHP = SHPOpen( aFileName.toAscii().data(), "rb" );
93     Parse(aHSHP);
94     
95     QApplication::setOverrideCursor( Qt::WaitCursor );
96     
97     startDocOperation();
98
99     HYDROData_Iterator anIter( doc() );
100     int anInd = 0;
101     QStringList anExistingNames;
102     std::vector<int> anAllowedIndexes;
103     for( ; anIter.More(); anIter.Next() )
104       anExistingNames.push_back(anIter.Current()->GetName());
105
106     QFileInfo aFileInfo(aFileName);
107     QString aBaseFileName = aFileInfo.baseName();
108
109     if (aHSHP->nShapeType == 3 || aHSHP->nShapeType == 23)
110     {
111       anInd = 0;
112       for (;anAllowedIndexes.size() < mySHPObjects.size();)
113       {
114         if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)))
115         {
116           anAllowedIndexes.push_back(anInd);
117           anInd++;
118         }
119         else
120           anInd++;
121       }
122       
123       for (size_t i = 0; i < mySHPObjects.size(); i++ )
124       {
125         ProcessSHPPolyXY(mySHPObjects[i], aBaseFileName, anAllowedIndexes[i]);
126       }
127     }
128     else if (aHSHP->nShapeType == 13)
129     {
130       anInd = 0;
131       for (;anAllowedIndexes.size() < mySHPObjects.size();)
132       {
133         if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)) &&
134             !anExistingNames.contains(aBaseFileName + "_Poly3D_" + QString::number(anInd)) &&
135             !anExistingNames.contains(aBaseFileName + "_Bath_" + QString::number(anInd)))
136         {
137           anAllowedIndexes.push_back(anInd);
138           anInd++;
139         }
140         else
141           anInd++;
142       }
143       for (size_t i = 0; i < mySHPObjects.size(); i++ )
144         ProcessSHPPoly3D(mySHPObjects[i], aBaseFileName, anAllowedIndexes[i]);
145     }
146     commitDocOperation();
147     commit();
148     
149     for (size_t i = 0; i < mySHPObjects.size(); i++ )
150       free (mySHPObjects[i]);
151
152     mySHPObjects.clear();
153     SHPClose(aHSHP);
154   }
155   else if (anExt == "sx")
156   {
157     QFile aFile (aFileName);
158     aFile.open(QIODevice::ReadOnly);
159
160     Parse(aFile);
161
162     QApplication::setOverrideCursor( Qt::WaitCursor );
163
164     startDocOperation();
165
166     ProcessSX();
167
168     commitDocOperation();
169     commit();
170     aFile.close();
171   }
172
173   module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
174   
175   QApplication::restoreOverrideCursor();
176 }
177
178 void HYDROGUI_ImportPolylineOp::ProcessSX()
179 {
180   Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
181
182   Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( doc()->CreateObject( KIND_POLYLINE ) );
183
184   Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( doc()->CreateObject( KIND_BATHYMETRY ) );
185   HYDROData_Bathymetry::AltitudePoints aAPoints;
186
187   int aNSect = myCurveBlocks.size();
188   for ( int i = 0 ; i < aNSect ; i++ )
189   {
190     bool aSectClosure = true;
191     HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE; 
192     aPolylineXY->AddSection( TCollection_AsciiString(myCurveBlocks[i].myName.toStdString().c_str()), aSectType, myCurveBlocks[i].myIsConnected );
193   
194     for ( int k = 0 ; k < myCurveBlocks[i].myXYZPoints.size() ; k+=3 )
195     {
196       HYDROData_PolylineXY::Point aSectPoint;
197       aSectPoint.SetX( myCurveBlocks[i].myXYZPoints[k].X() );
198       aSectPoint.SetY( myCurveBlocks[i].myXYZPoints[k].Y() );
199       aPolylineXY->AddPoint( i, aSectPoint );
200   
201       aAPoints.Append(myCurveBlocks[i].myXYZPoints[k]);
202     }
203   }
204   QString aFileName = myFileDlg->selectedFile();
205   QFileInfo aFileInfo(aFileName);
206   QString aBaseFileName = aFileInfo.baseName();
207   QString aBathName = aBaseFileName + "_bath_1";
208   QString aPolyXYName = aBaseFileName + "_polyXY_1";
209   QString aPoly3DName = aBaseFileName + "_poly3D_1";
210   
211   int anInd = 2;
212   for (;HYDROGUI_Tool::FindObjectByName( module(), aBathName, KIND_BATHYMETRY) || 
213         HYDROGUI_Tool::FindObjectByName( module(), aPolyXYName, KIND_POLYLINEXY) ||
214         HYDROGUI_Tool::FindObjectByName( module(), aPoly3DName, KIND_POLYLINE);)
215   {
216     aBathName = aBaseFileName + "_bath_" + QString::number(anInd);
217     aPolyXYName = aBaseFileName + "_polyXY_" + QString::number(anInd);
218     aPoly3DName = aBaseFileName + "_poly3D_" + QString::number(anInd);
219     anInd++;
220   }
221   
222   aPolylineXY->SetName( aPolyXYName );
223   aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
224   aPolylineXY->Update();
225   
226   aBath->SetAltitudePoints(aAPoints);
227   aBath->SetName( aBathName );
228   
229   aPolylineObj->SetPolylineXY (aPolylineXY, false);
230   aPolylineObj->SetAltitudeObject(aBath);
231   
232   aPolylineObj->SetBorderColor( HYDROData_Polyline3D::DefaultBorderColor() );
233   aPolylineObj->SetName( aPoly3DName );
234   
235   aPolylineObj->Update();
236   
237   size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
238   if ( anActiveViewId == 0 )
239     anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
240   
241   module()->setObjectVisible( anActiveViewId, aPolylineXY, true );
242   module()->setObjectVisible( anActiveViewId, aPolylineObj, true );
243   
244   module()->setIsToUpdate( aPolylineObj );
245 }
246
247 void HYDROGUI_ImportPolylineOp::ProcessSHPPolyXY(SHPObject* anObj, QString theFileName, int theInd)
248 {
249   //if (anObj->nSHPType != SHPT_ARC && anObj->nSHPType != SHPT_ARCM)
250   // return false;
251   Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
252   
253   int nParts = anObj->nParts;
254   for ( int i = 0 ; i < nParts ; i++ )
255   {
256     int StartIndex = anObj->panPartStart[i];
257     int EndIndex;
258     if (i != nParts - 1)
259       EndIndex = anObj->panPartStart[i + 1];
260     else
261       EndIndex = anObj->nVertices;
262
263     bool IsClosed = false;
264     HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE; 
265     if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
266         anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] )
267     {
268       IsClosed = true;
269       aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true);
270     }
271     else
272       aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false);
273     
274     if (IsClosed)
275       EndIndex--;
276     for ( int k = StartIndex; k < EndIndex ; k++ )
277     {
278       HYDROData_PolylineXY::Point aSectPoint;
279       aSectPoint.SetX( anObj->padfX[k] );
280       aSectPoint.SetY( anObj->padfY[k] );
281       aPolylineXY->AddPoint( i, aSectPoint );
282     }
283
284   }
285   
286   aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
287   aPolylineXY->SetName( theFileName + "_PolyXY_" + QString::number(theInd) );
288   
289   aPolylineXY->Update();
290   
291   size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
292   if ( anActiveViewId == 0 )
293     anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
294   
295   module()->setObjectVisible( anActiveViewId, aPolylineXY, true );
296   
297   module()->setIsToUpdate( aPolylineXY );
298 }
299
300 void HYDROGUI_ImportPolylineOp::ProcessSHPPoly3D(SHPObject* anObj, QString theFileName, int theInd)
301 {
302   Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
303
304   Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( doc()->CreateObject( KIND_POLYLINE ) );
305
306   Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( doc()->CreateObject( KIND_BATHYMETRY ) );
307   HYDROData_Bathymetry::AltitudePoints aAPoints;
308
309   int nParts = anObj->nParts;
310   for ( int i = 0 ; i < nParts ; i++ )
311   {
312     //bool aSectClosure = true;
313     int StartIndex = anObj->panPartStart[i];
314     int EndIndex;
315     if (i != nParts - 1)
316       EndIndex = anObj->panPartStart[i + 1];
317     else
318       EndIndex = anObj->nVertices;
319
320     bool IsClosed = false;
321     HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE; 
322     if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
323         anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] &&
324         anObj->padfZ[StartIndex] == anObj->padfZ[EndIndex - 1])
325     {
326       IsClosed = true;
327       aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true );
328     }
329     else
330       aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false  );
331     
332     if (IsClosed)
333       EndIndex--;
334     for ( int k = StartIndex ; k < EndIndex ; k++ )
335     {
336       HYDROData_PolylineXY::Point aSectPoint;
337       aSectPoint.SetX( anObj->padfX[k] );
338       aSectPoint.SetY( anObj->padfY[k] );
339       aPolylineXY->AddPoint( i, aSectPoint );
340       aAPoints.Append(gp_XYZ (anObj->padfX[k], anObj->padfY[k], anObj->padfZ[k]));
341     }
342   }
343
344
345   QString aBathName = theFileName + "_bath_" + QString::number(theInd);
346   QString aPolyXYName = theFileName + "_polyXY_" + QString::number(theInd);
347   QString aPoly3DName = theFileName + "_poly3D_" + QString::number(theInd);
348
349   aPolylineXY->SetName( aPolyXYName );
350   aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
351   aPolylineXY->Update();
352   
353   aBath->SetAltitudePoints(aAPoints);
354   aBath->SetName( aBathName );
355
356   aPolylineObj->SetPolylineXY (aPolylineXY, false);
357   aPolylineObj->SetAltitudeObject(aBath);
358
359   aPolylineObj->SetBorderColor( HYDROData_Polyline3D::DefaultBorderColor() );
360   aPolylineObj->SetName( aPoly3DName );
361   
362   aPolylineObj->Update();
363
364   size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
365   if ( anActiveViewId == 0 )
366     anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
367
368   module()->setObjectVisible( anActiveViewId, aPolylineXY, true );
369   module()->setObjectVisible( anActiveViewId, aPolylineObj, true );
370
371   module()->setIsToUpdate( aPolylineObj );
372 }
373
374
375 bool HYDROGUI_ImportPolylineOp::Parse( QFile& theFile)
376 {
377   if ( !theFile.isOpen() )
378     return false;
379
380   QString aLine;
381   QString aBLine;
382   QStringList aList;
383   QStringList aBList;
384   myCurveBlocks.clear();
385   bool aTotStat = true;
386
387   aLine = theFile.readLine().simplified();
388   aList = aLine.split( ' ', QString::SkipEmptyParts );
389
390   for (;!theFile.atEnd();) 
391   {
392     if (aList[0] == "B" && (aList[1] == "C" || aList[1] == "P" || aList[1] == "N"))
393     {  
394       HYDROGUI_CurveBlock aCurveBlockInfo;
395       if (aList[1] == "C")
396         aCurveBlockInfo.myType = 1;
397       else if (aList[1] == "P")
398         aCurveBlockInfo.myType = 2;
399       else if (aList[1] == "N")
400         aCurveBlockInfo.myType = 2;
401
402       if (aList.size() == 9)
403       {
404         for (int j = 2; j < 8; j++)
405           aCurveBlockInfo.myRefCoords.push_back(aList[j].toDouble());
406         aCurveBlockInfo.myRefRatio = aList[8].toDouble();
407       }
408
409       QString Name;
410       do
411       {
412         aBLine = theFile.readLine().simplified();
413         aBList = aBLine.split( ' ', QString::SkipEmptyParts );
414          
415         if (aBList[0] == "CP")
416         {
417           if (aBList.size() == 2 && (aBList[1] == "0" || aBList[1] == "1" || aBList[1] == "2"))
418             aCurveBlockInfo.myCurvePlane = aBList[1].toInt();
419           else if (aBList.size() == 3 && (aBList[1] == "0" || aBList[1] == "1") && (aBList[2] == "0" || aBList[2] == "1"))
420           {
421             aCurveBlockInfo.myIsClosed = aBList[1].toInt();
422             aCurveBlockInfo.myIsClosed = aBList[2].toInt();
423           }
424           else
425           {
426             for (int j = 1; j < aBList.size(); j++)
427               aCurveBlockInfo.myAdditionalCurveInfo.push_back(aBList[j].toDouble());
428           }
429         }
430         if (aBList[0] == "CN")
431         {
432            for (int i = 1; i < aBList.size(); i++)
433              Name += aBList[i] + "_"; 
434            Name.remove(Name.size() - 1, 1);
435            aCurveBlockInfo.myName = Name;
436         }
437       } while (!theFile.atEnd() && aBLine[0] == 'C' );
438
439       bool aStat;
440       aTotStat = true;
441       do
442       {
443         if (aBList.size() >= 3 && aBLine[0] != 'B' && aBLine[0] != 'C') {
444           gp_XYZ anXYZ;
445           anXYZ.SetX (aBList[0].toDouble(&aStat));  
446           aTotStat = aTotStat && aStat;
447           anXYZ.SetY (aBList[1].toDouble(&aStat));
448           aTotStat = aTotStat && aStat;
449           anXYZ.SetZ (aBList[2].toDouble(&aStat));
450           aTotStat = aTotStat && aStat;
451
452           aCurveBlockInfo.myXYZPoints.push_back(anXYZ);
453           
454           aBLine = theFile.readLine().simplified();
455           aBList = aBLine.split( ' ', QString::SkipEmptyParts );
456         }
457         else 
458           break;
459     
460       } while (!theFile.atEnd()/* && aBLine[0] == 'B'*/ );
461       if (aTotStat)
462         myCurveBlocks.push_back(aCurveBlockInfo);
463
464     }
465     else
466     {
467       aLine = theFile.readLine().simplified();
468       aList = aLine.split( ' ', QString::SkipEmptyParts );
469     }
470
471   }
472
473   return true;
474
475 }
476
477 void HYDROGUI_ImportPolylineOp::Parse(SHPHandle theHandle)
478 {
479   int aShapeType;
480   mySHPObjects.clear();
481   SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
482   if (aShapeType == 3 || aShapeType == 13 || aShapeType == 23) 
483   {
484     for (int i = 0; i < theHandle->nRecords; i++) 
485       mySHPObjects.push_back(SHPReadObject(theHandle, i));
486   }
487 }