您现在的位置:网站首页 > 经验分享 > dxf数据格式输出输出类
设计师介绍:

昵称:烦夫子
类别:界面/平面设计师
年龄:38
现所在地:北京

查看该设计师的主页>>

关注好友

统计中心

主页浏览总数:24261
总积分:89
文章数:88
作品数:70

dxf数据格式输出输出类

作者:烦夫子  更新时间: 2007-11-19   浏览人数:19030  评论:0  
分享到:

#ifndef __H_DXFIO12__
#define __H_DXFIO12__

#include "Vector.h"

class CDxfIO12
{
//Attributes
public:
FILE* m_pFile;
CString m_csFileName;
CString m_csGroupLabel;
CString m_csLayerName;
int m_iColor;
int m_iEntityOrder;
int m_iGroup;
char m_szBuf[256];

//Methods
public:

CDxfIO12();
virtual ~CDxfIO12();

int OpenFile(CString csFileName);
int CreateFile (CString csFileName);
int CloseFile();
int MakeFile(CString csFName);
int GetFile (CString csFName);

int PutHead();
int PutEOF();
int PutGroupAndLabel(int Group, CString csLabel);
int PutLayerAndColor();
int PutStartSection(CString csSection);
int PutEndSection();
int PutStartBlock(CString csBlockName);
int PutEndBlock();

int PutLocation(Vector pt);
int PutPoint(Vector pnt);
int PutLine(Vector p1, Vector p2);
int PutPolyline(Vector pt[], int iNum);
int PutCircle(Vector pc, float rad);
int PutText(CString csLabel, Vector pt);

int GetHead();
int GetEOF();
int GetGroupAndLabel();
int GetLayerAndColor();
int FindSection (const char *SectionName, BOOL bReset = FALSE);
int SkipSeqEnd ();
int GetStartSection(CString &csSection);
int GetEndSection();
int GetStartBlock(CString &csBlockName);
int GetEndBlock();
BOOL IsRecord ();
int SkipToRecord ();

int GetXY (Vector &pt);
int GetLocation(Vector &pt);
int GetPoint(Vector &pnt);
int GetLine(Vector &p1, Vector &p2);
int GetPolyline(Vector *&ptList, int &iNum);
int GetCircle(Vector &pc, float &rad);
int GetText(CString &csLabel, Vector &pt);
};

#endif

//DxfIO12.CPP
#include "StdAfx.h"
#include "DxfIO12.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


CDxfIO12::CDxfIO12()
{
m_pFile = 0;
m_iEntityOrder = 30;
m_csLayerName = _T("0");
m_iColor = 100;
}

CDxfIO12::~CDxfIO12()
{
CloseFile();
}

int CDxfIO12::CreateFile(CString csFName)
{
if(m_pFile) CloseFile();

m_pFile = fopen(csFName, "w+t");

if(m_pFile)
m_csFileName = csFName;

return !(m_pFile!=NULL);
}

//打开DXF12文件
int CDxfIO12::OpenFile(CString csFName)
{
//如以前打开,则先关闭
if(m_pFile) CloseFile();

//打开文件
m_pFile = fopen(csFName, "r+t");

//赋文件名
if(m_pFile)
m_csFileName = csFName;

return !(m_pFile!=NULL);
}

int CDxfIO12::CloseFile()
{
int error = 0;

if(m_pFile)
{
fclose(m_pFile);

m_pFile = NULL;
}

return !(error!=0);
}

int CDxfIO12::MakeFile(CString csFileName)
{
int error = 0;

error = OpenFile(csFileName);
if(error!=0) return error;

error = PutHead();

//Entities...
if(error == 0)
error = PutStartSection(_T("ENTITIES"));

if(error == 0)
error = PutPoint(Vector(50, 50));

if(error == 0)
error = PutLine(Vector(50, 100), Vector(100, 150));

if(error == 0)
error = PutCircle(Vector(75, 200), 25.0);

if(error == 0)
{
Vector ptArr[5];
for(int i=0; i<5; i++)
{
ptArr[i].x = 50 + i*15.5;
ptArr[i].y = 250 + (i%2) * 20.0;
//ptArr[i].z = 0;
}

error = PutPolyline(ptArr, 5);
}

if(error == 0)
error = PutEndSection();

if(error == 0)
error = PutEOF();

if(error == 0)
error = CloseFile();

return error;
}


