Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / dxf / DXFMemoryDriver.java @ 881

History | View | Annotate | Download (14.4 KB)

1
/*
2
 * @(#)DXFMemoryDriver    29-dic-2004
3
 * 
4
 * @author jmorell (jose.morell@gmail.com)
5
 */
6
package com.iver.cit.gvsig.fmap.drivers.dxf;
7

    
8
import java.awt.Color;
9
import java.awt.Font;
10
import java.awt.geom.Point2D;
11
import java.io.File;
12
import java.io.IOException;
13
import java.util.ArrayList;
14

    
15
import org.cresques.cts.IProjection;
16
import org.cresques.cts.ProjectionPool;
17
import org.cresques.io.DxfFile;
18
import org.cresques.px.IObjList;
19
import org.cresques.px.dxf.AcadColor;
20
import org.cresques.px.dxf.DxfFeatureMaker;
21
import org.cresques.px.dxf.DxfHeaderManager;
22
import org.cresques.px.gml.Feature;
23
import org.cresques.px.gml.InsPoint;
24
import org.cresques.px.gml.LineString;
25
import org.cresques.px.gml.Point;
26
import org.cresques.px.gml.Polygon;
27

    
28
import com.hardcode.gdbms.engine.data.DriverException;
29
import com.hardcode.gdbms.engine.data.ReadDriver;
30
import com.hardcode.gdbms.engine.values.IntValue;
31
import com.hardcode.gdbms.engine.values.Value;
32
import com.hardcode.gdbms.engine.values.ValueFactory;
33
import com.iver.cit.gvsig.fmap.core.FPoint3D;
34
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
35
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
36
import com.iver.cit.gvsig.fmap.core.FShape;
37
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
38
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
39
import com.iver.cit.gvsig.fmap.drivers.MemoryDriver;
40
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
41
import com.iver.cit.gvsig.fmap.rendering.Legend;
42
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
43
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
44

    
45
/**
46
 * @author jmorell (jose.morell@gmail.com)
47
 * @version 29-dic-2004
48
 */
