Statistics
| Revision:

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

History | View | Annotate | Download (9.67 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.util.AcadExtrusionCalculator;
52
import com.iver.cit.jdwglib.util.FMapUtil;
53

    
54
/**
55
 * The DwgSolid class represents a DWG Solid
56
 * 
57
 * @author jmorell, azabala
58
 */
59
public class DwgSolid extends DwgObject 
60
        implements IDwgExtrusionable, 
61
        IDwg3DTestable, IDwg2FMap, IDwgBlockMember{
62
        
63
        
64
        public DwgSolid(int index) {
65
                super(index);
66
        }
67
        private double thickness;
68
        private double elevation;
69
        private double[] corner1;
70
        private double[] corner2;
71
        private double[] corner3;
72
        private double[] corner4;
73
        private double[] extrusion;
74
        
75
        /**
76
         * Read a Solid in the DWG format Version 15
77
         * 
78
         * @param data Array of unsigned bytes obtained from the DWG binary file
79
         * @param offset The current bit offset where the value begins
80
         * @throws Exception If an unexpected bit value is found in the DWG file. Occurs
81
         *                    when we are looking for LwPolylines.
82
         */
83
        public void readDwgSolidV15(int[] data, int offset) throws Exception {
84
                
85
        }
86
        /**
87
         * @return Returns the corner1.
88
         */
89
        public double[] getCorner1() {
90
                return corner1;
91
        }
92
        /**
93
         * @param corner1 The corner1 to set.
94
         */
95
        public void setCorner1(double[] corner1) {
96
                this.corner1 = corner1;
97
        }
98
        /**
99
         * @return Returns the corner2.
100
         */
101
        public double[] getCorner2() {
102
                return corner2;
103
        }
104
        /**
105
         * @param corner2 The corner2 to set.
106
         */
107
        public void setCorner2(double[] corner2) {
108
                this.corner2 = corner2;
109
        }
110
        /**
111
         * @return Returns the corner3.
112
         */
113
        public double[] getCorner3() {
114
                return corner3;
115
        }
116
        /**
117
         * @param corner3 The corner3 to set.
118
         */
119
        public void setCorner3(double[] corner3) {
120
                this.corner3 = corner3;
121
        }
122
        /**
123
         * @return Returns the corner4.
124
         */
125
        public double[] getCorner4() {
126
                return corner4;
127
        }
128
        /**
129
         * @param corner4 The corner4 to set.
130
         */
131
        public void setCorner4(double[] corner4) {
132
                this.corner4 = corner4;
133
        }
134
    /**
135
     * @return Returns the elevation.
136
     */
137
    public double getElevation() {
138
        return elevation;
139
    }
140
    /**
141
     * @param elevation The elevation to set.
142
     */
143
    public void setElevation(double elevation) {
144
        this.elevation = elevation;
145
    }
146
    /**
147
     * @return Returns the extrusion.
148
     */
149
    public double[] getExtrusion() {
150
        return extrusion;
151
    }
152
        /* (non-Javadoc)
153
         * @see java.lang.Object#clone()
154
         */
155
//        public Object clone() {
156
//                DwgSolid dwgSolid = new DwgSolid(index);
157
//                dwgSolid.setType(type);
158
//                dwgSolid.setHandle(handle);
159
//                dwgSolid.setVersion(version);
160
//                dwgSolid.setMode(mode);
161
//                dwgSolid.setLayerHandle(layerHandle);
162
//                dwgSolid.setColor(color);
163
//                dwgSolid.setNumReactors(numReactors);
164
//                dwgSolid.setNoLinks(noLinks);
165
//                dwgSolid.setLinetypeFlags(linetypeFlags);
166
//                dwgSolid.setPlotstyleFlags(plotstyleFlags);
167
//                dwgSolid.setSizeInBits(sizeInBits);
168
//                dwgSolid.setExtendedData(extendedData);
169
//                dwgSolid.setGraphicData(graphicData);
170
//                //dwgSolid.setInsideBlock(insideBlock);
171
//                dwgSolid.setThickness(thickness);
172
//                dwgSolid.setElevation(elevation);
173
//                dwgSolid.setCorner1(corner1);
174
//                dwgSolid.setCorner2(corner2);
175
//                dwgSolid.setCorner3(corner3);
176
//                dwgSolid.setCorner4(corner4);
177
//                dwgSolid.setExtrusion(extrusion);
178
//                return dwgSolid;
179
//        }
180
        /**
181
         * @return Returns the thickness.
182
         */
183
        public double getThickness() {
184
                return thickness;
185
        }
186
        /**
187
         * @param thickness The thickness to set.
188
         */
189
        public void setThickness(double thickness) {
190
                this.thickness = thickness;
191
        }
192
        /**
193
         * @param extrusion The extrusion to set.
194
         */
195
        public void setExtrusion(double[] extrusion) {
196
                this.extrusion = extrusion;
197
        }
198
        /* (non-Javadoc)
199
         * @see com.iver.cit.jdwglib.dwg.IDwgExtrusionable#applyExtrussion()
200
         */
201
        public void applyExtrussion() {
202
                 double[] corner1 = getCorner1();
203
         double[] corner2 = getCorner2();
204
         double[] corner3 = getCorner3();
205
         double[] corner4 = getCorner4();
206
         double[] solidExt = getExtrusion();
207
         corner1 = AcadExtrusionCalculator.extrude2(corner1, solidExt);
208
         corner2 = AcadExtrusionCalculator.extrude2(corner2, solidExt);
209
         corner3 = AcadExtrusionCalculator.extrude2(corner3, solidExt);
210
         corner4 = AcadExtrusionCalculator.extrude2(corner4, solidExt);
211
         
212
         
213
         setCorner1(corner1);
214
         setCorner2(corner2);
215
         setCorner3(corner3);
216
         setCorner4(corner4);
217
        }
218
        /* (non-Javadoc)
219
         * @see com.iver.cit.jdwglib.dwg.IDwg3DTestable#has3DData()
220
         */
221
        public boolean has3DData() {
222
                return (getElevation() != 0.0);
223
        }
224
        public double getZ() {
225
                return getElevation();
226
        }
227
        public IGeometry toFMapGeometry(boolean is3DFile) {
228
                FPolyline2D solid = null;
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

    
246
                        solid = FMapUtil.ptsTo3DPolygon(pts);
247
                        
248
                        
249
                } else {
250
                        pts.add(p1);
251
                        pts.add(p2);
252
                        pts.add(p3);
253
                        pts.add(p4);
254
                        solid = FMapUtil.ptsTo2DPolygon(pts);
255
                }
256
                return ShapeFactory.createGeometry(solid);
257
                
258
        }
259
        public String toFMapString(boolean is3DFile) {
260
                if(is3DFile){
261
                        return "FPolyline3D";
262
                }else{
263
                        return "FPolyline2D";
264
                }
265
        }
266
        
267
        public String toString(){
268
                return "Solid";
269
        }
270
        public void transform2Block(double[] bPoint, Point2D insPoint, double[] scale, double rot, 
271
                        List dwgObjectsWithoutBlocks, 
272
                        Map handleObjWithoutBlocks,
273
                        DwgFile callBack) {
274
                DwgSolid transformedEntity = null;
275
                double[] corner1 = this.getCorner1();
276
                double[] corner2 = this.getCorner2();
277
                double[] corner3 = this.getCorner3();
278
                double[] corner4 = this.getCorner4();
279
                Point2D pointAux = new Point2D.Double(corner1[0] - bPoint[0], corner1[1] - bPoint[1]);
280
                double laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot));
