Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libDwg / src / org / gvsig / dwg / lib / objects / DwgLwPolyline.java @ 28969

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 org.gvsig.dwg.lib.objects;
36

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

    
42
import org.gvsig.dwg.lib.DwgFile;
43
import org.gvsig.dwg.lib.DwgObject;
44
import org.gvsig.dwg.lib.IDwg2FMap;
45
import org.gvsig.dwg.lib.IDwg3DTestable;
46
import org.gvsig.dwg.lib.IDwgBlockMember;
47
import org.gvsig.dwg.lib.IDwgExtrusionable;
48
import org.gvsig.dwg.lib.IDwgPolyline;
49
import org.gvsig.dwg.lib.IDwgVertex;
50
import org.gvsig.dwg.lib.util.AcadExtrusionCalculator;
51
import org.gvsig.dwg.lib.util.FMapUtil;
52
import org.gvsig.dwg.lib.util.GisModelCurveCalculator;
53
import org.gvsig.fmap.geom.Geometry;
54
import org.gvsig.fmap.geom.GeometryFactory;
55
import org.gvsig.fmap.geom.GeometryManager;
56
import org.gvsig.fmap.geom.primitive.GeneralPathX;
57
import org.gvsig.fmap.geom.primitive.NullGeometry;
58

    
59

    
60
/**
61
 * The DwgLwPolyline class represents a DWG LwPolyline
62
 *
63
 * @author jmorell
64
 */
65
public class DwgLwPolyline extends DwgObject
66
        implements IDwgPolyline, IDwgExtrusionable,
