MFC集成OpenCascade

1、OpenCasCad环境变量设置

在我的电脑的环境变量中增加OpenCasCade的相关配置信息

头文件:CSF_OCCTIncludePath:D:\Program Files\OpenCASCADE-7.3.0-vc14-64\opencascade-7.3.0\inc

Lib文件:CSF_OCCTLibPath:D:\Program Files\OpenCASCADE-7.3.0-vc14-64\opencascade-7.3.0\win64\vc14\lib

bin文件: PATH路径中增加occt的相关路径,特别注意标红的路径

 

 

2、新建MFC应用

打开VS2019->新建项目->MFC应用(没有的话使用左下VS安装程序下载MFC板块)->确定

创建完成之后,一定要先将上面的x86改成x64 ,再进行配置

 

3、配置VS项目

右键项目->属性->VC++目录中设置包含目录,及库目录

 

 

 在连接器中增加所依赖的lib文件

 

 技巧:

在openCasCade的lib路径中,输入cmd,在命令行中输入dir /b >lib.txt,将会把该目录中的所有lib文件名重定向到lib.txt中,然后讲所有的lib列表添加到依赖项中

 

 

4、修改pch.h中,在pch.h中将常用的opencascad里的头文件

复制代码
#include <BRepTools.hxx>  
#include <Standard_DefineHandle.hxx>  
#include <DsgPrs_LengthPresentation.hxx>  
#include <GCPnts_TangentialDeflection.hxx>  
#include <Geom_Axis2Placement.hxx>  
#include <Geom_CartesianPoint.hxx>  
#include <Geom_Line.hxx>  
#include <Geom_Surface.hxx>  
#include <BRepAdaptor_Surface.hxx>  
#include <GeomAbs_CurveType.hxx>  
#include <GeomAdaptor_Curve.hxx>  
#include <GeomTools_Curve2dSet.hxx>  
#include <gp_Vec.hxx>  
#include <Graphic3d_NameOfMaterial.hxx>  
#include <MMgt_TShared.hxx>  
#include <OSD_Environment.hxx>  
#include <Precision.hxx>  
#include <Prs3d_IsoAspect.hxx>  
#include <Prs3d_LineAspect.hxx>  
#include <Prs3d_Projector.hxx>  
#include <Prs3d_Text.hxx>  
#include <Quantity_Factor.hxx>  
#include <Quantity_Length.hxx>  
#include <Quantity_NameOfColor.hxx>  
#include <Quantity_PhysicalQuantity.hxx>  
#include <Quantity_PlaneAngle.hxx>  
#include <Quantity_TypeOfColor.hxx>  
#include <SelectMgr_EntityOwner.hxx>  
#include <SelectMgr_SelectableObject.hxx>  
#include <SelectMgr_Selection.hxx>  
#include <SelectMgr_SelectionManager.hxx>  
#include <SelectMgr_ListOfFilter.hxx>  
#include <SelectMgr_Filter.hxx>  
#include <StdSelect_EdgeFilter.hxx>  
#include <StdSelect_ShapeTypeFilter.hxx>  
#include <Standard_Boolean.hxx>  
#include <Standard_CString.hxx>  
#include <Standard_ErrorHandler.hxx>  
#include <Standard_Integer.hxx>  
#include <Standard_IStream.hxx>  
#include <Standard_Macro.hxx>  
#include <Standard_NotImplemented.hxx>  
#include <Standard_OStream.hxx>  
#include <Standard_Real.hxx>  
#include <StdPrs_Curve.hxx>  
#include <StdPrs_Point.hxx>  
#include <StdPrs_PoleCurve.hxx>  
#include <TCollection_AsciiString.hxx>  
#include <TColgp_Array1OfPnt2d.hxx>  
#include <TColgp_HArray1OfPnt2d.hxx>  
#include <TCollection_AsciiString.hxx>  
#include <TColStd_HSequenceOfTransient.hxx>  
#include <TColStd_MapIteratorOfMapOfTransient.hxx>  
#include <TColStd_MapOfTransient.hxx>  
#include <TopExp_Explorer.hxx>  
#include <TopoDS.hxx>  
#include <TopoDS_Compound.hxx>  
#include <TopoDS_Shape.hxx>  
#include <TopoDS_Solid.hxx>  
#include <TopoDS_Vertex.hxx>  
#include <TopExp.hxx>  
#include <TopTools_HSequenceOfShape.hxx>  
#include <UnitsAPI.hxx>  
#include <V3d_View.hxx>  
#include <V3d_Viewer.hxx>  
#include <WNT_Window.hxx>  
#include <Prs3d_PointAspect.hxx>  
#include <AIS_Point.hxx>  
#include <AIS_InteractiveContext.hxx>
#include <AIS_Shape.hxx>
#include <BRep_Tool.hxx>  
#include <BRepAlgoAPI_Fuse.hxx>  
#include <BRepBuilderAPI_MakeEdge.hxx>  
#include <BRepBuilderAPI_MakeFace.hxx>  
#include <BRepBuilderAPI_MakeWire.hxx>  
#include <BRepBuilderAPI_MakeVertex.hxx>  
#include <BRepBuilderAPI_Transform.hxx>  
#include <BRepPrimAPI_MakeCone.hxx>  
#include <BRepPrimAPI_MakeRevol.hxx>  
#include <BRepFilletAPI_MakeFillet.hxx>  
#include <BRepBuilderAPI_Copy.hxx>  
#include <BRepBuilderAPI_MakePolygon.hxx>  
#include <BRepLib.hxx>  
#include <BRepOffsetAPI_MakeThickSolid.hxx>  
#include <BRepOffsetAPI_ThruSections.hxx>  
#include <BRepPrimAPI_MakeCylinder.hxx>  
#include <BRepPrimAPI_MakePrism.hxx>  
#include <BRepPrimAPI_MakeTorus.hxx>  
#include <BRepAlgoAPI_Section.hxx>  
#include <BRepPrimAPI_MakeSphere.hxx>  
#include <BRepFeat_SplitShape.hxx>  
#include <TColgp_HArray1OfPnt.hxx>  
#include <GeomAPI_Interpolate.hxx>  
#include <GC_MakeArcOfCircle.hxx>  
#include <GC_MakeSegment.hxx>  
#include <GC_MakeCircle.hxx>  
#include <GCE2d_MakeSegment.hxx>  
#include <gp.hxx>  
#include <gp_Ax1.hxx>  
#include <gp_Ax2.hxx>  
#include <gp_Ax2d.hxx>  
#include <gp_Dir.hxx>  
#include <gp_Dir2d.hxx>  
#include <gp_Pnt.hxx>  
#include <gp_Pnt2d.hxx>  
#include <gp_Trsf.hxx>  
#include <gp_Vec.hxx>  
#include <Geom_CylindricalSurface.hxx>  
#include <Geom_Plane.hxx>  
#include <Geom_Surface.hxx>  
#include <Geom_TrimmedCurve.hxx>  
#include <Geom2d_Ellipse.hxx>  
#include <Geom2d_TrimmedCurve.hxx>  
#include <TopExp_Explorer.hxx>  
#include <TopoDS.hxx>  
#include <TopoDS_Edge.hxx>  
#include <TopoDS_Face.hxx>  
#include <TopoDS_Wire.hxx>  
#include <TopoDS_Shape.hxx>  
#include <TopoDS_Compound.hxx>  
#include <GCPnts_AbscissaPoint.hxx>  
#include <BRepAdaptor_Curve.hxx>  
#include <GeomLib.hxx>  
#include <GeomConvert_CompCurveToBSplineCurve.hxx>  
#include <TopTools_ListOfShape.hxx>  
#include <TopTools_ListIteratorOfListOfShape.hxx>  
#include <TopTools_DataMapOfShapeInteger.hxx>  
#include <TopTools_DataMapOfShapeReal.hxx>  
#include <TopTools_IndexedDataMapOfShapeAddress.hxx>  
#include <V3d_PositionalLight.hxx>  
#include <V3d_DirectionalLight.hxx>  
#include <V3d_AmbientLight.hxx>  
#include <IGESControl_Controller.hxx>  
#include <IGESControl_Writer.hxx>  
#include <Interface_Static.hxx>  
#include <OpenGl_GraphicDriver.hxx>  
#include <Graphic3d_GraphicDriver.hxx>
复制代码

 

