diff options
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/plugins/import/imp_dxf/imp_dxf_section.c')
-rw-r--r-- | src/others/mimesh/libg3d-0.0.8/plugins/import/imp_dxf/imp_dxf_section.c | 233 |
1 files changed, 233 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_dxf/imp_dxf_section.c b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_dxf/imp_dxf_section.c new file mode 100644 index 0000000..39cd155 --- /dev/null +++ b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_dxf/imp_dxf_section.c | |||
@@ -0,0 +1,233 @@ | |||
1 | /* $Id: imp_dxf_section.c 312 2008-11-17 18:28:56Z mmmaddd $ */ | ||
2 | |||
3 | /* | ||
4 | libg3d - 3D object loading library | ||
5 | |||
6 | Copyright (C) 2005-2009 Markus Dahms <mad@automagically.de> | ||
7 | |||
8 | This library is free software; you can redistribute it and/or | ||
9 | modify it under the terms of the GNU Lesser General Public | ||
10 | License as published by the Free Software Foundation; either | ||
11 | version 2.1 of the License, or (at your option) any later version. | ||
12 | |||
13 | This library is distributed in the hope that it will be useful, | ||
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | Lesser General Public License for more details. | ||
17 | |||
18 | You should have received a copy of the GNU Lesser General Public | ||
19 | License along with this library; if not, write to the Free Software | ||
20 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
21 | */ | ||
22 | #include <string.h> | ||
23 | #include <math.h> | ||
24 | |||
25 | #include <g3d/context.h> | ||
26 | |||
27 | #include "imp_dxf.h" | ||
28 | #include "imp_dxf_prop.h" | ||
29 | #include "imp_dxf_chunks.h" | ||
30 | #include "imp_dxf_entities.h" | ||
31 | |||
32 | static DxfChunkInfo *dxf_get_chunk_info(DxfChunkInfo *chunks, gint32 id) | ||
33 | { | ||
34 | guint32 i; | ||
35 | |||
36 | for(i = 0; chunks[i].id != DXF_CODE_INVALID; i ++) | ||
37 | if(chunks[i].id == id) | ||
38 | return &(chunks[i]); | ||
39 | return NULL; | ||
40 | } | ||
41 | |||
42 | static DxfEntityInfo *dxf_get_entity_info(const gchar *str) | ||
43 | { | ||
44 | guint32 i; | ||
45 | |||
46 | for(i = 0; dxf_entities[i].name != NULL; i ++) | ||
47 | if(strcmp(dxf_entities[i].name, str) == 0) | ||
48 | return &(dxf_entities[i]); | ||
49 | #if DEBUG > 0 | ||
50 | g_debug("unknown entity: %s", str); | ||
51 | #endif | ||
52 | return NULL; | ||
53 | } | ||
54 | |||
55 | static gboolean dxf_entity_finalize(DxfGlobalData *global, guint32 sid, | ||
56 | DxfEntityData *edata, DxfEntityInfo *einfo, DxfEntityProps *eprop) | ||
57 | { | ||
58 | DxfLocalData *local; | ||
59 | |||
60 | if(einfo->callback) { | ||
61 | local = g_new0(DxfLocalData, 1); | ||
62 | local->sid = sid; | ||
63 | local->eid = einfo->id; | ||
64 | local->edata = edata; | ||
65 | local->eprop = eprop; | ||
66 | |||
67 | einfo->callback(global, local); | ||
68 | |||
69 | g_free(local); | ||
70 | } | ||
71 | return TRUE; | ||
72 | } | ||
73 | |||
74 | static gboolean dxf_read_chunk(DxfGlobalData *global, DxfChunkInfo *cinfo, | ||
75 | DxfEntityProps *eprop) | ||
76 | { | ||
77 | gint32 i; | ||
78 | gdouble dbl; | ||
79 | gchar str[DXF_MAX_LINE + 1]; | ||
80 | |||
81 | switch(cinfo->type) { | ||
82 | case DXF_T_UNKNOWN: | ||
83 | if(cinfo->id == 9) /* variable name */ | ||
84 | return dxf_debug_var(global, NULL); | ||
85 | break; | ||
86 | case DXF_T_EMPTY: | ||
87 | return TRUE; | ||
88 | break; | ||
89 | case DXF_T_INT16: | ||
90 | i = dxf_read_int16(global); | ||
91 | dxf_prop_set_int(eprop, cinfo->id, i); | ||
92 | break; | ||
93 | case DXF_T_INT32: | ||
94 | i = dxf_read_int32(global); | ||
95 | dxf_prop_set_int(eprop, cinfo->id, i); | ||
96 | break; | ||
97 | case DXF_T_FLOAT64: | ||
98 | dbl = dxf_read_float64(global); | ||
99 | dxf_prop_set_dbl(eprop, cinfo->id, dbl); | ||
100 | break; | ||
101 | case DXF_T_STRING: | ||
102 | dxf_read_string(global, str); | ||
103 | dxf_prop_set_str(eprop, cinfo->id, str); | ||
104 | break; | ||
105 | } | ||
106 | return TRUE; | ||
107 | } | ||
108 | |||
109 | static gboolean dxf_parse_chunks(DxfGlobalData *global, DxfChunkInfo *chunks, | ||
110 | gint32 parentid, const gchar *section) | ||
111 | { | ||
112 | gint32 key; | ||
113 | DxfChunkInfo *chunk_info; | ||
114 | DxfEntityData *edata; | ||
115 | DxfEntityInfo *einfo = NULL; | ||
116 | DxfEntityProps *eprop = NULL; | ||
117 | gchar str[DXF_MAX_LINE + 1]; | ||
118 | G3DFloat pcnt, prev_pcnt = 0.0; | ||
119 | |||
120 | #if DEBUG > 0 | ||
121 | g_debug("\\[%s]", section); | ||
122 | #endif | ||
123 | |||
124 | edata = g_new0(DxfEntityData, 1); | ||
125 | |||
126 | if((strcmp(section, "ENTITIES") == 0) || | ||
127 | (strcmp(section, "BLOCKS") == 0)) { | ||
128 | edata->object = g_slist_nth_data(global->model->objects, 0); | ||
129 | edata->material = g_slist_nth_data(edata->object->materials, 0); | ||
130 | } | ||
131 | |||
132 | while(TRUE) { | ||
133 | key = dxf_read_code(global); | ||
134 | chunk_info = dxf_get_chunk_info(chunks, key); | ||
135 | |||
136 | if(key == DXF_CODE_INVALID) { | ||
137 | g_free(edata); | ||
138 | return FALSE; | ||
139 | } | ||
140 | |||
141 | if(key == 0) { /* new entity or end of section */ | ||
142 | if(einfo) { | ||
143 | dxf_entity_finalize(global, parentid, edata, einfo, eprop); | ||
144 | dxf_prop_cleanup(eprop); | ||
145 | eprop = NULL; | ||
146 | } | ||
147 | dxf_read_string(global, str); | ||
148 | DXF_TEST_ENDSEC(str); | ||
149 | einfo = dxf_get_entity_info(str); | ||
150 | eprop = dxf_prop_create(); | ||
151 | #if DEBUG > 0 | ||
152 | g_debug("| entity: %s", str); | ||
153 | #endif | ||
154 | } | ||
155 | |||
156 | #if DEBUG > 0 | ||
157 | if(chunk_info) | ||
158 | g_debug("\\ %s[%+4d]: %s%s%s (line %d)", | ||
159 | key ? " " : "", | ||
160 | key, chunk_info->description, | ||
161 | key ? "" : ": ", | ||
162 | key ? "" : str, | ||
163 | g3d_stream_line(global->stream)); | ||
164 | else | ||
165 | g_warning("unknown chunk type %d in line %d", key, | ||
166 | g3d_stream_line(global->stream)); | ||
167 | #endif | ||
168 | |||
169 | if(chunk_info) { | ||
170 | dxf_read_chunk(global, chunk_info, eprop); | ||
171 | } /* chunk_info */ | ||
172 | else { | ||
173 | DXF_HANDLE_UNKNOWN(global, key, str, section); | ||
174 | } | ||
175 | |||
176 | pcnt = (G3DFloat)g3d_stream_tell(global->stream) / | ||
177 | (G3DFloat)g3d_stream_size(global->stream); | ||
178 | if((pcnt - prev_pcnt) > 0.01) { | ||
179 | prev_pcnt = pcnt; | ||
180 | g3d_context_update_progress_bar(global->context, pcnt, TRUE); | ||
181 | } | ||
182 | g3d_context_update_interface(global->context); | ||
183 | } /* endless loop */ | ||
184 | |||
185 | g_free(edata); | ||
186 | return FALSE; | ||
187 | } | ||
188 | |||
189 | gboolean dxf_section_HEADER(DxfGlobalData *global) | ||
190 | { | ||
191 | #if 0 | ||
192 | return dxf_parse_chunks(global, dxf_chunks, DXF_ID_HEADER, "HEADER"); | ||
193 | #else | ||
194 | return dxf_skip_section(global); | ||
195 | #endif | ||
196 | } | ||
197 | |||
198 | gboolean dxf_section_ENTITIES(DxfGlobalData *global) | ||
199 | { | ||
200 | return dxf_parse_chunks(global, dxf_chunks, DXF_ID_ENTITIES, "ENTITIES"); | ||
201 | } | ||
202 | |||
203 | gboolean dxf_section_BLOCKS(DxfGlobalData *global) | ||
204 | { | ||
205 | return dxf_parse_chunks(global, dxf_chunks, DXF_ID_BLOCKS, "BLOCKS"); | ||
206 | } | ||
207 | |||
208 | gboolean dxf_section_TABLES(DxfGlobalData *global) | ||
209 | { | ||
210 | #if 0 | ||
211 | return dxf_parse_chunks(global, dxf_chunks, DXF_ID_TABLES, "TABLES"); | ||
212 | #else | ||
213 | return dxf_skip_section(global); | ||
214 | #endif | ||
215 | } | ||
216 | |||
217 | gboolean dxf_section_OBJECTS(DxfGlobalData *global) | ||
218 | { | ||
219 | #if 0 | ||
220 | return dxf_parse_chunks(global, dxf_chunks, DXF_ID_OBJECTS, "OBJECTS"); | ||
221 | #else | ||
222 | return dxf_skip_section(global); | ||
223 | #endif | ||
224 | } | ||
225 | |||
226 | gboolean dxf_section_CLASSES(DxfGlobalData *global) | ||
227 | { | ||
228 | #if 0 | ||
229 | return dxf_parse_chunks(global, dxf_chunks, DXF_ID_CLASSES, "TABLES"); | ||
230 | #else | ||
231 | return dxf_skip_section(global); | ||
232 | #endif | ||
233 | } | ||