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