From ed4b1a142c9438bcdef71c1170d15675b90ccaa9 Mon Sep 17 00:00:00 2001 From: Anuraag Agrawal Date: Thu, 8 Feb 2024 14:56:08 +0900 Subject: [PATCH] Allow reading/writing step files to string --- src/SWIG_files/wrapper/STEPCAFControl.i | 42 ++++++++++++++++ src/SWIG_files/wrapper/STEPCAFControl.pyi | 2 + test/core_ocaf_unittest.py | 61 +++++++++++++++++++++++ 3 files changed, 105 insertions(+) diff --git a/src/SWIG_files/wrapper/STEPCAFControl.i b/src/SWIG_files/wrapper/STEPCAFControl.i index d8a248bda..2351aea0b 100644 --- a/src/SWIG_files/wrapper/STEPCAFControl.i +++ b/src/SWIG_files/wrapper/STEPCAFControl.i @@ -1873,6 +1873,27 @@ Loads a file from stream and returns the read status. @param thename [in] auxili ") ReadStream; IFSelect_ReturnStatus ReadStream(Standard_CString theName, std::istream & theIStream); + %feature("autodoc", " +Parameters +---------- +theName: str +theIStream: str + +Return +------- +IFSelect_ReturnStatus + +Description +----------- +Loads a file from a string and returns the read status. @param thename [in] auxiliary file name @param src [in] the string to read and return read status. +") ReadString; + %extend{ + IFSelect_ReturnStatus ReadString(Standard_CString theName, const std::string & src) { + std::stringstream s(src); + return self->ReadStream(theName, s); + } + } + /****************** Reader ******************/ /**** md5 signature: c54201c04d6a5ca89c65eb2fb14b8396 ****/ %feature("compactdefaultargs") Reader; @@ -2554,6 +2575,27 @@ Writes all the produced models into the stream. provided for use like single-fil ") WriteStream; IFSelect_ReturnStatus WriteStream(std::ostream & theStream); + %feature("autodoc", " +Return +------- +str + +Description +----------- +Writes all the produced models to a string and returns it. If full_precision is False, the default precision of std::stringstream is used which regularly causes rounding. +") WriteString; + %extend{ + std::string WriteString(bool full_precision = true) { + std::stringstream s; + if(full_precision) { + s.precision(17); + s.setf(std::ios::scientific); + } + self->WriteStream(s); + return s.str(); + } + } + /****************** Writer ******************/ /**** md5 signature: 056d4f3221d283b7d58d92ddd5c40dd7 ****/ %feature("compactdefaultargs") Writer; diff --git a/src/SWIG_files/wrapper/STEPCAFControl.pyi b/src/SWIG_files/wrapper/STEPCAFControl.pyi index 40f26f2cd..71657fda1 100644 --- a/src/SWIG_files/wrapper/STEPCAFControl.pyi +++ b/src/SWIG_files/wrapper/STEPCAFControl.pyi @@ -163,6 +163,7 @@ class STEPCAFControl_Reader: @overload def Perform(self, filename: str, doc: TDocStd_Document, theProgress: Optional[Message_ProgressRange] = Message_ProgressRange()) -> bool: ... def ReadFile(self, theFileName: str) -> IFSelect_ReturnStatus: ... + def ReadString(self, theFilename: str, src: str) -> IFSelect_ReturnStatus: ... def Reader(self) -> STEPControl_Reader: ... def SetColorMode(self, colormode: bool) -> None: ... def SetGDTMode(self, gdtmode: bool) -> None: ... @@ -212,6 +213,7 @@ class STEPCAFControl_Writer: @overload def Transfer(self, theLabelSeq: TDF_LabelSequence, theMode: Optional[STEPControl_StepModelType] = STEPControl_AsIs, theIsMulti: Optional[str] = 0, theProgress: Optional[Message_ProgressRange] = Message_ProgressRange()) -> bool: ... def Write(self, theFileName: str) -> IFSelect_ReturnStatus: ... + def WriteString(self, full_precision: bool = True) -> str: ... def Writer(self) -> STEPControl_Writer: ... # harray1 classes diff --git a/test/core_ocaf_unittest.py b/test/core_ocaf_unittest.py index 173ecbd9b..62953ed36 100644 --- a/test/core_ocaf_unittest.py +++ b/test/core_ocaf_unittest.py @@ -119,6 +119,67 @@ def test_read_step_file(self) -> None: a_shape = shape_tool.GetShape(label_shp) self.assertFalse(a_shape.IsNull()) + def test_read_write_step_string(self) -> None: + """Writes a step file as a string and reads it back""" + ##### write + + ### initialisation + doc = TDocStd_Document("pythonocc-doc") + self.assertTrue(doc is not None) + + # Get root assembly + shape_tool = XCAFDoc_DocumentTool.ShapeTool(doc.Main()) + colors = XCAFDoc_DocumentTool.ColorTool(doc.Main()) + ### create the shape to export + test_shape = BRepPrimAPI_MakeBox(100.0, 100.0, 100.0).Shape() + + ### add shape + shp_label = shape_tool.AddShape(test_shape) + ### set a color for this shape + r = 1.0 + g = b = 0.5 + red_color = Quantity_Color(r, g, b, Quantity_TypeOfColor.Quantity_TOC_RGB) + colors.SetColor(shp_label, red_color, XCAFDoc_ColorGen) + # write file + WS = XSControl_WorkSession() + writer = STEPCAFControl_Writer(WS, False) + writer.Transfer(doc, STEPControl_AsIs) + step_str = writer.WriteString() + + ##### read + + # create an handle to a document + doc = TDocStd_Document("pythonocc-doc") + # Get root assembly + shape_tool = XCAFDoc_DocumentTool.ShapeTool(doc.Main()) + l_colors = XCAFDoc_DocumentTool.ColorTool(doc.Main()) + step_reader = STEPCAFControl_Reader() + step_reader.SetColorMode(True) + step_reader.SetLayerMode(True) + step_reader.SetNameMode(True) + step_reader.SetMatMode(True) + status = step_reader.ReadString("myfile.step", step_str) + self.assertEqual(status, IFSelect_RetDone) + step_reader.Transfer(doc) + + labels = TDF_LabelSequence() + color_labels = TDF_LabelSequence() + + shape_tool.GetFreeShapes(labels) + + self.assertEqual(labels.Length(), 1) + sub_shapes_labels = TDF_LabelSequence() + self.assertFalse(shape_tool.IsAssembly(labels.Value(1))) + shape_tool.GetSubShapes(labels.Value(1), sub_shapes_labels) + self.assertEqual(sub_shapes_labels.Length(), 0) + + l_colors.GetColors(color_labels) + self.assertEqual(color_labels.Length(), 1) + + label_shp = labels.Value(1) + a_shape = shape_tool.GetShape(label_shp) + self.assertFalse(a_shape.IsNull()) + def test_read_step_material(self) -> None: # create an handle to a document doc = TDocStd_Document("pythonocc-doc")