Salome HOME
0022616: [CEA 1038] Improve the quality of stl and vtk exports
[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_BaseObject.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   GetOperations()->Export(anOriginal, theFileName, theFormatName);
117 }
118
119 //=============================================================================
120 /*!
121  *  ImportFile
122  */
123 //=============================================================================
124 GEOM::ListOfGBO* GEOM_IInsertOperations_i::ImportFile
125                    (const char* theFileName,
126                     const char* theFormatName)
127 {
128   GEOM::ListOfGBO_var aSeq = new GEOM::ListOfGBO;
129
130   //Set a not done flag
131   GetOperations()->SetNotDone();
132
133   //Import the shape from the file
134   Handle(TColStd_HSequenceOfTransient) aHSeq = GetOperations()->Import(theFileName, theFormatName);
135
136   if (!GetOperations()->IsDone() || aHSeq.IsNull()) {
137     return aSeq._retn();
138   }
139
140   // Copy created objects.
141   Standard_Integer aLength = aHSeq->Length();
142
143   aSeq->length(aLength);
144   for (Standard_Integer i = 1; i <= aLength; i++)
145     aSeq[i-1] = GetBaseObject(Handle(GEOM_BaseObject)::DownCast(aHSeq->Value(i)));
146
147   return aSeq._retn();
148 }
149
150 //=============================================================================
151 /*!
152  *  ReadValue
153  */
154 //=============================================================================
155 char* GEOM_IInsertOperations_i::ReadValue(const char* theFileName,
156                                           const char* theFormatName,
157                                           const char* theParameterName)
158 {
159   //Set a not done flag
160   GetOperations()->SetNotDone();
161
162   TCollection_AsciiString aUnits = GetOperations()->ReadValue
163     (theFileName, theFormatName, theParameterName);
164
165   return CORBA::string_dup(aUnits.ToCString());
166 }
167
168 //=============================================================================
169 /*!
170  *  RestoreShape
171  */
172 //=============================================================================
173 GEOM::GEOM_Object_ptr GEOM_IInsertOperations_i::RestoreShape (const SALOMEDS::TMPFile& theStream)
174 {
175   GEOM::GEOM_Object_var aGEOMObject;
176
177   //Set a not done flag
178   GetOperations()->SetNotDone();
179
180   if (theStream.length() < 1)
181     return aGEOMObject._retn();
182
183   char* buf = (char*)theStream.NP_data();
184   std::istringstream aStream (buf);
185
186   Handle(GEOM_Object) anObject = GetOperations()->RestoreShape(aStream);
187   if (!GetOperations()->IsDone() || anObject.IsNull())
188     return aGEOMObject._retn();
189
190   return GetObject(anObject);
191 }
192
193 //=============================================================================
194 /*!
195  *  LoadTexture
196  */
197 //=============================================================================
198 CORBA::Long GEOM_IInsertOperations_i::LoadTexture(const char* theTextureFile)
199 {
200   GetOperations()->SetNotDone();
201   return GetOperations()->LoadTexture( theTextureFile );
202 }
203
204 //=============================================================================
205 /*!
206  *  AddTexture
207  */
208 //=============================================================================
209 CORBA::Long GEOM_IInsertOperations_i::AddTexture(CORBA::Long theWidth, CORBA::Long theHeight,
210                                                  const SALOMEDS::TMPFile& theTexture)
211 {
212   GetOperations()->SetNotDone();
213
214 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
215   Handle(TColStd_HArray1OfByte) aTexture;
216 #else
217   Handle(TDataStd_HArray1OfByte) aTexture;
218 #endif
219
220   if ( theTexture.length() > 0 ) {
221 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
222     aTexture = new TColStd_HArray1OfByte (1, theTexture.length());
223 #else
224     aTexture = new TDataStd_HArray1OfByte (1, theTexture.length());
225 #endif
226
227     for ( int i = 0; i < theTexture.length(); i++ )
228       aTexture->SetValue( i+1, (Standard_Byte)theTexture[i] );
229   }
230   return GetOperations()->AddTexture( theWidth, theHeight, aTexture );
231 }
232
233 //=============================================================================
234 /*!
235  *  GetTexture
236  */
237 //=============================================================================
238 SALOMEDS::TMPFile* GEOM_IInsertOperations_i::GetTexture(CORBA::Long theID,
239                                                         CORBA::Long& theWidth,
240                                                         CORBA::Long& theHeight)
241 {
242   int aWidth, aHeight;
243 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
244   Handle(TColStd_HArray1OfByte) aTextureImpl =
245 #else
246   Handle(TDataStd_HArray1OfByte) aTextureImpl =
247 #endif
248     GetOperations()->GetTexture(theID, aWidth, aHeight);
249   theWidth  = aWidth;
250   theHeight = aHeight;
251   SALOMEDS::TMPFile_var aTexture;
252   if ( !aTextureImpl.IsNull() ) {
253     aTexture = new SALOMEDS::TMPFile;
254     aTexture->length( aTextureImpl->Length() );
255     for ( int i = aTextureImpl->Lower(); i <= aTextureImpl->Upper(); i++ )
256       aTexture[i-aTextureImpl->Lower()] = aTextureImpl->Value( i );
257   }
258   return aTexture._retn();
259 }
260
261 //=============================================================================
262 /*!
263  *  GetAllTextures
264  */
265 //=============================================================================
266 GEOM::ListOfLong* GEOM_IInsertOperations_i::GetAllTextures()
267 {
268   std::list<int> localIDs = GetOperations()->GetAllTextures();
269   GEOM::ListOfLong_var anIDs = new GEOM::ListOfLong(localIDs.size());
270   anIDs->length(localIDs.size());
271   std::list<int>::const_iterator anIt;
272   int i = 0;
273   for( anIt = localIDs.begin(); anIt != localIDs.end(); ++anIt, i++)
274     anIDs[i] = *anIt;
275   return anIDs._retn();
276 }
277
278