Salome HOME
Merge remote-tracking branch 'origin/BR_LAND_COVER' into BR_v14_rc
[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         ShapeMap aMap;
69
70         switch ( anObject->GetKind() )
71         {
72         case KIND_STREAM:
73             aMap = extract( Handle(HYDROData_Stream)::DownCast( anObject ) );
74             break;
75         case KIND_DIGUE:
76         case KIND_CHANNEL:
77             aMap = extract( Handle(HYDROData_Channel)::DownCast( anObject ) );
78             break;
79         case KIND_OBSTACLE:
80             aMap = extract( Handle(HYDROData_Obstacle)::DownCast( anObject ) );
81             break;
82         }
83
84         startDocOperation();
85
86         for ( ShapeMap::Iterator it( aMap ); it.More(); it.Next() )
87         {
88             TopoDS_Shape aLine = it.Key();
89             QString aName = QString( "%1_%2" ).arg( anObject->GetName() ).arg( it.Value() );
90             if ( !HYDROGUI_Tool::FindObjectByName( aModule, aName ).IsNull() )
91             {
92                 int num = 1;
93                 while ( !HYDROGUI_Tool::FindObjectByName( aModule, aName + QString( "_%1" ).arg( num ) ).IsNull() )
94                     num++;
95
96                 aName += QString( "_%1" ).arg( num );
97             }
98
99             Handle(HYDROData_PolylineXY) aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
100             if ( !aPolylineObj.IsNull() )
101             {
102                 aPolylineObj->SetName( aName );
103                 aPolylineObj->ImportShape( aLine );
104                 aPolylineObj->Update();
105             }
106         }
107
108         commitDocOperation();
109     }
110
111     aModule->update( anUpdateFlags );
112
113     commit();
114 }
115
116 HYDROGUI_PolylineExtractionOp::ShapeMap HYDROGUI_PolylineExtractionOp::extract( const Handle(HYDROData_Stream)& theStream )
117 {
118     ShapeMap aMap;
119     if ( !theStream.IsNull() )
120     {
121         aMap.Bind( theStream->GetLeftShape(), "Left bank" );
122         aMap.Bind( theStream->GetRightShape(), "Right bank" );
123
124         HYDROData_SequenceOfObjects aProfiles = theStream->GetProfiles();
125         for ( int i = 1; i <= aProfiles.Length(); i++ )
126         {
127             Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( aProfiles.Value( i ) );
128             aMap.Bind( aProfile->GetTopShape(), aProfile->GetName() );
129         }
130     }
131     return aMap;
132 }
133
134 HYDROGUI_PolylineExtractionOp::ShapeMap HYDROGUI_PolylineExtractionOp::extract( const Handle(HYDROData_Channel)& aChannel )
135 {
136     ShapeMap aMap;
137     if ( !aChannel.IsNull() )
138     {
139         HYDROData_SequenceOfObjects aGroups = aChannel->GetGroups();
140         int aNum = qMin( aGroups.Length(), 2 );
141         for ( int i = 1; i <= aNum; i++ )
142         {
143             Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( aGroups.Value( i ) );
144             TopTools_SequenceOfShape aShapes;
145             aGroup->GetShapes( aShapes );
146             if ( !aShapes.IsEmpty() )
147             {
148                 TopoDS_Wire aWire;
149                 BRep_Builder aBuilder;
150                 aBuilder.MakeWire( aWire );
151
152                 for ( int s = 1; s <= aShapes.Length(); s++ )
153                     aBuilder.Add( aWire, aShapes.Value( s ) );
154                 aMap.Bind( aWire, i > 1 ? "Right bank" : "Left bank" );
155             }
156         }
157     }
158     return aMap;
159 }
160
161 HYDROGUI_PolylineExtractionOp::ShapeMap HYDROGUI_PolylineExtractionOp::extract( const Handle(HYDROData_Obstacle)& theObstacle )
162 {
163     ShapeMap aMap;
164     if ( !theObstacle.IsNull() )
165     {
166         TopoDS_Wire aWire;
167         BRep_Builder aBuilder;
168         aBuilder.MakeWire( aWire );
169         HYDROData_SequenceOfObjects aGroups = theObstacle->GetGroups();
170         for ( int i = 1; i <= aGroups.Length(); i++ )
171         {
172             Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( aGroups.Value( i ) );
173             TopTools_SequenceOfShape aShapes;
174             aGroup->GetShapes( aShapes );
175             for ( int s = 1; s <= aShapes.Length(); s++ )
176                 aBuilder.Add( aWire, aShapes.Value( s ) );
177         }
178         aMap.Bind( aWire, "Contour" );
179     }
180     return aMap;
181 }