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