Statistics
| Revision:

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

History | View | Annotate | Download (72.1 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.javacad.util.ArcFromBulgeCalculator;
55
import net.jmorell.jdwglib.dwg.DwgFile;
56
import net.jmorell.jdwglib.dwg.DwgObject;
57
import net.jmorell.jdwglib.dwg.DwgObjectSpecificDataItem;
58
import net.jmorell.jdwglib.dwg.DxfName;
59

    
60
import org.cresques.px.dxf.AcadColor;
61
import org.cresques.px.dxf.DxfCalArcs;
62
import org.cresques.px.dxf.DxfInsert;
63

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

    
83
import com.iver.cit.gvsig.fmap.drivers.dgn.ByteUtils;
84

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

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

    
104
        /* (non-Javadoc)
105
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#open(java.io.File)
106
         */
107
        public void open(File f) throws IOException {
108
            m_Fich = f;
109
        }
110
        
111
        public void initialize() throws IOException {
112
                float heightText = 10;
113
                
114
                attr.setLoadedInMemory(true);
115
                
116
                dwg = new DwgFile();
117
                long time = System.currentTimeMillis();
118
                dwg.read(m_Fich.getAbsolutePath());
119
                long timeElapsed = System.currentTimeMillis()-time;
120
                System.out.println("Tiempo empleado en leer el fichero dwg = " + timeElapsed);
121
                dwgObjects = dwg.getDwgObjects();
122
                
123
                //int nAtt = featureMaker.getAttributes().size();
124
                
125
                // Campos de las MemoryLayer:
126
            //Value[] auxRow = new Value[9+nAtt];
127
            Value[] auxRow = new Value[9];
128
                ArrayList arrayFields = new ArrayList();
129
                arrayFields.add("ID");
130
                arrayFields.add("Entity");
131
                arrayFields.add("Layer");
132
                arrayFields.add("Color");
133
                arrayFields.add("Elevation");
134
                arrayFields.add("Thickness");
135
                arrayFields.add("Text");
136
                arrayFields.add("HeightText");
137
                arrayFields.add("RotationText");
138
                /*for (int i=0;i<nAtt;i++) {
139
                        String att[] = new String[2];
140
                        att = (String[])featureMaker.getAttributes().get(i);
141
                        arrayFields.add(att[0]);
142
                }*/
143
                
144
            getTableModel().setColumnIdentifiers(arrayFields.toArray());
145
                
146
            boolean addingToBlock = false;
147
            
148
                time = System.currentTimeMillis();
149
                
150
                // Para intentar entender la manera de asignar la layer a cada elemento ...
151
                /*for (int i=0;i<dwgObjects.size();i++) {
152
                        DwgObject obj = (DwgObject)dwgObjects.get(i);
153
                        if (obj.getType()==0x33) {
154
                                System.out.println("obj.getHandle().get(0) = " + obj.getHandle().get(0));
155
                                System.out.println("obj.getHandle().get(1) = " + obj.getHandle().get(1));
156
                                if (obj.getHandle().size()>2) System.out.println("obj.getHandle().get(2) = " + obj.getHandle().get(2));
157
                                if (obj.getHandle().size()>3) System.out.println("obj.getHandle().get(3) = " + obj.getHandle().get(3));
158
                        }
159
                }*/
160
                
161
                Vector layerTable = createLayerTable(dwgObjects);
162
                
163
                for (int i=0;i<dwgObjects.size();i++) {
164
                        
165
            auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory.createValue(0);
166
            auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory.createValue(0);
167
            auxRow[ID_FIELD_TEXT] = ValueFactory.createNullValue();
168
                        
169
                        DwgObject entity = (DwgObject)dwgObjects.get(i);
170
                        if (entity.getType()==0x11 && !addingToBlock) {
171
                                System.out.println("Encuentra un Arc ...");
172
                                DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("CENTER");
173
                                double[] coord = (double[])data.getValue();
174
                                Point2D center = new Point2D.Double(coord[0], coord[1]);
175
                                data = entity.getDwgObjectSpecificDataItem("RADIUS");
176
                                double radius = ((Double)data.getValue()).doubleValue();
177
                                data = entity.getDwgObjectSpecificDataItem("START_ANGLE");
178
                                double startAngle = Math.toDegrees(((Double)data.getValue()).doubleValue());
179
                                data = entity.getDwgObjectSpecificDataItem("END_ANGLE");
180
                                double endAngle = Math.toDegrees(((Double)data.getValue()).doubleValue());
181
                                Point2D[] arc = createArc(center, radius, startAngle, endAngle);
182
                                FPolyline2D arcc = createPolyline3D(arc);
183
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
184
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("Arc"));
185
                                int[] layerHandle = (int[])entity.getLayer();
186
                                String layerName = getLayerName(entity, layerTable);
187
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
188
                                int color = entity.getColor();
189
                                int colorByLayer = setColorByLayer(layerHandle, layerTable);
190
                                //if (color==0) color ByBlock
191
                                if (color==256) color = colorByLayer;
192
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
193
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
194
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
195
                    // Attributes
196
                    /*for (int j=0;j<nAtt;j++) {
197
                                    String[] attributes = new String[2];
198
                                    attributes = (String[])featureMaker.getAttributes().get(j);
199
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
200
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
201
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
202
                        }
203
                    }*/
204
                                addShape(arcc, auxRow);
205
                        } else if (entity.getType()==0x12 && !addingToBlock) {
206
                                System.out.println("Encuentra un Circle ...");
207
                                DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("CENTER");
208
                                double[] coord = (double[])data.getValue();
209
                                Point2D center = new Point2D.Double(coord[0], coord[1]);
210
                                data = entity.getDwgObjectSpecificDataItem("RADIUS");
211
                                double radius = ((Double)data.getValue()).doubleValue();
212
                                FPolyline2D circle = createCircle(center, radius);
213
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
214
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("Circle"));
215
                                int[] layerHandle = (int[])entity.getLayer();
216
                                String layerName = getLayerName(entity, layerTable);
217
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
218
                                int color = entity.getColor();
219
                                int colorByLayer = setColorByLayer(layerHandle, layerTable);
220
                                //if (color==0) color ByBlock
221
                                if (color==256) color = colorByLayer;
222
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
223
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
224
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
225
                    // Attributes
226
                    /*for (int j=0;j<nAtt;j++) {
227
                                    String[] attributes = new String[2];
228
                                    attributes = (String[])featureMaker.getAttributes().get(j);
229
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
230
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
231
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
232
                        }
233
                    }*/
234
                                addShape(circle, auxRow);
235
                        } else if (entity.getType()==0x13 && !addingToBlock) {
236
                                System.out.println("Encuentra una Line ...");
237
                                DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("P1");
238
                                double[] coord = (double[])data.getValue();
239
                                Point2D p1 = new Point2D.Double(coord[0], coord[1]);
240
                                data = entity.getDwgObjectSpecificDataItem("P2");
241
                                coord = (double[])data.getValue();
242
                                Point2D p2 = new Point2D.Double(coord[0], coord[1]);
243
                                FPolyline2D line = createLine(p1, p2);
244
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
245
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("Line"));
246
                                int[] layerHandle = (int[])entity.getLayer();
247
                                String layerName = getLayerName(entity, layerTable);
248
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
249
                                int color = entity.getColor();
250
                                int colorByLayer = setColorByLayer(layerHandle, layerTable);
251
                                //if (color==0) color ByBlock
252
                                if (color==256) color = colorByLayer;
253
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
254
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
255
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
256
                    // Attributes
257
                    /*for (int j=0;j<nAtt;j++) {
258
                                    String[] attributes = new String[2];
259
                                    attributes = (String[])featureMaker.getAttributes().get(j);
260
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
261
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
262
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
263
                        }
264
                    }*/
265
                                addShape(line, auxRow);
266
                        } else if (entity.getType()==0x1B && !addingToBlock) {
267
                                System.out.println("Encuentra un Point ...");
268
                                DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("POINT");
269
                                double[] coord = (double[])data.getValue();
270
                                Point2D p = new Point2D.Double(coord[0], coord[1]);
271
                                FPoint2D point = createPoint(p);
272
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
273
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("Point"));
274
                                int[] layerHandle = (int[])entity.getLayer();
275
                                String layerName = getLayerName(entity, layerTable);
276
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
277
                                int color = entity.getColor();
278
                                int colorByLayer = setColorByLayer(layerHandle, layerTable);
279
                                //if (color==0) color ByBlock
280
                                if (color==256) color = colorByLayer;
281
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
282
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
283
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
284
                    // Attributes
285
                    /*for (int j=0;j<nAtt;j++) {
286
                                    String[] attributes = new String[2];
287
                                    attributes = (String[])featureMaker.getAttributes().get(j);
288
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
289
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
290
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
291
                        }
292
                    }*/
293
                                addShape(point, auxRow);
