Statistics
| Revision:

root / trunk / libraries / libDwg / src / com / iver / cit / jdwglib / dwg / objects / DwgLwPolyline.java @ 10820

History | View | Annotate | Download (11.5 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
import java.util.Map;
41

    
42
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
43
import com.iver.cit.gvsig.fmap.core.IGeometry;
44
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
45
import com.iver.cit.jdwglib.dwg.DwgFile;
46
import com.iver.cit.jdwglib.dwg.DwgObject;
47
import com.iver.cit.jdwglib.dwg.IDwg2FMap;
48
import com.iver.cit.jdwglib.dwg.IDwg3DTestable;
49
import com.iver.cit.jdwglib.dwg.IDwgBlockMember;
50
import com.iver.cit.jdwglib.dwg.IDwgExtrusionable;
51
import com.iver.cit.jdwglib.dwg.IDwgPolyline;
52
import com.iver.cit.jdwglib.dwg.IDwgVertex;
53
import com.iver.cit.jdwglib.util.AcadExtrusionCalculator;
54
import com.iver.cit.jdwglib.util.FMapUtil;
55
import com.iver.cit.jdwglib.util.GisModelCurveCalculator;
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
                int flags = getFlag();
222
                List pts = getVertices();
223
                double[] bulges = getBulges();
224
                List newPts = new ArrayList();
225
                double[] newBulges = null;
226
                if(bulges != null){
227
                        newBulges = new double[bulges.length];
228
                        System.arraycopy(bulges, 0, newBulges, 0, bulges.length);
229
                }else{
230
                        bulges = new double[pts.size() ];
231
                        newBulges = new double[bulges.length];
232
                        //dwg spec says numVertex (numSegments + 1)
233
                        //TODO Check this
234
                        for(int i = 0; i < newBulges.length; i++)
235
                                bulges[i] = 0d;
236
                        newBulges = new double[bulges.length];
237
                        System.arraycopy(bulges, 0, newBulges, 0, bulges.length);
238
                }
239
                // TODO: Aqu? pueden existir casos no contemplados ...
240
//        System.out.println("flags = " + flags);
241
        if (flags==512 || flags==776 || flags==768) {//closed
242
                        newBulges = new double[bulges.length+1];
243
                        for (int j=0;j<pts.size();j++) {
244
                                newPts.add(pts.get(j));
245
                        }
246
                        newPts.add(pts.get(0));
247
                        newBulges[pts.size()] = 0;
248
                } else {
249
                        for (int j=0;j<pts.size();j++) {
250
                                newPts.add(pts.get(j));
251
                        }
252
                }
253
                if (newPts.size() > 0) {
254
                        setBulges(newBulges);
255
                        List points = GisModelCurveCalculator.calculateGisModelBulge(newPts, newBulges);
256
                        setVertices(points);
257
                } 
258
//                if (pts.size() > 0) {
259
//                        setBulges(newBulges);
260
//                        List points = GisModelCurveCalculator.calculateGisModelBulge(newPts, newBulges);
261
//                        setVertices(points);
262
//                } 
263
//                else {
264
////                        System.out.println("Encontrada polil?nea sin puntos ...");
265
//                        // TODO: No se debe mandar nunca una polil?nea sin puntos, si esto
266
//                        // ocurre es porque existe un error que hay que corregir ...
267
//                }
268
        }
269
        /* (non-Javadoc)
270
         * @see com.iver.cit.jdwglib.dwg.IDwgExtrusionable#applyExtrussion()
271
         */
272
        public void applyExtrussion() {
273
                if (getVertices() == null)
274
                        return;
275
                 List vertices = getVertices();
276
         double[] lwPolylineExt = getNormal();
277
         // Normals and Extrusions aren`t the same
278
         if (lwPolylineExt[0]==0 && lwPolylineExt[1]==0 && lwPolylineExt[2]==0) 
279
                 lwPolylineExt[2] = 1.0;
280
         
281
         double elev = getElevation();
282
         List lwPolylinePoints3D = new ArrayList();
283
         for (int j=0;j<vertices.size();j++) {
284
                 double[] point = new double[3];
285
             point[0] = ((double[])vertices.get(j))[0];
286
             point[1] = ((double[])vertices.get(j))[1];
287
             point[2] = elev;
288
             lwPolylinePoints3D.add(AcadExtrusionCalculator.
289
                             extrude2(point, lwPolylineExt));
290
         }
291
         setElevation(elev);
292
         List newVertices = new ArrayList();
293
         for (int j=0;j<vertices.size();j++) {
294
                 double[] point = (double[]) lwPolylinePoints3D.get(j);
295
             newVertices.add(new double[]{point[0], point[1]});
296
         }
297
         setVertices(newVertices);
298
        }