int CDxfIO12::GetFile(CString csFileName)
{
int error = 0;
int iNumPnt = 0;
Vector *pvArr = NULL;

error = OpenFile(csFileName);
if(error!=0) return error;

//Entities...
if(error == 0)
error = FindSection (_T("ENTITIES"));

error = GetPolyline(pvArr, iNumPnt);

error = CloseFile();

return error;
}

int CDxfIO12::PutGroupAndLabel(int iGroup, CString csLabel)
{
CString temp;
int error = 0;

temp.Format("%-3i %s ", iGroup, csLabel);

error = fprintf(m_pFile, "%s", temp);
error = !(error == temp.GetLength());

return(error);
}

int CDxfIO12::PutEOF()
{
return PutGroupAndLabel(0, _T("EOF"));
}

int CDxfIO12::PutHead()
{
int error = 0;

error = PutGroupAndLabel(0, _T("SECTION"));

if (error == 0)
error = PutGroupAndLabel(2, _T("HEADER"));

if (error == 0)
error = PutGroupAndLabel(9, _T("$ACADVER"));

if (error == 0)
error = PutGroupAndLabel(1, _T("AC1009"));

if (error == 0)
error = PutGroupAndLabel(9, _T("$EXTMIN"));

if (error == 0)
error = PutGroupAndLabel(10, _T("0.000"));

if (error == 0)
error = PutGroupAndLabel(20, _T("0.000"));

if (error == 0)
error = PutGroupAndLabel(30, _T("0.0"));

if (error == 0)
error = PutGroupAndLabel(9, _T("$EXTMAX"));

if (error == 0)
error = PutGroupAndLabel(10, _T("1000.000"));

if (error == 0)
error = PutGroupAndLabel(20, _T("1000.00"));

if (error == 0)
error = PutGroupAndLabel(30, _T("0.00"));

if (error == 0)
error = PutGroupAndLabel(9, _T("$LIMMAX"));

if (error == 0)
error = PutGroupAndLabel(10, _T("1000.000"));

if (error == 0)
error = PutGroupAndLabel(20, _T("1000.000"));

if (error == 0)
error = PutGroupAndLabel(9, _T("$LIMMIN"));

if (error == 0)
error = PutGroupAndLabel(10, _T("-100.000"));

if (error == 0)
error = PutGroupAndLabel(20, _T("-100.000"));

if (error == 0)
error = PutGroupAndLabel(0, _T("ENDSEC"));

return(error);
}

int CDxfIO12::PutLayerAndColor()
{
int error=0;

m_csGroupLabel.Format("%-x", m_iEntityOrder++);
error = PutGroupAndLabel(5, m_csGroupLabel);

if(error == 0)
error = PutGroupAndLabel(8, m_csLayerName);

//if (error == 0)
//{
// m_csGroupLabel.Format("%d", m_iColor);
// error = PutGroupAndLabel(62, m_csGroupLabel);
//}

return(error);
}

int CDxfIO12::PutCircle(Vector cen, float rad)
{
int error=0;

/* entity type */
error = PutGroupAndLabel(0, _T("CIRCLE"));
if (error == 0)
error = PutLayerAndColor();

/* origin */
if (error == 0)
{
m_csGroupLabel.Format("%-15.6lf", cen.x);
error = PutGroupAndLabel(10, m_csGroupLabel);
}

if (error == 0)
{
m_csGroupLabel.Format("%-15.6lf", cen.y);
error = PutGroupAndLabel(20, m_csGroupLabel);
}

if (error == 0)
{
m_csGroupLabel.Format("%-15.6lf", 0);
error = PutGroupAndLabel(30, m_csGroupLabel);
}

/* radius */
if (error == 0)
{
m_csGroupLabel.Format("%-15.6lf", rad);
error = PutGroupAndLabel(40, m_csGroupLabel);
}

return(error);
}