294
                        } else if (entity.getType()==0x0F && !addingToBlock) {
295
                                System.out.println("Encuentra una Polyline2D ...");
296
                                DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("FLAGS");
297
                                int flags = ((Integer)data.getValue()).intValue();
298
                                data = entity.getDwgObjectSpecificDataItem("FIRST_VERTEX_HANDLE");
299
                                int[] firstVertexHandle = (int[])data.getValue();
300
                                byte[] firstHandleBytes = new byte[]{0,0,0,0};
301
                                if (firstVertexHandle.length>2) firstHandleBytes[3] = (byte)firstVertexHandle[2];
302
                                if (firstVertexHandle.length>3) firstHandleBytes[2] = (byte)firstVertexHandle[3];
303
                                if (firstVertexHandle.length>4) firstHandleBytes[1] = (byte)firstVertexHandle[4];
304
                                if (firstVertexHandle.length>5) firstHandleBytes[0] = (byte)firstVertexHandle[5];
305
                                int firstHandle = ByteUtils.bytesToInt(firstHandleBytes, new int[]{0});
306
                                data = entity.getDwgObjectSpecificDataItem("LAST_VERTEX_HANDLE");
307
                                int[] lastVertexHandle = (int[])data.getValue();
308
                                byte[] lastHandleBytes = new byte[]{0,0,0,0};
309
                                if (lastVertexHandle.length>2) lastHandleBytes[3] = (byte)lastVertexHandle[2];
310
                                if (lastVertexHandle.length>3) lastHandleBytes[2] = (byte)lastVertexHandle[3];
311
                                if (lastVertexHandle.length>4) lastHandleBytes[1] = (byte)lastVertexHandle[4];
312
                                if (lastVertexHandle.length>5) lastHandleBytes[0] = (byte)lastVertexHandle[5];
313
                                int lastHandle = ByteUtils.bytesToInt(lastHandleBytes, new int[]{0});
314
                                Vector pts = new Vector();
315
                                Vector bulges = new Vector();
316
                                double[] pt = new double[2];
317
                                
318
                                for (int j=0;j<dwgObjects.size();j++) {
319
                                        DwgObject obj = (DwgObject)dwgObjects.get(j);
320
                                        Vector handle = obj.getHandle();
321
                                        byte[] handleBytes = new byte[]{0,0,0,0};
322
                                        if (handle.size()>2) handleBytes[3] = ((Integer)handle.get(2)).byteValue();
323
                                        if (handle.size()>3) handleBytes[2] = ((Integer)handle.get(3)).byteValue();
324
                                        if (handle.size()>4) handleBytes[1] = ((Integer)handle.get(4)).byteValue();
325
                                        if (handle.size()>5) handleBytes[0] = ((Integer)handle.get(5)).byteValue();
326
                                        int objHandle = ByteUtils.bytesToInt(handleBytes, new int[]{0});
327
                                        if (objHandle==firstHandle && ((Integer)handle.get(1)).intValue()==firstVertexHandle[1]) {
328
                                                int k=0;
329
                                                while (true) {
330
                                                        DwgObject vertex = (DwgObject)dwgObjects.get(j+k);
331
                                                        Vector vertexHandle = vertex.getHandle();
332
                                                        byte[] vHandleBytes = new byte[]{0,0,0,0};
333
                                                        if (vertexHandle.size()>2) vHandleBytes[3] = ((Integer)vertexHandle.get(2)).byteValue();
334
                                                        if (vertexHandle.size()>3) vHandleBytes[2] = ((Integer)vertexHandle.get(3)).byteValue();
335
                                                        if (vertexHandle.size()>4) vHandleBytes[1] = ((Integer)vertexHandle.get(4)).byteValue();
336
                                                        if (vertexHandle.size()>5) vHandleBytes[0] = ((Integer)vertexHandle.get(5)).byteValue();
337
                                                        int vHandle = ByteUtils.bytesToInt(vHandleBytes, new int[]{0});
338
                                                        data = vertex.getDwgObjectSpecificDataItem("POINT");
339
                                                        pt = (double[])data.getValue();
340
                                                        pts.add(new Point2D.Double(pt[0], pt[1]));
341
                                                        data = vertex.getDwgObjectSpecificDataItem("BULGE");
342
                                                        double bulge = ((Double)data.getValue()).doubleValue();
343
                                                        bulges.add(new Double(bulge));
344
                                                        k++;
345
                                                        if (vHandle==lastHandle && ((Integer)vertexHandle.get(1)).intValue()==lastVertexHandle[1]) {
346
                                                                break;
347
                                                        }
348
                                                }
349
                                        }
350
                                }
351
                                
352
                                Point2D[] newPts = new Point2D[pts.size()];
353
                                if (flags==0 || flags==2 || flags==4 || flags==8 || flags==16 || flags==32 || flags==64 || flags==128) {
354
                                        for (int j=0;j<pts.size();j++) {
355
                                                newPts[j] = (Point2D)pts.get(j);
356
                                        }
357
                                } else if (flags==1 || flags==3 || flags==5 || flags==9 || flags==17 || flags==33 || flags==65 || flags==129) {
358
                                        newPts = new Point2D[pts.size()+1];
359
                                        for (int j=0;j<pts.size();j++) {
360
                                                newPts[j] = (Point2D)pts.get(j);
361
                                        }
362
                                        newPts[pts.size()] = (Point2D)pts.get(0);
363
                                        bulges.add(new Double(0));
364
                                }
365
                                FPolyline2D pline = createPolyline2D(newPts, bulges);
366
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
367
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("Polyline2D"));
368
                                int[] layerHandle = (int[])entity.getLayer();
369
                                String layerName = getLayerName(entity, layerTable);
370
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
371
                                int color = entity.getColor();
372
                                int colorByLayer = setColorByLayer(layerHandle, layerTable);
373
                                //if (color==0) color ByBlock
374
                                if (color==256) color = colorByLayer;
375
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
376
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
377
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
378
                    // Attributes
379
                    /*for (int j=0;j<nAtt;j++) {
380
                                    String[] attributes = new String[2];
381
                                    attributes = (String[])featureMaker.getAttributes().get(j);
382
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
383
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
384
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
385
                        }
386
                    }*/
387
                                addShape(pline, auxRow);
388
                        // Implementaci?n parcial ...
389
                        } else if (entity.getType()==0x10 && !addingToBlock) {
390
                                System.out.println("Encuentra una Polyline3D ...");
391
                                DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("CLOSED_FLAGS");
392
                                int flags = ((Integer)data.getValue()).intValue();
393
                                data = entity.getDwgObjectSpecificDataItem("FIRST_VERTEX_HANDLE");
394
                                int[] firstVertexHandle = (int[])data.getValue();
395
                                data = entity.getDwgObjectSpecificDataItem("LAST_VERTEX_HANDLE");
396
                                int[] lastVertexHandle = (int[])data.getValue();
397
                                data = entity.getDwgObjectSpecificDataItem("SEQEND_HANDLE");
398
                                int[] seqendHandle = (int[])data.getValue();
399
                                int vertexNumber = calculateVertexNumber(firstVertexHandle, lastVertexHandle);
400
                                Point2D[] pts = new Point2D[vertexNumber];
401
                                double[] pt = new double[2];
402
                                
403
                                for (int j=0;j<dwgObjects.size();j++) {
404
                                        DwgObject v = (DwgObject)dwgObjects.get(j);
405
                                        Vector handle = v.getHandle();
406
                                        if (((Integer)handle.get(handle.size()-2)).intValue()==firstVertexHandle[handle.size()-2] && ((Integer)handle.get(handle.size()-1)).intValue()==firstVertexHandle[handle.size()-1]) {
407
                                                for (int k=0; k<vertexNumber; k++) {
408
                                                        DwgObject vertex = (DwgObject)dwgObjects.get(j+k);
409
                                                        data = vertex.getDwgObjectSpecificDataItem("POINT");
410
                                                        pt = (double[])data.getValue();
411
                                                        pts[k] = new Point2D.Double(pt[0], pt[1]);
412
                                                }
413
                                        }
414
                                }
415
                                Point2D[] newPts = new Point2D[pts.length];
416
                                if (flags==0 || flags==2 || flags==4 || flags==8 || flags==16 || flags==32 || flags==64 || flags==128) {
417
                                        newPts = pts;
418
                                } else if (flags==1 || flags==3 || flags==5 || flags==9 || flags==17 || flags==33 || flags==65 || flags==129) {
419
                                        newPts = new Point2D[pts.length+1];
420
                                        for (int j=0;j<pts.length;j++) {
421
                                                newPts[j] = pts[j];
422
                                        }
423
                                        newPts[pts.length] = pts[0];
424
                                }
425
                                FPolyline2D pline3d = createPolyline3D(newPts);
426
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
427
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("Polyline3D"));
428
                                int[] layerHandle = (int[])entity.getLayer();
429
                                String layerName = getLayerName(entity, layerTable);
430
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
431
                                int color = entity.getColor();
432
                                int colorByLayer = setColorByLayer(layerHandle, layerTable);
433
                                //if (color==0) color ByBlock
434
                                if (color==256) color = colorByLayer;
435
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
436
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
437
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
438
                    // Attributes
439
                    /*for (int j=0;j<nAtt;j++) {
440
                                    String[] attributes = new String[2];
441
                                    attributes = (String[])featureMaker.getAttributes().get(j);
442
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
443
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
444
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
445
                        }
446
                    }*/
447
                                addShape(pline3d, auxRow);
448
                        } else if (entity.getType()==0x1 && !addingToBlock) {
449
                                System.out.println("Encuentra un Text ...");
450
                                DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("INSERTION_POINT");
451
                                Point2D p = (Point2D)data.getValue();
452
                                FPoint2D point = createPoint(p);
453
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
454
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("Text"));
455
                                int[] layerHandle = (int[])entity.getLayer();
