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& aFail) {
132 SetErrorCode(aFail.GetMessageString());
136 //Make a Python command
137 GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
143 //=============================================================================
147 //=============================================================================
148 void GEOMImpl_IInsertOperations::Export
149 (const Handle(GEOM_Object) theOriginal,
150 const TCollection_AsciiString& theFileName,
151 const TCollection_AsciiString& theFormatName)
155 if (theOriginal.IsNull()) return;
157 if ( !GEOMImpl_IECallBack::GetCallBack( theFormatName )->Export( GetDocID(), theOriginal, theFileName, theFormatName ) )
162 //=============================================================================
166 //=============================================================================
167 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IInsertOperations::Import
168 (const TCollection_AsciiString& theFileName,
169 const TCollection_AsciiString& theFormatName)
173 if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
175 Handle(TColStd_HSequenceOfTransient) aSeq =
176 GEOMImpl_IECallBack::GetCallBack( theFormatName )->Import( GetDocID(), theFormatName, theFileName );
181 //=============================================================================
185 //=============================================================================
186 TCollection_AsciiString GEOMImpl_IInsertOperations::ReadValue
187 (const TCollection_AsciiString& theFileName,
188 const TCollection_AsciiString& theFormatName,
189 const TCollection_AsciiString& theParameterName)
192 TCollection_AsciiString aValue;
193 if (theFileName.IsEmpty() || theFormatName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
195 aValue = GEOMImpl_IECallBack::GetCallBack( theFormatName )->ReadValue( GetDocID(), theFileName, theFormatName, theParameterName );
201 //=============================================================================
205 //=============================================================================
206 Handle(GEOM_Object) GEOMImpl_IInsertOperations::RestoreShape (std::istringstream& theStream)
210 //Add a new result object
211 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
213 //Add a Copy function
214 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
215 if (aFunction.IsNull()) return NULL;
217 //Check if the function is set correctly
218 if (aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
220 //Read a shape from the stream
223 BRepTools::Read(aShape, theStream, B);
224 if (aShape.IsNull()) {
225 SetErrorCode("RestoreShape error: BREP reading failed");
229 aFunction->SetValue(aShape);
231 //Special dump to avoid restored shapes publication.
232 //See correcponding code in GEOM_Engine.cxx (method ProcessFunction)
233 //GEOM::TPythonDump(aFunction) << "#";
237 if ( const char* env_var = getenv( "GEOM_IGNORE_RESTORE_SHAPE" ) )
238 ignore = atoi( env_var ) > 1;
241 GEOM::TPythonDump(aFunction) << result
242 << " = geompy.RestoreShape(\"\") # the shape string has not been dump for performance reason";
250 int GEOMImpl_IInsertOperations::LoadTexture(const TCollection_AsciiString& theTextureFile)
254 if (theTextureFile.IsEmpty()) return 0;
256 Handle(TColStd_HArray1OfByte) aTexture;
258 FILE* fp = fopen(theTextureFile.ToCString(), "r");
261 std::list<std::string> lines;
265 if ((fgets(buffer, 4096, fp)) == NULL) break;
266 int aLen = strlen(buffer);
267 if (buffer[aLen-1] == '\n') buffer[aLen-1] = '\0';
268 lines.push_back(buffer);
269 maxlen = std::max(maxlen, (int)strlen(buffer));
274 int lenbytes = maxlen/8;
275 if (maxlen%8) lenbytes++;
277 if (lenbytes == 0 || lines.empty())
280 std::list<unsigned char> bytedata;
281 std::list<std::string>::const_iterator it;
282 for (it = lines.begin(); it != lines.end(); ++it) {
283 std::string line = *it;
284 size_t lenline = (line.size()/8 + (line.size()%8 ? 1 : 0)) * 8;
285 for (size_t i = 0; i < lenline/8; i++) {
286 unsigned char byte = 0;
287 for (int j = 0; j < 8; j++)
288 byte = (byte << 1) + ( i*8+j < line.size() && line[i*8+j] != '0' ? 1 : 0 );
289 bytedata.push_back(byte);
291 for (int i = lenline/8; i < lenbytes; i++)
292 bytedata.push_back((unsigned char)0);
295 if (bytedata.empty() || bytedata.size() != lines.size()*lenbytes)
298 aTexture = new TColStd_HArray1OfByte (1, lines.size()*lenbytes);
300 std::list<unsigned char>::iterator bdit;
302 for (i = 1, bdit = bytedata.begin(); bdit != bytedata.end(); ++bdit, ++i)
303 aTexture->SetValue(i, (Standard_Byte)(*bdit));
305 int aTextureId = GetEngine()->addTexture(GetDocID(), lenbytes*8, lines.size(), aTexture, theTextureFile);
306 if (aTextureId > 0) SetErrorCode(OK);
310 int GEOMImpl_IInsertOperations::AddTexture(int theWidth, int theHeight,
311 const Handle(TColStd_HArray1OfByte)& theTexture)
314 int aTextureId = GetEngine()->addTexture(GetDocID(), theWidth, theHeight, theTexture);
315 if (aTextureId > 0) SetErrorCode(OK);
319 Handle(TColStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
320 int& theWidth, int& theHeight)
324 Handle(TColStd_HArray1OfByte) aTexture;
326 theWidth = theHeight = 0;
327 TCollection_AsciiString aFileName;
329 if (theTextureId <= 0)
332 aTexture = GetEngine()->getTexture(GetDocID(), theTextureId, theWidth, theHeight, aFileName);
334 if (theWidth > 0 && theHeight > 0 && aTexture->Length() > 0) SetErrorCode(OK);
339 std::list<int> GEOMImpl_IInsertOperations::GetAllTextures()
342 std::list<int> id_list = GetEngine()->getAllTextures(GetDocID());
347 //=============================================================================
351 //=============================================================================
352 bool GEOMImpl_IInsertOperations::TransferData
353 (const Handle(GEOM_Object) &theObjectFrom,
354 const Handle(GEOM_Object) &theObjectTo,
355 const int theFindMethod,
356 std::list<TransferDatum> &theResult)
360 if (theObjectFrom.IsNull() || theObjectTo.IsNull()) {
364 //Add a new Transfer Data object object
365 Handle(GEOM_Object) aTDObj =
366 GetEngine()->AddObject(GetDocID(), GEOM_TRANSFER_DATA);
368 //Add a Transfer Data function for created object
369 Handle(GEOM_Function) aFunction =
370 aTDObj->AddFunction(GEOMImpl_CopyDriver::GetID(), TRANSFER_DATA);
372 //Check if the function is set correctly
373 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) {
377 Handle(GEOM_Function) aFunctionFrom = theObjectFrom->GetLastFunction();
378 Handle(GEOM_Function) aFunctionTo = theObjectTo->GetLastFunction();
380 if (aFunctionFrom.IsNull() || aFunctionTo.IsNull()) {
384 GEOMImpl_ITransferData aTD(aFunction);
386 aTD.SetRef1(aFunctionFrom);
387 aTD.SetRef2(aFunctionTo);
388 aTD.SetFindMethod(theFindMethod);
393 if (!GetSolver()->ComputeFunction(aFunction)) {
394 SetErrorCode("Transfer data failed");
398 catch (Standard_Failure& aFail) {
399 SetErrorCode(aFail.GetMessageString());
403 // Fill result list of data.
406 Handle(TColStd_HArray1OfExtendedString) aDatumName = aTD.GetDatumName();
407 Handle(TColStd_HArray1OfInteger) aDatumMaxVal = aTD.GetDatumMaxVal();
408 Handle(TColStd_HArray1OfInteger) aDatumVal = aTD.GetDatumVal();
410 if (!aDatumName.IsNull() && !aDatumMaxVal.IsNull() && !aDatumVal.IsNull()) {
412 Standard_Integer aNbDatum = aDatumName->Length();
414 for (i = 1; i <= aNbDatum; ++i) {
415 if (aDatumMaxVal->Value(i) > 0) {
416 TransferDatum aDatum;
418 aDatum.myName = TCollection_AsciiString(aDatumName->Value(i));
419 aDatum.myNumber = aDatumVal->Value(i);
420 aDatum.myMaxNumber = aDatumMaxVal->Value(i);
421 theResult.push_back(aDatum);
426 //Make a Python command
427 GEOM::TPythonDump pd (aFunction);
428 pd << "geompy.TransferData(" << theObjectFrom << ", " << theObjectTo;
431 switch (theFindMethod) {
432 case TD_GET_IN_PLACE:
433 pd << "FSM_GetInPlace";
435 case TD_GET_IN_PLACE_OLD:
436 pd << "FSM_GetInPlace_Old";
438 case TD_GET_IN_PLACE_BY_HISTORY:
440 pd << "FSM_GetInPlaceByHistory";