Statistics
| Revision:

root / trunk / libraries / libDwg / src / com / iver / cit / jdwglib / dwg / objects / DwgPolyline2D.java @ 10054

History | View | Annotate | Download (15.3 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.DwgFile;
44
import com.iver.cit.jdwglib.dwg.DwgHandleReference;
45
import com.iver.cit.jdwglib.dwg.DwgObject;
46
import com.iver.cit.jdwglib.dwg.IDwg2FMap;
47
import com.iver.cit.jdwglib.dwg.IDwg3DTestable;
48
import com.iver.cit.jdwglib.dwg.IDwgBlockMember;
49
import com.iver.cit.jdwglib.dwg.IDwgExtrusionable;
50
import com.iver.cit.jdwglib.dwg.IDwgPolyline;
51
import com.iver.cit.jdwglib.util.AcadExtrusionCalculator;
52
import com.iver.cit.jdwglib.util.FMapUtil;
53
import com.iver.cit.jdwglib.util.GisModelCurveCalculator;
54

    
55
/**
56
 * The DwgPolyline2D class represents a DWG Polyline2D
57
 * 
58
 * @author jmorell
59
 */
60
public class DwgPolyline2D extends DwgObject 
61
        implements IDwgPolyline, IDwgExtrusionable, IDwg3DTestable, IDwg2FMap, IDwgBlockMember{
62
        
63
        
64
        public DwgPolyline2D(int index) {
65
                super(index);
66
                // TODO Auto-generated constructor stub
67
        }
68
        private int flags;
69
        private int curveType;
70
        private double initWidth;
71
        private double endWidth;
72
        private double thickness;
73
        private double elevation;
74
        private double[] extrusion;
75
        private DwgHandleReference firstVertexHandle = null;
76
        private DwgHandleReference lastVertexHandle = null;
77
        private DwgHandleReference seqendHandle = null;
78
        private Point2D[] pts;
79
        private double[] bulges;
80
        
81
        /**
82
         * @return Returns the firstVertexHandle.
83
         */
84
        public DwgHandleReference getFirstVertexHandle() {
85
                return firstVertexHandle;
86
        }
87
        /**
88
         * @param firstVertexHandle The firstVertexHandle to set.
89
         */
90
        public void setFirstVertexHandle(DwgHandleReference firstVertexHandle) {
91
                this.firstVertexHandle = firstVertexHandle;
92
        }
93
        /**
94
         * @return Returns the flags.
95
         */
96
        public int getFlags() {
97
                return flags;
98
        }
99
        /**
100
         * @param flags The flags to set.
101
         */
102
        public void setFlags(int flags) {
103
                this.flags = flags;
104
        }
105
        /**
106
         * @return Returns the lastVertexHandle.
107
         */
108
        public DwgHandleReference getLastVertexHandle() {
109
                return lastVertexHandle;
110
        }
111
        /**
112
         * @param lastVertexHandle The lastVertexHandle to set.
113
         */
114
        public void setLastVertexHandle(DwgHandleReference lastVertexHandle) {
115
                this.lastVertexHandle = lastVertexHandle;
116
        }
117
        /**
118
         * @return Returns the pts.
119
         */
120
        public Point2D[] getPts() {
121
                return pts;
122
        }
123
        /**
124
         * @param pts The pts to set.
125
         */
126
        public void setPts(Point2D[] pts) {
127
                this.pts = pts;
128
        }
129
        /**
130
         * @return Returns the bulges.
131
         */
132
        public double[] getBulges() {
133
                return bulges;
134
        }
135
        /**
136
         * @param bulges The bulges to set.
137
         */
138
        public void setBulges(double[] bulges) {
139
                this.bulges = bulges;
140
        }
141
        /**
142
         * @return Returns the initWidth.
143
         */
144
        public double getInitWidth() {
145
                return initWidth;
146
        }
147
        /**
148
         * @param initWidth The initWidth to set.
149
         */
150
        public void setInitWidth(double initWidth) {
151
                this.initWidth = initWidth;
152
        }
153
        /**
154
         * @return Returns the seqendHandle.
155
         */
156
        public DwgHandleReference getSeqendHandle() {
157
                return seqendHandle;
158
        }
159
        /**
160
         * @param seqendHandle The seqendHandle to set.
161
         */
162
        public void setSeqendHandle(DwgHandleReference seqendHandle) {
163
                this.seqendHandle = seqendHandle;
164
        }
165
        /**
166
         * @return Returns the thickness.
167
         */
168
        public double getThickness() {
169
                return thickness;
170
        }
171
        /**
172
         * @param thickness The thickness to set.
173
         */
174
        public void setThickness(double thickness) {
175
                this.thickness = thickness;
176
        }
177
        /**
178
         * @return Returns the curveType.
179
         */
180
        public int getCurveType() {
181
                return curveType;
182
        }
183
        /**
184
         * @param curveType The curveType to set.
185
         */
186
        public void setCurveType(int curveType) {
187
                this.curveType = curveType;
188
        }
189
        /**
190
         * @return Returns the elevation.
191
         */
192
        public double getElevation() {
193
                return elevation;
194
        }
195
        /**
196
         * @param elevation The elevation to set.
197
         */
198
        public void setElevation(double elevation) {
199
                this.elevation = elevation;
200
        }
201
        /**
202
         * @return Returns the endWidth.
203
         */
204
        public double getEndWidth() {
205
                return endWidth;
206
        }
207
        /**
208
         * @param endWidth The endWidth to set.
209
         */
210
        public void setEndWidth(double endWidth) {
211
                this.endWidth = endWidth;
212
        }
213
        /**
214
         * @return Returns the extrusion.
215
         */
216
        public double[] getExtrusion() {
217
                return extrusion;
218
        }
219
        /**
220
         * @param extrusion The extrusion to set.
221
         */
222
        public void setExtrusion(double[] extrusion) {
223
                this.extrusion = extrusion;
224
        }
225
        /* (non-Javadoc)
226
         * @see java.lang.Object#clone()
227
         */
228
        public Object clone() {
229
                DwgPolyline2D dwgPolyline2D = new DwgPolyline2D(flags);
230
                dwgPolyline2D.setType(type);
231
                dwgPolyline2D.setHandle(handle);
232
                dwgPolyline2D.setVersion(version);
233
                dwgPolyline2D.setMode(mode);
234
                dwgPolyline2D.setLayerHandle(layerHandle);
235
                dwgPolyline2D.setColor(color);
236
                dwgPolyline2D.setNumReactors(numReactors);
237
                dwgPolyline2D.setNoLinks(noLinks);
238
                dwgPolyline2D.setLinetypeFlags(linetypeFlags);
239
                dwgPolyline2D.setPlotstyleFlags(plotstyleFlags);
240
                dwgPolyline2D.setSizeInBits(sizeInBits);
241
                dwgPolyline2D.setExtendedData(extendedData);
242
                dwgPolyline2D.setGraphicData(graphicData);
243
                //dwgPolyline2D.setInsideBlock(insideBlock);
244
                dwgPolyline2D.setFlags(flags);
245
                dwgPolyline2D.setCurveType(curveType);
246
                dwgPolyline2D.setInitWidth(initWidth);
247
                dwgPolyline2D.setEndWidth(endWidth);
248
                dwgPolyline2D.setThickness(thickness);
249
                dwgPolyline2D.setElevation(elevation);
250
                dwgPolyline2D.setExtrusion(extrusion);
251
                dwgPolyline2D.setFirstVertexHandle(firstVertexHandle);
252
                dwgPolyline2D.setLastVertexHandle(lastVertexHandle);
253
                dwgPolyline2D.setSeqendHandle(seqendHandle);
254
                dwgPolyline2D.setPts(pts);
255
                dwgPolyline2D.setBulges(bulges);
256
                return dwgPolyline2D;
257
        }
258
        
259
        public void calculateGisModel(DwgFile dwgFile){
260
                int flags = getFlags();
261
//                En estas dos lineas de abajo y en el resto del metodo
262
//                se mantiene el mecanismo anterior al refactoring.
263
//                TODO: Pensar si deberiamos coger el handle completo.
264
//                Tal vez deberiamos tomar el handle completo y evaluar
265
//                a donde apuntan (pueden haber 2 handles con codigo y offset
266
//                distintos y que, sin embargo apunten al mismo objeto).
267
                int firstHandle = getFirstVertexHandle().getOffset();
268
                int lastHandle = getLastVertexHandle().getOffset();
269
                ArrayList pts = new ArrayList();
270
                ArrayList bulges = new ArrayList();
271
                double[] pt = new double[3];
272
                double bulge = 0d;
273
                
274
                DwgObject first = dwgFile.getDwgObjectFromHandle(firstHandle);
275
                DwgObject last = dwgFile.getDwgObjectFromHandle(lastHandle);
276
                if(first == null || last == null){
277
                        System.out.println("Polyline2D con vertices inicial o final a null");
278
                        return;
279
                }
280
                
281
                if(!(first instanceof DwgVertex2D)){
282
                        System.out.println("El primer vertice de Polyline2D es "+
283
                                                        first.getClass().getName());
284
                        return;
285
                }
286
                
287
                if(!(last instanceof DwgVertex2D)){
288
                        System.out.println("El primer vertice de Polyline2D es "+
289
                                                        first.getClass().getName());
290
                        return;
291
                }
292
                
293
                 int firstObjIdx = dwgFile.getIndexOf(first);
294
                 int lastObjIdx =  dwgFile.getIndexOf(last);
295
                 if(firstObjIdx == -1 || lastObjIdx == -1){
296
                         System.out.println("Calculate GIS Model: Problemas en la LinkedList: 1?="+firstObjIdx+",Ultimo="+lastObjIdx);
297
                         return;
298
                 }
299
                 
300
//                 pt = ((DwgVertex2D)first).getPoint();
301
//                 pts.add(new Point2D.Double(pt[0], pt[1]));
302
//                 bulge = ((DwgVertex2D)first).getBulge();
303
//                 bulges.add(new Double(bulge));
304
                 
305
                 for(int i = firstObjIdx; i <= lastObjIdx; i++){
306
                         DwgObject obj = dwgFile.getDwgObject(i);
307
                         if(obj instanceof DwgVertex2D){
308
                                 DwgVertex2D vertex = (DwgVertex2D) obj;
309
                                 pt = vertex.getPoint();
310
                                pts.add(new Point2D.Double(pt[0], pt[1]));
311
                                bulge = vertex.getBulge();
312
                                bulges.add(new Double(bulge));
313
                         }else{
314
                                 System.out.println("Encontrado "+obj.getClass().getName()+" en la lista de vertices de Polyline2D");
315
                         }
316
                 }//for
317
                 
318
                if (pts.size()>0) {
319
                        Point2D[] newPts = new Point2D[pts.size()];
320
                        if ((flags & 0x1)==0x1) {
321
                                newPts = new Point2D[pts.size()+1];
322
                                for (int j=0;j<pts.size();j++) {
323
                                        newPts[j] = (Point2D)pts.get(j);
324
                                }
325
                                newPts[pts.size()] = (Point2D)pts.get(0);
326
                                bulges.add(new Double(0));
327
                        } else {
328
                                for (int j=0;j<pts.size();j++) {
329
                                        newPts[j] = (Point2D)pts.get(j);
330
                                }
331
                        }
332
                        double[] bs = new double[bulges.size()];
333
                        for (int j=0;j<bulges.size();j++) {
334
                                bs[j] = ((Double)bulges.get(j)).doubleValue();
335
                        }
336
                        setBulges(bs);
337
                        Point2D[] points = GisModelCurveCalculator.
338
                                        calculateGisModelBulge(newPts, bs);
339
                        setPts(points);
340
                } else {
341
                        System.out.println("Encontrada polil?nea sin puntos ...");
342
                        // TODO: No se debe mandar nunca una polil?nea sin puntos, si esto
343
                        // ocurre es porque existe un error que hay que corregir ...
344
                }
345
        }
346
        //TODO Este metodo es antiguo y MUY INEFICIENTE
347
        //No obstante lo dejo porque hace referencia a SeqEnd y a un posible bug
348
        public void calculateGisModel(List dwgObjects) {
349
                int flags = getFlags();
350
//                En estas dos lineas de abajo y en el resto del metodo
351
//                se mantiene el mecanismo anterior al refactoring.
352
//                TODO: Pensar si deberiamos coger el handle completo.
353
//                Tal vez deberiamos tomar el handle completo y evaluar
354
//                a donde apuntan (pueden haber 2 handles con codigo y offset
355
//                distintos y que, sin embargo apunten al mismo objeto).
356
                int firstHandle = getFirstVertexHandle().getOffset();
357
                int lastHandle = getLastVertexHandle().getOffset();
358
                ArrayList pts = new ArrayList();
359
                ArrayList bulges = new ArrayList();
360
                double[] pt = new double[3];
361
                
362
                //TODO Esto cambiarlo. Es lento y poco elegante
363
                
364
                for (int j=0;j<dwgObjects.size();j++) {
365
                        DwgObject firstVertex = (DwgObject)dwgObjects.get(j);
366
                        
367
                        if (firstVertex instanceof DwgVertex2D) {
368
                                int vertexHandle = firstVertex.getHandle().getOffset();
369
                                if (vertexHandle==firstHandle) {
370
                                        int k=0;
371
                                        while (true) {
372
                                                DwgObject vertex = (DwgObject)dwgObjects.get(j+k);
373
                                                int vHandle = vertex.getHandle().getOffset();
374
                                                if (vertex instanceof DwgVertex2D) {
375
                                                        pt = ((DwgVertex2D)vertex).getPoint();
376
                                                        pts.add(new Point2D.Double(pt[0], pt[1]));
377
                                                        double bulge = ((DwgVertex2D)vertex).getBulge();
378
                                                        bulges.add(new Double(bulge));
379
                                                        k++;
380
                                                        if (vHandle==lastHandle && vertex instanceof DwgVertex2D) {
381
                                                                break;
382
                                                        }
383
                                                } else if (vertex instanceof DwgSeqend) {
384
                            // 051116, jmorell: Polil?neas_ACAD2000.dwg tiene un DwgSeqend en mitad de
385
                            //una secuencia de v?rtices. Precauci?n con esto puesto que es posible que esta
386
                            // condici?n fuera requerida en la carga de otros DWGs.
387
                            //break;
388
                            k++;
389
                                                }
390
                                        }//while
391
                                }//if first handle
392
                        }//if
393
                }//for
394
                
395
                if (pts.size()>0) {
396
                        Point2D[] newPts = new Point2D[pts.size()];
397
                        if ((flags & 0x1)==0x1) {
398
                                newPts = new Point2D[pts.size()+1];
399
                                for (int j=0;j<pts.size();j++) {
400
                                        newPts[j] = (Point2D)pts.get(j);
401
                                }
402
                                newPts[pts.size()] = (Point2D)pts.get(0);
403
                                bulges.add(new Double(0));
404
                        } else {
405
                                for (int j=0;j<pts.size();j++) {
406
                                        newPts[j] = (Point2D)pts.get(j);
407
                                }
408
                        }
409
                        double[] bs = new double[bulges.size()];
410
                        for (int j=0;j<bulges.size();j++) {
411
                                bs[j] = ((Double)bulges.get(j)).doubleValue();
412
                        }
413
                        setBulges(bs);
414
                        Point2D[] points = GisModelCurveCalculator.
415
                                        calculateGisModelBulge(newPts, bs);
416
                        setPts(points);
417
                } else {
418
//                        System.out.println("Encontrada polil?nea sin puntos ...");
419
                        // TODO: No se debe mandar nunca una polil?nea sin puntos, si esto
420
                        // ocurre es porque existe un error que hay que corregir ...
421
                }
422
        }
423
        /* (non-Javadoc)
424
         * @see com.iver.cit.jdwglib.dwg.IDwgExtrusionable#applyExtrussion()
425
         */
426
        public void applyExtrussion() {
427
                  if(getPts() == null)
428
                        return;
429
                  Point2D[] vertices = getPts();
430
              double[] polyline2DExt = getExtrusion();
431
              double elev = getElevation();
432
              double[][] polylinePoints3D = new double[vertices.length][3];
433
              for (int j=0;j<vertices.length;j++) {
434
                  polylinePoints3D[j][0] = vertices[j].getX();
435
                  polylinePoints3D[j][1] = vertices[j].getY();
436
                  polylinePoints3D[j][2] = elev;
437
                  polylinePoints3D[j] = AcadExtrusionCalculator.CalculateAcadExtrusion(polylinePoints3D[j], polyline2DExt);
438
              }
439
              setElevation(elev);
440
              for (int j=0;j<vertices.length;j++) {
441
                  vertices[j] = new Point2D.Double(polylinePoints3D[j][0], polylinePoints3D[j][1]);
442
              }
443
              setPts(vertices);
444
        }
445
        /* (non-Javadoc)
446
         * @see com.iver.cit.jdwglib.dwg.IDwg3DTestable#has3DData()
447
         */
448
        public boolean has3DData() {
449
                return (getElevation() != 0.0);
450
        }
451
        /* (non-Javadoc)
452
         * @see com.iver.cit.jdwglib.dwg.IDwg3DTestable#getZ()
453
         */
454
        public double getZ() {
455
                return getElevation();
456
        }
457
        /* (non-Javadoc)
458
         * @see com.iver.cit.jdwglib.dwg.IDwg2FMap#toFMapGeometry(boolean)
459
         */
460
        public FShape toFMapGeometry(boolean is3DFile) {
461
                FPolyline2D pline = null;
462
                Point2D[] points = getPts();
463
                double elev = getElevation();
464
                
465
                if (points != null) {
466
                        if (is3DFile) {
467
                                double[][] pline3D = new double[points.length][3];
468
                                for (int j = 0; j < points.length; j++) {
469
                                        pline3D[j][0] = points[j].getX();
470
                                        pline3D[j][1] = points[j].getY();
471
                                        pline3D[j][2] = elev;
472
                                }
473
                                pline = FMapUtil.points3DToFPolyline3D(pline3D);
474
                        } else {
475
                                pline = FMapUtil.points2DToFPolyline2D(points);
476
                        }
477
                }
478
                return pline;
479
        }
480
        /* (non-Javadoc)
481
         * @see com.iver.cit.jdwglib.dwg.IDwg2FMap#toFMapString(boolean)
482
         */
483
        public String toFMapString(boolean is3DFile) {
484
                if(is3DFile)
485
                        return "FPolyline3D";
486
                else
487
                        return "FPolyline2D";
488
        }
489
        
490
        public String toString(){
491
                return "Polyline2D";
492
        }
493
        public void transform2Block(double[] bPoint, Point2D insPoint, double[] scale, double rot, List dwgObjectsWithoutBlocks, DwgFile callBack) {
494
//                DwgPolyline2D transformedEntity = null;
495
                Point2D[] vertices = this.getPts();
496
                if (vertices != null) {
497
                    Point2D[] transformedVertices = new Point2D[vertices.length];
498
                        for (int i=0;i<vertices.length;i++) {
499
                            Point2D pointAux = new Point2D.Double(vertices[i].getX() - bPoint[0], vertices[i].getY() - bPoint[1]);
500
                                double laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot));
501
                                double laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot));
502
                                transformedVertices[i] = new Point2D.Double(laX, laY);
503
                        }
504
//                        transformedEntity = (DwgPolyline2D)this.clone();
505
                        setPts(transformedVertices);
506
                        setElevation((this.getElevation() * scale[2]));
507
                        dwgObjectsWithoutBlocks.add(this);
508
                }
509
        }
510
}