Salome HOME
- Bathymethries are colored now
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ExportCalculationOp.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_ExportCalculationOp.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_Tool.h"
28
29 #include <HYDROData_CalculationCase.h>
30
31 #include <GeometryGUI.h>
32
33 #include <SalomeApp_Study.h>
34
35 #include <TopoDS_Shell.hxx>
36 #include <BRepTools.hxx>
37
38 #include <LightApp_Application.h>
39 #include <LightApp_UpdateFlags.h>
40
41 #include <SUIT_MessageBox.h>
42 #include <SUIT_Desktop.h>
43
44 #include <QApplication>
45
46 HYDROGUI_ExportCalculationOp::HYDROGUI_ExportCalculationOp( HYDROGUI_Module* theModule )
47 : HYDROGUI_Operation( theModule )
48 {
49   setName( tr( "EXPORT_CALCULATION" ) );
50 }
51
52 HYDROGUI_ExportCalculationOp::~HYDROGUI_ExportCalculationOp()
53 {
54 }
55
56 void HYDROGUI_ExportCalculationOp::startOperation()
57 {
58   HYDROGUI_Operation::startOperation();
59
60   onApply();
61 }
62
63 void HYDROGUI_ExportCalculationOp::abortOperation()
64 {
65   HYDROGUI_Operation::abortOperation();
66 }
67
68 void HYDROGUI_ExportCalculationOp::commitOperation()
69 {
70   HYDROGUI_Operation::commitOperation();
71 }
72
73 bool HYDROGUI_ExportCalculationOp::processApply( int& theUpdateFlags,
74                                                 QString& theErrorMsg )
75 {
76   // Get the selected calculation case
77   Handle(HYDROData_CalculationCase) aCalculation = 
78     Handle(HYDROData_CalculationCase)::DownCast( 
79       HYDROGUI_Tool::GetSelectedObject( module() ) );
80
81   if ( aCalculation.IsNull() ) {
82     theErrorMsg = tr( "EXPORT_FAILED" );
83     return false;
84   }
85
86   QString anErrorMsg;
87   bool anIsOk = false;
88
89   TopoDS_Shell aShell = aCalculation->GetShell();
90
91   if ( !aShell.IsNull() ) {
92     // TODO move this code to anothe place?
93     std::ostringstream aStreamShape;
94     // Write TopoDS_Shape in ASCII format to the stream
95     BRepTools::Write( aShell, aStreamShape );
96     // Returns the number of bytes that have been stored in the stream's buffer.
97     int aSize = aStreamShape.str().size();
98     // Allocate octect buffer of required size
99     CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
100     // Copy ostrstream content to the octect buffer
101     memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
102     // Create TMPFile
103     SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
104
105     // Get active study
106     SalomeApp_Study* aStudy = 
107       dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
108
109     // Restore shape from the stream and get the GEOM object
110     GEOM::GEOM_Gen_var aGeomEngine = GeometryGUI::GetGeomGen(); // TODO: get GEOM engine in another way?
111     GEOM::GEOM_IInsertOperations_var anInsOp = aGeomEngine->GetIInsertOperations( module()->getStudyId() );
112     GEOM::GEOM_Object_var aGeomObj = anInsOp->RestoreShape( aSeqFile );
113
114     // Puplish the GEOM object
115     if ( !aGeomObj->_is_nil() ) {
116       QString aName = tr( "OBJ_PREFIX" ) + aCalculation->GetName();
117
118       SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy( aStudy->studyDS() );
119       SALOMEDS::SObject_var aResultSO = 
120         aGeomEngine->PublishInStudy( aDSStudy, SALOMEDS::SObject::_nil(), 
121                                      aGeomObj, qPrintable( aName ) );
122       if ( !aResultSO->_is_nil() ) {
123         theUpdateFlags = UF_ObjBrowser;
124         anIsOk = true;
125       } 
126     }
127   } else {
128     anErrorMsg = tr( "RESULT_SHAPE_NULL" );
129   }
130
131   if ( !anIsOk ) {
132     theErrorMsg = tr( "EXPORT_FAILED" );
133
134     if( !anErrorMsg.isEmpty() ) {
135       theErrorMsg.prepend( anErrorMsg + "\n" );
136     }
137   }
138
139   return anIsOk;
140 }
141
142 void HYDROGUI_ExportCalculationOp::onApply()
143 {
144   QApplication::setOverrideCursor( Qt::WaitCursor );
145
146   int anUpdateFlags = 0;
147   QString anErrorMsg;
148
149   bool aResult = false;
150   
151   try {
152     aResult = processApply( anUpdateFlags, anErrorMsg );
153   }
154   catch ( Standard_Failure )
155   {
156     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
157     anErrorMsg = aFailure->GetMessageString();
158     aResult = false;
159   }
160   catch ( ... )
161   {
162     aResult = false;
163   }
164   
165   QApplication::restoreOverrideCursor();
166
167   if ( aResult ) {
168     module()->update( anUpdateFlags );
169     commit();
170
171     // Show message box
172     SUIT_MessageBox::information( module()->getApp()->desktop(),
173                                   tr( "EXPORT_STATUS" ),
174                                   tr( "EXPORT_FINISHED") ); 
175   } 
176   else {
177     abort();
178     QString aMsg = tr( "EXPORT_FAILED" );
179     /* TODO: improve error processing
180     if( !anErrorMsg.isEmpty() )
181       aMsg.prepend( anErrorMsg + "\n" );
182     */
183     SUIT_MessageBox::critical( module()->getApp()->desktop(),
184                                tr( "EXPORT_STATUS" ),
185                                aMsg ); 
186   }
187 }