Statistics
| Revision:

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

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

    
53
/**
54
 * The DwgSolid class represents a DWG Solid
55
 *
56
 * @author jmorell, azabala
57
 */
58
public class DwgSolid extends DwgObject
59
        implements IDwgExtrusionable,
60
        IDwg3DTestable, IDwg2FMap, IDwgBlockMember{
61

    
62

    
63
        public DwgSolid(int index) {
64
                super(index);
65
        }
66
        private double thickness;
67
        private double elevation;
68
        private double[] corner1;
69
        private double[] corner2;
70
        private double[] corner3;
71
        private double[] corner4;
72
        private double[] extrusion;
73

    
74
        /**
75
         * Read a Solid in the DWG format Version 15
76
         *
77
         * @param data Array of unsigned bytes obtained from the DWG binary file
78
         * @param offset The current bit offset where the value begins
79
         * @throws Exception If an unexpected bit value is found in the DWG file. Occurs
80
         *                    when we are looking for LwPolylines.
81
         */
82
        public void readDwgSolidV15(int[] data, int offset) throws Exception {
83

    
84
        }
85
        /**
86
         * @return Returns the corner1.
87
         */
88
        public double[] getCorner1() {
89
                return corner1;
90
        }
91
        /**
92
         * @param corner1 The corner1 to set.
93
         */
94
        public void setCorner1(double[] corner1) {
95
                this.corner1 = corner1;
96
        }
97
        /**
98
         * @return Returns the corner2.
99
         */
100
        public double[] getCorner2() {
101
                return corner2;
102
        }
103
        /**
104
         * @param corner2 The corner2 to set.
105
         */
106
        public void setCorner2(double[] corner2) {
107
                this.corner2 = corner2;
108
        }
109
        /**
110
         * @return Returns the corner3.
111
         */
112
        public double[] getCorner3() {
113
                return corner3;
114
        }
115
        /**
116
         * @param corner3 The corner3 to set.
117
         */
118
        public void setCorner3(double[] corner3) {
119
                this.corner3 = corner3;
120
        }
121
        /**
122
         * @return Returns the corner4.
123
         */
124
        public double[] getCorner4() {
125
                return corner4;
126
        }
127
        /**
128
         * @param corner4 The corner4 to set.
129
         */
130
        public void setCorner4(double[] corner4) {
131
                this.corner4 = corner4;
132
        }
133
    /**
134
     * @return Returns the elevation.
135
     */
136
    public double getElevation() {
137
        return elevation;
138
    }
139
    /**
140
     * @param elevation The elevation to set.
141
     */
142
    public void setElevation(double elevation) {
143
        this.elevation = elevation;
144
    }
145
    /**
146
     * @return Returns the extrusion.
147
     */
148
    public double[] getExtrusion() {
149
        return extrusion;
150
    }
151
        /* (non-Javadoc)
152
         * @see java.lang.Object#clone()
153
         */
154
//        public Object clone() {
155
//                DwgSolid dwgSolid = new DwgSolid(index);
156
//                dwgSolid.setType(type);
157
//                dwgSolid.setHandle(handle);
158
//                dwgSolid.setVersion(version);
159
//                dwgSolid.setMode(mode);
160
//                dwgSolid.setLayerHandle(layerHandle);
161
//                dwgSolid.setColor(color);
162
//                dwgSolid.setNumReactors(numReactors);
163
//                dwgSolid.setNoLinks(noLinks);
164
//                dwgSolid.setLinetypeFlags(linetypeFlags);
165
//                dwgSolid.setPlotstyleFlags(plotstyleFlags);
166
//                dwgSolid.setSizeInBits(sizeInBits);
167
//                dwgSolid.setExtendedData(extendedData);
168
//                dwgSolid.setGraphicData(graphicData);
169
//                //dwgSolid.setInsideBlock(insideBlock);
170
//                dwgSolid.setThickness(thickness);
171
//                dwgSolid.setElevation(elevation);
172
//                dwgSolid.setCorner1(corner1);
173
//                dwgSolid.setCorner2(corner2);
174
//                dwgSolid.setCorner3(corner3);
175
//                dwgSolid.setCorner4(corner4);
176
//                dwgSolid.setExtrusion(extrusion);
177
//                return dwgSolid;
178
//        }
179
        /**
180
         * @return Returns the thickness.
181
         */
182
        public double getThickness() {
183
                return thickness;
184
        }
185
        /**
186
         * @param thickness The thickness to set.
187
         */
188
        public void setThickness(double thickness) {
189
                this.thickness = thickness;
190
        }
191
        /**
192
         * @param extrusion The extrusion to set.
193
         */
194
        public void setExtrusion(double[] extrusion) {
195
                this.extrusion = extrusion;
196
        }
197
        /* (non-Javadoc)
198
         * @see com.iver.cit.jdwglib.dwg.IDwgExtrusionable#applyExtrussion()
199
         */
200
        public void applyExtrussion() {
201
                 double[] corner1 = getCorner1();
202
         double[] corner2 = getCorner2();
203
         double[] corner3 = getCorner3();
204
         double[] corner4 = getCorner4();
205
         double[] solidExt = getExtrusion();
206
         corner1 = AcadExtrusionCalculator.extrude2(corner1, solidExt);
207
         corner2 = AcadExtrusionCalculator.extrude2(corner2, solidExt);
208
         corner3 = AcadExtrusionCalculator.extrude2(corner3, solidExt);
209
         corner4 = AcadExtrusionCalculator.extrude2(corner4, solidExt);
210

    
211

    
212
         setCorner1(corner1);
213
         setCorner2(corner2);
214
         setCorner3(corner3);
215
         setCorner4(corner4);
216
        }
217
        /* (non-Javadoc)
218
         * @see com.iver.cit.jdwglib.dwg.IDwg3DTestable#has3DData()
219
         */
220
        public boolean has3DData() {
221
                return (getElevation() != 0.0);
222
        }
223
        public double getZ() {
224
                return getElevation();
225
        }
226
        public Geometry toFMapGeometry(boolean is3DFile) throws CreateGeometryException {
227
                int subType = getGeometrySubType(is3DFile);
228

    
229
                double[] p1 = getCorner1();
230
                double[] p2 = getCorner2();
231
                double[] p3 = getCorner3();
232
                double[] p4 = getCorner4();
233
                double elev = getElevation();
234
                List pts = new ArrayList();
235

    
236
                if (is3DFile) {
237
                        double[] p13d = new double[]{p1[0], p1[1], elev};
238
                        double[] p23d = new double[]{p2[0], p2[1], elev};
239
                        double[] p33d = new double[]{p3[0], p3[1], elev};
240
                        double[] p43d = new double[]{p4[0], p4[1], elev};
241
                        pts.add(p13d);
242
                        pts.add(p23d);
243
                        pts.add(p33d);
244
                        pts.add(p43d);
245
                } else {
246
                        pts.add(p1);
247
                        pts.add(p2);
248
                        pts.add(p3);
249
                        pts.add(p4);
250
                }
251
                return FMapUtil.ptsToPolygon(pts, subType);
252
//                return ShapeFactory.createGeometry(solid);
253

    
254
        }
255
        public String toFMapString(boolean is3DFile) {
256
                if(is3DFile){
257
                        return "FPolyline3D";
258
                }else{
259
                        return "FPolyline2D";
260
                }
261
        }
262

    
263
        public String toString(){
264
                return "Solid";
265
        }
266
        public void transform2Block(double[] bPoint, Point2D insPoint, double[] scale, double rot,
267
                        List dwgObjectsWithoutBlocks,
268
                        Map handleObjWithoutBlocks,
269
                        DwgFile callBack) {
270
                DwgSolid transformedEntity = null;
271
                double[] corner1 = this.getCorner1();
272
                double[] corner2 = this.getCorner2();
273
                double[] corner3 = this.getCorner3();
274
                double[] corner4 = this.getCorner4();
275
                Point2D pointAux = new Point2D.Double(corner1[0] - bPoint[0], corner1[1] - bPoint[1]);
276
                double laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot));
