Salome HOME
Child profile object is added for Polyline 3D if bathymetry is selcted (Bug #235).
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Poly3DOp.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_Poly3DOp.h"
24
25 #include "HYDROGUI_Module.h"
26 #include "HYDROGUI_Tool.h"
27 #include "HYDROGUI_Poly3DDlg.h"
28 #include "HYDROGUI_UpdateFlags.h"
29
30 #include <HYDROData_Document.h>
31 #include <HYDROData_Polyline3D.h>
32 #include <HYDROData_Profile.h>
33 #include <HYDROData_PolylineXY.h>
34 #include <HYDROData_Bathymetry.h>
35
36 #include <HYDROData_OperationsFactory.h>
37
38 HYDROGUI_Poly3DOp::HYDROGUI_Poly3DOp( HYDROGUI_Module* theModule,
39                                             const bool theIsEdit )
40 : HYDROGUI_Operation( theModule ),
41   myIsEdit( theIsEdit ),
42   myEditedObject( 0 )
43 {
44   setName( theIsEdit ? tr( "EDIT_POLYLINE_3D" ) : tr( "CREATE_POLYLINE_3D" ) );
45 }
46
47 HYDROGUI_Poly3DOp::~HYDROGUI_Poly3DOp()
48 {
49 }
50
51 HYDROGUI_InputPanel* HYDROGUI_Poly3DOp::createInputPanel() const
52 {
53   return new HYDROGUI_Poly3DDlg( module(), getName() );
54 }
55
56 void HYDROGUI_Poly3DOp::startOperation()
57 {
58   HYDROGUI_Operation::startOperation();
59
60   HYDROGUI_Poly3DDlg* aPanel = (HYDROGUI_Poly3DDlg*)inputPanel();
61   aPanel->reset();
62   aPanel->setMode( myIsEdit );
63
64   QString aPoly3DName;
65   if( myIsEdit )
66   {
67     myEditedObject = Handle(HYDROData_Polyline3D)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
68     if( !myEditedObject.IsNull() )
69       aPoly3DName = myEditedObject->GetName();
70   }
71   else
72   {
73     aPoly3DName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_POLYLINE_3D_NAME" ) );
74   }
75   aPanel->setResultName( aPoly3DName );
76
77   QString aPolyName, aProfileName, aBathName;
78   if( myIsEdit && !myEditedObject.IsNull() )
79   {
80     Handle(HYDROData_ProfileUZ) aProfileUZ = myEditedObject->GetProfileUZ();
81     if( !aProfileUZ.IsNull() )
82     {
83       Handle(HYDROData_Entity) aProfile = aProfileUZ->GetFatherObject();
84       if ( !aProfile.IsNull() )
85         aProfileName = aProfile->GetName();
86     }
87
88     Handle(HYDROData_Entity) aPoly = myEditedObject->GetPolylineXY();
89     if( !aPoly.IsNull() )
90       aPolyName = aPoly->GetName();
91
92     Handle(HYDROData_Bathymetry) aBathymetry = myEditedObject->GetBathymetry();
93     if( !aBathymetry.IsNull() )
94       aBathName = aBathymetry->GetName();
95
96     aPanel->setSelectedObjects( aPolyName, aProfileName, aBathName );
97   }
98   else if( !myIsEdit )
99   {
100     Handle(HYDROData_Profile) aSelectedProfile =
101       Handle(HYDROData_Profile)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
102     if( !aSelectedProfile.IsNull() )
103     {
104       QString aSelectedName = aSelectedProfile->GetName();
105       aPanel->setPreselectedObject( aSelectedName );
106     }
107   }
108 }
109
110 bool HYDROGUI_Poly3DOp::processApply( int& theUpdateFlags,
111                                          QString& theErrorMsg )
112 {
113   HYDROGUI_Poly3DDlg* aPanel = dynamic_cast<HYDROGUI_Poly3DDlg*>( inputPanel() );
114
115   QString aResultName = aPanel->getResultName();
116   if( aResultName.isEmpty() )
117     return false;
118
119   QString aPolyName, aProfileName, aBathName;
120   if( !aPanel->getSelectedObjects( aPolyName, aProfileName, aBathName ) )
121     return false;
122
123   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aResultName ) )
124   {
125     // check that there are no other objects with the same name in the document
126     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aResultName );
127     if( !anObject.IsNull() )
128     {
129       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aResultName );
130       return false;
131     }
132   }
133
134   Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( 
135     HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
136   Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast( 
137     HYDROGUI_Tool::FindObjectByName( module(), aPolyName, KIND_POLYLINEXY ) );
138   Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( 
139     HYDROGUI_Tool::FindObjectByName( module(), aBathName, KIND_BATHYMETRY ) );
140
141   if ( aPolyline.IsNull() || ( aProfile.IsNull() && aBath.IsNull() ) )
142     return false;
143
144   Handle(HYDROData_Polyline3D) aResult;
145   if( myIsEdit )
146   {
147     aResult = myEditedObject;
148   }
149   else
150   {
151     aResult = Handle(HYDROData_Polyline3D)::DownCast( doc()->CreateObject( KIND_POLYLINE ) );
152   }
153
154   if( aResult.IsNull() )
155     return false;
156
157   aResult->SetName( aResultName );
158
159   aResult->SetPolylineXY( aPolyline, false );
160   if ( !aProfile.IsNull() )
161   {
162     Handle(HYDROData_ProfileUZ) aProfileUZ = aProfile->GetProfileUZ();
163     if( aProfileUZ.IsNull() )
164       return false;
165     
166     aResult->SetProfileUZ( aProfileUZ );
167   }
168   else
169   {
170     aResult->SetBathymetry( aBath );
171   }
172
173   if( !myIsEdit )
174   {
175     aResult->SetBorderColor( HYDROData_Polyline3D::DefaultBorderColor() );
176   }
177
178   aResult->Update();
179
180   if( !myIsEdit )
181   {
182     size_t aViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
183     module()->setObjectVisible( aViewId, aPolyline, false );
184     module()->setObjectVisible( aViewId, aProfile, false );
185     module()->setObjectVisible( aViewId, aResult, true );
186   }
187
188   theUpdateFlags = UF_Model | UF_Viewer | UF_GV_Forced | UF_OCCViewer | UF_OCC_Forced;
189   return true;
190 }