Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / dwg / DwgMemoryDriver.java @ 1868

History | View | Annotate | Download (52.9 KB)

1
/*
2
 * Created on 14-abr-2005
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program 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
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
package com.iver.cit.gvsig.fmap.drivers.dwg;
45

    
46
import java.awt.Color;
47
import java.awt.Font;
48
import java.awt.geom.Point2D;
49
import java.io.File;
50
import java.io.IOException;
51
import java.util.ArrayList;
52
import java.util.Vector;
53

    
54
import net.jmorell.jdwglib.dwg.DwgFile;
55
import net.jmorell.jdwglib.dwg.DwgObject;
56
import net.jmorell.jdwglib.dwg.DwgObjectSpecificDataItem;
57
import net.jmorell.jdwglib.dwg.DxfName;
58

    
59
import org.cresques.px.dxf.AcadColor;
60
import org.cresques.px.dxf.DxfInsert;
61

    
62
import com.hardcode.gdbms.engine.data.driver.DriverException;
63
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
64
import com.hardcode.gdbms.engine.values.IntValue;
65
import com.hardcode.gdbms.engine.values.Value;
66
import com.hardcode.gdbms.engine.values.ValueFactory;
67
import com.iver.cit.gvsig.fmap.core.FPoint2D;
68
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
69
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
70
import com.iver.cit.gvsig.fmap.core.FShape;
71
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
72
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
73
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
74
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
75
import com.iver.cit.gvsig.fmap.drivers.MemoryDriver;
76
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
77
import com.iver.cit.gvsig.fmap.rendering.Legend;
78
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
79
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
80

    
81
import com.iver.cit.gvsig.fmap.drivers.dgn.ByteUtils;
82

    
83
public class DwgMemoryDriver extends MemoryDriver implements WithDefaultLegend {
84
        private final int ID_FIELD_ID=0;
85
        private final int ID_FIELD_ENTITY=1;
86
        private final int ID_FIELD_LAYER=2;
87
        private final int ID_FIELD_COLOR=3;
88
        private final int ID_FIELD_ELEVATION=4;
89
        private final int ID_FIELD_THICKNESS=5;
90
        private final int ID_FIELD_TEXT=6;
91
        private final int ID_FIELD_HEIGHTTEXT=7;
92
        private final int ID_FIELD_ROTATIONTEXT=8;
93

    
94
        VectorialUniqueValueLegend defaultLegend;
95
        private String path;
96
        private File m_Fich;
97
        
98
        private DwgFile dwg;
99
        private Vector dwgObjects;
100
        private DriverAttributes attr = new DriverAttributes();
101

    
102
        /* (non-Javadoc)
103
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#open(java.io.File)
104
         */
105
        public void open(File f) throws IOException {
106
            m_Fich = f;
107
        }
108
        
