svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libDwg / src / org / gvsig / dwg / lib / readers / v15 / DwgLwPolylineReader15.java @ 28969
History | View | Annotate | Download (7.65 KB)
1 |
/*
|
---|---|
2 |
* Created on 25-ene-2007 by azabala
|
3 |
*
|
4 |
*/
|
5 |
package org.gvsig.dwg.lib.readers.v15; |
6 |
|
7 |
import java.awt.geom.Point2D; |
8 |
import java.util.ArrayList; |
9 |
import java.util.List; |
10 |
|
11 |
import org.gvsig.dwg.lib.CorruptedDwgEntityException; |
12 |
import org.gvsig.dwg.lib.DwgObject; |
13 |
import org.gvsig.dwg.lib.DwgUtil; |
14 |
import org.gvsig.dwg.lib.objects.DwgLwPolyline; |
15 |
|
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 DwgLwPolylineReader15 extends AbstractDwg15Reader { |
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 15 solo puede leer DwgLwPolyLine");
|
38 |
DwgLwPolyline line = (DwgLwPolyline) dwgObj; |
39 |
List val = null; |
40 |
int bitPos = offset;
|
41 |
bitPos = headTailReader.readObjectHeader(data, bitPos, dwgObj); |
42 |
val = DwgUtil.getBitShort(data, bitPos); |
43 |
bitPos = ((Integer) val.get(0)).intValue(); |
44 |
int flag = ((Integer) val.get(1)).intValue(); |
45 |
line.setFlag(flag); |
46 |
|
47 |
/*
|
48 |
* Flag son 4 bits que indican si la lwpline tiene ancho,
|
49 |
* (3er bit), elevacion (4o bit), grosor (2? bit) y vector
|
50 |
* de extrusion (1er bit).
|
51 |
* 5? bit -> n? de bulges
|
52 |
* 60 bit -> n? de widths
|
53 |
*
|
54 |
* ahora se verifican y se van leyendo
|
55 |
* */
|
56 |
|
57 |
|
58 |
double constWidth = 0d; |
59 |
if( (flag & 0x4) > 0){ |
60 |
val = DwgUtil.getBitDouble(data, bitPos); |
61 |
bitPos = ((Integer) val.get(0)).intValue(); |
62 |
constWidth = ((Double) val.get(1)).doubleValue(); |
63 |
}//if
|
64 |
line.setConstWidth(constWidth); |
65 |
|
66 |
double elev = 0d; |
67 |
if ( (flag & 0x8) > 0){ |
68 |
val = DwgUtil.getBitDouble(data, bitPos); |
69 |
bitPos = ((Integer) val.get(0)).intValue(); |
70 |
elev = ((Double) val.get(1)).doubleValue(); |
71 |
} |
72 |
line.setElevation(elev); |
73 |
|
74 |
double thickness = 0d; |
75 |
if ( (flag & 0x2) > 0){ |
76 |
val = DwgUtil.getBitDouble(data, bitPos); |
77 |
bitPos = ((Integer) val.get(0)).intValue(); |
78 |
thickness = ((Double) val.get(1)).doubleValue(); |
79 |
} |
80 |
line.setThickness(thickness); |
81 |
|
82 |
double nx = 0d; |
83 |
double ny = 0d; |
84 |
double nz = 0d; |
85 |
if( (flag & 0x1) > 0){ |
86 |
val = DwgUtil.getBitDouble(data, bitPos); |
87 |
bitPos = ((Integer) val.get(0)).intValue(); |
88 |
nx = ((Double) val.get(1)).doubleValue(); |
89 |
|
90 |
val = DwgUtil.getBitDouble(data, bitPos); |
91 |
bitPos = ((Integer) val.get(0)).intValue(); |
92 |
ny = ((Double) val.get(1)).doubleValue(); |
93 |
|
94 |
val = DwgUtil.getBitDouble(data, bitPos); |
95 |
bitPos = ((Integer) val.get(0)).intValue(); |
96 |
nz = ((Double) val.get(1)).doubleValue(); |
97 |
} |
98 |
line.setNormal(new double[]{nx, ny, nz}); |
99 |
|
100 |
val = DwgUtil.getBitLong(data, bitPos); |
101 |
bitPos = ((Integer) val.get(0)).intValue(); |
102 |
int numberOfPoints = ((Integer) val.get(1)).intValue(); |
103 |
|
104 |
int numberOfBulges = 0; |
105 |
if ((flag & 0x10) > 0){ |
106 |
val = DwgUtil.getBitLong(data, bitPos); |
107 |
bitPos = ((Integer) val.get(0)).intValue(); |
108 |
numberOfBulges = ((Integer) val.get(1)).intValue(); |
109 |
} |
110 |
|
111 |
int numberOfWidths = 0; |
112 |
if ((flag & 0x20) > 0){ |
113 |
val = DwgUtil.getBitLong(data, bitPos); |
114 |
bitPos = ((Integer) val.get(0)).intValue(); |
115 |
numberOfWidths = ((Integer) val.get(1)).intValue(); |
116 |
} |
117 |
|
118 |
if(numberOfPoints > 0){ |
119 |
//Esto es una chapuza, pero las LwPolylines no se est?n
|
120 |
//leyendo bien y en ocasiones nos llegan entidades
|
121 |
//con MILLONES de puntos (OutOfMemoryException)
|
122 |
//Plantear en la lista de PythonCAD
|
123 |
if(numberOfPoints > 10000) |
124 |
throw new CorruptedDwgEntityException("LwPolyline corrupta"); |
125 |
List vertices = new ArrayList(); |
126 |
val = DwgUtil.getRawDouble(data, bitPos); |
127 |
bitPos = ((Integer) val.get(0)).intValue(); |
128 |
double x0 = ((Double) val.get(1)).doubleValue(); |
129 |
|
130 |
val = DwgUtil.getRawDouble(data, bitPos); |
131 |
bitPos = ((Integer) val.get(0)).intValue(); |
132 |
double y0 = ((Double) val.get(1)).doubleValue(); |
133 |
|
134 |
vertices.add(new double[]{x0, y0}); |
135 |
|
136 |
|
137 |
/*
|
138 |
* TODO azabala
|
139 |
* Algunos metodos de DwgUtil lanzan excepciones inexperadas
|
140 |
* cuando trabajan para rellenar clases de LwPolyline:
|
141 |
* ->getDefaultDouble
|
142 |
* ->getBits
|
143 |
* etc
|
144 |
*
|
145 |
* Estas excepciones son del tipo OutOfBounds (se intenta leer un bit
|
146 |
* cuyo orden excede de la capacidad del array)
|
147 |
* La especificaci?n 15 de DWG es seguida al pie de la letra por Pythoncad,
|
148 |
* (y por nosotros) as? que puede ser que el problema est? en los metodos
|
149 |
* de DwgUtil.
|
150 |
*
|
151 |
* Intentar capturar la excepcion para que no se pierdan las entidades que las
|
152 |
* provocan, y luego solucionar el error
|
153 |
*
|
154 |
*
|
155 |
* */
|
156 |
for (int i = 1; i < numberOfPoints; i++) { |
157 |
val = DwgUtil.getDefaultDouble(data, bitPos, x0); |
158 |
bitPos = ((Integer) val.get(0)).intValue(); |
159 |
double x = ((Double) val.get(1)).doubleValue(); |
160 |
|
161 |
val = DwgUtil.getDefaultDouble(data, bitPos, y0); |
162 |
bitPos = ((Integer) val.get(0)).intValue(); |
163 |
double y = ((Double) val.get(1)).doubleValue(); |
164 |
|
165 |
vertices.add(new double[]{x, y}); |
166 |
|
167 |
x0 = x;//se proporcionan como valores por defecto las coordenadas del pto anterior
|
168 |
y0 = y; |
169 |
} |
170 |
line.setVertices(vertices); |
171 |
}else{
|
172 |
System.out.println("vertices == 0: lanzamos una excepcion??"); |
173 |
} |
174 |
|
175 |
|
176 |
|
177 |
if(numberOfBulges > 0){ |
178 |
if (numberOfBulges != numberOfPoints){
|
179 |
System.out.println("bulges != vertices: lanzamos una excepcion??"); |
180 |
} |
181 |
double[] bulges = new double[numberOfBulges]; |
182 |
for(int i = 0; i < numberOfBulges; i++){ |
183 |
//TODO OJOOOOOO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
184 |
//En PythonCAD se hacia getRawDouble, pero en la especificacion
|
185 |
//(pagina 136) dice que es BitDouble (BD)
|
186 |
val = DwgUtil.getBitDouble(data, bitPos); |
187 |
bitPos = ((Integer) val.get(0)).intValue(); |
188 |
bulges[i] = ((Double) val.get(1)).doubleValue(); |
189 |
} |
190 |
line.setBulges(bulges); |
191 |
}else{
|
192 |
double[] bulges = new double[numberOfPoints]; |
193 |
for(int i = 0; i < numberOfPoints; i++) |
194 |
bulges[i] = 0d;
|
195 |
line.setBulges(bulges); |
196 |
} |
197 |
|
198 |
if(numberOfWidths > 0){ |
199 |
if (numberOfWidths != numberOfPoints){
|
200 |
System.out.println("widths != vertices: lanzamos una excepcion??"); |
201 |
} |
202 |
double[][] widths = new double[numberOfWidths][2]; |
203 |
for (int i = 0; i < numberOfWidths; i++) { |
204 |
val = DwgUtil.getBitDouble(data, bitPos); |
205 |
bitPos = ((Integer) val.get(0)).intValue(); |
206 |
double sw = ((Double) val.get(1)).doubleValue(); |
207 |
|
208 |
val = DwgUtil.getBitDouble(data, bitPos); |
209 |
bitPos = ((Integer) val.get(0)).intValue(); |
210 |
double ew = ((Double) val.get(1)).doubleValue(); |
211 |
widths[i][0] = sw;
|
212 |
widths[i][1] = ew;
|
213 |
} |
214 |
line.setWidths(widths); |
215 |
}else{
|
216 |
line.setWidths(new double[0][0]); |
217 |
} |
218 |
|
219 |
|
220 |
|
221 |
/*
|
222 |
* azabala: de siempre se producen excepciones al tratar de leer
|
223 |
* LwPolyline (si no se veian es pq se capturaban y no se sacaba
|
224 |
* traza)
|
225 |
*
|
226 |
* Se producen siempre que numVertices == 0 ????
|
227 |
* Probar a saltarmelos.
|
228 |
*
|
229 |
*
|
230 |
*
|
231 |
* TODO Ver por qu? aparecen (y lo peor, pq una vez se da una se propagan
|
232 |
* todas)
|
233 |
*
|
234 |
* */
|
235 |
bitPos = headTailReader.readObjectTailer(data, bitPos, line); |
236 |
} |
237 |
} |