int CDxfIO12::PutLocation(Vector pt)
{
int error=0;
double x,y,z;
double x1 = 0.0, y1 =0.0, z1 = 0.0;
boolean alignpt = TRUE;

x = pt.x;
y = pt.y;
z = 0;

/* write out insertion point */
if (error == 0)
{
m_csGroupLabel.Format("%-15.6lf", x);
error = PutGroupAndLabel(10, m_csGroupLabel);
}

if (error == 0)
{
m_csGroupLabel.Format("%-15.6lf", y);
error = PutGroupAndLabel(20, m_csGroupLabel);
}

if (error == 0)
{
m_csGroupLabel.Format("%-15.6lf", z);
error = PutGroupAndLabel(30, m_csGroupLabel);
}

/* write out alignment point */
/*if (alignpt)
{
if (error == 0)
{
m_csGroupLabel.Format("%-15.6lf", x1);
error = PutGroupAndLabel(11, m_csGroupLabel);
}

if (error == 0)
{
m_csGroupLabel.Format("%-15.6lf", y1);
error = PutGroupAndLabel(31, m_csGroupLabel);
}

if (error == 0)
{
m_csGroupLabel.Format("%-15.6lf", z1);
error = PutGroupAndLabel(31, m_csGroupLabel);
}
}*/

return(error);
}

int CDxfIO12::PutText(CString csLabel, Vector pt)
{
int error=0;
float fTextSize = 5.0;

/* entity type */
error = PutGroupAndLabel(0, _T("TEXT"));
if (error == 0)
error = PutLayerAndColor();

/* location */
if (error == 0)
error = PutLocation(pt);

/* size */
if (error == 0)
{
m_csGroupLabel.Format("%-15.6lf", fTextSize);
error = PutGroupAndLabel(40, m_csGroupLabel);
}

/* string */
if (error == 0)
{
m_csGroupLabel = csLabel;
error = PutGroupAndLabel(1, m_csGroupLabel);
}

/* rotation */
if (error == 0)
error = PutGroupAndLabel(50, _T("0.000"));

/* aspect */
if (error == 0)
error = PutGroupAndLabel(41, _T("0.600")); //60 / 100

/* slant */
if (error == 0)
error = PutGroupAndLabel(51, _T("0"));

/* relative origin */
if (error == 0)
error = PutGroupAndLabel(71, _T("1"));

return(error);
}

int CDxfIO12::PutPoint(Vector pt)
{
int error =0;

/* entity type */
error = PutGroupAndLabel(0, _T("POINT"));

if (error == 0)
error = PutLayerAndColor();

/* coord */
if (error == 0)
{
m_csGroupLabel.Format("%-15.6f", pt.x);
error = PutGroupAndLabel(10, m_csGroupLabel);
}

if (error == 0)
{
m_csGroupLabel.Format("%-15.6f", pt.y);
error = PutGroupAndLabel(20, m_csGroupLabel);
}

if (error == 0)
{
m_csGroupLabel.Format("%-15.6f", 0);
error = PutGroupAndLabel(30, m_csGroupLabel);
}

return(error);
}

int CDxfIO12::PutLine(Vector p1, Vector p2)
{
int error = 0;

/* entity type */
error = PutGroupAndLabel(0, _T("LINE"));

if (error == 0)
error = PutLayerAndColor();

/* coord 1*/
if (error == 0)
{
m_csGroupLabel.Format("%-15.6f", p1.x);
error = PutGroupAndLabel(10, m_csGroupLabel);
}

if (error == 0)
{
m_csGroupLabel.Format("%-15.6f", p1.y);
error = PutGroupAndLabel(20, m_csGroupLabel);
}

if (error == 0)
{
m_csGroupLabel.Format("%-15.6f", 0);
error = PutGroupAndLabel(30, m_csGroupLabel);
}

/* coord 2*/
if (error == 0)
{
m_csGroupLabel.Format("%-15.6f", p2.x);
error = PutGroupAndLabel(11, m_csGroupLabel);
}

if (error == 0)
{
m_csGroupLabel.Format("%-15.6f", p2.y);
error = PutGroupAndLabel(21, m_csGroupLabel);
}

if (error == 0)
{
m_csGroupLabel.Format("%-15.6f", 0);
error = PutGroupAndLabel(31, m_csGroupLabel);
}

return(error);
}

