110 lines
4.0 KiB
Plaintext
110 lines
4.0 KiB
Plaintext
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='';
|
||
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 }) {
|
||
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()=>{
|
||
//NAPI日志接口测试
|
||
let OCCTRender = await import("libOCCTRender.so")
|
||
hilog.info(DOMAIN, 'testTag', 'Test NAPI 2 + 3 = %{public}d', OCCTRender.add(2, 3));
|
||
})
|
||
// 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%');
|
||
}
|
||
} |