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 <Basics_OCCTVersion.hxx>
45 #include "utilities.h"
47 #include <Utils_ExceptHandlers.hxx>
49 #include <TFunction_DriverTable.hxx>
50 #include <TFunction_Driver.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_HArray1OfReal.hxx>
68 #include <Standard_Failure.hxx>
69 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
71 //=============================================================================
75 //=============================================================================
76 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
77 : GEOM_IOperations(theEngine, theDocID)
79 MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
80 myShapesOperations = new GEOMImpl_IShapesOperations(GetEngine(), GetDocID());
81 myGroupOperations = new GEOMImpl_IGroupOperations(GetEngine(), GetDocID());
82 myFieldOperations = new GEOMImpl_IFieldOperations(GetEngine(), GetDocID());
85 //=============================================================================
89 //=============================================================================
90 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
92 MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
93 delete myShapesOperations;
94 delete myGroupOperations;
95 delete myFieldOperations;
98 //=============================================================================
102 //=============================================================================
103 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy (Handle(GEOM_Object) theOriginal)
107 if (theOriginal.IsNull()) return NULL;
109 //Add a new Copy object
110 Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
112 //Add a Copy function for creation a copy object
113 Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
115 //Check if the function is set correctly
116 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
118 GEOMImpl_ICopy aCI(aFunction);
120 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
121 if (aRefFunction.IsNull()) return NULL;
123 aCI.SetOriginal(aRefFunction);
125 //Compute the Copy value
128 if (!GetSolver()->ComputeFunction(aFunction)) {
129 SetErrorCode("Copy driver failed");
133 catch (Standard_Failure) {
134 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
135 SetErrorCode(aFail->GetMessageString());
139 //Make a Python command
140 GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
146 //=============================================================================
150 //=============================================================================
151 void GEOMImpl_IInsertOperations::Export
152 (const Handle(GEOM_Object) theOriginal,
153 const TCollection_AsciiString& theFileName,
154 const TCollection_AsciiString& theFormatName)
158 if (theOriginal.IsNull()) return;
160 if ( !GEOMImpl_IECallBack::GetCallBack( theFormatName )->Export( GetDocID(), theOriginal, theFileName, theFormatName ) )
165 //=============================================================================
169 //=============================================================================
170 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IInsertOperations::Import
171 (const TCollection_AsciiString& theFileName,
172 const TCollection_AsciiString& theFormatName)
176 if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
178 Handle(TColStd_HSequenceOfTransient) aSeq =
179 GEOMImpl_IECallBack::GetCallBack( theFormatName )->Import( GetDocID(), theFormatName, theFileName );
184 //=============================================================================
188 //=============================================================================
189 TCollection_AsciiString GEOMImpl_IInsertOperations::ReadValue
190 (const TCollection_AsciiString& theFileName,
191 const TCollection_AsciiString& theFormatName,
192 const TCollection_AsciiString& theParameterName)
195 TCollection_AsciiString aValue;
196 if (theFileName.IsEmpty() || theFormatName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
198 aValue = GEOMImpl_IECallBack::GetCallBack( theFormatName )->ReadValue( GetDocID(), theFileName, theFormatName, theParameterName );
204 //=============================================================================
208 //=============================================================================
209 Handle(GEOM_Object) GEOMImpl_IInsertOperations::RestoreShape (std::istringstream& theStream)
213 //Add a new result object
214 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
216 //Add a Copy function
217 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
218 if (aFunction.IsNull()) return NULL;
220 //Check if the function is set correctly
221 if (aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
223 //Read a shape from the stream
226 BRepTools::Read(aShape, theStream, B);
227 if (aShape.IsNull()) {
228 SetErrorCode("RestoreShape error: BREP reading failed");
232 aFunction->SetValue(aShape);
234 //Special dump to avoid restored shapes publication.
235 //See correcponding code in GEOM_Engine.cxx (method ProcessFunction)
236 //GEOM::TPythonDump(aFunction) << "#";
240 if ( const char* env_var = getenv( "GEOM_IGNORE_RESTORE_SHAPE" ) )
241 ignore = atoi( env_var ) > 1;
244 GEOM::TPythonDump(aFunction) << result
245 << " = 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 Handle(TColStd_HArray1OfByte) aTexture;
261 FILE* fp = fopen(theTextureFile.ToCString(), "r");
264 std::list<std::string> lines;
268 if ((fgets(buffer, 4096, fp)) == NULL) break;
269 int aLen = strlen(buffer);
270 if (buffer[aLen-1] == '\n') buffer[aLen-1] = '\0';
271 lines.push_back(buffer);
272 maxlen = std::max(maxlen, (int)strlen(buffer));
277 int lenbytes = maxlen/8;
278 if (maxlen%8) lenbytes++;
280 if (lenbytes == 0 || lines.empty())
283 std::list<unsigned char> bytedata;
284 std::list<std::string>::const_iterator it;
285 for (it = lines.begin(); it != lines.end(); ++it) {
286 std::string line = *it;
287 int lenline = (line.size()/8 + (line.size()%8 ? 1 : 0)) * 8;
288 for (int i = 0; i < lenline/8; i++) {
289 unsigned char byte = 0;
290 for (int j = 0; j < 8; j++)
291 byte = (byte << 1) + ( i*8+j < line.size() && line[i*8+j] != '0' ? 1 : 0 );
292 bytedata.push_back(byte);
294 for (int i = lenline/8; i < lenbytes; i++)
295 bytedata.push_back((unsigned char)0);
298 if (bytedata.empty() || bytedata.size() != lines.size()*lenbytes)
301 aTexture = new TColStd_HArray1OfByte (1, lines.size()*lenbytes);
303 std::list<unsigned char>::iterator bdit;
305 for (i = 1, bdit = bytedata.begin(); bdit != bytedata.end(); ++bdit, ++i)
306 aTexture->SetValue(i, (Standard_Byte)(*bdit));
308 int aTextureId = GetEngine()->addTexture(GetDocID(), lenbytes*8, lines.size(), aTexture, theTextureFile);
309 if (aTextureId > 0) SetErrorCode(OK);
313 int GEOMImpl_IInsertOperations::AddTexture(int theWidth, int theHeight,
314 const Handle(TColStd_HArray1OfByte)& theTexture)
317 int aTextureId = GetEngine()->addTexture(GetDocID(), theWidth, theHeight, theTexture);
318 if (aTextureId > 0) SetErrorCode(OK);
322 Handle(TColStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
323 int& theWidth, int& theHeight)
327 Handle(TColStd_HArray1OfByte) aTexture;
329 theWidth = theHeight = 0;
330 TCollection_AsciiString aFileName;
332 if (theTextureId <= 0)
335 aTexture = GetEngine()->getTexture(GetDocID(), theTextureId, theWidth, theHeight, aFileName);
337 if (theWidth > 0 && theHeight > 0 && aTexture->Length() > 0) SetErrorCode(OK);
342 std::list<int> GEOMImpl_IInsertOperations::GetAllTextures()
345 std::list<int> id_list = GetEngine()->getAllTextures(GetDocID());
350 //=============================================================================
354 //=============================================================================
355 bool GEOMImpl_IInsertOperations::TransferData
356 (const Handle(GEOM_Object) &theObjectFrom,
357 const Handle(GEOM_Object) &theObjectTo,
358 const int theFindMethod,
359 std::list<TransferDatum> &theResult)
363 if (theObjectFrom.IsNull() || theObjectTo.IsNull()) {
367 //Add a new Transfer Data object object
368 Handle(GEOM_Object) aTDObj =
369 GetEngine()->AddObject(GetDocID(), GEOM_TRANSFER_DATA);
371 //Add a Transfer Data function for created object
372 Handle(GEOM_Function) aFunction =
373 aTDObj->AddFunction(GEOMImpl_CopyDriver::GetID(), TRANSFER_DATA);
375 //Check if the function is set correctly
376 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) {
380 Handle(GEOM_Function) aFunctionFrom = theObjectFrom->GetLastFunction();
381 Handle(GEOM_Function) aFunctionTo = theObjectTo->GetLastFunction();
383 if (aFunctionFrom.IsNull() || aFunctionTo.IsNull()) {
387 GEOMImpl_ITransferData aTD(aFunction);
389 aTD.SetRef1(aFunctionFrom);
390 aTD.SetRef2(aFunctionTo);
391 aTD.SetFindMethod(theFindMethod);
396 if (!GetSolver()->ComputeFunction(aFunction)) {
397 SetErrorCode("Transfer data failed");
401 catch (Standard_Failure) {
402 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
403 SetErrorCode(aFail->GetMessageString());
407 // Fill result list of data.
410 Handle(TColStd_HArray1OfExtendedString) aDatumName = aTD.GetDatumName();
411 Handle(TColStd_HArray1OfInteger) aDatumMaxVal = aTD.GetDatumMaxVal();
412 Handle(TColStd_HArray1OfInteger) aDatumVal = aTD.GetDatumVal();
414 if (!aDatumName.IsNull() && !aDatumMaxVal.IsNull() && !aDatumVal.IsNull()) {
416 Standard_Integer aNbDatum = aDatumName->Length();
418 for (i = 1; i <= aNbDatum; ++i) {
419 if (aDatumMaxVal->Value(i) > 0) {
420 TransferDatum aDatum;
422 aDatum.myName = TCollection_AsciiString(aDatumName->Value(i));
423 aDatum.myNumber = aDatumVal->Value(i);
424 aDatum.myMaxNumber = aDatumMaxVal->Value(i);
425 theResult.push_back(aDatum);
430 //Make a Python command
431 GEOM::TPythonDump pd (aFunction);
432 pd << "geompy.TransferData(" << theObjectFrom << ", " << theObjectTo;
435 switch (theFindMethod) {
436 case TD_GET_IN_PLACE:
437 pd << "FSM_GetInPlace";
439 case TD_GET_IN_PLACE_OLD:
440 pd << "FSM_GetInPlace_Old";
442 case TD_GET_IN_PLACE_BY_HISTORY:
444 pd << "FSM_GetInPlaceByHistory";