root / trunk / libraries / libDwg / src / com / iver / cit / jdwglib / dwg / objects / DwgLine.java @ 10820
History | View | Annotate | Download (8.19 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.util.AcadExtrusionCalculator; |
51 |
import com.iver.cit.jdwglib.util.FMapUtil; |
52 |
|
53 |
/**
|
54 |
* The DwgLine class represents a DWG Line
|
55 |
*
|
56 |
* @author jmorell, azabala
|
57 |
*/
|
58 |
public class DwgLine extends DwgObject |
59 |
implements /*IDwgExtrusionable,*/ IDwg3DTestable, IDwg2FMap, IDwgBlockMember{ |
60 |
public DwgLine(int index) { |
61 |
super(index);
|
62 |
// TODO Auto-generated constructor stub
|
63 |
} |
64 |
private double[] p1; |
65 |
private double[] p2; |
66 |
private double thickness; |
67 |
private double[] extrusion; |
68 |
private boolean zflag = false; |
69 |
|
70 |
/**
|
71 |
* @return Returns the p1.
|
72 |
*/
|
73 |
public double[] getP1() { |
74 |
return p1;
|
75 |
} |
76 |
/**
|
77 |
* @param p1 The p1 to set.
|
78 |
*/
|
79 |
public void setP1(double[] p1) { |
80 |
this.p1 = p1;
|
81 |
} |
82 |
/**
|
83 |
* @return Returns the p2.
|
84 |
*/
|
85 |
public double[] getP2() { |
86 |
return p2;
|
87 |
} |
88 |
/**
|
89 |
* @param p2 The p2 to set.
|
90 |
*/
|
91 |
public void setP2(double[] p2) { |
92 |
this.p2 = p2;
|
93 |
} |
94 |
/**
|
95 |
* @return Returns the extrusion.
|
96 |
*/
|
97 |
public double[] getExtrusion() { |
98 |
return extrusion;
|
99 |
} |
100 |
/**
|
101 |
* @param extrusion The extrusion to set.
|
102 |
*/
|
103 |
public void setExtrusion(double[] extrusion) { |
104 |
this.extrusion = extrusion;
|
105 |
} |
106 |
/**
|
107 |
* @return Returns the thickness.
|
108 |
*/
|
109 |
public double getThickness() { |
110 |
return thickness;
|
111 |
} |
112 |
/**
|
113 |
* @param thickness The thickness to set.
|
114 |
*/
|
115 |
public void setThickness(double thickness) { |
116 |
this.thickness = thickness;
|
117 |
} |
118 |
/* (non-Javadoc)
|
119 |
* @see java.lang.Object#clone()
|
120 |
*/
|
121 |
// public Object clone() {
|
122 |
// DwgLine dwgLine = new DwgLine(index);
|
123 |
// dwgLine.setType(type);
|
124 |
// dwgLine.setHandle(handle);
|
125 |
// dwgLine.setVersion(version);
|
126 |
// dwgLine.setMode(mode);
|
127 |
// dwgLine.setLayerHandle(layerHandle);
|
128 |
// dwgLine.setColor(color);
|
129 |
// dwgLine.setNumReactors(numReactors);
|
130 |
// dwgLine.setNoLinks(noLinks);
|
131 |
// dwgLine.setLinetypeFlags(linetypeFlags);
|
132 |
// dwgLine.setPlotstyleFlags(plotstyleFlags);
|
133 |
// dwgLine.setSizeInBits(sizeInBits);
|
134 |
// dwgLine.setExtendedData(extendedData);
|
135 |
// dwgLine.setGraphicData(graphicData);
|
136 |
// //dwgLine.setInsideBlock(insideBlock);
|
137 |
// dwgLine.setP1(p1);
|
138 |
// dwgLine.setP2(p2);
|
139 |
// dwgLine.setThickness(thickness);
|
140 |
// dwgLine.setExtrusion(extrusion);
|
141 |
// dwgLine.setSubEntityHandle(subEntityHandle);
|
142 |
// return dwgLine;
|
143 |
// }
|
144 |
/**
|
145 |
* @return Returns the zflag.
|
146 |
*/
|
147 |
public boolean isZflag() { |
148 |
return zflag;
|
149 |
} |
150 |
/**
|
151 |
* @param zflag The zflag to set.
|
152 |
*/
|
153 |
public void setZflag(boolean zflag) { |
154 |
this.zflag = zflag;
|
155 |
} |
156 |
/* (non-Javadoc)
|
157 |
* @see com.iver.cit.jdwglib.dwg.IDwgExtrusionable#applyExtrussion()
|
158 |
*/
|
159 |
public void applyExtrussion() { |
160 |
double[] lineP1 = getP1(); |
161 |
double[] lineP2 = getP2(); |
162 |
boolean zflag = isZflag();
|
163 |
if (zflag) {
|
164 |
// elev = 0.0;
|
165 |
lineP1 = new double[]{lineP1[0], lineP1[1], 0.0}; |
166 |
lineP2 = new double[]{lineP2[0], lineP2[1], 0.0}; |
167 |
} |
168 |
double[] lineExt = getExtrusion(); |
169 |
lineP1 = AcadExtrusionCalculator.extrude2(lineP1, lineExt); |
170 |
lineP2 = AcadExtrusionCalculator.extrude2(lineP2, lineExt); |
171 |
setP1(lineP1); |
172 |
setP2(lineP2); |
173 |
} |
174 |
/* (non-Javadoc)
|
175 |
* @see com.iver.cit.jdwglib.dwg.IDwg3DTestable#has3DData()
|
176 |
*/
|
177 |
public boolean has3DData() { |
178 |
if (!isZflag()) {
|
179 |
double z1 = getP1()[2]; |
180 |
double z2 = getP2()[2]; |
181 |
if (z1!=0.0 || z2!=0.0) |
182 |
return true; |
183 |
}//TODO y si zflag vale true? REVISAR
|
184 |
return false; |
185 |
} |
186 |
public double getZ() { |
187 |
double[] p1 = getP1(); |
188 |
double[] p2 = getP2(); |
189 |
if(isZflag()){
|
190 |
if (p1[2] == p2[2]) |
191 |
return p1[2]; |
192 |
else
|
193 |
return 0d; |
194 |
} else {
|
195 |
return 0d; |
196 |
} |
197 |
} |
198 |
public IGeometry toFMapGeometry(boolean is3DFile) { |
199 |
FPolyline2D line = null;
|
200 |
double[] p1 = getP1(); |
201 |
double[] p2 = getP2(); |
202 |
if (is3DFile && isZflag()) {
|
203 |
List lin3D = new ArrayList(); |
204 |
lin3D.add(p1); |
205 |
lin3D.add(p2); |
206 |
line = FMapUtil.points3DToFPolyline3D(lin3D); |
207 |
} else if (is3DFile && ! isZflag()) { |
208 |
List lin3D = new ArrayList(); |
209 |
p1[2] = 0d; |
210 |
p2[2] = 0d; |
211 |
lin3D.add(p1); |
212 |
lin3D.add(p2); |
213 |
line = FMapUtil.points3DToFPolyline3D(lin3D); |
214 |
} else {
|
215 |
List lin = new ArrayList(); |
216 |
lin.add(p1); |
217 |
lin.add(p2); |
218 |
line = FMapUtil.points2DToFPolyline2D(lin); |
219 |
} |
220 |
return ShapeFactory.createGeometry(line);
|
221 |
|
222 |
} |
223 |
public String toFMapString(boolean is3DFile) { |
224 |
if(is3DFile){
|
225 |
return "FPolyline3D"; |
226 |
}else{
|
227 |
return "FPolyline2D"; |
228 |
} |
229 |
} |
230 |
|
231 |
public String toString(){ |
232 |
return "Line"; |
233 |
} |
234 |
public void transform2Block(double[] bPoint, Point2D insPoint, |
235 |
double[] scale, double rot, |
236 |
List dwgObjectsWithoutBlocks,
|
237 |
Map handleObjWithoutBlocks,
|
238 |
DwgFile callBack) { |
239 |
DwgLine transformedEntity = null;
|
240 |
double[] p1 = this.getP1(); |
241 |
if(p1.length < 3) |
242 |
{ |
243 |
double[] newp1 = new double[3]; |
244 |
System.arraycopy(p1, 0, newp1, 0, p1.length); |
245 |
p1 = newp1; |
246 |
setP1(newp1); |
247 |
} |
248 |
double[] p2 = this.getP2(); |
249 |
if(p2.length < 3) |
250 |
{ |
251 |
double[] newp2 = new double[3]; |
252 |
System.arraycopy(p2, 0, newp2, 0, p2.length); |
253 |
p2 = newp2; |
254 |
setP2(newp2); |
255 |
} |
256 |
Point2D pointAux = new Point2D.Double(p1[0] - bPoint[0], p1[1] - bPoint[1]); |
257 |
double laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot)); |
258 |
double laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot)); |
259 |
double[] transformedP1 = null; |
260 |
if (this.isZflag() ) { |
261 |
double laZ = p1[2] * scale[2]; |
262 |
transformedP1 = new double[]{laX, laY, laZ}; |
263 |
} else {
|
264 |
transformedP1 = new double[]{laX, laY, 0d}; |
265 |
} |
266 |
//double[] transformedP1 = new double[]{laX, laY};
|
267 |
pointAux = new Point2D.Double(p2[0] - bPoint[0], p2[1] - bPoint[1]); |
268 |
laX = insPoint.getX() + ((pointAux.getX()*scale[0])*Math.cos(rot) + (pointAux.getY()*scale[1])*(-1)*Math.sin(rot)); |
269 |
laY = insPoint.getY() + ((pointAux.getX()*scale[0])*Math.sin(rot) + (pointAux.getY()*scale[1])*Math.cos(rot)); |
270 |
double[] transformedP2 = null; |
271 |
if (this.isZflag() ) { |
272 |
double laZ = p2[2] * scale[2]; |
273 |
transformedP2 = new double[]{laX, laY, laZ}; |
274 |
} else {
|
275 |
transformedP2 = new double[]{laX, laY, 0d}; |
276 |
} |
277 |
//double[] transformedP2 = new double[]{laX, laY};
|
278 |
transformedEntity = (DwgLine)this.clone();
|
279 |
transformedEntity.setP1(transformedP1); |
280 |
transformedEntity.setP2(transformedP2); |
281 |
dwgObjectsWithoutBlocks.add(transformedEntity); |
282 |
handleObjWithoutBlocks.put(new Integer(transformedEntity.getHandle().getOffset()), transformedEntity); |
283 |
// dwgObjectsWithoutBlocks.add(this);
|
284 |
} |
285 |
public Object clone(){ |
286 |
DwgLine dwgLine = new DwgLine(index);
|
287 |
this.fill(dwgLine);
|
288 |
return dwgLine;
|
289 |
} |
290 |
|
291 |
protected void fill(DwgObject obj){ |
292 |
super.fill(obj);
|
293 |
DwgLine myObj = (DwgLine)obj; |
294 |
|
295 |
myObj.setExtrusion(extrusion); |
296 |
myObj.setP1(p1); |
297 |
myObj.setP2(p2); |
298 |
myObj.setThickness(thickness); |
299 |
|
300 |
} |
301 |
|
302 |
} |