2026-01-15 14:22:43 +08:00
|
|
|
|
# BIM Engine SDK 使用文档
|
|
|
|
|
|
|
|
|
|
|
|
BIM Engine SDK 是一个用于 3D BIM 模型展示的 JavaScript SDK,支持原生 HTML、Vue 2/3 和 React 框架。
|
|
|
|
|
|
|
|
|
|
|
|
## 目录
|
|
|
|
|
|
|
|
|
|
|
|
- [安装](#安装)
|
|
|
|
|
|
- [原生 HTML 使用](#原生-html-使用)
|
|
|
|
|
|
- [Vue 3 使用](#vue-3-使用)
|
|
|
|
|
|
- [Vue 2 使用](#vue-2-使用)
|
|
|
|
|
|
- [React 使用](#react-使用)
|
|
|
|
|
|
- [API 参考](#api-参考)
|
|
|
|
|
|
- [类型定义](#类型定义)
|
|
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
|
|
## 安装
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
### NPM 安装(推荐)
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
|
|
|
|
|
```bash
|
2026-01-15 16:46:53 +08:00
|
|
|
|
npm install @fishdingding/bim-engine-sdk
|
2026-01-15 14:22:43 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
### Yarn 安装
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
yarn add @fishdingding/bim-engine-sdk
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### PNPM 安装
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
pnpm add @fishdingding/bim-engine-sdk
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### CDN / 本地文件引入
|
|
|
|
|
|
|
|
|
|
|
|
如果不使用包管理器,可以直接下载 JS 文件引入:
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
|
<script src="./lib/bim-engine-sdk.umd.js"></script>
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
|
|
## 原生 HTML 使用
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
### 方式一:使用 NPM + 构建工具
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
|
<html lang="zh-CN">
|
|
|
|
|
|
<head>
|
|
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
|
|
<title>BIM Engine Demo</title>
|
|
|
|
|
|
<style>
|
|
|
|
|
|
* { margin: 0; padding: 0; box-sizing: border-box; }
|
|
|
|
|
|
html, body { width: 100%; height: 100%; overflow: hidden; }
|
|
|
|
|
|
#app { width: 100%; height: 100%; }
|
|
|
|
|
|
</style>
|
|
|
|
|
|
</head>
|
|
|
|
|
|
<body>
|
|
|
|
|
|
<div id="app"></div>
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
<script type="module">
|
|
|
|
|
|
import { BimEngine } from '@fishdingding/bim-engine-sdk';
|
|
|
|
|
|
|
2026-01-15 14:22:43 +08:00
|
|
|
|
// 1. 创建引擎实例
|
2026-01-15 16:46:53 +08:00
|
|
|
|
const engine = new BimEngine('app', {
|
|
|
|
|
|
locale: 'zh-CN',
|
|
|
|
|
|
theme: 'light'
|
2026-01-15 14:22:43 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 初始化 3D 引擎
|
|
|
|
|
|
const success = engine.engine.initialize({
|
2026-01-15 16:46:53 +08:00
|
|
|
|
version: 'v2',
|
|
|
|
|
|
showStats: false,
|
|
|
|
|
|
showViewCube: true
|
2026-01-15 14:22:43 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
|
|
// 3. 加载模型
|
|
|
|
|
|
engine.engine.loadModel('./model/your-model', {
|
2026-01-15 16:46:53 +08:00
|
|
|
|
position: [0, 0, 0],
|
|
|
|
|
|
rotation: [0, 0, 0],
|
|
|
|
|
|
scale: [1, 1, 1]
|
2026-01-15 14:22:43 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
</script>
|
|
|
|
|
|
</body>
|
|
|
|
|
|
</html>
|
|
|
|
|
|
```
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
### 方式二:使用 UMD(无构建工具)
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
|
|
|
|
|
```html
|
2026-01-15 16:46:53 +08:00
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
|
<html lang="zh-CN">
|
|
|
|
|
|
<head>
|
|
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
|
|
<title>BIM Engine Demo</title>
|
|
|
|
|
|
<!-- 引入 UMD 版本 -->
|
|
|
|
|
|
<script src="https://unpkg.com/@fishdingding/bim-engine-sdk/dist/bim-engine-sdk.umd.js"></script>
|
|
|
|
|
|
<style>
|
|
|
|
|
|
* { margin: 0; padding: 0; box-sizing: border-box; }
|
|
|
|
|
|
html, body { width: 100%; height: 100%; overflow: hidden; }
|
|
|
|
|
|
#app { width: 100%; height: 100%; }
|
|
|
|
|
|
</style>
|
|
|
|
|
|
</head>
|
|
|
|
|
|
<body>
|
|
|
|
|
|
<div id="app"></div>
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
<script>
|
|
|
|
|
|
// 1. 创建引擎实例
|
|
|
|
|
|
const engine = new LyzBimEngineSDK.BimEngine('app', {
|
2026-01-15 14:22:43 +08:00
|
|
|
|
locale: 'zh-CN',
|
|
|
|
|
|
theme: 'light'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
// 2. 初始化 3D 引擎
|
|
|
|
|
|
const success = engine.engine.initialize({
|
2026-01-15 14:22:43 +08:00
|
|
|
|
version: 'v2',
|
|
|
|
|
|
showStats: false,
|
|
|
|
|
|
showViewCube: true
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
if (success) {
|
|
|
|
|
|
// 3. 加载模型
|
|
|
|
|
|
engine.engine.loadModel('./model/your-model', {
|
|
|
|
|
|
position: [0, 0, 0],
|
|
|
|
|
|
rotation: [0, 0, 0],
|
|
|
|
|
|
scale: [1, 1, 1]
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
</script>
|
|
|
|
|
|
</body>
|
|
|
|
|
|
</html>
|
|
|
|
|
|
```
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
### 完整功能示例
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
```javascript
|
|
|
|
|
|
let engine = null;
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
// 初始化
|
|
|
|
|
|
function init() {
|
|
|
|
|
|
engine = new LyzBimEngineSDK.BimEngine('app', {
|
|
|
|
|
|
locale: 'zh-CN',
|
|
|
|
|
|
theme: 'light'
|
|
|
|
|
|
});
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
engine.engine.initialize({
|
|
|
|
|
|
version: 'v2',
|
|
|
|
|
|
showStats: false,
|
|
|
|
|
|
showViewCube: true
|
|
|
|
|
|
});
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
engine.engine.loadModel('./model/building');
|
|
|
|
|
|
}
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
// 回到主视角
|
|
|
|
|
|
function goHome() {
|
|
|
|
|
|
engine.engine.CameraGoHome();
|
|
|
|
|
|
}
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
// 激活测量功能
|
|
|
|
|
|
// mode: 'distance' | 'minDistance' | 'angle' | 'elevation' | 'volume' | 'laserDistance' | 'slope' | 'spaceVolume'
|
|
|
|
|
|
function activateMeasure(mode) {
|
|
|
|
|
|
engine.engine.activateMeasure(mode);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 停用测量
|
|
|
|
|
|
function deactivateMeasure() {
|
|
|
|
|
|
engine.engine.deactivateMeasure();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 切换主题
|
|
|
|
|
|
function setTheme(theme) {
|
|
|
|
|
|
engine.setTheme(theme); // 'light' | 'dark'
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 切换语言
|
|
|
|
|
|
function setLocale(locale) {
|
|
|
|
|
|
engine.setLocale(locale); // 'zh-CN' | 'en-US'
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 销毁
|
|
|
|
|
|
function destroy() {
|
|
|
|
|
|
engine.destroy();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
window.onload = init;
|
2026-01-15 14:22:43 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
|
|
## Vue 3 使用
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
### 1. 安装
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
|
|
|
|
|
```bash
|
2026-01-15 16:46:53 +08:00
|
|
|
|
npm install @fishdingding/bim-engine-sdk
|
2026-01-15 14:22:43 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
### 2. 组件封装
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
|
|
|
|
|
```vue
|
2026-01-15 16:46:53 +08:00
|
|
|
|
<!-- components/BimViewer.vue -->
|
2026-01-15 14:22:43 +08:00
|
|
|
|
<template>
|
|
|
|
|
|
<div ref="containerRef" class="bim-viewer"></div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
<script setup>
|
2026-01-15 14:22:43 +08:00
|
|
|
|
import { ref, onMounted, onBeforeUnmount } from 'vue';
|
2026-01-15 16:46:53 +08:00
|
|
|
|
import { BimEngine } from '@fishdingding/bim-engine-sdk';
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
|
|
|
|
|
// Props
|
2026-01-15 16:46:53 +08:00
|
|
|
|
const props = defineProps({
|
|
|
|
|
|
modelUrl: { type: String, default: '' },
|
|
|
|
|
|
modelOptions: { type: Object, default: () => ({}) },
|
|
|
|
|
|
engineOptions: { type: Object, default: () => ({}) },
|
|
|
|
|
|
locale: { type: String, default: 'zh-CN' },
|
|
|
|
|
|
theme: { type: String, default: 'light' }
|
2026-01-15 14:22:43 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Emits
|
2026-01-15 16:46:53 +08:00
|
|
|
|
const emit = defineEmits(['ready', 'error']);
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
|
|
|
|
|
// Refs
|
2026-01-15 16:46:53 +08:00
|
|
|
|
const containerRef = ref(null);
|
|
|
|
|
|
let engine = null;
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
|
|
|
|
|
// 初始化引擎
|
|
|
|
|
|
const initEngine = () => {
|
|
|
|
|
|
if (!containerRef.value) return;
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
engine = new BimEngine(containerRef.value, {
|
|
|
|
|
|
locale: props.locale,
|
|
|
|
|
|
theme: props.theme
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const success = engine.engine?.initialize({
|
|
|
|
|
|
version: 'v2',
|
|
|
|
|
|
showStats: false,
|
|
|
|
|
|
showViewCube: true,
|
|
|
|
|
|
...props.engineOptions
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
|
|
emit('ready', engine);
|
|
|
|
|
|
if (props.modelUrl) {
|
|
|
|
|
|
engine.engine?.loadModel(props.modelUrl, props.modelOptions);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
throw new Error('3D 引擎初始化失败');
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
2026-01-15 16:46:53 +08:00
|
|
|
|
emit('error', error);
|
2026-01-15 14:22:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
// 暴露方法
|
2026-01-15 14:22:43 +08:00
|
|
|
|
defineExpose({
|
|
|
|
|
|
getEngine: () => engine,
|
2026-01-15 16:46:53 +08:00
|
|
|
|
loadModel: (url, options) => engine?.engine?.loadModel(url, options),
|
|
|
|
|
|
goHome: () => engine?.engine?.CameraGoHome(),
|
|
|
|
|
|
activateMeasure: (mode) => engine?.engine?.activateMeasure(mode),
|
|
|
|
|
|
deactivateMeasure: () => engine?.engine?.deactivateMeasure(),
|
|
|
|
|
|
setTheme: (theme) => engine?.setTheme(theme),
|
|
|
|
|
|
setLocale: (locale) => engine?.setLocale(locale)
|
2026-01-15 14:22:43 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
onMounted(() => initEngine());
|
2026-01-15 14:22:43 +08:00
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
|
|
engine?.destroy();
|
|
|
|
|
|
engine = null;
|
|
|
|
|
|
});
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
|
.bim-viewer {
|
|
|
|
|
|
width: 100%;
|
|
|
|
|
|
height: 100%;
|
|
|
|
|
|
}
|
|
|
|
|
|
</style>
|
|
|
|
|
|
```
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
### 3. 使用组件
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
|
|
|
|
|
```vue
|
|
|
|
|
|
<!-- App.vue -->
|
|
|
|
|
|
<template>
|
|
|
|
|
|
<div class="app">
|
|
|
|
|
|
<BimViewer
|
|
|
|
|
|
ref="viewerRef"
|
|
|
|
|
|
model-url="./model/building"
|
|
|
|
|
|
:model-options="{ position: [0, 0, 0] }"
|
|
|
|
|
|
locale="zh-CN"
|
|
|
|
|
|
theme="light"
|
|
|
|
|
|
@ready="onEngineReady"
|
|
|
|
|
|
@error="onEngineError"
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
|
|
<div class="toolbar">
|
|
|
|
|
|
<button @click="goHome">主视角</button>
|
|
|
|
|
|
<button @click="measure('distance')">距离测量</button>
|
|
|
|
|
|
<button @click="stopMeasure">停止测量</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
<script setup>
|
2026-01-15 14:22:43 +08:00
|
|
|
|
import { ref } from 'vue';
|
|
|
|
|
|
import BimViewer from './components/BimViewer.vue';
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
const viewerRef = ref(null);
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
const onEngineReady = (engine) => {
|
2026-01-15 14:22:43 +08:00
|
|
|
|
console.log('引擎已就绪', engine);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
const onEngineError = (error) => {
|
2026-01-15 14:22:43 +08:00
|
|
|
|
console.error('引擎错误', error);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
const goHome = () => viewerRef.value?.goHome();
|
|
|
|
|
|
const measure = (mode) => viewerRef.value?.activateMeasure(mode);
|
|
|
|
|
|
const stopMeasure = () => viewerRef.value?.deactivateMeasure();
|
2026-01-15 14:22:43 +08:00
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style>
|
|
|
|
|
|
.app {
|
|
|
|
|
|
width: 100vw;
|
|
|
|
|
|
height: 100vh;
|
|
|
|
|
|
position: relative;
|
|
|
|
|
|
}
|
|
|
|
|
|
.toolbar {
|
|
|
|
|
|
position: absolute;
|
|
|
|
|
|
top: 20px;
|
|
|
|
|
|
left: 20px;
|
|
|
|
|
|
z-index: 100;
|
|
|
|
|
|
}
|
|
|
|
|
|
</style>
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
|
|
## Vue 2 使用
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
### 1. 安装
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
|
|
|
|
|
```bash
|
2026-01-15 16:46:53 +08:00
|
|
|
|
npm install @fishdingding/bim-engine-sdk
|
2026-01-15 14:22:43 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
### 2. 组件封装
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
|
|
|
|
|
```vue
|
2026-01-15 16:46:53 +08:00
|
|
|
|
<!-- components/BimViewer.vue -->
|
2026-01-15 14:22:43 +08:00
|
|
|
|
<template>
|
|
|
|
|
|
<div ref="container" class="bim-viewer"></div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script>
|
2026-01-15 16:46:53 +08:00
|
|
|
|
import { BimEngine } from '@fishdingding/bim-engine-sdk';
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
|
name: 'BimViewer',
|
|
|
|
|
|
|
|
|
|
|
|
props: {
|
2026-01-15 16:46:53 +08:00
|
|
|
|
modelUrl: { type: String, default: '' },
|
|
|
|
|
|
modelOptions: { type: Object, default: () => ({}) },
|
|
|
|
|
|
engineOptions: { type: Object, default: () => ({}) },
|
|
|
|
|
|
locale: { type: String, default: 'zh-CN' },
|
|
|
|
|
|
theme: { type: String, default: 'light' }
|
2026-01-15 14:22:43 +08:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
data() {
|
|
|
|
|
|
return {
|
|
|
|
|
|
engine: null
|
|
|
|
|
|
};
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
mounted() {
|
|
|
|
|
|
this.initEngine();
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
beforeDestroy() {
|
|
|
|
|
|
this.destroyEngine();
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
|
|
initEngine() {
|
|
|
|
|
|
try {
|
|
|
|
|
|
this.engine = new BimEngine(this.$refs.container, {
|
|
|
|
|
|
locale: this.locale,
|
|
|
|
|
|
theme: this.theme
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const success = this.engine.engine.initialize({
|
|
|
|
|
|
version: 'v2',
|
|
|
|
|
|
showStats: false,
|
|
|
|
|
|
showViewCube: true,
|
|
|
|
|
|
...this.engineOptions
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
|
|
this.$emit('ready', this.engine);
|
|
|
|
|
|
if (this.modelUrl) {
|
|
|
|
|
|
this.engine.engine.loadModel(this.modelUrl, this.modelOptions);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
throw new Error('3D 引擎初始化失败');
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
this.$emit('error', error);
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
destroyEngine() {
|
|
|
|
|
|
if (this.engine) {
|
|
|
|
|
|
this.engine.destroy();
|
|
|
|
|
|
this.engine = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 公开方法
|
2026-01-15 16:46:53 +08:00
|
|
|
|
getEngine() { return this.engine; },
|
|
|
|
|
|
loadModel(url, options) { this.engine?.engine?.loadModel(url, options); },
|
|
|
|
|
|
goHome() { this.engine?.engine?.CameraGoHome(); },
|
|
|
|
|
|
activateMeasure(mode) { this.engine?.engine?.activateMeasure(mode); },
|
|
|
|
|
|
deactivateMeasure() { this.engine?.engine?.deactivateMeasure(); },
|
|
|
|
|
|
setTheme(theme) { this.engine?.setTheme(theme); },
|
|
|
|
|
|
setLocale(locale) { this.engine?.setLocale(locale); }
|
2026-01-15 14:22:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
|
.bim-viewer {
|
|
|
|
|
|
width: 100%;
|
|
|
|
|
|
height: 100%;
|
|
|
|
|
|
}
|
|
|
|
|
|
</style>
|
|
|
|
|
|
```
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
### 3. 使用组件
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
|
|
|
|
|
```vue
|
|
|
|
|
|
<!-- App.vue -->
|
|
|
|
|
|
<template>
|
|
|
|
|
|
<div class="app">
|
|
|
|
|
|
<BimViewer
|
|
|
|
|
|
ref="viewer"
|
|
|
|
|
|
model-url="./model/building"
|
|
|
|
|
|
:model-options="{ position: [0, 0, 0] }"
|
|
|
|
|
|
locale="zh-CN"
|
|
|
|
|
|
theme="light"
|
|
|
|
|
|
@ready="onEngineReady"
|
|
|
|
|
|
@error="onEngineError"
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
|
|
<div class="toolbar">
|
|
|
|
|
|
<button @click="goHome">主视角</button>
|
|
|
|
|
|
<button @click="measure('distance')">距离测量</button>
|
|
|
|
|
|
<button @click="stopMeasure">停止测量</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
|
import BimViewer from './components/BimViewer.vue';
|
|
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
|
name: 'App',
|
2026-01-15 16:46:53 +08:00
|
|
|
|
components: { BimViewer },
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
|
|
|
|
|
methods: {
|
2026-01-15 16:46:53 +08:00
|
|
|
|
onEngineReady(engine) { console.log('引擎已就绪', engine); },
|
|
|
|
|
|
onEngineError(error) { console.error('引擎错误', error); },
|
|
|
|
|
|
goHome() { this.$refs.viewer.goHome(); },
|
|
|
|
|
|
measure(mode) { this.$refs.viewer.activateMeasure(mode); },
|
|
|
|
|
|
stopMeasure() { this.$refs.viewer.deactivateMeasure(); }
|
2026-01-15 14:22:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style>
|
2026-01-15 16:46:53 +08:00
|
|
|
|
.app { width: 100vw; height: 100vh; position: relative; }
|
|
|
|
|
|
.toolbar { position: absolute; top: 20px; left: 20px; z-index: 100; }
|
2026-01-15 14:22:43 +08:00
|
|
|
|
</style>
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
|
|
## React 使用
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
### 1. 安装
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
|
|
|
|
|
```bash
|
2026-01-15 16:46:53 +08:00
|
|
|
|
npm install @fishdingding/bim-engine-sdk
|
2026-01-15 14:22:43 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
### 2. Hook 封装
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
```jsx
|
|
|
|
|
|
// hooks/useBimEngine.js
|
2026-01-15 14:22:43 +08:00
|
|
|
|
import { useEffect, useRef, useState, useCallback } from 'react';
|
2026-01-15 16:46:53 +08:00
|
|
|
|
import { BimEngine } from '@fishdingding/bim-engine-sdk';
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
export function useBimEngine(options = {}) {
|
|
|
|
|
|
const containerRef = useRef(null);
|
|
|
|
|
|
const engineRef = useRef(null);
|
2026-01-15 14:22:43 +08:00
|
|
|
|
const [isReady, setIsReady] = useState(false);
|
2026-01-15 16:46:53 +08:00
|
|
|
|
const [error, setError] = useState(null);
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (!containerRef.current) return;
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
const engine = new BimEngine(containerRef.current, {
|
|
|
|
|
|
locale: options.locale || 'zh-CN',
|
|
|
|
|
|
theme: options.theme || 'light'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const success = engine.engine?.initialize({
|
|
|
|
|
|
version: 'v2',
|
|
|
|
|
|
showStats: false,
|
|
|
|
|
|
showViewCube: true,
|
|
|
|
|
|
...options.engineOptions
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
|
|
engineRef.current = engine;
|
|
|
|
|
|
setIsReady(true);
|
|
|
|
|
|
options.onReady?.(engine);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
throw new Error('3D 引擎初始化失败');
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (err) {
|
2026-01-15 16:46:53 +08:00
|
|
|
|
setError(err);
|
|
|
|
|
|
options.onError?.(err);
|
2026-01-15 14:22:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
|
engineRef.current?.destroy();
|
|
|
|
|
|
engineRef.current = null;
|
|
|
|
|
|
setIsReady(false);
|
|
|
|
|
|
};
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
const loadModel = useCallback((url, modelOptions) => {
|
2026-01-15 14:22:43 +08:00
|
|
|
|
engineRef.current?.engine?.loadModel(url, modelOptions);
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
const goHome = useCallback(() => {
|
|
|
|
|
|
engineRef.current?.engine?.CameraGoHome();
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
const activateMeasure = useCallback((mode) => {
|
2026-01-15 14:22:43 +08:00
|
|
|
|
engineRef.current?.engine?.activateMeasure(mode);
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
const deactivateMeasure = useCallback(() => {
|
|
|
|
|
|
engineRef.current?.engine?.deactivateMeasure();
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
const setTheme = useCallback((theme) => {
|
2026-01-15 14:22:43 +08:00
|
|
|
|
engineRef.current?.setTheme(theme);
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
const setLocale = useCallback((locale) => {
|
2026-01-15 14:22:43 +08:00
|
|
|
|
engineRef.current?.setLocale(locale);
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
containerRef,
|
|
|
|
|
|
engine: engineRef.current,
|
|
|
|
|
|
isReady,
|
|
|
|
|
|
error,
|
|
|
|
|
|
loadModel,
|
|
|
|
|
|
goHome,
|
|
|
|
|
|
activateMeasure,
|
|
|
|
|
|
deactivateMeasure,
|
|
|
|
|
|
setTheme,
|
|
|
|
|
|
setLocale
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
### 3. 组件封装
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
```jsx
|
|
|
|
|
|
// components/BimViewer.jsx
|
|
|
|
|
|
import React, { useEffect, useImperativeHandle, forwardRef } from 'react';
|
2026-01-15 14:22:43 +08:00
|
|
|
|
import { useBimEngine } from '../hooks/useBimEngine';
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
export const BimViewer = forwardRef((props, ref) => {
|
2026-01-15 14:22:43 +08:00
|
|
|
|
const {
|
|
|
|
|
|
modelUrl,
|
|
|
|
|
|
modelOptions,
|
|
|
|
|
|
locale = 'zh-CN',
|
|
|
|
|
|
theme = 'light',
|
|
|
|
|
|
className,
|
|
|
|
|
|
style,
|
|
|
|
|
|
onReady,
|
|
|
|
|
|
onError
|
|
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
|
containerRef,
|
|
|
|
|
|
engine,
|
|
|
|
|
|
isReady,
|
|
|
|
|
|
loadModel,
|
|
|
|
|
|
goHome,
|
|
|
|
|
|
activateMeasure,
|
|
|
|
|
|
deactivateMeasure,
|
|
|
|
|
|
setTheme,
|
|
|
|
|
|
setLocale
|
2026-01-15 16:46:53 +08:00
|
|
|
|
} = useBimEngine({ locale, theme, onReady, onError });
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
useImperativeHandle(ref, () => ({
|
2026-01-15 14:22:43 +08:00
|
|
|
|
getEngine: () => engine,
|
|
|
|
|
|
loadModel,
|
|
|
|
|
|
goHome,
|
|
|
|
|
|
activateMeasure,
|
|
|
|
|
|
deactivateMeasure,
|
|
|
|
|
|
setTheme,
|
|
|
|
|
|
setLocale
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (isReady && modelUrl) {
|
|
|
|
|
|
loadModel(modelUrl, modelOptions);
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [isReady, modelUrl, modelOptions, loadModel]);
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div
|
|
|
|
|
|
ref={containerRef}
|
|
|
|
|
|
className={className}
|
2026-01-15 16:46:53 +08:00
|
|
|
|
style={{ width: '100%', height: '100%', ...style }}
|
2026-01-15 14:22:43 +08:00
|
|
|
|
/>
|
|
|
|
|
|
);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
BimViewer.displayName = 'BimViewer';
|
|
|
|
|
|
```
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
### 4. 使用组件
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
```jsx
|
|
|
|
|
|
// App.jsx
|
2026-01-15 14:22:43 +08:00
|
|
|
|
import React, { useRef } from 'react';
|
|
|
|
|
|
import { BimViewer } from './components/BimViewer';
|
|
|
|
|
|
|
|
|
|
|
|
function App() {
|
2026-01-15 16:46:53 +08:00
|
|
|
|
const viewerRef = useRef(null);
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
const handleReady = (engine) => console.log('引擎已就绪', engine);
|
|
|
|
|
|
const handleError = (error) => console.error('引擎错误', error);
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div style={{ width: '100vw', height: '100vh', position: 'relative' }}>
|
|
|
|
|
|
<BimViewer
|
|
|
|
|
|
ref={viewerRef}
|
|
|
|
|
|
modelUrl="./model/building"
|
|
|
|
|
|
modelOptions={{ position: [0, 0, 0] }}
|
|
|
|
|
|
locale="zh-CN"
|
|
|
|
|
|
theme="light"
|
|
|
|
|
|
onReady={handleReady}
|
|
|
|
|
|
onError={handleError}
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
|
|
<div style={{ position: 'absolute', top: 20, left: 20, zIndex: 100 }}>
|
2026-01-15 16:46:53 +08:00
|
|
|
|
<button onClick={() => viewerRef.current?.goHome()}>主视角</button>
|
|
|
|
|
|
<button onClick={() => viewerRef.current?.activateMeasure('distance')}>距离测量</button>
|
|
|
|
|
|
<button onClick={() => viewerRef.current?.deactivateMeasure()}>停止测量</button>
|
2026-01-15 14:22:43 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default App;
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
|
|
## API 参考
|
|
|
|
|
|
|
|
|
|
|
|
### BimEngine
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
主引擎类。
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
|
|
|
|
|
#### 构造函数
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
```javascript
|
|
|
|
|
|
import { BimEngine } from '@fishdingding/bim-engine-sdk';
|
|
|
|
|
|
|
|
|
|
|
|
new BimEngine(container, options)
|
2026-01-15 14:22:43 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
| 参数 | 类型 | 说明 |
|
|
|
|
|
|
|------|------|------|
|
|
|
|
|
|
| container | HTMLElement \| string | 容器元素或容器 ID |
|
2026-01-15 16:46:53 +08:00
|
|
|
|
| options.locale | string | 语言:'zh-CN' \| 'en-US',默认 'zh-CN' |
|
|
|
|
|
|
| options.theme | string | 主题:'light' \| 'dark',默认 'light' |
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
|
|
|
|
|
#### 属性
|
|
|
|
|
|
|
|
|
|
|
|
| 属性 | 类型 | 说明 |
|
|
|
|
|
|
|------|------|------|
|
|
|
|
|
|
| engine | EngineManager | 3D 引擎管理器 |
|
|
|
|
|
|
| toolbar | ToolbarManager | 工具栏管理器 |
|
|
|
|
|
|
| dialog | DialogManager | 弹窗管理器 |
|
|
|
|
|
|
| measure | MeasureDialogManager | 测量面板管理器 |
|
|
|
|
|
|
|
|
|
|
|
|
#### 方法
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
| 方法 | 参数 | 说明 |
|
|
|
|
|
|
|------|------|------|
|
|
|
|
|
|
| setLocale(locale) | 'zh-CN' \| 'en-US' | 设置语言 |
|
|
|
|
|
|
| getLocale() | - | 获取当前语言 |
|
|
|
|
|
|
| setTheme(theme) | 'light' \| 'dark' | 设置主题 |
|
|
|
|
|
|
| destroy() | - | 销毁引擎 |
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
|
|
### EngineManager
|
|
|
|
|
|
|
|
|
|
|
|
3D 引擎管理器,通过 `engine.engine` 访问。
|
|
|
|
|
|
|
|
|
|
|
|
#### 方法
|
|
|
|
|
|
|
|
|
|
|
|
| 方法 | 参数 | 返回值 | 说明 |
|
|
|
|
|
|
|------|------|--------|------|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
| initialize(options) | EngineOptions | boolean | 初始化 3D 引擎 |
|
|
|
|
|
|
| isInitialized() | - | boolean | 检查是否已初始化 |
|
|
|
|
|
|
| loadModel(url, options) | string, ModelLoadOptions | void | 加载模型 |
|
|
|
|
|
|
| CameraGoHome() | - | void | 回到主视角 |
|
|
|
|
|
|
| activateMeasure(mode) | MeasureMode | void | 激活测量功能 |
|
|
|
|
|
|
| deactivateMeasure() | - | void | 停用测量功能 |
|
|
|
|
|
|
| getCurrentMeasureType() | - | MeasureMode \| null | 获取当前测量类型 |
|
|
|
|
|
|
| getEngine() | - | any | 获取原始引擎实例 |
|
|
|
|
|
|
| destroy() | - | void | 销毁引擎 |
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
|
|
## 类型定义
|
|
|
|
|
|
|
|
|
|
|
|
### EngineOptions
|
|
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
|
interface EngineOptions {
|
2026-01-15 16:46:53 +08:00
|
|
|
|
version?: 'v1' | 'v2'; // WebGL 版本,默认 'v2'
|
|
|
|
|
|
showStats?: boolean; // 是否显示性能统计,默认 false
|
|
|
|
|
|
showViewCube?: boolean; // 是否显示视图立方体,默认 true
|
2026-01-15 14:22:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### ModelLoadOptions
|
|
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
|
interface ModelLoadOptions {
|
2026-01-15 16:46:53 +08:00
|
|
|
|
position?: [number, number, number]; // 位置,默认 [0, 0, 0]
|
|
|
|
|
|
rotation?: [number, number, number]; // 旋转(弧度),默认 [0, 0, 0]
|
|
|
|
|
|
scale?: [number, number, number]; // 缩放,默认 [1, 1, 1]
|
|
|
|
|
|
id?: string; // 模型 ID(可选)
|
2026-01-15 14:22:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### MeasureMode
|
|
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
|
type MeasureMode =
|
|
|
|
|
|
| 'distance' // 距离测量
|
|
|
|
|
|
| 'minDistance' // 最小距离测量
|
|
|
|
|
|
| 'angle' // 角度测量
|
|
|
|
|
|
| 'elevation' // 标高测量
|
|
|
|
|
|
| 'volume' // 体积测量
|
|
|
|
|
|
| 'laserDistance' // 激光测距
|
|
|
|
|
|
| 'slope' // 坡度测量
|
|
|
|
|
|
| 'spaceVolume'; // 空间体积测量
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
|
|
## 注意事项
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
1. **容器尺寸**:确保容器元素有明确的宽度和高度
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
2. **销毁清理**:页面关闭或组件卸载前务必调用 `destroy()`
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
3. **浏览器兼容**:需要支持 WebGL 的现代浏览器
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
|
|
## 常见问题
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
### Q: 模型加载失败?
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
检查:
|
2026-01-15 14:22:43 +08:00
|
|
|
|
- 模型路径是否正确
|
|
|
|
|
|
- 网络请求是否正常(F12 查看 Network)
|
2026-01-15 16:46:53 +08:00
|
|
|
|
- 模型格式是否支持
|
2026-01-15 14:22:43 +08:00
|
|
|
|
|
|
|
|
|
|
### Q: 如何监听引擎事件?
|
|
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
|
engine.on('modelLoaded', (data) => {
|
|
|
|
|
|
console.log('模型已加载', data);
|
|
|
|
|
|
});
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
|
|
## 技术支持
|
|
|
|
|
|
|
2026-01-15 16:46:53 +08:00
|
|
|
|
- NPM 包地址:https://www.npmjs.com/package/@fishdingding/bim-engine-sdk
|
|
|
|
|
|
- 如有问题,请联系技术支持
|