456
                                String layerName = getLayerName(entity, layerTable);
457
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
458
                                int color = entity.getColor();
459
                                int colorByLayer = setColorByLayer(layerHandle, layerTable);
460
                                //if (color==0) color ByBlock
461
                                if (color==256) color = colorByLayer;
462
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
463
                                data = entity.getDwgObjectSpecificDataItem("TEXT");
464
                                String text = (String)data.getValue();
465
                auxRow[ID_FIELD_TEXT] = ValueFactory.createValue(new String(text));
466
                                data = entity.getDwgObjectSpecificDataItem("HEIGHT");
467
                                heightText = ((Float)data.getValue()).floatValue();
468
                    auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory.createValue(heightText);
469
                                data = entity.getDwgObjectSpecificDataItem("DATA_FLAG");
470
                                int dflag = ((Integer)data.getValue()).intValue();
471
                                if ((dflag & 0x8) == 0) {
472
                                        data = entity.getDwgObjectSpecificDataItem("ROTATION_ANGLE");
473
                                        double textRot = ((Double)data.getValue()).doubleValue();
474
                            auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory.createValue(Math.toDegrees(textRot));
475
                                } else {
476
                            auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory.createValue(0.0);
477
                                }
478
                                if ((dflag & 0x1)==0) {
479
                                        data = entity.getDwgObjectSpecificDataItem("ELEVATION");
480
                                        double elev = ((Double)data.getValue()).doubleValue();
481
                            auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(elev);
482
                                }
483
                                data = entity.getDwgObjectSpecificDataItem("THICKNESS");
484
                                double thickness = ((Double)data.getValue()).doubleValue();
485
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(thickness);
486
                    // Attributes
487
                    /*for (int j=0;j<nAtt;j++) {
488
                                    String[] attributes = new String[2];
489
                                    attributes = (String[])featureMaker.getAttributes().get(j);
490
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
491
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
492
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
493
                        }
494
                    }*/
495
                                addShape(point, auxRow);
496
                        } else if (entity.getType()==0x4) {
497
                                System.out.println("Encuentra un Block ...");
498
                                DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("NAME");
499
                                String blockName = (String)data.getValue();
500
                                addingToBlock = true;
501
                                //pto = (Point2D.Double)point.get(0);
502
                                //FShape nuevoShp;
503
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
504
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("Block"));
505
                                int[] layerHandle = (int[])entity.getLayer();
506
                                String layerName = getLayerName(entity, layerTable);
507
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
508
                                int color = entity.getColor();
509
                                int colorByLayer = setColorByLayer(layerHandle, layerTable);
510
                                //if (color==0) color ByBlock
511
                                if (color==256) color = colorByLayer;
512
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
513
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
514
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
515
                    // Attributes
516
                    /*for (int j=0;j<nAtt;j++) {
517
                                    String[] attributes = new String[2];
518
                                    attributes = (String[])featureMaker.getAttributes().get(j);
519
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
520
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
521
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
522
                        }
523
                    }*/
524
                                //addShape(point, auxRow);
525
                        } else if (entity.getType()==0x5) {
526
                                System.out.println("Encuentra un Endblk ...");
527
                                addingToBlock = false;
528
                                //pto = (Point2D.Double)point.get(0);
529
                                //FShape nuevoShp;
530
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
531
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("EndBlk"));
532
                                int[] layerHandle = (int[])entity.getLayer();
533
                                String layerName = getLayerName(entity, layerTable);
534
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
535
                                int color = entity.getColor();
536
                                int colorByLayer = setColorByLayer(layerHandle, layerTable);
537
                                //if (color==0) color ByBlock
538
                                if (color==256) color = colorByLayer;
539
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
540
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
541
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
542
                    // Attributes
543
                    /*for (int j=0;j<nAtt;j++) {
544
                                    String[] attributes = new String[2];
545
                                    attributes = (String[])featureMaker.getAttributes().get(j);
546
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
547
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
548
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
549
                        }
550
                    }*/
551
                                //addShape(point, auxRow);
552
                        } else if (entity.getType()==0x30) {
553
                                System.out.println("Encuentra un Block Control Object ...");
554
                                //addingToBlock = false;
555
                                //pto = (Point2D.Double)point.get(0);
556
                                //FShape nuevoShp;
557
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
558
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("Block Control Object"));
559
                                int[] layerHandle = (int[])entity.getLayer();
560
                                String layerName = getLayerName(entity, layerTable);
561
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
562
                                int color = entity.getColor();
563
                                int colorByLayer = setColorByLayer(layerHandle, layerTable);
564
                                //if (color==0) color ByBlock
565
                                if (color==256) color = colorByLayer;
566
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
567
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
568
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
569
                    // Attributes
570
                    /*for (int j=0;j<nAtt;j++) {
571
                                    String[] attributes = new String[2];
572
                                    attributes = (String[])featureMaker.getAttributes().get(j);
573
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
574
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
575
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
576
                        }
577
                    }*/
578
                                //addShape(point, auxRow);
579
                        } else if (entity.getType()==0x31) {
580
                                System.out.println("Encuentra un Block Header ...");
581
                                addingToBlock = true;
582
                                //pto = (Point2D.Double)point.get(0);
583
                                //FShape nuevoShp;
584
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
585
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("Block Header"));
586
                                int[] layerHandle = (int[])entity.getLayer();
587
                                String layerName = getLayerName(entity, layerTable);
588
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
589
                                int color = entity.getColor();
590
                                int colorByLayer = setColorByLayer(layerHandle, layerTable);
591
                                //if (color==0) color ByBlock
592
                                if (color==256) color = colorByLayer;
593
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
594
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
595
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
596
                    // Attributes
597
                    /*for (int j=0;j<nAtt;j++) {
598
                                    String[] attributes = new String[2];
599
                                    attributes = (String[])featureMaker.getAttributes().get(j);
600
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
601
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
602
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
603
                        }
604
                    }*/
605
                                //addShape(point, auxRow);
606
                        } else if (entity.getType()==0x7 && !addingToBlock) {
607
                                System.out.println("Encuentra un Insert ...");
608
                                DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("INSERTION_POINT");
609
                                double[] p = (double[])data.getValue();
610
                                Point2D point = new Point2D.Double(p[0], p[1]);
611
                                data = entity.getDwgObjectSpecificDataItem("SCALE");
612
                                double[] scale = (double[])data.getValue();
613
                                data = entity.getDwgObjectSpecificDataItem("ROTATION");
614
                double rot = ((Double)data.getValue()).doubleValue();
615
                                data = entity.getDwgObjectSpecificDataItem("BLOCK_HEADER_HANDLE");
616
                                int[] blockHandle = (int[])data.getValue();
617
                                
618
                                manageInsert(dwgObjects, point, scale, rot, blockHandle, auxRow);
619
                                
620
                                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
621
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("Insert"));
622
                                int[] layerHandle = (int[])entity.getLayer();
623
                                String layerName = getLayerName(entity, layerTable);
624
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
625
                                int color = entity.getColor();
626
                                int colorByLayer = setColorByLayer(layerHandle, layerTable);
627
                                //if (color==0) color ByBlock
628
                                if (color==256) color = colorByLayer;
629
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
630
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
631
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
632
                    // Attributes
633
                    /*for (int j=0;j<nAtt;j++) {
634
                                    String[] attributes = new String[2];
635
                                    attributes = (String[])featureMaker.getAttributes().get(j);
636
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
637
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
638
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
639
                        }
640
                    }*/
641
                    FPoint2D fPoint = createPoint(point);
642
                                addShape(fPoint, auxRow);
643
                        } else if (entity.getType()==0x2C && !addingToBlock) {
644
                                System.out.println("Encuentra un MText ...");
645
                                DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("INSERTION_POINT");
646
                                double[] coord = (double[])data.getValue();
647
                                Point2D p = new Point2D.Double(coord[0], coord[1]);
648
                                FPoint2D point = createPoint(p);
649
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
650
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("MText"));
651
                                int[] layerHandle = (int[])entity.getLayer();
652
                                String layerName = getLayerName(entity, layerTable);
653
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
654
                                int color = entity.getColor();
655
                                int colorByLayer = setColorByLayer(layerHandle, layerTable);
656
                                //if (color==0) color ByBlock
657
                                if (color==256) color = colorByLayer;
658
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
659
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
660
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
661
                    // Attributes
662
                    /*for (int j=0;j<nAtt;j++) {
663
                                    String[] attributes = new String[2];
664
                                    attributes = (String[])featureMaker.getAttributes().get(j);
665
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
666
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
667
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
668
                        }
669
                    }*/
670
                                addShape(point, auxRow);
