Rust 3D建模插件库kittycad-modeling-cmds的使用,提供高效CAD建模命令与几何图形处理功能
Rust 3D建模插件库kittycad-modeling-cmds的使用
安装
在项目目录中运行以下Cargo命令:
cargo add kittycad-modeling-cmds
或者在Cargo.toml中添加以下行:
kittycad-modeling-cmds = "0.2.131"
元数据
- 版本: 0.2.131
- Rust版本: v1.74.0
- 许可证: MIT
- 大小: 76.4 KiB
代码示例
以下是一个使用kittycad-modeling-cmds库进行3D建模的基本示例:
use kittycad_modeling_cmds::{Command, Engine, Path};
fn main() -> anyhow::Result<()> {
// 创建一个新的建模引擎
let mut engine = Engine::new();
// 创建一个路径
let mut path = Path::new();
path.move_to(0.0, 0.0, 0.0);
path.line_to(10.0, 0.0, 0.0);
path.line_to(10.0, 10.0, 0.0);
path.line_to(0.0, 10.0, 0.0);
path.close();
// 将路径转换为3D实体
let extrude_cmd = Command::extrude(path, 5.0);
engine.execute(extrude_cmd)?;
// 输出结果
let model = engine.get_model()?;
println!("生成模型: {:?}", model);
Ok(())
}
更完整的示例
下面是一个更完整的示例,展示如何使用kittycad-modeling-cmds创建更复杂的3D模型:
use kittycad_modeling_cmds::{Command, Engine, Path, Point3};
fn create_complex_model() -> anyhow::Result<()> {
// 初始化引擎
let mut engine = Engine::new();
// 创建基础形状
let base_path = create_rectangle_path(20.0, 20.0);
engine.execute(Command::extrude(base_path, 2.0))?;
// 创建圆柱体
let cylinder_path = create_circle_path(5.0, 32);
engine.execute(Command::extrude(cylinder_path, 15.0)
.with_position(Point3::new(0.0, 0.0, 2.0)))?;
// 布尔运算
let cutout_path = create_rectangle_path(10.0, 5.0);
engine.execute(Command::extrude(cutout_path, 2.0)
.with_position(Point3::new(0.0, 0.0, 0.0))
.as_cut())?;
// 导出模型
let model = engine.get_model()?;
println!("最终模型顶点数: {}", model.vertices.len());
Ok(())
}
fn create_rectangle_path(width: f64, height: f64) -> Path {
let mut path = Path::new();
path.move_to(-width/2.0, -height/2.0, 0.0);
path.line_to(width/2.0, -height/2.0, 0.0);
path.line_to(width/2.0, height/2.0, 0.0);
path.line_to(-width/2.0, height/2.0, 0.0);
path.close();
path
}
fn create_circle_path(radius: f64, segments: usize) -> Path {
let mut path = Path::new();
for i in 0..=segments {
let angle = 2.0 * std::f64::consts::PI * i as f64 / segments as f64;
let x = radius * angle.cos();
let y = radius * angle.sin();
if i == 0 {
path.move_to(x, y, 0.0);
} else {
path.line_to(x, y, 0.0);
}
}
path.close();
path
}
完整示例demo
以下是一个结合了多个功能的完整示例,展示如何创建一个带孔的3D模型:
use kittycad_modeling_cmds::{Command, Engine, Path, Point3};
fn main() -> anyhow::Result<()> {
// 初始化建模引擎
let mut engine = Engine::new();
// 创建底座 - 一个长方体
let base = create_box(30.0, 20.0, 5.0);
engine.execute(base)?;
// 创建圆柱体
let cylinder = create_cylinder(8.0, 15.0)
.with_position(Point3::new(0.0, 0.0, 5.0));
engine.execute(cylinder)?;
// 创建孔洞
let hole = create_cylinder(4.0, 20.0)
.with_position(Point3::new(0.0, 0.0, 0.0))
.as_cut();
engine.execute(hole)?;
// 创建侧面的凹槽
let slot = create_slot(10.0, 3.0, 5.0)
.with_position(Point3::new(-15.0, 0.0, 2.5));
engine.execute(slot.as_cut())?;
// 获取最终模型
let model = engine.get_model()?;
println!("模型信息:");
println!("顶点数: {}", model.vertices.len());
println!("面数: {}", model.faces.len());
Ok(())
}
// 创建长方体
fn create_box(width: f64, depth: f64, height: f64) -> Command {
let mut path = Path::new();
path.move_to(-width/2.0, -depth/2.0, 0.0);
path.line_to(width/2.0, -depth/2.0, 0.0);
path.line_to(width/2.0, depth/2.0, 0.0);
path.line_to(-width/2.0, depth/2.0, 0.0);
path.close();
Command::extrude(path, height)
}
// 创建圆柱体
fn create_cylinder(radius: f64, height: f64) -> Command {
let segments = 32;
let mut path = Path::new();
for i in 0..=segments {
let angle = 2.0 * std::f64::consts::PI * i as f64 / segments as f64;
let x = radius * angle.cos();
let y = radius * angle.sin();
if i == 0 {
path.move_to(x, y, 0.0);
} else {
path.line_to(x, y, 0.0);
}
}
path.close();
Command::extrude(path, height)
}
// 创建槽形
fn create_slot(length: f64, width: f64, height: f64) -> Command {
let radius = width / 2.0;
let half_length = length / 2.0 - radius;
let mut path = Path::new();
// 开始于左半圆
path.move_to(-half_length, -radius, 0.0);
// 左半圆
for i in 0..=16 {
let angle = std::f64::consts::PI * (1.0 + i as f64 / 16.0);
let x = -half_length + radius * angle.cos();
let y = radius * angle.sin();
path.line_to(x, y, 0.0);
}
// 顶部直线
path.line_to(half_length, radius, 0.0);
// 右半圆
for i in 0..=16 {
let angle = std::f64::consts::PI * (i as f64 / 16.0);
let x = half_length + radius * angle.cos();
let y = radius * angle.sin();
path.line_to(x, y, 0.0);
}
// 底部直线
path.line_to(-half_length, -radius, 0.0);
path.close();
Command::extrude(path, height)
}
所有者
- kittycad/crate-owners
- Adam Chalmers
- Jess Frazelle
- Paul Tagliamonte
这个库提供了强大的3D建模功能,适合需要高效CAD建模命令和几何图形处理的Rust开发者使用。通过组合基本命令可以创建复杂的3D模型,并支持各种几何变换和布尔运算。
1 回复
Rust 3D建模插件库kittycad-modeling-cmds使用指南
概述
kittycad-modeling-cmds是一个用于3D建模和CAD操作的Rust库,提供高效的几何图形处理和建模命令功能。这个库特别适合需要程序化生成3D模型的场景,如参数化设计、自动化建模等。
主要特性
- 支持多种基本几何体创建和操作
- 提供布尔运算(并集、差集、交集)
- 支持2D到3D的转换操作(如拉伸、旋转)
- 高效的几何计算和转换
- 与KittyCAD平台集成
安装
在Cargo.toml中添加依赖:
[dependencies]
kittycad-modeling-cmds = "0.1"
完整示例代码
use kittycad_modeling_cmds::{
modeling::{self, Geometry, BooleanOperationType, PathSegment, Transform},
Point2D, Point3D,
extrude_path,
bspline_surface,
error::ModelingError,
};
fn main() -> Result<(), ModelingError> {
// 1. 创建基本几何体
let cube = modeling::create_cube(
Point3D::new(0.0, 0.0, 0.0), // 中心点
10.0, // 边长
)?;
let sphere = modeling::create_sphere(
Point3D::new(5.0, 5.0, 5.0), // 中心点
3.0, // 半径
)?;
// 2. 布尔运算
let union = modeling::boolean_operation(
&cube,
&sphere,
BooleanOperationType::Union,
)?;
// 3. 2D到3D转换
let path = vec![
PathSegment::LineTo(Point2D::new(10.0, 0.0)),
PathSegment::LineTo(Point2D::new(10.0, 10.0)),
PathSegment::LineTo(Point2D::new(0.0, 10.0)),
PathSegment::Close,
];
let extruded = extrude_path(
&path,
5.0, // 拉伸高度
None, // 可选: 拉伸角度(用于锥形拉伸)
)?;
// 4. 模型变换
let mut transformed_cube = modeling::create_cube(Point3D::origin(), 5.0)?;
// 移动模型
transformed_cube.transform(Transform::translation(10.0, 0.0, 0.0));
// 旋转模型
transformed_cube.transform(Transform::rotation_x(45.0_f64.to_radians()));
// 缩放模型
transformed_cube.transform(Transform::scale(2.0, 1.0, 1.0));
// 5. 复杂曲线创建
let control_points = vec![
vec![Point3D::new(0.0, 0.0, 0.0), Point3D::new(1.0, 0.0, 1.0)],
vec![Point3D::new(0.0, 1.0, 1.0), Point3D::new(1.0, 1.0, 0.0)],
];
let surface = bspline_surface(
control_points,
vec![0.0, 0.0, 1.0, 1.0], // u节点向量
vec![0.0, 0.0, 1.0, 1.0], // v节点向量
1, // u次数
1, // v次数
)?;
Ok(())
}
// 与KittyCAD平台集成的异步示例
#[cfg(feature = "async")]
async fn upload_to_kittycad() -> Result<(), Box<dyn std::error::Error>> {
use kittycad_modeling_cmds::{
client::KittycadClient,
modeling::{self, Geometry},
};
let client = KittycadClient::new("your-api-key");
let cube = modeling::create_cube(Point3D::origin(), 10.0)?;
// 上传模型到KittyCAD平台
let response = client.upload_model(&cube, "my_cube").await?;
println!("Model uploaded with ID: {:?}", response.id);
Ok(())
}
性能提示
- 对于复杂模型,考虑使用
Arc
共享几何数据 - 批量操作时使用事务可以提高性能
- 重用几何对象而不是重复创建
错误处理
use kittycad_modeling_cmds::error::ModelingError;
let result = modeling::create_sphere(Point3D::origin(), -1.0);
match result {
Ok(sphere) => println!("Sphere created"),
Err(ModelingError::InvalidParameter(msg)) => {
eprintln!("Invalid parameter: {}", msg);
},
Err(e) => eprintln!("Error: {:?}", e),
}
这个库为Rust开发者提供了强大的3D建模能力,特别适合需要将CAD功能集成到Rust应用程序中的场景。