Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libDwg / src / org / gvsig / dwg / lib / objects / DwgLine.java @ 28969

History | View | Annotate | Download (8.1 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.util.AcadExtrusionCalculator;
48
import org.gvsig.dwg.lib.util.FMapUtil;
49
import org.gvsig.fmap.geom.Geometry;
50
import org.gvsig.fmap.geom.GeometryFactory;
51
import org.gvsig.fmap.geom.GeometryManager;
52

    
53

    
54
/**
55
 * The DwgLine class represents a DWG Line
56
 *
57
 * @author jmorell, azabala
58
 */
59
public class DwgLine extends DwgObject
60
        implements /*IDwgExtrusionable,*/ IDwg3DTestable, IDwg2FMap, IDwgBlockMember{
61
        public DwgLine(int index) {
62
                super(index);
63
                // TODO Auto-generated constructor stub
64
        }
65
        private double[] p1;
66
        private double[] p2;
67
        private double thickness;
68
        private double[] extrusion;
69
        private boolean zflag = false;
70

    
71
        /**
72
         * @return Returns the p1.
73
         */
74
        public double[] getP1() {
75
                return p1;
76
        }
77
        /**
78
         * @param p1 The p1 to set.
79
         */
80
        public void setP1(double[] p1) {
81
                this.p1 = p1;
82
        }
83
        /**
84
         * @return Returns the p2.
85
         */
86
        public double[] getP2() {
87
                return p2;
88
        }
89
        /**
90
         * @param p2 The p2 to set.
91
         */
92
        public void setP2(double[] p2) {
93
                this.p2 = p2;
94
        }
95
        /**
96
         * @return Returns the extrusion.
97
         */
98
        public double[] getExtrusion() {
99
                return extrusion;
100
        }
101
        /**
102
         * @param extrusion The extrusion to set.
103
         */
104
        public void setExtrusion(double[] extrusion) {
105
                this.extrusion = extrusion;
106
        }
107
        /**
108
         * @return Returns the thickness.
109
         */
110
        public double getThickness() {
111
                return thickness;
112
        }
113
        /**
114
         * @param thickness The thickness to set.
115
         */
116
        public void setThickness(double thickness) {
117
                this.thickness = thickness;
118
        }
119
        /* (non-Javadoc)
120
         * @see java.lang.Object#clone()
121
         */
122
//        public Object clone() {
123
//                DwgLine dwgLine = new DwgLine(index);
124
//                dwgLine.setType(type);
125
//                dwgLine.setHandle(handle);
126
//                dwgLine.setVersion(version);
127
//                dwgLine.setMode(mode);
128
//                dwgLine.setLayerHandle(layerHandle);
129
//                dwgLine.setColor(color);
130
//                dwgLine.setNumReactors(numReactors);
131
//                dwgLine.setNoLinks(noLinks);
132
//                dwgLine.setLinetypeFlags(linetypeFlags);
133
//                dwgLine.setPlotstyleFlags(plotstyleFlags);
134
//                dwgLine.setSizeInBits(sizeInBits);
135
//                dwgLine.setExtendedData(extendedData);
136
//                dwgLine.setGraphicData(graphicData);
137
//                //dwgLine.setInsideBlock(insideBlock);
138
//                dwgLine.setP1(p1);
139
//                dwgLine.setP2(p2);
140
//                dwgLine.setThickness(thickness);
141
//                dwgLine.setExtrusion(extrusion);
142
//                dwgLine.setSubEntityHandle(subEntityHandle);
143
//                return dwgLine;
144
//        }
145
    /**
146
     * @return Returns the zflag.
147
     */
148
    public boolean isZflag() {
149
        return zflag;
150
    }
151
    /**
152
     * @param zflag The zflag to set.
153
     */
154
    public void setZflag(boolean zflag) {
155
        this.zflag = zflag;
156
    }
157
        /* (non-Javadoc)
158
         * @see com.iver.cit.jdwglib.dwg.IDwgExtrusionable#applyExtrussion()
159
         */
160
        public void applyExtrussion() {
161
                 double[] lineP1 = getP1();
162
         double[] lineP2 = getP2();
163
         boolean zflag = isZflag();
164
         if (zflag) {
165
             // elev = 0.0;
166
             lineP1 = new double[]{lineP1[0], lineP1[1], 0.0};
167
             lineP2 = new double[]{lineP2[0], lineP2[1], 0.0};
168
         }
169
         double[] lineExt = getExtrusion();
170
         lineP1 = AcadExtrusionCalculator.extrude2(lineP1, lineExt);
171
         lineP2 = AcadExtrusionCalculator.extrude2(lineP2, lineExt);
172
         setP1(lineP1);
173
         setP2(lineP2);
174
        }
175
        /* (non-Javadoc)
176
         * @see com.iver.cit.jdwglib.dwg.IDwg3DTestable#has3DData()
177
         */
178
        public boolean has3DData() {
179
          if (!isZflag()) {
180
                double z1 = getP1()[2];
181
                double z2 = getP2()[2];
182
                if (z1!=0.0 || z2!=0.0)
183
                        return  true;
184
          }//TODO y si zflag vale true? REVISAR
185
          return false;
186
        }
187
        public double getZ() {
188
                double[] p1 = getP1();
189
                double[] p2 = getP2();
190
                if(isZflag()){
191
                        if (p1[2] == p2[2])
192
                                return p1[2];
193
                        else
194
                                return 0d;
195
                } else {
196
                                return 0d;
197
                }
198
        }
199
        public Geometry toFMapGeometry(boolean is3DFile) {
200
                GeometryFactory gFactory = GeometryManager.getInstance().getGeometryFactory();
201
                double[] p1 = getP1();
202
                double[] p2 = getP2();
203

    
204

    
205
                if (is3DFile && isZflag()) {
206
                        return gFactory.createLine2DZ(
207
                                        new Point2D.Double(p1[0], p1[1]),
208
                                        new Point2D.Double(p2[0], p2[1]),
209
                                        p1[2]);
210
                } else if (is3DFile && ! isZflag()) {
211
                        return gFactory.createLine2DZ(
212
                                        new Point2D.Double(p1[0], p1[1]),
213
                                        new Point2D.Double(p2[0], p2[1]),
214
                                        0);
215
                } else {
216
                        return gFactory.createLine2D(
217
                                        new Point2D.Double(p1[0], p1[1]),
218
                                        new Point2D.Double(p2[0], p2[1]));
219
                }
220

    
221
        }
222
        public String toFMapString(boolean is3DFile) {
223
                if(is3DFile){
224
                        return "FPolyline3D";
225
                }else{
226
                        return "FPolyline2D";
227
                }
228
        }
229

    
230
        public String toString(){
231
                return "Line";
232
        }
233
        public void transform2Block(double[] bPoint, Point2D insPoint,
234
                        double[] scale, double rot,
235
                        List dwgObjectsWithoutBlocks,
236
                        Map handleObjWithoutBlocks,
237
                        DwgFile callBack) {
238
                DwgLine transformedEntity = null;
239
                double[] p1 = this.getP1();
240
                if(p1.length < 3)
241
                {
242
                        double[] newp1 = new double[3];
243
                        System.arraycopy(p1, 0, newp1, 0, p1.length);
244
                        p1 = newp1;
245
                        setP1(newp1);
246
                }
247
                double[] p2 = this.getP2();
248
                if(p2.length < 3)
249
                {
250
                        double[] newp2 = new double[3];
251
                        System.arraycopy(p2, 0, newp2, 0, p2.length);
252
                        p2 = newp2;
253
                        setP2(newp2);
254
                }
255
                Point2D pointAux = new Point2D.Double(p1[0] - bPoint[0], p1[1] - bPoint[1]);
256
                double laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot));
257
                double laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot));
