1 // Copyright (C) 2007-2016 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_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"
39 #include <GEOM_Function.hxx>
40 #include <GEOM_PythonDump.hxx>
41 #include "GEOM_ISubShape.hxx"
43 #include "utilities.h"
45 #include <Utils_ExceptHandlers.hxx>
47 #include <TFunction_DriverTable.hxx>
48 #include <TFunction_Driver.hxx>
49 #include <TDF_ChildIDIterator.hxx>
50 #include <TDF_Tool.hxx>
51 #include <TDataStd_Integer.hxx>
52 #include <TNaming_NamedShape.hxx>
53 #include <TDataStd_Comment.hxx>
54 #include <TopTools_IndexedMapOfShape.hxx>
58 #include <TopoDS_Vertex.hxx>
59 #include <BRep_Builder.hxx>
60 #include <BRep_Tool.hxx>
61 #include <BRepTools.hxx>
64 #include <TColStd_HArray1OfReal.hxx>
66 #include <Standard_Failure.hxx>
67 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
69 //=============================================================================
73 //=============================================================================
74 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
75 : GEOM_IOperations(theEngine, theDocID)
77 MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
78 myShapesOperations = new GEOMImpl_IShapesOperations(GetEngine(), GetDocID());
79 myGroupOperations = new GEOMImpl_IGroupOperations(GetEngine(), GetDocID());
80 myFieldOperations = new GEOMImpl_IFieldOperations(GetEngine(), GetDocID());
83 //=============================================================================
87 //=============================================================================
88 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
90 MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
91 delete myShapesOperations;
92 delete myGroupOperations;
93 delete myFieldOperations;
96 //=============================================================================
100 //=============================================================================
101 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy (Handle(GEOM_Object) theOriginal)
105 if (theOriginal.IsNull()) return NULL;
107 //Add a new Copy object
108 Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
110 //Add a Copy function for creation a copy object
111 Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
113 //Check if the function is set correctly
114 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
116 GEOMImpl_ICopy aCI(aFunction);
118 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
119 if (aRefFunction.IsNull()) return NULL;
121 aCI.SetOriginal(aRefFunction);
123 //Compute the Copy value
126 if (!GetSolver()->ComputeFunction(aFunction)) {
127 SetErrorCode("Copy driver failed");
131 catch (Standard_Failure) {
132 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
133 SetErrorCode(aFail->GetMessageString());
137 //Make a Python command
138 GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
144 //=============================================================================
148 //=============================================================================
149 void GEOMImpl_IInsertOperations::Export
150 (const Handle(GEOM_Object) theOriginal,
151 const TCollection_AsciiString& theFileName,
152 const TCollection_AsciiString& theFormatName)
156 if (theOriginal.IsNull()) return;
158 if ( !GEOMImpl_IECallBack::GetCallBack( theFormatName )->Export( GetDocID(), theOriginal, theFileName, theFormatName ) )
163 //=============================================================================
167 //=============================================================================
168 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IInsertOperations::Import
169 (const TCollection_AsciiString& theFileName,
170 const TCollection_AsciiString& theFormatName)
174 if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
176 Handle(TColStd_HSequenceOfTransient) aSeq =
177 GEOMImpl_IECallBack::GetCallBack( theFormatName )->Import( GetDocID(), theFormatName, theFileName );
182 //=============================================================================
186 //=============================================================================
187 TCollection_AsciiString GEOMImpl_IInsertOperations::ReadValue
188 (const TCollection_AsciiString& theFileName,
189 const TCollection_AsciiString& theFormatName,
190 const TCollection_AsciiString& theParameterName)
193 TCollection_AsciiString aValue;
194 if (theFileName.IsEmpty() || theFormatName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
196 aValue = GEOMImpl_IECallBack::GetCallBack( theFormatName )->ReadValue( GetDocID(), theFileName, theFormatName, theParameterName );
202 //=============================================================================
206 //=============================================================================
207 Handle(GEOM_Object) GEOMImpl_IInsertOperations::RestoreShape (std::istringstream& theStream)
211 //Add a new result object
212 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
214 //Add a Copy function
215 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
216 if (aFunction.IsNull()) return NULL;
218 //Check if the function is set correctly
219 if (aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
221 //Read a shape from the stream
224 BRepTools::Read(aShape, theStream, B);
225 if (aShape.IsNull()) {
226 SetErrorCode("RestoreShape error: BREP reading failed");
230 aFunction->SetValue(aShape);
232 //Special dump to avoid restored shapes publication.
233 //See correcponding code in GEOM_Engine.cxx (method ProcessFunction)
234 //GEOM::TPythonDump(aFunction) << "#";
238 if ( const char* env_var = getenv( "GEOM_IGNORE_RESTORE_SHAPE" ) )
239 ignore = atoi( env_var ) > 1;
242 GEOM::TPythonDump(aFunction) << result
243 << " = geompy.RestoreShape(\"\") # the shape string has not been dump for performance reason";
251 int GEOMImpl_IInsertOperations::LoadTexture(const TCollection_AsciiString& theTextureFile)
255 if (theTextureFile.IsEmpty()) return 0;
257 Handle(TColStd_HArray1OfByte) aTexture;
259 FILE* fp = fopen(theTextureFile.ToCString(), "r");
262 std::list<std::string> lines;
266 if ((fgets(buffer, 4096, fp)) == NULL) break;
267 int aLen = strlen(buffer);
268 if (buffer[aLen-1] == '\n') buffer[aLen-1] = '\0';
269 lines.push_back(buffer);
270 maxlen = std::max(maxlen, (int)strlen(buffer));
275 int lenbytes = maxlen/8;
276 if (maxlen%8) lenbytes++;
278 if (lenbytes == 0 || lines.empty())
281 std::list<unsigned char> bytedata;
282 std::list<std::string>::const_iterator it;
283 for (it = lines.begin(); it != lines.end(); ++it) {
284 std::string line = *it;
285 int lenline = (line.size()/8 + (line.size()%8 ? 1 : 0)) * 8;
286 for (int i = 0; i < lenline/8; i++) {
287 unsigned char byte = 0;
288 for (int j = 0; j < 8; j++)
289 byte = (byte << 1) + ( i*8+j < line.size() && line[i*8+j] != '0' ? 1 : 0 );
290 bytedata.push_back(byte);
292 for (int i = lenline/8; i < lenbytes; i++)
293 bytedata.push_back((unsigned char)0);
296 if (bytedata.empty() || bytedata.size() != lines.size()*lenbytes)
299 aTexture = new TColStd_HArray1OfByte (1, lines.size()*lenbytes);
301 std::list<unsigned char>::iterator bdit;
303 for (i = 1, bdit = bytedata.begin(); bdit != bytedata.end(); ++bdit, ++i)
304 aTexture->SetValue(i, (Standard_Byte)(*bdit));
306 int aTextureId = GetEngine()->addTexture(GetDocID(), lenbytes*8, lines.size(), aTexture, theTextureFile);
307 if (aTextureId > 0) SetErrorCode(OK);
311 int GEOMImpl_IInsertOperations::AddTexture(int theWidth, int theHeight,
312 const Handle(TColStd_HArray1OfByte)& theTexture)
315 int aTextureId = GetEngine()->addTexture(GetDocID(), theWidth, theHeight, theTexture);
316 if (aTextureId > 0) SetErrorCode(OK);
320 Handle(TColStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
321 int& theWidth, int& theHeight)
325 Handle(TColStd_HArray1OfByte) aTexture;
327 theWidth = theHeight = 0;
328 TCollection_AsciiString aFileName;
330 if (theTextureId <= 0)
333 aTexture = GetEngine()->getTexture(GetDocID(), theTextureId, theWidth, theHeight, aFileName);
335 if (theWidth > 0 && theHeight > 0 && aTexture->Length() > 0) SetErrorCode(OK);
340 std::list<int> GEOMImpl_IInsertOperations::GetAllTextures()
343 std::list<int> id_list = GetEngine()->getAllTextures(GetDocID());
348 //=============================================================================
352 //=============================================================================
353 bool GEOMImpl_IInsertOperations::TransferData
354 (const Handle(GEOM_Object) &theObjectFrom,
355 const Handle(GEOM_Object) &theObjectTo,
356 const int theFindMethod,
357 std::list<TransferDatum> &theResult)
361 if (theObjectFrom.IsNull() || theObjectTo.IsNull()) {
365 //Add a new Transfer Data object object
366 Handle(GEOM_Object) aTDObj =
367 GetEngine()->AddObject(GetDocID(), GEOM_TRANSFER_DATA);
369 //Add a Transfer Data function for created object
370 Handle(GEOM_Function) aFunction =
371 aTDObj->AddFunction(GEOMImpl_CopyDriver::GetID(), TRANSFER_DATA);
373 //Check if the function is set correctly
374 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) {
378 Handle(GEOM_Function) aFunctionFrom = theObjectFrom->GetLastFunction();
379 Handle(GEOM_Function) aFunctionTo = theObjectTo->GetLastFunction();
381 if (aFunctionFrom.IsNull() || aFunctionTo.IsNull()) {
385 GEOMImpl_ITransferData aTD(aFunction);
387 aTD.SetRef1(aFunctionFrom);
388 aTD.SetRef2(aFunctionTo);
389 aTD.SetFindMethod(theFindMethod);
394 if (!GetSolver()->ComputeFunction(aFunction)) {
395 SetErrorCode("Transfer data failed");
399 catch (Standard_Failure) {
400 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
401 SetErrorCode(aFail->GetMessageString());
405 // Fill result list of data.
408 Handle(TColStd_HArray1OfExtendedString) aDatumName = aTD.GetDatumName();
409 Handle(TColStd_HArray1OfInteger) aDatumMaxVal = aTD.GetDatumMaxVal();
410 Handle(TColStd_HArray1OfInteger) aDatumVal = aTD.GetDatumVal();
412 if (!aDatumName.IsNull() && !aDatumMaxVal.IsNull() && !aDatumVal.IsNull()) {
414 Standard_Integer aNbDatum = aDatumName->Length();
416 for (i = 1; i <= aNbDatum; ++i) {
417 if (aDatumMaxVal->Value(i) > 0) {
418 TransferDatum aDatum;
420 aDatum.myName = TCollection_AsciiString(aDatumName->Value(i));
421 aDatum.myNumber = aDatumVal->Value(i);
422 aDatum.myMaxNumber = aDatumMaxVal->Value(i);
423 theResult.push_back(aDatum);
428 //Make a Python command
429 GEOM::TPythonDump pd (aFunction);
430 pd << "geompy.TransferData(" << theObjectFrom << ", " << theObjectTo;
433 switch (theFindMethod) {
434 case TD_GET_IN_PLACE:
435 pd << "FSM_GetInPlace";
437 case TD_GET_IN_PLACE_OLD:
438 pd << "FSM_GetInPlace_Old";
440 case TD_GET_IN_PLACE_BY_HISTORY:
442 pd << "FSM_GetInPlaceByHistory";