Statistics
| Revision:

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

History | View | Annotate | Download (13.3 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
import org.cresques.px.Extent;
24
import org.cresques.px.dxf.DxfEntityMaker;
25

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

    
33
public class DxfFile extends GeoFile {
34
        
35
        long lineNr = 0;
36

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

    
170
                                        v.clear();
171
                                        v.add(grp);
172
                                }
173
                                while (true) {
174
                                        grp = readGrp();
175
                                        if (grp.code == 0)        break;
176
                                        v.add(grp);
177
                                }
178
                                table.add(v);
179
                        } else {
180
                                System.err.println("Dxf: Error de secuencia");
181
                                grp = readGrp();        
182
                        }
183
                }
184
                System.out.println("Dxf: Seccion TABLAS: " + layerCnt + " Capas. ");
185
        }
186
        
187
        private void readAnySection() throws NumberFormatException, IOException {
188
                System.out.println("Dxf: Seccion '"+((String) grp.getData())+"', linea "+ l);
189
                while (true) {
190
                        grp = readGrp();
191
                        if (grp.equals(0, "ENDSEC")) break;
192
                        else if (grp.equals(0, "EOF")) break;
193
                }
194
        }
195
        
196
        private void readEntities() throws NumberFormatException, Exception {
197
                System.out.println("Dxf: Seccion ENTITIES, linea "+ l);
198
                int entityCnt = 0;
199
                int cntVeces = 0;
200
                DxfGroupVector v = new DxfGroupVector();
201
                grp = readGrp();
202
                while (true) {
203
                        if (grp.equals(0, "ENDSEC")) break;
204
                        else if (grp.equals(0, "EOF")) break;
205
                        else if (grp.code == 0) {
206
                                if (v.size() > 0) {
207
                                        String lastEntity = (String) ((DxfGroup) v.get(0)).data;
208
                                        if (lastEntity.compareTo("POLYLINE") == 0) {
209
                                                entityMaker.createPolyline(v);
210
                                        } else if (lastEntity.compareTo("VERTEX") == 0) {
211
                                                entityMaker.addVertex(v);
212
                                        } else if (lastEntity.compareTo("SEQEND") == 0) {
213
                                                entityMaker.endSeq();
214
                                        } else if (lastEntity.compareTo("LWPOLYLINE") == 0) {
215
                                                entityMaker.createLwPolyline(v);
216
                                        } else if (lastEntity.compareTo("LINE") == 0) {
217
                                                entityMaker.createLine(v);
218
                                        } else if (lastEntity.compareTo("TEXT") == 0) {
219
                                                entityMaker.createText(v);
220
                                        } else if (lastEntity.compareTo("SOLID") == 0) {
221
                                                entityMaker.createSolid(v);
222
                                        } else
223
                                                System.err.println("Dxf: Entidad "+lastEntity+" desconocida.");
224
                                }
225
                                v.clear();
226
                                v.add(grp);
227
                                while (true) {
228
                                        grp = readGrp();
229
                                        if (grp.code == 0)        break;
230
                                        v.add(grp);
231
                                }
232
                                entityCnt++;
233
                        }
234
                        cntVeces++;
235
                }
236
                System.out.println("Dxf: Seccion ENTITIES" + entityCnt + " Entidades, "+ cntVeces + " veces.");
237
                //System.out.println("Seccion ENTITIES, linea "+ l+ " (SALGO)");
238
        }
239
        
240
        public void save(String fName) throws IOException {
241
                long t2, t1;
242
                t1 = getTime();
243
                fName = DataSource.normalize(fName);
244
                FileWriter fw = new FileWriter(fName);
245
                // COMMENTAIRES DU TRADUCTEUR
246
//                fw.write(DxfGroup.toString(999, Integer.toString(features.size()) + " features")); 
247
                fw.write(DxfGroup.toString(999, "TRANSLATION BY geo.cresques.io.DxfFile"));
248
                fw.write(DxfGroup.toString(999, "DATE : " + (new Date()).toString()));
249
                writeHeader(fw);
250
                writeTables(fw);
251
                writeEntities(fw);
252
                fw.write(DxfGroup.toString(0, "EOF"));
253
                fw.flush();
254
                fw.close();
255
                t2 = getTime();
256
                System.out.println("DxfFile.save(): Tiempo salvando: " + (t2-t1)/1000 + " seg.");
257
        }
258
        