281
                double laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot));
282
                double[] transformedP1 = new double[]{laX, laY};
283
                pointAux = new Point2D.Double(corner2[0] - bPoint[0], corner2[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[] transformedP2 = new double[]{laX, laY};
287
                pointAux = new Point2D.Double(corner3[0] - bPoint[0], corner3[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[] transformedP3 = new double[]{laX, laY};
291
                pointAux = new Point2D.Double(corner4[0] - bPoint[0], corner4[1] - bPoint[1]);
292
                laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot));
293
                laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot));
294
                double[] transformedP4 = new double[]{laX, laY};
295
                transformedEntity = (DwgSolid)this.clone();
296
                transformedEntity.setCorner1(transformedP1);
297
                transformedEntity.setCorner2(transformedP2);
298
                transformedEntity.setCorner3(transformedP3);
299
                transformedEntity.setCorner4(transformedP4);
300
                transformedEntity.setElevation(this.getElevation() * scale[2]);
301
                dwgObjectsWithoutBlocks.add(transformedEntity);
302
                handleObjWithoutBlocks.put(new Integer(transformedEntity.getHandle().getOffset()), transformedEntity);
303
        }
304
        
305
        public Object clone(){
306
                DwgSolid obj = new DwgSolid(index);
307
                this.fill(obj);
308
                return obj;
309
        }
310
        
311
        protected void fill(DwgObject obj){
312
                super.fill(obj);
313
                DwgSolid myObj = (DwgSolid)obj;
314

    
315
                myObj.setCorner1(corner1);
316
                myObj.setCorner2(corner2);
317
                myObj.setCorner3(corner3);
318
                myObj.setCorner4(corner4);
319
                myObj.setElevation(elevation);
320
                myObj.setExtrusion(extrusion);
321
                myObj.setThickness(thickness);
322

    
323
        }
324

    
325
}