Salome HOME
c3941407ae7a4cf369e2b33b80091bd213ffe44c
[modules/geom.git] / src / GEOM_I / GEOM_IInsertOperations_i.cc
1 //  Copyright (C) 2007-2010  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 <TColStd_HSequenceOfAsciiString.hxx>
38 #include <TDataStd_HArray1OfByte.hxx>
39
40 //=============================================================================
41 /*!
42  *   constructor:
43  */
44 //=============================================================================
45 GEOM_IInsertOperations_i::GEOM_IInsertOperations_i (PortableServer::POA_ptr thePOA,
46                                                     GEOM::GEOM_Gen_ptr theEngine,
47                                                     ::GEOMImpl_IInsertOperations* theImpl)
48      :GEOM_IOperations_i(thePOA, theEngine, theImpl)
49 {
50   MESSAGE("GEOM_IInsertOperations_i::GEOM_IInsertOperations_i");
51 }
52
53 //=============================================================================
54 /*!
55  *  destructor
56  */
57 //=============================================================================
58 GEOM_IInsertOperations_i::~GEOM_IInsertOperations_i()
59 {
60   MESSAGE("GEOM_IInsertOperations_i::~GEOM_IInsertOperations_i");
61 }
62
63
64 //=============================================================================
65 /*!
66  *  MakeCopy
67  */
68 //=============================================================================
69 GEOM::GEOM_Object_ptr GEOM_IInsertOperations_i::MakeCopy(GEOM::GEOM_Object_ptr theOriginal)
70 {
71   GEOM::GEOM_Object_var aGEOMObject;
72
73   //Set a not done flag
74   GetOperations()->SetNotDone();
75
76   //Get the reference shape
77   Handle(GEOM_Object) anOriginal = GetObjectImpl(theOriginal);
78   if (anOriginal.IsNull()) return aGEOMObject._retn();
79
80   //Create the copy
81   Handle(GEOM_Object) anObject = GetOperations()->MakeCopy(anOriginal);
82
83   if (!GetOperations()->IsDone() || anObject.IsNull()) return aGEOMObject._retn();
84
85   return GetObject(anObject);
86 }
87
88 //=============================================================================
89 /*!
90  *  Export
91  */
92 //=============================================================================
93 void GEOM_IInsertOperations_i::Export
94                    (GEOM::GEOM_Object_ptr theOriginal,
95                     const char*           theFileName,
96                     const char*           theFormatName)
97 {
98   GEOM::GEOM_Object_var aGEOMObject = GEOM::GEOM_Object::_duplicate(theOriginal);
99
100   //Set a not done flag
101   GetOperations()->SetNotDone();
102
103   //Get the reference shape
104   Handle(GEOM_Object) anOriginal = GetObjectImpl(theOriginal);
105   if (anOriginal.IsNull()) return;
106
107   //Export the shape to the file
108   char* aFileName   = strdup(theFileName);
109   char* aFormatName = strdup(theFormatName);
110   GetOperations()->Export(anOriginal, aFileName, aFormatName);
111   free(aFileName);
112   free(aFormatName);
113 }
114
115 //=============================================================================
116 /*!
117  *  ImportFile
118  */
119 //=============================================================================
120 GEOM::GEOM_Object_ptr GEOM_IInsertOperations_i::ImportFile
121                    (const char* theFileName,
122                     const char* theFormatName)
123 {
124   GEOM::GEOM_Object_var aGEOMObject;
125
126   //Set a not done flag
127   GetOperations()->SetNotDone();
128
129   //Import the shape from the file
130   char* aFileName   = strdup(theFileName);
131   char* aFormatName = strdup(theFormatName);
132   Handle(GEOM_Object) anObject = GetOperations()->Import(aFileName, aFormatName);
133
134   if( strcmp(aFormatName,"IGES_UNIT")==0 && !anObject.IsNull() ) {
135     free(aFileName);
136     free(aFormatName);
137     return GetObject(anObject);
138   }
139
140   free(aFileName);
141   free(aFormatName);
142
143   if (!GetOperations()->IsDone() || anObject.IsNull()) {
144     return aGEOMObject._retn();
145   }
146
147   return GetObject(anObject);
148 }
149
150 //=============================================================================
151 /*!
152  *  ImportTranslators
153  */
154 //=============================================================================
155 void GEOM_IInsertOperations_i::ImportTranslators
156   (GEOM::string_array_out theFormats, GEOM::string_array_out thePatterns)
157 {
158   // allocate the CORBA arrays
159   GEOM::string_array_var aFormatsArray  = new GEOM::string_array();
160   GEOM::string_array_var aPatternsArray = new GEOM::string_array();
161
162   // Get sequences of available formats
163   Handle(TColStd_HSequenceOfAsciiString) aFormats  = new TColStd_HSequenceOfAsciiString;
164   Handle(TColStd_HSequenceOfAsciiString) aPatterns = new TColStd_HSequenceOfAsciiString;
165   if (GetOperations()->ImportTranslators(aFormats, aPatterns)) {
166     const int formSize = aFormats->Length();
167     if (formSize == aPatterns->Length()) {
168       aFormatsArray->length(formSize);
169       aPatternsArray->length(formSize);
170
171       // fill the local CORBA arrays with values from sequences
172       CORBA::Long i = 1;
173       for (; i <= formSize; i++) {
174         aFormatsArray[i-1]  = CORBA::string_dup(aFormats->Value(i).ToCString());
175         aPatternsArray[i-1] = CORBA::string_dup(aPatterns->Value(i).ToCString());
176       }
177     }
178   }
179
180   // initialize out-parameters with local arrays
181   theFormats  = aFormatsArray._retn();
182   thePatterns = aPatternsArray._retn();
183 }
184
185 //=============================================================================
186 /*!
187  *  ExportTranslators
188  */
189 //=============================================================================
190 void GEOM_IInsertOperations_i::ExportTranslators
191   (GEOM::string_array_out theFormats, GEOM::string_array_out thePatterns)
192 {
193   // allocate the CORBA arrays
194   GEOM::string_array_var aFormatsArray  = new GEOM::string_array();
195   GEOM::string_array_var aPatternsArray = new GEOM::string_array();
196
197   // Get sequences of available formats
198   Handle(TColStd_HSequenceOfAsciiString) aFormats  = new TColStd_HSequenceOfAsciiString;
199   Handle(TColStd_HSequenceOfAsciiString) aPatterns = new TColStd_HSequenceOfAsciiString;
200   if (GetOperations()->ExportTranslators(aFormats, aPatterns)) {
201     const int formSize = aFormats->Length();
202     if (formSize == aPatterns->Length()) {
203       aFormatsArray->length(formSize);
204       aPatternsArray->length(formSize);
205
206       // fill the local CORBA arrays with values from sequences
207       CORBA::Long i = 1;
208       for (; i <= formSize; i++) {
209         aFormatsArray[i-1]  = CORBA::string_dup(aFormats->Value(i).ToCString());
210         aPatternsArray[i-1] = CORBA::string_dup(aPatterns->Value(i).ToCString());
211       }
212     }
213   }
214
215   // initialize out-parameters with local arrays
216   theFormats  = aFormatsArray._retn();
217   thePatterns = aPatternsArray._retn();
218 }
219
220 CORBA::Long GEOM_IInsertOperations_i::LoadTexture(const char* theTextureFile)
221 {
222   GetOperations()->SetNotDone();
223   return GetOperations()->LoadTexture( theTextureFile );
224 }
225
226 CORBA::Long GEOM_IInsertOperations_i::AddTexture(CORBA::Long theWidth, CORBA::Long theHeight,
227                                                  const SALOMEDS::TMPFile& theTexture)
228 {
229   GetOperations()->SetNotDone();
230   Handle(TDataStd_HArray1OfByte) aTexture;
231   if ( theTexture.length() > 0 ) {
232     aTexture = new TDataStd_HArray1OfByte( 1, theTexture.length() );
233     for ( int i = 0; i < theTexture.length(); i++ )
234       aTexture->SetValue( i+1, (Standard_Byte)theTexture[i] );
235   }
236   return GetOperations()->AddTexture( theWidth, theHeight, aTexture );
237 }
238
239 SALOMEDS::TMPFile* GEOM_IInsertOperations_i::GetTexture(CORBA::Long theID,
240                                                         CORBA::Long& theWidth,
241                                                         CORBA::Long& theHeight)
242 {
243   int aWidth, aHeight;
244   Handle(TDataStd_HArray1OfByte) aTextureImpl = GetOperations()->GetTexture( theID, aWidth, aHeight );
245   theWidth  = aWidth;
246   theHeight = aHeight;
247   SALOMEDS::TMPFile_var aTexture;
248   if ( !aTextureImpl.IsNull() ) {
249     aTexture = new SALOMEDS::TMPFile;
250     aTexture->length( aTextureImpl->Length() );
251     for ( int i = aTextureImpl->Lower(); i <= aTextureImpl->Upper(); i++ )
252       aTexture[i-aTextureImpl->Lower()] = aTextureImpl->Value( i );
253   }
254   return aTexture._retn();
255 }
256
257 GEOM::ListOfLong* GEOM_IInsertOperations_i::GetAllTextures()
258 {
259   std::list<int> localIDs = GetOperations()->GetAllTextures();
260   GEOM::ListOfLong_var anIDs = new GEOM::ListOfLong(localIDs.size());
261   anIDs->length(localIDs.size());
262   std::list<int>::const_iterator anIt;
263   int i = 0;
264   for( anIt = localIDs.begin(); anIt != localIDs.end(); ++anIt, i++)
265     anIDs[i] = *anIt;
266   return anIDs._retn();
267 }