259
        public void writeHeader(FileWriter fw) throws IOException {
260
                fw.write(DxfGroup.toString(0, "SECTION"));
261
                fw.write(DxfGroup.toString(2, "HEADER"));
262
                fw.write(DxfGroup.toString(9, "$ACADVER"));
263
                        fw.write(DxfGroup.toString(1, "AC1009"));
264
                fw.write(DxfGroup.toString(9, "$INSBASE"));
265
                        fw.write(DxfGroup.toString(10, 0.0, 1));
266
                        fw.write(DxfGroup.toString(20, 0.0, 1));
267
                        fw.write(DxfGroup.toString(30, 0.0, 1));
268
                fw.write(DxfGroup.toString(9, "$EXTMIN"));
269
                        fw.write(DxfGroup.toString(10, extent.minX(), 6));
270
                        fw.write(DxfGroup.toString(20, extent.minY(), 6));
271
                        fw.write(DxfGroup.toString(30, 0.0, 6));
272
                fw.write(DxfGroup.toString(9, "$EXTMAX"));
273
                        fw.write(DxfGroup.toString(10, extent.maxX(), 6));
274
                        fw.write(DxfGroup.toString(20, extent.maxY(), 6));
275
                        fw.write(DxfGroup.toString(30, 0.0, 6));
276
                fw.write(DxfGroup.toString(9, "$LIMMIN"));
277
                        fw.write(DxfGroup.toString(10, extent.minX(), 6));
278
                        fw.write(DxfGroup.toString(20, extent.minY(), 6));
279
                fw.write(DxfGroup.toString(9, "$LIMMAX"));
280
                        fw.write(DxfGroup.toString(10, extent.maxX(), 6));
281
                        fw.write(DxfGroup.toString(20, extent.maxY(), 6));
282
                fw.write(DxfGroup.toString(9, "$ORTHOMODE")+DxfGroup.toString(70, 0));
283
                fw.write(DxfGroup.toString(9, "$REGENMODE")+DxfGroup.toString(70, 1));
284
                fw.write(DxfGroup.toString(9, "$FILLMODE")+        DxfGroup.toString(70, 1));
285
                fw.write(DxfGroup.toString(9, "$QTEXTMODE")+DxfGroup.toString(70, 0));
286
                fw.write(DxfGroup.toString(9, "$MIRRTEXT")+        DxfGroup.toString(70, 1));
287
                fw.write(DxfGroup.toString(9, "$DRAGMODE")+        DxfGroup.toString(70, 2));
288
                fw.write(DxfGroup.toString(9, "$LTSCALE")+        DxfGroup.toString(40, 1.0, 1));
289
                fw.write(DxfGroup.toString(9, "$OSMODE")+        DxfGroup.toString(70, 0));
290
                fw.write(DxfGroup.toString(9, "$ATTMODE")+        DxfGroup.toString(70, 1));
291
                fw.write(DxfGroup.toString(9, "$TEXTSIZE")+        DxfGroup.toString(7, "STANDARD"));
292
                fw.write(DxfGroup.toString(9, "$CLAYER")+        DxfGroup.toString(8, "0"));
293
                fw.write(DxfGroup.toString(9, "$CELTYPE")+        DxfGroup.toString(6, "CONTINUOUS"));
294
                fw.write(DxfGroup.toString(9, "$CECOLOR")+        DxfGroup.toString(62, 256));
295
                fw.write(DxfGroup.toString(9, "$DIMSCALE")+        DxfGroup.toString(40, 1.0, 1));
296
                fw.write(DxfGroup.toString(9, "$DIMASZ")+        DxfGroup.toString(40, 0.18, 2));
297
                fw.write(DxfGroup.toString(9, "$DIMEXO")+        DxfGroup.toString(40, 0.0625, 4));
298
                fw.write(DxfGroup.toString(9, "$DIMDLI")+        DxfGroup.toString(40, 0.38, 2));
299
                fw.write(DxfGroup.toString(9, "$DIMRND")+        DxfGroup.toString(40, 0.0, 1));
300
                fw.write(DxfGroup.toString(9, "$DIMDLE")+        DxfGroup.toString(40, 0.0, 1));
301
                fw.write(DxfGroup.toString(9, "$DIMEXE")+        DxfGroup.toString(40, 0.18, 2));
302
                fw.write(DxfGroup.toString(9, "$DIMTP")+        DxfGroup.toString(40, 0.0, 1));
303
                fw.write(DxfGroup.toString(9, "$DIMTM")+        DxfGroup.toString(40, 0.0, 1));
304
                fw.write(DxfGroup.toString(9, "$DIMTXT")+        DxfGroup.toString(40, 0.18, 2));
305
                fw.write(DxfGroup.toString(9, "$DIMCEN")+        DxfGroup.toString(40, 0.09, 2));
306
                fw.write(DxfGroup.toString(9, "$DIMTSZ")+        DxfGroup.toString(40, 0.0, 1));
307
                
308
                fw.write(DxfGroup.toString(9, "$ELEVATION"));
309
                        fw.write(DxfGroup.toString(40, 0.0, 3));
310
                fw.write(DxfGroup.toString(9, "$LIMCHECK"));
311
                        fw.write(DxfGroup.toString(70, 1));
312
                fw.write(DxfGroup.toString(9, "$LUNITS"));
313
                        fw.write(DxfGroup.toString(70, 2));
314
                fw.write(DxfGroup.toString(9, "$LUPREC"));
315
                        fw.write(DxfGroup.toString(70, 2));
316
                fw.write(DxfGroup.toString(0, "ENDSEC"));
317
        }