671
                        } else if (entity.getType()==0x1F && !addingToBlock) {
672
                                System.out.println("Encuentra un Solid ...");
673
                                DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("CORNER1");
674
                                double[] coord = (double[])data.getValue();
675
                                Point2D p1 = new Point2D.Double(coord[0], coord[1]);
676
                                data = entity.getDwgObjectSpecificDataItem("CORNER2");
677
                                coord = (double[])data.getValue();
678
                                Point2D p2 = new Point2D.Double(coord[0], coord[1]);
679
                                data = entity.getDwgObjectSpecificDataItem("CORNER3");
680
                                coord = (double[])data.getValue();
681
                                Point2D p3 = new Point2D.Double(coord[0], coord[1]);
682
                                data = entity.getDwgObjectSpecificDataItem("CORNER4");
683
                                coord = (double[])data.getValue();
684
                                Point2D p4 = new Point2D.Double(coord[0], coord[1]);
685
                                FPolygon2D solid = createSolid(new Point2D[]{p1, p2, p4, p3});
686
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
687
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("Solid"));
688
                                int[] layerHandle = (int[])entity.getLayer();
689
                                String layerName = getLayerName(entity, layerTable);
690
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
691
                                int color = entity.getColor();
692
                                int colorByLayer = setColorByLayer(layerHandle, layerTable);
693
                                //if (color==0) color ByBlock
694
                                if (color==256) color = colorByLayer;
695
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
696
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
697
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
698
                    // Attributes
699
                    /*for (int j=0;j<nAtt;j++) {
700
                                    String[] attributes = new String[2];
701
                                    attributes = (String[])featureMaker.getAttributes().get(j);
702
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
703
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
704
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
705
                        }
706
                    }*/
707
                                addShape(solid, auxRow);
708
                        } else if (entity.getType()==0x24 && !addingToBlock) {
709
                                System.out.println("Encuentra un Spline ...");
710
                                DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("SCENARIO");
711
                                int sc = ((Integer)data.getValue()).intValue();
712
                                if (sc==1) {
713
                                        data = entity.getDwgObjectSpecificDataItem("CONTROL_POINTS");
714
                                } else if (sc==2) {
715
                                        data = entity.getDwgObjectSpecificDataItem("FIT_POINTS");
716
                                }
717
                                double[][] vertices = (double[][])data.getValue();
718
                                Point2D[] pts = new Point2D[vertices.length];
719
                                double[] pt = new double[2];
720
                                for (int j=0; j<vertices.length; j++) {
721
                                         pt = (double[])vertices[j];
722
                                         pts[j] = new Point2D.Double(pt[0], pt[1]);
723
                                }
724
                                FPolyline2D spline = createLwPolyline(pts);
725
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
726
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("Spline"));
727
                                int[] layerHandle = (int[])entity.getLayer();
728
                                String layerName = getLayerName(entity, layerTable);
729
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
730
                                int color = entity.getColor();
731
                                int colorByLayer = setColorByLayer(layerHandle, layerTable);
732
                                //if (color==0) color ByBlock
733
                                if (color==256) color = colorByLayer;
734
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
735
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
736
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
737
                    // Attributes
738
                    /*for (int j=0;j<nAtt;j++) {
739
                                    String[] attributes = new String[2];
740
                                    attributes = (String[])featureMaker.getAttributes().get(j);
741
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
742
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
743
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
744
                        }
745
                    }*/
746
                                addShape(spline, auxRow);
747
                        } else if (dwg.getDxfNames().size()>0) {
748
                                int type = 0;
749
                                for (int j=0; j<dwg.getDxfNames().size(); j++) {
750
                                        String dxfName = ((DxfName)dwg.getDxfNames().get(j)).getDxfName();
751
                                        if (dxfName.equals("LWPOLYLINE")) {
752
                                                type = ((DxfName)dwg.getDxfNames().get(j)).getKey();
753
                                                break;
754
                                        }
755
                                }
756
                                if (entity.getType()==type) {
757
                                        DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("VERTICES");
758
                                        Vector vertices = (Vector)data.getValue();
759
                                        Point2D[] pts = new Point2D[vertices.size()];
760
                                        double[] pt = new double[2];
761
                                        for (int j=0; j<vertices.size(); j++) {
762
                                                 pt = (double[])vertices.get(j);
763
                                                 pts[j] = new Point2D.Double(pt[0], pt[1]);
764
                                        }
765
                                        FPolyline2D lwpline = createLwPolyline(pts);
766
                        auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
767
                        auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("LwPolyline"));
768
                                        int[] layerHandle = (int[])entity.getLayer();
769
                                        String layerName = getLayerName(entity, layerTable);
770
                        auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(layerName);
771
                                        int color = entity.getColor();
772
                                        int colorByLayer = setColorByLayer(layerHandle, layerTable);
773
                                        //if (color==0) color ByBlock
774
                                        if (color==256) color = colorByLayer;
775
                        auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(color);
776
                            auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
777
                            auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
778
                            // Attributes
779
                            /*for (int j=0;j<nAtt;j++) {
780
                                            String[] attributes = new String[2];
781
                                            attributes = (String[])featureMaker.getAttributes().get(j);
782
                                auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
783
                                if (!fea.getProp(attributes[0]).equals(attributes[1])) {
784
                                        auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
785
                                }
786
                            }*/
787
                                        addShape(lwpline, auxRow);
788
                                }
789
                        } else {
790
                                System.out.println("Detectado dwgObject pendiente de implementar");
791
                        }
792
                }
793
                timeElapsed = System.currentTimeMillis()-time;
794
                System.out.println("Tiempo empleado por el driver en importar el contenido del dwg = " + timeElapsed);
795
                
796
                defaultLegend = LegendFactory.createVectorialUniqueValueLegend(getShapeType());
797
                defaultLegend.setFieldName("Color");
798
                defaultLegend.setLabelField("Text");
799
                defaultLegend.setDefaultSymbol(new FSymbol(getShapeType()));
800
                defaultLegend.getDefaultSymbol().setShapeVisible(false);
801
                defaultLegend.getDefaultSymbol().setFontSizeInPixels(false);
802
                defaultLegend.getDefaultSymbol().setFont(new Font("SansSerif", Font.PLAIN, 9));
803
                defaultLegend.getDefaultSymbol().setFontColor(Color.BLACK);
804
                defaultLegend.getDefaultSymbol().setFontSize(heightText);
805
                defaultLegend.getDefaultSymbol().setStyle(FConstant.SYMBOL_STYLE_DGNSPECIAL);
806
                defaultLegend.getDefaultSymbol().setSize(3);
807
                defaultLegend.getDefaultSymbol().setSizeInPixels(true);
808
                
809
                defaultLegend.setLabelHeightField("HeightText");
810
                defaultLegend.setLabelRotationField("RotationText");
811
                
812
        ObjectDriver rs = this;
813
                IntValue clave;
814
                FSymbol theSymbol = null;
815
                
816
                try {
817
                    // TODO: Provisional hasta que cambiemos los s?mbolos.
818
                   /*  BufferedImage bi= new BufferedImage(5, 5, BufferedImage.TYPE_INT_ARGB);
819
                   Graphics2D big = bi.createGraphics();
820
                   Color color=new Color(0,0,0,0);
821
                   big.setBackground(color);
822
                   big.clearRect(0, 0, 5, 5);
823
                   Paint fillProv = null; 
824
                   Rectangle2D rProv = new Rectangle();
825
                   rProv.setFrame(0, 0,5,5);
826
                   fillProv = new TexturePaint(bi,rProv); */                    
827
                    
828
                        for (long j = 0; j < rs.getRowCount(); j++)
829
                        {
830
                                clave = (IntValue)rs.getFieldValue(j,ID_FIELD_COLOR);
831
                                if (defaultLegend.getSymbolByValue(clave) == null)
832
                                {
833
                                        theSymbol = new FSymbol(getShapeType());
834
                                        theSymbol.setDescription(clave.toString());
835
                                        theSymbol.setColor(AcadColor.getColor(clave.getValue()));
836
                                        // theSymbol.setFill(fillProv);
837
                                        // 050202, jmorell: Asigna los colores de Autocad a los bordes
838
                                        //                                        de los pol?gonos.
839
                                        theSymbol.setOutlineColor(AcadColor.getColor(clave.getValue()));
840
                                        
841
                                        theSymbol.setStyle(FConstant.SYMBOL_STYLE_DGNSPECIAL);
842
                                        theSymbol.setSize(3);
843
                                        theSymbol.setSizeInPixels(true);
844
                                        
845
                                        defaultLegend.addSymbol(clave, theSymbol);
846
                                }
847
                        } // for
848
                } catch (DriverException e) {
849
                        e.printStackTrace();
850
                        throw new IOException("Error al poner la leyenda por defecto en el Dwg");
851
                }
852
        }
853
        
854
        /**
855
         * En principio exportamos a FPolyline2D, despues estudiaremos la conveniencia
856
         * de exportar a FPolygon2D, o a las dos cosas ...
857
         * Esta clase construye el circle del mismo modo que en DxfFeatureMaker.
858
         * Se podr?a renombrar esta clase a createCircleFeature y a?adir otra
859
         * createCircleEntity ...
860
         * La parte de creaci?n de la FPolyline2D est? sacada del DxfMemoryDriver ...
861
         * @param center
862
         * @param radius
863
         * @return
864
         */
865
        private FPolyline2D createCircle(Point2D c, double r) {
866
                Point2D[] pts = new Point2D[360];
867
                int angulo = 0;
868
                for (angulo=0; angulo<360; angulo++) {
869
                        pts[angulo] = new Point2D.Double(c.getX(), c.getY());
870
                        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));
871
                }
872
                GeneralPathX genPathX = new GeneralPathX();
873
                genPathX.moveTo(pts[0].getX(), pts[0].getY());
