Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libDwg / src / org / gvsig / dwg / lib / objects / DwgArc.java @ 34887

History | View | Annotate | Download (7.75 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.dwg.lib.util.FMapUtil;
49
import org.gvsig.fmap.geom.Geometry;
50
import org.gvsig.fmap.geom.GeometryLocator;
51
import org.gvsig.fmap.geom.GeometryManager;
52
import org.gvsig.fmap.geom.exception.CreateGeometryException;
53
import org.gvsig.fmap.geom.primitive.Arc;
54
import org.gvsig.fmap.geom.primitive.Point;
55
import org.gvsig.fmap.geom.util.UtilFunctions;
56

    
57

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

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

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

    
77

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

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

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

    
162
        }
163

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

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

    
176
                GeometryManager gMan = GeometryLocator.getGeometryManager();
177

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

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

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

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

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

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

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

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

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

    
276
        }
277

    
278
        public int getGeometryType() {
279
                return Geometry.TYPES.ARC;
280
        }
281

    
282
}