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 #include <TColStd_HArray1OfByte.hxx>
67 #include <TColStd_HArray1OfReal.hxx>
69 #include <Standard_Failure.hxx>
70 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
72 //=============================================================================
76 //=============================================================================
77 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
78 : GEOM_IOperations(theEngine, theDocID)
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());
86 //=============================================================================
90 //=============================================================================
91 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
93 MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
94 delete myShapesOperations;
95 delete myGroupOperations;
96 delete myFieldOperations;
99 //=============================================================================
103 //=============================================================================
104 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy (Handle(GEOM_Object) theOriginal)
108 if (theOriginal.IsNull()) return NULL;
110 //Add a new Copy object
111 Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
113 //Add a Copy function for creation a copy object
114 Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
116 //Check if the function is set correctly
117 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
119 GEOMImpl_ICopy aCI(aFunction);
121 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
122 if (aRefFunction.IsNull()) return NULL;
124 aCI.SetOriginal(aRefFunction);
126 //Compute the Copy value
129 if (!GetSolver()->ComputeFunction(aFunction)) {
130 SetErrorCode("Copy driver failed");
134 catch (Standard_Failure) {
135 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
136 SetErrorCode(aFail->GetMessageString());
140 //Make a Python command
141 GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
147 //=============================================================================
151 //=============================================================================
152 void GEOMImpl_IInsertOperations::Export
153 (const Handle(GEOM_Object) theOriginal,
154 const TCollection_AsciiString& theFileName,
155 const TCollection_AsciiString& theFormatName)
159 if (theOriginal.IsNull()) return;
161 if ( !GEOMImpl_IECallBack::GetCallBack( theFormatName )->Export( GetDocID(), theOriginal, theFileName, theFormatName ) )
166 //=============================================================================
170 //=============================================================================
171 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IInsertOperations::Import
172 (const TCollection_AsciiString& theFileName,
173 const TCollection_AsciiString& theFormatName)
177 if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
179 Handle(TColStd_HSequenceOfTransient) aSeq =
180 GEOMImpl_IECallBack::GetCallBack( theFormatName )->Import( GetDocID(), theFormatName, theFileName );
185 //=============================================================================
189 //=============================================================================
190 TCollection_AsciiString GEOMImpl_IInsertOperations::ReadValue
191 (const TCollection_AsciiString& theFileName,
192 const TCollection_AsciiString& theFormatName,
193 const TCollection_AsciiString& theParameterName)
196 TCollection_AsciiString aValue;
197 if (theFileName.IsEmpty() || theFormatName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
199 aValue = GEOMImpl_IECallBack::GetCallBack( theFormatName )->ReadValue( GetDocID(), theFileName, theFormatName, theParameterName );
205 //=============================================================================
209 //=============================================================================
210 Handle(GEOM_Object) GEOMImpl_IInsertOperations::RestoreShape (std::istringstream& theStream)
214 //Add a new result object
215 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
217 //Add a Copy function
218 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
219 if (aFunction.IsNull()) return NULL;
221 //Check if the function is set correctly
222 if (aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
224 //Read a shape from the stream
227 BRepTools::Read(aShape, theStream, B);
228 if (aShape.IsNull()) {
229 SetErrorCode("RestoreShape error: BREP reading failed");
233 aFunction->SetValue(aShape);
235 //Special dump to avoid restored shapes publication.
236 //See correcponding code in GEOM_Engine.cxx (method ProcessFunction)
237 //GEOM::TPythonDump(aFunction) << "#";
239 GEOM::TPythonDump(aFunction) << result
240 << " = geompy.RestoreShape(\"\") # the shape string has not been dump for performance reason";
247 int GEOMImpl_IInsertOperations::LoadTexture(const TCollection_AsciiString& theTextureFile)
251 if (theTextureFile.IsEmpty()) return 0;
253 Handle(TColStd_HArray1OfByte) aTexture;
255 FILE* fp = fopen(theTextureFile.ToCString(), "r");
258 std::list<std::string> lines;
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));
271 int lenbytes = maxlen/8;
272 if (maxlen%8) lenbytes++;
274 if (lenbytes == 0 || lines.empty())
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);
288 for (int i = lenline/8; i < lenbytes; i++)
289 bytedata.push_back((unsigned char)0);
292 if (bytedata.empty() || bytedata.size() != lines.size()*lenbytes)
295 aTexture = new TColStd_HArray1OfByte (1, lines.size()*lenbytes);
297 std::list<unsigned char>::iterator bdit;
299 for (i = 1, bdit = bytedata.begin(); bdit != bytedata.end(); ++bdit, ++i)
300 aTexture->SetValue(i, (Standard_Byte)(*bdit));
302 int aTextureId = GetEngine()->addTexture(GetDocID(), lenbytes*8, lines.size(), aTexture, theTextureFile);
303 if (aTextureId > 0) SetErrorCode(OK);
307 int GEOMImpl_IInsertOperations::AddTexture(int theWidth, int theHeight,
308 const Handle(TColStd_HArray1OfByte)& theTexture)
311 int aTextureId = GetEngine()->addTexture(GetDocID(), theWidth, theHeight, theTexture);
312 if (aTextureId > 0) SetErrorCode(OK);
316 Handle(TColStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
317 int& theWidth, int& theHeight)
321 Handle(TColStd_HArray1OfByte) aTexture;
323 theWidth = theHeight = 0;
324 TCollection_AsciiString aFileName;
326 if (theTextureId <= 0)
329 aTexture = GetEngine()->getTexture(GetDocID(), theTextureId, theWidth, theHeight, aFileName);
331 if (theWidth > 0 && theHeight > 0 && aTexture->Length() > 0) SetErrorCode(OK);
336 std::list<int> GEOMImpl_IInsertOperations::GetAllTextures()
339 std::list<int> id_list = GetEngine()->getAllTextures(GetDocID());