Salome HOME
Merge branch 'BR_IMPROVEMENTS' of ssh://git.salome-platform.org/modules/hydro into...
[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
34 #include <HYDROData_Profile.h>
35
36 #include <SUIT_Desktop.h>
37 #include <SUIT_FileDlg.h>
38 #include <LightApp_Application.h>
39
40 #include <QApplication>
41 #include <QFile>
42
43 HYDROGUI_ImportPolylineOp::HYDROGUI_ImportPolylineOp( HYDROGUI_Module* theModule )
44 : HYDROGUI_Operation( theModule )
45 {
46   setName( tr( "IMPORT_POLYLINE" ) );
47 }
48
49 HYDROGUI_ImportPolylineOp::~HYDROGUI_ImportPolylineOp()
50 {
51 }
52
53 void HYDROGUI_ImportPolylineOp::startOperation()
54 {
55   HYDROGUI_Operation::startOperation();
56
57   myFileDlg = new SUIT_FileDlg( module()->getApp()->desktop(), true );
58   myFileDlg->setWindowTitle( getName() );
59   myFileDlg->setFileMode( SUIT_FileDlg::ExistingFiles );
60   myFileDlg->setFilter( tr("POLYLINE_FILTER") );
61
62   connect( myFileDlg, SIGNAL( accepted() ), this, SLOT( onApply() ) );
63   connect( myFileDlg, SIGNAL( rejected() ), this, SLOT( onCancel() ) );
64
65   myFileDlg->exec();
66 }
67
68 void HYDROGUI_ImportPolylineOp::onApply()
69 {
70   if ( !myFileDlg )
71   {
72     abort();
73     return;
74   }
75
76   QString aFileName = myFileDlg->selectedFile();
77   if ( aFileName.isEmpty() )
78   {
79     abort();
80     return;
81   }
82
83   QFile aFile (aFileName);
84   aFile.open(QIODevice::ReadOnly);
85
86   Parse(aFile);
87
88   QApplication::setOverrideCursor( Qt::WaitCursor );
89
90   startDocOperation();
91
92   Process();
93
94   commitDocOperation();
95   commit();
96
97   module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
98
99   aFile.close();
100
101   QApplication::restoreOverrideCursor();
102 }
103
104 void HYDROGUI_ImportPolylineOp::Process()
105 {
106
107   Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
108
109   Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( doc()->CreateObject( KIND_POLYLINE ) );
110
111   Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( doc()->CreateObject( KIND_BATHYMETRY ) );;
112   HYDROData_Bathymetry::AltitudePoints aAPoints;
113
114   int aNSect = myCurveBlocks.size();
115   for ( int i = 0 ; i < aNSect ; i++ )
116   {
117     bool aSectClosure = true;
118     HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE; 
119     aPolylineXY->AddSection( TCollection_AsciiString(myCurveBlocks[i].myName.toStdString().c_str()), aSectType, myCurveBlocks[i].myIsClosed );
120
121     for ( int k = 0 ; k < myCurveBlocks[i].myXYZPoints.size() ; k+=3 )
122     {
123       HYDROData_PolylineXY::Point aSectPoint;
124       aSectPoint.SetX( myCurveBlocks[i].myXYZPoints[k].X() );
125       aSectPoint.SetY( myCurveBlocks[i].myXYZPoints[k].Y() );
126       aPolylineXY->AddPoint( i, aSectPoint );
127
128           aAPoints.Append(myCurveBlocks[i].myXYZPoints[k]);
129     }
130   }
131   aPolylineXY->SetName("P_XY");
132   aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
133   aPolylineXY->Update();
134   
135   aBath->SetAltitudePoints(aAPoints);
136   aBath->SetName("P_B");
137
138   aPolylineObj->SetPolylineXY (aPolylineXY, false);
139   aPolylineObj->SetAltitudeObject(aBath);
140
141   aPolylineObj->SetBorderColor( HYDROData_Polyline3D::DefaultBorderColor() );
142   aPolylineObj->SetName("P_3D");
143   
144   aPolylineObj->Update();
145
146
147   size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
148   if ( anActiveViewId == 0 )
149     anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
150
151   module()->setObjectVisible( anActiveViewId, aPolylineXY, true );
152   module()->setObjectVisible( anActiveViewId, aPolylineObj, true );
153
154   module()->setIsToUpdate( aPolylineObj );
155 }
156
157 bool HYDROGUI_ImportPolylineOp::Parse( QFile& theFile)
158 {
159   if ( !theFile.isOpen() )
160     return false;
161
162   QString aLine;
163   QString aBLine;
164   QStringList aList;
165   QStringList aBList;
166   myCurveBlocks.clear();
167   bool aTotStat = true;
168
169   aLine = theFile.readLine().simplified();
170   aList = aLine.split( ' ', QString::SkipEmptyParts );
171
172   for (;!theFile.atEnd();) 
173   {
174     if (aList[0] == "B" && (aList[1] == "C" || aList[1] == "P" || aList[1] == "N"))
175     {  
176       HYDROGUI_CurveBlock aCurveBlockInfo;
177       if (aList[1] == "C")
178         aCurveBlockInfo.myType = 1;
179       else if (aList[1] == "P")
180         aCurveBlockInfo.myType = 2;
181       else if (aList[1] == "N")
182         aCurveBlockInfo.myType = 2;
183
184       QString Name;
185       do
186       {
187         aBLine = theFile.readLine().simplified();
188         aBList = aBLine.split( ' ', QString::SkipEmptyParts );
189          
190         if (aBList[0] == "CP")
191         {
192           if (aBList.size() == 2 && (aBList[1] == "0" || aBList[1] == "1" || aBList[1] == "2"))
193             aCurveBlockInfo.myCurvePlane = aBList[1].toInt();
194           if (aBList.size() == 3 && (aBList[1] == "0" || aBList[1] == "1") && (aBList[2] == "0" || aBList[2] == "1"))
195           {
196             aCurveBlockInfo.myIsClosed = aBList[1].toInt();
197             aCurveBlockInfo.myIsClosed = aBList[2].toInt();
198           }
199         }
200         if (aBList[0] == "CN")
201         {
202            for (int i = 1; i < aBList.size(); i++)
203              Name += aBList[i] + "_"; 
204            Name.remove(Name.size() - 1, 1);
205            aCurveBlockInfo.myName = Name;
206         }
207       } while (!theFile.atEnd() && aBLine[0] == 'C' );
208
209       bool aStat;
210       aTotStat = true;
211       do
212       {
213         if (aBList.size() >= 3 && aBLine[0] != 'B' && aBLine[0] != 'C') {
214           gp_XYZ anXYZ;
215           anXYZ.SetX (aBList[0].toDouble(&aStat));  
216           aTotStat = aTotStat && aStat;
217           anXYZ.SetY (aBList[1].toDouble(&aStat));
218           aTotStat = aTotStat && aStat;
219           anXYZ.SetZ (aBList[2].toDouble(&aStat));
220           aTotStat = aTotStat && aStat;
221
222           aCurveBlockInfo.myXYZPoints.push_back(anXYZ);
223           
224           aBLine = theFile.readLine().simplified();
225           aBList = aBLine.split( ' ', QString::SkipEmptyParts );
226         }
227         else 
228           break;
229     
230       } while (!theFile.atEnd()/* && aBLine[0] == 'B'*/ );
231       if (aTotStat)
232         myCurveBlocks.push_back(aCurveBlockInfo);
233
234     }
235     else
236     {
237       aLine = theFile.readLine().simplified();
238       aList = aLine.split( ' ', QString::SkipEmptyParts );
239     }
240
241   }
242
243   return true;
244
245 }