tpaviot / pythonocc-core

Python package for 3D geometry CAD/BIM/CAM
GNU Lesser General Public License v3.0
1.39k stars 380 forks source link

Disable log printing when calling write_step_file #1367

Closed GiovaGa closed 2 months ago

GiovaGa commented 2 months ago

Is there any way to disable the following log:

*******************************************************************
******        Statistics on Transfer (Write)                 ******

*******************************************************************
******        Transfer Mode = 0  I.E.  As Is       ******
******        Transferring Shape, ShapeType = 0                      ******
** WorkSession : Sending all data
 Step File Name : [...] (31535 ents)  Write  Done

It is printed every time I call OCC.Extend.DataExchange.write_step_file. As I am doing some batch jobs, it could hide some more useful messages.

If it matters I am working on Windows 11 (64 bit).

Tanneguydv commented 2 months ago

Here a simple code to bypass the logs:

import os
import sys
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox
from  OCC.Extend.DataExchange import write_step_file

def create_box():
    # Create a simple box with dimensions 10x20x30
    return BRepPrimAPI_MakeBox(10.0, 20.0, 30.0).Shape()

def execute_without_print(func, *args, **kwargs):
    # Save original stdout and stderr
    stdout_fd = sys.stdout.fileno()
    stderr_fd = sys.stderr.fileno()

    # Create a temporary file to capture the output
    with open(os.devnull, 'w') as devnull:
        # Redirect stdout and stderr to /dev/null
        os.dup2(devnull.fileno(), stdout_fd)
        os.dup2(devnull.fileno(), stderr_fd)

        try:
            # Execute the function
            return func(*args, **kwargs)
        finally:
            # Restore stdout and stderr
            sys.stdout = sys.__stdout__
            sys.stderr = sys.__stderr__

# Example 
box = create_box()

# Execute write_step_file without printing any messages
execute_without_print(write_step_file, box, "box.step")

print("STEP file has been written successfully.")
GiovaGa commented 2 months ago

Not really what I was expecting, but it surely does what I need. I still think it could be useful to have a parameter in write_step_file (and probably other functions too) to disable logging.

Does depend on the OpenCascade C++ code? Otherwise I can try to contribute to this goal

Tanneguydv commented 2 months ago

Indeed, it depends on the C++ code

GiovaGa commented 2 months ago

Ok, then I guess this issue can be closed. Thank you for the support