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" );
88 TopoDS_Shell aShell = aCalculation->GetShell();
90 if ( !aShell.IsNull() ) {
91 // TODO move this code to anothe place?
92 std::ostringstream aStreamShape;
93 // Write TopoDS_Shape in ASCII format to the stream
94 BRepTools::Write( aShell, aStreamShape );
95 // Returns the number of bytes that have been stored in the stream's buffer.
96 int aSize = aStreamShape.str().size();
97 // Allocate octect buffer of required size
98 CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
99 // Copy ostrstream content to the octect buffer
100 memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
102 SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
105 SalomeApp_Study* aStudy =
106 dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
108 // Restore shape from the stream and get the GEOM object
109 GEOM::GEOM_Gen_var aGeomEngine = GeometryGUI::GetGeomGen(); // TODO: get GEOM engine in another way?
110 GEOM::GEOM_IInsertOperations_var anInsOp = aGeomEngine->GetIInsertOperations( module()->getStudyId() );
111 GEOM::GEOM_Object_var aGeomObj = anInsOp->RestoreShape( aSeqFile );
113 // Puplish the GEOM object
114 if ( !aGeomObj->_is_nil() ) {
115 QString aName = tr( "OBJ_PREFIX" ) + aCalculation->GetName();
117 SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy( aStudy->studyDS() );
118 SALOMEDS::SObject_var aResultSO =
119 aGeomEngine->PublishInStudy( aDSStudy, SALOMEDS::SObject::_nil(),
120 aGeomObj, qPrintable( aName ) );
121 if ( !aResultSO->_is_nil() ) {
122 theUpdateFlags = UF_ObjBrowser;
127 theErrorMsg = tr( "RESULT_SHAPE_NULL" );
130 if ( !anIsOk && theErrorMsg.isEmpty() ) {
131 theErrorMsg = tr( "EXPORT_FAILED" );
137 void HYDROGUI_ExportCalculationOp::onApply()
139 QApplication::setOverrideCursor( Qt::WaitCursor );
141 int anUpdateFlags = 0;
144 bool aResult = false;
147 aResult = processApply( anUpdateFlags, anErrorMsg );
149 catch ( Standard_Failure )
151 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
152 anErrorMsg = aFailure->GetMessageString();
160 QApplication::restoreOverrideCursor();
163 module()->update( anUpdateFlags );
167 SUIT_MessageBox::information( module()->getApp()->desktop(),
168 tr( "EXPORT_STATUS" ),
169 tr( "EXPORT_FINISHED") );
173 QString aMsg = tr( "INPUT_VALID_DATA" );
174 if( !anErrorMsg.isEmpty() )
175 aMsg.prepend( anErrorMsg + "\n" );
176 SUIT_MessageBox::critical( module()->getApp()->desktop(),
177 tr( "EXPORT_STATUS" ),