-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCplConverter.cs
186 lines (151 loc) · 5.2 KB
/
CplConverter.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Reflection;
using GemBox.Spreadsheet;
using System.Runtime.Versioning;
using System.Windows.Forms;
namespace CPL_Converter
{
class CplConverter
{
private String FilePath = "";
private StreamReader sr;
private int ColumnCount = 0;
private String[] ColumnNames = { "Designator", "Layer", "Mid X", "Mid Y", "Rotation" };
private bool AutoSave = false;
public CplConverter(String OutFileName)
{
FilePath = OutFileName;
}
// Разрешение автосохранения
public void AutoSaveEnable()
{
AutoSave = true;
}
// Запрещение автосохранения
public void AutoSaveDisable()
{
AutoSave = false;
}
// Функция рассчета числа колонок в текстовом файле
private int GetColumnCount(String Input)
{
int Result = 0;
// Разделение строки по пробелам
String[] Row = Input.Split(' ');
for (int i = 0; i < Row.Length; i++)
{
if (Row[i] != "")
{
Result++;
}
}
return Result;
}
// Функция исправления слов в строке
private String LineCorrection(String InputStr)
{
InputStr = InputStr.Replace("TopLayer", "Top");
InputStr = InputStr.Replace("BottomLayer", "Bottom");
return InputStr;
}
//Функция выделения данных из строки
private String[] ParseRow(String Input)
{
// Разделение строки по пробелам
String[] Row = Input.Split(' ');
String[] Result = new string[ColumnCount];
int ColPos = 0;
// Выделение информации из строки
for (int i = 0; i < Row.Length; i++)
{
if (Row[i] != "")
{
if (ColPos < ColumnCount)
{
Result[ColPos] = Row[i];
ColPos++;
}
}
}
return Result;
}
[SupportedOSPlatform("windows")]
// Сохранение данных в MS Excel
private int OutInExcel(StreamReader inputSr)
{
String Line;
String[] RowData;
int RowCount = 2;
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
ExcelFile workbook = new ExcelFile();
var worksheet = workbook.Worksheets.Add("List1");
// Заполнение шапки таблицы
for (int i = 1; i <= ColumnNames.Length; i++)
{
worksheet.Cells[0, i - 1].Value = ColumnNames[i - 1];
}
// Заполнение данными
while ((Line = inputSr.ReadLine()) != null)
{
Line = LineCorrection(Line);
RowData = ParseRow(Line);
for (int i = 1; i <= ColumnCount; i++)
{
worksheet.Cells[RowCount - 1, i - 1].Value = RowData[i - 1];
}
RowCount++;
}
// Сохранение данных
if (AutoSave)
{
workbook.Save(FilePath);
}
else
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Excel files (*.xlsx)|*.xlsx";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.Title = "Путь для сохранения файла";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
workbook.Save(saveFileDialog1.FileName);
}
else
{
}
}
// Возврат количество обработанных строк
return RowCount - 2;
}
[SupportedOSPlatform("windows")]
public int HandleCPL(String InputFileName)
{
int RowCount = 0;
sr = new StreamReader(InputFileName);
String Line;
ColumnCount = 0;
// Поиск данных о расположении компонентов
while ((Line = sr.ReadLine()) != null)
{
if (Line.Contains("Designator"))
{
ColumnCount = GetColumnCount(Line);
break;
}
}
// Если удалось выделить колонки
if (ColumnCount > 0)
{
RowCount = OutInExcel(sr);
}
sr.Close();
return RowCount;
}
}
}