Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libDwg / src / com / iver / cit / jdwglib / dwg / objects / DwgLwPolyline.java @ 9830

History | View | Annotate | Download (12.8 KB)

1
/* jdwglib. Java Library for reading Dwg files.
2
 * 
3
 * Author: Jose Morell Rama (jose.morell@gmail.com).
4
 * Port from the Pythoncad Dwg library by Art Haas.
5
 *
6
 * Copyright (C) 2005 Jose Morell, IVER TI S.A. 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
 * Jose Morell (jose.morell@gmail.com)
25
 * 
26
 * or
27
 *
28
 * IVER TI S.A.
29
 *  C/Salamanca, 50
30
 *  46005 Valencia
31
 *  Spain
32
 *  +34 963163400
33
 *  dac@iver.es
34
 */
35
package com.iver.cit.jdwglib.dwg.objects;
36

    
37
import java.awt.geom.Point2D;
38
import java.util.ArrayList;
39
import java.util.List;
40

    
41
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
42
import com.iver.cit.gvsig.fmap.core.FShape;
43
import com.iver.cit.jdwglib.dwg.DwgObject;
44
import com.iver.cit.jdwglib.dwg.DwgUtil;
45
import com.iver.cit.jdwglib.dwg.IDwg2FMap;
46
import com.iver.cit.jdwglib.dwg.IDwg3DTestable;
47
import com.iver.cit.jdwglib.dwg.IDwgExtrusionable;
48
import com.iver.cit.jdwglib.dwg.IDwgPolyline;
49
import com.iver.cit.jdwglib.util.AcadExtrusionCalculator;
50
import com.iver.cit.jdwglib.util.FMapUtil;
51
import com.iver.cit.jdwglib.util.GisModelCurveCalculator;
52

    
53
/**
54
 * The DwgLwPolyline class represents a DWG LwPolyline
55
 * 
56
 * @author jmorell
57
 */
58
public class DwgLwPolyline extends DwgObject 
59
        implements IDwgPolyline, IDwgExtrusionable, 