258
                double[] transformedP1 = null;
259
        if (this.isZflag() ) {
260
                        double laZ = p1[2] * scale[2];
261
                        transformedP1 = new double[]{laX, laY, laZ};
262
                } else {
263
                        transformedP1 = new double[]{laX, laY, 0d};
264
                }
265
                //double[] transformedP1 = new double[]{laX, laY};
266
                pointAux = new Point2D.Double(p2[0] - bPoint[0], p2[1] - bPoint[1]);
267
                laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot));
268
                laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot));
269
        double[] transformedP2 = null;
270
                if (this.isZflag() ) {
271
                        double laZ = p2[2] * scale[2];
272
                        transformedP2 = new double[]{laX, laY, laZ};
273
                } else {
274
                        transformedP2 = new double[]{laX, laY, 0d};
275
                }
276
                //double[] transformedP2 = new double[]{laX, laY};
277
                transformedEntity = (DwgLine)this.clone();
278
                transformedEntity.setP1(transformedP1);
279
                transformedEntity.setP2(transformedP2);
280
                dwgObjectsWithoutBlocks.add(transformedEntity);
281
                handleObjWithoutBlocks.put(new Integer(transformedEntity.getHandle().getOffset()), transformedEntity);
282
//                dwgObjectsWithoutBlocks.add(this);
283
        }
284
        public Object clone(){
285
                DwgLine dwgLine = new DwgLine(index);
286
                this.fill(dwgLine);
287
                return dwgLine;
288
        }
289

    
290
        protected void fill(DwgObject obj){
291
                super.fill(obj);
292
                DwgLine myObj = (DwgLine)obj;
293

    
294
                myObj.setExtrusion(extrusion);
295
                myObj.setP1(p1);
296
                myObj.setP2(p2);
297
                myObj.setThickness(thickness);
298

    
299
        }
300

    
301
}