//--------------------------------------------------
#include <Classes.hpp>
#include <stdio.h>
#include <Forms.hpp>
#include "dxfImport.h"
#include "StdioFile.h"
DxfImport::DxfImport()
{
}
double DxfImport::StringToDouble(AnsiString value)
{
char *tempbuff;
int len, i, dotpos;
double ret;
len = value.Length();
tempbuff = new char[len+1];
strcpy(tempbuff, value.c_str());
if (*tempbuff == '-' || *tempbuff == '+' || *tempbuff == '.')
i = 1;
else
i = 0;
for (; i<len; i++)
{
if ( !isdigit(*(tempbuff + i)) && *(tempbuff + i) != '.' )
{
delete tempbuff;
return 0;
}
}
try
{
ret = atof(tempbuff);
}
catch(Exception &e)
{
ret = 0;
}
delete tempbuff;
return ret;
}
AnsiString DxfImport::kVal(KeySet * keyset, AnsiString Key )
{
int i;
for(i=0; i<keyset->Count(); i++)
if(Key == keyset->Keys[i]->Key)
return keyset->Keys[i]->Value;
return "";
}
int DxfImport::FindLayer(AnsiString Name )
{
int i;
for(i=0;i<Layer.Count();i++)
if(Name == Layer.Layers[i]->Name)
return i;
return -1;
}
bool DxfImport::IsDXFCommand(AnsiString InText )
{
if(InText=="POINT"||InText=="LINE"||InText=="POLYLINE"||InText=="CIRCLE"||InText=="ARC" || InText=="TEXT" || InText=="INSERT")
return true;
else
return false;
}
bool DxfImport::PrepareDXFBlock( DataSet * MyKey, DxfCadBlock *block)
{
int p=0;
bool isclosed=false;
char tempbuff[200];
if(MyKey->Type=="POINT")
{
}
else if(MyKey->Type=="LINE")
{
DxfCadLine line;
line.P1.X = StringToDouble(kVal(&(MyKey->KeySets),"10"));
line.P1.Y = StringToDouble(kVal(&(MyKey->KeySets),"20"));
line.P2.X = StringToDouble(kVal(&(MyKey->KeySets),"11"));
line.P2.Y = StringToDouble(kVal(&(MyKey->KeySets),"21"));
if (!block->AddGraph(&line))
{
sprintf(tempbuff, "BlockElements超过最大限值:%d", DXF_BLOCK_MAX_ELEMENTS);
Application->MessageBox(tempbuff, "Dxf转换错误", MB_OK);
return false;
}
}
else if(MyKey->Type=="ARC")
{
DxfCadArc arc;
arc.Center.X = StringToDouble(kVal(&(MyKey->KeySets),"10"));
arc.Center.Y = StringToDouble(kVal(&(MyKey->KeySets),"20"));
arc.Radius = StringToDouble(kVal(&(MyKey->KeySets),"40"));
arc.Angle1 = StringToDouble(kVal(&(MyKey->KeySets),"50"));
arc.Angle2 = StringToDouble(kVal(&(MyKey->KeySets),"51"));
if (!block->AddGraph(&arc))
{
sprintf(tempbuff, "BlockElements超过最大限值:%d", DXF_BLOCK_MAX_ELEMENTS);
Application->MessageBox(tempbuff, "Dxf转换错误", MB_OK);
return false;
}
}
else if(MyKey->Type=="CIRCLE")
{
DxfCadArc circle;
circle.Center.X= StringToDouble(kVal(&(MyKey->KeySets),"10"));
circle.Center.Y= StringToDouble(kVal(&(MyKey->KeySets),"20"));
circle.Radius = StringToDouble(kVal(&(MyKey->KeySets),"40"));
circle.Angle1=0.0;
circle.Angle2=360.0;
if (!block->AddGraph(&circle))
{
sprintf(tempbuff, "BlockElements超过最大限值:%d", DXF_BLOCK_MAX_ELEMENTS);
Application->MessageBox(tempbuff, "Dxf转换错误", MB_OK);
return false;
}
}
else if(MyKey->Type=="POLYLINE")
{
DxfCadPolyLine poly;
int index=0;
while(stricmp(MyKey->KeySets.Keys[index]->Value, "VERTEX") && index < MyKey->KeySets.Count()-1)
{
if(MyKey->KeySets.Keys[index]->Key !="70")
{
int tempValue;
tempValue = StringToDouble(MyKey->KeySets.Keys[index]->Value);
if( tempValue ==1 )
{
isclosed=true;
break;
}
}
index++;
}
while (p < MyKey->KeySets.Count()-1)
{
while( stricmp(MyKey->KeySets.Keys[p]->Value, "VERTEX") && ( p < MyKey->KeySets.Count()-1))
p=p+1;
if(p<MyKey->KeySets.Count()-1)
{
while(stricmp(MyKey->KeySets.Keys[p]->Key, "10") && (p<MyKey->KeySets.Count()-1))
p=p+1;
tagCadPoint tempoint;
tempoint.X = StringToDouble(MyKey->KeySets.Keys[p]->Value);
while(stricmp(MyKey->KeySets.Keys[p]->Key, "20") && (p<MyKey->KeySets.Count()-1))
p=p+1;
tempoint.Y = StringToDouble(MyKey->KeySets.Keys[p]->Value);
if (!poly.Push(&tempoint))
{
char tempbuff[200];
sprintf(tempbuff, "Polyline超过最大点限值:%d", DXF_MAX_POINTS);
Application->MessageBox(tempbuff, "Dxf转换错误", MB_OK);
return false;
}
while(stricmp(MyKey->KeySets.Keys[p]->Key, "0") && (p<MyKey->KeySets.Count()-1))
p=p+1;
}
}
if(isclosed==true && poly.m_iPoints > 0 )
{
tagCadPoint tempoint;
tempoint.X = poly.Points[0]->X;
tempoint.Y = poly.Points[0]->Y;
if (!poly.Push(&tempoint))
{
char tempbuff[200];
sprintf(tempbuff, "Polyline超过最大点限值:%d", DXF_MAX_POINTS);
Application->MessageBox(tempbuff, "Dxf转换错误", MB_OK);
return false;
}
}
if (!block->AddGraph(&poly))
{
sprintf(tempbuff, "BlockElements超过最大限值:%d", DXF_BLOCK_MAX_ELEMENTS);
Application->MessageBox(tempbuff, "Dxf转换错误", MB_OK);
return false;
}
}
return true;
}
bool DxfImport::PrepareDXFEntity( DataSet * MyKey)
{
long layerNo=0;
long p=0;
bool isclosed=false;
char tempbuff[200];
DxfCadLine line;
DxfCadArc arc;
DxfCadArc circle;
DxfCadText text;
DxfCadPolyLine poly;
layerNo =FindLayer(kVal(&(MyKey->KeySets),"8"));
if (layerNo < 0) layerNo=0;
if(MyKey->Type=="POINT")
{
}
else if(MyKey->Type=="LINE")
{
line.LayerNo = layerNo;
line.P1.X = StringToDouble(kVal(&(MyKey->KeySets),"10"));
line.P1.Y = StringToDouble(kVal(&(MyKey->KeySets),"20"));
line.P2.X = StringToDouble(kVal(&(MyKey->KeySets),"11"));
line.P2.Y = StringToDouble(kVal(&(MyKey->KeySets),"21"));
if (!Graph->AddGraph(&line))
{
sprintf(tempbuff, "Graph超过最大限值:%d", DXF_MAX_GRAPHS);
Application->MessageBox(tempbuff, "Dxf转换错误", MB_OK);
return false;
}
}
else if(MyKey->Type=="ARC")
{
arc.LayerNo = layerNo;
arc.Center.X = StringToDouble(kVal(&(MyKey->KeySets),"10"));
arc.Center.Y = StringToDouble(kVal(&(MyKey->KeySets),"20"));
arc.Radius = StringToDouble(kVal(&(MyKey->KeySets),"40"));
arc.Angle1 = StringToDouble(kVal(&(MyKey->KeySets),"50"));
arc.Angle2 = StringToDouble(kVal(&(MyKey->KeySets),"51"));
if (!Graph->AddGraph(&arc))
{
sprintf(tempbuff, "Graph超过最大限值:%d", DXF_MAX_GRAPHS);
Application->MessageBox(tempbuff, "Dxf转换错误", MB_OK);
return false;
}
}
else if(MyKey->Type=="CIRCLE")
{
circle.LayerNo = layerNo;
circle.Center.X= StringToDouble(kVal(&(MyKey->KeySets),"10"));
circle.Center.Y= StringToDouble(kVal(&(MyKey->KeySets),"20"));
circle.Radius = StringToDouble(kVal(&(MyKey->KeySets),"40"));
circle.Angle1=0.0;
circle.Angle2=360.0;
if (!Graph->AddGraph(&circle))
{
sprintf(tempbuff, "Graph超过最大限值:%d", DXF_MAX_GRAPHS);
Application->MessageBox(tempbuff, "Dxf转换错误", MB_OK);
return false;
}
}
else if(MyKey->Type=="TEXT")
{
text.LayerNo = layerNo;
text.StartPoint.X= StringToDouble(kVal(&(MyKey->KeySets),"10"));
text.Star
评论1