-
Notifications
You must be signed in to change notification settings - Fork 0
/
truss.cpp
343 lines (283 loc) · 9.52 KB
/
truss.cpp
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
#include "simpletruss.h"
#include "floortruss.h"
#include <wx/string.h>
#include <wx/dc.h>
#include <cstdlib>
#include <cerrno>
#include <cstring>
wxString Truss::packStripped() const {
wxString packed = pack();
int end_pos = -1;
// By looking for the first semicolon, we will strip off the title and any
// comments attached to the truss, suitable for storing it in the database.
int semi_pos = packed.Find(';');
if (semi_pos != -1)
packed = packed.substr(0, semi_pos);
// If there is an F/O/P, get rid of it too. Note that we really only want
// to get rid of the F, since it is different for each job, but the O and P
// stay the same so we will add them back on if they were there.
int f_pos = packed.Find('F');
int o_pos = packed.Find('O');
int p_pos = packed.Find('P');
if (f_pos != -1)
end_pos = f_pos;
if (o_pos != -1 && (end_pos == -1 || o_pos < end_pos))
end_pos = o_pos;
if (p_pos != -1 && (end_pos == -1 || p_pos < end_pos))
end_pos = p_pos;
if (end_pos != -1)
packed = packed.substr(0, end_pos);
if (o_pos != -1)
packed += 'O';
if (p_pos != -1)
packed += 'P';
return packed;
}
// A serialized truss is in the following format:
// 'T' or 'S' = triangle or square (simple/floor truss)
// <float>x<float> = width x height
// 'F' = full truck, 'O' = oversize, 'P' = special permit.
// ';' <job title/description text>.
Truss *Truss::unpack(const char *str) {
Truss *truss;
if (!str || str[0] == 0)
return 0;
float width, height = 0;
char *end;
if (str[0] == 'T' || str[0] == 'S') {
errno = 0;
width = strtod(&str[1], &end);
if (end == &str[1] || errno)
return 0;
char *hstart;
if (*end == 'x') {
hstart = end+1;
height = strtod(hstart, &end);
if (hstart == end || errno)
return 0;
}
} else {
return 0;
}
if (str[0] == 'T')
truss = new SimpleTruss(width, height);
else if (str[0] == 'S')
truss = height ? new FloorTruss(width, height) : new FloorTruss(width);
else
return 0;
unsigned flags = 0;
// end = the character after the height = start of flags.
do {
if (*end == 'F')
flags |= Truss::kFull;
else if (*end == 'O')
flags |= Truss::kOversize;
else if (*end == 'P')
flags |= Truss::kVeryOversize;
else
break;
++end;
} while (true);
truss->set(flags);
if (*end == ';') {
++end;
truss->unescapeTextAndNotes(end);
}
return truss;
}
namespace {
// measure specified in inches
wxString nice_print_measurement(float measure) {
wxString s;
unsigned feet = measure / 12.0f;
if (feet) {
s << feet;
s << '\'';
}
measure -= feet * 12;
unsigned inches = measure;
measure -= inches;
unsigned frac = measure * 10000;
/*
// For 1/2, 1/4's, 1/8's, and 1/16's, write a fraction.
const char * const sixteenths[] = {
"", "1/16", "1/8", "3/16", "1/4", "5/16", "3/8", "7/16", "1/2",
"9/16", "5/8", "11/16", "3/4", "13/16", "7/8", "15/16"
};
if (frac == 0) {
if (inches)
s << inches << wxT('"');
} else if (frac % 625 == 0) {
if (inches)
s << inches << wxT(' ');
s << sixteenths[frac/625] << wxT('"');
} else {
wxString extra;
extra.Printf("%.3f\"", inches + measure);
s << extra;
}
*/
// Round to the nearest inch:
if (measure)
++inches;
if (inches)
s << inches << wxT('"');
return s;
}
}
void Truss::drawGuide(wxDC &dc, unsigned left, unsigned top, unsigned length,
float inchLen, bool is_vertical, unsigned line_height) {
wxString text(nice_print_measurement(inchLen));
int textheight;
int textwidth;
dc.GetTextExtent(text, &textwidth, &textheight);
dc.SetBrush(*wxWHITE_BRUSH);
unsigned ends_len = line_height * 0.65;
unsigned round = line_height * 0.2; // For the rounded rectangles.
unsigned pad = 1 + line_height * 0.1;
unsigned lesspad = 1 + line_height * 0.05;
if (is_vertical) {
bool text_horizontal = false;
int seglen = length / 2 - textwidth / 2;
if (seglen < 5) {
text_horizontal = true;
seglen = length / 2 - textheight / 2;
}
// Line segments:
dc.DrawLine(left + textheight / 2, top,
left + textheight / 2, top + seglen - 2);
dc.DrawLine(left + textheight / 2, top + length - seglen + 2,
left + textheight / 2, top + length);
// Thingies on the ends:
dc.DrawLine(left + textheight / 2 - ends_len / 2, top,
left + textheight / 2 + ends_len / 2, top);
dc.DrawLine(left + textheight / 2 - ends_len / 2, top + length,
left + textheight / 2 + ends_len / 2, top + length);
// If it's too cramped, draw the text horizontally instead.
// Note the horizontal line uses textheight/2 as an adjustment anyways,
// since the line drawing does that above.
if (text_horizontal) {
dc.DrawRoundedRectangle(left - textwidth / 2 + textheight / 2 - pad,
top + length / 2 - textheight / 2 - pad, textwidth + 2 * pad,
textheight + pad, round);
dc.DrawText(text, left - textwidth / 2 + textheight / 2,
top + length / 2 - textheight / 2);
} else {
dc.DrawRoundedRectangle(left,
top + length / 2 - textwidth / 2 - pad,
textheight + pad, textwidth + 2 * pad, round);
dc.DrawRotatedText(text, left,
top + length / 2 + textwidth / 2 + 2, 90);
// Add 2 because at small resolutions, sometimes the ' gets cut
// off, though it looks fine at high resolutions (printed) where
// the 2 won't really have any effect.
}
} else {
int seglen = length / 2 - textwidth / 2;
dc.DrawLine(left, top + textheight / 2,
left + seglen - pad, top + textheight / 2);
dc.DrawLine(left + length - seglen + pad, top + textheight / 2,
left + length, top + textheight / 2);
dc.DrawLine(left, top + textheight / 2 - ends_len / 2,
left, top + textheight / 2 + ends_len / 2);
dc.DrawLine(left + length, top + textheight / 2 - ends_len / 2,
left + length, top + textheight / 2 + ends_len / 2);
dc.DrawRoundedRectangle(left + length / 2 - textwidth / 2 - pad,
top - pad, textwidth + 2 * pad, textheight + pad, round);
dc.DrawText(text, left + length / 2 - textwidth / 2, top);
}
}
wxString Truss::escapeTextAndNotes() const {
if (text.empty() && notes.empty())
return wxT("");
wxString ret(wxT(";"));
unsigned len = text.length();
for (unsigned i = 0; i < len; ++i) {
#if defined(__GNUC__)
switch ((typeof(wxT(' ')))text[i]) {
#elif defined(_MSC_VER)
switch ((decltype(wxT(' ')))text[i]) {
#else
#error Sorry, your compiler needs some sort of typeof feature!
#endif
case wxT(';'):
ret += wxT("\\;");
break;
case wxT('\r'):
continue;
case wxT('\n'):
ret += wxT("\\n");
break;
case wxT('\\'):
ret += wxT("\\\\");
break;
default:
ret += text[i];
}
}
len = notes.length();
if (!len)
return ret;
ret += wxT(';');
for (unsigned i = 0; i < len; ++i) {
#if defined(__GNUC__)
switch ((typeof(wxT(' ')))notes[i]) {
#elif defined(_MSC_VER)
switch ((decltype(wxT(' ')))notes[i]) {
#else
#error See above - you need a typeof intrinsic.
#endif
case wxT(';'):
ret += wxT("\\;");
break;
case wxT('\r'):
continue;
case wxT('\n'):
ret += wxT("\\n");
break;
case wxT('\\'):
ret += wxT("\\\\");
break;
default:
ret += notes[i];
}
}
return ret;
}
void Truss::unescapeTextAndNotes(const char *serialized) {
text.clear();
notes.clear();
wxString *appending = &text;
unsigned i = 0;
while (true) {
switch (serialized[i]) {
case '\\':
++i;
switch (serialized[i]) {
case '\\':
*appending += wxT('\\');
break;
case ';':
*appending += wxT(';');
break;
case 'n':
*appending += wxT('\n');
break;
default:
break;
}
break;
case ';':
if (appending == &text)
appending = ¬es;
break;
case 0:
return;
default:
*appending += serialized[i];
break;
}
++i;
}
// Return inside the while loop, when null character found.
}