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>
34 #include <SalomeApp_Study.h>
36 #include <TopoDS_Shell.hxx>
37 #include <BRepTools.hxx>
39 #include <LightApp_Application.h>
40 #include <LightApp_UpdateFlags.h>
42 #include <SUIT_MessageBox.h>
43 #include <SUIT_Desktop.h>
45 #include <QApplication>
47 HYDROGUI_ExportCalculationOp::HYDROGUI_ExportCalculationOp( HYDROGUI_Module* theModule )
48 : HYDROGUI_Operation( theModule )
50 setName( tr( "EXPORT_CALCULATION" ) );
53 HYDROGUI_ExportCalculationOp::~HYDROGUI_ExportCalculationOp()
57 void HYDROGUI_ExportCalculationOp::startOperation()
59 HYDROGUI_Operation::startOperation();
64 void HYDROGUI_ExportCalculationOp::abortOperation()
66 HYDROGUI_Operation::abortOperation();
69 void HYDROGUI_ExportCalculationOp::commitOperation()
71 HYDROGUI_Operation::commitOperation();
74 bool HYDROGUI_ExportCalculationOp::processApply( int& theUpdateFlags,
75 QString& theErrorMsg )
77 // Get the selected calculation case
78 Handle(HYDROData_CalculationCase) aCalculation =
79 Handle(HYDROData_CalculationCase)::DownCast(
80 HYDROGUI_Tool::GetSelectedObject( module() ) );
82 if ( aCalculation.IsNull() ) {
83 theErrorMsg = tr( "NULL_DATA_OBJECT" );
90 TopoDS_Shell aShell = aCalculation->GetShell();
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 );
104 SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
107 SalomeApp_Study* aStudy =
108 dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
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 );
115 // Puplish the GEOM object
116 if ( !aGeomObj->_is_nil() ) {
117 QString aName = GEOMBase::GetDefaultName( tr( "OBJ_PREFIX" ) + aCalculation->GetName() );
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;
130 theErrorMsg = tr( "IMPOSSIBLE_TO_CREATE_GEOM_SHAPE" );
133 theErrorMsg = tr( "RESULT_SHAPE_NULL" );
139 void HYDROGUI_ExportCalculationOp::onApply()
141 QApplication::setOverrideCursor( Qt::WaitCursor );
143 int anUpdateFlags = 0;
146 bool aResult = false;
149 aResult = processApply( anUpdateFlags, anErrorMsg );
151 catch ( Standard_Failure )
153 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
154 anErrorMsg = aFailure->GetMessageString();
162 QApplication::restoreOverrideCursor();
165 module()->update( anUpdateFlags );
169 SUIT_MessageBox::information( module()->getApp()->desktop(),
170 tr( "EXPORT_STATUS" ),
171 tr( "EXPORT_FINISHED") );
176 if ( anErrorMsg.isEmpty() )
177 anErrorMsg = tr( "EXPORT_DATA_FAILED" );
179 anErrorMsg.prepend( tr( "EXPORT_FAILED" ) + "\n" );
181 SUIT_MessageBox::critical( module()->getApp()->desktop(),
182 tr( "EXPORT_STATUS" ),