1 // Copyright (C) 2007-2022 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"
44 #include <Utils_ExceptHandlers.hxx>
46 #include <TFunction_DriverTable.hxx>
47 #include <TFunction_Driver.hxx>
48 #include <TDF_ChildIDIterator.hxx>
49 #include <TDF_Tool.hxx>
50 #include <TDataStd_Integer.hxx>
51 #include <TNaming_NamedShape.hxx>
52 #include <TDataStd_Comment.hxx>
53 #include <TopTools_IndexedMapOfShape.hxx>
57 #include <TopoDS_Vertex.hxx>
58 #include <BRep_Builder.hxx>
59 #include <BRep_Tool.hxx>
60 #include <BRepTools.hxx>
63 #include <TColStd_HArray1OfReal.hxx>
65 #include <Standard_Failure.hxx>
66 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
68 //=============================================================================
72 //=============================================================================
73 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine)
74 : GEOM_IOperations(theEngine)
76 MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
77 myShapesOperations = new GEOMImpl_IShapesOperations(GetEngine());
78 myGroupOperations = new GEOMImpl_IGroupOperations(GetEngine());
79 myFieldOperations = new GEOMImpl_IFieldOperations(GetEngine());
82 //=============================================================================
86 //=============================================================================
87 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
89 MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
90 delete myShapesOperations;
91 delete myGroupOperations;
92 delete myFieldOperations;
95 //=============================================================================
99 //=============================================================================
100 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy (Handle(GEOM_Object) theOriginal)
104 if (theOriginal.IsNull()) return NULL;
106 //Add a new Copy object
107 Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GEOM_COPY);
109 //Add a Copy function for creation a copy object
110 Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
112 //Check if the function is set correctly
113 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
115 GEOMImpl_ICopy aCI(aFunction);
117 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
118 if (aRefFunction.IsNull()) return NULL;
120 aCI.SetOriginal(aRefFunction);
122 //Compute the Copy value
125 if (!GetSolver()->ComputeFunction(aFunction)) {
126 SetErrorCode("Copy driver failed");
130 catch (Standard_Failure& aFail) {
131 SetErrorCode(aFail.GetMessageString());
135 //Make a Python command
136 GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
142 //=============================================================================
146 //=============================================================================
147 void GEOMImpl_IInsertOperations::Export
148 (const Handle(GEOM_Object) theOriginal,
149 const TCollection_AsciiString& theFileName,
150 const TCollection_AsciiString& theFormatName)
154 if (theOriginal.IsNull()) return;
156 if ( !GEOMImpl_IECallBack::GetCallBack( theFormatName )->Export( theOriginal, theFileName, theFormatName ) )
161 //=============================================================================
165 //=============================================================================
166 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IInsertOperations::Import
167 (const TCollection_AsciiString& theFileName,
168 const TCollection_AsciiString& theFormatName)
172 if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
174 Handle(TColStd_HSequenceOfTransient) aSeq =
175 GEOMImpl_IECallBack::GetCallBack( theFormatName )->Import( theFormatName, theFileName );
180 //=============================================================================
184 //=============================================================================
185 TCollection_AsciiString GEOMImpl_IInsertOperations::ReadValue
186 (const TCollection_AsciiString& theFileName,
187 const TCollection_AsciiString& theFormatName,
188 const TCollection_AsciiString& theParameterName)
191 TCollection_AsciiString aValue;
192 if (theFileName.IsEmpty() || theFormatName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
194 aValue = GEOMImpl_IECallBack::GetCallBack( theFormatName )->ReadValue( theFileName, theFormatName, theParameterName );
200 //=============================================================================
204 //=============================================================================
205 Handle(GEOM_Object) GEOMImpl_IInsertOperations::RestoreShape (std::istringstream& theStream)
209 //Add a new result object
210 Handle(GEOM_Object) result = GetEngine()->AddObject(GEOM_COPY);
212 //Add a Copy function
213 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
214 if (aFunction.IsNull()) return NULL;
216 //Check if the function is set correctly
217 if (aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
219 //Read a shape from the stream
222 BRepTools::Read(aShape, theStream, B);
223 if (aShape.IsNull()) {
224 SetErrorCode("RestoreShape error: BREP reading failed");
228 aFunction->SetValue(aShape);
230 //Special dump to avoid restored shapes publication.
231 //See correcponding code in GEOM_Engine.cxx (method ProcessFunction)
232 //GEOM::TPythonDump(aFunction) << "#";
236 if ( const char* env_var = getenv( "GEOM_IGNORE_RESTORE_SHAPE" ) )
237 ignore = atoi( env_var ) > 1;
240 GEOM::TPythonDump(aFunction) << result
241 << " = geompy.RestoreShape(\"\") # the shape string has not been dump for performance reason";
249 int GEOMImpl_IInsertOperations::LoadTexture(const TCollection_AsciiString& theTextureFile)
253 if (theTextureFile.IsEmpty()) return 0;
255 Handle(TColStd_HArray1OfByte) aTexture;
257 FILE* fp = fopen(theTextureFile.ToCString(), "r");
260 std::list<std::string> lines;
264 if ((fgets(buffer, 4096, fp)) == NULL) break;
265 int aLen = strlen(buffer);
266 if (buffer[aLen-1] == '\n') buffer[aLen-1] = '\0';
267 lines.push_back(buffer);
268 maxlen = std::max(maxlen, (int)strlen(buffer));
273 int lenbytes = maxlen/8;
274 if (maxlen%8) lenbytes++;
276 if (lenbytes == 0 || lines.empty())
279 std::list<unsigned char> bytedata;
280 std::list<std::string>::const_iterator it;
281 for (it = lines.begin(); it != lines.end(); ++it) {
282 std::string line = *it;
283 size_t lenline = (line.size()/8 + (line.size()%8 ? 1 : 0)) * 8;
284 for (size_t i = 0; i < lenline/8; i++) {
285 unsigned char byte = 0;
286 for (int j = 0; j < 8; j++)
287 byte = (byte << 1) + ( i*8+j < line.size() && line[i*8+j] != '0' ? 1 : 0 );
288 bytedata.push_back(byte);
290 for (int i = lenline/8; i < lenbytes; i++)
291 bytedata.push_back((unsigned char)0);
294 if (bytedata.empty() || bytedata.size() != lines.size()*lenbytes)
297 aTexture = new TColStd_HArray1OfByte (1, lines.size()*lenbytes);
299 std::list<unsigned char>::iterator bdit;
301 for (i = 1, bdit = bytedata.begin(); bdit != bytedata.end(); ++bdit, ++i)
302 aTexture->SetValue(i, (Standard_Byte)(*bdit));
304 int aTextureId = GetEngine()->addTexture(lenbytes*8, lines.size(), aTexture, theTextureFile);
305 if (aTextureId > 0) SetErrorCode(OK);
309 int GEOMImpl_IInsertOperations::AddTexture(int theWidth, int theHeight,
310 const Handle(TColStd_HArray1OfByte)& theTexture)
313 int aTextureId = GetEngine()->addTexture(theWidth, theHeight, theTexture);
314 if (aTextureId > 0) SetErrorCode(OK);
318 Handle(TColStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
319 int& theWidth, int& theHeight)
323 Handle(TColStd_HArray1OfByte) aTexture;
325 theWidth = theHeight = 0;
326 TCollection_AsciiString aFileName;
328 if (theTextureId <= 0)
331 aTexture = GetEngine()->getTexture(theTextureId, theWidth, theHeight, aFileName);
333 if (theWidth > 0 && theHeight > 0 && aTexture->Length() > 0) SetErrorCode(OK);
338 std::list<int> GEOMImpl_IInsertOperations::GetAllTextures()
341 std::list<int> id_list = GetEngine()->getAllTextures();
346 //=============================================================================
350 //=============================================================================
351 bool GEOMImpl_IInsertOperations::TransferData
352 (const Handle(GEOM_Object) &theObjectFrom,
353 const Handle(GEOM_Object) &theObjectTo,
354 const int theFindMethod,
355 std::list<TransferDatum> &theResult)
359 if (theObjectFrom.IsNull() || theObjectTo.IsNull()) {
363 //Add a new Transfer Data object object
364 Handle(GEOM_Object) aTDObj =
365 GetEngine()->AddObject(GEOM_TRANSFER_DATA);
367 //Add a Transfer Data function for created object
368 Handle(GEOM_Function) aFunction =
369 aTDObj->AddFunction(GEOMImpl_CopyDriver::GetID(), TRANSFER_DATA);
371 //Check if the function is set correctly
372 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) {
376 Handle(GEOM_Function) aFunctionFrom = theObjectFrom->GetLastFunction();
377 Handle(GEOM_Function) aFunctionTo = theObjectTo->GetLastFunction();
379 if (aFunctionFrom.IsNull() || aFunctionTo.IsNull()) {
383 GEOMImpl_ITransferData aTD(aFunction);
385 aTD.SetRef1(aFunctionFrom);
386 aTD.SetRef2(aFunctionTo);
387 aTD.SetFindMethod(theFindMethod);
392 if (!GetSolver()->ComputeFunction(aFunction)) {
393 SetErrorCode("Transfer data failed");
397 catch (Standard_Failure& aFail) {
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";