109
        public void initialize() throws IOException {
110
                float heightText = 10;
111
                
112
                attr.setLoadedInMemory(true);
113
                
114
                dwg = new DwgFile();
115
                long time = System.currentTimeMillis();
116
                dwg.read(m_Fich.getAbsolutePath());
117
                long timeElapsed = System.currentTimeMillis()-time;
118
                System.out.println("Tiempo empleado en leer el fichero dwg = " + timeElapsed);
119
                dwgObjects = dwg.getDwgObjects();
120
                
121
                //int nAtt = featureMaker.getAttributes().size();
122
                
123
                // Campos de las MemoryLayer:
124
            //Value[] auxRow = new Value[9+nAtt];
125
            Value[] auxRow = new Value[9];
126
                ArrayList arrayFields = new ArrayList();
127
                arrayFields.add("ID");
128
                arrayFields.add("Entity");
129
                arrayFields.add("Layer");
130
                arrayFields.add("Color");
131
                arrayFields.add("Elevation");
132
                arrayFields.add("Thickness");
133
                arrayFields.add("Text");
134
                arrayFields.add("HeightText");
135
                arrayFields.add("RotationText");
136
                /*for (int i=0;i<nAtt;i++) {
137
                        String att[] = new String[2];
138
                        att = (String[])featureMaker.getAttributes().get(i);
139
                        arrayFields.add(att[0]);
140
                }*/
141
                
142
            getTableModel().setColumnIdentifiers(arrayFields.toArray());
143
                
144
            boolean addingToBlock = false;
145
            
146
                time = System.currentTimeMillis();
147
                for (int i=0;i<dwgObjects.size();i++) {
148
                        
149
            auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory.createValue(0);
150
            auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory.createValue(0);
151
            auxRow[ID_FIELD_TEXT] = ValueFactory.createNullValue();
152
                        
153
                        DwgObject entity = (DwgObject)dwgObjects.get(i);
154
                        if (entity.getType()==0x11 && !addingToBlock) {
155
                                DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("CENTER");
156
                                double[] coord = (double[])data.getValue();
157
                                Point2D center = new Point2D.Double(coord[0], coord[1]);
158
                                data = entity.getDwgObjectSpecificDataItem("RADIUS");
159
                                double radius = ((Double)data.getValue()).doubleValue();
160
                                data = entity.getDwgObjectSpecificDataItem("START_ANGLE");
161
                                double startAngle = Math.toDegrees(((Double)data.getValue()).doubleValue());
162
                                data = entity.getDwgObjectSpecificDataItem("END_ANGLE");
163
                                double endAngle = Math.toDegrees(((Double)data.getValue()).doubleValue());
164
                                FPolyline2D arc = createArc(center, radius, startAngle, endAngle);
165
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
166
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
167
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
168
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
169
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
170
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
171
                    // Attributes
172
                    /*for (int j=0;j<nAtt;j++) {
173
                                    String[] attributes = new String[2];
174
                                    attributes = (String[])featureMaker.getAttributes().get(j);
175
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
176
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
177
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
178
                        }
179
                    }*/
180
                                addShape(arc, auxRow);
181
                        } else if (entity.getType()==0x12 && !addingToBlock) {
182
                                DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("CENTER");
183
                                double[] coord = (double[])data.getValue();
184
                                Point2D center = new Point2D.Double(coord[0], coord[1]);
185
                                data = entity.getDwgObjectSpecificDataItem("RADIUS");
186
                                double radius = ((Double)data.getValue()).doubleValue();
187
                                FPolyline2D circle = createCircle(center, radius);
188
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
189
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
190
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
191
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
192
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
193
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
194
                    // Attributes
195
                    /*for (int j=0;j<nAtt;j++) {
196
                                    String[] attributes = new String[2];
197
                                    attributes = (String[])featureMaker.getAttributes().get(j);
198
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
199
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
200
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
201
                        }
202
                    }*/
203
                                addShape(circle, auxRow);
204
                        } else if (entity.getType()==0x13 && !addingToBlock) {
205
                                DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("P1");
206
                                double[] coord = (double[])data.getValue();
207
                                Point2D p1 = new Point2D.Double(coord[0], coord[1]);
208
                                data = entity.getDwgObjectSpecificDataItem("P2");
209
                                coord = (double[])data.getValue();
210
                                Point2D p2 = new Point2D.Double(coord[0], coord[1]);
211
                                FPolyline2D line = createLine(p1, p2);
212
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
213
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
214
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
215
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
216
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
217
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
218
                    // Attributes
219
                    /*for (int j=0;j<nAtt;j++) {
220
                                    String[] attributes = new String[2];
221
                                    attributes = (String[])featureMaker.getAttributes().get(j);
222
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
223
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
224
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
225
                        }
226
                    }*/
227
                                addShape(line, auxRow);
228
                        } else if (entity.getType()==0x1B && !addingToBlock) {
229
                                DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("POINT");
230
                                double[] coord = (double[])data.getValue();
231
                                Point2D p = new Point2D.Double(coord[0], coord[1]);
232
                                FPoint2D point = createPoint(p);
233
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
234
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
235
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
236
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
237
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
238
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
239
                    // Attributes
240
                    /*for (int j=0;j<nAtt;j++) {
241
                                    String[] attributes = new String[2];
242
                                    attributes = (String[])featureMaker.getAttributes().get(j);
243
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
244
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
245
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
246
                        }
247
                    }*/
248
                                addShape(point, auxRow);
249
                        } else if (entity.getType()==0x0F && !addingToBlock) {
250
                                DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("FLAGS");
251
                                int flags = ((Integer)data.getValue()).intValue();
252
                                data = entity.getDwgObjectSpecificDataItem("FIRST_VERTEX_HANDLE");
253
                                int[] firstVertexHandle = (int[])data.getValue();
254
                                byte[] firstHandleBytes = new byte[]{0,0,0,0};
255
                                if (firstVertexHandle.length>2) firstHandleBytes[3] = (byte)firstVertexHandle[2];
256
                                if (firstVertexHandle.length>3) firstHandleBytes[2] = (byte)firstVertexHandle[3];
257
                                if (firstVertexHandle.length>4) firstHandleBytes[1] = (byte)firstVertexHandle[4];
258
                                if (firstVertexHandle.length>5) firstHandleBytes[0] = (byte)firstVertexHandle[5];
259
                                int firstHandle = ByteUtils.bytesToInt(firstHandleBytes, new int[]{0});
260
                                data = entity.getDwgObjectSpecificDataItem("LAST_VERTEX_HANDLE");
261
                                int[] lastVertexHandle = (int[])data.getValue();
262
                                byte[] lastHandleBytes = new byte[]{0,0,0,0};
263
                                if (lastVertexHandle.length>2) lastHandleBytes[3] = (byte)lastVertexHandle[2];
264
                                if (lastVertexHandle.length>3) lastHandleBytes[2] = (byte)lastVertexHandle[3];
265
                                if (lastVertexHandle.length>4) lastHandleBytes[1] = (byte)lastVertexHandle[4];
266
                                if (lastVertexHandle.length>5) lastHandleBytes[0] = (byte)lastVertexHandle[5];
267
                                int lastHandle = ByteUtils.bytesToInt(lastHandleBytes, new int[]{0});
268
                                Vector pts = new Vector();
269
                                double[] pt = new double[2];
270
                                
271
                                for (int j=0;j<dwgObjects.size();j++) {
272
                                        DwgObject obj = (DwgObject)dwgObjects.get(j);
273
                                        Vector handle = obj.getHandle();
274
                                        byte[] handleBytes = new byte[]{0,0,0,0};
275
                                        if (handle.size()>2) handleBytes[3] = ((Integer)handle.get(2)).byteValue();
276
                                        if (handle.size()>3) handleBytes[2] = ((Integer)handle.get(3)).byteValue();
277
                                        if (handle.size()>4) handleBytes[1] = ((Integer)handle.get(4)).byteValue();
278
                                        if (handle.size()>5) handleBytes[0] = ((Integer)handle.get(5)).byteValue();
279
                                        int objHandle = ByteUtils.bytesToInt(handleBytes, new int[]{0});
280
                                        if (objHandle==firstHandle && ((Integer)handle.get(1)).intValue()==firstVertexHandle[1]) {
281
                                                int k=0;
282
                                                while (true) {
283
                                                        DwgObject vertex = (DwgObject)dwgObjects.get(j+k);
284
                                                        Vector vertexHandle = vertex.getHandle();
285
                                                        byte[] vHandleBytes = new byte[]{0,0,0,0};
286
                                                        if (vertexHandle.size()>2) vHandleBytes[3] = ((Integer)vertexHandle.get(2)).byteValue();
287
                                                        if (vertexHandle.size()>3) vHandleBytes[2] = ((Integer)vertexHandle.get(3)).byteValue();
288
                                                        if (vertexHandle.size()>4) vHandleBytes[1] = ((Integer)vertexHandle.get(4)).byteValue();
289
                                                        if (vertexHandle.size()>5) vHandleBytes[0] = ((Integer)vertexHandle.get(5)).byteValue();
290
                                                        int vHandle = ByteUtils.bytesToInt(vHandleBytes, new int[]{0});
291
                                                        data = vertex.getDwgObjectSpecificDataItem("POINT");
292
                                                        pt = (double[])data.getValue();
293
                                                        pts.add(new Point2D.Double(pt[0], pt[1]));
294
                                                        k++;
295
                                                        if (vHandle==lastHandle && ((Integer)vertexHandle.get(1)).intValue()==lastVertexHandle[1]) {
296
                                                                break;
297
                                                        }
298
                                                }
299
                                        }
300
                                }
301
                                
302
                                Point2D[] newPts = new Point2D[pts.size()];
303
                                if (flags==0 || flags==2 || flags==4 || flags==8 || flags==16 || flags==32 || flags==64 || flags==128) {
304
                                        for (int j=0;j<pts.size();j++) {
305
                                                newPts[j] = (Point2D)pts.get(j);
306
                                        }
307
                                } else if (flags==1 || flags==3 || flags==5 || flags==9 || flags==17 || flags==33 || flags==65 || flags==129) {
308
                                        newPts = new Point2D[pts.size()+1];
309
                                        for (int j=0;j<pts.size();j++) {
310
                                                newPts[j] = (Point2D)pts.get(j);
311
                                        }
312
                                        newPts[pts.size()] = (Point2D)pts.get(0);
313
                                }
314
                                FPolyline2D pline = createPolyline2D(newPts);
315
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
316
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
317
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
318
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
319
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
320
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
321
                    // Attributes
322
                    /*for (int j=0;j<nAtt;j++) {
323
                                    String[] attributes = new String[2];
324
                                    attributes = (String[])featureMaker.getAttributes().get(j);
325
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
326
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
327
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
328
                        }
329
                    }*/
330
                                addShape(pline, auxRow);
331
                        } else if (entity.getType()==0x10 && !addingToBlock) {
332
                                DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("CLOSED_FLAGS");
333
                                int flags = ((Integer)data.getValue()).intValue();
334
                                data = entity.getDwgObjectSpecificDataItem("FIRST_VERTEX_HANDLE");
335
                                int[] firstVertexHandle = (int[])data.getValue();
336
                                data = entity.getDwgObjectSpecificDataItem("LAST_VERTEX_HANDLE");
337
                                int[] lastVertexHandle = (int[])data.getValue();
338
                                data = entity.getDwgObjectSpecificDataItem("SEQEND_HANDLE");
339
                                int[] seqendHandle = (int[])data.getValue();
340
                                int vertexNumber = calculateVertexNumber(firstVertexHandle, lastVertexHandle);
341
                                Point2D[] pts = new Point2D[vertexNumber];
342
                                double[] pt = new double[2];
343
                                
344
                                for (int j=0;j<dwgObjects.size();j++) {
345
                                        DwgObject v = (DwgObject)dwgObjects.get(j);
346
                                        Vector handle = v.getHandle();
347
                                        if (((Integer)handle.get(handle.size()-2)).intValue()==firstVertexHandle[handle.size()-2] && ((Integer)handle.get(handle.size()-1)).intValue()==firstVertexHandle[handle.size()-1]) {
348
                                                for (int k=0; k<vertexNumber; k++) {
349
                                                        DwgObject vertex = (DwgObject)dwgObjects.get(j+k);
350
                                                        data = vertex.getDwgObjectSpecificDataItem("POINT");
351
                                                        pt = (double[])data.getValue();
352
                                                        pts[k] = new Point2D.Double(pt[0], pt[1]);
353
                                                }
354
                                        }
355
                                }
356
                                Point2D[] newPts = new Point2D[pts.length];
357
                                if (flags==0 || flags==2 || flags==4 || flags==8 || flags==16 || flags==32 || flags==64 || flags==128) {
358
                                        newPts = pts;
359
                                } else if (flags==1 || flags==3 || flags==5 || flags==9 || flags==17 || flags==33 || flags==65 || flags==129) {
360
                                        newPts = new Point2D[pts.length+1];
361
                                        for (int j=0;j<pts.length;j++) {
362
                                                newPts[j] = pts[j];
363
                                        }
364
                                        newPts[pts.length] = pts[0];
365
                                }
366
                                FPolyline2D pline3d = createPolyline2D(newPts);
367
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
368
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
369
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
370
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
371
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
372
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
373
                    // Attributes
374
                    /*for (int j=0;j<nAtt;j++) {
375
                                    String[] attributes = new String[2];
376
                                    attributes = (String[])featureMaker.getAttributes().get(j);
377
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
378
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
379
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
380
                        }
381
                    }*/
382
                                addShape(pline3d, auxRow);
383
                        } else if (entity.getType()==0x1 && !addingToBlock) {
384
                                DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("INSERTION_POINT");
385
                                Point2D p = (Point2D)data.getValue();
386
                                FPoint2D point = createPoint(p);
387
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
388
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
389
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
390
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
391
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
392
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
393
                    // Attributes
394
                    /*for (int j=0;j<nAtt;j++) {
395
                                    String[] attributes = new String[2];
396
                                    attributes = (String[])featureMaker.getAttributes().get(j);
397
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
398
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
399
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
400
                        }
401
                    }*/
402
                                addShape(point, auxRow);
403
                        } else if (entity.getType()==0x4) {
404
                                DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("NAME");
405
                                String blockName = (String)data.getValue();
406
                                addingToBlock = true;
407
                                //pto = (Point2D.Double)point.get(0);
408
                                //FShape nuevoShp;
409
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
410
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
411
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
412
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
413
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
414
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
415
                    // Attributes
416
                    /*for (int j=0;j<nAtt;j++) {
417
                                    String[] attributes = new String[2];
418
                                    attributes = (String[])featureMaker.getAttributes().get(j);
419
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
420
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
421
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
422
                        }
423
                    }*/
424
                                //addShape(point, auxRow);
425
                        } else if (entity.getType()==0x5) {
426
                                addingToBlock = false;
427
                                //pto = (Point2D.Double)point.get(0);
428
                                //FShape nuevoShp;
429
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
430
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
431
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
432
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
433
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
434
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
435
                    // Attributes
436
                    /*for (int j=0;j<nAtt;j++) {
437
                                    String[] attributes = new String[2];
438
                                    attributes = (String[])featureMaker.getAttributes().get(j);
439
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
440
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
441
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
442
                        }
443
                    }*/
444
                                //addShape(point, auxRow);
445
                        } else if (entity.getType()==0x30) {
446
                                //addingToBlock = false;
447
                                //pto = (Point2D.Double)point.get(0);
448
                                //FShape nuevoShp;
449
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
450
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
451
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
452
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
453
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
454
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
455
                    // Attributes
456
                    /*for (int j=0;j<nAtt;j++) {
457
                                    String[] attributes = new String[2];
458
                                    attributes = (String[])featureMaker.getAttributes().get(j);
459
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
460
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
461
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
462
                        }
463
                    }*/
464
                                //addShape(point, auxRow);
465
                        } else if (entity.getType()==0x31) {
466
                                //addingToBlock = false;
467
                                //pto = (Point2D.Double)point.get(0);
468
                                //FShape nuevoShp;
469
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
470
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
471
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
472
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
473
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
474
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
475
                    // Attributes
476
                    /*for (int j=0;j<nAtt;j++) {
477
                                    String[] attributes = new String[2];
478
                                    attributes = (String[])featureMaker.getAttributes().get(j);
479
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
480
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
481
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
482
                        }
483
                    }*/
484
                                //addShape(point, auxRow);
485
                        } else if (entity.getType()==0x7 && !addingToBlock) {
486
                                DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("INSERTION_POINT");
487
                                double[] p = (double[])data.getValue();
488
                                Point2D point = new Point2D.Double(p[0], p[1]);
489
                                data = entity.getDwgObjectSpecificDataItem("SCALE");
490
                                double[] scale = (double[])data.getValue();
491
                                data = entity.getDwgObjectSpecificDataItem("ROTATION");
492
                double rot = ((Double)data.getValue()).doubleValue();
493
                                data = entity.getDwgObjectSpecificDataItem("BLOCK_HEADER_HANDLE");
494
                                int[] blockHandle = (int[])data.getValue();
495
                                
496
                                manageInsert(dwgObjects, point, scale, rot, blockHandle, auxRow);
497
                                
498
                                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
499
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
500
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
501
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
502
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
503
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
504
                    // Attributes
505
                    /*for (int j=0;j<nAtt;j++) {
506
                                    String[] attributes = new String[2];
507
                                    attributes = (String[])featureMaker.getAttributes().get(j);
508
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
509
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
510
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
511
                        }
512
                    }*/
513
                    FPoint2D fPoint = createPoint(point);
514
                                addShape(fPoint, auxRow);
515
                        } else if (entity.getType()==0x2C && !addingToBlock) {
516
                                DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("INSERTION_POINT");
517
                                double[] coord = (double[])data.getValue();
518
                                Point2D p = new Point2D.Double(coord[0], coord[1]);
519
                                FPoint2D point = createPoint(p);
520
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
521
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
522
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
523
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
524
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
525
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
526
                    // Attributes
527
                    /*for (int j=0;j<nAtt;j++) {
528
                                    String[] attributes = new String[2];
529
                                    attributes = (String[])featureMaker.getAttributes().get(j);
530
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
531
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
532
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
533
                        }
534
                    }*/
535
                                addShape(point, auxRow);
536
                        } else if (entity.getType()==0x1F && !addingToBlock) {
537
                                DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("CORNER1");
538
                                double[] coord = (double[])data.getValue();
539
                                Point2D p1 = new Point2D.Double(coord[0], coord[1]);
540
                                data = entity.getDwgObjectSpecificDataItem("CORNER2");
541
                                coord = (double[])data.getValue();
542
                                Point2D p2 = new Point2D.Double(coord[0], coord[1]);
543
                                data = entity.getDwgObjectSpecificDataItem("CORNER3");
544
                                coord = (double[])data.getValue();
545
                                Point2D p3 = new Point2D.Double(coord[0], coord[1]);
546
                                data = entity.getDwgObjectSpecificDataItem("CORNER4");
547
                                coord = (double[])data.getValue();
548
                                Point2D p4 = new Point2D.Double(coord[0], coord[1]);
549
                                FPolygon2D solid = createSolid(new Point2D[]{p1, p2, p4, p3});
550
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
551
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
552
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
553
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
554
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
555
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
556
                    // Attributes
557
                    /*for (int j=0;j<nAtt;j++) {
558
                                    String[] attributes = new String[2];
559
                                    attributes = (String[])featureMaker.getAttributes().get(j);
560
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
561
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
562
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
563
                        }
564
                    }*/
565
                                addShape(solid, auxRow);
566
                        } else if (entity.getType()==0x24 && !addingToBlock) {
567
                                DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("SCENARIO");
568
                                int sc = ((Integer)data.getValue()).intValue();
569
                                if (sc==1) {
570
                                        data = entity.getDwgObjectSpecificDataItem("CONTROL_POINTS");
571
                                } else if (sc==2) {
572
                                        data = entity.getDwgObjectSpecificDataItem("FIT_POINTS");
573
                                }
574
                                double[][] vertices = (double[][])data.getValue();
575
                                Point2D[] pts = new Point2D[vertices.length];
576
                                double[] pt = new double[2];
577
                                for (int j=0; j<vertices.length; j++) {
578
                                         pt = (double[])vertices[j];
579
                                         pts[j] = new Point2D.Double(pt[0], pt[1]);
580
                                }
581
                                FPolyline2D spline = createLwPolyline(pts);
582
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
583
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
584
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
585
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
586
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
587
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
588
                    // Attributes
589
                    /*for (int j=0;j<nAtt;j++) {
590
                                    String[] attributes = new String[2];
591
                                    attributes = (String[])featureMaker.getAttributes().get(j);
592
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
593
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
594
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
595
                        }
596
                    }*/
597
                                addShape(spline, auxRow);
598
                        } else if (dwg.getDxfNames().size()>0) {
599
                                int type = 0;
600
                                for (int j=0; j<dwg.getDxfNames().size(); j++) {
601
                                        String dxfName = ((DxfName)dwg.getDxfNames().get(j)).getDxfName();
602
                                        if (dxfName.equals("LWPOLYLINE")) {
603
                                                type = ((DxfName)dwg.getDxfNames().get(j)).getKey();
604
                                                break;
605
                                        }
606
                                }
607
                                if (entity.getType()==type) {
608
                                        DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("VERTICES");
609
                                        Vector vertices = (Vector)data.getValue();
610
                                        Point2D[] pts = new Point2D[vertices.size()];
611
                                        double[] pt = new double[2];
612
                                        for (int j=0; j<vertices.size(); j++) {
613
                                                 pt = (double[])vertices.get(j);
614
                                                 pts[j] = new Point2D.Double(pt[0], pt[1]);
615
                                        }
616
                                        FPolyline2D lwpline = createLwPolyline(pts);
617
                        auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
618
                        auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
619
                        auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
620
                        auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
621
                            auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
622
                            auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
623
                            // Attributes
624
                            /*for (int j=0;j<nAtt;j++) {
625
                                            String[] attributes = new String[2];
626
                                            attributes = (String[])featureMaker.getAttributes().get(j);
627
                                auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
628
                                if (!fea.getProp(attributes[0]).equals(attributes[1])) {
629
                                        auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
630
                                }
631
                            }*/
632
                                        addShape(lwpline, auxRow);
633
                                }
634
                        } else {
635
                                System.out.println("Detectado dwgObject pendiente de implementar");
636
                        }
637
                }
638
                timeElapsed = System.currentTimeMillis()-time;
639
                System.out.println("Tiempo empleado por el driver en importar el contenido del dwg = " + timeElapsed);
640
                
641
                defaultLegend = LegendFactory.createVectorialUniqueValueLegend(getShapeType());
642
                defaultLegend.setFieldName("Color");
643
                defaultLegend.setLabelField("Text");
644
                defaultLegend.setDefaultSymbol(new FSymbol(getShapeType()));
645
                defaultLegend.getDefaultSymbol().setShapeVisible(false);
646
                defaultLegend.getDefaultSymbol().setFontSizeInPixels(false);
647
                defaultLegend.getDefaultSymbol().setFont(new Font("SansSerif", Font.PLAIN, 9));
648
                defaultLegend.getDefaultSymbol().setFontColor(Color.BLACK);
649
                defaultLegend.getDefaultSymbol().setFontSize(heightText);
650
                defaultLegend.getDefaultSymbol().setStyle(FConstant.SYMBOL_STYLE_DGNSPECIAL);
651
                defaultLegend.getDefaultSymbol().setSize(3);
652
                defaultLegend.getDefaultSymbol().setSizeInPixels(true);
653
                
654
                defaultLegend.setLabelHeightField("HeightText");
655
                defaultLegend.setLabelRotationField("RotationText");
656
                
657
        ObjectDriver rs = this;
658
                IntValue clave;
659
                FSymbol theSymbol = null;
660
                
661
                try {
662
                    // TODO: Provisional hasta que cambiemos los s?mbolos.
663
                   /*  BufferedImage bi= new BufferedImage(5, 5, BufferedImage.TYPE_INT_ARGB);
664
                   Graphics2D big = bi.createGraphics();
665
                   Color color=new Color(0,0,0,0);
666
                   big.setBackground(color);
667
                   big.clearRect(0, 0, 5, 5);
668
                   Paint fillProv = null; 
669
                   Rectangle2D rProv = new Rectangle();
670
                   rProv.setFrame(0, 0,5,5);
671
                   fillProv = new TexturePaint(bi,rProv); */                    
672
                    
673
                        for (long j = 0; j < rs.getRowCount(); j++)
674
                        {
675
                                clave = (IntValue)rs.getFieldValue(j,ID_FIELD_COLOR);
676
                                if (defaultLegend.getSymbolByValue(clave) == null)
677
                                {
678
                                        theSymbol = new FSymbol(getShapeType());
679
                                        theSymbol.setDescription(clave.toString());
680
                                        theSymbol.setColor(AcadColor.getColor(clave.getValue()));
681
                                        // theSymbol.setFill(fillProv);
682
                                        // 050202, jmorell: Asigna los colores de Autocad a los bordes
683
                                        //                                        de los pol?gonos.
684
                                        theSymbol.setOutlineColor(AcadColor.getColor(clave.getValue()));
685
                                        
686
                                        theSymbol.setStyle(FConstant.SYMBOL_STYLE_DGNSPECIAL);
687
                                        theSymbol.setSize(3);
688
                                        theSymbol.setSizeInPixels(true);
689
                                        
690
                                        defaultLegend.addSymbol(clave, theSymbol);
691
                                }
692
                        } // for
693
                } catch (DriverException e) {
694
                        e.printStackTrace();
695
                        throw new IOException("Error al poner la leyenda por defecto en el Dwg");
696
                }
697
        }
