Salome HOME
Fix for the bug #45: check and warning when the same image is used in 2 arguments.
[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
35 #include <HYDROData_OperationsFactory.h>
36
37 HYDROGUI_Poly3DOp::HYDROGUI_Poly3DOp( HYDROGUI_Module* theModule,
38                                             const bool theIsEdit )
39 : HYDROGUI_Operation( theModule ),
40   myIsEdit( theIsEdit ),
41   myEditedObject( 0 )
42 {
43   setName( theIsEdit ? tr( "EDIT_POLYLINE_3D" ) : tr( "CREATE_POLYLINE_3D" ) );
44 }
45
46 HYDROGUI_Poly3DOp::~HYDROGUI_Poly3DOp()
47 {
48 }
49
50 HYDROGUI_InputPanel* HYDROGUI_Poly3DOp::createInputPanel() const
51 {
52   return new HYDROGUI_Poly3DDlg( module(), getName() );
53 }
54
55 void HYDROGUI_Poly3DOp::startOperation()
56 {
57   HYDROGUI_Operation::startOperation();
58
59   HYDROGUI_Poly3DDlg* aPanel = (HYDROGUI_Poly3DDlg*)inputPanel();
60   aPanel->reset();
61   aPanel->setMode( myIsEdit );
62
63   QString aPoly3DName;
64   if( myIsEdit )
65   {
66     myEditedObject = Handle(HYDROData_Polyline3D)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
67     if( !myEditedObject.IsNull() )
68       aPoly3DName = myEditedObject->GetName();
69   }
70   else
71   {
72     aPoly3DName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_POLYLINE_3D_NAME" ) );
73   }
74   aPanel->setResultName( aPoly3DName );
75
76   QString aSelectedName1, aSelectedName2;
77   if( myIsEdit && !myEditedObject.IsNull() )
78   {
79     Handle(HYDROData_Entity) anObject1 = myEditedObject->GetProfileUZ()->GetFatherObject();
80     if( !anObject1.IsNull() )
81       aSelectedName1 = anObject1->GetName();
82     Handle(HYDROData_Entity) anObject2 = myEditedObject->GetPolylineXY();
83     if( !anObject2.IsNull() )
84       aSelectedName2 = anObject2->GetName();
85     aPanel->setSelectedObjects( aSelectedName1, aSelectedName2 );
86   }
87   else if( !myIsEdit )
88   {
89     Handle(HYDROData_Profile) aSelectedProfile =
90       Handle(HYDROData_Profile)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
91     if( !aSelectedProfile.IsNull() )
92     {
93       QString aSelectedName = aSelectedProfile->GetName();
94       aPanel->setPreselectedObject( aSelectedName );
95     }
96   }
97 }
98
99 bool HYDROGUI_Poly3DOp::processApply( int& theUpdateFlags,
100                                          QString& theErrorMsg )
101 {
102   HYDROGUI_Poly3DDlg* aPanel = dynamic_cast<HYDROGUI_Poly3DDlg*>( inputPanel() );
103
104   QString aResultName = aPanel->getResultName();
105   if( aResultName.isEmpty() )
106     return false;
107
108   QString aSelectedName1, aSelectedName2;
109   if( !aPanel->getSelectedObjects( aSelectedName1, aSelectedName2 ) )
110     return false;
111
112   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aResultName ) )
113   {
114     // check that there are no other objects with the same name in the document
115     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aResultName );
116     if( !anObject.IsNull() )
117     {
118       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aResultName );
119       return false;
120     }
121   }
122
123   Handle(HYDROData_Entity) anObject1 =
124     HYDROGUI_Tool::FindObjectByName( module(), aSelectedName1, KIND_PROFILE ) ;
125   Handle(HYDROData_Entity) anObject2 =
126     HYDROGUI_Tool::FindObjectByName( module(), aSelectedName2, KIND_POLYLINEXY );
127   if( anObject1.IsNull() || anObject2.IsNull() )
128     return false;
129
130   Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( anObject1 );
131   Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast( anObject2 );
132
133   if( aProfile.IsNull() || aPolyline.IsNull() )
134     return false;
135
136   Handle(HYDROData_ProfileUZ) aProfileUZ = aProfile->GetProfileUZ();
137   if( aProfileUZ.IsNull() )
138     return false;
139
140   Handle(HYDROData_Polyline3D) aResult;
141   if( myIsEdit )
142   {
143     aResult = myEditedObject;
144     aResult->RemoveProfileUZ();
145     aResult->RemovePolylineXY();
146   }
147   else
148   {
149     aResult = Handle(HYDROData_Polyline3D)::DownCast( doc()->CreateObject( KIND_POLYLINE ) );
150   }
151
152   if( aResult.IsNull() )
153     return false;
154
155   aResult->SetName( aResultName );
156   aResult->SetProfileUZ( aProfileUZ );
157   aResult->SetPolylineXY( aPolyline );
158
159   aResult->Update();
160
161   if( !myIsEdit )
162   {
163     size_t aViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
164     module()->setObjectVisible( aViewId, anObject1, false );
165     module()->setObjectVisible( aViewId, anObject2, false );
166     module()->setObjectVisible( aViewId, aResult, true );
167   }
168
169   theUpdateFlags = UF_Model | UF_Viewer | UF_GV_Forced | UF_OCCViewer | UF_OCC_Forced;
170   return true;
171 }