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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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 <OCCViewer_ViewPort3d.h>
30 #include <OCCViewer_ViewWindow.h>
31 #include <SUIT_Desktop.h>
32 #include <SUIT_MessageBox.h>
33 #include <CurveCreator_Utils.hxx>
34 #include <gp_Pnt2d.hxx>
35 #include <BRepLib_MakeVertex.hxx>
36 #include <BRep_Builder.hxx>
37 #include <BRepBuilderAPI_MakeVertex.hxx>
38 #include <TopoDS_Vertex.hxx>
39 #include <TopoDS_Compound.hxx>
40 #include <AIS_Shape.hxx>
42 #include <QMouseEvent>
44 HYDROGUI_SplitPolylinesOp::HYDROGUI_SplitPolylinesOp( HYDROGUI_Module* theModule )
45 : HYDROGUI_Operation( theModule ),
47 mySplitPointPreview( 0 )
49 setName( tr( "SPLIT_POLYLINES" ) );
52 HYDROGUI_SplitPolylinesOp::~HYDROGUI_SplitPolylinesOp()
57 void HYDROGUI_SplitPolylinesOp::startOperation()
59 HYDROGUI_Operation::startOperation();
61 HYDROGUI_SplitPolylinesDlg* aPanel =
62 ::qobject_cast<HYDROGUI_SplitPolylinesDlg*>( inputPanel() );
66 aPanel->setPolylinesFromSelection();
67 LightApp_Application* anApp = module()->getApp();
68 OCCViewer_ViewManager* aViewManager =
69 dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
70 aPanel->setOCCViewer( aViewManager ? aViewManager->getOCCViewer() : 0 );
71 setPreviewManager( aViewManager );
76 HYDROGUI_InputPanel* HYDROGUI_SplitPolylinesOp::createInputPanel() const
78 HYDROGUI_SplitPolylinesDlg* aDlg = new HYDROGUI_SplitPolylinesDlg( module(), getName() );
79 connect( aDlg, SIGNAL( pointMoved() ), this, SLOT( OnUpdatePreview() ) );
80 connect( aDlg, SIGNAL( modeChanged() ), this, SLOT( OnUpdatePreview() ) );
81 connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( OnUpdatePreview() ) );
85 bool HYDROGUI_SplitPolylinesOp::processApply( int& theUpdateFlags,
87 QStringList& theBrowseObjectsEntries )
89 HYDROGUI_SplitPolylinesDlg* aPanel = ::qobject_cast<HYDROGUI_SplitPolylinesDlg*>( inputPanel() );
93 //TODO: QString aName = aPanel->GetResultName();
94 Handle( HYDROData_PolylineXY ) aMainPolyline = aPanel->GetMainPolyline();
95 Handle( HYDROData_PolylineXY ) aToolPolyline = aPanel->GetToolPolyline();
96 HYDROData_SequenceOfObjects aPolylinesList = aPanel->GetPolylines();
97 gp_Pnt2d aPoint = aPanel->GetPoint();
98 double aTolerance = 1E-2; //TODO
100 HYDROData_PolylineOperator anOp;
101 switch( aPanel->GetMode() )
103 case HYDROGUI_SplitPolylinesDlg::ByPoint:
104 anOp.Split( doc(), aMainPolyline, aPoint, aTolerance );
106 case HYDROGUI_SplitPolylinesDlg::ByTool:
108 bool isIntersected = false;
109 anOp.Split( doc(), aMainPolyline, aToolPolyline, aTolerance, isIntersected);
113 const QString aTitle = tr("SPLIT_POLYLINE_BY_TOOL_WARNING_TITLE");
114 const QString aMsg = tr("SPLIT_POLYLINE_BY_TOOL_WARNING_MSG");
115 SUIT_MessageBox::warning(module()->getApp()->desktop(), aTitle, aMsg);
119 case HYDROGUI_SplitPolylinesDlg::Split:
120 anOp.Split( doc(), aPolylinesList, aTolerance );
124 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
128 void HYDROGUI_SplitPolylinesOp::abortOperation()
131 HYDROGUI_Operation::abortOperation();
134 void HYDROGUI_SplitPolylinesOp::commitOperation()
137 HYDROGUI_Operation::commitOperation();
140 void HYDROGUI_SplitPolylinesOp::erasePreview()
148 HYDROGUI_SplitPolylinesDlg* aPanel =
149 ::qobject_cast<HYDROGUI_SplitPolylinesDlg*>( inputPanel() );
153 OCCViewer_ViewManager* aViewManager = getPreviewManager();
156 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
158 if ( mySplitPointPreview )
160 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
161 if ( !aCtx.IsNull() )
163 if ( aCtx->HasOpenedContext() )
164 aCtx->CloseLocalContext();
165 aCtx->Erase( mySplitPointPreview, Standard_False );
167 mySplitPointPreview = 0;
170 if ( aPanel->GetMode() == HYDROGUI_SplitPolylinesDlg::ByPoint )
172 disconnect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
173 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
174 connect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
175 aViewer, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
181 void HYDROGUI_SplitPolylinesOp::OnUpdatePreview()
183 HYDROGUI_SplitPolylinesDlg* aPanel = ::qobject_cast<HYDROGUI_SplitPolylinesDlg*>( inputPanel() );
185 OCCViewer_ViewManager* aViewManager = getPreviewManager();
188 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
190 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
191 if ( !aCtx.IsNull() )
195 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
196 myPreviewPrs->setSelectionMode( AIS_Shape::SelectionMode( TopAbs_FACE ) );
199 if ( mySplitPointPreview )
201 if ( aCtx->HasOpenedContext() )
202 aCtx->CloseLocalContext();
203 if ( aPanel->GetMode() == HYDROGUI_SplitPolylinesDlg::ByPoint )
205 if ( !aCtx->IsDisplayed( mySplitPointPreview ) )
206 aCtx->Display( mySplitPointPreview, Standard_False );
209 aCtx->Erase( mySplitPointPreview, Standard_False );
212 aCtx->ClearSelected(true);
215 if ( aPanel->GetMode() == HYDROGUI_SplitPolylinesDlg::ByPoint )
217 disconnect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
218 aViewer, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
219 connect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
220 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
224 disconnect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
225 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
226 connect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
227 aViewer, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
232 if( myPreviewPrs && aPanel )
235 TopoDS_Compound aCmp;
236 aBB.MakeCompound( aCmp );
238 switch( aPanel->GetMode() )
240 case HYDROGUI_SplitPolylinesDlg::ByPoint:
242 gp_Pnt2d aPnt = aPanel->GetPoint();
243 TopoDS_Vertex aVertex = BRepLib_MakeVertex( gp_Pnt( aPnt.X(), aPnt.Y(), 0.0 ) );
244 if( !aPanel->GetMainPolyline().IsNull() )
245 aBB.Add( aCmp, aPanel->GetMainPolyline()->GetShape() );
246 aBB.Add( aCmp, aVertex );
249 case HYDROGUI_SplitPolylinesDlg::ByTool:
251 if( !aPanel->GetMainPolyline().IsNull() )
252 aBB.Add( aCmp, aPanel->GetMainPolyline()->GetShape() );
253 if( !aPanel->GetToolPolyline().IsNull() )
254 aBB.Add( aCmp, aPanel->GetToolPolyline()->GetShape() );
257 case HYDROGUI_SplitPolylinesDlg::Split:
259 HYDROData_SequenceOfObjects aPolylines = aPanel->GetPolylines();
260 for( int i=aPolylines.Lower(), n=aPolylines.Upper(); i<=n; i++ )
262 Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( aPolylines.Value( i ) );
263 if( !aPolyline.IsNull() )
264 aBB.Add( aCmp, aPolyline->GetShape() );
271 myPreviewPrs->setShape( aCmp );
275 void HYDROGUI_SplitPolylinesOp::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
277 OCCViewer_ViewManager* aViewManager = getPreviewManager();
281 OCCViewer_ViewWindow* aViewWindow = (OCCViewer_ViewWindow*)aViewManager->getActiveView();
285 OCCViewer_ViewPort3d* aViewPort = aViewWindow->getViewPort();
289 Handle(V3d_View) aView = aViewPort->getView();
290 if ( aView.IsNull() )
293 OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer();
297 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
301 gp_Pnt aPnt = CurveCreator_Utils::ConvertClickToPoint( theEvent->x(), theEvent->y(), aView );
302 BRep_Builder aBuilder;
303 TopoDS_Compound aShape;
304 aBuilder.MakeCompound(aShape);
305 aBuilder.Add(aShape, BRepBuilderAPI_MakeVertex( aPnt ) );
307 if ( mySplitPointPreview )
309 if ( aCtx->HasOpenedContext() )
310 aCtx->CloseLocalContext();
311 aCtx->Erase( mySplitPointPreview, Standard_False );
314 mySplitPointPreview = new AIS_Shape( aShape );
315 if ( aCtx->HasOpenedContext() )
316 aCtx->CloseLocalContext();
317 aCtx->Display( mySplitPointPreview, Standard_False );
319 aCtx->UpdateCurrentViewer();