698
        
699
        /**
700
         * En principio exportamos a FPolyline2D, despues estudiaremos la conveniencia
701
         * de exportar a FPolygon2D, o a las dos cosas ...
702
         * Esta clase construye el circle del mismo modo que en DxfFeatureMaker.
703
         * Se podr?a renombrar esta clase a createCircleFeature y a?adir otra
704
         * createCircleEntity ...
705
         * La parte de creaci?n de la FPolyline2D est? sacada del DxfMemoryDriver ...
706
         * @param center
707
         * @param radius
708
         * @return
709
         */
710
        private FPolyline2D createCircle(Point2D c, double r) {
711
                Point2D[] pts = new Point2D[360];
712
                int angulo = 0;
713
                for (angulo=0; angulo<360; angulo++) {
714
                        pts[angulo] = new Point2D.Double(c.getX(), c.getY());
715
                        pts[angulo].setLocation(pts[angulo].getX() + r * Math.sin(angulo*Math.PI/(double)180.0), pts[angulo].getY() + r * Math.cos(angulo*Math.PI/(double)180.0));
716
                }
717
                GeneralPathX genPathX = new GeneralPathX();
718
                genPathX.moveTo(pts[0].getX(), pts[0].getY());
719
                for (int i=1; i<pts.length; i++) {
720
                        genPathX.lineTo(pts[i].getX(), pts[i].getY());
721
                }
722
                return new FPolyline2D(genPathX);
723
        }