60
        IDwg3DTestable, IDwg2FMap{
61
        public DwgLwPolyline(int index) {
62
                super(index);
63
                // TODO Auto-generated constructor stub
64
        }
65
        private int flag;
66
        private double constWidth;
67
        private double elevation;
68
        private double thickness;
69
        private double[] normal;
70
        private Point2D[] vertices;
71
        private double[] bulges;
72
        private double[][] widths;
73
        
74
        /**
75
         * Read a LwPolyline in the DWG format Version 15
76
         * 
77
         * @param data Array of unsigned bytes obtained from the DWG binary file
78
         * @param offset The current bit offset where the value begins
79
         * @throws Exception If an unexpected bit value is found in the DWG file. Occurs
80
         *                    when we are looking for LwPolylines.
81
         */
82
        public void readDwgLwPolylineV15(int[] data, int offset) throws Exception {
83
                //System.out.println("DwgLwPolyline.readDwgLwPolyline() executed ...");
84
                int bitPos = offset;
85
                bitPos = readObjectHeaderV15(data, bitPos);
86
                ArrayList v = DwgUtil.getBitShort(data, bitPos);
87
                bitPos = ((Integer)v.get(0)).intValue();
88
                int flag = ((Integer)v.get(1)).intValue();
89
                this.flag = flag;
90
                // Condici?n emp?rica. Si flag es menor que cero no se trata de LwPolylines ...
91
                if (flag>=0) {
92
                        double constWidth = 0.0;
93
                        if ((flag & 0x4)>0) {
94
                                v = DwgUtil.getBitDouble(data, bitPos);
95
                                bitPos = ((Integer)v.get(0)).intValue();
96
                                constWidth = ((Double)v.get(1)).doubleValue();
97
                        }
98
                        this.constWidth = constWidth;
99
                        double elev = 0.0;
100
                        if ((flag & 0x8)>0) {
101
                                v = DwgUtil.getBitDouble(data, bitPos);
102
                                bitPos = ((Integer)v.get(0)).intValue();
103
                                elev = ((Double)v.get(1)).doubleValue();
104
                        }
105
                        elevation = elev;
106
                        double thickness = 0.0;
107
                        if ((flag & 0x2)>0) {
108
                                v = DwgUtil.getBitDouble(data, bitPos);
109
                                bitPos = ((Integer)v.get(0)).intValue();
110
                                thickness = ((Double)v.get(1)).doubleValue();
111
                        }
112
                        this.thickness = thickness;
113
                        double nx = 0.0, ny = 0.0, nz = 0.0;
114
                        if ((flag & 0x1)>0) {
115
                                v = DwgUtil.getBitDouble(data, bitPos);
116
                                bitPos = ((Integer)v.get(0)).intValue();
117
                                nx = ((Double)v.get(1)).doubleValue();
118
                                v = DwgUtil.getBitDouble(data, bitPos);
119
                                bitPos = ((Integer)v.get(0)).intValue();
120
                                ny = ((Double)v.get(1)).doubleValue();
121
                                v = DwgUtil.getBitDouble(data, bitPos);
122
                                bitPos = ((Integer)v.get(0)).intValue();
123
                                nz = ((Double)v.get(1)).doubleValue();
124
                        }
125
                        normal = new double[]{nx, ny, nz};
126
                        v = DwgUtil.getBitLong(data, bitPos);
127
                        bitPos = ((Integer)v.get(0)).intValue();
128
                        int np = ((Integer)v.get(1)).intValue();
129
                        // TODO: Condici?n emp?rica. Si hay m?s de 10000 puntos no se trata de LwPolylines.
130
                    // Este tema hay que revisarlo porque si pueden existir LwPolylines con m?s de
131
                    // 10000 v?rtices ...
132
            // Se han encontrado lwplines con np = 0. Estas lwplines tambi?n ser?n
133
            // ignoradas.
134
                        if (np>0 && np<10000) {
135
                                long nb = 0;
136
                                if ((flag & 0x10)>0) {
137
                                        v = DwgUtil.getBitLong(data, bitPos);
138
                                        bitPos = ((Integer)v.get(0)).intValue();
139
                                        nb = ((Integer)v.get(1)).intValue();
140
                                }
141
                                long nw = 0;
142
                                if ((flag & 0x20)>0) {
143
                                        v = DwgUtil.getBitLong(data, bitPos);
144
                                        bitPos = ((Integer)v.get(0)).intValue();
145
                                        nw = ((Integer)v.get(1)).intValue();
146
                                }
147
                //System.out.println("np = " + np);
148
                                Point2D[] vertices = new Point2D[np];
149
                                v = DwgUtil.getRawDouble(data, bitPos);
150
                                bitPos = ((Integer)v.get(0)).intValue();
151
                                double vx = ((Double)v.get(1)).doubleValue();
152
                                v = DwgUtil.getRawDouble(data, bitPos);
153
                                bitPos = ((Integer)v.get(0)).intValue();
154
                                double vy = ((Double)v.get(1)).doubleValue();
155
                                vertices[0] = new Point2D.Double(vx, vy);
156
                                for (int i=1; i<(np); i++) {
157
                                        v = DwgUtil.getDefaultDouble(data, bitPos, vx);
158
                                        bitPos = ((Integer)v.get(0)).intValue();
159
                                        double x = ((Double)v.get(1)).doubleValue();
160
                                        v = DwgUtil.getDefaultDouble(data, bitPos, vy);
161
                                        bitPos = ((Integer)v.get(0)).intValue();
162
                                        double y = ((Double)v.get(1)).doubleValue();
163
                                        vertices[i] = new Point2D.Double(x, y);
164
                                        vx = x;
165
                                        vy = y;
166
                                }
167
                                this.vertices = vertices;
168
                                double[] bulges = new double[0];
169
                                if (nb>0) {
170
                                        bulges = new double[(int)nb];
171
                                        for (int i=0; i<nb; i++) {
172
                                                v = DwgUtil.getRawDouble(data, bitPos);
173
                                                bitPos = ((Integer)v.get(0)).intValue();
174
                                                double bulge = ((Double)v.get(1)).doubleValue();
175
                                                bulges[i] = bulge;
176
                                        }
177
                                } else if (nb==0) {
178
                                        bulges = new double[(int)np];
179
                                        for (int i=0;i<(int)np; i++) {
180
                                            bulges[i] = 0;
181
                                        }
182
                                }
183
                                this.bulges = bulges;
184
                                if (nw>0) {
185
                                        double[][] widths = new double[(int)nw][2];
186
                                        for (int i=0; i<nw; i++) {
187
                                                v = DwgUtil.getBitDouble(data, bitPos);
188
                                                bitPos = ((Integer)v.get(0)).intValue();
189
                                                double sw = ((Double)v.get(1)).doubleValue();
190
                                                v = DwgUtil.getBitDouble(data, bitPos);
191
                                                bitPos = ((Integer)v.get(0)).intValue();
192
                                                double ew = ((Double)v.get(1)).doubleValue();
193
                                                widths[i][0] = sw;
194
                                                widths[i][1] = ew;
195
                                        }
196
                                        this.widths = widths;
197
                                }
198
                                bitPos = readObjectTailV15(data, bitPos);
199
                        }
200
                }
201
        }
202
        /**
203
         * @return Returns the bulges.
204
         */
205
        public double[] getBulges() {
206
                return bulges;
207
        }
208
        /**
209
         * @param bulges The bulges to set.
210
         */
211
        public void setBulges(double[] bulges) {
212
                this.bulges = bulges;
213
        }
214
        /**
215
         * @return Returns the flag.
216
         */
217
        public int getFlag() {
218
                return flag;
219
        }
220
        /**
221
         * @param flag The flag to set.
222
         */
223
        public void setFlag(int flag) {
224
                this.flag = flag;
225
        }
226
        /**
227
         * @return Returns the vertices.
228
         */
229
        public Point2D[] getVertices() {
230
                return vertices;
231
        }
232
        /**
233
         * @param vertices The vertices to set.
234
         */
235
        public void setVertices(Point2D[] vertices) {
236
                this.vertices = vertices;
237
        }
238
    /**
239
     * @return Returns the elevation.
240
     */
241
    public double getElevation() {
242
        return elevation;
243
    }
244
    /**
245
     * @param elevation The elevation to set.
246
     */
247
    public void setElevation(double elevation) {
248
        this.elevation = elevation;
249
    }
250
    /**
251
     * @return Returns the normal.
252
     */
253
    public double[] getNormal() {
254
        return normal;
255
    }
256
        /* (non-Javadoc)
257
         * @see java.lang.Object#clone()
258
         */
259
        public Object clone() {
260
                DwgLwPolyline dwgLwPolyline = new DwgLwPolyline(index);
261
                dwgLwPolyline.setType(type);
262
                dwgLwPolyline.setHandle(handle);
263
                dwgLwPolyline.setVersion(version);
264
                dwgLwPolyline.setMode(mode);
265
                dwgLwPolyline.setLayerHandle(layerHandle);
266
                dwgLwPolyline.setColor(color);
267
                dwgLwPolyline.setNumReactors(numReactors);
268
                dwgLwPolyline.setNoLinks(noLinks);
269
                dwgLwPolyline.setLinetypeFlags(linetypeFlags);
270
                dwgLwPolyline.setPlotstyleFlags(plotstyleFlags);
271
                dwgLwPolyline.setSizeInBits(sizeInBits);
272
                dwgLwPolyline.setExtendedData(extendedData);
273
                dwgLwPolyline.setGraphicData(graphicData);
274
                //dwgLwPolyline.setInsideBlock(insideBlock);
275
                dwgLwPolyline.setFlag(flag);
276
                dwgLwPolyline.setElevation(elevation);
277
                dwgLwPolyline.setConstWidth(constWidth);
278
                dwgLwPolyline.setThickness(thickness);
279
                dwgLwPolyline.setNormal(normal);
280
                dwgLwPolyline.setVertices(vertices);
281
                dwgLwPolyline.setBulges(bulges);
282
                dwgLwPolyline.setWidths(widths);
283
                return dwgLwPolyline;
284
        }
285
        /**
286
         * @return Returns the constWidth.
287
         */
288
        public double getConstWidth() {
289
                return constWidth;
290
        }
291
        /**
292
         * @param constWidth The constWidth to set.
293
         */
294
        public void setConstWidth(double constWidth) {
295
                this.constWidth = constWidth;
296
        }
297
        /**
298
         * @return Returns the thickness.
299
         */
300
        public double getThickness() {
301
                return thickness;
302
        }
303
        /**
304
         * @param thickness The thickness to set.
305
         */
306
        public void setThickness(double thickness) {
307
                this.thickness = thickness;
308
        }
309
        /**
310
         * @return Returns the widths.
311
         */
312
        public double[][] getWidths() {
313
                return widths;
314
        }
315
        /**
316
         * @param widths The widths to set.
317
         */
318
        public void setWidths(double[][] widths) {
319
                this.widths = widths;
320
        }
321
        /**
322
         * @param normal The normal to set.
323
         */
324
        public void setNormal(double[] normal) {
325
                this.normal = normal;
326
        }
327
        /* (non-Javadoc)
328
         * @see com.iver.cit.jdwglib.dwg.IDwgPolyline#calculateGisModel(java.util.List)
329
         */
330
        public void calculateGisModel(List dwgObjs) {
331
                if (getVertices() == null)
332
                        return;
333
                int flags = getFlag();
334
                Point2D[] pts = getVertices();
335
                double[] bulges = getBulges();
336
                Point2D[] newPts = new Point2D[pts.length];
337
                double[] newBulges = new double[bulges.length];
338
                // TODO: Aqu? pueden existir casos no contemplados ...
339
//        System.out.println("flags = " + flags);
340
        if (flags==512 || flags==776 || flags==768) {
341
                        newPts = new Point2D[pts.length+1];
342
                        newBulges = new double[bulges.length+1];
343
                        for (int j=0;j<pts.length;j++) {
344
                                newPts[j] = (Point2D)pts[j];
345
                        }
346
                        newPts[pts.length] = (Point2D)pts[0];
347
                        newBulges[pts.length] = 0;
348
                } else {
349
                        for (int j=0;j<pts.length;j++) {
350
                                newPts[j] = (Point2D)pts[j];
351
                        }
352
                }
353
                if (pts.length>0) {
354
                        setBulges(newBulges);
355
                        Point2D[] points = GisModelCurveCalculator.calculateGisModelBulge(newPts, newBulges);
356
                        setVertices(points);
357
                } else {
358
//                        System.out.println("Encontrada polil?nea sin puntos ...");
359
                        // TODO: No se debe mandar nunca una polil?nea sin puntos, si esto
360
                        // ocurre es porque existe un error que hay que corregir ...
361
                }
362
        }
363
        /* (non-Javadoc)
364
         * @see com.iver.cit.jdwglib.dwg.IDwgExtrusionable#applyExtrussion()
365
         */
366
        public void applyExtrussion() {
367
                if (getVertices() == null)
368
                        return;
369
                 Point2D[] vertices = getVertices();
370
         double[] lwPolylineExt = getNormal();
371
         // Normals and Extrusions aren`t the same
372
         if (lwPolylineExt[0]==0 && lwPolylineExt[1]==0 && lwPolylineExt[2]==0) lwPolylineExt[2] = 1.0;
373
         double elev = getElevation();
374
         double[][] lwPolylinePoints3D = new double[vertices.length][3];
375
         for (int j=0;j<vertices.length;j++) {
376
             lwPolylinePoints3D[j][0] = vertices[j].getX();
377
             lwPolylinePoints3D[j][1] = vertices[j].getY();
378
             lwPolylinePoints3D[j][2] = elev;
379
             lwPolylinePoints3D[j] = AcadExtrusionCalculator.CalculateAcadExtrusion(lwPolylinePoints3D[j], lwPolylineExt);
380
         }
381
         setElevation(elev);
382
         for (int j=0;j<vertices.length;j++) {
383
             vertices[j] = new Point2D.Double(lwPolylinePoints3D[j][0], lwPolylinePoints3D[j][1]);
384
         }
385
         setVertices(vertices);
386
        }
387
        /* (non-Javadoc)
388
         * @see com.iver.cit.jdwglib.dwg.IDwg3DTestable#has3DData()
389
         */
390
        public boolean has3DData() {
391
                return getElevation() != 0.0;
392
        }
393
        public double getZ() {
394
                return getElevation();
395
        }
396
        public FShape toFMapGeometry(boolean is3DFile) {
397
                FPolyline2D lwpline = null;
398
                Point2D[] pts = getVertices();
399
                double elev = getElevation();
400
                if (pts != null) {
401
                        
402
                        if (is3DFile) {
403
                                double[][] pline3D = new double[pts.length][3];
404
                                for (int j = 0; j < pts.length; j++) {
405
                                        pline3D[j][0] = pts[j].getX();
406
                                        pline3D[j][1] = pts[j].getY();
407
                                        pline3D[j][2] = elev;
408
                                }
409
                                lwpline = FMapUtil.points3DToFPolyline3D(pline3D);
410
                        } else {
411
                                lwpline = FMapUtil.points2DToFPolyline2D(pts);
412
                        }
413
                        
414
                }
415
                return lwpline;
416
        }
417
        public String toFMapString(boolean is3DFile) {
418
                if(is3DFile)
419
                        return "FPolyline3D";
420
                else
421
                        return "FPolyline2D";
422
        }
423
        
424
        public String toString(){
425
                return "LwPolyline";
426
        }
427
}