Salome HOME
refs #653, #665 - 669: start implementation of features.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_PolylineExtractionOp.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_PolylineExtractionOp.h"
20
21 #include <GraphicsView_Viewer.h>
22
23 #include "HYDROGUI_Module.h"
24 #include "HYDROGUI_Operations.h"
25 #include "HYDROGUI_Tool.h"
26 #include "HYDROGUI_UpdateFlags.h"
27
28 #include <HYDROData_Profile.h>
29 #include <HYDROData_Document.h>
30 #include <HYDROData_PolylineXY.h>
31 #include <HYDROData_ShapesGroup.h>
32
33 #include <LightApp_Application.h>
34 #include <LightApp_Displayer.h>
35
36 #include <OCCViewer_ViewModel.h>
37 #include <OCCViewer_ViewManager.h>
38
39 #include <BRep_Builder.hxx>
40
41 HYDROGUI_PolylineExtractionOp::HYDROGUI_PolylineExtractionOp( HYDROGUI_Module* theModule )
42 : HYDROGUI_Operation( theModule )
43 {
44   setName( tr( "POLYLINE_EXTRACTION" ) );
45 }
46
47 HYDROGUI_PolylineExtractionOp::~HYDROGUI_PolylineExtractionOp()
48 {
49 }
50
51 void HYDROGUI_PolylineExtractionOp::startOperation()
52 {
53     HYDROGUI_Operation::startOperation();
54
55     int anUpdateFlags = UF_ObjBrowser | UF_Model;
56     HYDROGUI_Module* aModule = module();
57
58     HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( aModule );
59     if ( aSeq.IsEmpty() )
60         return;
61
62     for ( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
63     {
64         Handle(HYDROData_Entity) anObject = aSeq.Value( anIndex );
65         if ( anObject.IsNull() )
66             continue;
67
68         ShapeInfoList aList;
69
70         switch ( anObject->GetKind() )
71         {
72         case KIND_STREAM:
73             aList = extract( Handle(HYDROData_Stream)::DownCast( anObject ) );
74             break;
75         case KIND_DIGUE:
76         case KIND_CHANNEL:
77             aList = extract( Handle(HYDROData_Channel)::DownCast( anObject ) );
78             break;
79         case KIND_OBSTACLE:
80             aList = extract( Handle(HYDROData_Obstacle)::DownCast( anObject ) );
81             break;
82         }
83
84         startDocOperation();
85
86         for ( ShapeInfoList::const_iterator it = aList.begin(); it != aList.end(); ++it )
87         {
88             const ShapeInfo& inf = *it;
89             TopoDS_Shape aLine = inf.shape();
90             QString aName = QString( "%1_%2" ).arg( anObject->GetName() ).arg( inf.name() );
91             if ( !HYDROGUI_Tool::FindObjectByName( aModule, aName ).IsNull() )
92             {
93                 int num = 1;
94                 while ( !HYDROGUI_Tool::FindObjectByName( aModule, aName + QString( "_%1" ).arg( num ) ).IsNull() )
95                     num++;
96
97                 aName += QString( "_%1" ).arg( num );
98             }
99
100             Handle(HYDROData_PolylineXY) aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
101             if ( !aPolylineObj.IsNull() )
102             {
103                 aPolylineObj->SetName( aName );
104                 aPolylineObj->ImportShape( aLine );
105                 aPolylineObj->Update();
106             }
107         }
108
109         commitDocOperation();
110     }
111
112     aModule->update( anUpdateFlags );
113
114     commit();
115 }
116
117 HYDROGUI_PolylineExtractionOp::ShapeInfoList HYDROGUI_PolylineExtractionOp::extract( const Handle(HYDROData_Stream)& theStream ) const
118 {
119     ShapeInfoList aList;
120     if ( !theStream.IsNull() )
121     {
122         aList.append( ShapeInfo( theStream->GetLeftShape(), QString( "Left bank" ) ) );
123         aList.append( ShapeInfo( theStream->GetRightShape(), QString( "Right bank" ) ) );
124
125         HYDROData_SequenceOfObjects aProfiles = theStream->GetProfiles();
126         for ( int i = 1; i <= aProfiles.Length(); i++ )
127         {
128             Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( aProfiles.Value( i ) );
129             aList.append( ShapeInfo( aProfile->GetTopShape(), QString( "Profile_%1" ).arg( i ) ) );
130         }
131     }
132     return aList;
133 }
134
135 HYDROGUI_PolylineExtractionOp::ShapeInfoList HYDROGUI_PolylineExtractionOp::extract( const Handle(HYDROData_Channel)& aChannel ) const
136 {
137     ShapeInfoList aList;
138     if ( !aChannel.IsNull() )
139     {
140         HYDROData_SequenceOfObjects aGroups = aChannel->GetGroups();
141         int aNum = qMin( aGroups.Length(), 2 );
142         for ( int i = 1; i <= aNum; i++ )
143         {
144             Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( aGroups.Value( i ) );
145             TopTools_SequenceOfShape aShapes;
146             aGroup->GetShapes( aShapes );
147             if ( !aShapes.IsEmpty() )
148             {
149                 TopoDS_Wire aWire;
150                 BRep_Builder aBuilder;
151                 aBuilder.MakeWire( aWire );
152
153                 for ( int s = 1; s <= aShapes.Length(); s++ )
154                     aBuilder.Add( aWire, aShapes.Value( s ) );
155                 aList.append( ShapeInfo( aWire, i > 1 ? "Right bank" : "Left bank" ) );
156             }
157         }
158     }
159     return aList;
160 }
161
162 HYDROGUI_PolylineExtractionOp::ShapeInfoList HYDROGUI_PolylineExtractionOp::extract( const Handle(HYDROData_Obstacle)& theObstacle ) const
163 {
164     ShapeInfoList aList;
165     if ( !theObstacle.IsNull() )
166     {
167         TopoDS_Wire aWire;
168         BRep_Builder aBuilder;
169         aBuilder.MakeWire( aWire );
170         HYDROData_SequenceOfObjects aGroups = theObstacle->GetGroups();
171         for ( int i = 1; i <= aGroups.Length(); i++ )
172         {
173             Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( aGroups.Value( i ) );
174             TopTools_SequenceOfShape aShapes;
175             aGroup->GetShapes( aShapes );
176             for ( int s = 1; s <= aShapes.Length(); s++ )
177                 aBuilder.Add( aWire, aShapes.Value( s ) );
178         }
179         aList.append( ShapeInfo( aWire, QString( "Contour" ) ) );
180     }
181     return aList;
182 }