summaryrefslogtreecommitdiff
path: root/app/bin/dxfoutput.c
blob: 91a822f17a4e94bf45bc25a6e7e92605949bb912 (plain)
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
/** \file dxfoutput.c
 * Exporting DXF files
*/

/*  XTrkCad - Model Railroad CAD
 *  Copyright (C) 2005 Dave Bullis
 *
 *  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 <stdio.h>
#include <string.h>
#include <time.h>
#ifdef WINDOWS
  #include <io.h>
  #include <windows.h>
  #if _MSC_VER >=1400
    #define strdup _strdup
  #endif
#else
  #include <errno.h>
#endif

#include <xtrkcad-config.h>
#include <locale.h>
#include <assert.h>
#include "track.h"
#include "i18n.h"
#include <dynstring.h>

#include "dxfformat.h"

static struct wFilSel_t * exportDXFFile_fs;

static void DxfLine(
    drawCmd_p d,
    coOrd p0,
    coOrd p1,
    wDrawWidth width,
    wDrawColor color)
{
    DynString command = NaS;
    DynStringMalloc(&command, 100);
    DxfLineCommand(&command,
                   curTrackLayer + 1,
                   p0.x, p0.y,
                   p1.x, p1.y,
                   ((d->options&DC_DASH) != 0));
    fputs(DynStringToCStr(&command), (FILE *)d->d);
    DynStringFree(&command);
}

static void DxfArc(
    drawCmd_p d,
    coOrd p,
    DIST_T r,
    ANGLE_T angle0,
    ANGLE_T angle1,
    BOOL_T drawCenter,
    wDrawWidth width,
    wDrawColor color)
{
    DynString command = NaS;
    DynStringMalloc(&command, 100);
    angle0 = NormalizeAngle(90.0-(angle0+angle1));

    if (angle1 >= 360.0) {
        DxfCircleCommand(&command,
                         curTrackLayer + 1,
                         p.x,
                         p.y,
                         r,
                         ((d->options&DC_DASH) != 0));
    } else {
        DxfArcCommand(&command,
                      curTrackLayer + 1,
                      p.x,
                      p.y,
                      r,
                      angle0,
                      angle1,
                      ((d->options&DC_DASH) != 0));
    }

    fputs(DynStringToCStr(&command), (FILE *)d->d);
    DynStringFree(&command);
}

static void DxfString(
    drawCmd_p d,
    coOrd p,
    ANGLE_T a,
    char * s,
    wFont_p fp,
    FONTSIZE_T fontSize,
    wDrawColor color)
{
    DynString command = NaS;
    DynStringMalloc(&command, 100);
    DxfTextCommand(&command,
                   curTrackLayer + 1,
                   p.x,
                   p.y,
                   fontSize,
                   s);
    fputs(DynStringToCStr(&command), (FILE *)d->d);
    DynStringFree(&command);
}

static void DxfBitMap(
    drawCmd_p d,
    coOrd p,
    wDrawBitMap_p bm,
    wDrawColor color)
{
}

static void DxfFillPoly(
    drawCmd_p d,
    int cnt,
    coOrd * pts,
    wDrawColor color)
{
    int inx;

    for (inx=1; inx<cnt; inx++) {
        DxfLine(d, pts[inx-1], pts[inx], 0, color);
    }

    DxfLine(d, pts[cnt-1], pts[0], 0, color);
}

static void DxfFillCircle(drawCmd_p d, coOrd center, DIST_T radius,
                          wDrawColor color)
{
    DxfArc(d, center, radius, 0.0, 360, FALSE, 0, color);
}


static drawFuncs_t dxfDrawFuncs = {
    0,
    DxfLine,
    DxfArc,
    DxfString,
    DxfBitMap,
    DxfFillPoly,
    DxfFillCircle
};

static drawCmd_t dxfD = {
    NULL, &dxfDrawFuncs, 0, 1.0, 0.0, {0.0,0.0}, {0.0,0.0}, Pix2CoOrd, CoOrd2Pix, 100.0
};

static int DoExportDXFTracks(
    int cnt,
    char ** fileName,
    void * data)
{
    time_t clock;
    char *oldLocale;
	DynString command = NaS;
	FILE * dxfF;

    assert(fileName != NULL);
    assert(cnt == 1);

	DynStringMalloc(&command, 100);

	SetCurrentPath(DXFPATHKEY, fileName[ 0 ]);
    dxfF = fopen(fileName[0], "w");

    if (dxfF==NULL) {
        NoticeMessage(MSG_OPEN_FAIL, _("Continue"), NULL, "DXF", fileName[0],
                      strerror(errno));
        return FALSE;
    }

    oldLocale = SaveLocale("C");
    wSetCursor(wCursorWait);
    time(&clock);
 
	DxfPrologue(&command, 10, 0.0, 0.0, mapD.size.x, mapD.size.y);
	fputs(DynStringToCStr(&command), dxfF);
	dxfD.d = (wDraw_p)dxfF;

    DrawSelectedTracks(&dxfD);

	DynStringClear(&command);
	DxfEpilogue(&command);
	fputs(DynStringToCStr(&command), dxfF);

    fclose(dxfF);
    RestoreLocale(oldLocale);
    Reset();
    wSetCursor(wCursorNormal);
    return TRUE;
}

/**
* Create and show the dialog for selected the DXF export filename
*/

void DoExportDXF(void)
{
    //if (selectedTrackCount <= 0) {
    //    ErrorMessage(MSG_NO_SELECTED_TRK);
    //    return;
    //}
    assert(selectedTrackCount > 0);

    if (exportDXFFile_fs == NULL)
        exportDXFFile_fs = wFilSelCreate(mainW, FS_SAVE, 0, _("Export to DXF"),
                                         sDXFFilePattern, DoExportDXFTracks, NULL);

    wFilSelect(exportDXFFile_fs, curDirName);
}