874
                for (int i=1; i<pts.length; i++) {
875
                        genPathX.lineTo(pts[i].getX(), pts[i].getY());
876
                }
877
                return new FPolyline2D(genPathX);
878
        }
879
        
880
        /**
881
         * Como createCircle ...
882
         * @param c
883
         * @param r
884
         * @param sa
885
         * @param ea
886
         * @return
887
         */
888
        private Point2D[] createArc(Point2D c, double r, double sa, double ea) {
889
                int isa = (int)sa;
890
                int iea = (int)ea;
891
                double angulo;
892
                Point2D[] pts;
893
                if (sa <= ea) {
894
                        pts = new Point2D[(iea-isa)+2];
895
                        angulo = sa;
896
                        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));
897
                        for (int i=1; i<=(iea-isa)+1; i++) {
898
                                angulo = (double)(isa+i);
899
                                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));
900
                        }
901
                        angulo = ea;
902
                        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));
903
                } else {
904
                        pts = new Point2D[(360-isa)+iea+2];
905
                        angulo = sa;
906
                        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));
907
                        for (int i=1; i<=(360-isa); i++) {
908
                                angulo = (double)(isa+i);
909
                                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));
910
                        }
911
                        for (int i=(360-isa)+1; i<=(360-isa)+iea; i++) {
912
                                angulo = (double)(i-(360-isa));
913
                                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));
914
                        }
915
                        angulo = ea;
916
                        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));
917
                }
918
                return pts;
919
                /*GeneralPathX genPathX = new GeneralPathX();
920
                genPathX.moveTo(pts[0].getX(), pts[0].getY());
921
                for (int i=1; i<pts.length; i++) {
922
                        genPathX.lineTo(pts[i].getX(), pts[i].getY());
923
                }
924
                return new FPolyline2D(genPathX);*/
925
        }
926
        
927
        /**
928
         * Los nombres de estos m?todos pueden cambiarse por otros m?s apropiados, por
929
         * ejemplo:
930
         * createLine --> dwgLineToFPolyline2D ... ?mejor as?? ...
931
         * @param p1
932
         * @param p2
933
         * @return
934
         */
935
        private FPolyline2D createLine(Point2D p1, Point2D p2) {
936
                Point2D[] pts = new Point2D[]{p1, p2};
937
                GeneralPathX genPathX = new GeneralPathX();
938
                genPathX.moveTo(pts[0].getX(), pts[0].getY());
939
                for (int i=1; i<pts.length; i++) {
940
                        genPathX.lineTo(pts[i].getX(), pts[i].getY());
941
                }
942
                return new FPolyline2D(genPathX);
943
        }
944
        
945
        private FPoint2D createPoint(Point2D p) {
946
                return new FPoint2D(p.getX(), p.getY());
947
        }
948
        
949
        private FPolyline2D createLwPolyline(Point2D[] pts) {
950
                GeneralPathX genPathX = new GeneralPathX();
951
                genPathX.moveTo(pts[0].getX(), pts[0].getY());
952
                for (int i=1; i<pts.length; i++) {
953
                        genPathX.lineTo(pts[i].getX(), pts[i].getY());
954
                }
955
                return new FPolyline2D(genPathX);
956
        }
957
        
958
        private FPolyline2D createPolyline3D(Point2D[] pts) {
959
                GeneralPathX genPathX = new GeneralPathX();
960
                genPathX.moveTo(pts[0].getX(), pts[0].getY());
961
                for (int i=1; i<pts.length; i++) {
962
                        genPathX.lineTo(pts[i].getX(), pts[i].getY());
963
                }
964
                return new FPolyline2D(genPathX);
965
        }
966
        
967
        private FPolyline2D createPolyline2D(Point2D[] inPts, Vector bulges) {
968
                Vector pts = new Vector();
969
                Point2D init = new Point2D.Double();
970
                Point2D end = new Point2D.Double();
971
                for (int i=0; i<inPts.length; i++) {
972
                        init = inPts[i];
973
                        if (i!=inPts.length-1) end = inPts[i+1];
974
                        if (((Double)bulges.get(i)).doubleValue()==0 || i==inPts.length-1) {
975
                                pts.add(init);
976
                        } else {
977
                                ArcFromBulgeCalculator arcCalculator = new ArcFromBulgeCalculator(init, end, ((Double)bulges.get(i)).doubleValue());
978
                                Vector arc = arcCalculator.getPoints(1);
979
                                for (int j=0;j<arc.size();j++) {
980
                                        pts.add(arc.get(j));
981
                                }
982
                                pts.remove(pts.size()-1);
983
                        }
984
                }
985
                GeneralPathX genPathX = new GeneralPathX();
986
                genPathX.moveTo(((Point2D)pts.get(0)).getX(), ((Point2D)pts.get(0)).getY());
987
                for (int i=1; i<pts.size(); i++) {
988
                        genPathX.lineTo(((Point2D)pts.get(i)).getX(), ((Point2D)pts.get(i)).getY());
989
                }
990
                return new FPolyline2D(genPathX);
991
        }
992
        
993
        private FPolygon2D createSolid(Point2D[] pts) {
994
                GeneralPathX genPathX = new GeneralPathX();
995
                genPathX.moveTo(pts[0].getX(), pts[0].getY());
996
                for (int i=1; i<pts.length; i++) {
997
                        genPathX.lineTo(pts[i].getX(), pts[i].getY());
998
                }
999
                genPathX.closePath();
1000
                return new FPolygon2D(genPathX);
1001
        }
1002
        
1003
        private int calculateVertexNumber(int[] firstVertexHandle, int[] lastVertexHandle) {
1004
                if (firstVertexHandle.length==lastVertexHandle.length) {
1005
                        if (firstVertexHandle[firstVertexHandle.length-2]==lastVertexHandle[firstVertexHandle.length-2]) {
1006
                                return (lastVertexHandle[firstVertexHandle.length-1]-firstVertexHandle[firstVertexHandle.length-1]+1);
1007
                        } else {
1008
                                int aux = 255-firstVertexHandle[firstVertexHandle.length-1]+1;
1009
                                int aux2 = lastVertexHandle[firstVertexHandle.length-1]+1;
1010
                                int aux3 = (lastVertexHandle[firstVertexHandle.length-2]-firstVertexHandle[firstVertexHandle.length-2]-1)*255;
1011
                                int vn = aux + aux2 + aux3;
1012
                                return vn;
1013
                        }
1014
                } else if (firstVertexHandle.length==(lastVertexHandle.length-1)) {
1015
                        int aux = 255-firstVertexHandle[firstVertexHandle.length-1]+1;
1016
                        int aux2 = lastVertexHandle[firstVertexHandle.length]+1;
1017
                        int aux3 = (lastVertexHandle[lastVertexHandle.length-2]-1)*255;
1018
                        int vn = aux + aux2 + aux3;
1019
                        return vn;
1020
                } else {
1021
                        System.out.println("ERROR: firstVertexHandle y lastVertexHandle tienen tama?os inesperados");
1022
                        return 0;
1023
                }
1024
        }
1025
        
