Repository navigation
Expand file tree
/
Copy pathAutoDXFExtract.lpr
More file actions
223 lines (193 loc) · 5.24 KB
/
Copy pathAutoDXFExtract.lpr
File metadata and controls
223 lines (193 loc) · 5.24 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
program AutoDXFExtract;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes, SysUtils, CustApp,
{ you can add units after this }
{$IFDEF XMLVersionLaz2}
laz2_XMLRead, laz2_XMLWrite, laz2_DOM,
{$ELSE}
XMLRead, XMLWrite, DOM
{$endif}
;
type
{ TAutoDXFExtract }
TAutoDXFExtract = class(TCustomApplication)
protected
fFile: TXMLDocument;
fFileName: string;
fFileValid: boolean;
procedure DoRun; override;
procedure XMLErrorHandler(E: EXMLReadError);
function getLayer(id: integer): TDOMNode;
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
procedure WriteHelp; virtual;
end;
{ TAutoDXFExtract }
function ExtractFileNameEX(const AFileName:String): String;
var
I: integer;
begin
I := LastDelimiter('.'+PathDelim+DriveDelim,AFileName);
if (I=0) or (AFileName[I] <> '.')
then
I := MaxInt;
Result := ExtractFileName(Copy(AFileName,1,I-1));
end;
procedure TAutoDXFExtract.DoRun;
var
ErrorMsg: String;
Parser: TDOMParser;
Src: TXMLInputSource;
Input: TFileStream;
i: integer;
ThisLayer: TDOMNode;
done: boolean;
thisID, thisName: string;
outFilePath: string;
outfile: TextFile;
allnames: string;
groupType: string;
justName: string;
pathToInput: string;
scale: string;
begin
// quick check parameters
ErrorMsg:=CheckOptions('hiops',['help','input','output','path', 'scale']);
if ErrorMsg<>'' then begin
ShowException(Exception.Create(ErrorMsg));
Terminate;
Exit;
end;
// parse parameters
if HasOption('h','help') then begin
WriteHelp;
Terminate;
Exit;
end;
{ add your program here }
if not HasOption('i','input') then begin
WriteHelp;
Terminate;
Exit;
end;
if not HasOption('o','output') then begin
WriteLn('Default output used "dxf_Makefile"');
outFilePath := 'dxf_Makefile';
end else
begin
outFilePath := GetOptionValue('o', 'output');
end;
pathToInput := '../svg_files';
if HasOption('p','path') then begin
pathToInput:=GetOptionValue('p','path');
end;
AssignFile(outFile, outFilePath);
Rewrite(outFile); // creating the file
fFileName := GetOptionValue('i', 'input');
justName := ExtractFileNameEx(fFileName);
if fileExists(fFileName) then
begin
try
fFileValid := true;
// create a parser object
Parser := TDOMParser.Create;
// get the actual file from disk
Input := TFileStream.Create(fFileName, fmOpenRead);
// create the input source
Src := TXMLInputSource.Create(Input);
// we want validation
Parser.Options.Validate := True;
// assign a error handler which will receive notifications
Parser.OnError := @XMLErrorHandler;
// now do the job
Parser.Parse(Src, fFile);
except
end;
Input.free;
Parser.Free;
Src.Free;
end else begin
WriteLn('Oh no, input file no good!');
fFileValid := false;
Terminate;
Exit;
end;
//we have an input
i := 0;
done := false;
WriteLn('Everything looks good, writing makefile to "', outFilePath, '"');
allnames := '';
repeat begin
ThisLayer := getLayer(i);
if Assigned(ThisLayer) then begin
groupType := TDOMElement(ThisLayer).GetAttribute('inkscape:groupmode');
if groupType = 'layer' then begin
thisID := TDOMElement(ThisLayer).GetAttribute('id');
thisName := TDOMElement(ThisLayer).GetAttribute('inkscape:label');
WriteLn('Found layer "', thisID, '", Layer is "', thisName, '".');
allnames := allnames + justname + '_' + thisName + '.dxf ';
Writeln(outFile, '%_' + thisName + '.eps: ' + pathToInput + '/%.svg' + #10+
#9+'inkscape -E $@ $< --export-id=' + thisID + #10 + #10);
end;
end else begin
done := true;
end;
i := i+1;
end until done;
scale := '1';
if HasOption('s','scale') then begin
scale:=GetOptionValue('s','scale');
end;
Writeln(outFile, '%.dxf: %.eps' +#10 +
#9+'pstoedit -xscale ' + scale + ' -yscale ' + scale + ' -dt -f dxf:-polyaslines $< $@' +#10+#10);
Writeln(outFile, 'all: ' + allnames);
CloseFile(outFile);
// stop program loop
Terminate;
end;
procedure TAutoDXFExtract.XMLErrorHandler(E: EXMLReadError);
begin
if (E.Severity = esError) or (E.Severity = esFatal) then
fFileValid := false; //There was an error, file is no longer valid
end;
function TAutoDXFExtract.getLayer(id: integer): TDOMNode;
var layers: TDOMNodeList;
layerExists : boolean;
begin
layers := fFile.GetElementsByTagName('g');
layerExists := layers.count > id;
if layerExists then begin
result := layers.Item[id];
end else
begin
result := nil;
end;
layers.Free;
end;
constructor TAutoDXFExtract.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
StopOnException:=True;
end;
destructor TAutoDXFExtract.Destroy;
begin
inherited Destroy;
end;
procedure TAutoDXFExtract.WriteHelp;
begin
{ add your help code here }
writeln('Usage: ',ExeName,' -i <input_file> -o <output_file>');
end;
var
Application: TAutoDXFExtract;
begin
Application:=TAutoDXFExtract.Create(nil);
Application.Title:='AutoDXFExtract';
Application.Run;
Application.Free;
end.