// ModelInfoTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <mmsystem.h>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#include "DXFLine.h"
#include "DXFCircle.h"
#include <iosfwd>
#include <fstream>
using namespace std;
string ReadLine(istringstream& instr,char* istrBuffer,UINT& curByteSize,int& rowIndex);
// 唯一的应用程序对象
CWinApp theApp;
void execute();
void writeBuffer();
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
HMODULE hModule = ::GetModuleHandle(NULL);
if (hModule != NULL)
{
// 初始化 MFC 并在失败时显示错误
if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
{
// TODO: 更改错误代码以符合您的需要
_tprintf(_T("错误: MFC 初始化失败\n"));
nRetCode = 1;
}
else
{
// TODO: 在此处为应用程序的行为编写代码。
execute();
}
}
else
{
// TODO: 更改错误代码以符合您的需要
_tprintf(_T("错误: GetModuleHandle 失败\n"));
nRetCode = 1;
}
return nRetCode;
}
/// <summary> 创建执行程序. </summary>
///
/// <remarks> zhengzhe, 2011/11/9. </remarks>
vector<DXFMeta*> dxfVector; //存放所有DXF图元
const char* srcFile = "c:\\dxftest\\t4.dxf";
const char* desFile = "c:\\dxftest\\t4_1.dxf";
const char* infoFile = "c:\\dxftest\\t4_1_1.txt";
CFile m_curFile(_T(srcFile),CFile::modeRead);
UINT uFileSize = (UINT)m_curFile.GetLength();
char* istrBuffer = new char[uFileSize]; //输入缓冲区
//string str(strBuffer);
//std::istringstream instr(str);
istringstream instr;
void writeInfo();
void execute()
{
wcout.imbue(locale("chs"));
clock_t start,end;
start = clock();
m_curFile.Read(istrBuffer,uFileSize);
m_curFile.Close();
instr.str(istrBuffer);
int rowIndex = 0,pos = 0;
UINT curByteSize = 0;
int num=0;
char *preLine=istrBuffer ,*curLine=istrBuffer;
while(!instr.eof())
{
//读取并返回一行数据
char tmp[256];
instr.getline(tmp,256);
++rowIndex;
curByteSize+=(strlen(tmp)+1)*sizeof(char);
CString s(tmp);
//开始读取实体数据
if (s.Trim()=="ENTITIES")
{
do
{
instr.getline(tmp,256);
++rowIndex;
curByteSize+=(strlen(tmp)+1)*sizeof(char);
s=tmp;
DXFMeta * pMeta=NULL;
if (s.Trim()=="LINE")
{
pMeta= new DXFLine(rowIndex,curByteSize);
instr>>*(DXFLine*)pMeta;
}
if (s.Trim()=="CIRCLE")
{
pMeta = new DXFCircle(rowIndex,curByteSize);
instr>>*(DXFCircle*)pMeta;
}
if(pMeta!=NULL){
//cout<<*pMeta<<endl;
dxfVector.push_back(pMeta);
//cout <<"vector size:" <<dxfVector.size()<<endl;
rowIndex = pMeta->GetRowEndIndex();
curByteSize=pMeta->GetSeekEndPos();
}
} while (s.Trim()!="ENDSEC");
}
if (s.Trim()=="EOF")
break;
//cout << "Row Index:"<<rowIndex<<"\tByteSize:" << curByteSize <<"\t is :"<<s <<endl;
//instr.seekg(curByteSize,ios::beg);
}
writeBuffer();
writeInfo();
if (dxfVector.size()>0)
{
for (UINT i=0;i<dxfVector.size();i++)
{
delete dxfVector[i];
}
}
end = clock();
cout << "vector size:" <<dxfVector.size()<<endl;
cout << "Run time:" << (double)(end-start)/CLOCKS_PER_SEC <<"seconds " <<endl;
}
void writeBuffer(){
UINT oSize = uFileSize;
for (UINT i=0;i<dxfVector.size();i++)
{
oSize += dxfVector[i]->GetCurByteSize()-dxfVector[i]->GetOrgByteSize();
}
char* ostrBuffer= new char[oSize+1]; //输出缓冲区
ostrBuffer[oSize]='\0';
ostringstream outstr(ostrBuffer);
instr.seekg(0,ios::beg);
for (UINT i=0;i<dxfVector.size();i++)
{
//copy源信息中vector和当前的
streamoff inPos = instr.tellg();
//如果当前输出的字节偏移数与
streamoff offSize = dxfVector[i]->GetSeekStartPos()-inPos;
ULONG len = (UINT)offSize/sizeof(char);
char *tmp=new char[len+1];
strncpy(tmp,&istrBuffer[inPos],(size_t)len); //将滑动的字节信息拷贝到tmp
tmp[len]='\0';
outstr<<tmp; //根据滑动的信息,移动输出指针
instr.seekg(offSize,ios::cur); //根据滑动信息,移动输入指针
outstr<<dxfVector[i]->OutputFormat();//输出新的节点数据信息,更新输出指针
instr.seekg(dxfVector[i]->GetOrgByteSize(),ios::cur);//输入指针跳过原信息的位置
}
string out = outstr.str();//不能用outstr.str().c_str(),必须使用outstr.str()生成string对象以保存信息
CFile m_outFile(_T(desFile),CFile::modeCreate|CFile::modeWrite);
outstr.flush();
m_outFile.Write(out.c_str(),oSize);
m_outFile.Close();
}
void writeInfo()
{
ofstream fileOut;
fileOut.open(infoFile);
if (!fileOut)
{
cout << "打开文件失败" <<endl;
}
for (UINT i=0;i<dxfVector.size();i++)
{
fileOut<<*dxfVector[i]<<endl;
//cout << *dxfVector[i] <<endl;
}
fileOut.close();
}
DXFTest
最新推荐文章于 2025-04-05 18:30:28 发布