如何在自己的项目中使用OpenCascade7.3 (VS2017)

项目初始设置

在VS2017中创建一个单文档MFC项目(本例以mOCCT为名称)
在项目属性的VC++页面设置包含目录、库目录,在链接器的输入中添加OCC库目录下的所有.lib文件名称

添加头文件

在stdafx.h中添加:

#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include 
#include 
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   

添加代码

MFC文档分成三个结构:

  • Application
  • Doc
  • View
    在Application中mOCCT.h中的类中加入代码:
Handle(Graphic3d_GraphicDriver) m_GraphicDriver;

在对应的类的实现文件mOCCT.cpp的类的构造函数中添加:

try {
		Handle(Aspect_DisplayConnection) aDisplayConnection;
		m_GraphicDriver = new OpenGl_GraphicDriver(aDisplayConnection);
	}
	catch (Standard_Failure) {
		AfxMessageBox(L"(Error Ocured in Initializing the Opencascade graphic variable.)");
	}

接着在头文件mOCCT.h中添加一个获取m_GraphicDriver的方法:

 Handle(Graphic3d_GraphicDriver) GetGraphicDriver(){return m_GraphicDriver;}  

接着处理Document Class在mOCCTDoc.h中定义的类中添加:

Handle(AIS_InteractiveContext) myAISContext;  
Handle(V3d_Viewer) myViewer;  
Handle(V3d_Viewer) GetViewer(void) {return myViewer;}

接着去实现文档mOCCTDoc.cpp中类的构造器中添加:

	Handle(Graphic3d_GraphicDriver) theGraphicDriver = ((CmOCCTApp*)AfxGetApp())->GetGraphicDriver();

	myViewer = new V3d_Viewer(theGraphicDriver);
	myViewer->SetDefaultLights();
	myViewer->SetLightOn();

	myAISContext = new AIS_InteractiveContext(myViewer);

	myAISContext->SetDisplayMode(AIS_Shaded,true);
	myAISContext->SetAutomaticHilight(Standard_False);

去mOCCTView.h中在定义的CmOCCTView Class中添加:

 Handle(V3d_View) myView;  

并重写OnInitialUpdate()函数:

 virtual void OnInitialUpdate();  

在对应的实现文件中实现该函数:

void CmOCCTView::OnInitialUpdate() {
	myView = GetDocument()->GetViewer()->CreateView();
	myView->SetShadingModel(V3d_GOURAUD);
	Handle(Graphic3d_GraphicDriver) theGraphicDriver = ((CmOCCTApp*)AfxGetApp())->GetGraphicDriver();
	Aspect_Handle aWindowHandle = (Aspect_Handle)GetSafeHwnd();
	Handle(WNT_Window) aWntWindow = new WNT_Window(GetSafeHwnd());
	myView->SetWindow(aWntWindow);
	if (!aWntWindow->IsMapped()) {
		aWntWindow->Map();
	}
	Standard_Integer w = 100;
	Standard_Integer h = 100;
	aWntWindow->Size(w, h);
	::PostMessage(GetSafeHwnd(), WM_SIZE, SIZE_RESTORED, w + h * 65536);
	myView->FitAll();

	myView->ZBufferTriedronSetup(Quantity_NOC_RED, Quantity_NOC_GREEN, Quantity_NOC_BLUE1, 0.8, 0.05, 12);
	myView->TriedronDisplay(Aspect_TOTP_LEFT_LOWER, Quantity_NOC_WHITE, 0.2, V3d_ZBUFFER);
}

在下方的OnDraw中加入:

 myView->MustBeResized();  
 myView->Update();  

编译生成即可看到界面:
如何在自己的项目中使用OpenCascade7.3 (VS2017)_第1张图片
注意:

  1. 在添加代码时会在new关键字处报错
    将当前文件下的
#ifdef _DEBUG
#define new DEBUG_NEW
#endif`

删去即可
2. 如果编译提醒缺少dll,请将opencascade的win64/vc14/bin目录以及所有第三方库的bin目录添加到系统环境变量Path,如此可以搜索到相应dll文件。或者将dll文件复制到exe文件目录下。

绘图

在mOCCTDoc.cpp和mOCCTDoc.h中写入DrawSphere代码:(需要在mOCCTDoc.cpp中include 头文件 #include “mOCCTView.h”)

//mOCCTDoc.cpp:
void CmOCCTDoc::DrawSphere(double Radius) {
	BRepPrimAPI_MakeSphere mkSphere(Radius);
	TopoDS_Shape Sphere = mkSphere.Shape();
	Handle(AIS_Shape) myAISSphere = new AIS_Shape(Sphere);
	myAISContext->Display(myAISSphere, Standard_False);
	CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
	CMDIChildWnd *pChild = (CMDIChildWnd *)pFrame->GetActiveFrame();
	CmOCCTView *pView = (CmOCCTView *)pChild->GetActiveView();
	pView->FitAll();
}

//mOCCTDoc.h:
	void CmOCCTDoc::DrawSphere(double Radius);

在mOCCTView.h中加入一个类方法:

	void FitAll() { if (!myView.IsNull()) myView->FitAll();  myView->ZFitAll(); };

接着在CmOCCTView::OnDraw中调用绘球函数:

	pDoc->DrawSphere(6);

编译生成即可

如何在自己的项目中使用OpenCascade7.3 (VS2017)_第2张图片

鼠标拖拽

在 mOCCTView.cpp及mOCCTView.h中添加OnMouseMove函数:

//mOCCTView.h
protected:
		afx_msg void OnMouseMove(UINT nFlags, CPoint point);


// mOCCTView.cpp
BEGIN_MESSAGE_MAP(CmOCCTView, CView)
	...
	ON_WM_MOUSEMOVE()
	...
END_MESSAGE_MAP()

...

void CmOCCTView::OnMouseMove(UINT nFlags, CPoint point)  
 {  
     // TODO: Add your message handler code here and/or call default  
     CView::OnMouseMove(nFlags, point);  
     if(nFlags && MK_LBUTTON){  
            myView->Rotate(point.x,point.y);  
            myView->Rotation(point.x,point.y);  
     }  
 } 

如此可实现鼠标左键的拖拽旋转

参考资料:http://milindasf.blogspot.com/2014/03/how-to-build-simple-opencascade-program.html

你可能感兴趣的:(OpenCascade)