Salome HOME
Merge branch 'master' into V9_dev
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IInsertOperations.cxx
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 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_IInsertOperations.hxx>
26
27 #include <GEOMImpl_CopyDriver.hxx>
28 #include <GEOMImpl_ExportDriver.hxx>
29 #include <GEOMImpl_ImportDriver.hxx>
30 #include <GEOMImpl_ICopy.hxx>
31 #include <GEOMImpl_IImportExport.hxx>
32 #include <GEOMImpl_ITransferData.hxx>
33 #include <GEOMImpl_Types.hxx>
34 #include "GEOMImpl_IShapesOperations.hxx"
35 #include "GEOMImpl_IGroupOperations.hxx"
36 #include "GEOMImpl_IFieldOperations.hxx"
37 #include "GEOMImpl_IECallBack.hxx"
38
39 #include <GEOM_Function.hxx>
40 #include <GEOM_PythonDump.hxx>
41 #include "GEOM_ISubShape.hxx"
42
43 #include <Basics_OCCTVersion.hxx>
44
45 #include "utilities.h"
46 #include <OpUtil.hxx>
47 #include <Utils_ExceptHandlers.hxx>
48
49 #include <TFunction_DriverTable.hxx>
50 #include <TFunction_Driver.hxx>
51 #include <TDF_ChildIDIterator.hxx>
52 #include <TDF_Tool.hxx>
53 #include <TDataStd_Integer.hxx>
54 #include <TNaming_NamedShape.hxx>
55 #include <TDataStd_Comment.hxx>
56 #include <TopTools_IndexedMapOfShape.hxx>
57 #include <TopExp.hxx>
58
59 #include <TopoDS.hxx>
60 #include <TopoDS_Vertex.hxx>
61 #include <BRep_Builder.hxx>
62 #include <BRep_Tool.hxx>
63 #include <BRepTools.hxx>
64 #include <gp_Pnt.hxx>
65
66 #include <TColStd_HArray1OfReal.hxx>
67
68 #include <Standard_Failure.hxx>
69 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
70
71 //=============================================================================
72 /*!
73  *  constructor
74  */
75 //=============================================================================
76 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine)
77 : GEOM_IOperations(theEngine)
78 {
79   MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
80   myShapesOperations = new GEOMImpl_IShapesOperations(GetEngine());
81   myGroupOperations = new GEOMImpl_IGroupOperations(GetEngine());
82   myFieldOperations = new GEOMImpl_IFieldOperations(GetEngine());
83 }
84
85 //=============================================================================
86 /*!
87  *  destructor
88  */
89 //=============================================================================
90 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
91 {
92   MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
93   delete myShapesOperations;
94   delete myGroupOperations;
95   delete myFieldOperations;
96 }
97
98 //=============================================================================
99 /*!
100  *  MakeCopy
101  */
102 //=============================================================================
103 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy (Handle(GEOM_Object) theOriginal)
104 {
105   SetErrorCode(KO);
106
107   if (theOriginal.IsNull()) return NULL;
108
109   //Add a new Copy object
110   Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GEOM_COPY);
111
112   //Add a Copy function for creation a copy object
113   Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
114
115   //Check if the function is set correctly
116   if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
117
118   GEOMImpl_ICopy aCI(aFunction);
119
120   Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
121   if (aRefFunction.IsNull()) return NULL;
122
123   aCI.SetOriginal(aRefFunction);
124
125   //Compute the Copy value
126   try {
127     OCC_CATCH_SIGNALS;
128     if (!GetSolver()->ComputeFunction(aFunction)) {
129       SetErrorCode("Copy driver failed");
130       return NULL;
131     }
132   }
133   catch (Standard_Failure) {
134     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
135     SetErrorCode(aFail->GetMessageString());
136     return NULL;
137   }
138
139   //Make a Python command
140   GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
141
142   SetErrorCode(OK);
143   return aCopy;
144 }
145
146 //=============================================================================
147 /*!
148  *  Export
149  */
150 //=============================================================================
151 void GEOMImpl_IInsertOperations::Export
152                      (const Handle(GEOM_Object)      theOriginal,
153                       const TCollection_AsciiString& theFileName,
154                       const TCollection_AsciiString& theFormatName)
155 {
156   SetErrorCode(KO);
157
158   if (theOriginal.IsNull()) return;
159
160   if ( !GEOMImpl_IECallBack::GetCallBack( theFormatName )->Export( theOriginal, theFileName, theFormatName ) )
161     return;
162   SetErrorCode(OK);
163 }
164
165 //=============================================================================
166 /*!
167  *  Import
168  */
169 //=============================================================================
170 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IInsertOperations::Import
171                                  (const TCollection_AsciiString& theFileName,
172                                   const TCollection_AsciiString& theFormatName)
173 {
174   SetErrorCode(KO);
175
176   if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
177
178   Handle(TColStd_HSequenceOfTransient) aSeq =
179     GEOMImpl_IECallBack::GetCallBack( theFormatName )->Import( theFormatName, theFileName );
180   SetErrorCode(OK);
181   return aSeq;
182 }
183
184 //=============================================================================
185 /*!
186  *  ReadValue
187  */
188 //=============================================================================
189 TCollection_AsciiString GEOMImpl_IInsertOperations::ReadValue
190                                  (const TCollection_AsciiString& theFileName,
191                                   const TCollection_AsciiString& theFormatName,
192                                   const TCollection_AsciiString& theParameterName)
193 {
194   SetErrorCode(KO);
195   TCollection_AsciiString aValue;
196   if (theFileName.IsEmpty() || theFormatName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
197
198   aValue = GEOMImpl_IECallBack::GetCallBack( theFormatName )->ReadValue( theFileName, theFormatName, theParameterName );
199
200   SetErrorCode(OK);
201   return aValue;
202 }
203
204 //=============================================================================
205 /*!
206  *  RestoreShape
207  */
208 //=============================================================================
209 Handle(GEOM_Object) GEOMImpl_IInsertOperations::RestoreShape (std::istringstream& theStream)
210 {
211   SetErrorCode(KO);
212
213   //Add a new result object
214   Handle(GEOM_Object) result = GetEngine()->AddObject(GEOM_COPY);
215
216   //Add a Copy function
217   Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
218   if (aFunction.IsNull()) return NULL;
219
220   //Check if the function is set correctly
221   if (aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
222
223   //Read a shape from the stream
224   TopoDS_Shape aShape;
225   BRep_Builder B;
226   BRepTools::Read(aShape, theStream, B);
227   if (aShape.IsNull()) {
228     SetErrorCode("RestoreShape error: BREP reading failed");
229   }
230
231   //Set function value
232   aFunction->SetValue(aShape);
233
234   //Special dump to avoid restored shapes publication.
235   //See correcponding code in GEOM_Engine.cxx (method ProcessFunction)
236   //GEOM::TPythonDump(aFunction) << "#";
237
238   bool ignore = false;
239
240   if ( const char* env_var = getenv( "GEOM_IGNORE_RESTORE_SHAPE" ) )
241     ignore = atoi( env_var ) > 1;
242
243   if ( !ignore ) {
244     GEOM::TPythonDump(aFunction) << result
245     << " = geompy.RestoreShape(\"\") # the shape string has not been dump for performance reason";
246   }
247
248   SetErrorCode(OK);
249
250   return result;
251 }
252
253 int GEOMImpl_IInsertOperations::LoadTexture(const TCollection_AsciiString& theTextureFile)
254 {
255   SetErrorCode(KO);
256
257   if (theTextureFile.IsEmpty()) return 0;
258
259   Handle(TColStd_HArray1OfByte) aTexture;
260
261   FILE* fp = fopen(theTextureFile.ToCString(), "r");
262   if (!fp) return 0;
263
264   std::list<std::string> lines;
265   char buffer[4096];
266   int maxlen = 0;
267   while (!feof(fp)) {
268     if ((fgets(buffer, 4096, fp)) == NULL) break;
269     int aLen = strlen(buffer);
270     if (buffer[aLen-1] == '\n') buffer[aLen-1] = '\0';
271     lines.push_back(buffer);
272     maxlen = std::max(maxlen, (int)strlen(buffer));
273   }
274
275   fclose(fp);
276
277   int lenbytes = maxlen/8;
278   if (maxlen%8) lenbytes++;
279
280   if (lenbytes == 0 || lines.empty())
281     return 0;
282
283   std::list<unsigned char> bytedata;
284   std::list<std::string>::const_iterator it;
285   for (it = lines.begin(); it != lines.end(); ++it) {
286     std::string line = *it;
287     int lenline = (line.size()/8 + (line.size()%8 ? 1 : 0)) * 8;
288     for (int i = 0; i < lenline/8; i++) {
289       unsigned char byte = 0;
290       for (int j = 0; j < 8; j++)
291         byte = (byte << 1) + ( i*8+j < line.size() && line[i*8+j] != '0' ? 1 : 0 );
292       bytedata.push_back(byte);
293     }
294     for (int i = lenline/8; i < lenbytes; i++)
295       bytedata.push_back((unsigned char)0);
296   }
297
298   if (bytedata.empty() || bytedata.size() != lines.size()*lenbytes)
299     return 0;
300
301   aTexture = new TColStd_HArray1OfByte (1, lines.size()*lenbytes);
302
303   std::list<unsigned char>::iterator bdit;
304   int i;
305   for (i = 1, bdit = bytedata.begin(); bdit != bytedata.end(); ++bdit, ++i)
306     aTexture->SetValue(i, (Standard_Byte)(*bdit));
307
308   int aTextureId = GetEngine()->addTexture(lenbytes*8, lines.size(), aTexture, theTextureFile);
309   if (aTextureId > 0) SetErrorCode(OK);
310   return aTextureId;
311 }
312   
313 int GEOMImpl_IInsertOperations::AddTexture(int theWidth, int theHeight, 
314                                            const Handle(TColStd_HArray1OfByte)& theTexture)
315 {
316   SetErrorCode(KO);
317   int aTextureId = GetEngine()->addTexture(theWidth, theHeight, theTexture);
318   if (aTextureId > 0) SetErrorCode(OK);
319   return aTextureId;
320 }
321
322 Handle(TColStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
323                                                                      int& theWidth, int& theHeight)
324 {
325   SetErrorCode(KO);
326   
327   Handle(TColStd_HArray1OfByte) aTexture;
328
329   theWidth = theHeight = 0;
330   TCollection_AsciiString aFileName;
331
332   if (theTextureId <= 0)
333     return aTexture;
334
335   aTexture = GetEngine()->getTexture(theTextureId, theWidth, theHeight, aFileName);
336
337   if (theWidth > 0 && theHeight > 0 && aTexture->Length() > 0) SetErrorCode(OK);
338
339   return aTexture;
340 }
341
342 std::list<int> GEOMImpl_IInsertOperations::GetAllTextures()
343 {
344   SetErrorCode(KO);
345   std::list<int> id_list = GetEngine()->getAllTextures();
346   SetErrorCode(OK);
347   return id_list;
348 }
349
350 //=============================================================================
351 /*!
352  *  TransferData
353  */
354 //=============================================================================
355 bool GEOMImpl_IInsertOperations::TransferData
356                           (const Handle(GEOM_Object)      &theObjectFrom,
357                            const Handle(GEOM_Object)      &theObjectTo,
358                            const int                       theFindMethod,
359                                  std::list<TransferDatum> &theResult)
360 {
361   SetErrorCode(KO);
362
363   if (theObjectFrom.IsNull() || theObjectTo.IsNull()) {
364     return false;
365   }
366
367   //Add a new Transfer Data object object
368   Handle(GEOM_Object) aTDObj =
369     GetEngine()->AddObject(GEOM_TRANSFER_DATA);
370
371   //Add a Transfer Data function for created object
372   Handle(GEOM_Function) aFunction =
373     aTDObj->AddFunction(GEOMImpl_CopyDriver::GetID(), TRANSFER_DATA);
374
375   //Check if the function is set correctly
376   if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) {
377     return false;
378   }
379
380   Handle(GEOM_Function) aFunctionFrom = theObjectFrom->GetLastFunction();
381   Handle(GEOM_Function) aFunctionTo   = theObjectTo->GetLastFunction();
382
383   if (aFunctionFrom.IsNull() || aFunctionTo.IsNull()) {
384     return false;
385   }
386
387   GEOMImpl_ITransferData aTD(aFunction);
388
389   aTD.SetRef1(aFunctionFrom);
390   aTD.SetRef2(aFunctionTo);
391   aTD.SetFindMethod(theFindMethod);
392
393   // Transfer data
394   try {
395     OCC_CATCH_SIGNALS;
396     if (!GetSolver()->ComputeFunction(aFunction)) {
397       SetErrorCode("Transfer data failed");
398       return false;
399     }
400   }
401   catch (Standard_Failure) {
402     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
403     SetErrorCode(aFail->GetMessageString());
404     return false;
405   }
406
407   // Fill result list of data.
408   theResult.clear();
409
410   Handle(TColStd_HArray1OfExtendedString) aDatumName   = aTD.GetDatumName();
411   Handle(TColStd_HArray1OfInteger)        aDatumMaxVal = aTD.GetDatumMaxVal();
412   Handle(TColStd_HArray1OfInteger)        aDatumVal    = aTD.GetDatumVal();
413
414   if (!aDatumName.IsNull() && !aDatumMaxVal.IsNull() && !aDatumVal.IsNull()) {
415     Standard_Integer i;
416     Standard_Integer aNbDatum = aDatumName->Length();
417
418     for (i = 1; i <= aNbDatum; ++i) {
419       if (aDatumMaxVal->Value(i) > 0) {
420         TransferDatum aDatum;
421
422         aDatum.myName      = TCollection_AsciiString(aDatumName->Value(i));
423         aDatum.myNumber    = aDatumVal->Value(i);
424         aDatum.myMaxNumber = aDatumMaxVal->Value(i);
425         theResult.push_back(aDatum);
426       }
427     }
428   }
429
430   //Make a Python command
431   GEOM::TPythonDump pd (aFunction);
432   pd << "geompy.TransferData(" << theObjectFrom << ", " << theObjectTo;
433   pd << ", GEOM.";
434
435   switch (theFindMethod) {
436   case TD_GET_IN_PLACE:
437     pd << "FSM_GetInPlace";
438     break;
439   case TD_GET_IN_PLACE_OLD:
440     pd << "FSM_GetInPlace_Old";
441     break;
442   case TD_GET_IN_PLACE_BY_HISTORY:
443   default:
444     pd << "FSM_GetInPlaceByHistory";
445     break;
446   }
447   pd << ")";
448
449   SetErrorCode(OK);
450
451   return true;
452 }