-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathU_ZPLFileHandling.pas
More file actions
225 lines (201 loc) · 7.51 KB
/
U_ZPLFileHandling.pas
File metadata and controls
225 lines (201 loc) · 7.51 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/// <summary>
/// Copyright Fabian Duron (C) 2024 OpenZPL Generator - File handling
/// used in OpenZPL Generator
/// </summary>
unit U_ZPLFileHandling;
interface
uses U_ZPLObjects, U_ZPLTypes, vcl.Controls;
function SaveToFile(zplDocument : TZPLDocument; FileName: String): Boolean;
function LoadFromFile(zplDocument : TZPLDocument; FileName: String ; var sMSG : String): Boolean;
implementation
uses xml.XMLDoc, XML.XMLIntf ;
function SaveToFile(zplDocument : TZPLDocument; FileName: String): Boolean;
function boolToStr(b : Boolean) : String;
begin
if b then
Result := 'True'
else
Result := 'False';
end;
var
RootNode: IXMLNode;
i: Integer;
XMLDocument1 : TXMLDocument ;
anObject : TZPLObject;
begin
XMLDocument1 := TXMLDocument.Create(nil);
XMLDocument1.Active := True;
XMLDocument1.Options := [doNodeAutoIndent];
try
XMLDocument1.Version := '1.0';
RootNode := XMLDocument1.AddChild('vZPLDocument');
with RootNode do
begin
Attributes['version'] := '1.2';
Attributes['start'] := zplDocument.StartCommand;
Attributes['end'] := zplDocument.EndCommand;
Attributes['DPI'] := zplDocument.DPI;
Attributes['printer_name'] := zplDocument.PrinterName;
Attributes['document_name'] := zplDocument.DocumentName;
Attributes['width'] := zplDocument.Width;
Attributes['height'] := zplDocument.Height;
Attributes['page_orientation'] := zplDocument.Orientation;
Attributes['LeftShift'] := zplDocument.LeftShift;
Attributes['TopShift'] := zplDocument.TopShift;
end;
with RootNode.AddChild('objects'), zplDocument do
for anObject in ZPLObjects do
begin
with AddChild('object') do //Adding an object to the XML list
begin
case anObject.ObjectKind of
okBarCode:
begin
Attributes['kind'] := 'B';
with anObject as TZPLBarcodeObject do
begin
Attributes['text'] := Text;
Attributes['variable'] := Variable;
Attributes['show_text'] := ShowText;
Attributes['type'] := BarcodeType;
Attributes['magnification'] := Magnification;
end;
end;
okForm:
begin
Attributes['kind'] := 'F';
with anObject as TZPLFormObject do
begin
Attributes['type'] := ShapeType;
Attributes['border'] := BorderThickness;
end;
end;
okLabel:
begin
Attributes['kind'] := 'T';
with anObject as TZPLTextObject do
begin
Attributes['font_code'] := FontCode; //Store just one character
Attributes['text'] := Text;
Attributes['variable'] := Variable;
end;
end;
end;
Attributes['left'] := anObject.Left;
Attributes['top'] := anObject.Top;
Attributes['width'] := anObject.Width;
Attributes['height'] := anObject.Height;
Attributes['scr_dpi'] := anObject.ScreenDPI;
Attributes['name'] := anObject.Name;
Attributes['locked'] := boolToStr(anObject.Locked) ;
Attributes['visible'] := boolToStr(anObject.isVisible) ;
end; //Adding an object to the XML list
end;
XMLDocument1.SaveToFile(FileName);
finally
XMLDocument1.Active := False;
end;
end;
function LoadFromFile(zplDocument : TZPLDocument; FileName: String; var sMSG : String): Boolean;
var
K : tZPLObjectKind;
RootNode, ObjectNode, CarPrice: IXMLNode;
XMLDocument1 : IXMLDocument ; //Use an Interface
XMLObjects, XMLObject : IXMLNode; //
anObject : TZPLObject;
knd : Char;
begin
Result := False;
XMLDocument1 := TXMLDocument.Create(nil);
XMLDocument1.LoadFromFile(FileName); //Load and activate
try
//Check header
RootNode := XMLDocument1.ChildNodes.FindNode('vZPLDocument');
if Assigned(RootNode) then //There is a vZPLDocument Child Node
begin
if RootNode.Attributes['version'] = '1.2' then
begin
with RootNode, zplDocument do
begin
StartCommand := Attributes['start'];
EndCommand := Attributes['end'];
DPI := Attributes['DPI'];
PrinterName := Attributes['printer_name'];
DocumentName := Attributes['document_name'];
Width := Attributes['width'];
Height := Attributes['height'];
Orientation := Attributes['page_orientation'];
LeftShift := Attributes['LeftShift'];
TopShift := Attributes['TopShift'];
end;
XMLObjects := RootNode.ChildNodes.FindNode('objects');
if Assigned(XMLObjects) then
begin
XMLObject := XMLObjects.ChildNodes.First;
while Assigned(XMLObject) do
with XMLObject do
begin
if NodeName = 'object' then
begin
//<object kind="T" font_code="0" text="Label" left="5" top="12" width="36" height="16" scr_dpi="96"/>
knd := string(Attributes['kind'])[1];
case knd of
'T' : k := okLabel;
'B' : k := okBarCode;
'F' : k := okForm;
end;
anObject := zplDocument.AddObject(k, False); //Agregar el objeto leído a la lista
anObject.Selected := False;
with anObject do
begin
Left := Attributes['left'];
top := Attributes['top'];
width := Attributes['width'];
height := Attributes['height'];
ScreenDPI := Attributes['scr_dpi'];
Name := Attributes['name'];
Locked := Attributes['locked'] = 'True';
isVisible := Attributes['visible'] = 'True';
end;
case anObject.ObjectKind of
okBarCode:
with anObject as TZPLBarcodeObject do
begin
Text := Attributes['text'];
Variable := Attributes['variable'];
ShowText := Attributes['show_text'];
BarcodeType := Attributes['type'];
Magnification := Attributes['magnification'];
end;
okForm:
with anObject as tZPLFormObject do
begin
ShapeType := Attributes['type'];
BorderThickness := Attributes['border'];
end;
okLabel:
with anObject as TZPLTextObject do
begin
FontCode := Attributes['font_code'];
Text := Attributes['text'];
Variable := Attributes['variable'];
end;
end;
end;
XMLObject := XMLObject.NextSibling;
end;
zplDocument.UnselectObjects;
zplDocument.RedrawHandles;
Result := True;
end
end
else
sMSG := 'The file is not a valid vZPL Label version';
end
else
sMSG := 'The file is not a valid vZPL label';
finally
XMLDocument1.Active := False;
end;
end;
end.