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