97 lines
3.2 KiB
Plaintext
97 lines
3.2 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;
|
|
const TAG = 'ModelView';
|
|
|
|
@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 loadNativeLibrary() {
|
|
try {
|
|
let NaviteOCCT = await import("libopencax.so")
|
|
console.info(`[NDK] 模块类型: ${typeof NaviteOCCT}`);
|
|
console.info(`[NDK] 模块值: ${JSON.stringify(NaviteOCCT)}`);
|
|
console.info(`[NDK] 所有属性: ${Object.keys(NaviteOCCT).join(', ')}`);
|
|
if (!NaviteOCCT) throw new Error("模块为 undefined");
|
|
} catch (e) {
|
|
console.error(`[NDK] 加载失败: ${e.message}`, e);
|
|
// 此处会触发你看到的错误
|
|
}
|
|
}
|
|
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 {
|
|
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()=>{
|
|
this.loadNativeLibrary();
|
|
})
|
|
Button('测试so是否存在').onClick(()=>{
|
|
try {
|
|
fs.accessSync('libs/x86_64/libopencax.so');
|
|
console.info("动态库存在!");
|
|
} catch (e) {
|
|
console.error("动态库不存在!");
|
|
}
|
|
})
|
|
}
|
|
XComponent({
|
|
id: this.displayContrId,
|
|
type: 'surface',
|
|
controller: this.displayController
|
|
})
|
|
.width('100%')
|
|
.height('100%')
|
|
.backgroundColor('#333333');
|
|
}
|
|
.width('100%')
|
|
.height('100%');
|
|
}
|
|
} |