Salome HOME
refs #727, #736: fix for the first bug and for the second part (Bug2) of the last...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_SplitPolylinesOp.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_SplitPolylinesOp.h>
20 #include <HYDROGUI_SplitPolylinesDlg.h>
21 #include <HYDROGUI_Module.h>
22 #include <HYDROGUI_UpdateFlags.h>
23 #include <HYDROGUI_Shape.h>
24 #include <HYDROData_PolylineOperator.h>
25 #include <HYDROData_Document.h>
26 #include <LightApp_Application.h>
27 #include <OCCViewer_ViewModel.h>
28 #include <OCCViewer_ViewManager.h>
29 #include <SUIT_Desktop.h>
30 #include <SUIT_MessageBox.h>
31 #include <gp_Pnt2d.hxx>
32 #include <BRepLib_MakeVertex.hxx>
33 #include <BRep_Builder.hxx>
34 #include <TopoDS_Vertex.hxx>
35 #include <TopoDS_Compound.hxx>
36 #include <AIS_Shape.hxx>
37
38 HYDROGUI_SplitPolylinesOp::HYDROGUI_SplitPolylinesOp( HYDROGUI_Module* theModule )
39 : HYDROGUI_Operation( theModule ),
40   myPreviewPrs( 0 )
41 {
42   setName( tr( "SPLIT_POLYLINES" ) );
43 }
44
45 HYDROGUI_SplitPolylinesOp::~HYDROGUI_SplitPolylinesOp()
46 {
47 }
48
49 void HYDROGUI_SplitPolylinesOp::startOperation()
50 {
51   HYDROGUI_Operation::startOperation();
52
53   HYDROGUI_SplitPolylinesDlg* aPanel = 
54     ::qobject_cast<HYDROGUI_SplitPolylinesDlg*>( inputPanel() );
55   if ( !aPanel )
56     return;
57
58   aPanel->setPolylinesFromSelection();
59   LightApp_Application* anApp = module()->getApp();
60   OCCViewer_ViewManager* aViewManager =
61     dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
62   aPanel->setOCCViewer( aViewManager ? aViewManager->getOCCViewer() : 0 );
63   setPreviewManager( aViewManager );
64
65   OnUpdatePreview();
66 }
67
68 HYDROGUI_InputPanel* HYDROGUI_SplitPolylinesOp::createInputPanel() const
69 {
70   HYDROGUI_SplitPolylinesDlg* aDlg = new HYDROGUI_SplitPolylinesDlg( module(), getName() );
71   connect( aDlg, SIGNAL( pointMoved() ), this, SLOT( OnUpdatePreview() ) );
72   connect( aDlg, SIGNAL( modeChanged() ), this, SLOT( OnUpdatePreview() ) );
73   connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( OnUpdatePreview() ) );
74   return aDlg;
75 }
76
77 bool HYDROGUI_SplitPolylinesOp::processApply( int& theUpdateFlags,
78                                               QString& theErrorMsg,
79                                               QStringList& theBrowseObjectsEntries )
80 {
81   HYDROGUI_SplitPolylinesDlg* aPanel = ::qobject_cast<HYDROGUI_SplitPolylinesDlg*>( inputPanel() );
82   if ( !aPanel )
83     return false;
84
85   //TODO: QString aName = aPanel->GetResultName();
86   Handle( HYDROData_PolylineXY ) aMainPolyline = aPanel->GetMainPolyline();
87   Handle( HYDROData_PolylineXY ) aToolPolyline = aPanel->GetToolPolyline();
88   HYDROData_SequenceOfObjects aPolylinesList = aPanel->GetPolylines();
89   gp_Pnt2d aPoint = aPanel->GetPoint();
90   double aTolerance = 1E-2; //TODO
91
92   HYDROData_PolylineOperator anOp;
93   switch( aPanel->GetMode() )
94   {
95   case HYDROGUI_SplitPolylinesDlg::ByPoint:
96     anOp.Split( doc(), aMainPolyline, aPoint, aTolerance );
97     break;
98   case HYDROGUI_SplitPolylinesDlg::ByTool:
99   {
100     bool isIntersected = false;
101     anOp.Split( doc(), aMainPolyline, aToolPolyline, aTolerance, isIntersected);
102
103     if (!isIntersected)
104     {
105       const QString aTitle = tr("SPLIT_POLYLINE_BY_TOOL_WARNING_TITLE");
106       const QString aMsg = tr("SPLIT_POLYLINE_BY_TOOL_WARNING_MSG");
107       SUIT_MessageBox::warning(module()->getApp()->desktop(), aTitle, aMsg);
108     }
109     break;
110   }
111   case HYDROGUI_SplitPolylinesDlg::Split:
112     anOp.Split( doc(), aPolylinesList, aTolerance );
113     break;
114   }
115   
116   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
117   return true;
118 }
119
120 void HYDROGUI_SplitPolylinesOp::abortOperation()
121 {
122   erasePreview();
123   HYDROGUI_Operation::abortOperation();
124 }
125
126 void HYDROGUI_SplitPolylinesOp::commitOperation()
127 {
128   erasePreview();
129   HYDROGUI_Operation::commitOperation();
130 }
131
132 void HYDROGUI_SplitPolylinesOp::erasePreview()
133 {
134   if( myPreviewPrs )
135   {
136     delete myPreviewPrs;
137     myPreviewPrs = 0;
138   }
139 }
140
141 void HYDROGUI_SplitPolylinesOp::OnUpdatePreview()
142 {
143   OCCViewer_ViewManager* aViewManager = getPreviewManager();
144   if ( aViewManager )
145   {
146     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
147     {
148       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
149       if ( !aCtx.IsNull() )
150       {
151         if( !myPreviewPrs )
152         {
153           myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
154           myPreviewPrs->setSelectionMode( AIS_Shape::SelectionMode( TopAbs_VERTEX ) );
155         }
156         aCtx->ClearSelected();
157       }
158     }
159   }
160
161   HYDROGUI_SplitPolylinesDlg* aPanel = ::qobject_cast<HYDROGUI_SplitPolylinesDlg*>( inputPanel() );
162   if( myPreviewPrs && aPanel )
163   {
164     BRep_Builder aBB;
165     TopoDS_Compound aCmp;
166     aBB.MakeCompound( aCmp );
167
168     switch( aPanel->GetMode() )
169     {
170     case HYDROGUI_SplitPolylinesDlg::ByPoint:
171       {
172         gp_Pnt2d aPnt = aPanel->GetPoint();
173         TopoDS_Vertex aVertex = BRepLib_MakeVertex( gp_Pnt( aPnt.X(), aPnt.Y(), 0.0 ) );
174         if( !aPanel->GetMainPolyline().IsNull() )
175           aBB.Add( aCmp, aPanel->GetMainPolyline()->GetShape() );
176         aBB.Add( aCmp, aVertex );
177         break;
178       }
179     case HYDROGUI_SplitPolylinesDlg::ByTool:
180       {
181         if( !aPanel->GetMainPolyline().IsNull() )
182           aBB.Add( aCmp, aPanel->GetMainPolyline()->GetShape() );
183         if( !aPanel->GetToolPolyline().IsNull() )
184           aBB.Add( aCmp, aPanel->GetToolPolyline()->GetShape() );
185         break;
186       }
187     case HYDROGUI_SplitPolylinesDlg::Split:
188       {
189         HYDROData_SequenceOfObjects aPolylines = aPanel->GetPolylines();
190         for( int i=aPolylines.Lower(), n=aPolylines.Upper(); i<=n; i++ )
191         {
192           Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( aPolylines.Value( i ) );
193           if( !aPolyline.IsNull() )
194             aBB.Add( aCmp, aPolyline->GetShape() );
195         }
196         break;
197       }
198     }
199
200
201     myPreviewPrs->setShape( aCmp );
202   }
203 }