import argparse
import os
from OCC.Core.STEPControl import STEPControl_Reader
from OCC.Core.BRepMesh import BRepMesh_IncrementalMesh
from OCC.Core.StlAPI import StlAPI_Writer
from OCC.Core.IFSelect import IFSelect_ReturnStatus
def convert_step_to_stl(step_path, output_path, mesh_deflection=0.1):
reader = STEPControl_Reader()
status = reader.ReadFile(step_path)
if status == IFSelect_ReturnStatus.IFSelect_RetDone:
reader.TransferRoot()
shape = reader.Shape()
mesh = BRepMesh_IncrementalMesh(shape, mesh_deflection)
writer = StlAPI_Writer()
err = writer.Write(mesh.Shape(), output_path)
if err == IFSelect_ReturnStatus.IFSelect_RetDone:
print(f"Successfully converted {step_path} to {output_path}")
else:
print(f"Failed to convert {step_path} to STL format.")
else:
print(f"Could not read the STEP file: {step_path}")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Convert STEP files to STL format.')
parser.add_argument("--step_dir", type=str, required=True, help="Directory containing the step files")
parser.add_argument("--output_dir", type=str, required=True, help="Directory to save the converted STL files")
parser.add_argument("--mesh_deflection", type=float, default=0.1, help="Mesh deflection for the BRepMesh_IncrementalMesh function")
args = parser.parse_args()
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
for step_file in os.listdir(args.step_dir):
if step_file.lower().endswith(".step"):
step_path = os.path.join(args.step_dir, step_file)
base, ext = os.path.splitext(step_file)
output_path = os.path.join(args.output_dir, f"{base}.stl")
convert_step_to_stl(step_path, output_path, args.mesh_deflection)
conda install -c conda-forge pythonocc-core=7.5.1
conda install --offline noarch_occwl-1.0.0-py_0.tar.bz2
python step2stl.py --step_dir /private/123/input33 --output /private/123/output33