71 lines
2.3 KiB
Python
71 lines
2.3 KiB
Python
import sys
|
|
from pathlib import Path
|
|
|
|
from build123d import export_step, export_stl
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
from engine.fastener_defaults import build_fastener # noqa: E402
|
|
|
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
|
|
|
# 每种标准件对应的 bd_warehouse 类名 + 需要的参数、默认标准
|
|
PART_SPECS = {
|
|
"hex_bolt": {"class_name": "HexHeadScrew", "default_type": "iso4014"},
|
|
"hex_nut": {"class_name": "HexNut", "default_type": "iso4032"},
|
|
}
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) < 3:
|
|
print("用法: python scripts/generate_fastener.py <hex_bolt|hex_nut> <size> [length] [standard]")
|
|
print("例: python scripts/generate_fastener.py hex_bolt M5-0.8 200 iso4014")
|
|
print("例: python scripts/generate_fastener.py hex_nut M5-0.8")
|
|
sys.exit(1)
|
|
|
|
part_type = sys.argv[1]
|
|
size = sys.argv[2]
|
|
|
|
if part_type not in PART_SPECS:
|
|
print(f"未知零件类型 '{part_type}',目前支持: {list(PART_SPECS)}")
|
|
sys.exit(1)
|
|
|
|
spec = PART_SPECS[part_type]
|
|
params = {"size": size}
|
|
name_suffix = size.replace("-", "_")
|
|
|
|
if part_type == "hex_bolt":
|
|
length = float(sys.argv[3]) if len(sys.argv) > 3 else 200
|
|
fastener_type = sys.argv[4] if len(sys.argv) > 4 else spec["default_type"]
|
|
params["length"] = length
|
|
params["fastener_type"] = fastener_type
|
|
name_suffix += f"_L{int(length)}"
|
|
else:
|
|
fastener_type = sys.argv[3] if len(sys.argv) > 3 else spec["default_type"]
|
|
params["fastener_type"] = fastener_type
|
|
|
|
part = build_fastener(spec["class_name"], **params)
|
|
|
|
name = f"{part_type}_{name_suffix}"
|
|
out_dir = BASE_DIR / "output" / name
|
|
out_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
step_path = out_dir / "model.step"
|
|
stl_path = out_dir / "model.stl"
|
|
export_step(part, str(step_path))
|
|
export_stl(part, str(stl_path))
|
|
|
|
bbox = part.bounding_box()
|
|
print(f"[成功] {name} (面数: {len(part.faces())}, 真实螺纹已确认)")
|
|
print(f" 规格: {size} 标准: {fastener_type}")
|
|
print(f" 体积: {part.volume:.2f} mm^3")
|
|
print(
|
|
f" 包围盒: X[{bbox.min.X:.2f},{bbox.max.X:.2f}] "
|
|
f"Y[{bbox.min.Y:.2f},{bbox.max.Y:.2f}] Z[{bbox.min.Z:.2f},{bbox.max.Z:.2f}]"
|
|
)
|
|
print(f" STEP: {step_path}")
|
|
print(f" STL: {stl_path}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|