49
public class DXFMemoryDriver extends MemoryDriver implements WithDefaultLegend {
50
        private final int ID_FIELD_ID=0;
51
        private final int ID_FIELD_ENTITY=1;
52
        private final int ID_FIELD_LAYER=2;
53
        private final int ID_FIELD_COLOR=3;
54
        private final int ID_FIELD_ELEVATION=4;
55
        private final int ID_FIELD_THICKNESS=5;
56
        private final int ID_FIELD_TEXT=6;
57
        private final int ID_FIELD_HEIGHTTEXT=7;
58
        private final int ID_FIELD_ROTATIONTEXT=8;
59

    
60
        VectorialUniqueValueLegend defaultLegend;
61
        private String path;
62
        private File m_Fich;
63
        
64
        private DxfFile.EntityFactory featureMaker;
65
        private DxfFile dxfFeatureFile;
66
        private IObjList.vector features;
67
        
68
        /**
69
         * Habilita la utilizaci?n del lector del HEADER del DXF.
70
         */
71
        private DxfFile.VarSettings headerManager;
72

    
73
        /* (non-Javadoc)
74
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#open(java.io.File)
75
         */
76
        public void open(File f) throws IOException {
77
            m_Fich = f;
78
        }
79
        
80
        public void initialize() throws IOException {
81
                IProjection proj = ProjectionPool.get("ed50utm30");
82
                featureMaker = new DxfFeatureMaker(proj);
83
                dxfFeatureFile = new DxfFile(proj, m_Fich.getAbsolutePath(), featureMaker);
84
                dxfFeatureFile.load();
85
                features = (IObjList.vector)((DxfFeatureMaker)featureMaker).getObjects();
86
                
87
                //String acadVersion = (String)((DxfHeaderManager)headerManager).getAcadVersion();
88
                //System.out.println("initialize(): acadVersion = " + acadVersion);
89
                
90
                int nAtt = featureMaker.getAttributes().size();
91
            Value[] auxRow = new Value[6+nAtt];
92
                ArrayList arrayFields = new ArrayList();
93
                arrayFields.add("ID");
94
                arrayFields.add("Entity");
95
                arrayFields.add("Layer");
96
                arrayFields.add("Color");
97
                arrayFields.add("Elevation");
98
                arrayFields.add("Thickness");
99
                for (int i=0;i<nAtt;i++) {
100
                        String att[] = new String[2];
101
                        att = (String[])featureMaker.getAttributes().get(i);
102
                        arrayFields.add(att[0]);
103
                }
104
                
105
                // Campos de las MemoryLayer:
106
            Value[] auxRowTxt = new Value[9+nAtt];
107
                ArrayList arrayFieldsTxt = new ArrayList();
108
                arrayFieldsTxt.add("ID");
109
                arrayFieldsTxt.add("Entity");
110
                arrayFieldsTxt.add("Layer");
111
                arrayFieldsTxt.add("Color");
112
                arrayFieldsTxt.add("Elevation");
113
                arrayFieldsTxt.add("Thickness");
114
                arrayFieldsTxt.add("Text");
115
                arrayFieldsTxt.add("HeightText");
116
                arrayFieldsTxt.add("RotationText");
117
                for (int i=0;i<nAtt;i++) {
118
                        String att[] = new String[2];
119
                        att = (String[])featureMaker.getAttributes().get(i);
120
                        arrayFieldsTxt.add(att[0]);
121
                }
122
                
123
            getTableModel().setColumnIdentifiers(arrayFields.toArray());
124
                
125
                for (int i=0;i<features.size();i++) {
126
                        
127
            auxRowTxt[ID_FIELD_HEIGHTTEXT] = ValueFactory.createValue(0);
128
            auxRowTxt[ID_FIELD_ROTATIONTEXT] = ValueFactory.createValue(0);
129
            auxRowTxt[ID_FIELD_TEXT] = ValueFactory.createNullValue();
130
                        
131
                        Feature fea = (Feature)features.get(i);
132
                        if (fea.getGeometry() instanceof Point) {
133
                                Point point = (Point)fea.getGeometry();
134
                                Point2D pto = new Point2D.Double();
135
                                pto = (Point2D.Double)point.get(0);
136
                                FShape nuevoShp;
137
                if (point.isText) {
138
                        //auxRowTxt[ID_FIELD_ID] = new Integer(i);
139
                                        auxRowTxt[ID_FIELD_ID] = ValueFactory.createValue(i);
140
                        auxRowTxt[ID_FIELD_ENTITY] = ValueFactory.createValue(new String(fea.getProp("dxfEntity")));
141
                        auxRowTxt[ID_FIELD_LAYER] = ValueFactory.createValue(new String(fea.getProp("layer")));
142
                        int auxInt = Integer.parseInt(fea.getProp("color"));
143
                        auxRowTxt[ID_FIELD_COLOR] = ValueFactory.createValue(auxInt);
144
                        auxRowTxt[ID_FIELD_TEXT] = ValueFactory.createValue(new String(fea.getProp("text")));
145
                        double auxH = Double.parseDouble(fea.getProp("textHeight"));
146
                        auxRowTxt[ID_FIELD_HEIGHTTEXT] = ValueFactory.createValue(auxH);
147
                        double auxR = Double.parseDouble(fea.getProp("textRotation"));
148
                        auxRowTxt[ID_FIELD_ROTATIONTEXT] = ValueFactory.createValue(auxR);
149
                        double auxE = Double.parseDouble(fea.getProp("elevation"));
150
                        auxRowTxt[ID_FIELD_ELEVATION] = ValueFactory.createValue(auxE);
151
                        double auxT = Double.parseDouble(fea.getProp("thickness"));
152
                        auxRowTxt[ID_FIELD_THICKNESS] = ValueFactory.createValue(auxT);
153
                        // Attributes
154
                        for (int j=0;j<nAtt;j++) {
155
                                            String[] attributes = new String[2];
156
                                            attributes = (String[])featureMaker.getAttributes().get(j);
157
                                auxRowTxt[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
158
                                if (!fea.getProp(attributes[0]).equals(attributes[1])) {
159
                                        auxRowTxt[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
160
                                }
161
                        }
162
                                        nuevoShp = new FPoint3D(pto.getX(),pto.getY(),0);
163
                                        addShape(nuevoShp, auxRowTxt);
164
                } else {
165
                        auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
166
                        auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String(fea.getProp("dxfEntity")));
167
                        auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String(fea.getProp("layer")));
168
                        int auxInt = Integer.parseInt(fea.getProp("color"));
169
                        auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(auxInt);
170
                        double auxE = Double.parseDouble(fea.getProp("elevation"));
171
                        auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(auxE);
172
                        double auxT = Double.parseDouble(fea.getProp("thickness"));
173
                        auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(auxT);
174
                        // Attributes
175
                        for (int j=0;j<nAtt;j++) {
176
                                            String[] attributes = new String[2];
177
                                            attributes = (String[])featureMaker.getAttributes().get(j);
178
                                auxRow[6+j] = ValueFactory.createValue(new String((String)attributes[1]));
179
                                if (!fea.getProp(attributes[0]).equals(attributes[1])) {
180
                                        auxRow[6+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
181
                                }
182
                        }
183
                        nuevoShp = new FPoint3D(pto.getX(),pto.getY(),0);                                
184
                                        addShape(nuevoShp, auxRow);
185
                }
186
                        } else if (fea.getGeometry() instanceof InsPoint) {
187
                                InsPoint insPoint = (InsPoint)fea.getGeometry();
188
                                Point2D pto = new Point2D.Double();
189
                                pto = (Point2D.Double)insPoint.get(0);
190
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
191
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String(fea.getProp("dxfEntity")));
192
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String(fea.getProp("layer")));
193
                int auxInt = Integer.parseInt(fea.getProp("color"));
194
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(auxInt);
195
                //auxRow[4] = new String(fea.getProp("text"));
196
                    double auxE = Double.parseDouble(fea.getProp("elevation"));
197
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(auxE);
198
                    // Attributes
199
                    for (int j=0;j<nAtt;j++) {
200
                                    String[] attributes = new String[2];
201
                                    attributes = (String[])featureMaker.getAttributes().get(j);
202
                        auxRow[6+j] = ValueFactory.createValue(new String((String)attributes[1]));
203
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
204
                                auxRow[6+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
205
                        }
206
                    }
207
                                FShape nuevoShp = new FPoint3D(pto.getX(),pto.getY(),0);
208
                                addShape(nuevoShp, auxRow);                                        
209
                        } else if (fea.getGeometry() instanceof LineString) {
210
                                GeneralPathX genPathX = new GeneralPathX();
211
                                Point2D[] pts = new Point2D[fea.getGeometry().pointNr()];
212
                                for (int j=0; j<fea.getGeometry().pointNr(); j++) {
213
                                        pts[j] = (Point2D)fea.getGeometry().get(j);
214
                                }
215
                                genPathX.moveTo(pts[0].getX(), pts[0].getY());
216
                                for (int j=1; j < pts.length; j++) {
217
                                        genPathX.lineTo(pts[j].getX(), pts[j].getY());
218
                                }
219
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
220
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String(fea.getProp("dxfEntity")));
221
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String(fea.getProp("layer")));
222
                int auxInt = Integer.parseInt(fea.getProp("color"));
223
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(auxInt);
224
                    double auxE = Double.parseDouble(fea.getProp("elevation"));
225
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(auxE);
226
                    double auxT = Double.parseDouble(fea.getProp("thickness"));
227
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(auxT);
228
                    // Attributes
229
                    for (int j=0;j<nAtt;j++) {
230
                                    String[] attributes = new String[2];
231
                                    attributes = (String[])featureMaker.getAttributes().get(j);
232
                        auxRow[6+j] = ValueFactory.createValue(new String((String)attributes[1]));
233
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
234
                                auxRow[6+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
235
                        }
236
                    }
237
                                FShape nuevoShp = new FPolyline2D(genPathX);
238
                                addShape(nuevoShp, auxRow);
239
                        } else if (fea.getGeometry() instanceof Polygon) {
240
                                GeneralPathX genPathX = new GeneralPathX();
241
                                Point2D[] pts = new Point2D[fea.getGeometry().pointNr()];
242
                                for (int j=0; j<fea.getGeometry().pointNr(); j++) {
243
                                        pts[j] = (Point2D)fea.getGeometry().get(j);
244
                                }
245
                                genPathX.moveTo(pts[0].getX(), pts[0].getY());
246
                                for (int j=1; j < pts.length; j++) {
247
                                        genPathX.lineTo(pts[j].getX(), pts[j].getY());
248
                                }
249
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
250
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String(fea.getProp("dxfEntity")));
251
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String(fea.getProp("layer")));
252
                int auxInt = Integer.parseInt(fea.getProp("color"));
