Statistics
| Revision:

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

History | View | Annotate | Download (11.2 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.FShape;
44
import com.iver.cit.jdwglib.dwg.DwgFile;
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.dwg.IDwgVertex;
52
import com.iver.cit.jdwglib.util.AcadExtrusionCalculator;
53
import com.iver.cit.jdwglib.util.FMapUtil;
54
import com.iver.cit.jdwglib.util.GisModelCurveCalculator;
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
        
85
        public DwgLwPolyline(int index) {
86
                super(index);
87
                vertices = new ArrayList();
88
        }
89
        /**
90
         * @return Returns the bulges.
91
         */
92
        public double[] getBulges() {
93
                return bulges;
94
        }
95
        /**
96
         * @param bulges The bulges to set.
97
         */
98
        public void setBulges(double[] bulges) {
99
                this.bulges = bulges;
100
        }
101
        /**
102
         * @return Returns the flag.
103
         */
104
        public int getFlag() {
105
                return flag;
106
        }
107
        /**
108
         * @param flag The flag to set.
109
         */
110
        public void setFlag(int flag) {
111
                this.flag = flag;
112
        }
113
        /**
114
         * @return Returns the vertices.
115
         */
116
        public List getVertices() {
117
                return vertices;
118
        }
119
        /**
120
         * @param vertices The vertices to set.
121
         */
122
        public void setVertices(List vertices) {
123
                this.vertices = vertices;
124
        }
125
    /**
126
     * @return Returns the elevation.
127
     */
128
    public double getElevation() {
129
        return elevation;
130
    }
131
    /**
132
     * @param elevation The elevation to set.
133
     */
134
    public void setElevation(double elevation) {
135
        this.elevation = elevation;
136
    }
137
    /**
138
     * @return Returns the normal.
139
     */
140
    public double[] getNormal() {
141
        return normal;
142
    }
143
        /* (non-Javadoc)
144
         * @see java.lang.Object#clone()
145
         */
146
//        public Object clone() {
147
//                DwgLwPolyline dwgLwPolyline = new DwgLwPolyline(index);
148
//                dwgLwPolyline.setType(type);
149
//                dwgLwPolyline.setHandle(handle);
150
//                dwgLwPolyline.setVersion(version);
151
//                dwgLwPolyline.setMode(mode);
152
//                dwgLwPolyline.setLayerHandle(layerHandle);
153
//                dwgLwPolyline.setColor(color);
154
//                dwgLwPolyline.setNumReactors(numReactors);
155
//                dwgLwPolyline.setNoLinks(noLinks);
156
//                dwgLwPolyline.setLinetypeFlags(linetypeFlags);
157
//                dwgLwPolyline.setPlotstyleFlags(plotstyleFlags);
158
//                dwgLwPolyline.setSizeInBits(sizeInBits);
159
//                dwgLwPolyline.setExtendedData(extendedData);
160
//                dwgLwPolyline.setGraphicData(graphicData);
161
//                //dwgLwPolyline.setInsideBlock(insideBlock);
162
//                dwgLwPolyline.setFlag(flag);
163
//                dwgLwPolyline.setElevation(elevation);
164
//                dwgLwPolyline.setConstWidth(constWidth);
165
//                dwgLwPolyline.setThickness(thickness);
166
//                dwgLwPolyline.setNormal(normal);
167
//                dwgLwPolyline.setVertices(vertices);
168
//                dwgLwPolyline.setBulges(bulges);
169
//                dwgLwPolyline.setWidths(widths);
170
//                return dwgLwPolyline;
171
//        }
172
        /**
173
         * @return Returns the constWidth.
174
         */
175
        public double getConstWidth() {
176
                return constWidth;
177
        }
178
        /**
179
         * @param constWidth The constWidth to set.
180
         */
181
        public void setConstWidth(double constWidth) {
182
                this.constWidth = constWidth;
183
        }
184
        /**
185
         * @return Returns the thickness.
186
         */
187
        public double getThickness() {
188
                return thickness;
189
        }
190
        /**
191
         * @param thickness The thickness to set.
192
         */
193
        public void setThickness(double thickness) {
194
                this.thickness = thickness;
195
        }
196
        /**
197
         * @return Returns the widths.
198
         */
199
        public double[][] getWidths() {
200
                return widths;
201
        }
202
        /**
203
         * @param widths The widths to set.
204
         */
205
        public void setWidths(double[][] widths) {
206
                this.widths = widths;
207
        }
208
        /**
209
         * @param normal The normal to set.
210
         */
211
        public void setNormal(double[] normal) {
212
                this.normal = normal;
213
        }
214
        /* (non-Javadoc)
215
         * @see com.iver.cit.jdwglib.dwg.IDwgPolyline#calculateGisModel(java.util.List)
216
         */
217
        public void calculateGisModel(DwgFile dwgFile) {
218
                if (getVertices() == null)
219
                        return;
220
                int flags = getFlag();
221
                List pts = getVertices();
222
                double[] bulges = getBulges();
223
                List newPts = new ArrayList();
224
                double[] newBulges = null;
225
                if(bulges != null){
226
                        newBulges = new double[bulges.length];
227
                        System.arraycopy(bulges, 0, newBulges, 0, bulges.length);
228
                }else{
229
                        bulges = new double[pts.size() ];
230
                        newBulges = new double[bulges.length];
231
                        //dwg spec says numVertex (numSegments + 1)
232
                        //TODO Check this
233
                        for(int i = 0; i < newBulges.length; i++)
234
                                bulges[i] = 0d;
235
                        newBulges = new double[bulges.length];
236
                        System.arraycopy(bulges, 0, newBulges, 0, bulges.length);
237
                }
238
                // TODO: Aqu? pueden existir casos no contemplados ...
239
//        System.out.println("flags = " + flags);
240
        if (flags==512 || flags==776 || flags==768) {//closed
241
                        newBulges = new double[bulges.length+1];
242
                        for (int j=0;j<pts.size();j++) {
243
                                newPts.add(pts.get(j));
244
                        }
245
                        newPts.add(pts.get(0));
246
                        newBulges[pts.size()] = 0;
247
                } else {
248
                        for (int j=0;j<pts.size();j++) {
249
                                newPts.add(pts.get(j));
250
                        }
251
                }
252
                if (newPts.size() > 0) {
253
                        setBulges(newBulges);
254
                        List points = GisModelCurveCalculator.calculateGisModelBulge(newPts, newBulges);
255
                        setVertices(points);
256
                } 
257
//                if (pts.size() > 0) {
258
//                        setBulges(newBulges);
259
//                        List points = GisModelCurveCalculator.calculateGisModelBulge(newPts, newBulges);
260
//                        setVertices(points);
261
//                } 
262
//                else {
263
////                        System.out.println("Encontrada polil?nea sin puntos ...");
264
//                        // TODO: No se debe mandar nunca una polil?nea sin puntos, si esto
265
//                        // ocurre es porque existe un error que hay que corregir ...
266
//                }
267
        }
268
        /* (non-Javadoc)
269
         * @see com.iver.cit.jdwglib.dwg.IDwgExtrusionable#applyExtrussion()
270
         */
271
        public void applyExtrussion() {
272
                if (getVertices() == null)
273
                        return;
274
                 List vertices = getVertices();
275
         double[] lwPolylineExt = getNormal();
276
         // Normals and Extrusions aren`t the same
277
         if (lwPolylineExt[0]==0 && lwPolylineExt[1]==0 && lwPolylineExt[2]==0) 
278
                 lwPolylineExt[2] = 1.0;
279
         
280
         double elev = getElevation();
281
         List lwPolylinePoints3D = new ArrayList();
282
         for (int j=0;j<vertices.size();j++) {
283
                 double[] point = new double[3];
284
             point[0] = ((double[])vertices.get(j))[0];
285
             point[1] = ((double[])vertices.get(j))[1];
286
             point[2] = elev;
287
             lwPolylinePoints3D.add(AcadExtrusionCalculator.
288
                             extrude2(point, lwPolylineExt));
289
         }
290
         setElevation(elev);
291
         List newVertices = new ArrayList();
292
         for (int j=0;j<vertices.size();j++) {
293
                 double[] point = (double[]) lwPolylinePoints3D.get(j);
294
             newVertices.add(new double[]{point[0], point[1]});
295
         }
296
         setVertices(newVertices);
297
        }
298
        /* (non-Javadoc)
299
         * @see com.iver.cit.jdwglib.dwg.IDwg3DTestable#has3DData()
300
         */
301
        public boolean has3DData() {
302
                return getElevation() != 0.0;
303
        }
304
        public double getZ() {
305
                return getElevation();
306
        }
307
        public FShape toFMapGeometry(boolean is3DFile) {
308
                FPolyline2D lwpline = null;
309
                List pts = getVertices();
310
                double elev = getElevation();
311
                if (pts != null && pts.size() > 0) {
312
                        
313
                        if (is3DFile) {
314
                                List pline3D = new ArrayList();
315
                                for (int j = 0; j < pts.size(); j++) {
316
                                        double[] pt = new double[3];
317
                                        pt[0] = ((double[])pts.get(j))[0];
318
                                        pt[1] = ((double[])pts.get(j))[1];
319
                                        pt[2] = elev;
320
                                        pline3D.add(pt);
321
                                }
322
                                lwpline = FMapUtil.points3DToFPolyline3D(pline3D);
323
                        } else {
324
                                lwpline = FMapUtil.points2DToFPolyline2D(pts);
325
                        }
326
                        
327
                }
328
                return lwpline;
329
        }
330
        public String toFMapString(boolean is3DFile) {
331
                if(is3DFile)
332
                        return "FPolyline3D";
333
                else
334
                        return "FPolyline2D";
335
        }
336
        
337
        public String toString(){
338
                return "LwPolyline";
339
        }
340
        
341
        
342
        public void transform2Block(double[] bPoint, Point2D insPoint,
343
                        double[] scale, double rot, 
344
                        List dwgObjectsWithoutBlocks, 
345
                        Map handleObjWithoutBlocks, DwgFile callBack) {
346
                DwgLwPolyline transformedEntity = null;
347
                List vertices = this.getVertices();
348
                if (vertices!=null) {
349
                    List transformedVertices = new ArrayList();
350
                        for (int i=0; i< vertices.size(); i++) {
351
                                double[] point = (double[]) vertices.get(i);
352
                                double[] pointAux = new double[]{point[0] - bPoint[0], 
353
                                                        point[1] - bPoint[1]};
354
                                double laX = insPoint.getX() + ((pointAux[0]*scale[0])*Math.cos(rot) + (pointAux[1]*scale[1])*(-1)*Math.sin(rot));
355
                                double laY = insPoint.getY() + ((pointAux[0]*scale[0])*Math.sin(rot) + (pointAux[1]*scale[1])*Math.cos(rot));
356
                                transformedVertices.add(new double[]{laX, laY});
357
                        }
358
                        transformedEntity = (DwgLwPolyline)this.clone();
359
                        transformedEntity.setVertices(transformedVertices);
360
                        transformedEntity.setElevation((this.getElevation() * scale[2]));
361
                        transformedEntity.setHandle(this.getHandle());
362
                        dwgObjectsWithoutBlocks.add(transformedEntity);
363
                        handleObjWithoutBlocks.put(new Integer(transformedEntity.getHandle().getOffset()), transformedEntity);
364
//                        dwgObjectsWithoutBlocks.add(this);
365
                }//if
366
        }
367
        
368
        public Object clone(){
369
                DwgLwPolyline dwgLwPolyline = new DwgLwPolyline(index);
370
                this.fill(dwgLwPolyline);
371
                return dwgLwPolyline;
372
        }
373
        
374
        protected void fill(DwgObject obj){
375
                super.fill(obj);
376
                DwgLwPolyline myObj = (DwgLwPolyline)obj;
377

    
378
                myObj.setBulges(bulges);
379
                myObj.setConstWidth(constWidth);
380
                myObj.setElevation(elevation);
381
                myObj.setFlag(flag);
382
                myObj.setNormal(normal);
383
                myObj.setThickness(thickness);
384
                myObj.setVertices(vertices);
385
                myObj.setWidths(widths);
386

    
387
        }
388
        public void addVertex(IDwgVertex vertex) {
389
                vertices.add(vertex.getPoint());
390
                
391
        }
392

    
393
}