int CDxfIO12::PutPolyline(Vector pt[], int iNumPt)
{
int error=0;
int i;

error = PutGroupAndLabel(0, _T("POLYLINE"));

/* put layer and Color */
if (error == 0)
error = PutLayerAndColor();

/* vectices follow flag */
if (error == 0)
error = PutGroupAndLabel(66, _T(" 1"));

/* starting and ending widths */
if (error == 0)
error = PutGroupAndLabel(40, _T("0"));
if (error == 0)
error = PutGroupAndLabel(41, _T("0"));

for (i=0; (i < iNumPt) && (error == 0); i++)
{
error = PutGroupAndLabel(0, _T("VERTEX"));
if (error == 0)
error = PutLayerAndColor();

if (error == 0)
{
m_csGroupLabel.Format("%-15.6lf", pt[i].x);
error = PutGroupAndLabel(10, m_csGroupLabel);
}
if (error == 0)
{
m_csGroupLabel.Format("%-15.6lf", pt[i].y);
error = PutGroupAndLabel(20, m_csGroupLabel);
}
if (error == 0)
{
m_csGroupLabel.Format("%-15.6lf", pt[i].x);
error = PutGroupAndLabel(30, m_csGroupLabel);
}
}

if (error == 0)
error = PutGroupAndLabel(0, _T("SEQEND"));

return(error);
}

int CDxfIO12::PutStartSection(CString csSectionName)
{
int error=0;

error = PutGroupAndLabel(0, _T("SECTION"));

if (error == 0)
error = PutGroupAndLabel(2, csSectionName);
return(error);
}

int CDxfIO12::PutEndSection()
{
int error=0;

error = PutGroupAndLabel(0, _T("ENDSEC"));

return(error);
}

int CDxfIO12::PutStartBlock(CString csBlockName)
{
int error=0;

error = PutGroupAndLabel(0, _T("BLOCK"));

/* layer name */
if (error == 0)
error = PutGroupAndLabel(8, m_csLayerName);

/* block name */
if (error == 0)
error = PutGroupAndLabel(2, csBlockName);

/* block type flags */
if (error == 0)
error = PutGroupAndLabel(70, _T("0"));

/* block base point */
if (error == 0)
error = PutGroupAndLabel(10, _T("0.000"));
if (error == 0)
error = PutGroupAndLabel(20, _T("0.000"));
if (error == 0)
error = PutGroupAndLabel(30, _T("0.000"));

return(error);
}

int CDxfIO12::PutEndBlock()
{
return(PutGroupAndLabel(0, _T("ENDBLK")));
}

//////////////// Import Functions /////////////////

int CDxfIO12:: GetHead()
{
int error=0;

return(error);
}

int CDxfIO12:: GetEOF()
{
int error=0;

return(error);
}

//得到组的标号
int CDxfIO12:: GetGroupAndLabel()
{
if (NULL == fgets (m_szBuf, 255, m_pFile))
return -1;
m_iGroup = atoi (m_szBuf);

if (NULL == fgets (m_szBuf, 255, m_pFile))
return -1;
m_csGroupLabel = m_szBuf;
m_csGroupLabel.TrimLeft ();
m_csGroupLabel.TrimRight (' ');
m_csGroupLabel.TrimRight (' ');

return 0;
}

BOOL CDxfIO12::IsRecord ()
{
if (m_iGroup != 0)
{
return(FALSE);
}

if (//(stricmp(m_csGroupLabel, "3DLINE") == 0) ||
//(stricmp(m_csGroupLabel, "ARC") == 0) ||
//(stricmp(m_csGroupLabel, "CIRCLE") == 0) ||
//(stricmp(m_csGroupLabel, "INSERT") == 0) ||
//(stricmp(m_csGroupLabel, "LINE") == 0) ||
//(stricmp(m_csGroupLabel, "POINT") == 0) ||
//(stricmp(m_csGroupLabel, "TEXT") == 0) ||
//(stricmp(m_csGroupLabel, "ENDBLK") == 0) ||
(stricmp(m_csGroupLabel, "POLYLINE") == 0) ||
(stricmp(m_csGroupLabel, "ENDSEC") == 0) ||
(stricmp(m_csGroupLabel, "EOF") == 0) )
{
return(TRUE);
}
return(FALSE);
}

int CDxfIO12::SkipToRecord()
{
do
{
if (GetGroupAndLabel() != 0)
{
return(-1);
}

while (m_iGroup != 0)
{
if (GetGroupAndLabel() != 0)
{
return(-1);
}
}
} while (!IsRecord());

return(0);
}

int CDxfIO12:: GetLayerAndColor()
{
return 0;
}