318
        
319
        public void writeTables(FileWriter fw) throws IOException {
320
                fw.write(DxfGroup.toString(0, "SECTION"));
321
                fw.write(DxfGroup.toString(2, "TABLES"));
322
                //writeStyleTable(fw);
323
                //writeLTypeTable(fw);
324
                //writeAppidTable(fw);
325
                writeLayerTable(fw);
326
                fw.write(DxfGroup.toString(0, "ENDSEC"));
327

    
328
                fw.write(DxfGroup.toString(0, "ENDBLK"));
329
        }
330
        /*
331
         *   0
332
TABLE
333
  2
334
APPID
335
  5
336
14
337
100
338
AcDbSymbolTable
339
 70
340
     1
341
  0
342
APPID
343
  5
344
15
345
100
346
AcDbSymbolTableRecord
347
100
348
AcDbRegAppTableRecord
349
  2
350
ACAD
351
 70
352
     0
353
  0
354
ENDTAB
355
         */
356
        public void writeAppidTable(FileWriter fw) throws IOException {
357
                fw.write(DxfGroup.toString(0, "TABLE"));
358
                fw.write(DxfGroup.toString(2, "APPID"));
359
                fw.write(DxfGroup.toString(5, 14));
360
                fw.write(DxfGroup.toString(100, "AcDbSymbolTable"));
361
                fw.write(DxfGroup.toString(0, "APPID"));
362
                fw.write(DxfGroup.toString(5, 15));
363
                fw.write(DxfGroup.toString(100, "AcDbSymbolTableRecord"));
364
                fw.write(DxfGroup.toString(100, "AcDbRegAppTableRecord"));
365
                fw.write(DxfGroup.toString(2, "ACAD"));
366
                fw.write(DxfGroup.toString(70, 1));
367
                fw.write(DxfGroup.toString(0, "ENDTAB"));
368
        }
369
        
370
        /*public void writeStyleTable(FileWriter fw) throws IOException {
371
                fw.write(DxfGroup.toString(0, "TABLE"));
372
                fw.write(DxfGroup.toString(2, "STYLE"));
373
                fw.write(DxfGroup.toString(70, 1));
374
                DxfTABLE_STYLE_ITEM style =
375
                        new DxfTABLE_STYLE_ITEM("STANDARD", 0, 0f, 1f, 0f, 0, 1.0f, "xxx.txt", "yyy.txt");
376
                fw.write(style.toString());
377
                fw.write(DxfGroup.toString(0, "ENDTAB"));
378
        }*/
379
        
380
        /*public void writeLTypeTable(FileWriter fw) throws IOException {
381
                fw.write(DxfGroup.toString(0, "TABLE"));
382
                fw.write(DxfGroup.toString(2, "LTYPE"));
383
                fw.write(DxfGroup.toString(70, 1));
384
                DxfTABLE_LTYPE_ITEM ltype =
385
                        new DxfTABLE_LTYPE_ITEM("CONTINUE", 0, "", 65, 0f, new float[0]);
386
                fw.write(ltype.toString());
387
                fw.write(DxfGroup.toString(0, "ENDTAB"));
388
        }*/
389
        
390
        public void writeLayerTable(FileWriter fw) throws IOException {
391
                fw.write(DxfGroup.toString(0, "TABLE"));
392
                fw.write(DxfGroup.toString(2, "LAYER"));
393
                //                fw.write(DxfGroup.toString(70, 2));
394
                
395

    
396
                //layer = new DxfLayer(layerName, 0, 131, "CONTINUOUS");
397
                //fw.write(layer.toString());
398
                fw.write(((DxfEntityMaker) entityMaker).getLayers().toDxfString());
399
                                
400
                fw.write(DxfGroup.toString(0, "ENDTAB"));
401
        }
402

    
403
        public void writeEntities(FileWriter fw) throws IOException {
404
                // ECRITURE DES FEATURES
405
                fw.write(DxfGroup.toString(0, "SECTION"));
406
                fw.write(DxfGroup.toString(2, "ENTITIES"));                
407
                fw.write(((DxfEntityMaker) entityMaker).getEntities().toDxfString());
408
                fw.write(DxfGroup.toString(0, "ENDSEC"));
409
                fw.write(DxfGroup.toString(0, "ENDBLK"));
410
        }
411
        
412
        public void reProject(ReProjection rp) {
413
                System.out.println("Dxf: reproyectando ...");
414
                entityMaker.reProject(rp);
415
        }
416
}