1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_ExportCalculationOp.h"
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_Tool.h"
29 #include <HYDROData_CalculationCase.h>
31 #include <GeometryGUI.h>
33 #include <SalomeApp_Study.h>
35 #include <TopoDS_Shell.hxx>
36 #include <BRepTools.hxx>
38 #include <LightApp_Application.h>
39 #include <LightApp_UpdateFlags.h>
41 #include <SUIT_MessageBox.h>
42 #include <SUIT_Desktop.h>
44 #include <QApplication>
46 HYDROGUI_ExportCalculationOp::HYDROGUI_ExportCalculationOp( HYDROGUI_Module* theModule )
47 : HYDROGUI_Operation( theModule )
49 setName( tr( "EXPORT_CALCULATION" ) );
52 HYDROGUI_ExportCalculationOp::~HYDROGUI_ExportCalculationOp()
56 void HYDROGUI_ExportCalculationOp::startOperation()
58 HYDROGUI_Operation::startOperation();
63 void HYDROGUI_ExportCalculationOp::abortOperation()
65 HYDROGUI_Operation::abortOperation();
68 void HYDROGUI_ExportCalculationOp::commitOperation()
70 HYDROGUI_Operation::commitOperation();
73 bool HYDROGUI_ExportCalculationOp::processApply( int& theUpdateFlags,
74 QString& theErrorMsg )
76 // Get the selected calculation case
77 Handle(HYDROData_CalculationCase) aCalculation =
78 Handle(HYDROData_CalculationCase)::DownCast(
79 HYDROGUI_Tool::GetSelectedObject( module() ) );
81 if ( aCalculation.IsNull() ) {
82 theErrorMsg = tr( "EXPORT_FAILED" );
89 TopoDS_Shell aShell = aCalculation->GetShell();
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 );
103 SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
106 SalomeApp_Study* aStudy =
107 dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
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 );
114 // Puplish the GEOM object
115 if ( !aGeomObj->_is_nil() ) {
116 QString aName = tr( "OBJ_PREFIX" ) + aCalculation->GetName();
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;
128 anErrorMsg = tr( "RESULT_SHAPE_NULL" );
132 theErrorMsg = tr( "EXPORT_FAILED" );
134 if( !anErrorMsg.isEmpty() ) {
135 theErrorMsg.prepend( anErrorMsg + "\n" );
142 void HYDROGUI_ExportCalculationOp::onApply()
144 QApplication::setOverrideCursor( Qt::WaitCursor );
146 int anUpdateFlags = 0;
149 bool aResult = false;
152 aResult = processApply( anUpdateFlags, anErrorMsg );
154 catch ( Standard_Failure )
156 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
157 anErrorMsg = aFailure->GetMessageString();
165 QApplication::restoreOverrideCursor();
168 module()->update( anUpdateFlags );
172 SUIT_MessageBox::information( module()->getApp()->desktop(),
173 tr( "EXPORT_STATUS" ),
174 tr( "EXPORT_FINISHED") );
178 QString aMsg = tr( "EXPORT_FAILED" );
179 /* TODO: improve error processing
180 if( !anErrorMsg.isEmpty() )
181 aMsg.prepend( anErrorMsg + "\n" );
183 SUIT_MessageBox::critical( module()->getApp()->desktop(),
184 tr( "EXPORT_STATUS" ),