int CDxfIO12:: FindSection(const char *SectionName, BOOL bReset)
{
do
{
if ((stricmp(SectionName, "BLOCKS") == 0) &&
(stricmp(m_csGroupLabel, "ENTITIES") == 0))
{
return(-1L); /* no BLOCKS section */
}

do
{
if (GetGroupAndLabel() != 0)
{
return(-1L);
}
} while ( (m_iGroup != 0) || (stricmp(m_csGroupLabel, "SECTION") != 0) );

if (GetGroupAndLabel() != 0)
{
return(-1L);
}
} while ( !( (m_iGroup == 2) && (stricmp(m_csGroupLabel, SectionName) == 0) ) );

return 0;
}

int CDxfIO12::SkipSeqEnd()
{
int error = 0;

while ((error == 0) && (stricmp(m_csGroupLabel, "SEQEND") != 0))
{
error = GetGroupAndLabel ();
}

/*
// move until next Group 0
if (error == 0)
{
do
{
error = GetGroupAndLabel ();
} while ((error == 0) && (m_iGroup != 0));
}
*/
return(error);
}

int CDxfIO12:: GetStartSection(CString &csSection)
{
int error=0;

return(error);
}

int CDxfIO12:: GetEndSection()
{
int error=0;

return(error);
}

int CDxfIO12:: GetStartBlock(CString &csBlockName)
{
int error=0;

return(error);
}

int CDxfIO12:: GetEndBlock()
{
int error=0;

return(error);
}


int CDxfIO12:: GetLocation(Vector &pt)
{
int error=0;

return(error);
}

int CDxfIO12:: GetPoint(Vector &pnt)
{
int error=0;

return(error);
}

int CDxfIO12:: GetLine(Vector &p1, Vector &p2)
{
int error=0;

return(error);
}

//得到折线数组,返回折线点列表 和点数
int CDxfIO12:: GetPolyline(Vector *&ptList, int &iNum)
{
int temp, iNumBuf = 0;
BOOL Closed;
Vector* pvBuf = NULL;

Closed = FALSE; /* reset for new line */

/* go through stuff before vertexes and check for closed poly flag */
while (stricmp(m_csGroupLabel, "VERTEX") != 0)
{
if (m_iGroup == 70)
{
temp = atoi(m_csGroupLabel);
if (0x0001 & temp)
Closed = TRUE;
}

//if (m_iGroup == 38)
// sscanf( m_csGroupLabel, "%lf", &z);

if (GetGroupAndLabel() != 0)
return(-1);
}

iNum = 0;
iNumBuf = ((iNum / 10) + 1) * 10;
pvBuf = new Vector [iNumBuf];
if (!pvBuf)
return -1;
memset (pvBuf, 0, iNumBuf * sizeof (Vector));

do
{
if (GetGroupAndLabel() != 0)
return(-1);
while (m_iGroup != 0)
{
switch (m_iGroup)
{
case 10 :
pvBuf[iNum].x = abs(atof(m_csGroupLabel)); //atof(m_csGroupLabel)
break;
case 20 :
//需计算Y轴的绝对值,因系统中坐标系与CAD中相反
pvBuf[iNum].y = abs(atof(m_csGroupLabel));
break;
case 30 :
case 38 :
break;
default :
break;
}

if (GetGroupAndLabel() != 0)
{
return(-1);
}
}
iNum ++;

if ((iNum+1) >= iNumBuf)
{
Vector *ptmp = NULL;
iNumBuf = ((iNum / 10 + 1) * 10);
ptmp = new Vector [iNumBuf];
if (!ptmp)
{
delete [] pvBuf;
return -1;
}

memcpy (ptmp, pvBuf, iNum * sizeof(Vector));
delete [] pvBuf;
pvBuf = ptmp;
}
} while (stricmp(m_csGroupLabel, "SEQEND") != 0);

ptList = new Vector [iNum + Closed];
if (NULL == ptList)
{
delete [] pvBuf;
return -1;
}
memcpy (ptList, pvBuf, iNum * sizeof (Vector));
delete [] pvBuf;

/* close up the poly */
if (Closed)
{
ptList [iNum] = ptList [0];
iNum ++;
}

/* move past SEQEND */
SkipSeqEnd();

return 0;
}

int CDxfIO12:: GetCircle(Vector &pc, float &rad)
{
int error=0;

return(error);
}

int CDxfIO12:: GetText(CString &csLabel, Vector &pt)
{
int error=0;

return(error);
}

(目前有0人发表看法,  我要发表评论
<<上一篇: 完整的图像扫描类     下一篇: 二值图像轮廓跟踪源码>>
我要评论:
  只有登录后才能评论!
评论者: 匿名游客    (立即登录 或 注册)