253
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(auxInt);
254
                    double auxE = Double.parseDouble(fea.getProp("elevation"));
255
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(auxE);
256
                    double auxT = Double.parseDouble(fea.getProp("thickness"));
257
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(auxT);
258
                    // Attributes
259
                    for (int j=0;j<nAtt;j++) {
260
                                    String[] attributes = new String[2];
261
                                    attributes = (String[])featureMaker.getAttributes().get(j);
262
                        auxRow[6+j] = ValueFactory.createValue(new String((String)attributes[1]));
263
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
264
                                auxRow[6+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
265
                        }
266
                    }
267
                                FShape nuevoShp = new FPolygon2D(genPathX);
268
                                addShape(nuevoShp, auxRow);
269
                        } else {
270
                                System.out.println("Detectado feature desconocido");
271
                        }
272
                }
273
                
274
                defaultLegend = LegendFactory.createVectorialUniqueValueLegend(getShapeType());
275
                defaultLegend.setFieldName("Color");
276
                
277
                //defaultLegend.setLabelField("Text");
278
                defaultLegend.setDefaultSymbol(new FSymbol(getShapeType()));
279
                defaultLegend.getDefaultSymbol().setShapeVisible(false);
280
                defaultLegend.getDefaultSymbol().setFontSizeInPixels(false);