1026
        private void manageInsert(Vector dwgObjects, Point2D insPoint, double[] scale, double rot, int[] blockHandle, Value[] auxRow) {
1027
                byte[] blockHandleBytes = new byte[]{0,0,0,0};
1028
                if (blockHandle.length>2) blockHandleBytes[3] = (byte)blockHandle[2];
1029
                if (blockHandle.length>3) blockHandleBytes[2] = (byte)blockHandle[3];
1030
                if (blockHandle.length>4) blockHandleBytes[1] = (byte)blockHandle[4];
1031
                if (blockHandle.length>5) blockHandleBytes[0] = (byte)blockHandle[5];
1032
                int bHandle = ByteUtils.bytesToInt(blockHandleBytes, new int[]{0});
1033
                for (int j=0;j<dwgObjects.size();j++) {
1034
                        DwgObject obj = (DwgObject)dwgObjects.get(j);
1035
                        DwgObjectSpecificDataItem dataa = obj.getDwgObjectSpecificDataItem("BLOCK_ENTITY_HANDLE");
1036
                        //System.out.println("obj.getType() = " + obj.getType());
1037
                        //System.out.println("dataa = " + dataa);
1038
                        if (dataa!=null) {
1039
                                int[] handle = (int[])dataa.getValue();
1040
                                byte[] handleBytes = new byte[]{0,0,0,0};
1041
                                if (handle.length>2) handleBytes[3] = (byte)handle[2];
1042
                                if (handle.length>3) handleBytes[2] = (byte)handle[3];
1043
                                if (handle.length>4) handleBytes[1] = (byte)handle[4];
1044
                                if (handle.length>5) handleBytes[0] = (byte)handle[5];
1045
                                int objHandle = ByteUtils.bytesToInt(handleBytes, new int[]{0});
1046
                                //System.out.println("objHandle = " + objHandle);
1047
                                //System.out.println("bHandle = " + bHandle);
1048
                                if (objHandle==bHandle+1 && handle[1]==blockHandle[1]) {
1049
                                        //obj = (DwgObject)dwgObjects.get(j+1);
1050
                                        //System.out.println("Hemos encontrado el bloque");
1051
                                        //System.out.println("obj.getType() = " + obj.getType());
1052
                                        DwgObjectSpecificDataItem data = obj.getDwgObjectSpecificDataItem("BASE_POINT");
1053
                                        double[] bPoint = (double[])data.getValue();
1054
                                        //System.out.println("bPoint[0] = " + bPoint[0]);
1055
                                        //System.out.println("bPoint[1] = " + bPoint[1]);
1056
                                        data = obj.getDwgObjectSpecificDataItem("FIRST_ENTITY_HANDLE");
1057
                                        int[] firstEntityHandle = (int[])data.getValue();
1058
                                        //System.out.println("firstEntityHandle[0] = " + firstEntityHandle[0]);
1059
                                        //System.out.println("firstEntityHandle[1] = " + firstEntityHandle[1]);
1060
                                        //if (firstEntityHandle.length>2) System.out.println("firstEntityHandle[2] = " + firstEntityHandle[2]);
1061
                                        //if (firstEntityHandle.length>3) System.out.println("firstEntityHandle[3] = " + firstEntityHandle[3]);
1062
                                        byte[] firstEntityBytes = new byte[]{0,0,0,0};
1063
                                        if (firstEntityHandle.length>2) firstEntityBytes[3] = (byte)firstEntityHandle[2];
1064
                                        if (firstEntityHandle.length>3) firstEntityBytes[2] = (byte)firstEntityHandle[3];
1065
                                        if (firstEntityHandle.length>4) firstEntityBytes[1] = (byte)firstEntityHandle[4];
1066
                                        if (firstEntityHandle.length>5) firstEntityBytes[0] = (byte)firstEntityHandle[5];
1067
                                        int firstHandle = ByteUtils.bytesToInt(firstEntityBytes, new int[]{0});
1068
                                        //System.out.println("firstHandle = " + firstHandle);
1069
                                        data = obj.getDwgObjectSpecificDataItem("LAST_ENTITY_HANDLE");
1070
                                        int[] lastEntityHandle = (int[])data.getValue();
1071
                                        byte[] lastEntityBytes = new byte[]{0,0,0,0};
1072
                                        if (lastEntityHandle.length>2) lastEntityBytes[3] = (byte)lastEntityHandle[2];
1073
                                        if (lastEntityHandle.length>3) lastEntityBytes[2] = (byte)lastEntityHandle[3];
1074
                                        if (lastEntityHandle.length>4) lastEntityBytes[1] = (byte)lastEntityHandle[4];
1075
                                        if (lastEntityHandle.length>5) lastEntityBytes[0] = (byte)lastEntityHandle[5];
1076
                                        int lastHandle = ByteUtils.bytesToInt(lastEntityBytes, new int[]{0});
1077
                                        //System.out.println("lastHandle = " + lastHandle);
1078
                                        
1079
                                        for (int k=0;k<dwgObjects.size();k++) {
1080
                                                DwgObject ent = (DwgObject)dwgObjects.get(k);
1081
                                                Vector entHandle = ent.getHandle();
1082
                                                byte[] entHandleBytes = new byte[]{0,0,0,0};
1083
                                                if (entHandle.size()>2) entHandleBytes[3] = ((Integer)entHandle.get(2)).byteValue();
1084
                                                if (entHandle.size()>3) entHandleBytes[2] = ((Integer)entHandle.get(3)).byteValue();
1085
                                                if (entHandle.size()>4) entHandleBytes[1] = ((Integer)entHandle.get(4)).byteValue();
1086
                                                if (entHandle.size()>5) entHandleBytes[0] = ((Integer)entHandle.get(5)).byteValue();
1087
                                                int eHandle = ByteUtils.bytesToInt(entHandleBytes, new int[]{0});
1088
                                                //System.out.println("eHandle = " + eHandle);
1089
                                                if (eHandle==firstHandle && ((Integer)entHandle.get(1)).intValue()==firstEntityHandle[1]) {
1090
                                                        //System.out.println("Hemos encontrado el primer elemento");
1091
                                                        int l=0;
1092
                                                        while (true) {
1093
                                                                DwgObject blockEntity = (DwgObject)dwgObjects.get(k+l);
1094
                                                                Vector blockEntityHandle = blockEntity.getHandle();
1095
                                                                byte[] blockEntityHandleBytes = new byte[]{0,0,0,0};
1096
                                                                if (blockEntityHandle.size()>2) blockEntityHandleBytes[3] = ((Integer)blockEntityHandle.get(2)).byteValue();
1097
                                                                if (blockEntityHandle.size()>3) blockEntityHandleBytes[2] = ((Integer)blockEntityHandle.get(3)).byteValue();
1098
                                                                if (blockEntityHandle.size()>4) blockEntityHandleBytes[1] = ((Integer)blockEntityHandle.get(4)).byteValue();
1099
                                                                if (blockEntityHandle.size()>5) blockEntityHandleBytes[0] = ((Integer)blockEntityHandle.get(5)).byteValue();
1100
                                                                int bEntityHandle = ByteUtils.bytesToInt(blockEntityHandleBytes, new int[]{0});
1101
                                                                manageBlockEntity(blockEntity, bPoint, insPoint, scale, rot, auxRow);
1102
                                                                l++;
1103
                                                                if (bEntityHandle==lastHandle && ((Integer)entHandle.get(1)).intValue()==lastEntityHandle[1]) {
1104
                                                                        break;
1105
                                                                }
1106
                                                        }
1107
                                                }
1108
                                        }
1109
                                }
1110
                        }
1111
                }
1112
        }
1113
        
1114
        private void manageBlockEntity(DwgObject entity, double[] bPoint, Point2D insPoint, double[] scale, double rot, Value[] auxRow) {
1115
                if (entity.getType()==0x11) {
1116
                        System.out.println("Encuentra un arco dentro de un bloque ...");
1117
                        DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("CENTER");
1118
                        double[] coord = (double[])data.getValue();
1119
                        Point2D center = new Point2D.Double(coord[0], coord[1]);
1120
                        data = entity.getDwgObjectSpecificDataItem("RADIUS");
1121
                        double radius = ((Double)data.getValue()).doubleValue();
1122
                        data = entity.getDwgObjectSpecificDataItem("START_ANGLE");
1123
                        double startAngle = Math.toDegrees(((Double)data.getValue()).doubleValue());
1124
                        data = entity.getDwgObjectSpecificDataItem("END_ANGLE");
1125
                        double endAngle = Math.toDegrees(((Double)data.getValue()).doubleValue());
1126
                        //FPolyline2D arc = createArc(center, radius, startAngle, endAngle);
1127
                        Point2D[] arc = createArc(center, radius, startAngle, endAngle);
1128
                        for (int i=0;i<arc.length;i++) {
1129
                                Point2D pointAux = new Point2D.Double(arc[i].getX() - bPoint[0], arc[i].getY() - bPoint[1]);
1130
                                double laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot));
1131
                                double laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot));
1132
                                arc[i] = new Point2D.Double(laX, laY);
1133
                        }
1134
                        FPolyline2D arcc = createPolyline3D(arc);
1135
            auxRow[ID_FIELD_ID] = ValueFactory.createValue(0);
1136
            auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
1137
            auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
1138
            auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
1139
                auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
1140
                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
1141
                // Attributes
1142
                /*for (int j=0;j<nAtt;j++) {
1143
                                String[] attributes = new String[2];
1144
                                attributes = (String[])featureMaker.getAttributes().get(j);
1145
                    auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
1146
                    if (!fea.getProp(attributes[0]).equals(attributes[1])) {
1147
                            auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
1148
                    }
1149
                }*/
1150
                        addShape(arcc, auxRow);
1151
                } else if (entity.getType()==0x12) {
1152
                        System.out.println("Encuentra un c?rculo dentro de un bloque ...");
1153
                        DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("CENTER");
1154
                        double[] coord = (double[])data.getValue();
1155
                        Point2D center = new Point2D.Double(coord[0], coord[1]);
1156
                        data = entity.getDwgObjectSpecificDataItem("RADIUS");
1157
                        double radius = ((Double)data.getValue()).doubleValue();
1158
                        
1159
                        Point2D pointAux = new Point2D.Double(center.getX() - bPoint[0], center.getY() - bPoint[1]);
1160
                        double laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(Math.toRadians(rot)) + (pointAux.getY()*scale[1])*(-1)*Math.sin(Math.toRadians(rot)));
1161
                        double laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(Math.toRadians(rot)) + (pointAux.getY()*scale[1])*Math.cos(Math.toRadians(rot)));
1162
                        center = new Point2D.Double(laX, laY);
1163
                        // Escala en X = escala en Y ...
1164
                        radius = radius * scale[0];
1165
                        
1166
                        FPolyline2D circle = createCircle(center, radius);
1167
            auxRow[ID_FIELD_ID] = ValueFactory.createValue(0);
1168
            auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
1169
            auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
1170
            auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
1171
                auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
1172
                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
1173
                // Attributes
1174
                /*for (int j=0;j<nAtt;j++) {
1175
                                String[] attributes = new String[2];
1176
                                attributes = (String[])featureMaker.getAttributes().get(j);
1177
                    auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
1178
                    if (!fea.getProp(attributes[0]).equals(attributes[1])) {
1179
                            auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
1180
                    }
1181
                }*/
1182
                        addShape(circle, auxRow);
