Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libDwg / src / com / iver / cit / jdwglib / dwg / readers / objreaders / v2004 / DwgLwPolylineReader2004.java @ 23641

History | View | Annotate | Download (6.9 KB)

1
/*
2
 * Created on 25-ene-2007 by azabala
3
 *
4
 */
5
package com.iver.cit.jdwglib.dwg.readers.objreaders.v2004;
6

    
7
import java.awt.geom.Point2D;
8
import java.util.ArrayList;
9
import java.util.List;
10

    
11
import com.iver.cit.jdwglib.dwg.CorruptedDwgEntityException;
12
import com.iver.cit.jdwglib.dwg.DwgHandleReference;
13
import com.iver.cit.jdwglib.dwg.DwgObject;
14
import com.iver.cit.jdwglib.dwg.DwgUtil;
15
import com.iver.cit.jdwglib.dwg.objects.DwgLwPolyline;
16

    
17
/**
18
 * @author alzabord
19
 *
20
 * TODO To change the template for this generated type comment go to Window -
21
 * Preferences - Java - Code Style - Code Templates
22
 */
23
public class DwgLwPolylineReader2004 extends AbstractDwg2004Reader {
24

    
25
        /*
26
         *TODO El metodo original de J.Morell daba excepciones (que eran
27
         *ocultadas con un catch(Exception){}...BUF!
28
         *
29
         * Este metodo trata de copiar literalmente el de Python, para ver
30
         * si somos capaces de resolver el bug.
31
         *
32
         * */
33
        public void readSpecificObj(int[] data, int offset, DwgObject dwgObj) throws RuntimeException, CorruptedDwgEntityException {
34

    
35
                        if (!(dwgObj instanceof DwgLwPolyline))
36
                                throw new RuntimeException(
37
                                                "ArcReader 2004 solo puede leer DwgLwPolyLine");
38
                        DwgLwPolyline line = (DwgLwPolyline) dwgObj;
39
                        int bitPos = offset;
40
                        ArrayList val;
41

    
42

    
43

    
44
                        bitPos = readObjectHeader(data, bitPos, dwgObj);
45

    
46
                        val = DwgUtil.getBitShort(data, bitPos);
47
                        bitPos = ((Integer) val.get(0)).intValue();
48
                        int flag = ((Integer) val.get(1)).intValue();
49
                        line.setFlag(flag);
50

    
51
                        /*
52
                         * Flag son 4 bits que indican si la lwpline tiene ancho,
53
                         * (3er bit), elevacion (4o bit), grosor (2? bit) y vector
54
                         * de extrusion (1er bit).
55
                         * 5? bit -> n? de bulges
56
                         * 60 bit -> n? de widths
57
                         *
58
                         * ahora se verifican y se van leyendo
59
                         * */
60

    
61

    
62
                        double constWidth = 0d;
63
                    if( (flag & 0x4) > 0){
64
                            val = DwgUtil.getBitDouble(data, bitPos);
65
                                bitPos = ((Integer) val.get(0)).intValue();
66
                                constWidth = ((Double) val.get(1)).doubleValue();
67
                    }//if
68
                    line.setConstWidth(constWidth);
69

    
70
                    double elev = 0d;
71
                    if ( (flag & 0x8) > 0){
72
                            val = DwgUtil.getBitDouble(data, bitPos);
73
                            bitPos = ((Integer) val.get(0)).intValue();
74
                            elev = ((Double) val.get(1)).doubleValue();
75
                    }
76
                    line.setElevation(elev);
77

    
78
                    double thickness = 0d;
79
                    if ( (flag & 0x2) > 0){
80
                            val = DwgUtil.getBitDouble(data, bitPos);
81
                            bitPos = ((Integer) val.get(0)).intValue();
82
                            thickness = ((Double) val.get(1)).doubleValue();
83
                    }
84
                    line.setThickness(thickness);
85

    
86
                    double nx = 0d;
87
                    double ny = 0d;
88
                    double nz = 0d;
89
                    if( (flag & 0x1) > 0){
90
                            val = DwgUtil.getBitDouble(data, bitPos);
91
                            bitPos = ((Integer) val.get(0)).intValue();
92
                            nx = ((Double) val.get(1)).doubleValue();
93

    
94
                            val = DwgUtil.getBitDouble(data, bitPos);
95
                            bitPos = ((Integer) val.get(0)).intValue();
96
                            ny = ((Double) val.get(1)).doubleValue();
97

    
98
                            val = DwgUtil.getBitDouble(data, bitPos);
99
                            bitPos = ((Integer) val.get(0)).intValue();
100
                            nz = ((Double) val.get(1)).doubleValue();
101
                    }
102
                    line.setNormal(new double[]{nx, ny, nz});
103

    
104
                    val = DwgUtil.getBitLong(data, bitPos);
105
                    bitPos = ((Integer) val.get(0)).intValue();
106
                    int numberOfPoints = ((Integer) val.get(1)).intValue();
107

    
108
                    int numberOfBulges = 0;
109
                    if ((flag & 0x10) > 0){
110
                            val = DwgUtil.getBitLong(data, bitPos);
111
                             bitPos = ((Integer) val.get(0)).intValue();
112
                             numberOfBulges = ((Integer) val.get(1)).intValue();
113
                    }
114

    
115
                    int numberOfWidths = 0;
116
                    if ((flag & 0x20) > 0){
117
                            val = DwgUtil.getBitLong(data, bitPos);
118
                             bitPos = ((Integer) val.get(0)).intValue();
119
                             numberOfWidths = ((Integer) val.get(1)).intValue();
120
                    }
121

    
122
                    if(numberOfPoints > 0){
123
                            //Esto es una chapuza, pero las LwPolylines no se est?n
124
                            //leyendo bien y en ocasiones nos llegan entidades
125
                            //con MILLONES de puntos (OutOfMemoryException)
126
                            //Plantear en la lista de PythonCAD
127
                            if(numberOfPoints > 10000)
128
                                    throw new CorruptedDwgEntityException("LwPolyline corrupta");
129
                            List vertices = new ArrayList();
130
                              val = DwgUtil.getRawDouble(data, bitPos);
131
                              bitPos = ((Integer) val.get(0)).intValue();
132
                                  double x0 = ((Double) val.get(1)).doubleValue();
133

    
134
                                  val = DwgUtil.getRawDouble(data, bitPos);
135
                                  bitPos = ((Integer) val.get(0)).intValue();
136
                                  double y0 = ((Double) val.get(1)).doubleValue();
137

    
138
                                  vertices.add(new double[]{x0, y0});
139

    
140
                                  for (int i = 1; i < numberOfPoints; i++) {
141
                                          val = DwgUtil.getDefaultDouble(data, bitPos, x0);
142
                                          bitPos = ((Integer) val.get(0)).intValue();
143
                                          double x = ((Double) val.get(1)).doubleValue();
144

    
145
                                          val = DwgUtil.getDefaultDouble(data, bitPos, y0);
146
                                          bitPos = ((Integer) val.get(0)).intValue();
147
                                          double y = ((Double) val.get(1)).doubleValue();
148

    
149
                                          vertices.add(new double[]{x, y});
150

    
151
                                          x0 = x;//se proporcionan como valores por defecto las coordenadas del pto anterior
152
                                          y0 = y;
153
                                  }
154
                                  line.setVertices(vertices);
155
                    }else{
156
                            System.out.println("vertices == 0: lanzamos una excepcion??");
157
                    }
158

    
159

    
160

    
161
                        if(numberOfBulges > 0){
162
                                if (numberOfBulges != numberOfPoints){
163
                                    System.out.println("bulges != vertices: lanzamos una excepcion??");
164
                            }
165
                                double[] bulges = new double[numberOfBulges];
166
                                for(int i = 0; i < numberOfBulges; i++){
167
                                        //TODO OJOOOOOO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
168
                                        //En PythonCAD se hacia getRawDouble, pero en la especificacion
169
                                        //(pagina 136) dice que es BitDouble (BD)
170
                                        val = DwgUtil.getBitDouble(data, bitPos);
171
                                        bitPos = ((Integer) val.get(0)).intValue();
172
                                        bulges[i] = ((Double) val.get(1)).doubleValue();
173
                                }
174
                                line.setBulges(bulges);
175
                        }else{
176
                                double[] bulges = new double[numberOfPoints];
177
                                for(int i = 0; i < numberOfPoints; i++)
178
                                        bulges[i] = 0d;
179
                                line.setBulges(bulges);
180
                        }
181

    
182
                        if(numberOfWidths > 0){
183
                            if (numberOfWidths != numberOfPoints){
184
                                    System.out.println("widths != vertices: lanzamos una excepcion??");
185
                            }
186
                                double[][] widths = new double[numberOfWidths][2];
187
                                for (int i = 0; i < numberOfWidths; i++) {
188
                                        val = DwgUtil.getBitDouble(data, bitPos);
189
                                        bitPos = ((Integer) val.get(0)).intValue();
190
                                        double sw = ((Double) val.get(1)).doubleValue();
191

    
192
                                        val = DwgUtil.getBitDouble(data, bitPos);
193
                                        bitPos = ((Integer) val.get(0)).intValue();
194
                                        double ew = ((Double) val.get(1)).doubleValue();
195
                                        widths[i][0] = sw;
196
                                        widths[i][1] = ew;
197
                                }
198
                                line.setWidths(widths);
199
                        }else{
200
                                line.setWidths(new double[0][0]);
201
                        }
202

    
203
                        line.inserta();
204

    
205

    
206
                        /*
207
                         * azabala: de siempre se producen excepciones al tratar de leer
208
                         * LwPolyline (si no se veian es pq se capturaban y no se sacaba
209
                         * traza)
210
                         *
211
                         * Se producen siempre que numVertices == 0 ????
212
                         * Probar a saltarmelos.
213
                         *
214
                         *
215
                         *
216
                         * TODO Ver por qu? aparecen (y lo peor, pq una vez se da una se propagan
217
                         * todas)
218
                         *
219
                         * */
220
                                bitPos = readObjectTailer(data, bitPos, line);
221
        }
222
}