Salome HOME
Migration to OCCT 7.0
[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, int theDocID)
77 : GEOM_IOperations(theEngine, theDocID)
78 {
79   MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
80   myShapesOperations = new GEOMImpl_IShapesOperations(GetEngine(), GetDocID());
81   myGroupOperations = new GEOMImpl_IGroupOperations(GetEngine(), GetDocID());
82   myFieldOperations = new GEOMImpl_IFieldOperations(GetEngine(), GetDocID());
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(GetDocID(), 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( GetDocID(), 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( GetDocID(), 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( GetDocID(), 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(GetDocID(), 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   GEOM::TPythonDump(aFunction) << result
239     << " = geompy.RestoreShape(\"\") # the shape string has not been dump for performance reason";
240
241   SetErrorCode(OK);
242
243   return result;
244 }
245
246 int GEOMImpl_IInsertOperations::LoadTexture(const TCollection_AsciiString& theTextureFile)
247 {
248   SetErrorCode(KO);
249
250   if (theTextureFile.IsEmpty()) return 0;
251
252   Handle(TColStd_HArray1OfByte) aTexture;
253
254   FILE* fp = fopen(theTextureFile.ToCString(), "r");
255   if (!fp) return 0;
256
257   std::list<std::string> lines;
258   char buffer[4096];
259   int maxlen = 0;
260   while (!feof(fp)) {
261     if ((fgets(buffer, 4096, fp)) == NULL) break;
262     int aLen = strlen(buffer);
263     if (buffer[aLen-1] == '\n') buffer[aLen-1] = '\0';
264     lines.push_back(buffer);
265     maxlen = std::max(maxlen, (int)strlen(buffer));
266   }
267
268   fclose(fp);
269
270   int lenbytes = maxlen/8;
271   if (maxlen%8) lenbytes++;
272
273   if (lenbytes == 0 || lines.empty())
274     return 0;
275
276   std::list<unsigned char> bytedata;
277   std::list<std::string>::const_iterator it;
278   for (it = lines.begin(); it != lines.end(); ++it) {
279     std::string line = *it;
280     int lenline = (line.size()/8 + (line.size()%8 ? 1 : 0)) * 8;
281     for (int i = 0; i < lenline/8; i++) {
282       unsigned char byte = 0;
283       for (int j = 0; j < 8; j++)
284         byte = (byte << 1) + ( i*8+j < line.size() && line[i*8+j] != '0' ? 1 : 0 );
285       bytedata.push_back(byte);
286     }
287     for (int i = lenline/8; i < lenbytes; i++)
288       bytedata.push_back((unsigned char)0);
289   }
290
291   if (bytedata.empty() || bytedata.size() != lines.size()*lenbytes)
292     return 0;
293
294   aTexture = new TColStd_HArray1OfByte (1, lines.size()*lenbytes);
295
296   std::list<unsigned char>::iterator bdit;
297   int i;
298   for (i = 1, bdit = bytedata.begin(); bdit != bytedata.end(); ++bdit, ++i)
299     aTexture->SetValue(i, (Standard_Byte)(*bdit));
300
301   int aTextureId = GetEngine()->addTexture(GetDocID(), lenbytes*8, lines.size(), aTexture, theTextureFile);
302   if (aTextureId > 0) SetErrorCode(OK);
303   return aTextureId;
304 }
305   
306 int GEOMImpl_IInsertOperations::AddTexture(int theWidth, int theHeight, 
307                                            const Handle(TColStd_HArray1OfByte)& theTexture)
308 {
309   SetErrorCode(KO);
310   int aTextureId = GetEngine()->addTexture(GetDocID(), theWidth, theHeight, theTexture);
311   if (aTextureId > 0) SetErrorCode(OK);
312   return aTextureId;
313 }
314
315 Handle(TColStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
316                                                                      int& theWidth, int& theHeight)
317 {
318   SetErrorCode(KO);
319   
320   Handle(TColStd_HArray1OfByte) aTexture;
321
322   theWidth = theHeight = 0;
323   TCollection_AsciiString aFileName;
324
325   if (theTextureId <= 0)
326     return aTexture;
327
328   aTexture = GetEngine()->getTexture(GetDocID(), theTextureId, theWidth, theHeight, aFileName);
329
330   if (theWidth > 0 && theHeight > 0 && aTexture->Length() > 0) SetErrorCode(OK);
331
332   return aTexture;
333 }
334
335 std::list<int> GEOMImpl_IInsertOperations::GetAllTextures()
336 {
337   SetErrorCode(KO);
338   std::list<int> id_list = GetEngine()->getAllTextures(GetDocID());
339   SetErrorCode(OK);
340   return id_list;
341 }
342
343 //=============================================================================
344 /*!
345  *  TransferData
346  */
347 //=============================================================================
348 bool GEOMImpl_IInsertOperations::TransferData
349                           (const Handle(GEOM_Object)      &theObjectFrom,
350                            const Handle(GEOM_Object)      &theObjectTo,
351                            const int                       theFindMethod,
352                                  std::list<TransferDatum> &theResult)
353 {
354   SetErrorCode(KO);
355
356   if (theObjectFrom.IsNull() || theObjectTo.IsNull()) {
357     return false;
358   }
359
360   //Add a new Transfer Data object object
361   Handle(GEOM_Object) aTDObj =
362     GetEngine()->AddObject(GetDocID(), GEOM_TRANSFER_DATA);
363
364   //Add a Transfer Data function for created object
365   Handle(GEOM_Function) aFunction =
366     aTDObj->AddFunction(GEOMImpl_CopyDriver::GetID(), TRANSFER_DATA);
367
368   //Check if the function is set correctly
369   if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) {
370     return false;
371   }
372
373   Handle(GEOM_Function) aFunctionFrom = theObjectFrom->GetLastFunction();
374   Handle(GEOM_Function) aFunctionTo   = theObjectTo->GetLastFunction();
375
376   if (aFunctionFrom.IsNull() || aFunctionTo.IsNull()) {
377     return false;
378   }
379
380   GEOMImpl_ITransferData aTD(aFunction);
381
382   aTD.SetRef1(aFunctionFrom);
383   aTD.SetRef2(aFunctionTo);
384   aTD.SetFindMethod(theFindMethod);
385
386   // Transfer data
387   try {
388     OCC_CATCH_SIGNALS;
389     if (!GetSolver()->ComputeFunction(aFunction)) {
390       SetErrorCode("Transfer data failed");
391       return false;
392     }
393   }
394   catch (Standard_Failure) {
395     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
396     SetErrorCode(aFail->GetMessageString());
397     return false;
398   }
399
400   // Fill result list of data.
401   theResult.clear();
402
403   Handle(TColStd_HArray1OfExtendedString) aDatumName   = aTD.GetDatumName();
404   Handle(TColStd_HArray1OfInteger)        aDatumMaxVal = aTD.GetDatumMaxVal();
405   Handle(TColStd_HArray1OfInteger)        aDatumVal    = aTD.GetDatumVal();
406
407   if (!aDatumName.IsNull() && !aDatumMaxVal.IsNull() && !aDatumVal.IsNull()) {
408     Standard_Integer i;
409     Standard_Integer aNbDatum = aDatumName->Length();
410
411     for (i = 1; i <= aNbDatum; ++i) {
412       if (aDatumMaxVal->Value(i) > 0) {
413         TransferDatum aDatum;
414
415         aDatum.myName      = TCollection_AsciiString(aDatumName->Value(i));
416         aDatum.myNumber    = aDatumVal->Value(i);
417         aDatum.myMaxNumber = aDatumMaxVal->Value(i);
418         theResult.push_back(aDatum);
419       }
420     }
421   }
422
423   //Make a Python command
424   GEOM::TPythonDump pd (aFunction);
425   pd << "geompy.TransferData(" << theObjectFrom << ", " << theObjectTo;
426   pd << ", GEOM.";
427
428   switch (theFindMethod) {
429   case TD_GET_IN_PLACE:
430     pd << "FSM_GetInPlace";
431     break;
432   case TD_GET_IN_PLACE_OLD:
433     pd << "FSM_GetInPlace_Old";
434     break;
435   case TD_GET_IN_PLACE_BY_HISTORY:
436   default:
437     pd << "FSM_GetInPlaceByHistory";
438     break;
439   }
440   pd << ")";
441
442   SetErrorCode(OK);
443
444   return true;
445 }