1183
                } else if (entity.getType()==0x13) {
1184
                        System.out.println("Encuentra una l?nea dentro de un bloque ...");
1185
                        DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("P1");
1186
                        double[] coord = (double[])data.getValue();
1187
                        Point2D p1 = new Point2D.Double(coord[0], coord[1]);
1188
                        data = entity.getDwgObjectSpecificDataItem("P2");
1189
                        coord = (double[])data.getValue();
1190
                        Point2D p2 = new Point2D.Double(coord[0], coord[1]);
1191
                        
1192
                        Point2D pointAux = new Point2D.Double(p1.getX() - bPoint[0], p1.getY() - bPoint[1]);
1193
                        double laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot));
1194
                        double laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot));
1195
                        p1 = new Point2D.Double(laX, laY);
1196
                        pointAux = new Point2D.Double(p2.getX() - bPoint[0], p2.getY() - bPoint[1]);
1197
                        laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot));
1198
                        laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot));
1199
                        p2 = new Point2D.Double(laX, laY);
1200
                        
1201
                        FPolyline2D line = createLine(p1, p2);
1202
            auxRow[ID_FIELD_ID] = ValueFactory.createValue(0);
1203
            auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
1204
            auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
1205
            auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
1206
                auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
1207
                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
1208
                // Attributes
1209
                /*for (int j=0;j<nAtt;j++) {
1210
                                String[] attributes = new String[2];
1211
                                attributes = (String[])featureMaker.getAttributes().get(j);
1212
                    auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
1213
                    if (!fea.getProp(attributes[0]).equals(attributes[1])) {
1214
                            auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
1215
                    }
1216
                }*/
1217
                        addShape(line, auxRow);
1218
                } else if (entity.getType()==0x7) {
1219
                        System.out.println("Encuentra un insert dentro de un bloque ...");
1220
                        DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("INSERTION_POINT");
1221
                        double[] p = (double[])data.getValue();
1222
                        Point2D newInsPoint = new Point2D.Double(p[0], p[1]);
1223
                        data = entity.getDwgObjectSpecificDataItem("SCALE");
1224
                        double[] newScale = (double[])data.getValue();
1225
                        data = entity.getDwgObjectSpecificDataItem("ROTATION");
1226
            double newRot = ((Double)data.getValue()).doubleValue();
1227
                        data = entity.getDwgObjectSpecificDataItem("BLOCK_HEADER_HANDLE");
1228
                        int[] newBlockHandle = (int[])data.getValue();
1229
                        
1230
                        Point2D pointAux = new Point2D.Double(newInsPoint.getX() - bPoint[0], newInsPoint.getY() - bPoint[1]);
1231
                        double laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot));
1232
                        double laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot));
1233
                        newInsPoint = new Point2D.Double(laX, laY);
1234
                        
1235
                        newScale = new double[]{scale[0]*newScale[0], scale[1]*newScale[1], scale[2]*newScale[2]};
1236
                        
1237
                        manageInsert(dwgObjects, newInsPoint, newScale, newRot, newBlockHandle, auxRow);
1238
                        
1239
                        /*auxRow[ID_FIELD_ID] = ValueFactory.createValue(0);
1240
            auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
1241
            auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
1242
            auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
1243
                auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
1244
                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);*/
1245
                // Attributes
1246
                /*for (int j=0;j<nAtt;j++) {
1247
                                String[] attributes = new String[2];
1248
                                attributes = (String[])featureMaker.getAttributes().get(j);
1249
                    auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
1250
                    if (!fea.getProp(attributes[0]).equals(attributes[1])) {
1251
                            auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
1252
                    }
1253
                }*/
1254
                /*FPoint2D fPoint = createPoint(newInsPoint);
1255
                        addShape(fPoint, auxRow);*/
1256
                } else if (entity.getType()==0x0F) {
1257
                        System.out.println("Encuentra una polil?nea dentro de un bloque ...");
1258
                        DwgObjectSpecificDataItem data = entity.getDwgObjectSpecificDataItem("FLAGS");
1259
                        int flags = ((Integer)data.getValue()).intValue();
1260
                        data = entity.getDwgObjectSpecificDataItem("FIRST_VERTEX_HANDLE");
1261
                        int[] firstVertexHandle = (int[])data.getValue();
1262
                        byte[] firstHandleBytes = new byte[]{0,0,0,0};
1263
                        if (firstVertexHandle.length>2) firstHandleBytes[3] = (byte)firstVertexHandle[2];
1264
                        if (firstVertexHandle.length>3) firstHandleBytes[2] = (byte)firstVertexHandle[3];
1265
                        if (firstVertexHandle.length>4) firstHandleBytes[1] = (byte)firstVertexHandle[4];
1266
                        if (firstVertexHandle.length>5) firstHandleBytes[0] = (byte)firstVertexHandle[5];
1267
                        int firstHandle = ByteUtils.bytesToInt(firstHandleBytes, new int[]{0});
1268
                        data = entity.getDwgObjectSpecificDataItem("LAST_VERTEX_HANDLE");
1269
                        int[] lastVertexHandle = (int[])data.getValue();
1270
                        byte[] lastHandleBytes = new byte[]{0,0,0,0};
1271
                        if (lastVertexHandle.length>2) lastHandleBytes[3] = (byte)lastVertexHandle[2];
1272
                        if (lastVertexHandle.length>3) lastHandleBytes[2] = (byte)lastVertexHandle[3];
1273
                        if (lastVertexHandle.length>4) lastHandleBytes[1] = (byte)lastVertexHandle[4];
1274
                        if (lastVertexHandle.length>5) lastHandleBytes[0] = (byte)lastVertexHandle[5];
1275
                        int lastHandle = ByteUtils.bytesToInt(lastHandleBytes, new int[]{0});
1276
                        Vector pts = new Vector();
1277
                        Vector bulges = new Vector();
1278
                        double[] pt = new double[2];
1279
                        
1280
                        for (int j=0;j<dwgObjects.size();j++) {
1281
                                DwgObject obj = (DwgObject)dwgObjects.get(j);
1282
                                Vector handle = obj.getHandle();
1283
                                byte[] handleBytes = new byte[]{0,0,0,0};
1284
                                if (handle.size()>2) handleBytes[3] = ((Integer)handle.get(2)).byteValue();
1285
                                if (handle.size()>3) handleBytes[2] = ((Integer)handle.get(3)).byteValue();
1286
                                if (handle.size()>4) handleBytes[1] = ((Integer)handle.get(4)).byteValue();
1287
                                if (handle.size()>5) handleBytes[0] = ((Integer)handle.get(5)).byteValue();
1288
                                int objHandle = ByteUtils.bytesToInt(handleBytes, new int[]{0});
1289
                                if (objHandle==firstHandle && ((Integer)handle.get(1)).intValue()==firstVertexHandle[1]) {
1290
                                        int k=0;
1291
                                        while (true) {
1292
                                                DwgObject vertex = (DwgObject)dwgObjects.get(j+k);
1293
                                                Vector vertexHandle = vertex.getHandle();
1294
                                                byte[] vHandleBytes = new byte[]{0,0,0,0};
1295
                                                if (vertexHandle.size()>2) vHandleBytes[3] = ((Integer)vertexHandle.get(2)).byteValue();
1296
                                                if (vertexHandle.size()>3) vHandleBytes[2] = ((Integer)vertexHandle.get(3)).byteValue();
1297
                                                if (vertexHandle.size()>4) vHandleBytes[1] = ((Integer)vertexHandle.get(4)).byteValue();
1298
                                                if (vertexHandle.size()>5) vHandleBytes[0] = ((Integer)vertexHandle.get(5)).byteValue();
1299
                                                int vHandle = ByteUtils.bytesToInt(vHandleBytes, new int[]{0});
1300
                                                data = vertex.getDwgObjectSpecificDataItem("POINT");
1301
                                                pt = (double[])data.getValue();
1302
                                                pts.add(new Point2D.Double(pt[0], pt[1]));
1303
                                                data = vertex.getDwgObjectSpecificDataItem("BULGE");
1304
                                                double bulge = ((Double)data.getValue()).doubleValue();
1305
                                                bulges.add(new Double(bulge));
1306
                                                k++;
1307
                                                if (vHandle==lastHandle && ((Integer)vertexHandle.get(1)).intValue()==lastVertexHandle[1]) {
1308
                                                        break;
1309
                                                }
1310
                                        }
1311
                                }
1312
                        }
1313
                        
1314
                        Point2D[] newPts = new Point2D[pts.size()];
1315
                        if (flags==0 || flags==2 || flags==4 || flags==8 || flags==16 || flags==32 || flags==64 || flags==128) {
1316
                                for (int j=0;j<pts.size();j++) {
1317
                                        newPts[j] = (Point2D)pts.get(j);
1318
                                }
1319
                        } else if (flags==1 || flags==3 || flags==5 || flags==9 || flags==17 || flags==33 || flags==65 || flags==129) {
1320
                                newPts = new Point2D[pts.size()+1];
1321
                                for (int j=0;j<pts.size();j++) {
1322
                                        newPts[j] = (Point2D)pts.get(j);
1323
                                }
1324
                                newPts[pts.size()] = (Point2D)pts.get(0);
1325
                                bulges.add(new Double(0));
1326
                        }
1327
                        
1328
                        for (int i=0;i<newPts.length;i++) {
1329
                                Point2D pointAux = new Point2D.Double(newPts[i].getX() - bPoint[0], newPts[i].getY() - bPoint[1]);
1330
                                double laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot));