724
        
725
        /**
726
         * Como createCircle ...
727
         * @param c
728
         * @param r
729
         * @param sa
730
         * @param ea
731
         * @return
732
         */
733
        private FPolyline2D createArc(Point2D c, double r, double sa, double ea) {
734
                int isa = (int)sa;
735
                int iea = (int)ea;
736
                double angulo;
737
                Point2D[] pts;
738
                if (sa <= ea) {
739
                        pts = new Point2D[(iea-isa)+2];
740
                        angulo = sa;
741
                        pts[0] = new Point2D.Double(c.getX() + r * Math.cos(angulo*Math.PI/(double)180.0), c.getY() + r * Math.sin(angulo*Math.PI/(double)180.0));
742
                        for (int i=1; i<=(iea-isa)+1; i++) {
743
                                angulo = (double)(isa+i);
744
                                pts[i] = new Point2D.Double(c.getX() + r * Math.cos(angulo*Math.PI/(double)180.0), c.getY() + r * Math.sin(angulo*Math.PI/(double)180.0));
745
                        }
746
                        angulo = ea;
747
                        pts[(iea-isa)+1] = new Point2D.Double(c.getX() + r * Math.cos(angulo*Math.PI/(double)180.0), c.getY() + r * Math.sin(angulo*Math.PI/(double)180.0));
748
                } else {
749
                        pts = new Point2D[(360-isa)+iea+2];
750
                        angulo = sa;
751
                        pts[0] = new Point2D.Double(c.getX() + r * Math.cos(angulo*Math.PI/(double)180.0), c.getY() + r * Math.sin(angulo*Math.PI/(double)180.0));
752
                        for (int i=1; i<=(360-isa); i++) {
753
                                angulo = (double)(isa+i);
754
                                pts[i] = new Point2D.Double(c.getX() + r * Math.cos(angulo*Math.PI/(double)180.0), c.getY() + r * Math.sin(angulo*Math.PI/(double)180.0));
755
                        }
756
                        for (int i=(360-isa)+1; i<=(360-isa)+iea; i++) {
757
                                angulo = (double)(i-(360-isa));
758
                                pts[i] = new Point2D.Double(c.getX() + r * Math.cos(angulo*Math.PI/(double)180.0), c.getY() + r * Math.sin(angulo*Math.PI/(double)180.0));
759
                        }
760
                        angulo = ea;
761
                        pts[(360-isa)+iea+1] = new Point2D.Double(c.getX() + r * Math.cos(angulo*Math.PI/(double)180.0), c.getY() + r * Math.sin(angulo*Math.PI/(double)180.0));
762
                }
763
                GeneralPathX genPathX = new GeneralPathX();
764
                genPathX.moveTo(pts[0].getX(), pts[0].getY());
765
                for (int i=1; i<pts.length; i++) {
766
                        genPathX.lineTo(pts[i].getX(), pts[i].getY());
767
                }
768
                return new FPolyline2D(genPathX);
769
        }
