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