Salome HOME
0022490: [CEA 1057] Default tick of "Detect self-intersections" in Partition
[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::ListOfGO* GEOM_IInsertOperations_i::ImportFile
129                    (const char* theFileName,
130                     const char* theFormatName)
131 {
132   GEOM::ListOfGO_var aSeq = new GEOM::ListOfGO;
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(TColStd_HSequenceOfTransient) aHSeq = GetOperations()->Import(aFileName, aFormatName);
141
142   if( strcmp(aFormatName,"IGES_UNIT")==0 && !aHSeq.IsNull() ) {
143     free(aFileName);
144     free(aFormatName);
145     return aSeq._retn();
146   }
147
148   free(aFileName);
149   free(aFormatName);
150
151   if (!GetOperations()->IsDone() || aHSeq.IsNull()) {
152     return aSeq._retn();
153   }
154
155   // Copy created objects.
156   Standard_Integer aLength = aHSeq->Length();
157
158   aSeq->length(aLength);
159
160   for (Standard_Integer i = 1; i <= aLength; i++) {
161     aSeq[i-1] = GetObject(Handle(GEOM_Object)::DownCast(aHSeq->Value(i)));
162   }
163
164   return aSeq._retn();
165 }
166
167 //=============================================================================
168 /*!
169  *  ReadValue
170  */
171 //=============================================================================
172 char* GEOM_IInsertOperations_i::ReadValue(const char* theFileName,
173                                           const char* theFormatName,
174                                           const char* theParameterName)
175 {
176   //Set a not done flag
177   GetOperations()->SetNotDone();
178
179   char* aFileName      = strdup(theFileName);
180   char* aFormatName    = strdup(theFormatName);
181   char* aParameterName = strdup(theParameterName);
182
183   TCollection_AsciiString aUnits = GetOperations()->ReadValue
184     (aFileName, aFormatName, aParameterName);
185
186   free(aFileName);
187   free(aFormatName);
188   free(aParameterName);
189
190   return CORBA::string_dup(aUnits.ToCString());
191 }
192
193 //=============================================================================
194 /*!
195  *  ImportTranslators
196  */
197 //=============================================================================
198 void GEOM_IInsertOperations_i::ImportTranslators
199   (GEOM::string_array_out theFormats, GEOM::string_array_out thePatterns)
200 {
201   // allocate the CORBA arrays
202   GEOM::string_array_var aFormatsArray  = new GEOM::string_array();
203   GEOM::string_array_var aPatternsArray = new GEOM::string_array();
204
205   // Get sequences of available formats
206   Handle(TColStd_HSequenceOfAsciiString) aFormats  = new TColStd_HSequenceOfAsciiString;
207   Handle(TColStd_HSequenceOfAsciiString) aPatterns = new TColStd_HSequenceOfAsciiString;
208   if (GetOperations()->ImportTranslators(aFormats, aPatterns)) {
209     const int formSize = aFormats->Length();
210     if (formSize == aPatterns->Length()) {
211       aFormatsArray->length(formSize);
212       aPatternsArray->length(formSize);
213
214       // fill the local CORBA arrays with values from sequences
215       CORBA::Long i = 1;
216       for (; i <= formSize; i++) {
217         aFormatsArray[i-1]  = CORBA::string_dup(aFormats->Value(i).ToCString());
218         aPatternsArray[i-1] = CORBA::string_dup(aPatterns->Value(i).ToCString());
219       }
220     }
221   }
222
223   // initialize out-parameters with local arrays
224   theFormats  = aFormatsArray._retn();
225   thePatterns = aPatternsArray._retn();
226 }
227
228 //=============================================================================
229 /*!
230  *  ExportTranslators
231  */
232 //=============================================================================
233 void GEOM_IInsertOperations_i::ExportTranslators
234   (GEOM::string_array_out theFormats, GEOM::string_array_out thePatterns)
235 {
236   // allocate the CORBA arrays
237   GEOM::string_array_var aFormatsArray  = new GEOM::string_array();
238   GEOM::string_array_var aPatternsArray = new GEOM::string_array();
239
240   // Get sequences of available formats
241   Handle(TColStd_HSequenceOfAsciiString) aFormats  = new TColStd_HSequenceOfAsciiString;
242   Handle(TColStd_HSequenceOfAsciiString) aPatterns = new TColStd_HSequenceOfAsciiString;
243   if (GetOperations()->ExportTranslators(aFormats, aPatterns)) {
244     const int formSize = aFormats->Length();
245     if (formSize == aPatterns->Length()) {
246       aFormatsArray->length(formSize);
247       aPatternsArray->length(formSize);
248
249       // fill the local CORBA arrays with values from sequences
250       CORBA::Long i = 1;
251       for (; i <= formSize; i++) {
252         aFormatsArray[i-1]  = CORBA::string_dup(aFormats->Value(i).ToCString());
253         aPatternsArray[i-1] = CORBA::string_dup(aPatterns->Value(i).ToCString());
254       }
255     }
256   }
257
258   // initialize out-parameters with local arrays
259   theFormats  = aFormatsArray._retn();
260   thePatterns = aPatternsArray._retn();
261 }
262
263 //=============================================================================
264 /*!
265  *  RestoreShape
266  */
267 //=============================================================================
268 GEOM::GEOM_Object_ptr GEOM_IInsertOperations_i::RestoreShape (const SALOMEDS::TMPFile& theStream)
269 {
270   GEOM::GEOM_Object_var aGEOMObject;
271
272   //Set a not done flag
273   GetOperations()->SetNotDone();
274
275   if (theStream.length() < 1)
276     return aGEOMObject._retn();
277
278   char* buf = (char*)theStream.NP_data();
279   std::istringstream aStream (buf);
280
281   Handle(GEOM_Object) anObject = GetOperations()->RestoreShape(aStream);
282   if (!GetOperations()->IsDone() || anObject.IsNull())
283     return aGEOMObject._retn();
284
285   return GetObject(anObject);
286 }
287
288 //=============================================================================
289 /*!
290  *  LoadTexture
291  */
292 //=============================================================================
293 CORBA::Long GEOM_IInsertOperations_i::LoadTexture(const char* theTextureFile)
294 {
295   GetOperations()->SetNotDone();
296   return GetOperations()->LoadTexture( theTextureFile );
297 }
298
299 //=============================================================================
300 /*!
301  *  AddTexture
302  */
303 //=============================================================================
304 CORBA::Long GEOM_IInsertOperations_i::AddTexture(CORBA::Long theWidth, CORBA::Long theHeight,
305                                                  const SALOMEDS::TMPFile& theTexture)
306 {
307   GetOperations()->SetNotDone();
308
309 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
310   Handle(TColStd_HArray1OfByte) aTexture;
311 #else
312   Handle(TDataStd_HArray1OfByte) aTexture;
313 #endif
314
315   if ( theTexture.length() > 0 ) {
316 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
317     aTexture = new TColStd_HArray1OfByte (1, theTexture.length());
318 #else
319     aTexture = new TDataStd_HArray1OfByte (1, theTexture.length());
320 #endif
321
322     for ( int i = 0; i < theTexture.length(); i++ )
323       aTexture->SetValue( i+1, (Standard_Byte)theTexture[i] );
324   }
325   return GetOperations()->AddTexture( theWidth, theHeight, aTexture );
326 }
327
328 //=============================================================================
329 /*!
330  *  GetTexture
331  */
332 //=============================================================================
333 SALOMEDS::TMPFile* GEOM_IInsertOperations_i::GetTexture(CORBA::Long theID,
334                                                         CORBA::Long& theWidth,
335                                                         CORBA::Long& theHeight)
336 {
337   int aWidth, aHeight;
338 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
339   Handle(TColStd_HArray1OfByte) aTextureImpl =
340 #else
341   Handle(TDataStd_HArray1OfByte) aTextureImpl =
342 #endif
343     GetOperations()->GetTexture(theID, aWidth, aHeight);
344   theWidth  = aWidth;
345   theHeight = aHeight;
346   SALOMEDS::TMPFile_var aTexture;
347   if ( !aTextureImpl.IsNull() ) {
348     aTexture = new SALOMEDS::TMPFile;
349     aTexture->length( aTextureImpl->Length() );
350     for ( int i = aTextureImpl->Lower(); i <= aTextureImpl->Upper(); i++ )
351       aTexture[i-aTextureImpl->Lower()] = aTextureImpl->Value( i );
352   }
353   return aTexture._retn();
354 }
355
356 //=============================================================================
357 /*!
358  *  GetAllTextures
359  */
360 //=============================================================================
361 GEOM::ListOfLong* GEOM_IInsertOperations_i::GetAllTextures()
362 {
363   std::list<int> localIDs = GetOperations()->GetAllTextures();
364   GEOM::ListOfLong_var anIDs = new GEOM::ListOfLong(localIDs.size());
365   anIDs->length(localIDs.size());
366   std::list<int>::const_iterator anIt;
367   int i = 0;
368   for( anIt = localIDs.begin(); anIt != localIDs.end(); ++anIt, i++)
369     anIDs[i] = *anIt;
370   return anIDs._retn();
371 }
372
373 //=============================================================================
374 /*!
375  *  Export a shape to XAO format
376  *  \param shape The shape to export
377  *  \param groups The list of groups to export
378  *  \param fields The list of fields to export
379  *  \param author The author of the export
380  *  \param fileName The name of the exported file
381  *  \return boolean indicating if export was succeful.
382  */
383 //=============================================================================
384 CORBA::Boolean GEOM_IInsertOperations_i::ExportXAO(GEOM::GEOM_Object_ptr shape,
385                                                    const GEOM::ListOfGO& groups,
386                                                    const GEOM::ListOfFields&  fields,
387                                                    const char* author,
388                                                    const char* fileName)
389 {
390   bool isGood = false;
391   // Set a not done flag
392   GetOperations()->SetNotDone();
393   
394   // Get the reference shape
395   Handle(GEOM_Object) reference = GetObjectImpl(shape);
396   
397   // Get the reference groups
398   int ind = 0;
399   std::list<Handle(GEOM_Object)> groupsObj;
400   for (; ind < groups.length(); ind++)
401   {
402     Handle(GEOM_Object) gobj = GetObjectImpl(groups[ind]);
403     if (gobj.IsNull()) return false;
404     groupsObj.push_back(gobj);
405   }
406
407   // Get the reference fields
408   ind = 0;
409   std::list<Handle(GEOM_Field)> fieldsObj;
410   for (; ind < fields.length(); ind++)
411   {
412     Handle(GEOM_Field) fobj = Handle(GEOM_Field)::DownCast(GetBaseObjectImpl(fields[ind]));
413     if (fobj.IsNull()) return false;
414     fieldsObj.push_back(fobj);
415   }
416   
417   if (!reference.IsNull())
418   {
419     // Export XAO
420     isGood = GetOperations()->ExportXAO(reference, groupsObj, fieldsObj, author, fileName);
421   }
422   
423   return isGood;
424 }
425
426 //=============================================================================
427 /*!
428  *  Import a shape from XAO format
429  *  \param fileName The name of the file to import
430  *  \param shape The imported shape
431  *  \param subShapes The list of imported subShapes
432  *  \param groups The list of imported groups
433  *  \param fields The list of imported fields
434  *  \return boolean indicating if import was succeful.
435  */
436 //=============================================================================
437 CORBA::Boolean GEOM_IInsertOperations_i::ImportXAO(const char* fileName,
438                                                    GEOM::GEOM_Object_out shape,
439                                                    GEOM::ListOfGO_out subShapes,
440                                                    GEOM::ListOfGO_out groups,
441                                                    GEOM::ListOfFields_out fields)
442 {
443   GEOM::GEOM_Object_var vshape;
444   shape = vshape._retn();
445   
446   subShapes = new GEOM::ListOfGO;
447   groups = new GEOM::ListOfGO;
448   fields = new GEOM::ListOfFields;
449   
450   // Set a not done flag
451   GetOperations()->SetNotDone();
452   
453   Handle(TColStd_HSequenceOfTransient) importedSubShapes = new TColStd_HSequenceOfTransient();
454   Handle(TColStd_HSequenceOfTransient) importedGroups = new TColStd_HSequenceOfTransient();
455   Handle(TColStd_HSequenceOfTransient) importedFields = new TColStd_HSequenceOfTransient();
456   Handle(GEOM_Object) hshape;
457   bool res = GetOperations()->ImportXAO(fileName, hshape, importedSubShapes, importedGroups, importedFields);
458   
459   if (!GetOperations()->IsDone() || !res)
460     return false;
461   
462   // parse fields
463   int n = importedSubShapes->Length();
464   subShapes->length(n);
465   for (int i = 1; i <= n; i++)
466   {
467     (*subShapes)[i - 1] = GetObject(Handle(GEOM_Object)::DownCast(importedSubShapes->Value(i)));
468   }
469   
470   // parse groups
471   n = importedGroups->Length();
472   groups->length(n);
473   for (int i = 1; i <= n; i++)
474   {
475     (*groups)[i - 1] = GetObject(Handle(GEOM_Object)::DownCast(importedGroups->Value(i)));
476   }
477   
478   // parse fields
479   n = importedFields->Length();
480   fields->length(n);
481   for (int i = 1; i <= n; i++)
482   {
483         (*fields)[i - 1] = GEOM::GEOM_Field::_narrow(
484                 GetBaseObject(Handle(GEOM_Field)::DownCast(importedFields->Value(i))));
485   }
486   
487   shape = GetObject(hshape);
488   
489   return res;
490 }