Salome HOME
0022616: [CEA 1038] Improve the quality of stl and vtk exports
[modules/geom.git] / src / IGESPlugin / IGESPlugin_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 "IGESPlugin_IOperations.hxx"
22 #include "IGESPlugin_ExportDriver.hxx"
23 #include "IGESPlugin_ImportDriver.hxx"
24 #include "IGESPlugin_IExport.hxx"
25 #include "IGESPlugin_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 IGESPlugin_IOperations::IGESPlugin_IOperations( GEOM_Engine* theEngine, int theDocID )
42 : GEOMImpl_IBaseIEOperations( theEngine, theDocID )
43 {
44   MESSAGE( "IGESPlugin_IOperations::IGESPlugin_IOperations" );
45 }
46
47 //=============================================================================
48 /*!
49  *  Destructor
50  */
51 //=============================================================================
52 IGESPlugin_IOperations::~IGESPlugin_IOperations()
53 {
54   MESSAGE( "IGESPlugin_IOperations::~IGESPlugin_IOperations" );
55 }
56
57 //=============================================================================
58 /*!
59  *  ExportIGES
60  *  Export a shape to IGES 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 theVersion The version of IGES format which defines, whether to write
66  *                    only faces (5.1 IGES format) or shells and solids also (5.3 IGES format).
67  */
68 //=============================================================================
69 void IGESPlugin_IOperations::ExportIGES( const Handle(GEOM_Object)      theOriginal,
70                                                  const TCollection_AsciiString& theFileName,
71                                                  const TCollection_AsciiString& theVersion )
72 {
73   SetErrorCode(KO);
74   if( theOriginal.IsNull() ) return;
75
76   Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
77   if( aRefFunction.IsNull() ) return;  //There is no function which creates an object to be exported
78
79   //Add a new result object
80   Handle(GEOM_Object) result = GetEngine()->AddObject( GetDocID(), GEOM_IMPORT);
81
82   //Add an Export function
83   Handle(GEOM_Function) aFunction = result->AddFunction( IGESPlugin_ExportDriver::GetID(), EXPORT_SHAPE );
84   if( aFunction.IsNull() ) return;
85
86   //Check if the function is set correctly
87   if( aFunction->GetDriverGUID() != IGESPlugin_ExportDriver::GetID() ) return;
88
89   //Set parameters
90   IGESPlugin_IExport aCI( aFunction );
91   aCI.SetOriginal( aRefFunction );
92   aCI.SetFileName( theFileName );
93   aCI.SetVersion( theVersion );
94
95   //Perform the Export
96   try {
97 #if OCC_VERSION_LARGE > 0x06010000
98     OCC_CATCH_SIGNALS;
99 #endif
100     if( !GetSolver()->ComputeFunction( aFunction ) ) {
101       SetErrorCode( "Not enough space on disk, or you haven't permissions to write this directory" );
102       return;
103     }
104   }
105   catch( Standard_Failure ) {
106     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
107     SetErrorCode( aFail->GetMessageString() );
108     return;
109   }
110
111   //Make a Python command
112   GEOM::TPythonDump(aFunction) << "geompy.ExportIGES(" << theOriginal << ", \""
113     << theFileName.ToCString() << "\", \"" << theVersion.ToCString() << "\" )";
114
115   SetErrorCode(OK);
116 }
117
118 //=============================================================================
119 /*!
120  *  ImportIGES
121  *  Import a shape from IGES format
122  *  \param theFileName The name of the file to import
123  *  \return List of GEOM_Objects, containing the created shape and propagation groups.
124  */
125 //=============================================================================
126 Handle(TColStd_HSequenceOfTransient)
127 IGESPlugin_IOperations::ImportIGES( const TCollection_AsciiString& theFileName,
128                                             const bool theIsIgnoreUnits )
129 {
130   SetErrorCode(KO);
131   if( theFileName.IsEmpty() ) return NULL;
132
133   //Add a new result object
134   Handle(GEOM_Object) anImported = GetEngine()->AddObject( GetDocID(), GEOM_IMPORT );
135
136   //Add an Import function
137   Handle(GEOM_Function) aFunction =
138     anImported->AddFunction( IGESPlugin_ImportDriver::GetID(), IMPORT_SHAPE);
139   if (aFunction.IsNull()) return NULL;
140
141   //Check if the function is set correctly
142   if (aFunction->GetDriverGUID() != IGESPlugin_ImportDriver::GetID()) return NULL;
143
144   //Set parameters
145   IGESPlugin_IImport aCI( aFunction );
146   aCI.SetFileName( theFileName );
147   aCI.SetIsIgnoreUnits( theIsIgnoreUnits );
148
149   //Perform the Import
150   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
151
152   try {
153 #if OCC_VERSION_LARGE > 0x06010000
154     OCC_CATCH_SIGNALS;
155 #endif
156     if( !GetSolver()->ComputeFunction( aFunction ) ) {
157       SetErrorCode( "Import driver failed" );
158       return NULL;
159     }
160     aSeq->Append(anImported);
161
162     // Greate material groups.
163     // MakeMaterialGroups( anImported, aSeq );
164   }
165   catch( Standard_Failure ) {
166     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
167     SetErrorCode( aFail->GetMessageString() );
168     return NULL;
169   }
170
171   //Make a Python command
172   GEOM::TPythonDump pd (aFunction);
173   if( theIsIgnoreUnits )
174     pd << aSeq << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\", True)";
175   else
176     pd << aSeq << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\")";
177   SetErrorCode(OK);
178
179   return aSeq;
180 }
181
182 //=============================================================================
183 /*!
184  *  ReadValue
185  */
186 //=============================================================================
187 TCollection_AsciiString IGESPlugin_IOperations::ReadValue
188                                  ( const TCollection_AsciiString& theFileName,
189                                    const TCollection_AsciiString& theParameterName )
190 {
191   SetErrorCode(KO);
192
193   TCollection_AsciiString aValue, anError;
194
195   if (theFileName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
196
197   aValue = IGESPlugin_ImportDriver::GetValue( theFileName, theParameterName, anError );
198
199   if( aValue.IsEmpty() ) {
200     if( anError.IsEmpty() )
201       anError = theFileName + " doesn't contain requested parameter";
202     return aValue;
203   }
204   if (anError.IsEmpty())
205     SetErrorCode(OK);
206   else
207     SetErrorCode(anError.ToCString());
208
209   return aValue;
210 }