OpenCasCade – 载入IGES文件

本例是仿照官方示例 ImportExport,如图:

ImportExport

 

来实现的,说明下大概流程以及自己尝试的方法,遇到的问题,以及如何解决的。

添加 ImportIGES 消息处理函数

1. 添加事件菜单

如图,在 IDR_MAINFRAME 的菜单栏中添加 Import…->IGES 二级菜单(以后写载入其他格式的文件都在此)。

IGES_Menu

 

2.  在 IGES 上右键选择 添加事件处理程序

void OnImportIGES(),(放在doc类中)

消息处理函数

 

3. 在 Doc 类中 添加三个 ReadIGES 函数

 

 

1

2

3

4

5

6

void CHuiStyleDoc::ReadIGES(const Handle(AIS_InteractiveContext)& anInteractiveContext)

{

Handle(TopTools_HSequenceOfShape) aSequence = CHuiStyleDoc::ReadIGES();

for(int i=1;i<= aSequence->Length();i++)

anInteractiveContext->Display(new AIS_Shape(aSequence->Value(i)));

}

 

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

Handle(TopTools_HSequenceOfShape) CHuiStyleDoc::ReadIGES()

{

CFileDialog dlg(TRUE,

NULL,

NULL,

OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,

"IGES Files (*.iges , *.igs)|*.iges; *.igs|All Files (*.*)|*.*||",

NULL );

 

TCHAR tchBuf[80];

 

CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);

CString initdir = (CASROOTValue + "\\..\\data\\iges");

 

dlg.m_ofn.lpstrInitialDir = initdir;

 

Handle(TopTools_HSequenceOfShape) aSequence = new TopTools_HSequenceOfShape();

if (dlg.DoModal() == IDOK)

{

SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));

CString C = dlg.GetPathName();

Standard_CString aFileName = (Standard_CString)(LPCTSTR)C;

Standard_Integer status = ReadIGES(aFileName,aSequence);

if (status != IFSelect_RetDone)

{

MessageBox(0,"Error : The file is not read","CasCade Error",MB_ICONERROR);

}

 

SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));

}

return aSequence;

}

 

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Standard_Integer CHuiStyleDoc::ReadIGES(const Standard_CString& aFileName,

Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)

{

IGESControl_Reader Reader;

 

Standard_Integer status = Reader.ReadFile(aFileName);

 

if (status != IFSelect_RetDone) return status;

Reader.TransferRoots();

TopoDS_Shape aShape = Reader.OneShape();    

aHSequenceOfShape->Append(aShape);

 

return status;

}

 

4. 添加 CColoredShapes 类

这个可以直接使用 x:\xxxx\opencascade-6.7.1\samples\mfc\standard\05_ImportExport\src 目录下的 ColoredShapes.h 和 ColoredShapes.cpp 文件,添加到项目中即可。但是你需要修改这些文件来适合你的需求,我直接使用的时候,发现它包含了很多其他头文件,并且级联包含其他,最后会弄出很多问题,所以这里我删去了很多,只保留我需要使用的,下面是我删减后的文件:

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

// ColoredShape.h: interface for the CColoredShape class.

//

//

 

#if !defined(AFX_COLOREDSHAPES_H__C6419AF3_A78A_11D1_8C93_00AA00D10994__INCLUDED_)

#define AFX_COLOREDSHAPES_H__C6419AF3_A78A_11D1_8C93_00AA00D10994__INCLUDED_

 

#if _MSC_VER >= 1000

#pragma once

#endif // _MSC_VER >= 1000

 

#include <TopTools_DataMapOfShapeInteger.hxx>

 

class CColoredShapes : public CObject  

{

public:

CColoredShapes();

void Add(const Quantity_NameOfColor aColor, const TopoDS_Shape& aShape);

void Remove(const TopoDS_Shape& aShape);

 

void Display( Handle(AIS_InteractiveContext)& anAIScontext);

 

TopoDS_ListOfShape getShapes();

Quantity_NameOfColor getShapeColor(TopoDS_Shape aShape);

 

protected:

// Declare CArchive >> operator

DECLARE_SERIAL(CColoredShapes);

 

 

private:

TopTools_DataMapOfShapeInteger m_colorMap;

TopoDS_ListOfShape   m_shapeList;

};

 

