1. 实例的编译运行

新建工程时,可以直接通过cmake生成,但如果工程没有编译完全或者安装成功的话,在安装目录没法找到GUISupport/MFC/VTKMFCSettings.cmake,从而无法正常新建工程。这里主要讲解手动新建工程的过程,以方便大家的学习

和进一步理解。

a.新建工程,然后将vtkSDI文件夹直接覆盖工程里面的文件,res文件可以不复制。

b.设置头文件

VTK与MFC联合编程实现的单文档实例vtkSDI详解

b.添加lib路径和文件

VTK与MFC联合编程实现的单文档实例vtkSDI详解

c.修改property里面的general选项里的character set的设置。

VTK与MFC联合编程实现的单文档实例vtkSDI详解

d.编译运行

VTK与MFC联合编程实现的单文档实例vtkSDI详解

e.选择vtkdata中的vtk文件,然后进行查看处理

VTK与MFC联合编程实现的单文档实例vtkSDI详解

 

VTK与MFC联合编程实现的单文档实例vtkSDI详解

值得注意的是,要运用vtk和MFC联合编程,编译VTK源代码的时候一定要选择vtk gui support里的MFC.

 

2.实例源码详解

学过MFC的基本都知道,MFC中view类主要处理显示视图,doc类处理文档,mainframe主要为整个窗口的和工程的设置管理。由此,VTK与MFC联合编程时,需要主要的是数据操作,以及显示要很好的与MFC中的结构结合,做到MVC分离的要求和规范,视图-模型和控制一定要处理开来,尤其是大工程的处理时,才能不混乱。

 

a.view类设置

头文件

 

 
  1. // vtkSDIView.h : interface of the CvtkSDIView class

  2. //

  3.  
  4.  
  5. #pragma once

  6.  
 
  1. //添加MFC显示窗口的头文件

  2. #include "vtkMFCWindow.h"

  3.  
  4. class CvtkSDIView : public CView

  5. {

  6. public:

  7. virtual ~CvtkSDIView();

  8. #ifdef _DEBUG

  9. virtual void AssertValid() const;

  10. virtual void Dump(CDumpContext& dc) const;

  11. #endif

  12.  
  13. CvtkSDIDoc* GetDocument() const;

 
  1. //获得vtkRender的指针

  2. vtkRenderer* GetRenderer() { ASSERT(pvtkRenderer); return pvtkRenderer; }

  3.  
  4. virtual void OnDraw(CDC* pDC); // overridden to draw this view

  5.  
  6. afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);

  7. afx_msg void OnDestroy();

  8. afx_msg BOOL OnEraseBkgnd(CDC* pDC);

  9. afx_msg void OnSize(UINT nType, int cx, int cy);

  10.  
  11. private:

 
  1. //显示的变量,vtkrender和vtkMFCWindow

  2. vtkRenderer *pvtkRenderer;

  3. vtkMFCWindow *pvtkMFCWindow;

  4.  
  5. protected:

  6. DECLARE_DYNCREATE(CvtkSDIView)

  7. CvtkSDIView();

  8.  
  9. virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);

  10. virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);

  11. virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);

  12.  
  13. DECLARE_MESSAGE_MAP()

  14. public:

  15. virtual void OnInitialUpdate();

  16. };

  17.  
  18. #ifndef _DEBUG // debug version in vtkSDIView.cpp

  19. inline CvtkSDIDoc* CvtkSDIView::GetDocument() const

  20. { return reinterpret_cast<CvtkSDIDoc*>(m_pDocument); }

  21. #endif

 

