Salome HOME
0023505: Sigsegv with fuse on cylinder and cone
[modules/geom.git] / src / STEPPlugin / STEPPlugin_IOperations.cxx
1 // Copyright (C) 2014-2016  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                                    const bool IsCreateAssemblies)
173 {
174   SetErrorCode(KO);
175   if( theFileName.IsEmpty() ) return NULL;
176
177   //Add a new result object
178   Handle(GEOM_Object) anImported = GetEngine()->AddObject( GetDocID(), GEOM_IMPORT );
179
180   //Add an Import function
181   Handle(GEOM_Function) aFunction =
182     anImported->AddFunction( STEPPlugin_ImportDriver::GetID(), IMPORT_SHAPE);
183   if (aFunction.IsNull()) return NULL;
184
185   //Check if the function is set correctly
186   if (aFunction->GetDriverGUID() != STEPPlugin_ImportDriver::GetID()) return NULL;
187
188   //Set parameters
189   STEPPlugin_IImport aCI( aFunction );
190   aCI.SetFileName( theFileName );
191   aCI.SetIsIgnoreUnits( theIsIgnoreUnits );
192   aCI.SetIsCreateAssemblies( IsCreateAssemblies );
193
194   //Perform the Import
195   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
196
197   try {
198     OCC_CATCH_SIGNALS;
199     if( !GetSolver()->ComputeFunction( aFunction ) ) {
200       SetErrorCode( "Import driver failed" );
201       return NULL;
202     }
203     aSeq->Append(anImported);
204
205     // Greate material groups.
206     MakeMaterialGroups( anImported, aSeq );
207   }
208   catch( Standard_Failure ) {
209     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
210     SetErrorCode( aFail->GetMessageString() );
211     return NULL;
212   }
213
214   //Make a Python command
215   GEOM::TPythonDump pd (aFunction);
216   pd << aSeq << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\", ";
217   pd << (theIsIgnoreUnits ? "True" : "False");
218   pd << ", " << (IsCreateAssemblies ? "True" : "False");
219   pd << ")";
220   SetErrorCode(OK);
221
222   return aSeq;
223 }
224
225 //=============================================================================
226 /*!
227  *  ReadValue
228  */
229 //=============================================================================
230 TCollection_AsciiString
231 STEPPlugin_IOperations::ReadValue( const TCollection_AsciiString& theFileName,
232                                    const TCollection_AsciiString& theParameterName )
233 {
234   SetErrorCode(KO);
235
236   TCollection_AsciiString aValue, anError;
237
238   if (theFileName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
239
240   aValue = STEPPlugin_ImportDriver::GetValue( theFileName, theParameterName, anError );
241
242   if( aValue.IsEmpty() ) {
243     if( anError.IsEmpty() )
244       anError = theFileName + " doesn't contain requested parameter";
245     return aValue;
246   }
247   if (anError.IsEmpty())
248     SetErrorCode(OK);
249   else
250     SetErrorCode(anError.ToCString());
251
252   return aValue;
253 }