-
Notifications
You must be signed in to change notification settings - Fork 0
/
DxfReader.cs
161 lines (139 loc) · 4.79 KB
/
DxfReader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Dxf2UrScript
{
public class DxfReader
{
private static Dictionary<string, IParse> parseDictionary;
public StreamReader streamReader { get; private set; }
public string Code { get; private set; } // string1
public string Value { get; private set; } // string2
public long Counter { get; private set; } // номер строки следующей строки, шаг 2
private DxfReader(StreamReader streamReader)
{
this.Code = "";
this.Value = "";
this.Counter = 0;
this.streamReader = streamReader;
}
private DxfReader(DxfReader reader)
{
this.Code = reader.Code;
this.Value = reader.Value;
this.Counter = reader.Counter;
this.streamReader = reader.streamReader;
}
public static DxfReader Create (string path)
{
try
{
StreamReader streamReader = new StreamReader(path);
initParseDictionary();
return new DxfReader(streamReader);
}
catch
{
Console.WriteLine("Такой файл не существует");
return null;
}
}
public static DxfReader Clone (DxfReader reader)
{
return new DxfReader(reader);
}
private static void initParseDictionary()
{
parseDictionary = new Dictionary<string, IParse>();
parseDictionary.Add("LINE", new ParseLine());
// parseDictionary.Add("LWPOLYLINE", new ParsePolyLine()); // не понятны значимые коды
parseDictionary.Add("ARC", new ParseArc());
parseDictionary.Add("CIRCLE", new ParseCircle());
}
/// <summary>
/// Calling IParse implemintation to create a figure
/// </summary>
/// <param name="parseType"></param>
/// <param name="readerPosition"></param>
public static long CallParser(IParse parseType, DxfReader reader)
{
return parseType.ParseFigure(reader);
}
/// <summary>
/// Go to the Entity section
/// </summary>
private void InEntities() // private
{
do
{
GetLineCouple();
// Console.WriteLine(Code);
// Console.WriteLine(Value);
} while (
!(string.IsNullOrEmpty(this.Value) && string.IsNullOrEmpty(this.Code)) &&
!(this.Value.Equals("ENTITIES") && this.Code.Equals("2"))
);
}
/*
public void PrintAllFile()
{
string str = "";
while((str = this.streamReader.ReadLine()) != null)
{
Console.WriteLine(str);
}
streamReader.Close();
}
*/
/// <summary>
/// Get code line and value line from dxf file
/// </summary>
public void GetLineCouple() //private??
{
if (!string.IsNullOrEmpty(this.Code = this.streamReader.ReadLine()))
{
this.Code = this.Code.Trim();
}
if (!string.IsNullOrEmpty(this.Value = this.streamReader.ReadLine()))
{
this.Value = this.Value.Trim();
}
this.Counter += 2;
}
public void GoToLine(long number)
{
this.streamReader.DiscardBufferedData();
this.streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
this.streamReader.BaseStream.Position = 0;
this.Counter = 0;
for (int i=0; i<number; i++)
{
this.streamReader.ReadLine();
this.Counter++;
}
}
public void ParseFigures()
{
InEntities();
do
{
GetLineCouple();
if (!string.IsNullOrEmpty(this.Value) || !string.IsNullOrEmpty(this.Code))
{
if (parseDictionary.ContainsKey(this.Value))
{
long temp = CallParser(parseDictionary[this.Value], this);
this.GoToLine(temp);
}
}
} while (
!(string.IsNullOrEmpty(this.Value) && string.IsNullOrEmpty(this.Code)) &&
!(this.Value.Equals("ENDSEC") && this.Code.Equals("0"))
);
streamReader.Close();
}
}
}