Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/SWIG_files/wrapper/STEPCAFControl.i
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/SWIG_files/wrapper/STEPCAFControl.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand Down Expand Up @@ -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
Expand Down
61 changes: 61 additions & 0 deletions test/core_ocaf_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that doing a round trip would be the simplest way to ensure both writing and reading have assertions applied to them. I noticed that test_write_step_file may not have any assertions being applied

##### 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")
Expand Down