Statistics
| Revision:

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

History | View | Annotate | Download (10.9 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.List;
39
import java.util.Map;
40

    
41
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
42
import com.iver.cit.gvsig.fmap.core.FShape;
43
import com.iver.cit.jdwglib.dwg.DwgFile;
44
import com.iver.cit.jdwglib.dwg.DwgObject;
45
import com.iver.cit.jdwglib.dwg.IDwg2FMap;
46
import com.iver.cit.jdwglib.dwg.IDwg3DTestable;
47
import com.iver.cit.jdwglib.dwg.IDwgBlockMember;
48
import com.iver.cit.jdwglib.dwg.IDwgExtrusionable;
49
import com.iver.cit.jdwglib.dwg.IDwgPolyline;
50
import com.iver.cit.jdwglib.util.AcadExtrusionCalculator;
51
import com.iver.cit.jdwglib.util.FMapUtil;
52
import com.iver.cit.jdwglib.util.GisModelCurveCalculator;
53

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

    
362
                myObj.setBulges(bulges);
363
                myObj.setConstWidth(constWidth);
364
                myObj.setElevation(elevation);
365
                myObj.setFlag(flag);
366
                myObj.setNormal(normal);
367
                myObj.setThickness(thickness);
368
                myObj.setVertices(vertices);
369
                myObj.setWidths(widths);
370

    
371
        }
372

    
373
}