Statistics
| Revision:

root / trunk / libraries / libDwg / src / com / iver / cit / jdwglib / dwg / objects / DwgSpline.java @ 9825

History | View | Annotate | Download (12.1 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.util.ArrayList;
38

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

    
43
/**
44
 * The DwgSpline class represents a DWG Spline
45
 * 
46
 * @author jmorell
47
 */
48
public class DwgSpline extends DwgObject implements IDwg3DTestable{
49
        public DwgSpline(int index) {
50
                super(index);
51
                // TODO Auto-generated constructor stub
52
        }
53
        private int scenario;
54
        private int degree;
55
        private double fitTolerance;
56
        private double[] beginTanVector;
57
        private double[] endTanVector;
58
        private boolean rational;
59
        private boolean closed;
60
        private boolean periodic;
61
        private double knotTolerance;
62
        private double controlTolerance;
63
        private double[] knotPoints;
64
        private double[][] controlPoints;
65
        private double[] weights;
66
        private double[][] fitPoints;
67
        
68
        /**
69
         * Read a Spline in the DWG format Version 15
70
         * 
71
         * @param data Array of unsigned bytes obtained from the DWG binary file
72
         * @param offset The current bit offset where the value begins
73
         * @throws Exception If an unexpected bit value is found in the DWG file. Occurs
74
         *                    when we are looking for LwPolylines.
75
         */
76
        public void readDwgSplineV15(int[] data, int offset) throws Exception {
77
                int bitPos = offset;
78
                bitPos = readObjectHeaderV15(data, bitPos);
79
                ArrayList v = DwgUtil.getBitShort(data, bitPos);
80
                bitPos = ((Integer)v.get(0)).intValue();
81
                int sc = ((Integer)v.get(1)).intValue();
82
                scenario = sc;
83
                v = DwgUtil.getBitShort(data, bitPos);
84
                bitPos = ((Integer)v.get(0)).intValue();
85
                int deg = ((Integer)v.get(1)).intValue();
86
                degree = deg;
87
                int knotsNumber = 0;
88
                int controlPointsNumber = 0;
89
                int fitPointsNumber = 0;
90
                boolean weight = false;
91
                if (sc==2) {
92
                        v = DwgUtil.getBitDouble(data, bitPos);
93
                        bitPos = ((Integer)v.get(0)).intValue();
94
                        double ft = ((Double)v.get(1)).doubleValue();
95
                        fitTolerance = ft;
96
                        v = DwgUtil.getBitDouble(data, bitPos);
97
                        bitPos = ((Integer)v.get(0)).intValue();
98
                        double x = ((Double)v.get(1)).doubleValue();
99
                        v = DwgUtil.getBitDouble(data, bitPos);
100
                        bitPos = ((Integer)v.get(0)).intValue();
101
                        double y = ((Double)v.get(1)).doubleValue();
102
                        v = DwgUtil.getBitDouble(data, bitPos);
103
                        bitPos = ((Integer)v.get(0)).intValue();
104
                        double z = ((Double)v.get(1)).doubleValue();
105
                        double[] coord = new double[]{x, y, z};
106
                        beginTanVector = coord;
107
                        v = DwgUtil.getBitDouble(data, bitPos);
108
                        bitPos = ((Integer)v.get(0)).intValue();
109
                        x = ((Double)v.get(1)).doubleValue();
110
                        v = DwgUtil.getBitDouble(data, bitPos);
111
                        bitPos = ((Integer)v.get(0)).intValue();
112
                        y = ((Double)v.get(1)).doubleValue();
113
                        v = DwgUtil.getBitDouble(data, bitPos);
114
                        bitPos = ((Integer)v.get(0)).intValue();
115
                        z = ((Double)v.get(1)).doubleValue();
116
                        coord = new double[]{x, y, z};
117
                        endTanVector = coord;
118
                        v = DwgUtil.getBitShort(data, bitPos);
119
                        bitPos = ((Integer)v.get(0)).intValue();
120
                        fitPointsNumber = ((Integer)v.get(1)).intValue();
121
                } else if (sc==1) {
122
                        v = DwgUtil.testBit(data, bitPos);
123
                        bitPos = ((Integer)v.get(0)).intValue();
124
                        boolean rat = ((Boolean)v.get(1)).booleanValue();
125
                        rational = rat;
126
                        v = DwgUtil.testBit(data, bitPos);
127
                        bitPos = ((Integer)v.get(0)).intValue();
128
                        boolean closed = ((Boolean)v.get(1)).booleanValue();
129
                        this.closed = closed;
130
                        v = DwgUtil.testBit(data, bitPos);
131
                        bitPos = ((Integer)v.get(0)).intValue();
132
                        boolean per = ((Boolean)v.get(1)).booleanValue();
133
                        periodic = per;
134
                        v = DwgUtil.getBitDouble(data, bitPos);
135
                        bitPos = ((Integer)v.get(0)).intValue();
136
                        double ktol = ((Double)v.get(1)).doubleValue();
137
                        knotTolerance = ktol;
138
                        v = DwgUtil.getBitDouble(data, bitPos);
139
                        bitPos = ((Integer)v.get(0)).intValue();
140
                        double ctol = ((Double)v.get(1)).doubleValue();
141
                        controlTolerance = ctol;
142
                        v = DwgUtil.getBitLong(data, bitPos);
143
                        bitPos = ((Integer)v.get(0)).intValue();
144
                        knotsNumber = ((Integer)v.get(1)).intValue();
145
                        v = DwgUtil.getBitLong(data, bitPos);
146
                        bitPos = ((Integer)v.get(0)).intValue();
147
                        controlPointsNumber = ((Integer)v.get(1)).intValue();
148
                        v = DwgUtil.testBit(data, bitPos);
149
                        bitPos = ((Integer)v.get(0)).intValue();
150
                        weight = ((Boolean)v.get(1)).booleanValue();
151
                } else {
152
                        System.out.println("ERROR: Escenario desconocido");
153
                }
154
                if (knotsNumber>0) {
155
                        double[] knotpts = new double[knotsNumber];
156
                        for (int i=0;i<knotsNumber;i++) {
157
                                v = DwgUtil.getBitDouble(data, bitPos);
158
                                bitPos = ((Integer)v.get(0)).intValue();
159
                                knotpts[i] = ((Double)v.get(1)).doubleValue();
160
                        }
161
                        knotPoints = knotpts;
162
                }
163
                if (controlPointsNumber>0) {
164
                        // Si el n?mero de weights no coincide con el de ctrlpts habr? problemas ...
165
                        double[][] ctrlpts = new double[controlPointsNumber][3];
166
                        double[] weights = new double[controlPointsNumber];
167
                        for (int i=0;i<controlPointsNumber;i++) {
168
                                v = DwgUtil.getBitDouble(data, bitPos);
169
                                bitPos = ((Integer)v.get(0)).intValue();
170
                                double x = ((Double)v.get(1)).doubleValue();
171
                                v = DwgUtil.getBitDouble(data, bitPos);
172
                                bitPos = ((Integer)v.get(0)).intValue();
173
                                double y = ((Double)v.get(1)).doubleValue();
174
                                v = DwgUtil.getBitDouble(data, bitPos);
175
                                bitPos = ((Integer)v.get(0)).intValue();
176
                                double z = ((Double)v.get(1)).doubleValue();
177
                                //double[] coord = new double[]{x, y, z};
178
                                ctrlpts[i][0] = x;
179
                                ctrlpts[i][1] = y;
180
                                ctrlpts[i][2] = z;
181
                                if (weight) {
182
                                        v = DwgUtil.getBitDouble(data, bitPos);
183
                                        bitPos = ((Integer)v.get(0)).intValue();
184
                                        weights[i] = ((Double)v.get(1)).doubleValue();
185
                                }
186
                        }
187
                        controlPoints = ctrlpts;
188
                        if (weight) {
189
                                this.weights = weights;
190
                        }
191
                }
192
                if (fitPointsNumber>0) {
193
                        double[][] fitpts = new double[fitPointsNumber][3];
194
                        for (int i=0;i<fitPointsNumber;i++) {
195
                                v = DwgUtil.getBitDouble(data, bitPos);
196
                                bitPos = ((Integer)v.get(0)).intValue();
197
                                double x = ((Double)v.get(1)).doubleValue();
198
                                v = DwgUtil.getBitDouble(data, bitPos);
199
                                bitPos = ((Integer)v.get(0)).intValue();
200
                                double y = ((Double)v.get(1)).doubleValue();
201
                                v = DwgUtil.getBitDouble(data, bitPos);
202
                                bitPos = ((Integer)v.get(0)).intValue();
203
                                double z = ((Double)v.get(1)).doubleValue();
204
                                fitpts[i][0] = x;
205
                                fitpts[i][1] = y;
206
                                fitpts[i][2] = z;
207
                        }
208
                        fitPoints = fitpts;
209
                }
210
                bitPos = readObjectTailV15(data, bitPos);
211
        }
212
        /**
213
         * @return Returns the closed.
214
         */
215
        public boolean isClosed() {
216
                return closed;
217
        }
218
        /**
219
         * @param closed The closed to set.
220
         */
221
        public void setClosed(boolean closed) {
222
                this.closed = closed;
223
        }
224
        /**
225
         * @return Returns the controlPoints.
226
         */
227
        public double[][] getControlPoints() {
228
                return controlPoints;
229
        }
230
        /**
231
         * @param controlPoints The controlPoints to set.
232
         */
233
        public void setControlPoints(double[][] controlPoints) {
234
                this.controlPoints = controlPoints;
235
        }
236
        /**
237
         * @return Returns the fitPoints.
238
         */
239
        public double[][] getFitPoints() {
240
                return fitPoints;
241
        }
242
        /**
243
         * @param fitPoints The fitPoints to set.
244
         */
245
        public void setFitPoints(double[][] fitPoints) {
246
                this.fitPoints = fitPoints;
247
        }
248
        /**
249
         * @return Returns the knotPoints.
250
         */
251
        public double[] getKnotPoints() {
252
                return knotPoints;
253
        }
254
        /**
255
         * @param knotPoints The knotPoints to set.
256
         */
257
        public void setKnotPoints(double[] knotPoints) {
258
                this.knotPoints = knotPoints;
259
        }
260
        /**
261
         * @return Returns the scenario.
262
         */
263
        public int getScenario() {
264
                return scenario;
265
        }
266
        /**
267
         * @param scenario The scenario to set.
268
         */
269
        public void setScenario(int scenario) {
270
                this.scenario = scenario;
271
        }
272
        /* (non-Javadoc)
273
         * @see java.lang.Object#clone()
274
         */
275
        public Object clone() {
276
                DwgSpline dwgSpline = new DwgSpline(index);
277
                dwgSpline.setType(type);
278
                dwgSpline.setHandle(handle);
279
                dwgSpline.setVersion(version);
280
                dwgSpline.setMode(mode);
281
                dwgSpline.setLayerHandle(layerHandle);
282
                dwgSpline.setColor(color);
283
                dwgSpline.setNumReactors(numReactors);
284
                dwgSpline.setNoLinks(noLinks);
285
                dwgSpline.setLinetypeFlags(linetypeFlags);
286
                dwgSpline.setPlotstyleFlags(plotstyleFlags);
287
                dwgSpline.setSizeInBits(sizeInBits);
288
                dwgSpline.setExtendedData(extendedData);
289
                dwgSpline.setGraphicData(graphicData);
290
                //dwgSpline.setInsideBlock(insideBlock);
291
                dwgSpline.setScenario(scenario);
292
                dwgSpline.setDegree(degree);
293
                dwgSpline.setFitTolerance(fitTolerance);
294
                dwgSpline.setBeginTanVector(beginTanVector);
295
                dwgSpline.setEndTanVector(endTanVector);
296
                dwgSpline.setRational(rational);
297
                dwgSpline.setClosed(closed);
298
                dwgSpline.setPeriodic(periodic);
299
                dwgSpline.setKnotTolerance(knotTolerance);
300
                dwgSpline.setControlTolerance(controlTolerance);
301
                dwgSpline.setKnotPoints(knotPoints);
302
                dwgSpline.setControlPoints(controlPoints);
303
                dwgSpline.setWeights(weights);
304
                dwgSpline.setFitPoints(fitPoints);
305
                return dwgSpline;
306
        }
307
        /**
308
         * @return Returns the beginTanVector.
309
         */
310
        public double[] getBeginTanVector() {
311
                return beginTanVector;
312
        }
313
        /**
314
         * @param beginTanVector The beginTanVector to set.
315
         */
316
        public void setBeginTanVector(double[] beginTanVector) {
317
                this.beginTanVector = beginTanVector;
318
        }
319
        /**
320
         * @return Returns the controlTolerance.
321
         */
322
        public double getControlTolerance() {
323
                return controlTolerance;
324
        }
325
        /**
326
         * @param controlTolerance The controlTolerance to set.
327
         */
328
        public void setControlTolerance(double controlTolerance) {
329
                this.controlTolerance = controlTolerance;
330
        }
331
        /**
332
         * @return Returns the degree.
333
         */
334
        public int getDegree() {
335
                return degree;
336
        }
337
        /**
338
         * @param degree The degree to set.
339
         */
340
        public void setDegree(int degree) {
341
                this.degree = degree;
342
        }
343
        /**
344
         * @return Returns the endTanVector.
345
         */
346
        public double[] getEndTanVector() {
347
                return endTanVector;
348
        }
349
        /**
350
         * @param endTanVector The endTanVector to set.
351
         */
352
        public void setEndTanVector(double[] endTanVector) {
353
                this.endTanVector = endTanVector;
354
        }
355
        /**
356
         * @return Returns the fitTolerance.
357
         */
358
        public double getFitTolerance() {
359
                return fitTolerance;
360
        }
361
        /**
362
         * @param fitTolerance The fitTolerance to set.
363
         */
364
        public void setFitTolerance(double fitTolerance) {
365
                this.fitTolerance = fitTolerance;
366
        }
367
        /**
368
         * @return Returns the knotTolerance.
369
         */
370
        public double getKnotTolerance() {
371
                return knotTolerance;
372
        }
373
        /**
374
         * @param knotTolerance The knotTolerance to set.
375
         */
376
        public void setKnotTolerance(double knotTolerance) {
377
                this.knotTolerance = knotTolerance;
378
        }
379
        /**
380
         * @return Returns the periodic.
381
         */
382
        public boolean isPeriodic() {
383
                return periodic;
384
        }
385
        /**
386
         * @param periodic The periodic to set.
387
         */
388
        public void setPeriodic(boolean periodic) {
389
                this.periodic = periodic;
390
        }
391
        /**
392
         * @return Returns the rational.
393
         */
394
        public boolean isRational() {
395
                return rational;
396
        }
397
        /**
398
         * @param rational The rational to set.
399
         */
400
        public void setRational(boolean rational) {
401
                this.rational = rational;
402
        }
403
        /**
404
         * @return Returns the weights.
405
         */
406
        public double[] getWeights() {
407
                return weights;
408
        }
409
        /**
410
         * @param weights The weights to set.
411
         */
412
        public void setWeights(double[] weights) {
413
                this.weights = weights;
414
        }
415
        /* (non-Javadoc)
416
         * @see com.iver.cit.jdwglib.dwg.IDwg3DTestable#has3DData()
417
         */
418
        public boolean has3DData() {
419
                double[][] pts = getControlPoints();   
420
                if(pts == null)
421
                        return false;
422
                double z = 0d;
423
                for (int j=0;j<pts.length;j++) {
424
                                z = pts[j][2];
425
                                if (z != 0.0) 
426
                                        return true;
427
                }//for
428
                return false;
429
        }
430
}