Salome HOME
Update copyrights 2014.
[modules/geom.git] / src / GEOM_I / GEOM_IInsertOperations_i.cc
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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
23 #ifdef WIN32
24 #pragma warning( disable:4786 )
25 #endif
26
27 #include <Standard_Stream.hxx>
28
29 #include "GEOM_IInsertOperations_i.hh"
30
31 #include "utilities.h"
32 #include "OpUtil.hxx"
33 #include "Utils_ExceptHandlers.hxx"
34
35 #include "GEOM_Engine.hxx"
36 #include "GEOM_Object.hxx"
37
38 #include <Basics_OCCTVersion.hxx>
39
40 #include <TColStd_HSequenceOfAsciiString.hxx>
41
42 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
43 #include <TColStd_HArray1OfByte.hxx>
44 #else
45 #include <TDataStd_HArray1OfByte.hxx>
46 #endif
47
48 //=============================================================================
49 /*!
50  *  constructor
51  */
52 //=============================================================================
53 GEOM_IInsertOperations_i::GEOM_IInsertOperations_i (PortableServer::POA_ptr thePOA,
54                                                     GEOM::GEOM_Gen_ptr theEngine,
55                                                     ::GEOMImpl_IInsertOperations* theImpl)
56      :GEOM_IOperations_i(thePOA, theEngine, theImpl)
57 {
58   MESSAGE("GEOM_IInsertOperations_i::GEOM_IInsertOperations_i");
59 }
60
61 //=============================================================================
62 /*!
63  *  destructor
64  */
65 //=============================================================================
66 GEOM_IInsertOperations_i::~GEOM_IInsertOperations_i()
67 {
68   MESSAGE("GEOM_IInsertOperations_i::~GEOM_IInsertOperations_i");
69 }
70
71
72 //=============================================================================
73 /*!
74  *  MakeCopy
75  */
76 //=============================================================================
77 GEOM::GEOM_Object_ptr GEOM_IInsertOperations_i::MakeCopy(GEOM::GEOM_Object_ptr theOriginal)
78 {
79   GEOM::GEOM_Object_var aGEOMObject;
80
81   //Set a not done flag
82   GetOperations()->SetNotDone();
83
84   //Get the reference shape
85   Handle(GEOM_Object) anOriginal = GetObjectImpl(theOriginal);
86   if (anOriginal.IsNull()) return aGEOMObject._retn();
87
88   //Create the copy
89   Handle(GEOM_Object) anObject = GetOperations()->MakeCopy(anOriginal);
90
91   if (!GetOperations()->IsDone() || anObject.IsNull()) return aGEOMObject._retn();
92
93   return GetObject(anObject);
94 }
95
96 //=============================================================================
97 /*!
98  *  Export
99  */
100 //=============================================================================
101 void GEOM_IInsertOperations_i::Export
102                    (GEOM::GEOM_Object_ptr theOriginal,
103                     const char*           theFileName,
104                     const char*           theFormatName)
105 {
106   GEOM::GEOM_Object_var aGEOMObject = GEOM::GEOM_Object::_duplicate(theOriginal);
107
108   //Set a not done flag
109   GetOperations()->SetNotDone();
110
111   //Get the reference shape
112   Handle(GEOM_Object) anOriginal = GetObjectImpl(theOriginal);
113   if (anOriginal.IsNull()) return;
114
115   //Export the shape to the file
116   char* aFileName   = strdup(theFileName);
117   char* aFormatName = strdup(theFormatName);
118   GetOperations()->Export(anOriginal, aFileName, aFormatName);
119   free(aFileName);
120   free(aFormatName);
121 }
122
123 //=============================================================================
124 /*!
125  *  ImportFile
126  */
127 //=============================================================================
128 GEOM::GEOM_Object_ptr GEOM_IInsertOperations_i::ImportFile
129                    (const char* theFileName,
130                     const char* theFormatName)
131 {
132   GEOM::GEOM_Object_var aGEOMObject;
133
134   //Set a not done flag
135   GetOperations()->SetNotDone();
136
137   //Import the shape from the file
138   char* aFileName   = strdup(theFileName);
139   char* aFormatName = strdup(theFormatName);
140   Handle(GEOM_Object) anObject = GetOperations()->Import(aFileName, aFormatName);
141
142   if( strcmp(aFormatName,"IGES_UNIT")==0 && !anObject.IsNull() ) {
143     free(aFileName);
144     free(aFormatName);
145     return GetObject(anObject);
146   }
147
148   free(aFileName);
149   free(aFormatName);
150
151   if (!GetOperations()->IsDone() || anObject.IsNull()) {
152     return aGEOMObject._retn();
153   }
154
155   return GetObject(anObject);
156 }
157
158 //=============================================================================
159 /*!
160  *  ReadValue
161  */
162 //=============================================================================
163 char* GEOM_IInsertOperations_i::ReadValue(const char* theFileName,
164                                           const char* theFormatName,
165                                           const char* theParameterName)
166 {
167   //Set a not done flag
168   GetOperations()->SetNotDone();
169
170   char* aFileName      = strdup(theFileName);
171   char* aFormatName    = strdup(theFormatName);
172   char* aParameterName = strdup(theParameterName);
173
174   TCollection_AsciiString aUnits = GetOperations()->ReadValue
175     (aFileName, aFormatName, aParameterName);
176
177   free(aFileName);
178   free(aFormatName);
179   free(aParameterName);
180
181   return CORBA::string_dup(aUnits.ToCString());
182 }
183
184 //=============================================================================
185 /*!
186  *  ImportTranslators
187  */
188 //=============================================================================
189 void GEOM_IInsertOperations_i::ImportTranslators
190   (GEOM::string_array_out theFormats, GEOM::string_array_out thePatterns)
191 {
192   // allocate the CORBA arrays
193   GEOM::string_array_var aFormatsArray  = new GEOM::string_array();
194   GEOM::string_array_var aPatternsArray = new GEOM::string_array();
195
196   // Get sequences of available formats
197   Handle(TColStd_HSequenceOfAsciiString) aFormats  = new TColStd_HSequenceOfAsciiString;
198   Handle(TColStd_HSequenceOfAsciiString) aPatterns = new TColStd_HSequenceOfAsciiString;
199   if (GetOperations()->ImportTranslators(aFormats, aPatterns)) {
200     const int formSize = aFormats->Length();
201     if (formSize == aPatterns->Length()) {
202       aFormatsArray->length(formSize);
203       aPatternsArray->length(formSize);
204
205       // fill the local CORBA arrays with values from sequences
206       CORBA::Long i = 1;
207       for (; i <= formSize; i++) {
208         aFormatsArray[i-1]  = CORBA::string_dup(aFormats->Value(i).ToCString());
209         aPatternsArray[i-1] = CORBA::string_dup(aPatterns->Value(i).ToCString());
210       }
211     }
212   }
213
214   // initialize out-parameters with local arrays
215   theFormats  = aFormatsArray._retn();
216   thePatterns = aPatternsArray._retn();
217 }
218
219 //=============================================================================
220 /*!
221  *  ExportTranslators
222  */
223 //=============================================================================
224 void GEOM_IInsertOperations_i::ExportTranslators
225   (GEOM::string_array_out theFormats, GEOM::string_array_out thePatterns)
226 {
227   // allocate the CORBA arrays
228   GEOM::string_array_var aFormatsArray  = new GEOM::string_array();
229   GEOM::string_array_var aPatternsArray = new GEOM::string_array();
230
231   // Get sequences of available formats
232   Handle(TColStd_HSequenceOfAsciiString) aFormats  = new TColStd_HSequenceOfAsciiString;
233   Handle(TColStd_HSequenceOfAsciiString) aPatterns = new TColStd_HSequenceOfAsciiString;
234   if (GetOperations()->ExportTranslators(aFormats, aPatterns)) {
235     const int formSize = aFormats->Length();
236     if (formSize == aPatterns->Length()) {
237       aFormatsArray->length(formSize);
238       aPatternsArray->length(formSize);
239
240       // fill the local CORBA arrays with values from sequences
241       CORBA::Long i = 1;
242       for (; i <= formSize; i++) {
243         aFormatsArray[i-1]  = CORBA::string_dup(aFormats->Value(i).ToCString());
244         aPatternsArray[i-1] = CORBA::string_dup(aPatterns->Value(i).ToCString());
245       }
246     }
247   }
248
249   // initialize out-parameters with local arrays
250   theFormats  = aFormatsArray._retn();
251   thePatterns = aPatternsArray._retn();
252 }
253
254 //=============================================================================
255 /*!
256  *  RestoreShape
257  */
258 //=============================================================================
259 GEOM::GEOM_Object_ptr GEOM_IInsertOperations_i::RestoreShape (const SALOMEDS::TMPFile& theStream)
260 {
261   GEOM::GEOM_Object_var aGEOMObject;
262
263   //Set a not done flag
264   GetOperations()->SetNotDone();
265
266   if (theStream.length() < 1)
267     return aGEOMObject._retn();
268
269   char* buf = (char*)theStream.NP_data();
270   std::istringstream aStream (buf);
271
272   Handle(GEOM_Object) anObject = GetOperations()->RestoreShape(aStream);
273   if (!GetOperations()->IsDone() || anObject.IsNull())
274     return aGEOMObject._retn();
275
276   return GetObject(anObject);
277 }
278
279 //=============================================================================
280 /*!
281  *  LoadTexture
282  */
283 //=============================================================================
284 CORBA::Long GEOM_IInsertOperations_i::LoadTexture(const char* theTextureFile)
285 {
286   GetOperations()->SetNotDone();
287   return GetOperations()->LoadTexture( theTextureFile );
288 }
289
290 //=============================================================================
291 /*!
292  *  AddTexture
293  */
294 //=============================================================================
295 CORBA::Long GEOM_IInsertOperations_i::AddTexture(CORBA::Long theWidth, CORBA::Long theHeight,
296                                                  const SALOMEDS::TMPFile& theTexture)
297 {
298   GetOperations()->SetNotDone();
299
300 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
301   Handle(TColStd_HArray1OfByte) aTexture;
302 #else
303   Handle(TDataStd_HArray1OfByte) aTexture;
304 #endif
305
306   if ( theTexture.length() > 0 ) {
307 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
308     aTexture = new TColStd_HArray1OfByte (1, theTexture.length());
309 #else
310     aTexture = new TDataStd_HArray1OfByte (1, theTexture.length());
311 #endif
312
313     for ( int i = 0; i < theTexture.length(); i++ )
314       aTexture->SetValue( i+1, (Standard_Byte)theTexture[i] );
315   }
316   return GetOperations()->AddTexture( theWidth, theHeight, aTexture );
317 }
318
319 //=============================================================================
320 /*!
321  *  GetTexture
322  */
323 //=============================================================================
324 SALOMEDS::TMPFile* GEOM_IInsertOperations_i::GetTexture(CORBA::Long theID,
325                                                         CORBA::Long& theWidth,
326                                                         CORBA::Long& theHeight)
327 {
328   int aWidth, aHeight;
329 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
330   Handle(TColStd_HArray1OfByte) aTextureImpl =
331 #else
332   Handle(TDataStd_HArray1OfByte) aTextureImpl =
333 #endif
334     GetOperations()->GetTexture(theID, aWidth, aHeight);
335   theWidth  = aWidth;
336   theHeight = aHeight;
337   SALOMEDS::TMPFile_var aTexture;
338   if ( !aTextureImpl.IsNull() ) {
339     aTexture = new SALOMEDS::TMPFile;
340     aTexture->length( aTextureImpl->Length() );
341     for ( int i = aTextureImpl->Lower(); i <= aTextureImpl->Upper(); i++ )
342       aTexture[i-aTextureImpl->Lower()] = aTextureImpl->Value( i );
343   }
344   return aTexture._retn();
345 }
346
347 //=============================================================================
348 /*!
349  *  GetAllTextures
350  */
351 //=============================================================================
352 GEOM::ListOfLong* GEOM_IInsertOperations_i::GetAllTextures()
353 {
354   std::list<int> localIDs = GetOperations()->GetAllTextures();
355   GEOM::ListOfLong_var anIDs = new GEOM::ListOfLong(localIDs.size());
356   anIDs->length(localIDs.size());
357   std::list<int>::const_iterator anIt;
358   int i = 0;
359   for( anIt = localIDs.begin(); anIt != localIDs.end(); ++anIt, i++)
360     anIDs[i] = *anIt;
361   return anIDs._retn();
362 }
363
364 //=============================================================================
365 /*!
366  *  Export a shape to XAO format
367  *  \param shape The shape to export
368  *  \param groups The list of groups to export
369  *  \param fields The list of fields to export
370  *  \param author The author of the export
371  *  \param fileName The name of the exported file
372  *  \return boolean indicating if export was succeful.
373  */
374 //=============================================================================
375 CORBA::Boolean GEOM_IInsertOperations_i::ExportXAO(GEOM::GEOM_Object_ptr shape,
376                                                    const GEOM::ListOfGO& groups,
377                                                    const GEOM::ListOfFields&  fields,
378                                                    const char* author,
379                                                    const char* fileName)
380 {
381   bool isGood = false;
382   // Set a not done flag
383   GetOperations()->SetNotDone();
384   
385   // Get the reference shape
386   Handle(GEOM_Object) reference = GetObjectImpl(shape);
387   
388   // Get the reference groups
389   int ind = 0;
390   std::list<Handle(GEOM_Object)> groupsObj;
391   for (; ind < groups.length(); ind++)
392   {
393     Handle(GEOM_Object) gobj = GetObjectImpl(groups[ind]);
394     if (gobj.IsNull()) return false;
395     groupsObj.push_back(gobj);
396   }
397
398   // Get the reference fields
399   ind = 0;
400   std::list<Handle(GEOM_Field)> fieldsObj;
401   for (; ind < fields.length(); ind++)
402   {
403     Handle(GEOM_Field) fobj = Handle(GEOM_Field)::DownCast(GetBaseObjectImpl(fields[ind]));
404     if (fobj.IsNull()) return false;
405     fieldsObj.push_back(fobj);
406   }
407   
408   if (!reference.IsNull())
409   {
410     // Export XAO
411     isGood = GetOperations()->ExportXAO(reference, groupsObj, fieldsObj, author, fileName);
412   }
413   
414   return isGood;
415 }
416
417 //=============================================================================
418 /*!
419  *  Import a shape from XAO format
420  *  \param fileName The name of the file to import
421  *  \param shape The imported shape
422  *  \param subShapes The list of imported subShapes
423  *  \param groups The list of imported groups
424  *  \param fields The list of imported fields
425  *  \return boolean indicating if import was succeful.
426  */
427 //=============================================================================
428 CORBA::Boolean GEOM_IInsertOperations_i::ImportXAO(const char* fileName,
429                                                    GEOM::GEOM_Object_out shape,
430                                                    GEOM::ListOfGO_out subShapes,
431                                                    GEOM::ListOfGO_out groups,
432                                                    GEOM::ListOfFields_out fields)
433 {
434   GEOM::GEOM_Object_var vshape;
435   shape = vshape._retn();
436   
437   subShapes = new GEOM::ListOfGO;
438   groups = new GEOM::ListOfGO;
439   fields = new GEOM::ListOfFields;
440   
441   // Set a not done flag
442   GetOperations()->SetNotDone();
443   
444   Handle(TColStd_HSequenceOfTransient) importedSubShapes = new TColStd_HSequenceOfTransient();
445   Handle(TColStd_HSequenceOfTransient) importedGroups = new TColStd_HSequenceOfTransient();
446   Handle(TColStd_HSequenceOfTransient) importedFields = new TColStd_HSequenceOfTransient();
447   Handle(GEOM_Object) hshape;
448   bool res = GetOperations()->ImportXAO(fileName, hshape, importedSubShapes, importedGroups, importedFields);
449   
450   if (!GetOperations()->IsDone() || !res)
451     return false;
452   
453   // parse fields
454   int n = importedSubShapes->Length();
455   subShapes->length(n);
456   for (int i = 1; i <= n; i++)
457   {
458     (*subShapes)[i - 1] = GetObject(Handle(GEOM_Object)::DownCast(importedSubShapes->Value(i)));
459   }
460   
461   // parse groups
462   n = importedGroups->Length();
463   groups->length(n);
464   for (int i = 1; i <= n; i++)
465   {
466     (*groups)[i - 1] = GetObject(Handle(GEOM_Object)::DownCast(importedGroups->Value(i)));
467   }
468   
469   // parse fields
470   n = importedFields->Length();
471   fields->length(n);
472   for (int i = 1; i <= n; i++)
473   {
474         (*fields)[i - 1] = GEOM::GEOM_Field::_narrow(
475                 GetBaseObject(Handle(GEOM_Field)::DownCast(importedFields->Value(i))));
476   }
477   
478   shape = GetObject(hshape);
479   
480   return res;
481 }