Statistics
| Revision:

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

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

    
41
import org.gvsig.dwg.lib.DwgFile;
42
import org.gvsig.dwg.lib.DwgObject;
43
import org.gvsig.dwg.lib.IDwg2FMap;
44
import org.gvsig.dwg.lib.IDwg3DTestable;
45
import org.gvsig.dwg.lib.IDwgBlockMember;
46
import org.gvsig.dwg.lib.IDwgExtrusionable;
47
import org.gvsig.dwg.lib.util.AcadExtrusionCalculator;
48
import org.gvsig.fmap.geom.Geometry;
49
import org.gvsig.fmap.geom.GeometryLocator;
50
import org.gvsig.fmap.geom.GeometryManager;
51
import org.gvsig.fmap.geom.exception.CreateGeometryException;
52
import org.gvsig.fmap.geom.primitive.Arc;
53
import org.gvsig.fmap.geom.primitive.Point;
54
import org.gvsig.fmap.geom.util.UtilFunctions;
55

    
56

    
57
/**
58
 * The DwgArc class represents a DWG Arc
59
 *
60
 * @author jmorell
61
 */
62
public class DwgArc extends DwgObject
63
        implements IDwgExtrusionable, IDwg3DTestable, IDwg2FMap, IDwgBlockMember {
64

    
65
        private double[] center;
66
        private double radius;
67
        private double thickness;
68
        private double[] extrusion;
69
        private double initAngle;
70
        private double endAngle;
71

    
72
        public DwgArc(int index) {
73
                super(index);
74
        }
75

    
76

    
77
        /**
78
         * @return Returns the center.
79
         */
80
        public double[] getCenter() {
81
                return center;
82
        }
83
        /**
84
         * @param center The center to set.
85
         */
86
        public void setCenter(double[] center) {
87
                this.center = center;
88
        }
89
        /**
90
         * @return Returns the endAngle.
91
         */
92
        public double getEndAngle() {
93
                return endAngle;
94
        }
95
        /**
96
         * @param endAngle The endAngle to set.
97
         */
98
        public void setEndAngle(double endAngle) {
99
                this.endAngle = endAngle;
100
        }
101
        /**
102
         * @return Returns the initAngle.
103
         */
104
        public double getInitAngle() {
105
                return initAngle;
106
        }
107
        /**
108
         * @param initAngle The initAngle to set.
109
         */
110
        public void setInitAngle(double initAngle) {
111
                this.initAngle = initAngle;
112
        }
113
        /**
114
         * @return Returns the radius.
115
         */
116
        public double getRadius() {
117
                return radius;
118
        }
119
        /**
120
         * @param radius The radius to set.
121
         */
122
        public void setRadius(double radius) {
123
                this.radius = radius;
124
        }
125
    /**
126
     * @return Returns the extrusion.
127
     */
128
    public double[] getExtrusion() {
129
        return extrusion;
130
    }
131

    
132
        /**
133
         * @return Returns the thickness.
134
         */
135
        public double getThickness() {
136
                return thickness;
137
        }
138
        /**
139
         * @param thickness The thickness to set.
140
         */
141
        public void setThickness(double thickness) {
142
                this.thickness = thickness;
143
        }
144
        /**
145
         * @param extrusion The extrusion to set.
146
         */
147
        public void setExtrusion(double[] extrusion) {
148
                this.extrusion = extrusion;
149
        }
150

    
151
        /* (non-Javadoc)
152
         * @see com.iver.cit.jdwglib.dwg.IDwgExtrusionable#applyExtrussion()
153
         */
154
        public void applyExtrussion() {
155
                 double[] arcCenter = getCenter();
156
         double[] arcExt = getExtrusion();
157
         arcCenter = AcadExtrusionCalculator.
158
                         extrude2(arcCenter, arcExt);
159
         setCenter(arcCenter);
160

    
161
        }
162

    
163
        /* (non-Javadoc)
164
         * @see com.iver.cit.jdwglib.dwg.IDwg3DTestable#has3DData()
165
         */
166
        public boolean has3DData() {
167
                return (getCenter()[2] != 0.0);
168
        }
169

    
170
        /* (non-Javadoc)
171
         * @see com.iver.cit.jdwglib.dwg.IDwg2FMap#toFMapGeometry()
172
         */
173
        public Geometry toFMapGeometry(boolean is3DFile) throws CreateGeometryException {
174

    
175
                GeometryManager gMan = GeometryLocator.getGeometryManager();
176

    
177
                double[] center = getCenter();
178
                //FIXME: ¿qué hacer cuando llega un radio negativo?
179
                //        De momento, tomamos el valor absoluto del radio, pero
180
                //        ¿habría que modificar de alguna manera los angulos?
181
                double radius = Math.abs(getRadius());
182
                double angSt = getInitAngle();
183
                double angEnd = getEndAngle();
184
                double angExt = UtilFunctions.angleDistance(angSt, angEnd);
185
                Arc arc = (Arc) gMan.create(getGeometryType(),getGeometrySubType(is3DFile));
186
                Point point = (Point) gMan.create(Geometry.TYPES.POINT,getGeometrySubType(is3DFile));
187
                point.setCoordinates(center);
188
                arc.setPoints(point, radius, angSt, -angExt);
189
                return arc;
190
        }
191

    
192
        public String toString(){
193
                return "Arc";
194
        }
195

    
196
        /* (non-Javadoc)
197
         * @see com.iver.cit.jdwglib.dwg.IDwg2FMap#toFMapString()
198
         */
199
        public String toFMapString(boolean is3dFile) {
200
                if(is3dFile) {
201
                        return "FPolyline3D";
202
                } else {
203
                        return "FPolyline2D";
204
                }
205
        }
206

    
207
        /* (non-Javadoc)
208
         * @see com.iver.cit.jdwglib.dwg.IDwg3DTestable#getZ()
209
         */
210
        public double getZ() {
211
                return getCenter()[2];
212
        }
213

    
214
        public void transform2Block(double[] bPoint, Point2D insPoint, double[] scale, double rot,
215
                        List dwgObjectsWithoutBlocks, Map handleObjWithoutBlocks, DwgFile callBack) {
216
                //DwgArc transformedEntity = new DwgArc(getIndex());
217
                double[] center = this.getCenter();
218
                Point2D pointAux = new Point2D.Double(center[0] - bPoint[0], center[1] - bPoint[1]);
219
                double laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot));
