1 // Copyright (C) 2007-2015 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 <TFunction_Logbook.hxx>
52 #include <TDF_ChildIDIterator.hxx>
53 #include <TDF_Tool.hxx>
54 #include <TDataStd_Integer.hxx>
55 #include <TNaming_NamedShape.hxx>
56 #include <TDataStd_Comment.hxx>
57 #include <TopTools_IndexedMapOfShape.hxx>
61 #include <TopoDS_Vertex.hxx>
62 #include <BRep_Builder.hxx>
63 #include <BRep_Tool.hxx>
64 #include <BRepTools.hxx>
67 #include <TColStd_HArray1OfByte.hxx>
68 #include <TColStd_HArray1OfReal.hxx>
70 #include <Standard_Failure.hxx>
71 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
73 //=============================================================================
77 //=============================================================================
78 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
79 : GEOM_IOperations(theEngine, theDocID)
81 MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
82 myShapesOperations = new GEOMImpl_IShapesOperations(GetEngine(), GetDocID());
83 myGroupOperations = new GEOMImpl_IGroupOperations(GetEngine(), GetDocID());
84 myFieldOperations = new GEOMImpl_IFieldOperations(GetEngine(), GetDocID());
87 //=============================================================================
91 //=============================================================================
92 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
94 MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
95 delete myShapesOperations;
96 delete myGroupOperations;
97 delete myFieldOperations;
100 //=============================================================================
104 //=============================================================================
105 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy (Handle(GEOM_Object) theOriginal)
109 if (theOriginal.IsNull()) return NULL;
111 //Add a new Copy object
112 Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
114 //Add a Copy function for creation a copy object
115 Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
117 //Check if the function is set correctly
118 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
120 GEOMImpl_ICopy aCI(aFunction);
122 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
123 if (aRefFunction.IsNull()) return NULL;
125 aCI.SetOriginal(aRefFunction);
127 //Compute the Copy value
130 if (!GetSolver()->ComputeFunction(aFunction)) {
131 SetErrorCode("Copy driver failed");
135 catch (Standard_Failure) {
136 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
137 SetErrorCode(aFail->GetMessageString());
141 //Make a Python command
142 GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
148 //=============================================================================
152 //=============================================================================
153 void GEOMImpl_IInsertOperations::Export
154 (const Handle(GEOM_Object) theOriginal,
155 const TCollection_AsciiString& theFileName,
156 const TCollection_AsciiString& theFormatName)
160 if (theOriginal.IsNull()) return;
162 if ( !GEOMImpl_IECallBack::GetCallBack( theFormatName )->Export( GetDocID(), theOriginal, theFileName, theFormatName ) )
167 //=============================================================================
171 //=============================================================================
172 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IInsertOperations::Import
173 (const TCollection_AsciiString& theFileName,
174 const TCollection_AsciiString& theFormatName)
178 if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
180 Handle(TColStd_HSequenceOfTransient) aSeq =
181 GEOMImpl_IECallBack::GetCallBack( theFormatName )->Import( GetDocID(), theFormatName, theFileName );
186 //=============================================================================
190 //=============================================================================
191 TCollection_AsciiString GEOMImpl_IInsertOperations::ReadValue
192 (const TCollection_AsciiString& theFileName,
193 const TCollection_AsciiString& theFormatName,
194 const TCollection_AsciiString& theParameterName)
197 TCollection_AsciiString aValue;
198 if (theFileName.IsEmpty() || theFormatName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
200 aValue = GEOMImpl_IECallBack::GetCallBack( theFormatName )->ReadValue( GetDocID(), theFileName, theFormatName, theParameterName );
206 //=============================================================================
210 //=============================================================================
211 Handle(GEOM_Object) GEOMImpl_IInsertOperations::RestoreShape (std::istringstream& theStream)
215 //Add a new result object
216 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
218 //Add a Copy function
219 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
220 if (aFunction.IsNull()) return NULL;
222 //Check if the function is set correctly
223 if (aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
225 //Read a shape from the stream
228 BRepTools::Read(aShape, theStream, B);
229 if (aShape.IsNull()) {
230 SetErrorCode("RestoreShape error: BREP reading failed");
234 aFunction->SetValue(aShape);
236 //Special dump to avoid restored shapes publication.
237 //See correcponding code in GEOM_Engine.cxx (method ProcessFunction)
238 //GEOM::TPythonDump(aFunction) << "#";
240 GEOM::TPythonDump(aFunction) << result
241 << " = geompy.RestoreShape(\"\") # the shape string has not been dump for performance reason";
248 int GEOMImpl_IInsertOperations::LoadTexture(const TCollection_AsciiString& theTextureFile)
252 if (theTextureFile.IsEmpty()) return 0;
254 Handle(TColStd_HArray1OfByte) aTexture;
256 FILE* fp = fopen(theTextureFile.ToCString(), "r");
259 std::list<std::string> lines;
263 if ((fgets(buffer, 4096, fp)) == NULL) break;
264 int aLen = strlen(buffer);
265 if (buffer[aLen-1] == '\n') buffer[aLen-1] = '\0';
266 lines.push_back(buffer);
267 maxlen = std::max(maxlen, (int)strlen(buffer));
272 int lenbytes = maxlen/8;
273 if (maxlen%8) lenbytes++;
275 if (lenbytes == 0 || lines.empty())
278 std::list<unsigned char> bytedata;
279 std::list<std::string>::const_iterator it;
280 for (it = lines.begin(); it != lines.end(); ++it) {
281 std::string line = *it;
282 int lenline = (line.size()/8 + (line.size()%8 ? 1 : 0)) * 8;
283 for (int i = 0; i < lenline/8; i++) {
284 unsigned char byte = 0;
285 for (int j = 0; j < 8; j++)
286 byte = (byte << 1) + ( i*8+j < line.size() && line[i*8+j] != '0' ? 1 : 0 );
287 bytedata.push_back(byte);
289 for (int i = lenline/8; i < lenbytes; i++)
290 bytedata.push_back((unsigned char)0);
293 if (bytedata.empty() || bytedata.size() != lines.size()*lenbytes)
296 aTexture = new TColStd_HArray1OfByte (1, lines.size()*lenbytes);
298 std::list<unsigned char>::iterator bdit;
300 for (i = 1, bdit = bytedata.begin(); bdit != bytedata.end(); ++bdit, ++i)
301 aTexture->SetValue(i, (Standard_Byte)(*bdit));
303 int aTextureId = GetEngine()->addTexture(GetDocID(), lenbytes*8, lines.size(), aTexture, theTextureFile);
304 if (aTextureId > 0) SetErrorCode(OK);
308 int GEOMImpl_IInsertOperations::AddTexture(int theWidth, int theHeight,
309 const Handle(TColStd_HArray1OfByte)& theTexture)
312 int aTextureId = GetEngine()->addTexture(GetDocID(), theWidth, theHeight, theTexture);
313 if (aTextureId > 0) SetErrorCode(OK);
317 Handle(TColStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
318 int& theWidth, int& theHeight)
322 Handle(TColStd_HArray1OfByte) aTexture;
324 theWidth = theHeight = 0;
325 TCollection_AsciiString aFileName;
327 if (theTextureId <= 0)
330 aTexture = GetEngine()->getTexture(GetDocID(), theTextureId, theWidth, theHeight, aFileName);
332 if (theWidth > 0 && theHeight > 0 && aTexture->Length() > 0) SetErrorCode(OK);
337 std::list<int> GEOMImpl_IInsertOperations::GetAllTextures()
340 std::list<int> id_list = GetEngine()->getAllTextures(GetDocID());
345 //=============================================================================
349 //=============================================================================
350 bool GEOMImpl_IInsertOperations::TransferData
351 (const Handle(GEOM_Object) &theObjectFrom,
352 const Handle(GEOM_Object) &theObjectTo,
353 const int theFindMethod,
354 std::list<TransferDatum> &theResult)
358 if (theObjectFrom.IsNull() || theObjectTo.IsNull()) {
362 //Add a new Transfer Data object object
363 Handle(GEOM_Object) aTDObj =
364 GetEngine()->AddObject(GetDocID(), GEOM_TRANSFER_DATA);
366 //Add a Transfer Data function for created object
367 Handle(GEOM_Function) aFunction =
368 aTDObj->AddFunction(GEOMImpl_CopyDriver::GetID(), TRANSFER_DATA);
370 //Check if the function is set correctly
371 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) {
375 Handle(GEOM_Function) aFunctionFrom = theObjectFrom->GetLastFunction();
376 Handle(GEOM_Function) aFunctionTo = theObjectTo->GetLastFunction();
378 if (aFunctionFrom.IsNull() || aFunctionTo.IsNull()) {
382 GEOMImpl_ITransferData aTD(aFunction);
384 aTD.SetRef1(aFunctionFrom);
385 aTD.SetRef2(aFunctionTo);
386 aTD.SetFindMethod(theFindMethod);
391 if (!GetSolver()->ComputeFunction(aFunction)) {
392 SetErrorCode("Transfer data failed");
396 catch (Standard_Failure) {
397 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
398 SetErrorCode(aFail->GetMessageString());
402 // Fill result list of data.
405 Handle(TColStd_HArray1OfExtendedString) aDatumName = aTD.GetDatumName();
406 Handle(TColStd_HArray1OfInteger) aDatumMaxVal = aTD.GetDatumMaxVal();
407 Handle(TColStd_HArray1OfInteger) aDatumVal = aTD.GetDatumVal();
409 if (!aDatumName.IsNull() && !aDatumMaxVal.IsNull() && !aDatumVal.IsNull()) {
411 Standard_Integer aNbDatum = aDatumName->Length();
413 for (i = 1; i <= aNbDatum; ++i) {
414 if (aDatumMaxVal->Value(i) > 0) {
415 TransferDatum aDatum;
417 aDatum.myName = TCollection_AsciiString(aDatumName->Value(i));
418 aDatum.myNumber = aDatumVal->Value(i);
419 aDatum.myMaxNumber = aDatumMaxVal->Value(i);
420 theResult.push_back(aDatum);
425 //Make a Python command
426 GEOM::TPythonDump pd (aFunction);
427 pd << "geompy.TransferData(" << theObjectFrom << ", " << theObjectTo;
430 switch (theFindMethod) {
431 case TD_GET_IN_PLACE:
432 pd << "FSM_GetInPlace";
434 case TD_GET_IN_PLACE_OLD:
435 pd << "FSM_GetInPlace_Old";
437 case TD_GET_IN_PLACE_BY_HISTORY:
439 pd << "FSM_GetInPlaceByHistory";