Salome HOME
EDF 2281 : Add 2 primitives for hexa mesh
[modules/geom.git] / src / GEOM_I / GEOM_IInsertOperations_i.cc
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21
22 #ifdef WNT
23 #pragma warning( disable:4786 )
24 #endif
25
26 #include <Standard_Stream.hxx>
27
28 #include "GEOM_IInsertOperations_i.hh"
29
30 #include "utilities.h"
31 #include "OpUtil.hxx"
32 #include "Utils_ExceptHandlers.hxx"
33
34 #include "GEOM_Engine.hxx"
35 #include "GEOM_Object.hxx"
36
37 #include <Basics_OCCTVersion.hxx>
38
39 #include <TColStd_HSequenceOfAsciiString.hxx>
40
41 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
42 #include <TColStd_HArray1OfByte.hxx>
43 #else
44 #include <TDataStd_HArray1OfByte.hxx>
45 #endif
46
47 //=============================================================================
48 /*!
49  *  constructor
50  */
51 //=============================================================================
52 GEOM_IInsertOperations_i::GEOM_IInsertOperations_i (PortableServer::POA_ptr thePOA,
53                                                     GEOM::GEOM_Gen_ptr theEngine,
54                                                     ::GEOMImpl_IInsertOperations* theImpl)
55      :GEOM_IOperations_i(thePOA, theEngine, theImpl)
56 {
57   MESSAGE("GEOM_IInsertOperations_i::GEOM_IInsertOperations_i");
58 }
59
60 //=============================================================================
61 /*!
62  *  destructor
63  */
64 //=============================================================================
65 GEOM_IInsertOperations_i::~GEOM_IInsertOperations_i()
66 {
67   MESSAGE("GEOM_IInsertOperations_i::~GEOM_IInsertOperations_i");
68 }
69
70
71 //=============================================================================
72 /*!
73  *  MakeCopy
74  */
75 //=============================================================================
76 GEOM::GEOM_Object_ptr GEOM_IInsertOperations_i::MakeCopy(GEOM::GEOM_Object_ptr theOriginal)
77 {
78   GEOM::GEOM_Object_var aGEOMObject;
79
80   //Set a not done flag
81   GetOperations()->SetNotDone();
82
83   //Get the reference shape
84   Handle(GEOM_Object) anOriginal = GetObjectImpl(theOriginal);
85   if (anOriginal.IsNull()) return aGEOMObject._retn();
86
87   //Create the copy
88   Handle(GEOM_Object) anObject = GetOperations()->MakeCopy(anOriginal);
89
90   if (!GetOperations()->IsDone() || anObject.IsNull()) return aGEOMObject._retn();
91
92   return GetObject(anObject);
93 }
94
95 //=============================================================================
96 /*!
97  *  Export
98  */
99 //=============================================================================
100 void GEOM_IInsertOperations_i::Export
101                    (GEOM::GEOM_Object_ptr theOriginal,
102                     const char*           theFileName,
103                     const char*           theFormatName)
104 {
105   GEOM::GEOM_Object_var aGEOMObject = GEOM::GEOM_Object::_duplicate(theOriginal);
106
107   //Set a not done flag
108   GetOperations()->SetNotDone();
109
110   //Get the reference shape
111   Handle(GEOM_Object) anOriginal = GetObjectImpl(theOriginal);
112   if (anOriginal.IsNull()) return;
113
114   //Export the shape to the file
115   char* aFileName   = strdup(theFileName);
116   char* aFormatName = strdup(theFormatName);
117   GetOperations()->Export(anOriginal, aFileName, aFormatName);
118   free(aFileName);
119   free(aFormatName);
120 }
121
122 //=============================================================================
123 /*!
124  *  ImportFile
125  */
126 //=============================================================================
127 GEOM::GEOM_Object_ptr GEOM_IInsertOperations_i::ImportFile
128                    (const char* theFileName,
129                     const char* theFormatName)
130 {
131   GEOM::GEOM_Object_var aGEOMObject;
132
133   //Set a not done flag
134   GetOperations()->SetNotDone();
135
136   //Import the shape from the file
137   char* aFileName   = strdup(theFileName);
138   char* aFormatName = strdup(theFormatName);
139   Handle(GEOM_Object) anObject = GetOperations()->Import(aFileName, aFormatName);
140
141   if( strcmp(aFormatName,"IGES_UNIT")==0 && !anObject.IsNull() ) {
142     free(aFileName);
143     free(aFormatName);
144     return GetObject(anObject);
145   }
146
147   free(aFileName);
148   free(aFormatName);
149
150   if (!GetOperations()->IsDone() || anObject.IsNull()) {
151     return aGEOMObject._retn();
152   }
153
154   return GetObject(anObject);
155 }
156
157 //=============================================================================
158 /*!
159  *  ReadValue
160  */
161 //=============================================================================
162 char* GEOM_IInsertOperations_i::ReadValue(const char* theFileName,
163                                           const char* theFormatName,
164                                           const char* theParameterName)
165 {
166   //Set a not done flag
167   GetOperations()->SetNotDone();
168
169   char* aFileName      = strdup(theFileName);
170   char* aFormatName    = strdup(theFormatName);
171   char* aParameterName = strdup(theParameterName);
172
173   TCollection_AsciiString aUnits = GetOperations()->ReadValue
174     (aFileName, aFormatName, aParameterName);
175
176   free(aFileName);
177   free(aFormatName);
178   free(aParameterName);
179
180   return CORBA::string_dup(aUnits.ToCString());
181 }
182
183 //=============================================================================
184 /*!
185  *  ImportTranslators
186  */
187 //=============================================================================
188 void GEOM_IInsertOperations_i::ImportTranslators
189   (GEOM::string_array_out theFormats, GEOM::string_array_out thePatterns)
190 {
191   // allocate the CORBA arrays
192   GEOM::string_array_var aFormatsArray  = new GEOM::string_array();
193   GEOM::string_array_var aPatternsArray = new GEOM::string_array();
194
195   // Get sequences of available formats
196   Handle(TColStd_HSequenceOfAsciiString) aFormats  = new TColStd_HSequenceOfAsciiString;
197   Handle(TColStd_HSequenceOfAsciiString) aPatterns = new TColStd_HSequenceOfAsciiString;
198   if (GetOperations()->ImportTranslators(aFormats, aPatterns)) {
199     const int formSize = aFormats->Length();
200     if (formSize == aPatterns->Length()) {
201       aFormatsArray->length(formSize);
202       aPatternsArray->length(formSize);
203
204       // fill the local CORBA arrays with values from sequences
205       CORBA::Long i = 1;
206       for (; i <= formSize; i++) {
207         aFormatsArray[i-1]  = CORBA::string_dup(aFormats->Value(i).ToCString());
208         aPatternsArray[i-1] = CORBA::string_dup(aPatterns->Value(i).ToCString());
209       }
210     }
211   }
212
213   // initialize out-parameters with local arrays
214   theFormats  = aFormatsArray._retn();
215   thePatterns = aPatternsArray._retn();
216 }
217
218 //=============================================================================
219 /*!
220  *  ExportTranslators
221  */
222 //=============================================================================
223 void GEOM_IInsertOperations_i::ExportTranslators
224   (GEOM::string_array_out theFormats, GEOM::string_array_out thePatterns)
225 {
226   // allocate the CORBA arrays
227   GEOM::string_array_var aFormatsArray  = new GEOM::string_array();
228   GEOM::string_array_var aPatternsArray = new GEOM::string_array();
229
230   // Get sequences of available formats
231   Handle(TColStd_HSequenceOfAsciiString) aFormats  = new TColStd_HSequenceOfAsciiString;
232   Handle(TColStd_HSequenceOfAsciiString) aPatterns = new TColStd_HSequenceOfAsciiString;
233   if (GetOperations()->ExportTranslators(aFormats, aPatterns)) {
234     const int formSize = aFormats->Length();
235     if (formSize == aPatterns->Length()) {
236       aFormatsArray->length(formSize);
237       aPatternsArray->length(formSize);
238
239       // fill the local CORBA arrays with values from sequences
240       CORBA::Long i = 1;
241       for (; i <= formSize; i++) {
242         aFormatsArray[i-1]  = CORBA::string_dup(aFormats->Value(i).ToCString());
243         aPatternsArray[i-1] = CORBA::string_dup(aPatterns->Value(i).ToCString());
244       }
245     }
246   }
247
248   // initialize out-parameters with local arrays
249   theFormats  = aFormatsArray._retn();
250   thePatterns = aPatternsArray._retn();
251 }
252
253 //=============================================================================
254 /*!
255  *  RestoreShape
256  */
257 //=============================================================================
258 GEOM::GEOM_Object_ptr GEOM_IInsertOperations_i::RestoreShape (const SALOMEDS::TMPFile& theStream)
259 {
260   GEOM::GEOM_Object_var aGEOMObject;
261
262   //Set a not done flag
263   GetOperations()->SetNotDone();
264
265   if (theStream.length() < 1)
266     return aGEOMObject._retn();
267
268   char* buf = (char*)theStream.NP_data();
269   std::istringstream aStream (buf);
270
271   Handle(GEOM_Object) anObject = GetOperations()->RestoreShape(aStream);
272   if (!GetOperations()->IsDone() || anObject.IsNull())
273     return aGEOMObject._retn();
274
275   return GetObject(anObject);
276 }
277
278 //=============================================================================
279 /*!
280  *  LoadTexture
281  */
282 //=============================================================================
283 CORBA::Long GEOM_IInsertOperations_i::LoadTexture(const char* theTextureFile)
284 {
285   GetOperations()->SetNotDone();
286   return GetOperations()->LoadTexture( theTextureFile );
287 }
288
289 //=============================================================================
290 /*!
291  *  AddTexture
292  */
293 //=============================================================================
294 CORBA::Long GEOM_IInsertOperations_i::AddTexture(CORBA::Long theWidth, CORBA::Long theHeight,
295                                                  const SALOMEDS::TMPFile& theTexture)
296 {
297   GetOperations()->SetNotDone();
298
299 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
300   Handle(TColStd_HArray1OfByte) aTexture;
301 #else
302   Handle(TDataStd_HArray1OfByte) aTexture;
303 #endif
304
305   if ( theTexture.length() > 0 ) {
306 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
307     aTexture = new TColStd_HArray1OfByte (1, theTexture.length());
308 #else
309     aTexture = new TDataStd_HArray1OfByte (1, theTexture.length());
310 #endif
311
312     for ( int i = 0; i < theTexture.length(); i++ )
313       aTexture->SetValue( i+1, (Standard_Byte)theTexture[i] );
314   }
315   return GetOperations()->AddTexture( theWidth, theHeight, aTexture );
316 }
317
318 //=============================================================================
319 /*!
320  *  GetTexture
321  */
322 //=============================================================================
323 SALOMEDS::TMPFile* GEOM_IInsertOperations_i::GetTexture(CORBA::Long theID,
324                                                         CORBA::Long& theWidth,
325                                                         CORBA::Long& theHeight)
326 {
327   int aWidth, aHeight;
328 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
329   Handle(TColStd_HArray1OfByte) aTextureImpl =
330 #else
331   Handle(TDataStd_HArray1OfByte) aTextureImpl =
332 #endif
333     GetOperations()->GetTexture(theID, aWidth, aHeight);
334   theWidth  = aWidth;
335   theHeight = aHeight;
336   SALOMEDS::TMPFile_var aTexture;
337   if ( !aTextureImpl.IsNull() ) {
338     aTexture = new SALOMEDS::TMPFile;
339     aTexture->length( aTextureImpl->Length() );
340     for ( int i = aTextureImpl->Lower(); i <= aTextureImpl->Upper(); i++ )
341       aTexture[i-aTextureImpl->Lower()] = aTextureImpl->Value( i );
342   }
343   return aTexture._retn();
344 }
345
346 //=============================================================================
347 /*!
348  *  GetAllTextures
349  */
350 //=============================================================================
351 GEOM::ListOfLong* GEOM_IInsertOperations_i::GetAllTextures()
352 {
353   std::list<int> localIDs = GetOperations()->GetAllTextures();
354   GEOM::ListOfLong_var anIDs = new GEOM::ListOfLong(localIDs.size());
355   anIDs->length(localIDs.size());
356   std::list<int>::const_iterator anIt;
357   int i = 0;
358   for( anIt = localIDs.begin(); anIt != localIDs.end(); ++anIt, i++)
359     anIDs[i] = *anIt;
360   return anIDs._retn();
361 }