Salome HOME
write dbf with Hydro shapefiles, attribute string "PartName" with polyline name
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_PolylineOp.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_PolylineOp.h"
20
21 #include "HYDROGUI_Module.h"
22 #include "HYDROGUI_DataObject.h"
23 #include "HYDROGUI_PolylineDlg.h"
24 #include "HYDROGUI_Tool.h"
25 #include "HYDROGUI_Tool2.h"
26 #include "HYDROGUI_UpdateFlags.h"
27
28 #include <HYDROData_Document.h>
29 #include <HYDROData_PolylineOperator.h>
30 #include <HYDROData_TopoCurve.h>
31
32 #include <CurveCreator_Curve.hxx>
33 #include <CurveCreator_Displayer.hxx>
34 #include <CurveCreator_Utils.hxx>
35
36 #include <LightApp_Application.h>
37 #include <LightApp_SelectionMgr.h>
38 #include <LightApp_UpdateFlags.h>
39
40 #include <OCCViewer_ViewManager.h>
41 #include <OCCViewer_ViewModel.h>
42 #include <OCCViewer_ViewWindow.h>
43
44 #include <OCCViewer_AISSelector.h>
45
46 #include <Precision.hxx>
47
48 #include <SUIT_MessageBox.h>
49 #include <SUIT_Desktop.h>
50
51 #include <TopoDS_Shape.hxx>
52 #include <TopoDS_Wire.hxx>
53
54 //#define _DEVDEBUG_
55 #include "HYDRO_trace.hxx"
56
57 //static int ZValueIncrement = 0;
58 static const double HYDROGUI_MAXIMAL_DEFLECTION = 1e-2;
59
60 HYDROGUI_PolylineOp::HYDROGUI_PolylineOp( HYDROGUI_Module* theModule, bool theIsEdit )
61 : HYDROGUI_Operation( theModule ), 
62   myIsEdit( theIsEdit ),
63   myCurve( NULL )
64 {
65   setName( theIsEdit ? tr( "EDIT_POLYLINE" ) : tr( "CREATE_POLYLINE" ) );
66 }
67
68 HYDROGUI_PolylineOp::~HYDROGUI_PolylineOp()
69 {
70   erasePreview();
71 }
72
73 /**
74  * Redirect the delete action to input panel
75  */
76 void HYDROGUI_PolylineOp::deleteSelected()
77 {
78   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
79   aPanel->deleteSelected();
80 }
81
82 /**
83  * Checks whether there are some to delete
84  */
85 bool HYDROGUI_PolylineOp::deleteEnabled()
86 {
87   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
88   return aPanel->deleteEnabled();
89 }
90
91 /**
92  * Set Z layer for the operation preview.
93  \param theLayer a layer position
94  */
95 void HYDROGUI_PolylineOp::updatePreviewZLayer( int theLayer )
96 {
97   HYDROGUI_Operation::updatePreviewZLayer( theLayer );
98
99   int aZLayer = getPreviewZLayer();
100   if ( aZLayer >= 0 )
101   {
102     if( getPreviewManager() )
103     {
104       if ( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
105       {
106         Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
107         if( !aCtx.IsNull() )
108         {
109           Handle(AIS_InteractiveObject) anObject = myCurve->getAISObject( true );
110           aCtx->SetZLayer( anObject, aZLayer );
111         }
112       }
113     }
114   }
115 }
116
117 void HYDROGUI_PolylineOp::startOperation()
118 {
119   DEBTRACE("startOperation");
120   if( myIsEdit )
121   {
122     if ( isApplyAndClose() )
123           myEditedObject = Handle(HYDROData_PolylineXY)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
124     if ( !myEditedObject.IsNull() && !myEditedObject->IsEditable() )
125     {
126       // Polyline is imported from GEOM module an is not recognized as
127       // polyline or spline and exist only as shape presentation
128       SUIT_MessageBox::critical( module()->getApp()->desktop(),
129                                  tr( "POLYLINE_IS_UNEDITABLE_TLT" ),
130                                  tr( "POLYLINE_IS_UNEDITABLE_MSG" ) );
131       abort();
132       return;
133     }
134   }
135
136   if( myCurve )
137     delete myCurve;
138
139   myCurve = new CurveCreator_Curve( CurveCreator::Dim2d );
140
141   HYDROGUI_Operation::startOperation();
142
143   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
144   aPanel->reset();
145
146   LightApp_Application* anApp = module()->getApp();
147   OCCViewer_ViewManager* aViewManager =
148     dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
149   aPanel->setOCCViewer( aViewManager ? aViewManager->getOCCViewer() : 0 );
150   setPreviewManager( aViewManager );
151
152   if ( isApplyAndClose() )
153     setCursor();
154
155   QString aPolylineName;
156   if( !myEditedObject.IsNull() )
157   {
158     NCollection_Sequence<TCollection_AsciiString>           aSectNames;
159     NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
160     NCollection_Sequence<bool>                              aSectClosures;
161     bool aIsInCustom = myEditedObject->GetIsInCustomFlag();
162     myEditedObject->SetIsInCustomFlag( true );
163     myEditedObject->GetSections( aSectNames, aSectTypes, aSectClosures );
164     myEditedObject->SetIsInCustomFlag( aIsInCustom );
165
166     if (!aSectNames.IsEmpty())
167     {
168       for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
169       {
170         QString aSectName = HYDROGUI_Tool::ToQString( aSectNames.Value( i ) );
171         HYDROData_PolylineXY::SectionType aSectType = aSectTypes.Value( i );
172         bool aSectClosure = aSectClosures.Value( i );
173
174         CurveCreator::SectionType aCurveType = CurveCreator::Polyline;
175         if( aSectType == HYDROData_PolylineXY::SECTION_SPLINE )
176           aCurveType = CurveCreator::Spline;
177
178         CurveCreator::Coordinates aCurveCoords;
179
180         HYDROData_PolylineXY::PointsList aSectPointsList =
181           myEditedObject->GetPoints( i - 1 );
182         for (int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k)
183         {
184           const HYDROData_PolylineXY::Point& aSectPoint =
185             aSectPointsList.Value( k );
186           aCurveCoords.push_back( aSectPoint.X() );
187           aCurveCoords.push_back( aSectPoint.Y() );
188         }
189
190         Quantity_Color aColor = CurveCreator_Utils::getRandColor();      
191         QColor aQColor = CurveCreator_Utils::colorConv(aColor);
192         myEditedObject->GetSectionColor(i-1, aQColor);
193
194         myCurve->addSectionInternal( aSectName.toStdString(),
195           aCurveType, aSectClosure, aCurveCoords, CurveCreator_Utils::colorConv(aQColor) );
196       }
197     }
198     else
199     {
200       std::deque<CurveCreator::Coordinates> aPs;
201       std::deque<bool> isCloseds;
202       std::vector<TopoDS_Wire> aWires;
203       HYDROData_PolylineOperator::GetWires(myEditedObject, aWires);
204       const int aSCount = aWires.size();
205       bool isError = false;
206       for (int aSI = 0; aSI < aSCount; ++aSI)
207       {
208         HYDROData_TopoCurve aCurve, aCurve2;
209         std::list<gp_XYZ> aPs2;
210         int aMaxPieceCount;
211         if (!aCurve.Initialize(aWires[aSI]) ||
212           (aMaxPieceCount = aCurve.BSplinePiecewiseCurve(
213             HYDROGUI_MAXIMAL_DEFLECTION * 0.1, aCurve2)) == 0)
214         {
215           isError = true;
216           break;
217         }
218
219         double aDefl;
220         aMaxPieceCount *= 100;
221         int aPieceCount = 0;
222         while (aPieceCount < aMaxPieceCount &&
223           (aDefl = HYDROData_PolylineOperator::ReduceDeflection(
224               HYDROGUI_MAXIMAL_DEFLECTION, aCurve2, aPieceCount)) >
225             HYDROGUI_MAXIMAL_DEFLECTION);
226         if (aDefl < 0 || !aCurve2.ValuesInKnots(aPs2))
227         {
228           isError = true;
229           break;
230         }
231
232         aPs.push_back(CurveCreator::Coordinates());
233         CurveCreator::Coordinates& aPs3 = aPs.back();
234         std::list<gp_XYZ>::const_iterator aLastPIt = aPs2.end();
235         std::list<gp_XYZ>::const_iterator aPIt = aPs2.begin();
236         for (; aPIt != aLastPIt; ++aPIt)
237         {
238           const gp_XYZ aP = *aPIt;
239           aPs3.push_back(aP.X());
240           aPs3.push_back(aP.Y());
241         }
242         isCloseds.push_back(aCurve.IsClosed());
243       }
244
245       if (!isError)
246       {
247         const TCollection_AsciiString aNamePrefix = "Section_";
248         for (int aSI = 0; aSI < aSCount; ++aSI)
249         {
250           myCurve->addSectionInternal((aNamePrefix + (aSI + 1)).ToCString(),
251             CurveCreator::Spline, isCloseds[aSI], aPs[aSI], CurveCreator_Utils::getRandColor());
252         }
253       }
254     }
255
256     aPolylineName = myEditedObject->GetName();
257   }
258   else
259   {
260     aPolylineName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_POLYLINE_NAME" ) );
261   }
262
263   aPanel->setPolylineName( aPolylineName );
264   aPanel->setCurve( myCurve );
265
266   displayPreview();
267 }
268
269 void HYDROGUI_PolylineOp::abortOperation()
270 {
271   restoreCursor();
272
273   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
274   if ( aPanel )
275     aPanel->setOCCViewer( 0 );
276   erasePreview();
277
278   HYDROGUI_Operation::abortOperation();
279 }
280
281 void HYDROGUI_PolylineOp::commitOperation()
282 {
283   DEBTRACE("commitOperation");
284   if ( isApplyAndClose() )
285   {
286     restoreCursor();
287
288     HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
289     if ( aPanel )
290       aPanel->setOCCViewer( 0 );
291   }
292
293   erasePreview();
294
295   HYDROGUI_Operation::commitOperation();
296 }
297
298 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
299 {
300   HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
301   connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
302   return aDlg;
303 }
304
305 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
306                                         QString& theErrorMsg,
307                                         QStringList& theBrowseObjectsEntries )
308 {
309   DEBTRACE("processApply");
310   HYDROGUI_PolylineDlg* aPanel = ::qobject_cast<HYDROGUI_PolylineDlg*>( inputPanel() );
311   if ( !aPanel )
312     return false;
313
314   QString aPolylineName = aPanel->getPolylineName().simplified();
315   if ( aPolylineName.isEmpty() )
316   {
317     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
318     return false;
319   }
320
321   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aPolylineName ) )
322   {
323     // check that there are no other objects with the same name in the document
324     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aPolylineName );
325     if( !anObject.IsNull() )
326     {
327       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aPolylineName );
328       return false;
329     }
330   }
331
332   if ( myCurve->getNbSections() <= 0 )
333   {
334     theErrorMsg = tr( "EMPTY_POLYLINE_DATA" );
335     return false;
336   }
337
338   Handle(HYDROData_PolylineXY) aPolylineObj;
339   if( myIsEdit )
340   {
341     aPolylineObj = myEditedObject;
342     aPolylineObj->RemoveSections();
343   }
344   else
345   {
346     aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
347   }
348
349   if( aPolylineObj.IsNull() )
350     return false;
351
352   aPolylineObj->SetName( aPolylineName );
353
354   for ( int i = 0 ; i < myCurve->getNbSections() ; i++ )
355   {
356     TCollection_AsciiString aSectName = HYDROGUI_Tool::ToAsciiString( myCurve->getSectionName( i ).c_str() );
357     CurveCreator::SectionType aCurveType =  myCurve->getSectionType( i );
358     bool aSectClosure = myCurve->isClosed( i );
359
360     HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
361
362     if ( aCurveType == CurveCreator::Spline )
363       aSectType = HYDROData_PolylineXY::SECTION_SPLINE;
364
365     aPolylineObj->AddSection( aSectName, aSectType, aSectClosure );
366
367     Quantity_Color aColor = myCurve->getColorSection(i);
368     aPolylineObj->SetSectionColor(i, CurveCreator_Utils::colorConv(aColor));
369
370     // Add the points from section
371     CurveCreator::Coordinates aCurveCoords = myCurve->getCoords( i );
372
373     if ( aCurveCoords.size() <= 2 )
374     {
375       theErrorMsg = tr( "NUMBER_OF_SECTION_POINTS_INCORRECT" );
376       return false;
377     }
378
379     for ( int k = 0 ; k + 1 < aCurveCoords.size() ; k++ )
380     {
381       HYDROData_PolylineXY::Point aSectPoint;
382
383       aSectPoint.SetX( aCurveCoords.at( k ) );
384       k++;
385       aSectPoint.SetY( aCurveCoords.at( k ) );
386
387       aPolylineObj->AddPoint( i, aSectPoint );
388     }
389   }
390
391  //if ( !myIsEdit )
392  //{
393  //  aPolylineObj->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
394  //}
395
396   // Update the wire of polyline
397   aPolylineObj->Update();
398   module()->setIsToUpdate( aPolylineObj );
399
400   // the viewer should be release from the widget before the module update it
401   // because it has an opened local context and updated presentation should not be displayed in it
402   if ( aPanel )
403     aPanel->setOCCViewer( 0 );
404
405   theUpdateFlags = UF_Model;
406
407   // the polyline should be rebuild in all viewers, where it is displayed
408   theUpdateFlags |= UF_Viewer | UF_GV_Forced;
409   theUpdateFlags |= UF_OCCViewer | UF_OCC_Forced;
410   theUpdateFlags |= UF_VTKViewer;
411
412   size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
413   if ( anActiveViewId == 0 )
414   {
415     anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
416   }
417
418   if( !myIsEdit )
419   {
420     module()->setObjectVisible( anActiveViewId, aPolylineObj, true );
421     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aPolylineObj );
422     theBrowseObjectsEntries.append( anEntry );
423   }  
424
425   return true;
426 }
427
428 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
429 {
430   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
431   if( !aPanel )
432     return;
433   if( !myCurve )
434     return;
435   CurveCreator_Displayer* aDisplayer = myCurve->getDisplayer();
436   if( !aDisplayer )
437     return;
438   //QList<int> aSelSections = aPanel->getSelectedSections();
439   bool aIsHl = false;
440   //if( aSelSections.contains(i) ){
441   // TODO
442   //aDisplayer->highlight( myCurve->getAISObject(), aIsHl );
443   //}
444 }
445
446 void HYDROGUI_PolylineOp::displayPreview()
447 {
448   if( getPreviewManager() )
449   {
450     if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
451     {
452       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
453       if( !aCtx.IsNull() )
454       {
455         CurveCreator_Displayer* aDisplayer = new CurveCreator_Displayer( aCtx, getPreviewZLayer() );
456         myCurve->setDisplayer( aDisplayer );
457         aDisplayer->display( myCurve->getAISObject( true ), true );
458       }
459     }
460   }
461 }
462
463 void HYDROGUI_PolylineOp::erasePreview()
464 {
465   CurveCreator_Displayer* aDisplayer = myCurve ? myCurve->getDisplayer() : 0;
466   if( getPreviewManager() && aDisplayer )
467   {
468     if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
469     {
470       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
471       if( !aCtx.IsNull() )
472       {
473         aDisplayer->eraseAll( true );
474       }
475     }
476   }
477
478   setPreviewManager( NULL );
479   if ( myCurve )
480   {
481     delete myCurve;
482     myCurve = NULL;
483   }
484 }