1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_ImportPolylineOp.h"
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>
34 #include <HYDROData_Profile.h>
36 #include <SUIT_Desktop.h>
37 #include <SUIT_FileDlg.h>
38 #include <LightApp_Application.h>
40 #include <QApplication>
44 HYDROGUI_ImportPolylineOp::HYDROGUI_ImportPolylineOp( HYDROGUI_Module* theModule )
45 : HYDROGUI_Operation( theModule )
47 setName( tr( "IMPORT_POLYLINE" ) );
50 HYDROGUI_ImportPolylineOp::~HYDROGUI_ImportPolylineOp()
54 void HYDROGUI_ImportPolylineOp::startOperation()
56 HYDROGUI_Operation::startOperation();
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") );
63 connect( myFileDlg, SIGNAL( accepted() ), this, SLOT( onApply() ) );
64 connect( myFileDlg, SIGNAL( rejected() ), this, SLOT( onCancel() ) );
69 void HYDROGUI_ImportPolylineOp::onApply()
77 QString aFileName = myFileDlg->selectedFile();
78 if ( aFileName.isEmpty() )
84 QFile aFile (aFileName);
85 aFile.open(QIODevice::ReadOnly);
89 QApplication::setOverrideCursor( Qt::WaitCursor );
98 module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
102 QApplication::restoreOverrideCursor();
105 void HYDROGUI_ImportPolylineOp::Process()
108 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
110 Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( doc()->CreateObject( KIND_POLYLINE ) );
112 Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( doc()->CreateObject( KIND_BATHYMETRY ) );
113 HYDROData_Bathymetry::AltitudePoints aAPoints;
115 int aNSect = myCurveBlocks.size();
116 for ( int i = 0 ; i < aNSect ; i++ )
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 );
122 for ( int k = 0 ; k < myCurveBlocks[i].myXYZPoints.size() ; k+=3 )
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 );
129 aAPoints.Append(myCurveBlocks[i].myXYZPoints[k]);
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";
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);)
144 aBathName = aBaseFileName + "_bath_" + QString::number(anInd);
145 aPolyXYName = aBaseFileName + "_polyXY_" + QString::number(anInd);
146 aPoly3DName = aBaseFileName + "_poly3D_" + QString::number(anInd);
150 aPolylineXY->SetName( aPolyXYName );
151 aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
152 aPolylineXY->Update();
154 aBath->SetAltitudePoints(aAPoints);
155 aBath->SetName( aBathName );
157 aPolylineObj->SetPolylineXY (aPolylineXY, false);
158 aPolylineObj->SetAltitudeObject(aBath);
160 aPolylineObj->SetBorderColor( HYDROData_Polyline3D::DefaultBorderColor() );
161 aPolylineObj->SetName( aPoly3DName );
163 aPolylineObj->Update();
166 size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
167 if ( anActiveViewId == 0 )
168 anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
170 module()->setObjectVisible( anActiveViewId, aPolylineXY, true );
171 module()->setObjectVisible( anActiveViewId, aPolylineObj, true );
173 module()->setIsToUpdate( aPolylineObj );
176 bool HYDROGUI_ImportPolylineOp::Parse( QFile& theFile)
178 if ( !theFile.isOpen() )
185 myCurveBlocks.clear();
186 bool aTotStat = true;
188 aLine = theFile.readLine().simplified();
189 aList = aLine.split( ' ', QString::SkipEmptyParts );
191 for (;!theFile.atEnd();)
193 if (aList[0] == "B" && (aList[1] == "C" || aList[1] == "P" || aList[1] == "N"))
195 HYDROGUI_CurveBlock aCurveBlockInfo;
197 aCurveBlockInfo.myType = 1;
198 else if (aList[1] == "P")
199 aCurveBlockInfo.myType = 2;
200 else if (aList[1] == "N")
201 aCurveBlockInfo.myType = 2;
203 if (aList.size() == 9)
205 for (int j = 2; j < 8; j++)
206 aCurveBlockInfo.myRefCoords.push_back(aList[j].toDouble());
207 aCurveBlockInfo.myRefRatio = aList[8].toDouble();
213 aBLine = theFile.readLine().simplified();
214 aBList = aBLine.split( ' ', QString::SkipEmptyParts );
216 if (aBList[0] == "CP")
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"))
222 aCurveBlockInfo.myIsClosed = aBList[1].toInt();
223 aCurveBlockInfo.myIsClosed = aBList[2].toInt();
227 for (int j = 1; j < aBList.size(); j++)
228 aCurveBlockInfo.myAdditionalCurveInfo.push_back(aBList[j].toDouble());
231 if (aBList[0] == "CN")
233 for (int i = 1; i < aBList.size(); i++)
234 Name += aBList[i] + "_";
235 Name.remove(Name.size() - 1, 1);
236 aCurveBlockInfo.myName = Name;
238 } while (!theFile.atEnd() && aBLine[0] == 'C' );
244 if (aBList.size() >= 3 && aBLine[0] != 'B' && aBLine[0] != 'C') {
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;
253 aCurveBlockInfo.myXYZPoints.push_back(anXYZ);
255 aBLine = theFile.readLine().simplified();
256 aBList = aBLine.split( ' ', QString::SkipEmptyParts );
261 } while (!theFile.atEnd()/* && aBLine[0] == 'B'*/ );
263 myCurveBlocks.push_back(aCurveBlockInfo);
268 aLine = theFile.readLine().simplified();
269 aList = aLine.split( ' ', QString::SkipEmptyParts );