实现文件

 

 
  1. // vtkSDIView.cpp : implementation of the CvtkSDIView class

  2. //

  3.  
  4. #include "stdafx.h"

  5. #include "vtkSDI.h"

  6.  
  7. #include "vtkSDIDoc.h"

  8. #include "vtkSDIView.h"

  9.  
  10. #include "vtkCallbackCommand.h"

  11.  
  12. #ifdef _DEBUG

  13. #define new DEBUG_NEW

  14. #endif

  15.  
  16.  
  17. // CvtkSDIView

  18.  
  19. IMPLEMENT_DYNCREATE(CvtkSDIView, CView)

  20.  
  21. BEGIN_MESSAGE_MAP(CvtkSDIView, CView)

  22. // Standard printing commands

  23. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)

  24. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)

  25. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)

  26. ON_WM_CREATE()

  27. ON_WM_DESTROY()

  28. ON_WM_ERASEBKGND()

  29. ON_WM_SIZE()

  30. END_MESSAGE_MAP()

  31.  
  32. // CvtkSDIView construction/destruction

  33.  
  34. CvtkSDIView::CvtkSDIView()

  35. {

  36. this->pvtkMFCWindow = NULL;

  37.  
  38. //新建视图类对象

  39. this->pvtkRenderer = vtkRenderer::New();

  40. }

  41.  
  42. CvtkSDIView::~CvtkSDIView()

  43. {

  44. // delete generic vtk window

  45. if (this->pvtkMFCWindow) delete this->pvtkMFCWindow;

  46. }

  47.  
  48. void CvtkSDIView::OnDraw(CDC* pDC)

  49. {

  50. CvtkSDIDoc* pDoc = GetDocument();

  51. ASSERT_VALID(pDoc);

  52.  
  53. if (this->pvtkMFCWindow)

  54. {

  55. if (pDC->IsPrinting())

  56. this->pvtkMFCWindow->DrawDC(pDC);

  57. }

  58. }

  59.  
  60.  
  61. // CvtkSDIView printing

  62.  
  63. BOOL CvtkSDIView::OnPreparePrinting(CPrintInfo* pInfo)

  64. {

  65. // default preparation

  66. return DoPreparePrinting(pInfo);

  67. }

  68.  
  69. void CvtkSDIView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)

  70. {

  71. // TODO: add extra initialization before printing

  72. }

  73.  
  74. void CvtkSDIView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)

  75. {

  76. // TODO: add cleanup after printing

  77. }

  78.  
  79.  
  80. // CvtkSDIView diagnostics

  81.  
  82. #ifdef _DEBUG

  83. void CvtkSDIView::AssertValid() const

  84. {

  85. CView::AssertValid();

  86. }

  87.  
  88. void CvtkSDIView::Dump(CDumpContext& dc) const

  89. {

  90. CView::Dump(dc);

  91. }

  92.  
  93. CvtkSDIDoc* CvtkSDIView::GetDocument() const // non-debug version is inline

  94. {

  95. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CvtkSDIDoc)));

  96. return (CvtkSDIDoc*)m_pDocument;

  97. }

  98. #endif //_DEBUG

  99.  
  100.  
  101. // CvtkSDIView message handlers

  102.  
  103. int CvtkSDIView::OnCreate(LPCREATESTRUCT lpCreateStruct)

  104. {

  105. if (CView::OnCreate(lpCreateStruct) == -1)

  106. return -1;

  107.  
  108. return 0;

  109. }

  110.  
  111. void CvtkSDIView::OnDestroy()

  112. {

  113. // Delete the the renderer, window and interactor objects.

  114. if (this->pvtkRenderer) this->pvtkRenderer->Delete();

  115.  
  116. // destroy base

  117. CView::OnDestroy();

  118. }

  119.  
  120. BOOL CvtkSDIView::OnEraseBkgnd(CDC* pDC)

  121. {

  122. return TRUE;

  123. }

  124.  
  125. void CvtkSDIView::OnSize(UINT nType, int cx, int cy)

  126. {

  127. CView::OnSize(nType, cx, cy);

  128.  
  129. if (this->pvtkMFCWindow)

  130. this->pvtkMFCWindow->MoveWindow(0, 0, cx, cy);

  131. }

  132.  
  133. static void handle_double_click(vtkObject* obj, unsigned long,

  134. void*, void*)

  135. {

 
  1. //交互响应回调函数,输出提示信息

  2. vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::SafeDownCast(obj);

  3. if(iren && iren->GetRepeatCount())

  4. {

  5. AfxMessageBox("Double Click");

  6. }

  7. }

  8.  
  9. void CvtkSDIView::OnInitialUpdate()

  10. {

  11. CView::OnInitialUpdate();

  12.  
 
  1. //初始化的时候创建vtkMFCwindow对象

  2. if (this->pvtkMFCWindow) delete this->pvtkMFCWindow;

  3. this->pvtkMFCWindow = new vtkMFCWindow(this);

  4.  
 
  1. //将render对象添加到实现窗口vtkMFCwindow中

  2. this->pvtkMFCWindow->GetRenderWindow()->AddRenderer(this->pvtkRenderer);

  3.  
  4. //添加交互中鼠标左键响应回调函数

  5. vtkCallbackCommand* callback = vtkCallbackCommand::New();

  6. callback->SetCallback(handle_double_click);

  7. this->pvtkMFCWindow->GetInteractor()->AddObserver(vtkCommand::LeftButtonPressEvent, callback, 1.0);

  8. callback->Delete();

  9.  
  10. }

 

 

