1. OCC库的获取
- 从github上获取 git@github.com:tpaviot/oce.git,自己编译
- 官网获取二进制包(获取下来的只有release 版本的,而且VS版本不一定适合自己)
- 官网源码,然后自己编译(稍微折腾点,建议按步骤1的方法,github上整理好的,用CMake直接编译,很方便)
- 本人编译好的,vs2013的win32及x64版本
occ-vs2013-x64-debug-release
occ-vs2013-win32-debug-release
2. MFC对话框程序,使用OCC显示BOX
- 头文件OCC相关代码
#include "V3d_Viewer.hxx"
#include "AIS_InteractiveContext.hxx"// COccDialogDemoDlg dialog
class COccDialogDemoDlg : public CDialogEx
{//其余代码,略//...
private:Handle(V3d_Viewer) m_pOccViewer;Handle(V3d_View) m_pOccView;Handle(AIS_InteractiveContext) m_pOccAISContext;
}
- Cpp文件OCC相关代码
BOOL COccDialogDemoDlg::OnInitDialog()
{CDialogEx::OnInitDialog();//其他代码,略//....try{//使用OpenGL来显示Handle(Aspect_DisplayConnection) aDisplayConnection;Handle(OpenGl_GraphicDriver) aGraphicDriver = new OpenGl_GraphicDriver(aDisplayConnection);//创建3D视图器TCollection_ExtendedString myViewerName("MyOccViewer");m_pOccViewer = new V3d_Viewer(aGraphicDriver, myViewerName.ToExtString());// set parameters for V3d_Viewer// defines default lights -// positional-light 0.3 0.0 0.0// directional-light V3d_XnegYposZpos// directional-light V3d_XnegYneg// ambient-light//设置默认灯光并开启灯光m_pOccViewer->SetDefaultLights();// activates all the lights defined in this viewerm_pOccViewer->SetLightOn();// set background color to blackm_pOccViewer->SetDefaultBackgroundColor(Quantity_NOC_BLACK);//创建3d视图Handle(WNT_Window) aWNTWindow = new WNT_Window(GetSafeHwnd());m_pOccView = m_pOccViewer->CreateView();m_pOccView->SetWindow(aWNTWindow);//创建交互上下文m_pOccAISContext = new AIS_InteractiveContext(m_pOccViewer);//显示一个boxTopoDS_Shape aShape = BRepPrimAPI_MakeBox(100, 200, 300);Handle(AIS_Shape) anAISShape = new AIS_Shape(aShape);m_pOccAISContext->Display(anAISShape);}catch (Standard_Failure const& anException){Standard_SStream aSStream;aSStream << "An exception was caught: " << anException.GetMessageString() << ends;CString aMsg(aSStream.str().c_str());AfxMessageBox(aMsg);}catch (...){AfxMessageBox(_T("unkonwn exception"));}return TRUE; // return TRUE unless you set the focus to a control
}void COccDialogDemoDlg::OnPaint()
{if (m_pOccView){m_pOccView->Redraw();}
}
- 依赖的occ库
TKPrimd.lib
TKV3dd.lib
TKerneld.lib
TKBRepd.lib
TKMathd.lib
TKOpenGld.lib
TKTopAlgod.lib
TKServiced.lib
3. 运行截图
4. 额外说明
因为OCC使用OpenGL显示用到了着色器,根据OCC抛出异常提示可知,需要指示OCC程序找到着色器相关文件,最简单的是设置环境变量CSF_ShadersDirectory或CASROOT,详细可参看代码,如下部分截图。
要成功运行以上程序,必须设置好任意一个环境变量,本地测试可以直接修改系统变量,本人的做法是用个批处理文件启动vs,在启动前设置好CSF_ShadersDirectory,后续通过vs运行的程序,就自动继承了次环境变量了,如下:
@echo off
set CSF_ShadersDirectory=E:\OCCTest\Third-Party\OCE-0.18\share\oce\src\Shaders
"D:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe"
其中OCE-0.18\share\oce\src\Shaders为occ开发库中的目录