#include "Render.h" #include "Aspect_TypeOfLine.hxx" #include "Font_FontAspect.hxx" #include "STEPControl_Reader.hxx" #ifndef NATIVE_TAG #define NATIVE_TAG "Render" #endif static std::mutex renderMutex; namespace NativeNXEA { Render::Render(int w, int h): width(0), height(0), v3dTri(new V3dTri), v3dTriCube(new V3dTriCube), v3dCamera(new V3dCamera), v3dCtx(new V3dCtx), v3dOgd(new V3dOGD), v3dView(new V3dView), v3dDrawer(new V3dDrawer), v3dViewer(new V3dViewer), v3dWin(new V3dWin), v3dViewContr(new V3dViewController) { width = w; height = h; } Render::~Render() { shapes_.clear(); } bool Render::init(EGLDisplay eglDisp,EGLContext ctx,EGLConfig eglConfig) { //初始化OpenGL if (!v3dOgd->InitV3dOGD(eglDisp,ctx,eglConfig)) { HILOG_ERROR(NATIVE_TAG, "Init GraphicDriver Fail!"); return false; } //初始化视口管理 if (!v3dViewer->InitV3dViewer(v3dOgd->graphicDriver)) { HILOG_ERROR(NATIVE_TAG, "Init Viewer Fail!"); return false; } //初始化OCCT内部上下文 if (!v3dCtx->InitV3dCtx(v3dViewer->viewer)) { HILOG_ERROR(NATIVE_TAG, "Init Ctx Fail!"); return false; }else{ //初始化OCCT上下文的Drawer(全局属性设置) v3dDrawer->InitV3dAllAspect(v3dCtx->ctx); } //初始化绑定Window if (!v3dWin->InitV3dWin(width, height)) { HILOG_ERROR(NATIVE_TAG, "Init Window Fail!"); return false; } //初始化视图 if (!v3dView->InitV3dView(v3dViewer->viewer,ctx,v3dWin->win)) { HILOG_ERROR(NATIVE_TAG, "Init View Fail!"); return false; } else { v3dView->InitViewOption(); } if (!v3dCamera->InitV3dCamera(v3dView->view)) { HILOG_ERROR(NATIVE_TAG, "Init Camera Fail!"); return false; } //初始化世界坐标系 if (!v3dTri->InitV3dTri(v3dCtx->ctx)) { HILOG_ERROR(NATIVE_TAG, "Init AXIS Fail!"); return false; } //初始化正方体视角切换指示器 if (!v3dTriCube->InitV3dTriCube(v3dCtx->ctx)) { HILOG_ERROR(NATIVE_TAG, "Init AXIS Cuba Fail!"); return false; } //InitDevText(); v3dView->ResetView(); return true; } bool Render::loadModel(const std::string &filePath) { // 清除现有模型 for (auto &shape : shapes_) { v3dCtx->ctx->Remove(shape, false); } shapes_.clear(); // 加载STEP文件 STEPControl_Reader reader; IFSelect_ReturnStatus status = reader.ReadFile(filePath.c_str()); if (status != IFSelect_RetDone) { printf("Error: Failed to read STEP file: %s\n", filePath.c_str()); return false; } // 将文件内容转换为OCCT形状 reader.TransferRoots(); int numShapes = reader.NbShapes(); if (numShapes <= 0) { printf("Error: No shapes found in STEP file\n"); return false; } // 加载所有形状 for (int i = 1; i <= numShapes; i++) { TopoDS_Shape shape = reader.Shape(i); Bnd_Box bbox; BRepBndLib::Add(shape, bbox); double xmin, ymin, zmin, xmax, ymax, zmax; bbox.Get(xmin, ymin, zmin, xmax, ymax, zmax); gp_Pnt center((xmin + xmax) / 2.0, (ymin + ymax) / 2.0, (zmin + zmax) / 2.0); gp_Vec translation(-center.X(), -center.Y(), -center.Z()); gp_Trsf move; move.SetTranslation(translation); BRepBuilderAPI_Transform transformer(shape, move); shape = transformer.Shape(); if (!shape.IsNull()) { Handle(AIS_Shape) aisShape = new AIS_Shape(shape); // 设置材质 Handle(Prs3d_Drawer) drawer = aisShape->Attributes(); drawer->SetFaceBoundaryDraw(true); drawer->SetWireAspect(v3dDrawer->v3d_LineAspect); drawer->SetShadingAspect(v3dDrawer->v3d_ShadingAspect); v3dCtx->ctx->Display(aisShape, true); shapes_.push_back(aisShape); } } v3dView->view->ZFitAll(); v3dView->ResetView(); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "LoadModel", "Successfully loaded STEP file with %{public}d shapes", numShapes); return true; } // setTranslation void Render::SetMoveTo(float tx, float ty) { v3dCamera->SetMoveTo(tx, ty); } void Render::render() { if (v3dView->view.IsNull()) { OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Render", "View Is Null"); return; } v3dView->MustBeResized(); v3dView->Redraw(); } void Render::resize(int w, int h) { HILOG_ERROR(NATIVE_TAG, "InPut Size:%{public}d,%{public}d", w, h); v3dWin->Resize(w, h); } void Render::setRotation(float xAngle, float yAngle) { v3dCamera->SetRotation(xAngle, yAngle); } void Render::setZoomLevel(float factor) { v3dTri->SetZoomLevel(std::max(0.1f, std::min(factor, 5.0f))); // 限制缩放范围 } void Render::setClearColor(float r, float g, float b, float a) { v3dView->SetClearColor(r, g, b, a); } void Render::resetView() { v3dView->ResetView(); } void Render::SwitchView(std::string str) { v3dCamera->SwitchView(str); } void Render::InitDevText(){ Handle(AIS_TextLabel) aTextLabel = new AIS_TextLabel(); const char16_t chinese_array[] = u"新时代社会主义中国接班人"; aTextLabel->SetText(chinese_array); gp_Pnt position(0-(width/3), 0-(height/3), 0.0); // 例如,在原点 aTextLabel->SetPosition(position); v3dCtx->ctx->Display(aTextLabel, Standard_True); HILOG_ERROR(NATIVE_TAG, "aTextLabel字体名字:%{public}s", aTextLabel->FontName().ToCString()); HILOG_ERROR(NATIVE_TAG, "aTextLabel字体名字:%{public}s", aTextLabel->Attributes()->TextAspect()->Aspect()->Font().ToCString()); } } // namespace NativeOpenCAX