a.Doc类设置
 

头文件

 

 
  1. // vtkSDIDoc.h : interface of the CvtkSDIDoc class

  2. //

  3.  
  4.  
  5. #pragma once

  6.  
  7. class CvtkSDIDoc : public CDocument

  8. {

  9. protected: // create from serialization only

  10. CvtkSDIDoc();

  11. DECLARE_DYNCREATE(CvtkSDIDoc)

  12.  
  13. // Attributes

  14. private:

  15. vtkDataSetReader *pvtkDataSetReader;

  16.  
  17. // Operations

  18. public:

  19.  
  20. // Overrides

  21. public:

  22. virtual BOOL OnNewDocument();

  23. virtual void Serialize(CArchive& ar);

  24.  
  25. // Implementation

  26. public:

  27. virtual ~CvtkSDIDoc();

  28. #ifdef _DEBUG

  29. virtual void AssertValid() const;

  30. virtual void Dump(CDumpContext& dc) const;

  31. #endif

  32.  
  33. private:

  34. void ExecutePipeline();

  35. void RemoveActors();

  36.  
 
  1. //数据处理和显示的mapper和actor对象

  2. vtkDataSetMapper *pvtkDataSetMapper;

  3. vtkActor *pvtkActor;

  4.  
  5. vtkActor2D *pvtkActor2D;

  6. vtkTextMapper *pvtkTextMapper;

  7.  
  8. // Generated message map functions

  9. protected:

  10. DECLARE_MESSAGE_MAP()

  11. public:

  12. virtual void OnCloseDocument();

  13. virtual BOOL OnOpenDocument(LPCTSTR lpszPathName);

  14. };

  15.  

 

实现文件

 

 
  1. // vtkSDIDoc.cpp : implementation of the CvtkSDIDoc class

  2. //

  3.  
  4. #include "stdafx.h"

  5. #include "vtkSDI.h"

  6.  
  7. #include "vtkSDIDoc.h"

  8. #include "vtkSDIView.h"

  9.  
  10. #ifdef _DEBUG

  11. #define new DEBUG_NEW

  12. #endif

  13.  
  14.  
  15. // CvtkSDIDoc

  16.  
  17. IMPLEMENT_DYNCREATE(CvtkSDIDoc, CDocument)

  18.  
  19. BEGIN_MESSAGE_MAP(CvtkSDIDoc, CDocument)

  20. END_MESSAGE_MAP()

  21.  
  22.  
  23. // CvtkSDIDoc construction/destruction

  24.  
  25. CvtkSDIDoc::CvtkSDIDoc()

  26. {

  27. this->pvtkDataSetReader = NULL;

  28.  
  29. // 创建对象,从而可以显示

  30. this->pvtkDataSetMapper = vtkDataSetMapper::New();

  31. this->pvtkActor = vtkActor::New();

  32. this->pvtkActor2D = vtkActor2D::New();

  33. this->pvtkTextMapper = vtkTextMapper::New();

  34. }

  35.  
  36. CvtkSDIDoc::~CvtkSDIDoc()

  37. {

  38. }

  39.  
  40. BOOL CvtkSDIDoc::OnNewDocument()

  41. {

  42. if (!CDocument::OnNewDocument())

  43. return FALSE;

  44.  
  45. //移除原有actor对象

  46. RemoveActors();

  47.  
  48. //执行数据处理流程

  49. ExecutePipeline();

  50.  
  51. return TRUE;

  52. }

  53.  
  54.  
  55.  
  56.  
  57. // CvtkSDIDoc serialization

  58.  
  59. void CvtkSDIDoc::Serialize(CArchive& ar)

  60. {

  61. if (ar.IsStoring())

  62. {

  63. // TODO: add storing code here

  64. }

  65. else

  66. {

  67. // TODO: add loading code here

  68. }

  69. }

  70.  
  71.  
  72. // CvtkSDIDoc diagnostics

  73.  
  74. #ifdef _DEBUG

  75. void CvtkSDIDoc::AssertValid() const

  76. {

  77. CDocument::AssertValid();

  78. }

  79.  
  80. void CvtkSDIDoc::Dump(CDumpContext& dc) const

  81. {

  82. CDocument::Dump(dc);

  83. }

  84. #endif //_DEBUG

  85.  
  86.  
  87. // CvtkSDIDoc commands

  88.  
  89. void CvtkSDIDoc::RemoveActors()

  90. {

  91. //活的view类对象

  92. POSITION pos = this->GetFirstViewPosition();

  93. CvtkSDIView *pcvtkSDIView = NULL;

  94.  
  95. if (pos)

  96. {

  97. pcvtkSDIView = (CvtkSDIView *)GetNextView(pos);

  98. }

  99. else // return

  100. {

  101. ASSERT(FALSE);

  102. return;

  103. }

  104.  
  105. //移除actor对象

  106. pcvtkSDIView->GetRenderer()->RemoveActor(this->pvtkActor);

  107. pcvtkSDIView->GetRenderer()->RemoveActor(this->pvtkActor2D);

  108. }

  109.  
 
  1. //openfile 打开数据文件,将数据导入

  2. BOOL CvtkSDIDoc::OnOpenDocument(LPCTSTR lpszPathName)

  3. {

  4. if (!CDocument::OnOpenDocument(lpszPathName))

  5. return FALSE;

  6.  
  7. //移除对象

  8. RemoveActors();

  9.  
  10. //读入数据

  11. this->pvtkDataSetReader = vtkDataSetReader::New();

  12. this->pvtkDataSetReader->SetFileName(lpszPathName);

  13.  
  14. //运行数据导入流程,导入数据

  15. ExecutePipeline();

  16.  
  17. return TRUE;

  18. }

