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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
/** \file dxfformat.c
* Formating of DXF commands and parameters
*/
/* XTrkCad - Model Railroad CAD
* Copyright (C)2017 Martin Fischer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <dynstring.h>
#include "dxfformat.h"
extern char *sProdNameUpper;
extern long units; /**< meaning is 0 = English, 1 = metric */
static char *dxfDimensionDefaults[][3] = { /**< default values for dimensions, English, metric and DXF variable name */
{ "1.0", "25.0", "$DIMTXT" },
{ "0.8", "20.0", "$DIMASZ"}
};
/**
* Build and format layer name. The name is created by appending the layer number
* to the basic layer name.
*
* \param result OUT buffer for result
* \param name IN base part of name
* \param layer IN layer number
*/
void DxfLayerName(DynString *result, char *name, int layer)
{
DynStringPrintf(result, DXF_INDENT "8\n%s%d\n", name, layer);
}
/**
* Build and format a position. If it specifies a point the value
* is assumed to be in inches and will be converted to millimeters
* if the metric system is active.
* If it is an angle (group codes 50 to 59) it is not converted.
*
* \param result OUT buffer for result
* \param type IN type of position following DXF specs
* \param value IN position
*/
void DxfFormatPosition(DynString *result, int type, double value)
{
if (units == 1)
{
if( type < 50 || type > 58 )
value *= 25.4;
}
DynStringPrintf(result, DXF_INDENT "%d\n%0.6f\n", type, value);
}
/**
* Build and format the line style definition
*
* \param result OUT buffer for result
* \param type IN line style TRUE for dashed, FALSE for solid lines
*/
void DxfLineStyle(DynString *result, int isDashed)
{
DynStringPrintf(result, DXF_INDENT "6\n%s\n",
(isDashed ? "DASHED" : "CONTINUOUS"));
}
/**
* Build and format layer name. The name is created by appending the layer number
* to the basic layer name. The result is appended to the existing result buffer.
*
* \param output OUT buffer for result
* \param basename IN base part of name
* \param layer IN layer number
*/
static void
DxfAppendLayerName(DynString *output, int layer)
{
DynString formatted = NaS;
DynStringMalloc(&formatted, 0);
DxfLayerName(&formatted, sProdNameUpper, layer);
DynStringCatStr(output, &formatted);
DynStringFree(&formatted);
}
/**
* Build and format a position. The result is appended to the existing result buffer.
*
* \param output OUT buffer for result
* \param type IN type of position following DXF specs
* \param value IN position
*/
static void
DxfAppendPosition(DynString *output, int type, double value)
{
DynString formatted = NaS;
DynStringMalloc(&formatted, 0);
DxfFormatPosition(&formatted, type, value);
DynStringCatStr(output, &formatted);
DynStringFree(&formatted);
}
/**
* Build and format the line style definition. The result is appended to the existing result buffer.
*
* \param result OUT buffer for result
* \param type IN line style TRUE for dashed, FALSE for solid lines
*/
static void
DxfAppendLineStyle(DynString *output, int style)
{
DynString formatted = NaS;
DynStringMalloc(&formatted, 0);
DxfLineStyle(&formatted, style);
DynStringCatStr(output, &formatted);
DynStringFree(&formatted);
}
/**
* Format a complete LINE command after DXF spec
*
* \param result OUT buffer for the completed command
* \param layer IN number part of the layer
* \param x0, y0 IN first endpoint
* \param x1, y1 IN second endpoint
* \param style IN line style, TRUE for dashed, FALSE for continuous
*/
void
DxfLineCommand(DynString *result, int layer, double x0,
double y0, double x1, double y1, int style)
{
DynStringCatCStr(result, DXF_INDENT "0\nLINE\n");
DxfAppendLayerName(result, layer);
DxfAppendPosition(result, 10, x0);
DxfAppendPosition(result, 20, y0);
DxfAppendPosition(result, 11, x1);
DxfAppendPosition(result, 21, y1);
DxfAppendLineStyle(result, style);
}
/**
* Format a complete CIRCLE command after DXF spec
*
* \param result OUT buffer for the completed command
* \param layer IN number part of the layer
* \param x, y IN center point
* \param r IN radius
* \param style IN line style, TRUE for dashed, FALSE for continuous
*/
void
DxfCircleCommand(DynString *result, int layer, double x,
double y, double r, int style)
{
DynStringCatCStr(result, DXF_INDENT "0\nCIRCLE\n");
DxfAppendPosition(result, 10, x);
DxfAppendPosition(result, 20, y);
DxfAppendPosition(result, 40, r);
DxfAppendLayerName(result, layer);
DxfAppendLineStyle(result, style);
}
/**
* Format a complete ARC command after DXF spec
*
* \param result OUT buffer for the completed command
* \param layer IN number part of the layer
* \param x, y IN center point
* \param r IN radius
* \param a0 IN starting angle
* \param a1 IN ending angle
* \param style IN line style, TRUE for dashed, FALSE for continuous
*/
void
DxfArcCommand(DynString *result, int layer, double x, double y,
double r, double a0, double a1, int style)
{
DynStringCatCStr(result, DXF_INDENT "0\nARC\n");
DxfAppendPosition(result, 10, x);
DxfAppendPosition(result, 20, y);
DxfAppendPosition(result, 40, r);
DxfAppendPosition(result, 50, a0);
DxfAppendPosition(result, 51, a0+a1);
DxfAppendLayerName(result, layer);
DxfAppendLineStyle(result, style);
}
/**
* Format a complete TEXT command after DXF spec
*
* \param result OUT buffer for the completed command
* \param layer IN number part of the layer
* \param x, y IN text position
* \param size IN font size
* \param text IN text
*/
void
DxfTextCommand(DynString *result, int layer, double x,
double y, double size, char *text)
{
DynStringCatCStr(result, DXF_INDENT "0\nTEXT\n");
DynStringCatCStrs(result, DXF_INDENT "1\n", text, "\n", NULL);
DxfAppendPosition(result, 10, x);
DxfAppendPosition(result, 20, y);
DxfAppendPosition(result, 40, size/72.0);
DxfAppendLayerName(result, layer);
}
/**
* Append the header lines needed to define the measurement system. This includes the
* definition of the measurement system (metric or English) vie the $MEASUREMENT variable
* and the units i.e. inches for English and mm for metric.
*
* \PARAM result OUT buffer for the completed command
*/
void
DxfUnits(DynString *result)
{
char *value;
DynStringCatCStr(result, DXF_INDENT "9\n$MEASUREMENT\n 70\n");
if (units == 1) {
value = "1\n";
} else {
value = "0\n";
}
DynStringCatCStr(result, value);
DynStringCatCStr(result, DXF_INDENT "9\n$INSUNITS\n 70\n");
if (units == 1) {
value = "4\n";
} else {
value = "1\n";
}
DynStringCatCStr(result, value);
}
/**
* Define a size of dimensions. The default values are taken from
* static array dxfDimensionDefaults
*
* \PARAM result OUT the completed command is appended to this buffer
* \PARAM dimension IN dimension variable to set
*/
void
DxfDimensionSize(DynString *result, enum DXF_DIMENSIONS dimension )
{
DynString formatted;
DynStringMalloc(&formatted, 0);
DynStringPrintf(&formatted,
DXF_INDENT "9\n%s\n 40\n%s\n",
dxfDimensionDefaults[dimension][2],
dxfDimensionDefaults[dimension][units]);
DynStringCatStr(result, &formatted);
DynStringFree(&formatted);
}
/**
* Create the complete prologue for a DXF file. Includes the header section,
* a table for line styles and a table for layers.
*
* \param result OUT buffer for the completed command
* \param layerCount IN count of defined layers
* \param x0, y0 IN minimum (left bottom) position
* \param x1, y1 IN maximum (top right) position
*/
void
DxfPrologue(DynString *result, int layerCount, double x0, double y0, double x1,
double y1)
{
int i;
DynString layer = NaS;
DynStringMalloc(&layer, 0);
DynStringCatCStr(result, "\
0\nSECTION\n\
2\nHEADER\n\
9\n$ACADVER\n 1\nAC1009\n");
DxfUnits(result);
DxfDimensionSize(result, DXF_DIMTEXTSIZE);
DxfDimensionSize(result, DXF_DIMARROWSIZE);
DynStringCatCStr(result, " 9\n$EXTMIN\n");
DxfAppendPosition(result, 10, x0);
DxfAppendPosition(result, 20, y0);
DynStringCatCStr(result, " 9\n$EXTMAX\n");
DxfAppendPosition(result, 10, x1);
DxfAppendPosition(result, 20, y1);
DynStringCatCStr(result, "\
9\n$TEXTSTYLE\n 7\nSTANDARD\n\
0\nENDSEC\n\
0\nSECTION\n\
2\nTABLES\n\
0\nTABLE\n\
2\nLTYPE\n\
0\nLTYPE\n 2\nCONTINUOUS\n 70\n0\n\
3\nSolid line\n\
72\n65\n 73\n0\n 40\n0\n\
0\nLTYPE\n 2\nDASHED\n 70\n0\n\
3\n__ __ __ __ __ __ __ __ __ __ __ __ __ __ __\n\
72\n65\n 73\n2\n 40\n0.15\n 49\n0.1\n 49\n-0.05\n\
0\nLTYPE\n 2\nDOT\n 70\n0\n\
3\n...............................................\n\
72\n65\n 73\n2\n 40\n0.1\n 49\n0\n 49\n-0.05\n\
0\nENDTAB\n\
0\nTABLE\n\
2\nLAYER\n\
70\n0\n");
for (i = 0; i < layerCount; i++) {
DynStringPrintf(&layer,
DXF_INDENT"0\nLAYER\n 2\n%s%d\n 6\nCONTINUOUS\n 62\n7\n 70\n0\n",
sProdNameUpper, i + 1);
DynStringCatStr(result, &layer);
}
DynStringCatCStr(result, "\
0\nENDTAB\n\
0\nENDSEC\n\
0\nSECTION\n\
2\nENTITIES\n");
}
/**
* Create the file footer for a DXF file. Closes the open section and places
* an end-of-file marker
*
* \param result OUT buffer for the completed command
*/
void
DxfEpilogue(DynString *result)
{
DynStringCatCStr(result, DXF_INDENT "0\nENDSEC\n" DXF_INDENT "0\nEOF\n");
}
|