原文链接
本例仿照官方示例 ImportExport,如图:

添加 ImportIGES 消息处理函数
1. 添加事件菜单
如图,在 IDR_MAINFRAME 的菜单栏中添加 Import…->IGES 二级菜单(以后写载入其他格式的文件都在此)。

2. 在 IGES 上右键选择 添加事件处理程序
void OnImportIGES(),(放在doc类中)

3. 在 Doc 类中 添加三个 ReadIGES 函数
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)));
}
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;
}
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 文件,添加到项目中即可。但是你需要修改这些文件来适合你的需求,我直接使用的时候,发现它包含了很多其他头文件,并且级联包含其他,最后会弄出很多问题,所以这里我删去了很多,只保留我需要使用的,下面是我删减后的文件:
// 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_)
// 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类 中添加 成员变量:
protected:
CColoredShapes* m_pcoloredshapeList;
在 Doc类 的 初始化函数 中添加:
m_pcoloredshapeList = new CColoredShapes();
在 Doc类 的 析构函数 中添加:
if( m_pcoloredshapeList ) delete m_pcoloredshapeList;
在 Doc类 的 Serialize 函数中添加:
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 函数中添加:
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函数
void CHuiStyleDoc::Fit()
{
CMainFrame *pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
CHuiStyleView *pView = (CHuiStyleView *) pFrame->GetActiveView();
pView->FitAll();
}
void FitAll() { if ( !myView.IsNull() ) myView->FitAll(); myView->ZFitAll(); };
至此,我们就可以载入 **IGES **文件了,看下运行示例:

网友评论