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