DXFTest

// 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();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C#中实现dxf文件的读取和保存,可以使用AutoCAD提供的API。以下是实现的基本步骤: 1. 引用AutoCAD.Interop.dll和AutoCAD.Interop.Common.dll到C#项目中。 2. 创建AutoCAD应用程序对象,并打开dxf文件。 ``` AutoCAD.Application acApp = new AutoCAD.Application(); acApp.Documents.Open("d:\\test.dxf", false); ``` 3. 获取AutoCAD文档对象,通过Document对象获取模型空间和图形对象集合。 ``` Document acDoc = acApp.ActiveDocument; Database acDb = acDoc.Database; BlockTable acBlkTbl = acDb.BlockTable; BlockTableRecord acBlkTblRec = acBlkTbl[BlockTableRecord.ModelSpace]; ``` 4. 通过Entity对象遍历图形对象集合,获取实体数据。 ``` foreach (ObjectId acObjId in acBlkTblRec) { Entity acEnt = acTrans.GetObject(acObjId, OpenMode.ForRead) as Entity; if (acEnt != null) { // 获取实体数据 } } ``` 5. 保存dxf文件。 ``` acDoc.SaveAs("d:\\new.dxf", DwgVersion.AC1024); ``` 完整代码示例: ``` using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Interop; using Autodesk.AutoCAD.Interop.Common; namespace DXFTest { class Program { static void Main(string[] args) { // 打开dxf文件 AutoCAD.Application acApp = new AutoCAD.Application(); acApp.Documents.Open("d:\\test.dxf", false); // 获取文档对象 Document acDoc = acApp.ActiveDocument; Database acDb = acDoc.Database; BlockTable acBlkTbl = acDb.BlockTable; BlockTableRecord acBlkTblRec = acBlkTbl[BlockTableRecord.ModelSpace]; // 遍历图形对象集合 foreach (ObjectId acObjId in acBlkTblRec) { Entity acEnt = acTrans.GetObject(acObjId, OpenMode.ForRead) as Entity; if (acEnt != null) { // 获取实体数据 Point3d acPt = acEnt.GeometricExtents.MinPoint; double acWidth = acEnt.GeometricExtents.MaxPoint.X - acEnt.GeometricExtents.MinPoint.X; double acHeight = acEnt.GeometricExtents.MaxPoint.Y - acEnt.GeometricExtents.MinPoint.Y; // 打印实体数据 System.Console.WriteLine("Entity: ({0}, {1}), Width: {2}, Height: {3}", acPt.X, acPt.Y, acWidth, acHeight); } } // 保存dxf文件 acDoc.SaveAs("d:\\new.dxf", DwgVersion.AC1024); // 关闭AutoCAD应用程序 acApp.Quit(); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值