Statistics
| Revision:

root / trunk / org.gvsig.dwg / org.gvsig.dwg.lib / src / main / java / org / gvsig / dwg / lib / objects / DwgLwPolyline.java @ 74

History | View | Annotate | Download (11.4 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.exception.CreateGeometryException;
55

    
56

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

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

    
85

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

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

    
313
        public Geometry toFMapGeometry(boolean is3DFile) throws CreateGeometryException {
314
                return FMapUtil.ptsToMultiLine(getVertices(),
315
                                getGeometrySubType(is3DFile));
316
        }
317

    
318
        public String toFMapString(boolean is3DFile) {
319
                if(is3DFile) {
320
                        return "FPolyline3D";
321
                } else {
322
                        return "FPolyline2D";
323
                }
324
        }
325

    
326
        public String toString(){
327
                return "LwPolyline";
328
        }
329

    
330

    
331
        public void transform2Block(double[] bPoint, Point2D insPoint,
332
                        double[] scale, double rot,
333
                        List dwgObjectsWithoutBlocks,
334
                        Map handleObjWithoutBlocks, DwgFile callBack) {
335
                DwgLwPolyline transformedEntity = null;
336
                List vertices = this.getVertices();
337
                Object vertice;
338
                double[] point;
339
                if (vertices!=null) {
340
                    List transformedVertices = new ArrayList();
341
                        for (int i=0; i< vertices.size(); i++) {
342
                                vertice = vertices.get(i);
343
                                if (vertice instanceof Point2D.Double){
344
                                        point = new double[] {((Point2D.Double)vertices.get(i)).x, ((Point2D.Double)vertices.get(i)).y};
345
                                } else {
346
                                        point = (double[]) vertices.get(i);
347
                                }
348
                                double[] pointAux = new double[]{point[0] - bPoint[0],
349
                                                        point[1] - bPoint[1]};
350
                                double laX = insPoint.getX() + ((pointAux[0]*scale[0])*Math.cos(rot) + (pointAux[1]*scale[1])*(-1)*Math.sin(rot));
351
                                double laY = insPoint.getY() + ((pointAux[0]*scale[0])*Math.sin(rot) + (pointAux[1]*scale[1])*Math.cos(rot));
352
                                transformedVertices.add(new double[]{laX, laY});
353
                        }
354
                        transformedEntity = (DwgLwPolyline)this.clone();
355
                        transformedEntity.setVertices(transformedVertices);
356
                        transformedEntity.setElevation((this.getElevation() * scale[2]));
357
                        transformedEntity.setHandle(this.getHandle());
358
                        dwgObjectsWithoutBlocks.add(transformedEntity);
359
                        handleObjWithoutBlocks.put(new Integer(transformedEntity.getHandle().getOffset()), transformedEntity);
360
//                        dwgObjectsWithoutBlocks.add(this);
361
                }//if
362
        }
363

    
364
        public Object clone(){
365
                DwgLwPolyline dwgLwPolyline = new DwgLwPolyline(index);
366
                this.fill(dwgLwPolyline);
367
                return dwgLwPolyline;
368
        }
369

    
370
        protected void fill(DwgObject obj){
371
                super.fill(obj);
372
                DwgLwPolyline myObj = (DwgLwPolyline)obj;
373

    
374
                myObj.setBulges(bulges);
375
                myObj.setConstWidth(constWidth);
376
                myObj.setElevation(elevation);
377
                myObj.setFlag(flag);
378
                myObj.setNormal(normal);
379
                myObj.setThickness(thickness);
380
                myObj.setVertices(vertices);
381
                myObj.setWidths(widths);
382

    
383
        }
384
        public void addVertex(IDwgVertex vertex) {
385
                vertices.add(vertex.getPoint());
386

    
387
        }
388

    
389
        public int getGeometryType() {
390
                return Geometry.TYPES.MULTICURVE;
391
        }
392

    
393
}