Statistics
| Revision:

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

History | View | Annotate | Download (8.98 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.Vector;
39

    
40
import com.iver.cit.jdwglib.dwg.DwgObject;
41
import com.iver.cit.jdwglib.dwg.DwgUtil;
42

    
43
/**
44
 * The DwgLwPolyline class represents a DWG LwPolyline
45
 * 
46
 * @author jmorell
47
 */
48
public class DwgLwPolyline extends DwgObject {
49
        private int flag;
50
        private double constWidth;
51
        private double elevation;
52
        private double thickness;
53
        private double[] normal;
54
        private Point2D[] vertices;
55
        private double[] bulges;
56
        private double[][] widths;
57
        
58
        /**
59
         * Read a LwPolyline in the DWG format Version 15
60
         * 
61
         * @param data Array of unsigned bytes obtained from the DWG binary file
62
         * @param offset The current bit offset where the value begins
63
         * @throws Exception If an unexpected bit value is found in the DWG file. Occurs
64
         *                    when we are looking for LwPolylines.
65
         */
66
        public void readDwgLwPolylineV15(int[] data, int offset) throws Exception {
67
                //System.out.println("DwgLwPolyline.readDwgLwPolyline() executed ...");
68
                int bitPos = offset;
69
                bitPos = readObjectHeaderV15(data, bitPos);
70
                Vector v = DwgUtil.getBitShort(data, bitPos);
71
                bitPos = ((Integer)v.get(0)).intValue();
72
                int flag = ((Integer)v.get(1)).intValue();
73
                this.flag = flag;
74
                // Condici?n emp?rica. Si flag es menor que cero no se trata de LwPolylines ...
75
                if (flag>=0) {
76
                        double constWidth = 0.0;
77
                        if ((flag & 0x4)>0) {
78
                                v = DwgUtil.getBitDouble(data, bitPos);
79
                                bitPos = ((Integer)v.get(0)).intValue();
80
                                constWidth = ((Double)v.get(1)).doubleValue();
81
                        }
82
                        this.constWidth = constWidth;
83
                        double elev = 0.0;
84
                        if ((flag & 0x8)>0) {
85
                                v = DwgUtil.getBitDouble(data, bitPos);
86
                                bitPos = ((Integer)v.get(0)).intValue();
87
                                elev = ((Double)v.get(1)).doubleValue();
88
                        }
89
                        elevation = elev;
90
                        double thickness = 0.0;
91
                        if ((flag & 0x2)>0) {
92
                                v = DwgUtil.getBitDouble(data, bitPos);
93
                                bitPos = ((Integer)v.get(0)).intValue();
94
                                thickness = ((Double)v.get(1)).doubleValue();
95
                        }
96
                        this.thickness = thickness;
97
                        double nx = 0.0, ny = 0.0, nz = 0.0;
98
                        if ((flag & 0x1)>0) {
99
                                v = DwgUtil.getBitDouble(data, bitPos);
100
                                bitPos = ((Integer)v.get(0)).intValue();
101
                                nx = ((Double)v.get(1)).doubleValue();
102
                                v = DwgUtil.getBitDouble(data, bitPos);
103
                                bitPos = ((Integer)v.get(0)).intValue();
104
                                ny = ((Double)v.get(1)).doubleValue();
105
                                v = DwgUtil.getBitDouble(data, bitPos);
106
                                bitPos = ((Integer)v.get(0)).intValue();
107
                                nz = ((Double)v.get(1)).doubleValue();
108
                        }
109
                        normal = new double[]{nx, ny, nz};
110
                        v = DwgUtil.getBitLong(data, bitPos);
111
                        bitPos = ((Integer)v.get(0)).intValue();
112
                        int np = ((Integer)v.get(1)).intValue();
113
                        // TODO: Condici?n emp?rica. Si hay m?s de 10000 puntos no se trata de LwPolylines.
114
                    // Este tema hay que revisarlo porque si pueden existir LwPolylines con m?s de
115
                    // 10000 v?rtices ...
116
            // Se han encontrado lwplines con np = 0. Estas lwplines tambi?n ser?n
117
            // ignoradas.
118
                        if (np>0 && np<10000) {
119
                                long nb = 0;
120
                                if ((flag & 0x10)>0) {
121
                                        v = DwgUtil.getBitLong(data, bitPos);
122
                                        bitPos = ((Integer)v.get(0)).intValue();
123
                                        nb = ((Integer)v.get(1)).intValue();
124
                                }
125
                                long nw = 0;
126
                                if ((flag & 0x20)>0) {
127
                                        v = DwgUtil.getBitLong(data, bitPos);
128
                                        bitPos = ((Integer)v.get(0)).intValue();
129
                                        nw = ((Integer)v.get(1)).intValue();
130
                                }
131
                //System.out.println("np = " + np);
132
                                Point2D[] vertices = new Point2D[np];
133
                                v = DwgUtil.getRawDouble(data, bitPos);
134
                                bitPos = ((Integer)v.get(0)).intValue();
135
                                double vx = ((Double)v.get(1)).doubleValue();
136
                                v = DwgUtil.getRawDouble(data, bitPos);
137
                                bitPos = ((Integer)v.get(0)).intValue();
138
                                double vy = ((Double)v.get(1)).doubleValue();
139
                                vertices[0] = new Point2D.Double(vx, vy);
140
                                for (int i=1; i<(np); i++) {
141
                                        v = DwgUtil.getDefaultDouble(data, bitPos, vx);
142
                                        bitPos = ((Integer)v.get(0)).intValue();
143
                                        double x = ((Double)v.get(1)).doubleValue();
144
                                        v = DwgUtil.getDefaultDouble(data, bitPos, vy);
145
                                        bitPos = ((Integer)v.get(0)).intValue();
146
                                        double y = ((Double)v.get(1)).doubleValue();
147
                                        vertices[i] = new Point2D.Double(x, y);
148
                                        vx = x;
149
                                        vy = y;
150
                                }
151
                                this.vertices = vertices;
152
                                double[] bulges = new double[0];
153
                                if (nb>0) {
154
                                        bulges = new double[(int)nb];
155
                                        for (int i=0; i<nb; i++) {
156
                                                v = DwgUtil.getRawDouble(data, bitPos);
157
                                                bitPos = ((Integer)v.get(0)).intValue();
158
                                                double bulge = ((Double)v.get(1)).doubleValue();
159
                                                bulges[i] = bulge;
160
                                        }
161
                                } else if (nb==0) {
162
                                        bulges = new double[(int)np];
163
                                        for (int i=0;i<(int)np; i++) {
164
                                            bulges[i] = 0;
165
                                        }
166
                                }
167
                                this.bulges = bulges;
168
                                if (nw>0) {
169
                                        double[][] widths = new double[(int)nw][2];
170
                                        for (int i=0; i<nw; i++) {
171
                                                v = DwgUtil.getBitDouble(data, bitPos);
172
                                                bitPos = ((Integer)v.get(0)).intValue();
173
                                                double sw = ((Double)v.get(1)).doubleValue();
174
                                                v = DwgUtil.getBitDouble(data, bitPos);
175
                                                bitPos = ((Integer)v.get(0)).intValue();
176
                                                double ew = ((Double)v.get(1)).doubleValue();
177
                                                widths[i][0] = sw;
178
                                                widths[i][1] = ew;
179
                                        }
180
                                        this.widths = widths;
181
                                }
182
                                bitPos = readObjectTailV15(data, bitPos);
183
                        }
184
                }
185
        }
186
        /**
187
         * @return Returns the bulges.
188
         */
189
        public double[] getBulges() {
190
                return bulges;
191
        }
192
        /**
193
         * @param bulges The bulges to set.
194
         */
195
        public void setBulges(double[] bulges) {
196
                this.bulges = bulges;
197
        }
198
        /**
199
         * @return Returns the flag.
200
         */
201
        public int getFlag() {
202
                return flag;
203
        }
204
        /**
205
         * @param flag The flag to set.
206
         */
207
        public void setFlag(int flag) {
208
                this.flag = flag;
209
        }
210
        /**
211
         * @return Returns the vertices.
212
         */
213
        public Point2D[] getVertices() {
214
                return vertices;
215
        }
216
        /**
217
         * @param vertices The vertices to set.
218
         */
219
        public void setVertices(Point2D[] vertices) {
220
                this.vertices = vertices;
221
        }
222
    /**
223
     * @return Returns the elevation.
224
     */
225
    public double getElevation() {
226
        return elevation;
227
    }
228
    /**
229
     * @param elevation The elevation to set.
230
     */
231
    public void setElevation(double elevation) {
232
        this.elevation = elevation;
233
    }
234
    /**
235
     * @return Returns the normal.
236
     */
237
    public double[] getNormal() {
238
        return normal;
239
    }
240
        /* (non-Javadoc)
241
         * @see java.lang.Object#clone()
242
         */
243
        public Object clone() {
244
                DwgLwPolyline dwgLwPolyline = new DwgLwPolyline();
245
                dwgLwPolyline.setType(type);
246
                dwgLwPolyline.setHandle(handle);
247
                dwgLwPolyline.setVersion(version);
248
                dwgLwPolyline.setMode(mode);
249
                dwgLwPolyline.setLayerHandle(layerHandle);
250
                dwgLwPolyline.setColor(color);
251
                dwgLwPolyline.setNumReactors(numReactors);
252
                dwgLwPolyline.setNoLinks(noLinks);
253
                dwgLwPolyline.setLinetypeFlags(linetypeFlags);
254
                dwgLwPolyline.setPlotstyleFlags(plotstyleFlags);
255
                dwgLwPolyline.setSizeInBits(sizeInBits);
256
                dwgLwPolyline.setExtendedData(extendedData);
257
                dwgLwPolyline.setGraphicData(graphicData);
258
                //dwgLwPolyline.setInsideBlock(insideBlock);
259
                dwgLwPolyline.setFlag(flag);
260
                dwgLwPolyline.setElevation(elevation);
261
                dwgLwPolyline.setConstWidth(constWidth);
262
                dwgLwPolyline.setThickness(thickness);
263
                dwgLwPolyline.setNormal(normal);
264
                dwgLwPolyline.setVertices(vertices);
265
                dwgLwPolyline.setBulges(bulges);
266
                dwgLwPolyline.setWidths(widths);
267
                return dwgLwPolyline;
268
        }
269
        /**
270
         * @return Returns the constWidth.
271
         */
272
        public double getConstWidth() {
273
                return constWidth;
274
        }
275
        /**
276
         * @param constWidth The constWidth to set.
277
         */
278
        public void setConstWidth(double constWidth) {
279
                this.constWidth = constWidth;
280
        }
281
        /**
282
         * @return Returns the thickness.
283
         */
284
        public double getThickness() {
285
                return thickness;
286
        }
287
        /**
288
         * @param thickness The thickness to set.
289
         */
290
        public void setThickness(double thickness) {
291
                this.thickness = thickness;
292
        }
293
        /**
294
         * @return Returns the widths.
295
         */
296
        public double[][] getWidths() {
297
                return widths;
298
        }
299
        /**
300
         * @param widths The widths to set.
301
         */
302
        public void setWidths(double[][] widths) {
303
                this.widths = widths;
304
        }
305
        /**
306
         * @param normal The normal to set.
307
         */
308
        public void setNormal(double[] normal) {
309
                this.normal = normal;
310
        }
311
}