Files
make-cad/test/archive/test_new_arrays.py
T
2025-06-30 23:12:35 +08:00

156 lines
5.2 KiB
Python

"""
测试新增的2D阵列和径向阵列功能
"""
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
try:
from simplecadapi.operations import *
from simplecadapi.core import *
import cadquery as cq
import math
print("=== 新增阵列功能测试 ===")
# 创建基础几何体
print("1. 创建测试几何体...")
small_box = make_box(0.2, 0.2, 0.2)
small_cylinder = make_cylinder(0.1, 0.3)
print(f"✓ 小立方体和小圆柱创建成功")
# 测试2D阵列
print("\n2. 测试2D阵列...")
try:
# 矩形网格阵列
array_2d = pattern_2d(
body=small_box,
x_direction=(1, 0, 0), # X方向
y_direction=(0, 1, 0), # Y方向
x_count=3,
y_count=2,
x_spacing=0.5,
y_spacing=0.6
)
print(f"✓ 2D阵列成功: 实体数={array_2d.cq_solid.solids().size()}, 期望={3*2}")
# 导出2D阵列
if array_2d.cq_solid is not None:
cq.exporters.export(array_2d.cq_solid, "output/test_2d_array.stl")
print("✓ 2D阵列导出成功: output/test_2d_array.stl")
except Exception as e:
print(f"✗ 2D阵列测试失败: {e}")
import traceback
traceback.print_exc()
# 测试倾斜的2D阵列
print("\n3. 测试倾斜2D阵列...")
try:
# 倾斜网格阵列
array_2d_tilted = pattern_2d(
body=small_cylinder,
x_direction=(1, 0.5, 0), # 倾斜X方向
y_direction=(0, 1, 0.2), # 倾斜Y方向
x_count=2,
y_count=3,
x_spacing=0.4,
y_spacing=0.5
)
print(f"✓ 倾斜2D阵列成功: 实体数={array_2d_tilted.cq_solid.solids().size()}, 期望={2*3}")
# 导出倾斜2D阵列
if array_2d_tilted.cq_solid is not None:
cq.exporters.export(array_2d_tilted.cq_solid, "output/test_2d_array_tilted.stl")
print("✓ 倾斜2D阵列导出成功: output/test_2d_array_tilted.stl")
except Exception as e:
print(f"✗ 倾斜2D阵列测试失败: {e}")
import traceback
traceback.print_exc()
# 测试径向阵列
print("\n4. 测试径向/环形阵列...")
try:
# 创建一个偏离中心的小立方体用于径向阵列
offset_box = make_box(0.15, 0.15, 0.15)
# 径向阵列 - 绕Z轴
center_point = make_point(0, 0, 0)
radial_array = pattern_radial(
body=offset_box,
center=center_point,
axis=(0, 0, 1), # Z轴
count=6,
angle=2 * math.pi # 完整圆周
)
print(f"✓ 径向阵列成功: 实体数={radial_array.cq_solid.solids().size()}, 期望=6")
# 导出径向阵列
if radial_array.cq_solid is not None:
cq.exporters.export(radial_array.cq_solid, "output/test_radial_array.stl")
print("✓ 径向阵列导出成功: output/test_radial_array.stl")
except Exception as e:
print(f"✗ 径向阵列测试失败: {e}")
import traceback
traceback.print_exc()
# 测试部分径向阵列
print("\n5. 测试部分径向阵列...")
try:
# 部分径向阵列 - 只旋转120度
partial_radial = pattern_radial(
body=small_cylinder,
center=make_point(0, 0, 0),
axis=(0, 0, 1),
count=4,
angle=2 * math.pi / 3 # 120度
)
print(f"✓ 部分径向阵列成功: 实体数={partial_radial.cq_solid.solids().size()}, 期望=4")
# 导出部分径向阵列
if partial_radial.cq_solid is not None:
cq.exporters.export(partial_radial.cq_solid, "output/test_partial_radial_array.stl")
print("✓ 部分径向阵列导出成功: output/test_partial_radial_array.stl")
except Exception as e:
print(f"✗ 部分径向阵列测试失败: {e}")
import traceback
traceback.print_exc()
# 组合阵列测试
print("\n6. 测试组合阵列...")
try:
# 先创建线性阵列,然后对结果做径向阵列
linear_first = pattern_linear(small_box, direction=(1, 0, 0), count=3, spacing=0.3)
# 注意:对于复杂的组合,当前实现可能需要改进
print(f"✓ 线性阵列作为基础: 实体数={linear_first.cq_solid.solids().size()}")
# 导出组合阵列的第一步
if linear_first.cq_solid is not None:
cq.exporters.export(linear_first.cq_solid, "output/test_combined_array_step1.stl")
print("✓ 组合阵列第一步导出成功")
except Exception as e:
print(f"✗ 组合阵列测试失败: {e}")
print("\n=== 新增阵列功能测试总结 ===")
print("✓ 2D矩形阵列:功能实现")
print("✓ 倾斜2D阵列:功能实现")
print("✓ 径向/环形阵列:功能实现")
print("✓ 部分径向阵列:功能实现")
print("✓ 阵列功能大幅增强!")
except Exception as e:
print(f"测试失败: {e}")
import traceback
traceback.print_exc()