770
        
771
        /**
772
         * Los nombres de estos m?todos pueden cambiarse por otros m?s apropiados, por
773
         * ejemplo:
774
         * createLine --> dwgLineToFPolyline2D ... ?mejor as?? ...
775
         * @param p1
776
         * @param p2
777
         * @return
778
         */
779
        private FPolyline2D createLine(Point2D p1, Point2D p2) {
780
                Point2D[] pts = new Point2D[]{p1, p2};
781
                GeneralPathX genPathX = new GeneralPathX();
782
                genPathX.moveTo(pts[0].getX(), pts[0].getY());
783
                for (int i=1; i<pts.length; i++) {
784
                        genPathX.lineTo(pts[i].getX(), pts[i].getY());
785
                }
786
                return new FPolyline2D(genPathX);
787
        }
788
        
789
        private FPoint2D createPoint(Point2D p) {
790
                return new FPoint2D(p.getX(), p.getY());
791
        }
792
        
793
        private FPolyline2D createLwPolyline(Point2D[] pts) {
794
                GeneralPathX genPathX = new GeneralPathX();
795
                genPathX.moveTo(pts[0].getX(), pts[0].getY());
796
                for (int i=1; i<pts.length; i++) {
797
                        genPathX.lineTo(pts[i].getX(), pts[i].getY());
798
                }
799
                return new FPolyline2D(genPathX);
800
        }