299
        /* (non-Javadoc)
300
         * @see com.iver.cit.jdwglib.dwg.IDwg3DTestable#has3DData()
301
         */
302
        public boolean has3DData() {
303
                return getElevation() != 0.0;
304
        }
305
        public double getZ() {
306
                return getElevation();
307
        }
308
        public IGeometry toFMapGeometry(boolean is3DFile) {
309
                FPolyline2D lwpline = null;
310
                List pts = getVertices();
311
                double elev = getElevation();
312
                if (pts != null && pts.size() > 0) {
313
                        
314
                        if (is3DFile) {
315
                                List pline3D = new ArrayList();
316
                                for (int j = 0; j < pts.size(); j++) {
317
                                        Object vertex = pts.get(j);
318
                                        double[] pt = new double[3];
319
                                        if(vertex instanceof double[]){
320
                                                double[] vertexArray = (double[])vertex;
321
                                                pt[0] = vertexArray[0];
322
                                                pt[1] = vertexArray[1];
323
                                        }else if(vertex instanceof Point2D){
324
                                                Point2D vertexPt = (Point2D)vertex;
325
                                                pt[0] = vertexPt.getX();
326
                                                pt[1] = vertexPt.getY();
327
                                        }
328
                                        pt[2] = elev;
329
                                        pline3D.add(pt);
330
                                }
331
                                lwpline = FMapUtil.points3DToFPolyline3D(pline3D);
332
                        } else {
333
                                lwpline = FMapUtil.points2DToFPolyline2D(pts);
334
                        }
335
                        
336
                }
337
                return ShapeFactory.createGeometry(lwpline);
338
        }
339
        public String toFMapString(boolean is3DFile) {
340
                if(is3DFile)
341
                        return "FPolyline3D";
342
                else
343
                        return "FPolyline2D";
344
        }
345
        
346
        public String toString(){
347
                return "LwPolyline";
348
        }
349
        
350
        
351
        public void transform2Block(double[] bPoint, Point2D insPoint,
352
                        double[] scale, double rot, 
353
                        List dwgObjectsWithoutBlocks, 
354
                        Map handleObjWithoutBlocks, DwgFile callBack) {
355
                DwgLwPolyline transformedEntity = null;
356
                List vertices = this.getVertices();
357
                if (vertices!=null) {
358
                    List transformedVertices = new ArrayList();
359
                        for (int i=0; i< vertices.size(); i++) {
360
                                double[] point = (double[]) vertices.get(i);
361
                                double[] pointAux = new double[]{point[0] - bPoint[0], 
362
                                                        point[1] - bPoint[1]};
363
                                double laX = insPoint.getX() + ((pointAux[0]*scale[0])*Math.cos(rot) + (pointAux[1]*scale[1])*(-1)*Math.sin(rot));
364
                                double laY = insPoint.getY() + ((pointAux[0]*scale[0])*Math.sin(rot) + (pointAux[1]*scale[1])*Math.cos(rot));
365
                                transformedVertices.add(new double[]{laX, laY});
366
                        }
367
                        transformedEntity = (DwgLwPolyline)this.clone();
368
                        transformedEntity.setVertices(transformedVertices);
369
                        transformedEntity.setElevation((this.getElevation() * scale[2]));
370
                        transformedEntity.setHandle(this.getHandle());
371
                        dwgObjectsWithoutBlocks.add(transformedEntity);
372
                        handleObjWithoutBlocks.put(new Integer(transformedEntity.getHandle().getOffset()), transformedEntity);
373
//                        dwgObjectsWithoutBlocks.add(this);
374
                }//if
375
        }
376
        
377
        public Object clone(){
378
                DwgLwPolyline dwgLwPolyline = new DwgLwPolyline(index);
379
                this.fill(dwgLwPolyline);
380
                return dwgLwPolyline;
381
        }
382
        
383
        protected void fill(DwgObject obj){
384
                super.fill(obj);
385
                DwgLwPolyline myObj = (DwgLwPolyline)obj;
386

    
387
                myObj.setBulges(bulges);
388
                myObj.setConstWidth(constWidth);
389
                myObj.setElevation(elevation);
390
                myObj.setFlag(flag);
391
                myObj.setNormal(normal);
392
                myObj.setThickness(thickness);
393
                myObj.setVertices(vertices);
394
                myObj.setWidths(widths);
395

    
396
        }
397
        public void addVertex(IDwgVertex vertex) {
398
                vertices.add(vertex.getPoint());
399
                
400
        }
401

    
402
}