root / trunk / org.gvsig.dwg / org.gvsig.dwg.lib / src / main / java / org / gvsig / dwg / lib / objects / DwgLine.java @ 5
History | View | Annotate | Download (8.52 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.util.AcadExtrusionCalculator; |
47 |
import org.gvsig.fmap.geom.Geometry; |
48 |
import org.gvsig.fmap.geom.GeometryLocator; |
49 |
import org.gvsig.fmap.geom.GeometryManager; |
50 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
51 |
import org.gvsig.fmap.geom.primitive.Curve; |
52 |
import org.gvsig.fmap.geom.primitive.Point; |
53 |
|
54 |
|
55 |
/**
|
56 |
* The DwgLine class represents a DWG Line
|
57 |
*
|
58 |
* @author jmorell, azabala
|
59 |
*/
|
60 |
public class DwgLine extends DwgObject |
61 |
implements /*IDwgExtrusionable,*/ IDwg3DTestable, IDwg2FMap, IDwgBlockMember{ |
62 |
public DwgLine(int index) { |
63 |
super(index);
|
64 |
// TODO Auto-generated constructor stub
|
65 |
} |
66 |
private double[] p1; |
67 |
private double[] p2; |
68 |
private double thickness; |
69 |
private double[] extrusion; |
70 |
private boolean zflag = false; |
71 |
|
72 |
/**
|
73 |
* @return Returns the p1.
|
74 |
*/
|
75 |
public double[] getP1() { |
76 |
return p1;
|
77 |
} |
78 |
/**
|
79 |
* @param p1 The p1 to set.
|
80 |
*/
|
81 |
public void setP1(double[] p1) { |
82 |
this.p1 = p1;
|
83 |
} |
84 |
/**
|
85 |
* @return Returns the p2.
|
86 |
*/
|
87 |
public double[] getP2() { |
88 |
return p2;
|
89 |
} |
90 |
/**
|
91 |
* @param p2 The p2 to set.
|
92 |
*/
|
93 |
public void setP2(double[] p2) { |
94 |
this.p2 = p2;
|
95 |
} |
96 |
/**
|
97 |
* @return Returns the extrusion.
|
98 |
*/
|
99 |
public double[] getExtrusion() { |
100 |
return extrusion;
|
101 |
} |
102 |
/**
|
103 |
* @param extrusion The extrusion to set.
|
104 |
*/
|
105 |
public void setExtrusion(double[] extrusion) { |
106 |
this.extrusion = extrusion;
|
107 |
} |
108 |
/**
|
109 |
* @return Returns the thickness.
|
110 |
*/
|
111 |
public double getThickness() { |
112 |
return thickness;
|
113 |
} |
114 |
/**
|
115 |
* @param thickness The thickness to set.
|
116 |
*/
|
117 |
public void setThickness(double thickness) { |
118 |
this.thickness = thickness;
|
119 |
} |
120 |
/* (non-Javadoc)
|
121 |
* @see java.lang.Object#clone()
|
122 |
*/
|
123 |
// public Object clone() {
|
124 |
// DwgLine dwgLine = new DwgLine(index);
|
125 |
// dwgLine.setType(type);
|
126 |
// dwgLine.setHandle(handle);
|
127 |
// dwgLine.setVersion(version);
|
128 |
// dwgLine.setMode(mode);
|
129 |
// dwgLine.setLayerHandle(layerHandle);
|
130 |
// dwgLine.setColor(color);
|
131 |
// dwgLine.setNumReactors(numReactors);
|
132 |
// dwgLine.setNoLinks(noLinks);
|
133 |
// dwgLine.setLinetypeFlags(linetypeFlags);
|
134 |
// dwgLine.setPlotstyleFlags(plotstyleFlags);
|
135 |
// dwgLine.setSizeInBits(sizeInBits);
|
136 |
// dwgLine.setExtendedData(extendedData);
|
137 |
// dwgLine.setGraphicData(graphicData);
|
138 |
// //dwgLine.setInsideBlock(insideBlock);
|
139 |
// dwgLine.setP1(p1);
|
140 |
// dwgLine.setP2(p2);
|
141 |
// dwgLine.setThickness(thickness);
|
142 |
// dwgLine.setExtrusion(extrusion);
|
143 |
// dwgLine.setSubEntityHandle(subEntityHandle);
|
144 |
// return dwgLine;
|
145 |
// }
|
146 |
/**
|
147 |
* @return Returns the zflag.
|
148 |
*/
|
149 |
public boolean isZflag() { |
150 |
return zflag;
|
151 |
} |
152 |
/**
|
153 |
* @param zflag The zflag to set.
|
154 |
*/
|
155 |
public void setZflag(boolean zflag) { |
156 |
this.zflag = zflag;
|
157 |
} |
158 |
/* (non-Javadoc)
|
159 |
* @see com.iver.cit.jdwglib.dwg.IDwgExtrusionable#applyExtrussion()
|
160 |
*/
|
161 |
public void applyExtrussion() { |
162 |
double[] lineP1 = getP1(); |
163 |
double[] lineP2 = getP2(); |
164 |
boolean zflag = isZflag();
|
165 |
if (zflag) {
|
166 |
// elev = 0.0;
|
167 |
lineP1 = new double[]{lineP1[0], lineP1[1], 0.0}; |
168 |
lineP2 = new double[]{lineP2[0], lineP2[1], 0.0}; |
169 |
} |
170 |
double[] lineExt = getExtrusion(); |
171 |
lineP1 = AcadExtrusionCalculator.extrude2(lineP1, lineExt); |
172 |
lineP2 = AcadExtrusionCalculator.extrude2(lineP2, lineExt); |
173 |
setP1(lineP1); |
174 |
setP2(lineP2); |
175 |
} |
176 |
/* (non-Javadoc)
|
177 |
* @see com.iver.cit.jdwglib.dwg.IDwg3DTestable#has3DData()
|
178 |
*/
|
179 |
public boolean has3DData() { |
180 |
if (!isZflag()) {
|
181 |
double z1 = getP1()[2]; |
182 |
double z2 = getP2()[2]; |
183 |
if (z1!=0.0 || z2!=0.0) { |
184 |
return true; |
185 |
} |
186 |
}//TODO y si zflag vale true? REVISAR
|
187 |
return false; |
188 |
} |
189 |
public double getZ() { |
190 |
double[] p1 = getP1(); |
191 |
double[] p2 = getP2(); |
192 |
if(isZflag()){
|
193 |
if (p1[2] == p2[2]) { |
194 |
return p1[2]; |
195 |
} else {
|
196 |
return 0d; |
197 |
} |
198 |
} else {
|
199 |
return 0d; |
200 |
} |
201 |
} |
202 |
public Geometry toFMapGeometry(boolean is3DFile) throws CreateGeometryException { |
203 |
GeometryManager gMan = GeometryLocator.getGeometryManager(); |
204 |
|
205 |
double[] p1 = getP1(); |
206 |
double[] p2 = getP2(); |
207 |
|
208 |
Point point1 = (Point) gMan.create(Geometry.TYPES.POINT, |
209 |
getGeometrySubType(is3DFile)); |
210 |
point1.setCoordinates(p1); |
211 |
|
212 |
Point point2 = (Point) gMan.create(Geometry.TYPES.POINT, |
213 |
getGeometrySubType(is3DFile)); |
214 |
point2.setCoordinates(p2); |
215 |
|
216 |
Curve line = (Curve) gMan.create(getGeometryType(), |
217 |
getGeometrySubType(is3DFile)); |
218 |
line.setPoints(point1, point2); |
219 |
return line;
|
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 |
public int getGeometryType() { |
302 |
return Geometry.TYPES.CURVE;
|
303 |
} |
304 |
|
305 |
|
306 |
} |