Files
massive-cloud/alg/point_porj/archive/CAD2Mesh.py

55 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from OCC.Core.STEPControl import STEPControl_Reader
from OCC.Core.IFSelect import IFSelect_RetDone
from OCC.Core.BRepMesh import BRepMesh_IncrementalMesh
from OCC.Core.StlAPI import StlAPI_Writer
import time
import argparse # 命令行参数解析
def step2lightweight_stl(mesh_precision, input_step, output_stl):
"""STEP转轻量化STL,纯后台处理,无显示,体积暴减"""
start_total = time.time()
# 1. 读取STEP文件
print(f"【1/3】读取STEP文件:{input_step}")
step_reader = STEPControl_Reader()
if step_reader.ReadFile(input_step) != IFSelect_RetDone:
raise Exception("STEP文件读取失败!检查路径/文件完整性")
step_reader.TransferRoots()
shape = step_reader.OneShape()
print(f"【1/3】读取完成,耗时:{time.time()-start_total:.2f}秒")
# 2. 核心:网格离散化
print(f"【2/3】网格离散化(精度{mesh_precision}mm),开始轻量化...")
mesh = BRepMesh_IncrementalMesh(shape, mesh_precision, False)
mesh.Perform()
if not mesh.IsDone():
raise Exception("网格离散化失败!")
print(f"【2/3】轻量化完成,耗时:{time.time()-start_total:.2f}秒")
# 3. 保存为STL
print(f"【3/3】保存轻量化STL:{output_stl}")
stl_writer = StlAPI_Writer()
stl_writer.Write(shape, output_stl)
total_time = time.time() - start_total
print(f"✅ 转换完成!总耗时:{total_time:.2f}秒")
print(f"💡 提示:STL文件可直接用FreeCAD/MeshLab/3D Viewer打开")
if __name__ == "__main__":
# ========== 命令行参数定义 ==========
parser = argparse.ArgumentParser(description="STEP 转 轻量化 STL (命令行args版)")
parser.add_argument("--precision", "-p", type=float, default=1.0, help="网格精度 MESH_PRECISION (mm),越大文件越小")
parser.add_argument("--input", "-i", type=str, default="High_Speed_Data/CAD/11.step", help="输入STEP文件路径 INPUT_STEP_PATH")
parser.add_argument("--output", "-o", type=str, default="High_Speed_Data/Mesh/11.stl", help="输出STL文件路径 OUTPUT_STL_PATH")
# 解析参数
args = parser.parse_args()
# 调用转换函数
try:
step2lightweight_stl(
mesh_precision=args.precision,
input_step=args.input,
output_stl=args.output
)
except Exception as e:
print(f"❌ 处理失败:{str(e)}")