Salome HOME
a9f31f6247b9e1a90df11e995289b93745a30f9d
[modules/geom.git] / src / VTKPlugin / VTKPlugin_IOperations.cxx
1 // Copyright (C) 2014-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // internal includes
21 #include "VTKPlugin_IOperations.hxx"
22 #include "VTKPlugin_ExportDriver.hxx"
23 #include "VTKPlugin_IExport.hxx"
24
25 // KERNEL includes
26 #include <utilities.h>
27
28 // GEOM includes
29 #include "GEOM_PythonDump.hxx"
30 #include "GEOMImpl_Types.hxx"
31
32 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
33
34 //=============================================================================
35 /*!
36  *  Constructor
37  */
38 //=============================================================================
39 VTKPlugin_IOperations::VTKPlugin_IOperations( GEOM_Engine* theEngine, int theDocID )
40 : GEOMImpl_IBaseIEOperations( theEngine, theDocID )
41 {
42   MESSAGE( "VTKPlugin_IOperations::VTKPlugin_IOperations" );
43 }
44
45 //=============================================================================
46 /*!
47  *  Destructor
48  */
49 //=============================================================================
50 VTKPlugin_IOperations::~VTKPlugin_IOperations()
51 {
52   MESSAGE( "VTKPlugin_IOperations::~VTKPlugin_IOperations" );
53 }
54
55 //=============================================================================
56 /*!
57  *  ExportVTK
58  *  Export a shape to VTK format
59  *  \param theOriginal The shape to export
60  *  \param theFileName The name of the file to exported
61  *  \param theDeflection The deflection of the shape to exported
62  */
63 //=============================================================================
64 void VTKPlugin_IOperations::ExportVTK( const Handle(GEOM_Object)      theOriginal,
65                                        const TCollection_AsciiString& theFileName,
66                                        const double                   theDeflection )
67 {
68   SetErrorCode(KO);
69   if( theOriginal.IsNull() ) return;
70
71   Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
72   if( aRefFunction.IsNull() ) return;  //There is no function which creates an object to be exported
73
74   //Add a new result object
75   Handle(GEOM_Object) result = GetEngine()->AddObject( GetDocID(), GEOM_IMPORT);
76
77   //Add an Export function
78   Handle(GEOM_Function) aFunction = result->AddFunction( VTKPlugin_ExportDriver::GetID(), EXPORT_SHAPE );
79   if( aFunction.IsNull() ) return;
80
81   //Check if the function is set correctly
82   if( aFunction->GetDriverGUID() != VTKPlugin_ExportDriver::GetID() ) return;
83
84   //Set parameters
85   VTKPlugin_IExport aCI( aFunction );
86   aCI.SetOriginal( aRefFunction );
87   aCI.SetFileName( theFileName );
88   aCI.SetDeflection( theDeflection );
89
90   //Perform the Export
91   try {
92     OCC_CATCH_SIGNALS;
93     if( !GetSolver()->ComputeFunction( aFunction ) ) {
94       SetErrorCode( "Not enough space on disk, or you haven't permissions to write this directory" );
95       return;
96     }
97   }
98   catch( Standard_Failure ) {
99     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
100     SetErrorCode( aFail->GetMessageString() );
101     return;
102   }
103
104   //Make a Python command
105   GEOM::TPythonDump(aFunction) << "geompy.ExportVTK(" << theOriginal << ", \""
106     << theFileName.ToCString() << "\", " << theDeflection << ")";
107
108   SetErrorCode(OK);
109 }