801
        
802
        private FPolyline2D createPolyline2D(Point2D[] pts) {
803
                GeneralPathX genPathX = new GeneralPathX();
804
                genPathX.moveTo(pts[0].getX(), pts[0].getY());
805
                for (int i=1; i<pts.length; i++) {
806
                        genPathX.lineTo(pts[i].getX(), pts[i].getY());
807
                }
808
                return new FPolyline2D(genPathX);
809
        }
810
        
811
        private FPolygon2D createSolid(Point2D[] pts) {
812
                GeneralPathX genPathX = new GeneralPathX();
813
                genPathX.moveTo(pts[0].getX(), pts[0].getY());
814
                for (int i=1; i<pts.length; i++) {
815
                        genPathX.lineTo(pts[i].getX(), pts[i].getY());
816
                }
817
                genPathX.closePath();
818
                return new FPolygon2D(genPathX);
819
        }
820
        
821
        private int calculateVertexNumber(int[] firstVertexHandle, int[] lastVertexHandle) {
822
                if (firstVertexHandle.length==lastVertexHandle.length) {
823
                        if (firstVertexHandle[firstVertexHandle.length-2]==lastVertexHandle[firstVertexHandle.length-2]) {
824
                                return (lastVertexHandle[firstVertexHandle.length-1]-firstVertexHandle[firstVertexHandle.length-1]+1);
825
                        } else {
826
                                int aux = 255-firstVertexHandle[firstVertexHandle.length-1]+1;
827
                                int aux2 = lastVertexHandle[firstVertexHandle.length-1]+1;
828
                                int aux3 = (lastVertexHandle[firstVertexHandle.length-2]-firstVertexHandle[firstVertexHandle.length-2]-1)*255;
829
                                int vn = aux + aux2 + aux3;
830
                                return vn;
831
                        }
832
                } else if (firstVertexHandle.length==(lastVertexHandle.length-1)) {
833
                        int aux = 255-firstVertexHandle[firstVertexHandle.length-1]+1;
834
                        int aux2 = lastVertexHandle[firstVertexHandle.length]+1;
835
                        int aux3 = (lastVertexHandle[lastVertexHandle.length-2]-1)*255;
836
                        int vn = aux + aux2 + aux3;
837
                        return vn;
838
                } else {
839
                        System.out.println("ERROR: firstVertexHandle y lastVertexHandle tienen tama?os inesperados");
840
                        return 0;
841
                }
842
        }
843
        
844
        private void manageBlockEntity(DwgObject entity, double[] bPoint, Point2D insPoint, double[] scale, double rot, Value[] auxRow) {
845
                if (entity.getType()==0x11) {
846
                        /*DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("CENTER");
847
                        double[] coord = (double[])data.getValue();
848
                        Point2D center = new Point2D.Double(coord[0], coord[1]);
849
                        data = entity.getDwgObjectSpecificDataItem("RADIUS");
850
                        double radius = ((Double)data.getValue()).doubleValue();
851
                        data = entity.getDwgObjectSpecificDataItem("START_ANGLE");
852
                        double startAngle = Math.toDegrees(((Double)data.getValue()).doubleValue());
853
                        data = entity.getDwgObjectSpecificDataItem("END_ANGLE");
854
                        double endAngle = Math.toDegrees(((Double)data.getValue()).doubleValue());
855
                        FPolyline2D arc = createArc(center, radius, startAngle, endAngle);
856
            auxRow[ID_FIELD_ID] = ValueFactory.createValue(0);
857
            auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
858
            auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
859
            auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
860
                auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
861
                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);*/
862
                // Attributes
863
                /*for (int j=0;j<nAtt;j++) {
864
                                String[] attributes = new String[2];
865
                                attributes = (String[])featureMaker.getAttributes().get(j);
866
                    auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
867
                    if (!fea.getProp(attributes[0]).equals(attributes[1])) {
868
                            auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
869
                    }
870
                }*/
871
                        //addShape(arc, auxRow);
872
                } else if (entity.getType()==0x12) {
873
                        DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("CENTER");
874
                        double[] coord = (double[])data.getValue();
875
                        Point2D center = new Point2D.Double(coord[0], coord[1]);
876
                        data = entity.getDwgObjectSpecificDataItem("RADIUS");
877
                        double radius = ((Double)data.getValue()).doubleValue();
878
                        
879
                        Point2D pointAux = new Point2D.Double(center.getX() - bPoint[0], center.getY() - bPoint[1]);
880
                        double laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(Math.toRadians(rot)) + (pointAux.getY()*scale[1])*(-1)*Math.sin(Math.toRadians(rot)));
881
                        double laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(Math.toRadians(rot)) + (pointAux.getY()*scale[1])*Math.cos(Math.toRadians(rot)));
