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>
33 #include <HYDROData_Iterator.h>
35 #include <HYDROData_Profile.h>
37 #include <SUIT_Desktop.h>
38 #include <SUIT_FileDlg.h>
39 #include <LightApp_Application.h>
41 #include <QApplication>
44 #include <QMessageBox>
47 HYDROGUI_ImportPolylineOp::HYDROGUI_ImportPolylineOp( HYDROGUI_Module* theModule )
48 : HYDROGUI_Operation( theModule )
50 setName( tr( "IMPORT_POLYLINE" ) );
53 HYDROGUI_ImportPolylineOp::~HYDROGUI_ImportPolylineOp()
57 void HYDROGUI_ImportPolylineOp::startOperation()
59 HYDROGUI_Operation::startOperation();
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") );
66 connect( myFileDlg, SIGNAL( accepted() ), this, SLOT( onApply() ) );
67 connect( myFileDlg, SIGNAL( rejected() ), this, SLOT( onCancel() ) );
72 void HYDROGUI_ImportPolylineOp::onApply()
80 QString aFileName = myFileDlg->selectedFile();
81 if ( aFileName.isEmpty() )
87 QString anExt = aFileName.split('.', QString::SkipEmptyParts).back();
92 aHSHP = SHPOpen( aFileName.toAscii().data(), "rb" );
95 QApplication::setOverrideCursor( Qt::WaitCursor );
99 HYDROData_Iterator anIter( doc() );
101 QStringList anExistingNames;
102 std::vector<int> anAllowedIndexes;
103 for( ; anIter.More(); anIter.Next() )
104 anExistingNames.push_back(anIter.Current()->GetName());
106 QFileInfo aFileInfo(aFileName);
107 QString aBaseFileName = aFileInfo.baseName();
109 if (aHSHP->nShapeType == 3 || aHSHP->nShapeType == 23)
112 for (;anAllowedIndexes.size() < mySHPObjects.size();)
114 if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)))
116 anAllowedIndexes.push_back(anInd);
123 for (size_t i = 0; i < mySHPObjects.size(); i++ )
125 ProcessSHPPolyXY(mySHPObjects[i], aBaseFileName, anAllowedIndexes[i]);
128 else if (aHSHP->nShapeType == 13)
131 for (;anAllowedIndexes.size() < mySHPObjects.size();)
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)))
137 anAllowedIndexes.push_back(anInd);
143 for (size_t i = 0; i < mySHPObjects.size(); i++ )
144 ProcessSHPPoly3D(mySHPObjects[i], aBaseFileName, anAllowedIndexes[i]);
146 commitDocOperation();
149 for (size_t i = 0; i < mySHPObjects.size(); i++ )
150 free (mySHPObjects[i]);
152 mySHPObjects.clear();
155 module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
157 QApplication::restoreOverrideCursor();
160 void HYDROGUI_ImportPolylineOp::ProcessSHPPolyXY(SHPObject* anObj, QString theFileName, int theInd)
162 //if (anObj->nSHPType != SHPT_ARC && anObj->nSHPType != SHPT_ARCM)
164 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
166 int nParts = anObj->nParts;
167 for ( int i = 0 ; i < nParts ; i++ )
169 int StartIndex = anObj->panPartStart[i];
172 EndIndex = anObj->panPartStart[i + 1];
174 EndIndex = anObj->nVertices;
176 bool IsClosed = false;
177 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
178 if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
179 anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] )
182 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true);
185 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false);
189 for ( int k = StartIndex; k < EndIndex ; k++ )
191 HYDROData_PolylineXY::Point aSectPoint;
192 aSectPoint.SetX( anObj->padfX[k] );
193 aSectPoint.SetY( anObj->padfY[k] );
194 aPolylineXY->AddPoint( i, aSectPoint );
199 aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
200 aPolylineXY->SetName( theFileName + "_PolyXY_" + QString::number(theInd) );
202 aPolylineXY->Update();
204 size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
205 if ( anActiveViewId == 0 )
206 anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
208 module()->setObjectVisible( anActiveViewId, aPolylineXY, true );
210 module()->setIsToUpdate( aPolylineXY );
213 void HYDROGUI_ImportPolylineOp::ProcessSHPPoly3D(SHPObject* anObj, QString theFileName, int theInd)
215 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
217 Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( doc()->CreateObject( KIND_POLYLINE ) );
219 Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( doc()->CreateObject( KIND_BATHYMETRY ) );
220 HYDROData_Bathymetry::AltitudePoints aAPoints;
222 int nParts = anObj->nParts;
223 for ( int i = 0 ; i < nParts ; i++ )
225 //bool aSectClosure = true;
226 int StartIndex = anObj->panPartStart[i];
229 EndIndex = anObj->panPartStart[i + 1];
231 EndIndex = anObj->nVertices;
233 bool IsClosed = false;
234 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
235 if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
236 anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] &&
237 anObj->padfZ[StartIndex] == anObj->padfZ[EndIndex - 1])
240 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true );
243 aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false );
247 for ( int k = StartIndex ; k < EndIndex ; k++ )
249 HYDROData_PolylineXY::Point aSectPoint;
250 aSectPoint.SetX( anObj->padfX[k] );
251 aSectPoint.SetY( anObj->padfY[k] );
252 aPolylineXY->AddPoint( i, aSectPoint );
253 aAPoints.Append(gp_XYZ (anObj->padfX[k], anObj->padfY[k], anObj->padfZ[k]));
258 QString aBathName = theFileName + "_bath_" + QString::number(theInd);
259 QString aPolyXYName = theFileName + "_polyXY_" + QString::number(theInd);
260 QString aPoly3DName = theFileName + "_poly3D_" + QString::number(theInd);
262 aPolylineXY->SetName( aPolyXYName );
263 aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
264 aPolylineXY->Update();
266 aBath->SetAltitudePoints(aAPoints);
267 aBath->SetName( aBathName );
269 aPolylineObj->SetPolylineXY (aPolylineXY, false);
270 aPolylineObj->SetAltitudeObject(aBath);
272 aPolylineObj->SetBorderColor( HYDROData_Polyline3D::DefaultBorderColor() );
273 aPolylineObj->SetName( aPoly3DName );
275 aPolylineObj->Update();
277 size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
278 if ( anActiveViewId == 0 )
279 anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
281 module()->setObjectVisible( anActiveViewId, aPolylineXY, true );
282 module()->setObjectVisible( anActiveViewId, aPolylineObj, true );
284 module()->setIsToUpdate( aPolylineObj );
287 void HYDROGUI_ImportPolylineOp::Parse(SHPHandle theHandle)
290 mySHPObjects.clear();
291 SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
292 if (aShapeType == 3 || aShapeType == 13 || aShapeType == 23)
294 for (int i = 0; i < theHandle->nRecords; i++)
295 mySHPObjects.push_back(SHPReadObject(theHandle, i));