5、修改项目中的各个类

xxxApp.h

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

添加包含的头文件

#include <Standard_Macro.hxx>
#include <Graphic3d_GraphicDriver.hxx>

 

在相应类的构造函数中添加

复制代码
    try
    {
        Handle(Aspect_DisplayConnection) aDisplayConnection;
        m_GraphicDriver = new OpenGl_GraphicDriver(aDisplayConnection);
    }
    catch(Standard_Failure)
    {
        AfxMessageBox(_T("Error ocured in Initializing the Opencascad graphic variable."));
    }
复制代码

 

 

注意如果new报错(因为该文件开头对new做了重定义),有下面两种方法,一是把整个#define Debug注释掉

另一种是仅把define new DEBUG_NEW注释掉

 

 

 

 

xxxdoc类的修改

xxxdoc.h

    Handle(AIS_InteractiveContext) m_AISContext;
    Handle(V3d_Viewer) m_Viewer;

 

 

 xxxdoc.cpp 在构造函数中添加

    Handle(Graphic3d_GraphicDriver) theGraphicDriver = ((COCCDemoApp*)AfxGetApp())->GetGraphicDriver();
    m_Viewer = new V3d_Viewer(theGraphicDriver);
    m_Viewer->SetDefaultLights();
    m_Viewer->SetLightOn();

    m_AISContext = new AIS_InteractiveContext(m_Viewer);
    m_AISContext->SetDisplayMode(AIS_Shaded, true);
    m_AISContext->SetAutomaticHilight(Standard_False);

 

 

 

xxxview类的修改

xxxview.h

Handle(V3d_View) m_View;
void FitAll();
virtual void OnInitialUpdate();

 

 

xxxview.cpp中的添加

复制代码
void COCCDemoView::FitAll()
{
    // TODO: 在此处添加实现代码.
    if (!m_View.IsNull())
    {
        m_View->FitAll();
        m_View->ZFitAll();
    }
}
复制代码
复制代码
void COCCDemoView::OnInitialUpdate()
{
    CView::OnInitialUpdate();

    // TODO: 在此添加专用代码和/或调用基类
    m_View = GetDocument()->GetViewer()->CreateView();
    m_View->SetShadingModel(V3d_GOURAUD);

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

    Aspect_Handle aWindowHandle = (Aspect_Handle)GetSafeHwnd();
    Handle(WNT_Window) aWntWindow = new WNT_Window(GetSafeHwnd());
    m_View->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);
    m_View->FitAll();

    m_View->ZBufferTriedronSetup(Quantity_NOC_RED, Quantity_NOC_GREEN, Quantity_NOC_BLUE1, 0.8, 0.05, 12);
    m_View->TriedronDisplay(Aspect_TOTP_LEFT_LOWER, Quantity_NOC_WHITE, 0.2, V3d_ZBUFFER);
}
复制代码

 

 在绘制函数中OnDraw中添加

    if (m_View)
    {
        m_View->MustBeResized();
        m_View->Update();
    }

 

 

6、运行效果

 

posted @   unicornsir  阅读(279)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示