OpenCAX/entry/src/main/ets/pages/modelView.ets
2026-02-21 00:28:54 +08:00

120 lines
4.4 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { hilog } from '@kit.PerformanceAnalysisKit';
import fs from '@ohos.file.fs';
import fileIO from '@ohos.fileio';
import { Context } from '@kit.AbilityKit';
//import OCCTRender from 'libocctrender.so';
const DOMAIN = 0x0000;
@Component
export struct ModelView {
private displayController: XComponentController = new XComponentController();
private displayContrId: string = 'OCCTRenderer';
@State modelPath: string = '';
@State modelName:string='model.step';
@State nativeWindow:string='';
@State loadStatus: string = '未测试';
aboutToAppear() {
this.copyRawFileToSandbox();
}
async copyRawFileToSandbox() {
try {
const context = getContext(this);
this.modelPath = `${context.filesDir}/${this.modelName}`;
const arrayBuffer:Uint8Array = await context.resourceManager.getRawFileContent(this.modelName);
const buffer = arrayBuffer.buffer;
console.log('Raw file size:', arrayBuffer.byteLength);
if (fs.accessSync(this.modelPath)) {
fs.unlinkSync(this.modelPath);
}
const fd = fileIO.openSync(this.modelPath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE,0o666);
const bytesWritten = fileIO.writeSync(fd, buffer);
console.log('Bytes written:', bytesWritten);
fileIO.closeSync(fd);
console.log('SanBox File:', arrayBuffer.byteLength);
console.log('WriteModelPath:', this.modelPath);
} catch (err) {
let msg = 'Unknown error';
if (err instanceof Error) {
msg = err.message;
} else if (typeof err === 'string') {
msg = err;
}
console.error(`Copy failed: ${msg}`);
throw new Error(`Failed to copy ${this.modelName} to sandbox: ${msg}`);
}
}
build() {
Flex({ direction: FlexDirection.Column }) {
Row(){
Button('加载模型').onClick(()=>{
try {
// 调用 native 初始化渲染器
console.log('displayContrId:', this.displayContrId);
console.log('NativeWindow:', this.nativeWindow);
console.log('ModelPath:', this.modelPath);
hilog.info(0x0000, 'ModelView', 'Load Model');
// 复制模型文件并获取路径
console.info('Model copied to:', this.modelPath);
// 调用 native 加载
//OCCTRender.loadModel(this.displayContrId, this.modelPath);
} catch (e) {
hilog.error(0x0000, 'ModelView', `LoadModel Failed: ${JSON.stringify(e)}`);
}
})
Button('测试库加载').onClick(async()=>{
try {
let OCCTRender = await import("libocctrender.so")
console.info(`[NDK] 模块类型: ${typeof OCCTRender}`);
console.info(`[NDK] 模块值: ${JSON.stringify(OCCTRender)}`);
console.info(`[NDK] 所有属性: ${Object.keys(OCCTRender).join(', ')}`);
if (!OCCTRender) throw new Error("模块为 undefined");
} catch (e) {
console.error(`[NDK] 加载失败: ${e.message}`, e);
// 此处会触发你看到的错误
}
})
}
XComponent({
id: this.displayContrId,
type: 'surface',
controller: this.displayController
})
// .onLoad(() => {
// // 获取 native window必须在 onLoad 后才能获取)
// this.nativeWindow = this.displayController.getXComponentSurfaceId();
// if (this.nativeWindow === undefined || this.nativeWindow === '') {
// hilog.error(0x0000, 'ModelView', 'Failed to get native window');
// return;
// }
// // 获取 XComponent 尺寸
// let width = 0;
// let height = 0;
// try {
// OCCTRender.initRenderer(this.displayContrId, this.nativeWindow, { width: 800, height: 600 });
// console.info('Init Render Good');
// }catch(e){
// console.info('Init Render Faile');
// }
// })
// .onDestroy(() => {
// // 销毁时清理资源
// OCCTRender.destroyRenderer(this.displayContrId);
// })
// .onTouch((event) => {
// // 简单鼠标/触摸拖拽旋转
// if (event.type === TouchType.Move) {
// const dx = event.tiltX;
// const dy = event.tiltY;
// //occt.onMouseEvent(this.xcomponentId, dx, dy);
// }
// })
.width('100%')
.height('100%')
.backgroundColor('#333333');
}
.width('100%')
.height('100%');
}
}