1331
                                double laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot));
1332
                                newPts[i] = new Point2D.Double(laX, laY);
1333
                        }
1334
                        
1335
                        FPolyline2D pline = createPolyline2D(newPts, bulges);
1336
            auxRow[ID_FIELD_ID] = ValueFactory.createValue(0);
1337
            auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String("dxfEntity"));
1338
            auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String("layer"));
1339
            auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(0);
1340
                auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(0.0);
1341
                auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(0.0);
1342
                // Attributes
1343
                /*for (int j=0;j<nAtt;j++) {
1344
                                String[] attributes = new String[2];
1345
                                attributes = (String[])featureMaker.getAttributes().get(j);
1346
                    auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
1347
                    if (!fea.getProp(attributes[0]).equals(attributes[1])) {
1348
                            auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
1349
                    }
1350
                }*/
1351
                        addShape(pline, auxRow);
1352
                }
1353
        }
1354
        
1355
        /**
1356
         * LayerTable podr?a ser un objeto y LayerTableRecord tambi?n ...
1357
         * @param dwgObjects
1358
         * @return
1359
         */
1360
        private Vector createLayerTable(Vector dwgObjects) {
1361
                Vector layerTable = new Vector();
1362
                for (int i=0;i<dwgObjects.size();i++) {
1363
                        DwgObject obj = (DwgObject)dwgObjects.get(i);
1364
                        if (obj.getType()==0x33) {
1365
                                //System.out.println("Encuentra un objeto layer ...");
1366
                                Vector layerTableRecord = new Vector();
1367
                                layerTableRecord.add(obj.getHandle());
1368
                                layerTableRecord.add(((String)obj.getDwgObjectSpecificDataItem("NAME").getValue()));
1369
                                //System.out.println("((Integer)obj.getDwgObjectSpecificDataItem(COLOR).getValue()).intValue() = " + ((Integer)obj.getDwgObjectSpecificDataItem("COLOR").getValue()).intValue());
1370
                                layerTableRecord.add(((Integer)obj.getDwgObjectSpecificDataItem("COLOR").getValue()));
1371
                                //layerTableRecord.add(obj.getDwgObjectSpecificDataItem("LAYER_CONTROL_HANDLE").getValue());
1372
                                layerTable.add(layerTableRecord);
1373
                        }
1374
                }
1375
                return layerTable;
1376
        }
1377
        
1378
        //private String setLayerName(int[] layerHandle, Vector layerTable) {
1379
        private String getLayerName(DwgObject entity, Vector layerTable) {
1380
                String layerName = "";
1381
                int[] layerHandle = (int[])entity.getLayer();
1382
                System.out.println("layerHandle[0] = " + layerHandle[0]);
1383
                if (layerHandle.length>1) System.out.println("layerHandle[1] = " + layerHandle[1]);
1384
                if (layerHandle.length>2) System.out.println("layerHandle[2] = " + layerHandle[2]);
1385
                if (layerHandle.length>3) System.out.println("layerHandle[3] = " + layerHandle[3]);
1386
                byte[] layerBytes = new byte[]{0,0,0,0};
1387
                if (layerHandle.length>2) layerBytes[3] = (byte)layerHandle[2];
1388
                if (layerHandle.length>3) layerBytes[2] = (byte)layerHandle[3];
1389
                if (layerHandle.length>4) layerBytes[1] = (byte)layerHandle[4];
1390
                if (layerHandle.length>5) layerBytes[0] = (byte)layerHandle[5];
1391
                int layer = ByteUtils.bytesToInt(layerBytes, new int[]{0});
1392
                for (int j=0;j<layerTable.size();j++) {
1393
                        Vector layerTableRecord = (Vector)layerTable.get(j);
1394
                        Vector layerHandleVect = (Vector)layerTableRecord.get(0);
1395
                        System.out.println("layerHandleVect.get(0) = " + layerHandleVect.get(0));
1396
                        if (layerHandleVect.size()>1) System.out.println("layerHandleVect.get(1) = " + layerHandleVect.get(1));
1397
                        if (layerHandleVect.size()>2) System.out.println("layerHandleVect.get(2) = " + layerHandleVect.get(2));
1398
                        if (layerHandleVect.size()>3) System.out.println("layerHandleVect.get(3) = " + layerHandleVect.get(3));
1399
                        byte[] layerHandleBytes = new byte[]{0,0,0,0};
1400
                        if (layerHandleVect.size()>2) layerHandleBytes[3] = (byte)((Integer)layerHandleVect.get(2)).intValue();
1401
                        if (layerHandleVect.size()>3) layerHandleBytes[2] = (byte)((Integer)layerHandleVect.get(3)).intValue();
1402
                        if (layerHandleVect.size()>4) layerHandleBytes[1] = (byte)((Integer)layerHandleVect.get(4)).intValue();
1403
                        if (layerHandleVect.size()>5) layerHandleBytes[0] = (byte)((Integer)layerHandleVect.get(5)).intValue();
1404
                        int lHandle = ByteUtils.bytesToInt(layerHandleBytes, new int[]{0});
1405
                        /*int[] layerControlHandle = (int[])layerTableRecord.get(3);
1406
                        byte[] layerControlBytes = new byte[]{0,0,0,0};
1407
                        if (layerControlHandle.length>2) layerControlBytes[3] = (byte)layerControlHandle[2];
1408
                        if (layerControlHandle.length>3) layerControlBytes[2] = (byte)layerControlHandle[3];
1409
                        if (layerControlHandle.length>4) layerControlBytes[1] = (byte)layerControlHandle[4];
1410
                        if (layerControlHandle.length>5) layerControlBytes[0] = (byte)layerControlHandle[5];
1411
                        int layerControl = ByteUtils.bytesToInt(layerControlBytes, new int[]{0});*/
1412
                        System.out.println("lHandle = " + lHandle);
1413
                        System.out.println("layer = " + layer);
1414
                        if (lHandle==layer) {
1415
                                layerName = (String)layerTableRecord.get(1);
1416
                                System.out.println("layerName = " + layerName);
1417
                        }
1418
                }
1419
                return layerName;
1420
        }
1421
        
1422
        private int setColorByLayer(int[] layerHandle, Vector layerTable) {
1423
                int colorByLayer = 0;
1424
                byte[] layerBytes = new byte[]{0,0,0,0};
1425
                if (layerHandle.length>2) layerBytes[3] = (byte)layerHandle[2];
1426
                if (layerHandle.length>3) layerBytes[2] = (byte)layerHandle[3];
1427
                if (layerHandle.length>4) layerBytes[1] = (byte)layerHandle[4];
1428
                if (layerHandle.length>5) layerBytes[0] = (byte)layerHandle[5];
1429
                int layer = ByteUtils.bytesToInt(layerBytes, new int[]{0});
1430
                for (int j=0;j<layerTable.size();j++) {
1431
                        Vector layerTableRecord = (Vector)layerTable.get(j);
1432
                        Vector layerHandleVect = (Vector)layerTableRecord.get(0);
1433
                        byte[] layerHandleBytes = new byte[]{0,0,0,0};
1434
                        if (layerHandleVect.size()>2) layerHandleBytes[3] = (byte)((Integer)layerHandleVect.get(2)).intValue();
1435
                        if (layerHandleVect.size()>3) layerHandleBytes[2] = (byte)((Integer)layerHandleVect.get(3)).intValue();
1436
                        if (layerHandleVect.size()>4) layerHandleBytes[1] = (byte)((Integer)layerHandleVect.get(4)).intValue();
1437
                        if (layerHandleVect.size()>5) layerHandleBytes[0] = (byte)((Integer)layerHandleVect.get(5)).intValue();
1438
                        int lHandle = ByteUtils.bytesToInt(layerHandleBytes, new int[]{0});
1439
                        //System.out.println("layerHandle = " + layerHandle);
1440
                        //System.out.println("layer = " + layer);
1441
                        if (lHandle==layer) {
1442
                                colorByLayer = ((Integer)layerTableRecord.get(2)).intValue();
1443
                                //System.out.println("colorByLayer = " + colorByLayerObj.intValue());
1444
                        }
1445
                }
1446
                return colorByLayer;
1447
        }
1448
        
1449
        /* (non-Javadoc)
1450
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#accept(java.io.File)
1451
         */
1452
        public boolean accept(File f) {
1453
                return f.getName().toUpperCase().endsWith("DWG");
1454
        }
1455

    
1456
        /* (non-Javadoc)
1457
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getShapeType()
1458
         */
1459
        public int getShapeType() {
1460
                return FShape.MULTI;
1461
        }
1462

    
1463
        /* (non-Javadoc)
1464
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getName()
1465
         */
1466
        public String getName() {
1467
                return "gvSIG DWG Memory Driver";
1468
        }
1469

    
1470
        /* (non-Javadoc)
1471
         * @see com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend#getDefaultLegend()
1472
         */
1473
        public Legend getDefaultLegend() {
1474
                return defaultLegend;
1475
        }
1476

    
1477
    /* (non-Javadoc)
1478
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
1479
     */
1480
    public DriverAttributes getDriverAttributes() {
1481
        return attr;
1482
    }
1483

    
1484
}