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