882
                        center = new Point2D.Double(laX, laY);
883
                        // Escala en X = escala en Y ...
884
                        radius = radius * scale[0];
885
                        
886
                        FPolyline2D circle = createCircle(center, radius);
887
            auxRow[ID_FIELD_ID] = ValueFactory.createValue(0);
888
            auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
889
            auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
890
            auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
891
                auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
892
                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
893
                // Attributes
894
                /*for (int j=0;j<nAtt;j++) {
895
                                String[] attributes = new String[2];
896
                                attributes = (String[])featureMaker.getAttributes().get(j);
897
                    auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
898
                    if (!fea.getProp(attributes[0]).equals(attributes[1])) {
899
                            auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
900
                    }
901
                }*/
902
                        addShape(circle, auxRow);
903
                } else if (entity.getType()==0x13) {
904
                        DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("P1");
905
                        double[] coord = (double[])data.getValue();
906
                        Point2D p1 = new Point2D.Double(coord[0], coord[1]);
907
                        data = entity.getDwgObjectSpecificDataItem("P2");
908
                        coord = (double[])data.getValue();
909
                        Point2D p2 = new Point2D.Double(coord[0], coord[1]);
910
                        
911
                        Point2D pointAux = new Point2D.Double(p1.getX() - bPoint[0], p1.getY() - bPoint[1]);
912
                        double laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot));
913
                        double laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot));
914
                        p1 = new Point2D.Double(laX, laY);
915
                        pointAux = new Point2D.Double(p2.getX() - bPoint[0], p2.getY() - bPoint[1]);
916
                        laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot));
917
                        laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot));
918
                        p2 = new Point2D.Double(laX, laY);
919
                        
920
                        FPolyline2D line = createLine(p1, p2);
921
            auxRow[ID_FIELD_ID] = ValueFactory.createValue(0);
922
            auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
923
            auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
924
            auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
925
                auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
926
                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
927
                // Attributes
928
                /*for (int j=0;j<nAtt;j++) {
929
                                String[] attributes = new String[2];
930
                                attributes = (String[])featureMaker.getAttributes().get(j);
931
                    auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
932
                    if (!fea.getProp(attributes[0]).equals(attributes[1])) {
933
                            auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
934
                    }
935
                }*/
936
                        addShape(line, auxRow);
937
                } else if (entity.getType()==0x7) {
938
                        DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("INSERTION_POINT");
939
                        double[] p = (double[])data.getValue();
940
                        Point2D newInsPoint = new Point2D.Double(p[0], p[1]);
941
                        data = entity.getDwgObjectSpecificDataItem("SCALE");
942
                        double[] newScale = (double[])data.getValue();
943
                        data = entity.getDwgObjectSpecificDataItem("ROTATION");
944
            double newRot = ((Double)data.getValue()).doubleValue();
945
                        data = entity.getDwgObjectSpecificDataItem("BLOCK_HEADER_HANDLE");
946
                        int[] newBlockHandle = (int[])data.getValue();
947
                        
948
                        Point2D pointAux = new Point2D.Double(newInsPoint.getX() - bPoint[0], newInsPoint.getY() - bPoint[1]);
949
                        double laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot));
950
                        double laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot));
951
                        newInsPoint = new Point2D.Double(laX, laY);
952
                        
953
                        newScale = new double[]{scale[0]*newScale[0], scale[1]*newScale[1], scale[2]*newScale[2]};
954
                        
955
                        manageInsert(dwgObjects, newInsPoint, newScale, newRot, newBlockHandle, auxRow);
956
                        
957
                        /*auxRow[ID_FIELD_ID] = ValueFactory.createValue(0);
958
            auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
959
            auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
960
            auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
961
                auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
962
                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);*/
963
                // Attributes
964
                /*for (int j=0;j<nAtt;j++) {
965
                                String[] attributes = new String[2];
966
                                attributes = (String[])featureMaker.getAttributes().get(j);
967
                    auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
968
                    if (!fea.getProp(attributes[0]).equals(attributes[1])) {
969
                            auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
970
                    }
971
                }*/
972
                /*FPoint2D fPoint = createPoint(newInsPoint);
973
                        addShape(fPoint, auxRow);*/
974
                }
975
        }
976
        
