Statistics
| Revision:

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

History | View | Annotate | Download (12.7 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
 * The DwgLwPolyline class represents a DWG LwPolyline
58
 *
59
 * @author jmorell
60
 */
61
public class DwgLwPolyline extends DwgObject
62
        implements IDwgPolyline, IDwgExtrusionable,
63
        IDwg3DTestable, IDwg2FMap, IDwgBlockMember {
64

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

    
84
    public DwgLwPolyline(int index) {
85
        super(index);
86
        vertices = new ArrayList();
87
    }
88

    
89
    /**
90
     * @return Returns the bulges.
91
     */
92
    public double[] getBulges() {
93
        return bulges;
94
    }
95

    
96
    /**
97
     * @param bulges The bulges to set.
98
     */
99
    public void setBulges(double[] bulges) {
100
        this.bulges = bulges;
101
    }
102

    
103
    /**
104
     * @return Returns the flag.
105
     */
106
    public int getFlag() {
107
        return flag;
108
    }
109

    
110
    /**
111
     * @param flag The flag to set.
112
     */
113
    public void setFlag(int flag) {
114
        this.flag = flag;
115
    }
116

    
117
    /**
118
     * @return Returns the vertices.
119
     */
120
    public List getVertices() {
121
        return vertices;
122
    }
123

    
124
    /**
125
     * @param vertices The vertices to set.
126
     */
127
    public void setVertices(List vertices) {
128
        this.vertices = vertices;
129
    }
130

    
131
    /**
132
     * @return Returns the elevation.
133
     */
134
    public double getElevation() {
135
        return elevation;
136
    }
137

    
138
    /**
139
     * @param elevation The elevation to set.
140
     */
141
    public void setElevation(double elevation) {
142
        this.elevation = elevation;
143
    }
144

    
145
    /**
146
     * @return Returns the normal.
147
     */
148
    public double[] getNormal() {
149
        return normal;
150
    }
151
    /* (non-Javadoc)
152
     * @see java.lang.Object#clone()
153
     */
154
//        public Object clone() {
155
//                DwgLwPolyline dwgLwPolyline = new DwgLwPolyline(index);
156
//                dwgLwPolyline.setType(type);
157
//                dwgLwPolyline.setHandle(handle);
158
//                dwgLwPolyline.setVersion(version);
159
//                dwgLwPolyline.setMode(mode);
160
//                dwgLwPolyline.setLayerHandle(layerHandle);
161
//                dwgLwPolyline.setColor(color);
162
//                dwgLwPolyline.setNumReactors(numReactors);
163
//                dwgLwPolyline.setNoLinks(noLinks);
164
//                dwgLwPolyline.setLinetypeFlags(linetypeFlags);
165
//                dwgLwPolyline.setPlotstyleFlags(plotstyleFlags);
166
//                dwgLwPolyline.setSizeInBits(sizeInBits);
167
//                dwgLwPolyline.setExtendedData(extendedData);
168
//                dwgLwPolyline.setGraphicData(graphicData);
169
//                //dwgLwPolyline.setInsideBlock(insideBlock);
170
//                dwgLwPolyline.setFlag(flag);
171
//                dwgLwPolyline.setElevation(elevation);
172
//                dwgLwPolyline.setConstWidth(constWidth);
173
//                dwgLwPolyline.setThickness(thickness);
174
//                dwgLwPolyline.setNormal(normal);
175
//                dwgLwPolyline.setVertices(vertices);
176
//                dwgLwPolyline.setBulges(bulges);
177
//                dwgLwPolyline.setWidths(widths);
178
//                return dwgLwPolyline;
179
//        }
180

    
181
    /**
182
     * @return Returns the constWidth.
183
     */
184
    public double getConstWidth() {
185
        return constWidth;
186
    }
187

    
188
    /**
189
     * @param constWidth The constWidth to set.
190
     */
191
    public void setConstWidth(double constWidth) {
192
        this.constWidth = constWidth;
193
    }
194

    
195
    /**
196
     * @return Returns the thickness.
197
     */
198
    public double getThickness() {
199
        return thickness;
200
    }
201

    
202
    /**
203
     * @param thickness The thickness to set.
204
     */
205
    public void setThickness(double thickness) {
206
        this.thickness = thickness;
207
    }
208

    
209
    /**
210
     * @return Returns the widths.
211
     */
212
    public double[][] getWidths() {
213
        return widths;
214
    }
215

    
216
    /**
217
     * @param widths The widths to set.
218
     */
219
    public void setWidths(double[][] widths) {
220
        this.widths = widths;
221
    }
222

    
223
    /**
224
     * @param normal The normal to set.
225
     */
226
    public void setNormal(double[] normal) {
227
        this.normal = normal;
228
    }
229
    /* (non-Javadoc)
230
     * @see com.iver.cit.jdwglib.dwg.IDwgPolyline#calculateGisModel(java.util.List)
231
     */
232

    
233
    public void calculateGisModel(DwgFile dwgFile) {
234
        if (getVertices() == null) {
235
            return;
236
        }
237
        int flags = getFlag();
238
        List pts = getVertices();
239
        double[] bulges = getBulges();
240
        List newPts = new ArrayList();
241
        double[] newBulges = null;
242
        if (bulges != null) {
243
            newBulges = new double[bulges.length];
244
            System.arraycopy(bulges, 0, newBulges, 0, bulges.length);
245
        } else {
246
            bulges = new double[pts.size()];
247
            newBulges = new double[bulges.length];
248
                        //dwg spec says numVertex (numSegments + 1)
249
            //TODO Check this
250
            for (int i = 0; i < newBulges.length; i++) {
251
                bulges[i] = 0d;
252
            }
253
            newBulges = new double[bulges.length];
254
            System.arraycopy(bulges, 0, newBulges, 0, bulges.length);
255
        }
256
        // TODO: Aqu� pueden existir casos no contemplados ...
257
//        System.out.println("flags = " + flags);
258
        if (flags == 512 || flags == 776 || flags == 768) {//closed
259
            newBulges = new double[bulges.length + 1];
260
            for (int j = 0; j < pts.size(); j++) {
261
                newPts.add(pts.get(j));
262
            }
263
            newPts.add(pts.get(0));
264
            newBulges[pts.size()] = 0;
265
        } else {
266
            for (int j = 0; j < pts.size(); j++) {
267
                newPts.add(pts.get(j));
268
            }
269
        }
270
        if (newPts.size() > 0) {
271
            setBulges(newBulges);
272
            List points = GisModelCurveCalculator.calculateGisModelBulge(newPts, newBulges);
273
            setVertices(points);
274
        }
275
//                if (pts.size() > 0) {
276
//                        setBulges(newBulges);
277
//                        List points = GisModelCurveCalculator.calculateGisModelBulge(newPts, newBulges);
278
//                        setVertices(points);
279
//                }
280
//                else {
281
////                        System.out.println("Encontrada polil�nea sin puntos ...");
282
//                        // TODO: No se debe mandar nunca una polil�nea sin puntos, si esto
283
//                        // ocurre es porque existe un error que hay que corregir ...
284
//                }
285
    }
286
    /* (non-Javadoc)
287
     * @see com.iver.cit.jdwglib.dwg.IDwgExtrusionable#applyExtrussion()
288
     */
289

    
290
    public void applyExtrussion() {
291
        if (getVertices() == null) {
292
            return;
293
        }
294
        List vertices = getVertices();
295
        double[] lwPolylineExt = getNormal();
296
        // Normals and Extrusions aren`t the same
297
        if (lwPolylineExt[0] == 0 && lwPolylineExt[1] == 0 && lwPolylineExt[2] == 0) {
298
            lwPolylineExt[2] = 1.0;
299
        }
300

    
301
        double elev = getElevation();
302
        List lwPolylinePoints3D = new ArrayList();
303
        for (int j = 0; j < vertices.size(); j++) {
304
            double[] point = new double[3];
305
            point[0] = ((double[]) vertices.get(j))[0];
306
            point[1] = ((double[]) vertices.get(j))[1];
307
            point[2] = elev;
308
            lwPolylinePoints3D.add(AcadExtrusionCalculator.
309
                    extrude2(point, lwPolylineExt));
310
        }
311
        setElevation(elev);
312
        List newVertices = new ArrayList();
313
        for (int j = 0; j < vertices.size(); j++) {
314
            double[] point = (double[]) lwPolylinePoints3D.get(j);
315
            newVertices.add(new double[]{point[0], point[1]});
316
        }
317
        setVertices(newVertices);
318
    }
319
    /* (non-Javadoc)
320
     * @see com.iver.cit.jdwglib.dwg.IDwg3DTestable#has3DData()
321
     */
322

    
323
    public boolean has3DData() {
324
        return getElevation() != 0.0;
325
    }
326

    
327
    public double getZ() {
328
        return getElevation();
329
    }
330

    
331
    public Geometry toFMapGeometry(boolean is3DFile) throws CreateGeometryException {
332
        return FMapUtil.ptsToLine(getVertices(),
333
                getGeometrySubType(is3DFile));
334
    }
335

    
336
    public String toFMapString(boolean is3DFile) {
337
        if (is3DFile) {
338
            return "FPolyline3D";
339
        } else {
340
            return "FPolyline2D";
341
        }
342
    }
343

    
344
    public String toString() {
345
        return "LwPolyline";
346
    }
347

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

    
381
    public Object clone() {
382
        DwgLwPolyline dwgLwPolyline = new DwgLwPolyline(index);
383
        this.fill(dwgLwPolyline);
384
        return dwgLwPolyline;
385
    }
386

    
387
    protected void fill(DwgObject obj) {
388
        super.fill(obj);
389
        DwgLwPolyline myObj = (DwgLwPolyline) obj;
390

    
391
        myObj.setBulges(bulges);
392
        myObj.setConstWidth(constWidth);
393
        myObj.setElevation(elevation);
394
        myObj.setFlag(flag);
395
        myObj.setNormal(normal);
396
        myObj.setThickness(thickness);
397
        myObj.setVertices(vertices);
398
        myObj.setWidths(widths);
399

    
400
    }
401

    
402
    public void addVertex(IDwgVertex vertex) {
403
        vertices.add(vertex.getPoint());
404

    
405
    }
406

    
407
    public int getGeometryType() {
408
        return Geometry.TYPES.MULTICURVE;
409
    }
410

    
411
}