Salome HOME
cc4f35fc59a08a7b9198127835cc16ede3ea9e61
[modules/geom.git] / src / STEPPlugin / STEPPlugin_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 "STEPPlugin_IOperations.hxx"
22 #include "STEPPlugin_ExportDriver.hxx"
23 #include "STEPPlugin_ImportDriver.hxx"
24 #include "STEPPlugin_IExport.hxx"
25 #include "STEPPlugin_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 STEPPlugin_IOperations::STEPPlugin_IOperations( GEOM_Engine* theEngine )
43 : GEOMImpl_IBaseIEOperations( theEngine )
44 {
45   MESSAGE( "STEPPlugin_IOperations::STEPPlugin_IOperations" );
46 }
47
48 //=============================================================================
49 /*!
50  *  Destructor
51  */
52 //=============================================================================
53 STEPPlugin_IOperations::~STEPPlugin_IOperations()
54 {
55   MESSAGE( "STEPPlugin_IOperations::~STEPPlugin_IOperations" );
56 }
57
58 //=============================================================================
59 /*!
60  *
61  */
62 //=============================================================================
63 static GEOM::TPythonDump& operator<<
64           (GEOM::TPythonDump                        &theDump,
65            const STEPPlugin_IOperations::LengthUnit  theState)
66 {
67   switch (theState) {
68   case STEPPlugin_IOperations::LengthUnit_Inch:
69     theDump << "GEOM.LU_INCH";
70     break;
71   case STEPPlugin_IOperations::LengthUnit_Millimeter:
72     theDump << "GEOM.LU_MILLIMETER";
73     break;
74   case STEPPlugin_IOperations::LengthUnit_Foot:
75     theDump << "GEOM.LU_FOOT";
76     break;
77   case STEPPlugin_IOperations::LengthUnit_Mile:
78     theDump << "GEOM.LU_MILE";
79     break;
80   case STEPPlugin_IOperations::LengthUnit_Meter:
81     theDump << "GEOM.LU_METER";
82     break;
83   case STEPPlugin_IOperations::LengthUnit_Kilometer:
84     theDump << "GEOM.LU_KILOMETER";
85     break;
86   case STEPPlugin_IOperations::LengthUnit_Milliinch:
87     theDump << "GEOM.LU_MILLIINCH";
88     break;
89   case STEPPlugin_IOperations::LengthUnit_Micrometer:
90     theDump << "GEOM.LU_MICROMETER";
91     break;
92   case STEPPlugin_IOperations::LengthUnit_Centimeter:
93     theDump << "GEOM.LU_CENTIMETER";
94     break;
95   case STEPPlugin_IOperations::LengthUnit_Microinch:
96     theDump << "GEOM.LU_MICROINCH";
97     break;
98   default:
99     break;
100   }
101
102   return theDump;
103 }
104
105 //=============================================================================
106 /*!
107  *  ExportSTEP
108  *  Export a shape to STEP format
109  *  \param theOriginal The shape to export
110  *  \param theFileName The name of the file to exported
111  *  \param theUnit the length unit
112  */
113 //=============================================================================
114 void STEPPlugin_IOperations::ExportSTEP
115                     (const Handle(GEOM_Object)      theOriginal,
116                      const TCollection_AsciiString &theFileName,
117                      const LengthUnit               theUnit)
118 {
119   SetErrorCode(KO);
120   if( theOriginal.IsNull() ) return;
121
122   Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
123   if( aRefFunction.IsNull() ) return;  //There is no function which creates an object to be exported
124
125   //Add a new result object
126   Handle(GEOM_Object) result = GetEngine()->AddObject( GEOM_IMPORT);
127
128   //Add an Export function
129   Handle(GEOM_Function) aFunction = result->AddFunction( STEPPlugin_ExportDriver::GetID(), EXPORT_SHAPE );
130   if( aFunction.IsNull() ) return;
131
132   //Check if the function is set correctly
133   if( aFunction->GetDriverGUID() != STEPPlugin_ExportDriver::GetID() ) return;
134
135   //Set parameters
136   STEPPlugin_IExport aCI( aFunction );
137   aCI.SetOriginal( aRefFunction );
138   aCI.SetFileName( theFileName );
139   aCI.SetUnit( theUnit );
140
141   //Perform the Export
142   try {
143     OCC_CATCH_SIGNALS;
144     if( !GetSolver()->ComputeFunction( aFunction ) ) {
145       SetErrorCode( "Not enough space on disk, or you haven't permissions to write this directory" );
146       return;
147     }
148   }
149   catch( Standard_Failure& aFail ) {
150     SetErrorCode( aFail.GetMessageString() );
151     return;
152   }
153
154   //Make a Python command
155   std::string convFileName = Kernel_Utils::BackSlashToSlash(theFileName.ToCString());
156   GEOM::TPythonDump(aFunction) << "geompy.ExportSTEP(" << theOriginal << ", \""
157     << convFileName.c_str() << "\", " << theUnit << " )";
158
159   SetErrorCode(OK);
160 }
161
162 //=============================================================================
163 /*!
164  *  ImportSTEP
165  *  Import a shape from STEP format
166  *  \param theFileName The name of the file to import
167  *  \return List of GEOM_Objects, containing the created shape and propagation groups.
168  */
169 //=============================================================================
170 Handle(TColStd_HSequenceOfTransient)
171 STEPPlugin_IOperations::ImportSTEP(const TCollection_AsciiString& theFileName,
172                                    const bool theIsIgnoreUnits,
173                                    const bool IsCreateAssemblies)
174 {
175   SetErrorCode(KO);
176   if( theFileName.IsEmpty() ) return NULL;
177
178   //Add a new result object
179   Handle(GEOM_Object) anImported = GetEngine()->AddObject( GEOM_IMPORT );
180
181   //Add an Import function
182   Handle(GEOM_Function) aFunction =
183     anImported->AddFunction( STEPPlugin_ImportDriver::GetID(), IMPORT_SHAPE);
184   if (aFunction.IsNull()) return NULL;
185
186   //Check if the function is set correctly
187   if (aFunction->GetDriverGUID() != STEPPlugin_ImportDriver::GetID()) return NULL;
188
189   //Set parameters
190   STEPPlugin_IImport aCI( aFunction );
191   aCI.SetFileName( theFileName );
192   aCI.SetIsIgnoreUnits( theIsIgnoreUnits );
193   aCI.SetIsCreateAssemblies( IsCreateAssemblies );
194
195   //Perform the Import
196   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
197
198   try {
199     OCC_CATCH_SIGNALS;
200     if( !GetSolver()->ComputeFunction( aFunction ) ) {
201       SetErrorCode( "Import driver failed" );
202       return NULL;
203     }
204     aSeq->Append(anImported);
205
206     // Create material groups.
207     MakeMaterialGroups( anImported, aSeq );
208   }
209   catch( Standard_Failure& aFail ) {
210     SetErrorCode( aFail.GetMessageString() );
211     return NULL;
212   }
213
214   //Make a Python command
215   GEOM::TPythonDump pd (aFunction);
216   std::string convFileName =  Kernel_Utils::BackSlashToSlash( theFileName.ToCString() );
217   pd << aSeq << " = geompy.ImportSTEP(\"" << convFileName.c_str() << "\", ";
218   pd << (theIsIgnoreUnits ? "True" : "False");
219   pd << ", " << (IsCreateAssemblies ? "True" : "False");
220   pd << ")";
221   SetErrorCode(OK);
222
223   return aSeq;
224 }
225
226 //=============================================================================
227 /*!
228  *  ReadValue
229  */
230 //=============================================================================
231 TCollection_AsciiString
232 STEPPlugin_IOperations::ReadValue( const TCollection_AsciiString& theFileName,
233                                    const TCollection_AsciiString& theParameterName )
234 {
235   SetErrorCode(KO);
236
237   TCollection_AsciiString aValue, anError;
238
239   if (theFileName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
240
241   aValue = STEPPlugin_ImportDriver::GetValue( theFileName, theParameterName, anError );
242
243   if( aValue.IsEmpty() ) {
244     if( anError.IsEmpty() )
245       anError = theFileName + " doesn't contain requested parameter";
246     return aValue;
247   }
248   if (anError.IsEmpty())
249     SetErrorCode(OK);
250   else
251     SetErrorCode(anError.ToCString());
252
253   return aValue;
254 }