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