Salome HOME
0022616: [CEA 1038] Improve the quality of stl and vtk exports
[modules/geom.git] / src / STLPlugin / STLPlugin_IOperations.cxx
1 // Copyright (C) 2014  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 "STLPlugin_IOperations.hxx"
22 #include "STLPlugin_ExportDriver.hxx"
23 #include "STLPlugin_ImportDriver.hxx"
24 #include "STLPlugin_IExport.hxx"
25 #include "STLPlugin_IImport.hxx"
26
27 // KERNEL includes
28 #include <utilities.h>
29
30 // GEOM includes
31 #include "GEOM_PythonDump.hxx"
32 #include "GEOMImpl_Types.hxx"
33
34 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
35
36 //=============================================================================
37 /*!
38  *  Constructor
39  */
40 //=============================================================================
41 STLPlugin_IOperations::STLPlugin_IOperations( GEOM_Engine* theEngine, int theDocID )
42 : GEOMImpl_IBaseIEOperations( theEngine, theDocID )
43 {
44   MESSAGE( "STLPlugin_IOperations::STLPlugin_IOperations" );
45 }
46
47 //=============================================================================
48 /*!
49  *  Destructor
50  */
51 //=============================================================================
52 STLPlugin_IOperations::~STLPlugin_IOperations()
53 {
54   MESSAGE( "STLPlugin_IOperations::~STLPlugin_IOperations" );
55 }
56
57 //=============================================================================
58 /*!
59  *  ExportSTL
60  *  Export a shape to STL format
61  *  \param theOriginal The shape to export
62  *  \param theFileName The name of the file to exported
63  *  \param theIsASCII The format of the exported file (ASCII or Binary)
64  *  \param theDeflection The deflection of the shape to exported
65  *  \param theIsRelative The mode for writing the file. If True (default value),
66  *         the deflection is calculated relatively to the size of the shape;
67  *         if False, the user defined deflection is used.
68  */
69 //=============================================================================
70 void STLPlugin_IOperations::ExportSTL( const Handle(GEOM_Object)      theOriginal,
71                                        const TCollection_AsciiString& theFileName,
72                                        const bool                     theIsASCII,
73                                        const double                   theDeflection,
74                                        const bool                     theIsRelative )
75 {
76   SetErrorCode(KO);
77   if( theOriginal.IsNull() ) return;
78
79   Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
80   if( aRefFunction.IsNull() ) return;  //There is no function which creates an object to be exported
81
82   //Add a new result object
83   Handle(GEOM_Object) result = GetEngine()->AddObject( GetDocID(), GEOM_IMPORT);
84
85   //Add an Export function
86   Handle(GEOM_Function) aFunction = result->AddFunction( STLPlugin_ExportDriver::GetID(), EXPORT_SHAPE );
87   if( aFunction.IsNull() ) return;
88
89   //Check if the function is set correctly
90   if( aFunction->GetDriverGUID() != STLPlugin_ExportDriver::GetID() ) return;
91
92   //Set parameters
93   STLPlugin_IExport aCI( aFunction );
94   aCI.SetOriginal( aRefFunction );
95   aCI.SetFileName( theFileName );
96   aCI.SetIsASCII( theIsASCII );
97   aCI.SetIsRelative( theIsRelative );
98   aCI.SetDeflection( theDeflection );
99
100   //Perform the Export
101   try {
102 #if OCC_VERSION_LARGE > 0x06010000
103     OCC_CATCH_SIGNALS;
104 #endif
105     if( !GetSolver()->ComputeFunction( aFunction ) ) {
106       SetErrorCode( "Not enough space on disk, or you haven't permissions to write this directory" );
107       return;
108     }
109   }
110   catch( Standard_Failure ) {
111     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
112     SetErrorCode( aFail->GetMessageString() );
113     return;
114   }
115
116   //Make a Python command
117   GEOM::TPythonDump(aFunction) << "geompy.ExportSTL(" << theOriginal << ", \""
118     << theFileName.ToCString() << "\", " << theIsASCII << ", " << theDeflection << ", "
119     << theIsRelative << ")";
120
121   SetErrorCode(OK);
122 }
123
124 //=============================================================================
125 /*!
126  *  ImportSTL
127  *  Import a shape from STL format
128  *  \param theFileName The name of the file to import
129  *  \return List of GEOM_Objects, containing the created shape and propagation groups.
130  */
131 //=============================================================================
132 Handle(TColStd_HSequenceOfTransient)
133 STLPlugin_IOperations::ImportSTL( const TCollection_AsciiString& theFileName )
134 {
135   SetErrorCode(KO);
136   if( theFileName.IsEmpty() ) return NULL;
137
138   //Add a new result object
139   Handle(GEOM_Object) anImported = GetEngine()->AddObject( GetDocID(), GEOM_IMPORT );
140
141   //Add an Import function
142   Handle(GEOM_Function) aFunction =
143     anImported->AddFunction( STLPlugin_ImportDriver::GetID(), IMPORT_SHAPE);
144   if (aFunction.IsNull()) return NULL;
145
146   //Check if the function is set correctly
147   if (aFunction->GetDriverGUID() != STLPlugin_ImportDriver::GetID()) return NULL;
148
149   //Set parameters
150   STLPlugin_IImport aCI( aFunction );
151   aCI.SetFileName( theFileName );
152
153   //Perform the Import
154   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
155
156   try {
157 #if OCC_VERSION_LARGE > 0x06010000
158     OCC_CATCH_SIGNALS;
159 #endif
160     if( !GetSolver()->ComputeFunction( aFunction ) ) {
161       SetErrorCode( "Import driver failed" );
162       return NULL;
163     }
164     aSeq->Append(anImported);
165
166     // Greate material groups.
167     // MakeMaterialGroups( anImported, aSeq );
168   }
169   catch( Standard_Failure ) {
170     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
171     SetErrorCode( aFail->GetMessageString() );
172     return NULL;
173   }
174
175   //Make a Python command
176   GEOM::TPythonDump pd (aFunction);
177   pd << aSeq << " = geompy.ImportSTL(\"" << theFileName.ToCString() << "\" )";
178   SetErrorCode(OK);
179
180   return aSeq;
181 }