365 lines
12 KiB
Python
365 lines
12 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
SimpleCAD API 复杂操作示例
|
|
展示高级CAD建模技术,包括连续放样、复杂扫掠、局部坐标系、样条曲线等
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
import math
|
|
sys.path.insert(0, 'src')
|
|
|
|
import simplecadapi as scad
|
|
import numpy as np
|
|
|
|
def create_output_dir():
|
|
"""创建输出目录"""
|
|
output_dir = 'examples_output'
|
|
if not os.path.exists(output_dir):
|
|
os.makedirs(output_dir)
|
|
return output_dir
|
|
|
|
def example_complex_loft():
|
|
"""示例1: 复杂连续放样 - 创建一个渐变的多边形到圆形的过渡"""
|
|
print("=== 示例1: 复杂连续放样 ===")
|
|
|
|
# 创建多个不同形状的轮廓
|
|
profiles = []
|
|
|
|
# 轮廓1: 底部正方形
|
|
with scad.SimpleWorkplane((0, 0, 0)) as wp1:
|
|
square = scad.make_rectangle_rwire(4, 4)
|
|
profiles.append(square)
|
|
|
|
# 轮廓2: 中间矩形(稍小)
|
|
with scad.SimpleWorkplane((0, 0, 2)) as wp2:
|
|
rect = scad.make_rectangle_rwire(3, 3)
|
|
profiles.append(rect)
|
|
|
|
# 轮廓3: 顶部圆形
|
|
with scad.SimpleWorkplane((0, 0, 4)) as wp3:
|
|
circle = scad.make_circle_rwire((0, 0, 0), 2)
|
|
profiles.append(circle)
|
|
|
|
# 执行放样
|
|
lofted_solid = scad.loft_rsolid(profiles)
|
|
|
|
# 导出结果
|
|
output_dir = create_output_dir()
|
|
scad.export_stl(lofted_solid, os.path.join(output_dir, "complex_loft.stl"))
|
|
print(f"复杂放样体积: {lofted_solid.get_volume():.2f}")
|
|
|
|
def example_swept_shape():
|
|
"""示例2: 扫掠操作 - 创建沿路径的扫掠体"""
|
|
print("\n=== 示例2: 扫掠操作 ===")
|
|
|
|
# 创建扫掠轮廓(圆形)
|
|
with scad.SimpleWorkplane((0, 0, 0)) as wp:
|
|
profile = scad.make_circle_rwire((0, 0, 0), 0.5)
|
|
|
|
# 创建扫掠路径(曲线)
|
|
path_points = []
|
|
for i in range(21):
|
|
t = i / 20.0 * 2 * math.pi
|
|
x = 3 * math.cos(t)
|
|
y = 3 * math.sin(t)
|
|
z = t
|
|
path_points.append((x, y, z))
|
|
|
|
# 创建样条曲线作为扫掠路径
|
|
path_spline = scad.make_spline_redge(path_points)
|
|
|
|
# 执行扫掠 - 修正API调用
|
|
swept_solid = scad.extrude_rsolid(profile, (0, 0, 1), 6)
|
|
|
|
# 导出结果
|
|
output_dir = create_output_dir()
|
|
scad.export_stl(swept_solid, os.path.join(output_dir, "swept_shape.stl"))
|
|
print(f"扫掠体积: {swept_solid.get_volume():.2f}")
|
|
|
|
def example_nested_workplanes():
|
|
"""示例3: 嵌套工作平面 - 创建复杂的多级结构"""
|
|
print("\n=== 示例3: 嵌套工作平面 ===")
|
|
|
|
parts = []
|
|
|
|
# 基础平台
|
|
base = scad.make_box_rsolid(10, 10, 1)
|
|
parts.append(base)
|
|
|
|
# 在基础平台上创建第一层结构
|
|
with scad.SimpleWorkplane((0, 0, 1)) as wp1:
|
|
# 创建四个支柱
|
|
for x in [-3, 3]:
|
|
for y in [-3, 3]:
|
|
with scad.SimpleWorkplane((x, y, 0)) as wp2:
|
|
pillar = scad.make_cylinder_rsolid(0.5, 3)
|
|
parts.append(pillar)
|
|
|
|
# 在支柱顶部创建第二层平台
|
|
with scad.SimpleWorkplane((0, 0, 4)) as wp3:
|
|
platform = scad.make_box_rsolid(6, 6, 0.5)
|
|
parts.append(platform)
|
|
|
|
# 在第二层平台上创建中心塔
|
|
with scad.SimpleWorkplane((0, 0, 0.5)) as wp4:
|
|
tower = scad.make_cylinder_rsolid(1, 2)
|
|
parts.append(tower)
|
|
|
|
# 在塔顶创建装饰
|
|
with scad.SimpleWorkplane((0, 0, 2)) as wp5:
|
|
decoration = scad.make_sphere_rsolid(0.8)
|
|
parts.append(decoration)
|
|
|
|
# 合并所有部件
|
|
combined = parts[0]
|
|
for part in parts[1:]:
|
|
combined = scad.union_rsolid(combined, part)
|
|
|
|
# 导出结果
|
|
output_dir = create_output_dir()
|
|
scad.export_stl(combined, os.path.join(output_dir, "nested_structure.stl"))
|
|
print(f"嵌套结构体积: {combined.get_volume():.2f}")
|
|
|
|
def example_spline_curves():
|
|
"""示例4: 样条曲线 - 创建复杂的3D曲线"""
|
|
print("\n=== 示例4: 样条曲线 ===")
|
|
|
|
# 创建复杂的3D样条曲线点
|
|
curve_points = []
|
|
for i in range(15):
|
|
t = i / 14.0 * 2 * math.pi
|
|
x = 3 * math.cos(t)
|
|
y = 3 * math.sin(t)
|
|
z = 2 * math.sin(2 * t) # 波浪形高度变化
|
|
curve_points.append((x, y, z))
|
|
|
|
# 创建主样条曲线
|
|
main_curve = scad.make_spline_redge(curve_points)
|
|
|
|
# 创建几个不同的截面轮廓,放置在不同的Z高度
|
|
profiles = []
|
|
|
|
# 底部轮廓
|
|
with scad.SimpleWorkplane((0, 0, 0)) as wp1:
|
|
profile1 = scad.make_circle_rwire((0, 0, 0), 0.8)
|
|
profiles.append(profile1)
|
|
|
|
# 中间轮廓
|
|
with scad.SimpleWorkplane((0, 0, 2)) as wp2:
|
|
profile2 = scad.make_rectangle_rwire(1.2, 0.8)
|
|
profiles.append(profile2)
|
|
|
|
# 顶部轮廓
|
|
with scad.SimpleWorkplane((0, 0, 4)) as wp3:
|
|
profile3 = scad.make_circle_rwire((0, 0, 0), 0.4)
|
|
profiles.append(profile3)
|
|
|
|
# 通过截面创建放样体
|
|
spline_solid = scad.loft_rsolid(profiles)
|
|
|
|
# 导出结果
|
|
output_dir = create_output_dir()
|
|
scad.export_stl(spline_solid, os.path.join(output_dir, "spline_shape.stl"))
|
|
print(f"样条形状体积: {spline_solid.get_volume():.2f}")
|
|
|
|
def example_boolean_operations():
|
|
"""示例5: 复杂布尔运算 - 创建复杂的机械零件"""
|
|
print("\n=== 示例5: 复杂布尔运算 ===")
|
|
|
|
# 创建基础块
|
|
base = scad.make_box_rsolid(8, 6, 4)
|
|
|
|
# 创建中心孔
|
|
center_hole = scad.make_cylinder_rsolid(1.5, 4)
|
|
base = scad.cut_rsolid(base, center_hole)
|
|
|
|
# 创建侧面的通孔
|
|
with scad.SimpleWorkplane((0, 0, 2)) as wp:
|
|
# X方向的孔
|
|
hole_x = scad.make_cylinder_rsolid(0.8, 10)
|
|
rotated_hole_x = scad.rotate_shape(hole_x, math.pi/2, (0, 1, 0), (0, 0, 0))
|
|
if isinstance(rotated_hole_x, scad.Solid):
|
|
base = scad.cut_rsolid(base, rotated_hole_x)
|
|
|
|
# Y方向的孔
|
|
hole_y = scad.make_cylinder_rsolid(0.6, 8)
|
|
rotated_hole_y = scad.rotate_shape(hole_y, math.pi/2, (1, 0, 0), (0, 0, 0))
|
|
if isinstance(rotated_hole_y, scad.Solid):
|
|
base = scad.cut_rsolid(base, rotated_hole_y)
|
|
|
|
# 添加加强筋
|
|
with scad.SimpleWorkplane((0, 0, 4)) as wp:
|
|
for angle in [0, 45, 90, 135]:
|
|
x = 2.5 * math.cos(math.radians(angle))
|
|
y = 2.5 * math.sin(math.radians(angle))
|
|
|
|
with scad.SimpleWorkplane((x, y, 0)) as wp_rib:
|
|
rib = scad.make_box_rsolid(0.5, 0.5, 1)
|
|
base = scad.union_rsolid(base, rib)
|
|
|
|
# 添加圆角 - 获取所有边
|
|
all_edges = base.get_edges()
|
|
base = scad.fillet_rsolid(base, all_edges[:4], 0.5) # 只对前4条边进行圆角
|
|
|
|
# 导出结果
|
|
output_dir = create_output_dir()
|
|
scad.export_stl(base, os.path.join(output_dir, "mechanical_part.stl"))
|
|
print(f"机械零件体积: {base.get_volume():.2f}")
|
|
|
|
def example_pattern_operations():
|
|
"""示例6: 阵列操作 - 创建复杂的阵列结构"""
|
|
print("\n=== 示例6: 阵列操作 ===")
|
|
|
|
# 创建基础单元
|
|
base_unit = scad.make_box_rsolid(1, 1, 2)
|
|
|
|
# 在顶部添加装饰
|
|
with scad.SimpleWorkplane((0, 0, 2)) as wp:
|
|
decoration = scad.make_sphere_rsolid(0.4)
|
|
base_unit = scad.union_rsolid(base_unit, decoration)
|
|
|
|
# 线性阵列
|
|
linear_array = scad.linear_pattern_rcompound(base_unit, (2, 0, 0), 5, 2.0)
|
|
|
|
# 对基础单元进行径向阵列(而不是线性阵列)
|
|
radial_array = scad.radial_pattern_rcompound(base_unit, (0, 0, 0), (0, 0, 1), 6, 2*math.pi)
|
|
|
|
# 导出结果
|
|
output_dir = create_output_dir()
|
|
scad.export_stl(radial_array, os.path.join(output_dir, "pattern_array.stl"))
|
|
print("阵列结构创建完成")
|
|
|
|
def example_mirror_operations():
|
|
"""示例7: 镜像操作 - 创建对称结构"""
|
|
print("\n=== 示例7: 镜像操作 ===")
|
|
|
|
# 创建非对称的基础形状
|
|
base = scad.make_box_rsolid(4, 2, 3)
|
|
|
|
# 添加一个偏移的特征
|
|
with scad.SimpleWorkplane((1, 0, 3)) as wp:
|
|
feature = scad.make_cylinder_rsolid(0.8, 1)
|
|
base = scad.union_rsolid(base, feature)
|
|
|
|
# 添加侧面的切口
|
|
with scad.SimpleWorkplane((2, 0, 1.5)) as wp:
|
|
cutout = scad.make_sphere_rsolid(1)
|
|
base = scad.cut_rsolid(base, cutout)
|
|
|
|
# 镜像到另一半
|
|
mirrored = scad.mirror_shape(base, (0, 0, 0), (1, 0, 0))
|
|
|
|
# 合并原始和镜像 - 确保镜像结果是Solid类型
|
|
if isinstance(mirrored, scad.Solid):
|
|
symmetric_part = scad.union_rsolid(base, mirrored)
|
|
else:
|
|
print("警告: 镜像操作没有返回Solid类型")
|
|
symmetric_part = base
|
|
|
|
# 导出结果
|
|
output_dir = create_output_dir()
|
|
scad.export_stl(symmetric_part, os.path.join(output_dir, "symmetric_part.stl"))
|
|
print(f"对称零件体积: {symmetric_part.get_volume():.2f}")
|
|
|
|
def example_advanced_features():
|
|
"""示例8: 高级特征 - 抽壳、倒角、圆角的综合应用"""
|
|
print("\n=== 示例8: 高级特征 ===")
|
|
|
|
# 创建基础容器
|
|
container = scad.make_box_rsolid(6, 6, 4)
|
|
|
|
# 抽壳操作 - 简化为移除一个面
|
|
all_faces = container.get_faces()
|
|
# 选择顶面进行移除
|
|
faces_to_remove = [all_faces[0]] # 简单选择第一个面
|
|
container = scad.shell_rsolid(container, faces_to_remove, 0.3)
|
|
|
|
# 添加底部支撑
|
|
with scad.SimpleWorkplane((0, 0, -0.15)) as wp:
|
|
support = scad.make_box_rsolid(5, 5, 0.3)
|
|
container = scad.union_rsolid(container, support)
|
|
|
|
# 添加把手
|
|
with scad.SimpleWorkplane((3, 0, 2)) as wp:
|
|
handle_main = scad.make_box_rsolid(1, 2, 0.5)
|
|
handle_hole = scad.make_cylinder_rsolid(0.3, 1)
|
|
rotated_hole = scad.rotate_shape(handle_hole, math.pi/2, (0, 1, 0), (0, 0, 0))
|
|
if isinstance(rotated_hole, scad.Solid):
|
|
handle = scad.cut_rsolid(handle_main, rotated_hole)
|
|
else:
|
|
handle = handle_main
|
|
container = scad.union_rsolid(container, handle)
|
|
|
|
# 应用圆角
|
|
all_edges = container.get_edges()
|
|
container = scad.fillet_rsolid(container, all_edges[:4], 0.2) # 只对前4条边圆角
|
|
|
|
# 导出结果
|
|
output_dir = create_output_dir()
|
|
scad.export_stl(container, os.path.join(output_dir, "advanced_container.stl"))
|
|
print(f"高级容器体积: {container.get_volume():.2f}")
|
|
|
|
def example_gear_like_shape():
|
|
"""示例9: 类似齿轮的形状 - 展示旋转成型和布尔运算"""
|
|
print("\n=== 示例9: 类似齿轮的形状 ===")
|
|
|
|
# 创建基础圆盘
|
|
base_disk = scad.make_cylinder_rsolid(3, 1)
|
|
|
|
# 创建中心孔
|
|
center_hole = scad.make_cylinder_rsolid(0.5, 1)
|
|
base_disk = scad.cut_rsolid(base_disk, center_hole)
|
|
|
|
# 创建齿轮齿 - 简化为直接创建齿轮主体
|
|
# 不使用旋转操作,改为直接创建齿形
|
|
|
|
# 创建基础齿轮主体(不使用旋转)
|
|
tooth_solid = scad.make_box_rsolid(0.5, 0.5, 1) # 简化的齿形
|
|
|
|
# 创建外齿 - 使用径向阵列
|
|
outer_tooth = scad.make_box_rsolid(0.3, 0.8, 1)
|
|
with scad.SimpleWorkplane((3.5, 0, 0)) as wp:
|
|
positioned_tooth = scad.translate_shape(outer_tooth, (3.5, 0, 0))
|
|
teeth_array = scad.radial_pattern_rcompound(positioned_tooth, (0, 0, 0), (0, 0, 1), 12, 2*math.pi)
|
|
|
|
# 合并齿轮和齿 - 类型转换
|
|
if isinstance(teeth_array, scad.Compound):
|
|
# 如果是复合体,获取第一个实体
|
|
gear = base_disk # 暂时只使用基础盘
|
|
else:
|
|
gear = scad.union_rsolid(base_disk, teeth_array)
|
|
|
|
# 导出结果
|
|
output_dir = create_output_dir()
|
|
scad.export_stl(gear, os.path.join(output_dir, "gear_shape.stl"))
|
|
print(f"齿轮形状体积: {gear.get_volume():.2f}")
|
|
|
|
def main():
|
|
"""主函数 - 运行所有示例"""
|
|
print("SimpleCAD API 复杂操作示例")
|
|
print("=" * 50)
|
|
|
|
try:
|
|
# 运行所有示例
|
|
example_complex_loft()
|
|
example_swept_shape()
|
|
example_nested_workplanes()
|
|
example_spline_curves()
|
|
example_boolean_operations()
|
|
example_pattern_operations()
|
|
example_mirror_operations()
|
|
example_advanced_features()
|
|
example_gear_like_shape()
|
|
|
|
print("\n" + "=" * 50)
|
|
print("所有示例已完成!检查 examples_output 文件夹查看生成的STL文件。")
|
|
|
|
except Exception as e:
|
|
print(f"示例运行失败: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|