0% found this document useful (0 votes)
18 views

Java Part1

The document discusses parsing a DXF file. It defines classes and enums to represent DXF sections, groups and variables. The program reads a DXF file line by line and uses the classes to parse each element and check for errors.

Uploaded by

ciyopo3479
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Java Part1

The document discusses parsing a DXF file. It defines classes and enums to represent DXF sections, groups and variables. The program reads a DXF file line by line and uses the classes to parse each element and check for errors.

Uploaded by

ciyopo3479
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DXF_Parser
{
class Program
{
enum State
{
SECTION_START,
SECTION_CONTINUE,
EOF,
NONE
}
const string FILENAME = @"c:\temp\test1.txt";
static void Main(string[] args)
{
StreamReader reader = new StreamReader(FILENAME);
State state = State.SECTION_START;
Section section = new Section();
string line = "";
Result result = null;
int lineCount = 0;
while ((state != State.EOF) && (line = reader.ReadLine()) != null)
{
line = line.Trim();
lineCount++;
//if (line.Length > 0)
//{
switch (state)
{
case State.SECTION_START:
//section = new Section();
//section.AddSection();
result = section.Parse(line);
if (result.status != "OK") Error(lineCount, line,
result);
state = State.SECTION_CONTINUE;
break;

case State.SECTION_CONTINUE:
if (line == "EOF")
{
state = State.EOF;
}
else
{
result = section.Parse(line);
switch (result.status)
{
case "OK":
break;
case "ENDSEC":
state = State.SECTION_START;
section.row = 1;
break;
default:
Error(lineCount, line, result);
break;
}
}
break;
}
//}
}
Console.Write("Press Return");
Console.ReadLine();
}
static void Error(int lineCount, string line, Result error)
{
Console.WriteLine("Error : {0}, Line Number : {1}, Line : {2}",
error.message, lineCount, line);
}

}
public class Result
{
public string status { get; set; }
public string message { get; set; }
}
public enum GROUP_CODE
{
ENTITY_TYPE = 0,
PRIMARY_TEXT_VALUE = 1,
NAME = 2,
TEXT = 3,
ENTITY_HANDLE = 5,
LINE_TYPE = 6,
TEXT_STYLE_NAME = 7,
LAYER_NAME = 8,
DXF = 9,
PRIMARY_POINT = 10,
PRIMARY_POINT_CORNER = 11,
PRIMARY_POINT_2 = 12,
PRIMARY_POINT_3 = 13,
PRIMARY_POINT_4 = 14,
PRIMARY_POINT_5 = 15,
PRIMARY_POINT_6 = 16,
PRIMARY_POINT_7 = 17,
PRIMARY_POINT_8 = 18,
PRIMARY_POINT_9 = 19,
Y_VALUE = 20,
Y_VALUE_CORNER = 21,
Y_VALUE_2 = 22,
Y_VALUE_3 = 23,
Y_VALUE_4 = 24,
Y_VALUE_5 = 25,
Y_VALUE_6 = 26,
Y_VALUE_7 = 27,
Y_VALUE_8 = 28,
Y_VALUE_9 = 29,
Z_VALUE = 30,
Z_VALUE_CORNER = 31,
Z_VALUE_2 = 32,
Z_VALUE_3 = 33,
Z_VALUE_4 = 34,
Z_VALUE_5 = 35,
Z_VALUE_6 = 36,
Z_VALUE_7 = 37,
Z_VALUE_8 = 38,
Z_VALUE_9 = 39,
DOUBLE_PRECISION = 40,
DOUBLE_PRECISION_1 = 41,
DOUBLE_PRECISION_2 = 42,
DOUBLE_PRECISION_3 = 43,
DOUBLE_PRECISION_4 = 44,
LINE_TYPE_SCALE = 48,
ANGLE = 50,
ANGLE_1 = 51,
COLOR_NUMBER = 62,
NOT_IN_SPECIFICATION = 65,
INT_VALUE = 70,
INT_VALUE_1 = 71,
INT_VALUE_2 = 72,
INT_VALUE_3 = 73,
INT_VALUE_4 = 74,
INT_VALUE_5 = 75,
INT_VALUE_6 = 76,
INT_VALUE_7 = 77,
INT_VALUE_8 = 78,
INT_VALUE_9 = 79,
PROXY_CAPABILITY_FLAG = 90,
SUBCLASS_MARKER = 100,
UCF_X = 110,
UCF_X1 = 111,
UCF_X2 = 112,
UCF_Y = 120,
UCF_Y1 = 121,
UCF_Y2 = 122,
UCF_Z = 130,
UCF_Z1 = 131,
UCF_Z2 = 132,

DOUBLE_PRECISION_POINT = 140,
DOUBLE_PRECISION_POINT_1 = 141,
DOUBLE_PRECISION_POINT_2 = 142,
DOUBLE_PRECISION_POINT_3 = 143,
DOUBLE_PRECISION_POINT_4 = 144,
DOUBLE_PRECISION_POINT_5 = 145,
DOUBLE_PRECISION_POINT_6 = 146,
DOUBLE_PRECISION_POINT_7 = 147,
DOUBLE_PRECISION_POINT_8 = 148,
DOUBLE_PRECISION_POINT_9 = 149,

INSTANCE_COUNT = 91,
INT_16 = 280,
INT_16_1 = 281,
BOOLEAN = 290,
SOFT_POINTER_HANDLE = 330,
HARD_POINTER_HANDLE_7 = 347,
LINE_WEIGHT = 370,
PLOT_STYLE =380
}
public enum READ_STATE
{
GET_CREATE_SECTION,
GET_OBJECT_TYPE,
GET_OBJECT,
GET_GROUP_CODE,
GET_LAYER,
GET_LAYER_NAME,
GET_SECTION_DATA,
GET_VARIABLE_NAME,
GET_VARIABLE_TYPE,
GET_VARIABLE_VALUES,
NONE
}
public class Section
{
public static List<Section> sections = new List<Section>();
GROUP_CODE currentGroupCode { get; set; }
Section currentSection { get; set; }
public int row = 1;
string sectionName { get; set; }
READ_STATE readState = READ_STATE.NONE;
public int numberValues { get; set; }
public int valueCounter = 0;

public void AddSection()


{
sections.Add(this);
}
public Result Parse(string line)
{
Result result = new Result() { status = "OK" };
int number = 0;

switch (row)
{
case 1:
number = int.Parse(line);
if (number != (int)GROUP_CODE.ENTITY_TYPE)
{
result.status = "ERROR";
result.message = "Bad Section Code";
}
break;
case 2:
switch (line)
{
case "SECTION" :
break;
case "EOF" :
result.status = "EOF";
break;
default :
result.status = "ERROR";
result.message = "Expecting Section";
break;
}
break;

case 3:
number = int.Parse(line);
if (number != (int)GROUP_CODE.NAME)
{
result.status = "ERROR";
result.message = "Bad Group Code";
}
break;
case 4:
sectionName = line;
readState = READ_STATE.GET_CREATE_SECTION;
break;
default:
if (readState == READ_STATE.GET_CREATE_SECTION)
{
switch (sectionName)
{
case "HEADER":
currentSection = new Header();
break;
case "ENTITIES":
currentSection = new Entities();
break;
case "CLASSES":
currentSection = new Class();
break;
case "TABLES":
currentSection = new Table();
break;
default :
break;
}
currentSection.sectionName = sectionName;
Section.sections.Add(currentSection);
readState = READ_STATE.GET_SECTION_DATA;
}
switch (sectionName)
{
case "HEADER":
result = ((Header)currentSection).HeaderParse(line);
break;
case "ENTITIES":
result = ((Entities)currentSection).EntityParse(line);
break;
case "CLASSES":
result = ((Class)currentSection).ClassParse(line);
break;
case "TABLES":
result = ((Table)currentSection).TableParse(line);
break;
default :
break;
}
break;

}
row++;

return result;
}
}
public class Header : Section
{
public GROUP_CODE groupCode { get; set; }

GROUP_CODE currentGroupCode { get; set; }


public Dictionary<string, DXF_Header_Variable> dictHeaderVariables;
DXF_Header_Variable currentVariable = null;
READ_STATE readState = READ_STATE.GET_GROUP_CODE;

public Result HeaderParse(string line)


{

Result result = new Result() { status = "OK" };


switch (readState)
{
case READ_STATE.GET_GROUP_CODE:
currentGroupCode = (GROUP_CODE)Enum.Parse(typeof(GROUP_CODE),
line);
readState = READ_STATE.GET_VARIABLE_NAME;
break;
case READ_STATE.GET_VARIABLE_NAME:
if (line == "ENDSEC")
{
result.status = line;
}
else
{
if (dictHeaderVariables == null) dictHeaderVariables = new
Dictionary<string, DXF_Header_Variable>();
currentVariable = new DXF_Header_Variable();
dictHeaderVariables.Add(line, currentVariable);
currentVariable.name = line;
readState = READ_STATE.GET_VARIABLE_TYPE;
}
break;
case READ_STATE.GET_VARIABLE_TYPE:
groupCode = (GROUP_CODE)Enum.Parse(typeof(GROUP_CODE), line);
switch (groupCode)
{
case GROUP_CODE.ENTITY_TYPE :
readState = READ_STATE.GET_VARIABLE_NAME;
break;
case GROUP_CODE.DXF:
readState = READ_STATE.GET_VARIABLE_NAME;
break;
default:
readState = READ_STATE.GET_VARIABLE_VALUES;
break;
}
break;
case READ_STATE.GET_VARIABLE_VALUES:
switch (groupCode)
{
case GROUP_CODE.ANGLE:
currentVariable.angle = double.Parse(line);
break;
case GROUP_CODE.BOOLEAN :
currentVariable.logic = (line == "0")? false : true;
break;
case GROUP_CODE.COLOR_NUMBER:
currentVariable.intValue = int.Parse(line);
break;
case GROUP_CODE.DOUBLE_PRECISION:
currentVariable.primaryPoint = double.Parse(line);
break;
case GROUP_CODE.ENTITY_HANDLE:
currentVariable.intValue = int.Parse(line);
break;
case GROUP_CODE.HARD_POINTER_HANDLE_7:
currentVariable.text = line;
break;
case GROUP_CODE.INT_VALUE:
currentVariable.intValue = int.Parse(line);
break;
case GROUP_CODE.INT_16:
currentVariable.intValue = int.Parse(line);
break;
case GROUP_CODE.LINE_TYPE:
currentVariable.text = line;
break;
case GROUP_CODE.LAYER_NAME:
currentVariable.intValue = int.Parse(line);
break;
case GROUP_CODE.LINE_WEIGHT:
currentVariable.intValue = int.Parse(line);
break;
case GROUP_CODE.NAME:
currentVariable.text = line;
break;
case GROUP_CODE.PLOT_STYLE:
currentVariable.intValue = int.Parse(line);
break;
case GROUP_CODE.PRIMARY_POINT:
currentVariable.primaryPoint = double.Parse(line);
break;
case GROUP_CODE.PRIMARY_TEXT_VALUE:
currentVariable.primaryTextValue = line;
break;
case GROUP_CODE.TEXT:
currentVariable.text = line;
break;
case GROUP_CODE.TEXT_STYLE_NAME:
currentVariable.text = line;
break;
case GROUP_CODE.Y_VALUE:
currentVariable.y = double.Parse(line);
break;
case GROUP_CODE.Z_VALUE:
currentVariable.z = double.Parse(line);
break;
default :
break;
}
readState = READ_STATE.GET_VARIABLE_TYPE;
break;
}

return result;
}
}
public class DXF_Header_Variable
{
public double angle { get; set; }
public double primaryPoint { get; set; }
public double y { get; set; }
public double z { get; set; }
public string primaryTextValue { get; set; }
public string name { get; set; }
public int intValue { get; set; }
public string text { get; set; }
public Boolean logic { get; set; }
}
public class Entities : Section
{
public GROUP_CODE groupCode { get; set; }
GROUP_CODE currentGroupCode { get; set; }
public List<Entity> entities;
Entity currentEntity = null;
READ_STATE readState = READ_STATE.GET_GROUP_CODE;

public Result EntityParse(string line)


{

Result result = new Result() { status = "OK" };


switch (readState)
{
case READ_STATE.GET_GROUP_CODE:
currentGroupCode = (GROUP_CODE)Enum.Parse(typeof(GROUP_CODE),
line);
readState = READ_STATE.GET_VARIABLE_NAME;
break;
case READ_STATE.GET_VARIABLE_NAME:
if (line == "ENDSEC")
{
result.status = line;
}
else
{

if (entities == null) entities = new List<Entity>();


currentEntity = new Entity();
entities.Add(currentEntity);
currentEntity.name = line;
readState = READ_STATE.GET_LAYER;
}
break;
case READ_STATE.GET_LAYER:
groupCode = (GROUP_CODE)Enum.Parse(typeof(GROUP_CODE), line);
readState = READ_STATE.GET_LAYER_NAME;
break;
case READ_STATE.GET_LAYER_NAME:
currentEntity.layerName = int.Parse(line);
readState = READ_STATE.GET_VARIABLE_TYPE;
break;
case READ_STATE.GET_VARIABLE_TYPE:
groupCode = (GROUP_CODE)Enum.Parse(typeof(GROUP_CODE), line);
if (groupCode == GROUP_CODE.ENTITY_TYPE)
{
readState = READ_STATE.GET_VARIABLE_NAME;
}
else
{
readState = READ_STATE.GET_VARIABLE_VALUES;
}
break;
case READ_STATE.GET_VARIABLE_VALUES:
double value = double.Parse(line);
switch (groupCode)
{
case GROUP_CODE.PRIMARY_POINT :
currentEntity.primaryPoint = value;
break;
case GROUP_CODE.Y_VALUE:
currentEntity.y = value;
break;
case GROUP_CODE.Z_VALUE:
currentEntity.z = value;
break;

case GROUP_CODE.PRIMARY_POINT_CORNER :
currentEntity.primaryPoint = value;
break;
case GROUP_CODE.Y_VALUE_CORNER :
currentEntity.yCorner = value;
break;
case GROUP_CODE.Z_VALUE_CORNER:
currentEntity.zCorner = value;
break;
}

readState = READ_STATE.GET_VARIABLE_TYPE;
break;
}

return result;
}
}
public class Entity
{
public double primaryPoint { get; set; }
public double y { get; set; }
public double z { get; set; }
public double primaryPointCorner { get; set; }
public double yCorner { get; set; }
public double zCorner { get; set; }
public string name { get; set; }
public int layerName { get; set; }
}
public class Class : Section
{
public Dictionary<string, Class_Type> dictClassVariables;
Class_Type currentClass = null;
GROUP_CODE currentGroupCode { get; set; }
string className { get; set; }
READ_STATE readState = READ_STATE.GET_GROUP_CODE;

public Result ClassParse(string line)


{

Result result = new Result() { status = "OK" };


switch (readState)
{
case READ_STATE.GET_GROUP_CODE:
currentGroupCode = (GROUP_CODE)Enum.Parse(typeof(GROUP_CODE),
line);
readState = READ_STATE.GET_VARIABLE_NAME;
break;
case READ_STATE.GET_VARIABLE_NAME:
if (line == "ENDSEC")
{
result.status = line;
}
else
{
readState = READ_STATE.GET_VARIABLE_TYPE;
}
break;
case READ_STATE.GET_VARIABLE_TYPE:
currentGroupCode = (GROUP_CODE)Enum.Parse(typeof(GROUP_CODE),
line);
switch (currentGroupCode)
{
case GROUP_CODE.ENTITY_TYPE:
readState = READ_STATE.GET_VARIABLE_NAME;
break;
default:
readState = READ_STATE.GET_VARIABLE_VALUES;
break;
}
break;
case READ_STATE.GET_VARIABLE_VALUES:
switch (currentGroupCode)
{
case GROUP_CODE.PRIMARY_TEXT_VALUE :
if (dictClassVariables == null) dictClassVariables = new
Dictionary<string,Class_Type>();
currentClass = new Class_Type();
dictClassVariables.Add(line, currentClass);
currentClass.dxfName = line;
break;

case GROUP_CODE.NAME:
currentClass.className = line;
break;

case GROUP_CODE.TEXT:
currentClass.applicationName = line;
break;

case GROUP_CODE.PROXY_CAPABILITY_FLAG:
currentClass.proxyCapabilityFlag = uint.Parse(line);
break;

case GROUP_CODE.INSTANCE_COUNT:
currentClass.instance = int.Parse(line);
break;

case GROUP_CODE.INT_16:
currentClass.wasAProxyFlag = (line == "0") ? false :
true;
break;
case GROUP_CODE.INT_16_1:
currentClass.isAnEntityFlag = (line == "0") ? false :
true;
break;

default:
break;
}
readState = READ_STATE.GET_VARIABLE_TYPE;
break;
}

return result;
}
}
public class Class_Type
{
public string dxfName { get; set; }
public string className { get; set; }
public string applicationName { get; set; }
public uint proxyCapabilityFlag { get; set; }
public int instance { get; set; }
public Boolean wasAProxyFlag { get; set; }
public Boolean isAnEntityFlag { get; set; }

You might also like