977
        private void manageInsert(Vector dwgObjects, Point2D insPoint, double[] scale, double rot, int[] blockHandle, Value[] auxRow) {
978
                byte[] blockHandleBytes = new byte[]{0,0,0,0};
979
                if (blockHandle.length>2) blockHandleBytes[3] = (byte)blockHandle[2];
980
                if (blockHandle.length>3) blockHandleBytes[2] = (byte)blockHandle[3];
981
                if (blockHandle.length>4) blockHandleBytes[1] = (byte)blockHandle[4];
982
                if (blockHandle.length>5) blockHandleBytes[0] = (byte)blockHandle[5];
983
                int bHandle = ByteUtils.bytesToInt(blockHandleBytes, new int[]{0});
984
                for (int j=0;j<dwgObjects.size();j++) {
985
                        DwgObject obj = (DwgObject)dwgObjects.get(j);
986
                        DwgObjectSpecificDataItem dataa = obj.getDwgObjectSpecificDataItem("BLOCK_ENTITY_HANDLE");
987
                        //System.out.println("obj.getType() = " + obj.getType());
988
                        //System.out.println("dataa = " + dataa);
989
                        if (dataa!=null) {
990
                                int[] handle = (int[])dataa.getValue();
991
                                byte[] handleBytes = new byte[]{0,0,0,0};
992
                                if (handle.length>2) handleBytes[3] = (byte)handle[2];
993
                                if (handle.length>3) handleBytes[2] = (byte)handle[3];
994
                                if (handle.length>4) handleBytes[1] = (byte)handle[4];
995
                                if (handle.length>5) handleBytes[0] = (byte)handle[5];
996
                                int objHandle = ByteUtils.bytesToInt(handleBytes, new int[]{0});
997
                                //System.out.println("objHandle = " + objHandle);
998
                                //System.out.println("bHandle = " + bHandle);
999
                                if (objHandle==bHandle+1 && handle[1]==blockHandle[1]) {
1000
                                        //obj = (DwgObject)dwgObjects.get(j+1);
1001
                                        //System.out.println("Hemos encontrado el bloque");
1002
                                        //System.out.println("obj.getType() = " + obj.getType());
1003
                                        DwgObjectSpecificDataItem data = obj.getDwgObjectSpecificDataItem("BASE_POINT");
1004
                                        double[] bPoint = (double[])data.getValue();
1005
                                        //System.out.println("bPoint[0] = " + bPoint[0]);
1006
                                        //System.out.println("bPoint[1] = " + bPoint[1]);
1007
                                        data = obj.getDwgObjectSpecificDataItem("FIRST_ENTITY_HANDLE");
1008
                                        int[] firstEntityHandle = (int[])data.getValue();
1009
                                        //System.out.println("firstEntityHandle[0] = " + firstEntityHandle[0]);
1010
                                        //System.out.println("firstEntityHandle[1] = " + firstEntityHandle[1]);
1011
                                        //if (firstEntityHandle.length>2) System.out.println("firstEntityHandle[2] = " + firstEntityHandle[2]);
1012
                                        //if (firstEntityHandle.length>3) System.out.println("firstEntityHandle[3] = " + firstEntityHandle[3]);
1013
                                        byte[] firstEntityBytes = new byte[]{0,0,0,0};
1014
                                        if (firstEntityHandle.length>2) firstEntityBytes[3] = (byte)firstEntityHandle[2];
1015
                                        if (firstEntityHandle.length>3) firstEntityBytes[2] = (byte)firstEntityHandle[3];
1016
                                        if (firstEntityHandle.length>4) firstEntityBytes[1] = (byte)firstEntityHandle[4];
1017
                                        if (firstEntityHandle.length>5) firstEntityBytes[0] = (byte)firstEntityHandle[5];
1018
                                        int firstHandle = ByteUtils.bytesToInt(firstEntityBytes, new int[]{0});
1019
                                        //System.out.println("firstHandle = " + firstHandle);
1020
                                        data = obj.getDwgObjectSpecificDataItem("LAST_ENTITY_HANDLE");
1021
                                        int[] lastEntityHandle = (int[])data.getValue();
1022
                                        byte[] lastEntityBytes = new byte[]{0,0,0,0};
1023
                                        if (lastEntityHandle.length>2) lastEntityBytes[3] = (byte)lastEntityHandle[2];
1024
                                        if (lastEntityHandle.length>3) lastEntityBytes[2] = (byte)lastEntityHandle[3];
1025
                                        if (lastEntityHandle.length>4) lastEntityBytes[1] = (byte)lastEntityHandle[4];
1026
                                        if (lastEntityHandle.length>5) lastEntityBytes[0] = (byte)lastEntityHandle[5];
1027
                                        int lastHandle = ByteUtils.bytesToInt(lastEntityBytes, new int[]{0});
1028
                                        //System.out.println("lastHandle = " + lastHandle);
1029
                                        
1030
                                        for (int k=0;k<dwgObjects.size();k++) {
1031
                                                DwgObject ent = (DwgObject)dwgObjects.get(k);
1032
                                                Vector entHandle = ent.getHandle();
1033
                                                byte[] entHandleBytes = new byte[]{0,0,0,0};
1034
                                                if (entHandle.size()>2) entHandleBytes[3] = ((Integer)entHandle.get(2)).byteValue();
1035
                                                if (entHandle.size()>3) entHandleBytes[2] = ((Integer)entHandle.get(3)).byteValue();
1036
                                                if (entHandle.size()>4) entHandleBytes[1] = ((Integer)entHandle.get(4)).byteValue();
1037
                                                if (entHandle.size()>5) entHandleBytes[0] = ((Integer)entHandle.get(5)).byteValue();
1038
                                                int eHandle = ByteUtils.bytesToInt(entHandleBytes, new int[]{0});
1039
                                                //System.out.println("eHandle = " + eHandle);
1040
                                                if (eHandle==firstHandle && ((Integer)entHandle.get(1)).intValue()==firstEntityHandle[1]) {
1041
                                                        //System.out.println("Hemos encontrado el primer elemento");
1042
                                                        int l=0;
1043
                                                        while (true) {
1044
                                                                DwgObject blockEntity = (DwgObject)dwgObjects.get(k+l);
1045
                                                                Vector blockEntityHandle = blockEntity.getHandle();
1046
                                                                byte[] blockEntityHandleBytes = new byte[]{0,0,0,0};
1047
                                                                if (blockEntityHandle.size()>2) blockEntityHandleBytes[3] = ((Integer)blockEntityHandle.get(2)).byteValue();
1048
                                                                if (blockEntityHandle.size()>3) blockEntityHandleBytes[2] = ((Integer)blockEntityHandle.get(3)).byteValue();
1049
                                                                if (blockEntityHandle.size()>4) blockEntityHandleBytes[1] = ((Integer)blockEntityHandle.get(4)).byteValue();
1050
                                                                if (blockEntityHandle.size()>5) blockEntityHandleBytes[0] = ((Integer)blockEntityHandle.get(5)).byteValue();
1051
                                                                int bEntityHandle = ByteUtils.bytesToInt(blockEntityHandleBytes, new int[]{0});
1052
                                                                manageBlockEntity(blockEntity, bPoint, insPoint, scale, rot, auxRow);
1053
                                                                l++;
1054
                                                                if (bEntityHandle==lastHandle && ((Integer)entHandle.get(1)).intValue()==lastEntityHandle[1]) {
1055
                                                                        break;
1056
                                                                }
1057
                                                        }
1058
                                                }
1059
                                        }
1060
                                }
1061
                        }
1062
                }
1063
        }
1064

    
1065
        /* (non-Javadoc)
1066
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#accept(java.io.File)
1067
         */
1068
        public boolean accept(File f) {
1069
                return f.getName().toUpperCase().endsWith("DWG");
1070
        }
1071

    
1072
        /* (non-Javadoc)
1073
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getShapeType()
1074
         */
1075
        public int getShapeType() {
1076
                return FShape.MULTI;
1077
        }
1078

    
1079
        /* (non-Javadoc)
1080
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getName()
1081
         */
1082
        public String getName() {
1083
                return "gvSIG DWG Memory Driver";
1084
        }
1085

    
1086
        /* (non-Javadoc)
1087
         * @see com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend#getDefaultLegend()
1088
         */
1089
        public Legend getDefaultLegend() {
1090
                return defaultLegend;
1091
        }
1092

    
1093
    /* (non-Javadoc)
1094
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
1095
     */
1096
    public DriverAttributes getDriverAttributes() {
1097
        return attr;
1098
    }
1099

    
1100
}