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