Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / dxf / DXFMemoryDriver.java @ 839

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

    
320
        /* (non-Javadoc)
321
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#accept(java.io.File)
322
         */
323
        public boolean accept(File f) {
324
                return f.getName().toUpperCase().endsWith("DXF");
325
        }
326

    
327
        /* (non-Javadoc)
328
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getShapeType()
329
         */
330
        public int getShapeType() {
331
                return FShape.MULTI;
332
        }
333

    
334
        /* (non-Javadoc)
335
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getName()
336
         */
337
        public String getName() {
338
                return "gvSIG DXF Memory Driver";
339
        }
340

    
341
        /* (non-Javadoc)
342
         * @see com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend#getDefaultLegend()
343
         */
344
        public Legend getDefaultLegend() {
345
                return defaultLegend;
346
        }
347

    
348
}