Salome HOME
Feature #197: translation or georeferencing of OBSTACLES Box and Cylinder.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_TranslateObstacleOp.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_TranslateObstacleOp.h"
24
25 #include "HYDROGUI_TranslateObstacleDlg.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_Shape.h"
28 #include "HYDROGUI_Tool.h"
29 #include "HYDROGUI_UpdateFlags.h"
30
31 #include <LightApp_Application.h>
32 #include <LightApp_SelectionMgr.h>
33 #include <LightApp_UpdateFlags.h>
34
35 #include <SUIT_MessageBox.h>
36 #include <SUIT_Desktop.h>
37
38 #include <OCCViewer_ViewManager.h>
39 #include <OCCViewer_ViewModel.h>
40 #include <OCCViewer_ViewWindow.h>
41
42
43 HYDROGUI_TranslateObstacleOp::HYDROGUI_TranslateObstacleOp( HYDROGUI_Module* theModule )
44 : HYDROGUI_Operation( theModule ), 
45   myPreviewPrs( NULL )
46 {
47   setName( tr( "TRANSLATE_OBSTACLE" ) );
48 }
49
50 HYDROGUI_TranslateObstacleOp::~HYDROGUI_TranslateObstacleOp()
51 {
52   erasePreview();
53 }
54
55 void HYDROGUI_TranslateObstacleOp::startOperation()
56 {
57   HYDROGUI_Operation::startOperation();
58
59   // Get panel and reset its state
60   HYDROGUI_TranslateObstacleDlg* aPanel = (HYDROGUI_TranslateObstacleDlg*)inputPanel();
61   aPanel->reset();
62
63   // Get the edited object
64   myEditedObject = Handle(HYDROData_Obstacle)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
65   if ( myEditedObject.IsNull() ) {
66     abort();
67     return;
68   }
69   else if ( myEditedObject->IsMustBeUpdated() ) {
70     myEditedObject->Update();
71   }
72
73   // Set the edited object name to the panel
74   aPanel->setName( myEditedObject->GetName() );
75
76   // Create preview
77   createPreview();
78 }
79
80 void HYDROGUI_TranslateObstacleOp::abortOperation()
81 {
82   erasePreview();
83   abortDocOperation();
84
85   HYDROGUI_Operation::abortOperation();
86 }
87
88 void HYDROGUI_TranslateObstacleOp::commitOperation()
89 {
90   erasePreview();
91
92   HYDROGUI_Operation::commitOperation();
93 }
94
95 HYDROGUI_InputPanel* HYDROGUI_TranslateObstacleOp::createInputPanel() const
96 {
97   HYDROGUI_TranslateObstacleDlg* aPanel = new HYDROGUI_TranslateObstacleDlg( module(), getName() );
98
99   connect( aPanel, SIGNAL( argumentsChanged() ), this, SLOT( onArgumentsChanged() ) );
100
101   return aPanel;
102 }
103
104 bool HYDROGUI_TranslateObstacleOp::processApply( int& theUpdateFlags,
105                                                  QString& theErrorMsg )
106 {
107   HYDROGUI_TranslateObstacleDlg* aPanel = ::qobject_cast<HYDROGUI_TranslateObstacleDlg*>( inputPanel() );
108   if ( !aPanel || myEditedObject.IsNull() ) {
109     return false;
110   }
111
112   // Get the translated shape
113   TopoDS_Shape aTranslatedShape = getTranslatedShape();
114   if ( aTranslatedShape.IsNull() ) {
115     return false;
116   }
117
118   // Erase preview
119   erasePreview();
120
121   // Set the translated shape to the obstacle
122   myEditedObject->SetShape3D( aTranslatedShape ); 
123   myEditedObject->Update();
124
125   module()->setIsToUpdate( myEditedObject );
126
127   // Set update flags
128   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
129
130   return true;
131 }
132
133 void HYDROGUI_TranslateObstacleOp::createPreview()
134 {
135   if ( myEditedObject.IsNull() ) {
136     return;
137   }
138
139   // Create preview presentation if necessary
140   if ( !myPreviewPrs ) {
141     LightApp_Application* anApp = module()->getApp();
142     OCCViewer_ViewManager* aViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
143       anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
144   
145     if ( aViewManager ) {
146       if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
147         Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
148         if ( !aCtx.IsNull() ) {
149           myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL );
150           myPreviewPrs->setFillingColor( myEditedObject->GetFillingColor(), false, false );
151           myPreviewPrs->setBorderColor( myEditedObject->GetBorderColor(), false, false );
152         }
153       }
154     }
155   }
156   
157   // Get the translated shape
158   TopoDS_Shape aTranslatedShape = getTranslatedShape();
159
160   // Set the translated shape to the preview presentation
161   if ( myPreviewPrs ) {
162     myPreviewPrs->setShape( aTranslatedShape );
163   }
164 }
165
166 void HYDROGUI_TranslateObstacleOp::erasePreview()
167 {
168   if( myPreviewPrs ) {
169     delete myPreviewPrs;
170     myPreviewPrs = 0;
171   }
172 }
173
174 void HYDROGUI_TranslateObstacleOp::onArgumentsChanged()
175 {
176   // Update preview
177   createPreview();
178 }
179
180 TopoDS_Shape HYDROGUI_TranslateObstacleOp::getTranslatedShape() const
181 {
182   TopoDS_Shape aTranslatedShape;
183
184   HYDROGUI_TranslateObstacleDlg* aPanel = (HYDROGUI_TranslateObstacleDlg*)inputPanel();
185   if ( aPanel && !myEditedObject.IsNull() ) {
186     double aDx = aPanel->getDx();
187     double aDy = aPanel->getDy();
188     double aDz = aPanel->getDz();
189
190     TopoDS_Shape aShape = myEditedObject->GetShape3D();
191     gp_Trsf aTrsf;
192     gp_Vec aVec( aDx, aDy, aDz );
193     aTrsf.SetTranslation(aVec);
194     TopLoc_Location aLocOrig = aShape.Location();
195     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
196     TopLoc_Location aLocRes( aTrsf * aTrsfOrig );
197     aTranslatedShape = aShape.Located( aLocRes );
198   }
199
200   return aTranslatedShape;
201 }