From 9ef623eadebba11360f4fbad944e0035cb5f7912 Mon Sep 17 00:00:00 2001 From: jfa Date: Thu, 27 Sep 2012 14:21:09 +0000 Subject: [PATCH] Mantis issue 0021706: [CEA 578] Python access to BrepTools::Read --- idl/GEOM_Gen.idl | 11 ++++- src/GEOM/GEOM_Engine.cxx | 10 +++++ src/GEOMImpl/GEOMImpl_IInsertOperations.cxx | 41 +++++++++++++++++++ src/GEOMImpl/GEOMImpl_IInsertOperations.hxx | 2 + src/GEOM_I/GEOM_IInsertOperations_i.cc | 45 +++++++++++++++++++++ src/GEOM_I/GEOM_IInsertOperations_i.hh | 2 + src/GEOM_SWIG/GEOM_TestOthers.py | 5 +++ src/GEOM_SWIG/geompyDC.py | 31 ++++++++++++-- 8 files changed, 143 insertions(+), 4 deletions(-) diff --git a/idl/GEOM_Gen.idl b/idl/GEOM_Gen.idl index e8c5cc4dc..8f90021eb 100644 --- a/idl/GEOM_Gen.idl +++ b/idl/GEOM_Gen.idl @@ -360,7 +360,8 @@ module GEOM // # Internal methods (For sub-shape identification) // ###################################################################### /*! - * \brief Get geometric shape of the object as a byte stream + * \brief Get geometric shape of the object as a byte stream in BRep format + * \note GEOM_IInsertOperations::RestoreShape() method can be used to restore shape from a BRep stream. */ SALOMEDS::TMPFile GetShapeStream(); @@ -3278,6 +3279,14 @@ module GEOM void ExportTranslators (out string_array theFormats, out string_array thePatterns); + /*! + * \brief Read a shape from the binary stream, containing its bounding representation (BRep). + * \note GEOM_Object::GetShapeStream() method can be used to obtain the shape's BRep stream. + * \param theStream The BRep binary stream. + * \return New GEOM_Object, containing the shape, read from theStream. + */ + GEOM_Object RestoreShape (in SALOMEDS::TMPFile theStream); + /*! * \brief Load texture from file * \param theTextureFile texture file name diff --git a/src/GEOM/GEOM_Engine.cxx b/src/GEOM/GEOM_Engine.cxx index 22a5fe5c7..32d3e97c2 100644 --- a/src/GEOM/GEOM_Engine.cxx +++ b/src/GEOM/GEOM_Engine.cxx @@ -1023,6 +1023,16 @@ bool ProcessFunction(Handle(GEOM_Function)& theFunction, //Check if its internal function which doesn't requires dumping if(aDescr == "None") return false; + //Check the very specific case of RestoreShape function, + //which is not dumped, but the result can be published by the user. + //We do not publish such objects to decrease danger of dumped script failure. + if(aDescr.Value(1) == '#') { + TCollection_AsciiString anObjEntry; + TDF_Tool::Entry(theFunction->GetOwnerEntry(), anObjEntry); + theIgnoreObjs.insert(anObjEntry); + return false; + } + // 0020001 PTv, check for critical functions, which require dump of objects if (theIsPublished) { diff --git a/src/GEOMImpl/GEOMImpl_IInsertOperations.cxx b/src/GEOMImpl/GEOMImpl_IInsertOperations.cxx index 2a83dd58d..45e66e116 100644 --- a/src/GEOMImpl/GEOMImpl_IInsertOperations.cxx +++ b/src/GEOMImpl/GEOMImpl_IInsertOperations.cxx @@ -46,7 +46,9 @@ #include #include +#include #include +#include #include #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1 @@ -581,6 +583,45 @@ Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr() myResMgrUser->Find("Import") || myResMgrUser->Find("Export")); } +//============================================================================= +/*! + * RestoreShape + */ +//============================================================================= +Handle(GEOM_Object) GEOMImpl_IInsertOperations::RestoreShape (std::istringstream& theStream) +{ + SetErrorCode(KO); + + //Add a new result object + Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_COPY); + + //Add a Copy function + Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF); + if (aFunction.IsNull()) return NULL; + + //Check if the function is set correctly + if (aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL; + + //Read a shape from the stream + TopoDS_Shape aShape; + BRep_Builder B; + BRepTools::Read(aShape, theStream, B); + if (aShape.IsNull()) { + SetErrorCode("RestoreShape error: BREP reading failed"); + } + + //Set function value + aFunction->SetValue(aShape); + + //Special dump to avoid restored shapes publication. + //See correcponding code in GEOM_Engine.cxx (method ProcessFunction) + GEOM::TPythonDump(aFunction) << "#"; + + SetErrorCode(OK); + + return result; +} + int GEOMImpl_IInsertOperations::LoadTexture(const TCollection_AsciiString& theTextureFile) { SetErrorCode(KO); diff --git a/src/GEOMImpl/GEOMImpl_IInsertOperations.hxx b/src/GEOMImpl/GEOMImpl_IInsertOperations.hxx index c59411216..56ea89d42 100644 --- a/src/GEOMImpl/GEOMImpl_IInsertOperations.hxx +++ b/src/GEOMImpl/GEOMImpl_IInsertOperations.hxx @@ -72,6 +72,8 @@ class GEOMImpl_IInsertOperations : public GEOM_IOperations { const TCollection_AsciiString& theFormat, Handle(TCollection_HAsciiString)& theLibName); + Standard_EXPORT Handle(GEOM_Object) RestoreShape (std::istringstream& theStream); + Standard_EXPORT int LoadTexture(const TCollection_AsciiString& theTextureFile); Standard_EXPORT int AddTexture(int theWidth, int theHeight, diff --git a/src/GEOM_I/GEOM_IInsertOperations_i.cc b/src/GEOM_I/GEOM_IInsertOperations_i.cc index cd1b443fa..57a20c3b3 100644 --- a/src/GEOM_I/GEOM_IInsertOperations_i.cc +++ b/src/GEOM_I/GEOM_IInsertOperations_i.cc @@ -250,12 +250,47 @@ void GEOM_IInsertOperations_i::ExportTranslators thePatterns = aPatternsArray._retn(); } +//============================================================================= +/*! + * RestoreShape + */ +//============================================================================= +GEOM::GEOM_Object_ptr GEOM_IInsertOperations_i::RestoreShape (const SALOMEDS::TMPFile& theStream) +{ + GEOM::GEOM_Object_var aGEOMObject; + + //Set a not done flag + GetOperations()->SetNotDone(); + + if (theStream.length() < 1) + return aGEOMObject._retn(); + + char* buf = (char*)theStream.NP_data(); + std::istringstream aStream (buf); + + Handle(GEOM_Object) anObject = GetOperations()->RestoreShape(aStream); + if (!GetOperations()->IsDone() || anObject.IsNull()) + return aGEOMObject._retn(); + + return GetObject(anObject); +} + +//============================================================================= +/*! + * LoadTexture + */ +//============================================================================= CORBA::Long GEOM_IInsertOperations_i::LoadTexture(const char* theTextureFile) { GetOperations()->SetNotDone(); return GetOperations()->LoadTexture( theTextureFile ); } +//============================================================================= +/*! + * AddTexture + */ +//============================================================================= CORBA::Long GEOM_IInsertOperations_i::AddTexture(CORBA::Long theWidth, CORBA::Long theHeight, const SALOMEDS::TMPFile& theTexture) { @@ -280,6 +315,11 @@ CORBA::Long GEOM_IInsertOperations_i::AddTexture(CORBA::Long theWidth, CORBA::Lo return GetOperations()->AddTexture( theWidth, theHeight, aTexture ); } +//============================================================================= +/*! + * GetTexture + */ +//============================================================================= SALOMEDS::TMPFile* GEOM_IInsertOperations_i::GetTexture(CORBA::Long theID, CORBA::Long& theWidth, CORBA::Long& theHeight) @@ -303,6 +343,11 @@ SALOMEDS::TMPFile* GEOM_IInsertOperations_i::GetTexture(CORBA::Long theID, return aTexture._retn(); } +//============================================================================= +/*! + * GetAllTextures + */ +//============================================================================= GEOM::ListOfLong* GEOM_IInsertOperations_i::GetAllTextures() { std::list localIDs = GetOperations()->GetAllTextures(); diff --git a/src/GEOM_I/GEOM_IInsertOperations_i.hh b/src/GEOM_I/GEOM_IInsertOperations_i.hh index 820570016..97ca19c70 100644 --- a/src/GEOM_I/GEOM_IInsertOperations_i.hh +++ b/src/GEOM_I/GEOM_IInsertOperations_i.hh @@ -61,6 +61,8 @@ class GEOM_I_EXPORT GEOM_IInsertOperations_i : void ExportTranslators (GEOM::string_array_out theFormats, GEOM::string_array_out thePatterns); + GEOM::GEOM_Object_ptr RestoreShape (const SALOMEDS::TMPFile& theStream); + CORBA::Long LoadTexture(const char* theTextureFile); CORBA::Long AddTexture(CORBA::Long theWidth, CORBA::Long theHeight, const SALOMEDS::TMPFile& theTexture); diff --git a/src/GEOM_SWIG/GEOM_TestOthers.py b/src/GEOM_SWIG/GEOM_TestOthers.py index 9eee4f171..4db5841a0 100644 --- a/src/GEOM_SWIG/GEOM_TestOthers.py +++ b/src/GEOM_SWIG/GEOM_TestOthers.py @@ -95,6 +95,11 @@ def TestExportImport (geompy, shape): os.remove(fileExportImportIGES) os.remove(fileExportImportSTEP) + # Test RestoreShape from binary BRep stream + aStream = shape.GetShapeStream() + aNewShape = geompy.RestoreShape(aStream) + geompy.addToStudy(aNewShape, "aNewShape") + print "OK" diff --git a/src/GEOM_SWIG/geompyDC.py b/src/GEOM_SWIG/geompyDC.py index ed0c12272..0dd628e5a 100644 --- a/src/GEOM_SWIG/geompyDC.py +++ b/src/GEOM_SWIG/geompyDC.py @@ -2169,11 +2169,11 @@ class geompyDC(GEOM._objref_GEOM_Gen): theR2 Radius of the second cone base. theH Cone height. - Note: + Note: If both radiuses are non-zero, the cone will be truncated. If the radiuses are equal, a cylinder will be created instead. - Returns: + Returns: New GEOM.GEOM_Object, containing the created cone. """ # Example: see GEOM_TestAll.py @@ -2209,7 +2209,7 @@ class geompyDC(GEOM._objref_GEOM_Gen): If both radiuses are non-zero, the cone will be truncated. If the radiuses are equal, a cylinder will be created instead. - Returns: + Returns: New GEOM.GEOM_Object, containing the created cone. """ # Example: see GEOM_TestAll.py @@ -7340,6 +7340,31 @@ class geompyDC(GEOM._objref_GEOM_Gen): # Example: see GEOM_TestOthers.py return self.ImportFile(theFileName, "STEP") + ## Read a shape from the binary stream, containing its bounding representation (BRep). + # @note This method will not be dumped to the python script by DumpStudy functionality. + # @note GEOM.GEOM_Object.GetShapeStream() method can be used to obtain the shape's BRep stream. + # @param theStream The BRep binary stream. + # @return New GEOM_Object, containing the shape, read from theStream. + # + # @ref swig_Import_Export "Example" + def RestoreShape (self, theStream): + """ + Read a shape from the binary stream, containing its bounding representation (BRep). + + Note: + shape.GetShapeStream() method can be used to obtain the shape's BRep stream. + + Parameters: + theStream The BRep binary stream. + + Returns: + New GEOM_Object, containing the shape, read from theStream. + """ + # Example: see GEOM_TestOthers.py + anObj = self.InsertOp.RestoreShape(theStream) + RaiseIfFailed("RestoreShape", self.InsertOp) + return anObj + ## Export the given shape into a file with given name. # @param theObject Shape to be stored in the file. # @param theFileName Name of the file to store the given shape in. -- 2.39.2