Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libCq CMS for java.old / src / org / cresques / io / DxfFile.java @ 12

History | View | Annotate | Download (9.18 KB)

1
/*
2
 * Created on 30-abr-2004
3
 *
4
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
5
 */
6
 
7
package org.cresques.io;
8

    
9
import java.io.BufferedReader;
10
import java.io.FileNotFoundException;
11
import java.io.FileReader;
12
import java.io.FileWriter;
13
import java.io.IOException;
14
import java.io.InputStream;
15
import java.io.InputStreamReader;
16
import java.io.Reader;
17
import java.util.Date;
18
import java.util.Hashtable;
19
import java.util.Vector;
20

    
21
import org.cresques.geo.Projection;
22
import org.cresques.geo.ReProjection;
23

    
24
/**
25
 * Clase de soporte para ficheros .dxf de AutoCAD
26
 * 
27
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>* @author administrador
28
 * TODO extension .dxf sin implementar
29
 */
30

    
31
public class DxfFile extends GeoFile {
32
        private String name;
33
        
34
        long lineNr = 0;
35

    
36
        String buf = null;
37
        
38
        BufferedReader fi;
39
        long l = 0;
40
        int count = 0;
41
        DxfGroup grp = null;
42
        
43
        EntityFactory entityMaker = null;
44
        
45
        /**
46
         * Crea los objetos en el Modelo correspondiente.
47
         * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
48
         */
49
        public interface EntityFactory {
50
                public void createLayer(DxfGroupVector v) throws Exception ;
51
                public void createPolyline(DxfGroupVector v) throws Exception ;
52
                public void addVertex(DxfGroupVector v) throws Exception ;
53
                public void endSeq() throws Exception ;
54
                public void createLwPolyline(DxfGroupVector v) throws Exception ;
55
                public void createLine(DxfGroupVector v) throws Exception ;
56
                public void createText(DxfGroupVector v) throws Exception ;
57
                public void createSolid(DxfGroupVector v) throws Exception ;
58
                public void reProject(ReProjection rp);
59
        };
60
        
61
        public DxfFile(Projection proj, String name, EntityFactory maker) {
62
                super(proj);
63
                this.name = name;
64
                entityMaker = maker;
65
        }
66
        
67
        /**
68
         * Carga un .gml
69
         */
70
        
71
        public GeoFile load() {
72
                System.out.println("Dxf: Cargando "+name+" ...");
73
                try {
74
                        if (ZipFileFolder.isUrl(name)) {
75
                                ZipFileFolder zFolder = new ZipFileFolder(name);
76
                                InputStream is = zFolder.getInputStream(name);
77
                                return load(new InputStreamReader(is));
78
                        } else 
79
                                return load(new FileReader(name));
80
                } catch (FileNotFoundException fnfe) {
81
                        fnfe.printStackTrace();
82
                } catch (Exception e) {
83
                        System.err.println("Dxf: ERROR."+l+"lineas leidas");
84
                        e.printStackTrace();
85
                }
86
                return this;
87
        }
88
        
89
        public GeoFile load(Reader fr) throws NumberFormatException, Exception {
90
                System.out.println("Dxf: Cargando '"+name+"' ...");
91
                fi = new BufferedReader(fr);
92
                while ((grp = readGrp()) != null) {
93
                        l+=2;
94
                        
95
                        if (grp.equals(0, "EOF"))                break;
96
                        if (grp.equals(0, "SECTION"))
97
                                readSection();
98
                }
99
                fi.close();
100
                System.out.println("Dxf: '"+name+"' cargado. ("+l+" l?neas).");
101
                this.lineNr = l;
102
                //extent.add(collection.getExtent());
103
                return this;
104
        }
105
        
106
        private DxfGroup readGrp() throws NumberFormatException, IOException {
107
                DxfGroup g = DxfGroup.read(fi);
108
                if (g != null) l += 2;
109
                return g;
110
        }
111
        
112
        private void readSection() throws NumberFormatException, Exception {
113
                while (true) {
114
                        grp = readGrp();
115
                        if (grp.code == 2)
116
                                if (((String) grp.data).compareTo("HEADER") == 0)
117
                                        readAnySection();
118
                                else if (((String) grp.data).compareTo("CLASSES") == 0)
119
                                        readAnySection();
120
                                else if (((String) grp.data).compareTo("TABLES") == 0)
121
                                        readTables();
122
                                else if (((String) grp.data).compareTo("BLOCKS") == 0)
123
                                        readAnySection();
124
                                else if (((String) grp.data).compareTo("ENTITIES") == 0)
125
                                        readEntities();
126
                                else if (((String) grp.data).compareTo("OBJECTS") == 0)
127
                                        readAnySection();
128
                                else {
129
                                        System.out.println("DxfRead: Seccion "+grp.data);
130
                                        readAnySection();
131
                                }
132
                        else
133
                                System.err.println("Dxf: Codigo/Seccion desconocidos" + grp);
134
                        if (grp.equals(0, "EOF")) break;
135
                        if (grp.equals(0, "ENDSEC")) break;
136
                }                
137
        }
138
        
139
        private void readTables() throws NumberFormatException, Exception {
140
                System.out.println("Dxf: Seccion TABLAS, linea "+ l);
141
                int layerCnt = 0;
142
                String tableAct = "NONAME";
143
                
144
                Hashtable tables = new Hashtable();
145
                Vector table = new Vector();
146
                DxfGroupVector v = new DxfGroupVector();
147
                
148
                grp = readGrp();
149
                while (true) {
150
                        if (grp.code == 0) {
151
                                String data = (String) grp.getData();
152
                                if (data.compareTo("ENDSEC") == 0 || data.compareTo("EOF") == 0)
153
                                        break;
154
                                else if (data.compareTo("ENDTAB") == 0) {
155
                                        tables.put(tableAct, table);
156
                                        table = new Vector();
157
                                        grp = readGrp();
158
                                        continue;
159
                                } else {
160
                                        if (table.size()==1) {
161
                                                tableAct = v.getDataAsString(2);
162
                                                System.out.println("Dxf: Tabla "+tableAct);
163
                                        } else
164
                                                if (tableAct.compareTo("LAYER") == 0 && v.size()>0) {
165
                                                        entityMaker.createLayer(v);
166
                                                        layerCnt++;
167
                                                }
168

    
169
                                        v.clear();
170
                                        v.add(grp);
171
                                }
172
                                while (true) {
173
                                        grp = readGrp();
174
                                        if (grp.code == 0)        break;
175
                                        v.add(grp);
176
                                }
177
                                table.add(v);
178
                        } else {
179
                                System.err.println("Dxf: Error de secuencia");
180
                                grp = readGrp();        
181
                        }
182
                }
183
                System.out.println("Dxf: Seccion TABLAS: " + layerCnt + " Capas. ");
184
        }
185
        
186
        private void readAnySection() throws NumberFormatException, IOException {
187
                System.out.println("Dxf: Seccion '"+((String) grp.getData())+"', linea "+ l);
188
                while (true) {
189
                        grp = readGrp();
190
                        if (grp.equals(0, "ENDSEC")) break;
191
                        else if (grp.equals(0, "EOF")) break;
192
                }
193
        }
194
        
195
        private void readEntities() throws NumberFormatException, Exception {
196
                System.out.println("Dxf: Seccion ENTITIES, linea "+ l);
197
                int entityCnt = 0;
198
                int cntVeces = 0;
199
                DxfGroupVector v = new DxfGroupVector();
200
                grp = readGrp();
201
                while (true) {
202
                        if (grp.equals(0, "ENDSEC")) break;
203
                        else if (grp.equals(0, "EOF")) break;
204
                        else if (grp.code == 0) {
205
                                if (v.size() > 0) {
206
                                        String lastEntity = (String) ((DxfGroup) v.get(0)).data;
207
                                        if (lastEntity.compareTo("POLYLINE") == 0) {
208
                                                entityMaker.createPolyline(v);
209
                                        } else if (lastEntity.compareTo("VERTEX") == 0) {
210
                                                entityMaker.addVertex(v);
211
                                        } else if (lastEntity.compareTo("SEQEND") == 0) {
212
                                                entityMaker.endSeq();
213
                                        } else if (lastEntity.compareTo("LWPOLYLINE") == 0) {
214
                                                entityMaker.createLwPolyline(v);
215
                                        } else if (lastEntity.compareTo("LINE") == 0) {
216
                                                entityMaker.createLine(v);
217
                                        } else if (lastEntity.compareTo("TEXT") == 0) {
218
                                                entityMaker.createText(v);
219
                                        } else if (lastEntity.compareTo("SOLID") == 0) {
220
                                                entityMaker.createSolid(v);
221
                                        } else
222
                                                System.err.println("Dxf: Entidad "+lastEntity+" desconocida.");
223
                                }
224
                                v.clear();
225
                                v.add(grp);
226
                                while (true) {
227
                                        grp = readGrp();
228
                                        if (grp.code == 0)        break;
229
                                        v.add(grp);
230
                                }
231
                                entityCnt++;
232
                        }
233
                        cntVeces++;
234
                }
235
                System.out.println("Dxf: Seccion ENTITIES" + entityCnt + " Entidades, "+ cntVeces + " veces.");
236
                //System.out.println("Seccion ENTITIES, linea "+ l+ " (SALGO)");
237
        }
238
        
239
        public void save(String fName) throws IOException {
240
                fName = DataSource.normalize(fName);
241
                FileWriter fw = new FileWriter(fName);
242
                writeHeader(fw);
243
                writeSections(fw);
244
                writeEntities(fw);
245
                fw.close();
246
        }
247
        
248
        public void writeHeader(FileWriter fw) throws IOException {
249
                        // COMMENTAIRES DU TRADUCTEUR
250
//                        fw.write(DxfGroup.toString(999, Integer.toString(features.size()) + " features")); 
251
                        fw.write(DxfGroup.toString(999, "TRANSLATION BY geo.cresques.io.DxfFile"));
252
                        fw.write(DxfGroup.toString(999, "DATE : " + (new Date()).toString()));
253
                        // ECRITURE DU HEADER
254
                        fw.write(DxfGroup.toString(0, "SECTION"));
255
                        fw.write(DxfGroup.toString(2, "HEADER"));
256
                        fw.write(DxfGroup.toString(9, "$ACADVER"));
257
                                fw.write(DxfGroup.toString(1, "AC1009"));
258
                        fw.write(DxfGroup.toString(9, "$CECOLOR"));
259
                                fw.write(DxfGroup.toString(62, 256));
260
                        fw.write(DxfGroup.toString(9, "$CELTYPE"));
261
                                fw.write(DxfGroup.toString(6, "DUPLAN"));
262
                        fw.write(DxfGroup.toString(9, "$CLAYER"));
263
                                fw.write(DxfGroup.toString(8, 0));
264
                        fw.write(DxfGroup.toString(9, "$ELEVATION"));
265
                                fw.write(DxfGroup.toString(40, 0.0, 3));
266
                        fw.write(DxfGroup.toString(9, "$EXTMAX"));
267
                                fw.write(DxfGroup.toString(10, extent.maxX(), 6));
268
                                fw.write(DxfGroup.toString(20, extent.maxX(), 6));
269
                                //fw.write(DxfGroup.toString(30, envelope.getMaxX(), 6));
270
                        fw.write(DxfGroup.toString(9, "$EXTMIN"));
271
                                fw.write(DxfGroup.toString(10, extent.minX(), 6));
272
                                fw.write(DxfGroup.toString(20, extent.minX(), 6));
273
                                //fw.write(DxfGroup.toString(30, envelope.getMaxX(), 6));
274
                        fw.write(DxfGroup.toString(9, "$INSBASE"));
275
                                fw.write(DxfGroup.toString(10, 0.0, 1));
276
                                fw.write(DxfGroup.toString(20, 0.0, 1));
277
                                fw.write(DxfGroup.toString(30, 0.0, 1));
278
                        fw.write(DxfGroup.toString(9, "$LIMCHECK"));
279
                                fw.write(DxfGroup.toString(70, 1));
280
                        fw.write(DxfGroup.toString(9, "$LIMMAX"));
281
                                fw.write(DxfGroup.toString(10, extent.maxX(), 6));
282
                                fw.write(DxfGroup.toString(20, extent.maxX(), 6));
283
                        fw.write(DxfGroup.toString(9, "$LIMMIN"));
284
                                fw.write(DxfGroup.toString(10, extent.minX(), 6));
285
                                fw.write(DxfGroup.toString(20, extent.minX(), 6));
286
                        fw.write(DxfGroup.toString(9, "$LUNITS"));
287
                                fw.write(DxfGroup.toString(70, 2));
288
                        fw.write(DxfGroup.toString(9, "$LUPREC"));
289
                                fw.write(DxfGroup.toString(70, 2));
290
                        fw.write(DxfGroup.toString(0, "ENDSEC"));
291

    
292
        }
293
        
294
        public void writeSections(FileWriter fw) throws IOException {
295
                writeTables(fw);
296
        }
297
        
298
        public void writeTables(FileWriter fw) throws IOException {
299
        }
300

    
301
        public void writeEntities(FileWriter fw) throws IOException {
302
        }
303
        
304
        public void reProject(ReProjection rp) {
305
                System.out.println("Dxf: reproyectando ...");
306
                entityMaker.reProject(rp);
307
        }
308
}