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