277
                double laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot));
278
                double[] transformedP1 = new double[]{laX, laY};
279
                pointAux = new Point2D.Double(corner2[0] - bPoint[0], corner2[1] - bPoint[1]);
280
                laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot));
281
                laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot));
282
                double[] transformedP2 = new double[]{laX, laY};
283
                pointAux = new Point2D.Double(corner3[0] - bPoint[0], corner3[1] - bPoint[1]);
284
                laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot));
285
                laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot));
286
                double[] transformedP3 = new double[]{laX, laY};
287
                pointAux = new Point2D.Double(corner4[0] - bPoint[0], corner4[1] - bPoint[1]);
288
                laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot));
289
                laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot));
290
                double[] transformedP4 = new double[]{laX, laY};
291
                transformedEntity = (DwgSolid)this.clone();
292
                transformedEntity.setCorner1(transformedP1);
293
                transformedEntity.setCorner2(transformedP2);
294
                transformedEntity.setCorner3(transformedP3);
295
                transformedEntity.setCorner4(transformedP4);
296
                transformedEntity.setElevation(this.getElevation() * scale[2]);
297
                dwgObjectsWithoutBlocks.add(transformedEntity);
298
                handleObjWithoutBlocks.put(new Integer(transformedEntity.getHandle().getOffset()), transformedEntity);
299
        }
300

    
301
        public Object clone(){
302
                DwgSolid obj = new DwgSolid(index);
303
                this.fill(obj);
304
                return obj;
305
        }
306

    
307
        protected void fill(DwgObject obj){
308
                super.fill(obj);
309
                DwgSolid myObj = (DwgSolid)obj;
310

    
311
                myObj.setCorner1(corner1);
312
                myObj.setCorner2(corner2);
313
                myObj.setCorner3(corner3);
314
                myObj.setCorner4(corner4);
315
                myObj.setElevation(elevation);
316
                myObj.setExtrusion(extrusion);
317
                myObj.setThickness(thickness);
318

    
319
        }
320

    
321

    
322
        public int getGeometrySubType(boolean is3DFile) {
323
                if (is3DFile) {
324
                        return Geometry.SUBTYPES.GEOM3D;
325
                } else {
326
                        return Geometry.SUBTYPES.GEOM2D;
327
                }
328
        }
329

    
330
        public int getGeometryType() {
331
                // return Geometry.TYPES.SOLID; FIXME not implemented
332
                return Geometry.TYPES.SURFACE;
333
        }
334

    
335
}