www.pudn.com > ReadDxf.rar > ReadDxfDoc.cpp


// ReadDxfDoc.cpp : implementation of the CReadDxfDoc class 
// 
 
#include "stdafx.h" 
#include "ReadDxf.h" 
#include "MainFrm.h" 
#include "ReadDxfDoc.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CReadDxfDoc 
 
IMPLEMENT_DYNCREATE(CReadDxfDoc, CDocument) 
 
BEGIN_MESSAGE_MAP(CReadDxfDoc, CDocument) 
	//{{AFX_MSG_MAP(CReadDxfDoc) 
	ON_COMMAND(ID_FILE_IMPORT, OnFileImport) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CReadDxfDoc construction/destruction 
 
CReadDxfDoc::CReadDxfDoc() 
{ 
	// TODO: add one-time construction code here 
	pPointList=NULL;PointNum=0; 
} 
 
CReadDxfDoc::~CReadDxfDoc() 
{ 
	delete[] pPointList; 
} 
 
BOOL CReadDxfDoc::OnNewDocument() 
{ 
	if(pPointList!=NULL) 
		delete[] pPointList; 
	pPointList=NULL; 
	PointNum=0; 
	if (!CDocument::OnNewDocument()) 
		return FALSE; 
 
	// TODO: add reinitialization code here 
	// (SDI documents will reuse this document) 
 
	return TRUE; 
} 
 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CReadDxfDoc serialization 
 
void CReadDxfDoc::Serialize(CArchive& ar) 
{ 
	if (ar.IsStoring()) 
	{ 
		// TODO: add storing code here 
	} 
	else 
	{ 
		// TODO: add loading code here 
	} 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CReadDxfDoc diagnostics 
 
#ifdef _DEBUG 
void CReadDxfDoc::AssertValid() const 
{ 
	CDocument::AssertValid(); 
} 
 
void CReadDxfDoc::Dump(CDumpContext& dc) const 
{ 
	CDocument::Dump(dc); 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CReadDxfDoc commands 
 
int CReadDxfDoc::GetPointNum_File(FILE *pf) 
{ 
	char ch[128]; 
	do{ 
		fscanf(pf,"%s\n",ch); 
	}while(!strcmp(ch,"ENTITIES")==0); 
	do 
	{ 
		fscanf(pf,"%s\n",ch); 
		if(strcmp(ch,"POINT")==0) 
			PointNum++; 
	}while(!strcmp(ch,"ENDSEC")==0); 
	return PointNum; 
} 
 
void CReadDxfDoc::OnFileImport()  
{ 
	// TODO: Add your command handler code here 
	CMainFrame* pFrm=(CMainFrame*)AfxGetApp()->m_pMainWnd; 
	CFileDialog ImDxfDlg(TRUE,"dxf",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
		"R14 dxf|*dxf|",NULL); 
	if(ImDxfDlg.DoModal()==IDCANCEL) 
		return; 
	FILE* pf=NULL;	 
	if((pf=fopen(ImDxfDlg.GetPathName(),"rb"))==NULL) 
	{ 
		AfxMessageBox("Open file error!"); 
		return; 
	} 
	int Num=0; 
	Num=GetPointNum_File(pf); 
	pPointList=new MYPOINT[Num]; 
	if(pPointList==NULL) 
		AfxMessageBox("Memery Error!"); 
	//----------------------------// 
	pFrm->SetProgressRange(Num/1024,1); 
	rewind(pf); 
	int i=0; 
	char ch[128]; 
	do{ 
		fscanf(pf,"%s\n",ch); 
	}while(!strcmp(ch,"ENTITIES")==0); 
	do 
	{ 
		fscanf(pf,"%s\n",ch); 
		if(strcmp(ch,"10")==0&&PointNum!=0) 
		{ 
			fscanf(pf,"%f\n",&(pPointList[i].x)); 
			fscanf(pf,"%*s\n"); 
			fscanf(pf,"%f\n",&(pPointList[i].y)); 
			fscanf(pf,"%*s\n"); 
			fscanf(pf,"%f\n",&(pPointList[i].z)); 
			i++; 
			pFrm->SetPos(i/1024); 
		} 
	}while(!strcmp(ch,"ENDSEC")==0); 
	pFrm->SetPos(0); 
	//----------------------------// 
	fclose(pf); 
	UpdateAllViews(NULL); 
}