Create code
Browse files
code
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from OCC.Core.STEPControl import STEPControl_Reader
|
| 2 |
+
from OCC.Core.IFSelect import IFSelect_RetDone
|
| 3 |
+
from OCC.Core.StlAPI import StlAPI_Writer
|
| 4 |
+
from OCC.Core.BRepMesh import BRepMesh_IncrementalMesh
|
| 5 |
+
import os
|
| 6 |
+
import sys
|
| 7 |
+
import math
|
| 8 |
+
|
| 9 |
+
class TeeOutput:
|
| 10 |
+
def __init__(self, *files):
|
| 11 |
+
self.files = files
|
| 12 |
+
def write(self, data):
|
| 13 |
+
for f in self.files:
|
| 14 |
+
f.write(data)
|
| 15 |
+
f.flush()
|
| 16 |
+
def flush(self):
|
| 17 |
+
for f in self.files:
|
| 18 |
+
f.flush()
|
| 19 |
+
|
| 20 |
+
def remove_extension(fname):
|
| 21 |
+
lower = fname.lower()
|
| 22 |
+
if lower.endswith(".step"):
|
| 23 |
+
return fname[:-5]
|
| 24 |
+
elif lower.endswith(".stp"):
|
| 25 |
+
return fname[:-4]
|
| 26 |
+
return fname
|
| 27 |
+
|
| 28 |
+
def step_to_stl(step_file_path, output_stl_path,
|
| 29 |
+
mesh_linear_deflection=0.05,
|
| 30 |
+
mesh_angular_deflection=0.3):
|
| 31 |
+
"""Convert a single STEP file to STL (dense mesh)."""
|
| 32 |
+
print(f"\nReading STEP file: {step_file_path}")
|
| 33 |
+
step_reader = STEPControl_Reader()
|
| 34 |
+
status = step_reader.ReadFile(step_file_path)
|
| 35 |
+
if status != IFSelect_RetDone:
|
| 36 |
+
raise ValueError(f"Cannot read STEP file: {step_file_path}")
|
| 37 |
+
|
| 38 |
+
print("STEP file read successfully.")
|
| 39 |
+
step_reader.TransferRoots()
|
| 40 |
+
shape = step_reader.OneShape()
|
| 41 |
+
|
| 42 |
+
if shape.IsNull():
|
| 43 |
+
raise ValueError("No shape found in STEP file.")
|
| 44 |
+
|
| 45 |
+
print("Generating dense mesh...")
|
| 46 |
+
# Smaller linear deflection → denser mesh
|
| 47 |
+
mesh = BRepMesh_IncrementalMesh(shape, mesh_linear_deflection, True,
|
| 48 |
+
mesh_angular_deflection, True)
|
| 49 |
+
mesh.Perform()
|
| 50 |
+
if not mesh.IsDone():
|
| 51 |
+
raise RuntimeError("Mesh generation failed.")
|
| 52 |
+
|
| 53 |
+
print("Mesh generation complete. Writing STL...")
|
| 54 |
+
stl_writer = StlAPI_Writer()
|
| 55 |
+
stl_writer.SetASCIIMode(False) # Binary STL for compact size
|
| 56 |
+
success = stl_writer.Write(shape, output_stl_path)
|
| 57 |
+
|
| 58 |
+
if not success:
|
| 59 |
+
raise RuntimeError(f"Failed to write STL: {output_stl_path}")
|
| 60 |
+
|
| 61 |
+
print(f"STL saved successfully: {output_stl_path}")
|
| 62 |
+
|
| 63 |
+
def process_step_folder(input_dir, output_dir,
|
| 64 |
+
mesh_linear_deflection=0.05,
|
| 65 |
+
mesh_angular_deflection=0.3):
|
| 66 |
+
"""Process all STEP/STP files in a directory and convert to STL."""
|
| 67 |
+
if not os.path.exists(output_dir):
|
| 68 |
+
os.makedirs(output_dir)
|
| 69 |
+
|
| 70 |
+
log_path = os.path.join(output_dir, "conversion_log.txt")
|
| 71 |
+
log_file = open(log_path, "a", buffering=1)
|
| 72 |
+
sys.stdout = TeeOutput(sys.stdout, log_file)
|
| 73 |
+
|
| 74 |
+
files = sorted([f for f in os.listdir(input_dir)
|
| 75 |
+
if f.lower().endswith((".step", ".stp"))])
|
| 76 |
+
total = len(files)
|
| 77 |
+
if total == 0:
|
| 78 |
+
print("No STEP files found in directory.")
|
| 79 |
+
return
|
| 80 |
+
|
| 81 |
+
print(f"Found {total} STEP files in '{input_dir}'")
|
| 82 |
+
processed = 0
|
| 83 |
+
|
| 84 |
+
for fname in files:
|
| 85 |
+
step_path = os.path.join(input_dir, fname)
|
| 86 |
+
stl_name = remove_extension(fname) + ".stl"
|
| 87 |
+
stl_path = os.path.join(output_dir, stl_name)
|
| 88 |
+
|
| 89 |
+
try:
|
| 90 |
+
step_to_stl(step_path, stl_path,
|
| 91 |
+
mesh_linear_deflection=mesh_linear_deflection,
|
| 92 |
+
mesh_angular_deflection=mesh_angular_deflection)
|
| 93 |
+
processed += 1
|
| 94 |
+
except Exception as e:
|
| 95 |
+
print(f"❌ Failed to process {fname}: {e}")
|
| 96 |
+
|
| 97 |
+
print(f"Progress: {processed}/{total} files complete.")
|
| 98 |
+
|
| 99 |
+
log_file.close()
|
| 100 |
+
print(f"\nConversion complete! {processed}/{total} STL files saved to '{output_dir}'")
|
| 101 |
+
|
| 102 |
+
if __name__ == "__main__":
|
| 103 |
+
input_folder = r"C:\Users\nandh\Desktop\diffusionNet\step"
|
| 104 |
+
output_folder = os.path.join(os.path.dirname(input_folder), "stl")
|
| 105 |
+
|
| 106 |
+
# Smaller deflection → denser mesh (try 0.01 for very dense)
|
| 107 |
+
process_step_folder(
|
| 108 |
+
input_folder,
|
| 109 |
+
output_folder,
|
| 110 |
+
mesh_linear_deflection=0.01, # denser mesh
|
| 111 |
+
mesh_angular_deflection=0.2
|
| 112 |
+
)
|