//释放创建的内存
 
  1. void CvtkSDIDoc::OnCloseDocument()

  2. {

  3. // delete data

  4. if (this->pvtkDataSetReader) this->pvtkDataSetReader->Delete();

  5.  
  6. // Delete the the objects used to form the visualisation.

  7. if (this->pvtkDataSetMapper) this->pvtkDataSetMapper->Delete();

  8. if (this->pvtkActor) this->pvtkActor->Delete();

  9. if (this->pvtkActor2D) this->pvtkActor2D->Delete();

  10. if (this->pvtkTextMapper) this->pvtkTextMapper->Delete();

  11.  
  12. CDocument::OnCloseDocument();

  13. }

  14.  
 
  1. //数据处理流程

  2. void CvtkSDIDoc::ExecutePipeline()

  3. {

  4. // 获得视图类对象

  5. POSITION pos = this->GetFirstViewPosition();

  6. CvtkSDIView *pcvtkSDIView = NULL;

  7.  
  8. if (pos)

  9. {

  10. pcvtkSDIView = (CvtkSDIView *)GetNextView(pos);

  11. }

  12. else // return

  13. {

  14. ASSERT(FALSE);

  15. return;

  16. }

  17.  
  18. if (pvtkDataSetReader) // 有数据,将数据添加到render中显示

  19. {

  20. this->pvtkDataSetMapper->SetInput(this->pvtkDataSetReader->GetOutput());

  21. this->pvtkActor->SetMapper(this->pvtkDataSetMapper);

  22.  
  23. this->pvtkTextMapper->SetInput(this->pvtkDataSetReader->GetFileName());

  24. this->pvtkTextMapper->GetTextProperty()->SetFontSize(12);

  25. this->pvtkActor2D->SetMapper(this->pvtkTextMapper);

  26.  
  27. pcvtkSDIView->GetRenderer()->SetBackground(0.0,0.0,0.4);

  28. pcvtkSDIView->GetRenderer()->AddActor(this->pvtkActor);

  29. pcvtkSDIView->GetRenderer()->AddActor(this->pvtkActor2D);

  30. pcvtkSDIView->GetRenderer()->ResetCamera();

  31. pvtkDataSetReader->Delete();

  32. pvtkDataSetReader = NULL;

  33. }

  34. else //无导入数据,显示textmapper, hello world

  35. {

  36. this->pvtkTextMapper->SetInput("Hello World");

  37. this->pvtkTextMapper->GetTextProperty()->SetFontSize(24);

  38. this->pvtkActor2D->SetMapper(this->pvtkTextMapper);

  39.  
  40. pcvtkSDIView->GetRenderer()->SetBackground(0.0,0.0,0.4);

  41. pcvtkSDIView->GetRenderer()->AddActor(this->pvtkActor2D);

  42. pcvtkSDIView->GetRenderer()->ResetCamera();

  43. }

  44. }


转:https://blog.csdn.net/pizibing880909/article/details/21714347?utm_source=tuicool

相关文章:

  • 2021-11-30
  • 2022-12-23
  • 2021-12-04
  • 2022-12-23
  • 2021-10-06
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-23
  • 2021-06-20
  • 2021-11-18
  • 2022-12-23
  • 2021-04-21
  • 2023-01-28
相关资源
相似解决方案