Chili3d

From znlgis/opengis-skills · View interactive version

Chili3d is an AI agent skill from the GitHub repository znlgis/opengis-skills. Use when building or extending browser-based parametric CAD in TypeScript with Chili3D — npm-workspaces rspack build, chili-wasm Open CASCADE bindings, @command/ICommand plus Plugin contracts and .chiliplugin packaging, STEP/IGES/BREP/STL exchange, ribbon and i18n contributions; also for \"SharedArr

How to use this skill

Skill file

项目地址: <https://github.com/xiangechen/chili3d>

在线体验: <https://chili3d.com/>(Cloudflare 部署:<https://chili3d.pages.dev>)

许可证: AGPL-3.0

最新版本: 0.8.0(2026-10-30,GitHub Release tag 0.8.0;npm 仅 @chili3d/core 发布同版 0.8.0,其余 @chili3d/* 为工作区包,截至 2026-11,来源 GitHub Releases)

包管理器: npm workspaces(根 package.json 与 packages/core/package.json 版本均为 0.8.0)

中文教程: <https://znlgis.github.io/cad/chili3d/>

概述

Chili3D 主要特性:


环境准备:本地部署与二次开发

仓库使用 npm workspaces(根目录有 package-lock.json),Node + npm 即可:

git clone https://github.com/xiangechen/chili3d.git
cd chili3d
npm install

npm run dev           # rspack dev server,http://localhost:8080
npm run build         # 生产构建

npm run test          # Rstest + Happy-DOM 全量测试
npm run testc         # 带覆盖率
npm run check         # Biome lint + 自动修复
npm run format        # Biome + clang-format(TS 与 C++)

自研 WASM 模块已随仓库提供(packages/wasm/lib),只有需要改 C++ 绑定时才重新编译:

npm run setup:wasm    # 一次性准备 Emscripten/CMake 依赖
npm run build:wasm    # cmake --preset release 编译,产物回拷到 wasm 包 lib

Docker 部署(compose.yml 已在仓库根):

docker compose up -d  # 构建并在 http://localhost:8080 提供服务

构建产物是静态站点,可直接部署到 Nginx / Cloudflare Pages / GitHub Pages。

在线试用(免安装)

直接打开 <https://chili3d.com/>:

  1. 选择「新建」或导入 STEP/IGES/BREP
  2. 左侧工具栏:Sketch / Box / Cylinder / Sphere / ...
  3. 右侧属性面板:编辑参数、变换
  4. 顶部 Ribbon:Boolean / Fillet / Chamfer / Offset / Array
  5. 文件 → 导出(STEP / IGES / BREP / STL)

核心 API

项目结构

chili3d/
├── cpp/                    # chili-wasm:CMake + Emscripten 绑定 Open CASCADE
│   └── src/                # opencascade.cpp / factory.cpp / converter.cpp / shape.cpp / mesher.cpp ...
├── plugins/                # 插件示例(npm workspaces 的一部分)
│   ├── helloworld-js/ helloworld-ts/ macro/ visual-programming/
├── packages/
│   ├── core/               # 模型、命令、应用框架(@chili3d/core)
│   ├── three/              # Three.js 渲染(@chili3d/three)
│   ├── wasm/               # chili-wasm 的 TS 封装(@chili3d/wasm)
│   ├── builder/            # 拉伸/旋转/扫掠/放样等特征构建器(@chili3d/builder)
│   ├── element/            # Web Components 控件(@chili3d/element)
│   ├── parametric/         # 参数化功能定义
│   ├── ui/                 # UI 组件
│   ├── web/                # 主 Web 应用(@chili3d/web)
│   ├── app/                # 应用入口(@chili3d/app)
│   ├── storage/            # 项目存储
│   ├── i18n/               # 国际化
│   └── ai/                 # AI 助手集成(@chili3d/ai,依赖 @anthropic-ai/sdk 与 openai)
├── rspack.config.ts        # 打包与 dev server
├── rstest.config.ts        # 测试(rstest + happy-dom)
└── biome.json              # lint / format(4 空格、110 列、双引号)

二次开发的正确姿势

Chili3D 是应用 + 插件模型,不是可 npm install 的嵌入式 SDK:@chili3d/* 中只有 @chili3d/core 发布到了 npm(0.8.0),web/wasm/app/builder 等仍是工作区包。因此二次开发有两条路:

  1. 写插件(推荐):在 plugins/ 下建包,导出 Plugin 对象,npm run package 产出 .chiliplugin,拖拽进应用安装。
  2. 改源码:fork 仓库,在 packages/* 内扩展(npm run build 会同时构建应用与 plugins/)。

整站嵌入自有系统时可用 iframe,但 WASM 依赖 SharedArrayBuffer,宿主站点必须返回 COOP/COEP 头:

<iframe src="https://chili3d.com/" allow="cross-origin-isolated"
        style="width:100%;height:100vh;border:0"></iframe>

需配置 Cross-Origin-Opener-Policy: same-origin 与 Cross-Origin-Embedder-Policy: require-corp(或本地 dev server 已是 cross-origin isolated)。


命令系统

命令是 ICommand 实现类,用 command() 装饰器注册到 CommandStore;元数据是 CommandData(key / icon / toggle / helpText / helpUrl / isApplicationCommand):

import { command, type I18nKeys, type IApplication, type ICommand, PubSub } from '@chili3d/core';

@command({
    key: 'demo.hello' as any,                       // key 属于 CommandKeys 联合类型
    icon: { type: 'path', value: 'icons/hello.svg' }, // 也可传 svg/png/url 或字符串
    helpText: 'demo.hello.description' as any,
})
export class HelloWorldCommand implements ICommand {
    async execute(application: IApplication): Promise<void> {
        // 应用级通知走 PubSub 默认实例,文案 key 交给 i18n
        PubSub.default.pub('showToast', 'demo.hello.message' as I18nKeys);
    }
}

IApplication 暴露 mainWindow dataExchange visualFactory shapeProvider services storage views documents pluginManager lastCommand executingCommand activeView,以及 newDocument(name) / openDocument(id) / loadDocument(data) / loadFileFromUrl(url)。需要中途取消的命令继承 CancelableCommand(实现 ICancelableCommand,带 cancel() 与 disposeStack)。


插件契约(Plugin)

@chili3d/core 的 Plugin 类型是声明式的:把命令、Ribbon、i18n、服务与「AI 手册章节」一次性交给框架。

// plugins/my-plugin/src/index.ts
import type { CommandKeys, Plugin } from '@chili3d/core';
import { MyCommand } from './commands/my';

const MyPlugin: Plugin = {
    commands: [MyCommand],
    ribbons: [
        {
            tabName: 'ribbon.tab.manager',
            groups: [{ groupName: 'ribbon.group.other', items: ['demo.hello' as CommandKeys] }],
        },
    ],
    // 追加到 AI 助手读取的应用手册,让助手知道按钮做什么
    guide: [{ name: 'My Plugin', content: 'The “My” button …' }],
    i18nResources: [
        { language: 'zh-CN', display: '简体中文', translation: { 'command.demo.hello': 'TS 插件' } },
    ],
};

export default MyPlugin;

配套文件:manifest.json(name / version / main: dist/main.js / engines.chili3d: ">=0.6" / importmap)与 importmap.json(插件运行时外部模块映射)。


直接调用几何内核

@chili3d/wasm 提供 initWasm():浏览器里由 Emscripten 自己取 .wasm,Node(如 MCP 服务与集成测试)需传入 wasmBinary:

import { initWasm } from '@chili3d/wasm';

// 浏览器
const wasm = await initWasm();
// Node:需要把二进制读出来
// const wasm = await initWasm({ wasmBinary: fs.readFileSync('packages/wasm/lib/chili-wasm.wasm') });

// wasm 上挂的是 OCCT 暴露出来的 BRep* / Geom* 类,构造结果再交给 @chili3d/wasm 的
// converter / factory / shapeProvider 转成 Chili3D 的 shape 与可视对象

@chili3d/wasm 的导出面:initWasm(wasm.ts)、converter、factory、shapeProvider,另有 geometry / shape / surface / curve / stlWriter 等内部模块;C++ 侧入口是 cpp/src/opencascade.cpp。


文件 I/O


渲染层(@chili3d/three)

渲染包内是 threeView.ts / threeGeometry.ts / threeGeometryFactory.ts / threeHelper.ts / threeHighlighter.ts / materials.ts / meshExporter.ts / cameraController.ts / threeAnnotation.ts / outlinePass.js,负责把 shape 转成 three 几何、高亮、剖面、导出与相机控制。改材质与高亮行为从这里入手,命令与文档逻辑保持在 @chili3d/core,不要把 OCCT 调用写进 UI 层。


典型工作流

输入:2D 草图轮廓或 STEP/IGES 文件;输出:STEP/STL 模型或 .chiliplugin 插件包。

工作流一:在线参数化建模与导出

  1. 打开 <https://chili3d.com/> 或本地部署实例
  2. 新建项目,使用 Sketch 工具绘制 2D 轮廓
  3. 通过 Extrude/Revolve 将草图转为 3D 实体
  4. 添加圆角、倒角等修饰特征
  5. 使用布尔运算(Union/Cut/Intersect)组合多个实体
  6. 导出为 STEP/STL 用于下游加工或 3D 打印

工作流二:开发一个插件并装入应用

  1. 复制 plugins/helloworld-ts 作骨架(manifest.json + importmap.json + src/index.ts + src/commands/)
  2. 用 @command({ key, icon, helpText }) 定义命令类,实现 ICommand.execute(application)
  3. 在 Plugin 对象里声明 commands / ribbons / i18nResources / guide
  4. 进入插件包目录(如 cd plugins/helloworld-ts)后 npm install && npm run build && npm run package 产出 .chiliplugin(根目录没有 package 脚本)
  5. 打开 Chili3D,把 .chiliplugin 拖进窗口 → Ribbon 出现按钮,点击执行
  6. npm run test 与 npm run check 通过后提交(Biome:4 空格、110 列、双引号)

适用场景


性能要点

  1. WASM 内核走 SharedArrayBuffer,未启用 COOP/COEP 时退化为单线程
  2. 大模型先压缩 STEP,导入后转 BREP 缓存
  3. 渲染端关注 @chili3d/three 的几何转换与高亮开销,避免每帧重建 BRep 网格
  4. 频繁布尔运算放在命令内一次性提交,减少撤销栈与网格重生成次数
  5. 浏览器内存有限,STEP 源文件建议控制在百 MB 以内,超出部分服务端处理

常见问题

| 报错 / 现象 | 原因 | 处置 |

|------------|------|------|

| 加载 chili-wasm 失败 | 站点未启用 COOP/COEP,SharedArrayBuffer 不可用 | 配置响应头 Cross-Origin-Opener-Policy/Cross-Origin-Embedder-Policy,确认 cross-origin-isolated |

| Node 里 initWasm() 取不到 wasm | 浏览器外没有 fetch 与 import.meta.url,Emscripten 无法自动定位文件 | 显式传 wasmBinary |

| 无法导入 DWG | 当前版本不支持 DWG | 先转 DXF/STEP 再导入 |

| 中文菜单 | 默认语言跟随浏览器/首次启动为英文 | 在语言设置中切换中文(内置 zh-CN 资源) |

| 构建慢或内存溢出 | 跑的是 dev 模式产物,或 Node 内存上限不足 | 用 npm run build 生成生产构建;必要时调大 NODE_OPTIONS=--max-old-space-size |

| 插件不出现按钮 | manifest.json 的 main 路径错误、importmap.json 映射缺失,或 Ribbon items 不是已注册命令 key | 逐项核对三处配置 |

| 改 C++ 绑定后无效果 | 未重新编译 WASM,或产物未回拷 | 执行 npm run setup:wasm + npm run build:wasm |

AI 使用建议


相关技能


参考资源

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.