Salome HOME
05e232f3844010e8c45eeab4fae54cfe7435657c
[modules/geom.git] / src / VTKPlugin / VTKPlugin_IOperations.cxx
1 // Copyright (C) 2014-2023  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 <Basics_DirUtils.hxx>
27 #include <utilities.h>
28
29 // GEOM includes
30 #include "GEOM_PythonDump.hxx"
31 #include "GEOMImpl_Types.hxx"
32
33 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
34
35 //=============================================================================
36 /*!
37  *  Constructor
38  */
39 //=============================================================================
40 VTKPlugin_IOperations::VTKPlugin_IOperations( GEOM_Engine* theEngine )
41 : GEOMImpl_IBaseIEOperations( theEngine )
42 {
43   MESSAGE( "VTKPlugin_IOperations::VTKPlugin_IOperations" );
44 }
45
46 //=============================================================================
47 /*!
48  *  Destructor
49  */
50 //=============================================================================
51 VTKPlugin_IOperations::~VTKPlugin_IOperations()
52 {
53   MESSAGE( "VTKPlugin_IOperations::~VTKPlugin_IOperations" );
54 }
55
56 //=============================================================================
57 /*!
58  *  ExportVTK
59  *  Export a shape to VTK format
60  *  \param theOriginal The shape to export
61  *  \param theFileName The name of the file to exported
62  *  \param theDeflection The deflection of the shape to exported
63  */
64 //=============================================================================
65 void VTKPlugin_IOperations::ExportVTK( const Handle(GEOM_Object)      theOriginal,
66                                        const TCollection_AsciiString& theFileName,
67                                        const double                   theDeflection )
68 {
69   SetErrorCode(KO);
70   if( theOriginal.IsNull() ) return;
71
72   Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
73   if( aRefFunction.IsNull() ) return;  //There is no function which creates an object to be exported
74
75   //Add a new result object
76   Handle(GEOM_Object) result = GetEngine()->AddObject( GEOM_IMPORT);
77
78   //Add an Export function
79   Handle(GEOM_Function) aFunction = result->AddFunction( VTKPlugin_ExportDriver::GetID(), EXPORT_SHAPE );
80   if( aFunction.IsNull() ) return;
81
82   //Check if the function is set correctly
83   if( aFunction->GetDriverGUID() != VTKPlugin_ExportDriver::GetID() ) return;
84
85   //Set parameters
86   VTKPlugin_IExport aCI( aFunction );
87   aCI.SetOriginal( aRefFunction );
88   aCI.SetFileName( theFileName );
89   aCI.SetDeflection( theDeflection );
90
91   //Perform the Export
92   try {
93     OCC_CATCH_SIGNALS;
94     if( !GetSolver()->ComputeFunction( aFunction ) ) {
95       SetErrorCode( "Not enough space on disk, or you haven't permissions to write this directory" );
96       return;
97     }
98   }
99   catch( Standard_Failure& aFail ) {
100     SetErrorCode( aFail.GetMessageString() );
101     return;
102   }
103
104   //Make a Python command
105   std::string convFileName = Kernel_Utils::BackSlashToSlash(theFileName.ToCString());
106   GEOM::TPythonDump(aFunction) << "geompy.ExportVTK(" << theOriginal << ", \""
107     << convFileName.c_str() << "\", " << theDeflection << ")";
108
109   SetErrorCode(OK);
110 }