Salome HOME
rnc:
[modules/geom.git] / src / GEOM_I / GEOM_IInsertOperations_i.cc
1 // Copyright (C) 2007-2011  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  *  ImportTranslators
160  */
161 //=============================================================================
162 void GEOM_IInsertOperations_i::ImportTranslators
163   (GEOM::string_array_out theFormats, GEOM::string_array_out thePatterns)
164 {
165   // allocate the CORBA arrays
166   GEOM::string_array_var aFormatsArray  = new GEOM::string_array();
167   GEOM::string_array_var aPatternsArray = new GEOM::string_array();
168
169   // Get sequences of available formats
170   Handle(TColStd_HSequenceOfAsciiString) aFormats  = new TColStd_HSequenceOfAsciiString;
171   Handle(TColStd_HSequenceOfAsciiString) aPatterns = new TColStd_HSequenceOfAsciiString;
172   if (GetOperations()->ImportTranslators(aFormats, aPatterns)) {
173     const int formSize = aFormats->Length();
174     if (formSize == aPatterns->Length()) {
175       aFormatsArray->length(formSize);
176       aPatternsArray->length(formSize);
177
178       // fill the local CORBA arrays with values from sequences
179       CORBA::Long i = 1;
180       for (; i <= formSize; i++) {
181         aFormatsArray[i-1]  = CORBA::string_dup(aFormats->Value(i).ToCString());
182         aPatternsArray[i-1] = CORBA::string_dup(aPatterns->Value(i).ToCString());
183       }
184     }
185   }
186
187   // initialize out-parameters with local arrays
188   theFormats  = aFormatsArray._retn();
189   thePatterns = aPatternsArray._retn();
190 }
191
192 //=============================================================================
193 /*!
194  *  ExportTranslators
195  */
196 //=============================================================================
197 void GEOM_IInsertOperations_i::ExportTranslators
198   (GEOM::string_array_out theFormats, GEOM::string_array_out thePatterns)
199 {
200   // allocate the CORBA arrays
201   GEOM::string_array_var aFormatsArray  = new GEOM::string_array();
202   GEOM::string_array_var aPatternsArray = new GEOM::string_array();
203
204   // Get sequences of available formats
205   Handle(TColStd_HSequenceOfAsciiString) aFormats  = new TColStd_HSequenceOfAsciiString;
206   Handle(TColStd_HSequenceOfAsciiString) aPatterns = new TColStd_HSequenceOfAsciiString;
207   if (GetOperations()->ExportTranslators(aFormats, aPatterns)) {
208     const int formSize = aFormats->Length();
209     if (formSize == aPatterns->Length()) {
210       aFormatsArray->length(formSize);
211       aPatternsArray->length(formSize);
212
213       // fill the local CORBA arrays with values from sequences
214       CORBA::Long i = 1;
215       for (; i <= formSize; i++) {
216         aFormatsArray[i-1]  = CORBA::string_dup(aFormats->Value(i).ToCString());
217         aPatternsArray[i-1] = CORBA::string_dup(aPatterns->Value(i).ToCString());
218       }
219     }
220   }
221
222   // initialize out-parameters with local arrays
223   theFormats  = aFormatsArray._retn();
224   thePatterns = aPatternsArray._retn();
225 }
226
227 CORBA::Long GEOM_IInsertOperations_i::LoadTexture(const char* theTextureFile)
228 {
229   GetOperations()->SetNotDone();
230   return GetOperations()->LoadTexture( theTextureFile );
231 }
232
233 CORBA::Long GEOM_IInsertOperations_i::AddTexture(CORBA::Long theWidth, CORBA::Long theHeight,
234                                                  const SALOMEDS::TMPFile& theTexture)
235 {
236   GetOperations()->SetNotDone();
237
238 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
239   Handle(TColStd_HArray1OfByte) aTexture;
240 #else
241   Handle(TDataStd_HArray1OfByte) aTexture;
242 #endif
243
244   if ( theTexture.length() > 0 ) {
245 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
246     aTexture = new TColStd_HArray1OfByte (1, theTexture.length());
247 #else
248     aTexture = new TDataStd_HArray1OfByte (1, theTexture.length());
249 #endif
250
251     for ( int i = 0; i < theTexture.length(); i++ )
252       aTexture->SetValue( i+1, (Standard_Byte)theTexture[i] );
253   }
254   return GetOperations()->AddTexture( theWidth, theHeight, aTexture );
255 }
256
257 SALOMEDS::TMPFile* GEOM_IInsertOperations_i::GetTexture(CORBA::Long theID,
258                                                         CORBA::Long& theWidth,
259                                                         CORBA::Long& theHeight)
260 {
261   int aWidth, aHeight;
262 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
263   Handle(TColStd_HArray1OfByte) aTextureImpl =
264 #else
265   Handle(TDataStd_HArray1OfByte) aTextureImpl =
266 #endif
267     GetOperations()->GetTexture(theID, aWidth, aHeight);
268   theWidth  = aWidth;
269   theHeight = aHeight;
270   SALOMEDS::TMPFile_var aTexture;
271   if ( !aTextureImpl.IsNull() ) {
272     aTexture = new SALOMEDS::TMPFile;
273     aTexture->length( aTextureImpl->Length() );
274     for ( int i = aTextureImpl->Lower(); i <= aTextureImpl->Upper(); i++ )
275       aTexture[i-aTextureImpl->Lower()] = aTextureImpl->Value( i );
276   }
277   return aTexture._retn();
278 }
279
280 GEOM::ListOfLong* GEOM_IInsertOperations_i::GetAllTextures()
281 {
282   std::list<int> localIDs = GetOperations()->GetAllTextures();
283   GEOM::ListOfLong_var anIDs = new GEOM::ListOfLong(localIDs.size());
284   anIDs->length(localIDs.size());
285   std::list<int>::const_iterator anIt;
286   int i = 0;
287   for( anIt = localIDs.begin(); anIt != localIDs.end(); ++anIt, i++)
288     anIDs[i] = *anIt;
289   return anIDs._retn();
290 }