Salome HOME
Merge branch 'BR_v14_rc' of ssh://git.salome-platform.org/modules/hydro into BR_v14_rc
[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   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
88   HYDROData_PolylineOperator anOp;
89   switch( aPanel->GetMode() )
90   {
91   case HYDROGUI_SplitPolylinesDlg::ByPoint:
92     anOp.Split( doc(), aName.toLatin1().data(), aMainPolyline, aPoint );
93     break;
94   case HYDROGUI_SplitPolylinesDlg::ByTool:
95     anOp.Split( doc(), aName.toLatin1().data(), aMainPolyline, aToolPolyline );
96     break;
97   case HYDROGUI_SplitPolylinesDlg::Split:
98     anOp.Split( doc(), aName.toLatin1().data(), aPolylinesList );
99     break;
100   }
101   
102   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
103   return true;
104 }
105
106 void HYDROGUI_SplitPolylinesOp::abortOperation()
107 {
108   erasePreview();
109   HYDROGUI_Operation::abortOperation();
110 }
111
112 void HYDROGUI_SplitPolylinesOp::commitOperation()
113 {
114   erasePreview();
115   HYDROGUI_Operation::commitOperation();
116 }
117
118 void HYDROGUI_SplitPolylinesOp::erasePreview()
119 {
120   if( myPreviewPrs )
121   {
122     delete myPreviewPrs;
123     myPreviewPrs = 0;
124   }
125 }
126
127 void HYDROGUI_SplitPolylinesOp::OnUpdatePreview()
128 {
129   OCCViewer_ViewManager* aViewManager = getPreviewManager();
130   if ( aViewManager )
131   {
132     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
133     {
134       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
135       if ( !aCtx.IsNull() )
136       {
137         if( !myPreviewPrs )
138           myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
139         aCtx->ClearSelected();
140       }
141     }
142   }
143
144   HYDROGUI_SplitPolylinesDlg* aPanel = ::qobject_cast<HYDROGUI_SplitPolylinesDlg*>( inputPanel() );
145   if( myPreviewPrs && aPanel )
146   {
147     BRep_Builder aBB;
148     TopoDS_Compound aCmp;
149     aBB.MakeCompound( aCmp );
150
151     switch( aPanel->GetMode() )
152     {
153     case HYDROGUI_SplitPolylinesDlg::ByPoint:
154       {
155         gp_Pnt2d aPnt = aPanel->GetPoint();
156         TopoDS_Vertex aVertex = BRepLib_MakeVertex( gp_Pnt( aPnt.X(), aPnt.Y(), 0.0 ) );
157         if( !aPanel->GetMainPolyline().IsNull() )
158           aBB.Add( aCmp, aPanel->GetMainPolyline()->GetShape() );
159         aBB.Add( aCmp, aVertex );
160         break;
161       }
162     case HYDROGUI_SplitPolylinesDlg::ByTool:
163       {
164         if( !aPanel->GetMainPolyline().IsNull() )
165           aBB.Add( aCmp, aPanel->GetMainPolyline()->GetShape() );
166         if( !aPanel->GetToolPolyline().IsNull() )
167           aBB.Add( aCmp, aPanel->GetToolPolyline()->GetShape() );
168         break;
169       }
170     case HYDROGUI_SplitPolylinesDlg::Split:
171       {
172         HYDROData_SequenceOfObjects aPolylines = aPanel->GetPolylines();
173         for( int i=aPolylines.Lower(), n=aPolylines.Upper(); i<=n; i++ )
174         {
175           Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( aPolylines.Value( i ) );
176           if( !aPolyline.IsNull() )
177             aBB.Add( aCmp, aPolyline->GetShape() );
178         }
179         break;
180       }
181     }
182
183
184     myPreviewPrs->setShape( aCmp );
185   }
186 }