1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include <Standard_Stream.hxx>
25 #include <GEOMImpl_IInsertOperations.hxx>
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"
38 #include <GEOM_Function.hxx>
39 #include <GEOM_PythonDump.hxx>
40 #include "GEOM_ISubShape.hxx"
42 #include <Basics_OCCTVersion.hxx>
44 #include "utilities.h"
46 #include <Utils_ExceptHandlers.hxx>
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>
60 #include <TopoDS_Vertex.hxx>
61 #include <BRep_Builder.hxx>
62 #include <BRep_Tool.hxx>
63 #include <BRepTools.hxx>
66 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
67 #include <TColStd_HArray1OfByte.hxx>
68 #include <TColStd_HArray1OfReal.hxx>
70 #include <TDataStd_HArray1OfByte.hxx>
73 #include <Standard_Failure.hxx>
74 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
76 //=============================================================================
80 //=============================================================================
81 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
82 : GEOM_IOperations(theEngine, theDocID)
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());
90 //=============================================================================
94 //=============================================================================
95 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
97 MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
98 delete myShapesOperations;
99 delete myGroupOperations;
100 delete myFieldOperations;
103 //=============================================================================
107 //=============================================================================
108 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy (Handle(GEOM_Object) theOriginal)
112 if (theOriginal.IsNull()) return NULL;
114 //Add a new Copy object
115 Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
117 //Add a Copy function for creation a copy object
118 Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
120 //Check if the function is set correctly
121 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
123 GEOMImpl_ICopy aCI(aFunction);
125 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
126 if (aRefFunction.IsNull()) return NULL;
128 aCI.SetOriginal(aRefFunction);
130 //Compute the Copy value
132 #if OCC_VERSION_LARGE > 0x06010000
135 if (!GetSolver()->ComputeFunction(aFunction)) {
136 SetErrorCode("Copy driver failed");
140 catch (Standard_Failure) {
141 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
142 SetErrorCode(aFail->GetMessageString());
146 //Make a Python command
147 GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
153 //=============================================================================
157 //=============================================================================
158 void GEOMImpl_IInsertOperations::Export
159 (const Handle(GEOM_Object) theOriginal,
160 const TCollection_AsciiString& theFileName,
161 const TCollection_AsciiString& theFormatName)
165 if (theOriginal.IsNull()) return;
167 if ( !GEOMImpl_IECallBack::GetCallBack( theFormatName )->Export( GetDocID(), theOriginal, theFileName, theFormatName ) )
172 //=============================================================================
176 //=============================================================================
177 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IInsertOperations::Import
178 (const TCollection_AsciiString& theFileName,
179 const TCollection_AsciiString& theFormatName)
183 if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
185 Handle(TColStd_HSequenceOfTransient) aSeq =
186 GEOMImpl_IECallBack::GetCallBack( theFormatName )->Import( GetDocID(), theFormatName, theFileName );
191 //=============================================================================
195 //=============================================================================
196 TCollection_AsciiString GEOMImpl_IInsertOperations::ReadValue
197 (const TCollection_AsciiString& theFileName,
198 const TCollection_AsciiString& theFormatName,
199 const TCollection_AsciiString& theParameterName)
202 TCollection_AsciiString aValue;
203 if (theFileName.IsEmpty() || theFormatName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
205 aValue = GEOMImpl_IECallBack::GetCallBack( theFormatName )->ReadValue( GetDocID(), theFileName, theFormatName, theParameterName );
211 //=============================================================================
215 //=============================================================================
216 Handle(GEOM_Object) GEOMImpl_IInsertOperations::RestoreShape (std::istringstream& theStream)
220 //Add a new result object
221 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
223 //Add a Copy function
224 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
225 if (aFunction.IsNull()) return NULL;
227 //Check if the function is set correctly
228 if (aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
230 //Read a shape from the stream
233 BRepTools::Read(aShape, theStream, B);
234 if (aShape.IsNull()) {
235 SetErrorCode("RestoreShape error: BREP reading failed");
239 aFunction->SetValue(aShape);
241 //Special dump to avoid restored shapes publication.
242 //See correcponding code in GEOM_Engine.cxx (method ProcessFunction)
243 //GEOM::TPythonDump(aFunction) << "#";
245 GEOM::TPythonDump(aFunction) << result
246 << " = geompy.RestoreShape(\"\") # the shape string has not been dump for performance reason";
253 int GEOMImpl_IInsertOperations::LoadTexture(const TCollection_AsciiString& theTextureFile)
257 if (theTextureFile.IsEmpty()) return 0;
259 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
260 Handle(TColStd_HArray1OfByte) aTexture;
262 Handle(TDataStd_HArray1OfByte) aTexture;
265 FILE* fp = fopen(theTextureFile.ToCString(), "r");
268 std::list<std::string> lines;
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));
281 int lenbytes = maxlen/8;
282 if (maxlen%8) lenbytes++;
284 if (lenbytes == 0 || lines.empty())
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);
298 for (int i = lenline/8; i < lenbytes; i++)
299 bytedata.push_back((unsigned char)0);
302 if (bytedata.empty() || bytedata.size() != lines.size()*lenbytes)
305 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
306 aTexture = new TColStd_HArray1OfByte (1, lines.size()*lenbytes);
308 aTexture = new TDataStd_HArray1OfByte (1, lines.size()*lenbytes);
311 std::list<unsigned char>::iterator bdit;
313 for (i = 1, bdit = bytedata.begin(); bdit != bytedata.end(); ++bdit, ++i)
314 aTexture->SetValue(i, (Standard_Byte)(*bdit));
316 int aTextureId = GetEngine()->addTexture(GetDocID(), lenbytes*8, lines.size(), aTexture, theTextureFile);
317 if (aTextureId > 0) SetErrorCode(OK);
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)
325 const Handle(TDataStd_HArray1OfByte)& theTexture)
329 int aTextureId = GetEngine()->addTexture(GetDocID(), theWidth, theHeight, theTexture);
330 if (aTextureId > 0) SetErrorCode(OK);
334 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
335 Handle(TColStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
337 Handle(TDataStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
339 int& theWidth, int& theHeight)
343 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
344 Handle(TColStd_HArray1OfByte) aTexture;
346 Handle(TDataStd_HArray1OfByte) aTexture;
349 theWidth = theHeight = 0;
350 TCollection_AsciiString aFileName;
352 if (theTextureId <= 0)
355 aTexture = GetEngine()->getTexture(GetDocID(), theTextureId, theWidth, theHeight, aFileName);
357 if (theWidth > 0 && theHeight > 0 && aTexture->Length() > 0) SetErrorCode(OK);
362 std::list<int> GEOMImpl_IInsertOperations::GetAllTextures()
365 std::list<int> id_list = GetEngine()->getAllTextures(GetDocID());