diff --git a/src/Base/Stream.cpp b/src/Base/Stream.cpp index e962be914..009cf7e23 100644 --- a/src/Base/Stream.cpp +++ b/src/Base/Stream.cpp @@ -40,6 +40,7 @@ #include "Stream.h" #include "Swap.h" #include "FileInfo.h" +#include using namespace Base; @@ -533,6 +534,58 @@ IODeviceIStreambuf::seekpos(std::streambuf::pos_type pos, return seekoff(pos, std::ios_base::beg); } +// --------------------------------------------------------- + +PyStreambuf::PyStreambuf(PyObject* o) : inp(o) +{ + setg (buffer+pbSize, + buffer+pbSize, + buffer+pbSize); +} + +int PyStreambuf::underflow() +{ + if (gptr() < egptr()) { + return *gptr(); + } + + int numPutback; + numPutback = gptr() - eback(); + if (numPutback > pbSize) { + numPutback = pbSize; + } + + memcpy (buffer+(pbSize-numPutback), gptr()-numPutback, numPutback); + + int num=0; + for (int i=0; i(res)[0]; + num++; + buffer[pbSize+i] = c; + if (c == '\n') + break; + } + catch (Py::Exception& e) { + e.clear(); + if (num == 0) + return EOF; + break; + } + } + + setg (buffer+(pbSize-numPutback), + buffer+pbSize, + buffer+pbSize+num); + + return *gptr(); +} + // --------------------------------------------------------- Streambuf::Streambuf(const std::string& data) diff --git a/src/Base/Stream.h b/src/Base/Stream.h index 1836f2116..f083e1e58 100644 --- a/src/Base/Stream.h +++ b/src/Base/Stream.h @@ -35,6 +35,7 @@ class QByteArray; class QIODevice; class QBuffer; +typedef struct _object PyObject; namespace Base { @@ -232,6 +233,21 @@ protected: static const int bufSize = 1024; // size of the data buffer char buffer[bufSize+pbSize]; // data buffer }; + +class BaseExport PyStreambuf : public std::streambuf +{ +public: + PyStreambuf(PyObject* o); + +protected: + int underflow(); + +private: + static const int pbSize = 4; + static const int bufSize = 1024; + char buffer[bufSize+pbSize]; + PyObject* inp; +}; class BaseExport Streambuf : public std::streambuf { diff --git a/src/Mod/Part/App/TopoShape.cpp b/src/Mod/Part/App/TopoShape.cpp index c17aa3b1a..86e50b98e 100644 --- a/src/Mod/Part/App/TopoShape.cpp +++ b/src/Mod/Part/App/TopoShape.cpp @@ -609,6 +609,32 @@ void TopoShape::importBrep(const char *FileName) } } +void TopoShape::importBrep(std::istream& str) +{ + try { + // read brep-file + BRep_Builder aBuilder; + TopoDS_Shape aShape; +#if OCC_HEX_VERSION >= 0x060300 + Handle_Message_ProgressIndicator pi = new ProgressIndicator(100); + pi->NewScope(100, "Reading BREP file..."); + pi->Show(); + BRepTools::Read(aShape,str,aBuilder,pi); + pi->EndScope(); +#else + BRepTools::Read(aShape,str,aBuilder); +#endif + this->_Shape = aShape; + } + catch (Standard_Failure) { + Handle(Standard_Failure) aFail = Standard_Failure::Caught(); + throw Base::Exception(aFail->GetMessageString()); + } + catch (const std::exception& e) { + throw Base::Exception(e.what()); + } +} + void TopoShape::write(const char *FileName) const { Base::FileInfo File(FileName); diff --git a/src/Mod/Part/App/TopoShape.h b/src/Mod/Part/App/TopoShape.h index 389511d05..9f3b17562 100644 --- a/src/Mod/Part/App/TopoShape.h +++ b/src/Mod/Part/App/TopoShape.h @@ -120,6 +120,7 @@ public: void importIges(const char *FileName); void importStep(const char *FileName); void importBrep(const char *FileName); + void importBrep(std::istream&); void exportIges(const char *FileName) const; void exportStep(const char *FileName) const; void exportBrep(const char *FileName) const; diff --git a/src/Mod/Part/App/TopoShapePy.xml b/src/Mod/Part/App/TopoShapePy.xml index 1a953e2c6..f31f99f56 100644 --- a/src/Mod/Part/App/TopoShapePy.xml +++ b/src/Mod/Part/App/TopoShapePy.xml @@ -48,6 +48,11 @@ Sub-elements such as vertices, edges or faces are accessible as: Export the content of this shape to an STL mesh file. + + + Import the content to this shape of a string in BREP format. + + Extrude the shape along a direction. diff --git a/src/Mod/Part/App/TopoShapePyImp.cpp b/src/Mod/Part/App/TopoShapePyImp.cpp index 8982a1792..3aef9eab9 100644 --- a/src/Mod/Part/App/TopoShapePyImp.cpp +++ b/src/Mod/Part/App/TopoShapePyImp.cpp @@ -311,6 +311,30 @@ PyObject* TopoShapePy::exportBrep(PyObject *args) Py_Return; } +PyObject* TopoShapePy::importBrep(PyObject *args) +{ + PyObject* input; + if (!PyArg_ParseTuple(args, "O", &input)) + //char* input; + //if (!PyArg_ParseTuple(args, "s", &input)) + return NULL; + + try { + // read brep + Base::PyStreambuf buf(input); + std::istream str(0); + str.rdbuf(&buf); + //std::stringstream str(input); + getTopoShapePtr()->importBrep(str); + } + catch (const Base::Exception& e) { + PyErr_SetString(PyExc_Exception,e.what()); + return NULL; + } + + Py_Return; +} + PyObject* TopoShapePy::exportStl(PyObject *args) { char* filename;