220
                double laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot));
221
                double laZ = center[2] * scale[2];
222
                double[] transformedCenter = new double[]{laX, laY, laZ};
223
                double radius = this.getRadius();
224
                double transformedRadius = radius * scale[0];
225
                double initAngle = this.getInitAngle();
226
                double endAngle = this.getEndAngle();
227
                double transformedInitAngle = initAngle + rot;
228
                if (transformedInitAngle<0) {
229
                    transformedInitAngle = transformedInitAngle + (2*Math.PI);
230
                } else if (transformedInitAngle>(2*Math.PI)) {
231
                    transformedInitAngle = transformedInitAngle - (2*Math.PI);
232
                }
233
                double transformedEndAngle = endAngle + rot;
234
                if (transformedEndAngle<0) {
235
                    transformedEndAngle = transformedEndAngle + (2*Math.PI);
236
                } else if (transformedEndAngle>(2*Math.PI)) {
237
                    transformedEndAngle = transformedEndAngle - (2*Math.PI);
238
                }
239
                DwgArc transformedEntity = (DwgArc) this.clone();
240
                transformedEntity.setCenter(transformedCenter);
241
                transformedEntity.setRadius(transformedRadius);
242
                transformedEntity.setInitAngle(transformedInitAngle);
243
                transformedEntity.setEndAngle(transformedEndAngle);
244
                transformedEntity.setHandle(this.getHandle());
245

    
246
                dwgObjectsWithoutBlocks.add(transformedEntity);
247
                handleObjWithoutBlocks.put(new Integer(transformedEntity.getHandle().getOffset()), transformedEntity);
248

    
249
//                setCenter(transformedCenter);
250
//                setRadius(transformedRadius);
251
//                setInitAngle(transformedInitAngle);
252
//                setEndAngle(transformedEndAngle);
253
//                dwgObjectsWithoutBlocks.add(this);
254
        }
255
        /* (non-Javadoc)
256
         * @see java.lang.Object#clone()
257
         */
258
public Object clone(){
259
                DwgArc obj = new DwgArc(index);
260
                this.fill(obj);
261
                return obj;
262
        }
263

    
264
        protected void fill(DwgObject obj){
265
                super.fill(obj);
266
                DwgArc myObj = (DwgArc)obj;
267

    
268
                myObj.setCenter(center);
269
                myObj.setEndAngle(endAngle);
270
                myObj.setExtrusion(extrusion);
271
                myObj.setInitAngle(initAngle);
272
                myObj.setRadius(radius);
273
                myObj.setThickness(thickness);
274

    
275
        }
276

    
277

    
278
        public int getGeometrySubType(boolean is3DFile) {
279
                if (is3DFile) {
280
                        return Geometry.SUBTYPES.GEOM3D;
281
                } else {
282
                        return Geometry.SUBTYPES.GEOM2D;
283
                }
284
        }
285

    
286
        public int getGeometryType() {
287
                return Geometry.TYPES.ARC;
288
        }
289

    
290
}