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