281
                defaultLegend.getDefaultSymbol().setFont(new Font("SansSerif", Font.PLAIN, 9));
282
                defaultLegend.getDefaultSymbol().setFontColor(Color.BLACK);
283
                float heightText = 10;
284
                defaultLegend.getDefaultSymbol().setFontSize(heightText);
285
                defaultLegend.setLabelHeightField("HeightText");
286
                defaultLegend.setLabelRotationField("RotationText");
287
                
288
        ReadDriver rs = this;
289
                IntValue clave;
290
                FSymbol theSymbol = null;
291
                
292
                try {
293
                        for (long j = 0; j < rs.getRowCount(); j++)
294
                        {
295
                                System.out.println("initialize(): j = " + j);
296
                                clave =(IntValue) rs.getFieldValue(j,ID_FIELD_COLOR);
297
                                if (defaultLegend.getSymbolByValue(clave) == null)
298
                                {
299
                                        theSymbol = new FSymbol(getShapeType());                                
300
                                        theSymbol.setDescription(clave.toString());
301
                                        theSymbol.setColor(AcadColor.getColor(clave.getValue()));
302
                                        defaultLegend.addSymbol(clave, theSymbol);
303
                                }
304
                        } // for
305
                } catch (DriverException e) {
306
                        e.printStackTrace();
307
                        throw new IOException("Error al poner la legenda por defecto en el DXF");
308
                }
309
        }
310

    
311
        /* (non-Javadoc)
312
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#accept(java.io.File)
313
         */
314
        public boolean accept(File f) {
315
                return f.getName().toUpperCase().endsWith("DXF");
316
        }
317

    
318
        /* (non-Javadoc)
319
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getShapeType()
320
         */
321
        public int getShapeType() {
322
                return FShape.MULTI;
323
        }
324

    
325
        /* (non-Javadoc)
326
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getName()
327
         */
328
        public String getName() {
329
                return "gvSIG DXF Memory Driver";
330
        }
331

    
332
        /* (non-Javadoc)
333
         * @see com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend#getDefaultLegend()
334
         */
335
        public Legend getDefaultLegend() {
336
                return defaultLegend;
337
        }
338

    
339
}