1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dxfrenderer.cpp
3 // Purpose: DXF reader and renderer
4 // Author: Sandro Sigala
8 // Copyright: (c) Sandro Sigala
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
23 #include "wx/wfstream.h"
24 #include "wx/txtstrm.h"
27 #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
31 #include <OpenGL/glu.h>
38 #include "dxfrenderer.h"
40 #include "wx/listimpl.cpp"
41 WX_DEFINE_LIST(DXFEntityList
)
42 WX_DEFINE_LIST(DXFLayerList
)
44 // Conversion table from AutoCAD ACI colours to RGB values
45 static const struct { unsigned char r
, g
, b
; } aci_to_rgb
[256] = {
46 /* 0 */ {255, 255, 255},
48 /* 2 */ {255, 255, 0},
50 /* 4 */ { 0, 255, 255},
52 /* 6 */ {255, 0, 255},
53 /* 7 */ {255, 255, 255},
54 /* 8 */ {128, 128, 128},
55 /* 9 */ {192, 192, 192},
57 /* 11 */ {255, 127, 127},
59 /* 13 */ {204, 102, 102},
61 /* 15 */ {153, 76, 76},
63 /* 17 */ {127, 63, 63},
65 /* 19 */ { 76, 38, 38},
66 /* 20 */ {255, 63, 0},
67 /* 21 */ {255, 159, 127},
68 /* 22 */ {204, 51, 0},
69 /* 23 */ {204, 127, 102},
70 /* 24 */ {153, 38, 0},
71 /* 25 */ {153, 95, 76},
72 /* 26 */ {127, 31, 0},
73 /* 27 */ {127, 79, 63},
74 /* 28 */ { 76, 19, 0},
75 /* 29 */ { 76, 47, 38},
76 /* 30 */ {255, 127, 0},
77 /* 31 */ {255, 191, 127},
78 /* 32 */ {204, 102, 0},
79 /* 33 */ {204, 153, 102},
80 /* 34 */ {153, 76, 0},
81 /* 35 */ {153, 114, 76},
82 /* 36 */ {127, 63, 0},
83 /* 37 */ {127, 95, 63},
84 /* 38 */ { 76, 38, 0},
85 /* 39 */ { 76, 57, 38},
86 /* 40 */ {255, 191, 0},
87 /* 41 */ {255, 223, 127},
88 /* 42 */ {204, 153, 0},
89 /* 43 */ {204, 178, 102},
90 /* 44 */ {153, 114, 0},
91 /* 45 */ {153, 133, 76},
92 /* 46 */ {127, 95, 0},
93 /* 47 */ {127, 111, 63},
94 /* 48 */ { 76, 57, 0},
95 /* 49 */ { 76, 66, 38},
96 /* 50 */ {255, 255, 0},
97 /* 51 */ {255, 255, 127},
98 /* 52 */ {204, 204, 0},
99 /* 53 */ {204, 204, 102},
100 /* 54 */ {153, 153, 0},
101 /* 55 */ {153, 153, 76},
102 /* 56 */ {127, 127, 0},
103 /* 57 */ {127, 127, 63},
104 /* 58 */ { 76, 76, 0},
105 /* 59 */ { 76, 76, 38},
106 /* 60 */ {191, 255, 0},
107 /* 61 */ {223, 255, 127},
108 /* 62 */ {153, 204, 0},
109 /* 63 */ {178, 204, 102},
110 /* 64 */ {114, 153, 0},
111 /* 65 */ {133, 153, 76},
112 /* 66 */ { 95, 127, 0},
113 /* 67 */ {111, 127, 63},
114 /* 68 */ { 57, 76, 0},
115 /* 69 */ { 66, 76, 38},
116 /* 70 */ {127, 255, 0},
117 /* 71 */ {191, 255, 127},
118 /* 72 */ {102, 204, 0},
119 /* 73 */ {153, 204, 102},
120 /* 74 */ { 76, 153, 0},
121 /* 75 */ {114, 153, 76},
122 /* 76 */ { 63, 127, 0},
123 /* 77 */ { 95, 127, 63},
124 /* 78 */ { 38, 76, 0},
125 /* 79 */ { 57, 76, 38},
126 /* 80 */ { 63, 255, 0},
127 /* 81 */ {159, 255, 127},
128 /* 82 */ { 51, 204, 0},
129 /* 83 */ {127, 204, 102},
130 /* 84 */ { 38, 153, 0},
131 /* 85 */ { 95, 153, 76},
132 /* 86 */ { 31, 127, 0},
133 /* 87 */ { 79, 127, 63},
134 /* 88 */ { 19, 76, 0},
135 /* 89 */ { 47, 76, 38},
136 /* 90 */ { 0, 255, 0},
137 /* 91 */ {127, 255, 127},
138 /* 92 */ { 0, 204, 0},
139 /* 93 */ {102, 204, 102},
140 /* 94 */ { 0, 153, 0},
141 /* 95 */ { 76, 153, 76},
142 /* 96 */ { 0, 127, 0},
143 /* 97 */ { 63, 127, 63},
144 /* 98 */ { 0, 76, 0},
145 /* 99 */ { 38, 76, 38},
146 /* 100 */ { 0, 255, 63},
147 /* 101 */ {127, 255, 159},
148 /* 102 */ { 0, 204, 51},
149 /* 103 */ {102, 204, 127},
150 /* 104 */ { 0, 153, 38},
151 /* 105 */ { 76, 153, 95},
152 /* 106 */ { 0, 127, 31},
153 /* 107 */ { 63, 127, 79},
154 /* 108 */ { 0, 76, 19},
155 /* 109 */ { 38, 76, 47},
156 /* 110 */ { 0, 255, 127},
157 /* 111 */ {127, 255, 191},
158 /* 112 */ { 0, 204, 102},
159 /* 113 */ {102, 204, 153},
160 /* 114 */ { 0, 153, 76},
161 /* 115 */ { 76, 153, 114},
162 /* 116 */ { 0, 127, 63},
163 /* 117 */ { 63, 127, 95},
164 /* 118 */ { 0, 76, 38},
165 /* 119 */ { 38, 76, 57},
166 /* 120 */ { 0, 255, 191},
167 /* 121 */ {127, 255, 223},
168 /* 122 */ { 0, 204, 153},
169 /* 123 */ {102, 204, 178},
170 /* 124 */ { 0, 153, 114},
171 /* 125 */ { 76, 153, 133},
172 /* 126 */ { 0, 127, 95},
173 /* 127 */ { 63, 127, 111},
174 /* 128 */ { 0, 76, 57},
175 /* 129 */ { 38, 76, 66},
176 /* 130 */ { 0, 255, 255},
177 /* 131 */ {127, 255, 255},
178 /* 132 */ { 0, 204, 204},
179 /* 133 */ {102, 204, 204},
180 /* 134 */ { 0, 153, 153},
181 /* 135 */ { 76, 153, 153},
182 /* 136 */ { 0, 127, 127},
183 /* 137 */ { 63, 127, 127},
184 /* 138 */ { 0, 76, 76},
185 /* 139 */ { 38, 76, 76},
186 /* 140 */ { 0, 191, 255},
187 /* 141 */ {127, 223, 255},
188 /* 142 */ { 0, 153, 204},
189 /* 143 */ {102, 178, 204},
190 /* 144 */ { 0, 114, 153},
191 /* 145 */ { 76, 133, 153},
192 /* 146 */ { 0, 95, 127},
193 /* 147 */ { 63, 111, 127},
194 /* 148 */ { 0, 57, 76},
195 /* 149 */ { 38, 66, 76},
196 /* 150 */ { 0, 127, 255},
197 /* 151 */ {127, 191, 255},
198 /* 152 */ { 0, 102, 204},
199 /* 153 */ {102, 153, 204},
200 /* 154 */ { 0, 76, 153},
201 /* 155 */ { 76, 114, 153},
202 /* 156 */ { 0, 63, 127},
203 /* 157 */ { 63, 95, 127},
204 /* 158 */ { 0, 38, 76},
205 /* 159 */ { 38, 57, 76},
206 /* 160 */ { 0, 63, 255},
207 /* 161 */ {127, 159, 255},
208 /* 162 */ { 0, 51, 204},
209 /* 163 */ {102, 127, 204},
210 /* 164 */ { 0, 38, 153},
211 /* 165 */ { 76, 95, 153},
212 /* 166 */ { 0, 31, 127},
213 /* 167 */ { 63, 79, 127},
214 /* 168 */ { 0, 19, 76},
215 /* 169 */ { 38, 47, 76},
216 /* 170 */ { 0, 0, 255},
217 /* 171 */ {127, 127, 255},
218 /* 172 */ { 0, 0, 204},
219 /* 173 */ {102, 102, 204},
220 /* 174 */ { 0, 0, 153},
221 /* 175 */ { 76, 76, 153},
222 /* 176 */ { 0, 0, 127},
223 /* 177 */ { 63, 63, 127},
224 /* 178 */ { 0, 0, 76},
225 /* 179 */ { 38, 38, 76},
226 /* 180 */ { 63, 0, 255},
227 /* 181 */ {159, 127, 255},
228 /* 182 */ { 51, 0, 204},
229 /* 183 */ {127, 102, 204},
230 /* 184 */ { 38, 0, 153},
231 /* 185 */ { 95, 76, 153},
232 /* 186 */ { 31, 0, 127},
233 /* 187 */ { 79, 63, 127},
234 /* 188 */ { 19, 0, 76},
235 /* 189 */ { 47, 38, 76},
236 /* 190 */ {127, 0, 255},
237 /* 191 */ {191, 127, 255},
238 /* 192 */ {102, 0, 204},
239 /* 193 */ {153, 102, 204},
240 /* 194 */ { 76, 0, 153},
241 /* 195 */ {114, 76, 153},
242 /* 196 */ { 63, 0, 127},
243 /* 197 */ { 95, 63, 127},
244 /* 198 */ { 38, 0, 76},
245 /* 199 */ { 57, 38, 76},
246 /* 200 */ {191, 0, 255},
247 /* 201 */ {223, 127, 255},
248 /* 202 */ {153, 0, 204},
249 /* 203 */ {178, 102, 204},
250 /* 204 */ {114, 0, 153},
251 /* 205 */ {133, 76, 153},
252 /* 206 */ { 95, 0, 127},
253 /* 207 */ {111, 63, 127},
254 /* 208 */ { 57, 0, 76},
255 /* 209 */ { 66, 38, 76},
256 /* 210 */ {255, 0, 255},
257 /* 211 */ {255, 127, 255},
258 /* 212 */ {204, 0, 204},
259 /* 213 */ {204, 102, 204},
260 /* 214 */ {153, 0, 153},
261 /* 215 */ {153, 76, 153},
262 /* 216 */ {127, 0, 127},
263 /* 217 */ {127, 63, 127},
264 /* 218 */ { 76, 0, 76},
265 /* 219 */ { 76, 38, 76},
266 /* 220 */ {255, 0, 191},
267 /* 221 */ {255, 127, 223},
268 /* 222 */ {204, 0, 153},
269 /* 223 */ {204, 102, 178},
270 /* 224 */ {153, 0, 114},
271 /* 225 */ {153, 76, 133},
272 /* 226 */ {127, 0, 95},
273 /* 227 */ {127, 63, 111},
274 /* 228 */ { 76, 0, 57},
275 /* 229 */ { 76, 38, 66},
276 /* 230 */ {255, 0, 127},
277 /* 231 */ {255, 127, 191},
278 /* 232 */ {204, 0, 102},
279 /* 233 */ {204, 102, 153},
280 /* 234 */ {153, 0, 76},
281 /* 235 */ {153, 76, 114},
282 /* 236 */ {127, 0, 63},
283 /* 237 */ {127, 63, 95},
284 /* 238 */ { 76, 0, 38},
285 /* 239 */ { 76, 38, 57},
286 /* 240 */ {255, 0, 63},
287 /* 241 */ {255, 127, 159},
288 /* 242 */ {204, 0, 51},
289 /* 243 */ {204, 102, 127},
290 /* 244 */ {153, 0, 38},
291 /* 245 */ {153, 76, 95},
292 /* 246 */ {127, 0, 31},
293 /* 247 */ {127, 63, 79},
294 /* 248 */ { 76, 0, 19},
295 /* 249 */ { 76, 38, 47},
296 /* 250 */ { 51, 51, 51},
297 /* 251 */ { 91, 91, 91},
298 /* 252 */ {132, 132, 132},
299 /* 253 */ {173, 173, 173},
300 /* 254 */ {214, 214, 214},
301 /* 255 */ {255, 255, 255}
304 inline DXFVector
Cross(const DXFVector
& v1
, const DXFVector
& v2
)
306 return DXFVector(v1
.y
*v2
.z
- v1
.z
*v2
.y
, v1
.z
*v2
.x
- v1
.x
*v2
.z
, v1
.x
*v2
.y
- v1
.y
*v2
.x
);
309 void DXFFace::CalculateNormal()
319 float mod
= sqrt(n
.x
*n
.x
+ n
.y
*n
.y
+ n
.z
*n
.z
);
325 // convert an AutoCAD ACI colour to wxWidgets RGB colour
326 inline wxColour
ACIColourToRGB(int col
)
328 wxASSERT(col
>= 0 && col
<= 255);
329 return wxColour(aci_to_rgb
[col
].r
, aci_to_rgb
[col
].g
, aci_to_rgb
[col
].b
);
332 // DXFReader constructor
333 DXFRenderer::DXFRenderer()
338 // DXFReader destructor
339 DXFRenderer::~DXFRenderer()
344 // deallocate all the dynamic data
345 void DXFRenderer::Clear()
349 for (DXFLayerList::compatibility_iterator node
= m_layers
.GetFirst(); node
; node
= node
->GetNext())
351 DXFLayer
*current
= node
->GetData();
357 for (DXFEntityList::compatibility_iterator node
= m_entities
.GetFirst(); node
; node
= node
->GetNext())
359 DXFEntity
*current
= node
->GetData();
366 int DXFRenderer::GetLayerColour(const wxString
& layer
) const
368 for (DXFLayerList::compatibility_iterator node
= m_layers
.GetFirst(); node
; node
= node
->GetNext())
370 DXFLayer
*current
= node
->GetData();
371 if (current
->name
== layer
)
372 return current
->colour
;
377 // read two sequential lines
378 inline void GetLines(wxTextInputStream
& text
, wxString
& line1
, wxString
& line2
)
380 line1
= text
.ReadLine().Trim().Trim(false);
381 line2
= text
.ReadLine().Trim().Trim(false);
384 // parse header section: just skip everything
385 bool DXFRenderer::ParseHeader(wxInputStream
& stream
)
387 wxTextInputStream
text(stream
);
388 wxString line1
, line2
;
389 while (stream
.CanRead())
391 GetLines(text
, line1
, line2
);
392 if (line1
== wxT("0") && line2
== wxT("ENDSEC"))
398 // parse tables section: save layers name and colour
399 bool DXFRenderer::ParseTables(wxInputStream
& stream
)
401 wxTextInputStream
text(stream
);
402 wxString line1
, line2
;
405 while (stream
.CanRead())
407 GetLines(text
, line1
, line2
);
408 if (line1
== wxT("0") && inlayer
)
411 if (!layer
.name
.IsEmpty() && layer
.colour
!= -1)
413 DXFLayer
*p
= new DXFLayer
;
414 p
->name
= layer
.name
;
415 p
->colour
= layer
.colour
;
421 if (line1
== wxT("0") && line2
== wxT("ENDSEC"))
423 else if (line1
== wxT("0") && line2
== wxT("LAYER"))
427 if (line1
== wxT("2")) // layer name
429 else if (line1
== wxT("62")) // ACI colour
440 // This method is used instead of numStr.ToDouble(d) because the latter
441 // (wxString::ToDouble()) takes the systems proper locale into account,
442 // whereas the implementation below works with the default locale.
443 // (Converting numbers that are formatted in the default locale can fail
444 // with system locales that use e.g. the comma as the decimal separator.)
445 static double ToDouble(const wxString
& numStr
)
448 std::string
numStr_(numStr
.c_str());
449 std::istringstream
iss(numStr_
);
456 // parse entities section: save 3DFACE and LINE entities
457 bool DXFRenderer::ParseEntities(wxInputStream
& stream
)
459 wxTextInputStream
text(stream
);
460 wxString line1
, line2
;
461 int state
= 0; // 0: none, 1: 3DFACE, 2: LINE
465 while (stream
.CanRead())
467 GetLines(text
, line1
, line2
);
468 if (line1
== wxT("0") && state
> 0)
471 if (state
== 1) // 3DFACE
473 DXFFace
*p
= new DXFFace
;
478 p
->CalculateNormal();
482 p
->colour
= GetLayerColour(layer
);
483 m_entities
.Append(p
);
484 colour
= -1; layer
= wxEmptyString
;
485 v
[0] = v
[1] = v
[2] = v
[3] = DXFVector();
488 else if (state
== 2) // LINE
490 DXFLine
*p
= new DXFLine
;
496 p
->colour
= GetLayerColour(layer
);
497 m_entities
.Append(p
);
498 colour
= -1; layer
= wxEmptyString
;
499 v
[0] = v
[1] = v
[2] = v
[3] = DXFVector();
503 if (line1
== wxT("0") && line2
== wxT("ENDSEC"))
505 else if (line1
== wxT("0") && line2
== wxT("3DFACE"))
507 else if (line1
== wxT("0") && line2
== wxT("LINE"))
511 const double d
=ToDouble(line2
);
513 if (line1
== wxT("10"))
515 else if (line1
== wxT("20"))
517 else if (line1
== wxT("30"))
519 else if (line1
== wxT("11"))
521 else if (line1
== wxT("21"))
523 else if (line1
== wxT("31"))
525 else if (line1
== wxT("12"))
527 else if (line1
== wxT("22"))
529 else if (line1
== wxT("32"))
531 else if (line1
== wxT("13"))
533 else if (line1
== wxT("23"))
535 else if (line1
== wxT("33"))
537 else if (line1
== wxT("8")) // layer
539 else if (line1
== wxT("62")) // colour
550 // parse and load a DXF file
551 // currently pretty limited, but knows enought do handle 3DFACEs and LINEs
552 bool DXFRenderer::Load(wxInputStream
& stream
)
555 wxTextInputStream
text(stream
);
557 wxString line1
, line2
;
558 while (stream
.CanRead())
560 GetLines(text
, line1
, line2
);
561 if (line1
== wxT("999")) // comment
563 else if (line1
== wxT("0") && line2
== wxT("SECTION"))
565 GetLines(text
, line1
, line2
);
566 if (line1
== wxT("2"))
568 if (line2
== wxT("HEADER"))
570 if (!ParseHeader(stream
))
573 else if (line2
== wxT("TABLES"))
575 if (!ParseTables(stream
))
578 else if (line2
== wxT("ENTITIES"))
580 if (!ParseEntities(stream
))
592 inline float mymin(float x
, float y
) { return x
< y
? x
: y
; }
593 inline float mymax(float x
, float y
) { return x
> y
? x
: y
; }
595 // Scale object boundings to [-5,5]
596 void DXFRenderer::NormalizeEntities()
598 // calculate current min and max boundings of object
599 DXFVector
minv(10e20f
, 10e20f
, 10e20f
);
600 DXFVector
maxv(-10e20f
, -10e20f
, -10e20f
);
601 for (DXFEntityList::compatibility_iterator node
= m_entities
.GetFirst(); node
; node
= node
->GetNext())
603 DXFEntity
*p
= node
->GetData();
604 if (p
->type
== DXFEntity::Line
)
606 DXFLine
*line
= (DXFLine
*)p
;
607 const DXFVector
*v
[2] = { &line
->v0
, &line
->v1
};
608 for (int i
= 0; i
< 2; ++i
)
610 minv
.x
= mymin(v
[i
]->x
, minv
.x
);
611 minv
.y
= mymin(v
[i
]->y
, minv
.y
);
612 minv
.z
= mymin(v
[i
]->z
, minv
.z
);
613 maxv
.x
= mymax(v
[i
]->x
, maxv
.x
);
614 maxv
.y
= mymax(v
[i
]->y
, maxv
.y
);
615 maxv
.z
= mymax(v
[i
]->z
, maxv
.z
);
617 } else if (p
->type
== DXFEntity::Face
)
619 DXFFace
*face
= (DXFFace
*)p
;
620 const DXFVector
*v
[4] = { &face
->v0
, &face
->v1
, &face
->v2
, &face
->v3
};
621 for (int i
= 0; i
< 4; ++i
)
623 minv
.x
= mymin(v
[i
]->x
, minv
.x
);
624 minv
.y
= mymin(v
[i
]->y
, minv
.y
);
625 minv
.z
= mymin(v
[i
]->z
, minv
.z
);
626 maxv
.x
= mymax(v
[i
]->x
, maxv
.x
);
627 maxv
.y
= mymax(v
[i
]->y
, maxv
.y
);
628 maxv
.z
= mymax(v
[i
]->z
, maxv
.z
);
633 // rescale object down to [-5,5]
634 DXFVector
span(maxv
.x
- minv
.x
, maxv
.y
- minv
.y
, maxv
.z
- minv
.z
);
635 float factor
= mymin(mymin(10.0f
/ span
.x
, 10.0f
/ span
.y
), 10.0f
/ span
.z
);
636 for (DXFEntityList::compatibility_iterator node2
= m_entities
.GetFirst(); node2
; node2
= node2
->GetNext())
638 DXFEntity
*p
= node2
->GetData();
639 if (p
->type
== DXFEntity::Line
)
641 DXFLine
*line
= (DXFLine
*)p
;
642 DXFVector
*v
[2] = { &line
->v0
, &line
->v1
};
643 for (int i
= 0; i
< 2; ++i
)
645 v
[i
]->x
-= minv
.x
+ span
.x
/2; v
[i
]->x
*= factor
;
646 v
[i
]->y
-= minv
.y
+ span
.y
/2; v
[i
]->y
*= factor
;
647 v
[i
]->z
-= minv
.z
+ span
.z
/2; v
[i
]->z
*= factor
;
649 } else if (p
->type
== DXFEntity::Face
)
651 DXFFace
*face
= (DXFFace
*)p
;
652 DXFVector
*v
[4] = { &face
->v0
, &face
->v1
, &face
->v2
, &face
->v3
};
653 for (int i
= 0; i
< 4; ++i
)
655 v
[i
]->x
-= minv
.x
+ span
.x
/2; v
[i
]->x
*= factor
;
656 v
[i
]->y
-= minv
.y
+ span
.y
/2; v
[i
]->y
*= factor
;
657 v
[i
]->z
-= minv
.z
+ span
.z
/2; v
[i
]->z
*= factor
;
663 // OpenGL renderer for DXF entities
664 void DXFRenderer::Render() const
669 for (DXFEntityList::compatibility_iterator node
= m_entities
.GetFirst(); node
; node
= node
->GetNext())
671 DXFEntity
*p
= node
->GetData();
672 wxColour c
= ACIColourToRGB(p
->colour
);
673 if (p
->type
== DXFEntity::Line
)
675 DXFLine
*line
= (DXFLine
*)p
;
677 glColor3f(c
.Red()/255.0,c
.Green()/255.0,c
.Blue()/255.0);
678 glVertex3f(line
->v0
.x
, line
->v0
.y
, line
->v0
.z
);
679 glVertex3f(line
->v1
.x
, line
->v1
.y
, line
->v1
.z
);
682 else if (p
->type
== DXFEntity::Face
)
684 DXFFace
*face
= (DXFFace
*)p
;
685 glBegin(GL_TRIANGLES
);
686 glColor3f(c
.Red()/255.0,c
.Green()/255.0,c
.Blue()/255.0);
687 glNormal3f(face
->n
.x
, face
->n
.y
, face
->n
.z
);
688 glVertex3f(face
->v0
.x
, face
->v0
.y
, face
->v0
.z
);
689 glVertex3f(face
->v1
.x
, face
->v1
.y
, face
->v1
.z
);
690 glVertex3f(face
->v2
.x
, face
->v2
.y
, face
->v2
.z
);