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