Salome HOME
22752: [EDF] Provide explicit feedback on what has been done by Shape Processing...
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IInsertOperations.cxx
1 // Copyright (C) 2007-2014  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_Types.hxx>
33 #include "GEOMImpl_IShapesOperations.hxx"
34 #include "GEOMImpl_IGroupOperations.hxx"
35 #include "GEOMImpl_IFieldOperations.hxx"
36 #include "GEOMImpl_IECallBack.hxx"
37
38 #include <GEOM_Function.hxx>
39 #include <GEOM_PythonDump.hxx>
40 #include "GEOM_ISubShape.hxx"
41
42 #include <Basics_OCCTVersion.hxx>
43
44 #include "utilities.h"
45 #include <OpUtil.hxx>
46 #include <Utils_ExceptHandlers.hxx>
47
48 #include <TFunction_DriverTable.hxx>
49 #include <TFunction_Driver.hxx>
50 #include <TFunction_Logbook.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_HArray1OfByte.hxx>
67 #include <TColStd_HArray1OfReal.hxx>
68
69 #include <Standard_Failure.hxx>
70 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
71
72 //=============================================================================
73 /*!
74  *  constructor
75  */
76 //=============================================================================
77 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
78 : GEOM_IOperations(theEngine, theDocID)
79 {
80   MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
81   myShapesOperations = new GEOMImpl_IShapesOperations(GetEngine(), GetDocID());
82   myGroupOperations = new GEOMImpl_IGroupOperations(GetEngine(), GetDocID());
83   myFieldOperations = new GEOMImpl_IFieldOperations(GetEngine(), GetDocID());
84 }
85
86 //=============================================================================
87 /*!
88  *  destructor
89  */
90 //=============================================================================
91 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
92 {
93   MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
94   delete myShapesOperations;
95   delete myGroupOperations;
96   delete myFieldOperations;
97 }
98
99 //=============================================================================
100 /*!
101  *  MakeCopy
102  */
103 //=============================================================================
104 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy (Handle(GEOM_Object) theOriginal)
105 {
106   SetErrorCode(KO);
107
108   if (theOriginal.IsNull()) return NULL;
109
110   //Add a new Copy object
111   Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
112
113   //Add a Copy function for creation a copy object
114   Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
115
116   //Check if the function is set correctly
117   if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
118
119   GEOMImpl_ICopy aCI(aFunction);
120
121   Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
122   if (aRefFunction.IsNull()) return NULL;
123
124   aCI.SetOriginal(aRefFunction);
125
126   //Compute the Copy value
127   try {
128     OCC_CATCH_SIGNALS;
129     if (!GetSolver()->ComputeFunction(aFunction)) {
130       SetErrorCode("Copy driver failed");
131       return NULL;
132     }
133   }
134   catch (Standard_Failure) {
135     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
136     SetErrorCode(aFail->GetMessageString());
137     return NULL;
138   }
139
140   //Make a Python command
141   GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
142
143   SetErrorCode(OK);
144   return aCopy;
145 }
146
147 //=============================================================================
148 /*!
149  *  Export
150  */
151 //=============================================================================
152 void GEOMImpl_IInsertOperations::Export
153                      (const Handle(GEOM_Object)      theOriginal,
154                       const TCollection_AsciiString& theFileName,
155                       const TCollection_AsciiString& theFormatName)
156 {
157   SetErrorCode(KO);
158
159   if (theOriginal.IsNull()) return;
160
161   if ( !GEOMImpl_IECallBack::GetCallBack( theFormatName )->Export( GetDocID(), theOriginal, theFileName, theFormatName ) )
162     return;
163   SetErrorCode(OK);
164 }
165
166 //=============================================================================
167 /*!
168  *  Import
169  */
170 //=============================================================================
171 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IInsertOperations::Import
172                                  (const TCollection_AsciiString& theFileName,
173                                   const TCollection_AsciiString& theFormatName)
174 {
175   SetErrorCode(KO);
176
177   if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
178
179   Handle(TColStd_HSequenceOfTransient) aSeq =
180     GEOMImpl_IECallBack::GetCallBack( theFormatName )->Import( GetDocID(), theFormatName, theFileName );
181   SetErrorCode(OK);
182   return aSeq;
183 }
184
185 //=============================================================================
186 /*!
187  *  ReadValue
188  */
189 //=============================================================================
190 TCollection_AsciiString GEOMImpl_IInsertOperations::ReadValue
191                                  (const TCollection_AsciiString& theFileName,
192                                   const TCollection_AsciiString& theFormatName,
193                                   const TCollection_AsciiString& theParameterName)
194 {
195   SetErrorCode(KO);
196   TCollection_AsciiString aValue;
197   if (theFileName.IsEmpty() || theFormatName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
198
199   aValue = GEOMImpl_IECallBack::GetCallBack( theFormatName )->ReadValue( GetDocID(), theFileName, theFormatName, theParameterName );
200
201   SetErrorCode(OK);
202   return aValue;
203 }
204
205 //=============================================================================
206 /*!
207  *  RestoreShape
208  */
209 //=============================================================================
210 Handle(GEOM_Object) GEOMImpl_IInsertOperations::RestoreShape (std::istringstream& theStream)
211 {
212   SetErrorCode(KO);
213
214   //Add a new result object
215   Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
216
217   //Add a Copy function
218   Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
219   if (aFunction.IsNull()) return NULL;
220
221   //Check if the function is set correctly
222   if (aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
223
224   //Read a shape from the stream
225   TopoDS_Shape aShape;
226   BRep_Builder B;
227   BRepTools::Read(aShape, theStream, B);
228   if (aShape.IsNull()) {
229     SetErrorCode("RestoreShape error: BREP reading failed");
230   }
231
232   //Set function value
233   aFunction->SetValue(aShape);
234
235   //Special dump to avoid restored shapes publication.
236   //See correcponding code in GEOM_Engine.cxx (method ProcessFunction)
237   //GEOM::TPythonDump(aFunction) << "#";
238
239   GEOM::TPythonDump(aFunction) << result
240     << " = geompy.RestoreShape(\"\") # the shape string has not been dump for performance reason";
241
242   SetErrorCode(OK);
243
244   return result;
245 }
246
247 int GEOMImpl_IInsertOperations::LoadTexture(const TCollection_AsciiString& theTextureFile)
248 {
249   SetErrorCode(KO);
250
251   if (theTextureFile.IsEmpty()) return 0;
252
253   Handle(TColStd_HArray1OfByte) aTexture;
254
255   FILE* fp = fopen(theTextureFile.ToCString(), "r");
256   if (!fp) return 0;
257
258   std::list<std::string> lines;
259   char buffer[4096];
260   int maxlen = 0;
261   while (!feof(fp)) {
262     if ((fgets(buffer, 4096, fp)) == NULL) break;
263     int aLen = strlen(buffer);
264     if (buffer[aLen-1] == '\n') buffer[aLen-1] = '\0';
265     lines.push_back(buffer);
266     maxlen = std::max(maxlen, (int)strlen(buffer));
267   }
268
269   fclose(fp);
270
271   int lenbytes = maxlen/8;
272   if (maxlen%8) lenbytes++;
273
274   if (lenbytes == 0 || lines.empty())
275     return 0;
276
277   std::list<unsigned char> bytedata;
278   std::list<std::string>::const_iterator it;
279   for (it = lines.begin(); it != lines.end(); ++it) {
280     std::string line = *it;
281     int lenline = (line.size()/8 + (line.size()%8 ? 1 : 0)) * 8;
282     for (int i = 0; i < lenline/8; i++) {
283       unsigned char byte = 0;
284       for (int j = 0; j < 8; j++)
285         byte = (byte << 1) + ( i*8+j < line.size() && line[i*8+j] != '0' ? 1 : 0 );
286       bytedata.push_back(byte);
287     }
288     for (int i = lenline/8; i < lenbytes; i++)
289       bytedata.push_back((unsigned char)0);
290   }
291
292   if (bytedata.empty() || bytedata.size() != lines.size()*lenbytes)
293     return 0;
294
295   aTexture = new TColStd_HArray1OfByte (1, lines.size()*lenbytes);
296
297   std::list<unsigned char>::iterator bdit;
298   int i;
299   for (i = 1, bdit = bytedata.begin(); bdit != bytedata.end(); ++bdit, ++i)
300     aTexture->SetValue(i, (Standard_Byte)(*bdit));
301
302   int aTextureId = GetEngine()->addTexture(GetDocID(), lenbytes*8, lines.size(), aTexture, theTextureFile);
303   if (aTextureId > 0) SetErrorCode(OK);
304   return aTextureId;
305 }
306   
307 int GEOMImpl_IInsertOperations::AddTexture(int theWidth, int theHeight, 
308                                            const Handle(TColStd_HArray1OfByte)& theTexture)
309 {
310   SetErrorCode(KO);
311   int aTextureId = GetEngine()->addTexture(GetDocID(), theWidth, theHeight, theTexture);
312   if (aTextureId > 0) SetErrorCode(OK);
313   return aTextureId;
314 }
315
316 Handle(TColStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
317                                                                      int& theWidth, int& theHeight)
318 {
319   SetErrorCode(KO);
320   
321   Handle(TColStd_HArray1OfByte) aTexture;
322
323   theWidth = theHeight = 0;
324   TCollection_AsciiString aFileName;
325
326   if (theTextureId <= 0)
327     return aTexture;
328
329   aTexture = GetEngine()->getTexture(GetDocID(), theTextureId, theWidth, theHeight, aFileName);
330
331   if (theWidth > 0 && theHeight > 0 && aTexture->Length() > 0) SetErrorCode(OK);
332
333   return aTexture;
334 }
335
336 std::list<int> GEOMImpl_IInsertOperations::GetAllTextures()
337 {
338   SetErrorCode(KO);
339   std::list<int> id_list = GetEngine()->getAllTextures(GetDocID());
340   SetErrorCode(OK);
341   return id_list;
342 }