#endif // !defined(AFX_COLOREDSHAPES_H__C6419AF3_A78A_11D1_8C93_00AA00D10994__INCLUDED_)

 

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

// ColoredShapes.cpp: implementation of the CColoredShape class.

//

//

 

#include "stdafx.h"

 

#include <afxtempl.h>

 

#include "ColoredShapes.h"

 

//

// Construction/Destruction

//

 

CColoredShapes::CColoredShapes()

{

 

}

 

 

void CColoredShapes::Add(const Quantity_NameOfColor aColor, const TopoDS_Shape& aShape)

{

m_shapeList.Clear();

m_shapeList.Append(aShape);

m_colorMap.Bind(aShape, aColor);

}

 

void CColoredShapes::Remove(const TopoDS_Shape& aShape)

{

m_colorMap.UnBind(aShape);

for ( TopoDS_ListIteratorOfListOfShape iter(m_shapeList); iter.More(); iter.Next() ) {

if(iter.Value() == aShape) {

m_shapeList.Remove(iter);

break;

}

}

}

 

IMPLEMENT_SERIAL(CColoredShapes, CObject,1);

 

// This schema contains all the Persistent Geometry and Topology

#include <ShapeSchema.hxx>  

 

// Tools to store TopoDS_Shape

#include <MgtBRep.hxx>

#include <PTopoDS_HShape.hxx>

#include <PTColStd_TransientPersistentMap.hxx>

#include <TopoDS_Shape.hxx>

 

// Tools to put Persistent Object in an archive

#include <Storage_Data.hxx>

#include <Storage_HSeqOfRoot.hxx>

#include <Storage_Root.hxx>

#include <PTColStd_PersistentTransientMap.hxx>

 

 

void CColoredShapes::Display(Handle(AIS_InteractiveContext)& anAIScontext)

{

for ( TopoDS_ListIteratorOfListOfShape iter(m_shapeList); iter.More(); iter.Next() )

{

Handle(AIS_Shape) ais = new AIS_Shape(iter.Value());

anAIScontext->SetColor(ais, (Quantity_NameOfColor)m_colorMap.Find(iter.Value()));

                anAIScontext->SetMaterial(ais, Graphic3d_NOM_GOLD, Standard_False);

anAIScontext->Display(ais, Standard_False);

}

}

 

5. 在Doc类中使用CColoredShapes对象

在 Doc类 中添加 成员变量

 

1

2

protected:

CColoredShapes* m_pcoloredshapeList;

在 Doc类 的 初始化函数 中添加:

 

1

m_pcoloredshapeList = new CColoredShapes();

在 Doc类 的 析构函数 中添加:

 

1

if( m_pcoloredshapeList ) delete m_pcoloredshapeList;

在 Doc类 的 Serialize 函数中添加:

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

void CHuiStyleDoc::Serialize(CArchive& ar)

{

if (ar.IsStoring())

{

// TODO: add storing code here

ar << m_pcoloredshapeList;

}

else

{

// Read from the archive the current CColoredShape

ar >> m_pcoloredshapeList;

 

// Display the new object

m_pcoloredshapeList->Display(myAISContext);

}

}

在 OnImportIGES 函数中添加:

 

1

2

3

4

5

6

7

8

9

10

11

void CHuiStyleDoc::OnImportIGES()

{

// TODO: 在此添加命令处理程序代码

Handle(TopTools_HSequenceOfShape) aSeqOfShape = CHuiStyleDoc::ReadIGES();

for(int i=1;i<= aSeqOfShape->Length();i++)

{

m_pcoloredshapeList->Add(Quantity_NOC_YELLOW, aSeqOfShape->Value(i));

m_pcoloredshapeList->Display(myAISContext);

}

Fit();

}

最后的 Fit函数 是用来调整显示的,调用 View类 中的 FitAll函数

 

1

2

3

4

5

6

void CHuiStyleDoc::Fit()

{

CMainFrame *pFrame =  (CMainFrame*)AfxGetApp()->m_pMainWnd;

CHuiStyleView *pView = (CHuiStyleView *) pFrame->GetActiveView();

pView->FitAll();

}

 

 

1

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

至此,我们就可以载入 IGES 文件了,看下运行示例:

sample

©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页