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