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