67
        IDwg3DTestable, IDwg2FMap, IDwgBlockMember{
68

    
69
        private int flag;
70
        private double constWidth;
71
        private double elevation;
72
        private double thickness;
73
        private double[] normal;
74
        private List vertices;
75
        /*
76
         *The bulge is the tangent of 1/4 of the included angle for the arc
77
         *between the selected vertex and the next vertex in the polyline's vertex list.
78
         *A negative bulge value indicates that the arc goes clockwise from
79
         *the selected vertex to the next vertex.
80
         *A bulge of 0 indicates a straight segment,
81
         *and a bulge of 1 is a semicircle.
82
         *
83
         *http://www.faqs.org/faqs/CAD/autolisp-faq/part2/section-6.html
84
         */
85
        private double[] bulges;
86
        private double[][] widths;
87

    
88

    
89
        public DwgLwPolyline(int index) {
90
                super(index);
91
                vertices = new ArrayList();
92
        }
93
        /**
94
         * @return Returns the bulges.
95
         */
96
        public double[] getBulges() {
97
                return bulges;
98
        }
99
        /**
100
         * @param bulges The bulges to set.
101
         */
102
        public void setBulges(double[] bulges) {
103
                this.bulges = bulges;
104
        }
105
        /**
106
         * @return Returns the flag.
107
         */
108
        public int getFlag() {
109
                return flag;
110
        }
111
        /**
112
         * @param flag The flag to set.
113
         */
114
        public void setFlag(int flag) {
115
                this.flag = flag;
116
        }
117
        /**
118
         * @return Returns the vertices.
119
         */
120
        public List getVertices() {
121
                return vertices;
122
        }
123
        /**
124
         * @param vertices The vertices to set.
125
         */
126
        public void setVertices(List vertices) {
127
                this.vertices = vertices;
128
        }
129
    /**
130
     * @return Returns the elevation.
131
     */
132
    public double getElevation() {
133
        return elevation;
134
    }
135
    /**
136
     * @param elevation The elevation to set.
137
     */
138
    public void setElevation(double elevation) {
139
        this.elevation = elevation;
140
    }
141
    /**
142
     * @return Returns the normal.
143
     */
144
    public double[] getNormal() {
145
        return normal;
146
    }
147
        /* (non-Javadoc)
148
         * @see java.lang.Object#clone()
149
         */
150
//        public Object clone() {
151
//                DwgLwPolyline dwgLwPolyline = new DwgLwPolyline(index);
152
//                dwgLwPolyline.setType(type);
153
//                dwgLwPolyline.setHandle(handle);
154
//                dwgLwPolyline.setVersion(version);
155
//                dwgLwPolyline.setMode(mode);
156
//                dwgLwPolyline.setLayerHandle(layerHandle);
157
//                dwgLwPolyline.setColor(color);
158
//                dwgLwPolyline.setNumReactors(numReactors);
159
//                dwgLwPolyline.setNoLinks(noLinks);
160
//                dwgLwPolyline.setLinetypeFlags(linetypeFlags);
161
//                dwgLwPolyline.setPlotstyleFlags(plotstyleFlags);
162
//                dwgLwPolyline.setSizeInBits(sizeInBits);
163
//                dwgLwPolyline.setExtendedData(extendedData);
164
//                dwgLwPolyline.setGraphicData(graphicData);
165
//                //dwgLwPolyline.setInsideBlock(insideBlock);
166
//                dwgLwPolyline.setFlag(flag);
167
//                dwgLwPolyline.setElevation(elevation);
168
//                dwgLwPolyline.setConstWidth(constWidth);
169
//                dwgLwPolyline.setThickness(thickness);
170
//                dwgLwPolyline.setNormal(normal);
171
//                dwgLwPolyline.setVertices(vertices);
172
//                dwgLwPolyline.setBulges(bulges);
173
//                dwgLwPolyline.setWidths(widths);
174
//                return dwgLwPolyline;
175
//        }
176
        /**
177
         * @return Returns the constWidth.
178
         */
179
        public double getConstWidth() {
180
                return constWidth;
181
        }
182
        /**
183
         * @param constWidth The constWidth to set.
184
         */
185
        public void setConstWidth(double constWidth) {
186
                this.constWidth = constWidth;
187
        }
188
        /**
189
         * @return Returns the thickness.
190
         */
191
        public double getThickness() {
192
                return thickness;
193
        }
194
        /**
195
         * @param thickness The thickness to set.
196
         */
197
        public void setThickness(double thickness) {
198
                this.thickness = thickness;
199
        }
200
        /**
201
         * @return Returns the widths.
202
         */
203
        public double[][] getWidths() {
204
                return widths;
205
        }
206
        /**
207
         * @param widths The widths to set.
208
         */
209
        public void setWidths(double[][] widths) {
210
                this.widths = widths;
211
        }
212
        /**
213
         * @param normal The normal to set.
214
         */
215
        public void setNormal(double[] normal) {
216
                this.normal = normal;
217
        }
218
        /* (non-Javadoc)
219
         * @see com.iver.cit.jdwglib.dwg.IDwgPolyline#calculateGisModel(java.util.List)
220
         */
221
        public void calculateGisModel(DwgFile dwgFile) {
222
                if (getVertices() == null)
223
                        return;
224
                int flags = getFlag();
225
                List pts = getVertices();
226
                double[] bulges = getBulges();
227
                List newPts = new ArrayList();
228
                double[] newBulges = null;
229
                if(bulges != null){
230
                        newBulges = new double[bulges.length];
231
                        System.arraycopy(bulges, 0, newBulges, 0, bulges.length);
232
                }else{
233
                        bulges = new double[pts.size() ];
234
                        newBulges = new double[bulges.length];
235
                        //dwg spec says numVertex (numSegments + 1)
236
                        //TODO Check this
237
                        for(int i = 0; i < newBulges.length; i++)
238
                                bulges[i] = 0d;
239
                        newBulges = new double[bulges.length];
240
                        System.arraycopy(bulges, 0, newBulges, 0, bulges.length);
241
                }
242
                // TODO: Aqu� pueden existir casos no contemplados ...
243
//        System.out.println("flags = " + flags);
244
        if (flags==512 || flags==776 || flags==768) {//closed
245
                        newBulges = new double[bulges.length+1];
246
                        for (int j=0;j<pts.size();j++) {
247
                                newPts.add(pts.get(j));
248
                        }
249
                        newPts.add(pts.get(0));
250
                        newBulges[pts.size()] = 0;
251
                } else {
252
                        for (int j=0;j<pts.size();j++) {
253
                                newPts.add(pts.get(j));
254
                        }
255
                }
256
                if (newPts.size() > 0) {
257
                        setBulges(newBulges);
258
                        List points = GisModelCurveCalculator.calculateGisModelBulge(newPts, newBulges);
259
                        setVertices(points);
260
                }
261
//                if (pts.size() > 0) {
262
//                        setBulges(newBulges);
263
//                        List points = GisModelCurveCalculator.calculateGisModelBulge(newPts, newBulges);
264
//                        setVertices(points);
265
//                }
266
//                else {
267
////                        System.out.println("Encontrada polil�nea sin puntos ...");
268
//                        // TODO: No se debe mandar nunca una polil�nea sin puntos, si esto
269
//                        // ocurre es porque existe un error que hay que corregir ...
270
//                }
271
        }
272
        /* (non-Javadoc)
273
         * @see com.iver.cit.jdwglib.dwg.IDwgExtrusionable#applyExtrussion()
274
         */
275
        public void applyExtrussion() {
276
                if (getVertices() == null)
277
                        return;
278
                 List vertices = getVertices();
279
         double[] lwPolylineExt = getNormal();
280
         // Normals and Extrusions aren`t the same
281
         if (lwPolylineExt[0]==0 && lwPolylineExt[1]==0 && lwPolylineExt[2]==0)
282
                 lwPolylineExt[2] = 1.0;
283

    
284
         double elev = getElevation();
285
         List lwPolylinePoints3D = new ArrayList();
286
         for (int j=0;j<vertices.size();j++) {
287
                 double[] point = new double[3];
288
             point[0] = ((double[])vertices.get(j))[0];
289
             point[1] = ((double[])vertices.get(j))[1];
290
             point[2] = elev;
291
             lwPolylinePoints3D.add(AcadExtrusionCalculator.
292
                             extrude2(point, lwPolylineExt));
293
         }
294
         setElevation(elev);
295
         List newVertices = new ArrayList();
296
         for (int j=0;j<vertices.size();j++) {
297
                 double[] point = (double[]) lwPolylinePoints3D.get(j);
298
             newVertices.add(new double[]{point[0], point[1]});
299
         }
300
         setVertices(newVertices);
301
        }
302
        /* (non-Javadoc)
303
         * @see com.iver.cit.jdwglib.dwg.IDwg3DTestable#has3DData()
304
         */
305
        public boolean has3DData() {
306
                return getElevation() != 0.0;
307
        }
308
        public double getZ() {
309
                return getElevation();
310
        }
311
        public Geometry toFMapGeometry(boolean is3DFile) {
312
                int index = this.getIndex();
313
                GeometryFactory gFactory = GeometryManager.getInstance().getGeometryFactory();
314
                List vertices = getVertices();
315
                double elev = getElevation();
316

    
317
                if (vertices != null && vertices.size()>0){
318
                        GeneralPathX gp = new GeneralPathX();
319
                        Object vertice = vertices.get(0);
320
                        if(vertice instanceof double[]){
321
                                gp.moveTo(new Double(((double[])vertice)[0]).floatValue(),new Double(((double[])vertice)[1]).floatValue());
322
                        } else if (vertice instanceof Point2D){
323
                                gp.moveTo(new Double(((Point2D)vertice).getX()).floatValue(),
324
                                                new Double(((Point2D)vertice).getY()).floatValue());
325
                        }
326
                        for (int i = 1; i < vertices.size(); i++) {
327
                                vertice = vertices.get(i);
328
                                if(vertice instanceof double[]){
329
                                        gp.lineTo(new Double(((double[])vertice)[0]).floatValue(),new Double(((double[])vertice)[1]).floatValue());
330
                                } else if (vertice instanceof Point2D){
331
                                        gp.lineTo(new Double(((Point2D)vertice).getX()).floatValue(),
332
                                                        new Double(((Point2D)vertice).getY()).floatValue());
333
                                }
334
                        }
335
                        if (is3DFile) {
336
                                return gFactory.createPolyline3D(gp,new double[] {elev});
337
                        } else {
338
                                return gFactory.createPolyline2D(gp);
339
                        }
340
                } else {
341
                        return new NullGeometry();
342
                }
343
        }
344
        public String toFMapString(boolean is3DFile) {
345
                if(is3DFile)
346
                        return "FPolyline3D";
347
                else
348
                        return "FPolyline2D";
349
        }
350

    
351
        public String toString(){
352
                return "LwPolyline";
353
        }
354

    
355

    
356
        public void transform2Block(double[] bPoint, Point2D insPoint,
357
                        double[] scale, double rot,
358
                        List dwgObjectsWithoutBlocks,
359
                        Map handleObjWithoutBlocks, DwgFile callBack) {
360
                DwgLwPolyline transformedEntity = null;
361
                List vertices = this.getVertices();
362
                Object vertice;
363
                double[] point;
364
                if (vertices!=null) {
365
                    List transformedVertices = new ArrayList();
366
                        for (int i=0; i< vertices.size(); i++) {
367
                                vertice = vertices.get(i);
368
                                if (vertice instanceof Point2D.Double){
369
                                        point = new double[] {((Point2D.Double)vertices.get(i)).x, ((Point2D.Double)vertices.get(i)).y};
370
                                } else {
371
                                        point = (double[]) vertices.get(i);
372
                                }
373
                                double[] pointAux = new double[]{point[0] - bPoint[0],
374
                                                        point[1] - bPoint[1]};
375
                                double laX = insPoint.getX() + ((pointAux[0]*scale[0])*Math.cos(rot) + (pointAux[1]*scale[1])*(-1)*Math.sin(rot));
376
                                double laY = insPoint.getY() + ((pointAux[0]*scale[0])*Math.sin(rot) + (pointAux[1]*scale[1])*Math.cos(rot));
377
                                transformedVertices.add(new double[]{laX, laY});
378
                        }
379
                        transformedEntity = (DwgLwPolyline)this.clone();
380
                        transformedEntity.setVertices(transformedVertices);
381
                        transformedEntity.setElevation((this.getElevation() * scale[2]));
382
                        transformedEntity.setHandle(this.getHandle());
383
                        dwgObjectsWithoutBlocks.add(transformedEntity);
384
                        handleObjWithoutBlocks.put(new Integer(transformedEntity.getHandle().getOffset()), transformedEntity);
385
//                        dwgObjectsWithoutBlocks.add(this);
386
                }//if
387
        }
388

    
389
        public Object clone(){
390
                DwgLwPolyline dwgLwPolyline = new DwgLwPolyline(index);
391
                this.fill(dwgLwPolyline);
392
                return dwgLwPolyline;
393
        }
394

    
395
        protected void fill(DwgObject obj){
396
                super.fill(obj);
397
                DwgLwPolyline myObj = (DwgLwPolyline)obj;
398

    
399
                myObj.setBulges(bulges);
400
                myObj.setConstWidth(constWidth);
401
                myObj.setElevation(elevation);
402
                myObj.setFlag(flag);
403
                myObj.setNormal(normal);
404
                myObj.setThickness(thickness);
405
                myObj.setVertices(vertices);
406
                myObj.setWidths(widths);
407

    
408
        }
409
        public void addVertex(IDwgVertex vertex) {
410
                vertices.add(vertex.getPoint());
411

    
412
        }
413

    
414
}