Salome HOME
Migration to OCCT 7.0
[modules/geom.git] / src / GEOM_I / GEOM_IInsertOperations_i.cc
1 // Copyright (C) 2007-2016  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 #include "GEOMImpl_Types.hxx"
38
39 #include <Basics_OCCTVersion.hxx>
40
41 #include <TColStd_HSequenceOfAsciiString.hxx>
42
43 #include <TColStd_HArray1OfByte.hxx>
44
45 //=============================================================================
46 /*!
47  *  constructor
48  */
49 //=============================================================================
50 GEOM_IInsertOperations_i::GEOM_IInsertOperations_i (PortableServer::POA_ptr thePOA,
51                                                     GEOM::GEOM_Gen_ptr theEngine,
52                                                     ::GEOMImpl_IInsertOperations* theImpl)
53      :GEOM_IOperations_i(thePOA, theEngine, theImpl)
54 {
55   MESSAGE("GEOM_IInsertOperations_i::GEOM_IInsertOperations_i");
56 }
57
58 //=============================================================================
59 /*!
60  *  destructor
61  */
62 //=============================================================================
63 GEOM_IInsertOperations_i::~GEOM_IInsertOperations_i()
64 {
65   MESSAGE("GEOM_IInsertOperations_i::~GEOM_IInsertOperations_i");
66 }
67
68
69 //=============================================================================
70 /*!
71  *  MakeCopy
72  */
73 //=============================================================================
74 GEOM::GEOM_Object_ptr GEOM_IInsertOperations_i::MakeCopy(GEOM::GEOM_Object_ptr theOriginal)
75 {
76   GEOM::GEOM_Object_var aGEOMObject;
77
78   //Set a not done flag
79   GetOperations()->SetNotDone();
80
81   //Get the reference shape
82   HANDLE_NAMESPACE(GEOM_Object) anOriginal = GetObjectImpl(theOriginal);
83   if (anOriginal.IsNull()) return aGEOMObject._retn();
84
85   //Create the copy
86   HANDLE_NAMESPACE(GEOM_Object) anObject = GetOperations()->MakeCopy(anOriginal);
87
88   if (!GetOperations()->IsDone() || anObject.IsNull()) return aGEOMObject._retn();
89
90   return GetObject(anObject);
91 }
92
93 //=============================================================================
94 /*!
95  *  Export
96  */
97 //=============================================================================
98 void GEOM_IInsertOperations_i::Export
99                    (GEOM::GEOM_Object_ptr theOriginal,
100                     const char*           theFileName,
101                     const char*           theFormatName)
102 {
103   GEOM::GEOM_Object_var aGEOMObject = GEOM::GEOM_Object::_duplicate(theOriginal);
104
105   //Set a not done flag
106   GetOperations()->SetNotDone();
107
108   //Get the reference shape
109   HANDLE_NAMESPACE(GEOM_Object) anOriginal = GetObjectImpl(theOriginal);
110   if (anOriginal.IsNull()) return;
111
112   //Export the shape to the file
113   GetOperations()->Export(anOriginal, theFileName, theFormatName);
114 }
115
116 //=============================================================================
117 /*!
118  *  ImportFile
119  */
120 //=============================================================================
121 GEOM::ListOfGBO* GEOM_IInsertOperations_i::ImportFile
122                    (const char* theFileName,
123                     const char* theFormatName)
124 {
125   GEOM::ListOfGBO_var aSeq = new GEOM::ListOfGBO;
126
127   //Set a not done flag
128   GetOperations()->SetNotDone();
129
130   //Import the shape from the file
131   Handle(TColStd_HSequenceOfTransient) aHSeq = GetOperations()->Import(theFileName, theFormatName);
132
133   if (!GetOperations()->IsDone() || aHSeq.IsNull()) {
134     return aSeq._retn();
135   }
136
137   // Copy created objects.
138   Standard_Integer aLength = aHSeq->Length();
139
140   aSeq->length(aLength);
141   for (Standard_Integer i = 1; i <= aLength; i++)
142     aSeq[i-1] = GetBaseObject(HANDLE_NAMESPACE(GEOM_BaseObject)::DownCast(aHSeq->Value(i)));
143
144   return aSeq._retn();
145 }
146
147 //=============================================================================
148 /*!
149  *  ReadValue
150  */
151 //=============================================================================
152 char* GEOM_IInsertOperations_i::ReadValue(const char* theFileName,
153                                           const char* theFormatName,
154                                           const char* theParameterName)
155 {
156   //Set a not done flag
157   GetOperations()->SetNotDone();
158
159   TCollection_AsciiString aUnits = GetOperations()->ReadValue
160     (theFileName, theFormatName, theParameterName);
161
162   return CORBA::string_dup(aUnits.ToCString());
163 }
164
165 //=============================================================================
166 /*!
167  *  RestoreShape
168  */
169 //=============================================================================
170 GEOM::GEOM_Object_ptr GEOM_IInsertOperations_i::RestoreShape (const SALOMEDS::TMPFile& theStream)
171 {
172   GEOM::GEOM_Object_var aGEOMObject;
173
174   //Set a not done flag
175   GetOperations()->SetNotDone();
176
177   if (theStream.length() < 1)
178     return aGEOMObject._retn();
179
180   char* buf = (char*)theStream.NP_data();
181   std::istringstream aStream (buf);
182
183   HANDLE_NAMESPACE(GEOM_Object) anObject = GetOperations()->RestoreShape(aStream);
184   if (!GetOperations()->IsDone() || anObject.IsNull())
185     return aGEOMObject._retn();
186
187   return GetObject(anObject);
188 }
189
190 //=============================================================================
191 /*!
192  *  LoadTexture
193  */
194 //=============================================================================
195 CORBA::Long GEOM_IInsertOperations_i::LoadTexture(const char* theTextureFile)
196 {
197   GetOperations()->SetNotDone();
198   return GetOperations()->LoadTexture( theTextureFile );
199 }
200
201 //=============================================================================
202 /*!
203  *  AddTexture
204  */
205 //=============================================================================
206 CORBA::Long GEOM_IInsertOperations_i::AddTexture(CORBA::Long theWidth, CORBA::Long theHeight,
207                                                  const SALOMEDS::TMPFile& theTexture)
208 {
209   GetOperations()->SetNotDone();
210
211   Handle(TColStd_HArray1OfByte) aTexture;
212
213   if ( theTexture.length() > 0 ) {
214     aTexture = new TColStd_HArray1OfByte (1, theTexture.length());
215
216     for ( int i = 0; i < theTexture.length(); i++ )
217       aTexture->SetValue( i+1, (Standard_Byte)theTexture[i] );
218   }
219   return GetOperations()->AddTexture( theWidth, theHeight, aTexture );
220 }
221
222 //=============================================================================
223 /*!
224  *  GetTexture
225  */
226 //=============================================================================
227 SALOMEDS::TMPFile* GEOM_IInsertOperations_i::GetTexture(CORBA::Long theID,
228                                                         CORBA::Long& theWidth,
229                                                         CORBA::Long& theHeight)
230 {
231   int aWidth, aHeight;
232   Handle(TColStd_HArray1OfByte) aTextureImpl =
233     GetOperations()->GetTexture(theID, aWidth, aHeight);
234   theWidth  = aWidth;
235   theHeight = aHeight;
236   SALOMEDS::TMPFile_var aTexture;
237   if ( !aTextureImpl.IsNull() ) {
238     aTexture = new SALOMEDS::TMPFile;
239     aTexture->length( aTextureImpl->Length() );
240     for ( int i = aTextureImpl->Lower(); i <= aTextureImpl->Upper(); i++ )
241       aTexture[i-aTextureImpl->Lower()] = aTextureImpl->Value( i );
242   }
243   return aTexture._retn();
244 }
245
246 //=============================================================================
247 /*!
248  *  GetAllTextures
249  */
250 //=============================================================================
251 GEOM::ListOfLong* GEOM_IInsertOperations_i::GetAllTextures()
252 {
253   std::list<int> localIDs = GetOperations()->GetAllTextures();
254   GEOM::ListOfLong_var anIDs = new GEOM::ListOfLong(localIDs.size());
255   anIDs->length(localIDs.size());
256   std::list<int>::const_iterator anIt;
257   int i = 0;
258   for( anIt = localIDs.begin(); anIt != localIDs.end(); ++anIt, i++)
259     anIDs[i] = *anIt;
260   return anIDs._retn();
261 }
262
263 //=============================================================================
264 /*!
265  *  TransferData
266  */
267 //=============================================================================
268 CORBA::Boolean GEOM_IInsertOperations_i::TransferData
269         (GEOM::GEOM_Object_ptr                                 theObjectFrom,
270          GEOM::GEOM_Object_ptr                                 theObjectTo,
271          GEOM::find_shape_method                               theFindMethod,
272          GEOM::GEOM_IInsertOperations::ListOfTransferDatum_out theResult)
273 {
274   //Set a not done flag
275   GetOperations()->SetNotDone();
276
277   if (CORBA::is_nil(theObjectFrom) || CORBA::is_nil(theObjectTo))
278   {
279     return false;
280   }
281
282   //Get the reference shape
283   HANDLE_NAMESPACE(GEOM_Object) aShapeFrom = GetObjectImpl(theObjectFrom);
284   HANDLE_NAMESPACE(GEOM_Object) aShapeTo   = GetObjectImpl(theObjectTo);
285
286   if (aShapeFrom.IsNull() || aShapeTo.IsNull())
287   {
288     return false;
289   }
290
291   bool isOk = false;
292   std::list<GEOMImpl_IInsertOperations::TransferDatum> aData;
293   int aFindMethod = -1;
294
295   switch (theFindMethod) {
296   case GEOM::FSM_GetInPlace:
297     aFindMethod = TD_GET_IN_PLACE;
298     break;
299   case GEOM::FSM_GetInPlaceByHistory:
300     aFindMethod = TD_GET_IN_PLACE_BY_HISTORY;
301     break;
302   case GEOM::FSM_GetInPlace_Old:
303     aFindMethod = TD_GET_IN_PLACE_OLD;
304     break;
305   default:
306     break;
307   }
308
309   // Transfer data.
310   if (aFindMethod > 0) {
311     isOk = GetOperations()->TransferData
312       (aShapeFrom, aShapeTo, aFindMethod, aData);
313   }
314
315   if (isOk) {
316     // Copy results.
317     const int aNbDatum = aData.size();
318     GEOM::GEOM_IInsertOperations::ListOfTransferDatum_var aResult =
319       new GEOM::GEOM_IInsertOperations::ListOfTransferDatum;
320
321     aResult->length(aNbDatum);
322
323     // fill the local CORBA array with values from lists
324     std::list<GEOMImpl_IInsertOperations::TransferDatum>::const_iterator
325       anIt = aData.begin();
326     int i = 0;
327
328     for (; anIt != aData.end(); i++, anIt++) {
329       GEOM::GEOM_IInsertOperations::TransferDatum_var aDatum =
330         new GEOM::GEOM_IInsertOperations::TransferDatum;
331
332       aDatum->myName      = CORBA::string_dup(anIt->myName.ToCString());
333       aDatum->myNumber    = anIt->myNumber;
334       aDatum->myMaxNumber = anIt->myMaxNumber;
335       aResult[i]          = aDatum;
336     }
337
338     theResult = aResult._retn();
339   }
340
341   return isOk;
342 }